Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Really protect against unwanted indentation after undo #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions aggressive-indent.el
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ change."

;;; Preventing indentation
(defconst aggressive-indent--internal-dont-indent-if
'((memq last-command aggressive-indent-protected-commands)
(memq this-command aggressive-indent-protected-current-commands)
'((memq this-command aggressive-indent-protected-current-commands)
(region-active-p)
buffer-read-only
undo-in-progress
(null (buffer-modified-p))
(and (boundp 'smerge-mode) smerge-mode)
(equal (buffer-name) "*ediff-merge*")
Expand Down Expand Up @@ -471,6 +469,26 @@ 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)
(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."
(when aggressive-indent-mode
Expand Down Expand Up @@ -509,14 +527,17 @@ If BODY finishes, `while-no-input' returns whatever value BODY produced."
(aggressive-indent--local-electric t))
(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 '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 '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 'post-command-hook #'aggressive-indent--softly-indent-defun '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)
"Turn variable `electric-indent-mode' on or off locally, as per boolean ON."
Expand Down