summaryrefslogtreecommitdiff
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina <fgallina@cuca>2012-05-17 00:03:42 -0300
committerFabián Ezequiel Gallina <fgallina@gnu.org>2012-05-17 00:03:42 -0300
commit5eae76aed18a165854f55911ab8a5ac2bd83feed (patch)
tree0ccf6e287858ada92063403163860e135e2633ce /lisp/progmodes/python.el
parent90a41b9dc2f64c4cd087ca4651dbf6066617fb4f (diff)
Readjust indentation automatically on lines with closing parens.
For this to work `python-indent-post-self-insert-function' is added to the `post-self-insert-hook.' when python-mode is initialized. New functions: + `python-indent-post-self-insert-function'
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el24
1 files changed, 24 insertions, 0 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 3eed67ab3a4..47e6fc0380d 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -990,6 +990,27 @@ With numeric ARG, just insert that many colons. With
(indent-line-to indentation)))))))
(put 'python-indent-electric-colon 'delete-selection t)
+(defun python-indent-post-self-insert-function ()
+ "Adjust closing paren line indentation after a char is added.
+This function is intended to be added to the
+`post-self-insert-hook.' If a line renders a paren alone, after
+adding a char before it, the line will be re-indented
+automatically if needed."
+ (when (and (eq (char-before) last-command-event)
+ (= (current-indentation) 0)
+ (not (bolp))
+ (memq (char-after) '(?\) ?\] ?\})))
+ (let ((indentation (save-excursion
+ ;; If after going to the beginning of line the point
+ ;; is still inside a paren it's ok to do the trick
+ (goto-char (line-beginning-position))
+ (when (python-info-ppss-context 'paren)
+ (python-indent-calculate-indentation)))))
+ (when (and indentation
+ (< (current-indentation) indentation))
+ (save-excursion
+ (indent-line-to indentation))))))
+
;;; Navigation
@@ -2778,6 +2799,9 @@ if that value is non-nil."
(add-hook 'completion-at-point-functions
'python-completion-complete-at-point nil 'local)
+ (add-hook 'post-self-insert-hook
+ 'python-indent-post-self-insert-function nil 'local)
+
(setq imenu-create-index-function #'python-imenu-create-index)
(set (make-local-variable 'add-log-current-defun-function)