* lisp/electric.el (electric-indent-local-mode): New minor mode.
[bpt/emacs.git] / lisp / electric.el
index 1f95e68..9a89587 100644 (file)
@@ -207,6 +207,15 @@ point right after that char, and it should return t to cause indentation,
 This should be set by major modes such as `python-mode' since
 Python does not lend itself to fully automatic indentation.")
 
+(defvar electric-indent-functions-without-reindent
+  '(indent-relative indent-to-left-margin indent-relative-maybe
+    py-indent-line coffee-indent-line org-indent-line
+    haskell-indentation-indent-line haskell-indent-cycle haskell-simple-indent)
+  "List of indent functions that can't reindent.
+If `line-indent-function' is one of those, then `electric-indent-mode' will
+not try to reindent lines.  It is normally better to make the major
+mode set `electric-indent-inhibit', but this can be used as a workaround.")
+
 (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
@@ -238,8 +247,7 @@ Python does not lend itself to fully automatic indentation.")
         (let ((before (copy-marker (1- pos) t)))
           (save-excursion
             (unless (or (memq indent-line-function
-                              '(indent-relative indent-to-left-margin
-                                                indent-relative-maybe))
+                              electric-indent-functions-without-reindent)
                         electric-indent-inhibit)
               ;; Don't reindent the previous line if the indentation function
               ;; is not a real one.
@@ -255,9 +263,8 @@ Python does not lend itself to fully automatic indentation.")
              ;; Remove the trailing whitespace after indentation because
              ;; indentation may (re)introduce the whitespace.
              (delete-horizontal-space t)))))
-      (unless (or (memq indent-line-function '(indent-to-left-margin))
-                  (and electric-indent-inhibit
-                       (> pos (line-beginning-position))))
+      (unless (and electric-indent-inhibit
+                   (> pos (line-beginning-position)))
         (indent-according-to-mode)))))
 
 ;;;###autoload
@@ -289,6 +296,19 @@ insert a character from `electric-indent-chars'."
                          (delq #'electric-indent-post-self-insert-function
                                (cdr bp))))))))
 
+;;;###autoload
+(define-minor-mode electric-indent-local-mode
+  "Toggle `electric-indent-mode' only in this buffer."
+  :variable (buffer-local-value 'electric-indent-mode (current-buffer))
+  (cond
+   ((eq electric-indent-mode (default-value 'electric-indent-mode))
+    (kill-local-variable 'electric-indent-mode))
+   ((not (default-value 'electric-indent-mode))
+    ;; Locally enabled, but globally disabled.
+    (electric-indent-mode 1)                ; Setup the hooks.
+    (setq-default electric-indent-mode nil) ; But keep it globally disabled.
+    )))
+
 ;;; Electric pairing.
 
 (defcustom electric-pair-pairs