简体   繁体   English

Sublime Text 2为Emacs提供“Goto Anything”(或即时搜索)?

[英]Sublime Text 2's “Goto Anything” (or instant search) for Emacs?

I tried out Sublime Text 2 recently, and I found Goto Anything superbly useful for navigating source code ( Ctrl-P file@symbol seems to work really well). 我最近尝试了Sublime Text 2 ,我发现Goto Anything非常适用于导航源代码( Ctrl-P文件@符号似乎工作得非常好)。 Is there something similar for Emacs? Emacs有类似的东西吗? Preferably something that just works, without a ton of custom elisp. 最好的东西只是工作,没有大量的定制elisp。

What I've tried so far: 到目前为止我尝试过的:

  1. I've seen Helm and Anything , but as far as I understand neither of them is capable of actual "instant" search (see edit below). 我见过HelmAnything 但据我所知,他们都不能进行实际的“即时”搜索 (见下面的编辑)。

  2. I've used multi-occur-in-matching-buffers , but it too seems unable to satisfy the "instant" criterion. 我已经使用了multi-occur-in-matching-buffers ,但它似乎也无法满足“即时”标准。

  3. imenu / idomenu works well for single files, but doesn't work across files. imenu / idomenu适用于单个文件,但不适用于文件。

I currently use #2 and #3 together, as a poor substitute for Goto Anything. 我目前一起使用#2和#3,作为Goto Anything的不良替代品。

If not an exact clone of Goto Anything, then I could make do with a naive instant search solution (one that searches for a given string across all open buffers and displays results dynamically). 如果不是Goto Anything的精确克隆,那么我可以使用天真的即时搜索解决方案(一个在所有打开的缓冲区中搜索给定字符串并动态显示结果的解决方案)。 So that's acceptable too. 所以这也是可以接受的。

I use Emacs 24.2, so any v24-only elisp is also fine. 我使用Emacs 24.2,所以任何仅限v24的elisp也没问题。

EDIT : I gave Helm another shot, at event_jr's suggestion , and I found that it does support instant searching across all open buffers. 编辑 :我给头盔再出手,在event_jr的建议 ,我发现在所有打开的缓冲区支持即时搜索。 helm-multi-occur + helm-follow-mode comes surprisingly close to meeting my needs, the only minor issues being (at the risk of sounding nit-picky): helm-multi-occur + helm-follow-mode出乎意料地接近满足我的需求,唯一的小问题是(冒着听起来挑剔的风险):

  • I haven't found a way to turn on helm-follow-mode automatically when I run helm-multi-occur . 当我运行 helm-multi-occur时,我还没有找到一种 自动打开 helm-follow-mode I have to invoke it manually with Cc Cf . 我必须使用 Cc Cf手动调用它。 Anyone care to take a shot at this with a snippet of elisp? 有人想用一小段elisp来拍摄吗? (see edit #2 below) (见下面的编辑#2)

  • it isn't "intelligent" like ST2's Goto Anything (ie, it doesn't understand "symbols" in source code, like Goto Anything does). 它不像ST2的Goto Anything那样“智能”(也就是说,它不能理解源代码中的“符号”,就像Goto Anything那样)。

EDIT #2 : Now I've got most of Goto Anything, thanks to event_jr's answer below (and of course, thanks to Helm's creator, Thierry Volpiatto ). 编辑#2 :现在我已经获得了大部分Goto Anything,感谢下面的event_jr答案 (当然,感谢Helm的创作者Thierry Volpiatto )。 I recommend it heartily to anyone looking for a similar feature. 我衷心向所有寻找类似功能的人推荐它。 Below is the elisp I'm currently using: 以下是我目前正在使用的elisp:

;; instant recursive grep on a directory with helm
(defun instant-rgrep-using-helm ()
  "Recursive grep in a directory."
  (interactive)
  (let ((helm-after-initialize-hook #'helm-follow-mode))
    (helm-do-grep)))


;; instant search across all buffers with helm
(defun instant-search-using-helm ()
  "Multi-occur in all buffers backed by files."
  (interactive)
  (let ((helm-after-initialize-hook #'helm-follow-mode))
    (helm-multi-occur
     (delq nil
           (mapcar (lambda (b)
                     (when (buffer-file-name b) (buffer-name b)))
                   (buffer-list))))))

;; set keybindings
(global-set-key (kbd "C-M-s") 'instant-search-using-helm)
(global-set-key (kbd "C-M-S-s") 'helm-resume)
(global-set-key (kbd "C-M-g") 'instant-rgrep-using-helm)

Just use helm. 只需使用掌舵。

It is perhaps more configuration than you asked for, but once you get it configured how you like, it should be quite comfortable. 它可能比您要求的配置更多,但是一旦您按照自己喜欢的方式配置它,它应该非常舒适。 Very much like Emacs ;). 非常像Emacs;)。

And you should file a bug with Thierry for getting some more newbie friendly defaults. 你应该向Thierry提交一个bug,以获得更多新手友好的默认值。 He is quite responsive with issues. 他对问题非常敏感。

helm-multi-occur 舵的多发生

Primarily multi-buffer interactive "occur" is provided through helm-multi-occur . 主要通过helm-multi-occur提供多缓冲交互“发生”。 If you execute the command, you'll notice that you have to pick some buffers first (use C-SPC to select from the list, M-SPC to select all). 如果你执行命令,你会发现你必须先选择一些缓冲区(使用C-SPC从列表中选择, M-SPC选择全部)。 Then you can enter your query at the next prompt. 然后,您可以在下一个提示符下输入您的查询。 It's easy to make your own version that skips the buffer selection like so: 你可以轻松制作自己的版本来跳过缓冲区选择,如下所示:

(eval-after-load "helm-regexp"
    '(setq helm-source-moccur
           (helm-make-source "Moccur"
               'helm-source-multi-occur :follow 1)))

(defun my-helm-multi-all ()
  "multi-occur in all buffers backed by files."
  (interactive)
  (helm-multi-occur
   (delq nil
         (mapcar (lambda (b)
                   (when (buffer-file-name b) (buffer-name b)))
                 (buffer-list)))))

helm-buffers-list 掌舵缓冲器列表

Often you don't care about the exact occurrences of the query string, but want a list of all buffers that contain it. 通常,您不关心查询字符串的确切出现次数,但需要包含它的所有缓冲区的列表。

helm-buffers-list has some tricks up its sleeve. helm-buffers-list有一些技巧。 The first symbol you specify is filtering by major-mode, and you can use the "@" prefix to narrow the list to buffers that contain a string. 您指定的第一个符号是按主模式过滤,您可以使用“@”前缀将列表缩小到包含字符串的缓冲区。

To wit, "ruby @prompt" will show you a list of buffers whose major-mode contains "ruby" and whose contents contains "prompt". 也就是说,“ruby @prompt”将显示一个缓冲区列表,其主模式包含“ruby”,其内容包含“prompt”。 Or you can just use "@prompt" to show all buffers that contain "prompt". 或者您可以使用“@prompt”显示包含“prompt”的所有缓冲区。


Powerful and comfortable once you get used to it. 一旦你习惯了,它就会变得强大而舒适。


EDIT modified my-helm-multi-all to enable helm-follow-mode. 编辑修改了my-helm-multi-all以启用helm-follow-mode。

EDIT 2 update helm-follow-mode code to reflect helm changes. 编辑2更新helm-follow-mode代码以反映helm更改。

EDIT 3 updated again to reflect helm changes 编辑3再次更新以反映掌舵变化

Emacs has Projectile satisfy your need: Emacs拥有Projectile满足您的需求:

  • jump to a file in project 跳转到项目中的文件
  • multi-occur in project buffers 在项目缓冲区中多次出现
  1. Heml is far from the fuzzy searching of ST3. Heml远不是对ST3的模糊搜索。

  2. Fiplr looks promising but doesn't work on my laptop (see first issue on the github) Fiplr看起来很有前途,但在我的笔记本电脑上不起作用(请参阅github上的第一期)

  3. Simp.el looks like Fiplr but doesn't work either on my end. Simp.el看起来像Fiplr,但在我的结尾不起作用。

  4. Projectile works for me! 弹丸为我工作! Here's your solution! 这是你的解决方案!

I used also ido-mode and flx-ido for the fuzzy searching, 我还使用ido-mode和flx-ido进行模糊搜索,

and for the vertical way of displaying results I use this in my .emacs: 并且对于显示结果的垂直方式,我在我的.emacs中使用它:

;; Display ido results vertically, rather than horizontally
(setq ido-decorations (quote ("\n-> " "" "\n   " "\n   ..." "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]" " [Confirm]")))
(defun ido-disable-line-truncation () (set (make-local-variable 'truncate-lines) nil))
(add-hook 'ido-minibuffer-setup-hook 'ido-disable-line-truncation)
(defun ido-define-keys () ;; C-n/p is more intuitive in vertical layout
  (define-key ido-completion-map (kbd "C-n") 'ido-next-match)
  (define-key ido-completion-map (kbd "C-p") 'ido-prev-match))
(add-hook 'ido-setup-hook 'ido-define-keys)

Icicles offers some features that are similar to what it seems you are looking for. Icicles提供了一些类似于您正在寻找的功能。

  1. Cx b and Cx Cf , to choose buffers or files, allow multi-completion: you can type a pattern to match the buffer/file name and/or a pattern to match content in the buffer / file . Cx bCx Cf ,选择缓冲区或文件,允许多次完成:您可以键入模式以匹配缓冲区/文件名和/或模式以匹配 缓冲区 / 文件 中的内容 Candidates are filtered incrementally as you type (what you call "instant" is what Emacs calls "incremental"). 当您键入时,候选者会逐步过滤(您称之为“即时”的是Emacs所谓的“增量”)。 You can refine either or both search patterns progressively , narrowing the choices in different ways. 您可以逐步优化其中一种或两种搜索模式,从而以不同方式缩小选择范围。 You can visit any number of buffers/files that match, at the same time. 您可以同时访问任意数量的匹配缓冲区/文件。 You can also use the same method to search the marked files in Dired: CF . 您也可以使用相同的方法搜索Dired: CF的标记文件。

  2. Cc ` ( icicle-search ) incrementally searches across multiple buffers or files. Cc `icicle-search )在多个缓冲区或文件增量搜索。 Again, progressive refinement etc. 再次,渐进式细化等

The main difference between #1 and #2 is this: #1和#2之间的主要区别是:

  • For #1, you just want to find matching buffers or files. 对于#1,您只想查找匹配的缓冲区或文件。 You don't care immediately about finding particular occurrences --- any match suffices. 您不必立即关注查找特定事件---任何匹配都足够。

  • For #2, you provide the buffers or files to search, and you want to navigate among search hits. 对于#2,您提供要搜索的缓冲区或文件,并且您希望在搜索命中之间导航。

You can also use #1 to locate the buffers and files you want, then search their contents: The content-matching pattern you last used is available as the search pattern for Isearch ( Cs ). 您还可以使用#1找到所需的缓冲区和文件,然后搜索其内容:上次使用的内容匹配模式可用作Isearch( Cs )的搜索模式。

for emacs I customize and modify this solution (for use install helm): 对于emacs我定制和修改此解决方案(使用安装helm):

(defun helm-occur-from-point (initial-value)
  "Invoke `helm-occur' from point."
  (interactive)
  (let ((input initial-value)
        (bufs (list (buffer-name (current-buffer)))))
    ;; (isearch-exit)
    (helm-occur-init-source)
    (helm-attrset 'moccur-buffers bufs helm-source-occur)
    (helm-set-local-variable 'helm-multi-occur-buffer-list bufs)
    (helm-set-local-variable
     'helm-multi-occur-buffer-tick
     (cl-loop for b in bufs
              collect (buffer-chars-modified-tick (get-buffer b))))
    (helm :sources 'helm-source-occur
          :buffer "*helm occur*"
          :history 'helm-grep-history
          :input input
          :truncate-lines t)))

(defun get-point-text ()
    "Get 'interesting' text at point; either word, or region"
    (if mark-active
        (buffer-substring (mark) (point))
      (thing-at-point 'symbol)))
  (defun helm-occur-1 (initial-value)
    "Preconfigured helm for Occur with initial input."
    (helm-occur-from-point initial-value))
  (defun bk-helm-occur ()
    "Invoke helm-occur with initial input configured from text at point"
    (interactive)
    (helm-occur-1 (get-point-text)))
  (global-set-key (kbd "M-s-o") 'bk-helm-occur)

primary it based on @see https://news.ycombinator.com/item?id=6872508 but on last helm versions not work but fixed with my changes (just copy/paste from some internal helm modules) 主要基于@see https://news.ycombinator.com/item?id=6872508,但最后的helm版本不起作用,但修改了我的更改(只是从一些内部helm模块复制/粘贴)

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

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