我第一次涉足 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 那样突出显示页面上即将到来的匹配项。