branch: master
commit 64ae275d9bb200f199d2d5d9785001249e2def15
Author: Artur Malabarba <bruce.connor...@gmail.com>
Commit: Artur Malabarba <bruce.connor...@gmail.com>

    Fully reindent before saving
---
 aggressive-indent.el |   29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

diff --git a/aggressive-indent.el b/aggressive-indent.el
index 685f2af..5ee9e35 100644
--- a/aggressive-indent.el
+++ b/aggressive-indent.el
@@ -352,6 +352,23 @@ or messages."
 (defvar-local aggressive-indent--balanced-parens t
   "Non-nil if the current-buffer has balanced parens.")
 
+(defun aggressive-indent--proccess-changed-list-and-indent ()
+  "Indent the regions in `aggressive-indent--changed-list'."
+  (let ((inhibit-modification-hooks t)
+        (inhibit-point-motion-hooks t)
+        (indent-function
+         (if (cl-member-if #'derived-mode-p 
aggressive-indent-modes-to-prefer-defun)
+             #'aggressive-indent--softly-indent-defun 
#'aggressive-indent--softly-indent-region-and-on)))
+    ;; Take the 10 most recent changes.
+    (let ((cell (last aggressive-indent--changed-list 10)))
+      (when cell (setcdr cell nil)))
+    ;; (message "----------")
+    (while aggressive-indent--changed-list
+      ;; (message "%S" (car aggressive-indent--changed-list))
+      (apply indent-function (car aggressive-indent--changed-list))
+      (setq aggressive-indent--changed-list
+            (cdr aggressive-indent--changed-list)))))
+
 (defun aggressive-indent--indent-if-changed ()
   "Indent any region that changed in the last command loop."
   (when (and aggressive-indent--changed-list 
aggressive-indent--balanced-parens)
@@ -361,15 +378,7 @@ or messages."
                     (aggressive-indent--run-user-hooks))
           (while-no-input
             (redisplay)
-            (let ((inhibit-modification-hooks t)
-                  (inhibit-point-motion-hooks t)
-                  (indent-function
-                   (if (cl-member-if #'derived-mode-p 
aggressive-indent-modes-to-prefer-defun)
-                       #'aggressive-indent--softly-indent-defun 
#'aggressive-indent--softly-indent-region-and-on)))
-              (while aggressive-indent--changed-list
-                (apply indent-function (car aggressive-indent--changed-list))
-                (setq aggressive-indent--changed-list
-                      (cdr aggressive-indent--changed-list))))))))))
+            (aggressive-indent--proccess-changed-list-and-indent)))))))
 
 (defun aggressive-indent--check-parens ()
   "Check if parens are balanced in the current buffer.
@@ -413,9 +422,11 @@ Store result in `aggressive-indent--balanced-parens'."
           (aggressive-indent--local-electric t))
         (aggressive-indent--check-parens)
         (add-hook 'after-change-functions 
#'aggressive-indent--keep-track-of-changes nil 'local)
+        (add-hook 'before-save-hook 
#'aggressive-indent--proccess-changed-list-and-indent nil 'local)
         (add-hook 'post-command-hook #'aggressive-indent--indent-if-changed 
nil 'local))
     ;; Clean the hooks
     (remove-hook 'after-change-functions 
#'aggressive-indent--keep-track-of-changes 'local)
+    (remove-hook 'before-save-hook 
#'aggressive-indent--proccess-changed-list-and-indent 'local)
     (remove-hook 'post-command-hook #'aggressive-indent--indent-if-changed 
'local)
     (remove-hook 'post-command-hook #'aggressive-indent--softly-indent-defun 
'local)))
 

Reply via email to