是否在Emacs Lisp中替换字符?

是否在Emacs Lisp中替换字符?,emacs,elisp,Emacs,Elisp,Emacs Lisp有replace string,但没有replace char。我想用常规ASCII引号替换“排版”卷曲引号(此字符的Emacs代码为十六进制53979),我可以使用以下方法: (replace-string (make-string 1 ?\x53979) "'") 我认为用替换char会更好 最好的方法是什么 使用replace char肯定会更好。有没有办法改进我的代码 它真的慢到了重要的程度吗?我的elisp通常效率低得离谱,我从未注意到。(不过,如果您正在使用它构

Emacs Lisp有
replace string
,但没有
replace char
。我想用常规ASCII引号替换“排版”卷曲引号(此字符的Emacs代码为十六进制53979),我可以使用以下方法:

(replace-string (make-string 1 ?\x53979) "'")
我认为用
替换char
会更好

最好的方法是什么

使用replace char肯定会更好。有没有办法改进我的代码

它真的慢到了重要的程度吗?我的elisp通常效率低得离谱,我从未注意到。(不过,如果您正在使用它构建下一个MS live搜索,我只将其用于编辑器工具。)

此外,阅读文档:

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授权的。

为什么不直接使用

(replace-string "\x53979" "'")

如替换字符串文档中所建议的那样?

这是怎么回事

(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示例代码(来自博客等)粘贴到scratch缓冲区,然后立即按C-M-\(正确缩进),然后按M-x my replace smart quotes(修复smart quotes),最后按C-x C-e(运行它)

我发现卷曲报价总是HEXA2019,你确定你的情况是53979吗?您可以使用C-u C-x=检查缓冲区中的字符


我认为您可以在“替换智能引号”的定义中用“'”代替“\x2019”,这样就可以了。为了安全起见。

这是我在elisp中替换字符的方法:

(subst-char-in-string ?' ?’ "John's")
给出:

"John’s"
请注意,此函数不接受字符作为字符串。第一个和第二个参数必须是文字字符(使用
表示法或
字符串到字符

还要注意,如果可选的
inplace
参数为非nil,则此函数可能具有破坏性

"John’s"