簡體   English   中英

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

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

我最近嘗試了Sublime Text 2 ,我發現Goto Anything非常適用於導航源代碼( Ctrl-P文件@符號似乎工作得非常好)。 Emacs有類似的東西嗎? 最好的東西只是工作,沒有大量的定制elisp。

到目前為止我嘗試過的:

  1. 我見過HelmAnything 但據我所知,他們都不能進行實際的“即時”搜索 (見下面的編輯)。

  2. 我已經使用了multi-occur-in-matching-buffers ,但它似乎也無法滿足“即時”標准。

  3. imenu / idomenu適用於單個文件,但不適用於文件。

我目前一起使用#2和#3,作為Goto Anything的不良替代品。

如果不是Goto Anything的精確克隆,那么我可以使用天真的即時搜索解決方案(一個在所有打開的緩沖區中搜索給定字符串並動態顯示結果的解決方案)。 所以這也是可以接受的。

我使用Emacs 24.2,所以任何僅限v24的elisp也沒問題。

編輯 :我給頭盔再出手,在event_jr的建議 ,我發現在所有打開的緩沖區支持即時搜索。 helm-multi-occur + helm-follow-mode出乎意料地接近滿足我的需求,唯一的小問題是(冒着聽起來挑剔的風險):

  • 當我運行 helm-multi-occur時,我還沒有找到一種 自動打開 helm-follow-mode 我必須使用 Cc Cf手動調用它。 有人想用一小段elisp來拍攝嗎? (見下面的編輯#2)

  • 它不像ST2的Goto Anything那樣“智能”(也就是說,它不能理解源代碼中的“符號”,就像Goto Anything那樣)。

編輯#2 :現在我已經獲得了大部分Goto Anything,感謝下面的event_jr答案 (當然,感謝Helm的創作者Thierry Volpiatto )。 我衷心向所有尋找類似功能的人推薦它。 以下是我目前正在使用的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)

只需使用掌舵。

它可能比您要求的配置更多,但是一旦您按照自己喜歡的方式配置它,它應該非常舒適。 非常像Emacs;)。

你應該向Thierry提交一個bug,以獲得更多新手友好的默認值。 他對問題非常敏感。

舵的多發生

主要通過helm-multi-occur提供多緩沖交互“發生”。 如果你執行命令,你會發現你必須先選擇一些緩沖區(使用C-SPC從列表中選擇, M-SPC選擇全部)。 然后,您可以在下一個提示符下輸入您的查詢。 你可以輕松制作自己的版本來跳過緩沖區選擇,如下所示:

(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有一些技巧。 您指定的第一個符號是按主模式過濾,您可以使用“@”前綴將列表縮小到包含字符串的緩沖區。

也就是說,“ruby @prompt”將顯示一個緩沖區列表,其主模式包含“ruby”,其內容包含“prompt”。 或者您可以使用“@prompt”顯示包含“prompt”的所有緩沖區。


一旦你習慣了,它就會變得強大而舒適。


編輯修改了my-helm-multi-all以啟用helm-follow-mode。

編輯2更新helm-follow-mode代碼以反映helm更改。

編輯3再次更新以反映掌舵變化

Emacs擁有Projectile滿足您的需求:

  • 跳轉到項目中的文件
  • 在項目緩沖區中多次出現
  1. Heml遠不是對ST3的模糊搜索。

  2. Fiplr看起來很有前途,但在我的筆記本電腦上不起作用(請參閱github上的第一期)

  3. Simp.el看起來像Fiplr,但在我的結尾不起作用。

  4. 彈丸為我工作! 這是你的解決方案!

我還使用ido-mode和flx-ido進行模糊搜索,

並且對於顯示結果的垂直方式,我在我的.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提供了一些類似於您正在尋找的功能。

  1. Cx bCx Cf ,選擇緩沖區或文件,允許多次完成:您可以鍵入模式以匹配緩沖區/文件名和/或模式以匹配 緩沖區 / 文件 中的內容 當您鍵入時,候選者會逐步過濾(您稱之為“即時”的是Emacs所謂的“增量”)。 您可以逐步優化其中一種或兩種搜索模式,從而以不同方式縮小選擇范圍。 您可以同時訪問任意數量的匹配緩沖區/文件。 您也可以使用相同的方法搜索Dired: CF的標記文件。

  2. Cc `icicle-search )在多個緩沖區或文件增量搜索。 再次,漸進式細化等

#1和#2之間的主要區別是:

  • 對於#1,您只想查找匹配的緩沖區或文件。 您不必立即關注查找特定事件---任何匹配都足夠。

  • 對於#2,您提供要搜索的緩沖區或文件,並且您希望在搜索命中之間導航。

您還可以使用#1找到所需的緩沖區和文件,然后搜索其內容:上次使用的內容匹配模式可用作Isearch( Cs )的搜索模式。

對於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)

主要基於@see https://news.ycombinator.com/item?id=6872508,但最后的helm版本不起作用,但修改了我的更改(只是從一些內部helm模塊復制/粘貼)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM