5

I use multiple programming languages, and there are a considerable number of abbreviations that I use across all of their corresponding major mode (they are usually name of certain variables, but not limited to this category). One way of customizing Emacs is as follows: As explained here one needs to this in an Elisp file (see the example below) and load it in the init file.

Example Elisp file for storing the abbreviations (ignore that the syntax for the specified languages is not correct):

(when (boundp 'python-mode-abbrev-table)
  (clear-abbrev-table python-mode-abbrev-table))

(define-abbrev-table 'python-mode-abbrev-table
  
    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

(when (boundp 'matlab-mode-abbrev-table)
  (clear-abbrev-table matlab-mode-abbrev-table))

(define-abbrev-table 'matlab-mode-abbrev-table
  
    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

(set-default 'abbrev-mode t)

(setq save-abbrevs nil)

But then if you want to expand the list, you need to copy them for abbreviation of all major mode. Is there any way that I maintain only one list, and somehow call them for the needed major modes (maybe the solution is something like the \input{} in latex).

By the way, I prefer not to use the solution proposed here, as the usual/traditional way to set up abbreviations in Emacs was not very efficient for me (of course if I don't manage to find any other way I may have to use it).

Furthermore, each major mode can have its own abbrev table on top of the common one. In particular, I don't want any of the major modes to inherit all the abbreviations used in other modes (because language syntax is different in each language). Ideally, I'm trying to have something like this (I don't know any Elisp syntax, so ignore!):

;; Common ones 
(when (boundp 'python-mode-abbrev-table OR matlab-mode-abbrev-table)

(define-abbrev-table 'python-mode-abbrev-table AND matlab-mode-abbrev-table
  
    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ("r" "return")
    ("st" "string")
    ("eq" "==")
    ("v" "var x = 3")
    ("df" "x := 3")
    ("c" "const x = 3")
    ))

;; Python specific ones 
(when (boundp 'python-mode-abbrev-table)

(define-abbrev-table 'python-mode-abbrev-table
  
    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ))

;; MATLAB specific ones 
(when (boundp 'matlab-mode-abbrev-table)

(define-abbrev-table 'matlab-mode-abbrev-table
  
    ("p" "fmt.Printf(\"%v\\n\", hh▮)")
    ("pl" "fmt.Println(hh▮)")
    ))


(set-default 'abbrev-mode t)

(setq save-abbrevs nil)

I'm new to Emacs (and this is the first time posting here), so sorry if the question and the way I phrase it is too naive!

Drew
  • 75,699
  • 9
  • 109
  • 225

1 Answers1

2

I don't think there's a good UI to access it, but abbrev-tables can have parents, so you could create a new abbrev-table to hold your shared abbrevs and then setup those different major-mode abbrev-tables to have your new table as one of their parents.

Another option is to leave the major-mode tables as-is, but to turn local-abbrev-table into a list of abbrev-tables, e.g.:

(add-hook 'python-mode-hook
  (lambda ()
    (setq local-abbrev-table
          (list local-abbrev-table my-shared-abbrev-table))))

If those abbreviations are valid "almost everywhere" then you may just use the global-abbrev-table of course.

Finally, you could create a dummy minor-mode that does nothing else than provide an abbrev table:

(define-abbrev-table 'my-shared-abbrev-table ....)
(define-minor-mode my-shared-abbrevs-mode
  "Enable my shared abbrevs."
  :local t)
(add-to-list 'abbrev-minor-mode-table-alist
             (cons 'my-shared-abbrevs-mode
                   my-shared-abbrev-table))
Stefan
  • 26,154
  • 3
  • 46
  • 84
  • 2
    Thanks for your answer. To me the most straightforward solution is the fist one. But I couldn't figure it out how to implement it (sorry for my naiveness in emacs). I could only find [this](https://www.gnu.org/software/emacs/manual/html_node/elisp/Abbrev-Table-Properties.html#Abbrev-Table-Properties) which doesn't have an example. If I may ask, could you give me minimal example to define the shared abbrev-tabel and then how to tell the needed major mode to use this shared abbrev-tabel? – neuroprinciplist May 13 '19 at 19:15