gpt4 book ai didi

regex - emacs org 模式,只搜索标题

转载 作者:行者123 更新时间:2023-12-04 08:07:17 28 4
gpt4 key购买 nike

在 emacs 中,我希望能够仅搜索 org 模式文件中的“标题”。

思路一:只搜索可见
我可以通过隐藏所有内容来实现这一点,然后只显示大纲(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”并将其绑定(bind)一个热键吗?但仍然有能力去像'下一个查找,下一个查找'等..?

这个用例是搜索我的笔记。有些长约 7000 多行,我通常只搜索标题。

[编辑解决方案 1]
@abo-abo 的回答对我来说效果很好。我现在使用 helm-org-in-buffer-headings
即,我安装了 Melpa:
https://github.com/milkypostman/melpa#usage

然后我从包列表中安装了 helm: M-x package-list-packages
然后我编辑了我的 .emacs 并绑定(bind)了一个热键: (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)))))

然后将其绑定(bind)到热键:
(global-set-key (kbd "<f3>") 'worf-goto)

最佳答案

worf-goto来自 worf可以做到这一点,helm-org-in-buffer-headings 也可以来自 helm .
worf-goto实际使用 helm作为后端。除了helm-org-in-buffer-headings , 你得到:

  • 标题以与原始缓冲区中相同的方式着色
  • 您可以使用适当的数字
  • 选择所有具有相同级别的标题

    关于regex - emacs org 模式,只搜索标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28030946/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com