Mu4e: Add nuance to Gmail-specific behaviour

for the non-gmail accounts out there
This commit is contained in:
TEC 2020-10-01 19:16:33 +08:00
parent 940177613e
commit f7a2479064
No known key found for this signature in database
GPG key ID: 779591AFDB81F06C
2 changed files with 49 additions and 10 deletions

View file

@ -36,7 +36,8 @@ via IMAP) and ~mu~ (to index my mail into a format ~mu4e~ can understand).
#+end_quote
** Module Flags
+ ~+gmail~ Enables gmail-specific configuration.
+ ~+gmail~ Enables gmail-specific configuration for mail ~To~ or ~From~ a gmail
address, or a maildir with ~gmail~ in the name.
** Plugins
+ [[https://github.com/jeremy-compostella/org-msg][org-msg]]

View file

@ -325,7 +325,10 @@ Must be set before org-msg is loaded to take effect.")
(when (featurep! +gmail)
(after! mu4e
;; don't save message to Sent Messages, Gmail/IMAP takes care of this
(setq mu4e-sent-messages-behavior 'delete
(setq mu4e-sent-messages-behavior
(lambda ()
(if (string-match-p "@gmail.com\\'" (message-sendmail-envelope-from))
'delete 'sent))
;; don't need to run cleanup after indexing for gmail
mu4e-index-cleanup nil
@ -334,6 +337,19 @@ Must be set before org-msg is loaded to take effect.")
;; messages don't really "move"
mu4e-index-lazy-check t)
(defun +mu4e-msg-gmail-p (msg)
(or
(string-match-p "gmail"
(cond
((member (mu4e-message-field msg :to)
(plist-get mu4e~server-props :personal-addresses))
(mu4e-message-field msg :to))
((member (mu4e-message-field msg :from)
(plist-get mu4e~server-props :personal-addresses))
(mu4e-message-field msg :from))
(t "")))
(string-match-p "gmail" (mu4e-message-field msg :maildir))))
;; In my workflow, emails won't be moved at all. Only their flags/labels are
;; changed. Se we redefine the trash and refile marks not to do any moving.
;; However, the real magic happens in `+mu4e|gmail-fix-flags'.
@ -342,18 +358,39 @@ Must be set before org-msg is loaded to take effect.")
(defun +mu4e--mark-seen (docid _msg target)
(mu4e~proc-move docid (mu4e~mark-check-target target) "+S-u-N"))
(defvar +mu4e--last-invalid-gmail-action 0)
(delq! 'delete mu4e-marks #'assq)
(setf (alist-get 'trash mu4e-marks)
(setf (alist-get 'delete mu4e-marks)
(list
:char '("D" . "")
:prompt "Delete"
:show-target (lambda (target) "delete")
:action (lambda (docid msg target)
(if (+mu4e-msg-gmail-p msg)
(progn (message "The delete operation is invalid for Gmail accounts.")
(when (< 2 (- (float-time) +mu4e--last-invalid-gmail-action))
(sit-for 1))
(setq +mu4e--last-invalid-gmail-action (float-time)))
(mu4e~proc-remove docid))))
(alist-get 'trash mu4e-marks)
(list :char '("d" . "")
:prompt "dtrash"
:dyn-target (lambda (_target msg) (mu4e-get-trash-folder msg))
:action #'+mu4e--mark-seen)
:action (lambda (docid msg target)
(if (+mu4e-msg-gmail-p msg)
(+mu4e--mark-seen docid msg target)
(mu4e~proc-move docid (mu4e~mark-check-target target) "+T-N"))))
;; Refile will be my "archive" function.
(alist-get 'refile mu4e-marks)
(list :char '("r" . "")
:prompt "rrefile"
:dyn-target (lambda (_target msg) (mu4e-get-refile-folder msg))
:action #'+mu4e--mark-seen))
:action (lambda (docid msg target)
(if (+mu4e-msg-gmail-p msg)
(+mu4e--mark-seen docid msg target)
(mu4e~proc-move docid (mu4e~mark-check-target target) "-N")))
#'+mu4e--mark-seen))
;; This hook correctly modifies gmail flags on emails when they are marked.
;; Without it, refiling (archiving), trashing, and flagging (starring) email
@ -361,8 +398,9 @@ Must be set before org-msg is loaded to take effect.")
;; are ineffectual otherwise.
(add-hook! 'mu4e-mark-execute-pre-hook
(defun +mu4e-gmail-fix-flags-h (mark msg)
(pcase mark
(`trash (mu4e-action-retag-message msg "-\\Inbox,+\\Trash,-\\Draft"))
(`refile (mu4e-action-retag-message msg "-\\Inbox"))
(`flag (mu4e-action-retag-message msg "+\\Starred"))
(`unflag (mu4e-action-retag-message msg "-\\Starred")))))))
(when (+mu4e-msg-gmail-p msg)
(pcase mark
(`trash (mu4e-action-retag-message msg "-\\Inbox,+\\Trash,-\\Draft"))
(`refile (mu4e-action-retag-message msg "-\\Inbox"))
(`flag (mu4e-action-retag-message msg "+\\Starred"))
(`unflag (mu4e-action-retag-message msg "-\\Starred"))))))))