[英]Interactive Emacs Lisp function to swap two words with each other
我第一次進入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那樣突出顯示頁面上即將到來的匹配。
使用y-or-np
作為第一個:( (when (y-or-np "Swap?") do stuff
和regexp-quote
第二個:( (regexp-quote your-string)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.