6

I have defined some abbreviations for org-mode, but I want them to conditionally expand. For example, n2 expands to N_{2}, and I want to prevent that from happening in src-blocks.

The bottom of this page: https://www.gnu.org/software/emacs/manual/html_node/elisp/Abbrev-Expansion.html suggests I should use advice to achieve this. That doesn't seem too flexible though, I don't want all abbrevs to not work in src blocks, just some.

Are there other ways to get context-specific abbreviations?

Drew
  • 75,699
  • 9
  • 109
  • 225
John Kitchin
  • 11,555
  • 1
  • 19
  • 41

1 Answers1

5

Yes, you can tell the abbrev table to use an explicit function to test whether or not to fire in a given instance. The relevant argument in define-abbrev-table is :enable-function, which, according to the docstring, reads:

(define-abbrev-table TABLENAME DEFINITIONS &optional DOCSTRING &rest PROPS)

Define TABLENAME (a symbol) as an abbrev table name. Define abbrevs in it according to DEFINITIONS, which is a list of elements of the form (ABBREVNAME EXPANSION ...) that are passed to define-abbrev. PROPS is a property list to apply to the table. Properties with special meaning:

(blah blah blah)

  • :enable-function can be set to a function of no argument which returns non-nil if and only if the abbrevs in this table should be used for this instance of expand-abbrev.

So: the following toy example (untested) should work:

(define-abbrev-table 'org-mode-abbrev-table
  '(("test1"  "this is a test")
    ("test2"  "this is also a test"))
  "Table of abbrevs that should NOT fire when in a source block."
  :enable-function (lambda ()
                     (not (org-in-src-block-p))))
Dan
  • 32,584
  • 6
  • 98
  • 168