Q: how does one approximate the functionality of the non-existent pre-self-insert-hook
?
This question is inspired by the curiosity that is the combination of abbrev-mode
and self-insert-command
. The docstring for the latter:
Insert the character you type.
Whichever character you type to run this command is inserted. Before insertion,
expand-abbrev
is executed if the inserted character does not have word syntax and the previous character in the buffer does. After insertion, the value ofauto-fill-function
is called if theauto-fill-chars
table has a non-nil value for the inserted character. At the end, it runspost-self-insert-hook
.
It looks to be the case that the call to expand-abbrev
is baked directly into self-insert-command
-- I assume for efficiency reasons, because it is ancient, or both (feel free to comment if you happen to know why). Ordinarily, I would have expected such a function to run via a hook. However, although there is a post-self-insert-hook
, there is no pre-self-insert-hook
.
In the absence of such a hook, how does one approximate that functionality, and, more importantly, do so with a minimum of fuss?
None of the options I can think of are ideal:
- advice:
self-insert-command
is a primitive defined in C and therefore ignores advice - pre-command-hook: simple, but it's overkill because it runs before every command, and not just
self-insert-command
, and one may only want to run the command as part ofself-insert-command
- define a wrapper around
self-insert-command
: this appears to be whatorg-mode
andsmartparens
do (withorg-self-insert-command
andsp--self-insert-command
), but seems complicated by the fact that a) you'd need to rebind an awful lot of keys to use it, b) if one uses multiple major or minor modes that use their own wrapper functions, one needs to keep track of fallback functions so that one does not lose the functionality of other wrappers when rebinding keys (at least I think so; I'm not so sure I understand this part).