Unfortunately using quail to convert a character from beyond the Basic Latin and Latin-1 Supplement blocks (i.e. anything with a codepoint above 255) is not possible.
Firstly, the quail-translation-keymap
, quail-simple-translation-keymap
and quail-conversion-keymap
(which are used depending on precisely which quail-based input method you use) are assigned only for characters with codepoints below 256 (see
here,
here and
here, respectively).
Secondly, and more importantly, an input-method-function
can only act on a single-byte characters (apart from the DEL character —
source). Since the main underlying way quail works, is by setting the input-method-function
to quail-input-method
(see in the definition of quail-activate
here), quail can't easily deal with other input characters.
To be fair, the main point of quail and the like is to facilitate inputting non-ascii characters with a "standard" keyboard and your use-case doesn't usually come up.
Solution without using quail
Define your own special minor mode map. For instance:
(define-minor-mode convert-unicode-to-latex-mode
"A minor mode to convert inserted Unicode characters into their
canonical LaTeX representation."
:lighter " cutl"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "α")
;; ;; Just using the string is probably preferable to a lambda
;; (lambda () (interactive) (insert "\\alpha"))
"\\alpha") ;; As suggested by Basil, in the comments
;; [...] define other keys
map))
Alternatively, if you want to do this globally, just do:
(global-set-key (kbd "α") "\\alpha")
Lookup character in an alist
This was intended as a workaround to using lambdas, when defining many mappings, but just using a macro ((define-key map (kbd char) "latex_string")
) is preferable. The method relies on looking up the last entered character in an alist and inserting based on that. Kept here for completeness.
The looping over the alist with dolist
could obviously be used with any of the methods (macro, lambda, lookup function).
(defvar convert-unicode-to-latex-alist
'(("α" . "\\alpha")
("β" . "\\beta"))
"Alist of mappings from Unicode to LaTeX.
Used by `convert-unicode-to-latex--insert'.")
(defun convert-unicode-to-latex--insert ()
"Insert LaTeX based on last entered Unicode character.
Looks up the character in `convert-unicode-to-latex-alist'."
(interactive)
(let* ((key (key-description (this-single-command-keys)))
(mapping (assoc key convert-unicode-to-latex-alist)))
(if mapping
(insert (cdr mapping))
(message "Character %s not found in alist" key))))
(define-minor-mode convert-unicode-to-latex-mode
"A minor mode to convert inserted unicode characters into their
canonical LaTeX representation.
See `convert-unicode-to-latex-alist' and
`convert-unicode-to-latex--insert'."
:lighter " cutl"
:keymap (let ((map (make-sparse-keymap)))
(dolist (mapping convert-unicode-to-latex-alist)
(let ((key (car mapping))
(latex (cdr mapping)))
;; (define-key map
;; (kbd key) latex)
(define-key map
(kbd key) #'convert-unicode-to-latex--insert)))
map))
C-\
andLaTeX-UTF-8
. It does not work. However if I substituteα
withaaa
, for instance, it does the substitution. So the problem must be with that unicode typed in the.emacs
file. Am I right?abbrev-mode
as(define-abbrev-table 'global-abbrev-table '( ("α" "\\alpha") ("β" "\\beta") )) (abbrev-mode 1)
.