summaryrefslogtreecommitdiff
path: root/lisp/init-python.el
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2015-04-12 00:43:13 -0400
committerKyle Meyer <kyle@kyleam.com>2015-04-12 00:43:13 -0400
commitb610d2f78ea980ab18a83c7dde800dcaabd7fd3b (patch)
tree9132067a1aeb77f2079fce0f6ebcfe5a034af3e0 /lisp/init-python.el
parent7c641944ce85db15fe69800f511fb3f262fb1b09 (diff)
downloademacs.d-b610d2f78ea980ab18a83c7dde800dcaabd7fd3b.tar.gz
Add km/pyton-indent-post-self-insert-function
Diffstat (limited to 'lisp/init-python.el')
-rw-r--r--lisp/init-python.el30
1 files changed, 30 insertions, 0 deletions
diff --git a/lisp/init-python.el b/lisp/init-python.el
index 2eb4325..5842ed6 100644
--- a/lisp/init-python.el
+++ b/lisp/init-python.el
@@ -10,6 +10,11 @@
(add-hook 'python-mode-hook 'jedi:setup)
(add-hook 'python-mode-hook 'auto-complete-mode)
+(add-hook 'python-mode-hook
+ (lambda ()
+ (add-hook
+ 'post-self-insert-hook
+ #'km/python-indent-post-self-insert-function 'append 'local)))
(defun km/python-hook ()
(set (make-local-variable 'yas-fallback-behavior)
@@ -96,6 +101,31 @@ This is inspired by `ess-eval-function-or-paragraph-and-step'."
(goto-char pos)
n))
+(defun km/python-indent-post-self-insert-function ()
+ "Adjust indentation after insert of specfic characters.
+This is taken from `python-indent-post-self-insert-function'.
+Unlike that function, it does not rely on `electric-indent-mode'
+being turned on."
+ (when (and (not (bolp))
+ (let ((paren-start (python-syntax-context 'paren)))
+ ;; Check that point is inside parens.
+ (when paren-start
+ (not
+ ;; Filter the case where input is happening in the same
+ ;; line where the open paren is.
+ (= (line-number-at-pos)
+ (line-number-at-pos paren-start)))))
+ ;; When content has been added before the closing paren or a
+ ;; comma has been inserted, it's ok to do the trick.
+ (or
+ (memq (char-after) '(?\) ?\] ?\}))
+ (eq (char-before) ?,)))
+ (save-excursion
+ (goto-char (line-beginning-position))
+ (let ((indentation (python-indent-calculate-indentation)))
+ (when (< (current-indentation) indentation)
+ (indent-line-to indentation))))))
+
(after 'python
(define-key python-mode-map (kbd "C-c C-.")
'km/python-shell-send-buffer-up-to-line)