Last active
February 13, 2021 20:20
-
-
Save maxminoS/bd29609ae2969545efaa871cfd5755e8 to your computer and use it in GitHub Desktop.
Automatic mu4e contexts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; I wanted to be able to use mu4e contexts without showing my emails in GitHub dotfiles or creating another file to store the email accounts. | |
;; All email account information can be found in "~/.mbsyncrc", so I decided to work out a way to automatically create mu4e contexts based on all the sync-ed email accounts with mbsync. | |
;; (This method will not work for people not using mbsync, but the same idea can be applied to achieve the same results) | |
;; Since this function uses regular expressions to scrape the required data, it has some pre-defined format needed in "~/.mbsyncrc": | |
;; - "# Account: " is placed on top of every account to indicate different accounts | |
;; - "# Full Name: " below "# Account: " for the user's name | |
;; - "# SMTP " below "# Full Name: " needs the SMTP configurations | |
;; - "IMAPAccount ..." is used as the name of the account context | |
;; - "User ..." is the email address | |
;; - "Path ..." is the path to your context | |
;; All but "# Account: " should already be in your "~/.mbsyncrc" as they are part of the configuration. | |
;; The contexts are indexed and accessed by [0-9]. | |
;; Additionally, shortcuts are localized to the current context, for a separate, localized mail system to each account. | |
;; You may need to change the context variables to suit your own settings. | |
;; I acknowledge that this might not be the best way to solve the problem; it is just one that works. If you can come up with any better way to do this, your feedback is welcome. | |
(use-package mu4e | |
:custom | |
(mu4e-get-mail-command "mbsync -a") | |
:config | |
(setq mu4e-contexts (list )) | |
(defun emax/auto-add-mu4e-contexts () | |
"Automatically detects your .mbsyncrc configuration and creates an mu4e context for each email account. | |
This function uses a separator \"# Account: \" in .mbsyncrc to distinguish between accounts. It then uses IMAPAccount, User, and Path settings to create the contexts; it will also require \"# Full Name: \" to set the name and \"# SMTP \" in the next line of \"# Account: \" for the SMTP configurations. Each context automatically sets the designated folder and may need to be changed if a different setting is desired. | |
If ~/.mbsyncrc is changed, run this function again to refresh and add the new accounts as contexts. | |
This is limited to only 10 accounts due to its indexing method. | |
" | |
(with-temp-buffer | |
(insert-file-contents "~/.mbsyncrc") | |
(keep-lines "\\(?:# Account: \\|# Full Name: \\|# SMTP \\|IMAPAccount \\|User \\|Path \\)") | |
(replace-regexp "\\(?:# Full Name: \\|# SMTP \\|IMAPAccount \\|User \\|Path \\)" "\ ") | |
(let ((idx 0)) | |
(dolist (account (split-string (buffer-string) "\\(# Account: \\).*\n" t)) | |
(let* ((data (split-string account "\n" t)) | |
(full-name (car data)) | |
(smtp (nth 1 data)) | |
(imapaccount (nth 2 data)) | |
(user (nth 3 data)) | |
(path (concat "/" (file-name-nondirectory (directory-file-name (car (last data))))))) | |
(add-to-list 'mu4e-contexts | |
(make-mu4e-context | |
:name (concat (number-to-string idx) imapaccount) | |
:match-func | |
`(lambda (msg) | |
(when msg | |
(string-prefix-p ,path (mu4e-message-field msg :maildir)))) | |
:vars `((user-mail-address . ,user) | |
(user-full-name . ,full-name) | |
(smtpmail-smtp-server . ,smtp) | |
(mu4e-refile-folder . ,(concat path "/All")) | |
(mu4e-sent-folder . ,(concat path "/Sent")) | |
(mu4e-drafts-folder . ,(concat path "/Drafts")) | |
(mu4e-trash-folder . ,(concat path "/Trash")) | |
(mu4e-bookmarks . | |
((:name ,(concat "Unread - " user) | |
:query ,(concat "flag:unread AND NOT flag:trashed AND m:" path "/All") | |
:key ?u) | |
(:name ,(concat "Today - " user) | |
:query ,(concat "date:today..now AND m:" path "/All") | |
:key ?t) | |
(:name ,(concat "Week - " user) | |
:query ,(concat "date:7d..now AND m:" path "/All") | |
:key ?w) | |
(:name "Unread - All" | |
:query "flag:unread AND NOT flag:trashed" | |
:key ?U) | |
(:name "Today - All" | |
:query "date:today..now" | |
:key ?T) | |
(:name "Week - All" | |
:query "date:7d..now" | |
:key ?W))) | |
(mu4e-maildir-shortcuts . | |
((:maildir ,(concat path "/All") :key ?a) | |
(:maildir ,(concat path "/Sent") :key ?s) | |
(:maildir ,(concat path "/Draft") :key ?d) | |
(:maildir ,(concat path "/Trash") :key ?t))))) t)) | |
(setq idx (1+ idx)))))) | |
(emax/auto-add-mu4e-contexts)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment