summaryrefslogtreecommitdiffhomepage
path: root/lib/net/jao-eww-session.el
blob: fc158d6a81b51ad7a8cbd71930c323fce068f09d (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
;;; jao-eww-session.el --- Persistent eww sessions -*- lexical-binding: t; -*-

;; Copyright (C) 2003-2004, 2006-2009, 2012, 2021-2022, 2025-2026  Jose A Ortega Ruiz

;; Author: Jose A Ortega Ruiz <jao@gnu.org>
;; Version: 0.4
;; Keywords: hypermedia, eww, WWW

;; This file 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, or (at your option)
;; any later version.

;; This file 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 GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Code:

;;; Dependencies:

(require 'eww)
(require 'url)
(require 'multisession)

;;; Internals:

;;;; auxiliary functions

(define-multisession-variable jao-eww-session '(0))

(defun jao-eww-session--current (&optional s)
  (when s (setf (multisession-value jao-eww-session) s))
  (multisession-value jao-eww-session))

(defun jao-eww-session-eww-buffers (&optional skip)
  (seq-filter (lambda (b)
                (when (not (eq b skip))
                  (with-current-buffer b (derived-mode-p 'eww-mode))))
              (buffer-list)))

(defun jao-eww--current-url ()
  (when-let* ((url (eww-current-url))) (url-encode-url url)))

(defun jao-eww-session--current-urls (&optional skip-current)
  (let ((urls)
        (cb (current-buffer))
        (pos 0)
        (count 0))
    (dolist (b (jao-eww-session-eww-buffers (when skip-current cb))
               (list pos (reverse urls)))
      (set-buffer b)
      (when-let* ((url (jao-eww--current-url)))
        (when (eq b cb) (setq pos count))
        (setq count (1+ count))
        (push (cons url (jao-eww-buffer-title)) urls)))))

(defun jao-eww-session-urls (&optional s)
  (mapcar 'car (cadr (or s (jao-eww-session--current)))))

(defun jao-eww-session-offset (&optional s)
  (car (or s (jao-eww-session--current))))

(defun jao-eww-session-titles (&optional s)
  (let ((s (or s jao-eww-current-session)))
    (mapcar 'cdr (cadr s))))

(defun jao-eww-session--update ()
  (jao-eww-session--current (jao-eww-session--current-urls)))

(defun jao-eww-session--find-dups (urls)
  (seq-filter
   (lambda (b)
     (with-current-buffer b (member (jao-eww--current-url) urls)))
   (jao-eww-session-eww-buffers)))

;;;; save session on checkpoints
(add-hook 'eww-after-render-hook #'jao-eww-session--update)
(advice-add 'eww-back-url :after #'jao-eww-session--update)
(advice-add 'eww-forward-url :after #'jao-eww-session--update)

(defun jao-eww-buffer-title () (plist-get eww-data :title))

(defun jao-eww-session-load ()
  "Load last stored session into eww."
  (interactive)
  (let* ((urls (jao-eww-session-urls))
         (offset (jao-eww-session-offset))
         (dups (jao-eww-session--find-dups urls)))
    (dolist (url urls) (eww url 4))
    (seq-each #'kill-buffer dups)
    (unless (zerop offset)
      (switch-to-buffer (nth offset (jao-eww-session-eww-buffers))))))

(provide 'jao-eww-session)
;;; jao-eww-session.el ends here