Skip to content

Commit

Permalink
Be slightly more careful before clearing the changes list
Browse files Browse the repository at this point in the history
* aggressive-indent.el
(aggressive-indent--pre-command-change-head): New variable.
(aggressive-indent--pre-command): New function.
(aggressive-indent-mode): Add aggressive-indent--pre-command to
pre-command-hook and remove it when exiting minor mode.
  • Loading branch information
joaotavora committed Dec 13, 2019
1 parent 9bd3a85 commit 3f3add4
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions aggressive-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,25 @@ If BODY finishes, `while-no-input' returns whatever value BODY produced."
(when (timerp aggressive-indent--idle-timer)
(cancel-timer aggressive-indent--idle-timer)))))

(defvar aggressive-indent--pre-command-change-head nil)

(defun aggressive-indent--pre-command ()
"Hook run before every command while in `aggressive-indent-mode'.
Save the most recent element of
`aggressive-indent--changed-list'."
(setq aggressive-indent--pre-command-change-head
(car aggressive-indent--changed-list)))

(defun aggressive-indent--post-command ()
"Hook run after every command while in `aggressive-indent-mode'.
Clears `aggressive-indent--changed-list' iff the current
command (the one that's now finished) lives in
`aggressive-indent-protected-commands'."
(when (memq this-command aggressive-indent-protected-commands)
(setq aggressive-indent--changed-list nil)))
(while (not (eq aggressive-indent--pre-command-change-head
(pop aggressive-indent--changed-list))))))

(defun aggressive-indent--keep-track-of-changes (l r &rest _)
"Store the limits (L and R) of each change in the buffer."
Expand Down Expand Up @@ -517,13 +528,15 @@ command (the one that's now finished) lives in
(add-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes nil 'local)
(add-hook 'after-revert-hook #'aggressive-indent--clear-change-list nil 'local)
(add-hook 'before-save-hook #'aggressive-indent--proccess-changed-list-and-indent nil 'local)
(add-hook 'post-command-hook #'aggressive-indent--post-command nil 'local))
(add-hook 'post-command-hook #'aggressive-indent--post-command nil 'local)
(add-hook 'pre-command-hook #'aggressive-indent--pre-command nil 'local))
;; Clean the hooks
(when (timerp aggressive-indent--idle-timer)
(cancel-timer aggressive-indent--idle-timer))
(remove-hook 'after-change-functions #'aggressive-indent--keep-track-of-changes 'local)
(remove-hook 'after-revert-hook #'aggressive-indent--clear-change-list 'local)
(remove-hook 'before-save-hook #'aggressive-indent--proccess-changed-list-and-indent 'local)
(remove-hook 'pre-command-hook #'aggressive-indent--pre-command 'local)
(remove-hook 'post-command-hook #'aggressive-indent--post-command 'local)))

(defun aggressive-indent--local-electric (on)
Expand Down

0 comments on commit 3f3add4

Please sign in to comment.