2008-09-18 228 views
4

Emacs Lisp有replace-string,但沒有replace-char。我想(這個字符是十六進制的53979 Emacs的代碼)與普通的ASCII引號替換「印刷」彎引號,並且我可以這樣做:Emacs Lisp中的替換字符?

(replace-string (make-string 1 ?\x53979) "'") 

我認爲這將是更好地與replace-char

這樣做的最好方法是什麼?

回答

5

爲什麼不直接使用

(replace-string "\x53979" "'") 
作爲替換字符串的文件中建議

(while (search-forward "\x53979" nil t) 
    (replace-match "'" nil t)) 

1

這對replace-char來說肯定會更好。任何方式來改善我的代碼?

它是否真的慢到它重要的程度?我的elisp通常效率低下,我永遠都不會注意到。 (我只有在你正在構建下一個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許可的現在。

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(運行它)。

我發現捲髮報價總是六角2019,你確定它是53979你的情況?您可以使用C-u C-x =來檢查緩衝區中的字符。

我認爲你可以在my-replace-smart-quotes的定義中寫「'」來代替「\ x2019」並且沒問題。這只是爲了安全起見。

3

這是我在elisp的替換字符的方式:

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

給出:

"John’s" 

注意這個函數不接受字符的字符串。第一個和第二個參數必須是文字字符(使用?表示法或string-to-char)。

另請注意,如果可選inplace參數非零,則此函數可能具有破壞性。