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 assossiated with =sub-protocol=
39 through the custom variable =org-protocol-protocol-alist=.
41 It comes with three 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> =org-protocol-remember= is now obsoleted 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
73 - To load org-protocol.el add the following to your =.emacs=:
76 : (add-to-list 'load-path "~/path/to/org/protocol/")
77 : (require 'org-protocol)
79 * Browser / system setup
81 Windows users proceed to the section [[windows-setup][Windows]].
86 As of March 2009 Firefox users follow the steps documented on
87 http://kb.mozillazine.org/Register_protocol. Here is a summary:
89 1. Type "=about:config=" into the location bar and press enter.
90 2. Click "/I'll be careful, I promise!/" to continue.
91 3. Right-click on the grid
92 4. Choose "/New/" -> "/String/" from the context menu.
93 5. Enter "=network.protocol-handler.app.org-protocol=" as the properties name.
95 7. Leave the value blank.
96 8. Next time you try to open a location "=org-protocol://...=" FF will ask you for
97 the program to use. Enter the path to emacsclient.
102 :CUSTOM_ID: acrobat-reader-setup
105 Adapted from [[http://article.gmane.org/gmane.emacs.orgmode/6810]]
107 You place a javascript file for each menu entry in
108 =~/.adobe/Acrobat/<VERSION>/JavaScripts= on unix-like systems or
109 =c:/Program Files/Adobe/Acrobat <VERSION>/Reader/Javascripts/= on
110 Windows, or wherever your Adobe Reader Installation might look for
113 The examples given here will place new menu entries in the "Tools"
114 menu, after restarting Adobe Reader.
116 # <<acrobat-store-link-js>>
117 ***** org-store-link.js
118 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
119 : app.addMenuItem({cName:"org-store-link", cParent:"Tools",
120 : cExec:"app.launchURL('org-protocol://store-link://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title));"});
122 # <<acrobat-capture-js>>
124 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
125 : app.addMenuItem({cName:"org-capture", cParent:"Tools",
126 : cExec:"app.launchURL('org-protocol://capture://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
128 And this one, if you still use remember templates:
130 # <<acrobat-remember-js>>
131 ***** org-remember.js
132 : // from http://article.gmane.org/gmane.emacs.orgmode/6810
133 : app.addMenuItem({cName:"org-remember", cParent:"Tools",
134 : cExec:"app.launchURL('org-protocol://remember://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
140 Opera setup is described here:
141 http://www.opera.com/support/kb/view/535/.
143 To set up opera for use with org-protocol, follow these steps:
145 1. Choose "/Tools/" -> "/Prefences/" from the menu.
146 2. Select the tab "/Advanced/".
147 3. Choose "/Programs/" from the list on the left.
148 4. Now click the button "/Add/" on the very right.
149 5. In the new dialog window, enter "=org-protocol=" as "/Protocol/", choose the
150 radio button "/Open with other application/" and enter the path to
156 Windows users may register the "=org-protocol=" once for all by adjusting the
157 following to their facts, save it as *.reg file and double-click it. This
158 worked for me on Windows-XP Professional and the emasc23 from ourcomments.org
159 ([[http://ourcomments.org/cgi-bin/emacsw32-dl-latest.pl]]). I'm no Windows user
160 though and enhancements are more than welcome on the org-mode mailinglist. The
161 original file is from http://kb.mozillazine.org/Register_protocol.
166 [HKEY_CLASSES_ROOT\org-protocol]
169 [HKEY_CLASSES_ROOT\org-protocol\shell]
170 [HKEY_CLASSES_ROOT\org-protocol\shell\open]
171 [HKEY_CLASSES_ROOT\org-protocol\shell\open\command]
172 @="\"C:\\Programme\\Emacs\\emacs\\bin\\emacsclientw.exe\" \"%1\""
176 # <<test-org-protocol>>
177 *** Verify the installation
179 After your protocol is registered with your browse/OS, these links here
180 should work. Click on them and see if emacs reacts:
184 <li><a href="javascript:storeLink();">Org store-link</a></li>
185 <li><a href="javascript:capture();">Org capture (select some text if you like)</a></li>
186 <li><a href="javascript:remember();">Org remember (select some text please)</a></li>
191 # <<default-location>>
194 To actually use org-protocol add a bookmark to Firefox or opera.
196 Here is the URL to use as "/Location/" for browser bookmarks. Just remove the
197 line breaks and replace "=sub-protocol=" with the real sub-protocol to use:
199 : javascript:location.href='org-protocol://sub-protocol://'+
200 : encodeURIComponent(location.href)+'/'+
201 : encodeURIComponent(document.title)+'/'+
202 : encodeURIComponent(window.getSelection())
204 This URL may be used for all three standard handlers in =org-protocol.el=. Some
205 of the values will be ignored (e.g. =store-link:/= will use the URL and title
208 # <<org-protocol-store-link>>
209 * Links and bookmarks: =org-protocol-store-link=
211 =org-store-link= stores an Org-link insertable through =M-x org-insert-link= and
212 pushes the URL found on the =kill-ring= for yanking (=C-y=). The sub-protocol used
215 : emacsclient org-protocol:/store-link:/URL/TITLE
217 will store this Org-link:
223 In addition, =URL= will be pushed on the =kill-ring= for yanking ('=C-y='). You will
224 have to encode =URL= and/or =TITLE= if they contain slashes, and probably quote
227 To use this feature, add a bookmark with an arbitrary name (e.g.
228 "/Org: store-link/") and enter this as "=Location=":
230 : javascript:location.href='org-protocol://store-link://'+encodeURIComponent(location.href)
233 # <<org-protocol-capture>>
234 * Note taking and citations: =org-protocol-capture=
236 This one is triggered through the sub-protocol "=capture=" and consumes up to
239 : emacsclient org-protocol:/capture:/URL/TITLE/BODY
241 will pop up an /*Capture*/ buffer and fill the template with the data
244 To use this feature, add a bookmark with an arbitrary name (e.g.
245 "/Org: capture/") and enter this as "=Location=":
247 : javascript:location.href='org-protocol://capture://'+
248 : encodeURIComponent(location.href)+'/'+
249 : encodeURIComponent(document.title)+'/'+
250 : encodeURIComponent(window.getSelection())
252 The result depends on the template used. See [[example-template][An example capture template]]
255 Note, that this one, as opposed to the other two standard handlers, does not
256 mix with more parameters to emacsclient. All parameters but the
257 #'=org-protocol://org-capture://...=' one will be discarded.
259 # <<which-capture-template>>
260 *** Which capture template is used?
262 :ID: org:f56bcb9a-20e7-4b37-99f1-839a8821cc4b
265 You don't need to setup a capture template to use =org-protocol-capture=,
266 since Org-mode provides a default template for those cases. But, for
267 historical reasons, if a template with the key string "=w=" is defined, this
268 one will be choosen by default. This was done to make bookmarks used for
269 [[file:./org-annotation-helper.el][org-annotation-helper]] work without changing the template.
271 The problem with this solution would be, that only one template can be used
272 with the fuction. Luckily, =org-protocol-capture= understands a slightly
273 extended syntax to choose between several templates: If the first field of
274 the data submitted is exactly one character in length, this character will
275 be used to select the template.
277 Here we choose to use the "=x=" template:
279 : emacsclient org-protocol:/capture:/x/URL/TITLE/BODY
281 And, again, as bookmark location:
282 : javascript:location.href='org-protocol://capture://x/'+
283 : encodeURIComponent(location.href)+'/'+
284 : encodeURIComponent(document.title)+'/'+
285 : encodeURIComponent(window.getSelection())
287 # <<example-template>>
288 ***** An example capture template
290 #+begin_src emacs-lisp
291 (setq org-capture-templates
296 (file+headline "~/org/capture.org" "Notes")
297 "* %^{Title}\n\n Source: %u, %c\n\n %i"
299 ;; ... more templates here ...
303 - "=w=" :: makes this one the default template used for
304 "=org-protocol://capture://=" URLs.
305 - =entry= :: makes it a regular entry with a headline.
306 - =file+headline= :: files the note in file "=~/org/capture.org=" as child of
307 the headline "=Notes="
308 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
309 page you have been visiting when clicking on the link. The page
310 title will be the links description.
311 - '=%i=' :: will be replaced by the selected text in your browser window if
314 In addition, you may use the following placeholders in your template:
316 | Placeholders | Replacement |
317 |---------------+---------------------------|
318 | =%:link= | URL of the web-page |
319 | =%:description= | The title of the web-page |
320 | =%:initial= | Selected text. |
322 You may read more about templates and their special escape characters in the
323 [[http://orgmode.org/manual/Capture-templates.html#Capture-templates][Org-mode manual]].
326 # <<org-protocol-remember>>
327 *** Org-protocol-remember
329 The =org-protocol-remember= handler is now obsolete. How ever, the handler is
330 still available for backward compatibility. To use this handler, closely
331 follow the setup for the current =org-protocol-capture= handler, and simply
332 replace each occurrence of /capture/ with /remember/.
334 As remember templates look slightly different than capture templates, we
335 provide an example here.
337 # <<example-remember-template>>
338 ***** An example remember template
340 #+begin_src emacs-lisp
341 (setq org-remember-templates
342 '((?w "* %^{Title}\n\n Source: %u, %c\n\n %i" nil "Notes")))
345 - '=?w=' :: makes this one the default template used for
346 "=org-protocol://remember://=" URLs.
347 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
348 page you have been visiting when clicking on the link. The page
349 title will be the links description.
350 - '=%i=' :: will be replaced by the selected text in your browser window if
353 In addition, you may use the following placeholders in your template:
355 | Placeholders | Replacement |
356 |---------------+---------------------------|
357 | =%:link= | URL of the web-page |
358 | =%:description= | The title of the web-page |
359 | =%:initial= | Selected text. |
361 You may read more about templates and their special escape characters in the
362 [[http://orgmode.org/manual/Remember-templates.html#Remember-templates][Org-mode manual]].
365 * Edit published content: =org-protocol-open-source=
367 This one was designed to help with opening sources for editing when browsing
368 in the first place. =org-protocol-open-source= uses the custom variable
369 =org-protocol-project-alist= to map URLs to (local) filenames.
371 Let's take http://orgmode.org/worg/ as our example.
373 Our intention is to click a bookmark (or link) to open the source of the
374 published file we are reading in our favourite editor. The bookmark-URL above
375 could be used again. But since =org-protocol-open-source= regards the first
376 field only, this here will do:
378 : javascript:location.href='org-protocol://open-source://'+encodeURIComponent(location.href)
380 To open files publihed on Worg locally, =org-protocol-project-alist= should look
381 like this (you may skip the second project):
383 #+begin_src emacs-lisp
384 (setq org-protocol-project-alist
386 :base-url "http://orgmode.org/worg/"
387 :working-directory "/home/user/worg/"
388 :online-suffix ".html"
389 :working-suffix ".org")
390 ("My local Org-notes"
391 :base-url "http://localhost/org/"
392 :working-directory "/home/user/org/"
393 :online-suffix ".php"
394 :working-suffix ".org")))
397 If you're now browsing http://orgmode.org/worg/org-tutorials/org-protocol.php
398 and find a typo or have an idea how to enhance the documentation, simply click
399 the bookmark and start editing.
401 There are to functions to help you filling =org-protocol-project-alist= with
402 valid contents. First of which is =org-protocol-create= that guides you through
403 the process. If you're editing an Org-mode file that is part of a publishing
404 project in =org-publish-project-alist=, try
406 : M-x org-protocol-create-for-org RET
408 # <<open-source-rewritten-urls>>
409 *** Handle rewritten URLs
411 In some cases, replacing =:base-url= with =:working-directory= and
412 =:online-suffix= with =:working-suffix= will not yield the desired results.
414 Suppose you maintain an online store located at =http://example.com/=. The
415 local sources reside in =/home/user/example/=. While most of the URLs map
416 directly to local file names by stripping URL parameters from the end and
417 replacing the =:base-url= with =:working-diretory= and =:online-suffix= with
418 =working-suffix=, this might not work for rewritten URLs. It's common
419 practice, to serve all products in such a store through one file and rewrite
420 URLs, that do not match an existing file on the server.
422 That way, a request to =http://example.com/print/posters-A4.html= might be
423 rewritten on the server to something like
424 =http://example.com/shop/products.php/posters-A4.html.php=, where
425 =/posters-A4-digital.html.php= is the so called path info. Note, that the
426 browser will not notice the rewrite.
428 If you now click your =org-protocol://open-source://= bookmark, the handler
429 will probably not find a file named
430 =/home/user/example/print/posters-A4.html.php= and fail.
432 Or, even more simple, assume your browsing to =http://example.com/=. A file
433 named =/home/user/example/.php= is not likely to exist.
435 Since Org-mode commit =69b46e10aab3b2374ecbc1a963ba56e77102a9a4= from 15nth
436 Nov. 2009, such an entry in =org-protocol-project-alist= may hold an
437 additional property =:rewrites=. This property is a list of cons cells, each
438 of which maps a regular expression to a path, relative to the
439 =:working-directory=.
441 Now map the URL to the path =/home/user/example/products.php= by adding the
442 =:rewrites= property like this:
444 #+begin_src emacs-lisp
445 (setq org-protocol-project-alist
447 :base-url "http://example.com/"
448 :working-directory "/home/user/example/"
449 :online-suffix ".php"
450 :working-suffix ".php"
451 :rewrites (("example.com/print/" . "products.php")
452 ("example.com/$" . "index.php"))
456 Guess what the second =:rewrites= element does. Since =example.com/$= is used as
457 a regular expression, it maps =http://example.com/=, =https://example.com=,
458 =http://www.example.com/= and similar to =/home/user/example/index.php=.
460 The =:rewrites= are searched as a last resort if, and only if no existing file
467 Setting up org-protocol in [[http://conkeror.org/][Conkeror]] (an emacs inspired Mozilla web
468 browser) requires a slightly different method. You may simply add the
469 following snippets of code to your .conkerorrc file.[fn:tassilosblog]
471 For org-store-link, add the following to .conkerorrc:
473 : function org_store_link (url, title, window) {
474 : var cmd_str = 'emacsclient \"org-protocol://store-link://'+url+'/'+title+'\"';
475 : if (window != null) {
476 : window.minibuffer.message('Issuing ' + cmd_str);
478 : shell_command_blind(cmd_str);
481 : interactive("org-store-link", "Stores [[url][title]] as org link and copies url to emacs kill ring",
483 : org_store_link(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), I.window);
486 For org-capture (or org-remember --- just exchange /capture/ with /remember/), use
489 : function org_capture (url, title, selection, window) {
490 : var cmd_str = 'emacsclient \"org-protocol://capture://'+url+'/'+title+'/'+selection+'\"';
491 : if (window != null) {
492 : window.minibuffer.message('Issuing ' + cmd_str);
494 : shell_command_blind(cmd_str);
497 : interactive("org-capture", "Clip url, title, and selection to capture via org-protocol",
499 : org_capture(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), encodeURIComponent(I.buffer.top_frame.getSelection()), I.window);
502 Now, you should be able to invoke the commands from within conkeror
503 with =M-x org-store-link= and =M-x org-capture= (or remember).
505 Or, if you'd like your familiar emacs keybindings, you can add the
506 following to your .conkerorrc:
508 : define_key(content_buffer_normal_keymap, "C-c r", "org-capture");
509 : define_key(content_buffer_normal_keymap, "C-c l", "org-store-link");
511 [fn:tassilosblog] Adapted from Tassilo Horn's blog, "Calling
512 org-remember from inside conkeror," November 14, 2008.
513 http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/
520 Uzbl is a minimalistic webkit browser for Unix/Linux.
522 - [[http://www.uzbl.org/]]
524 You can pass encoded url data from uzbl to org-protocol by adding the
525 following lines to =.config/uzbl/config=.
531 @cbind \\r = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@/\@<document.getSelection()>\@"'
532 @cbind \\l = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@"'
536 These bind org-protocol-capture and org-store-line to "\r" and "\l" respectively.
538 # <<firefox-keybindings>>
539 * Keybindings for Firefox
541 You can add key bindings for the =org-protocol= commands using the keyconfig
544 First, install keyconfig from http://mozilla.dorando.at/keyconfig.xpi.
546 Open the keyconfig dialog by going to Tools and then Keyconfig.
548 Click the 'Add a new Key' button. Enter "Org store link" as the name.
549 Enter the following in the box with /* CODE */ in it:
551 : var orgProtoString = 'org-protocol://store-link://'+
552 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
553 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
554 : encodeURIComponent(window.getSelection());
556 : gBrowser.loadURI(orgProtoString);
558 Click OK. You will then need to bind a key by clicking in the box next to the
559 'Apply' button and pressing whatever key combination you want. Click 'Apply' to
560 store the keybinding.
562 Repeat the steps, but call the next key "Org capture" and use the code below:
564 : var orgProtoString = 'org-protocol://capture://'+
565 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
566 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
567 : encodeURIComponent(window.getSelection());
569 : gBrowser.loadURI(orgProtoString);
571 Click Close, then OK, and then restart Firefox. You should then be able to
572 access the org-protocol functions with your chosen keys.
574 # <<screencast-intro>>
575 * Screencast: small introduction to org-protocol.el
578 <object width="640" height="464"><param name="allowfullscreen"
579 value="true" /><param name="allowscriptaccess" value="always" /><param
581 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"
583 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"
584 type="application/x-shockwave-flash" allowfullscreen="true"
585 allowscriptaccess="always" width="640" height="464"></embed></object>
588 This screencast shows off some nice things you can do with Firefox,
589 Emacs, Org-mode and org-protocol.el.
591 It first shows how to create two bookmarklets, =org-capture= and
592 =org-store-link=. These bookmarklets enable your Firefox to talk to
593 emacsclient via a new protocol (=org-protocol://=); emacsclient then
594 parses the request and tells Emacs to capture or store stuff at the
595 relevant places in your Org files.
597 At the end of the screencast, we create two ubiquity commands from these
598 bookmarklets. Now in Firefox =ALT-SPC org-capture RET= creates a note