我首次涉足的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))))
)))
不幸的是,它並不突出像我搜索確實在頁面上即將開始的比賽。