bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#29305: 26.0.90; Wrong electrified indentation with Python multiline


From: Noam Postavsky
Subject: bug#29305: 26.0.90; Wrong electrified indentation with Python multiline string
Date: Tue, 21 Nov 2017 08:52:31 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (gnu/linux)

Lele Gaifax <lele@metapensiero.it> writes:

> where I'm going to add a comma *after* the multiline string: when I tried to
> find a solution, I thought I'd need to consider the state *at
> (beginning-of-line)*, in other words, morphing my experiment on top of
> your change to something like:
>
>     (when (and electric-indent-mode
>                (eq (char-before) last-command-event)
>                (not (python-syntax-context 'string))
>                (save-excursion
>                  (beginning-of-line)
>                  (not (eq (car (python-indent-context)) :inside-string))))
>
> but your simpler code tells that it is not needed...

Well, there is no big mystery I think.  It's simply that
`python-indent-context' goes to the beginning of line itself:

    (defun python-indent-context ()
      ...
      (save-restriction
        (prog-widen)
        (let ((ppss (save-excursion
                      (beginning-of-line)
                      (syntax-ppss))))

Actually, I forgot to check for an indent context of :inside-docstring
though.  I'm not sure if there is valid python code which could trigger
electric indent next to a docstring, but it's probably more correct to
do this instead:

>From a965850b20109218713583743873952a395b7e1c Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs@gmail.com>
Date: Sun, 19 Nov 2017 09:00:43 -0500
Subject: [PATCH v2] Disable eletric indent for python strings (Bug#29305)

* lisp/progmodes/python.el (python-indent-post-self-insert-function):
Do nothing when point or beginning of line is in string.
---
 lisp/progmodes/python.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d4226e5ce7..9e09bfc594 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1257,7 +1257,11 @@ python-indent-post-self-insert-function
 If a line renders a paren alone, after adding a char before it,
 the line will be re-indented automatically if needed."
   (when (and electric-indent-mode
-             (eq (char-before) last-command-event))
+             (eq (char-before) last-command-event)
+             (not (python-syntax-context 'string))
+             (save-excursion
+               (beginning-of-line)
+               (not (python-syntax-context 'string (syntax-ppss)))))
     (cond
      ;; Electric indent inside parens
      ((and
-- 
2.11.0


reply via email to

[Prev in Thread] Current Thread [Next in Thread]