xwwp-follow-link.el 11 KB

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