summary refs log tree commit diff stats
path: root/lisp/+eww.el
blob: aad602f2a2f7375d611aa95e93a035afb0ff71d2 (plain)
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
;;; +eww.el -*- lexical-binding: t; -*-

;;; Code:

(require 'bookmark)
(require 'eww)

;; Track whether the current page is readable

(defvar-local +eww-readable-p nil
  "Whether `eww-readable' has been toggled on the current buffer.")

(defun +eww-mark-readable (&rest _)
  "ADVICE to mark current eww buffer \"readable.\""
  (setq-local +eww-readable-p t))

(defun +eww-mark-unreadable (&rest _)
  "ADVICE to mark current eww buffer \"unreadable.\""
  (setq-local +eww-readable-p nil))

(defvar +eww-readable-unreadable-after-functions '(eww-render
                                                   eww-reload
                                                   eww-back-url)
  "Functions after which the page is rendered \"unreadable\".")

;;;###autoload
(define-minor-mode +eww-track-readable-mode
  "Track whether the current webpage has been rendered readable."
  :lighter ""
  (if +eww-track-readable-mode
      (progn
        (advice-add 'eww-readable :after #'+eww-mark-readable)
        (dolist (func +eww-readable-unreadable-after-functions)
          (advice-add func :after #'+eww-mark-unreadable)))
    (dolist (func +eww-readable-unreadable-after-functions)
      (advice-remove func #'+eww-mark-unreadable))
    (advice-remove 'eww-readable #'+eww-mark-readable)))

;; Integrate bookmarks in eww

(defun +eww-bookmark--make ()
  "Make eww bookmark record."
  `((filename . ,(plist-get eww-data :url))
    (title . ,(plist-get eww-data :title))
    (time . ,(current-time-string))
    (handler . ,#'+eww-bookmark-handler)
    (defaults . (,(concat
                   ;; url without the https and path
                   (replace-regexp-in-string
                    "/.*" ""
                    (replace-regexp-in-string
                     "\\`https?://" ""
                     (plist-get eww-data :url)))
                   " - "
                   ;; page title
                   (replace-regexp-in-string
                    "\\` +\\| +\\'" ""
                    (replace-regexp-in-string
                     "[\n\t\r ]+" " "
                     (plist-get eww-data :title))))))))

(defun +eww-bookmark-handler (bm)
  "Handler for eww bookmarks."
  (eww-browse-url (alist-get 'filename bm)))

(defun +eww-bookmark-setup ()
  "Setup eww bookmark integration."
  (setq-local bookmark-make-record-function #'eww-bookmark--make))

(provide '+eww)
;;; +eww.el ends here