emacsfirefox-addonorg-mode

Customized org-capture-template for Org Capture Extension


I am using the great Org Capture Extension Firefox addon to directly capture my web links into my Emacs documents.

A minimal org capture template is:

(setq org-capture-templates `(
     ("L" "Protocol Link" entry (file+headline "~/web.org" "Links")
      "* [[%:link][%:description]]\n")  

     ;; ... your other templates
))

I use this to bookmark articles, in peculiar a lot of them are on arxiv.org. The problem is that arxiv title pages contain [] characters, for instance:

[1606.04838] Optimization Methods for Large-Scale Machine Learning

This does not mix well with the [[%:link][%:description]] used in the template to create the org mode link. For instance a capture returns:

** [[https://arxiv.org/abs/1606.04838][[1606.04838] Optimization Methods for Large-Scale Machine Learning]]

and the Org-Mode link is broken because of the brackets in the "[1606.04838]" string.

How to solve this?


Solution

  • The cure is to transform the link [%:description] description into a string that does not contain squared brackets []. For this we can define a function that transforms the [, ] chars into (, ) ones.

    (defun transform-square-brackets-to-round-ones(string-to-transform)
      "Transforms [ into ( and ] into ), other chars left unchanged."
      (concat 
      (mapcar #'(lambda (c) (if (equal c ?[) ?\( (if (equal c ?]) ?\) c))) string-to-transform))
      )
    

    Then we can use this function into the org-capture-template. The %(sexp) syntax is used to evaluate the lisp code into the template:

    %(sexp) Evaluate Elisp sexp and replace with the result. For convenience, %:keyword (see below) placeholders within the expression will be expanded prior to this. The sexp must return a string.

    The modified org-capture-template is:

    (setq org-capture-templates '(
    
        ("L" "Protocol Link" entry (file+headline "~/web.org" "Links")
         "* [[%:link][%(transform-square-brackets-to-round-ones \"%:description\")]]\n")
    
        ;; ... your other templates
    ))
    

    Then when you click on the Firefox Org-Capture button the template is correctly expended to

    ** (1606.04838) Optimization Methods for Large-Scale Machine Learning
    

    with a well formed Org-Mode link (note that the [1606.04838] was turned into (1606.04838))