summaryrefslogtreecommitdiffhomepage
path: root/net/jao-maildir.el
diff options
context:
space:
mode:
Diffstat (limited to 'net/jao-maildir.el')
-rw-r--r--net/jao-maildir.el78
1 files changed, 37 insertions, 41 deletions
diff --git a/net/jao-maildir.el b/net/jao-maildir.el
index 701a848..c2a456f 100644
--- a/net/jao-maildir.el
+++ b/net/jao-maildir.el
@@ -41,12 +41,14 @@
(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)))
+(defun jao-maildir--update-counts ()
+ (dolist (mbox jao-maildirs)
+ (puthash mbox (jao-maildir--maildir-new-count mbox) jao-maildir-counts)))
+
+(defun jao-maildir--init-counts (maildirs)
+ (setq jao-maildir-counts (make-hash-table :test 'equal))
+ (setq jao-maildirs maildirs)
+ (jao-maildir--update-counts))
(defvar jao-maildir-tracked-maildirs)
(defvar jao-maildir-info-string "")
@@ -58,33 +60,27 @@
"Face used to highlihgt non-boring tracked maildirs"
:group 'jao-maildir-faces)
-(defun jao-maildir--tracked (m)
- (seq-find (lambda (e) (string-match-p (car e) m))
- jao-maildir-tracked-maildirs))
-
-(defun jao-maildir--update-info-string ()
- (let ((total 0)
- (counts))
- (dolist (c jao-maildir-counts)
- (when (> (cdr c) 0)
- (let* ((m (car c))
- (n (cdr c))
- (a (jao-maildir--tracked m))
- (an (cadr a))
- (ab (caddr a))
- (am (when a (format "%s%s" an n)))
- (f (when am (if (eq t ab) 'jao-maildir-emph ab)))
- (am (when am (if f (propertize am 'face f) am))))
- (when (null a) (setq total (+ n total)))
- (when a
- (if ab (push am counts) (setq counts (nconc counts (list am))))))))
- (let* ((total (if (> total 0) (format " %d " total) " "))
- (s (mapconcat 'identity counts " "))
- (s (if (string-empty-p s) s (concat s " ")))
- (s (format "%s%s%s" "" total s)))
- (setq jao-maildir-info-string s)
- (when jao-maildir-echo-p (jao-minibuffer-refresh))
- (force-mode-line-update t))))
+(defun jao-maildir--tracked-cnt (r)
+ (seq-reduce (lambda (c k)
+ (if (string-match-p r k) (+ c (gethash k jao-maildir-counts 0)) c))
+ (hash-table-keys jao-maildir-counts)
+ 0))
+
+(defun jao-maildir--update-info-string (&optional force)
+ (when (eq force t) (jao-maildir--update-counts))
+ (let ((counts))
+ (dolist (track jao-maildir-tracked-maildirs)
+ (let ((cnt (jao-maildir--tracked-cnt (car track))))
+ (when (> cnt 0)
+ (let* ((label (cadr track))
+ (face (caddr track))
+ (am (format "%s%s" label cnt))
+ (face1 (if (eq t face) 'jao-maildir-emph face))
+ (am (if face (propertize am 'face face1) am)))
+ (if face (push am counts) (setq counts (nconc counts (list am))))))))
+ (setq jao-maildir-info-string
+ (if (null counts) "" (format " %s " (mapconcat 'identity counts " "))))
+ (when jao-maildir-echo-p (jao-minibuffer-refresh))))
(defvar jao-maildir--watches nil)
@@ -100,29 +96,29 @@
(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)
+ (puthash mbox (jao-maildir--maildir-new-count mbox) jao-maildir-counts)
(jao-maildir--update-info-string)
- (when cb (funcall cb mbox)))))
+ (when cb (funcall cb mbox))
+ (when jao-maildir-echo-p
+ (message (if (eq (cadr e) 'created) "New mail" ""))))))
(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)
+ '(change attribute-change)
(jao-maildir--watcher mbox cb)))
jao-maildirs)))
;;;###autoload
(defun jao-maildir-setup (maildirs mode-line &optional cb)
- (setq jao-maildirs maildirs)
- (setq jao-maildir-counts (jao-maildir-counts))
+ (jao-maildir--init-counts maildirs)
(cond ((eq 'mode-line mode-line)
(add-to-list 'global-mode-string 'jao-maildir-info-string t))
(mode-line
- (jao-minibuffer-add-variable 'jao-maildir-info-string)))
+ (jao-minibuffer-add-variable 'jao-maildir-info-string)
+ (jao-maildir--update-info-string)))
(jao-maildir--setup-watches cb))