简体   繁体   English

emacs组织模式,仅搜索标头

[英]emacs org mode, search only headers

In emacs, I want to be able to search only the 'headers' in an org mode file. 在emacs中,我希望只能搜索组织模式文件中的“标题”。

Idea 1: Search only Visible 想法1:仅搜索可见
I could achieve this by hiding everything, then showing only the outline (S-TAB, S-TAB) and then maybe search all that is visible.(in this case it would be the whole table of content). 我可以通过隐藏所有内容来实现这一点,然后只显示轮廓(S-TAB,S-TAB),然后搜索所有可见的内容。(在这种情况下,它将是整个内容表)。 But how do I search only visible content? 但是,我如何只搜索可见内容? Cs searches everything. Cs搜索一切。

Idea 2: use regex 想法2:使用正则表达式
I can potentially do: 我可以这样做:

C-c / /             //opens regex search
\*.*heading        //start with * (escaped), followed by any chars, then heading.

But at the moment it's cumbersome to type all of that. 但目前输入所有这些都很麻烦。 Considering I've started learning emacs like 3 hours ago, can I automate this somehow? 考虑到我已经开始学习像3小时前的emacs,我能以某种方式自动化吗?
Eg, can I write a function to search with "*.*ARGUMENT" and tie it a hotkey? 例如,我可以编写一个函数来搜索“*。* ARGUMENT”并将其绑定为热键吗? but still have the ability to go like 'next find, next find' etc..? 但仍然有能力像'下一个找到,下一个找到'等...?

The use case for this is searching my notes. 用例就是搜索我的笔记。 Some are like ~7000+ lines long and I commonly only search the headers. 有些像〜7000 +行长,我通常只搜索标题。

[EDIT Solution 1] [编辑解决方案1]
@abo-abo's answer worked well for me. @ abo-abo的回答对我很有用。 I now use helm-org-in-buffer-headings 我现在使用helm-org-in-buffer-headings

Ie, I installed Melpa: https://github.com/milkypostman/melpa#usage 即,我安装了Melpa: https//github.com/milkypostman/melpa#usage

Then I installed helm from the package list: Mx package-list-packages 然后我从包列表中安装了helm: Mx package-list-packages

Then I edited my .emacs and bound a hotkey to it: 然后我编辑了我的.emacs并绑定了一个热键:
(global-set-key (kbd "C-=") 'helm-org-in-buffer-headings) ;Outline search.

I reloaded emacs and now when pressing Ctrl+= a searchable outline pops up that automatically narrows down as I type in additional characters. 我重新加载了emacs,现在当按下Ctrl + =时会弹出一个可搜索的轮廓,当我输入其他字符时会自动缩小。 The usual Cn, Cp , buttons work for navigation. 通常的Cn,Cp按钮用于导航。

Thanks! 谢谢!

[Edit Solution 2] Curiosity got the best of me. [编辑解决方案2]好奇心得到了我的好处。 After enjoying helm's heading search, I messed around with worf also. 在享受了头盔搜索之后,我也和worf一起搞砸了。 It is like helm (it uses helm) but looks nicer and I can select a 'level' of outline by pressing the number key. 它就像helm(它使用helm)但看起来更好,我可以通过按数字键来选择轮廓的“级别”。 I hacked out just the bits necessary for heading search, if of use: 如果使用的话,我只删除了标题搜索所需的位:

;; ——— WORF Utilities ———————————————————————————————————————————————————————————————
;; https://github.com/abo-abo/worf/blob/master/worf.el
(defun worf--pretty-heading (str lvl)
  "Prettify heading STR or level LVL."
  (setq str (or str ""))
  (setq str (propertize str 'face (nth (1- lvl) org-level-faces)))
  (let (desc)
    (while (and (string-match org-bracket-link-regexp str)
                (stringp (setq desc (match-string 3 str))))
      (setq str (replace-match
                 (propertize desc 'face 'org-link)
                 nil nil str)))
    str))
(defun worf--pattern-transformer (x)
  "Transform X to make 1-9 select the heading level in `worf-goto'."
  (if (string-match "^[1-9]" x)
      (setq x (format "^%s" x))
    x))

(defun worf-goto ()
  "Jump to a heading with `helm'."
  (interactive)
  (require 'helm-match-plugin)
  (let ((candidates
         (org-map-entries
          (lambda ()
            (let ((comp (org-heading-components))
                  (h (org-get-heading)))
              (cons (format "%d%s%s" (car comp)
                            (make-string (1+ (* 2 (1- (car comp)))) ?\ )
                            (if (get-text-property 0 'fontified h)
                                h
                              (worf--pretty-heading (nth 4 comp) (car comp))))
                    (point))))))
        helm-update-blacklist-regexps
        helm-candidate-number-limit)
    (helm :sources
          `((name . "Headings")
            (candidates . ,candidates)
            (action . (lambda (x) (goto-char x)
                         (call-interactively 'show-branches)
                         (worf-more)))
            (pattern-transformer . worf--pattern-transformer)))))

And then tied it to a hot key: 然后将其绑定到热键:

(global-set-key (kbd "<f3>") 'worf-goto)

worf-goto from worf can do this, so can helm-org-in-buffer-headings from helm . worf-gotoWORF可以做到这一点,这样可以helm-org-in-buffer-headings掌舵

worf-goto actually uses helm as a back end. worf-goto实际上使用helm作为后端。 In addition to helm-org-in-buffer-headings , you get: 除了helm-org-in-buffer-headings ,您还可以:

  • headings are colored in the same way as in the original buffer 标题的颜色与原始缓冲区中的颜色相同
  • you can select all headings with the same level using the appropriate digit 您可以使用相应的数字选择具有相同级别的所有标题

If you have ivy installed, you can use counsel-org-goto to search headings in the current buffer or counsel-org-goto-all to search the headings in all open org-mode buffers. 如果您安装了常春藤,您可以使用counsel-org-goto搜索当前缓冲区中的标题或counsel-org-goto-all来搜索所有打开的组织模式缓冲区中的标题。

It's a good option if you don't want to install the other things that come with worf. 如果你不想安装worf附带的其他东西,这是一个很好的选择。

If you don't want to rely on external packages, org, in fact, already offers this capability: the function is org-goto . 如果您不想依赖外部包,org实际上已经提供了这种功能:该功能是org-goto

If you want it to behave in a way similar to helm-org-in-buffer-headings , you have to set org-goto-interface to outline-path-completion , for instance by adding to your init file: 如果您希望它的行为类似于helm-org-in-buffer-headings ,则必须将org-goto-interfaceoutline-path-completion ,例如通过添加到init文件:

(setq org-goto-interface (quote outline-path-completion))

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

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