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

天涯浪子 提交于 2019-12-06 23:48:22

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/13600160/how-to-let-emacs-display-a-different-character-from-that-actually-stored

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!