1 ;;; mspools.el --- show mail spools waiting to be read
3 ;; Copyright (C) 1997, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Stephen Eglen <stephen@gnu.org>
7 ;; Maintainer: Stephen Eglen <stephen@gnu.org>
8 ;; Created: 22 Jan 1997
10 ;; location: http://www.anc.ed.ac.uk/~stephen/emacs/
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to the
26 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
31 ;; If you use a mail filter (e.g. procmail, filter) to put mail messages in
32 ;; folders, this file will let you see which folders have mail waiting
33 ;; to be read in them. It assumes that new mail for the file `folder'
34 ;; is written by the filter to a file called `folder.spool'. (If the
35 ;; file writes directly to `folder' you may lose mail if new mail
36 ;; arrives whilst you are reading the folder in emacs, hence the use
37 ;; of a spool file.) For example, the following procmail recipe puts
38 ;; any mail with `emacs' in the subject line into the spool file
39 ;; `emacs.spool', ready to go into the folder `emacs'.
44 ;; It also assumes that all of your spool files and mail folders live
45 ;; in the directory pointed to by `mspools-folder-directory', so you must
46 ;; set this (see Installation).
48 ;; When you run `mspools-show', it creates a *spools* buffer containing
49 ;; all of the spools in the folder directory that are waiting to be
50 ;; read. On each line is the spool name and its size in bytes. Move
51 ;; to the line of the folder that you would like to read, and then
52 ;; press return or space. The mailer (VM or RMAIL) should then read
53 ;; that folder and get the new mail for you. When you return to the
54 ;; *spools* buffer, you will either see "*" to indicate that the spool
55 ;; has been read, or the remaining unread spools, depending on the
56 ;; value of `mspools-update'.
58 ;; This file should work with both VM and RMAIL. See the variable
59 ;; `mspools-using-vm' for details.
61 ;;; Basic installation.
62 ;; (autoload 'mspools-show "mspools" "Show outstanding mail spools." t)
63 ;; (setq mspools-folder-directory "~/MAIL/")
65 ;; If you use VM, mspools-folder-directory will default to vm-folder-directory
66 ;; unless you have already given it a value.
70 ;; (global-set-key '[S-f1] 'mspools-show) ;Bind mspools-show to Shift F1.
71 ;; (setq mspools-update t) ;Automatically update buffer.
73 ;; Interface with the mail filter.
74 ;; We assume that the mail filter drops new mail into the spool
75 ;; `folder.spool'. If your spool files are something like folder.xyz
76 ;; for inbox `folder', then do:
77 ;; (setq mspools-suffix "xyz")
78 ;; If you use other conventions for your spool files, this code will
81 ;; Warning for VM users
82 ;; Don't use if you are not sure what you are doing. The value of
83 ;; vm-spool-files is altered, so you may not be able to read incoming
84 ;; mail with VM if this is incorrectly set.
86 ;; Useful settings for VM
87 ;; vm-auto-get-new-mail should be t (the default).
90 ;; Thanks to jond@mitre.org (Jonathan Doughty) for help with code for
91 ;; setting up vm-spool-files.
95 ;; What if users have mail spools in more than one directory? Extend
96 ;; mspools-folder-directory to be a list of directories? Currently,
97 ;; if mail spools are in other directories, the way to read them is to
98 ;; put a symbolic link to the spool into the mspools-folder-directory.
100 ;; I was going to add mouse support so that you could click on a line
101 ;; to visit the buffer. Tell me if you want it, and I can put the
102 ;; code in (I don't use the mouse much, so I haven't bothered with it
105 ;; Rather than showing size in bytes, could we see the number of msgs
106 ;; waiting? (Could be more time demanding / system dependent).
107 ;; Maybe just call a perl script to do all the hard work, and
108 ;; visualise the results in the buffer.
110 ;; Shrink wrap the buffer to remove excess white-space?
114 (defvar rmail-inbox-list
)
115 (defvar vm-crash-box
)
116 (defvar vm-folder-directory
)
117 (defvar vm-init-file
)
118 (defvar vm-init-file-loaded
)
119 (defvar vm-primary-inbox
)
120 (defvar vm-spool-files
)
124 (defgroup mspools nil
125 "Show mail spools waiting to be read."
127 :link
'(emacs-commentary-link :tag
"Commentary" "mspools.el")
130 (defcustom mspools-update nil
131 "*Non-nil means update *spools* buffer after visiting any folder."
135 (defcustom mspools-suffix
"spool"
136 "*Extension used for spool files (not including full stop)."
140 (defcustom mspools-using-vm
(fboundp 'vm
)
141 "*Non-nil if VM is used as mail reader, otherwise RMAIL is used."
145 (defcustom mspools-folder-directory
146 (if (boundp 'vm-folder-directory
)
149 "*Directory where mail folders are kept. Ensure it has a trailing /.
150 Defaults to `vm-folder-directory' if bound else to ~/MAIL/."
154 (defcustom mspools-vm-system-mail
(or (getenv "MAIL")
155 (concat rmail-spool-directory
157 "*Spool file for main mailbox. Only used by VM.
158 This needs to be set to your primary mail spool - mspools will not run
159 without it. By default this will be set to the environment variable
160 $MAIL. Otherwise it will use `rmail-spool-directory' to guess where
161 your primary spool is. If this fails, set it to something like
162 /usr/spool/mail/login-name."
166 ;;; Internal Variables
168 (defvar mspools-files nil
169 "List of entries (SPOOL . SIZE) giving spool name and file size.")
171 (defvar mspools-files-len nil
172 "Length of `mspools-files' list.")
174 (defvar mspools-buffer
"*spools*"
175 "Name of buffer for displaying spool info.")
177 (defvar mspools-mode-map nil
178 "Keymap for the *spools* buffer.")
184 ;; set up vm if not already loaded.
187 (if (and (not vm-init-file-loaded
) (file-readable-p vm-init-file
))
188 (load-file vm-init-file
))
189 (if (not mspools-folder-directory
)
190 (setq mspools-folder-directory vm-folder-directory
))
193 (defun mspools-set-vm-spool-files ()
194 "Set value of `vm-spool-files'. Only needed for VM."
195 (if (not (file-readable-p mspools-vm-system-mail
))
196 (error "Need to set mspools-vm-system-mail to the spool for primary inbox"))
197 (if (null mspools-folder-directory
)
198 (error "Set `mspools-folder-directory' to where the spool files are"))
204 (list vm-primary-inbox
205 mspools-vm-system-mail
; your mailbox
206 vm-crash-box
;crash for mailbox
209 ;; Mailing list inboxes
210 ;; must have VM already loaded to get vm-folder-directory.
212 "make the appropriate entry for vm-spool-files"
214 (concat mspools-folder-directory s
)
215 (concat mspools-folder-directory s
"." mspools-suffix
)
216 (concat mspools-folder-directory s
".crash")))
217 ;; So I create a vm-spool-files entry for each of those mail drops
218 (mapcar 'file-name-sans-extension
219 (directory-files mspools-folder-directory nil
220 (format "^[^.]+\\.%s" mspools-suffix
)))
224 ;;; MSPOOLS-SHOW -- the main function
225 (defun mspools-show ( &optional noshow
)
226 "Show the list of non-empty spool files in the *spools* buffer.
227 Buffer is not displayed if SHOW is non-nil."
229 (if (get-buffer mspools-buffer
)
232 (set-buffer mspools-buffer
)
233 (setq buffer-read-only nil
)
234 (delete-region (point-min) (point-max)))
235 ;; else buffer doesn't exist so create it
236 (get-buffer-create mspools-buffer
))
238 ;; generate the list of spool files
240 (mspools-set-vm-spool-files))
242 (mspools-get-spool-files)
243 (if (not noshow
) (pop-to-buffer mspools-buffer
))
245 (setq buffer-read-only t
)
249 (declare-function rmail-get-new-mail
"rmail" (&optional file-name
))
252 (declare-function vm-visit-folder
"ext:vm-startup" (folder &optional read-only
))
254 (defun mspools-visit-spool ()
255 "Visit the folder on the current line of the *spools* buffer."
257 (let ( spool-name folder-name
)
258 (setq spool-name
(mspools-get-spool-name))
259 (if (null spool-name
)
260 (message "No spool on current line")
262 (setq folder-name
(mspools-get-folder-from-spool spool-name
))
264 ;; put in a little "*" to indicate spool file has been read.
265 (if (not mspools-update
)
267 (setq buffer-read-only nil
)
271 (setq buffer-read-only t
)
274 (message "folder %s spool %s" folder-name spool-name
)
275 (if (eq (count-lines (point-min)
280 (forward-line (- 1 mspools-files-len
)) ;back to top of list
281 ;; else just on to next line
284 ;; Choose whether to use VM or RMAIL for reading folder.
286 (vm-visit-folder (concat mspools-folder-directory folder-name
))
288 (rmail (concat mspools-folder-directory folder-name
))
289 (setq rmail-inbox-list
290 (list (concat mspools-folder-directory spool-name
)))
291 (rmail-get-new-mail))
295 ;; generate new list of spools.
297 (mspools-show-again 'noshow
))))))
299 (defun mspools-get-folder-from-spool (name)
300 "Return folder name corresponding to the spool file NAME."
301 ;; Simply strip of the extension.
302 (file-name-sans-extension name
))
304 ;; Alternative version if you have more complicated mapping of spool name
306 ;(defun get-folder-from-spool-safe (name)
307 ; "Return the folder name corresponding to the spool file NAME."
308 ; (if (string-match "^\\(.*\\)\.spool$" name)
309 ; (substring name (match-beginning 1) (match-end 1))
310 ; (error "Could not extract folder name from spool name %s" name)))
313 ;(mspools-get-folder-from-spool "happy.spool")
314 ;(mspools-get-folder-from-spool "happy.sp")
316 (defun mspools-get-spool-name ()
317 "Return the name of the spool on the current line."
318 (let ((line-num (1- (count-lines (point-min)
323 (car (nth line-num mspools-files
))))
329 (setq mspools-mode-map
(make-sparse-keymap))
331 (define-key mspools-mode-map
"\C-c\C-c" 'mspools-visit-spool
)
332 (define-key mspools-mode-map
"\C-m" 'mspools-visit-spool
)
333 (define-key mspools-mode-map
" " 'mspools-visit-spool
)
334 (define-key mspools-mode-map
"?" 'mspools-help
)
335 (define-key mspools-mode-map
"q" 'mspools-quit
)
336 (define-key mspools-mode-map
"n" 'next-line
)
337 (define-key mspools-mode-map
"p" 'previous-line
)
338 (define-key mspools-mode-map
"g" 'revert-buffer
))
340 ;;; Spools mode functions
342 (defun mspools-revert-buffer (ignore noconfirm
)
343 "Re-run mspools-show to revert the *spools* buffer."
344 (mspools-show 'noshow
))
346 (defun mspools-show-again (&optional noshow
)
347 "Update the *spools* buffer. This is useful if mspools-update is
350 (mspools-show noshow
))
352 (defun mspools-help ()
353 "Show help for `mspools-mode'."
355 (describe-function 'mspools-mode
))
357 (defun mspools-quit ()
358 "Quit the *spools* buffer."
360 (kill-buffer mspools-buffer
))
362 (defun mspools-mode ()
363 "Major mode for output from mspools-show.
364 \\<mspools-mode-map>Move point to one of the items in this buffer, then use
365 \\[mspools-visit-spool] to go to the spool that the current line refers to.
366 \\[revert-buffer] to regenerate the list of spools.
367 \\{mspools-mode-map}"
368 (kill-all-local-variables)
369 (make-local-variable 'revert-buffer-function
)
370 (setq revert-buffer-function
'mspools-revert-buffer
)
371 (use-local-map mspools-mode-map
)
372 (setq major-mode
'mspools-mode
)
373 (setq mode-name
"MSpools")
374 (run-mode-hooks 'mspools-mode-hook
))
376 (defun mspools-get-spool-files ()
377 "Find the list of spool files and display them in *spools* buffer."
378 (let (folders head spool len beg end any
)
379 (if (null mspools-folder-directory
)
380 (error "Set `mspools-folder-directory' to where the spool files are"))
381 (setq folders
(directory-files mspools-folder-directory nil
382 (format "^[^.]+\\.%s$" mspools-suffix
)))
383 (setq folders
(mapcar 'mspools-size-folder folders
))
384 (setq folders
(delq nil folders
))
385 (setq mspools-files folders
)
386 (setq mspools-files-len
(length mspools-files
))
387 (set-buffer mspools-buffer
)
390 (setq head
(car folders
))
391 (setq spool
(car head
))
392 (setq len
(cdr head
))
393 (setq folders
(cdr folders
))
395 (insert (format " %10d %s" len spool
))
398 ;;(put-text-property beg end 'mouse-face 'highlight)
401 (delete-char -
1)) ;delete last RET
402 (goto-char (point-min))
405 (defun mspools-size-folder (spool)
406 "Return (SPOOL . SIZE ), if SIZE of spool file is non-zero."
407 ;; 7th file attribute is the size of the file in bytes.
408 (let ((file (concat mspools-folder-directory spool
))
410 (setq file
(or (file-symlink-p file
) file
))
411 (setq size
(nth 7 (file-attributes file
)))
412 ;; size could be nil if the sym-link points to a non-existent file
413 ;; so check this first.
414 (if (and size
(> size
0))
416 ;; else SPOOL is empty
421 ;; arch-tag: 8990b3ee-68c8-4892-98f1-51a735c8bac6
422 ;;; mspools.el ends here