简体   繁体   English

给区域搜索emacs

[英]giving region to search in emacs

Is there a way to provide a region to isearch? 有没有办法为isearch提供一个区域?

I want to overload isearch so that: 我想重载isearch以便:

Cs -> 1. isearch as usual if no region is selected, OR 2. isearch with region as to-be-used-for-search string Cs - > 1.如果没有选择区域则像往常一样,或者2.将区域作为搜索字符串用于搜索字符串

example: i 'select' the words "no exit", then Cs should search the buffer for "no exit". 例如:我'选择'单词“no exit”,然后Cs应该在缓冲区中搜索“no exit”。 In a way this can be done with Cs Cw Cw when the cursor is at the beginning of "no". 在某种程度上,当光标位于“否”的开头时,可以用Cs Cw Cw完成。 I wonder if there is a way to reverse this, so first select (some how) and then use the selection for searching 我想知道是否有办法扭转这种情况,所以首先选择(一些如何),然后使用选择进行搜索

You may find the commands narrow-to-region and widen useful, do Ch f narrow-to-region RET and Ch f widen RET to know more 您可能会发现命令narrow-to-regionwiden有用的,做 ˚F narrow-to-region RETCh˚F widen RET了解更多

UPDATE UPDATE

Here is some quick and dirty code to do what you want, I have not handled some cases for simplicity but this should give an idea 这里有一些快速而又脏的代码来做你想要的,我没有处理一些简单的情况,但这应该给出一个想法

(add-hook 'isearch-mode-end-hook (lambda ()
                                   (when (buffer-narrowed-p)
                                     (widen))))

(defun my-isearch(&optional start end)
  (interactive "r")
  (when (region-active-p)
      (narrow-to-region start end))
    (call-interactively 'isearch-forward-regexp))

UPDATE 2 更新2

Try the following function, this should do what you want 尝试以下功能,这应该做你想要的

(defun my-isearch(&optional start end)
  (interactive "r")
  (if (region-active-p)
      (progn
        (let ((string (buffer-substring-no-properties start end)))
          (deactivate-mark)
          (isearch-resume string nil nil t string nil)))
    (call-interactively 'isearch-forward-regexp)))

This is exactly what is available with Isearch+ ( isearch+.el ). 这正是Isearch +isearch+.el )提供的功能。 Whether searching is restricted to the active region is controlled by Boolean option isearchp-restrict-to-region-flag . 搜索是否仅限于活动区域由布尔选项isearchp-restrict-to-region-flag

You can use Cx n (command isearchp-toggle-region-restriction ) during search to toggle this behavior. 您可以在搜索期间使用Cx n (命令isearchp-toggle-region-restriction )来切换此行为。

A second Boolean option, isearchp-deactivate-region-flag , controls whether the region is to be deactivated during search (eg, so you can better see the search region). 第二个布尔选项isearchp-deactivate-region-flag控制在搜索期间是否要停用该区域(例如,这样您可以更好地查看搜索区域)。

Both options are true by default (search is restricted to the active region, and the region is deactivated during search). 默认情况下,这两个选项均为true(搜索仅限于活动区域,搜索期间区域处于停用状态)。

(For Isearch to be limited to the active region in Info , you must also use library Info+ ( info+.el ). (对于Isearch仅限于Info中的活动区域,您还必须使用库Info +info+.el )。


UPDATE UPDATE

Well, it was exactly what you described initially . 嗯,这最初描述的到底是什么。 Your later reply to comments shows that you do not in fact want to search the text in the region ; 您之后对评论的回复表明您实际上并不想搜索 该地区 的文字 ; you want to search for the text in the region , elsewhere in the buffer. 你要搜索 的区域中的文本 ,其他地方的缓冲区。 This does the former, not the latter. 这是前者,而不是后者。

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

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