summaryrefslogtreecommitdiff
path: root/lisp/init-gnus.el
blob: a7959a8180916808d992e7228eaa86143c7e9be7 (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
(require 'gnus)

(setq
 ;; Locations
 gnus-home-directory "~/.gnus.d"
 gnus-directory gnus-home-directory
 gnus-article-save-directory (expand-file-name "saved" gnus-directory)
 gnus-kill-files-directory (expand-file-name "scores" gnus-directory)
 ;; Startup files
 gnus-startup-file (expand-file-name "newsrc" gnus-home-directory)
 gnus-init-file (expand-file-name "gnus" gnus-home-directory)
 gnus-save-newsrc-file nil
 gnus-read-newsrc-file nil
 ;; Select methods
 imap-shell-program "/usr/lib/dovecot/imap -c ~/.dovecotrc"
 gnus-select-method '(nnimap "dov" (nnimap-stream shell))
 gnus-secondary-select-methods '((nntp "news.gmane.org"))
 ;; Groups
 gnus-topic-display-empty-topics nil
 gnus-group-list-inactive-groups nil
 ;; Messages
 message-send-mail-function 'message-send-mail-with-sendmail
 sendmail-program "/usr/bin/msmtp"
 message-sendmail-envelope-from 'header
 gnus-gcc-mark-as-read t
 message-citation-line-function 'message-insert-formatted-citation-line
 message-citation-line-format "%f wrote:"
 message-kill-buffer-on-exit t
 gnus-visible-headers '("^From" "^Subject" "^Date" "^To" "^Cc" "^User-Agent")
 gnus-confirm-mail-reply-to-news t
 footnote-section-tag ""
 ;; Threading
 gnus-thread-hide-subtree t
 gnus-thread-sort-functions '(gnus-thread-sort-by-most-recent-number)
 gnus-summary-line-format "%U%R %&user-date;%-20= %-15,15f  %B %S \n"
 gnus-sum-thread-tree-indent "  "
 gnus-sum-thread-tree-root "."
 gnus-sum-thread-tree-false-root "o "
 gnus-sum-thread-tree-single-indent ""
 gnus-sum-thread-tree-leaf-with-other "+-> "
 gnus-sum-thread-tree-vertical "| "
 gnus-sum-thread-tree-single-leaf "`-> "
 ;; Agent
 gnus-agent-go-online t
 gnus-agent-synchronize-flags t
 ;; Miscellaneous
 gnus-auto-select-next 'quietly
 mm-discouraged-alternatives '("text/html" "text/richtext")
 gnus-interactive-exit nil)

(defun km/sync-mail ()
  (interactive)
  (let ((bufname (get-buffer-create "*Mail sync*"))
        (default-directory "~")
        (process "mail-sync"))
    (with-current-buffer bufname
      (view-mode 1)
      (goto-char (point-max)))
    (display-buffer bufname)
    (if (process-live-p process)
        (message "Mail sync process is already running")
      (start-process process bufname km/sync-mail-cmd))))

(defvar km/sync-mail-cmd "sync-mail.sh"
  "Run sync mail script.")

;; http://www.emacswiki.org/emacs/GnusSync
(defun gnus-grace-exit-before-kill-emacs ()
  (if (and (fboundp 'gnus-alive-p)
           (gnus-alive-p))
      (let ((noninteractive t))
        (gnus-group-exit))))
(add-hook 'kill-emacs-hook 'gnus-grace-exit-before-kill-emacs)

;; Start in topic mode.
(add-hook 'gnus-group-mode-hook 'gnus-topic-mode)

(add-hook 'message-mode-hook
          '(lambda ()
             (flyspell-mode 1)))

(diminish 'mml-mode "Ml")

(defun km/follow-last-message-link ()
  "Follow link at bottom of message."
  (interactive)
  (km/gnus-end-of-article-buffer)
  (widget-backward 1)
  (widget-button-press (point)))

(define-key gnus-summary-mode-map
  (kbd "C-c j") 'km/follow-last-message-link)
(define-key gnus-article-mode-map
  (kbd "C-c j") 'km/follow-last-message-link)

(defun km/gnus-open-github-patch ()
  "Open patch from github email.
A new buffer with the patch contents is opened in another window."
  (interactive)
  (km/gnus-end-of-article-buffer)
  (search-backward "patch")
  (let ((url (thing-at-point 'url))
        (patch-buffer (generate-new-buffer "*gnus-github-patch*")))
    (switch-to-buffer-other-window patch-buffer)
    (url-insert-file-contents url)
    (diff-mode)
    (view-mode 1)))

(defun km/gnus-end-of-article-buffer ()
  "Move point to the end of the article buffer."
  ;; The next 3 lines are from `gnus-summary-widget-forward'.
  (gnus-summary-select-article)
  (gnus-configure-windows 'article)
  (select-window (gnus-get-buffer-window gnus-article-buffer))
  (goto-char (point-max)))

(require-package 'ido-gnus)
(define-key gnus-group-mode-map "j" 'ido-gnus-select-group)

(setq ido-gnus-num-articles 20)

(require 'notmuch)
(require 'org-gnus)

(define-key gnus-group-mode-map "GG" 'notmuch-search)
;; http://roland.entierement.nu/blog/2010/09/08/gnus-dovecot-offlineimap-search-a-howto.html
(defun km/notmuch-shortcut ()
  (define-key gnus-group-mode-map "GG" 'notmuch-search))

(defun km/notmuch-file-to-group (file)
  "Calculate the Gnus group name from the given file name."
  (let ((group (file-name-directory (directory-file-name (file-name-directory file)))))
    (setq group (replace-regexp-in-string ".*/mail/" "nnimap+dov:" group))
    (setq group (replace-regexp-in-string "/$" "" group))
    (if (string-match ":$" group)
        (concat group "INBOX")
      (replace-regexp-in-string ":\\." ":" group))))

(defun km/notmuch-goto-message-in-gnus ()
  "Open a summary buffer containing the current notmuch article."
  (interactive)
  (let ((group (km/notmuch-file-to-group (notmuch-show-get-filename)))
        (message-id (replace-regexp-in-string
                     "^id:" "" (notmuch-show-get-message-id))))
    (setq message-id (replace-regexp-in-string "\"" "" message-id))
    (if (and group message-id)
        (progn
    (switch-to-buffer "*Group*")
    (org-gnus-follow-link group message-id))
      (message "Couldn't get relevant infos for switching to Gnus."))))

(define-key notmuch-show-mode-map (kbd "C-c C-c") 'km/notmuch-goto-message-in-gnus)
(add-hook 'gnus-group-mode-hook 'km/notmuch-shortcut)

(setq notmuch-fcc-dirs nil)

;; Modified from
;; http://emacs-fu.blogspot.com/2008/12/some-simple-tricks-boxquote-footnote.html.
(defun km/snip-mail-quote (beg end &optional no-number quote-char)
  "Replace region lines with \"[n lines ...]\".
If NO-NUMBER is non-nil (or when called interactively with a
prefix argument), the number of lines is not added. The default
QUOTE-CHAR is \">\". Text following the snipped lines is placed
on a new line and the resulting paragraph is filled."
  (interactive "r\nP")
  (let ((nlines (count-lines beg end))
        (quote-char (or quote-char ">")))
    (delete-region beg end)
    (if no-number
        (insert (format "[...]"))
      (insert (format "[%d line%s ...]" nlines (if (= 1 nlines) "" "s"))))
    (search-backward "[")
    (unless (bolp)
      (newline))
    (search-forward "]")
    (unless (eolp)
      (newline)
      (insert quote-char)
      (just-one-space)
      (fill-paragraph))))

(define-key message-mode-map
  (kbd "C-c m s") 'km/snip-mail-quote)

(add-hook 'gnus-summary-mode-hook
          (lambda ()
            (gnus-define-keys gnus-summary-mode-map
              "c" km/gnus-catchup-and-goto-next-group
              "j" gnus-summary-next-unread-article
              ";" gnus-summary-universal-argument  ;; mutt's tag
              "e" gnus-summary-scroll-up)))

(add-hook 'gnus-group-mode-hook
          (lambda ()
            (gnus-define-keys gnus-group-mode-map
              "e" gnus-topic-select-group)))

(add-hook 'gnus-article-mode-hook
          (lambda ()
            (gnus-define-keys gnus-article-mode-map
              "e" shr-browse-url)))

;; Modified from http://www.xsteve.at/prg/gnus/

(defun km/gnus-select-or-bury ()
  "Start, select, or bury gnus."
  (interactive)
  (let ((bufname (buffer-name)))
    (if (or (string-equal "*Group*" bufname)
            (string-match "\*Summary" bufname)
            (string-match "\*Article" bufname))
        (km/bury-gnus)
      (if (get-buffer "*Group*")
          (km/unbury-gnus)
        (gnus-unplugged)))))

(defvar gnus-bury-window-configuration nil)

(defun km/unbury-gnus ()
  (let (dead-frame-p
        (windows-saved-p
         (and (boundp 'gnus-bury-window-configuration)
              gnus-bury-window-configuration)))
    (when windows-saved-p
      (unless (set-window-configuration gnus-bury-window-configuration)
        (setq dead-frame-p t)))
    (when (or dead-frame-p (not windows-saved-p))
      (switch-to-buffer "*Group*"))))

(defun km/bury-gnus ()
  (let ((buf nil)
        (bufname nil))
    (setq gnus-bury-window-configuration nil)
    (dolist (buf (buffer-list))
      (setq bufname (buffer-name buf))
      (when (or (string-equal "*Group*" bufname)
                (string-match "\*Summary" bufname)
                (string-match "\*Article" bufname))
        (unless gnus-bury-window-configuration
          (setq gnus-bury-window-configuration (current-window-configuration)))
        (delete-other-windows)
        (if (eq (current-buffer) buf)
            (bury-buffer)
          (bury-buffer buf))))))

;; From http://ivan.kanis.fr/ivan-gnus.el
(defun km/gnus-catchup-and-goto-next-group (&optional all)
  "Mark all articles in this group as read and select the next group.
If given a prefix, mark all articles, unread as well as ticked, as
read. Don't ask to confirm."
  (interactive "P")
  (save-excursion
    (gnus-summary-catchup all t))
  (gnus-summary-next-group))

;; From http://ivan.kanis.fr/ivan-gnus.el
(defadvice gnus-summary-next-group (before km/gnus-next-group activate)
  "Go to next group without selecting the first article."
  (ad-set-arg 0 t))

(define-prefix-command 'mail-map)
(global-set-key (kbd "C-x m") 'mail-map)

(define-key mail-map "g" 'gnus)
(define-key mail-map "b" 'km/gnus-select-or-bury)
(define-key mail-map "p" 'gnus-plugged)
(define-key mail-map "u" 'gnus-unplugged)
(define-key mail-map "s" 'km/sync-mail)
(define-key mail-map "n" 'notmuch-search)

(provide 'init-gnus)