-
Notifications
You must be signed in to change notification settings - Fork 18
/
xwwp-follow-link-consult.el
68 lines (50 loc) · 2.98 KB
/
xwwp-follow-link-consult.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
;;; xwwp-follow-link-consult.el --- Link navigation in `xwidget-webkit' sessions using `consult' -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Damien Merenne <[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
;; `consult' completion.
;;; Code:
(require 'xwwp-follow-link)
(require 'consult)
(defvar xwwp-follow-link-consult-candidates nil "List of candidates for the active completion")
(defvar xwwp-follow-link-consult-history
nil
"Stores history for links selected with command `xwwp-follow-link-completion-backend-consult'.")
(defun consult-xwwp-follow-link--preview (candidates update-fn)
"Create preview function for xwwp links CANDIDATES by calling UPDATE-FN, restore POINT when done."
(lambda (cand restore)
(if restore nil
(let* ((selected cand)
(rest (seq-filter (lambda (c) (not (eq c selected))) candidates))
(xwwp-follow-link-consult-candidates (cons selected rest)))
(funcall update-fn)))))
(defun xwwp-follow-link-consult-lookup (_ candidates cand)
"Lookup for cand in candidates."
(seq-find (lambda (c) (string= (car c) cand)) candidates))
(defclass xwwp-follow-link-completion-backend-consult (xwwp-follow-link-completion-backend) ())
(cl-defmethod xwwp-follow-link-candidates ((_ xwwp-follow-link-completion-backend-consult))
"Follow a link selected with consult."
(seq-map (lambda (c) `(,(cadr c) ,(caddr c))) xwwp-follow-link-consult-candidates))
(cl-defmethod xwwp-follow-link-read ((_ xwwp-follow-link-completion-backend-consult) prompt collection action update-fn)
"Select a link from COLLECTION a with consult showing PROMPT using UPDATE-FN to highlight the link in the xwidget session and execute ACTION once a link has been select."
(funcall action (cdr (consult--read collection
:prompt prompt
:state (consult-xwwp-follow-link--preview collection update-fn)
:require-match t
:sort nil
:lookup #'xwwp-follow-link-consult-lookup
:history 'xwwp-follow-link-consult-history))))
(provide 'xwwp-follow-link-consult)
;;; xwwp-follow-link-consult.el ends here