2009-03-30 09:47:34 -04:00
|
|
|
;;; org-protocol.el --- Intercept calls from emacsclient to trigger custom actions.
|
|
|
|
;;
|
2010-04-07 10:26:10 -04:00
|
|
|
;; Copyright (C) 2008, 2009, 2010
|
2009-08-04 18:59:09 -04:00
|
|
|
;; Free Software Foundation, Inc.
|
2009-03-30 09:47:34 -04:00
|
|
|
;;
|
|
|
|
;; Author: Bastien Guerry <bzg AT altern DOT org>
|
|
|
|
;; Author: Daniel M German <dmg AT uvic DOT org>
|
|
|
|
;; Author: Sebastian Rose <sebastian_rose AT gmx DOT de>
|
|
|
|
;; Author: Ross Patterson <me AT rpatterson DOT net>
|
|
|
|
;; Maintainer: Sebastian Rose <sebastian_rose AT gmx DOT de>
|
|
|
|
;; Keywords: org, emacsclient, wp
|
2011-03-07 09:27:39 -05:00
|
|
|
;; Version: 7.5
|
2009-03-30 09:47:34 -04:00
|
|
|
|
2009-08-04 18:59:09 -04:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
;;
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
2009-03-30 09:47:34 -04:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
2009-08-04 18:59:09 -04:00
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
2009-03-30 09:47:34 -04:00
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
2009-08-04 18:59:09 -04:00
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; Commentary:
|
|
|
|
;;
|
|
|
|
;; Intercept calls from emacsclient to trigger custom actions.
|
|
|
|
;;
|
2010-07-16 17:23:01 -04:00
|
|
|
;; This is done by advising `server-visit-files' to scan the list of filenames
|
|
|
|
;; for `org-protocol-the-protocol' and sub-protocols defined in
|
2009-03-30 09:47:34 -04:00
|
|
|
;; `org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'.
|
|
|
|
;;
|
|
|
|
;; Any application that supports calling external programs with an URL
|
|
|
|
;; as argument may be used with this functionality.
|
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;; Usage:
|
|
|
|
;; ------
|
|
|
|
;;
|
|
|
|
;; 1.) Add this to your init file (.emacs probably):
|
|
|
|
;;
|
|
|
|
;; (add-to-list 'load-path "/path/to/org-protocol/")
|
|
|
|
;; (require 'org-protocol)
|
|
|
|
;;
|
|
|
|
;; 3.) Ensure emacs-server is up and running.
|
|
|
|
;; 4.) Try this from the command line (adjust the URL as needed):
|
|
|
|
;;
|
|
|
|
;; $ emacsclient \
|
|
|
|
;; org-protocol://store-link://http:%2F%2Flocalhost%2Findex.html/The%20title
|
|
|
|
;;
|
|
|
|
;; 5.) Optionally add custom sub-protocols and handlers:
|
|
|
|
;;
|
|
|
|
;; (setq org-protocol-protocol-alist
|
|
|
|
;; '(("my-protocol"
|
|
|
|
;; :protocol "my-protocol"
|
2010-07-16 17:23:01 -04:00
|
|
|
;; :function my-protocol-handler-function)))
|
2009-03-30 09:47:34 -04:00
|
|
|
;;
|
|
|
|
;; A "sub-protocol" will be found in URLs like this:
|
|
|
|
;;
|
|
|
|
;; org-protocol://sub-protocol://data
|
|
|
|
;;
|
|
|
|
;; If it works, you can now setup other applications for using this feature.
|
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;; As of March 2009 Firefox users follow the steps documented on
|
|
|
|
;; http://kb.mozillazine.org/Register_protocol, Opera setup is described here:
|
|
|
|
;; http://www.opera.com/support/kb/view/535/
|
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;; Documentation
|
|
|
|
;; -------------
|
|
|
|
;;
|
|
|
|
;; org-protocol.el comes with and installs handlers to open sources of published
|
|
|
|
;; online content, store and insert the browser's URLs and cite online content
|
|
|
|
;; by clicking on a bookmark in Firefox, Opera and probably other browsers and
|
|
|
|
;; applications:
|
|
|
|
;;
|
|
|
|
;; * `org-protocol-open-source' uses the sub-protocol \"open-source\" and maps
|
|
|
|
;; URLs to local filenames defined in `org-protocol-project-alist'.
|
|
|
|
;;
|
|
|
|
;; * `org-protocol-store-link' stores an Org-link (if Org-mode is present) and
|
2010-07-16 17:23:01 -04:00
|
|
|
;; pushes the browsers URL to the `kill-ring' for yanking. This handler is
|
2009-03-30 09:47:34 -04:00
|
|
|
;; triggered through the sub-protocol \"store-link\".
|
|
|
|
;;
|
2010-06-22 10:20:13 -04:00
|
|
|
;; * Call `org-protocol-capture' by using the sub-protocol \"capture\". If
|
2010-07-16 17:23:01 -04:00
|
|
|
;; Org-mode is loaded, Emacs will pop-up a capture buffer and fill the
|
|
|
|
;; template with the data provided. I.e. the browser's URL is inserted as an
|
|
|
|
;; Org-link of which the page title will be the description part. If text
|
2009-03-30 09:47:34 -04:00
|
|
|
;; was select in the browser, that text will be the body of the entry.
|
|
|
|
;;
|
2010-06-22 10:20:13 -04:00
|
|
|
;; * Call `org-protocol-remember' by using the sub-protocol \"remember\".
|
|
|
|
;; This is provided for backward compatibility.
|
|
|
|
;; You may read `org-capture' as `org-remember' throughout this file if
|
|
|
|
;; you still use `org-remember'.
|
|
|
|
;;
|
2009-03-30 09:47:34 -04:00
|
|
|
;; You may use the same bookmark URL for all those standard handlers and just
|
|
|
|
;; adjust the sub-protocol used:
|
|
|
|
;;
|
|
|
|
;; location.href='org-protocol://sub-protocol://'+
|
|
|
|
;; encodeURIComponent(location.href)+'/'+
|
|
|
|
;; encodeURIComponent(document.title)+'/'+
|
|
|
|
;; encodeURIComponent(window.getSelection())
|
|
|
|
;;
|
2010-06-22 10:20:13 -04:00
|
|
|
;; The handler for the sub-protocol \"capture\" detects an optional template
|
2009-03-30 09:47:34 -04:00
|
|
|
;; char that, if present, triggers the use of a special template.
|
|
|
|
;; Example:
|
|
|
|
;;
|
|
|
|
;; location.href='org-protocol://sub-protocol://x/'+ ...
|
|
|
|
;;
|
|
|
|
;; use template ?x.
|
|
|
|
;;
|
2009-11-12 07:39:29 -05:00
|
|
|
;; Note, that using double slashes is optional from org-protocol.el's point of
|
|
|
|
;; view because emacsclient squashes the slashes to one.
|
2009-03-30 09:47:34 -04:00
|
|
|
;;
|
|
|
|
;;
|
|
|
|
;; provides: 'org-protocol
|
|
|
|
;;
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'org)
|
2009-03-31 14:39:54 -04:00
|
|
|
(eval-when-compile
|
|
|
|
(require 'cl))
|
2009-03-30 09:47:34 -04:00
|
|
|
|
2009-03-31 14:39:54 -04:00
|
|
|
(declare-function org-publish-get-project-from-filename "org-publish"
|
|
|
|
(filename &optional up))
|
2009-09-30 03:20:28 -04:00
|
|
|
(declare-function server-edit "server" (&optional arg))
|
2009-04-18 14:52:12 -04:00
|
|
|
|
2010-11-21 05:44:44 -05:00
|
|
|
(define-obsolete-function-alias
|
|
|
|
'org-protocol-unhex-compound 'org-link-unescape-compound
|
2011-02-17 14:21:54 -05:00
|
|
|
"2011-02-17")
|
2010-11-21 05:44:44 -05:00
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
'org-protocol-unhex-string 'org-link-unescape
|
2011-02-17 14:21:54 -05:00
|
|
|
"2011-02-17")
|
2010-11-21 05:44:44 -05:00
|
|
|
|
|
|
|
(define-obsolete-function-alias
|
|
|
|
'org-protocol-unhex-single-byte-sequence
|
|
|
|
'org-link-unescape-single-byte-sequence
|
2011-02-17 14:21:54 -05:00
|
|
|
"2011-02-17")
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
(defgroup org-protocol nil
|
|
|
|
"Intercept calls from emacsclient to trigger custom actions.
|
|
|
|
|
|
|
|
This is done by advising `server-visit-files' to scann the list of filenames
|
|
|
|
for `org-protocol-the-protocol' and sub-procols defined in
|
|
|
|
`org-protocol-protocol-alist' and `org-protocol-protocol-alist-default'."
|
|
|
|
:version "22.1"
|
|
|
|
:group 'convenience
|
|
|
|
:group 'org)
|
|
|
|
|
|
|
|
|
|
|
|
;;; Variables:
|
|
|
|
|
|
|
|
(defconst org-protocol-protocol-alist-default
|
2009-04-03 11:36:44 -04:00
|
|
|
'(("org-remember" :protocol "remember" :function org-protocol-remember :kill-client t)
|
2010-06-22 10:20:13 -04:00
|
|
|
("org-capture" :protocol "capture" :function org-protocol-capture :kill-client t)
|
2009-03-30 09:47:34 -04:00
|
|
|
("org-store-link" :protocol "store-link" :function org-protocol-store-link)
|
|
|
|
("org-open-source" :protocol "open-source" :function org-protocol-open-source))
|
|
|
|
"Default protocols to use.
|
|
|
|
See `org-protocol-protocol-alist' for a description of this variable.")
|
|
|
|
|
|
|
|
(defconst org-protocol-the-protocol "org-protocol"
|
|
|
|
"This is the protocol to detect if org-protocol.el is loaded.
|
2010-07-16 17:23:01 -04:00
|
|
|
`org-protocol-protocol-alist-default' and `org-protocol-protocol-alist' hold
|
|
|
|
the sub-protocols that trigger the required action. You will have to define
|
|
|
|
just one protocol handler OS-wide (MS-Windows) or per application (Linux).
|
|
|
|
That protocol handler should call emacsclient.")
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
;;; User variables:
|
|
|
|
|
|
|
|
(defcustom org-protocol-reverse-list-of-files t
|
2011-03-23 05:14:03 -04:00
|
|
|
"Non-nil means re-reverse the list of filenames passed on the command line.
|
2010-07-16 17:23:01 -04:00
|
|
|
The filenames passed on the command line are passed to the emacs-server in
|
|
|
|
reverse order. Set to t (default) to re-reverse the list, i.e. use the
|
|
|
|
sequence on the command line. If nil, the sequence of the filenames is
|
2009-03-30 09:47:34 -04:00
|
|
|
unchanged."
|
|
|
|
:group 'org-protocol
|
|
|
|
:type 'boolean)
|
|
|
|
|
|
|
|
(defcustom org-protocol-project-alist nil
|
2011-03-23 05:14:03 -04:00
|
|
|
"Map URLs to local filenames for `org-protocol-open-source' (open-source).
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
Each element of this list must be of the form:
|
|
|
|
|
|
|
|
(module-name :property value property: value ...)
|
|
|
|
|
|
|
|
where module-name is an arbitrary name. All the values are strings.
|
|
|
|
|
|
|
|
Possible properties are:
|
|
|
|
|
|
|
|
:online-suffix - the suffix to strip from the published URLs
|
|
|
|
:working-suffix - the replacement for online-suffix
|
|
|
|
:base-url - the base URL, e.g. http://www.example.com/project/
|
|
|
|
Last slash required.
|
|
|
|
:working-directory - the local working directory. This is, what base-url will
|
|
|
|
be replaced with.
|
2009-11-15 06:24:40 -05:00
|
|
|
:redirects - A list of cons cells, each of which maps a regular
|
|
|
|
expression to match to a path relative to :working-directory.
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
(setq org-protocol-project-alist
|
|
|
|
'((\"http://orgmode.org/worg/\"
|
|
|
|
:online-suffix \".php\"
|
|
|
|
:working-suffix \".org\"
|
|
|
|
:base-url \"http://orgmode.org/worg/\"
|
|
|
|
:working-directory \"/home/user/org/Worg/\")
|
|
|
|
(\"http://localhost/org-notes/\"
|
|
|
|
:online-suffix \".html\"
|
|
|
|
:working-suffix \".org\"
|
|
|
|
:base-url \"http://localhost/org/\"
|
2009-11-15 06:24:40 -05:00
|
|
|
:working-directory \"/home/user/org/\"
|
|
|
|
:rewrites ((\"org/?$\" . \"index.php\")))))
|
|
|
|
|
|
|
|
The last line tells `org-protocol-open-source' to open
|
|
|
|
/home/user/org/index.php, if the URL cannot be mapped to an existing
|
|
|
|
file, and ends with either \"org\" or \"org/\".
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
Consider using the interactive functions `org-protocol-create' and
|
|
|
|
`org-protocol-create-for-org' to help you filling this variable with valid contents."
|
|
|
|
:group 'org-protocol
|
|
|
|
:type 'alist)
|
|
|
|
|
|
|
|
(defcustom org-protocol-protocol-alist nil
|
|
|
|
"* Register custom handlers for org-protocol.
|
|
|
|
|
|
|
|
Each element of this list must be of the form:
|
|
|
|
|
2009-04-03 11:36:44 -04:00
|
|
|
(module-name :protocol protocol :function func :kill-client nil)
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
protocol - protocol to detect in a filename without trailing colon and slashes.
|
|
|
|
See rfc1738 section 2.1 for more on this.
|
|
|
|
If you define a protocol \"my-protocol\", `org-protocol-check-filename-for-protocol'
|
|
|
|
will search filenames for \"org-protocol:/my-protocol:/\"
|
|
|
|
and trigger your action for every match. `org-protocol' is defined in
|
2010-07-16 17:23:01 -04:00
|
|
|
`org-protocol-the-protocol'. Double and triple slashes are compressed
|
2009-03-30 09:47:34 -04:00
|
|
|
to one by emacsclient.
|
|
|
|
|
|
|
|
function - function that handles requests with protocol and takes exactly one
|
|
|
|
argument: the filename with all protocols stripped. If the function
|
|
|
|
returns nil, emacsclient and -server do nothing. Any non-nil return
|
|
|
|
value is considered a valid filename and thus passed to the server.
|
|
|
|
|
|
|
|
`org-protocol.el provides some support for handling those filenames,
|
|
|
|
if you stay with the conventions used for the standard handlers in
|
|
|
|
`org-protocol-protocol-alist-default'. See `org-protocol-split-data'.
|
|
|
|
|
2009-04-03 11:36:44 -04:00
|
|
|
kill-client - If t, kill the client immediately, once the sub-protocol is
|
2009-11-12 07:39:29 -05:00
|
|
|
detected. This is necessary for actions that can be interrupted by
|
2010-07-16 17:23:01 -04:00
|
|
|
`C-g' to avoid dangling emacsclients. Note, that all other command
|
2009-04-03 11:36:44 -04:00
|
|
|
line arguments but the this one will be discarded, greedy handlers
|
|
|
|
still receive the whole list of arguments though.
|
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
Here is an example:
|
|
|
|
|
|
|
|
(setq org-protocol-protocol-alist
|
|
|
|
'((\"my-protocol\"
|
|
|
|
:protocol \"my-protocol\"
|
2010-07-16 17:23:01 -04:00
|
|
|
:function my-protocol-handler-function)
|
2009-03-30 09:47:34 -04:00
|
|
|
(\"your-protocol\"
|
|
|
|
:protocol \"your-protocol\"
|
2010-07-16 17:23:01 -04:00
|
|
|
:function your-protocol-handler-function)))"
|
2009-03-30 09:47:34 -04:00
|
|
|
:group 'org-protocol
|
|
|
|
:type '(alist))
|
|
|
|
|
2010-09-01 01:15:48 -04:00
|
|
|
(defcustom org-protocol-default-template-key nil
|
2011-03-23 05:25:38 -04:00
|
|
|
"The default template key to use.
|
|
|
|
This is usually a single character string but can also be a
|
|
|
|
string with two characters."
|
2009-07-27 18:02:10 -04:00
|
|
|
:group 'org-protocol
|
|
|
|
:type 'string)
|
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
;;; Helper functions:
|
|
|
|
|
|
|
|
(defun org-protocol-sanitize-uri (uri)
|
2010-07-16 17:23:01 -04:00
|
|
|
"emacsclient compresses double and triple slashes.
|
2009-03-30 09:47:34 -04:00
|
|
|
Slashes are sanitized to double slashes here."
|
|
|
|
(when (string-match "^\\([a-z]+\\):/" uri)
|
|
|
|
(let* ((splitparts (split-string uri "/+")))
|
|
|
|
(setq uri (concat (car splitparts) "//" (mapconcat 'identity (cdr splitparts) "/")))))
|
|
|
|
uri)
|
|
|
|
|
2011-03-23 05:14:03 -04:00
|
|
|
(defun org-protocol-split-data (data &optional unhexify separator)
|
|
|
|
"Split what an org-protocol handler function gets as only argument.
|
|
|
|
DATA is that one argument. DATA is split at each occurrence of
|
|
|
|
SEPARATOR (regexp). If no SEPARATOR is specified or SEPARATOR is
|
2010-07-16 17:23:01 -04:00
|
|
|
nil, assume \"/+\". The results of that splitting are returned
|
2011-03-23 05:14:03 -04:00
|
|
|
as a list. If UNHEXIFY is non-nil, hex-decode each split part.
|
|
|
|
If UNHEXIFY is a function, use that function to decode each split
|
2010-07-16 17:23:01 -04:00
|
|
|
part."
|
2009-03-30 09:47:34 -04:00
|
|
|
(let* ((sep (or separator "/+"))
|
|
|
|
(split-parts (split-string data sep)))
|
|
|
|
(if unhexify
|
2009-04-18 16:16:37 -04:00
|
|
|
(if (fboundp unhexify)
|
|
|
|
(mapcar unhexify split-parts)
|
2010-12-15 14:01:51 -05:00
|
|
|
(mapcar 'org-link-unescape split-parts))
|
2009-03-30 09:47:34 -04:00
|
|
|
split-parts)))
|
|
|
|
|
2009-04-04 17:33:24 -04:00
|
|
|
(defun org-protocol-flatten-greedy (param-list &optional strip-path replacement)
|
2010-04-15 06:24:55 -04:00
|
|
|
"Greedy handlers might receive a list like this from emacsclient:
|
2011-03-23 05:25:38 -04:00
|
|
|
'((\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
|
2010-04-15 06:24:55 -04:00
|
|
|
where \"/dir/\" is the absolute path to emacsclients working directory. This
|
2011-03-23 05:25:38 -04:00
|
|
|
function transforms it into a flat list using `org-protocol-flatten' and
|
2009-04-04 17:33:24 -04:00
|
|
|
transforms the elements of that list as follows:
|
|
|
|
|
|
|
|
If strip-path is non-nil, remove the \"/dir/\" prefix from all members of
|
|
|
|
param-list.
|
|
|
|
|
|
|
|
If replacement is string, replace the \"/dir/\" prefix with it.
|
|
|
|
|
|
|
|
The first parameter, the one that contains the protocols, is always changed.
|
|
|
|
Everything up to the end of the protocols is stripped.
|
|
|
|
|
2009-04-05 09:28:31 -04:00
|
|
|
Note, that this function will always behave as if
|
|
|
|
`org-protocol-reverse-list-of-files' was set to t and the returned list will
|
|
|
|
reflect that. I.e. emacsclients first parameter will be the first one in the
|
|
|
|
returned list."
|
|
|
|
(let* ((l (org-protocol-flatten (if org-protocol-reverse-list-of-files
|
|
|
|
param-list
|
|
|
|
(reverse param-list))))
|
2009-04-04 17:33:24 -04:00
|
|
|
(trigger (car l))
|
|
|
|
(len 0)
|
|
|
|
dir
|
|
|
|
ret)
|
2009-04-05 09:28:31 -04:00
|
|
|
(when (string-match "^\\(.*\\)\\(org-protocol:/+[a-zA-z0-9][-_a-zA-z0-9]*:/+\\)\\(.*\\)" trigger)
|
2009-04-04 17:33:24 -04:00
|
|
|
(setq dir (match-string 1 trigger))
|
|
|
|
(setq len (length dir))
|
|
|
|
(setcar l (concat dir (match-string 3 trigger))))
|
|
|
|
(if strip-path
|
|
|
|
(progn
|
|
|
|
(dolist (e l ret)
|
|
|
|
(setq ret
|
2009-06-08 02:00:46 -04:00
|
|
|
(append ret
|
2009-04-04 17:33:24 -04:00
|
|
|
(list
|
|
|
|
(if (stringp e)
|
|
|
|
(if (stringp replacement)
|
|
|
|
(setq e (concat replacement (substring e len)))
|
|
|
|
(setq e (substring e len)))
|
|
|
|
e)))))
|
|
|
|
ret)
|
|
|
|
l)))
|
|
|
|
|
|
|
|
(defun org-protocol-flatten (l)
|
2010-04-15 06:24:55 -04:00
|
|
|
"Greedy handlers might receive a list like this from emacsclient:
|
2009-04-04 17:33:24 -04:00
|
|
|
'( (\"/dir/org-protocol:/greedy:/~/path1\" (23 . 12)) (\"/dir/param\")
|
2010-04-15 06:24:55 -04:00
|
|
|
where \"/dir/\" is the absolute path to emacsclients working directory.
|
|
|
|
This function transforms it into a flat list."
|
2009-04-04 17:33:24 -04:00
|
|
|
(if (null l) ()
|
|
|
|
(if (listp l)
|
|
|
|
(append (org-protocol-flatten (car l)) (org-protocol-flatten (cdr l)))
|
|
|
|
(list l))))
|
|
|
|
|
2011-03-23 05:14:03 -04:00
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
;;; Standard protocol handlers:
|
|
|
|
|
|
|
|
(defun org-protocol-store-link (fname)
|
2010-07-16 17:23:01 -04:00
|
|
|
"Process an org-protocol://store-link:// style url.
|
|
|
|
Additionally store a browser URL as an org link. Also pushes the
|
|
|
|
link's URL to the `kill-ring'.
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
The location for a browser's bookmark has to look like this:
|
|
|
|
|
|
|
|
javascript:location.href='org-protocol://store-link://'+ \\
|
|
|
|
encodeURIComponent(location.href)
|
|
|
|
encodeURIComponent(document.title)+'/'+ \\
|
|
|
|
|
|
|
|
Don't use `escape()'! Use `encodeURIComponent()' instead. The title of the page
|
|
|
|
could contain slashes and the location definitely will.
|
|
|
|
|
|
|
|
The sub-protocol used to reach this function is set in
|
|
|
|
`org-protocol-protocol-alist'."
|
|
|
|
(let* ((splitparts (org-protocol-split-data fname t))
|
|
|
|
(uri (org-protocol-sanitize-uri (car splitparts)))
|
|
|
|
(title (cadr splitparts))
|
|
|
|
orglink)
|
|
|
|
(if (boundp 'org-stored-links)
|
|
|
|
(setq org-stored-links (cons (list uri title) org-stored-links)))
|
|
|
|
(kill-new uri)
|
|
|
|
(message "`%s' to insert new org-link, `%s' to insert `%s'"
|
|
|
|
(substitute-command-keys"\\[org-insert-link]")
|
|
|
|
(substitute-command-keys"\\[yank]")
|
|
|
|
uri))
|
|
|
|
nil)
|
|
|
|
|
2011-03-23 05:14:03 -04:00
|
|
|
(defun org-protocol-remember (info)
|
2009-03-30 09:47:34 -04:00
|
|
|
"Process an org-protocol://remember:// style url.
|
|
|
|
|
2010-06-22 10:20:13 -04:00
|
|
|
The location for a browser's bookmark has to look like this:
|
|
|
|
|
|
|
|
javascript:location.href='org-protocol://remember://'+ \\
|
|
|
|
encodeURIComponent(location.href)+'/' \\
|
|
|
|
encodeURIComponent(document.title)+'/'+ \\
|
|
|
|
encodeURIComponent(window.getSelection())
|
|
|
|
|
|
|
|
See the docs for `org-protocol-capture' for more information."
|
|
|
|
|
|
|
|
(if (and (boundp 'org-stored-links)
|
2011-04-08 12:18:02 -04:00
|
|
|
(fboundp 'org-capture)
|
2010-06-22 10:20:13 -04:00
|
|
|
(org-protocol-do-capture info 'org-remember))
|
2011-04-08 12:18:02 -04:00
|
|
|
(message "Item remembered."))
|
2010-06-22 10:20:13 -04:00
|
|
|
nil)
|
|
|
|
|
2011-03-23 05:14:03 -04:00
|
|
|
(defun org-protocol-capture (info)
|
2010-06-22 10:20:13 -04:00
|
|
|
"Process an org-protocol://capture:// style url.
|
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
The sub-protocol used to reach this function is set in
|
|
|
|
`org-protocol-protocol-alist'.
|
|
|
|
|
2009-11-12 07:39:29 -05:00
|
|
|
This function detects an URL, title and optional text, separated by '/'
|
2009-03-30 09:47:34 -04:00
|
|
|
The location for a browser's bookmark has to look like this:
|
|
|
|
|
2010-06-22 10:20:13 -04:00
|
|
|
javascript:location.href='org-protocol://capture://'+ \\
|
2009-07-18 13:58:42 -04:00
|
|
|
encodeURIComponent(location.href)+'/' \\
|
2009-03-30 09:47:34 -04:00
|
|
|
encodeURIComponent(document.title)+'/'+ \\
|
|
|
|
encodeURIComponent(window.getSelection())
|
|
|
|
|
2009-07-27 18:02:10 -04:00
|
|
|
By default, it uses the character `org-protocol-default-template-key',
|
2010-06-22 10:20:13 -04:00
|
|
|
which should be associated with a template in `org-capture-templates'.
|
2009-07-27 18:02:10 -04:00
|
|
|
But you may prepend the encoded URL with a character and a slash like so:
|
2009-03-30 09:47:34 -04:00
|
|
|
|
2010-06-22 10:20:13 -04:00
|
|
|
javascript:location.href='org-protocol://capture://b/'+ ...
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
Now template ?b will be used."
|
|
|
|
(if (and (boundp 'org-stored-links)
|
2011-04-08 12:18:02 -04:00
|
|
|
(fboundp 'org-capture)
|
2010-06-22 10:20:13 -04:00
|
|
|
(org-protocol-do-capture info 'org-capture))
|
2011-04-08 12:18:02 -04:00
|
|
|
(message "Item captured."))
|
2009-03-30 09:47:34 -04:00
|
|
|
nil)
|
|
|
|
|
2010-06-22 10:20:13 -04:00
|
|
|
(defun org-protocol-do-capture (info capture-func)
|
|
|
|
"Support `org-capture' and `org-remember' alike.
|
|
|
|
CAPTURE-FUNC is either the symbol `org-remember' or `org-capture'."
|
|
|
|
(let* ((parts (org-protocol-split-data info t))
|
2011-03-23 05:25:38 -04:00
|
|
|
(template (or (and (>= 2 (length (car parts))) (pop parts))
|
2010-06-22 10:20:13 -04:00
|
|
|
org-protocol-default-template-key))
|
|
|
|
(url (org-protocol-sanitize-uri (car parts)))
|
|
|
|
(type (if (string-match "^\\([a-z]+\\):" url)
|
|
|
|
(match-string 1 url)))
|
2011-03-23 05:14:03 -04:00
|
|
|
(title (or (cadr parts) ""))
|
2010-06-22 10:20:13 -04:00
|
|
|
(region (or (caddr parts) ""))
|
|
|
|
(orglink (org-make-link-string
|
|
|
|
url (if (string-match "[^[:space:]]" title) title url)))
|
2010-06-23 10:21:22 -04:00
|
|
|
(org-capture-link-is-already-stored t) ;; avoid call to org-store-link
|
2010-06-22 10:20:13 -04:00
|
|
|
remember-annotation-functions)
|
|
|
|
(setq org-stored-links
|
|
|
|
(cons (list url title) org-stored-links))
|
|
|
|
(kill-new orglink)
|
|
|
|
(org-store-link-props :type type
|
|
|
|
:link url
|
|
|
|
:description title
|
2010-06-23 10:21:22 -04:00
|
|
|
:annotation orglink
|
2010-06-22 10:20:13 -04:00
|
|
|
:initial region)
|
|
|
|
(raise-frame)
|
|
|
|
(funcall capture-func nil template)))
|
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
(defun org-protocol-open-source (fname)
|
|
|
|
"Process an org-protocol://open-source:// style url.
|
|
|
|
|
|
|
|
Change a filename by mapping URLs to local filenames as set
|
|
|
|
in `org-protocol-project-alist'.
|
|
|
|
|
|
|
|
The location for a browser's bookmark should look like this:
|
|
|
|
|
|
|
|
javascript:location.href='org-protocol://open-source://'+ \\
|
|
|
|
encodeURIComponent(location.href)"
|
|
|
|
;; As we enter this function for a match on our protocol, the return value
|
|
|
|
;; defaults to nil.
|
|
|
|
(let ((result nil)
|
2010-12-15 14:01:51 -05:00
|
|
|
(f (org-link-unescape fname)))
|
2009-03-30 09:47:34 -04:00
|
|
|
(catch 'result
|
|
|
|
(dolist (prolist org-protocol-project-alist)
|
|
|
|
(let* ((base-url (plist-get (cdr prolist) :base-url))
|
|
|
|
(wsearch (regexp-quote base-url)))
|
|
|
|
|
|
|
|
(when (string-match wsearch f)
|
|
|
|
(let* ((wdir (plist-get (cdr prolist) :working-directory))
|
|
|
|
(strip-suffix (plist-get (cdr prolist) :online-suffix))
|
|
|
|
(add-suffix (plist-get (cdr prolist) :working-suffix))
|
2009-11-15 06:24:40 -05:00
|
|
|
;; Strip "[?#].*$" if `f' is a redirect with another
|
|
|
|
;; ending than strip-suffix here:
|
|
|
|
(f1 (substring f 0 (string-match "\\([\\?#].*\\)?$" f)))
|
|
|
|
(start-pos (+ (string-match wsearch f1) (length base-url)))
|
2009-03-30 09:47:34 -04:00
|
|
|
(end-pos (string-match
|
2009-11-15 06:24:40 -05:00
|
|
|
(regexp-quote strip-suffix) f1))
|
|
|
|
;; We have to compare redirects without suffix below:
|
|
|
|
(f2 (concat wdir (substring f1 start-pos end-pos)))
|
|
|
|
(the-file (concat f2 add-suffix)))
|
|
|
|
|
|
|
|
;; Note: the-file may still contain `%C3' et al here because browsers
|
|
|
|
;; tend to encode `ä' in URLs to `%25C3' - `%25' being `%'.
|
|
|
|
;; So the results may vary.
|
|
|
|
|
|
|
|
;; -- start redirects --
|
|
|
|
(unless (file-exists-p the-file)
|
|
|
|
(message "File %s does not exist.\nTesting for rewritten URLs." the-file)
|
|
|
|
(let ((rewrites (plist-get (cdr prolist) :rewrites)))
|
|
|
|
(when rewrites
|
|
|
|
(message "Rewrites found: %S" rewrites)
|
|
|
|
(mapc
|
|
|
|
(lambda (rewrite)
|
|
|
|
"Try to match a rewritten URL and map it to a real file."
|
|
|
|
;; Compare redirects without suffix:
|
|
|
|
(if (string-match (car rewrite) f2)
|
|
|
|
(throw 'result (concat wdir (cdr rewrite)))))
|
|
|
|
rewrites))))
|
|
|
|
;; -- end of redirects --
|
|
|
|
|
2009-03-30 09:47:34 -04:00
|
|
|
(if (file-readable-p the-file)
|
|
|
|
(throw 'result the-file))
|
|
|
|
(if (file-exists-p the-file)
|
|
|
|
(message "%s: permission denied!" the-file)
|
|
|
|
(message "%s: no such file or directory." the-file))))))
|
|
|
|
result)))
|
|
|
|
|
|
|
|
|
|
|
|
;;; Core functions:
|
|
|
|
|
2009-04-03 11:36:44 -04:00
|
|
|
(defun org-protocol-check-filename-for-protocol (fname restoffiles client)
|
2009-03-30 09:47:34 -04:00
|
|
|
"Detect if `org-protocol-the-protocol' and a known sub-protocol is used in fname.
|
|
|
|
Sub-protocols are registered in `org-protocol-protocol-alist' and
|
|
|
|
`org-protocol-protocol-alist-default'.
|
|
|
|
This is, how the matching is done:
|
|
|
|
|
|
|
|
(string-match \"protocol:/+sub-protocol:/+\" ...)
|
|
|
|
|
|
|
|
protocol and sub-protocol are regexp-quoted.
|
|
|
|
|
2010-07-16 17:23:01 -04:00
|
|
|
If a matching protocol is found, the protocol is stripped from fname and the
|
2009-03-30 09:47:34 -04:00
|
|
|
result is passed to the protocols function as the only parameter. If the
|
|
|
|
function returns nil, the filename is removed from the list of filenames
|
|
|
|
passed from emacsclient to the server.
|
|
|
|
If the function returns a non nil value, that value is passed to the server
|
|
|
|
as filename."
|
2011-03-23 05:14:03 -04:00
|
|
|
(let ((sub-protocols (append org-protocol-protocol-alist
|
|
|
|
org-protocol-protocol-alist-default)))
|
2009-03-30 09:47:34 -04:00
|
|
|
(catch 'fname
|
|
|
|
(let ((the-protocol (concat (regexp-quote org-protocol-the-protocol) ":/+")))
|
|
|
|
(when (string-match the-protocol fname)
|
|
|
|
(dolist (prolist sub-protocols)
|
2011-03-23 05:14:03 -04:00
|
|
|
(let ((proto (concat the-protocol
|
|
|
|
(regexp-quote (plist-get (cdr prolist) :protocol)) ":/+")))
|
2009-03-30 09:47:34 -04:00
|
|
|
(when (string-match proto fname)
|
|
|
|
(let* ((func (plist-get (cdr prolist) :function))
|
|
|
|
(greedy (plist-get (cdr prolist) :greedy))
|
|
|
|
(splitted (split-string fname proto))
|
|
|
|
(result (if greedy restoffiles (cadr splitted))))
|
2009-04-18 14:52:12 -04:00
|
|
|
(when (plist-get (cdr prolist) :kill-client)
|
|
|
|
(message "Greedy org-protocol handler. Killing client.")
|
|
|
|
(server-edit))
|
2009-03-30 09:47:34 -04:00
|
|
|
(when (fboundp func)
|
|
|
|
(unless greedy
|
|
|
|
(throw 'fname (funcall func result)))
|
|
|
|
(funcall func result)
|
|
|
|
(throw 'fname t))))))))
|
|
|
|
;; (message "fname: %s" fname)
|
|
|
|
fname)))
|
|
|
|
|
|
|
|
(defadvice server-visit-files (before org-protocol-detect-protocol-server activate)
|
|
|
|
"Advice server-visit-flist to call `org-protocol-modify-filename-for-protocol'."
|
|
|
|
(let ((flist (if org-protocol-reverse-list-of-files
|
|
|
|
(reverse (ad-get-arg 0))
|
2009-04-03 11:36:44 -04:00
|
|
|
(ad-get-arg 0)))
|
|
|
|
(client (ad-get-arg 1)))
|
2009-03-30 09:47:34 -04:00
|
|
|
(catch 'greedy
|
|
|
|
(dolist (var flist)
|
2011-03-23 05:14:03 -04:00
|
|
|
;; `\' to `/' on windows. FIXME: could this be done any better?
|
|
|
|
(let ((fname (expand-file-name (car var))))
|
|
|
|
(setq fname (org-protocol-check-filename-for-protocol
|
|
|
|
fname (member var flist) client))
|
2009-03-30 09:47:34 -04:00
|
|
|
(if (eq fname t) ;; greedy? We need the `t' return value.
|
|
|
|
(progn
|
|
|
|
(ad-set-arg 0 nil)
|
|
|
|
(throw 'greedy t))
|
|
|
|
(if (stringp fname) ;; probably filename
|
|
|
|
(setcar var fname)
|
2011-03-23 05:14:03 -04:00
|
|
|
(ad-set-arg 0 (delq var (ad-get-arg 0))))))))))
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
;;; Org specific functions:
|
|
|
|
|
|
|
|
(defun org-protocol-create-for-org ()
|
|
|
|
"Create a org-protocol project for the current file's Org-mode project.
|
|
|
|
This works, if the file visited is part of a publishing project in
|
2010-07-16 17:23:01 -04:00
|
|
|
`org-publish-project-alist'. This function calls `org-protocol-create' to do
|
2009-03-30 09:47:34 -04:00
|
|
|
most of the work."
|
|
|
|
(interactive)
|
2009-03-31 14:39:54 -04:00
|
|
|
(require 'org-publish)
|
2009-03-30 09:47:34 -04:00
|
|
|
(let ((all (or (org-publish-get-project-from-filename buffer-file-name))))
|
|
|
|
(if all (org-protocol-create (cdr all))
|
|
|
|
(message "Not in an org-project. Did mean %s?"
|
|
|
|
(substitute-command-keys"\\[org-protocol-create]")))))
|
|
|
|
|
2011-03-23 05:14:03 -04:00
|
|
|
(defun org-protocol-create (&optional project-plist)
|
2009-03-30 09:47:34 -04:00
|
|
|
"Create a new org-protocol project interactively.
|
|
|
|
An org-protocol project is an entry in `org-protocol-project-alist'
|
|
|
|
which is used by `org-protocol-open-source'.
|
2009-11-15 06:24:40 -05:00
|
|
|
Optionally use project-plist to initialize the defaults for this project. If
|
2009-03-30 09:47:34 -04:00
|
|
|
project-plist is the CDR of an element in `org-publish-project-alist', reuse
|
|
|
|
:base-directory, :html-extension and :base-extension."
|
|
|
|
(interactive)
|
2011-03-23 05:14:03 -04:00
|
|
|
(let ((working-dir (expand-file-name
|
|
|
|
(or (plist-get project-plist :base-directory)
|
|
|
|
default-directory)))
|
2009-03-30 09:47:34 -04:00
|
|
|
(base-url "http://orgmode.org/worg/")
|
|
|
|
(strip-suffix (or (plist-get project-plist :html-extension) ".html"))
|
|
|
|
(working-suffix (if (plist-get project-plist :base-extension)
|
|
|
|
(concat "." (plist-get project-plist :base-extension))
|
|
|
|
".org"))
|
|
|
|
(worglet-buffer nil)
|
|
|
|
(insert-default-directory t)
|
|
|
|
(minibuffer-allow-text-properties nil))
|
|
|
|
|
|
|
|
(setq base-url (read-string "Base URL of published content: " base-url nil base-url t))
|
|
|
|
(if (not (string-match "\\/$" base-url))
|
|
|
|
(setq base-url (concat base-url "/")))
|
|
|
|
|
|
|
|
(setq working-dir
|
|
|
|
(expand-file-name
|
|
|
|
(read-directory-name "Local working directory: " working-dir working-dir t)))
|
|
|
|
(if (not (string-match "\\/$" working-dir))
|
|
|
|
(setq working-dir (concat working-dir "/")))
|
|
|
|
|
|
|
|
(setq strip-suffix
|
|
|
|
(read-string
|
2011-03-23 05:14:03 -04:00
|
|
|
(concat "Extension to strip from published URLs (" strip-suffix "): ")
|
2009-03-30 09:47:34 -04:00
|
|
|
strip-suffix nil strip-suffix t))
|
|
|
|
|
|
|
|
(setq working-suffix
|
|
|
|
(read-string
|
2011-03-23 05:14:03 -04:00
|
|
|
(concat "Extension of editable files (" working-suffix "): ")
|
2009-03-30 09:47:34 -04:00
|
|
|
working-suffix nil working-suffix t))
|
|
|
|
|
2009-11-15 06:24:40 -05:00
|
|
|
(when (yes-or-no-p "Save the new org-protocol-project to your init file? ")
|
2009-03-30 09:47:34 -04:00
|
|
|
(setq org-protocol-project-alist
|
|
|
|
(cons `(,base-url . (:base-url ,base-url
|
|
|
|
:working-directory ,working-dir
|
|
|
|
:online-suffix ,strip-suffix
|
|
|
|
:working-suffix ,working-suffix))
|
|
|
|
org-protocol-project-alist))
|
2009-07-27 18:02:10 -04:00
|
|
|
(customize-save-variable 'org-protocol-project-alist org-protocol-project-alist))))
|
2009-03-30 09:47:34 -04:00
|
|
|
|
|
|
|
(provide 'org-protocol)
|
2009-08-08 00:34:24 -04:00
|
|
|
|
2009-09-18 02:10:59 -04:00
|
|
|
;; arch-tag: b5c5c2ac-77cf-4a94-a649-2163dff95846
|
2009-03-30 09:47:34 -04:00
|
|
|
;;; org-protocol.el ends here
|