思路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)
lispy-goto
。 - abo-abo