u
[emacs-helper.git] / eh-gnus-common.el
blob3257b421e793e4ba1936aefd88884631d85a6683
1 ;;; eh-gnus-common.el --- Tumashu's gnus configuation file -*- lexical-binding: t; -*-
3 ;; * Header
4 ;; Copyright (c) 2008-2009, Andy Stewart
5 ;; 2011-2019, Feng Shu
7 ;; Author: Andy Stewartf <lazycat.manatee@gmail.com>
8 ;; Feng Shu <tumashu@163.com>
9 ;; URL: https://github.com/tumashu/emacs-helper
10 ;; Version: 0.0.1
12 ;; This file is not part of GNU Emacs.
14 ;;; License:
16 ;; This program is free software; you can redistribute it and/or
17 ;; modify it under the terms of the GNU General Public License
18 ;; as published by the Free Software Foundation; either version 3
19 ;; of the License, or (at your option) any later version.
21 ;; This program is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
31 ;;; Commentary:
33 ;; * 简介 :README:
34 ;; 这个文件是tumashu个人专用的emacs配置文件,emacs中文用户可以参考。
36 ;;; Code:
38 ;; * 代码 :code:
40 ;; 存储设置
41 (setq gnus-init-file "~/gnus/eh-gnus-personal.el")
42 (setq gnus-home-directory "~/gnus/")
43 (setq gnus-default-directory "~/gnus/")
44 (setq gnus-directory "~/gnus/")
45 (setq gnus-article-save-directory "~/gnus/Saved/")
46 (setq message-directory "~/gnus/Mail/")
47 (setq mm-default-directory "~/gnus/")
49 ;; Require
50 (require 'gnus)
51 (require 'gnus-cache)
52 (require 'gnus-cite)
53 (require 'gnus-agent)
54 (require 'gnus-search)
55 (require 'nntp)
56 (require 'mm-encode)
57 (require 'mm-decode)
58 (require 'rfc2047)
59 (require 'gnus-demon)
60 (require 'eww)
62 ;; EBDB 设置
63 (require 'ebdb)
64 (require 'ebdb-i18n-chn)
66 (make-directory "~/gnus/ebdb/" t)
67 (setq ebdb-sources (list "~/gnus/ebdb/default"))
68 (setq ebdb-search-transform-functions
69 (list #'pyim-cregexp-build))
71 ;; ebdb-complete 设置
72 (ebdb-complete-enable)
74 ;; ebdb-gnus 设置
75 (require 'ebdb-gnus)
76 (setq ebdb-gnus-window-size 0.25)
77 (setq ebdb-gnus-auto-update-p
78 (lambda ()
79 (message
80 (substitute-command-keys
81 (concat "EBDB: `\\[ebdb-mua-update-records]': 更新联系人,"
82 "`\\[ebdb-mua-snarf-article]': Snarf 联系人。"
83 "`\\[ebdb-mua-edit-sender-notes]': 备注联系人。")))
84 'existing))
86 (defun eh-ebdb-insinuate-gnus ()
87 (define-key gnus-summary-mode-map (kbd ";") nil)
88 (define-key gnus-article-mode-map (kbd ";") nil)
89 (define-key gnus-summary-mode-map (kbd "<f5>") #'ebdb-mua-update-records)
90 (define-key gnus-article-mode-map (kbd "<f5>") #'ebdb-mua-update-records)
91 (define-key gnus-summary-mode-map (kbd "<f6>") #'ebdb-mua-snarf-article)
92 (define-key gnus-article-mode-map (kbd "<f6>") #'ebdb-mua-snarf-article)
93 (define-key gnus-summary-mode-map (kbd "<f7>") #'ebdb-mua-edit-sender-notes)
94 (define-key gnus-article-mode-map (kbd "<f7>") #'ebdb-mua-edit-sender-notes))
96 (advice-add 'ebdb-insinuate-gnus :after #'eh-ebdb-insinuate-gnus)
98 ;; ebdb-message 设置
99 (require 'ebdb-message)
100 (setq ebdb-message-window-size 0.25)
101 (setq ebdb-message-auto-update-p ebdb-gnus-auto-update-p)
103 ;; 使用 notmush 搜索邮件
105 ;; Notmuch 使用 Xapian 创建 index, Xapian (version < 1.5) 需要设置下面
106 ;; 的环境变量来启用 CJK 功能:
108 ;; export XAPIAN_CJK_NGRAM=1
110 ;; Xapian (version >= 1.5) 可以自动识别 CJK, 但要求编译的时候启用
111 ;; LIBICU.
113 ;; 记得使用下面的命令来重建邮件索引。
115 ;; notmuch reindex "*"
117 (setq gnus-search-notmuch-program "notmuch")
118 (setq gnus-search-default-engines
119 '((nnimap . gnus-search-imap)
120 (nnml . gnus-search-notmuch)))
122 ;; 如果命令 'notmuch search --format=text --output=files hello' 得到的
123 ;; 一个结果是: '/home/feng/gnus/Mail/mail/misc/9407', 而当前搜索的组名
124 ;; 称是 mail.misc, 那么下面的变量应该设置为
125 ;; "/home/feng/gnus/Mail/". 注意,这个变量设置不对,不显示搜到邮件。
126 (setq gnus-search-notmuch-remove-prefix
127 (expand-file-name message-directory))
129 ;; 邮件分类设置
130 (setq nnmail-treat-duplicates 'delete
131 nnmail-split-fancy-match-partial-words t
132 nnmail-mail-splitting-decodes t
133 nnmail-mail-splitting-charset 'utf-8
134 ;; 使用 nnimap 时,不用默认的分类方法。
135 nnimap-split-methods nil)
137 (setq gnus-keep-backlog 1000) ;启用 backlog 功能,163 和 QQ imap 速度太慢了。
139 ;; 设置一个组的最大 article 数,这个设置对 nnimap 163邮箱很有用,因为
140 ;; 如果没有这个设置,进入 163 INBOX 会卡死 emacs.
141 (setq gnus-newsgroup-maximum-articles 100000)
143 ;; 使用准确率较高的地址提取函数
144 (setq gnus-extract-address-components
145 'mail-extract-address-components)
147 ;; 默认禁用 nnfolder
148 (setq gnus-message-archive-group nil)
150 ;; 设置 message-mode 发信的方式,这里默认使用 /usr/sbin/sendmail.
151 ;; 在 `gnus-posting-styles' 中设置 "X-Message-SMTP-Method" 邮件头可以实现
152 ;; 更为复杂的邮件发送方式。
153 (setq message-send-mail-function 'message-smtpmail-send-it)
155 ;; Message 自动设置 "X-Message-SMTP-Method" 功能。
156 (defun eh-message-server-alist-function ()
157 (let* ((from (cadr (mail-extract-address-components
158 (save-restriction
159 (widen)
160 (message-narrow-to-headers-or-head)
161 (message-fetch-field "From")))))
162 (str (replace-regexp-in-string "^.*@" "" from)))
163 (when (> (length str) 0)
164 (format "smtp smtp.%s 465 %s" str from))))
166 (setq message-server-alist '((eh-message-server-alist-function)))
168 ;; 设置gnus默认编码: 如果常与国外联系,可以设置为utf-8
169 ;; 如果只在本国使用,可以设置为本地编码,比如: gbk
170 (setq gnus-default-charset 'gbk)
172 ;; 根据method来确定编码
173 (setq gnus-group-name-charset-method-alist
174 '(((nntp "news.newsfan.net") . gbk)
175 ((nntp "news.cn99.com") . gbk)))
177 ;; 根据组名称来确定组名称解析使用的编码
178 (setq gnus-group-name-charset-group-alist
179 '((".*" . gbk)))
181 ;; 确定组默认使用的编码。
182 (setq gnus-group-charset-alist
183 '((".*" . gbk)))
185 ;; 如果还有乱码,手动调整
186 (setq gnus-summary-show-article-charset-alist
187 '((1 . gbk)
188 (2 . utf-8)
189 (3 . big5)
190 (4 . utf-7)))
192 ;; 邮件MIME类型设置不正确时,gnus的处理方式。
193 (setq gnus-newsgroup-ignored-charsets
194 '(unknown-8bit x-unknown x-gbk))
196 ;; 设置邮件附件文件名的编码方式以及邮件subject的编码方式
197 (defalias 'mail-header-encode-parameter 'rfc2047-encode-parameter)
198 (add-to-list 'rfc2047-charset-encoding-alist '(gbk . B))
199 (add-to-list 'rfc2047-charset-encoding-alist '(gb18030 . B))
201 ;; 常规设置
202 (setq gnus-agent t) ; 开启agent
203 (setq read-mail-command 'gnus) ; 使用gnus阅读邮件
204 (setq mail-user-agent 'gnus-user-agent) ; 使用gnus发送邮件
205 (setq gnus-inhibit-startup-message t) ; 关闭启动时的画面
206 (setq gnus-novice-user nil) ; 关闭新手设置, 不进行确认
207 (setq gnus-expert-user t) ; 不询问用户
208 (setq gnus-show-threads t) ; 显示邮件threads
209 (setq gnus-interactive-exit t) ; 退出时进行交互式询问
210 (setq gnus-use-dribble-file t) ; 创建恢复文件
211 (setq gnus-always-read-dribble-file t) ; 读取恢复文件
212 (setq gnus-asynchronous t) ; 异步操作
213 (setq gnus-large-newsgroup 2000) ; 设置大容量的新闻组默认显示的大小
214 (setq gnus-large-ephemeral-newsgroup nil) ; 设置临时新闻组默认显示的大小
215 (setq gnus-read-active-file 'some)
216 (setq gnus-nov-is-evil nil)
217 (setq gnus-summary-ignore-duplicates t) ; 忽略具有相同ID的消息
218 (setq gnus-treat-fill-long-lines t) ; 自动折行
219 (setq message-confirm-send t) ; 发邮件前需要确认(防止误发)
220 (setq message-kill-buffer-on-exit t) ; 发送邮件后删除buffer
221 (setq message-syntax-checks '((sender . disabled))) ; 语法检查
222 (setq nnmail-expiry-wait 7) ; 邮件自动删除的期限 (单位: 天)
223 (setq nnmairix-allowfast-default t) ; 加快进入搜索结果的组
224 (setq gnus-use-correct-string-widths t) ; 使用正确的字符宽度
225 (setq gc-cons-threshold 3500000) ; 加快 gnus 的速度
226 (setq gnus-use-cross-reference t) ; 开启交叉索引
227 (setq gnus-summary-display-while-building 50) ; 生成 summary 时,每50封显示一下
229 ;; 进入 summer 模式时,禁止自动选择第一个article,
230 ;; 这样设置主要是因为有些 article 下载速度极慢,
231 ;; 会降低响应速度
232 (setq gnus-auto-select-first nil)
233 (setq gnus-auto-select-next nil)
235 ;; 设置 gnus 启动时,组级别大于3的不自动更新。
236 ;; 当你添加了许多速度慢的组时,比如 rss,imap 等,启动速度会相当慢。这时你
237 ;; 可以把它们的组级别设置为大于3的值,这样启动时就不自动更新了。
238 ;; 当你需要更新这些组的时候,使用 "4-g" "5-g" 等快捷键
239 (setq gnus-activate-level 3)
241 ;; 双窗口布局(水平)
242 (gnus-add-configuration
243 '(article
244 (vertical 1.0
245 (summary 0.25 point)
246 (article 1.0))))
248 ;; 设置图片显示方式
249 (setq mm-inline-large-images t)
250 (add-to-list 'mm-attachment-override-types "image/*")
252 ;; 设置summary缓冲区的显示格式
253 (setq gnus-extra-headers '(To From))
254 (setq nnmail-extra-headers gnus-extra-headers)
255 (setq gnus-summary-gather-subject-limit 'fuzzy)
256 (setq gnus-summary-make-false-root 'adopt)
257 (setq gnus-summary-line-format "%U%R%z %&user-date; %-16,16a %5k%I%B%s\n")
259 ;; 设置 threads 的样式
260 (setq gnus-thread-indent-level 0)
261 (setq gnus-summary-same-subject "")
262 (setq gnus-sum-thread-tree-indent " ")
263 (setq gnus-sum-thread-tree-single-indent " ")
264 (setq gnus-sum-thread-tree-root " ")
265 (setq gnus-sum-thread-tree-false-root " ")
266 (setq gnus-sum-thread-tree-vertical " |")
267 (setq gnus-sum-thread-tree-leaf-with-other " |-> ")
268 (setq gnus-sum-thread-tree-single-leaf " `-> ")
270 ;; 设置 `gnus-summary-line-format' 中的 %&user-date;
271 (setq gnus-user-date-format-alist
272 '(((gnus-seconds-today) . " %H:%M")
273 ((gnus-seconds-month) . " %d日")
274 ((gnus-seconds-year) . " %m-%d")
275 (t . "%Y年")))
277 ;; 将邮件的发出时间转换为本地时间
278 (add-hook 'gnus-article-prepare-hook #'gnus-article-date-local)
280 ;; 跟踪组的时间轴
281 (add-hook 'gnus-select-group-hook #'gnus-group-set-timestamp)
283 (defun eh-gnus-summary-setup ()
284 (interactive)
285 ;; summary buffer 行距设置
286 (setq line-spacing 5)
288 ;; Highlight 当前行
289 (hl-line-mode 1)
291 ;; 重新定义键盘绑定
292 (local-set-key (kbd "SPC")
293 (lambda ()
294 (interactive)
295 (gnus-summary-next-page)
296 (move-beginning-of-line 1)))
297 (local-set-key (kbd "C-p")
298 (lambda ()
299 (interactive)
300 (delete-other-windows)
301 (forward-line -1)))
302 (local-set-key (kbd "C-n")
303 (lambda ()
304 (interactive)
305 (delete-other-windows)
306 (forward-line 1)))
307 (local-set-key (kbd "<up>")
308 (lambda ()
309 (interactive)
310 (delete-other-windows)
311 (forward-line -1)))
312 (local-set-key (kbd "<down>")
313 (lambda ()
314 (interactive)
315 (delete-other-windows)
316 (forward-line 1))))
318 (add-hook 'gnus-summary-mode-hook #'eh-gnus-summary-setup)
320 ;; visual
321 (setq gnus-treat-emphasize t
322 gnus-treat-buttonize t
323 gnus-treat-buttonize-head 'head
324 gnus-treat-unsplit-urls 'last
325 gnus-treat-leading-whitespace 'head
326 gnus-treat-highlight-citation t
327 gnus-treat-highlight-signature t
328 gnus-treat-date-lapsed 'head
329 gnus-treat-strip-trailing-blank-lines t
330 gnus-treat-strip-cr t
331 gnus-treat-overstrike nil
332 gnus-treat-display-x-face t
333 gnus-treat-display-face t
334 gnus-treat-display-smileys nil
335 gnus-treat-x-pgp-sig 'head)
337 ;; 设置邮件报头显示的信息
338 (setq gnus-visible-headers
339 (mapconcat 'regexp-quote
340 '("From:" "Newsgroups:" "Subject:" "Date:"
341 "Organization:" "To:" "Cc:" "Followup-To" "Gnus-Warnings:"
342 "X-Sent:" "X-URL:" "User-Agent:" "X-Newsreader:"
343 "X-Mailer:" "Reply-To:" "X-Spam:" "X-Spam-Status:" "X-Now-Playing"
344 "X-Attachments" "X-Diagnostic" "X-RSS-URL")
345 "\\|"))
347 ;; 设置邮件日期显示格式,使用两行日期,一行具体日期时间,
348 ;; 另一行显示article, 距现在多长时间
349 (setq gnus-article-date-headers '(user-defined))
350 (setq gnus-article-time-format
351 (lambda (time)
352 (concat "X-Sent: "
353 (format-time-string "%Y年%m月%d日 星期%u %R" time)
354 "\n"
355 "X-Lasped: "
356 (article-lapsed-string time))))
358 ;; 用 Supercite 显示多种多样的引文形式
359 (setq sc-attrib-selection-list nil
360 sc-auto-fill-region-p nil
361 sc-blank-lines-after-headers 1
362 sc-citation-delimiter-regexp "[>]+\\|\\(: \\)+"
363 sc-cite-blank-lines-p nil
364 sc-confirm-always-p nil
365 sc-electric-references-p nil
366 sc-fixup-whitespace-p t
367 sc-nested-citation-p nil
368 sc-preferred-header-style 4
369 sc-use-only-preference-p nil)
371 ;; 构建 threads 时抓取旧文章标题,
372 ;; 注意: 网速不快时不要使用这个选项。
373 (setq gnus-fetch-old-headers nil)
375 ;; 聚集 threads 的方式
376 (setq gnus-summary-thread-gathering-function
377 'gnus-gather-threads-by-subject)
379 ;; Thread root 排序
380 (setq gnus-thread-sort-functions
381 '(gnus-thread-sort-by-most-recent-number
382 gnus-thread-sort-by-most-recent-date))
384 ;; Subthread 排序
385 (setq gnus-subthread-sort-functions
386 '(gnus-thread-sort-by-number
387 gnus-thread-sort-by-date))
389 ;; 自动跳到第一个没有阅读的组
390 (add-hook 'gnus-switch-on-after-hook
391 #'gnus-group-first-unread-group)
393 (add-hook 'gnus-summary-exit-hook
394 #'gnus-group-first-unread-group)
397 ;; * Footer
398 (provide 'eh-gnus-common)
400 ;; Local Variables:
401 ;; coding: utf-8-unix
402 ;; End:
404 ;;; eh-gnus-common.el ends here