From 3ec043aae4bfa73e217c728d683b6d88df1442fa Mon Sep 17 00:00:00 2001 From: jao Date: Mon, 5 Apr 2021 21:55:38 +0100 Subject: jao-eww-session: make do sans timers --- lib/net/jao-eww-session.el | 210 +++++++++------------------------------------ 1 file changed, 40 insertions(+), 170 deletions(-) (limited to 'lib/net/jao-eww-session.el') diff --git a/lib/net/jao-eww-session.el b/lib/net/jao-eww-session.el index 9970481..d3bc5b2 100644 --- a/lib/net/jao-eww-session.el +++ b/lib/net/jao-eww-session.el @@ -1,9 +1,9 @@ -;;; jao-eww-session.el --- Persistent emacs-eww sessions -*- lexical-binding: t; -*- +;;; jao-eww-session.el --- Persistent eww sessions -*- lexical-binding: t; -*- ;; Copyright (C) 2003, 2004, 2006, 2007, 2008, 2009, 2012, 2021 Jose A Ortega Ruiz ;; Author: Jose A Ortega Ruiz -;; Version: 0.3.6 +;; Version: 0.4 ;; Keywords: hypermedia, eww, WWW ;; This file is free software; you can redistribute it and/or modify @@ -23,133 +23,20 @@ ;;; Commentary: -;; INTRODUCTION: -;; -;; jao-eww-session provides persistent emacs-eww browsing sessions. When +;; jao-eww-session provides persistent eww browsing sessions. When ;; quitting eww (or, if you request it, at any other time while using ;; it) you can save the current eww session (that is, the set of open -;; tabs and the URLs they're visiting). Upon restarting emacs-eww -;; (possibly after restarting Emacs itself) you'll have the possibity -;; of recovering the saved session (that is, of re-opening the saved -;; tabs and URLs). You also have at your disposal a command to recover -;; the saved session at any other time. -;; -;; INSTALLATION: -;; -;; Just put this file somewhere on your Emacs load path and add the -;; following line to your .emacs file: -;; -;; (require 'jao-eww-session) -;; -;; After restarting Emacs (or evaluating the form above), each time -;; you start emacs-eww with 'eww' you'll get a prompt asking whether -;; your last browsing session should be loaded. Likewise, when -;; quitting the browser, you'll have the possibility of saving your -;; current session (overwriting the previous one). -;; -;; In addition, two new interactive functions are defined: +;; tabs and the URLs they're visiting). Upon restarting emacs, you'll +;; have the possibity of recovering the saved session (that is, of +;; re-opening the saved tabs and URLs). You also have at your disposal +;; a command to recover the saved session at any other time, via the +;; commands: ;; ;; jao-eww-session-load -- load the last stored session ;; jao-eww-session-save -- save the current session ;; -;; These functions can be invoked at any time while running emacs-eww. -;; Optionally, you can bind them to key shortcuts with the proper -;; variations of the following elisp magic in your .emacs: -;; (defun eww-add-keys () -;; (define-key eww-mode-map "S" 'jao-eww-session-save) -;; (define-key eww-mode-map "L" 'jao-eww-session-load)) -;; (add-hook 'eww-mode-hook 'eww-add-keys) -;; -;; CUSTOMIZATION: -;; -;; A new customization group, jao-eww-session, is available. There you can +;; A customization group, jao-eww-session, is available. There you can ;; customize the following variables: -;; -;; jao-eww-session-load-always -- if t, `jao-eww-session-load' will *not* ask -;; for confirmation (default nil) -;; jao-eww-session-save-always -- if t, `jao-eww-session-save' will *not* ask -;; for confirmation (default nil) -;; jao-eww-session-show-titles -- if t, the load prompt will list the -;; session URL titles (default t) -;; jao-eww-session-duplicate-tabs -- what to do when loading a session that -;; contains a URL already open -;; jao-eww-session-file -- the file where eww session info -;; is stored (default "~/.jao-eww-session") -;; jao-eww-session-autosave-period -- the period, in seconds, for automatic -;; session backup file updating. -;; -;; -;; You can also customize them in your .emacs file, to wit: -;; -;; (setq jao-eww-session-file "~/.emacs.d/jao-eww-session") -;; (setq jao-eww-session-save-always nil) -;; (setq jao-eww-session-load-always nil) -;; (setq jao-eww-session-show-titles t) -;; (setq jao-eww-session-duplicate-tabs 'ask) ; 'never, 'always, 'ask -;; -;; HISTORY: -;; -;; Version 0.4 (March, 2021): -;; -;; - Migrated to eww. -;; -;; Version 0.3.7 : -;; -;; - `jao-w3m-session-deactivate-builtin-sessions', to do what it -;; says. -;; -;; Version 0.3.6 (Sat Apr 19, 2008): -;; -;; - eww-session -> jao-eww-session to avoid collisions with -;; emacs-eww's session manager. -;; -;; Version 0.3.5 (Sun Jan 14, 2007): -;; -;; - automatic session backup every `jao-w3m-session-autosave-period' -;; seconds. -;; -;; Version 0.3.4 (Wed Jul 19, 2006): -;; -;; - save session file on quitting Emacs (without using -;; desktop.el) -;; -;; Version 0.3.3 (Thu Jun 8, 2006): -;; -;; - save session file with pretty print. -;; - handle correctly multiple emacs-eww (re)starts during a -;; single emacs session. -;; - save URLs in hexified form to allow & in them. -;; - code cleanup. -;; -;; Version 0.3.2 (Mon Sep 29, 2003): -;; -;; - bug fix: when searching or going to home/bookmarks/etc, -;; keep the current tab's focus. -;; -;; Version 0.3.1 (Tue Aug 26, 2003): -;; -;; - type of `jao-w3m-session-file' set to 'file' in customisation -;; buffer. -;; - bug fix: syntax error due to a typo in `jao-w3m-session-file' -;; -;; Version 0.3 (Mon Aug 25, 2003): -;; -;; - the load session tab lists the titles of the session's pages -;; (customizable via 'jao-eww-session-show-titles'). -;; - the duplicated tab prompt displays also the URL's title. -;; - bug fix: active tab in session now is correctly saved. -;; -;; Version 0.2 (Fri Aug 22, 2003): -;; -;; - the session info now includes the active tab, which gets -;; displayed when the session is reloaded. -;; - when reloading a session in a running emacs-w3m, if the -;; session contains a URL that is already being displayed by the -;; browser, the tab can be reused or duplicated (customizable -;; via `jao-w3m-session-duplicate-tabs'). -;; -;; Version 0.1 (Wed Aug 20, 2003) -- Initial release. -;; ;;; Code: @@ -193,28 +80,28 @@ the session is already displayed in a eww tab, jao-eww-session can: "File to save the eww session data." :type 'file) -(defvar jao-eww-session-autosave-period 180 - "A backup of the current session is saved with this period (in secs).") - ;;; Internals: ;;;; auxiliary functions (defvar jao-eww-current-session '(jao-eww-session 0 nil)) -(defun jao-eww-session--list-buffers () - (seq-filter (lambda (b) (with-current-buffer b (derived-mode-p 'eww-mode))) +(defun jao-eww-session--list-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 () +(defun jao-eww-session--current-urls (&optional skip-current) (let ((urls) (cb (current-buffer)) (pos 0) (count 0)) - (dolist (b (jao-eww-session--list-buffers) (list pos (reverse urls))) + (dolist (b (jao-eww-session--list-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)) @@ -233,10 +120,10 @@ the session is already displayed in a eww tab, jao-eww-session can: (let ((s (or s jao-eww-current-session))) (mapcar 'cdr (nth 2 s)))) -(defun jao-eww-session-current (&optional s) +(defun jao-eww-session--update-current (&optional skip-current) (save-current-buffer (setq jao-eww-current-session - (or s (cons 'jao-eww-session (jao-eww-session--current-urls)))))) + (cons 'jao-eww-session (jao-eww-session--current-urls skip-current))))) (defun jao-eww-session--find-dups (urls) (seq-filter @@ -259,7 +146,7 @@ the session is already displayed in a eww tab, jao-eww-session can: (format "Load last eww session %S? " (jao-eww-session-titles new-session)) "Load last eww session? ")))) - (jao-eww-session-current new-session)))) + (setq jao-eww-current-session new-session)))) (defun jao-eww-session-from-file (fname) (let ((fname (jao-eww-session--check--backup fname))) @@ -272,21 +159,18 @@ the session is already displayed in a eww tab, jao-eww-session can: (defun jao-eww-session-not-empty () (> (length (jao-eww-session-urls)) 0)) -(defun jao-eww-session--to--file (filename &optional is-auto) +(defun jao-eww-session--to--file (filename &optional is-auto skip) (require 'pp) (when (jao-eww-session-not-empty) - (let ((inhibit-message is-auto)) + (let ((inhibit-message is-auto) + (session (jao-eww-session--update-current skip))) (with-temp-buffer (insert ";;;; File generated by jao-eww-session. DO NOT EDIT!\n") - (pp (jao-eww-session-current) (current-buffer)) + (pp session (current-buffer)) (insert "\n" ";;;; End of " (file-name-nondirectory jao-eww-session-file) "\n") (write-region (point-min) (point-max) (expand-file-name filename)))))) -(defun jao-eww-session-current-to-file () - (jao-eww-session--to--file jao-eww-session-file)) - -(defvar jao-eww-session--timer nil) (defun jao-eww-session--backup-name (fname) (concat (expand-file-name fname) ".bak")) @@ -297,19 +181,16 @@ the session is already displayed in a eww tab, jao-eww-session can: bfname fname))) -(defun jao-eww-session--save-backup () - (jao-eww-session--to--file (jao-eww-session--backup-name jao-eww-session-file) t)) +(defun jao-eww-session--save-backup (&optional skip) + (let ((f (jao-eww-session--backup-name jao-eww-session-file))) + (jao-eww-session--to--file t skip))) -(defun jao-eww-session--restart--autosave () - (when (> jao-eww-session-autosave-period 0) - (when jao-eww-session--timer (cancel-timer jao-eww-session--timer)) - (setq jao-eww-session--timer - (run-at-time jao-eww-session-autosave-period - jao-eww-session-autosave-period - 'jao-eww-session--save-backup)))) +(defun jao-eww-session--save-backup-1 () + (when (derived-mode-p 'eww-mode) (jao-eww-session--save-backup t))) ;;;; save session on checkpoints -(add-to-list 'kill-emacs-query-functions #'jao-eww-session-save) +(add-hook 'kill-emacs-query-functions #'jao-eww-session-save) +(add-hook 'kill-buffer-hook #'jao-eww-session--save-backup-1) (add-hook 'eww-after-render-hook #'jao-eww-session--save-backup) (advice-add 'eww-back-url :after #'jao-eww-session--save-backup) (advice-add 'eww-forward-url :after #'jao-eww-session--save-backup) @@ -323,33 +204,22 @@ the session is already displayed in a eww tab, jao-eww-session can: (interactive) (when (and (jao-eww-session-not-empty) (or jao-eww-session-save-always (y-or-n-p "Save eww session? "))) - (jao-eww-session-current-to-file)) + (jao-eww-session--to--file jao-eww-session-file)) t) ;;;###autoload (defun jao-eww-session-load () "Load last stored session into eww." (interactive) - (let ((s (jao-eww-session-load-aux))) - (when s - (jao-eww-session--restart--autosave) - (let* ((urls (jao-eww-session-urls s)) - (offset (jao-eww-session-offset s)) - (buffers (unless (equal jao-eww-session-duplicate-tabs 'always) - (jao-eww-session--find-dups urls)))) - (dolist (url urls) (eww url 4)) - (seq-each #'kill-buffer buffers) - (unless (zerop offset) - (switch-to-buffer (nth offset (jao-eww-session--list-buffers)))))))) - -;;;###autoload -(defun jao-eww-session-set-autosave-period (secs) - "Set new value for the period between session backup autosaves." - (interactive "p") - (let ((secs (or secs (read-number "New period (secs): " 0)))) - (when (> secs 0) - (setq jao-eww-session-autosave-period secs) - (jao-eww-session--restart--autosave)))) + (when-let ((s (jao-eww-session-load-aux))) + (let* ((urls (jao-eww-session-urls s)) + (offset (jao-eww-session-offset s)) + (buffers (unless (equal jao-eww-session-duplicate-tabs 'always) + (jao-eww-session--find-dups urls)))) + (dolist (url urls) (eww url 4)) + (seq-each #'kill-buffer buffers) + (unless (zerop offset) + (switch-to-buffer (nth offset (jao-eww-session--list-buffers))))))) (provide 'jao-eww-session) ;;; jao-eww-session.el ends here -- cgit v1.2.3