-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmu4easy.el
477 lines (434 loc) · 18.9 KB
/
mu4easy.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
;;; mu4easy.el --- Packages + configs for using mu4e with multiple accounts -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2023 Daniel Fleischer
;; Author: Daniel Fleischer <[email protected]>
;; Keywords: mail
;; Homepage: https://github.com/danielfleischer/mu4easy
;; Version: 1.0
;; Package-Requires: ((emacs "25.1") (mu4e-column-faces "1.2.1") (mu4e-alert "1.0") (org-msg "4.0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package contains a collection of packages and configurations
;; making it easy and fun to use email with Emacs using mu, mu4e, mbsync
;; and other improvements. The setup supports multiple email providers
;; such as Google, Apple, GMX, Outlook and Proton. In addition to this
;; elisp package, there is an mbsync configuration that needs to be
;; changed and copied manually.
;; Please check out the repository
;; https://github.com/danielfleischer/mu4easy as it contains
;; documentation and an mbsync configuration example, which will not be
;; installed when installing the package with MELPA.
;;; Code:
(require 'smtpmail)
(require 'mu4e)
(require 'mu4e-icalendar)
(require 'mu4e-contrib)
(require 'mu4e-column-faces)
(require 'mu4e-alert)
(require 'org-msg)
(defgroup mu4easy nil
"Easy configuration for mu4e."
:group 'mail)
(defcustom mu4easy-accounts '("Gmail" "GMX" "Apple" "Proton")
"List of email accounts names, as defined on disk."
:type '(repeat string))
(defcustom mu4easy-greeting "Hi%s,\n\n"
"Email greeting where %s is the first name of 1-2 recipients.
See also `org-msg-greeting-fmt'."
:type '(string))
(defcustom mu4easy-signature "\n\n*Daniel Fleischer*"
"Signature; supports org syntax thanks to org-msg."
:type '(string))
(defcustom mu4easy-download-dir "~/Downloads"
"Location of downloads dir."
:type '(directory))
;;; Variables
(defun mu4easy-mail-link-description (msg)
"Creating a link description to be used with `org-store-link'.
Argument MSG msg at point."
(let ((subject (or (plist-get msg :subject)
"No subject"))
(date (or (format-time-string mu4e-headers-date-format
(mu4e-msg-field msg :date))
"No date"))
(to-from (mu4e~headers-from-or-to msg)))
(format "%s: %s (%s)" to-from subject date)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Hooks
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4easy--org-msg-hook ()
"Settings for org-msg mode."
(set-fill-column 120)
(turn-on-auto-fill)
(electric-indent-local-mode -1)
(turn-on-flyspell))
(defun mu4easy--compose-hook ()
"Settings for mu4e compose mode."
(set-fill-column 120)
(turn-on-auto-fill)
(electric-indent-local-mode -1)
(turn-on-flyspell))
(defun mu4easy--update-buffer ()
"Revert buffer when switching to mu4e to update the stats."
(when (derived-mode-p 'mu4e-main-mode)
(revert-buffer)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Update specific accounts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4easy-update-custom-account ()
"Run mbsync update for a specific account."
(interactive)
(let ((account (completing-read
"Select account: "
(cons "All" mu4easy-accounts) nil t nil nil "All"))
(command (format "INSIDE_EMACS=%s mbsync " emacs-version)))
(pcase account
("All" (concat command "-a"))
(else (concat command else)))))
(defun mu4easy-update-mail-and-index ()
"Run a mu4e update; if prefix, focus on a specific account."
(interactive)
(mu4e-kill-update-mail)
(if current-prefix-arg
(let ((mu4e-get-mail-command (mu4easy-update-custom-account)))
(mu4e-update-mail-and-index nil))
(mu4e-update-mail-and-index nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Trash without trashed flag
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setf (alist-get 'trash mu4e-marks)
'(:char ("d" . "▼")
:prompt "dtrash"
:dyn-target (lambda (target msg) (mu4e-get-trash-folder msg))
;; Here's the main difference to the regular trash mark, no +T
;; before -N so the message is not marked as IMAP-deleted, unless
;; it's Gmail.
:action (lambda (docid msg target)
(let ((maildir (mu4e-message-field msg :maildir)))
(if (string-match-p "Gmail\\|Google" maildir)
(mu4e--server-move docid (mu4e--mark-check-target target) "+T+S-u-N")
(mu4e--server-move docid (mu4e--mark-check-target target) "+S-u-N"))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Consistent Refile
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(setf (alist-get 'refile mu4e-marks)
'(:char ("r" . "▶")
:prompt "refile"
:dyn-target (lambda (target msg) (mu4e-get-refile-folder msg))
;; Notice the special treatment for Gmail.
:action (lambda (docid msg target)
(let ((maildir (mu4e-message-field msg :maildir)))
(if (string-match-p "Gmail\\|Google" maildir)
(mu4e--server-remove docid)
(mu4e--server-move docid (mu4e--mark-check-target target) "+S-u-N"))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Respond in text-mode if prefix
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sometimes I want to respond in text to HTML messages
;; e.g. when participating in github discussions using email
(defun mu4easy-org-msg-select-format (alternative)
"Wrapping function to override email format (html/text).
Argument ALTERNATIVE passthrough argument when advising."
(if current-prefix-arg '(text) alternative))
;; Text Mode Signature
(defun mu4easy-customize-org-msg (orig-fun &rest args)
"Fix for signature and greeting when email is text.
Argument ORIG-FUN function being advised.
Optional argument ARGS ."
(let ((res (apply orig-fun args)))
(when (equal (cadr args) '(text))
(setf (alist-get 'signature res)
(replace-regexp-in-string "\\([\*/]\\)" ""
org-msg-signature))
(setf (alist-get 'greeting-fmt res) ""))
res))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Macro for Contexts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(cl-defmacro mu4easy-context (&key c-name maildir mail smtp
(smtp-mail mail)
(smtp-port 587)
(smtp-type 'starttls)
(sent-action 'sent)
(name user-full-name)
(sig mu4easy-signature))
"Main macro for creating email accounts (contexts).
See examples in the README file.
C-NAME context name, used in mu4e UI; first letter is going to be
used as a shortcut.
MAILDIR mail dir under path/Mail/...
MAIL email address or alias.
SMTP address.
SMTP-MAIL email address for this account (not alias).
SMTP-TYPE default `starttls'.
SMTP-PORT default 587.
SENT-ACTION what to do after sending an email (copy to `sent' or delete);
see README.
NAME name can be set per account.
SIG signature string; supports org formatting thanks to org-msg."
(let
((inbox (concat "/" maildir "/Inbox"))
(sent (concat "/" maildir "/Sent"))
(trash (concat "/" maildir "/Trash"))
(refile (concat "/" maildir "/Archive"))
(draft (concat "/" maildir "/Drafts"))
(spam (concat "/" maildir "/Spam")))
`(make-mu4e-context
:name ,c-name
:match-func (lambda (msg)
(when msg
(string-match-p (concat "^/" ,maildir "/")
(mu4e-message-field msg :maildir))))
:vars '((user-mail-address . ,mail)
(user-full-name . ,name)
(mu4e-sent-folder . ,sent)
(mu4e-drafts-folder . ,draft)
(mu4e-trash-folder . ,trash)
(mu4e-refile-folder . ,refile)
(mu4e-compose-format-flowed . t)
(mu4e-sent-messages-behavior . ,sent-action)
(smtpmail-stream-type . ,smtp-type)
(smtpmail-smtp-service . ,smtp-port)
(smtpmail-smtp-user . ,smtp-mail)
(smtpmail-smtp-server . ,smtp)
(smtpmail-debug-info . t)
(smtpmail-debug-verbose . t)
(org-msg-signature . ,sig)
(mu4e-maildir-shortcuts .
((,inbox . ?i)
(,sent . ?s)
(,trash . ?t)
(,refile . ?a)
(,draft . ?d)
(,spam . ?g)))))))
(defvar mu4easy-today-query "date:today..now AND NOT maildir:/Trash/ AND NOT maildir:/Spam/")
(defvar mu4easy-trash-query "maildir:/Trash/")
(defvar mu4easy-inbox-query "maildir:/Inbox/")
(defvar mu4easy-unread-query "flag:new AND maildir:/Inbox/")
(defcustom mu4easy-bookmarks
`(( :name "Unread"
:query ,mu4easy-unread-query
:key ?u)
( :name "Inbox"
:query ,mu4easy-inbox-query
:key ?i)
( :name "Today"
:query ,mu4easy-today-query
:key ?t)
( :name "Flagged"
:query "flag:flagged"
:key ?f)
( :name "Tags"
:query "tag://"
:key ?T)
( :name "Trash"
:query ,mu4easy-trash-query
:key ?x
:hide-unread t)
( :name "Attachments"
:query "mime:application/pdf or mime:image/jpg or mime:image/png"
:key ?a
:hide-unread t))
"Preconfigured bookmarks for easy navigation.
See variable `mu4e-bookmarks'."
:type '(repeat (plist)))
(defcustom mu4easy-headers
'((:human-date . 18)
(:flags . 6)
(:maildir . 16)
(:from-or-to . 22)
(:mailing-list . 10)
(:tags . 10)
(:subject . 92))
"Format of headers.
See variable `mu4e-headers-fields'"
:type '(repeat (cons symbol integer)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Mail Identities
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defcustom mu4easy-contexts
'((mu4easy-context
:c-name "Google"
:maildir "Gmail"
:mail "[email protected]"
:smtp "smtp.gmail.com"
:sent-action delete)
(mu4easy-context
:c-name "1-GMX"
:maildir "GMX"
:mail "[email protected]"
:smtp "mail.gmx.com")
(mu4easy-context
:c-name "2-GMX-alias"
:maildir "GMX"
:mail "[email protected]"
:smtp "mail.gmx.com"
:smtp-mail "[email protected]")
(mu4easy-context
:c-name "Apple"
:maildir "Apple"
:mail "[email protected]"
:smtp "smtp.mail.me.com")
(mu4easy-context
:c-name "3-Apple-alias"
:maildir "Apple"
:mail "[email protected]"
:smtp "smtp.mail.me.com"
:smtp-mail "[email protected]")
(mu4easy-context
:c-name "Outlook"
:maildir "Outlook"
:mail "[email protected]"
:smtp "smtp-mail.outlook.com")
(mu4easy-context
:c-name "Proton"
:maildir "Proton"
:mail "[email protected]"
:smtp "127.0.0.1"
:smtp-type ssl
:smtp-port 999)
(mu4easy-context
:c-name "4-Proton-alias"
:maildir "Proton"
:mail "[email protected]"
:smtp "127.0.0.1"
:smtp-mail "[email protected]"
:smtp-type ssl
:smtp-port 999))
"Defining accounts and aliases.
After changing it, reload the minor mode.
See `mu4easy-context' for function signature."
:type '(repeat sexp))
(defun mu4easy--maps ()
"Define additional mapping for specific modes related to mu4e."
(define-key mu4e-main-mode-map (kbd "x") #'bury-buffer)
(define-key mu4e-main-mode-map (kbd "I") #'mu4e-update-index)
(define-key mu4e-main-mode-map (kbd "U") #'mu4easy-update-mail-and-index)
(define-key mu4e-view-mode-map (kbd "<tab>") #'shr-next-link)
(define-key mu4e-view-mode-map (kbd "<backtab>") #'shr-previous-link)
(define-key mu4e-headers-mode-map (kbd "M") #'mu4e-headers-mark-all)
(define-key mu4e-headers-mode-map (kbd "N") #'mu4e-headers-mark-all-unread-read))
;;;###autoload
(define-minor-mode mu4easy-mode
"Toggle mu4easy configuration and keymaps."
:lighter nil
:global t
(cond (mu4easy-mode
(mu4easy--maps)
(mu4e-icalendar-setup)
(mu4e-column-faces-mode)
(mu4e-alert-enable-notifications)
(mu4e-alert-enable-mode-line-display)
(org-msg-mode)
(add-hook 'org-msg-edit-mode-hook #'mu4easy--org-msg-hook)
(add-hook 'mu4e-compose-mode-hook #'mu4easy--compose-hook)
(add-hook 'mu4e-context-changed-hook #'mu4easy--update-buffer)
(advice-add 'org-msg-composition-parameters :around #'mu4easy-customize-org-msg)
(advice-add 'org-msg-get-alternatives :filter-return #'mu4easy-org-msg-select-format)
(add-to-list 'mu4e-view-actions
'("Apply Email" . mu4e-action-git-apply-mbox) t)
(setq mail-user-agent 'mu4e-user-agent)
(setq mu4e-contexts (mapcar #'eval mu4easy-contexts))
(setq mu4e-bookmarks mu4easy-bookmarks)
(setq mu4e-headers-fields mu4easy-headers)
(setq message-signature nil)
(setq message-citation-line-function 'message-insert-formatted-citation-line)
(setq message-kill-buffer-on-exit t)
(setq message-send-mail-function 'smtpmail-send-it)
(setq mu4e-attachment-dir (expand-file-name mu4easy-download-dir))
(setq mu4e-change-filenames-when-moving t)
(setq mu4e-completing-read-function 'completing-read)
(setq mu4e-compose-context-policy 'ask)
(setq mu4e-compose-format-flowed t)
(setq mu4e-compose-signature-auto-include nil)
(setq mu4e-confirm-quit nil)
(setq mu4e-context-policy 'pick-first)
(setq mu4e-get-mail-command (format "INSIDE_EMACS=%s mbsync -a" emacs-version))
(setq mu4e-headers-auto-update t)
(setq mu4e-headers-date-format "%d/%m/%Y %H:%M")
(setq mu4e-headers-include-related nil)
(setq mu4e-headers-skip-duplicates t)
(setq mu4e-index-cleanup t)
(setq mu4e-index-lazy-check nil)
(setq mu4e-main-hide-personal-addresses t)
(setq mu4e-main-buffer-name "*mu4e-main*")
(setq mu4e-mu-binary (or (executable-find "mu") "/usr/local/bin/mu"))
(setq mu4e-org-link-desc-func 'mu4easy-mail-link-description)
(setq mu4e-sent-messages-behavior 'sent)
(setq mu4e-update-interval 400)
(setq mu4e-use-fancy-chars t)
(setq message-citation-line-format "%N [%Y-%m-%d %a %H:%M] wrote:
")
(setq mu4e-icalendar-trash-after-reply nil)
(setq mu4e-icalendar-diary-file diary-file)
(setq org-msg-options "html-postamble:nil H:5 num:nil ^:{} toc:nil author:nil email:nil \\n:t tex:imagemagick")
(setq org-msg-startup "hidestars indent inlineimages")
(setq org-msg-default-alternatives '((new . (text html))
(reply-to-html . (text html))
(reply-to-text . (text))))
(setq org-msg-signature mu4easy-signature)
(setq org-msg-greeting-fmt mu4easy-greeting)
(setq org-msg-posting-style 'top-posting)
(setq org-msg-greeting-name-limit 2)
(setq org-msg-convert-citation t))
(t
;; remove all the hooks
(remove-hook 'org-msg-edit-mode-hook #'mu4easy--org-msg-hook)
(remove-hook 'mu4e-compose-mode-hook #'mu4easy--compose-hook)
(remove-hook 'mu4e-context-changed-hook #'mu4easy--update-buffer)
(advice-remove 'org-msg-composition-parameters #'mu4easy-customize-org-msg)
(advice-remove 'org-msg-get-alternatives #'mu4easy-org-msg-select-format)
(mu4e-column-faces-mode -1)
(mu4e-alert-disable-notifications)
(mu4e-alert-disable-mode-line-display)
(org-msg-mode -1)
(custom-reevaluate-setting 'mail-user-agent)
(custom-reevaluate-setting 'mu4e-contexts)
(custom-reevaluate-setting 'mu4e-bookmarks)
(custom-reevaluate-setting 'mu4e-headers-fields)
(custom-reevaluate-setting 'message-citation-line-function)
(custom-reevaluate-setting 'message-kill-buffer-on-exit)
(custom-reevaluate-setting 'message-send-mail-function)
(custom-reevaluate-setting 'mu4e-attachment-dir)
(custom-reevaluate-setting 'mu4e-change-filenames-when-moving)
(custom-reevaluate-setting 'mu4e-completing-read-function)
(custom-reevaluate-setting 'mu4e-compose-context-policy)
(custom-reevaluate-setting 'mu4e-compose-format-flowed)
(custom-reevaluate-setting 'mu4e-compose-signature-auto-include)
(custom-reevaluate-setting 'mu4e-confirm-quit)
(custom-reevaluate-setting 'mu4e-context-policy)
(custom-reevaluate-setting 'mu4e-get-mail-command)
(custom-reevaluate-setting 'mu4e-headers-auto-update)
(custom-reevaluate-setting 'mu4e-headers-date-format)
(custom-reevaluate-setting 'mu4e-headers-include-related)
(custom-reevaluate-setting 'mu4e-headers-skip-duplicates)
(custom-reevaluate-setting 'mu4e-index-cleanup)
(custom-reevaluate-setting 'mu4e-index-lazy-check)
(custom-reevaluate-setting 'mu4e-main-hide-personal-addresses)
(custom-reevaluate-setting 'mu4e-main-buffer-name)
(custom-reevaluate-setting 'mu4e-mu-binary)
(custom-reevaluate-setting 'mu4e-org-link-desc-func)
(custom-reevaluate-setting 'mu4e-sent-messages-behavior)
(custom-reevaluate-setting 'mu4e-update-interval)
(custom-reevaluate-setting 'mu4e-use-fancy-chars)
(custom-reevaluate-setting 'message-citation-line-format)
(custom-reevaluate-setting 'mu4e-icalendar-trash-after-reply)
(custom-reevaluate-setting 'mu4e-icalendar-diary-file)
(custom-reevaluate-setting 'org-msg-options)
(custom-reevaluate-setting 'org-msg-startup)
(custom-reevaluate-setting 'org-msg-default-alternatives)
(custom-reevaluate-setting 'org-msg-signature)
(custom-reevaluate-setting 'org-msg-greeting-fmt)
(custom-reevaluate-setting 'org-msg-posting-style)
(custom-reevaluate-setting 'org-msg-greeting-name-limit)
(custom-reevaluate-setting 'org-msg-convert-citation))))
(provide 'mu4easy)
;;; mu4easy.el ends here