Combining two operators in Evil-mode Emacs

Posted by Dyslexic Tangent on Super User See other posts from Super User or by Dyslexic Tangent
Published on 2012-09-02T21:48:14Z Indexed on 2012/09/03 3:40 UTC
Read the original article Hit count: 533

Filed under:
|
|

In vim I've remapped > and < when in visual mode to >gv and <gv respectively, like so:

vnoremap > >gv
vnoremap < <gv

Since my target for this question are folks experienced with emacs and not vim, what > and < do is indent/dedent visually selected text. What gv does is reselect the previously selected text. These maps cause > and < to indent/dedent and then reselect the previously selected text.

I'm trying out emacs with evil-mode and I'd like to do the same, but I'm having some difficulty figuring out how, exactly, to accomplish the automatic reselection.

It looks like I need to somehow call evil-shift-right and evil-visual-restore sequentially, but I don't know how to create a map that will do both, so I tried creating my own function which would call both sequentially and map that instead, but it didn't work, possibly due to the fact that both of them are defined, not as functions with defun but instead as operators with evil-define-operator.

I tried creating my own operators:

(evil-define-operator shift-left-reselect (beg end)
    (evil-shift-left beg end)
    (evil-visual-restore))

(evil-define-operator shift-right-reselect (beg end)
    (evil-shift-right beg end)
    (evil-visual-restore))

but that doesn't restore visual as expected. A stab in the dark gave me this:

(evil-define-operator shift-left-reselect (beg end)
    (evil-shift-left beg end)
    ('evil-visual-restore))

(evil-define-operator shift-right-reselect (beg end)
    (evil-shift-right beg end)
    ('evil-visual-restore))

but that selects one additional line whenever it is supposed to reselect.

For now I've been using the following, which only has the problem where it reselects an additional line in the < operator.

(evil-define-operator shift-right-reselect (beg end)
  (evil-shift-right beg end)
  (evil-visual-make-selection beg end))

(evil-define-operator shift-left-reselect (beg end)
  (evil-shift-left beg end)
  (evil-visual-make-selection beg end))

and I've mapped them:

(define-key evil-visual-state-map ">" 'shift-right-reselect)
(define-key evil-visual-state-map "<" 'shift-left-reselect)

any help / pointers / tips would be greatly appreciated.

Thanks in advance.

© Super User or respective owner

Related posts about vim

Related posts about emacs