【发布时间】:2009-04-20 13:27:22
【问题描述】:
我第一次进入古怪的 emacs lisp 世界是一个函数,它接受两个字符串并将它们相互交换:
(defun swap-strings (a b)
"Replace all occurances of a with b and vice versa"
(interactive "*sFirst Swap Word: \nsSecond Swap Word: ")
(save-excursion
(while (re-search-forward (concat a "\\|" b) nil t)
(if (equal (match-string 0) a)
(replace-match b)
(replace-match a)))))
这可行 - 但我坚持以下几点:
- 每次更换前如何提示用户确认? (我无法让
perform-replace工作) - 如何对字符串
a和b进行转义,这样如果它们包含任何正则表达式字符,它们就不会被解释为正则表达式?
编辑:我已经使用了一段时间的最终可复制粘贴代码是:
(defun swap-words (a b)
"Replace all occurances of a with b and vice versa"
(interactive "*sFirst Swap Word: \nsSecond Swap Word: ")
(save-excursion
(while (re-search-forward (concat (regexp-quote a) "\\|" (regexp-quote b)))
(if (y-or-n-p "Swap?")
(if (equal (match-string 0) a)
(replace-match (regexp-quote b))
(replace-match (regexp-quote a))))
)))
很遗憾,它不会像 I-search 那样在页面上突出显示即将到来的匹配项。
【问题讨论】: