题
Emacs Lisp 有 replace-string
但没有 replace-char
. 。我想用常规 ASCII 引号替换“印刷”大引号(该字符的 Emacs 代码是十六进制 53979),我可以这样做:
(replace-string (make-string 1 ?\x53979) "'")
我认为这样会更好 replace-char
.
做这个的最好方式是什么?
解决方案
为什么不直接使用
(replace-string "\x53979" "'")
或者
(while (search-forward "\x53979" nil t)
(replace-match "'" nil t))
按照替换字符串文档中的建议?
其他提示
这是我在 elisp 中替换字符的方法:
(subst-char-in-string ?' ?’ "John's")
给出:
"John’s"
请注意,此函数不接受字符作为字符串。第一个和第二个参数必须是文字字符(使用 ?
符号或 string-to-char
).
另请注意,如果可选的,此功能可能具有破坏性 inplace
参数非零。
如果使用 Replace-char 的话肯定会更好。有什么方法可以改进我的代码吗?
它真的慢到了重要的程度吗?我的 elisp 通常效率低得可笑,但我从来没有注意到。(不过,我只将它用于编辑器工具,YMMV,如果你正在用它构建下一个 MS 实时搜索。)
另外,阅读文档:
This function is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
(while (search-forward "’" nil t)
(replace-match "'" nil t))
这个答案现在可能是 GPL 许可的。
那这个呢
(defun my-replace-smart-quotes (beg end)
"replaces ’ (the curly typographical quote, unicode hexa 2019) to ' (ordinary ascii quote)."
(interactive "r")
(save-excursion
(format-replace-strings '(("\x2019" . "'")) nil beg end)))
一旦你在 dotemacs 中拥有了它,你就可以将 elisp 示例代码(来自博客等)粘贴到你的临时缓冲区中,然后立即按 C-M-\ (以正确缩进),然后按 M-x my-replace-smart-quotes (以修复智能引号),最后是 C-x C-e(运行它)。
我发现卷曲引号始终是 hexa 2019,您确定在您的情况下是 53979 吗?您可以使用 C-u C-x = 检查缓冲区中的字符。
我认为你可以在 my-replace-smart-quotes 的定义中写“’”代替“\x2019”,就可以了。这只是为了安全起见。
不隶属于 StackOverflow