-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathakmuch.el
187 lines (174 loc) · 5.99 KB
/
akmuch.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
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
(require 'akmuch-search)
(require 'akmuch-message)
(require 'akmuch-reply)
(require 'akmuch-summarize)
(require 'akmuch-tag)
(defvar akmuch-search '("tag:inbox"))
(defvar akmuch-search-history '())
(defvar akmuch-last-position 0)
(defvar akmuch-last-window-start 0)
(defvar akmuch-last-position-t 0)
(defvar akmuch-last-window-start-t 0)
(defvar akmuch-display-type 'search)
(defvar akmuch-quit-to 'search)
(defvar akmuch-current-thread-id)
(defvar akmuch-current-message-id)
(defvar akmuch-mail-indicator '(""))
(defcustom akmuch-subject-display-length 70
"number of characters to display subject")
(defcustom akmuch-default-dir "~/"
"The default directory for writing attachments to")
(defcustom akmuch-fill-messages t
"fill messages in view and reply buffers?")
(defcustom akmuch-long-line 100
"What should be considered long line for filling mail/reply")
(defcustom akmuch-summary-terms
'(("inbox" . "tag:inbox")
("unread" . "tag:unread")
("recent" . "date:-1d.."))
"Example summary terms")
(defun akmuch-quit ()
(interactive)
(if (eq akmuch-quit-to 'threadlist)
(progn (akmuch-thread-list t)
(setq akmuch-quit-to 'search))
(akmuch-display-search))
(akmuch-restore-state))
(defun akmuch-save-state ()
(cond
((eq akmuch-display-type 'search)
(setq akmuch-last-position (line-number-at-pos (point)))
(setq akmuch-last-window-start (window-start)))
((eq akmuch-display-type 'threadlist)
(setq akmuch-last-position-t (line-number-at-pos (point)))
(setq akmuch-last-window-start-t (window-start)))))
(defun akmuch-restore-state ()
(let ((wstart akmuch-last-window-start)
(lastpos akmuch-last-position))
(when (eq akmuch-display-type 'threadlist)
(setq wstart akmuch-last-window-start-t)
(setq lastpos akmuch-last-position-t))
(set-window-start nil wstart)
(akmuch-next (- lastpos 1))
(unless (pos-visible-in-window-p (point))
(recenter -2))))
(defun akmuch-next-message (&optional num)
(let (start theid)
(with-temp-buffer
(call-process "notmuch" nil (current-buffer) nil
"show" "--entire-thread" akmuch-current-thread-id)
(goto-char (point-min))
(search-forward-regexp
(concat "\^Lmessage{ "
(regexp-quote akmuch-current-message-id)))
(when
(if (< num 0)
(search-backward-regexp "\^Lmessage{ " nil t (+ (* -1 num) 1))
(search-forward-regexp "\^Lmessage{ " nil t num))
(setq start (point-at-bol)))
(if (null start)
(message "No more messages")
(goto-char (point-at-bol))
(search-forward-regexp " ")
(setq start (point))
(search-forward-regexp " ")
(setq theid (buffer-substring start (- (point) 1)))))
(when theid
(akmuch-view theid))))
(defun akmuch-get-threadid ()
(if (eq akmuch-display-type 'message)
akmuch-current-thread-id
(save-excursion
(goto-char (point-at-bol))
(when (search-forward-regexp "[ >]" nil t)
(let ((thread
(buffer-substring-no-properties
(point-at-bol) (- (point) 1))))
(when (eq akmuch-display-type 'search)
(setq akmuch-current-thread-id thread))
thread)))))
(defun akmuch-thread-list (&optional nomove)
(interactive)
(let ((thread (akmuch-get-threadid))
(buffer-read-only nil)
subj depth id start)
(message "got thread id %s, display type %s" thread akmuch-display-type)
(setq akmuch-display-type 'threadlist)
(erase-buffer)
(call-process "notmuch" nil (current-buffer) nil
"show" thread)
(goto-char (point-min))
(search-forward-regexp "Subject: ")
(setq header-line-format (buffer-substring (point) (point-at-eol)))
;; (setq subj (buffer-substring (point) (point-at-eol)))
(goto-char (point-min))
;; (insert (concat subj "\n"))
(setq start (point))
(while (search-forward-regexp "^\^Lmessage{ " nil t)
(setq id (buffer-substring
(point)
(progn (search-forward-regexp " ") (- (point) 1))))
(forward-char 6)
(setq depth (string-to-number (buffer-substring (point) (+ 2 (point)))))
(forward-line 2)
(delete-region start (point))
(insert (propertize id 'invisible t))
(insert (concat " " (make-string (* depth 2) 32)))
(forward-line 1)
(setq start (point)))
(delete-region (- (point) 1) (point-max))
(goto-char (point-min))
(unless nomove
(if (not (search-forward-regexp "unread[^)]*)$" nil t))
(goto-char (point-max)))
(akmuch-next 0))))
(defun akmuch-preview-message-text (id)
(with-temp-buffer
(call-process "notmuch" nil (current-buffer) nil
"show" id)
(goto-char (point-min))
(search-forward-regexp "^\^Lbody{")
(search-forward-regexp "^[^\^L]")
(let ((start (- (point) 1))
end)
(search-forward-regexp "^\^L")
(if (< (- (- (point) 1) start) 60)
(setq end (- (point) 1))
(setq end (+ start 60)))
(concat
(replace-regexp-in-string
"\n" "..."
(buffer-substring start end))
"..."))))
(defun akmuch-check-unread-timer ()
(when (get-buffer "mail")
(with-current-buffer "mail"
(when (string-equal mode-name "Akmuch")
(let (unread marked total)
(with-temp-buffer
(insert "tag:unread\ntag:*\n*")
(shell-command-on-region
(point-min) (point-max)
"notmuch count --batch"
(current-buffer) t)
(goto-char (point-min))
(setq unread (buffer-substring (point) (point-at-eol)))
(forward-line)
(setq marked (buffer-substring (point) (point-at-eol)))
(forward-line)
(setq total (buffer-substring (point) (point-at-eol))))
(setq mode-line-buffer-identification
(format "%s (U:%s M:%s T:%s)" (buffer-name) unread marked total))
(setq unread (string-to-number unread))
(if (> unread 0)
(setq akmuch-mail-indicator (list (format " ✉ (%d)" unread)))
(setq akmuch-mail-indicator (list ""))))))))
(defun akmuch-disable-following ()
(setq post-command-hook nil))
(defun akmuch-enable-following ()
(setq post-command-hook 'akmuch-highlight-current))
(defun akmuch ()
(interactive)
(switch-to-buffer "mail")
(akmuch-search-mode)
(akmuch-display-search))