org-protocol.el 24 KB

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