From d587dd9db92308c1711d732394a52654b4658d0d Mon Sep 17 00:00:00 2001 From: syl20bnr Date: Sun, 10 May 2015 22:49:07 -0400 Subject: [PATCH] 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). --- doc/DOCUMENTATION.md | 13 +++++++++++++ spacemacs/funcs.el | 20 +++++++++++--------- spacemacs/packages.el | 34 ++++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/doc/DOCUMENTATION.md b/doc/DOCUMENTATION.md index 3924e221a..818a08405 100644 --- a/doc/DOCUMENTATION.md +++ b/doc/DOCUMENTATION.md @@ -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 p 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`): diff --git a/spacemacs/funcs.el b/spacemacs/funcs.el index 86552863b..bc818737b 100644 --- a/spacemacs/funcs.el +++ b/spacemacs/funcs.el @@ -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))))) diff --git a/spacemacs/packages.el b/spacemacs/packages.el index ec60a58ef..093993cc3 100644 --- a/spacemacs/packages.el +++ b/spacemacs/packages.el @@ -719,18 +719,21 @@ Example: (evil-map visual \"<\" \"" 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 \"<\" \"