aboutsummaryrefslogtreecommitdiff
path: root/emacs/settings/init-mu4e.el
blob: 31ba5142d93ae76b9c231ae8ef9ee790c41fa8d7 (plain)
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
;;; init-mu4e.el --- Configuration for mu4e

;; Copyright (C) 2016 Miquel Sabaté Solà <mikisabate@gmail.com>
;;
;; 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.

;;; Commentary:
;;
;; Configuration for mu4e.  This configuration makes quite some assumptions.
;; Read the `emacs/README.md` file as provided in my `dotfiles` project to
;; get more details.

;;; Code:

;; TODO: sending emails is a bit screwed in my current setup...

;; TODO: refile
;;; http://comments.gmane.org/gmane.mail.mu.general/631
;;; https://www.djcbsoftware.nl/code/mu/mu4e/Refiling-messages.html
;;; https://www.djcbsoftware.nl/code/mu/mu4e/Smart-refiling.html
;;; http://www.djcbsoftware.nl/code/mu/mu4e/Retrieval-and-indexing.html

;; TODO: evil commands such as C-h, C-l
;; TODO: remove whole thread ?
;; TODO: signature below quote
;; TODO: gmail flags
;; TODO: msmtp

; I'm using an RPM that I've built on OBS which installs mu4e globally. See:
; https://build.opensuse.org/package/show/home:mssola/mu
(add-to-list 'load-path "/usr/share/emacs/site-lisp/mu4e")
(require 'mu4e)

(when (featurep 'mu4e)
  ; Define a proper shortcut.
  (global-set-key (kbd "C-c m") 'mu4e)

  ;; Diferent SMTP options that will be used for each context.

  (setq message-send-mail-function 'smtpmail-send-it
        mu4e-maildir (expand-file-name "~/.mail")
        starttls-use-gnutls t)

  (defun mssola-gmail-smtp ()
    (require 'smtpmail)

    (setq smtpmail-starttls-credentials
          '(("smtp.gmail.com" 587 nil nil))
          smtpmail-auth-credentials
          (expand-file-name "~/.authinfo.gpg")
          smtpmail-default-smtp-server "smtp.gmail.com"
          smtpmail-smtp-server "smtp.gmail.com"
          smtpmail-smtp-service 587))

  (defun mssola-suse-smtp ()
    (require 'smtpmail)

    (setq smtpmail-starttls-credentials
          '(("smtp.novell.com" 25 nil nil))
          smtpmail-auth-credentials
          (expand-file-name "~/.authinfo.gpg")
          smtpmail-default-smtp-server "smtp.novell.com"
          smtpmail-smtp-server "smtp.novell.com"
          smtpmail-smtp-service 25))

  ;; Define the different accounts that I'm using. This is only available since
  ;; mu 0.9.16.

  (setq mu4e-contexts
        `(
          ;; GMail
          ,(make-mu4e-context
            :name "gmail"
            :enter-func (lambda ()
                          (mu4e-message "Switching to gmail.com")
                          (mssola-gmail-smtp))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-contact-field-matches
                             msg :to "mikisabate@gmail.com")))
            :vars '(
                    (user-mail-address     . "mikisabate@gmail.com")
                    (user-full-name        . "Miquel Sabaté Solà")
                    (mu4e-reply-to-address . "mikisabate@gmail.com")
                    (mu4e-drafts-folder    . "/gmail/Drafts")
                    (mu4e-sent-folder      . "/gmail/Sent")
                    (mu4e-trash-folder     . "/gmail/Trash")))

          ;; suse.com
          ,(make-mu4e-context
            :name "comsuse"
            :enter-func (lambda ()
                          (mu4e-message "Switching to suse.com")
                          (mssola-suse-smtp))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-contact-field-matches
                             msg :to "msabate@suse.com")))
            :vars '(
                    (user-mail-address     . "msabate@suse.com")
                    (user-full-name        . "Miquel Sabaté Solà")
                    (mu4e-reply-to-address . "msabate@suse.com")
                    (mu4e-drafts-folder    . "/susecom/Drafts")
                    (mu4e-sent-folder      . "/susecom/Sent")
                    (mu4e-trash-folder     . "/susecom/Trash")))

          ;; suse.de
          ,(make-mu4e-context
            :name "desuse"
            :enter-func (lambda ()
                          (mu4e-message "Switching to suse.de")
                          (mssola-suse-smtp))
            :match-func (lambda (msg)
                          (when msg
                            (mu4e-message-contact-field-matches
                             msg :to "msabate@suse.de")))
            :vars '(
                    (user-mail-address     . "msabate@suse.de")
                    (user-full-name        . "Miquel Sabaté Solà")
                    (mu4e-reply-to-address . "msabate@suse.de")
                    (mu4e-drafts-folder    . "/susede/Drafts")
                    (mu4e-sent-folder      . "/susede/Sent")
                    (mu4e-trash-folder     . "/susede/Trash")))))

  ; If mu4e cannot figure things out, ask me.
  (setq mu4e-context-policy 'ask)
  (setq mu4e-compose-context-policy 'ask)

  ; Fill the `mu4e-user-mail-address-list' variable with the contexts.
  (setq mu4e-user-mail-address-list
        (delq nil
              (mapcar (lambda (context)
                        (when (mu4e-context-vars context)
                          (cdr (assq 'user-mail-address
                                     (mu4e-context-vars context)))))
                      mu4e-contexts)))

  ; Signature.
  (setq mu4e-compose-signature
        (concat
         "Miquel Sabaté Solà\n"
         "PGP: 4096R / 1BA5 3C7A C93D CA2A CFDF DA97 96BE 8C6F D89D 6565\n"))

  ; Sign outgoing emails
  (add-hook 'message-send-hook 'mml-secure-message-sign-pgpmime)

  ; Don't save message to Sent Messages, mbsync will take care of that.
  (setq mu4e-sent-messages-behavior 'delete)

  ;; To avoid UID clashes. See:
  ;; http://pragmaticemacs.com/emacs/fixing-duplicate-uid-errors-when-using-mbsync-and-mu4e/
  (setq mu4e-change-filenames-when-moving t)

  ; Miscellaneous settings.
  (setq mu4e-html2text-command "w3m -T text/html"
        mu4e-attachment-dir  "~/Downloads"
        mu4e-headers-date-format "%Y-%m-%d %H:%M"
        message-citation-line-format "%N @ %Y-%m-%d %H:%M %Z:\n"
        message-citation-line-function 'message-insert-formatted-citation-line
        message-kill-buffer-on-exit t
        mu4e-get-mail-command "mbsync -aqV"
        mu4e-update-interval 120
        mu4e-compose-dont-reply-to-self t
        mu4e-headers-skip-duplicates t
        mu4e-headers-include-related t
        mu4e-headers-auto-update t)

  ;; Setting up shortcuts that can be composed with mu4e commands. For
  ;; example, in this setup, pressin `ji` will jump to the inbox, and `ma`
  ;; will move an email to the `All Mail` folder.
  (setq mu4e-maildir-shortcuts
        '( ("/INBOX"  . ?i)
           ("/Sent"   . ?s)
           ("/Trash"  . ?t)
           ("/Drafts" . ?d)))

  ;; The headers to show in the headers list a pair of a field and its width,
  ;; with `nil' meaning 'unlimited' (better only use that for the last field.
  ;; These are the defaults:
  (setq mu4e-headers-fields
        '( (:date          .  18)
           (:mailing-list  .  15)
           (:from-or-to    .  20)
           (:subject       .  nil)))

  ; Show images
  (setq mu4e-view-show-images t
        mu4e-view-image-max-width 800)

  ; Use imagemagick, if available
  (when (fboundp 'imagemagick-register-types)
    (imagemagick-register-types))

  ; Webkit support for viewing HTML emails. See the following discussion:
  ; https://groups.google.com/forum/#!topic/mu-discuss/JqHEGycEyKI
  (defun my-mu4e-action-view-with-xwidget (msg)
    "View the body of the message inside xwidget-webkit."
    (unless (fboundp 'xwidget-webkit-browse-url)
      (mu4e-error "No xwidget support available"))
    (let* ((html (mu4e-message-field msg :body-html))
           (txt (mu4e-message-field msg :body-txt))
           (tmpfile (format "%s%x.html" temporary-file-directory (random t))))
      (unless (or html txt)
        (mu4e-error "No body part for this message"))
      (with-temp-buffer
        ;; simplistic -- but note that it's only an example...
        (insert (or html (concat "<pre>" txt "</pre>")))
        (write-file tmpfile)
        (xwidget-webkit-browse-url (concat "file://" tmpfile) t))))

  (add-to-list 'mu4e-view-actions
               '("xViewXWidget" . my-mu4e-action-view-with-xwidget) t)

  ; Spell check
  (add-hook 'mu4e-compose-mode-hook
            (lambda ()
              "My settings for message composition."
              (set-fill-column 99999)
              (flyspell-mode)))

    ; Restore some of my evil config.
    ; TODO: doesn't work
    (dolist (imap '(mu4e-main-mode-map mu4e-headers-mode-map mu4e-view-mode-map))
      (evil-define-key 'evil-mu4e-state imap
        "\C-h" 'evil-window-left
        "\C-l" 'evil-window-right
        "\C-j" 'evil-window-down
        "\C-k" 'evil-window-up
        "\M-p"  'helm-projectile-switch-project))

  ; Desktop notifications
  (use-package mu4e-alert
    :ensure t
    :config

    (mu4e-alert-set-default-style 'libnotify)
    (add-hook 'after-init-hook #'mu4e-alert-enable-notifications)
    (add-hook 'after-init-hook #'mu4e-alert-enable-mode-line-display)
    (setq mu4e-alert-interesting-mail-query
          "flag:unread AND NOT flag:trashed")
    (setq mu4e-alert-email-notification-types '(count)))

  ; Evil mode in mu4e
  (with-eval-after-load 'evil
    (use-package evil-mu4e
      :ensure t
      :config)))

(setq mu4e-update-interval 120)

(provide 'init-mu4e)
;;; init-mu4e.el ends here