Skip to content

Instantly share code, notes, and snippets.

@feklee
Last active September 22, 2019 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feklee/b855424f0a7932446bb2192d2a363731 to your computer and use it in GitHub Desktop.
Save feklee/b855424f0a7932446bb2192d2a363731 to your computer and use it in GitHub Desktop.
Emacs multi language configuration
;; EMACS configuration for editing files in different languages.
;;
;; When loading a text file, depending on the specified ispell
;; dictionary:
;;
;; * Sets keyboard layout (layout when starting Emacs needs to be
;; American)
;;
;; * If available, enables "typo-mode" with the correct language
;; option. "typo-mode" facilitates entering typographically correct
;; punctuation marks.
;;
;; Furthermore, language environments can be changed with the below
;; keyboard shortcuts. In addition to the settings above, the spell
;; checking language is adjusted.
;;
;; <C-M-0> German
;; <C-M-1> American
;; <C-M-2> Spanish
;; <C-M-3> Greek
;;
;; For running flyspell, hit <F8>. The author uses Hunspell.
;;
;; Example "test.txt" for testing:
;;
;; This is a sample text in German: Dies ist ein Beispieltext auf
;; Deutsch.
;;
;; Local IspellDict: german8
;;
;; Released into the public domain by Felix E. Klee
;; <felix.klee@inka.de>.
;; T1 German keyboard layout without AltGR and without <> (see
;; <https://emacs.stackexchange.com/a/47487/5327>):
(quail-define-package
"german-t1" "German" "DE1" t
"German (Deutsch) input method simulating the T1 layout"
nil t t t t nil nil nil nil nil t)
;; ^° 1! 2" 3§ 4$ 5% 6& 7/ 8( 9) 0= ß? ´`
;; qQ wW eE rR tT zZ uU iI oO pP üÜ +*
;; aA sS dD fF gG hH jJ kK lL öÖ äÄ #'
;; yY xX cC vV bB nN mM ,; .: -_
(quail-define-rules
(">" ?:)
("<" ?\;)
("`" ?^)
("~" ?°)
("°" ?°)
("-" ?ß)
("=" ?\´)
("`" ?\])
("y" ?z)
("[" ?ü)
("]" ?+)
(";" ?ö)
("'" ?ä)
("\\" ?#)
("z" ?y)
("/" ?-)
("@" ?\")
("#" ?§)
("^" ?&)
("&" ?/)
("*" ?\()
("Y" ?Z)
("(" ?\))
(")" ?=)
("_" ??)
("+" ?`)
("~" ?})
("{" ?Ü)
("}" ?*)
(":" ?Ö)
("\"" ?Ä)
("|" ?\')
("Z" ?Y)
("?" ?_)
)
;; Quick switching between languages for editing:
(defun my--edit-in-language (keep-dictionary new-dictionary
new-typo-language new-input-method)
(if new-input-method
(activate-input-method new-input-method)
(deactivate-input-method))
(if (bound-and-true-p typo-mode)
(typo-change-language new-typo-language))
(if (not keep-dictionary)
(ispell-change-dictionary new-dictionary)))
(defun my-edit-in-greek (&optional keep-dictionary)
(interactive)
(my--edit-in-language keep-dictionary "el_GR"
"French" 'greek))
(defun my-edit-in-spanish (&optional keep-dictionary)
(interactive)
(my--edit-in-language keep-dictionary "castellano8"
"English" 'spanish-keyboard))
(defun my-edit-in-french (&optional keep-dictionary)
(interactive)
(my--edit-in-language keep-dictionary "francais"
"French" 'french-azerty))
(defun my-edit-in-german (&optional keep-dictionary)
(interactive)
(my--edit-in-language keep-dictionary "german8"
"German" 'german-t1))
(defun my-edit-in-american (&optional keep-dictionary)
(interactive)
(my--edit-in-language keep-dictionary "american"
"English" nil))
(define-key global-map (kbd "C-M-0") 'my-edit-in-german)
(define-key global-map (kbd "C-M-1") 'my-edit-in-american)
(define-key global-map (kbd "C-M-2") 'my-edit-in-spanish)
(define-key global-map (kbd "C-M-3") 'my-edit-in-greek)
;; Sets up helpers for working with texts in different languages
;; (without changing dictionary):
(defun my-set-up-language-helpers ()
(flyspell-mode)
(if (boundp 'typo-mode)
(typo-mode))
(let ((d ispell-local-dictionary))
(cond
((not (bound-and-true-p d)) (my-edit-in-american))
((string-match "^\\(american\\|en_\\)" d)
(my-edit-in-american t))
((string-match "^\\(german\\|de_\\)" d)
(my-edit-in-german t))
((string-match "^\\(french\\|fr_\\)" d)
(my-edit-in-french t))
((string-match "^\\(castellano\\|es_\\)" d)
(my-edit-in-spanish t))
((string-match "^\\(el_\\)" d)
(my-edit-in-greek t))
(t (my-edit-in-american t)))))
;; Quick flyspell:
(global-set-key (kbd "<f8>") 'flyspell-buffer)
;; Support for editing plain text files:
(add-hook 'text-mode-hook 'my-set-up-language-helpers)
;; Basic customizations for spell checking:
(custom-set-variables
'(ispell-program-name "hunspell")
'(ispell-dictionary "american")
'(ispell-local-dictionary-alist
(quote
(("american" "[[:alpha:]]" "[^[:alpha:]]" "[']" t
("-d" "en_US")
nil iso-8859-1)
("german8" "[[:alpha:]]" "[^[:alpha:]]" "[']" t
("-d" "de_DE")
"~tex" iso-8859-1)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment