Search code examples
functionemacselispexpansionabbreviation

Equivalent of abbrev-mode but for functions?


I'm a big fan of abbrev-mode and I'd like something a bit similar: you start typing and as soon as you enter some punctation (or just a space would be enough) it invokes a function (if I type space after a special abbreviation, of course, just like abbrev-mode does).

I definitely do NOT want to execute some function every single time I hit space...

So instead of expanding the abbreviation using abbrev-mode, it would run a function of my choice.

Of course it needs to be compatible with abbrev-mode, which I use all the time.

How can I get this behavior?


Solution

  • One approach could be to use pre-abbrev-expand-hook. I don't use abbrev mode myself, but it rather sounds as if you could re-use the abbrev mode machinery this way, and simply define some 'abbreviations' which expand to themselves (or to nothing?), and then you catch them in that hook and take whatever action you wish to.

    The expand library is apparently related, and that provides expand-expand-hook, which may be another alternative?

    edit: Whoops; pre-abbrev-expand-hook is obsolete since 23.1

    abbrev-expand-functions is the correct variable to use:

    Wrapper hook around `expand-abbrev'. The functions on this special hook are called with one argument: a function that performs the abbrev expansion. It should return the abbrev symbol if expansion took place.

    See M-x find-function RET expand-abbrev RET for the code, and you'll also want to read C-h f with-wrapper-hook RET to understand how this hook is used.