complete-computing-environment/imap_unread_counts.org

76 lines
3.0 KiB
Org Mode

:PROPERTIES:
:ID: cce/imap_unread_counts
:ROAM_REFS: https://github.com/dabrahams/dwamacs/commit/0d6a991232e104a27ba4c85cf0c8044be3273d77
:ROAM_ALIASES: "Correct IMAP Unread Counts in Gnus"
:END:
#+TITLE: Correct IMAP unread counts - dabrahams/dwamacs@0d6a991 - GitHub
#+ARCOLOGY_KEY: cce/imap-unread-counts
#+PROPERTY: header-args :mkdirp yes :results none
#+PROPERTY: header-args:emacs-lisp :tangle imap-unread-counts.el
#+PROPERTY: header-args:yaml :tangle roles/endpoint/tasks/imap-unread-counts.yml
#+filetags: :CCE:
,#+CCE_MODULE: imap-unread-counts
,#+CCE_PRIORITY: 75
[[id:d17a9ccf-0bfe-426e-85b3-d15771d3ee03][Archive]]
Candidate for [[id:cce/cce][CCE]] module to restore unread message counts in [[id:cce/gnus][Gnus]], even with =MaxMessages= set in [[id:cce/mbsync][mbsync]] configuration.
Even with =MaxMessages 1000=, my =~/Maildir= is 8.0 gb, probably from [[id:3fff349b-282f-45de-a00d-7bbd82c5cca5][A bunch of cool tumblrs]] which I might move in to an actual Tumblr account again.
#+BEGIN_SRC emacs-lisp
(provide 'cce/imap-unread-counts)
(setq gnus-group-line-format "%S%p%P%5uy%5T: %(%B%G%B%)\n")
(require 'imap)
(defun gnus-nnimap-count-format (n)
(let ((method (or gnus-tmp-method gnus-select-method)))
(when (eq (car method) 'nnimap)
(let ((counts (nnimap-request-message-counts gnus-tmp-group method)))
(if counts (format "%d" (nth n counts)) "?")))))
(defun gnus-user-format-function-t (dummy)
(or (gnus-nnimap-count-format 0)
gnus-tmp-number-total))
(defun gnus-user-format-function-y (dummy)
(or (gnus-nnimap-count-format 1)
gnus-tmp-number-of-unread))
(defvar nnimap-message-count-cache-alist nil)
(defun nnimap-message-count-cache-clear ()
(setq nnimap-message-count-cache-alist nil))
(defun nnimap-message-count-cache-get (group)
(cadr (assoc group nnimap-message-count-cache-alist)))
(defun nnimap-message-count-cache-set (group count)
(push (list group count) nnimap-message-count-cache-alist))
(defun nnimap-request-message-counts (group method)
(or (nnimap-message-count-cache-get group)
(let ((counts (nnimap-fetch-message-counts group method)))
(nnimap-message-count-cache-set group counts)
counts)))
(defun nnimap-fetch-message-counts (group method)
(let ((imap-group (nnimap-decode-gnus-group (car (last (split-string group ":")))))
(server (cadr method)))
(when (nnimap-change-group imap-group server)
(message "Requesting message count for %s..." group)
(with-current-buffer (nnimap-buffer)
(let ((response
(assoc "MESSAGES"
(assoc "STATUS"
(nnimap-command "STATUS %S (MESSAGES UNSEEN)"
(utf7-encode imap-group t))))))
(message "Requesting message count for %s...done" group)
(and response
(mapcar #'string-to-number
(list
(nth 1 response) (nth 3 response)))))))))
(add-hook 'gnus-after-getting-new-news-hook 'nnimap-message-count-cache-clear)
#+END_SRC