Skip to content

Instantly share code, notes, and snippets.

@runejuhl
Created January 24, 2018 14:33
Show Gist options
  • Save runejuhl/df9060590ecb2f81dd5801e1e6882545 to your computer and use it in GitHub Desktop.
Save runejuhl/df9060590ecb2f81dd5801e1e6882545 to your computer and use it in GitHub Desktop.
notmuch functions to show unread count in hello buffer
(defun notmuch-hello-insert-buttons (searches)
"Insert buttons for SEARCHES.
SEARCHES must be a list of plists each of which should contain at
least the properties :name NAME :query QUERY and :count COUNT,
where QUERY is the query to start when the button for the
corresponding entry is activated, and COUNT should be the number
of messages matching the query. Such a plist can be computed
with `notmuch-hello-query-counts'."
(let* ((widest (notmuch-hello-longest-label searches))
(tags-and-width (notmuch-hello-tags-per-line widest))
(tags-per-line (car tags-and-width))
(column-width (cdr tags-and-width))
(column-indent 0)
(count 0)
(reordered-list (notmuch-hello-reflect searches tags-per-line))
;; Hack the display of the buttons used.
(widget-push-button-prefix "")
(widget-push-button-suffix ""))
;; dme: It feels as though there should be a better way to
;; implement this loop than using an incrementing counter.
(mapc (lambda (elem)
;; (not elem) indicates an empty slot in the matrix.
(when elem
(if (> column-indent 0)
(widget-insert (make-string column-indent ? )))
(let* ((name (plist-get elem :name))
(query (plist-get elem :query))
(oldest-first (case (plist-get elem :sort-order)
(newest-first nil)
(oldest-first t)
(otherwise notmuch-search-oldest-first)))
(search-type (eq (plist-get elem :search-type) 'tree))
(msg-count (plist-get elem :count))
(msg-count-unread (plist-get elem :unread-count)))
(widget-insert (format "%s "
(notmuch-hello-nice-number msg-count)))
(widget-insert (format "(%s) "
(notmuch-hello-nice-number msg-count-unread)))
(widget-create 'push-button
:notify #'notmuch-hello-widget-search
:notmuch-search-terms query
:notmuch-search-oldest-first oldest-first
:notmuch-search-type search-type
name)
(setq column-indent
(1+ (max 0 (- column-width (length name)))))))
(setq count (1+ count))
(when (eq (% count tags-per-line) 0)
(setq column-indent 0)
(widget-insert "\n")))
reordered-list)
;; If the last line was not full (and hence did not include a
;; carriage return), insert one now.
(unless (eq (% count tags-per-line) 0)
(widget-insert "\n"))))
(defun notmuch-hello-query-counts (query-list &rest options)
"Compute list of counts of matched messages from QUERY-LIST.
QUERY-LIST must be a list of saved-searches. Ideally each of
these is a plist but other options are available for backwards
compatibility: see `notmuch-saved-searches' for details.
The result is a list of plists each of which includes the
properties :name NAME, :query QUERY and :count COUNT, together
with any properties in the original saved-search.
The values :show-empty-searches, :filter and :filter-count from
options will be handled as specified for
`notmuch-hello-insert-searches'."
(with-temp-buffer
(dolist (elem query-list nil)
(let* ((count-query (or (notmuch-saved-search-get elem :count-query)
(notmuch-saved-search-get elem :query)))
(count-query-unread (if (string= count-query "*")
count-query
(concat
count-query
" and tag:unread")))
(filter-query (or (plist-get options :filter-count)
(plist-get options :filter))))
(insert
(replace-regexp-in-string
"\n" " "
(notmuch-hello-filtered-query count-query filter-query))
"\n"
(replace-regexp-in-string
"\n" " "
(notmuch-hello-filtered-query count-query-unread filter-query))
"\n")))
(unless (= (call-process-region (point-min) (point-max) notmuch-command
t t nil "count" "--batch") 0)
(notmuch-logged-error "notmuch count --batch failed"
"Please check that the notmuch CLI is new enough to support `count
--batch'. In general we recommend running matching versions of
the CLI and emacs interface."))
(goto-char (point-min))
(notmuch-remove-if-not
#'identity
(mapcar
(lambda (elem)
(let* ((elem-plist (notmuch-hello-saved-search-to-plist elem))
(search-query (plist-get elem-plist :query))
(filtered-query (notmuch-hello-filtered-query
search-query (plist-get options :filter)))
(message-count (prog1 (read (current-buffer))
(forward-line 1)))
(unread-count (prog1 (read (current-buffer))
(forward-line 1))))
(when (and filtered-query (or (plist-get options :show-empty-searches) (> message-count 0)))
(setq elem-plist (plist-put elem-plist :query filtered-query))
(plist-put elem-plist :count message-count)
(plist-put elem-plist :unread-count unread-count))))
query-list))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment