[Date Prev][Date Next][Thread Prev][][Date Index][Thread Index]

Persistent emacs-w3m sessions




hello,

i've put together a little elisp that lets you save and restore w3m
sessions (see code and comments below). perhaps something like this is
already available somewhere, but i wasn't able to find it.

any comments very welcome!!! please cc: me in your followups (i'm not
(yet) subscribed to the list, sorry).

cheers,
jao

-----8<-------- w3m-session.el ---------------------->8-----------------

;;; w3m-session.el --- Persistent emacs-w3m sessions

;; Copyright (C) 2003  Jose A Ortega Ruiz

;; Author: Jose A Ortega Ruiz <jao@member.fsf.org>
;; Version: 0.1
;; Keywords: hypermedia, w3m, 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 2, 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.

;;; Commentary:

;; INTRODUCTION:
;;
;; w3m-session provides persistent emacs-w3m browsing sessions. When
;; quitting w3m (or, if you request it, at any other time while using
;; it) you can save the current w3m session (that is, the set of open
;; tabs and the URLs they're visiting). Upon restarting emacs-w3m
;; (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 'w3m-session)
;;
;; After restarting Emacs (or evaluating the form above), each time
;; you start emacs-w3m with 'w3m' 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:
;;
;;   w3m-session-load  --  load the last stored session
;;   w3m-session-save  --  save the current session
;;
;; These functions can be invoked at any time while running emacs-w3m.
;; Optionally, you can bind them to key shortcuts with the proper
;; variations of the following elisp magic in your .emacs:
;;
;;   (define-key w3m-mode-map "S" 'w3m-session-save)
;;   (define-key w3m-mode-map "L" 'w3m-session-load)
;;
;; CUSTOMIZATION:
;;
;; A new customization group, w3m-session, is available. There you can
;; customize the following variables:
;;
;;   w3m-session-load-always  --  if t, w3m-session-load will *not* ask
;;                                for confirmation
;;   w3m-session-save-always  --  if t, w3m-session-load will *not* ask
;;                                for confirmation
;;   w3m-session-file         --  the file where w3m session info is stored
;;
;; HISTORY:
;;
;;   - Version 0.1 (Wed Aug 20) -- Initial release.
;;

;;; Code:

;;; Dependencies:

(require 'w3m)
(require 'advice)


;;; Custom variables:

(defgroup w3m-session nil
  "w3m - session saving in w3m."
  :group 'w3m
  :prefix "w3m-session-")

(defcustom w3m-session-file "~/.w3m-session"
  "File to save the w3m session data."
  :group 'w3m-session
  :type 'string)

(defcustom w3m-session-save-always nil
  "If on, always save w3m session without asking."
  :group 'w3m-session
  :type 'boolean)

(defcustom w3m-session-load-always nil
  "If on, always load w3m session without asking."
  :group 'w3m-session
  :type 'boolean)

;;; Interactive functions:

(defun w3m-session-save ()
  "Save the current w3m session."
  (interactive)
  (when (and (w3m-alive-p)
             (or w3m-session-save-always
                 (y-or-n-p "Save current w3m session? ")))
    (w3m-session-current-to-file)))

(defun w3m-session-load ()
  "Load last stored session into w3m."
  (interactive)
  (w3m-goto-url-new-session (w3m-session-load-aux) t))

;;; Internals:

;;;; advice w3m to use session management

(defadvice w3m (before jao-load-session activate)
  "Optionally load last w3m session on startup."
  (interactive
   (list (or (w3m-session-load-aux) w3m-home-page) t t)))

(defadvice w3m-quit (before jao-save-session activate)
  "Save session before quitting."
  (interactive)
  (w3m-session-save))

;;;; save session on exit using the desktop package
(eval-after-load "desktop"
  '(add-hook 'desktop-save-hook 'w3m-session-save))

;;;; auxiliar functions
(defun w3m-session-current ()
  (when (w3m-alive-p)
    (save-current-buffer
      (concat "group:"
              (mapconcat (lambda  (b) (set-buffer b) w3m-current-url)
                         (w3m-list-buffers) "&")))))

(defun w3m-session-from-file ()
  (save-current-buffer
    (let* ((buffer (find-file-noselect (expand-file-name w3m-session-file) t))
           (sexp (and buffer
                      (set-buffer buffer)
                      (goto-char (point-min))
                      (read buffer))))
      (when buffer (kill-buffer buffer))
      (and (equal 'w3m-session (car sexp)) (cadr sexp)))))

(defun w3m-session-load-aux ()
  (when (and (file-readable-p (expand-file-name w3m-session-file))
             (or w3m-session-load-always
                 (y-or-n-p "Load last w3m session? ")))
    (w3m-session-from-file)))

(defun w3m-session-current-to-file ()
  (save-current-buffer
    (set-buffer (find-file-noselect (expand-file-name w3m-session-file) t))
    (erase-buffer)
    (insert ";;;; File generated by w3m-session. DO NOT EDIT!\n")
    (insert "(w3m-session \"" (w3m-session-current) "\")")
    (insert "\n" ";;;; End of " (buffer-name) "\n")
    (save-buffer)
    (kill-buffer nil)))

(provide 'w3m-session)
;;; w3m-session.el ends here