Use universal argument for auto-indent pasted text and add doc

Using the prefix argument does not work nicely with evil since it is
already used to paste several times. Instead we use the universal
argument. To make it work with the evil functions we have to do some
trick to hide the universal argument temporarily (see around advice).
This commit is contained in:
syl20bnr 2015-05-10 22:49:07 -04:00
parent fe5c6ec1bc
commit d587dd9db9
3 changed files with 44 additions and 23 deletions

View File

@ -117,6 +117,8 @@
- [Helm-swoop](#helm-swoop)
- [Editing](#editing)
- [Paste text](#paste-text)
- [Paste Micro-state](#paste-micro-state)
- [Auto-indent pasted text](#auto-indent-pasted-text)
- [Text manipulation commands](#text-manipulation-commands)
- [Smartparens Strict mode](#smartparens-strict-mode)
- [Zooming](#zooming)
@ -1871,6 +1873,8 @@ Key Binding | Description
### Paste text
#### Paste Micro-state
Whenever you paste some text a `paste` micro-state is initiated. Pressing
<kbd>p</kbd> again will replace the pasted text with the previous
yanked (copied) text on the kill ring.
@ -1889,6 +1893,15 @@ Any other key | leave the micro-state
This micro-state can be disabled by setting
`dotspacemacs-enable-paste-micro-state` to `nil` in `~/.spacemacs`.
#### Auto-indent pasted text
By default any pasted text will be auto-indented. To paste text un-indented
use the universal argument.
It is possible to disable the auto-indentation for specific major-modes by
adding a major-mode to the variable `spacemacs-indent-sensitive-modes` in
your `dotspacemacs/config` function.
### Text manipulation commands
Text related commands (start with `x`):

View File

@ -144,6 +144,7 @@ the current state and point position."
(setq counter (1- counter)))))
;; from Prelude
;; TODO: dispatch these in the layers
(defvar spacemacs-indent-sensitive-modes
'(coffee-mode
python-mode
@ -897,12 +898,13 @@ The body of the advice is in BODY."
(if (<= (- end beg) spacemacs-yank-indent-threshold)
(indent-region beg end nil)))
(spacemacs|advise-commands "indent" (yank yank-pop evil-paste-after) after
"If current mode is not one of spacemacs-indent-sensitive-modes
indent yanked text (with prefix arg don't indent)."
(if (and (not (ad-get-arg 0))
(not (member major-mode spacemacs-indent-sensitive-modes))
(or (derived-mode-p 'prog-mode)
(member major-mode spacemacs-indent-sensitive-modes)))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))
(spacemacs|advise-commands
"indent" (yank yank-pop evil-paste-before evil-paste-after) after
"If current mode is not one of spacemacs-indent-sensitive-modes
indent yanked text (with universal arg don't indent)."
(if (and (not (equal '(4) (ad-get-arg 0)))
(not (member major-mode spacemacs-indent-sensitive-modes))
(or (derived-mode-p 'prog-mode)
(member major-mode spacemacs-indent-sensitive-modes)))
(let ((transient-mark-mode nil))
(yank-advised-indent-function (region-beginning) (region-end)))))

View File

@ -719,18 +719,21 @@ Example: (evil-map visual \"<\" \"<gv\")"
("<" spacemacs/scroll-half-page-up)
(">" spacemacs/scroll-half-page-down))
;; support for auto-indentation inhibition on universal argument
(spacemacs|advise-commands
"handle-indent" (evil-paste-before evil-paste-after) around
"Handle the universal prefix argument for auto-indentation."
(let ((prefix (ad-get-arg 0)))
(ad-set-arg 0 (unless (equal '(4) prefix) prefix))
ad-do-it
(ad-set-arg 0 prefix)))
;; pasting micro-state
(defadvice evil-paste-before (after spacemacs/evil-paste-before activate)
"Initate the paste micro-state after the execution of evil-paste-before"
(unless (evil-ex-p)
(spacemacs/paste-micro-state)))
(defadvice evil-paste-after (after spacemacs/evil-paste-after activate)
"Initate the paste micro-state after the execution of evil-paste-after"
(unless (evil-ex-p)
(spacemacs/paste-micro-state)))
(defadvice evil-visual-paste (after spacemacs/evil-visual-paste activate)
"Initate the paste micro-state after the execution of evil-visual-paste"
(spacemacs/paste-micro-state))
(spacemacs|advise-commands
"past-micro-state"
(evil-paste-before evil-paste-after evil-visual-paste) after
"Initate the paste micro-state."
(unless (evil-ex-p) (spacemacs/paste-micro-state)))
(defun spacemacs//paste-ms-doc ()
"The documentation for the paste micro-state."
(format (concat "[%s/%s] Type [p] or [P] to paste the previous or "
@ -743,11 +746,14 @@ Example: (evil-map visual \"<\" \"<gv\")"
("p" evil-paste-pop)
("P" evil-paste-pop-next))
(unless dotspacemacs-enable-paste-micro-state
(ad-disable-advice 'evil-paste-before 'after 'spacemacs/evil-paste-before)
(ad-disable-advice 'evil-paste-before 'after
'spacemacs/evil-paste-before-after-ad)
(ad-activate 'evil-paste-before)
(ad-disable-advice 'evil-paste-after 'after 'spacemacs/evil-paste-after)
(ad-disable-advice 'evil-paste-after 'after
'spacemacs/evil-paste-after-after-ad)
(ad-activate 'evil-paste-after)
(ad-disable-advice 'evil-visual-paste 'after 'spacemacs/evil-visual-paste)
(ad-disable-advice 'evil-visual-paste 'after
'spacemacs/evil-visual-paste)
(ad-activate 'evil-visual-paste))
;; define text objects