简体   繁体   English

Emacs - 如何在搜索(Cr)而不是查询替换(M-%)时进入递归编辑模式?

[英]Emacs - How to enter recursive edit mode when searching (C-r) instead of query-replace (M-%)?

I know how to enter recursive edit mode when query-replace ( M-% ), but sometimes I really don't want to modify the code instead I'm just in searching. 我知道如何在查询替换( M-% )时进入递归编辑模式,但有时我真的不想修改代码而只是在搜索中。 Such as when I was reading code, and from one point I saw a function call, then I want to jump to the declaration of the fucntion ( Cr Enter PageList::Init), when I have read the declaration I want to jump back to the calling of the function. 比如当我在读代码时,从一个点我看到一个函数调用,然后我想跳转到函数的声明( Cr Enter PageList :: Init),当我读完我要跳回的声明时调用函数。

How to enter recursive edit mode when searching (not only when query-replace)? 搜索时如何进入递归编辑模式(不仅在查询替换时)?

You enter a recursive edit by calling recursive-edit , but there is no default binding which invokes this anywhere (and doing so arbitrarily might be hazardous). 您通过调用recursive-edit进入递归编辑,但没有默认绑定在任何地方调用它(并且任意地这样做可能是危险的)。

In the case of query-replace there is some special-case handling which goes on when you type Cr which saves the current match data/point/mark/buffer/window configuration before entering the recursive edit, to ensure that it can restore things when you exit from it. query-replace的情况下,存在一些特殊情况处理,当您键入Cr时,它会在进入递归编辑之前保存当前匹配数据/点/标记/缓冲区/窗口配置,以确保它可以在恢复时恢复你退出它。

This need to wrap some situation-specific custom handling around calls to recursive-edit is very common to the existing use-cases, so there might not be a safe way to invoke it generally. 这需要围绕对recursive-edit调用包装一些特定于情境的自定义处理对于现有的用例非常常见,因此通常可能没有一种安全的方法来调用它。

My guess is that custom handling would certainly also be needed to support it during an isearch, and I see no such facility. 我的猜测是,在isearch期间,当然也需要自定义处理以支持它,我看不到这样的设施。

Do note that Emacs pushes point to the mark ring when you begin an isearch; 请注意,当你开始一个isearch时,Emacs会指向标记环; so for your specific use-case you would simply jump to & pop the mark with Cu C-SPC 因此,对于您的特定用例,您只需跳转到Cu C-SPC并弹出标记即可

You can open a recursive edit while you are searching, if you use Isearch+ . 如果您使用Isearch + ,则可以在搜索时打开递归编辑。 Then, Cx o is bound during Isearch to command isearchp-open-recursive-edit : 然后,在Isearch期间绑定Cx o以命令isearchp-open-recursive-edit

Invoke the editor command loop recursively, during Isearch. 在Isearch期间递归调用编辑器命令循环。 Use CMc to end the recursive edit and resume searching from there. 使用CMc结束递归编辑并从那里继续搜索。 Or use abort-recursive-edit to exit the recursive edit and cancel the previous search. 或者使用abort-recursive-edit退出递归编辑并取消之前的搜索。

If you do not want to use Isearch+ then this is all you need: 如果您不想使用Isearch +,那么这就是您所需要的:

(defun isearchp-open-recursive-edit ()
  "Invoke the editor command loop recursively, during Isearch.
Use `\\[exit-recursive-edit]' to end the recursive edit and resume searching from there.
Or use `abort-recursive-edit' to exit the recursive edit and cancel the previous search."
  (interactive)
  (with-isearch-suspended (recursive-edit))))

(define-key isearch-mode-map "\C-xo" 'isearchp-open-recursive-edit)

If you are asking about recursive editing generally , and not just during search, then the answer is command recursive-edit . 如果您一般都在询问递归编辑,而不仅仅是在搜索过程中,那么答案就是命令recursive-edit Just use Mx recursive-edit to open a recursive edit. 只需使用Mx recursive-edit即可打开递归编辑。 You can also bind this command to a key, of course. 当然,您也可以将此命令绑定到密钥。

(If the minibuffer is active, then you normally cannot use Mx recursive-edit . In that case, bind a key in the minibuffer keymaps to recursive-edit . Or you can set option enable-recursive-minibuffers to non- nil , to be able to invoke Mx recursive-edit from a minibuffer.) (如果迷你缓冲区处于活动状态,那么你通常不能使用Mx recursive-edit 。在这种情况下,将迷你缓冲区键盘中的一个键绑定到recursive-edit 。或者你可以将选项enable-recursive-minibuffers设置为非nil ,能够从迷你缓冲区调用Mx recursive-edit 。)

From your workflow description, I'm not sure you really need recursive edit mode. 从您的工作流程描述中,我不确定您是否真的需要递归编辑模式。

When you do a search in emacs, the current point is saved. 在emacs中进行搜索时,将保存当前点。 On my system Cr is reverse isearch. 在我的系统上,Cr是反向的。 When I hit it, I'm prompted for the search string and hit enter. 当我点击它时,我被提示输入搜索字符串并按Enter键。 This takes me to the first 'hit'. 这让我第一次'打'。 Hitting Cr againi takes me the next hit etc. Once you find the text you want, provided you don't hit enter, you can read the code and then hit Cg to cancel the search. 点击Cr againi会让我接下来打击等等。一旦找到了你想要的文本,如果你没有按回车,你可以阅读代码,然后点击Cg取消搜索。 This will jump your cursor back to the point where you started the search. 这会将光标跳回到您开始搜索的位置。 This is fine provided you can see all the code you want to read on screen. 这很好,只要您可以在屏幕上看到您想要阅读的所有代码。

When you can't read all the code on screen and you need to move around in the buffer, you have to hit enter. 当您无法读取屏幕上的所有代码并且需要在缓冲区中移动时,您必须按Enter键。 Once you do this, you have lost the saved point and I suspect this is where you thought of using a recursive edit mode so that you can jump back once you finish. 一旦你这样做,你就失去了保存点,我怀疑这是你想要使用递归编辑模式的地方,这样你就可以在完成后跳回去。 However, this won't work quite as you want because as soon as you hit enter to select the searched for string, you will come out of recursive mode and lose the saved point. 但是,这不会按照您的要求运行,因为只要您按Enter键选择搜索的字符串,您就会退出递归模式并丢失保存的点。

There are a couple of ways to fix this workflow to achieve what you want. 有几种方法可以修复此工作流程以实现您的目标。 In fact, your workflow is quite a common requirement. 实际上,您的工作流程是一个非常常见的要求。 Because of this, many programming modes already have this functionality built in. Therefore, the first thing to do would be to ensure your mode doesn't already have this - it is probably called something like jump to definition or similar. 因此,许多编程模式已经内置了这个功能。因此,首先要做的是确保你的模式还没有这个 - 它可能被称为跳转到定义或类似的东西。

If your mode doesn't have this support, then you can get what you want by saving the point and then jumping back to it later. 如果您的模式没有此支持,那么您可以通过保存该点然后稍后再回到它来获得所需的内容。 This is a really handy technique. 这是一个非常方便的技术。 All you need to do is C-SPACE twice. 您需要做的就是C-SPACE两次。 Then later, after moving the point to a new locaiton, you can just do a Cu C-SPACE to jump back to that point. 然后,在将点移动到新的位置之后,您可以执行Cu C-SPACE跳回到该点。 From the emacs manual 来自emacs手册

Instead of setting the mark in order to operate on a region, you can also use it to “remember” a position in the buffer (by typing 'C-SPC C-SPC'), and later jump back there (by typing 'Cu C-SPC'). 您也可以使用它来“记住”缓冲区中的位置(通过键入“C-SPC C-SPC”),然后跳回那里(通过键入'Cu'而不是设置标记以便在区域上操作C-SPC')。 *Note Mark Ring::, for details. * Note Mark Ring ::,详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM