evil: work on support for ]p, [p (adjust indent on paste)
Hi there, I would like to work on a patch to add support for ]p and [p to evil. I have some working code (see below) to adjust the indent after a call to evil-paste-after / evil-paste-before, but I would appreciate tips on how to integrate this with existing evil code (especially so that ]p will be a single undo step, that evil-paste-pop still works, …). All the best, Johann (defun get-common-indent-in-region (start end) (setq end (copy-marker end)) (unwind-protect (let (indentation) (save-excursion (goto-char start) (or (bolp) (forward-line 1)) (while (< (point) end) (skip-chars-forward " \t") (setq indentation (cons (current-column) indentation)) (forward-line 1))) (apply #'min indentation)) (move-marker end nil))) (defun my-evil-adjust-indent-afer-paste () (interactive) (unless (memq last-command '(evil-paste-after evil-paste-before)) (error "Previous command was not a line-wise evil-paste: %s" last-command)) (let* ((start (evil-get-marker ?\[)) (end (evil-get-marker ?\])) (common (get-common-indent-in-region start end)) (this (save-excursion (cond ((eq last-command 'evil-paste-before) (goto-char end) (forward-line 1)) (t (goto-char start) (forward-line -1))) (skip-chars-forward " \t") (current-column)))) (indent-rigidly start end (- this common))))
I was just going to implement the same thing! I'll take a look at it tomorrow and see if I can offer any assistance. (I sent this preliminary email so you wouldn't think the mailing list was dead :) On 6/27/14, Johann Klähn <[email protected]> wrote:
Hi there, I would like to work on a patch to add support for ]p and [p to evil. I have some working code (see below) to adjust the indent after a call to evil-paste-after / evil-paste-before, but I would appreciate tips on how to integrate this with existing evil code (especially so that ]p will be a single undo step, that evil-paste-pop still works, ...).
All the best, Johann
(defun get-common-indent-in-region (start end) (setq end (copy-marker end)) (unwind-protect (let (indentation) (save-excursion (goto-char start) (or (bolp) (forward-line 1)) (while (< (point) end) (skip-chars-forward " \t") (setq indentation (cons (current-column) indentation)) (forward-line 1))) (apply #'min indentation)) (move-marker end nil)))
(defun my-evil-adjust-indent-afer-paste () (interactive)
(unless (memq last-command '(evil-paste-after evil-paste-before)) (error "Previous command was not a line-wise evil-paste: %s" last-command))
(let* ((start (evil-get-marker ?\[)) (end (evil-get-marker ?\])) (common (get-common-indent-in-region start end)) (this (save-excursion (cond ((eq last-command 'evil-paste-before) (goto-char end) (forward-line 1)) (t (goto-char start) (forward-line -1))) (skip-chars-forward " \t") (current-column)))) (indent-rigidly start end (- this common))))
You may already know this, but you're going to want to define new commands for [p and ]p using the macro evil-define-command. I haven't though it all the way through yet, but you'll probably call evil-paste-before or evil-paste-after at the beginning and then reindent what was pasted. This should ensure that the commands are a single undo step. I'm not sure about evil-paste-pop, but if you post code for the commands I described I can see if you need to do anything funky to get it working. :) Thanks! ---Gordon Gustafson On 6/28/14, Gordon Gustafson <[email protected]> wrote:
I was just going to implement the same thing! I'll take a look at it tomorrow and see if I can offer any assistance. (I sent this preliminary email so you wouldn't think the mailing list was dead :)
On 6/27/14, Johann Klähn <[email protected]> wrote:
Hi there, I would like to work on a patch to add support for ]p and [p to evil. I have some working code (see below) to adjust the indent after a call to evil-paste-after / evil-paste-before, but I would appreciate tips on how to integrate this with existing evil code (especially so that ]p will be a single undo step, that evil-paste-pop still works, ...).
All the best, Johann
(defun get-common-indent-in-region (start end) (setq end (copy-marker end)) (unwind-protect (let (indentation) (save-excursion (goto-char start) (or (bolp) (forward-line 1)) (while (< (point) end) (skip-chars-forward " \t") (setq indentation (cons (current-column) indentation)) (forward-line 1))) (apply #'min indentation)) (move-marker end nil)))
(defun my-evil-adjust-indent-afer-paste () (interactive)
(unless (memq last-command '(evil-paste-after evil-paste-before)) (error "Previous command was not a line-wise evil-paste: %s" last-command))
(let* ((start (evil-get-marker ?\[)) (end (evil-get-marker ?\])) (common (get-common-indent-in-region start end)) (this (save-excursion (cond ((eq last-command 'evil-paste-before) (goto-char end) (forward-line 1)) (t (goto-char start) (forward-line -1))) (skip-chars-forward " \t") (current-column)))) (indent-rigidly start end (- this common))))
Am 30.06.2014 04:04, schrieb Gordon Gustafson:
You may already know this, but you're going to want to define new commands for [p and ]p using the macro evil-define-command. I haven't though it all the way through yet, but you'll probably call evil-paste-before or evil-paste-after at the beginning and then reindent what was pasted. This should ensure that the commands are a single undo step. I'm not sure about evil-paste-pop, but if you post code for the commands I described I can see if you need to do anything funky to get it working. :)
That's more or less what I would do. However, `evil-paste-pop` won't work because it relies on the previous command being `evil-paste-before` or `evil-paste-after`. So at least you would have to modify the code of `evil-paste-pop` accordingly. (should be easy--it may be even better to introduce a constant that defines the list of commands which are paste-commands or even use a command-property like :keep-visual or :repeat, i.e., add something like `:paste-command t` to the command definition and check in `evil-paste-pop` for this property). Best regards, Frank
(Sorry to everyone, seems my subscription to this list did not go through so I haven't been received your answers. I just stumbled across them via the gmane archive. Thus the reply-to-self.) Gordon, Frank, thanks for taking the time to answer. I originally tried to define a command as Gordon mentioned (see below), but this lead to two separate undo steps (pasting and indenting). Regarding evil-paste-pop I think I like the :paste-command suggestion most, I may end up implementing that. As I am short on time currently, I likely won't get to it this month. (evil-define-command evil-paste-before-adjusting-indent (count &optional register yank-handler) "Like `evil-paste-before' but adjusts the indent to the current line." :suppress-operator t (interactive "P<x>") (evil-paste-before count register yank-handler) (my-evil-adjust-indent-after-paste 'before)) On Fri, Jun 27, 2014 at 2:26 PM, Johann Klähn <[email protected]> wrote:
Hi there, I would like to work on a patch to add support for ]p and [p to evil. I have some working code (see below) to adjust the indent after a call to evil-paste-after / evil-paste-before, but I would appreciate tips on how to integrate this with existing evil code (especially so that ]p will be a single undo step, that evil-paste-pop still works, …).
All the best, Johann
(defun get-common-indent-in-region (start end) (setq end (copy-marker end)) (unwind-protect (let (indentation) (save-excursion (goto-char start) (or (bolp) (forward-line 1)) (while (< (point) end) (skip-chars-forward " \t") (setq indentation (cons (current-column) indentation)) (forward-line 1))) (apply #'min indentation)) (move-marker end nil)))
(defun my-evil-adjust-indent-afer-paste () (interactive)
(unless (memq last-command '(evil-paste-after evil-paste-before)) (error "Previous command was not a line-wise evil-paste: %s" last-command))
(let* ((start (evil-get-marker ?\[)) (end (evil-get-marker ?\])) (common (get-common-indent-in-region start end)) (this (save-excursion (cond ((eq last-command 'evil-paste-before) (goto-char end) (forward-line 1)) (t (goto-char start) (forward-line -1))) (skip-chars-forward " \t") (current-column)))) (indent-rigidly start end (- this common))))
participants (3)
-
Frank Fischer -
Gordon Gustafson -
Johann Klähn