* lisp/electric.el (electric-indent-inhibit): New var.

(electric-indent-post-self-insert-function): Use it.
* lisp/progmodes/python.el (python-mode): Set it.
This commit is contained in:
Stefan Monnier 2013-10-07 14:51:26 -04:00
parent d2e0e79548
commit 2abb4e6560
3 changed files with 25 additions and 9 deletions

View file

@ -1,5 +1,9 @@
2013-10-07 Stefan Monnier <monnier@iro.umontreal.ca> 2013-10-07 Stefan Monnier <monnier@iro.umontreal.ca>
* electric.el (electric-indent-inhibit): New var.
(electric-indent-post-self-insert-function): Use it.
* progmodes/python.el (python-mode): Set it.
* progmodes/ruby-mode.el (ruby-smie-rules): Tweak handling of * progmodes/ruby-mode.el (ruby-smie-rules): Tweak handling of
open braces. open braces.

View file

@ -187,7 +187,7 @@ Returns nil when we can't find this char."
(eq (char-before) last-command-event))))) (eq (char-before) last-command-event)))))
pos))) pos)))
;; Electric indentation. ;;; Electric indentation.
;; Autoloading variables is generally undesirable, but major modes ;; Autoloading variables is generally undesirable, but major modes
;; should usually set this variable by adding elements to the default ;; should usually set this variable by adding elements to the default
@ -202,6 +202,11 @@ Each function is called with one argument (the inserted char), with
point right after that char, and it should return t to cause indentation, point right after that char, and it should return t to cause indentation,
`no-indent' to prevent indentation or nil to let other functions decide.") `no-indent' to prevent indentation or nil to let other functions decide.")
(defvar-local electric-indent-inhibit nil
"If non-nil, reindentation is not appropriate for this buffer.
This should be set by major modes such as `python-mode' since
Python does not lend itself to fully automatic indentation.")
(defun electric-indent-post-self-insert-function () (defun electric-indent-post-self-insert-function ()
;; FIXME: This reindents the current line, but what we really want instead is ;; FIXME: This reindents the current line, but what we really want instead is
;; to reindent the whole affected text. That's the current line for simple ;; to reindent the whole affected text. That's the current line for simple
@ -229,12 +234,13 @@ point right after that char, and it should return t to cause indentation,
(unless (eq act 'do-indent) (nth 8 (syntax-ppss)))))))) (unless (eq act 'do-indent) (nth 8 (syntax-ppss))))))))
;; For newline, we want to reindent both lines and basically behave like ;; For newline, we want to reindent both lines and basically behave like
;; reindent-then-newline-and-indent (whose code we hence copied). ;; reindent-then-newline-and-indent (whose code we hence copied).
(when (< (1- pos) (line-beginning-position)) (when (<= pos (line-beginning-position))
(let ((before (copy-marker (1- pos) t))) (let ((before (copy-marker (1- pos) t)))
(save-excursion (save-excursion
(unless (memq indent-line-function (unless (or (memq indent-line-function
'(indent-relative indent-to-left-margin '(indent-relative indent-to-left-margin
indent-relative-maybe)) indent-relative-maybe))
electric-indent-inhibit)
;; Don't reindent the previous line if the indentation function ;; Don't reindent the previous line if the indentation function
;; is not a real one. ;; is not a real one.
(goto-char before) (goto-char before)
@ -248,7 +254,9 @@ point right after that char, and it should return t to cause indentation,
;; Remove the trailing whitespace after indentation because ;; Remove the trailing whitespace after indentation because
;; indentation may (re)introduce the whitespace. ;; indentation may (re)introduce the whitespace.
(delete-horizontal-space t)))) (delete-horizontal-space t))))
(unless (memq indent-line-function '(indent-to-left-margin)) (unless (or (memq indent-line-function '(indent-to-left-margin))
(and electric-indent-inhibit
(> pos (line-beginning-position))))
(indent-according-to-mode))))) (indent-according-to-mode)))))
;;;###autoload ;;;###autoload
@ -281,7 +289,7 @@ insert a character from `electric-indent-chars'."
(delq #'electric-indent-post-self-insert-function (delq #'electric-indent-post-self-insert-function
(cdr bp)))))))) (cdr bp))))))))
;; Electric pairing. ;;; Electric pairing.
(defcustom electric-pair-pairs (defcustom electric-pair-pairs
'((?\" . ?\")) '((?\" . ?\"))
@ -414,7 +422,7 @@ See options `electric-pair-pairs' and `electric-pair-skip-self'."
(remove-hook 'self-insert-uses-region-functions (remove-hook 'self-insert-uses-region-functions
#'electric-pair-will-use-region))) #'electric-pair-will-use-region)))
;; Automatically add newlines after/before/around some chars. ;;; Electric newlines after/before/around some chars.
(defvar electric-layout-rules '() (defvar electric-layout-rules '()
"List of rules saying where to automatically insert newlines. "List of rules saying where to automatically insert newlines.

View file

@ -3544,6 +3544,8 @@ list is returned as is."
(reverse acc)))) (reverse acc))))
(defvar electric-indent-inhibit)
;;;###autoload ;;;###autoload
(define-derived-mode python-mode prog-mode "Python" (define-derived-mode python-mode prog-mode "Python"
"Major mode for editing Python files. "Major mode for editing Python files.
@ -3572,7 +3574,9 @@ if that value is non-nil."
(set (make-local-variable 'indent-line-function) (set (make-local-variable 'indent-line-function)
#'python-indent-line-function) #'python-indent-line-function)
(set (make-local-variable 'indent-region-function) #'python-indent-region) (set (make-local-variable 'indent-region-function) #'python-indent-region)
;; Because indentation is not redundant, we cannot safely reindent code.
(setq-local electric-indent-inhibit t)
(set (make-local-variable 'paragraph-start) "\\s-*$") (set (make-local-variable 'paragraph-start) "\\s-*$")
(set (make-local-variable 'fill-paragraph-function) (set (make-local-variable 'fill-paragraph-function)
'python-fill-paragraph) 'python-fill-paragraph)