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 f:t *:t tex:t d:(HIDE) tags:not-in-toc author:nil
4 #+SETUPFILE: ../macros.setupfile
5 #+HTML_HEAD_EXTRA: <script type="text/javascript">
6 #+HTML_HEAD_EXTRA: <!--/*--><![CDATA[/*><!--*/
7 #+HTML_HEAD_EXTRA: function makeUrl() {
8 #+HTML_HEAD_EXTRA: return encodeURIComponent(location.href)+
9 #+HTML_HEAD_EXTRA: '/'+encodeURIComponent(document.title)+
10 #+HTML_HEAD_EXTRA: '/'+encodeURIComponent(window.getSelection());
12 #+HTML_HEAD_EXTRA: function storeLink() {
13 #+HTML_HEAD_EXTRA: document.location.href='org-protocol://store-link://'+makeUrl();
15 #+HTML_HEAD_EXTRA: function remember() {
16 #+HTML_HEAD_EXTRA: document.location.href='org-protocol://remember://'+makeUrl();
18 #+HTML_HEAD_EXTRA: function capture() {
19 #+HTML_HEAD_EXTRA: document.location.href='org-protocol://capture://'+makeUrl();
21 #+HTML_HEAD_EXTRA: /*]]>*///-->
22 #+HTML_HEAD_EXTRA: </script>
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'.
32 * About org-protocol.el
34 =org-protocol.el= is based on code and ideas from
35 [[file:./org-annotation-helper.org][org-annotation-helper.el]] and =org-browser-url.el=.
37 "=org-protocol:/sub-protocol:/=" triggers actions associated with =sub-protocol=
38 through the custom variable =org-protocol-protocol-alist=.
40 It comes with four predefined handlers:
41 - =org-protocol-store-link= ::
42 triggered through the sub-protocol "=store-link=". Stores an Org-link and
43 pushes the URL to the =kill-ring=.
44 - =org-protocol-capture= ::
45 Fill a =CAPTURE= buffer with information gathered somewhere else. This
46 handler is triggered through the "=capture=" sub-protocol and uses the
47 function =org-capture=.
48 - =org-protocol-remember= ::
49 Fills a remember buffer with information gathered somewhere else. This
50 handler is triggered through the "=remember=" sub-protocol and still
51 available for backward compatibility. This handler uses =org-remember=. Use
52 the current =org-protocol-capture=.
54 - =org-protocol-open-source= ::
55 "=open-source=". Maps URLs to local filenames. Use this to open sources of
56 already published contents in emacs for editing.
58 =org-protocol= helps creating custom handlers [[file:../org-tutorials/org-protocol-custom-handler.org][(tutorial)]] and so called
59 =org-protocol-projects=.
62 @<b>As of Org mode release 7.01 =org-protocol-remember= is now by =org-protocol-capture=.@</b>
63 If not stated otherwise, you may simply replace each occurrence of
64 /capture/ with /remember/ throughout this document, if you still want to use
65 remember templates. Use =M-x org-version= to find out about the version you're
71 - To load org-protocol.el add the following to your =.emacs=:
74 : (add-to-list 'load-path "~/path/to/org/protocol/")
75 : (require 'org-protocol)
77 * Browser / system setup
79 - [[Linux setup (Gnome)]]
80 - [[Linux setup (KDE)]]
83 - [[*Applications][Applications]]
84 - [[*Verify the installation][Verify the installation]]
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
95 Add a file =org.protocol= to =~/.kde/share/kde4/services/=:
100 protocol=org-protocol
101 exec=/usr/bin/emacsclient '%u'
111 Description=A protocol for org-mode
116 Windows users may register the "=org-protocol=" once for all by adjusting the
117 following to their facts, save it as *.reg file and double-click it. This
118 worked for me on Windows-XP Professional and the emasc23 from ourcomments.org
119 ([[http://ourcomments.org/cgi-bin/emacsw32-dl-latest.pl]]). I'm no Windows user
120 though and enhancements are more than welcome on the org-mode mailinglist. The
121 original file is from http://kb.mozillazine.org/Register_protocol.
126 [HKEY_CLASSES_ROOT\org-protocol]
129 [HKEY_CLASSES_ROOT\org-protocol\shell]
130 [HKEY_CLASSES_ROOT\org-protocol\shell\open]
131 [HKEY_CLASSES_ROOT\org-protocol\shell\open\command]
132 @="\"C:\\Programme\\Emacs\\emacs\\bin\\emacsclientw.exe\" \"%1\""
137 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.
139 After installing EmacsClient.app you should then [[Verify the installation]]. Once verified, you can begin [[Using org-protocol]].
144 If you are using Firefox on Mac OS X, see [[Mac OS X setup]].
147 Please refer to http://kb.mozillazine.org/Register_protocol and use
148 "org-protocol" as protocol.
152 :CUSTOM_ID: acrobat-reader-setup
155 Adapted from [[https://orgmode.org/list/loom.20080527T012114-502@post.gmane.org]]
157 You place a javascript file for each menu entry in
158 =~/.adobe/Acrobat/<VERSION>/JavaScripts= on unix-like systems or
159 =c:/Program Files/Adobe/Acrobat <VERSION>/Reader/Javascripts/= on
160 Windows, or wherever your Adobe Reader Installation might look for
163 The examples given here will place new menu entries in the "Tools"
164 menu, after restarting Adobe Reader.
166 ******* org-store-link.js
167 : // from https://orgmode.org/list/loom.20080527T012114-502@post.gmane.org
168 : app.addMenuItem({cName:"org-store-link", cParent:"Tools",
169 : cExec:"app.launchURL('org-protocol://store-link://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title));"});
171 ******* org-capture.js
172 : // from https://orgmode.org/list/loom.20080527T012114-502@post.gmane.org
173 : app.addMenuItem({cName:"org-capture", cParent:"Tools",
174 : cExec:"app.launchURL('org-protocol://capture://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
176 And this one, if you still use remember templates:
178 ******* org-remember.js
179 : // from https://orgmode.org/list/loom.20080527T012114-502@post.gmane.org
180 : app.addMenuItem({cName:"org-remember", cParent:"Tools",
181 : cExec:"app.launchURL('org-protocol://remember://' + encodeURIComponent(this.URL) + '/' + encodeURIComponent(this.info.Title) + '/');"});
185 If you are using Opera on Mac OS X, see [[Mac OS X setup]].
187 Opera setup is described here:
188 http://www.opera.com/support/kb/view/535/.
190 To set up opera for use with org-protocol, follow these steps:
192 1. Choose "/Tools/" -> "/Prefences/" from the menu.
193 2. Select the tab "/Advanced/".
194 3. Choose "/Programs/" from the list on the left.
195 4. Now click the button "/Add/" on the very right.
196 5. In the new dialog window, enter "=org-protocol=" as "/Protocol/", choose the
197 radio button "/Open with other application/" and enter the path to
201 Safari is only supported on Mac systems, not iOS systems.
203 See [[Mac OS X setup]] for directions.
205 *** Verify the installation
207 After your protocol is registered with your browser/OS, these links here
208 should work. Click on them and see if emacs reacts:
212 <li><a href="javascript:storeLink();">Org store-link</a></li>
213 <li><a href="javascript:capture();">Org capture (select some text if you like)</a></li>
214 <li><a href="javascript:remember();">Org remember (select some text please)</a></li>
220 To actually use org-protocol add a bookmark to Firefox or Opera.
222 Here is the URL to use as "/Location/" for browser bookmarks. Just remove the
223 line breaks and replace "=sub-protocol=" with the real sub-protocol to use:
225 : javascript:location.href='org-protocol://sub-protocol://'+
226 : encodeURIComponent(location.href)+'/'+
227 : encodeURIComponent(document.title)+'/'+
228 : encodeURIComponent(window.getSelection())
230 This URL may be used for all three standard handlers in =org-protocol.el=. Some
231 of the values will be ignored (e.g. =store-link:/= will use the URL and title
234 * Links and bookmarks: =org-protocol-store-link=
236 =org-store-link= stores an Org-link insertable through =M-x org-insert-link= and
237 pushes the URL found onto the =kill-ring= for yanking (=C-y=). The sub-protocol
238 used is "=store-link=":
240 : emacsclient org-protocol:/store-link:/URL/TITLE
242 will store this Org-link:
248 In addition, =URL= will be pushed on the =kill-ring= for yanking ('=C-y='). You will
249 have to encode =URL= and/or =TITLE= if they contain slashes, and probably quote
252 To use this feature, add a bookmark with an arbitrary name (e.g.
253 "/Org: store-link/") and enter this as "=Location=":
255 : javascript:location.href='org-protocol://store-link://'+encodeURIComponent(location.href)
257 * Note taking and citations: =org-protocol-capture=
259 This one is triggered through the sub-protocol "=capture=" and consumes up to
262 : emacsclient org-protocol:/capture:/URL/TITLE/BODY
264 will pop up an /*Capture*/ buffer and fill the template with the data
267 To use this feature, add a bookmark with an arbitrary name (e.g.
268 "/Org: capture/") and enter this as "=Location=":
270 : javascript:location.href='org-protocol://capture://'+
271 : encodeURIComponent(location.href)+'/'+
272 : encodeURIComponent(document.title)+'/'+
273 : encodeURIComponent(window.getSelection())
275 The result depends on the template used. See [[example-template][An example capture template]]
278 Note, that this one, as opposed to the other two standard handlers, does not
279 mix with more parameters to emacsclient. All parameters but the
280 #'=org-protocol://org-capture://...=' one will be discarded.
282 *** Which capture template is used?
284 :ID: org:f56bcb9a-20e7-4b37-99f1-839a8821cc4b
287 You don't need to setup a capture template to use
288 =org-protocol-capture=, since Org-mode provides a default template
289 for those cases. Newer versions provide an interactive interface
290 for choosing a template. You may provide a template to be used by
291 customizing the variable =org-capture-default-template= [fn:1].
293 The problem with this solution would be, that only one template
294 can be used with the fuction. Luckily, =org-protocol-capture=
295 understands a slightly extended syntax to choose between several
296 templates: If the first field of the data submitted is exactly one
297 character in length, this character will be used to select the
300 Here we choose to use the "=x=" template:
302 : emacsclient org-protocol:/capture:/x/URL/TITLE/BODY
304 And, again, as bookmark location:
305 : javascript:location.href='org-protocol://capture://x/'+
306 : encodeURIComponent(location.href)+'/'+
307 : encodeURIComponent(document.title)+'/'+
308 : encodeURIComponent(window.getSelection())
310 ***** An example capture template
312 #+begin_src emacs-lisp
313 (setq org-capture-templates
318 (file+headline "~/org/capture.org" "Notes")
319 "* %^{Title}\n\n Source: %u, %c\n\n %i"
321 ;; ... more templates here ...
325 - "=w=" :: makes this one the default template used for
326 "=org-protocol://capture://=" URLs.
327 - =entry= :: makes it a regular entry with a headline.
328 - =file+headline= :: files the note in file "=~/org/capture.org=" as child of
329 the headline "=Notes="
330 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
331 page you have been visiting when clicking on the link. The page
332 title will be the link's description.
333 - '=%i=' :: will be replaced by the selected text in your browser window if
336 In addition, you may use the following placeholders in your template:
338 | Placeholders | Replacement |
339 |---------------+---------------------------|
340 | =%:link= | URL of the web-page |
341 | =%:description= | The title of the web-page |
342 | =%:initial= | Selected text. |
344 You may read more about templates and their special escape characters in the
345 [[https://orgmode.org/manual/Capture-templates.html#Capture-templates][Org-mode manual]].
347 *** Org-protocol-remember
349 The =org-protocol-remember= handler is now obsolete. However, the handler is
350 still available for backward compatibility. To use this handler, closely
351 follow the setup for the current =org-protocol-capture= handler, and simply
352 replace each occurrence of /capture/ with /remember/.
354 As remember templates look slightly different than capture templates, we
355 provide an example here.
357 ***** An example remember template
359 #+begin_src emacs-lisp
360 (setq org-remember-templates
361 '((?w "* %^{Title}\n\n Source: %u, %c\n\n %i" nil "Notes")))
364 - '=?w=' :: makes this one the default template used for
365 "=org-protocol://remember://=" URLs.
366 - '=%c=' :: will be replaced by an Org-link pointing to the location of the
367 page you have been visiting when clicking on the link. The page
368 title will be the link's description.
369 - '=%i=' :: will be replaced by the selected text in your browser window if
372 In addition, you may use the following placeholders in your template:
374 | Placeholders | Replacement |
375 |---------------+---------------------------|
376 | =%:link= | URL of the web-page |
377 | =%:description= | The title of the web-page |
378 | =%:initial= | Selected text. |
380 You may read more about templates and their special escape characters in the
381 [[https://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 https://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 "https://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 https://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 *** Handle rewritten URLs
428 In some cases, replacing =:base-url= with =:working-directory= and
429 =:online-suffix= with =:working-suffix= will not yield the desired results.
431 Suppose you maintain an online store located at =http://example.com/=. The
432 local sources reside in =/home/user/example/=. While most of the URLs map
433 directly to local file names by stripping URL parameters from the end and
434 replacing the =:base-url= with =:working-diretory= and =:online-suffix= with
435 =:working-suffix=, this might not work for rewritten URLs. It's common
436 practice to serve all products in such a store through one file and rewrite
437 URLs that do not match an existing file on the server.
439 That way, a request to =http://example.com/print/posters-A4.html= might be
440 rewritten on the server to something like
441 =http://example.com/shop/products.php/posters-A4.html.php=, where
442 =/posters-A4-digital.html.php= is the so called path info. Note that the
443 browser will not notice the rewrite.
445 If you now click your =org-protocol://open-source://= bookmark, the handler
446 will probably not find a file named
447 =/home/user/example/print/posters-A4.html.php= and fail.
449 Or, even more simple, assume you're browsing =http://example.com/=. A file
450 named =/home/user/example/.php= is not likely to exist.
452 Since Org-mode commit =69b46e10aab3b2374ecbc1a963ba56e77102a9a4= from 15th
453 Nov. 2009, such an entry in =org-protocol-project-alist= may hold an
454 additional property =:rewrites=. This property is a list of cons cells, each
455 of which maps a regular expression to a path relative to the
456 =:working-directory=.
458 Now map the URL to the path =/home/user/example/products.php= by adding the
459 =:rewrites= property like this:
461 #+begin_src emacs-lisp
462 (setq org-protocol-project-alist
464 :base-url "http://example.com/"
465 :working-directory "/home/user/example/"
466 :online-suffix ".php"
467 :working-suffix ".php"
468 :rewrites (("example.com/print/" . "products.php")
469 ("example.com/$" . "index.php"))
473 Guess what the second =:rewrites= element does. Since =example.com/$= is used as
474 a regular expression, it maps =http://example.com/=, =https://example.com=,
475 =http://www.example.com/= and similar to =/home/user/example/index.php=.
477 The =:rewrites= are searched as a last resort if and only if no existing file
484 Setting up org-protocol in [[http://conkeror.org/][Conkeror]] (an emacs inspired Mozilla web
485 browser) requires a slightly different method. You may simply add the
486 following snippets of code to your .conkerorrc file[fn:2].
488 For org-store-link, add the following to .conkerorrc:
490 : function org_store_link (url, title, window) {
491 : var cmd_str = 'emacsclient \"org-protocol://store-link://'+url+'/'+title+'\"';
492 : if (window != null) {
493 : window.minibuffer.message('Issuing ' + cmd_str);
495 : shell_command_blind(cmd_str);
498 : interactive("org-store-link", "Stores [[url][title]] as org link and copies url to emacs kill ring",
500 : org_store_link(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), I.window);
503 For org-capture (or org-remember --- just exchange /capture/ with /remember/), use
506 : function org_capture (url, title, selection, window) {
507 : var cmd_str = 'emacsclient \"org-protocol://capture://'+url+'/'+title+'/'+selection+'\"';
508 : if (window != null) {
509 : window.minibuffer.message('Issuing ' + cmd_str);
511 : shell_command_blind(cmd_str);
514 : interactive("org-capture", "Clip url, title, and selection to capture via org-protocol",
516 : org_capture(encodeURIComponent(I.buffer.display_uri_string), encodeURIComponent(I.buffer.document.title), encodeURIComponent(I.buffer.top_frame.getSelection()), I.window);
519 Now, you should be able to invoke the commands from within conkeror
520 with =M-x org-store-link= and =M-x org-capture= (or remember).
522 Or, if you'd like your familiar emacs keybindings, you can add the
523 following to your .conkerorrc:
525 : define_key(content_buffer_normal_keymap, "C-c r", "org-capture");
526 : define_key(content_buffer_normal_keymap, "C-c l", "org-store-link");
533 Uzbl is a minimalistic webkit browser for Unix/Linux.
535 - [[http://www.uzbl.org/]]
537 You can pass encoded url data from uzbl to org-protocol by adding the
538 following lines to =.config/uzbl/config=.
544 @cbind \\r = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@/\@<document.getSelection()>\@"'
545 @cbind \\l = sh 'emacsclient "org-protocol://capture://\@<encodeURIComponent(window.location.href)>\@/\@<encodeURIComponent(document.title)>\@"'
549 These bind org-protocol-capture and org-store-line to "\r" and
552 * Keybindings for Firefox
554 You can add key bindings for the =org-protocol= commands using the
555 keyconfig Firefox extension.
557 First, install keyconfig from
558 http://mozilla.dorando.at/keyconfig.xpi.
560 Open the keyconfig dialog by going to Tools and then Keyconfig.
562 Click the 'Add a new Key' button. Enter "Org store link" as the name.
563 Enter the following in the box with /* CODE */ in it:
565 : var orgProtoString = 'org-protocol://store-link://'+
566 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
567 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
568 : encodeURIComponent(gBrowser.contentWindow.getSelection());
570 : gBrowser.loadURI(orgProtoString);
572 Click OK. You will then need to bind a key by clicking in the box
573 next to the 'Apply' button and pressing whatever key combination you
574 want. Click 'Apply' to store the keybinding.
576 Repeat the steps, but call the next key "Org capture" and use the
579 : var orgProtoString = 'org-protocol://capture://'+
580 : encodeURIComponent(gBrowser.currentURI.spec) + '/' +
581 : encodeURIComponent(gBrowser.contentWindow.document.title) + '/' +
582 : encodeURIComponent(content.window.getSelection());
584 : gBrowser.loadURI(orgProtoString);
586 Click Close, then OK, and then restart Firefox. You should then be
587 able to access the org-protocol functions with your chosen keys.
589 * Screencast: small introduction to org-protocol.el
592 <object width="640" height="464"><param name="allowfullscreen"
593 value="true" /><param name="allowscriptaccess" value="always" /><param
595 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"
597 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"
598 type="application/x-shockwave-flash" allowfullscreen="true"
599 allowscriptaccess="always" width="640" height="464"></embed></object>
602 This screencast shows off some nice things you can do with Firefox,
603 Emacs, Org-mode and org-protocol.el.
605 It first shows how to create two bookmarklets, =org-capture= and
606 =org-store-link=. These bookmarklets enable your Firefox to talk to
607 emacsclient via a new protocol (=org-protocol://=); emacsclient then
608 parses the request and tells Emacs to capture or store stuff at the
609 relevant places in your Org files.
611 At the end of the screencast, we create two ubiquity commands from
612 these bookmarklets. Now in Firefox =ALT-SPC org-capture RET= creates
613 a note in my Org files.
617 [fn:1] Before commit =fc49c1ec96b2c789f573ae1ba936b930a8494402=, 3rd
618 Sept. 2010, if a template with the key string "=w=" was defined, this
619 one was chosen by default. This was done to make bookmarks used for
620 [[file:./org-annotation-helper.org][org-annotation-helper]] work without changing the template.
622 [fn:2] Adapted from Tassilo Horn's [[http://tsdh.wordpress.com/2008/11/14/calling-org-remember-from-inside-conkeror/][blog post]], "Calling org-remember
623 from inside conkeror," November 14, 2008.