summaryrefslogtreecommitdiff
path: root/mail.el
blob: dd35b4d94a442d666f7bfd9fabb031a4c4afc2b3 (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
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

(use-package "/usr/local/share/emacs/site-lisp/mu/mu4e" 'mu4e
  (setq mu4e-maildir-list (list "/Users/durand/mbsync"))
  (setq user-mail-address "durand@jsdurand.xyz")
  (setq mu4e-completing-read-function #'completing-read)
  (setq message-confirm-send t)
  (setq mu4e~update-buffer-height 5)

  (setq mu4e-context-policy 'pick-first)
  (setq mu4e-attachment-dir "~/Downloads")
  (setq mu4e-mu-binary "/usr/local/bin/mu")
  
  (setq mu4e-view-use-gnus t)
  (setq mu4e-confirm-quit nil)
  ;; mbsync works a lot better!
  (setq mu4e-get-mail-command
        "mbsync mymail-inbox mymail-sent")
  (setq mu4e-change-filenames-when-moving t)
  (setq mu4e-view-show-addresses t)     ; show full addresses!
  (setq mu4e-view-show-images t)
  (setq mu4e-sent-messages-behavior 'delete)
  (setq mu4e-use-fancy-chars t)
  (setq message-send-mail-function 'smtpmail-send-it
        smtpmail-stream-type 'starttls
        smtpmail-default-smtp-server "smtp.gmail.com"
        smtpmail-smtp-server "smtp.gmail.com"
        smtpmail-smtp-service 587)

  (define-key mu4e-main-mode-map (vector ?q) #'bury-buffer)
  (define-key mu4e-main-mode-map (vector ?u) #'mu4e-update-mail-and-index)

  (add-to-list
   'mu4e-bookmarks
   (make-mu4e-bookmark
    :name "week no trash no archive"
    :query "date:7d..now AND NOT maildir:/trash AND NOT maildir:/archive"
    :key ?d))

  (add-to-list
   'mu4e-bookmarks
   (make-mu4e-bookmark
    :name "week starred"
    :query "date:7d..now AND maildir:/suivis"
    :key ?s))

  ;; (setq mu4e-maildir-shortcuts
  ;;       '(("/INBOX" . ?i)
  ;;         ("/archive" . ?a)
  ;;         ("/suivis" . ?s)
  ;;         ("/drafts" . ?d)))

  (setq mu4e-sent-folder "/mymail/Sent")
  
  ;; (setq mu4e-contexts
  ;;       (list
  ;;        (make-mu4e-context
  ;;         :name "Student"
  ;;         :enter-func (lambda () (mu4e-message "Entering Student context"))
  ;;         :leave-func (lambda () (mu4e-message "Leaving Student context"))
  ;;         ;; we match based on the contact-fields of the message
  ;;         :match-func
  ;;         (lambda (msg)
  ;;           (when msg
  ;;             (or
  ;;              (mu4e-message-contact-field-matches msg :from "tan\\|mlh\\|hsialc\\|tingyu.lee\\|tingyulee")
  ;;              (mu4e-message-contact-field-matches msg :to "tan\\|mlh\\|hsialc\\|tingyu.lee\\|tingyulee"))))
  ;;         :vars '((user-mail-address . "mmemmew@gmail.com")
  ;;                 (user-full-name . "李俊緯")
  ;;                 (mu4e-compose-signature . "生 俊緯")
  ;;                 (mu4e-sent-folder . "/gmail/sent")
  ;;                 (smtpmail-smtp-user . "mmemmew@gmail.com")
  ;;                 (smtpmail-local-domain . "gmail.com")
  ;;                 (smtpmail-default-smtp-server . "smtp.gmail.com")
  ;;                 (smtpmail-smtp-server . "smtp.gmail.com")
  ;;                 (smtpmail-smtp-service . 587)))
  ;;        (make-mu4e-context
  ;;         :name "NCTS"
  ;;         :enter-func (lambda () (mu4e-message "Switch to the NCTS context"))
  ;;         :leave-func (lambda () (mu4e-message "Leave NCTS context"))
  ;;         ;; no leave-func
  ;;         ;; we match based on the maildir of the message
  ;;         ;; this matches maildir /Arkham and its sub-directories
  ;;         :match-func (lambda (msg)
  ;;                       (when msg
  ;;                         (or
  ;;                          (mu4e-message-contact-field-matches msg :to "chunweilee@ncts.ntu.edu.tw")
  ;;                          (mu4e-message-contact-field-matches msg :from "chunweilee@ncts.ntu.edu.tw"))))
  ;;         :vars '((user-mail-address . "chunweilee@ncts.ntu.edu.tw")
  ;;                 (user-full-name . "李俊緯")
  ;;                 (mu4e-compose-signature .
  ;;                                         (concat
  ;;                                          "Sincerely Yours,\n"
  ;;                                          "俊緯"))))
  ;;        (make-mu4e-context
  ;;         :name "Durand"
  ;;         :enter-func (lambda () (mu4e-message "Entering Durand context"))
  ;;         :leave-func (lambda () (mu4e-message "Leaving Durand context"))
  ;;         ;; we match based on the contact-fields of the message
  ;;         :match-func (lambda (msg)
  ;;                       (when msg
  ;;                         (or
  ;;                          (mu4e-message-contact-field-matches msg :to "mmemmew@gmail.com")
  ;;                          (mu4e-message-contact-field-matches msg :from "mmemmew@gmail.com"))))
  ;;         :vars '((user-mail-address . "durand@jsdurand.xyz")
  ;;                 (user-full-name . "Durand")
  ;;                 (mu4e-compose-signature . "Sévère Durand")
  ;;                 (mu4e-sent-folder . "/gmail/sent")
  ;;                 (smtpmail-smtp-user . "mmemmew@gmail.com")
  ;;                 (smtpmail-local-domain . "gmail.com")
  ;;                 (smtpmail-default-smtp-server . "smtp.gmail.com")
  ;;                 (smtpmail-smtp-server . "smtp.gmail.com")
  ;;                 (smtpmail-smtp-service . 587)))))
  )

;; (setq smtpmail-debug-info t)
;; (setq smtpmail-debug-verb t)

;;; message mode

(require 'message)

(defun durand-set-fill-column ()
  "Set the fill-column to an appropriate value."
  (setq fill-column 70))

(add-hook 'message-mode-hook #'durand-set-fill-column)

;;; Quote a message

(defvar durand-quote-history nil
  "The history of names whom I have quoted.")

(autoload 'durand-take "common")

(defun durand-quote-message (&optional arg)
  "Quote the current message body, and add some meta-data.
Actually this only saves the quote text in the `kill-ring', and
does not modify the buffer directly.

If region is activated, only quote the text in the region.

If ARG is (list 4), query for the time and the name to quote.

If ARG is non-nil, and not equal to (list 4), omit the line about
the time and the one who says the quote; only query the author
and produce the quoted message."
  (interactive
   (list current-prefix-arg)
   gnus-summary-mode gnus-article-mode message-mode)
  (cond
   ((apply #'provided-mode-derived-p
           major-mode (command-modes #'durand-quote-message)))
   ((user-error
     (concat
      "The command only works in the following modes: "
      (mapconcat
       (lambda (obj) (format "%S" obj))
       (command-modes #'durand-quote-message)
       " ")))))
  (cond
   ((derived-mode-p 'gnus-summary-mode)
    (set-buffer gnus-article-buffer)))
  (let* ((date
          (cond
           ((and (listp arg)
                 (null (cdr arg))
                 (eq (car arg) 4))
            (read-string "Time of the quote: "))
           (arg nil)
           ((save-match-data
              (save-restriction
                (mail-narrow-to-head)
                (mail-fetch-field "Date"))))))
         (date
          (cond
           (date
            (format-time-string
             "%a, %e %b %Y %H:%M:%S %z"
             (let* ((time (parse-time-string date))
                    (first-six
                     (mapcar
                      (lambda (element) (or element 0))
                      (durand-take 6 time)))
                    (remain (nthcdr 6 time)))
               (encode-time (append first-six remain)))
             (current-time-zone)))))
         (author
          (cond
           (arg
            (read-string
             "Author of the quote: "
             nil 'durand-quote-history
             durand-quote-history t))
           ((save-match-data
              (save-restriction
                (mail-narrow-to-head)
                (mail-fetch-field "From"))))))
         (author-name
          (save-match-data
            (cond
             ((string-match
               (rx "\"" (group (1+ anychar)) "\" <")
               author)
              (car (split-string (match-string 1 author))))
             ((string-match
               (rx (group (1+ (not (any space "<")))) " <")
               author)
              (match-string 1 author))
             ((string-match
               (rx "<" (group (1+ (not (any "<>")))) ">")
               author)
              (match-string 1 author))
             (author))))
         (body
          (cond
           ((use-region-p)
            (buffer-substring-no-properties
             (region-beginning) (region-end)))
           ((save-match-data
              (save-restriction
                (widen)
                (article-goto-body)
                (buffer-substring-no-properties
                 (point) (point-max))))))))
    (kill-new
     (concat
      (cond
       (date
        (format
         ">>>>> Le %s, %s a dit:\n\n" date author)))
      (mapconcat
       (lambda (line)
         (cond
          ;; paranoia
          ((not (stringp line)) (format "%S" line))
          ((string-match-p
            (rx bos
                (zero-or-more space)
                (or
                 (seq
                  (1+ (not (any ">")))
                  "> ")
                 (1+ (literal ">"))
                 eos))
            line)
           line)
          ((concat "    " author-name "> " line))))
       (split-string body (string #xa))
       (string #xa))))
    (message "Quoted the mail.  To use it just yank it.")))

(define-key message-mode-map (vector 3 ?q) #'durand-quote-message)
(define-key gnus-summary-mode-map (vector 3 ?q) #'durand-quote-message)
(define-key gnus-article-mode-map (vector 3 ?q) #'durand-quote-message)

;;; Fix the quotation style

(defun durand-fix-quotation (&optional just-use-durand)
  "Fix the quotation style.
If JUST-USE-DURAND is non-nil, just use \"Durand\" as the
person."
  (interactive "P")
  (save-match-data
  (save-excursion
    (let* ((beg (region-beginning))
           (end (region-end))
           (end (progn (goto-char end) (point-marker)))
           (person (cond (just-use-durand "Durand")
                         ((read-string "Name: ")))))
      (goto-char beg)
      (while (< (point) (marker-position end))
        (cond
         ((re-search-forward
           (rx-to-string '(seq ">>" eol))
           (marker-position end)
           t)
          (replace-match "" nil nil nil 0)))
        (forward-line 1))
      (goto-char beg)
      (while (< (point) (marker-position end))
        (cond ((looking-at (rx-to-string
                            '(seq bol (zero-or-more (in space))
                              (group
                               (one-or-more
                                ">>"
                                (one-or-more (in space)))
                               (group (one-or-more (not (in ">\n"))))
                               (one-or-more
                                ">>"
                                (one-or-more (in space)))))
                            t))
               (let ((middle-text (match-string 2)))
                 (replace-match
                  (format "%s> %s" person middle-text) t t nil 1)))
              ((looking-at (rx-to-string
                            '(seq bol (one-or-more (in space))
                              (group
                               (one-or-more
                                ">>"
                                (one-or-more (in space)))))
                            t))
               (replace-match (format "%s> " person) t t nil 1)))
        (forward-line 1))
      (fill-region beg (marker-position end))
      (message "Processing...Done")))))

(define-key message-mode-map (vector 3 ?f) #'durand-fix-quotation)
(define-key gnus-summary-mode-map (vector 3 ?f) #'durand-fix-quotation)
(define-key gnus-article-mode-map (vector 3 ?f) #'durand-fix-quotation)

;;; update mails without mu4e

(defun durand-mail-process-output (output)
  "Normalize the OUTPUT emitted by mu4e."
  (let ((splitted (split-string output (rx-to-string (list 'any ?
 ?\n ?\r) t) t)))
    (or (car (last splitted)) "")))

(defun durand-mail-update-filter (process string)
  "Insert the ouput STRING into the buffer of PROCESS for updating \
mails."
  (let ((buffer (process-buffer process))
        (output (durand-mail-process-output string)))
    (cond
     ((buffer-live-p buffer)
      (display-buffer
       buffer
       (list (list #'display-buffer-in-side-window)
             (cons 'side 'bottom)
             (cons 'window-height
                   #'durand-fit-window-to-buffer-with-max)))
      (with-current-buffer buffer
        (cond
           ((and (stringp output)
                 (not (string= output "")))
            (delete-region (point-min) (point-max))
            (insert output)
            (set-marker
             (process-mark process) (point-max) buffer))))))))

(defun durand-mail-update-sentinel (process status)
  "Handle STATUS changes of the PROCESS for updating mails."
  (cond
   ((string= status "finished\n")
    (let ((buffer (process-buffer process)))
      (cond
       ((buffer-live-p buffer)
        (quit-window nil (get-buffer-window buffer))))))))

(defvar durand-mail-update-command
  (list "mbsync" "mymail-inbox" "mymail-sent")
  "Command to update mails.")

(defvar durand-mail-update-buffer "*mail*"
  "Buffer to display progess of updating mails.")

(defun durand-mail-update (&optional foreground-p)
  "Update mails.
If FOREGROUND-P is non-nil, also display the progress in a
separate buffer."
  (interactive (list t))
  (make-process
   :name "durand-mail-update"
   :command durand-mail-update-command
   :filter (cond (foreground-p #'durand-mail-update-filter))
   :sentinel (cond (foreground-p #'durand-mail-update-sentinel))
   :buffer (cond
            (foreground-p
             (let ((buffer
                    (get-buffer-create durand-mail-update-buffer)))
               (buffer-disable-undo buffer)
               buffer)))))

(defvar durand-mail-update-timer nil
  "A timer to automatically update mail in the background.")

(defun durand-mail-cancel-timer ()
  "Cancel `durand-mail-update-timer'."
  (cancel-timer durand-mail-update-timer))

(cond
 ((memq #'durand-mail-update
        (mapcar #'timer--function timer-list)))
 ((setq
   durand-mail-update-timer
   (run-with-timer 0 (* 60 30) #'durand-mail-update))))