Commit 6f5e57e7 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/electric.el: Streamline electric-indent's hook.

(electric-indent-chars): Revert to simple list.
(electric-indent-functions): New var.
(electric-indent-post-self-insert-function): Use it.
parent 5c3fe83f
2011-12-03 Stefan Monnier <monnier@iro.umontreal.ca> 2011-12-03 Stefan Monnier <monnier@iro.umontreal.ca>
* electric.el: Streamline electric-indent's hook.
(electric-indent-chars): Revert to simple list.
(electric-indent-functions): New var.
(electric-indent-post-self-insert-function): Use it.
* progmodes/prolog.el (prolog-find-value-by-system): Avoid error when * progmodes/prolog.el (prolog-find-value-by-system): Avoid error when
there's no inferior buffer (bug#10196). there's no inferior buffer (bug#10196).
(prolog-consult-compile): Don't use toggle-read-only. (prolog-consult-compile): Don't use toggle-read-only.
......
...@@ -197,11 +197,13 @@ Returns nil when we can't find this char." ...@@ -197,11 +197,13 @@ Returns nil when we can't find this char."
;; value, which only works well if the variable is preloaded. ;; value, which only works well if the variable is preloaded.
;;;###autoload ;;;###autoload
(defvar electric-indent-chars '(?\n) (defvar electric-indent-chars '(?\n)
"Characters that should cause automatic reindentation. "Characters that should cause automatic reindentation.")
Each entry of the list can be either a character or a cons of the
form (CHAR . PREDICATE) which means that CHAR should cause reindentation (defvar electric-indent-functions nil
only if PREDICATE returns non-nil. PREDICATE is called with no arguments "Special hook run to decide whether to auto-indent.
and with point before the inserted char.") Each function is called with one argument (the inserted char), with
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.")
(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
...@@ -212,18 +214,21 @@ and with point before the inserted char.") ...@@ -212,18 +214,21 @@ and with point before the inserted char.")
;; There might be a way to get it working by analyzing buffer-undo-list, but ;; There might be a way to get it working by analyzing buffer-undo-list, but
;; it looks challenging. ;; it looks challenging.
(let (pos) (let (pos)
(when (and (or (memq last-command-event electric-indent-chars) (when (and
(let ((cp (assq last-command-event electric-indent-chars))) ;; Don't reindent while inserting spaces at beginning of line.
(and cp (setq pos (electric--after-char-pos)) (or (not (memq last-command-event '(?\s ?\t)))
(save-excursion (save-excursion (skip-chars-backward " \t") (not (bolp))))
(goto-char (1- pos)) (setq pos (electric--after-char-pos))
(funcall (cdr cp)))))) (save-excursion
;; Don't reindent while inserting spaces at beginning of line. (goto-char pos)
(or (not (memq last-command-event '(?\s ?\t))) (let ((act (or (run-hook-with-args-until-success
(save-excursion (skip-chars-backward " \t") (not (bolp)))) 'electric-indent-functions
(setq pos (electric--after-char-pos)) last-command-event)
;; Not in a string or comment. (memq last-command-event electric-indent-chars))))
(not (nth 8 (save-excursion (syntax-ppss pos))))) (not
(or (memq act '(nil no-indent))
;; In a string or comment.
(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 (< (1- pos) (line-beginning-position))
...@@ -231,7 +236,7 @@ and with point before the inserted char.") ...@@ -231,7 +236,7 @@ and with point before the inserted char.")
(save-excursion (save-excursion
(unless (memq indent-line-function (unless (memq indent-line-function
'(indent-relative indent-to-left-margin '(indent-relative indent-to-left-margin
indent-relative-maybe)) indent-relative-maybe))
;; 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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment