/
notmuch-kill-mode.el
156 lines (128 loc) · 4.67 KB
/
notmuch-kill-mode.el
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
;;; notmuch-kill-mode.el --- Minor mode to mute notmuch threads
;; Copyright © 2013-2014 Kototama
;;
;; Author: Kototama <kototamo@gmail.com>
;;
;;
;; URL: http://github.com/kototama/notmuch-kill-mode
;; Version: 0.1
;; Keywords: mail, notmuch
;; This file is not part of GNU Emacs.
;;; Commentary:
;;
;;; License:
;; 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Code:
(define-minor-mode notmuch-kill-mode
"Toggle Notmuch Kill Mode.
Allows the killing of message threads. All new incoming
messages contained in a killed thread will have their
'unread' tag removed.
"
:group 'notmuch
:lighter " notmuch-kill"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c C-k") 'notmuch-kill-add-thread-to-kill-list)
map)
:after-hook (notmuch-kill-init))
(require 'notmuch-query)
(defvar notmuch-kill-kill-list-path "~/.kill-list")
(setq notmuch-kill-kill-list nil)
(defun notmuch-kill-init
()
"Initializes the mode."
(when (not (file-exists-p notmuch-kill-kill-list-path))
(notmuch-kill-write-kill-list))
(setq notmuch-kill-kill-list (notmuch-kill-read-kill-list))
(add-hook 'kill-buffer-hook 'notmuch-kill-kill-buffer-hook))
(defun notmuch-kill-kill-buffer-hook
(&optional exit-data)
"Called when a buffer is killed."
(when (or (eq major-mode 'notmuch-search-mode)
(eq major-mode 'notmuch-show-mode)
(eq major-mode 'notmuch-hello-mode))
(notmuch-kill-write-kill-list)))
(defun notmuch-kill-mark-as-read
(messageid)
"Removes the 'unread' tag of the message."
(notmuch-tag (concat "id:" messageid) '("-unread")))
(defun notmuch-kill-refresh-view
()
"Refresh the current view"
(cond ((eq major-mode 'notmuch-show-mode)
(notmuch-show-refresh-view))
((eq major-mode 'notmuch-search-mode)
(notmuch-search-refresh-view))))
(defun notmuch-kill-get-id
()
"Returns the message id of the mail being read or the message id of
the thread."
(cond ((eq major-mode 'notmuch-show-mode)
(notmuch-show-get-message-id))
((eq major-mode 'notmuch-search-mode)
(notmuch-search-find-thread-id))))
(defun notmuch-kill-mark-thread-as-read
(idstring)
"Marks the thread containing the message as read."
(notmuch-query-map-threads
(lambda (msg)
(let ((id (plist-get msg :id)))
(notmuch-kill-mark-as-read id)))
(notmuch-query-get-threads (list idstring))))
(defun notmuch-kill-mark-thread-as-read-and-refresh
(idstring)
(interactive (list (notmuch-kill-get-id)))
"Marks the thread containing the message as read and refreshed the view."
(notmuch-kill-mark-thread-as-read idstring)
(notmuch-kill-refresh-view))
(defun notmuch-kill-get-string-from-file
(pathname)
"Return file's content."
(with-temp-buffer
(insert-file-contents pathname)
(buffer-string)))
(defun notmuch-kill-read-kill-list
()
"Returns the content of the kill-list."
(let ((content (notmuch-kill-get-string-from-file
notmuch-kill-kill-list-path)))
(read content)))
(defun notmuch-kill-write-kill-list
()
"Writes the content of the kill-list to the disk."
(with-temp-file notmuch-kill-kill-list-path
(insert-string (prin1-to-string notmuch-kill-kill-list))))
(defun notmuch-kill-add-to-kill-list
(idstr)
"Adds the thread containing the message's id to the kill-list."
(setq notmuch-kill-kill-list (cons idstr notmuch-kill-kill-list)))
(defun notmuch-kill-add-thread-to-kill-list
(idstr)
"Adds the thread containing the message's id to the kill-list
and refreshes the view."
(interactive (list (notmuch-kill-get-id)))
(notmuch-kill-add-to-kill-list idstr)
(notmuch-kill-mark-thread-as-read-and-refresh idstr))
(defun notmuch-kill-process-kill-list
()
"Marks all thread of all messages contained in the kill-list as
read."
(dolist (idstr notmuch-kill-kill-list)
(notmuch-kill-mark-thread-as-read idstr))
(notmuch-kill-refresh-view)
(message "Kill-list processed."))
(provide 'notmuch-kill-mode)
;;; notmuch-kill-mode.el ends here