mirror of
https://github.com/jimeh/yank-indent.git
synced 2026-02-19 09:36:40 +00:00
Merge pull request #5 from jimeh/use-post-command-hook
This commit is contained in:
16
README.md
16
README.md
@@ -59,14 +59,6 @@ Place `yank-indent.el` somewhere in your `load-path` and require it. For example
|
|||||||
(global-yank-indent-mode t)
|
(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
|
## Usage
|
||||||
|
|
||||||
### `global-yank-indent-mode`
|
### `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
|
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
|
explicitly enable `yank-indent-mode` in a buffer, it will operate like normal
|
||||||
regardless of what major-mode the buffer is using.
|
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.
|
|
||||||
|
|||||||
@@ -127,37 +127,6 @@ specific modes and their derived modes from having
|
|||||||
(or (member major-mode yank-indent-global-exact-modes)
|
(or (member major-mode yank-indent-global-exact-modes)
|
||||||
(apply #'derived-mode-p yank-indent-global-derived-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
|
;;;###autoload
|
||||||
(define-minor-mode yank-indent-mode
|
(define-minor-mode yank-indent-mode
|
||||||
"Minor mode for automatically indenting yanked text.
|
"Minor mode for automatically indenting yanked text.
|
||||||
@@ -169,13 +138,8 @@ prefix argument is given during yanking."
|
|||||||
:lighter " YI"
|
:lighter " YI"
|
||||||
:group 'yank-indent
|
:group 'yank-indent
|
||||||
(if yank-indent-mode
|
(if yank-indent-mode
|
||||||
;; Auto-run advice setup if needed first time mode is enabled. Display
|
(add-hook 'post-command-hook #'yank-indent--post-command-hook nil 'local)
|
||||||
;; warning if advice setup has been undone.
|
(remove-hook 'post-command-hook #'yank-indent--post-command-hook 'local)))
|
||||||
(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)))))
|
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(define-globalized-minor-mode global-yank-indent-mode
|
(define-globalized-minor-mode global-yank-indent-mode
|
||||||
@@ -184,20 +148,19 @@ prefix argument is given during yanking."
|
|||||||
(when (yank-indent--should-enable-p)
|
(when (yank-indent--should-enable-p)
|
||||||
(yank-indent-mode 1))))
|
(yank-indent-mode 1))))
|
||||||
|
|
||||||
(defun yank-indent--after-yank-advice (&optional _)
|
(defun yank-indent--post-command-hook ()
|
||||||
"Conditionally indent the region (yanked text) after yanking.
|
"Conditionally indent yanked text.
|
||||||
|
|
||||||
Indentation is triggered only if all of the following conditions
|
Indentation is triggered only if all of the following conditions
|
||||||
are met:
|
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.
|
- Prefix argument was not provided.
|
||||||
- Region size that was yanked is less than or equal to
|
- Region size that was yanked is less than or equal to
|
||||||
`yank-indent-threshold'.
|
`yank-indent-threshold'."
|
||||||
|
(if (and (memq this-command '(yank yank-pop))
|
||||||
This function is used as advice for `yank' and `yank-pop'
|
yank-indent-mode
|
||||||
functions."
|
|
||||||
(if (and yank-indent-mode
|
|
||||||
(not current-prefix-arg))
|
(not current-prefix-arg))
|
||||||
(let ((beg (region-beginning))
|
(let ((beg (region-beginning))
|
||||||
(end (region-end))
|
(end (region-end))
|
||||||
|
|||||||
Reference in New Issue
Block a user