如何使用org-mode的“capture-refile”机制来建立自己的词汇表?

10
org-mode 具有 Capture-Refile-Archive 机制。我经常用它来记录笔记和日志。我还想用它来构建自己的词汇表。例如,当我阅读一些英文文本时遇到一些新单词,我希望只需键入 C-c c v e 就可以将该单词记录到我的词汇文件 Word-List-EN.org 中。我还希望将单词分类为字母类别,例如,cashew 将被记录到条目 /C/CA 中。我希望键入 C-c c v E 将我的世界语单词记录到类似 vortaro.org 的文件中。如何配置 org-capture-templates 来实现这一点?我已经阅读了 org-info,但仍无头绪。手册说我可以使用类似于 (function function-finding-location) 的东西。但是我必须定义自己的 function-finding-location。希望 elisp 大师能够帮助我!

树的标题应该是/C/CA吗?还是你的意思是斜杠表示子级别? - pmr
1
我看到的主要问题是:org-capture在调用时决定了位置。这意味着您无法在此时选择位置,因为您尚未插入单词(有道理,对吧?)。因此,您可能应该使用自定义的重文件操作而不是捕获操作。只需将捕获的单词发送到正确的文件(english.org),然后使用自定义的重文件操作即可。 - pmr
是的,/C/CA表示第二个子级,在“C”下有一个名为“CA”的条目。 - Vivodo
2个回答

5
我认为pmr的看法是正确的,即作为模板的选择文件位置功能对您的需求来说过早了。
要与org capture refile工作流程集成的一种方法是使用标准的org capture模板系统将您的新单词存入词汇缓冲区,然后使用自定义的elisp函数作为钩子,可能是org-capture-before-finalize-hook,将新的定义文件到正确的位置。
我写了下面的函数作为样本,提示一个单词和定义,并将其插入到正确的位置的org缓冲区中。如果它符合您的需求,您可以将其绑定到您选择的键上,或者将其用作挂钩方法的起点。
;;
;; remove extra spaces between stars and the headline text
;;
(defun tidy-headlines ()
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "^\\*+\\([[:space:]][[:space:]]+\\)" (point-max) t)
      (replace-match " " nil nil nil 1))))

;;
;; function for storing things in vocab files like cashew /C/CA/Cashew
;;
(defun insert-vocab-word (vocab-word definition)
  "prompts for a word then opens a vocab file and puts the word in place"
  (interactive "sWord: \nsDefinition: ")
  (find-file "~/org/vocab.org")
  (tidy-headlines)
  (goto-char (point-min))
  (let* ((first-char (upcase (substring vocab-word 0 1)))
         (first-two-chars (upcase (substring vocab-word 0 2))))
    (while (and (re-search-forward "^\\* " (point-max) t)
                (string< (buffer-substring-no-properties (point) (+ 1 (point))) first-char)))
    (if (string= (buffer-substring-no-properties (point) (+ 1 (point))) first-char)
        (let* ((end-of-subtree (save-excursion (org-end-of-subtree) (point))))
          (while (and (re-search-forward "^\\*\\* " end-of-subtree t)
                    (string< (buffer-substring-no-properties (point) (+ 2 (point))) first-two-chars)))
          (if (string= (buffer-substring-no-properties (point) (+ 2 (point))) first-two-chars)
              (let* ((end-of-subtree2 (save-excursion (org-end-of-subtree) (point))))
                (while (and (re-search-forward "^\\*\\*\\* " end-of-subtree2 t)
                            (string< (word-at-point) vocab-word)))
                (if (string< vocab-word (word-at-point))
                    (progn
                      (beginning-of-line)
                      (org-insert-heading t))
                  (org-insert-heading-after-current))
                (insert vocab-word)
                (org-insert-subheading t)
                (insert definition))
            (progn
              (if (string< first-two-chars (buffer-substring-no-properties (point) (+ 2 (point))))
                  (progn
                    (beginning-of-line)
                    (org-insert-heading t))
                (org-insert-heading-after-current))
              (insert first-two-chars)
              (org-insert-subheading t)
              (insert vocab-word)
              (org-insert-subheading t)
              (insert definition))))
      (progn
        (org-insert-heading-after-current)
        (insert first-char)
        (org-insert-subheading t)
        (insert first-two-chars)
        (org-insert-subheading t)
        (insert vocab-word)
        (org-insert-subheading t)
        (insert definition)))))

似乎“vocab.org”一开始就不应该为空。 - Dror

1

我曾经在获取org-capture或类似的东西时遇到了很大的问题,但是org-remember效果很好。以下是我的相关.emacs代码:

(require 'org-install)
(setq org-directory "~/.orgfiles/")
(setq org-default-notes-file (expand-file-name "~/.orgfiles/notes.org"))
(setq remember-annotation-functions '(org-remember-annotation))
(setq remember-handler-functions '(org-remember-handler))
(add-hook 'remember-mode-hook 'org-remember-apply-template)
(setq org-agenda-files '("~/.orgfiles"))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-cc" 'org-remember)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
(setq org-remember-templates
      '(("Todo" ?t "* TODO %^{Brief Description} %^g\n%?    Added: %U" "~/.orgfiles/TODO.org" "Tasks")
    ("Idea" ?i "* %^{Brief Description} %^g\n%?    Added: %U" "~/.orgfiles/Idea.org" "Ideas")
    ("Study" ?s "* %^{Brief Description} %^g\n%?    Added: %U" "~/.orgfiles/Study.org" "Study")
    ("Receipt" ?r "** %^{Brief Description} %U %^g\n%?" "~/.orgfiles/Receipt.org")
    )
)

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