org-protocol.el 25 KB

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