summaryrefslogtreecommitdiff
path: root/.config/emacs/gnus/init.el
blob: 5775d518e7b3fb8ccc86d53bd26ae8c79a6c59a6 (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
;;; -*- lexical-binding: t -*-

;;; Externalities.

;; user-full-name       from /etc/passwd; set with chfn(1).
;; user-mail-address    from EMAIL variable; set with ~/.profile,
;;                      ~/.xsessionrc, DE's convention-du-jour.

;; ~/.authinfo.gpg:
;;      machine imap.gmail.com login LOGIN password PASSWORD port 993
;;      machine smtp.gmail.com login LOGIN password PASSWORD port 587

;;; Þe Olde Setq.
(setq gnus-select-method
      '(nnimap "gmail"
               ;; TODO: try auth-source-xoauth2-plugin:
               ;; (nnimap-authenticator xoauth2)
               (nnimap-address "imap.gmail.com")
               (nnimap-server-port 993)
               (nnmail-expiry-target "nnimap+gmail:[Gmail]/Trash")
               (nnmail-expiry-wait immediate))
      gnus-secondary-select-methods
      '((nntp "archive.lwn.net")
        (nntp "news.gmane.io"))

      smtpmail-smtp-server "smtp.gmail.com"
      smtpmail-smtp-service 587

      ;; Archival of sent messages.
      gnus-gcc-mark-as-read t
      ;; The next setting makes the previous one useless; keeping both
      ;; for now because I'm not sure which I'll settle for.
      gnus-message-archive-group nil

      ;; Groups.
      gnus-group-uncollapsed-levels 2

      ;; Summary.
      gnus-auto-center-summary nil
      gnus-prompt-before-saving t
      gnus-summary-line-format "%*%U%R  %-16,16&user-date; %B%-23,23f  %s\n"
      gnus-summary-dummy-line-format "                     ╭                          %S\n"
      gnus-summary-make-false-root 'dummy
      gnus-sum-thread-tree-root "╭ "
      gnus-sum-thread-tree-false-root "┬ "
      gnus-sum-thread-tree-single-indent "  "
      gnus-sum-thread-tree-indent " "
      gnus-sum-thread-tree-single-leaf "╰► "
      gnus-sum-thread-tree-leaf-with-other "├► "
      gnus-sum-thread-tree-vertical "│"
      gnus-thread-sort-functions
      '(gnus-thread-sort-by-number
        (not gnus-thread-sort-by-most-recent-date))
      gnus-user-date-format-alist '(((gnus-seconds-today)
                                     . "%H:%M")
                                    ((+ 86400 (gnus-seconds-today))
                                     . "Yesterday %H:%M")
                                    ((* 6 86400)
                                     . "%a %H:%M")
                                    ((gnus-seconds-month)
                                     . "%a %d")
                                    ((gnus-seconds-year)
                                     . "%b %d")
                                    (t
                                     . "%F"))
      ;; Articles.
      gnus-cite-parse-max-size nil
      gnus-header-face-alist
      '(("From" nil gnus-header-from)
        ("Subject" nil gnus-header-subject)
        ("Date" nil eighters-date)
        ("Newsgroups:.*," nil gnus-header-newsgroups)
        ("" gnus-header-name gnus-header-content))
      gnus-sorted-header-list
      (list
       ;; What, when.
       "^Subject:" "^Summary:" "^Keywords:" "^Date:"
       ;; Who.
       "^From:" "^Organization:" "^Followup-To:" "^To:" "^Cc:" "^Newsgroups:")
      gnus-treat-display-smileys nil
      ;; Do not fill anything; let visual-line-mode wrap text.
      ;;; NB: for format=flowed, there is no setting to say "un-fill
      ;;; flowed lines", so we *enable* filling, setting an absurd
      ;;; line length limit, in order to un-fill flowed lines.
      fill-flowed-display-column most-positive-fixnum
      mm-fill-flowed t
      ;;; More long-line-folding settings.
      gnus-article-unfold-long-headers t
      gnus-treat-fill-article nil
      gnus-treat-fill-long-lines nil
      gnus-treat-fold-headers nil)

;;; Window configurations.

(defvar my/gnus-side-by-side-threshold 160)

(gnus-add-configuration
 '(article
   (if (>= (frame-width) my/gnus-side-by-side-threshold)
       '(horizontal 1.0
                    (summary 1.0 point)
                    (article 80))
     '(vertical 1.0
                (summary 0.25 point)
                (article 1.0)))))

(dolist (buf-name '(forward reply reply-yank))
  (gnus-add-configuration
   `(,buf-name
     (if (>= (frame-width) my/gnus-side-by-side-threshold)
         '(vertical 1.0
                    (summary 0.25)
                    (horizontal 1.0
                                (article 0.5)
                                (message 1.0 point)))
       '(vertical 1.0
                  (summary 0.2)
                  (article 0.2)
                  (message 1.0 point))))))

;;; Summary tweaks.

(defun my/gnus-toggle-article-wrap ()
  (interactive)
  (with-current-buffer gnus-article-buffer
    (visual-line-mode 'toggle)))

(defun my/gnus-summary-tweak-keys ()
  (keymap-local-set "C-c d v" 'my/gnus-toggle-article-wrap))

(add-hook 'gnus-summary-mode-hook 'my/gnus-summary-tweak-keys)

;; message-subject-re-regexp is used both in Gnus summary buffers to
;; detect and elide similar subjects in a thread, and by message mode
;; when replying, to determine what to strip from the subject.
;;
;; Some MUAs add cruft to the subject, turning "Re: bug#123: foobar"
;; into "RE: [External] : Re: bug#1234: foobar", which Debbugs will
;; then turn into "bug#1234: [External] : Re: bug#1234: foobar".
;;
;; The only way I can find to tell the Gnus summary code to
;; canonicalize all that cruft away is by tweaking this regexp, but
;; setting its global value causes message-mode to elide stuff it
;; shouldn't when crafting subjects.  Therefore, chase down the best
;; Gnus hook for the job, and set the regexp locally.
(defun my/gnus-reply-prefixes ()
  (mapcan (lambda (prefix) (list prefix (upcase prefix) (capitalize prefix)))
          '("re" "aw" "sv" "fw" "fwd")))

(setq my/gnus-summary-normalize-subject
      (rx-to-string
       `(seq bol
             (+ (or (seq word-start (or ,@(my/gnus-reply-prefixes)) word-end)
                    (seq "bug#" (+ digit))
                    (seq "[" (or "External" "SPAM UNSURE") "]"))
                (? (* space) ":") (* space)))))

(add-hook 'gnus-summary-generate-hook
          (lambda ()
            (setq-local message-subject-re-regexp
                        my/gnus-summary-normalize-subject)))

(let* ((initials (mapconcat (lambda (s) (substring s 0 1))
                            (split-string user-full-name)
                            nil))
       (sent-prefix (format "%s → " initials)))
  (setq gnus-summary-to-prefix sent-prefix
        gnus-summary-newsgroup-prefix sent-prefix))

;;; Article tweaks.

(defun my/gnus-article-eschew-tables ()
  ;; I set shr-fill-text to nil because I prefer letting
  ;; visual-line-mode manage wrapping.  Unfortunately, many HTML
  ;; emails rely on <table>s for layouts, and rendering can get ugly.
  ;; Work around this by treating <table> & children as any other
  ;; <div>.
  (make-local-variable 'shr-external-rendering-functions)
  (pcase-dolist (`(,tag . ,shr-function)
                 '((table . shr-tag-div)
                   (thead . shr-tag-div)
                   (tbody . shr-tag-div)
                   (tr    . shr-tag-ul)
                   (th    . shr-tag-li)
                   (td    . shr-tag-li)))
    (setf (alist-get tag shr-external-rendering-functions) shr-function)))

(defun my/gnus-article-has-html ()
  ;; Hard to tell the difference between
  ;; * the variable `gnus-article-mime-handles',
  ;; * the function `gnus-article-mime-handles',
  ;; * the variable `gnus-article-mime-handle-alist'.
  ;;
  ;; Stealing debbugs.el's patch-finding logic.
  (seq-some
   (lambda (handle)
     (string= (mm-handle-media-type (cdr handle)) "text/html"))
   (gnus-article-mime-handles)))

(defun my/gnus-article-should-wrap ()
  (save-excursion
    (message-goto-body)
    (let ((should-wrap nil)
          (has-html (my/gnus-article-has-html)))
      (while-let (((not should-wrap))
                  ((not (eobp)))
                  (current-line (thing-at-point 'line)))
        (setq should-wrap
              (and
               ;; The line is bigger than the target width.
               (> (length current-line)
                  (window-width (get-buffer-window gnus-article-buffer)))
               ;; The line is not boring (citation, diff addition/removal).
               (not (string-match-p "\\`[>+-]" current-line))
               ;; Lines that start with spaces are boring, except in
               ;; HTML parts: those are choked with <table> tags that
               ;; shr left-pads with spaces.
               ;; NB: HAS-HTML is a naive heuristic: we are assuming
               ;; that "any text/html part is present" means "we are
               ;; looking at this text/html part".
               (or (not (string-match-p "\\` " current-line)) has-html)))
        (forward-line))
      should-wrap)))

(defun my/gnus-article-wrap-maybe ()
  ;; Enable visual-line-mode when it helps, i.e. when the message has
  ;; long lines that are not part of citations nor patches.
  (with-current-buffer gnus-article-buffer
   (visual-line-mode
    (unless (my/gnus-article-should-wrap) -1))))

;; Article setup is tricky.  In order, `gnus-article-prepare'
;;
;; (1) calls `gnus-article-setup-buffer', which
;;     (a) calls `gnus-article-mode', which runs
;;         gnus-article-mode-hook,
;;     (b) sets truncate-lines from gnus-article-truncate-lines,
;;
;; (2) calls `gnus-display-mime', which may end up calling `mm-shr';
;;     this can call `shr-tag-table', which turns truncate-lines on
;;     unconditionally.
;;
;; (3) runs gnus-article-prepare-hook.
;;
;; Gnus will only run (1a) once, and skip that step when it re-uses
;; the same *Article* buffer for subsequent articles.  So for our
;; purposes, we need to
;;
;; (Ⅰ) hack the shr rendering functions in mode-hook, before `mm-shr'
;;     gets to work.
;; (Ⅱ) call `visual-line-mode' (if needed) in prepare-hook, after
;;     truncate-lines has been set.

(add-hook 'gnus-article-mode-hook 'my/gnus-article-eschew-tables)
(add-hook 'gnus-article-prepare-hook 'my/gnus-article-wrap-maybe)

;;; MIME display.
(defun my/mm-display-markdown-inline (handle)
  (mm-display-inline-fontify handle 'markdown-mode))

(with-eval-after-load 'mm-decode
  ;; bug-gnu-emacs:<jwvzfsnntlq.fsf-monnier+emacs@gnu.org>
  (setf (alist-get "text/markdown" mm-inline-media-tests nil nil 'equal)
        '(my/mm-display-markdown-inline)))

;;; Key bindings.
;;
;; m            compose
;;
;; Group buffer:
;;
;; L            list all groups
;; RET          view unread mail in group
;; C-u RET      view all mail in group
;; g            refresh
;; G G          search group
;;
;; Summary buffer:
;;
;; B m          move message to group
;; / N          fetch new
;; M-g          refresh (expire, move, fetch new, show unread)
;; C-u M-g      refresh (expire, move, fetch new, show all)
;; C-u g        show raw, undecoded message source; g to decode
;; T h          collapse (hide) thread
;; T s          expand (show) thread
;; T k, C-M-k   mark thread as read
;; M-1 T k      mark thread as unread
;; r            reply
;; R            reply (quoting)
;; S w          reply-all
;; S W          reply-all (quoting)
;; C-c C-f      forward
;; d            mark read
;; M-u          clear marks (≡ mark unread)
;; E            expire
;; #            toggle mark for next action
;; M-#, M P u   unmark for next action
;;
;;      Draft summary buffer:
;;
;;      D e     edit draft
;;
;; Article buffer:
;;
;; o            save attachment at point
;; K b          add button for inlined MIME part
;;
;; Composing:
;;
;; C-c C-c      send
;; C-c C-a      attach
;; C-c C-f s    change the subject (append "was:")
;;
;;; FAQ.
;;
;; - how to see *all mails*, not just unread?
;;      - C-u RET
;;
;; - how to do something on a bunch of mail matching a pattern?
;;      - M P R     ; mark all mails with subjects matching regexp
;;      - M-& <x>   ; do <x> on all marked mails
;;
;; - how to delete mail?
;;      - E to mark as expired
;;      - C-u M-g to refresh
;;
;; - how to remove groups deleted on the IMAP server?
;;      - b to iterate over "bogus" groups and remove them
;;
;; - how to list most-recent mails on top?
;;      - cf. gnus-thread-sort-functions
;;
;; - how to close a mail without going back to the group list?
;;      - = to make summary full-screen
;;
;; - how to get contact completion?
;;      - install ebdb from GNU ELPA
;;      - or just use message-mail-alias-type
;;
;; - how to refresh?
;;      - summary buffer:
;;          - / N (fetch new)
;;          - M-g (expire, move, fetch & redisplay)
;;      - group buffer: g
;;
;; - what do all those letters mean?
;;   (info "(gnus) Marking Articles")
;;      - O     old ≡ read during previous session
;;      - R     just read
;;      - r     manually marked as read
;;      - A     answered
;;      - E     expirable
;;      - G     cancelled (e.g. moved somewhere else)
;;      - .     unseen
;;
;; - how to subscribe to mailing lists?
;;      - to browse an NNTP server, either
;;          - hit B in the group buffer, then nntp *some server*
;;          - or add (nntp "*some server*") to gnus-secondary-methods
;;      - over the list: u
;;
;;; TODO.
;;
;; - gnus-summary-line-format (📎 for attachments)
;;
;; - how to archive mails and news locally?
;;
;; - describe-key is mostly useless in article mode:
;;   > X runs the command gnus-article-read-summary-keys
;;
;; - detect possibly missing attachments from keywords