Emacs Org Mode,仅搜索标题

11
在Emacs中,我想要仅搜索org模式文件中的“标题”。

思路1:仅搜索可见内容
我可以通过隐藏所有内容,然后仅显示大纲(S-TAB,S-TAB),然后也许搜索所有可见内容。(在这种情况下,将是整个目录表)。 但如何仅搜索可见内容? C-s会搜索所有内容。

思路2:使用正则表达式
我可以尝试:

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

但目前输入所有内容都很麻烦。考虑到我大约3个小时前才开始学习emacs,我是否可以以某种方式自动化这个过程?
例如,我能否编写一个函数来搜索“*.*参数”,并将其绑定到热键上?但仍然能够像“下一个查找,下一个查找”等一样进行操作吗?

使用案例是搜索我的笔记。有些笔记超过7000行,我通常只搜索标题。

[编辑解决方案1]
@abo-abo的答案对我有效。我现在使用helm-org-in-buffer-headings

也就是说,我安装了Melpa: https://github.com/milkypostman/melpa#usage

然后我从软件包列表中安装了helm: M-x package-list-packages

然后我编辑了我的.emacs文件并将热键绑定到它:
(global-set-key (kbd "C-=") 'helm-org-in-buffer-headings) ;Outline search.

我重新加载了emacs,现在按下Ctrl+=时会弹出一个可搜索的大纲,随着我输入更多字符而自动缩小范围。导航可使用常规的C-n、C-p和按钮。

谢谢!

[编辑解决方案2] 好奇心让我做了最好的事情。在享受helm的标题搜索之后,我也试用了worf。它像helm一样(它使用helm),但看起来更好,我可以通过按数字键选择大纲的“级别”。如果有用,我精简了必要的部分以进行标题搜索:

;; ——— 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)))))

然后将其绑定到热键:

(global-set-key (kbd "<f3>") 'worf-goto)
3个回答

8
来自worfworf-goto可以实现此功能,同样地,helmhelm-org-in-buffer-headings也可以。

worf-goto实际上使用helm作为后端。除了helm-org-in-buffer-headings之外,您还可以获得以下功能:

  • 标题的颜色与原始缓冲区相同
  • 您可以使用相应的数字选择所有具有相同级别的标题

worf-goto 真是太棒了。感谢分享。 - Leo Ufimtsev
1
很高兴你喜欢它。如果你想在Emacs Lisp文件中获取类似的功能,请尝试使用lispy-goto - abo-abo

4
如果您已经安装了ivy,您可以使用counsel-org-goto在当前缓冲区中搜索标题,或者使用counsel-org-goto-all在所有打开的org-mode缓冲区中搜索标题。如果您不想安装worf附带的其他组件,这是一个不错的选择。

4

如果你不想依赖外部包,那么实际上org已经提供了这个功能:该函数为org-goto

如果你希望它的行为类似于helm-org-in-buffer-headings,你需要将org-goto-interface设置为outline-path-completion,例如通过在你的init文件中添加:

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

1
我喜欢这种方法的唯一限制是它不会在标题中显示标签。 - Edman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接