;; jao-maildir.el -- Utilities for reading maildirs -*- lexical-binding: t; -*- ;; Copyright (c) 2019, 2020 jao ;; Author: jao ;; Start date: Sun Dec 01, 2019 15:48 ;; Keywords: mail ;; 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 3, 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. ;;; Comentary: ;; Inspecting the contents of maildirs and reporting it. ;;; Code: (defvar jao-maildirs nil) (defvar jao-maildir-counts nil) (defvar jao-maildir-debug-p nil) (defvar jao-maildir-echo-p t) (defun jao-maildir--maildir-new (mbox) (expand-file-name "new" mbox)) (defun jao-maildir--maildir-new-count (mbox) (- (length (directory-files (jao-maildir--maildir-new mbox))) 2)) ;;;###autoload (defun jao-maildir-counts () (setq jao-maildir-counts (mapcar (lambda (mbox) (cons mbox (jao-maildir--maildir-new-count mbox))) jao-maildirs))) (defvar jao-maildir-tracked-maildirs) (defvar jao-maildir-mode-line-string "") (defun jao-maildir--update-mode-line-string () (let ((total 0) (counts)) (dolist (c jao-maildir-counts) (when (> (cdr c) 0) (let* ((m (car c)) (n (cdr c)) (a (cdr (assoc m jao-maildir-tracked-maildirs)))) (when (null a) (setq total (+ n total))) (when a (push (format "%s%s" a n) counts))))) (let* ((total (if (> total 0) (format " %d " total) " ")) (s (propertize (mapconcat 'identity counts " ") 'face 'font-lock-function-name-face)) (s (if (string-empty-p s) s (concat s " ")))) (setq jao-maildir-mode-line-string (format "%s%s" total s)) (when (and jao-maildir-echo-p (not (string-empty-p s))) (message "Mail%s" s)) (force-mode-line-update t)))) (defvar jao-maildir--watches nil) (defun jao-maildir-cancel-watchers () (dolist (w jao-maildir--watches) (file-notify-rm-watch w)) (setq jao-maildir--watches nil)) (defun jao-maildir--log-watch (mbox e) (when jao-maildir-debug-p (message "[%s] watch: %s: %s" (current-time-string) mbox e))) (defun jao-maildir--watcher (mbox cb) (lambda (e) (jao-maildir--log-watch e mbox) (when (memq (cadr e) '(created deleted)) (setcdr (assoc mbox jao-maildir-counts) (jao-maildir--maildir-new-count mbox)) ;; (jao-maildir-counts) (jao-maildir--update-mode-line-string) (when cb (funcall cb mbox))))) (defun jao-maildir--setup-watches (cb) (jao-maildir-cancel-watchers) (setq jao-maildir--watches (mapcar (lambda (mbox) (file-notify-add-watch (jao-maildir--maildir-new mbox) '(change) (jao-maildir--watcher mbox cb))) jao-maildirs))) ;;;###autoload (defun jao-maildir-setup (maildirs &optional cb) (setq jao-maildirs maildirs) (setq jao-maildir-counts (jao-maildir-counts)) (add-to-list 'global-mode-string 'jao-maildir-mode-line-string t) (jao-maildir--setup-watches cb)) (provide 'jao-maildir) ;;; jao-maildir.el ends here