org-annotation-helper.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ;;; org-annotation-helper.el --- start remember from a web browser
  2. ;;
  3. ;; Author: bzg AT altern DOT org
  4. ;; Author: John Rakestraw
  5. ;; Author: dmg AT uvic DOT org
  6. ;;
  7. ;; Keywords: org remember
  8. ;;
  9. ;; Version 0.2, May 16, 2008
  10. ;;
  11. ;;; Commentary:
  12. ;;
  13. ;; [bzg:] This is an adapted version of the planner-mode extension the
  14. ;; was first posted by Geert Kloosterman <g.j.kloosterman@gmail.com> on
  15. ;; the Planner mailing list.
  16. ;;
  17. ;; [dmg:] I have updated and extended the function to allow for
  18. ;; handling of the selection (it is now available as a region, so it
  19. ;; can be used in a template using %i )
  20. ;;
  21. ;;
  22. ;; We want to be able to pass a URL and document title directly from a
  23. ;; web browser to Emacs.
  24. ;;
  25. ;; We define a remember:// url handler in the browser and use a shell
  26. ;; script to handle the protocol. This script passes the information
  27. ;; to a running Emacs process (using emacsclient/gnuclient). We use
  28. ;; bookmarklets to create the remember:// urls dynamicly.
  29. ;;
  30. ;; The protocol types currently recognized are:
  31. ;;
  32. ;; remember:// start `remember' with the url and title filled in
  33. ;; annotation:// similar to `planner-annotation-as-kill' (org?)
  34. ;;
  35. ;; The urls used internally will have the following form:
  36. ;;
  37. ;; remember://<the web page url>::remember::<the title>::remember::<selection>
  38. ;;
  39. ;; The title will be url-hex-encoded.
  40. ;;
  41. ;; The bookmarklets:
  42. ;;
  43. ;;----------------------------------------------------------------------
  44. ;; javascript:location.href='remember://' + location.href + \
  45. ;; '::remember::' + escape(document.title) + '::remember::' + escape(window.getSelection())
  46. ;;----------------------------------------------------------------------
  47. ;; javascript:location.href='annotation://' + location.href + '::remember::' +\
  48. ;; escape(document.title) ;;
  49. ;;----------------------------------------------------------------------
  50. ;;
  51. ;; The handler
  52. ;;
  53. ;;----------------------------------------------------------------------
  54. ;; #!/bin/sh
  55. ;; # org-annotation-helper -- pass a remember-url to emacs
  56. ;; #
  57. ;; # Author: Geert Kloosterman <g.j.kloosterman@gmail.com>
  58. ;; # Date: Sat Nov 19 22:33:18 2005
  59. ;;
  60. ;; if [ -z "$1" ]; then
  61. ;; echo "$0: Error: no arguments given!" 1>&2
  62. ;; exit 1
  63. ;; fi
  64. ;;
  65. ;; # To test uncomment following line
  66. ;; #echo $1 >> /tmp/remember.out
  67. ;;
  68. ;; emacsclient --eval "(progn (bzg/org-annotation-helper \"$1\" ) nil)"
  69. ;;----------------------------------------------------------------------
  70. ;;
  71. ;; To install:
  72. ;;
  73. ;; Step 0: Install this module
  74. ;;
  75. ;; * Install this script and require it in your .emacs (or wherever you
  76. ;; want to do it)
  77. ;;
  78. ;; (require 'org-annotation-helper)
  79. ;;
  80. ;;
  81. ;; Step 1: Install the remember script
  82. ;;
  83. ;; * Save the handler as a script, and make sure it is executable, i.e.
  84. ;; remember
  85. ;; * Try it:
  86. ;; Make sure emacs is running and you have started its server mode (server-start)
  87. ;; Run this command from the command line
  88. ;; remember 'remember://http%3A//orgmode.org/::remember::Org-Mode%20Homepage::remember::Notes'
  89. ;; Emacs should now show a remember window with a URL to remember.org
  90. ;;
  91. ;;
  92. ;; Step 2: add two bookmarklets
  93. ;;
  94. ;; For firefox:
  95. ;;
  96. ;; * Right click on the bookmarks area of Firefox.
  97. ;; * Select new bookmark.
  98. ;; * In location fill the javascript code above (the bookmarklet)
  99. ;; * Make sure "Load this bookmark in the sidebar is deselected
  100. ;;
  101. ;; Try it. You should have now a url that starts with "remember://"
  102. ;; and your browser will not know what do to with it.
  103. ;;
  104. ;; Step 3: Add the handler for the "remember://" URI
  105. ;;
  106. ;; Firefox
  107. ;;
  108. ;; To add a protocol handler (eg: remember://) in Firefox, take the
  109. ;; following steps:
  110. ;;
  111. ;; - type in "about:config" in the location bar
  112. ;; - right click, select New --> String
  113. ;; - the name should be "network.protocol-handler.app.remember"
  114. ;; - the value should be the executable, eg. "org-annotation-helper".
  115. ;; At least under Linux this does not need to be the full path to
  116. ;; the executable.
  117. ;;
  118. ;; See http://kb.mozillazine.org/Register_protocol for more details.
  119. ;;
  120. ;; Opera
  121. ;;
  122. ;; In Opera add the protocol in the Preferences->Advanced->Programs
  123. ;; dialog.
  124. ;;
  125. ;; Step 4: Configure a template
  126. ;; I personally use the following template for this mode
  127. ;;
  128. ;; (?w "* %u %c \n\n%i" "~/working/trunk/org/bookmarks.org" "Web links")
  129. ;;
  130. ;; %c will be replaced with the hyperlink to the page, displaying the title of the page
  131. ;; %i will be replaced with the selected text from the browser
  132. ;; By default the new remember notes are placed in the
  133. ;; bookmarks.org file under the "Web links" section, but it can be
  134. ;; easily overriden with C-u C-c C-c
  135. ;;
  136. ;; Step 5:
  137. ;; Enjoy
  138. (require 'url)
  139. (autoload 'url-unhex-string "url")
  140. (defun bzg/org-annotation-helper (info)
  141. (interactive)
  142. "Process an externally passed remember:// style url.
  143. URLSTRING consists of a protocol part and a url and title,
  144. separated by ::remember::
  145. The protocol types currently recognized are:
  146. remember:// start `remember' with the url and title
  147. annotation:// similar to `org-annotation-as-kill'."
  148. (let ((remember-annotation-functions nil))
  149. ;; The `parse-url' functions break on the embedded url,
  150. ;; since our format is fixed we'll split the url ourselves.
  151. (if (string-match "^\\([^:]*\\):\\(/*\\)\\(.*\\)" info)
  152. (let* ((b (get-buffer-create "*org-ann*"))
  153. (proto (match-string 1 info))
  154. (url_title_region (match-string 3 info))
  155. (splitparts (split-string url_title_region "::remember::"))
  156. (url (url-unhex-string (car splitparts)))
  157. (type (if (string-match "^\\([a-z]+\\):" url)
  158. (match-string 1 url)))
  159. (title (cadr splitparts))
  160. (region (url-unhex-string (caddr splitparts)))
  161. orglink)
  162. (setq title (if (> (length title) 0) (url-unhex-string title)))
  163. (setq orglink (org-make-link-string url title))
  164. (org-store-link-props :type type
  165. :link url
  166. :region region
  167. :description title)
  168. (setq org-stored-links
  169. (cons (list url title) org-stored-links))
  170. ;; FIXME can't access %a in the template -- how to set annotation?
  171. (raise-frame)
  172. (cond ((equal proto "remember")
  173. (kill-new orglink)
  174. (set-buffer b)
  175. (set-mark (point))
  176. (insert region)
  177. (org-remember ?w))
  178. ((equal proto "annotation")
  179. (message "Copied '%s' to the kill-ring." orglink)
  180. (kill-new orglink))
  181. (t (error "unrecognized org-helper protocol"))))
  182. (error "could not parse argument")))
  183. )
  184. (provide 'org-annotation-helper)