【问题标题】:Undo buffer-search in Emacs在 Emacs 中撤消缓冲区搜索
【发布时间】:2014-01-01 21:28:11
【问题描述】:

在当前缓冲区中执行(re-search-forward str) 之后,在某些情况下最好有一个简单的方法返回到先前的缓冲区位置。对于缓冲区更改,行为应该类似于(undo)。因此,如果我向前进行两次搜索,首先从位置 A 到 B,然后从 B 到 C,我想按一个键后退一步(从 C 到 B),再次按该键将使我处于一个..

【问题讨论】:

  • 不确定这是否能回答你的问题,但由于我经常使用正则表达式搜索,我将C-sC-r 绑定到isearch-forward-regexpisearch-backward-regexp 然后来回移动只是一个击中正确的组合键的问题。不按RET,即不退出搜索模式,C-g 将取消搜索并返回搜索开始的位置。如果我确实在其中一种找到的模式上按了RET,那么C-U SPC 也会带我回到我开始搜索的地方。

标签: emacs elisp


【解决方案1】:

如果你在 Lisp 代码中使用 re-search-forward(如果你使用它,你可能应该使用它,即使它是一个命令),那么 do 设置标记是为了能够回到你的起点。

相反,只需将起始位置 ((point)) 保存为变量beg,然后使用goto-char beg

(elisp) The Mark看到这一段:

 Novice Emacs Lisp programmers often try to use the mark for the
 wrong purposes.  The mark saves a location for the user's
 convenience.  An editing command should not alter the mark unless
 altering the mark is part of the user-level functionality of the
 command.  (And, in that case, this effect should be documented.)
 To remember a location for internal use in the Lisp program, store
 it in a Lisp variable.  For example:

      (let ((beg (point)))
        (forward-line 1)
        (delete-region beg (point))).

【讨论】:

  • 感谢您的回答,但我认为这不是我想要的。我正在研究主要模式或主要模式的增强,因此可以选择通过按下键盘快捷键向前搜索该点所指的“功能”的定义。现在稍后,用户可能会决定通过按下另一个键盘快捷键来返回原点。这就是我想要实现的目标。。
  • 在这种情况下,只需设置标记。用户可以使用标记环正常导航回该位置。或者,如果您想要一个直接移动到该特定位置的命令,则将该位置保存在全局变量中...
【解决方案2】:

有了这个

(global-set-key
 (kbd "M-p")
 (lambda()(interactive) (set-mark-command 4)))

我可以通过几个C-M-s一个一个地向后跳。

请注意,这适用于isearch-forward-regexp,不适用于普通 re-search-forward(这个没有设置标记)。 但是使用 elisp 没问题 - 只需在之前致电 push-mark re-search-forward.

【讨论】:

  • 谢谢!我会试试这个。@ 987654327@ 的参数的目的是什么? (即:号码4
  • 我不知道为什么 4. 任何数值都以相同的方式工作。如果您愿意,请使用 42。
  • Ok:) 我刚刚找到了show-marks 模式(emacswiki.org/emacs/show-marks.el)。也许这可以用于我的目的?
  • Håkon Hægland, abo-abo:您应该注意4 一个特殊值(不一定适用于此功能,但一般而言)。当使用C-u 时(一次,没有明确的数字),它传递原始值(4),当解释为数字前缀arg 时,它只是4。见C-h i g(elisp) Prefix Command Arguments。在代码中传递4'(4) 来触发函数的前缀参数行为的情况并不少见。
  • 另外,您可能会发现C-u C-SPC(或C-u C-@)足以满足您的需求。这与接受的答案相同,但使用现有绑定。
【解决方案3】:

总而言之,以下似乎可行:

(defun my-search-fun (str)
  (interactive)
  (push-mark)
  (beginning-of-buffer)
  (re-search-forward str))

(defun my-undo-search ()
  (interactive)
  (pop-mark)
  (goto-char (mark))

【讨论】:

    猜你喜欢
    • 2013-05-19
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多