+2013-11-06 Stefan Monnier <monnier@iro.umontreal.ca>
+
+ * electric.el (electric-indent-local-mode): New minor mode.
+ (electric-indent-functions-without-reindent): New var.
+ (electric-indent-post-self-insert-function): Use it.
+ * emacs-lisp/gv.el (buffer-local-value): Add setter.
+
2013-11-05 Eli Zaretskii <eliz@gnu.org>
* international/quail.el (quail-help): Be more explicit about the
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
(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.
;; 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
(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
(gv-define-simple-setter window-point set-window-point)
(gv-define-simple-setter window-start set-window-start)
+(gv-define-setter buffer-local-value (val var buf)
+ (macroexp-let2 nil v val
+ `(with-current-buffer ,buf (set (make-local-variable ,var) ,v))))
+
;;; Some occasionally handy extensions.
;; While several of the "places" below are not terribly useful for direct use,