【发布时间】:2017-07-07 03:39:01
【问题描述】:
Emacs Lisp 有replace-string,但没有replace-char。我想用常规的 ASCII 引号替换“印刷”花括号(这个字符的 Emacs 代码是十六进制 53979),我可以这样做:
(replace-string (make-string 1 ?\x53979) "'")
我认为replace-char 会更好。
最好的方法是什么?
【问题讨论】:
Emacs Lisp 有replace-string,但没有replace-char。我想用常规的 ASCII 引号替换“印刷”花括号(这个字符的 Emacs 代码是十六进制 53979),我可以这样做:
(replace-string (make-string 1 ?\x53979) "'")
我认为replace-char 会更好。
最好的方法是什么?
【问题讨论】:
这是我在 elisp 中替换字符的方式:
(subst-char-in-string ?' ?’ "John's")
给予:
"John’s"
请注意,此函数不接受字符作为字符串。第一个和第二个参数必须是文字字符(使用? 表示法或string-to-char)。
另请注意,如果可选的 inplace 参数不为零,则此函数可能具有破坏性。
【讨论】:
为什么不直接使用
(replace-string "\x53979" "'")
或
(while (search-forward "\x53979" nil t)
(replace-match "'" nil t))
按照替换字符串文档中的建议?
【讨论】:
用替换字符肯定会更好。有什么方法可以改进我的代码?
它真的慢到重要的程度吗?我的 elisp 通常效率低得可笑,我从来没有注意到。 (不过,我只将它用于编辑器工具,如果您正在使用它构建下一个 MS 实时搜索,则为 YMMV。)
另外,阅读文档:
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)))
一旦你的 dotmacs 中有这个,你可以将 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" 并且没问题。只是为了安全起见。
【讨论】: