1 #+TITLE: org-protocol.el -- Intercept calls from emacsclient to trigger custom actions
2 #+OPTIONS: H:3 num:nil toc:t \n:nil @:t ::t |:t -:t f:t *:t TeX:t LaTeX:t skip:nil d:(HIDE) tags:not-in-toc ^:{} author:nil
4 #+SETUPFILE: ../macros.setupfile
5 #+STYLE: <script type="text/javascript">
6 #+STYLE: <!--/*--><![CDATA[/*><!--*/
7 #+STYLE: function makeUrl() {
8 #+STYLE: return encodeURIComponent(location.href)+
9 #+STYLE: '/'+encodeURIComponent(document.title)+
10 #+STYLE: '/'+encodeURIComponent(window.getSelection());
12 #+STYLE: function storeLink() {
13 #+STYLE: document.location.href='org-protocol://store-link://'+makeUrl();
15 #+STYLE: function remember() {
16 #+STYLE: document.location.href='org-protocol://remember://'+makeUrl();
18 #+STYLE: function capture() {
19 #+STYLE: document.location.href='org-protocol://capture://'+makeUrl();
24 [[file:index.org][{Back to Worg's contibutions index}]]
26 org-protocol intercepts calls from emacsclient to trigger custom actions without
27 external dependencies. Only one protocol has to be configured with your external
28 applications or the operating system, to trigger an arbitrary number of custom
29 actions. Just register your custom sub-protocol and handler with the variable
30 `org-protocol-protocol-alist'.
33 * About org-protocol.el
35 =org-protocol.el= is based on code and ideas from [[file:./org-annotation-helper.org][org-annotation-helper.el]] and
38 "=org-protocol:/sub-protocol:/=" triggers actions associated with =sub-protocol=
39 through the custom variable =org-protocol-protocol-alist=.
41 It comes with four predefined handlers:
42 - =org-protocol-store-link= ::
43 triggered through the sub-protocol "=store-link=". Stores an Org-link and
44 pushes the URL to the =kill-ring=.
45 - =org-protocol-capture= ::
46 Fill a =CAPTURE= buffer with information gathered somewhere else. This
47 handler is triggered through the "=capture=" sub-protocol and uses the
48 function =org-capture=.
49 - =org-protocol-remember= ::
50 Fills a remember buffer with information gathered somewhere else. This
51 handler is triggered through the "=remember=" sub-protocol and still
52 available for backward compatibility. This handler uses =org-remember=. Use
53 the current =org-protocol-capture=.
55 - =org-protocol-open-source= ::
56 "=open-source=". Maps URLs to local filenames. Use this to open sources of
57 already published contents in emacs for editing.
59 =org-protocol= helps creating custom handlers [[file:../org-tutorials/org-protocol-custom-handler.org][(tutorial)]] and so called
60 =org-protocol-projects=.
63 @<b>As of Org mode release 7.01 =org-protocol-remember= is now by =org-protocol-capture=.@</b>
64 If not stated otherwise, you may simply replace each occurrence of
65 /capture/ with /remember/ throughout this document, if you still want to use
66 remember templates. Use =M-x org-version= to find out about the version you're
74 - To load org-protocol.el add the following to your =.emacs=:
77 : (add-to-list 'load-path "~/path/to/org/protocol/")
78 : (require 'org-protocol)
80 * Browser / system setup
82 - [[Linux setup (Gnome)]]
86 *** Linux setup (Gnome)
88 For this to work, you'll need the Gnome-Libraries to be installed.
90 : gconftool-2 -s /desktop/gnome/url-handlers/org-protocol/command '/usr/local/bin/emacsclient %s' --type String
91 : gconftool-2 -s /desktop/gnome/url-handlers/org-protocol/enabled --type Boolean true
97 Windows users may register the "=org-protocol=" once for all by adjusting the
98 following to their facts, save it as *.reg file and double-click it. This
99 worked for me on Windows-XP Professional and the emasc23 from ourcomments.org
100 ([[http://ourcomments.org/cgi-bin/emacsw32-dl-latest.pl]]). I'm no Windows user
101 though and enhancements are more than welcome on the org-mode mailinglist. The
102 original file is from http://kb.mozillazine.org/Register_protocol.
107 [HKEY_CLASSES_ROOT\org-protocol]
110 [HKEY_CLASSES_ROOT\org-protocol\shell]
111 [HKEY_CLASSES_ROOT\org-protocol\shell\open]
112 [HKEY_CLASSES_ROOT\org-protocol\shell\open\command]
113 @="\"C:\\Programme\\Emacs\\emacs\\bin\\emacsclientw.exe\" \"%1\""
118 Follow the directions for installing EmacsClient.app from https://github.com/neil-smithline-elisp/EmacsClient.app. This should configure the org-protocol for all Mac OS X browsers.
120 After installing EmacsClient.app you should then [[Verify the installation]]. Once verified, you can begin [[Using org-protocol]].
125 If you are using Firefox on Mac OS X, see [[Mac OS X setup]].
128 Please refer to http://kb.mozillazine.org/Register_protocol and use
129 "org-protocol" as protocol.
134 :CUSTOM_ID: acrobat-reader-setup
137 Adapted from [[http://article.gmane.org/gmane.emacs.orgmode/6810]]
139 You place a javascript file for each menu entry in
140 =~/.adobe/Acrobat/<VERSION>/JavaScripts= on unix-like systems or
141 =c:/Program Files/Adobe/Acrobat <VERSION>/Reader/Javascripts/= on
142 Windows, or wherever your Adobe Reader Installation might look for
145 The examples given here will place new menu entries in the "Tools"
146 menu, after restarting Adobe Reader.
148 # <<acrobat-store-link-js>>
149 ******* org-store-link.js
150 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
151 : app.addMenuItem({cName:"org-store-link", cParent:"Tools",
152 : cExec:"app.launchURL('org-protocol://store-link://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title));"});
154 # <<acrobat-capture-js>>
155 ******* org-capture.js
156 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
157 : app.addMenuItem({cName:"org-capture", cParent:"Tools",
158 : cExec:"app.launchURL('org-protocol://capture://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
160 And this one, if you still use remember templates:
162 # <<acrobat-remember-js>>
163 ******* org-remember.js
164 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
165 : app.addMenuItem({cName:"org-remember", cParent:"Tools",
166 : cExec:"app.launchURL('org-protocol://remember://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
172 If you are using Opera on Mac OS X, see [[Mac OS X setup]].
174 Opera setup is described here:
175 http://www.opera.com/support/kb/view/535/.
177 To set up opera for use with org-protocol, follow these steps:
179 1. Choose "/Tools/" -> "/Prefences/" from the menu.
180 2. Select the tab "/Advanced/".
181 3. Choose "/Programs/" from the list on the left.
182 4. Now click the button "/Add/" on the very right.
183 5. In the new dialog window, enter "=org-protocol=" as "/Protocol/", choose the
184 radio button "/Open with other application/" and enter the path to
187 # <<test-org-protocol>>
190 Safari is only supported on Mac systems, not iOS systems.
192 See [[Mac OS X setup]] for directions.
195 *** Verify the installation
197 After your protocol is registered with your browser/OS, these links here
198 should work. Click on them and see if emacs reacts:
202 <li><a href="javascript:storeLink();">Org store-link</a></li>
203 <li><a href="javascript:capture();">Org capture (select some text if you like)</a></li>
204 <li><a href="javascript:remember();">Org remember (select some text please)</a></li>
209 # <<default-location>>
212 To actually use org-protocol add a bookmark to Firefox or Opera.
214 Here is the URL to use as "/Location/" for browser bookmarks. Just remove the
215 line breaks and replace "=sub-protocol=" with the real sub-protocol to use:
217 : javascript:location.href='org-protocol://sub-protocol://'+
218 : encodeURIComponent(location.href)+'/'+
219 : encodeURIComponent(document.title)+'/'+
220 : encodeURIComponent(window.getSelection())
222 This URL may be used for all three standard handlers in =org-protocol.el=. Some
223 of the values will be ignored (e.g. =store-link:/= will use the URL and title
226 # <<org-protocol-store-link>>
227 * Links and bookmarks: =org-protocol-store-link=
229 =org-store-link= stores an Org-link insertable through =M-x org-insert-link= and
230 pushes the URL found onto the =kill-ring= for yanking (=C-y=). The sub-protocol
231 used is "=store-link=":
233 : emacsclient org-protocol:/store-link:/URL/TITLE
235 will store this Org-link:
241 In addition, =URL= will be pushed on the =kill-ring= for yanking ('=C-y='). You will
242 have to encode =URL= and/or =TITLE= if they contain slashes, and probably quote
245 To use this feature, add a bookmark with an arbitrary name (e.g.
246 "/Org: store-link/") and enter this as "=Location=":
248 : javascript:location.href='org-protocol://store-link://'+encodeURIComponent(location.href)
251 # <<org-protocol-capture>>
252 * Note taking and citations: =org-protocol-capture=
254 This one is triggered through the sub-protocol "=capture=" and consumes up to
257 : emacsclient org-protocol:/capture:/URL/TITLE/BODY
259 will pop up an /*Capture*/ buffer and fill the template with the data
262 To use this feature, add a bookmark with an arbitrary name (e.g.
263 "/Org: capture/") and enter this as "=Location=":
265 : javascript:location.href='org-protocol://capture://'+
266 : encodeURIComponent(location.href)+'/'+
267 : encodeURIComponent(document.title)+'/'+
268 : encodeURIComponent(window.getSelection())
270 The result depends on the template used. See [[example-template][An example capture template]]
273 Note, that this one, as opposed to the other two standard handlers, does not
274 mix with more parameters to emacsclient. All parameters but the
275 #'=org-protocol://org-capture://...=' one will be discarded.
277 # <<which-capture-template>>
278 *** Which capture template is used?
280 :ID: org:f56bcb9a-20e7-4b37-99f1-839a8821cc4b
283 You don't need to setup a capture template to use =org-protocol-capture=,
284 since Org-mode provides a default template for those cases. Newer versions
285 provide an interactive interface for choosing a template. You may provide a
286 template to be used by customizing the variable
287 =org-capture-default-template= [fn:1].
289 The problem with this solution would be, that only one template can be used
290 with the fuction. Luckily, =org-protocol-capture= understands a slightly
291 extended syntax to choose between several templates: If the first field of
292 the data submitted is exactly one character in length, this character will
293 be used to select the template.
295 Here we choose to use the "=x=" template:
297 : emacsclient org-protocol:/capture:/x/URL/TITLE/BODY
299 And, again, as bookmark location:
300 : javascript:location.href='org-protocol://capture://x/'+
301 : encodeURIComponent(location.href)+'/'+
302 : encodeURIComponent(document.title)+'/'+
303 : encodeURIComponent(window.getSelection())
305 # <<example-template>>
306 ***** An example capture template
308 #+begin_src emacs-lisp
309 (setq org-capture-templates
314 (file+headline "~/org/capture.org" "Notes")
315 "* %^{Title}\n\n Source: %u, %c\n\n %i"
317 ;; ... more templates here ...
321 - "=w=" :: makes this one the default template used for
322 "=org-protocol://capture://=" URLs.
323 - =entry= :: makes it a regular entry with a headline.
324 - =file+headline= :: files the note in file "=~/org/capture.org=" as child of
325 the headline "=Notes="
326 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
327 page you have been visiting when clicking on the link. The page
328 title will be the link's description.
329 - '=%i=' :: will be replaced by the selected text in your browser window if
332 In addition, you may use the following placeholders in your template:
334 | Placeholders | Replacement |
335 |---------------+---------------------------|
336 | =%:link= | URL of the web-page |
337 | =%:description= | The title of the web-page |
338 | =%:initial= | Selected text. |
340 You may read more about templates and their special escape characters in the
341 [[http://orgmode.org/manual/Capture-templates.html#Capture-templates][Org-mode manual]].
344 # <<org-protocol-remember>>
345 *** Org-protocol-remember
347 The =org-protocol-remember= handler is now obsolete. However, the handler is
348 still available for backward compatibility. To use this handler, closely
349 follow the setup for the current =org-protocol-capture= handler, and simply
350 replace each occurrence of /capture/ with /remember/.
352 As remember templates look slightly different than capture templates, we
353 provide an example here.
355 # <<example-remember-template>>
356 ***** An example remember template
358 #+begin_src emacs-lisp
359 (setq org-remember-templates
360 '((?w "* %^{Title}\n\n Source: %u, %c\n\n %i" nil "Notes")))
363 - '=?w=' :: makes this one the default template used for
364 "=org-protocol://remember://=" URLs.
365 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
366 page you have been visiting when clicking on the link. The page
367 title will be the link's description.
368 - '=%i=' :: will be replaced by the selected text in your browser window if
371 In addition, you may use the following placeholders in your template:
373 | Placeholders | Replacement |
374 |---------------+---------------------------|
375 | =%:link= | URL of the web-page |
376 | =%:description= | The title of the web-page |
377 | =%:initial= | Selected text. |
379 You may read more about templates and their special escape characters in the
380 [[http://orgmode.org/manual/Capture-templates.html#Capture-templates][Org-mode manual]].
383 * Edit published content: =org-protocol-open-source=
385 This one was designed to help with opening sources for editing when browsing
386 in the first place. =org-protocol-open-source= uses the custom variable
387 =org-protocol-project-alist= to map URLs to (local) filenames.
389 Let's take http://orgmode.org/worg/ as our example.
391 Our intention is to click a bookmark (or link) to open the source of the
392 published file we are reading in our favourite editor. The bookmark-URL above
393 could be used again. But since =org-protocol-open-source= regards the first
394 field only, this here will do:
396 : javascript:location.href='org-protocol://open-source://'+encodeURIComponent(location.href)
398 To open files publihed on Worg locally, =org-protocol-project-alist= should look
399 like this (you may skip the second project):
401 #+begin_src emacs-lisp
402 (setq org-protocol-project-alist
404 :base-url "http://orgmode.org/worg/"
405 :working-directory "/home/user/worg/"
406 :online-suffix ".html"
407 :working-suffix ".org")
408 ("My local Org-notes"
409 :base-url "http://localhost/org/"
410 :working-directory "/home/user/org/"
411 :online-suffix ".php"
412 :working-suffix ".org")))
415 If you're now browsing http://orgmode.org/worg/org-contrib/org-protocol.html
416 and find a typo or have an idea how to enhance the documentation, simply click
417 the bookmark and start editing.
419 There are two functions to help you fill =org-protocol-project-alist= with
420 valid contents. One possibility is =org-protocol-create= that guides you through
421 the process. If you're editing an Org-mode file that is part of a publishing
422 project in =org-publish-project-alist=, try
424 : M-x org-protocol-create-for-org RET
426 # <<open-source-rewritten-urls>>
427 *** Handle rewritten URLs
429 In some cases, replacing =:base-url= with =:working-directory= and
430 =:online-suffix= with =:working-suffix= will not yield the desired results.
432 Suppose you maintain an online store located at =http://example.com/=. The
433 local sources reside in =/home/user/example/=. While most of the URLs map
434 directly to local file names by stripping URL parameters from the end and
435 replacing the =:base-url= with =:working-diretory= and =:online-suffix= with
436 =:working-suffix=, this might not work for rewritten URLs. It's common
437 practice to serve all products in such a store through one file and rewrite
438 URLs that do not match an existing file on the server.
440 That way, a request to =http://example.com/print/posters-A4.html= might be
441 rewritten on the server to something like
442 =http://example.com/shop/products.php/posters-A4.html.php=, where
443 =/posters-A4-digital.html.php= is the so called path info. Note that the
444 browser will not notice the rewrite.
446 If you now click your =org-protocol://open-source://= bookmark, the handler
447 will probably not find a file named
448 =/home/user/example/print/posters-A4.html.php= and fail.
450 Or, even more simple, assume you're browsing =http://example.com/=. A file
451 named =/home/user/example/.php= is not likely to exist.
453 Since Org-mode commit =69b46e10aab3b2374ecbc1a963ba56e77102a9a4= from 15th
454 Nov. 2009, such an entry in =org-protocol-project-alist= may hold an
455 additional property =:rewrites=. This property is a list of cons cells, each
456 of which maps a regular expression to a path relative to the
457 =:working-directory=.
459 Now map the URL to the path =/home/user/example/products.php= by adding the
460 =:rewrites= property like this:
462 #+begin_src emacs-lisp
463 (setq org-protocol-project-alist
465 :base-url "http://example.com/"
466 :working-directory "/home/user/example/"
467 :online-suffix ".php"
468 :working-suffix ".php"
469 :rewrites (("example.com/print/" . "products.php")
470 ("example.com/$" . "index.php"))
474 Guess what the second =:rewrites= element does. Since =example.com/$= is used as
475 a regular expression, it maps =http://example.com/=, =https://example.com=,
476 =http://www.example.com/= and similar to =/home/user/example/index.php=.
478 The =:rewrites= are searched as a last resort if and only if no existing file
485 Setting up org-protocol in [[http://conkeror.org/][Conkeror]] (an emacs inspired Mozilla web
486 browser) requires a slightly different method. You may simply add the
487 following snippets of code to your .conkerorrc file.[fn:tassilosblog]
489 For org-store-link, add the following to .conkerorrc:
491 : function org_store_link (url, title, window) {
492 : var cmd_str = 'emacsclient \"org-protocol://store-link://'+url+'/'+title+'\"';
493 : if (window != null) {
494 : window.minibuffer.message('Issuing ' + cmd_str);
496 : shell_command_blind(cmd_str);
499 : interactive("org-store-link", "Stores [[url][title]] as org link and copies url to emacs kill ring",
501 : org_store_link(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), I.window);
504 For org-capture (or org-remember --- just exchange /capture/ with /remember/), use
507 : function org_capture (url, title, selection, window) {
508 : var cmd_str = 'emacsclient \"org-protocol://capture://'+url+'/'+title+'/'+selection+'\"';
509 : if (window != null) {
510 : window.minibuffer.message('Issuing ' + cmd_str);
512 : shell_command_blind(cmd_str);
515 : interactive("org-capture", "Clip url, title, and selection to capture via org-protocol",
517 : org_capture(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), encodeURIComponent(I.buffer.top_frame.getSelection()), I.window);
520 Now, you should be able to invoke the commands from within conkeror
521 with =M-x org-store-link= and =M-x org-capture= (or remember).
523 Or, if you'd like your familiar emacs keybindings, you can add the
524 following to your .conkerorrc:
526 : define_key(content_buffer_normal_keymap, "C-c r", "org-capture");
527 : define_key(content_buffer_normal_keymap, "C-c l", "org-store-link");
529 [fn:tassilosblog] Adapted from Tassilo Horn's blog, "Calling
530 org-remember from inside conkeror," November 14, 2008.
531 http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/
538 Uzbl is a minimalistic webkit browser for Unix/Linux.
540 - [[http://www.uzbl.org/]]
542 You can pass encoded url data from uzbl to org-protocol by adding the
543 following lines to =.config/uzbl/config=.
549 @cbind \\r = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@/\@<document.getSelection()>\@"'
550 @cbind \\l = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@"'
554 These bind org-protocol-capture and org-store-line to "\r" and "\l" respectively.
556 # <<firefox-keybindings>>
557 * Keybindings for Firefox
559 You can add key bindings for the =org-protocol= commands using the keyconfig
562 First, install keyconfig from http://mozilla.dorando.at/keyconfig.xpi.
564 Open the keyconfig dialog by going to Tools and then Keyconfig.
566 Click the 'Add a new Key' button. Enter "Org store link" as the name.
567 Enter the following in the box with /* CODE */ in it:
569 : var orgProtoString = 'org-protocol://store-link://'+
570 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
571 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
572 : encodeURIComponent(gBrowser.contentWindow.getSelection());
574 : gBrowser.loadURI(orgProtoString);
576 Click OK. You will then need to bind a key by clicking in the box next to the
577 'Apply' button and pressing whatever key combination you want. Click 'Apply' to
578 store the keybinding.
580 Repeat the steps, but call the next key "Org capture" and use the code below:
582 : var orgProtoString = 'org-protocol://capture://'+
583 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
584 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
585 : encodeURIComponent(content.window.getSelection());
587 : gBrowser.loadURI(orgProtoString);
589 Click Close, then OK, and then restart Firefox. You should then be able to
590 access the org-protocol functions with your chosen keys.
592 # <<screencast-intro>>
593 * Screencast: small introduction to org-protocol.el
596 <object width="640" height="464"><param name="allowfullscreen"
597 value="true" /><param name="allowscriptaccess" value="always" /><param
599 value="http://vimeo.com/moogaloop.swf?clip_id=5662410&server=vimeo.com&show_title=1&show_byline=1&show_portrait=1&color=FF7700&fullscreen=1"
601 src="http://vimeo.com/moogaloop.swf?clip_id=5662410&server=vimeo.com&show_title=1&show_byline=1&show_portrait=1&color=FF7700&fullscreen=1"
602 type="application/x-shockwave-flash" allowfullscreen="true"
603 allowscriptaccess="always" width="640" height="464"></embed></object>
606 This screencast shows off some nice things you can do with Firefox,
607 Emacs, Org-mode and org-protocol.el.
609 It first shows how to create two bookmarklets, =org-capture= and
610 =org-store-link=. These bookmarklets enable your Firefox to talk to
611 emacsclient via a new protocol (=org-protocol://=); emacsclient then
612 parses the request and tells Emacs to capture or store stuff at the
613 relevant places in your Org files.
615 At the end of the screencast, we create two ubiquity commands from these
616 bookmarklets. Now in Firefox =ALT-SPC org-capture RET= creates a note
622 [fn:1] Before commit =fc49c1ec96b2c789f573ae1ba936b930a8494402=, 3rd Sept. 2010,
623 if a template with the key string "=w=" was defined, this one was chosen by
624 default. This was done to make bookmarks used for [[file:./org-annotation-helper.el][org-annotation-helper]] work
625 without changing the template.