(or emacs irrelevant)

My org-protocol setup, part 1.

I'm quite busy with a project today, so I can't compose many words. However, pasting and explaining some code is fine. The basic idea is creating TODO tasks in certain org-mode files by clicking a link in Firefox, thanks to org-mode capture.

org-protocol starter

(require 'org-capture)
(require 'org-protocol)
(setq org-protocol-default-template-key "l")
(push '("l" "Link" entry (function org-handle-link)
        "* TODO %(org-wash-link)\nAdded: %U\n%(org-link-hooks)\n%?")
  • org-wash-link should clear up some redundancies in the TODO
  • org-handle-link should open the appropriate file and heading.
  • org-link-hooks should insert some extra information

Basically, when I capture a question on Stack Overflow, I don't want to see - Stack Overflow - as part of the TODO string, since the TODO itself is stored in wiki/stack.org/* Questions.

(defun org-wash-link ()
  (let ((link (caar org-stored-links))
        (title (cadar org-stored-links)))
    (setq title (replace-regexp-in-string
                 " - Stack Overflow" "" title))
    (org-make-link-string link title)))

This is just a hack for passing information around that functions from org-handle-link can use.

(defvar org-link-hook nil)

(defun org-link-hooks ()
      (mapconcat #'funcall
    (setq org-link-hook)))

This is the heart of the setup.

(defun org-handle-link ()
  (let ((link (caar org-stored-links))
    (cond ((string-match "^https://www.youtube.com/" link)
           (org-handle-link-youtube link))
          ((string-match (regexp-quote
                          "http://stackoverflow.com/") link)
           (find-file (org-expand "wiki/stack.org"))
           (goto-char (point-min))
           (re-search-forward "^\\*+ +Questions" nil t))
           (find-file (org-expand "ent.org"))
           (goto-char (point-min))
           (re-search-forward "^\\*+ +Articles" nil t)))))
  • Youtube links will be handled with org-handle-link-youtube
  • Stack Overflow links will be stored in wiki/stack.org/* Questions
  • all other links will be stored in ent.org/* Articles

I'll write down org-handle-link-youtube in a later post, since I would still like to sort out a few kinks with it. The main issue is that I'm sending two requests to Youtube: one to download the video, which is fine, since async handles it; and other to get the title of the video and put it in the heading. And this other request causes a perceptible delay when capturing.