xwwp-follow-link.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. ;;; xwwp-follow-link.el --- Link navigation in `xwidget-webkit' sessions -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020 Damien Merenne <dam@cosinux.org>, Q. Hong <qhong@mit.edu>
  3. ;; This file is NOT part of GNU Emacs.
  4. ;;; License:
  5. ;; This program is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Add support for navigating web pages in `xwidget-webkit' sessions using the
  17. ;; minibuffer completion.
  18. ;;; Code:
  19. (require 'xwidget)
  20. (require 'xwwp)
  21. (require 'eieio)
  22. (require 'cl-lib)
  23. (require 'ido)
  24. (defgroup xwwp-follow-link nil
  25. "`xwidget-webkit' follow link customizations."
  26. :group 'xwwp)
  27. (defcustom xwwp-follow-link-completion-system 'default
  28. "The completion system to be used by xwidget plus.
  29. Custom function should be a function that takes no arguments and
  30. returns an instance of an eieio class extending
  31. `xwwp-follow-link-completion-backend'."
  32. :group 'xwwp-follow-link
  33. :type '(radio
  34. (const :tag "Ido" ido)
  35. (const :tag "Helm" helm)
  36. (const :tag "Ivy" ivy)
  37. (const :tag "Default" default)
  38. (function :tag "Custom function")))
  39. (defcustom xwwp-follow-link-candidate-style '(("border" . "1px dashed blue")
  40. ("background" . "#0000ff20"))
  41. "Style to apply to candidate links."
  42. :type '(list (cons string string))
  43. :group 'xwwp-follow-link)
  44. (defcustom xwwp-follow-link-selected-style '(("border" . "1px dashed red")
  45. ("background" . "#ff000020"))
  46. "Style to apply to currently selected link."
  47. :type '(list (cons string string))
  48. :group 'xwwp-follow-link)
  49. (defun xwwp-follow-link-style-definition ()
  50. "Return the css definitions for the follow link feature."
  51. (concat (xwwp-css-make-class "xwwp-follow-link-candidate" xwwp-follow-link-candidate-style)
  52. (xwwp-css-make-class "xwwp-follow-link-selected" xwwp-follow-link-selected-style)))
  53. (xwwp-js-def follow-link cleanup ()
  54. "Remove all custom class from links.""
  55. window.__xwidget_plus_follow_link_candidates.forEach(a => {
  56. a.classList.remove('xwwp-follow-link-candidate', 'xwwp-follow-link-selected');
  57. });
  58. window.__xwidget_plus_follow_link_candidates = null;
  59. ")
  60. (xwwp-js-def follow-link highlight (ids selected)
  61. "Highlight IDS as candidate and SELECTED as selected.""
  62. window.__xwidget_plus_follow_link_candidates.forEach((a, id) => {
  63. a.classList.remove('xwwp-follow-link-candidate', 'xwwp-follow-link-selected');
  64. if (selected == id) {
  65. a.classList.add('xwwp-follow-link-selected');
  66. a.scrollIntoView({behavior: 'smooth', block: 'center'});
  67. } else if (ids && ids.includes(id)) {
  68. a.classList.add('xwwp-follow-link-candidate');
  69. }
  70. });
  71. ")
  72. (xwwp-js-def follow-link action (link-id)
  73. "Click on the link identified by LINK-ID""
  74. let selected = window.__xwidget_plus_follow_link_candidates[link_id];
  75. __xwidget_plus_follow_link_cleanup();
  76. selected.click();
  77. ")
  78. (xwwp-js-def follow-link fetch-links ()
  79. "Fetch all visible, non empty links from the current page.""
  80. var r = {};
  81. window.__xwidget_plus_follow_link_candidates = Array.from(document.querySelectorAll('a'));
  82. window.__xwidget_plus_follow_link_candidates.forEach((a, i) => {
  83. if (a.offsetWidth || a.offsetHeight || a.getClientRects().length) {
  84. if (a.innerText.match(/\\S/))
  85. r[i] = [a.innerText, a.href];
  86. }
  87. });
  88. return r;
  89. ")
  90. ;; Completion backend class
  91. (defclass xwwp-follow-link-completion-backend () ((collection) (text)))
  92. (cl-defmethod xwwp-follow-link-candidates ((_backend xwwp-follow-link-completion-backend))
  93. "Return the BACKEND selected link and the candidates.
  94. The return value is a list whose first element is the selected id
  95. link and the rest are the candidates ids.
  96. Return nil if the backend does not support narrowing selection list.")
  97. (cl-defmethod xwwp-follow-link-read ((_backend xwwp-follow-link-completion-backend)
  98. _prompt _collection _action _update-fn)
  99. "Use BACKEND to PROMPT the user for a link in COLLECTION.
  100. ACTION should be called with the resulting link.
  101. UPDATE-FN is a function that can be called when the candidates
  102. list is narrowed.It will highlight the link list in the
  103. browser.")
  104. ;; Default backend using completing-read
  105. (defclass xwwp-follow-link-completion-backend-default (xwwp-follow-link-completion-backend) ())
  106. (cl-defmethod xwwp-follow-link-candidates ((backend xwwp-follow-link-completion-backend-default))
  107. "Return the BACKEND selected link and the candidates.
  108. The return value is a list whose first element is the selected id
  109. link and the rest are the candidates ids.
  110. Return nil if the backend does not support narrowing selection list."
  111. (let* ((collection (oref backend collection))
  112. (text (oref backend text))
  113. (matches (seq-filter (lambda (i) (string-match-p (concat "^" (regexp-quote text)) (car i))) collection))
  114. (matches (seq-map #'cdr matches)))
  115. (if (= 1 (length matches))
  116. matches
  117. (cons nil matches))))
  118. (cl-defmethod xwwp-follow-link-read ((backend xwwp-follow-link-completion-backend-default) prompt collection action update-fn)
  119. "Use BACKEND to PROMPT the user for a link in COLLECTION.
  120. ACTION should be called with the resulting link.
  121. UPDATE-FN is a function that can be called when the candidates
  122. list is narrowed.It will highlight the link list in the
  123. browser."
  124. (funcall action (cdr (assoc (completing-read prompt (lambda (str pred flag)
  125. (oset backend text str)
  126. (funcall update-fn)
  127. (complete-with-action flag collection str pred))
  128. nil t)
  129. collection))))
  130. (declare-function xwwp-follow-link-completion-backend-ido "xwwp-follow-link-ido")
  131. (declare-function xwwp-follow-link-completion-backend-ivy "xwwp-follow-link-ivy")
  132. (declare-function xwwp-follow-link-completion-backend-helm "xwwp-follow-link-helm")
  133. (defun xwwp-follow-link-make-backend ()
  134. "Instanciate a completion backend."
  135. (cond ((eq xwwp-follow-link-completion-system 'default)
  136. #'xwwp-follow-link-completion-backend-default)
  137. ((eq xwwp-follow-link-completion-system 'ivy)
  138. (unless (require 'xwwp-follow-link-ivy nil t)
  139. (user-error "Install the `xwwp-follow-link-ivy' package to use `xwwp-follow-link' with `ivy'"))
  140. #'xwwp-follow-link-completion-backend-ivy)
  141. ((eq xwwp-follow-link-completion-system 'helm)
  142. (unless (require 'xwwp-follow-link-helm nil t)
  143. (user-error "Install the `xwwp-follow-link-helm' package to use `xwwp-follow-link' with `helm'"))
  144. #'xwwp-follow-link-completion-backend-helm)
  145. ((eq xwwp-follow-link-completion-system 'ido)
  146. (require 'xwwp-follow-link-ido)
  147. #'xwwp-follow-link-completion-backend-ido)
  148. ((eq xwwp-follow-link-completion-system 'default)
  149. #'xwwp-follow-link-completion-backend-default)
  150. (t xwwp-follow-link-completion-system)))
  151. (defvar xwwp-follow-link-completion-backend-instance '())
  152. (defun xwwp-follow-link-update (xwidget)
  153. "Highlight LINKS in XWIDGET buffer when updating candidates."
  154. (let ((links (xwwp-follow-link-candidates xwwp-follow-link-completion-backend-instance)))
  155. (when links
  156. (let* ((selected (car links))
  157. (candidates (cdr links)))
  158. (xwwp-follow-link-highlight xwidget (mapcar 'car candidates) (car selected))))))
  159. (defun xwwp-follow-link-trigger-action (xwidget selected)
  160. "Activate link matching SELECTED in XWIDGET LINKS.
  161. The SELECTED value is the cdr of an assoc in the collection passed to
  162. completion back end, which is of the form (numerical-id link-url)"
  163. (xwwp-follow-link-action xwidget (car selected)))
  164. (defun xwwp-follow-link-format-link (str)
  165. "Format link title STR."
  166. (setq str (replace-regexp-in-string "^[[:space:][:cntrl:]]+" "" str))
  167. (setq str (replace-regexp-in-string "[[:space:][:cntrl:]]+$" "" str))
  168. (setq str (replace-regexp-in-string "[[:cntrl:]]+" "/" str))
  169. (replace-regexp-in-string "[[:space:]]+" " " str))
  170. (defun xwwp-follow-link-prepare-links (links)
  171. "Prepare the alist of LINKS."
  172. (seq-sort-by (lambda (v) (cadr v)) #'<
  173. (seq-map (lambda (v) (list (xwwp-follow-link-format-link (aref (cdr v) 0))
  174. (string-to-number (car v))
  175. (aref (cdr v) 1)))
  176. links)))
  177. (defun xwwp-follow-link-callback (links)
  178. "Ask for a link belonging to the alist LINKS.
  179. LINKS maps a numerical ID to an array of form [link-text, link-uri]"
  180. (let* ((xwidget (xwidget-webkit-current-session))
  181. (links (xwwp-follow-link-prepare-links links)))
  182. (oset xwwp-follow-link-completion-backend-instance collection links)
  183. (condition-case nil
  184. (xwwp-follow-link-read xwwp-follow-link-completion-backend-instance
  185. "Link: " links
  186. (apply-partially #'xwwp-follow-link-trigger-action xwidget)
  187. (apply-partially #'xwwp-follow-link-update xwidget))
  188. (quit (xwwp-follow-link-cleanup xwidget)))
  189. (oset xwwp-follow-link-completion-backend-instance collection nil)))
  190. ;;;###autoload
  191. (defun xwwp-follow-link (&optional xwidget)
  192. "Ask for a link in the XWIDGET session or the current one and follow it."
  193. (interactive)
  194. (setq xwwp-follow-link-completion-backend-instance (funcall (xwwp-follow-link-make-backend)))
  195. (let ((xwidget (or xwidget (xwidget-webkit-current-session))))
  196. (xwwp-html-inject-style xwidget "__xwidget_plus_follow_link_style" (xwwp-follow-link-style-definition))
  197. (xwwp-js-execute xwidget 'follow-link)
  198. (xwwp-follow-link-fetch-links xwidget #'xwwp-follow-link-callback)))
  199. ;; Local Variables:
  200. ;; eval: (mmm-mode)
  201. ;; eval: (mmm-add-group 'elisp-js '((elisp-rawjs :submode js-mode
  202. ;; :face mmm-code-submode-face
  203. ;; :delimiter-mode nil
  204. ;; :front "xwwp--js \"" :back "\" js--")
  205. ;; (elisp-defjs :submode js-mode
  206. ;; :face mmm-code-submode-face
  207. ;; :delimiter-mode nil
  208. ;; :front "xwwp-js-def .*\n.*\"\"\n" :back "\")\n")))
  209. ;; mmm-classes: elisp-js
  210. ;; End:
  211. (provide 'xwwp-follow-link)
  212. ;;; xwwp-follow-link.el ends here