Enabling right-clicks in org-mode links
Posted October 21, 2013 at 07:58 PM | categories: org-mode | tags:
Updated October 21, 2013 at 08:45 PM
Out of the box you can click on org-mode links to make the do things. On my machine, all clicks are equal, left mouse, middle mouse, and right mouse all act as a "click". I was curious about whether I could get different behavior on a link with a left or right mouse click. It is easy enough to define a new link type . You define a function that is run when you click on the link.
To figure out what to do here, I looked into the events handling in emacs. According to this page , there are click events. So, after we click on a link, there should be a click event which was the last input event. We can get that, figure out which button was pressed, and run code accordingly. We will make the code add some lines to the buffer after the link about what happened.
Here is my link definition.
(setq counter 1) (org-add-link-type "test" ;; this function is run when you click (lambda (link-string) (let ((button (car last-input-event))) (cond ((eq button 'mouse-1) (end-of-line) (insert (format "\nclick %s. mouse-1 pressed %s\n" counter last-input-event)) (setq counter (+ counter 1))) ((eq button 'mouse-2) (end-of-line) (insert (format "\nclick %s. mouse-2 pressed %s\n" counter last-input-event)) (setq counter (+ counter 1))) ((eq button 'mouse-3) (end-of-line) (insert (format "\nclick %s. mouse-3 pressed %s\n" counter last-input-event)) (setq counter (+ counter 1)))))) ;; formatting (lambda (keyword desc format) (cond ((eq format 'html) (format "<pre>%s:%s</pre>" keyword desc)))))
Here we make a link. When you click on it, it adds lines right after the link telling you what was clicked on. I left-clicked, middle-clicked and right-clicked. The right-clicked result is the first line.
test:which-buttonclick 3. mouse-3 pressed (mouse-3 (#<window 46 on blog.org> 56959 (57 . 456) -320964819 nil 56959 (7 . 28) nil (1 . 8) (8 . 16)))
click 2. mouse-2 pressed (mouse-2 (#<window 46 on blog.org> 56959 (57 . 456) -320965724 nil 56959 (7 . 28) nil (1 . 8) (8 . 16)))
click 1. mouse-2 pressed (mouse-2 (#<window 46 on blog.org> 56959 (57 . 456) -320966660 nil 56959 (7 . 28) nil (1 . 8) (8 . 16)))
Curiously, this only shows that mouse-2 (for left or middle mouse) or mouse-3 (for right click) was pressed, never mouse-1. I am not sure what causes that. If I try to capture an event it does show mouse-1 is active.
(down-mouse-1 (#<window 34 on blog.org> 56437 (253 . 308) -322917920 nil 56437 (31 . 19) nil (93 . 4) (8 . 16)))
Anyway, it looks conceivable that you could have different link actions occur for different mouse clicks. I could see using this in a citation link, where a left click might open the citation in my bibtex file, and right clicking would open a pdf of the citation if it existed.
I have not figured out how flexible this might be, for example could you use modifier keys with mouse clicks? This code suggests that it is possible in emacs, but so far none of these make it into the last-input-event in the org-link clicks.
(S-down-mouse-1 (#<window 34 on blog.org> 56725 (1 . 299) -322897656 nil 56725 (0 . 18) nil (1 . 11) (8 . 16)))
It might be difficult remembering all the modifiers and clicks, but it would be cool if it was possible!
Copyright (C) 2013 by John Kitchin. See the License for information about copying.