org-protocol.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. ;;; org-protocol.el --- Intercept calls from emacsclient to trigger custom actions.
  2. ;;
  3. ;; Copyright (C) 2008-2012
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Bastien Guerry <bzg AT gnu DOT org>
  7. ;; Author: Daniel M German <dmg AT uvic DOT org>
  8. ;; Author: Sebastian Rose <sebastian_rose AT gmx DOT de>
  9. ;; Author: Ross Patterson <me AT rpatterson DOT net>
  10. ;; Maintainer: Sebastian Rose <sebastian_rose AT gmx DOT de>
  11. ;; Keywords: org, emacsclient, wp
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Intercept calls from emacsclient to trigger custom actions.
  28. ;;
  29. ;; This is done by advising `server-visit-files' to scan the list of filenames
  30. ;; for `org-protocol-the-protocol' and sub-protocols defined in
  31. ;; `org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'.
  32. ;;
  33. ;; Any application that supports calling external programs with an URL
  34. ;; as argument may be used with this functionality.
  35. ;;
  36. ;;
  37. ;; Usage:
  38. ;; ------
  39. ;;
  40. ;; 1.) Add this to your init file (.emacs probably):
  41. ;;
  42. ;; (add-to-list 'load-path "/path/to/org-protocol/")
  43. ;; (require 'org-protocol)
  44. ;;
  45. ;; 3.) Ensure emacs-server is up and running.
  46. ;; 4.) Try this from the command line (adjust the URL as needed):
  47. ;;
  48. ;; $ emacsclient \
  49. ;; org-protocol://store-link://http:%2F%2Flocalhost%2Findex.html/The%20title
  50. ;;
  51. ;; 5.) Optionally add custom sub-protocols and handlers:
  52. ;;
  53. ;; (setq org-protocol-protocol-alist
  54. ;; '(("my-protocol"
  55. ;; :protocol "my-protocol"
  56. ;; :function my-protocol-handler-function)))
  57. ;;
  58. ;; A "sub-protocol" will be found in URLs like this:
  59. ;;
  60. ;; org-protocol://sub-protocol://data
  61. ;;
  62. ;; If it works, you can now setup other applications for using this feature.
  63. ;;
  64. ;;
  65. ;; As of March 2009 Firefox users follow the steps documented on
  66. ;; http://kb.mozillazine.org/Register_protocol, Opera setup is described here:
  67. ;; http://www.opera.com/support/kb/view/535/
  68. ;;
  69. ;;
  70. ;; Documentation
  71. ;; -------------
  72. ;;
  73. ;; org-protocol.el comes with and installs handlers to open sources of published
  74. ;; online content, store and insert the browser's URLs and cite online content
  75. ;; by clicking on a bookmark in Firefox, Opera and probably other browsers and
  76. ;; applications:
  77. ;;
  78. ;; * `org-protocol-open-source' uses the sub-protocol \"open-source\" and maps
  79. ;; URLs to local filenames defined in `org-protocol-project-alist'.
  80. ;;
  81. ;; * `org-protocol-store-link' stores an Org-link (if Org-mode is present) and
  82. ;; pushes the browsers URL to the `kill-ring' for yanking. This handler is
  83. ;; triggered through the sub-protocol \"store-link\".
  84. ;;
  85. ;; * Call `org-protocol-capture' by using the sub-protocol \"capture\". If
  86. ;; Org-mode is loaded, Emacs will pop-up a capture buffer and fill the
  87. ;; template with the data provided. I.e. the browser's URL is inserted as an
  88. ;; Org-link of which the page title will be the description part. If text
  89. ;; was select in the browser, that text will be the body of the entry.
  90. ;;
  91. ;; * Call `org-protocol-remember' by using the sub-protocol \"remember\".
  92. ;; This is provided for backward compatibility.
  93. ;; You may read `org-capture' as `org-remember' throughout this file if
  94. ;; you still use `org-remember'.
  95. ;;
  96. ;; You may use the same bookmark URL for all those standard handlers and just
  97. ;; adjust the sub-protocol used:
  98. ;;
  99. ;; location.href='org-protocol://sub-protocol://'+
  100. ;; encodeURIComponent(location.href)+'/'+
  101. ;; encodeURIComponent(document.title)+'/'+
  102. ;; encodeURIComponent(window.getSelection())
  103. ;;
  104. ;; The handler for the sub-protocol \"capture\" detects an optional template
  105. ;; char that, if present, triggers the use of a special template.
  106. ;; Example:
  107. ;;
  108. ;; location.href='org-protocol://sub-protocol://x/'+ ...
  109. ;;
  110. ;; use template ?x.
  111. ;;
  112. ;; Note, that using double slashes is optional from org-protocol.el's point of
  113. ;; view because emacsclient squashes the slashes to one.
  114. ;;
  115. ;;
  116. ;; provides: 'org-protocol
  117. ;;
  118. ;;; Code:
  119. (require 'org)
  120. (eval-when-compile
  121. (require 'cl))
  122. (declare-function org-publish-get-project-from-filename "org-publish"
  123. (filename &optional up))
  124. (declare-function server-edit "server" (&optional arg))
  125. (define-obsolete-function-alias
  126. 'org-protocol-unhex-compound 'org-link-unescape-compound
  127. "2011-02-17")
  128. (define-obsolete-function-alias
  129. 'org-protocol-unhex-string 'org-link-unescape
  130. "2011-02-17")
  131. (define-obsolete-function-alias
  132. 'org-protocol-unhex-single-byte-sequence
  133. 'org-link-unescape-single-byte-sequence
  134. "2011-02-17")
  135. (defgroup org-protocol nil
  136. "Intercept calls from emacsclient to trigger custom actions.
  137. This is done by advising `server-visit-files' to scan the list of filenames
  138. for `org-protocol-the-protocol' and sub-protocols defined in
  139. `org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'."
  140. :version "22.1"
  141. :group 'convenience
  142. :group 'org)
  143. ;;; Variables:
  144. (defconst org-protocol-protocol-alist-default
  145. '(("org-remember" :protocol "remember" :function org-protocol-remember :kill-client t)
  146. ("org-capture" :protocol "capture" :function org-protocol-capture :kill-client t)
  147. ("org-store-link" :protocol "store-link" :function org-protocol-store-link)
  148. ("org-open-source" :protocol "open-source" :function org-protocol-open-source))
  149. "Default protocols to use.
  150. See `org-protocol-protocol-alist' for a description of this variable.")
  151. (defconst org-protocol-the-protocol "org-protocol"
  152. "This is the protocol to detect if org-protocol.el is loaded.
  153. `org-protocol-protocol-alist-default' and `org-protocol-protocol-alist' hold
  154. the sub-protocols that trigger the required action. You will have to define
  155. just one protocol handler OS-wide (MS-Windows) or per application (Linux).
  156. That protocol handler should call emacsclient.")
  157. ;;; User variables:
  158. (defcustom org-protocol-reverse-list-of-files t
  159. "Non-nil means re-reverse the list of filenames passed on the command line.
  160. The filenames passed on the command line are passed to the emacs-server in
  161. reverse order. Set to t (default) to re-reverse the list, i.e. use the
  162. sequence on the command line. If nil, the sequence of the filenames is
  163. unchanged."
  164. :group 'org-protocol
  165. :type 'boolean)
  166. (defcustom org-protocol-project-alist nil
  167. "Map URLs to local filenames for `org-protocol-open-source' (open-source).
  168. Each element of this list must be of the form:
  169. (module-name :property value property: value ...)
  170. where module-name is an arbitrary name. All the values are strings.
  171. Possible properties are:
  172. :online-suffix - the suffix to strip from the published URLs
  173. :working-suffix - the replacement for online-suffix
  174. :base-url - the base URL, e.g. http://www.example.com/project/
  175. Last slash required.
  176. :working-directory - the local working directory. This is, what base-url will
  177. be replaced with.
  178. :redirects - A list of cons cells, each of which maps a regular
  179. expression to match to a path relative to :working-directory.
  180. Example:
  181. (setq org-protocol-project-alist
  182. '((\"http://orgmode.org/worg/\"
  183. :online-suffix \".php\"
  184. :working-suffix \".org\"
  185. :base-url \"http://orgmode.org/worg/\"
  186. :working-directory \"/home/user/org/Worg/\")
  187. (\"http://localhost/org-notes/\"
  188. :online-suffix \".html\"
  189. :working-suffix \".org\"
  190. :base-url \"http://localhost/org/\"
  191. :working-directory \"/home/user/org/\"
  192. :rewrites ((\"org/?$\" . \"index.php\")))))
  193. The last line tells `org-protocol-open-source' to open
  194. /home/user/org/index.php, if the URL cannot be mapped to an existing
  195. file, and ends with either \"org\" or \"org/\".
  196. Consider using the interactive functions `org-protocol-create' and
  197. `org-protocol-create-for-org' to help you filling this variable with valid contents."
  198. :group 'org-protocol
  199. :type 'alist)
  200. (defcustom org-protocol-protocol-alist nil
  201. "* Register custom handlers for org-protocol.
  202. Each element of this list must be of the form:
  203. (module-name :protocol protocol :function func :kill-client nil)
  204. protocol - protocol to detect in a filename without trailing colon and slashes.
  205. See rfc1738 section 2.1 for more on this.
  206. If you define a protocol \"my-protocol\", `org-protocol-check-filename-for-protocol'
  207. will search filenames for \"org-protocol:/my-protocol:/\"
  208. and trigger your action for every match. `org-protocol' is defined in
  209. `org-protocol-the-protocol'. Double and triple slashes are compressed
  210. to one by emacsclient.
  211. function - function that handles requests with protocol and takes exactly one
  212. argument: the filename with all protocols stripped. If the function
  213. returns nil, emacsclient and -server do nothing. Any non-nil return
  214. value is considered a valid filename and thus passed to the server.
  215. `org-protocol.el provides some support for handling those filenames,
  216. if you stay with the conventions used for the standard handlers in
  217. `org-protocol-protocol-alist-default'. See `org-protocol-split-data'.
  218. kill-client - If t, kill the client immediately, once the sub-protocol is
  219. detected. This is necessary for actions that can be interrupted by
  220. `C-g' to avoid dangling emacsclients. Note, that all other command
  221. line arguments but the this one will be discarded, greedy handlers
  222. still receive the whole list of arguments though.
  223. Here is an example:
  224. (setq org-protocol-protocol-alist
  225. '((\"my-protocol\"
  226. :protocol \"my-protocol\"
  227. :function my-protocol-handler-function)
  228. (\"your-protocol\"
  229. :protocol \"your-protocol\"
  230. :function your-protocol-handler-function)))"
  231. :group 'org-protocol
  232. :type '(alist))
  233. (defcustom org-protocol-default-template-key nil
  234. "The default template key to use.
  235. This is usually a single character string but can also be a
  236. string with two characters."
  237. :group 'org-protocol
  238. :type 'string)
  239. ;;; Helper functions:
  240. (defun org-protocol-sanitize-uri (uri)
  241. "emacsclient compresses double and triple slashes.
  242. Slashes are sanitized to double slashes here."
  243. (when (string-match "^\\([a-z]+\\):/" uri)
  244. (let* ((splitparts (split-string uri "/+")))
  245. (setq uri (concat (car splitparts) "//" (mapconcat 'identity (cdr splitparts) "/")))))
  246. uri)
  247. (defun org-protocol-split-data (data &optional unhexify separator)
  248. "Split what an org-protocol handler function gets as only argument.
  249. DATA is that one argument. DATA is split at each occurrence of
  250. SEPARATOR (regexp). If no SEPARATOR is specified or SEPARATOR is
  251. nil, assume \"/+\". The results of that splitting are returned
  252. as a list. If UNHEXIFY is non-nil, hex-decode each split part.
  253. If UNHEXIFY is a function, use that function to decode each split
  254. part."
  255. (let* ((sep (or separator "/+"))
  256. (split-parts (split-string data sep)))
  257. (if unhexify
  258. (if (fboundp unhexify)
  259. (mapcar unhexify split-parts)
  260. (mapcar 'org-link-unescape split-parts))
  261. split-parts)))
  262. (defun org-protocol-flatten-greedy (param-list &optional strip-path replacement)
  263. "Greedy handlers might receive a list like this from emacsclient:
  264. '((\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
  265. where \"/dir/\" is the absolute path to emacsclients working directory. This
  266. function transforms it into a flat list using `org-protocol-flatten' and
  267. transforms the elements of that list as follows:
  268. If strip-path is non-nil, remove the \"/dir/\" prefix from all members of
  269. param-list.
  270. If replacement is string, replace the \"/dir/\" prefix with it.
  271. The first parameter, the one that contains the protocols, is always changed.
  272. Everything up to the end of the protocols is stripped.
  273. Note, that this function will always behave as if
  274. `org-protocol-reverse-list-of-files' was set to t and the returned list will
  275. reflect that. I.e. emacsclients first parameter will be the first one in the
  276. returned list."
  277. (let* ((l (org-protocol-flatten (if org-protocol-reverse-list-of-files
  278. param-list
  279. (reverse param-list))))
  280. (trigger (car l))
  281. (len 0)
  282. dir
  283. ret)
  284. (when (string-match "^\\(.*\\)\\(org-protocol:/+[a-zA-z0-9][-_a-zA-z0-9]*:/+\\)\\(.*\\)" trigger)
  285. (setq dir (match-string 1 trigger))
  286. (setq len (length dir))
  287. (setcar l (concat dir (match-string 3 trigger))))
  288. (if strip-path
  289. (progn
  290. (dolist (e l ret)
  291. (setq ret
  292. (append ret
  293. (list
  294. (if (stringp e)
  295. (if (stringp replacement)
  296. (setq e (concat replacement (substring e len)))
  297. (setq e (substring e len)))
  298. e)))))
  299. ret)
  300. l)))
  301. (defun org-protocol-flatten (l)
  302. "Greedy handlers might receive a list like this from emacsclient:
  303. '( (\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
  304. where \"/dir/\" is the absolute path to emacsclients working directory.
  305. This function transforms it into a flat list."
  306. (if (null l) ()
  307. (if (listp l)
  308. (append (org-protocol-flatten (car l)) (org-protocol-flatten (cdr l)))
  309. (list l))))
  310. ;;; Standard protocol handlers:
  311. (defun org-protocol-store-link (fname)
  312. "Process an org-protocol://store-link:// style url.
  313. Additionally store a browser URL as an org link. Also pushes the
  314. link's URL to the `kill-ring'.
  315. The location for a browser's bookmark has to look like this:
  316. javascript:location.href='org-protocol://store-link://'+ \\
  317. encodeURIComponent(location.href)
  318. encodeURIComponent(document.title)+'/'+ \\
  319. Don't use `escape()'! Use `encodeURIComponent()' instead. The title of the page
  320. could contain slashes and the location definitely will.
  321. The sub-protocol used to reach this function is set in
  322. `org-protocol-protocol-alist'."
  323. (let* ((splitparts (org-protocol-split-data fname t))
  324. (uri (org-protocol-sanitize-uri (car splitparts)))
  325. (title (cadr splitparts))
  326. orglink)
  327. (if (boundp 'org-stored-links)
  328. (setq org-stored-links (cons (list uri title) org-stored-links)))
  329. (kill-new uri)
  330. (message "`%s' to insert new org-link, `%s' to insert `%s'"
  331. (substitute-command-keys"\\[org-insert-link]")
  332. (substitute-command-keys"\\[yank]")
  333. uri))
  334. nil)
  335. (defun org-protocol-remember (info)
  336. "Process an org-protocol://remember:// style url.
  337. The location for a browser's bookmark has to look like this:
  338. javascript:location.href='org-protocol://remember://'+ \\
  339. encodeURIComponent(location.href)+'/' \\
  340. encodeURIComponent(document.title)+'/'+ \\
  341. encodeURIComponent(window.getSelection())
  342. See the docs for `org-protocol-capture' for more information."
  343. (if (and (boundp 'org-stored-links)
  344. (fboundp 'org-capture)
  345. (org-protocol-do-capture info 'org-remember))
  346. (message "Item remembered."))
  347. nil)
  348. (defun org-protocol-capture (info)
  349. "Process an org-protocol://capture:// style url.
  350. The sub-protocol used to reach this function is set in
  351. `org-protocol-protocol-alist'.
  352. This function detects an URL, title and optional text, separated by '/'
  353. The location for a browser's bookmark has to look like this:
  354. javascript:location.href='org-protocol://capture://'+ \\
  355. encodeURIComponent(location.href)+'/' \\
  356. encodeURIComponent(document.title)+'/'+ \\
  357. encodeURIComponent(window.getSelection())
  358. By default, it uses the character `org-protocol-default-template-key',
  359. which should be associated with a template in `org-capture-templates'.
  360. But you may prepend the encoded URL with a character and a slash like so:
  361. javascript:location.href='org-protocol://capture://b/'+ ...
  362. Now template ?b will be used."
  363. (if (and (boundp 'org-stored-links)
  364. (fboundp 'org-capture)
  365. (org-protocol-do-capture info 'org-capture))
  366. (message "Item captured."))
  367. nil)
  368. (defun org-protocol-do-capture (info capture-func)
  369. "Support `org-capture' and `org-remember' alike.
  370. CAPTURE-FUNC is either the symbol `org-remember' or `org-capture'."
  371. (let* ((parts (org-protocol-split-data info t))
  372. (template (or (and (>= 2 (length (car parts))) (pop parts))
  373. org-protocol-default-template-key))
  374. (url (org-protocol-sanitize-uri (car parts)))
  375. (type (if (string-match "^\\([a-z]+\\):" url)
  376. (match-string 1 url)))
  377. (title (or (cadr parts) ""))
  378. (region (or (caddr parts) ""))
  379. (orglink (org-make-link-string
  380. url (if (string-match "[^[:space:]]" title) title url)))
  381. (org-capture-link-is-already-stored t) ;; avoid call to org-store-link
  382. remember-annotation-functions)
  383. (setq org-stored-links
  384. (cons (list url title) org-stored-links))
  385. (kill-new orglink)
  386. (org-store-link-props :type type
  387. :link url
  388. :description title
  389. :annotation orglink
  390. :initial region)
  391. (raise-frame)
  392. (funcall capture-func nil template)))
  393. (defun org-protocol-open-source (fname)
  394. "Process an org-protocol://open-source:// style url.
  395. Change a filename by mapping URLs to local filenames as set
  396. in `org-protocol-project-alist'.
  397. The location for a browser's bookmark should look like this:
  398. javascript:location.href='org-protocol://open-source://'+ \\
  399. encodeURIComponent(location.href)"
  400. ;; As we enter this function for a match on our protocol, the return value
  401. ;; defaults to nil.
  402. (let ((result nil)
  403. (f (org-link-unescape fname)))
  404. (catch 'result
  405. (dolist (prolist org-protocol-project-alist)
  406. (let* ((base-url (plist-get (cdr prolist) :base-url))
  407. (wsearch (regexp-quote base-url)))
  408. (when (string-match wsearch f)
  409. (let* ((wdir (plist-get (cdr prolist) :working-directory))
  410. (strip-suffix (plist-get (cdr prolist) :online-suffix))
  411. (add-suffix (plist-get (cdr prolist) :working-suffix))
  412. ;; Strip "[?#].*$" if `f' is a redirect with another
  413. ;; ending than strip-suffix here:
  414. (f1 (substring f 0 (string-match "\\([\\?#].*\\)?$" f)))
  415. (start-pos (+ (string-match wsearch f1) (length base-url)))
  416. (end-pos (string-match
  417. (regexp-quote strip-suffix) f1))
  418. ;; We have to compare redirects without suffix below:
  419. (f2 (concat wdir (substring f1 start-pos end-pos)))
  420. (the-file (concat f2 add-suffix)))
  421. ;; Note: the-file may still contain `%C3' et al here because browsers
  422. ;; tend to encode `&auml;' in URLs to `%25C3' - `%25' being `%'.
  423. ;; So the results may vary.
  424. ;; -- start redirects --
  425. (unless (file-exists-p the-file)
  426. (message "File %s does not exist.\nTesting for rewritten URLs." the-file)
  427. (let ((rewrites (plist-get (cdr prolist) :rewrites)))
  428. (when rewrites
  429. (message "Rewrites found: %S" rewrites)
  430. (mapc
  431. (lambda (rewrite)
  432. "Try to match a rewritten URL and map it to a real file."
  433. ;; Compare redirects without suffix:
  434. (if (string-match (car rewrite) f2)
  435. (throw 'result (concat wdir (cdr rewrite)))))
  436. rewrites))))
  437. ;; -- end of redirects --
  438. (if (file-readable-p the-file)
  439. (throw 'result the-file))
  440. (if (file-exists-p the-file)
  441. (message "%s: permission denied!" the-file)
  442. (message "%s: no such file or directory." the-file))))))
  443. result)))
  444. ;;; Core functions:
  445. (defun org-protocol-check-filename-for-protocol (fname restoffiles client)
  446. "Detect if `org-protocol-the-protocol' and a known sub-protocol is used in fname.
  447. Sub-protocols are registered in `org-protocol-protocol-alist' and
  448. `org-protocol-protocol-alist-default'.
  449. This is, how the matching is done:
  450. (string-match \"protocol:/+sub-protocol:/+\" ...)
  451. protocol and sub-protocol are regexp-quoted.
  452. If a matching protocol is found, the protocol is stripped from fname and the
  453. result is passed to the protocols function as the only parameter. If the
  454. function returns nil, the filename is removed from the list of filenames
  455. passed from emacsclient to the server.
  456. If the function returns a non nil value, that value is passed to the server
  457. as filename."
  458. (let ((sub-protocols (append org-protocol-protocol-alist
  459. org-protocol-protocol-alist-default)))
  460. (catch 'fname
  461. (let ((the-protocol (concat (regexp-quote org-protocol-the-protocol) ":/+")))
  462. (when (string-match the-protocol fname)
  463. (dolist (prolist sub-protocols)
  464. (let ((proto (concat the-protocol
  465. (regexp-quote (plist-get (cdr prolist) :protocol)) ":/+")))
  466. (when (string-match proto fname)
  467. (let* ((func (plist-get (cdr prolist) :function))
  468. (greedy (plist-get (cdr prolist) :greedy))
  469. (split (split-string fname proto))
  470. (result (if greedy restoffiles (cadr split))))
  471. (when (plist-get (cdr prolist) :kill-client)
  472. (message "Greedy org-protocol handler. Killing client.")
  473. (server-edit))
  474. (when (fboundp func)
  475. (unless greedy
  476. (throw 'fname (funcall func result)))
  477. (funcall func result)
  478. (throw 'fname t))))))))
  479. ;; (message "fname: %s" fname)
  480. fname)))
  481. (defadvice server-visit-files (before org-protocol-detect-protocol-server activate)
  482. "Advice server-visit-flist to call `org-protocol-modify-filename-for-protocol'."
  483. (let ((flist (if org-protocol-reverse-list-of-files
  484. (reverse (ad-get-arg 0))
  485. (ad-get-arg 0)))
  486. (client (ad-get-arg 1)))
  487. (catch 'greedy
  488. (dolist (var flist)
  489. ;; `\' to `/' on windows. FIXME: could this be done any better?
  490. (let ((fname (expand-file-name (car var))))
  491. (setq fname (org-protocol-check-filename-for-protocol
  492. fname (member var flist) client))
  493. (if (eq fname t) ;; greedy? We need the `t' return value.
  494. (progn
  495. (ad-set-arg 0 nil)
  496. (throw 'greedy t))
  497. (if (stringp fname) ;; probably filename
  498. (setcar var fname)
  499. (ad-set-arg 0 (delq var (ad-get-arg 0))))))))))
  500. ;;; Org specific functions:
  501. (defun org-protocol-create-for-org ()
  502. "Create a org-protocol project for the current file's Org-mode project.
  503. This works, if the file visited is part of a publishing project in
  504. `org-publish-project-alist'. This function calls `org-protocol-create' to do
  505. most of the work."
  506. (interactive)
  507. (require 'org-publish)
  508. (let ((all (or (org-publish-get-project-from-filename buffer-file-name))))
  509. (if all (org-protocol-create (cdr all))
  510. (message "Not in an org-project. Did mean %s?"
  511. (substitute-command-keys"\\[org-protocol-create]")))))
  512. (defun org-protocol-create (&optional project-plist)
  513. "Create a new org-protocol project interactively.
  514. An org-protocol project is an entry in `org-protocol-project-alist'
  515. which is used by `org-protocol-open-source'.
  516. Optionally use project-plist to initialize the defaults for this project. If
  517. project-plist is the CDR of an element in `org-publish-project-alist', reuse
  518. :base-directory, :html-extension and :base-extension."
  519. (interactive)
  520. (let ((working-dir (expand-file-name
  521. (or (plist-get project-plist :base-directory)
  522. default-directory)))
  523. (base-url "http://orgmode.org/worg/")
  524. (strip-suffix (or (plist-get project-plist :html-extension) ".html"))
  525. (working-suffix (if (plist-get project-plist :base-extension)
  526. (concat "." (plist-get project-plist :base-extension))
  527. ".org"))
  528. (worglet-buffer nil)
  529. (insert-default-directory t)
  530. (minibuffer-allow-text-properties nil))
  531. (setq base-url (read-string "Base URL of published content: " base-url nil base-url t))
  532. (if (not (string-match "\\/$" base-url))
  533. (setq base-url (concat base-url "/")))
  534. (setq working-dir
  535. (expand-file-name
  536. (read-directory-name "Local working directory: " working-dir working-dir t)))
  537. (if (not (string-match "\\/$" working-dir))
  538. (setq working-dir (concat working-dir "/")))
  539. (setq strip-suffix
  540. (read-string
  541. (concat "Extension to strip from published URLs (" strip-suffix "): ")
  542. strip-suffix nil strip-suffix t))
  543. (setq working-suffix
  544. (read-string
  545. (concat "Extension of editable files (" working-suffix "): ")
  546. working-suffix nil working-suffix t))
  547. (when (yes-or-no-p "Save the new org-protocol-project to your init file? ")
  548. (setq org-protocol-project-alist
  549. (cons `(,base-url . (:base-url ,base-url
  550. :working-directory ,working-dir
  551. :online-suffix ,strip-suffix
  552. :working-suffix ,working-suffix))
  553. org-protocol-project-alist))
  554. (customize-save-variable 'org-protocol-project-alist org-protocol-project-alist))))
  555. (provide 'org-protocol)
  556. ;;; org-protocol.el ends here