summaryrefslogtreecommitdiffstats
path: root/lisp/ravi-init-mu.el
blob: 2a204ccf608452fee3a190fa69568f2e37fef3d8 (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
;;; ravi-init-mu.el --- mail

;; Copyright (C) 2014

;; Author:  <ravi@nero.lan>
;; Keywords:

;; 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 <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Set up mu4e conditionally
;;   - actual account information should be in local.el
;;   - requires definition of ravi/setup-mu4e-locals

;;; Code:

(setq ravi/epg-gpg-program "gpg2")
(setq epg-gpg-program ravi/epg-gpg-program)

(defvar ravi/use-mu-for-email nil
  "Use mu for email")
(defvar ravi/mu4e-update-interval 1800  ; 30 minutes
  "Basic update interval; all other update intervals are multiples")

(setq message-citation-line-format "On %a %Y-%m-%d %l:%M:%S%p %Z, %f wrote:"
      message-citation-line-function 'message-insert-formatted-citation-line)

(when ravi/use-mu-for-email
  (use-package mu4e
    :load-path (lambda () (ravi/emacs-file "site-lisp/mu/mu4e"))
    :commands mu4e
    :init
    (progn
      (add-to-list 'Info-additional-directory-list (ravi/emacs-file "site-lisp/mu/mu4e"))
      (defun ravi/switch-to-mu4e ()
        (interactive)
        (let ((buf (or (and (boundp 'mu4e~headers-buffer-name)
                             (get-buffer mu4e~headers-buffer-name))
                       (get-buffer "*mu4e-main*"))))
          (if buf
              (if (get-buffer-window buf t)
                  (select-window (get-buffer-window buf t))
                (switch-to-buffer buf))
            (mu4e))))
      (bind-key "C-'" 'ravi/switch-to-mu4e)
      )
    :config
    (progn

      ;; Basic identity for sending mail
      (require 'smtpmail)
      (setq send-mail-function 'smtpmail-send-it)
      (setq message-send-mail-function 'smtpmail-send-it)

      ;; Set up accounts
      (when (fboundp 'ravi/setup-mu4e-locals)
        (ravi/setup-mu4e-locals))

      (setq mu4e-compose-dont-reply-to-self t)
      (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)))

      (setq smtpmail-smtp-service 587)

      ;; Use async method of sending email, if possible
      (use-package async
        :config
        (progn
          (require 'smtpmail-async)
          ;; To do: set this from ravi/epg-gpg-program
          (add-hook 'async-smtpmail-before-send-hook (lambda () (setq epg-gpg-program "gpg2")))
          (setq send-mail-function 'async-smtpmail-send-it)
          (setq message-send-mail-function 'async-smtpmail-send-it)
          )
        :ensure t)

      ;; Basic mu4e configuration parameters
      (setq mu4e-mu-binary (ravi/emacs-file "site-lisp/mu/mu/mu"))
      (setq mu4e-maildir "~/.mail")

      ;; Poll accounts only as often as necessary
      (defvar ravi/mu4e-get-mail-attempts 0
        "Number of attempts so far to get mail")
      (defun ravi/check-whether-to-get-mail-for-account (context)
        (let* ((account-interval (when (mu4e-context-vars context)
                                   (assq 'ravi/account-update-interval (mu4e-context-vars context))))
               (account-update-interval (if account-interval (cdr account-interval) 1)))
          (if (and (> account-update-interval 0) (= (% ravi/mu4e-get-mail-attempts account-update-interval) 0))
              (concat " " (cdr (assq 'ravi/account-name (mu4e-context-vars context))))
            "")))
      (defun ravi/get-mu4e-get-mail-command ()
        "Figure out arguments to mbsync"
        (let ((ravi/all-channels (mapconcat 'ravi/check-whether-to-get-mail-for-account
                                            mu4e-contexts "")))
          (setq ravi/mu4e-get-mail-attempts (1+ ravi/mu4e-get-mail-attempts))
          (if (= (length ravi/all-channels) 0)
              "true"                    ;; do nothing command
            (concat "mbsync -q -q" ravi/all-channels)
            )))
      (add-hook 'mu4e-update-pre-hook
                (lambda ()
                  (setq mu4e-get-mail-command (ravi/get-mu4e-get-mail-command))
                  (message "Get mail command: %s" mu4e-get-mail-command)))
      ;(message "%d: '%s'" ravi/mu4e-get-mail-attempts (ravi/get-mu4e-get-mail-command))
      ;; Default update command if something goes wrong
      (setq mu4e-get-mail-command "mbsync -a -q -q")
      (setq mu4e-update-interval ravi/mu4e-update-interval)
      (setq mu4e-change-filenames-when-moving t)

      ;; User interface
      (setq mu4e-html-renderer 'w3m)
      (setq mu4e-html2text-command "w3m -T text/html")
      (setq mu4e-view-show-images t)
      (setq mu4e-view-show-addresses t)
      (when (boundp 'ravi/mu4e-maildir-shortcuts)
        (setq mu4e-maildir-shortcuts ravi/mu4e-maildir-shortcuts))

      (add-to-list 'mu4e-view-actions
                   '("ViewInBrowser" . mu4e-action-view-in-browser) t)

      ;; Show summary of all folders
      (use-package mu4e-maildirs-extension
        :config
        (progn
          (setq mu4e-maildirs-extension-custom-list
                (-remove (lambda (f) (s-contains? "/[Gmail]" f t)) (mu4e-get-maildirs)))
          (mu4e-maildirs-extension))
        :ensure t)

      ;; Display desktop notifications
      (use-package mu4e-alert
        :config
        (progn
          (mu4e-alert-set-default-style 'libnotify)
          (mu4e-alert-disable-mode-line-display)
          (mu4e-alert-enable-notifications))
        :ensure t)

      (setq message-citation-line-function 'message-insert-formatted-citation-line)
      (setq message-kill-buffer-on-exit t)
      (setq mu4e-compose-signature-auto-include nil)
      (bind-key "C-c C-d" 'ravi/erase-from-point mu4e-compose-mode-map)

      ;; Allow attaching files from dired
      (require 'gnus-dired)
      ;; make the `gnus-dired-mail-buffers' function also work on
      ;; message-mode derived modes, such as mu4e-compose-mode
      (defun gnus-dired-mail-buffers ()
        "Return a list of active message buffers."
        (let (buffers)
          (save-current-buffer
            (dolist (buffer (buffer-list t))
              (set-buffer buffer)
              (when (and (derived-mode-p 'message-mode)
                         (null message-sent-message-via))
                (push (buffer-name buffer) buffers))))
          (nreverse buffers)))
      (setq gnus-dired-mail-mode 'mu4e-user-agent)
      (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)

      (when ravi/use-helm-instead-of-ido
        (add-to-list 'helm-find-files-actions
                     '("Attach files for mu4e" . helm-mu4e-attach) t)

        (defun helm-mu4e-attach (_file)
          (gnus-dired-attach (helm-marked-candidates))))

      (imagemagick-register-types)
      )
    )
  )

(use-package elfeed
  :bind (("C-x w" . elfeed))
  :config
  (progn
    (use-package elfeed-goodies
      :config
      (elfeed-goodies/setup)
      :ensure t)
  :ensure t)

(provide 'ravi-init-mu)
;;; ravi-init-mu.el ends here