From fcc1c0f41764750f6edfc232c3587dcf69aa1a01 Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 4 Apr 2021 21:42:32 +0100 Subject: jao-eww-session clean-ups --- lib/net/jao-eww-session.el | 84 +++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 54 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 06f9287..9970481 100644 --- a/lib/net/jao-eww-session.el +++ b/lib/net/jao-eww-session.el @@ -95,7 +95,7 @@ ;; ;; Version 0.3.7 : ;; -;; - `jao-eww-session-deactivate-builtin-sessions', to do what it +;; - `jao-w3m-session-deactivate-builtin-sessions', to do what it ;; says. ;; ;; Version 0.3.6 (Sat Apr 19, 2008): @@ -105,7 +105,7 @@ ;; ;; Version 0.3.5 (Sun Jan 14, 2007): ;; -;; - automatic session backup every `jao-eww-session-autosave-period' +;; - automatic session backup every `jao-w3m-session-autosave-period' ;; seconds. ;; ;; Version 0.3.4 (Wed Jul 19, 2006): @@ -128,9 +128,9 @@ ;; ;; Version 0.3.1 (Tue Aug 26, 2003): ;; -;; - type of `jao-eww-session-file' set to 'file' in customisation +;; - type of `jao-w3m-session-file' set to 'file' in customisation ;; buffer. -;; - bug fix: syntax error due to a typo in `jao-eww-session-file' +;; - bug fix: syntax error due to a typo in `jao-w3m-session-file' ;; ;; Version 0.3 (Mon Aug 25, 2003): ;; @@ -143,15 +143,14 @@ ;; ;; - the session info now includes the active tab, which gets ;; displayed when the session is reloaded. -;; - when reloading a session in a running emacs-eww, if the +;; - 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-eww-session-duplicate-tabs'). +;; via `jao-w3m-session-duplicate-tabs'). ;; ;; Version 0.1 (Wed Aug 20, 2003) -- Initial release. ;; - ;;; Code: ;;; Dependencies: @@ -168,17 +167,14 @@ (defcustom jao-eww-session-save-always nil "If on, always save eww session without asking." - :group 'jao-eww-session :type 'boolean) (defcustom jao-eww-session-load-always nil "If on, always load eww session without asking." - :group 'jao-eww-session :type 'boolean) (defcustom jao-eww-session-show-titles t "If on, show URL titles in the load prompt." - :group 'jao-eww-session :type 'boolean) (defcustom jao-eww-session-duplicate-tabs 'never @@ -189,38 +185,30 @@ the session is already displayed in a eww tab, jao-eww-session can: - `never' create a new tab (just reload it), or - `always' duplicate the URL in a new tab, or - `ask' the user what to do." - :group 'jao-eww-session :type '(choice (const :value never) (const :value always) (const :value ask))) (defcustom jao-eww-session-file "~/.emacs.d/eww-session.eld" "File to save the eww session data." - :group 'jao-eww-session :type 'file) (defvar jao-eww-session-autosave-period 180 "A backup of the current session is saved with this period (in secs).") -(defvar jao-eww-url-filters nil "URL filters.") - ;;; Internals: ;;;; auxiliary functions (defvar jao-eww-current-session '(jao-eww-session 0 nil)) -(defun jao-eww-session--filter (url filters) - (cond ((not filters) url) - ((string-match-p (caar filters) url) - (cond ((functionp (cdar filters)) (funcall (cadr filters) url)) - ((stringp (cdar filters)) (cdar filters)))) - (t (jao-eww-session--filter url (cdr filters))))) - (defun jao-eww-session--list-buffers () (seq-filter (lambda (b) (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 () (let ((urls) (cb (current-buffer)) @@ -228,11 +216,10 @@ the session is already displayed in a eww tab, jao-eww-session can: (count 0)) (dolist (b (jao-eww-session--list-buffers) (list pos (reverse urls))) (set-buffer b) - (let ((url (jao-eww-session--filter (eww-current-url) jao-eww-url-filters))) - (when url - (when (eq b cb) (setq pos count)) - (setq count (1+ count)) - (push (cons (url-encode-url url) (jao-eww-buffer-title)) urls)))))) + (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) (let ((s (or s jao-eww-current-session))) @@ -251,28 +238,16 @@ the session is already displayed in a eww tab, jao-eww-session can: (setq jao-eww-current-session (or s (cons 'jao-eww-session (jao-eww-session--current-urls)))))) -(defun jao-eww-session-find-duplicated (urls) - (save-current-buffer - (let* ((duplicate-p - (lambda () - (y-or-n-p - (format "'%s' (%s) is already open. Duplicate tab? " - (jao-eww-buffer-title) (eww-current-url))))) - (urls (regexp-opt urls)) - (test-b - (lambda (b) - (set-buffer b) - (or (and (string-match urls (eww-current-url)) - (or (equal jao-eww-session-duplicate-tabs 'never) - (not (duplicate-p))) - b) - 'not))) - (buffers (mapcar test-b (jao-eww-session--list-buffers)))) - (delete 'not buffers)))) - -(defun jao-eww-session-close-buffers (buffers) - (save-current-buffer - (mapc 'kill-buffer buffers))) +(defun jao-eww-session--find-dups (urls) + (seq-filter + (lambda (b) + (with-current-buffer b + (when-let (url (jao-eww--current-url)) + (when (member url urls) + (or (eq jao-eww-session-duplicate-tabs 'never) + (not (y-or-n-p (format "'%s' (%s) is already open. Duplicate? " + (jao-eww-buffer-title) url)))))))) + (jao-eww-session--list-buffers))) (defun jao-eww-session-load-aux () (let ((new-session (jao-eww-session-from-file @@ -295,9 +270,11 @@ the session is already displayed in a eww tab, jao-eww-session can: (let ((sexp (read (current-buffer)))) (and (equal 'jao-eww-session (car sexp)) sexp)))))) +(defun jao-eww-session-not-empty () (> (length (jao-eww-session-urls)) 0)) + (defun jao-eww-session--to--file (filename &optional is-auto) (require 'pp) - (when (> (length (jao-eww-session-urls)) 0) + (when (jao-eww-session-not-empty) (let ((inhibit-message is-auto)) (with-temp-buffer (insert ";;;; File generated by jao-eww-session. DO NOT EDIT!\n") @@ -337,8 +314,6 @@ the session is already displayed in a eww tab, jao-eww-session can: (advice-add 'eww-back-url :after #'jao-eww-session--save-backup) (advice-add 'eww-forward-url :after #'jao-eww-session--save-backup) -;;; Interactive functions: - ;;;###autoload (defun jao-eww-buffer-title () (plist-get eww-data :title)) @@ -346,7 +321,8 @@ the session is already displayed in a eww tab, jao-eww-session can: (defun jao-eww-session-save () "Save the current eww session." (interactive) - (when (or jao-eww-session-save-always (y-or-n-p "Save current eww session? ")) + (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)) t) @@ -360,9 +336,9 @@ the session is already displayed in a eww tab, jao-eww-session can: (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-duplicated urls)))) + (jao-eww-session--find-dups urls)))) (dolist (url urls) (eww url 4)) - (when buffers (jao-eww-session-close-buffers buffers)) + (seq-each #'kill-buffer buffers) (unless (zerop offset) (switch-to-buffer (nth offset (jao-eww-session--list-buffers)))))))) -- cgit v1.2.3