forked from nobiot/Zero-to-Emacs-and-Org-roam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path+org-protocol-check-filename-for-protocol.el
58 lines (52 loc) · 2.51 KB
/
+org-protocol-check-filename-for-protocol.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(defun +org-protocol-check-filename-for-protocol (fname restoffiles _client)
"Check if `org-protocol-the-protocol' and a valid protocol are 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.
Old-style links such as \"protocol://sub-protocol://param1/param2\" are
also recognized.
If a matching protocol is found, the protocol is stripped from
fname and the result is passed to the protocol function as the
first parameter. The second parameter will be non-nil if FNAME
uses key=val&key2=val2-type arguments, or nil if FNAME uses
val/val2-type arguments. 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.
If the handler function is greedy, RESTOFFILES will also be passed to it.
CLIENT is ignored."
(let ((sub-protocols (append org-protocol-protocol-alist
org-protocol-protocol-alist-default)))
(catch 'fname
(let ((the-protocol (concat (regexp-quote org-protocol-the-protocol)
":/+")))
(when (string-match the-protocol fname)
(dolist (prolist sub-protocols)
(let ((proto
(concat the-protocol
(regexp-quote (plist-get (cdr prolist) :protocol))
"\\(:/+\\|/*\\?\\)")))
(when (string-match proto fname)
(let* ((func (plist-get (cdr prolist) :function))
(greedy (plist-get (cdr prolist) :greedy))
(split (split-string fname proto))
(result (if greedy restoffiles (cadr split)))
(new-style (string-match "/*?" (match-string 1 fname))))
(when (plist-get (cdr prolist) :kill-client)
(message "Greedy org-protocol handler. Killing client.")
(server-edit))
(when (fboundp func)
(unless greedy
(throw 'fname
(if new-style
(funcall func (org-protocol-parse-parameters
result new-style))
(warn "Please update your Org Protocol handler \
to deal with new-style links.")
(funcall func result))))
;; Greedy protocol handlers are responsible for
;; parsing their own filenames.
(funcall func result)
(throw 'fname t))))))))
fname)))