(eq (char-before) last-command-event)))))
pos)))
-;; Electric indentation.
+;;; Electric indentation.
;; Autoloading variables is generally undesirable, but major modes
;; should usually set this variable by adding elements to the default
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.")
+(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 ()
;; 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
(unless (eq act 'do-indent) (nth 8 (syntax-ppss))))))))
;; For newline, we want to reindent both lines and basically behave like
;; 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)))
(save-excursion
- (unless (memq indent-line-function
- '(indent-relative indent-to-left-margin
- indent-relative-maybe))
+ (unless (or (memq indent-line-function
+ '(indent-relative indent-to-left-margin
+ indent-relative-maybe))
+ electric-indent-inhibit)
;; Don't reindent the previous line if the indentation function
;; is not a real one.
(goto-char before)
;; Remove the trailing whitespace after indentation because
;; indentation may (re)introduce the whitespace.
(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)))))
;;;###autoload
(delq #'electric-indent-post-self-insert-function
(cdr bp))))))))
-;; Electric pairing.
+;;; Electric pairing.
(defcustom electric-pair-pairs
'((?\" . ?\"))
(remove-hook 'self-insert-uses-region-functions
#'electric-pair-will-use-region)))
-;; Automatically add newlines after/before/around some chars.
+;;; Electric newlines after/before/around some chars.
(defvar electric-layout-rules '()
"List of rules saying where to automatically insert newlines.
(reverse acc))))
\f
+(defvar electric-indent-inhibit)
+
;;;###autoload
(define-derived-mode python-mode prog-mode "Python"
"Major mode for editing Python files.
(set (make-local-variable 'indent-line-function)
#'python-indent-line-function)
(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 'fill-paragraph-function)
'python-fill-paragraph)