我首次涉足的Emacs Lisp的离奇世界是一个函数,它接受两个字符串,并与海誓山盟交换他们:互动的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)))))
这工作 - 但我m卡在以下内容:
- 如何在每次更换前提示用户确认? (我不能得到
perform-replace
工作) - 如何逃避字符串
a
和b
所以他们不会被解释为正则表达式,如果他们包含任何正则表达式字符?
编辑:我已经使用了一段时间的最后复制pastable代码:
(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))))
)))
不幸的是,它并不突出像我搜索确实在页面上即将开始的比赛。