Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use org-mode "capture-refile" mechanism to build my own vocabulary?

The org-mode has Capture-Refile-Archive mechanism. I often use it to take notes and logs. I also want to use it to build my own vocabulary. For example, when I encounters some new words while reading some English text, I want to simply type C-c c v e to record the word into my vocabulary file Word-List-EN.org. I also want that words are categorized into Letters, e.g., cashew will be recorded into entry /C/CA. I want to type C-c c v E to record my Esperanto words into some file like vortaro.org. How to config org-capture-templates to implement this? I read the org-info and still have no clue. The manual says that I can use something like (function function-finding-location). But I have to define my own function-finding-location. Hope elisp masters can help me!

like image 748
Vivodo Avatar asked Nov 25 '12 12:11

Vivodo


2 Answers

I think pmr is right about the fact that the functions for choosing a file location as part of a template happen too early for your needs.

One way to integrate with org capture refile workflow would be to use the standard org capture template system to file your new word into the vocab buffer then use a custom elisp function as a hook, probably into org-capture-before-finalize-hook, to file the new definition into the right location.

I wrote up the following function as a sample that prompts for a word and definition and inserts it into an org buffer in the right place. If it suits your needs you could bind this to a key of your choice, or use it as a starting point for the hook approach.

;;
;; 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)))))
like image 52
mrvwman Avatar answered Nov 27 '22 19:11

mrvwman


I had huge problems getting org-capture for something similar, but org-remember worked well. Here's my relevant .emacs bits:

(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")
    )
)
like image 43
Daniel Avatar answered Nov 27 '22 19:11

Daniel