【发布时间】:2015-03-17 19:57:12
【问题描述】:
在 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,我能以某种方式自动化吗?
例如,我可以编写一个函数来搜索“*.*ARGUMENT”并将其绑定一个热键吗?但仍然有能力去像'下一个查找,下一个查找'等..?
这个用例是搜索我的笔记。有些大约 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)
【问题讨论】:
标签: regex search emacs header org-mode