7

Q: how can I get abbrev tables to inherit properties?

Motivation: I'd like to mix and match abbrevs such that some expand automatically when typing, while others expand only when I explicitly tell them to via expand-abbrev. (See a previous question on this issue.)

Solution in a simple case: cribbing from the accepted answer on the above question, one can define a single abbrev to expand explicitly with:

(define-abbrev text-mode-abbrev-table "abbrev" "expansion value" nil
  :enable-function (lambda () (eq this-command 'expand-abbrev)))

Problem: I've got a whole bunch of abbrevs I'd like to use, so doing the above for each one would be tedious. It should be possible to use define-abbrev-table to automate everything, and then have the actual abbrev tables inherit from them. So, consider the following two tables:

(define-abbrev-table 'automatic-table
  '(("test1" "This should expand automatically")
    ;; ... more abbrevs here
    ("test2" "This should also expand automatically")))

(define-abbrev-table 'explicit-table
  '(("test3" "This should expand only after expand-abbrev")
    ;; ... more abbrevs here
    ("test4" "This should also expand only after expand-abbrev"))
  :enable-function (lambda () (eq this-command 'expand-abbrev)))

It seems like it should then be possible to do the following:

(define-abbrev-table 'text-mode-abbrev-table nil
  "Table's docstring"
  :parents (list explicit-table automatic-table))

It does not quite work out, however. text-mode-abbrev-table correctly inherits the abbrevs defined in automatic-table and explicit-table, but they always fire automatically when typing. In other words, the :enable-function property on explicit-table gets ignored.

How can I ensure that the :enable-function property gets inherited for the abbrevs in the relevant table without being applied to abbrevs in a different table?

3
  • Hmmm, as the implementor of those :parents and :enable-function thingies, I'd expect your code to work, so either there's some "subtle" detail that I'm missing or you're hitting a bug. I suggest you M-x report-emacs-bug with a concrete test case.
    – Stefan
    Feb 22, 2015 at 0:08
  • @Dan could you figure it out? Otherwise you might want to delete this.
    – tarsius
    Mar 27, 2015 at 1:44
  • works for me, with or without the doctring
    – Toothrot
    Apr 18, 2020 at 20:51

1 Answer 1

3

Ha! There's a subtle bug in your code: the 3rd argument of define-abbrev-table is the docstring, so it treats :enable-function as the docstring and the subsequent function as a property name (with missing value).

4
  • I've just pushed a fix for that problem in Emacs's "master" branch.
    – Stefan
    Apr 2, 2015 at 14:01
  • Thanks for the additional thought on this, although I'm still getting the same behavior even when I use a placeholder for the docstring (tested with emacs -Q). Any chance you could edit your answer to show the code that actually does work in case I've missed something?
    – Dan
    Apr 2, 2015 at 14:14
  • I just added "Docstring." before :enable-function and tested it with emacs24 -Q and it worked (i.e. after enabling text-mode and abbrev-mode, test1 SPC expands while test3 SPC doesn't).
    – Stefan
    Apr 2, 2015 at 14:38
  • I'm flabbergasted. I did exactly that and test3 SPC still expands. I cannot figure out what I've missed, so will keep trying.
    – Dan
    Apr 2, 2015 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.