evil-mode: How to automatically switch to insert mode after certain commands?

Óscar Fuentes ofv at wanadoo.es
Fri Mar 20 20:50:46 CET 2015


max6166 <max6166 at hotmail.com> writes:

> Thank you so much, Oscar. The above method is working great so far.

You're welcome.

> I was thinking of adapting your code for other purposes, but I am concerned
> about your warning. What are the potential problems with this code? 

The problem with advising `call-interactively' is that it is a core
function that invokes all other interactive commands. When we call
`evil-insert-state' from the advice, we are introducing side effects
that can collide with the state created by the command invoked by
`call-interactively'. One dumb example of this interaction is to add
`evil-normal-state' to `maybe-enter-insert-mode-commands': the advice
would revert the effect of `evil-normal-state' by calling
`evil-insert-state' just afterwards. I can't think of a realistic case
that could cause a problem, but the method makes me feel a bit uneasy
anyways. You can ask on emacs-help about the risks of adivind
`call-interactively', if you feel concerned. The Elisp experts there can
also suggest better methods for achieving what you want.

> For other readers, I think the last line is missing a hash and should be:
>
> (advice-add 'call-interactively :after #'maybe-enter-insert-mode)

The hash is "more correct" in the sense that AFAIK it allows some checks
(the byte compiler will warn if `maybe-enter-insert-mode' is not
defined, for example) but otherwise it is not strictly required.




More information about the implementations-list mailing list