Added support for multiple maildirs

This commit is contained in:
Philip K 2018-12-27 17:40:54 +01:00
parent bd47cec14f
commit 3c6fbda443
2 changed files with 81 additions and 76 deletions

View file

@ -1,18 +1,26 @@
** Usage
Put:
#+BEGIN_SRC lisp
(load-module "maildir")
#+END_SRC
into your =~/.stumpwmrc=
Then you can use =%D= in your mode line format. You can customize the
modeline format =*maildir-modeline-fmt*=. The relevant format options
are:
| Option | Description |
|--------+-------------------------------------------|
| =%l= | maildir label |
| =%n= | number of "new" messages (ie. unread) |
| =%c= | number of "current" messages (ie. seen) |
| =%t= | number of "temporary" messages (ie. misc) |
Put:
#+BEGIN_SRC lisp
(load-module "maildir")
#+END_SRC
into your =~/.stumpwmrc=
Then you can use "%M" in your mode line format.
You can customize the modeline format (*maildir-modeline-fmt*). See the
documentation for *maildir-modeline-fmt* for more information.
** Notes
The %M mode line formatter is also used by the 'mem' module.
** Tasks
*** TODO Add per mailbox formatter
To add a Maildir, add it to the =*maildir-alist*= alist. The car or
each item is it's label and the cdr it's pathname. Here's an example
of how to add a custom directory.
#+BEGIN_SRC lisp
(push (cons "Job" (realname "~/job-mail")) maildir:*maildir-alist*)
#+END_SRC
By default, only =~/Mail= is indexed. All directories that cannot be
found, are ignored.

View file

@ -13,12 +13,44 @@
;;; CODE:
(add-screen-mode-line-formatter #\M 'maildir-modeline)
(export '(*maildir-alist*
*maildir-modeline-fmt*
maildir-set-update-time))
(defvar *maildir-timer* nil)
(defvar *maildir-update-time* 900
"Time between two updates of the maildir informations (in seconds).")
(defvar *maildir-info* '()
"List of plists for number of {new,cur,tmp} mail for each mailbox")
(defvar *maildir-alist*
(list (cons "Mail" (merge-pathnames (make-pathname :directory '(:relative "Mail"))
(user-homedir-pathname))))
"Alist of pathnames to the mail directories with names. Defaults to just ~/Mail.")
(defvar *maildir-modeline-fmt* "[%l: %n] "
"The Default Value For Displaying Maildir information on the modeline.
@table @asis
@item %%
A literal '%'
@item %l
Label of the maildir
@item %n
New mails number
@item %c
Current mails number
@item %t
Temporary mails number
@end table")
(defun maildir-set-update-time (time-in-seconds)
"Set the maildir informations update interval."
(when *maildir-timer*
@ -28,15 +60,10 @@
(run-with-timer *maildir-update-time* *maildir-update-time*
'update-maildir-infos)))
(defvar *maildir-path*
(merge-pathnames (make-pathname :directory '(:relative "Mail"))
(user-homedir-pathname))
"Pathname to the mail directory. Defaults to ~/Mail.")
(defun maildir-mailboxes (maildir)
"Returns a list of all mailboxes in *maildir-path*."
(directory (merge-pathnames (make-pathname :directory '(:relative :wild))
maildir)))
(directory (merge-pathnames (make-pathname :directory '(:relative :wild))
maildir)))
(defun maildir-mailbox-dir (mailbox dir-name)
"Returns the specified sub-directory pathname for the provided mailbox."
@ -44,26 +71,17 @@
:name :wild :type :wild)
mailbox))
(defvar *maildir-new* '()
"Number of new mails for each mailbox.")
(defvar *maildir-cur* '()
"Number of mails for each mailbox.")
(defvar *maildir-tmp* '()
"Number of tmp mails for each mailbox.")
(defun update-maildir-infos ()
"Update mail counts for *maildir-path*."
(loop for m in (maildir-mailboxes *maildir-path*)
collect (length (directory (maildir-mailbox-dir m "new")))
into nb-new
collect (length (directory (maildir-mailbox-dir m "cur")))
into nb-cur
collect (length (directory (maildir-mailbox-dir m "tmp")))
into nb-tmp
finally (progn (setf *maildir-new* nb-new)
(setf *maildir-cur* nb-cur)
(setf *maildir-tmp* nb-tmp))))
"Update mail counts for *maildir-alist*."
(setf *maildir-info* '())
(loop for (label . dir) in *maildir-alist*
when (probe-file dir)
do (loop for m in (maildir-mailboxes dir)
sum (length (directory (maildir-mailbox-dir m "new"))) into nb-new
sum (length (directory (maildir-mailbox-dir m "cur"))) into nb-cur
sum (length (directory (maildir-mailbox-dir m "tmp"))) into nb-tmp
finally (push (list label :new nb-new :cur nb-cur :tmp nb-tmp)
*maildir-info*))))
;; modeline formatter
(defun maildir-modeline (ml)
@ -76,38 +94,17 @@
(setf *maildir-timer*
(run-with-timer *maildir-update-time* *maildir-update-time*
'update-maildir-infos)))
(format-expand *maildir-formatters-alist*
*maildir-modeline-fmt*))
(loop for (label . info) in *maildir-info*
collect (stumpwm:format-expand `((#\l ,(constantly label))
(#\n ,(lambda () (format nil "^[~A~D^]"
(if (plusp (getf info :new)) "^B" "")
(getf info :new))))
(#\c ,(lambda () (format nil "~D" (getf info :cur))))
(#\t ,(lambda () (format nil "~D" (getf info :tmp)))))
*maildir-modeline-fmt*)
into fmts
finally (return (format nil "~{~A~}" fmts))))
(defun maildir-get-new ()
(let ((total-new (reduce #'+ *maildir-new*)))
(format nil "^[~A~D^]" (if (plusp total-new) "^B" "") total-new)))
(defun maildir-get-cur ()
(let ((total-cur (reduce #'+ *maildir-cur*)))
(format nil "~D" total-cur)))
(defun maildir-get-tmp ()
(let ((total-tmp (reduce #'+ *maildir-tmp*)))
(format nil "~D" total-tmp)))
(defvar *maildir-formatters-alist*
'((#\n maildir-get-new)
(#\c maildir-get-cur)
(#\t maildir-get-tmp)))
(defvar *maildir-modeline-fmt* "%n %c"
"The default value for displaying maildir information on the modeline.
@table @asis
@item %%
A literal '%'
@item %n
New mails number
@item %c
Current mails number
@item %t
Temporary mails number
@end table
")
(stumpwm:add-screen-mode-line-formatter #\D #'maildir-modeline)