emacs-evil / evil

The extensible vi layer for Emacs.
GNU General Public License v3.0
3.33k stars 282 forks source link

major mode specific text objects #698

Open TheBB opened 8 years ago

TheBB commented 8 years ago

Originally reported by: Wouter Bolsterlee (Bitbucket: wbolster, GitHub: wbolster)


is it possible to specify (and bind) a major-mode specific text object, e.g. "inner-foo" when editing foo files?

it seems the inner and outer text object maps are not buffer-local. should i (make-variable-buffer-local) from a major mode hook?


TheBB commented 8 years ago

Original comment by Wouter Bolsterlee (Bitbucket: wbolster, GitHub: wbolster):


fwiw, i have a hack in place at https://github.com/wbolster/evil-text-object-python

TheBB commented 8 years ago

Original comment by Wouter Bolsterlee (Bitbucket: wbolster, GitHub: wbolster):


thanks!

follow-up question: what is the preferred way for a package to define additional text objects that are major mode specific? i think an autoloaded setup function for the user to call makes sense, but what should it do? add a hook? and what should it do with possibly existing local maps? (i guess define-key does not initialize the map automatically.)

TheBB commented 8 years ago

Original comment by Frank Fischer (Bitbucket: lyro, GitHub: lyro):


Something like

(define-key evil-operator-state-local-map "iw" 'my-inner-word)
(define-key evil-visual-state-local-map "iw" 'my-inner-word)

should work (from a major-mode hook). Alternatively you can create a buffer local keymap for both

(let ((m (make-sparse-keymap)))
  (define-key evil-operator-state-local-map "i" m)
  (define-key evil-visual-state-local-map "i" m)
  (define-key m "w" 'my-inner-word))

These *-local-map keymaps have precedence over the other keymaps. This way only the specified binding are replaced, the rest is still accessible through the global maps.

noctuid commented 4 years ago

I remember this previously not working, but it seems to work as expected now. Will double check it's not something in my config. Anyone had any issues with this?

(evil-define-key '(visual operator) emacs-lisp-mode-map
  "iw" #'foo)