省流版
将 TeXmacs 的 .tm
文件中的 unicode 编码 (如 \<#HEXD\>
还原为正常文字显示, 见 gist: tmd.lisp
背景故事
之前的一些课程笔记, 作业以及论文是用 TeXmacs 进行编辑的, 最近想要读取但是发现软件被删除了 (用 mac 的黄金储存导致的), 所以写了一个简单的程序来让文件容易阅读.
假如你还不知道什么是 TeXmacs, 可以理解为一个即像是 Word 一样所见即所得, 又像是 LaTeX 一样可以编辑优美数学公式 (甚至 TeXmacs 的数学公式因为所见即所得 + 有很好用的快捷键可以和手写公式速度持平甚至更快 前提是你比较熟练), 也可以安装各种插件进行拓展的一个开源编辑器.
那么为什么现在不用了呢? 因为跑去用 Emacs 和 org-mode 了. 虽然数学输入速度和美观程度可能稍微弱于 TeXmacs, 但是好处是容易编写插件以及是个臭写 Lisp 的.
原理解释
理论上来说, TeXmacs 的文件 .tm
就是一个 XML 文件, 其中的转义后的 unicode 可以用正则表达式 \\<#[a-fA-F0-9]{4}\\>
提取, 于是只需要实现:
- 提取 unicode
ppcre:do-matches
- 将 unicode code
parse-integer
-> unicode char code-char
- 其他保持不变
(defun unescape-unicode-to-stream (string stream)
"Unescape unicode to `stream'. "
(let ((start 0))
(ppcre:do-matches (match-start match-end *unicode-escape-regexp* string)
(write-string (subseq string start match-start) stream)
(let ((code (parse-integer
(subseq string (+ 3 match-start) (- match-end 2))
:radix 16)))
(write-char (code-char code) stream))
(setf start match-end))
(write-string (subseq string start) stream)))
核心代码如上
效果
