fix(setup)!: simplify package/load setup by not using a global advice

Use buffer-local post-commad-hook instead of global function advice on
yank and yank-pop. This avoids any global changes to Emacs' runtime
environment outside of the specific buffers within which
yank-indent-mode is enabled.

BREAKING CHANGE: Removed yank-indent-setup and yank-indent-teardown functions.
This commit is contained in:
2023-06-12 22:36:47 +01:00
parent 28b7ef837d
commit b047e91b92
2 changed files with 9 additions and 62 deletions

View File

@@ -59,14 +59,6 @@ Place `yank-indent.el` somewhere in your `load-path` and require it. For example
(global-yank-indent-mode t)
```
## Setup & Teardown
Required setup that registers advice on `yank` and `yank-pop` commands is
automatically done the first time that `yank-indent-mode` is enabled.
Should you want to though you can manually add/remove the required advice with
`yank-indent-setup` and `yank-indent-teardown`.
## Usage
### `global-yank-indent-mode`
@@ -92,11 +84,3 @@ Keep in mind that the include/exclude major-mode customizations only affect the
global mode and which buffers it enables `yank-indent-mode` in. If you
explicitly enable `yank-indent-mode` in a buffer, it will operate like normal
regardless of what major-mode the buffer is using.
## Under the Hood
`yank-indent` registers an advice for after `yank` and `yank-pop` commands. The
advice function verifies that `yank-indent-mode` mode is enabled in the current
buffer, prefix argument was not given, and the yanked/pasted text was within the
`yank-indent-threshold` in size. If all true, it will trigger indentation,
otherwise it does nothing.

View File

@@ -127,37 +127,6 @@ specific modes and their derived modes from having
(or (member major-mode yank-indent-global-exact-modes)
(apply #'derived-mode-p yank-indent-global-derived-modes))))
(defvar yank-indent--initial-setup nil)
(defun yank-indent--is-setup-p ()
"Return non-nil if required advice is setup."
(and (advice-member-p #'yank-indent--after-yank-advice #'yank)
(advice-member-p #'yank-indent--after-yank-advice #'yank-pop)))
;;;###autoload
(defun yank-indent-setup ()
"Setup advice on `yank' and `yank-pop' as required by `yank-indent-mode'.
First time `yank-indent-mode' is enabled it will automatically
call `yank-indent-setup' if needed.
Setup can be undone with `yank-indent-teardown', but enabling
`yank-indent-mode' again after that will not run setup again."
(interactive)
(advice-add #'yank :after #'yank-indent--after-yank-advice)
(advice-add #'yank-pop :after #'yank-indent--after-yank-advice)
(setq yank-indent--initial-setup t))
;;;###autoload
(defun yank-indent-teardown ()
"Undo `yank-indent-setup' by removing advice from `yank' and `yank-pop'.
If this is used, `yank-indent-setup' must be explicitly called
before `yank-indent-mode' will work again."
(interactive)
(advice-remove #'yank #'yank-indent--after-yank-advice)
(advice-remove #'yank-pop #'yank-indent--after-yank-advice))
;;;###autoload
(define-minor-mode yank-indent-mode
"Minor mode for automatically indenting yanked text.
@@ -169,13 +138,8 @@ prefix argument is given during yanking."
:lighter " YI"
:group 'yank-indent
(if yank-indent-mode
;; Auto-run advice setup if needed first time mode is enabled. Display
;; warning if advice setup has been undone.
(when (not (yank-indent--is-setup-p))
(if yank-indent--initial-setup
(message (concat "Warning: yank-indent-mode not available, "
"run `M-x yank-indent-setup' to setup."))
(yank-indent-setup)))))
(add-hook 'post-command-hook #'yank-indent--post-command-hook nil 'local)
(remove-hook 'post-command-hook #'yank-indent--post-command-hook 'local)))
;;;###autoload
(define-globalized-minor-mode global-yank-indent-mode
@@ -184,20 +148,19 @@ prefix argument is given during yanking."
(when (yank-indent--should-enable-p)
(yank-indent-mode 1))))
(defun yank-indent--after-yank-advice (&optional _)
"Conditionally indent the region (yanked text) after yanking.
(defun yank-indent--post-command-hook ()
"Conditionally indent yanked text.
Indentation is triggered only if all of the following conditions
are met:
- `yank-indent-mode' minor-mode is enabled in the current buffer.
- `this-command' is `yank' or `yank-pop'.
- `yank-indent-mode' is enabled.
- Prefix argument was not provided.
- Region size that was yanked is less than or equal to
`yank-indent-threshold'.
This function is used as advice for `yank' and `yank-pop'
functions."
(if (and yank-indent-mode
`yank-indent-threshold'."
(if (and (memq this-command '(yank yank-pop))
yank-indent-mode
(not current-prefix-arg))
(let ((beg (region-beginning))
(end (region-end))