-
Notifications
You must be signed in to change notification settings - Fork 18
/
xwwp-follow-link.el
280 lines (230 loc) · 11.8 KB
/
xwwp-follow-link.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
;;; xwwp-follow-link.el --- Link navigation in `xwidget-webkit' sessions -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Damien Merenne <[email protected]>, Q. Hong <[email protected]>
;; This file is NOT part of GNU Emacs.
;;; License:
;; This program is free software: you can redistribute it and/or modify
;; 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.
;; This program is distributed in the hope that it will be useful,
;; 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.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Add support for navigating web pages in `xwidget-webkit' sessions using the
;; minibuffer completion.
;;; Code:
(require 'xwidget)
(require 'xwwp)
(require 'eieio)
(require 'cl-lib)
(require 'ido)
(defgroup xwwp-follow-link nil
"`xwidget-webkit' follow link customizations."
:group 'xwwp)
(defcustom xwwp-follow-link-completion-system 'default
"The completion system to be used by xwidget plus.
Custom function should be a function that takes no arguments and
returns an instance of an eieio class extending
`xwwp-follow-link-completion-backend'."
:group 'xwwp-follow-link
:type '(radio
(const :tag "Ido" ido)
(const :tag "Helm" helm)
(const :tag "Ivy" ivy)
(const :tag "Default" default)
(function :tag "Custom function")))
(defcustom xwwp-follow-link-candidate-style '(("border" . "1px dashed blue")
("background" . "#0000ff20"))
"Style to apply to candidate links."
:type '(list (cons string string))
:group 'xwwp-follow-link)
(defcustom xwwp-follow-link-selected-style '(("border" . "1px dashed red")
("background" . "#ff000020"))
"Style to apply to currently selected link."
:type '(list (cons string string))
:group 'xwwp-follow-link)
(defun xwwp-follow-link-style-definition ()
"Return the css definitions for the follow link feature."
(concat (xwwp-css-make-class "xwwp-follow-link-candidate" xwwp-follow-link-candidate-style)
(xwwp-css-make-class "xwwp-follow-link-selected" xwwp-follow-link-selected-style)))
(xwwp-js-def follow-link cleanup-preview-1 ()
"Remove all custom class from links.""
if (window.__xwidget_plus_follow_link_candidates) {
window.__xwidget_plus_follow_link_candidates.forEach(a => {
a.classList.remove('xwwp-follow-link-candidate', 'xwwp-follow-link-selected');
});
}
")
(defun xwwp-follow-link-cleanup-preview ()
"Remove all custom class from links."
(xwwp-follow-link-cleanup-preview-1 (xwidget-webkit-current-session)))
(xwwp-js-def follow-link cleanup ()
"Remove all custom class from links and cleanup model.""
window.__xwidget_plus_follow_link_candidates.forEach(a => {
a.classList.remove('xwwp-follow-link-candidate', 'xwwp-follow-link-selected');
});
if (window.__xwidget_plus_follow_link_candidates)
window.history.back();
window.__xwidget_plus_follow_link_candidates = null;
")
(xwwp-js-def follow-link highlight (ids selected)
"Highlight IDS as candidate and SELECTED as selected.""
window.__xwidget_plus_follow_link_candidates.forEach((a, id) => {
a.classList.remove('xwwp-follow-link-candidate', 'xwwp-follow-link-selected');
if (selected == id) {
a.classList.add('xwwp-follow-link-selected');
a.scrollIntoView({behavior: 'smooth', block: 'center'});
} else if (ids && ids.includes(id)) {
a.classList.add('xwwp-follow-link-candidate');
}
});
")
(xwwp-js-def follow-link action (link-id)
"Click on the link identified by LINK-ID""
let selected = window.__xwidget_plus_follow_link_candidates[link_id];
__xwidget_plus_follow_link_cleanup();
selected.click();
")
(xwwp-js-def follow-link fetch-links ()
"Fetch all visible, non empty links from the current page.""
var r = {};
window.history.pushState({}, 'xwwp-follow-link');
window.__xwidget_plus_follow_link_candidates = Array.from(document.querySelectorAll('a'));
window.__xwidget_plus_follow_link_candidates.forEach((a, i) => {
if (a.offsetWidth || a.offsetHeight || a.getClientRects().length) {
if (a.innerText.match(/\\\\S/))
r[i] = [a.innerText, a.href];
}
});
return r;
")
;; Completion backend class
(defclass xwwp-follow-link-completion-backend () ((collection) (text)))
(cl-defmethod xwwp-follow-link-candidates ((_backend xwwp-follow-link-completion-backend))
"Return the BACKEND selected link and the candidates.
The return value is a list whose first element is the selected id
link and the rest are the candidates ids.
Return nil if the backend does not support narrowing selection list.")
(cl-defmethod xwwp-follow-link-read ((_backend xwwp-follow-link-completion-backend)
_prompt _collection _action _update-fn)
"Use BACKEND to PROMPT the user for a link in COLLECTION.
ACTION should be called with the resulting link.
UPDATE-FN is a function that can be called when the candidates
list is narrowed.It will highlight the link list in the
browser.")
;; Default backend using completing-read
(defclass xwwp-follow-link-completion-backend-default (xwwp-follow-link-completion-backend) ())
(cl-defmethod xwwp-follow-link-candidates ((backend xwwp-follow-link-completion-backend-default))
"Return the BACKEND selected link and the candidates.
The return value is a list whose first element is the selected id
link and the rest are the candidates ids.
Return nil if the backend does not support narrowing selection list."
(let* ((collection (oref backend collection))
(text (oref backend text))
(matches (seq-filter (lambda (i) (string-match-p (concat "^" (regexp-quote text)) (car i))) collection))
(matches (seq-map #'cdr matches)))
(if (= 1 (length matches))
matches
(cons nil matches))))
(cl-defmethod xwwp-follow-link-read ((backend xwwp-follow-link-completion-backend-default) prompt collection action update-fn)
"Use BACKEND to PROMPT the user for a link in COLLECTION.
ACTION should be called with the resulting link.
UPDATE-FN is a function that can be called when the candidates
list is narrowed.It will highlight the link list in the
browser."
(funcall action (cdr (assoc (completing-read prompt (lambda (str pred flag)
(oset backend text str)
(funcall update-fn)
(complete-with-action flag collection str pred))
nil t)
collection))))
(declare-function xwwp-follow-link-completion-backend-ido "xwwp-follow-link-ido")
(declare-function xwwp-follow-link-completion-backend-ivy "xwwp-follow-link-ivy")
(declare-function xwwp-follow-link-completion-backend-helm "xwwp-follow-link-helm")
(defun xwwp-follow-link-make-backend ()
"Instanciate a completion backend."
(cond ((eq xwwp-follow-link-completion-system 'default)
#'xwwp-follow-link-completion-backend-default)
((eq xwwp-follow-link-completion-system 'ivy)
(unless (require 'xwwp-follow-link-ivy nil t)
(user-error "Install the `xwwp-follow-link-ivy' package to use `xwwp-follow-link' with `ivy'"))
#'xwwp-follow-link-completion-backend-ivy)
((eq xwwp-follow-link-completion-system 'helm)
(unless (require 'xwwp-follow-link-helm nil t)
(user-error "Install the `xwwp-follow-link-helm' package to use `xwwp-follow-link' with `helm'"))
#'xwwp-follow-link-completion-backend-helm)
((eq xwwp-follow-link-completion-system 'ido)
(require 'xwwp-follow-link-ido)
#'xwwp-follow-link-completion-backend-ido)
((eq xwwp-follow-link-completion-system 'consult)
(unless (require 'xwwp-follow-link-consult nil t)
(user-error "Install the `xwwp-follow-link-consult' package to use `xwwp-follow-link' with `consult'"))
#'xwwp-follow-link-completion-backend-consult)
((eq xwwp-follow-link-completion-system 'default)
#'xwwp-follow-link-completion-backend-default)
(t xwwp-follow-link-completion-system)))
(defvar xwwp-follow-link-completion-backend-instance '())
(defun xwwp-follow-link-update (xwidget)
"Highlight LINKS in XWIDGET buffer when updating candidates."
(let ((links (xwwp-follow-link-candidates xwwp-follow-link-completion-backend-instance)))
(when links
(let* ((selected (car links))
(candidates (cdr links)))
(xwwp-follow-link-highlight xwidget (mapcar 'car candidates) (car selected))))))
(defun xwwp-follow-link-trigger-action (xwidget selected)
"Activate link matching SELECTED in XWIDGET LINKS.
The SELECTED value is the cdr of an assoc in the collection passed to
completion back end, which is of the form (numerical-id link-url)"
(xwwp-follow-link-action xwidget (car selected)))
(defun xwwp-follow-link-format-link (str)
"Format link title STR."
(setq str (replace-regexp-in-string "^[[:space:][:cntrl:]]+" "" str))
(setq str (replace-regexp-in-string "[[:space:][:cntrl:]]+$" "" str))
(setq str (replace-regexp-in-string "[[:cntrl:]]+" "/" str))
(replace-regexp-in-string "[[:space:]]+" " " str))
(defun xwwp-follow-link-prepare-links (links)
"Prepare the alist of LINKS."
(seq-sort-by (lambda (v) (cadr v)) #'<
(seq-map (lambda (v) (list (xwwp-follow-link-format-link (aref (cdr v) 0))
(string-to-number (car v))
(aref (cdr v) 1)))
links)))
(defun xwwp-follow-link-callback (links)
"Ask for a link belonging to the alist LINKS.
LINKS maps a numerical ID to an array of form [link-text, link-uri]"
(let* ((xwidget (xwidget-webkit-current-session))
(links (xwwp-follow-link-prepare-links links)))
(oset xwwp-follow-link-completion-backend-instance collection links)
(condition-case nil
(xwwp-follow-link-read xwwp-follow-link-completion-backend-instance
"Link: " links
(apply-partially #'xwwp-follow-link-trigger-action xwidget)
(apply-partially #'xwwp-follow-link-update xwidget))
(quit (xwwp-follow-link-cleanup xwidget)))
(oset xwwp-follow-link-completion-backend-instance collection nil)))
;;;###autoload
(defun xwwp-follow-link (&optional xwidget)
"Ask for a link in the XWIDGET session or the current one and follow it."
(interactive)
(setq xwwp-follow-link-completion-backend-instance (funcall (xwwp-follow-link-make-backend)))
(let ((xwidget (or xwidget (xwidget-webkit-current-session))))
(xwwp-html-inject-style xwidget "__xwidget_plus_follow_link_style" (xwwp-follow-link-style-definition))
(xwwp-js-inject xwidget 'follow-link)
(xwwp-follow-link-fetch-links xwidget #'xwwp-follow-link-callback)))
;; Local Variables:
;; eval: (mmm-mode)
;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode
;; :face mmm-code-submode-face
;; :delimiter-mode nil
;; :front "xwwp--js \"" :back "\" js--")
;; (elisp-defjs :submode js-mode
;; :face mmm-code-submode-face
;; :delimiter-mode nil
;; :front "xwwp-js-def .*\n.*\"\"\n" :back "\")\n")))
;; mmm-classes: elisp-js
;; End:
(provide 'xwwp-follow-link)
;;; xwwp-follow-link.el ends here