Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let Emacs display a different character from that actually stored?

Tags:

emacs

elisp

I want to implement dynamical text replacement (only the display is replaced, the actual stored file is not replaced) for Emacs, using Elisp.

For example, in LaTeX documents, I want to type \alpha, and let Emacs display it just as α, so it is easier to read. But in the result .tex file, I still want \alpha, instead of α to be saved. (Remark: I could use XeTeX or LuaTeX myself to support UTF-8 directly. But for the reason of collaboration and journal requirements, I don't want the UTF-8 characters to be directly saved in the .tex files. Alternatively I could use preview in AUCTeX. But that doesn't help when I am editing the formula)

An existing example is in org-mode, when we type [[link][name]], right after typing the last ], the displayed text is replaced by just the name, with hyperlink. On the other hand, when saving this file, the saved content is the original [[link][name]], different from the displayed one.

Any ideas how this could be implemented?

PS: The Display Specs That Replace The Text section of Emacs manual goes close. However, I need to specify the start and end points, instead of the desired string for the replacements. This means I need to do search after every user input to decide the start and end points. This looks unrealistic due to performance and complexity of algorithm.

like image 870
Yi Wang Avatar asked Nov 28 '12 07:11

Yi Wang


1 Answers

One way to do this is to add font lock keywords for the relevant modes, and to use compose-region to display the new glyph in place of the old string:

(font-lock-add-keywords
 'latex-mode `(("\\(\\\\alpha\\)"
                (0 (progn (compose-region (match-beginning 1)
                                          (match-end 1) "α")
                          nil)))))

Please also note that org-mode has a feature of this kind already built-in, not only for links as you mentioned, but also for LaTeX-like entities:

(setq org-pretty-entities t)
like image 169
François Févotte Avatar answered Sep 27 '22 22:09

François Févotte