Skip to content

Instantly share code, notes, and snippets.

@cofi
Last active December 13, 2015 19:29
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cofi/4963125 to your computer and use it in GitHub Desktop.
Save cofi/4963125 to your computer and use it in GitHub Desktop.
;; AceJump integration is now included in evil, this gist is only preserved for historical reasons.
;; Please use the provided integration (it's far more advanced)
;; AceJump is a nice addition to evil's standard motions.
;; The following definitions are necessary to define evil motions for ace-jump-mode (version 2).
;; ace-jump is actually a series of commands which makes handling by evil
;; difficult (and with some other things as well), using this macro we let it
;; appear as one.
(defmacro evil-enclose-ace-jump (&rest body)
`(let ((old-mark (mark))
(ace-jump-mode-scope 'window))
(remove-hook 'pre-command-hook #'evil-visual-pre-command t)
(remove-hook 'post-command-hook #'evil-visual-post-command t)
(unwind-protect
(progn
,@body
(recursive-edit))
(if (evil-visual-state-p)
(progn
(add-hook 'pre-command-hook #'evil-visual-pre-command nil t)
(add-hook 'post-command-hook #'evil-visual-post-command nil t)
(set-mark old-mark))
(push-mark old-mark)))))
(evil-define-motion evil-ace-jump-char-mode (count)
:type exclusive
(evil-enclose-ace-jump
(ace-jump-mode 5)))
(evil-define-motion evil-ace-jump-line-mode (count)
:type line
(evil-enclose-ace-jump
(ace-jump-mode 9)))
(evil-define-motion evil-ace-jump-word-mode (count)
:type exclusive
(evil-enclose-ace-jump
(ace-jump-mode 1)))
(evil-define-motion evil-ace-jump-char-to-mode (count)
:type exclusive
(evil-enclose-ace-jump
(ace-jump-mode 5)
(forward-char -1)))
(add-hook 'ace-jump-mode-end-hook 'exit-recursive-edit)
;; some proposals for binding:
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode)
(define-key evil-motion-state-map (kbd "C-SPC") #'evil-ace-jump-word-mode)
(define-key evil-operator-state-map (kbd "SPC") #'evil-ace-jump-char-mode) ; similar to f
(define-key evil-operator-state-map (kbd "C-SPC") #'evil-ace-jump-char-to-mode) ; similar to t
(define-key evil-operator-state-map (kbd "M-SPC") #'evil-ace-jump-word-mode)
;; different jumps for different visual modes
(defadvice evil-visual-line (before spc-for-line-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-line-mode))
(defadvice evil-visual-char (before spc-for-char-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode))
(defadvice evil-visual-block (before spc-for-char-jump activate)
(define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode))
@hoodunit
Copy link

Is there a particular reason this uses defadvice instead of hooks on evil states? Also, on my setup this doesn't restore the C-SPC keybinding to evil-ace-jump-char-mode after exiting evil visual line mode unless I add this:

(add-hook 'evil-visual-state-exit-hook
  (lambda () (define-key evil-motion-state-map (kbd "SPC") #'evil-ace-jump-char-mode)))

@cofi
Copy link
Author

cofi commented Aug 31, 2013

You can't differ the visual modes with the hook, so the advice is necessary.
As for the keybinding: I don't see any restoring of C-SPC. If you mean SPC as your code indicates: Works for me.

@joefromct
Copy link

I can't seem to find any documentation on evil now having ace-jump mode integrated... can you forward a reference?
Thanks,

Copy link

ghost commented Aug 8, 2014

Couldn't find a reference, but this worked nicely for me (24.4 from brew):

(global-set-key (kbd "C-ø") 'ace-jump-mode)
(define-key evil-normal-state-map (kbd "ø") 'ace-jump-mode)

@cofi
Copy link
Author

cofi commented Aug 11, 2014

@joefromct: Just use the bindings. Evil now already comes with definitions of the motions.

@endrebak: That works. But you don't have operator integration this way. You additionally need bindings in the motion state map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment