;;; savebuflist.el --- functions to save/restore a "session" ;;; Copyright (c) 2002, Brian St. Pierre ;;; Permission to use, copy, modify, and distribute this software and its ;;; documentation for any purpose, without fee, and without a written agreement ;;; is hereby granted, provided that the above copyright notice and this ;;; paragraph and the following two paragraphs appear in all copies. ;;; ;;; IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, ;;; INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST ;;; PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, ;;; EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;;; ;;; THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ;;; PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" ;;; BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, ;;; SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. ;;; Author: Brian St. Pierre ;;; Maintainer: Brian St. Pierre ;;; Keywords: session, buffer list ;;; Created: 2002-04-22 ;;; Modified: 2002-10-25 (v 0.2) ;;; ;;; 2003-07-09 v 0.3 ;;; - Leaning more towards "session management". ;;; - Added savebuflist-close. ;;; - Created savebuflist prefix keymap (C-c s ) (defun savebuflist-save (save-name) "Save a list of buffers currently visiting files to filename." (interactive "FSave buflist to: ") (let ((oldbuf (current-buffer)) (n 0)) (save-current-buffer (set-buffer (get-buffer-create "*savebuflist*")) (erase-buffer) (while (< n (length (buffer-list))) (let ((buf-filename (buffer-file-name (nth n (buffer-list))))) (if buf-filename (progn (beginning-of-line) (insert buf-filename) (newline)) ) ; end if ) ; end let file (setq n (+ n 1)) ) ; end while (write-file save-name) (kill-buffer (current-buffer)) ) ; end save-current-buffer ) ; end let oldbuf/n ) ; end defun (defun savebuflist-restore (filename) "Close current session, load buffers in previously saved session." (interactive "fLoad buflist from: ") (savebuflist-close) (let ((oldbuf (current-buffer)) (n 0)) (save-current-buffer (set-buffer (find-file-noselect filename)) (while (not (eobp)) (let ((buf-filename (substring (thing-at-point 'line) 0 -1))) (cond ((eq buf-filename filename) nil) ((not (file-readable-p buf-filename)) (message (concat "Can't read " buf-filename "."))) ((string= (file-name-nondirectory buf-filename) "TAGS") (visit-tags-table buf-filename)) (t (find-file-noselect buf-filename)) ) ; end cond ) ; end let buf-filename (forward-line) ) ; end while (kill-buffer (current-buffer)) ) ; end save-current-buffer ) ; end let oldbuf/n (buffer-menu) ) ; end defun (defun savebuflist-close () "Close all buffers that are visiting a file." (interactive) (let ( (buflist (buffer-list)) (n 0)) (while (< n (length buflist)) (let* ((buf (nth n buflist)) (buf-filename (buffer-file-name buf))) (if (and buf-filename (not (buffer-modified-p buf))) (kill-buffer buf)) ) ; end let buf/buf-filename (setq n (+ n 1)) ) ; end while ) ; end let buflist/n ) ; end defun ;;; Session Management ;;; ;;; These use savebuflist.el. ;;; FIXME: allow customization. (defvar savebuflist-prefix-map (make-sparse-keymap)) (defvar savebuflist-prefix-key "\C-cs") (define-prefix-command 'savebuflist-prefix-map) (global-set-key savebuflist-prefix-key savebuflist-prefix-map) (define-key savebuflist-prefix-map "r" 'savebuflist-restore) (define-key savebuflist-prefix-map "s" 'savebuflist-save) (define-key savebuflist-prefix-map "c" 'savebuflist-close) (provide 'savebuflist)