From dc4f2e532630fa58381b9d3e52350279c98e89b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Thu, 17 May 2012 00:03:36 -0300 Subject: [PATCH] Enhancements on indentation for lines after a backslash continuation. Backslashed continuations with parens in the middle are now handled correctly. Things like this are now indented properly: objects = Thing.objects.all() \ .filter( type="toy", status="bought" subtype="car" ) \ .aggregate( Sum('amount') ) \ .values_list() New Functions: * `python-info-beginning-of-backlash' returns the point where a backslashed line start. --- lisp/progmodes/python.el | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 5527d851d1d..ff790bdc25c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -701,7 +701,12 @@ START is the buffer position where the sexp starts." ((save-excursion (back-to-indentation) (when (looking-at "\\.") - (forward-line -1) + ;; If after moving one line back point is inside a paren it + ;; needs to move back until it's not anymore + (while (prog2 + (forward-line -1) + (and (not (bobp)) + (python-info-ppss-context 'paren)))) (goto-char (line-end-position)) (while (and (re-search-backward "\\." (line-beginning-position) t) @@ -742,10 +747,12 @@ START is the buffer position where the sexp starts." (current-column)))) (t (forward-line -1) + (goto-char (python-info-beginning-of-backlash)) (if (save-excursion (and - (python-info-line-ends-backslash-p) (forward-line -1) + (goto-char + (or (python-info-beginning-of-backlash) (point))) (python-info-line-ends-backslash-p))) ;; The two previous lines ended in a backslash so we must ;; respect previous line indentation. @@ -2535,11 +2542,31 @@ not inside a defun." With optional argument LINE-NUMBER, check that line instead." (save-excursion (save-restriction + (widen) (when line-number (goto-char line-number)) + (while (and (not (eobp)) + (goto-char (line-end-position)) + (python-info-ppss-context 'paren) + (not (equal (char-before (point)) ?\\))) + (forward-line 1)) + (when (equal (char-before) ?\\) + (point-marker))))) + +(defun python-info-beginning-of-backlash (&optional line-number) + "Return the point where the backlashed line starts." + (save-excursion + (save-restriction (widen) - (goto-char (line-end-position)) - (equal (char-after (1- (point))) ?\\)))) + (when line-number + (goto-char line-number)) + (when (python-info-line-ends-backslash-p) + (while (save-excursion + (goto-char (line-beginning-position)) + (python-info-ppss-context 'paren)) + (forward-line -1)) + (back-to-indentation) + (point-marker))))) (defun python-info-continuation-line-p () "Check if current line is continuation of another. -- 2.39.2