]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/aggressive-indent/aggressive-indent.el
Merge commit '3557254c6873aeb0b5070248aa0faf2ff0d29d04'
[gnu-emacs-elpa] / packages / aggressive-indent / aggressive-indent.el
index 7321fd5074a33eee4a4aa6caea92007878d61ee0..d6d201dbaa5850abd2014fc0d3da6fec2cd9ad9b 100644 (file)
@@ -4,7 +4,7 @@
 
 ;; Author: Artur Malabarba <emacs@endlessparentheses.com>
 ;; URL: http://github.com/Malabarba/aggressive-indent-mode
-;; Version: 1.0.1
+;; Version: 1.1.2
 ;; Package-Requires: ((emacs "24.1") (names "20150125.9") (cl-lib "0.5"))
 ;; Keywords: indent lisp maint tools
 ;; Prefix: aggressive-indent
@@ -18,7 +18,7 @@
 ;; to go wrong.
 ;;
 ;; `aggressive-indent-mode' is a minor mode that keeps your code always
-;; indented.  It reindents after every command, making it more reliable
+;; indented.  It reindents after every change, making it more reliable
 ;; than `electric-indent-mode'.
 ;;
 ;; ### Instructions ###
@@ -171,9 +171,18 @@ commands will NOT be followed by a re-indent."
   '((memq this-command aggressive-indent-protected-commands)
     (region-active-p)
     buffer-read-only
+    undo-in-progress
     (null (buffer-modified-p))
     (and (boundp 'smerge-mode) smerge-mode)
-    (string-match "\\`[[:blank:]]*\n?\\'" (or (thing-at-point 'line) ""))
+    (let ((line (thing-at-point 'line)))
+      (when (stringp line)
+        (or (string-match "\\`[[:blank:]]*\n?\\'" line)
+            ;; If the user is starting to type a comment.
+            (and (stringp comment-start)
+                 (string-match (concat "\\`[[:blank:]]*"
+                                       (substring comment-start 0 1)
+                                       "[[:blank:]]*$")
+                               line)))))
     (let ((sp (syntax-ppss)))
       ;; Comments.
       (or (and (not aggressive-indent-comments-too) (elt sp 4))
@@ -213,6 +222,15 @@ change."
   '(when (boundp 'ac-completing)
      (add-to-list 'aggressive-indent--internal-dont-indent-if
                   'ac-completing)))
+(eval-after-load 'iedit
+  '(when (boundp 'iedit-mode)
+     (add-to-list 'aggressive-indent--internal-dont-indent-if
+                  'iedit-mode)))
+(eval-after-load 'coq
+  '(add-to-list 'aggressive-indent--internal-dont-indent-if
+                '(and (derived-mode-p 'coq-mode)
+                      (not (string-match "\\.[[:space:]]*$"
+                                         (thing-at-point 'line))))))
 
 (defcustom dont-indent-if '()
   "List of variables and functions to prevent aggressive indenting.
@@ -245,28 +263,34 @@ erroring again."
                   (setq -has-errored t)
                   (message -error-message er))))))
 
+\f
 :autoload
-(defun indent-defun ()
+(defun indent-defun (&optional l r)
   "Indent current defun.
-Throw an error if parentheses are unbalanced."
+Throw an error if parentheses are unbalanced.
+If L and R are provided, use them for finding the start and end of defun."
   (interactive)
   (let ((p (point-marker)))
     (set-marker-insertion-type p t)
     (indent-region
-     (save-excursion (beginning-of-defun 1) (point))
-     (save-excursion (end-of-defun 1) (point)))
+     (save-excursion
+       (when l (goto-char l))
+       (beginning-of-defun 1) (point))
+     (save-excursion
+       (when r (goto-char r))
+       (end-of-defun 1) (point)))
     (goto-char p)))
 
-(defun -softly-indent-defun ()
+(defun -softly-indent-defun (&optional l r)
   "Indent current defun unobstrusively.
 Like `aggressive-indent-indent-defun', but without errors or
-messages."
+messages.  L and R passed to `aggressive-indent-indent-defun'."
   (unless (or (run-hook-wrapped
                'aggressive-indent--internal-dont-indent-if
                #'eval)
               (aggressive-indent--run-user-hooks))
     (cl-letf (((symbol-function 'message) #'ignore))
-      (ignore-errors (indent-defun)))))
+      (ignore-errors (indent-defun l r)))))
 
 :autoload
 (defun indent-region-and-on (l r)
@@ -294,12 +318,19 @@ until nothing more happens."
             (indent-according-to-mode))
           ;; And then we indent each following line until nothing happens.
           (forward-line 1)
-          (while (and (null (eobp))
-                      (/= (progn (skip-chars-forward "[:blank:]\n")
-                                 (point))
-                          (progn (indent-according-to-mode)
-                                 (point))))
-            (forward-line 1)))
+          (skip-chars-forward "[:blank:]\n")
+          (let* ((eod (ignore-errors
+                        (save-excursion (end-of-defun)
+                                        (point-marker))))
+                 (point-limit (if (and eod (< (point) eod))
+                                  eod (point-max-marker))))
+            (while (and (null (eobp))
+                        (< (point) point-limit)
+                        (/= (point)
+                            (progn (indent-according-to-mode)
+                                   (point))))
+              (forward-line 1)
+              (skip-chars-forward "[:blank:]\n"))))
       (goto-char p))))
 
 (defun -softly-indent-region-and-on (l r &rest _)
@@ -313,26 +344,26 @@ or messages."
     (cl-letf (((symbol-function 'message) #'ignore))
       (ignore-errors (indent-region-and-on l r)))))
 
-(defvar -changed-list-right nil
-  "List of right limit of regions changed in the last command loop.")
-
-(defvar -changed-list-left nil
-  "List of left limit of regions changed in the last command loop.")
+(defvar -changed-list nil
+  "List of (left right) limit of regions changed in the last command loop.")
 
 (defun -indent-if-changed ()
   "Indent any region that changed in the last command loop."
-  (let ((inhibit-modification-hooks t))
-    (when -changed-list-left
-      (-softly-indent-region-and-on
-       (apply #'min -changed-list-left)
-       (apply #'max -changed-list-right))
-      (setq -changed-list-left nil
-            -changed-list-right nil))))
+  (when -changed-list
+    (while-no-input
+      (let ((inhibit-modification-hooks t)
+            (inhibit-point-motion-hooks t)
+            (indent-function
+             (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
+                 #'-softly-indent-defun
+               #'-softly-indent-region-and-on)))
+        (while -changed-list
+          (apply indent-function (car -changed-list))
+          (setq -changed-list (cdr -changed-list)))))))
 
 (defun -keep-track-of-changes (l r &rest _)
   "Store the limits (L and R) of each change in the buffer."
-  (push l -changed-list-left)
-  (push r -changed-list-right))
+  (push (list l r) -changed-list))
 
 \f
 ;;; Minor modes
@@ -361,10 +392,9 @@ or messages."
                 (cl-member-if #'derived-mode-p dont-electric-modes))
             (-local-electric nil)
           (-local-electric t))
-        (if (cl-member-if #'derived-mode-p modes-to-prefer-defun)
-            (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
-          (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
-          (add-hook 'post-command-hook #'-indent-if-changed nil 'local)))
+        (add-hook 'after-change-functions #'-keep-track-of-changes nil 'local)
+        ;; (add-hook 'post-command-hook #'-softly-indent-defun nil 'local)
+        (add-hook 'post-command-hook #'-indent-if-changed nil 'local))
     ;; Clean the hooks
     (remove-hook 'after-change-functions #'-keep-track-of-changes 'local)
     (remove-hook 'post-command-hook #'-indent-if-changed 'local)