【问题标题】:emacs isearch with automatic application of a regex between charactersemacs isearch 在字符之间自动应用正则表达式
【发布时间】:2016-10-22 09:16:30
【问题描述】:

我想在 Emacs 的 isearch-forward 函数中有一个钩子,使它在搜索字符串时自动在输入字符之间应用正则表达式。例如,我想将此正则表达式设置为[-=<>]。如果我现在在 isearch 中输入foobar,它应该匹配foo<barfo=ob=>arf-o-o-b-a-r 等。

这样的功能是否已经可用?我调查了 ELPA 和 MELPA,但没有成功。万一这不可用,并且由于我的 Elisp 能力非常有限:如何实现?

【问题讨论】:

  • 您对“忽略”一词的使用让我感到困惑。听起来您希望 Emacs 自动调用 isearch-forward-regexp 并在原始模式的每个字符之间添加您的模式。这些示例非常清楚地表明了预期的结果,但措辞似乎不合适。
  • 对。希望现在好多了。

标签: regex emacs hook isearch


【解决方案1】:

好的,我在检查 Emacs 的hexl.el 后自己找到了解决方案。

这是代码。

(defun my-isearch-function ()
  "Make isearch skip characters -=<> while searching."
  (if (not isearch-regexp)
      (lambda (string &optional bound noerror count)
        (funcall
         (if isearch-forward
             're-search-forward
           're-search-backward)
         (mapconcat (lambda (c) (regexp-quote (string c))) string
                    "\\(?:[-=<>]*\\)?")
         bound
         noerror
         count))
    (isearch-search-fun-default)))


(defun toggle-my-isearch ()
  "Toggle my search mode.
If activated, incremental search skips characters -=<> while
searching.

For example, searching `foobar' matches `foo-bar' or `f-o-o=b<a>r'."
  (interactive)
  (if (eq isearch-search-fun-function 'isearch-search-fun-default)
      (progn
        (setq isearch-search-fun-function 'my-isearch-function)
        (message "my isearch on"))
    (setq isearch-search-fun-function 'isearch-search-fun-default)
    (message "my isearch off")))


(global-set-key (kbd "s-s") 'toggle-my-isearch)

【讨论】:

    【解决方案2】:

    我编写了一个名为flex-isearch 的包,它基本上在搜索字符串的每个字符之间插入“.*”(它比这更复杂一些)并切换到正则表达式搜索。当 isearch 失败时,它会自动执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多