A generalized org-link with an extendable menu of actions
Posted November 02, 2014 at 07:33 PM | categories: org | tags:
In org-ref the cite links are actually a menu of options. Here I want to explore generalizing the concept, and enable users to add new menu items. When you click on a cite link, it calls a function that constructs the menu as a string. Then, we read something from the minibuffer to say what should be done. We could store these things in variables, so that a user could add their own things. We need a list of characters, menu strings, and functions to run when to create the menu. Here we put together a little example code that shows the idea.
(defun option-a (link) (message "You picked option A for %s" link)) (defun option-b (link) (message "You picked option B for %s" link)) (defvar menu-funcs '(("a" "Option 1" option-a) ("b" "Option 2" option-b))) (defun menu-click (path) (message (concat (mapconcat (lambda (tup) (concat "[" (elt tup 0) "]" (elt tup 1) " ")) menu-funcs "") ": ")) (setq input (read-char-exclusive)) (funcall (elt (assoc (char-to-string input) menu-funcs) 2) path)) (org-add-link-type "menu" 'menu-click)
element Clicking on that link in Emacs gives me the desired menu!
and
Now, we test adding a new function.
(defun option-c (link) (message "You picked option C for %s" link)) (add-to-list 'menu-funcs '("c" "Option C" option-c))
c | Option C | option-c |
a | Option 1 | option-a |
b | Option 2 | option-b |
Well, that worked fine! You might want to append to the list instead of put it at the beginning, but that is really your choice.
(defun option-4 (link) (message "You picked option 4 for %s" link)) (add-to-list 'menu-funcs '("4" "Option 4" option-4) t)
c | Option C | option-c |
a | Option 1 | option-a |
b | Option 2 | option-b |
4 | Option 4 | option-4 |
I think this example more or less shows the basic idea here. These external functions may do a variety of things, like look up something on google, or some other web service, search for something on your hard drive, etc… For example in org-ref, clicking on a cite link gives you options to open the bibtex file, a pdf, a url or notes. This would allow you to open other things too, if you felt like it. For better or worse, you can modify the behavior of the link after it is defined.
Copyright (C) 2014 by John Kitchin. See the License for information about copying.
Org-mode version = 8.2.7c