Emacs Lisp replace-string
但没有replace-char
。我想用常规ASCII引号替换“印刷”卷曲引号(此字符的Emacs代码为十六进制53979),我可以这样做:
(replace-string (make-string 1 ?\x53979) "'")
我认为replace-char
会更好。
这样做的最佳方式是什么?
答案 0 :(得分:6)
为什么不使用
(replace-string "\x53979" "'")
或
(while (search-forward "\x53979" nil t)
(replace-match "'" nil t))
如replace-string文档中所建议的那样?
答案 1 :(得分:5)
这是我在elisp中替换字符的方式:
(subst-char-in-string ?' ?’ "John's")
给出:
"John’s"
请注意,此函数不接受字符作为字符串。第一个和第二个参数必须是文字字符(使用?
表示法或string-to-char
)。
另请注意,如果可选inplace
参数为非零,则此函数可能具有破坏性。
答案 2 :(得分:1)
使用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许可。
答案 3 :(得分:1)
这个怎么样?
(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示例代码(从博客等)粘贴到临时缓冲区,然后立即按CM- \(正确缩进)然后Mx my-replace-smart-quotes (修复智能引号),最后是Cx Ce(运行它)。
我发现卷曲的引用始终是hexa 2019,你确定在你的情况下它是53979吗?您可以使用C-u C-x =。
检查缓冲区中的字符我认为你可以在my-replace-smart-quotes的定义中用“'”代替“\ x2019”并且没问题。这只是为了安全起见。