Emacs 区分大小写替换字符串

发布于 2024-10-24 08:48:26 字数 823 浏览 5 评论 0原文

我刚刚问了一个相关的问题(setq 问题),但它明显不同,所以我决定分支这个问题。

在我的 .emacs 文件中,我定义了一个与 replace-string 命令的键绑定:

(define-key global-map "\C-r" 'replace-string)

replace-string 执行基本的搜索和替换。假设搜索字符串的第一个字母是小写,如果 case-fold-searchnilreplace-string 进行区分大小写的搜索,否则进行不区分大小写的搜索。

问题在于 case-fold-search 控制“搜索”(如 search-forward 命令)和“搜索和替换”(就像 replace-string 命令)。

问题是如何使 JUST replace-string 命令(或 Cr 绑定的任何内容)区分大小写,而保留 >search-forward 默认情况下不区分大小写。

也许我需要将 case-fold-search 设置为 nil 只是为了 replace-string 命令,但我不知道如何这样做。

I just asked a related question (setq question) but it's distinctly different, so I decided to branch off with this question.

In my .emacs file, I define a key binding to the replace-string command:

(define-key global-map "\C-r" 'replace-string)

replace-string does basic search and replace. Assuming the first letter of the search string is lowercase, if the case-fold-search is nil then replace-string does case-sensitive search, otherwise it does case-insensitive search.

The problem is that case-fold-search controls the "case-sensitiveness" of both "search" (like the search-forward command) and "search and replace" (like the replace-string command).

The question is how do I make JUST the replace-string command (or anything C-r is bound to) case-sensitive, leaving the search-forward case-insensitive as it is by default.

Perhaps I would need to set case-fold-search to nil just for the replace-string command, but I'm not sure how to do that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰葑 2024-10-31 08:48:26

将其放入您的 .emacs 中:

(defadvice replace-string (around turn-off-case-fold-search)
  (let ((case-fold-search nil))
    ad-do-it))

(ad-activate 'replace-string)

这正是您所说的,将 case-fold-search 设置为 nil 只是为了 replace-string

事实上,这几乎正是 中的示例Emacs Lisp 参考手册

于 2021 年 11 月 2 日编辑: 如上面的链接所示,defadvice 不再是实现此目的的推荐方法。新的建议实施是

(defun with-case-fold-search (orig-fun &rest args)
  (let ((case-fold-search t))
    (apply orig-fun args)))

(advice-add 'replace-string :around #'with-case-fold-search)

Put this in your .emacs:

(defadvice replace-string (around turn-off-case-fold-search)
  (let ((case-fold-search nil))
    ad-do-it))

(ad-activate 'replace-string)

This does exactly what you said, set case-fold-search to nil just for replace-string.

In fact this is almost exactly the example in the Emacs Lisp reference manual.

Edit on 2021-11-02: as the link above indicates, defadvice is no longer the recommended way to implement this. The new recommended implementation would be

(defun with-case-fold-search (orig-fun &rest args)
  (let ((case-fold-search t))
    (apply orig-fun args)))

(advice-add 'replace-string :around #'with-case-fold-search)
勿挽旧人 2024-10-31 08:48:26

尝试这个方法,它不需要建议:

(global-set-key (kbd "C-r") 
    (lambda () 
      (interactive) 
      (let ((case-fold-search nil)) 
        (call-interactively 'replace-string))))

Try this method, which does not require advice:

(global-set-key (kbd "C-r") 
    (lambda () 
      (interactive) 
      (let ((case-fold-search nil)) 
        (call-interactively 'replace-string))))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文