switching states via hooks

Vegard Øye vegard_oye at hotmail.com
Sun Jan 15 17:49:49 CET 2012


On 2012-01-14 01:35 +0100, Leo Alekseyev wrote:

> I was trying to get finer-grained control over which states
> buffers start up in. I defined something like
>
> (defun my-switch-to-emacs-state-hook ()
>   (if evil-mode (evil-emacs-state)))
>
> (add-hook 'org-src-mode-hook 'my-switch-to-emacs-state-hook)
>
> The hook runs, but the state switch doesn't take place. On the other
> hand, if I evaluate (my-switch-to-emacs-state-hook) in the buffer,
> the switch takes place. Can anyone shed light on why that might be
> happening?

Evil chooses the initial state for the buffer on the basis on its
major mode (well, it's usually the major mode -- minor modes can
also be configured in this way). Because certain modes jump through
some hoops as part of their initialization, Evil postpones its own
initialization with `post-command-hook'. This only happens once, so
when evaluating (my-switch-to-emacs-state-hook) by hand, after Evil's
initialization is finished, the switch works.

To set the initial state for a mode, use `evil-set-initial-state'.
Alternatively, you can force the initialization process with an
argument to `evil-initialize-state' (after commit 3b7d2e3):

    (defun my-switch-to-emacs-state-hook ()
      (when evil-mode (evil-initialize-state 'emacs)))

This will immediately set the initial state for the buffer, instead
of waiting for `post-command-hook'.

-- 
Vegard



More information about the implementations-list mailing list