jao-custom-w3m.el 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. ;; -*- lexical-binding: t -*-
  2. ;;; browse-url and afio
  3. (defun jao-w3m-find-url (url)
  4. (let* ((url (w3m-canonicalize-url url))
  5. (fn `(lambda (b)
  6. (with-current-buffer b
  7. (string= ,url (w3m-canonicalize-url w3m-current-url))))))
  8. (when-let (b (seq-find fn (w3m-list-buffers)))
  9. (pop-to-buffer b))))
  10. (defun jao-w3m-browse-url (url &rest r)
  11. (jao-afio-goto-www)
  12. (select-window (frame-first-window))
  13. (unless (jao-w3m-find-url url)
  14. (w3m-goto-url-new-session url)))
  15. (defun jao-w3m-download (arg)
  16. (interactive "P")
  17. (jao-download (w3m-anchor) arg))
  18. (setq jao-afio-use-w3m t)
  19. (setq jao-browse-url-function 'jao-w3m-browse-url)
  20. ;;; multipart html renderer
  21. (defun jao-w3m-html-renderer (handle)
  22. (let ((w3m-message-silent t)
  23. (w3m-fill-column 120)
  24. (mm-w3m-safe-url-regexp nil)
  25. (mm-inline-text-html-with-w3m-keymap t))
  26. (condition-case e
  27. (mm-inline-text-html-render-with-w3m handle)
  28. (error (message "Error rendering page with w3m: %s" e)
  29. (delete-region (point) (point-max))
  30. (let ((shr-use-fonts nil))
  31. (mm-shr handle))))))
  32. (setq mm-text-html-renderer 'jao-w3m-html-renderer)
  33. ;;; notmuch integration
  34. (defvar-local jao-notmuch--showing-images nil)
  35. (defun jao-notmuch--setup-w3m-images (&optional activate)
  36. (setq-local w3m-ignored-image-url-regexp
  37. (unless jao-notmuch--showing-images
  38. notmuch-show-text/html-blocked-images))
  39. (when activate
  40. (setq-local scroll-margin 0)
  41. (w3m-toggle-inline-images (if jao-notmuch--showing-images t 'turnoff))))
  42. (defun jao-notmuch--w3m-toggle-images ()
  43. (save-window-excursion
  44. (when (or (derived-mode-p 'notmuch-show-mode)
  45. (jao-notmuch-goto-message-buffer nil t))
  46. (goto-char (point-min))
  47. (when (re-search-forward "^\\[ text/html " nil t)
  48. (when (looking-at-p "(hidden)")
  49. (notmuch-show-toggle-part-invisibility))
  50. (forward-line 1)
  51. (setq jao-notmuch--showing-images (not jao-notmuch--showing-images))
  52. (jao-notmuch--setup-w3m-images t)))))
  53. (add-hook 'notmuch-show-mode-hook #'jao-notmuch--setup-w3m-images)
  54. ;;; org integration
  55. (defun jao-w3m-get-link ()
  56. (let ((wb (w3m-alive-p)))
  57. (when wb
  58. (let ((url (with-current-buffer wb w3m-current-url))
  59. (title (w3m-buffer-title wb)))
  60. (cons url title)))))
  61. (defun jao-insert-w3m-link ()
  62. (interactive)
  63. (let ((link (jao-w3m-get-link)))
  64. (when link (insert "[[" (car link) "][" (cdr link) "]]"))))
  65. (with-eval-after-load "org"
  66. (require 'ol-w3m nil t)
  67. (define-key org-mode-map "\C-cW" 'jao-insert-w3m-link))
  68. ;;; capture page
  69. (defun jao-w3m-capture-page ()
  70. (interactive)
  71. (let* ((title (w3m-current-title))
  72. (url w3m-current-url)
  73. (html (y-or-n-p "Save as HTML (y) or PS (n)? "))
  74. (basename (concat (read-string "File name: ")
  75. (if html ".html" ".ps")))
  76. (name (expand-file-name basename jao-sink-dir)))
  77. (if html
  78. (progn
  79. (w3m-view-source)
  80. (write-region (point-min) (point-max) name nil nil nil t)
  81. (w3m-view-source))
  82. (progn
  83. (split-window-horizontally 85)
  84. (w3m-redisplay-this-page)
  85. (ps-print-buffer name)
  86. (delete-other-windows)
  87. (w3m-redisplay-this-page)))
  88. (kill-new (format "[[doc:%s][%s]] ([[%s][original]])"
  89. basename title url))))
  90. ;;; consult narrowing
  91. (with-eval-after-load "w3m-util"
  92. (with-eval-after-load "consult"
  93. (defvar jao-consult-w3m-buffer-history nil)
  94. (defun jao-www--item (b)
  95. (with-current-buffer b
  96. (propertize (or w3m-current-title (buffer-name))
  97. 'buffer b
  98. 'url (or w3m-current-url (buffer-name)))))
  99. (defvar jao-consult-w3m-source
  100. (list :name "www buffer"
  101. :category 'www-buffer
  102. :hidden t
  103. :narrow (cons ?w "www")
  104. :annotate (lambda (b) (when b (get-text-property 0 'url b)))
  105. :history 'jao-consult-w3m-buffer-history
  106. :items (lambda ()
  107. (seq-map #'jao-www--item
  108. (seq-filter #'jao-www--buffer-p (buffer-list))))
  109. :action (lambda (b)
  110. (jao-afio-goto-www)
  111. (switch-to-buffer (get-text-property 0 'buffer b)))))
  112. (jao-consult-add-buffer-source 'jao-consult-w3m-source)))
  113. ;;; package
  114. (use-package w3m
  115. :ensure t
  116. :demand t
  117. :custom ((w3m-key-binding 'info)
  118. (w3m-display-mode 'dual-pane))
  119. :init
  120. (setq w3m-add-user-agent nil
  121. w3m-confirm-leaving-secure-page nil
  122. w3m-cookie-accept-bad-cookies t
  123. w3m-cookie-accept-domains '(".github.com"
  124. ".librarything.com"
  125. ".goodreads.com"
  126. ".sr.ht"
  127. ".gnu.org"
  128. ".codeberg.org"
  129. "codeberg.org"
  130. ".bookshop.org"
  131. ".reddit.com")
  132. w3m-cookie-reject-domains '(".")
  133. w3m-default-display-inline-images nil
  134. w3m-default-save-directory "~/doc/sink"
  135. w3m-do-cleanup-temp-files nil
  136. w3m-external-view-temp-directory "/tmp"
  137. w3m-fill-column 110
  138. w3m-goto-article-function 'jao-w3m-browse-url
  139. w3m-form-input-textarea-buffer-lines 40
  140. w3m-history-minimize-in-new-session t
  141. w3m-history-reuse-history-elements nil
  142. w3m-init-file nil
  143. w3m-image-no-idle-timer t
  144. w3m-make-new-session t
  145. w3m-profile-directory "~/.w3m"
  146. w3m-redisplay-pages-automatically-p nil
  147. w3m-resize-images t
  148. w3m-safe-url-regexp nil
  149. w3m-search-default-engine "duckduckgo" ; "google-en"
  150. w3m-select-buffer-horizontal-window nil
  151. w3m-select-buffer-window-ratio '(20 . 40)
  152. w3m-session-load-last-sessions t
  153. w3m-session-load-crashed-sessions 'ask
  154. w3m-show-graphic-icons-in-header-line nil
  155. w3m-show-graphic-icons-in-mode-line nil
  156. w3m-toggle-inline-images-permanently nil
  157. w3m-use-tab nil
  158. w3m-use-tab-line nil
  159. w3m-use-title-buffer-name t
  160. w3m-use-cookies t
  161. w3m-use-filter nil
  162. w3m-use-favicon nil
  163. w3m-use-header-line nil
  164. w3m-use-refresh nil
  165. w3m-use-symbol t)
  166. :config
  167. :bind (:map w3m-mode-map
  168. (("+" . w3m-zoom-in-image)
  169. ("-" . w3m-zoom-out-image)
  170. ("C-c C-@" . tracking-next-buffer)
  171. ("C-c C-SPC" . tracking-next-buffer)
  172. ("C-c C-b" . nil)
  173. ("C-l" . nil)
  174. ("C-c c" . jao-w3m-capture-page)
  175. ("b" . w3m-view-previous-page)
  176. ("B" . w3m-view-next-page)
  177. ("c" . w3m-print-this-url)
  178. ("d" . jao-w3m-download)
  179. ("D" . w3m-download)
  180. ("f" . w3m-lnum-follow)
  181. ("v" . jao-view-video)
  182. ("q" . w3m-delete-buffer)
  183. ("w" . org-w3m-copy-for-org-mode)
  184. ("x" . jao-rss-subscribe)
  185. ("y" . w3m-print-current-url))))
  186. ;;; textsec
  187. ;; the way in which w3m constructs ALT text for links to images confuses
  188. ;; makes it suspicious under textsec link check.
  189. (with-eval-after-load "textsec"
  190. (advice-add 'textsec-link-suspicious-p :override #'ignore))
  191. ;;; .
  192. (provide 'jao-custom-w3m)