Function: org-protocol-open-source
org-protocol-open-source is a byte-compiled function defined in
org-protocol.el.gz.
Signature
(org-protocol-open-source FNAME)
Documentation
Process an org-protocol://open-source?url= style URL with FNAME.
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?' +
new URLSearchParams({url: location.href})
or if you prefer to keep compatibility with older Org versions (9.0 to 9.4), consider the following expression:
javascript:location.href = 'org-protocol://open-source?url=' + \
encodeURIComponent(location.href)
Source Code
;; Defined in /usr/src/emacs/lisp/org/org-protocol.el.gz
(defun org-protocol-open-source (fname)
"Process an org-protocol://open-source?url= style URL with FNAME.
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?\\=' +
new URLSearchParams({url: location.href})
or if you prefer to keep compatibility with older Org versions (9.0 to 9.4),
consider the following expression:
javascript:location.href = \\='org-protocol://open-source?url=\\=' + \\
encodeURIComponent(location.href)"
;; As we enter this function for a match on our protocol, the return value
;; defaults to nil.
(let (;; (result nil)
(f (org-protocol-sanitize-uri
(plist-get (org-protocol-parse-parameters fname nil '(:url))
:url))))
(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))
;; 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)))
(end-pos (if strip-suffix
(string-match (regexp-quote strip-suffix) f1)
(length f1)))
;; We have to compare redirects without suffix below:
(f2 (concat wdir (substring f1 start-pos end-pos)))
(the-file (if add-suffix (concat f2 add-suffix) f2)))
;; 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)
(dolist (rewrite rewrites)
;; Try to match a rewritten URL and map it to
;; a real file. Compare redirects without
;; suffix.
(when (string-match (car rewrite) f1)
(let ((replacement
(concat (directory-file-name
(replace-match "" nil nil f1 1))
(cdr rewrite))))
(throw 'result (concat wdir replacement))))))))
;; -- end of redirects --
(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))))))
nil))) ;; FIXME: Really?