unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#37726: [PATCH] Indent python multiline strings to start and previous levels
@ 2019-10-13  4:29 Carlos Pita
  2019-10-13  5:04 ` bug#37726: Carlos Pita
  0 siblings, 1 reply; 12+ messages in thread
From: Carlos Pita @ 2019-10-13  4:29 UTC (permalink / raw)
  To: 37726

[-- Attachment #1: Type: text/plain, Size: 746 bytes --]

This adds an additional indentation level that matches indentation of
previous line in a multiline string. Then Tab iterates between 0, the
start indentation level and the previous line level. This is useful
when writing docstrings in numpy/google formats that use indentation
to convey meaning.

For example:

0    4    8
     This is the first item
           This is a sub item
>   >    >

Then pressing Tab while in the third line will iterate between 0, 4
and 8, starting in 8, then 4, then 0. Compare with the current
implementation which will jump just between 0 and 4, which is not
convenient when the subitem is more than one line. A cursory
google/reddit search will show that this is a problem for some people.

Best regards
--
Carlos

[-- Attachment #2: 0001-Indent-python-multiline-strings-to-start-and-previou.patch --]
[-- Type: text/x-patch, Size: 2518 bytes --]

From 22803067f9117772f954aa5abec4487d234df2a5 Mon Sep 17 00:00:00 2001
From: memeplex <carlosjosepita@gmail.com>
Date: Sat, 12 Oct 2019 17:04:01 -0300
Subject: [PATCH] Indent python multiline strings to start and previous levels

* progmodes/python.el (python-indent--calculate-indentation): add an
additional indentation point to match indentation of previous line in
a multiline string. Then Tab iterates between 0, the start indentation
level and the previous line level. This is useful when writing
docstrings in numpy/google formats.
---
 lisp/progmodes/python.el | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index ae5aff3..f2a88f9 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1,3 +1,4 @@
+
 ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2003-2019 Free Software Foundation, Inc.
@@ -1069,11 +1070,20 @@ python-indent--calculate-indentation
         (`(:no-indent . ,_) (prog-first-column)) ; usually 0
         (`(,(or :after-line
                 :after-comment
-                :inside-string
                 :after-backslash) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
+        (`(,(or :inside-string
+                :inside-docstring) . ,start)
+         ;; Copy previous indentation inside string
+         (let ((prev (progn (forward-line -1)
+                            (current-indentation)))
+               (base (progn (goto-char start)
+                            (current-indentation))))
+           (if (/= prev base)
+               (sort (list 0 prev base) #'<)
+             base)))
         (`(,(or :inside-paren-at-closing-paren
                 :inside-paren-at-closing-nested-paren) . ,start)
          (goto-char (+ 1 start))
@@ -1082,12 +1092,6 @@ python-indent--calculate-indentation
              (current-indentation)
            ;; Align with opening paren.
            (current-column)))
-        (`(:inside-docstring . ,start)
-         (let* ((line-indentation (current-indentation))
-                (base-indent (progn
-                               (goto-char start)
-                               (current-indentation))))
-           (max line-indentation base-indent)))
         (`(,(or :after-block-start
                 :after-backslash-first-line
                 :after-backslash-assignment-continuation
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-13  4:29 bug#37726: [PATCH] Indent python multiline strings to start and previous levels Carlos Pita
@ 2019-10-13  5:04 ` Carlos Pita
  2019-10-13 18:35   ` bug#37726: Carlos Pita
  0 siblings, 1 reply; 12+ messages in thread
From: Carlos Pita @ 2019-10-13  5:04 UTC (permalink / raw)
  To: 37726

[-- Attachment #1: Type: text/plain, Size: 298 bytes --]

I've amended the previous patch so that it more conveniently align
string continuations to opening quotes, for example:

x = """aaaa
          bbbb
>   >     >

If you prefer I could also add an extra level:

x = """aaaa
          bbbb
>   >  >  >

in order to support other styles of indentation.

[-- Attachment #2: 0001-Indent-python-multiline-strings-to-start-and-previou.patch --]
[-- Type: text/x-patch, Size: 2469 bytes --]

From f6aaefe6d1bf023e9f8a189bc4a497dcdd7e0211 Mon Sep 17 00:00:00 2001
From: memeplex <carlosjosepita@gmail.com>
Date: Sat, 12 Oct 2019 17:04:01 -0300
Subject: [PATCH] Indent python multiline strings to start and previous levels

* progmodes/python.el (python-indent--calculate-indentation): add an
additional indentation point to match indentation of previous line in
a multiline string. Then Tab iterates between 0, the start indentation
level and the previous line level. This is useful when writing
docstrings in numpy/google formats.
---
 lisp/progmodes/python.el | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index ae5aff3..33837f5 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1,3 +1,4 @@
+
 ;;; python.el --- Python's flying circus support for Emacs -*- lexical-binding: t -*-
 
 ;; Copyright (C) 2003-2019 Free Software Foundation, Inc.
@@ -1069,11 +1070,18 @@ python-indent--calculate-indentation
         (`(:no-indent . ,_) (prog-first-column)) ; usually 0
         (`(,(or :after-line
                 :after-comment
-                :inside-string
                 :after-backslash) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
+        (`(,(or :inside-string
+                :inside-docstring) . ,start)
+         ;; Copy previous indentation inside string
+         (let ((prev (progn (forward-line -1)
+                            (current-indentation)))
+               (base (progn (goto-char start)
+                            (current-column))))
+           (sort (delete-dups (list 0 prev base)) #'<)))
         (`(,(or :inside-paren-at-closing-paren
                 :inside-paren-at-closing-nested-paren) . ,start)
          (goto-char (+ 1 start))
@@ -1082,12 +1090,6 @@ python-indent--calculate-indentation
              (current-indentation)
            ;; Align with opening paren.
            (current-column)))
-        (`(:inside-docstring . ,start)
-         (let* ((line-indentation (current-indentation))
-                (base-indent (progn
-                               (goto-char start)
-                               (current-indentation))))
-           (max line-indentation base-indent)))
         (`(,(or :after-block-start
                 :after-backslash-first-line
                 :after-backslash-assignment-continuation
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-13  5:04 ` bug#37726: Carlos Pita
@ 2019-10-13 18:35   ` Carlos Pita
  2019-10-13 19:06     ` bug#37726: Carlos Pita
  0 siblings, 1 reply; 12+ messages in thread
From: Carlos Pita @ 2019-10-13 18:35 UTC (permalink / raw)
  To: 37726

[-- Attachment #1: Type: text/plain, Size: 91 bytes --]

Here is a version of the patch rebased on top of emacs-26 (the other
was for master / 27).

[-- Attachment #2: 0001-Indent-python-multiline-strings-to-start-and-previou.patch --]
[-- Type: text/x-patch, Size: 2115 bytes --]

From 1890c032210a022b9a0b928e37707e71b074e135 Mon Sep 17 00:00:00 2001
From: memeplex <carlosjosepita@gmail.com>
Date: Sun, 13 Oct 2019 14:29:55 -0300
Subject: [PATCH] Indent python multiline strings to start and previous levels

* progmodes/python.el (python-indent--calculate-indentation): add an
additional indentation point to match indentation of previous line in
a multiline string. Then Tab iterates between 0, the start indentation
level and the previous line level. This is useful when writing
docstrings in numpy/google formats.
---
 lisp/progmodes/python.el | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 8e7d9f2..9432bc3 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1035,19 +1035,20 @@ possibilities can be narrowed to specific indentation points."
         (`(:no-indent . ,_) (prog-first-column)) ; usually 0
         (`(,(or :after-line
                 :after-comment
-                :inside-string
                 :after-backslash
                 :inside-paren-at-closing-paren
                 :inside-paren-at-closing-nested-paren) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
-        (`(:inside-docstring . ,start)
-         (let* ((line-indentation (current-indentation))
-                (base-indent (progn
-                               (goto-char start)
-                               (current-indentation))))
-           (max line-indentation base-indent)))
+        (`(,(or :inside-string
+                :inside-docstring) . ,start)
+         ;; Copy previous indentation inside string
+         (let ((prev (progn (forward-line -1)
+                            (current-indentation)))
+               (base (progn (goto-char start)
+                            (current-column))))
+           (sort (delete-dups (list 0 prev base)) #'<)))
         (`(,(or :after-block-start
                 :after-backslash-first-line
                 :after-backslash-assignment-continuation
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-13 18:35   ` bug#37726: Carlos Pita
@ 2019-10-13 19:06     ` Carlos Pita
  2019-10-19  8:32       ` bug#37726: Carlos Pita
  2019-10-23  0:34       ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
  0 siblings, 2 replies; 12+ messages in thread
From: Carlos Pita @ 2019-10-13 19:06 UTC (permalink / raw)
  To: 37726; +Cc: Noam Postavsky

Noam, I'm CC'ing you because I see you have been committing some stuff
related to docstrings in python.el recently.

Btw, I'm glad to see some renewed activity in python.el which had been
quite forgotten for many months. A time ago I proposed myself to
maintain that module and I would still be glad to do it if you want.





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-13 19:06     ` bug#37726: Carlos Pita
@ 2019-10-19  8:32       ` Carlos Pita
  2019-10-23  0:25         ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
                           ` (2 more replies)
  2019-10-23  0:34       ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
  1 sibling, 3 replies; 12+ messages in thread
From: Carlos Pita @ 2019-10-19  8:32 UTC (permalink / raw)
  To: 37726; +Cc: Noam Postavsky

[-- Attachment #1: Type: text/plain, Size: 90 bytes --]

This new patch:

* Removes accidentally introduced blank line.
* Improves commit message.

[-- Attachment #2: 0001-Indent-python-multiline-strings-to-start-and-previou.patch --]
[-- Type: text/x-patch, Size: 2257 bytes --]

From 7f43abe32bb553a20cf342ae9ab67becbf947fb2 Mon Sep 17 00:00:00 2001
From: memeplex <carlosjosepita@gmail.com>
Date: Sat, 12 Oct 2019 17:04:01 -0300
Subject: [PATCH] Indent python multiline strings to start and previous levels
 (Bug#37726)

* progmodes/python.el (python-indent--calculate-indentation): Add an
additional indentation point to match indentation of previous line in
a multiline string. Then Tab iterates between 0, the start indentation
level and the previous line level.
---
 lisp/progmodes/python.el | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 8f82353280..ef812f11c9 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1069,11 +1069,18 @@ python-indent--calculate-indentation
         (`(:no-indent . ,_) (prog-first-column)) ; usually 0
         (`(,(or :after-line
                 :after-comment
-                :inside-string
                 :after-backslash) . ,start)
          ;; Copy previous indentation.
          (goto-char start)
          (current-indentation))
+        (`(,(or :inside-string
+                :inside-docstring) . ,start)
+         ;; Copy previous indentation inside string
+         (let ((prev (progn (forward-line -1)
+                            (current-indentation)))
+               (base (progn (goto-char start)
+                            (current-column))))
+           (sort (delete-dups (list 0 prev base)) #'<)))
         (`(,(or :inside-paren-at-closing-paren
                 :inside-paren-at-closing-nested-paren) . ,start)
          (goto-char (+ 1 start))
@@ -1082,12 +1089,6 @@ python-indent--calculate-indentation
              (current-indentation)
            ;; Align with opening paren.
            (current-column)))
-        (`(:inside-docstring . ,start)
-         (let* ((line-indentation (current-indentation))
-                (base-indent (progn
-                               (goto-char start)
-                               (current-indentation))))
-           (max line-indentation base-indent)))
         (`(,(or :after-block-start
                 :after-backslash-first-line
                 :after-backslash-assignment-continuation
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* bug#37726: [PATCH] Indent python multiline strings to start and previous levels
  2019-10-19  8:32       ` bug#37726: Carlos Pita
@ 2019-10-23  0:25         ` Noam Postavsky
  2020-08-09 18:46         ` bug#37726: Lars Ingebrigtsen
  2020-08-09 21:54         ` bug#37726: Lars Ingebrigtsen
  2 siblings, 0 replies; 12+ messages in thread
From: Noam Postavsky @ 2019-10-23  0:25 UTC (permalink / raw)
  To: Carlos Pita; +Cc: 37726

severity 137726 wishlist
quit

To be honest, I don't write enough python to say whether this makes
sense or not, but it looks fairly reasonable.

Carlos Pita <carlosjosepita@gmail.com> writes:

> * progmodes/python.el (python-indent--calculate-indentation): Add an
> additional indentation point to match indentation of previous line in
> a multiline string. Then Tab iterates between 0, the start indentation
                     ^
                     Sentences should be double-spaced.

> level and the previous line level.





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726: [PATCH] Indent python multiline strings to start and previous levels
  2019-10-13 19:06     ` bug#37726: Carlos Pita
  2019-10-19  8:32       ` bug#37726: Carlos Pita
@ 2019-10-23  0:34       ` Noam Postavsky
  1 sibling, 0 replies; 12+ messages in thread
From: Noam Postavsky @ 2019-10-23  0:34 UTC (permalink / raw)
  To: Carlos Pita; +Cc: 37726

Carlos Pita <carlosjosepita@gmail.com> writes:
>
> Btw, I'm glad to see some renewed activity in python.el which had been
> quite forgotten for many months. A time ago I proposed myself to
> maintain that module and I would still be glad to do it if you want.

Yeah, I think it would be good to have someone in charge of it.  If you
have a Savannah account, you can ask for write access by making a
"Request for Inclusion" to the "emacs" group at
<https://savannah.gnu.org/my/groups.php>





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-19  8:32       ` bug#37726: Carlos Pita
  2019-10-23  0:25         ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
@ 2020-08-09 18:46         ` Lars Ingebrigtsen
  2020-08-09 21:44           ` bug#37726: Glenn Morris
  2020-08-09 21:54         ` bug#37726: Lars Ingebrigtsen
  2 siblings, 1 reply; 12+ messages in thread
From: Lars Ingebrigtsen @ 2020-08-09 18:46 UTC (permalink / raw)
  To: Carlos Pita; +Cc: Noam Postavsky, 37726

Carlos Pita <carlosjosepita@gmail.com> writes:

> * progmodes/python.el (python-indent--calculate-indentation): Add an
> additional indentation point to match indentation of previous line in
> a multiline string. Then Tab iterates between 0, the start indentation
> level and the previous line level.

This sounds reasonable to me, so I've applied it to Emacs 28.  I'm no
Python expert, though, so if there are any objections, feel free to
revert.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2020-08-09 18:46         ` bug#37726: Lars Ingebrigtsen
@ 2020-08-09 21:44           ` Glenn Morris
  2020-08-09 21:50             ` bug#37726: Lars Ingebrigtsen
  0 siblings, 1 reply; 12+ messages in thread
From: Glenn Morris @ 2020-08-09 21:44 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: Carlos Pita, Noam Postavsky, 37726


This causes test failures; ref eg

https://hydra.nixos.org/build/125002682





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2020-08-09 21:44           ` bug#37726: Glenn Morris
@ 2020-08-09 21:50             ` Lars Ingebrigtsen
  0 siblings, 0 replies; 12+ messages in thread
From: Lars Ingebrigtsen @ 2020-08-09 21:50 UTC (permalink / raw)
  To: Glenn Morris; +Cc: Carlos Pita, Noam Postavsky, 37726

Glenn Morris <rgm@gnu.org> writes:

> This causes test failures; ref eg
>
> https://hydra.nixos.org/build/125002682

I see the same thing here -- I'll revert the patch.  

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2019-10-19  8:32       ` bug#37726: Carlos Pita
  2019-10-23  0:25         ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
  2020-08-09 18:46         ` bug#37726: Lars Ingebrigtsen
@ 2020-08-09 21:54         ` Lars Ingebrigtsen
  2020-10-13  3:56           ` bug#37726: Lars Ingebrigtsen
  2 siblings, 1 reply; 12+ messages in thread
From: Lars Ingebrigtsen @ 2020-08-09 21:54 UTC (permalink / raw)
  To: Carlos Pita; +Cc: Noam Postavsky, 37726

Carlos Pita <carlosjosepita@gmail.com> writes:

> This new patch:
>
> * Removes accidentally introduced blank line.
> * Improves commit message.
>

If you have a string like:

multiline = '''
bunch
of
lines
'''

After the patch, and if you hit TAB at the start of the lines, you'll
end up with:

multiline = '''
            bunch
            of
            lines
            '''

But that's a different string, isn't it?  The leading spaces are part of
the string itself, which isn't what we want.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 12+ messages in thread

* bug#37726:
  2020-08-09 21:54         ` bug#37726: Lars Ingebrigtsen
@ 2020-10-13  3:56           ` Lars Ingebrigtsen
  0 siblings, 0 replies; 12+ messages in thread
From: Lars Ingebrigtsen @ 2020-10-13  3:56 UTC (permalink / raw)
  To: Carlos Pita; +Cc: Noam Postavsky, 37726

Lars Ingebrigtsen <larsi@gnus.org> writes:

> But that's a different string, isn't it?  The leading spaces are part of
> the string itself, which isn't what we want.

So this wishlist patch doesn't seem applicable, and I'm closing this bug
report.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2020-10-13  3:56 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-13  4:29 bug#37726: [PATCH] Indent python multiline strings to start and previous levels Carlos Pita
2019-10-13  5:04 ` bug#37726: Carlos Pita
2019-10-13 18:35   ` bug#37726: Carlos Pita
2019-10-13 19:06     ` bug#37726: Carlos Pita
2019-10-19  8:32       ` bug#37726: Carlos Pita
2019-10-23  0:25         ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky
2020-08-09 18:46         ` bug#37726: Lars Ingebrigtsen
2020-08-09 21:44           ` bug#37726: Glenn Morris
2020-08-09 21:50             ` bug#37726: Lars Ingebrigtsen
2020-08-09 21:54         ` bug#37726: Lars Ingebrigtsen
2020-10-13  3:56           ` bug#37726: Lars Ingebrigtsen
2019-10-23  0:34       ` bug#37726: [PATCH] Indent python multiline strings to start and previous levels Noam Postavsky

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).