gpt4 book ai didi

emacs - 交互式 Emacs Lisp 功能,可相互交换两个单词

转载 作者:行者123 更新时间:2023-12-04 20:25:59 24 4
gpt4 key购买 nike

我第一次涉足 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 工作)
  • 如何转义字符串 ab所以如果它们包含任何正则表达式字符,它们就不会被解释为正则表达式?

  • 编辑:我使用了一段时间的最终可复制粘贴代码是:
    (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-n-p第一个:(when (y-or-n-p "Swap?") do stuff
    regexp-quote第二个:(regexp-quote your-string)

    关于emacs - 交互式 Emacs Lisp 功能,可相互交换两个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/768243/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com