(lisp-font-lock-keywords-2): Added `defpackage'.
[bpt/emacs.git] / lisp / mail / mspools.el
index ce5955a..538f8aa 100644 (file)
@@ -1,20 +1,21 @@
-;;; MSPOOLS.EL --- Show mail spools waiting to be read
+;;; mspools.el --- show mail spools waiting to be read.
 
-;; Copyright (C) 1997 Stephen Eglen
+;; Copyright (C) 1997 Free Software Foundation, Inc.
 
-;; Author: Stephen Eglen <stephene@cogs.susx.ac.uk>
-;; Maintainer: Stephen Eglen <stephene@cogs.susx.ac.uk>
+;; Author: Stephen Eglen <stephen@cns.ed.ac.uk>
+;; Maintainer: Stephen Eglen <stephen@cns.ed.ac.uk>
 ;; Created: 22 Jan 1997
-;; Version: 1.0
-;; Keywords:
+;; Keywords: mail
+;; location: http://www.cns.ed.ac.uk/people/stephen/emacs/
 
-;; This program is free software; you can redistribute it and/or modify
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs 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 program is distributed in the hope that it will be useful,
+;; GNU Emacs 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.
@@ -34,7 +35,7 @@
 ;; arrives whilst you are reading the folder in emacs, hence the use
 ;; of a spool file.)  For example, the following procmail recipe puts
 ;; any mail with `emacs' in the subject line into the spool file
-;; `apple.spool', ready to go into the folder `emacs'.
+;; `emacs.spool', ready to go into the folder `emacs'.
 ;:0:
 ;* ^Subject.*emacs
 ;emacs.spool
 ;; This file should work with both VM and RMAIL.  See the variable
 ;; `mspools-using-vm' for details.
 
-
-;;; Installation
-
-;; Basic
-;(autoload 'mspools-show "mspools" "Show outstanding mail spools." t)
-; Point to directory where spool files and folders are:
-; (setq mspools-folder-directory "~/MAIL/")
-
-;; Extras
-; possibly bind it to a key:
-;(global-set-key  '[S-f1] 'mspools-show)
-;(setq mspools-update t)
-
-;; Interface with the mail filter
-; We assume that the mail filter drops new mail into the spool
-; `folder.spool'.  If your spool files are something like folder.xyz
-; for inbox `folder', then do
-; (setq spool-suffix "xyz")
-; If you use other conventions for your spool files, this code will
-; need rewriting.
-
-;;; Warning for VM users
-;; Dont use if you are not sure what you are doing!  The value of
+;;; Basic installation.
+;; (autoload 'mspools-show "mspools" "Show outstanding mail spools." t)
+;; (setq mspools-folder-directory "~/MAIL/")
+;;
+;; If you use VM, mspools-folder-directory will default to vm-folder-directory
+;; unless you have already given it a value.
+
+;; Extras.
+;;
+;; (global-set-key '[S-f1] 'mspools-show) ;Bind mspools-show to Shift F1.
+;; (setq mspools-update t)                ;Automatically update buffer.
+
+;; Interface with the mail filter.
+;; We assume that the mail filter drops new mail into the spool
+;; `folder.spool'.  If your spool files are something like folder.xyz
+;; for inbox `folder', then do:
+;; (setq mspools-suffix "xyz")
+;; If you use other conventions for your spool files, this code will
+;; need rewriting.
+
+;; Warning for VM users
+;; Don't use if you are not sure what you are doing.  The value of
 ;; vm-spool-files is altered, so you may not be able to read incoming
 ;; mail with VM if this is incorrectly set.
 
 ;; Useful settings for VM
-;vm-auto-get-new-mail should be t (default t)
+;; vm-auto-get-new-mail should be t (the default).
 
-;;; Acknowledgements
-;; The code for setting up vm-spool-files came from 
-;;http://www-users.informatik.rwth-aachen.de/~berg/archive/procmail/0047.html
-;;  Thanks to jond@mitre.org (Jonathan Doughty)
+;; Acknowledgements
+;; Thanks to jond@mitre.org (Jonathan Doughty) for help with code for
+;; setting up vm-spool-files.
 
 ;;; TODO
 
 ;; What if users have mail spools in more than one directory?  Extend
-;; mspools-folder-directory to be a list of files?
+;; mspools-folder-directory to be a list of directories?  Currently,
+;; if mail spools are in other directories, the way to read them is to
+;; put a symbolic link to the spool into the mspools-folder-directory.
 
 ;; I was going to add mouse support so that you could click on a line
 ;; to visit the buffer.  Tell me if you want it, and I can put the
-;; code in (I dont use the mouse much, so I havent bothered with it so
-;; far).
-
+;; code in (I don't use the mouse much, so I haven't bothered with it
+;; so far).
 
 ;; Rather than showing size in bytes, could we see the number of msgs
 ;; waiting?  (Could be more time demanding / system dependent).
-;; Perl script counts the number of /^From / occurences.
-;; ?
-;; Include date
-;; (substring  (current-time-string (nth 4 (file-attributes "~/INBOX")))  4 19)
 ;; Maybe just call a perl script to do all the hard work, and
 ;; visualise the results in the buffer.
 
 ;; Shrink wrap the buffer to remove excess white-space?
 
+;;; Code:
 
 ;;; User Variables
 
+(defgroup mspools nil
+  "Show mail spools waiting to be read."
+  :group 'mail
+  :link '(emacs-commentary-link :tag "Commentary" "mspools.el")
+)
 
-(defvar mspools-update nil
-  "*Non-nil means update *spools* buffer after visiting any folder.")
-
-(defvar mspools-suffix "spool"
-  "*Extension used for spool files (not including full stop).")
+(defcustom mspools-update nil
+  "*Non-nil means update *spools* buffer after visiting any folder."
+  :type 'boolean
+  :group 'mspools)
 
-;;; Internal Variables
+(defcustom mspools-suffix "spool"
+  "*Extension used for spool files (not including full stop)."
+  :type 'string
+  :group 'mspools)
 
-(defvar mspools-vm-system-mail (getenv "MAIL")
-  "Main mailbox used.  Only used by VM.")
+(defcustom mspools-using-vm  (fboundp 'vm)
+  "*Non-nil if VM is used as mail reader, otherwise RMAIL is used."
+  :type 'boolean
+  :group 'mspools)
 
-(defvar mspools-vm-system-mail-crash 
-  (concat mspools-vm-system-mail ".crash")
-  "Crash box for main mailbox.  See also `mspools-vm-system-mail'.  
-Only used by VM." )
+(defcustom mspools-folder-directory
+  (if (boundp 'vm-folder-directory)
+      vm-folder-directory
+    "~/MAIL/")
+  "*Directory where mail folders are kept.  Ensure it has a trailing /.
+Defaults to `vm-folder-directory' if bound else to ~/MAIL/."
+  :type 'directory
+  :group 'mspools)
+
+(defcustom mspools-vm-system-mail (getenv "MAIL")
+  "*Spool file for main mailbox.  Only used by VM.
+This needs to be set to your primary mail spool - mspools will not run
+without it.  By default this will be set to the environment variable
+$MAIL.  Otherwise set it to something like /usr/spool/mail/login-name."
+  :type 'file
+  :group 'mspools)
 
+;;; Internal Variables
 
 (defvar mspools-files nil
   "List of entries (SPOOL . SIZE) giving spool name and file size.")
@@ -145,66 +164,62 @@ Only used by VM." )
 (defvar mspools-mode-map nil
   "Keymap for the *spools* buffer.")
 
-(defvar mspools-folder-directory 
-  (if (boundp 'vm-folder-directory)
-      vm-folder-directory
-    nil)
-  "Directory where mail folders are kept.  Defaults to
-`vm-folder-directory' if bound else nil.  Make sure it has a trailing /
-at the end. ")
-
-
-(defvar mspools-using-vm 
-  (fboundp 'vm)
-  "*Non-nil if VM is used as mail reader, otherwise RMAIL is used.")
-
-
 ;;; Code
 
 ;;; VM Specific code
 (if mspools-using-vm
-    (require 'vm-vars))
+    ;; set up vm if not already loaded.
+    (progn
+      (require 'vm-vars)
+      (if (and (not vm-init-file-loaded) (file-readable-p vm-init-file))
+         (load-file vm-init-file))
+      (if (not mspools-folder-directory)
+         (setq mspools-folder-directory vm-folder-directory))
+      ))
 
 (defun mspools-set-vm-spool-files ()
   "Set value of `vm-spool-files'.  Only needed for VM."
-  (setq                
-   vm-spool-files 
+  (if (null mspools-vm-system-mail)
+      (error "Need to set mspools-vm-system-mail to the spool for primary inbox"))
+  (if (null mspools-folder-directory)
+      (error "Set `mspools-folder-directory' to where the spool files are"))
+  (setq
+   vm-spool-files
    (append
     (list
      ;; Main mailbox
      (list vm-primary-inbox
-          mspools-vm-system-mail; your mailbox
-          mspools-vm-system-mail-crash ; crash for mailbox
+          mspools-vm-system-mail       ; your mailbox
+          vm-crash-box                 ;crash for mailbox
           ))
     
     ;; Mailing list inboxes
+    ;; must have VM already loaded to get vm-folder-directory.
     (mapcar '(lambda (s)
               "make the appropriate entry for vm-spool-files"
               (list
-               (concat vm-folder-directory s)
-               (concat vm-folder-directory s "." mspools-suffix)
-               (concat vm-folder-directory s ".crash")))
+               (concat mspools-folder-directory s)
+               (concat mspools-folder-directory s "." mspools-suffix)
+               (concat mspools-folder-directory s ".crash")))
            ;; So I create a vm-spool-files entry for each of those mail drops
            (mapcar 'file-name-sans-extension 
-                   (directory-files vm-folder-directory nil 
+                   (directory-files mspools-folder-directory nil 
                                     (format "^[^.]+\\.%s" mspools-suffix)))
            ))
    ))
 
-
-
 ;;; MSPOOLS-SHOW -- the main function
-(defun mspools-show ( &optional noshow) 
+(defun mspools-show ( &optional noshow)
   "Show the list of non-empty spool files in the *spools* buffer.
 Buffer is not displayed if SHOW is non-nil."
   (interactive)
   (if (get-buffer mspools-buffer)
       ;; buffer exists
       (progn
-       (set-buffer mspools-buffer)     
-       (setq buffer-read-only nil)      
+       (set-buffer mspools-buffer)
+       (setq buffer-read-only nil)
        (delete-region (point-min) (point-max)))
-    ;; else buff. doesnt exist so create it
+    ;; else buffer doesn't exist so create it
     (get-buffer-create mspools-buffer))
   
   ;; generate the list of spool files
@@ -218,55 +233,50 @@ Buffer is not displayed if SHOW is non-nil."
   (mspools-mode)
   )
 
-
-
-
 (defun mspools-visit-spool ()
   "Visit the folder on the current line of the *spools* buffer."
   (interactive)
   (let ( spool-name folder-name)
     (setq spool-name (mspools-get-spool-name))
-    (setq folder-name (mspools-get-folder-from-spool spool-name))
-
-    ;; put in a little "*" to indicate spool file has been read.
-    (if (not mspools-update)
-       (save-excursion
-         (setq buffer-read-only nil)
-         (beginning-of-line)
-         (insert "*")
-         (delete-char 1)
-         (setq buffer-read-only t)
-         ))
-    
-
-    (message "folder %s spool %s" folder-name spool-name)
-    (if (eq (count-lines (point-min) 
-                        (save-excursion
-                          (end-of-line)
-                          (point)))
-           mspools-files-len)
-       (next-line (- 1 mspools-files-len)) ;back to top of list
-      ;; else just on to next line
-      (next-line 1))
-
-    ;; Choose whether to use VM or RMAIL for reading folder.
-    (if mspools-using-vm 
-       (vm-visit-folder (concat mspools-folder-directory folder-name))
-      ;; else using RMAIL 
-      (rmail (concat mspools-folder-directory folder-name))
-      (setq rmail-inbox-list 
-           (list (concat mspools-folder-directory spool-name)))
-      (rmail-get-new-mail))
-    
-    
-    (if mspools-update
-       ;; generate new list of spools.
-       (save-excursion 
-         (mspools-show-again 'noshow)))
-    ))
-
-
+    (if (null spool-name)
+       (message "No spool on current line")
+      
+      (setq folder-name (mspools-get-folder-from-spool spool-name))
+      
+      ;; put in a little "*" to indicate spool file has been read.
+      (if (not mspools-update)
+         (save-excursion
+           (setq buffer-read-only nil)
+           (beginning-of-line)
+           (insert "*")
+           (delete-char 1)
+           (setq buffer-read-only t)
+           ))
 
+      (message "folder %s spool %s" folder-name spool-name)
+      (if (eq (count-lines (point-min)
+                          (save-excursion
+                            (end-of-line)
+                            (point)))
+             mspools-files-len)
+         (next-line (- 1 mspools-files-len)) ;back to top of list
+       ;; else just on to next line
+       (next-line 1))
+      
+      ;; Choose whether to use VM or RMAIL for reading folder.
+      (if mspools-using-vm
+         (vm-visit-folder (concat mspools-folder-directory folder-name))
+       ;; else using RMAIL
+       (rmail (concat mspools-folder-directory folder-name))
+       (setq rmail-inbox-list
+             (list (concat mspools-folder-directory spool-name)))
+       (rmail-get-new-mail))
+      
+      
+      (if mspools-update
+         ;; generate new list of spools.
+         (save-excursion
+           (mspools-show-again 'noshow))))))
 
 (defun mspools-get-folder-from-spool (name)
   "Return folder name corresponding to the spool file NAME."
@@ -285,8 +295,6 @@ Buffer is not displayed if SHOW is non-nil."
 ;(mspools-get-folder-from-spool "happy.spool")
 ;(mspools-get-folder-from-spool "happy.sp")
 
-
-
 (defun mspools-get-spool-name ()
   "Return the name of the spool on the current line."
   (let ((line-num (1- (count-lines (point-min)
@@ -307,10 +315,11 @@ Buffer is not displayed if SHOW is non-nil."
   (define-key mspools-mode-map " " 'mspools-visit-spool)
   (define-key mspools-mode-map "?" 'mspools-help)
   (define-key mspools-mode-map "q" 'mspools-quit)
+  (define-key mspools-mode-map "n" 'next-line)
+  (define-key mspools-mode-map "p" 'previous-line)
   (define-key mspools-mode-map "g" 'revert-buffer))
 
-
-;;; Spools mode functions  
+;;; Spools mode functions
 
 (defun mspools-revert-buffer (ignore noconfirm)
   "Re-run mspools-show to revert the *spools* buffer."
@@ -331,13 +340,12 @@ nil."
   "Quit the *spools* buffer."
   (interactive)
   (kill-buffer mspools-buffer))
-  
 
 (defun mspools-mode ()
   "Major mode for output from mspools-show.
 \\<mspools-mode-map>Move point to one of the items in this buffer, then use
 \\[mspools-visit-spool] to go to the spool that the current line refers to.
-\\[mspools-show-again] to regenerate the list of spools.
+\\[revert-buffer] to regenerate the list of spools.
 \\{mspools-mode-map}"
   (kill-all-local-variables)
   (make-local-variable 'revert-buffer-function)
@@ -347,14 +355,13 @@ nil."
   (setq mode-name "MSpools")
   )
 
-
 (defun mspools-get-spool-files ()
   "Find the list of spool files and display them in *spools* buffer."
   (let (folders head spool len beg end any)
-    (setq folders (directory-files mspools-folder-directory nil 
-                                  (format "^[^.]+\\.%s" mspools-suffix)))
-    
-    
+    (if (null mspools-folder-directory)
+       (error "Set `mspools-folder-directory' to where the spool files are"))
+    (setq folders (directory-files mspools-folder-directory nil
+                                  (format "^[^.]+\\.%s$" mspools-suffix)))
     (setq folders (mapcar 'mspools-size-folder folders))
     (setq folders (delq nil folders))
     (setq mspools-files folders)
@@ -377,18 +384,20 @@ nil."
     (goto-char (point-min))
     ))
 
-
-
 (defun mspools-size-folder (spool)
   "Return (SPOOL . SIZE ) iff SIZE of spool file is non-zero."
   ;; 7th file attribute is the size of the file in bytes.
-  (let ((size (nth 7 
-                  (file-attributes (concat mspools-folder-directory spool)))))
-    ;; todo (if (and (not (null size)) (> size 0))
-    (if (> size 0)
+  (let ((file (concat mspools-folder-directory spool))
+       size)
+    (setq file (or (file-symlink-p file) file))
+    (setq size (nth 7 (file-attributes file)))
+    ;; size could be nil if the sym-link points to a non-existent file
+    ;; so check this first.
+    (if (and size  (> size 0))
        (cons spool  size)
       ;; else SPOOL is empty
       nil)))
 
 (provide 'mspools)
-;;; MSPOOLS.EL ends here
+;;; mspools.el ends here
+