本文介绍了是否可以在Emacs中的isearch-forward之前预处理输入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果在 isearch-forward 提示符后键入 foo spam ,我该如何建议 isearch.el 中的某些函数将字符串转换为 foo [。?:,!]?[\n] spam 之前搜索?基本上,我想搜索文本中的两个相邻单词。我知道我可以使用 isearch-forward-regexp 来做到这一点,但是直接键入regexp既痛苦又容易出错,而且我有很多此类字符串可以搜索。我试图了解 isearch.el 中的代码,但失败了。希望一些Emacs专家可以帮助我。谢谢。

For example, if I type "foo spam" after the prompt of isearch-forward, how can I advice some functions in isearch.el to convert the string to "foo[.?:,!]?[ \n]spam" before search? Basically I want to search for the two adjacent words in the text. I know I can use isearch-forward-regexp to do that, but it's painful and error-prone to type out the regexp directly and I have many strings of that kind to search. I tried to understand code in isearch.el but failed. hope some Emacs gurus could help me out. Thanks.

推荐答案

在这里遇到了一个难题:

Here's a rough crack at it:

(defun isearch-transform-string ()
  (interactive)
  (let* ((string (replace-regexp-in-string
                  "foo" "bar" isearch-string)))
    (setq isearch-string string
          isearch-message (mapconcat 'isearch-text-char-description string ""))
    (isearch-search-and-update)))

(define-key isearch-mode-map (kbd "C-c C-t") 'isearch-transform-string)

我认为您必须将其绑定到 isearch-mode-map 中的键;我认为Mx在 isearch-mode 下工作不正确。

I think you have to bind it to a key in isearch-mode-map; I don't think M-x works quite right in isearch-mode.

如果您需要更多电源,则可以变量 isearch-search-fun-function ,您可以将其设置为返回函数的函数。返回的函数用于查找下一个匹配项。

If you require more power, there is a variable isearch-search-fun-function, which you can set to a function that returns a function. The returned function is used to find the next match.

这篇关于是否可以在Emacs中的isearch-forward之前预处理输入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 18:51