交互式Emacs口齿不清的功能,以交换两个单词的每一个其他的
题
我的第一次进军的古怪的世界的emacs的舌头是一个函数,它需要两个串和全部门办法与他们海誓山盟:
(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
所以他们不得解释为regex如果他们有包含任何regex字?
编辑:最终复制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))))
)))
不幸的是,它没有突出显示即将到来匹配的网页喜欢我-搜索。
解决方案
使用 y-or-n-p
第一: (when (y-or-n-p "Swap?") do stuff
和 regexp-quote
第二: (regexp-quote your-string)
其他提示
regexp-quote
是 已经提到的.
作为对该确认,如果你要问的用户之前 每 替换,你可以选择 query-replace-regexp
这不正是你想要的。
(你仍然可以处理Emacs的builtin transponse功能.)
不隶属于 StackOverflow