;;; 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))))))

(defun jao-eww-session-invisible-buffers ()
  (seq-filter (lambda (b) (null (get-buffer-window b)))
              (jao-eww-session-eww-buffers (current-buffer))))

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