Sebastian I
Sebastian I

Reputation: 41

Emacs abbrev-mode in natural languages

I frequently use abbrev-mode in Emacs when writing prose or just taking notes. It would be nice if there was any way to define language-specific abbreviations, e.g. if I write "proj" in an English text, it would expand to "project", whereas if I write it in a Swedish text, it would expand to "projekt". Likewise, "riskfac" would expand to "risk factor" in English but "riskfaktor" in Swedish. How to accomplish this?

It would be especially nice if this could be coupled to the ispell-dictionary that is currently used. I know there are different abbrev-tables, but these are specific to modes, not languages.

Any ideas here?

Upvotes: 4

Views: 544

Answers (3)

Mathias Dahl
Mathias Dahl

Reputation: 210

I had the same requirement (and am also switching between writing English and Swedish in Emacs) and solved it by defining two modes, one for English and one for Swedish. Both derive from a special "writer mode" that has some useful features when I am just writing text. I have some convenient keybindings (and even abbrevs that run code) to switch modes/languages. Since each language has its own mode, I can keep different abbrevs for the two languages.

Here is part of my setup:

(define-derived-mode writer-mode text-mode "W-EN"
  "Writer mode."
  (abbrev-mode 1))

(define-derived-mode writer-english-mode writer-mode "W-EN"
  "Writer mode - English.")

(define-derived-mode writer-swedish-mode writer-mode "W-SV"
  "Writer mode - Swedish.")

...

(defun writer-swedish ()
  (interactive)
  (writer-swedish-mode)
  (set-input-method 'swedish-postfix)
  (setq header-line-format " SV> ")
  (setq mode-line-format nil)
  (flyspell-mode -1)
  (writer-setup-bindings))

(defun writer-english ()
  (interactive)
  (writer-english-mode)
  (deactivate-input-method)
  (setq header-line-format " EN> ")
  (setq mode-line-format nil)
  (flyspell-mode 1)
  (writer-setup-bindings))

As you can see I also use an input method when writing Swedish. I have a keyboard with Swedish letters but try to stay with English keyboard layout since it is generally better when writing code.

Upvotes: 1

Drew
Drew

Reputation: 30708

Consider trying dynamic-completion-mode (standard library completion.el).

You can change between different dynamic-completion files, one for each language. Option save-completions-file-name holds the file name, but nothing says that you cannot change its value dynamically, e.g. using a command, in order to switch among several sets of completions. (Naturally, such a command should save to one file before switching to another.)

The "doc" for dynamic-completion-mode is in the Commentary of library completion.el. The library is old, but still quite useful, IMHO. Excerpts from the Commentary:

This watches all the words that you type and remembers them.  When
typing a new word, pressing "complete" (meta-return) "completes" the
word by inserting the most recently used word that begins with the
same characters.  If you press meta-return repeatedly, it cycles
through all the words it knows about.

If you like the completion then just continue typing, it is as if you
entered the text by hand.  If you want the inserted extra characters
to go away, type control-w or delete.  More options are described below.

The guesses are made in the order of the most recently "used".  Typing
in a word and then typing a separator character (such as a space) "uses"
the word.  So does moving a cursor over the word.  If no words are found,
it uses an extended version of the dabbrev style completion.

(See also Icicles completion for dynamic-completion-mode.)

Upvotes: 0

Phil Lord
Phil Lord

Reputation: 3063

For free text, I tend to use pabbrev.el (which I wrote!) but there are several other packages which now do the same thing -- a dynamic abbreviation expansion depending on what you have already written. This tends to give a degree of language specificity in practice.

Otherwise, I think you need something to switch the abbrev tables in different buffers. Perhaps you could hook this into input methods if you are using them, then Emacs would know which language you were using.

Upvotes: 1

Related Questions