Release 7.3
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / org-protocol.el
blob3a20c5f729ccabf6040f70a3efa83eef4aa60939
1 ;;; org-protocol.el --- Intercept calls from emacsclient to trigger custom actions.
2 ;;
3 ;; Copyright (C) 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
5 ;;
6 ;; Author: Bastien Guerry <bzg AT altern 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 ;; Version: 7.3
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software: you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation, either version 3 of the License, or
19 ;; (at your option) any later version.
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;; Commentary:
32 ;; Intercept calls from emacsclient to trigger custom actions.
34 ;; This is done by advising `server-visit-files' to scan the list of filenames
35 ;; for `org-protocol-the-protocol' and sub-protocols defined in
36 ;; `org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'.
38 ;; Any application that supports calling external programs with an URL
39 ;; as argument may be used with this functionality.
42 ;; Usage:
43 ;; ------
45 ;; 1.) Add this to your init file (.emacs probably):
47 ;; (add-to-list 'load-path "/path/to/org-protocol/")
48 ;; (require 'org-protocol)
50 ;; 3.) Ensure emacs-server is up and running.
51 ;; 4.) Try this from the command line (adjust the URL as needed):
53 ;; $ emacsclient \
54 ;; org-protocol://store-link://http:%2F%2Flocalhost%2Findex.html/The%20title
56 ;; 5.) Optionally add custom sub-protocols and handlers:
58 ;; (setq org-protocol-protocol-alist
59 ;; '(("my-protocol"
60 ;; :protocol "my-protocol"
61 ;; :function my-protocol-handler-function)))
63 ;; A "sub-protocol" will be found in URLs like this:
65 ;; org-protocol://sub-protocol://data
67 ;; If it works, you can now setup other applications for using this feature.
70 ;; As of March 2009 Firefox users follow the steps documented on
71 ;; http://kb.mozillazine.org/Register_protocol, Opera setup is described here:
72 ;; http://www.opera.com/support/kb/view/535/
75 ;; Documentation
76 ;; -------------
78 ;; org-protocol.el comes with and installs handlers to open sources of published
79 ;; online content, store and insert the browser's URLs and cite online content
80 ;; by clicking on a bookmark in Firefox, Opera and probably other browsers and
81 ;; applications:
83 ;; * `org-protocol-open-source' uses the sub-protocol \"open-source\" and maps
84 ;; URLs to local filenames defined in `org-protocol-project-alist'.
86 ;; * `org-protocol-store-link' stores an Org-link (if Org-mode is present) and
87 ;; pushes the browsers URL to the `kill-ring' for yanking. This handler is
88 ;; triggered through the sub-protocol \"store-link\".
90 ;; * Call `org-protocol-capture' by using the sub-protocol \"capture\". If
91 ;; Org-mode is loaded, Emacs will pop-up a capture buffer and fill the
92 ;; template with the data provided. I.e. the browser's URL is inserted as an
93 ;; Org-link of which the page title will be the description part. If text
94 ;; was select in the browser, that text will be the body of the entry.
96 ;; * Call `org-protocol-remember' by using the sub-protocol \"remember\".
97 ;; This is provided for backward compatibility.
98 ;; You may read `org-capture' as `org-remember' throughout this file if
99 ;; you still use `org-remember'.
101 ;; You may use the same bookmark URL for all those standard handlers and just
102 ;; adjust the sub-protocol used:
104 ;; location.href='org-protocol://sub-protocol://'+
105 ;; encodeURIComponent(location.href)+'/'+
106 ;; encodeURIComponent(document.title)+'/'+
107 ;; encodeURIComponent(window.getSelection())
109 ;; The handler for the sub-protocol \"capture\" detects an optional template
110 ;; char that, if present, triggers the use of a special template.
111 ;; Example:
113 ;; location.href='org-protocol://sub-protocol://x/'+ ...
115 ;; use template ?x.
117 ;; Note, that using double slashes is optional from org-protocol.el's point of
118 ;; view because emacsclient squashes the slashes to one.
121 ;; provides: 'org-protocol
123 ;;; Code:
125 (require 'org)
126 (eval-when-compile
127 (require 'cl))
129 (declare-function org-publish-get-project-from-filename "org-publish"
130 (filename &optional up))
131 (declare-function server-edit "server" (&optional arg))
134 (defgroup org-protocol nil
135 "Intercept calls from emacsclient to trigger custom actions.
137 This is done by advising `server-visit-files' to scann the list of filenames
138 for `org-protocol-the-protocol' and sub-procols defined in
139 `org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'."
140 :version "22.1"
141 :group 'convenience
142 :group 'org)
145 ;;; Variables:
147 (defconst org-protocol-protocol-alist-default
148 '(("org-remember" :protocol "remember" :function org-protocol-remember :kill-client t)
149 ("org-capture" :protocol "capture" :function org-protocol-capture :kill-client t)
150 ("org-store-link" :protocol "store-link" :function org-protocol-store-link)
151 ("org-open-source" :protocol "open-source" :function org-protocol-open-source))
152 "Default protocols to use.
153 See `org-protocol-protocol-alist' for a description of this variable.")
156 (defconst org-protocol-the-protocol "org-protocol"
157 "This is the protocol to detect if org-protocol.el is loaded.
158 `org-protocol-protocol-alist-default' and `org-protocol-protocol-alist' hold
159 the sub-protocols that trigger the required action. You will have to define
160 just one protocol handler OS-wide (MS-Windows) or per application (Linux).
161 That protocol handler should call emacsclient.")
164 ;;; User variables:
166 (defcustom org-protocol-reverse-list-of-files t
167 "* Non-nil means re-reverse the list of filenames passed on the command line.
168 The filenames passed on the command line are passed to the emacs-server in
169 reverse order. Set to t (default) to re-reverse the list, i.e. use the
170 sequence on the command line. If nil, the sequence of the filenames is
171 unchanged."
172 :group 'org-protocol
173 :type 'boolean)
176 (defcustom org-protocol-project-alist nil
177 "* Map URLs to local filenames for `org-protocol-open-source' (open-source).
179 Each element of this list must be of the form:
181 (module-name :property value property: value ...)
183 where module-name is an arbitrary name. All the values are strings.
185 Possible properties are:
187 :online-suffix - the suffix to strip from the published URLs
188 :working-suffix - the replacement for online-suffix
189 :base-url - the base URL, e.g. http://www.example.com/project/
190 Last slash required.
191 :working-directory - the local working directory. This is, what base-url will
192 be replaced with.
193 :redirects - A list of cons cells, each of which maps a regular
194 expression to match to a path relative to :working-directory.
196 Example:
198 (setq org-protocol-project-alist
199 '((\"http://orgmode.org/worg/\"
200 :online-suffix \".php\"
201 :working-suffix \".org\"
202 :base-url \"http://orgmode.org/worg/\"
203 :working-directory \"/home/user/org/Worg/\")
204 (\"http://localhost/org-notes/\"
205 :online-suffix \".html\"
206 :working-suffix \".org\"
207 :base-url \"http://localhost/org/\"
208 :working-directory \"/home/user/org/\"
209 :rewrites ((\"org/?$\" . \"index.php\")))))
211 The last line tells `org-protocol-open-source' to open
212 /home/user/org/index.php, if the URL cannot be mapped to an existing
213 file, and ends with either \"org\" or \"org/\".
215 Consider using the interactive functions `org-protocol-create' and
216 `org-protocol-create-for-org' to help you filling this variable with valid contents."
217 :group 'org-protocol
218 :type 'alist)
221 (defcustom org-protocol-protocol-alist nil
222 "* Register custom handlers for org-protocol.
224 Each element of this list must be of the form:
226 (module-name :protocol protocol :function func :kill-client nil)
228 protocol - protocol to detect in a filename without trailing colon and slashes.
229 See rfc1738 section 2.1 for more on this.
230 If you define a protocol \"my-protocol\", `org-protocol-check-filename-for-protocol'
231 will search filenames for \"org-protocol:/my-protocol:/\"
232 and trigger your action for every match. `org-protocol' is defined in
233 `org-protocol-the-protocol'. Double and triple slashes are compressed
234 to one by emacsclient.
236 function - function that handles requests with protocol and takes exactly one
237 argument: the filename with all protocols stripped. If the function
238 returns nil, emacsclient and -server do nothing. Any non-nil return
239 value is considered a valid filename and thus passed to the server.
241 `org-protocol.el provides some support for handling those filenames,
242 if you stay with the conventions used for the standard handlers in
243 `org-protocol-protocol-alist-default'. See `org-protocol-split-data'.
245 kill-client - If t, kill the client immediately, once the sub-protocol is
246 detected. This is necessary for actions that can be interrupted by
247 `C-g' to avoid dangling emacsclients. Note, that all other command
248 line arguments but the this one will be discarded, greedy handlers
249 still receive the whole list of arguments though.
251 Here is an example:
253 (setq org-protocol-protocol-alist
254 '((\"my-protocol\"
255 :protocol \"my-protocol\"
256 :function my-protocol-handler-function)
257 (\"your-protocol\"
258 :protocol \"your-protocol\"
259 :function your-protocol-handler-function)))"
260 :group 'org-protocol
261 :type '(alist))
263 (defcustom org-protocol-default-template-key nil
264 "The default org-remember-templates key to use."
265 :group 'org-protocol
266 :type 'string)
268 ;;; Helper functions:
270 (defun org-protocol-sanitize-uri (uri)
271 "emacsclient compresses double and triple slashes.
272 Slashes are sanitized to double slashes here."
273 (when (string-match "^\\([a-z]+\\):/" uri)
274 (let* ((splitparts (split-string uri "/+")))
275 (setq uri (concat (car splitparts) "//" (mapconcat 'identity (cdr splitparts) "/")))))
276 uri)
279 (defun org-protocol-split-data(data &optional unhexify separator)
280 "Split, what an org-protocol handler function gets as only argument.
281 DATA is that one argument. DATA is split at each occurrence of
282 SEPARATOR (regexp). If no SEPARATOR is specified or SEPARATOR is
283 nil, assume \"/+\". The results of that splitting are returned
284 as a list. If UNHEXIFY is non-nil, hex-decode each split part. If
285 UNHEXIFY is a function, use that function to decode each split
286 part."
287 (let* ((sep (or separator "/+"))
288 (split-parts (split-string data sep)))
289 (if unhexify
290 (if (fboundp unhexify)
291 (mapcar unhexify split-parts)
292 (mapcar 'org-protocol-unhex-string split-parts))
293 split-parts)))
295 ;; This inline function is needed in org-protocol-unhex-compound to do
296 ;; the right thing to decode UTF-8 char integer values.
297 (eval-when-compile
298 (if (>= emacs-major-version 23)
299 (defsubst org-protocol-char-to-string(c)
300 "Defsubst to decode UTF-8 character values in emacs 23 and beyond."
301 (char-to-string c))
302 (defsubst org-protocol-char-to-string (c)
303 "Defsubst to decode UTF-8 character values in emacs 22."
304 (string (decode-char 'ucs c)))))
306 (defun org-protocol-unhex-string(str)
307 "Unhex hexified unicode strings as returned from the JavaScript function
308 encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ü'."
309 (setq str (or str ""))
310 (let ((tmp "")
311 (case-fold-search t))
312 (while (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str)
313 (let* ((start (match-beginning 0))
314 (end (match-end 0))
315 (hex (match-string 0 str))
316 (replacement (org-protocol-unhex-compound hex)))
317 (setq tmp (concat tmp (substring str 0 start) replacement))
318 (setq str (substring str end))))
319 (setq tmp (concat tmp str))
320 tmp))
323 (defun org-protocol-unhex-compound (hex)
324 "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ü'."
325 (let* ((bytes (remove "" (split-string hex "%")))
326 (ret "")
327 (eat 0)
328 (sum 0))
329 (while bytes
330 (let* ((b (pop bytes))
331 (a (elt b 0))
332 (b (elt b 1))
333 (c1 (if (> a ?9) (+ 10 (- a ?A)) (- a ?0)))
334 (c2 (if (> b ?9) (+ 10 (- b ?A)) (- b ?0)))
335 (val (+ (lsh c1 4) c2))
336 (shift
337 (if (= 0 eat) ;; new byte
338 (if (>= val 252) 6
339 (if (>= val 248) 5
340 (if (>= val 240) 4
341 (if (>= val 224) 3
342 (if (>= val 192) 2 0)))))
344 (xor
345 (if (= 0 eat) ;; new byte
346 (if (>= val 252) 252
347 (if (>= val 248) 248
348 (if (>= val 240) 240
349 (if (>= val 224) 224
350 (if (>= val 192) 192 0)))))
351 128)))
352 (if (>= val 192) (setq eat shift))
353 (setq val (logxor val xor))
354 (setq sum (+ (lsh sum shift) val))
355 (if (> eat 0) (setq eat (- eat 1)))
356 (when (= 0 eat)
357 (setq ret (concat ret (org-protocol-char-to-string sum)))
358 (setq sum 0))
359 )) ;; end (while bytes
360 ret ))
362 (defun org-protocol-flatten-greedy (param-list &optional strip-path replacement)
363 "Greedy handlers might receive a list like this from emacsclient:
364 '( (\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
365 where \"/dir/\" is the absolute path to emacsclients working directory. This
366 function transforms it into a flat list utilizing `org-protocol-flatten' and
367 transforms the elements of that list as follows:
369 If strip-path is non-nil, remove the \"/dir/\" prefix from all members of
370 param-list.
372 If replacement is string, replace the \"/dir/\" prefix with it.
374 The first parameter, the one that contains the protocols, is always changed.
375 Everything up to the end of the protocols is stripped.
377 Note, that this function will always behave as if
378 `org-protocol-reverse-list-of-files' was set to t and the returned list will
379 reflect that. I.e. emacsclients first parameter will be the first one in the
380 returned list."
381 (let* ((l (org-protocol-flatten (if org-protocol-reverse-list-of-files
382 param-list
383 (reverse param-list))))
384 (trigger (car l))
385 (len 0)
387 ret)
388 (when (string-match "^\\(.*\\)\\(org-protocol:/+[a-zA-z0-9][-_a-zA-z0-9]*:/+\\)\\(.*\\)" trigger)
389 (setq dir (match-string 1 trigger))
390 (setq len (length dir))
391 (setcar l (concat dir (match-string 3 trigger))))
392 (if strip-path
393 (progn
394 (dolist (e l ret)
395 (setq ret
396 (append ret
397 (list
398 (if (stringp e)
399 (if (stringp replacement)
400 (setq e (concat replacement (substring e len)))
401 (setq e (substring e len)))
402 e)))))
403 ret)
404 l)))
407 (defun org-protocol-flatten (l)
408 "Greedy handlers might receive a list like this from emacsclient:
409 '( (\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
410 where \"/dir/\" is the absolute path to emacsclients working directory.
411 This function transforms it into a flat list."
412 (if (null l) ()
413 (if (listp l)
414 (append (org-protocol-flatten (car l)) (org-protocol-flatten (cdr l)))
415 (list l))))
417 ;;; Standard protocol handlers:
419 (defun org-protocol-store-link (fname)
420 "Process an org-protocol://store-link:// style url.
421 Additionally store a browser URL as an org link. Also pushes the
422 link's URL to the `kill-ring'.
424 The location for a browser's bookmark has to look like this:
426 javascript:location.href='org-protocol://store-link://'+ \\
427 encodeURIComponent(location.href)
428 encodeURIComponent(document.title)+'/'+ \\
430 Don't use `escape()'! Use `encodeURIComponent()' instead. The title of the page
431 could contain slashes and the location definitely will.
433 The sub-protocol used to reach this function is set in
434 `org-protocol-protocol-alist'."
435 (let* ((splitparts (org-protocol-split-data fname t))
436 (uri (org-protocol-sanitize-uri (car splitparts)))
437 (title (cadr splitparts))
438 orglink)
439 (if (boundp 'org-stored-links)
440 (setq org-stored-links (cons (list uri title) org-stored-links)))
441 (kill-new uri)
442 (message "`%s' to insert new org-link, `%s' to insert `%s'"
443 (substitute-command-keys"\\[org-insert-link]")
444 (substitute-command-keys"\\[yank]")
445 uri))
446 nil)
448 (defun org-protocol-remember (info)
449 "Process an org-protocol://remember:// style url.
451 The location for a browser's bookmark has to look like this:
453 javascript:location.href='org-protocol://remember://'+ \\
454 encodeURIComponent(location.href)+'/' \\
455 encodeURIComponent(document.title)+'/'+ \\
456 encodeURIComponent(window.getSelection())
458 See the docs for `org-protocol-capture' for more information."
460 (if (and (boundp 'org-stored-links)
461 (or (fboundp 'org-capture))
462 (org-protocol-do-capture info 'org-remember))
463 (message "Org-mode not loaded."))
464 nil)
466 (defun org-protocol-capture (info)
467 "Process an org-protocol://capture:// style url.
469 The sub-protocol used to reach this function is set in
470 `org-protocol-protocol-alist'.
472 This function detects an URL, title and optional text, separated by '/'
473 The location for a browser's bookmark has to look like this:
475 javascript:location.href='org-protocol://capture://'+ \\
476 encodeURIComponent(location.href)+'/' \\
477 encodeURIComponent(document.title)+'/'+ \\
478 encodeURIComponent(window.getSelection())
480 By default, it uses the character `org-protocol-default-template-key',
481 which should be associated with a template in `org-capture-templates'.
482 But you may prepend the encoded URL with a character and a slash like so:
484 javascript:location.href='org-protocol://capture://b/'+ ...
486 Now template ?b will be used."
487 (if (and (boundp 'org-stored-links)
488 (or (fboundp 'org-capture))
489 (org-protocol-do-capture info 'org-capture))
490 (message "Org-mode not loaded."))
491 nil)
493 (defun org-protocol-do-capture (info capture-func)
494 "Support `org-capture' and `org-remember' alike.
495 CAPTURE-FUNC is either the symbol `org-remember' or `org-capture'."
496 (let* ((parts (org-protocol-split-data info t))
497 (template (or (and (= 1 (length (car parts))) (pop parts))
498 org-protocol-default-template-key))
499 (url (org-protocol-sanitize-uri (car parts)))
500 (type (if (string-match "^\\([a-z]+\\):" url)
501 (match-string 1 url)))
502 (title(or (cadr parts) ""))
503 (region (or (caddr parts) ""))
504 (orglink (org-make-link-string
505 url (if (string-match "[^[:space:]]" title) title url)))
506 (org-capture-link-is-already-stored t) ;; avoid call to org-store-link
507 remember-annotation-functions)
508 (setq org-stored-links
509 (cons (list url title) org-stored-links))
510 (kill-new orglink)
511 (org-store-link-props :type type
512 :link url
513 :description title
514 :annotation orglink
515 :initial region)
516 (raise-frame)
517 (funcall capture-func nil template)))
520 (defun org-protocol-open-source (fname)
521 "Process an org-protocol://open-source:// style url.
523 Change a filename by mapping URLs to local filenames as set
524 in `org-protocol-project-alist'.
526 The location for a browser's bookmark should look like this:
528 javascript:location.href='org-protocol://open-source://'+ \\
529 encodeURIComponent(location.href)"
531 ;; As we enter this function for a match on our protocol, the return value
532 ;; defaults to nil.
533 (let ((result nil)
534 (f (org-protocol-unhex-string fname)))
535 (catch 'result
536 (dolist (prolist org-protocol-project-alist)
537 (let* ((base-url (plist-get (cdr prolist) :base-url))
538 (wsearch (regexp-quote base-url)))
540 (when (string-match wsearch f)
541 (let* ((wdir (plist-get (cdr prolist) :working-directory))
542 (strip-suffix (plist-get (cdr prolist) :online-suffix))
543 (add-suffix (plist-get (cdr prolist) :working-suffix))
544 ;; Strip "[?#].*$" if `f' is a redirect with another
545 ;; ending than strip-suffix here:
546 (f1 (substring f 0 (string-match "\\([\\?#].*\\)?$" f)))
547 (start-pos (+ (string-match wsearch f1) (length base-url)))
548 (end-pos (string-match
549 (regexp-quote strip-suffix) f1))
550 ;; We have to compare redirects without suffix below:
551 (f2 (concat wdir (substring f1 start-pos end-pos)))
552 (the-file (concat f2 add-suffix)))
554 ;; Note: the-file may still contain `%C3' et al here because browsers
555 ;; tend to encode `&auml;' in URLs to `%25C3' - `%25' being `%'.
556 ;; So the results may vary.
558 ;; -- start redirects --
559 (unless (file-exists-p the-file)
560 (message "File %s does not exist.\nTesting for rewritten URLs." the-file)
561 (let ((rewrites (plist-get (cdr prolist) :rewrites)))
562 (when rewrites
563 (message "Rewrites found: %S" rewrites)
564 (mapc
565 (lambda (rewrite)
566 "Try to match a rewritten URL and map it to a real file."
567 ;; Compare redirects without suffix:
568 (if (string-match (car rewrite) f2)
569 (throw 'result (concat wdir (cdr rewrite)))))
570 rewrites))))
571 ;; -- end of redirects --
573 (if (file-readable-p the-file)
574 (throw 'result the-file))
575 (if (file-exists-p the-file)
576 (message "%s: permission denied!" the-file)
577 (message "%s: no such file or directory." the-file))))))
578 result)))
581 ;;; Core functions:
583 (defun org-protocol-check-filename-for-protocol (fname restoffiles client)
584 "Detect if `org-protocol-the-protocol' and a known sub-protocol is used in fname.
585 Sub-protocols are registered in `org-protocol-protocol-alist' and
586 `org-protocol-protocol-alist-default'.
587 This is, how the matching is done:
589 (string-match \"protocol:/+sub-protocol:/+\" ...)
591 protocol and sub-protocol are regexp-quoted.
593 If a matching protocol is found, the protocol is stripped from fname and the
594 result is passed to the protocols function as the only parameter. If the
595 function returns nil, the filename is removed from the list of filenames
596 passed from emacsclient to the server.
597 If the function returns a non nil value, that value is passed to the server
598 as filename."
599 (let ((sub-protocols (append org-protocol-protocol-alist org-protocol-protocol-alist-default)))
600 (catch 'fname
601 (let ((the-protocol (concat (regexp-quote org-protocol-the-protocol) ":/+")))
602 (when (string-match the-protocol fname)
603 (dolist (prolist sub-protocols)
604 (let ((proto (concat the-protocol (regexp-quote (plist-get (cdr prolist) :protocol)) ":/+")))
605 (when (string-match proto fname)
606 (let* ((func (plist-get (cdr prolist) :function))
607 (greedy (plist-get (cdr prolist) :greedy))
608 (splitted (split-string fname proto))
609 (result (if greedy restoffiles (cadr splitted))))
610 (when (plist-get (cdr prolist) :kill-client)
611 (message "Greedy org-protocol handler. Killing client.")
612 (server-edit))
613 (when (fboundp func)
614 (unless greedy
615 (throw 'fname (funcall func result)))
616 (funcall func result)
617 (throw 'fname t))))))))
618 ;; (message "fname: %s" fname)
619 fname)))
622 (defadvice server-visit-files (before org-protocol-detect-protocol-server activate)
623 "Advice server-visit-flist to call `org-protocol-modify-filename-for-protocol'."
624 (let ((flist (if org-protocol-reverse-list-of-files
625 (reverse (ad-get-arg 0))
626 (ad-get-arg 0)))
627 (client (ad-get-arg 1)))
628 (catch 'greedy
629 (dolist (var flist)
630 (let ((fname (expand-file-name (car var)))) ;; `\' to `/' on windows. FIXME: could this be done any better?
631 (setq fname (org-protocol-check-filename-for-protocol fname (member var flist) client))
632 (if (eq fname t) ;; greedy? We need the `t' return value.
633 (progn
634 (ad-set-arg 0 nil)
635 (throw 'greedy t))
636 (if (stringp fname) ;; probably filename
637 (setcar var fname)
638 (ad-set-arg 0 (delq var (ad-get-arg 0))))))
639 ))))
641 ;;; Org specific functions:
643 (defun org-protocol-create-for-org ()
644 "Create a org-protocol project for the current file's Org-mode project.
645 This works, if the file visited is part of a publishing project in
646 `org-publish-project-alist'. This function calls `org-protocol-create' to do
647 most of the work."
648 (interactive)
649 (require 'org-publish)
650 (let ((all (or (org-publish-get-project-from-filename buffer-file-name))))
651 (if all (org-protocol-create (cdr all))
652 (message "Not in an org-project. Did mean %s?"
653 (substitute-command-keys"\\[org-protocol-create]")))))
656 (defun org-protocol-create(&optional project-plist)
657 "Create a new org-protocol project interactively.
658 An org-protocol project is an entry in `org-protocol-project-alist'
659 which is used by `org-protocol-open-source'.
660 Optionally use project-plist to initialize the defaults for this project. If
661 project-plist is the CDR of an element in `org-publish-project-alist', reuse
662 :base-directory, :html-extension and :base-extension."
663 (interactive)
664 (let ((working-dir (expand-file-name(or (plist-get project-plist :base-directory) default-directory)))
665 (base-url "http://orgmode.org/worg/")
666 (strip-suffix (or (plist-get project-plist :html-extension) ".html"))
667 (working-suffix (if (plist-get project-plist :base-extension)
668 (concat "." (plist-get project-plist :base-extension))
669 ".org"))
671 (worglet-buffer nil)
673 (insert-default-directory t)
674 (minibuffer-allow-text-properties nil))
676 (setq base-url (read-string "Base URL of published content: " base-url nil base-url t))
677 (if (not (string-match "\\/$" base-url))
678 (setq base-url (concat base-url "/")))
680 (setq working-dir
681 (expand-file-name
682 (read-directory-name "Local working directory: " working-dir working-dir t)))
683 (if (not (string-match "\\/$" working-dir))
684 (setq working-dir (concat working-dir "/")))
686 (setq strip-suffix
687 (read-string
688 (concat "Extension to strip from published URLs ("strip-suffix"): ")
689 strip-suffix nil strip-suffix t))
691 (setq working-suffix
692 (read-string
693 (concat "Extension of editable files ("working-suffix"): ")
694 working-suffix nil working-suffix t))
696 (when (yes-or-no-p "Save the new org-protocol-project to your init file? ")
697 (setq org-protocol-project-alist
698 (cons `(,base-url . (:base-url ,base-url
699 :working-directory ,working-dir
700 :online-suffix ,strip-suffix
701 :working-suffix ,working-suffix))
702 org-protocol-project-alist))
703 (customize-save-variable 'org-protocol-project-alist org-protocol-project-alist))))
705 (provide 'org-protocol)
707 ;; arch-tag: b5c5c2ac-77cf-4a94-a649-2163dff95846
708 ;;; org-protocol.el ends here