mu4e-notifications: show notifications for new messages

Filter out the _same_ new messages.
This commit is contained in:
Dirk-Jan C. Binnema 2023-01-26 21:35:42 +02:00
parent abf0e259a2
commit d3966926a9
1 changed files with 41 additions and 34 deletions

View File

@ -19,7 +19,6 @@
"Function for determining if a notification is to be emitted. "Function for determining if a notification is to be emitted.
If this is the case, the function should return non-nil. If this is the case, the function should return non-nil.
The function must accept an optional single parameter, unused for The function must accept an optional single parameter, unused for
now." now."
:type 'function :type 'function
@ -32,53 +31,61 @@ now."
The function is invoked when we need to emit a new-mail The function is invoked when we need to emit a new-mail
notification in some system-specific way. The function is invoked notification in some system-specific way. The function is invoked
when the query-items have been updated and when the query-items have been updated and
`mu4e-notification-filter' returns t. `mu4e-notification-filter' returns non-nil.
The function must accept an optional single parameter, unused for The function must accept an optional single parameter, unused for
now." now."
:type 'function :type 'function
:group 'mu4e-notification) :group 'mu4e-notification)
(defvar mu4e--last-delta-unread 0
"Last notified number.")
(defvar mu4e--notification-id nil
"The last notification id, so we can replace it.")
(defun mu4e--default-notification-filter (&optional _) (defun mu4e--default-notification-filter (&optional _)
"Return t if a notification should be shown. "Return t if a notification should be shown.
This default implementation does so when there are more unread This default implementation does so when the number of unread
messages since baseline for the favorite bookmark." messages changed since the last notification and it is greater
(when-let ((fav (mu4e-bookmark-favorite))) than zero."
(> (or (plist-get fav :delta-unread) 0) 0))) (when-let* ((fav (mu4e-bookmark-favorite))
(delta-unread (plist-get fav :delta-unread)))
(defvar mu4e--notification-id nil (when (and (> delta-unread 0)
"The last notification id, so we can replace it.") (not (= delta-unread mu4e--last-delta-unread)))
(setq mu4e--last-delta-unread delta-unread) ;; update
t ;; do show notification
)))
(defun mu4e--default-notification-function (&optional _) (defun mu4e--default-notification-function (&optional _)
"Default function for handling notifications. "Default function for handling notifications.
The default implementation uses emacs' built-in dbus-notification The default implementation uses emacs' built-in dbus-notification
support." support."
(when-let ((fav (mu4e-bookmark-favorite))) (when-let* ((fav (mu4e-bookmark-favorite))
(let* ((title "mu4e found new mail") (title "mu4e found new mail")
(delta-unread (or (plist-get fav :delta-unread) 0)) (delta-unread (or (plist-get fav :delta-unread) 0))
(body (format "%d new message%s in %s" (body (format "%d new message%s in %s"
delta-unread delta-unread
(if (= delta-unread 1) "" "s") (if (= delta-unread 1) "" "s")
(plist-get fav :name)))) (plist-get fav :name))))
(cond (cond
((fboundp 'notifications-notify) ((fboundp 'notifications-notify)
;; notifactions available ;; notifications available
(setq mu4e--notification-id (setq mu4e--notification-id
(notifications-notify (notifications-notify
:title title :title title
:body body :body body
:app-name "mu4e@emacs" :app-name "mu4e@emacs"
:replaces-id mu4e--notification-id :replaces-id mu4e--notification-id
;; a custom mu4e icon would be nice... ;; a custom mu4e icon would be nice...
;; :app-icon (ignore-errors ;; :app-icon (ignore-errors
;; (image-search-load-path ;; (image-search-load-path
;; "gnus/gnus.png")) ;; "gnus/gnus.png"))
:actions '("Show" "Favorite bookmark") :actions '("Show" "Favorite bookmark")
:on-action (lambda (_ _) (mu4e-jump-to-favorite))))) :on-action (lambda (_ _) (mu4e-jump-to-favorite)))))
;; ... TBI: other notifications ... ;; ... TBI: other notifications ...
(t ;; last resort (t ;; last resort
(mu4e-message "%s: %s" title body)))))) (mu4e-message "%s: %s" title body)))))
(defun mu4e--notification () (defun mu4e--notification ()
"Function called when the query items have been updated." "Function called when the query items have been updated."