Question

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?

Était-ce utile?

La 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.

Autres conseils

EDIT: Your revised question adds some key details that my answer didn't address. phils has provided one way to approach this issue. Another would be to use yasnippet . You can include arbitrary lisp code in your snippet templates, so you could do something like this:

# -*- mode: snippet -*-
# name: foobars
# key: fbf
# binding: direct-keybinding
# --
 `(foo-bar-for-the-win)`

You'd need to ensure your function didn't return anything, or it would be inserted in the buffer. I don't use abbrev-mode, so I don't know if this would introduce conflicts. yas/snippet takes a bit of experimenting to get it running, but it's pretty handy once you get it set up.

Original answer: You can bind space to any function you like. You could bind all of the punctuation keys to the same function, or to different functions.

(define-key your-mode-map " " 'your-choice-function)

You probably want to do this within a custom mode map, so you can return to normal behaviour when you switch modes. Globally setting space to anything but self-insert would be unhelpful.

Every abbrev is composed of several elements. Among the main elements are the name (e.g. "fbf"), the expansion (any string you like), and the hook (a function that gets called). In your case it sounds like you want the expansion to be the empty string and simply specify your foo-bar-for-the-win as the hook.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top