From f7a247906477a2b7e2be571253d478432da309f7 Mon Sep 17 00:00:00 2001 From: TEC Date: Thu, 1 Oct 2020 19:16:33 +0800 Subject: [PATCH] Mu4e: Add nuance to Gmail-specific behaviour for the non-gmail accounts out there --- modules/email/mu4e/README.org | 3 +- modules/email/mu4e/config.el | 56 +++++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/modules/email/mu4e/README.org b/modules/email/mu4e/README.org index a7b831243..e9396175c 100644 --- a/modules/email/mu4e/README.org +++ b/modules/email/mu4e/README.org @@ -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]] diff --git a/modules/email/mu4e/config.el b/modules/email/mu4e/config.el index b2808ada4..29c037ec6 100644 --- a/modules/email/mu4e/config.el +++ b/modules/email/mu4e/config.el @@ -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"))))))))