]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Allow for scrolling backwards through thread with DEL
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
2 ;
3 ; Copyright © Carl Worth
4 ;
5 ; This file is part of Notmuch.
6 ;
7 ; Notmuch is free software: you can redistribute it and/or modify it
8 ; under the terms of the GNU General Public License as published by
9 ; the Free Software Foundation, either version 3 of the License, or
10 ; (at your option) any later version.
11 ;
12 ; Notmuch is distributed in the hope that it will be useful, but
13 ; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ; General Public License for more details.
16 ;
17 ; You should have received a copy of the GNU General Public License
18 ; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;
20 ; Authors: Carl Worth <cworth@cworth.org>
21
22 (defvar notmuch-show-mode-map
23   (let ((map (make-sparse-keymap)))
24     ; I don't actually want all of these toggle commands occupying
25     ; keybindings. They steal valuable key-binding space, are hard
26     ; to remember, and act globally rather than locally.
27     ;
28     ; Will be much preferable to switch to direct manipulation for
29     ; toggling visibility of these components. Probably using
30     ; overlays-at to query and manipulate the current overlay.
31     (define-key map "a" 'notmuch-show-archive-thread)
32     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
33     (define-key map "c" 'notmuch-show-toggle-citations-visible)
34     (define-key map "h" 'notmuch-show-toggle-headers-visible)
35     (define-key map "n" 'notmuch-show-next-message)
36     (define-key map "p" 'notmuch-show-previous-message)
37     (define-key map (kbd "C-n") 'notmuch-show-next-line)
38     (define-key map (kbd "C-p") 'notmuch-show-previous-line)
39     (define-key map "q" 'kill-this-buffer)
40     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
41     (define-key map "w" 'notmuch-show-view-raw-message)
42     (define-key map "x" 'kill-this-buffer)
43     (define-key map "+" 'notmuch-show-add-tag)
44     (define-key map "-" 'notmuch-show-remove-tag)
45     (define-key map (kbd "DEL") 'notmuch-show-rewind)
46     (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
47     map)
48   "Keymap for \"notmuch show\" buffers.")
49 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
50
51 (defvar notmuch-show-signature-lines-max 6
52   "Maximum length of signature that will be hidden by default.")
53
54 (set 'notmuch-show-message-begin-regexp    "\fmessage{")
55 (set 'notmuch-show-message-end-regexp      "\fmessage}")
56 (set 'notmuch-show-header-begin-regexp     "\fheader{")
57 (set 'notmuch-show-header-end-regexp       "\fheader}")
58 (set 'notmuch-show-body-begin-regexp       "\fbody{")
59 (set 'notmuch-show-body-end-regexp         "\fbody}")
60 (set 'notmuch-show-attachment-begin-regexp "\fattachment{")
61 (set 'notmuch-show-attachment-end-regexp   "\fattachment}")
62 (set 'notmuch-show-part-begin-regexp       "\fpart{")
63 (set 'notmuch-show-part-end-regexp         "\fpart}")
64 (set 'notmuch-show-marker-regexp "\f\\(message\\|header\\|body\\|attachment\\|part\\)[{}].*$")
65
66 (set 'notmuch-show-id-regexp "ID: \\([^ ]*\\)")
67 (set 'notmuch-show-filename-regexp "Filename: \\(.*\\)$")
68 (set 'notmuch-show-tags-regexp "(\\([^)]*\\))$")
69
70 ; XXX: This should be a generic function in emacs somewhere, not here
71 (defun point-invisible-p ()
72   "Return whether the character at point is invisible.
73
74 Here visibility is determined by `buffer-invisibility-spec' and
75 the invisible property of any overlays for point. It doesn't have
76 anything to do with whether point is currently being displayed
77 within the current window."
78   (let ((prop (get-char-property (point) 'invisible)))
79     (if (eq buffer-invisibility-spec t)
80         prop
81       (or (memq prop buffer-invisibility-spec)
82           (assq prop buffer-invisibility-spec)))))
83
84 (defun notmuch-show-next-line ()
85   "Like builtin `next-line' but ensuring we end on a visible character.
86
87 By advancing forward until reaching a visible character.
88
89 Unlike builtin `next-line' this version accepts no arguments."
90   (interactive)
91   (set 'this-command 'next-line)
92   (call-interactively 'next-line)
93   (while (point-invisible-p)
94     (forward-char)))
95
96 (defun notmuch-show-previous-line ()
97   "Like builtin `previous-line' but ensuring we end on a visible character.
98
99 By advancing forward until reaching a visible character.
100
101 Unlike builtin `next-line' this version accepts no arguments."
102   (interactive)
103   (set 'this-command 'previous-line)
104   (call-interactively 'previous-line)
105   (while (point-invisible-p)
106     (forward-char)))
107
108 (defun notmuch-show-get-message-id ()
109   (save-excursion
110     (beginning-of-line)
111     (if (not (looking-at notmuch-show-message-begin-regexp))
112         (re-search-backward notmuch-show-message-begin-regexp))
113     (re-search-forward notmuch-show-id-regexp)
114     (buffer-substring (match-beginning 1) (match-end 1))))
115
116 (defun notmuch-show-get-filename ()
117   (save-excursion
118     (beginning-of-line)
119     (if (not (looking-at notmuch-show-message-begin-regexp))
120         (re-search-backward notmuch-show-message-begin-regexp))
121     (re-search-forward notmuch-show-filename-regexp)
122     (buffer-substring (match-beginning 1) (match-end 1))))
123
124 (defun notmuch-show-set-tags (tags)
125   (save-excursion
126     (beginning-of-line)
127     (if (not (looking-at notmuch-show-message-begin-regexp))
128         (re-search-backward notmuch-show-message-begin-regexp))
129     (re-search-forward notmuch-show-tags-regexp)
130     (let ((inhibit-read-only t)
131           (beg (match-beginning 1))
132           (end (match-end 1)))
133       (delete-region beg end)
134       (goto-char beg)
135       (insert (mapconcat 'identity tags " ")))))
136
137 (defun notmuch-show-get-tags ()
138   (save-excursion
139     (beginning-of-line)
140     (if (not (looking-at notmuch-show-message-begin-regexp))
141         (re-search-backward notmuch-show-message-begin-regexp))
142     (re-search-forward notmuch-show-tags-regexp)
143     (split-string (buffer-substring (match-beginning 1) (match-end 1)))))
144
145 (defun notmuch-show-add-tag (tag)
146   (interactive "sTag to add: ")
147   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "id:" (notmuch-show-get-message-id)))
148   (notmuch-show-set-tags (delete-dups (sort (cons tag (notmuch-show-get-tags)) 'string<))))
149
150 (defun notmuch-show-remove-tag (tag)
151   (interactive "sTag to remove: ")
152   (let ((tags (notmuch-show-get-tags)))
153     (if (member tag tags)
154         (progn
155           (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
156           (notmuch-show-set-tags (delete tag tags))))))
157
158 (defun notmuch-show-archive-thread ()
159   "Archive each message in thread, and show next thread from search.
160
161 Archive each message currrently shown by removing the \"inbox\"
162 tag from each. Then kill this buffer and show the next thread
163 from the search from which this thread was originally shown.
164
165 Note: This command is safe from any race condition of new messages
166 being delivered to the same thread. It does not archive the
167 entire thread, but only the messages shown in the current
168 buffer."
169   (interactive)
170   (save-excursion
171     (goto-char (point-min))
172     (while (not (eobp))
173       (notmuch-show-remove-tag "inbox")
174       (if (not (eobp))
175           (forward-char))
176       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
177           (goto-char (point-max)))))
178   (let ((parent-buffer notmuch-show-parent-buffer))
179     (kill-this-buffer)
180     (if parent-buffer
181         (progn
182           (switch-to-buffer parent-buffer)
183           (notmuch-search-show-thread)))))
184
185 (defun notmuch-show-view-raw-message ()
186   "View the raw email of the current message."
187   (interactive)
188   (view-file (notmuch-show-get-filename)))
189
190 (defun notmuch-show-move-to-current-message-summary-line ()
191   "Move to the beginning of the one-line summary of the current message.
192
193 This gives us a stable place to move to and work from since the
194 summary line is always visible. This is important since moving to
195 an invisible location is unreliable, (the main command loop moves
196 point either forward or backward to the next visible character
197 when a command ends with point on an invisible character).
198
199 Emits an error if point is not within a valid message, (that is
200 not pattern of `notmuch-show-message-begin-regexp' could be found
201 by searching backward)."
202   (beginning-of-line)
203   (if (not (looking-at notmuch-show-message-begin-regexp))
204       (if (re-search-backward notmuch-show-message-begin-regexp nil t)
205           (forward-line 2)
206         (error "Not within a valid message."))
207     (forward-line 2)))
208
209 (defun notmuch-show-last-message-p ()
210   "Predicate testing whether point is within the last message."
211   (save-window-excursion
212     (save-excursion
213       (notmuch-show-move-to-current-message-summary-line)
214       (not (re-search-forward notmuch-show-message-begin-regexp nil t)))))
215
216 (defun notmuch-show-message-unread-p ()
217   "Preficate testing whether current message is unread."
218   (member "unread" (notmuch-show-get-tags)))
219
220 (defun notmuch-show-next-message ()
221   "Advance to the beginning of the next message in the buffer.
222
223 Moves to the last visible character of the current message if
224 already on the last message in the buffer."
225   (interactive)
226   (notmuch-show-move-to-current-message-summary-line)
227   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
228       (notmuch-show-move-to-current-message-summary-line)
229     (goto-char (- (point-max) 1))
230     (while (point-invisible-p)
231       (backward-char)))
232   (recenter 0))
233
234 (defun notmuch-show-find-next-message ()
235   "Returns the position of the next message in the buffer.
236
237 Or the position of the last visible character of the current
238 message if already within the last message in the buffer."
239   ; save-excursion doesn't save our window position
240   ; save-window-excursion doesn't save point
241   ; Looks like we have to use both.
242   (save-excursion
243     (save-window-excursion
244       (notmuch-show-next-message)
245       (point))))
246
247 (defun notmuch-show-next-unread-message ()
248   "Advance to the beginning of the next unread message in the buffer.
249
250 Moves to the last visible character of the current message if
251 there are no more unread messages past the current point."
252   (notmuch-show-next-message)
253   (while (and (not (notmuch-show-last-message-p))
254               (not (notmuch-show-message-unread-p)))
255     (notmuch-show-next-message))
256   (if (not (notmuch-show-message-unread-p))
257       (notmuch-show-next-message)))
258
259 (defun notmuch-show-previous-message ()
260   "Backup to the beginning of the previous message in the buffer.
261
262 If within a message rather than at the beginning of it, then
263 simply move to the beginning of the current message."
264   (interactive)
265   (let ((start (point)))
266     (notmuch-show-move-to-current-message-summary-line)
267     (if (not (< (point) start))
268         ; Go backward twice to skip the current message's marker
269         (progn
270           (re-search-backward notmuch-show-message-begin-regexp nil t)
271           (re-search-backward notmuch-show-message-begin-regexp nil t)
272           (notmuch-show-move-to-current-message-summary-line)
273           ))
274     (recenter 0)))
275
276 (defun notmuch-show-find-previous-message ()
277   "Returns the position of the previous message in the buffer.
278
279 Or the position of the beginning of the current message if point
280 is originally within the message rather than at the beginning of
281 it."
282   ; save-excursion doesn't save our window position
283   ; save-window-excursion doesn't save point
284   ; Looks like we have to use both.
285   (save-excursion
286     (save-window-excursion
287       (notmuch-show-previous-message)
288       (point))))
289
290 (defun notmuch-show-mark-read-then-next-unread-message ()
291   "Remove unread tag from current message, then advance to next unread message."
292   (interactive)
293   (notmuch-show-remove-tag "unread")
294   (notmuch-show-next-unread-message))
295
296 (defun notmuch-show-rewind ()
297   "Do reverse scrolling compared to `notmuch-show-advance-marking-read-and-archiving'
298
299 Specifically, if the beginning of the previous email is fewer
300 than `window-height' lines from the current point, move to it
301 just like `notmuch-show-previous-message'.
302
303 Otherwise, just scroll down a screenful of the current message.
304
305 This command does not modify any message tags, (it does not undo
306 any effects from previous calls to
307 `notmuch-show-advance-marking-read-and-archiving'."
308   (interactive)
309   (let ((previous (notmuch-show-find-previous-message)))
310     (if (> (count-lines previous (point)) (- (window-height) next-screen-context-lines))
311         (progn
312           (condition-case nil
313               (scroll-down nil)
314             ((beginning-of-buffer) nil))
315           (goto-char (window-start)))
316       (notmuch-show-previous-message))))
317
318 (defun notmuch-show-advance-marking-read-and-archiving ()
319   "Advance through buffer, marking read and archiving.
320
321 This command is intended to be one of the simplest ways to
322 process a thread of email. It does the following:
323
324 If the current message in the thread is not yet fully visible,
325 scroll by a near screenful to read more of the message.
326
327 Otherwise, (the end of the current message is already within the
328 current window), remove the \"unread\" tag (if present) from the
329 current message and advance to the next message.
330
331 Finally, if there is no further message to advance to, and this
332 last message is already read, then archive the entire current
333 thread, (remove the \"inbox\" tag from each message). Also kill
334 this buffer, and display the next thread from the search from
335 which this thread was originally shown."
336   (interactive)
337   (let ((next (notmuch-show-find-next-message))
338         (unread (notmuch-show-message-unread-p)))
339     (if (> next (window-end))
340         (scroll-up nil)
341       (if unread
342           (notmuch-show-mark-read-then-next-unread-message)
343         (if (notmuch-show-last-message-p)
344             (notmuch-show-archive-thread)
345           (notmuch-show-next-unread-message))))))
346
347 (defun notmuch-show-markup-citations-region (beg end)
348   (goto-char beg)
349   (beginning-of-line)
350   (while (< (point) end)
351     (let ((beg-sub (point)))
352       (if (looking-at ">")
353           (progn
354             (while (looking-at ">")
355               (forward-line))
356             (let ((overlay (make-overlay beg-sub (point))))
357               (overlay-put overlay 'invisible 'notmuch-show-citation)
358               (overlay-put overlay 'before-string
359                            (concat "[" (number-to-string (count-lines beg-sub (point)))
360                                    " quoted lines.]\n")))))
361       (if (looking-at "--[ ]?$")
362           (let ((sig-lines (count-lines beg-sub end)))
363             (if (<= sig-lines notmuch-show-signature-lines-max)
364                 (progn
365                   (overlay-put (make-overlay beg-sub (+ beg-sub 1))
366                                'before-string
367                                (concat "[" (number-to-string sig-lines)
368                                        "-line signature.]"))
369                   (overlay-put (make-overlay (+ beg-sub 2) end)
370                                'invisible 'notmuch-show-signature)
371                   (goto-char end)))))
372       (forward-line))))
373
374 (defun notmuch-show-markup-body ()
375   (re-search-forward notmuch-show-body-begin-regexp)
376   (next-line 1)
377   (beginning-of-line)
378   (let ((beg (point)))
379     (re-search-forward notmuch-show-body-end-regexp)
380     (let ((end (match-beginning 0)))
381       (notmuch-show-markup-citations-region beg end)
382       (if (not (notmuch-show-message-unread-p))
383           (overlay-put (make-overlay beg end)
384                        'invisible 'notmuch-show-body-read)))))
385
386 (defun notmuch-show-markup-header ()
387   (re-search-forward notmuch-show-header-begin-regexp)
388   (next-line 2)
389   (beginning-of-line)
390   (let ((beg (point)))
391     (re-search-forward notmuch-show-header-end-regexp)
392     (overlay-put (make-overlay beg (match-beginning 0))
393                  'invisible 'notmuch-show-header)))
394
395 (defun notmuch-show-markup-message ()
396   (if (re-search-forward notmuch-show-message-begin-regexp nil t)
397       (progn
398         (notmuch-show-markup-header)
399         (notmuch-show-markup-body))
400     (goto-char (point-max))))
401
402 (defun notmuch-show-hide-markers ()
403   (save-excursion
404     (goto-char (point-min))
405     (while (not (eobp))
406       (if (re-search-forward notmuch-show-marker-regexp nil t)
407           (progn
408             (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
409                          'invisible 'notmuch-show-marker))
410         (goto-char (point-max))))))
411
412 (defun notmuch-show-markup-messages ()
413   (save-excursion
414     (goto-char (point-min))
415     (while (not (eobp))
416       (notmuch-show-markup-message)))
417   (notmuch-show-hide-markers))
418
419 (defun notmuch-show-toggle-citations-visible ()
420   "Toggle visibility of citations"
421   (interactive)
422   (if notmuch-show-citations-visible
423       (add-to-invisibility-spec 'notmuch-show-citation)
424     (remove-from-invisibility-spec 'notmuch-show-citation))
425   (set 'notmuch-show-citations-visible (not notmuch-show-citations-visible))
426   ; Need to force the redisplay for some reason
427   (force-window-update)
428   (redisplay t))
429
430 (defun notmuch-show-toggle-signatures-visible ()
431   "Toggle visibility of signatures"
432   (interactive)
433   (if notmuch-show-signatures-visible
434       (add-to-invisibility-spec 'notmuch-show-signature)
435     (remove-from-invisibility-spec 'notmuch-show-signature))
436   (set 'notmuch-show-signatures-visible (not notmuch-show-signatures-visible))
437   ; Need to force the redisplay for some reason
438   (force-window-update)
439   (redisplay t))
440
441 (defun notmuch-show-toggle-headers-visible ()
442   "Toggle visibility of header fields"
443   (interactive)
444   (if notmuch-show-headers-visible
445       (add-to-invisibility-spec 'notmuch-show-header)
446     (remove-from-invisibility-spec 'notmuch-show-header))
447   (set 'notmuch-show-headers-visible (not notmuch-show-headers-visible))
448   ; Need to force the redisplay for some reason
449   (force-window-update)
450   (redisplay t))
451
452 (defun notmuch-show-toggle-body-read-visible ()
453   "Toggle visibility of message bodies of read messages"
454   (interactive)
455   (if notmuch-show-body-read-visible
456       (add-to-invisibility-spec 'notmuch-show-body-read)
457     (remove-from-invisibility-spec 'notmuch-show-body-read))
458   (set 'notmuch-show-body-read-visible (not notmuch-show-body-read-visible))
459   ; Need to force the redisplay for some reason
460   (force-window-update)
461   (redisplay t))
462
463 ;;;###autoload
464 (defun notmuch-show-mode ()
465   "Major mode for viewing a thread with notmuch.
466
467 This buffer contains the results of the \"notmuch show\" command
468 for displaying a single thread of email from your email archives.
469
470 By default, various components of email messages, (citations,
471 signatures, already-read messages), are invisible to help you
472 focus on the most important things, (new text from unread
473 messages). See the various commands below for toggling the
474 visibility of hidden components.
475
476 The `notmuch-show-next-message' and
477 `notmuch-show-previous-message' commands, (bound to 'n' and 'p by
478 default), allow you to navigate to the next and previous
479 messages. Each time you navigate away from a message with
480 `notmuch-show-next-message' the current message will have its
481 \"unread\" tag removed.
482
483 You can add or remove tags from the current message with '+' and
484 '-'.  You can also archive all messages in the current
485 view, (remove the \"inbox\" tag from each), with
486 `notmuch-show-archive-thread' (bound to 'a' by default).
487
488 \\{notmuch-show-mode-map}"
489   (interactive)
490   (kill-all-local-variables)
491   (set (make-local-variable 'notmuch-show-headers-visible) t)
492   (notmuch-show-toggle-headers-visible)
493   (set (make-local-variable 'notmuch-show-body-read-visible) t)
494   (notmuch-show-toggle-body-read-visible)
495   (set (make-local-variable 'notmuch-show-citations-visible) t)
496   (notmuch-show-toggle-citations-visible)
497   (set (make-local-variable 'notmuch-show-signatures-visible) t)
498   (notmuch-show-toggle-signatures-visible)
499   (add-to-invisibility-spec 'notmuch-show-marker)
500   (use-local-map notmuch-show-mode-map)
501   (setq major-mode 'notmuch-show-mode
502         mode-name "notmuch-show")
503   (setq buffer-read-only t))
504
505 (defun notmuch-show (thread-id &optional parent-buffer)
506   "Run \"notmuch show\" with the given thread ID and display results.
507
508 The optional PARENT-BUFFER is the notmuch-search buffer from
509 which this notmuch-show command was executed, (so that the next
510 thread from that buffer can be show when done with this one)."
511   (interactive "sNotmuch show: ")
512   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
513     (switch-to-buffer buffer)
514     (notmuch-show-mode)
515     (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
516     (let ((proc (get-buffer-process (current-buffer)))
517           (inhibit-read-only t))
518       (if proc
519           (error "notmuch search process already running for query `%s'" query)
520         )
521       (erase-buffer)
522       (goto-char (point-min))
523       (save-excursion
524         (call-process "notmuch" nil t nil "show" thread-id)
525         (notmuch-show-markup-messages)
526         )
527       ; Move straight to the first unread message
528       (if (not (notmuch-show-message-unread-p))
529           (progn
530             (notmuch-show-next-unread-message)
531             ; But if there are no unread messages, go back to the
532             ; beginning of the buffer, and open up the bodies of all
533             ; read message.
534             (if (not (notmuch-show-message-unread-p))
535                 (progn
536                   (goto-char (point-min))
537                   (notmuch-show-toggle-body-read-visible)))))
538       )))
539
540 (defvar notmuch-search-mode-map
541   (let ((map (make-sparse-keymap)))
542     (define-key map "a" 'notmuch-search-archive-thread)
543     (define-key map "b" 'notmuch-search-scroll-down)
544     (define-key map "f" 'notmuch-search-filter)
545     (define-key map "n" 'next-line)
546     (define-key map "p" 'previous-line)
547     (define-key map "q" 'kill-this-buffer)
548     (define-key map "s" 'notmuch-search)
549     (define-key map "t" 'notmuch-search-filter-by-tag)
550     (define-key map "x" 'kill-this-buffer)
551     (define-key map (kbd "RET") 'notmuch-search-show-thread)
552     (define-key map "+" 'notmuch-search-add-tag)
553     (define-key map "-" 'notmuch-search-remove-tag)
554     (define-key map "<" 'beginning-of-buffer)
555     (define-key map ">" 'notmuch-search-goto-last-thread)
556     (define-key map "=" 'notmuch-search-refresh-view)
557     (define-key map "\M->" 'notmuch-search-goto-last-thread)
558     (define-key map " " 'notmuch-search-scroll-up)
559     (define-key map (kbd "<DEL>") 'notmuch-search-scroll-down)
560     map)
561   "Keymap for \"notmuch search\" buffers.")
562 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
563
564 (defun notmuch-search-scroll-up ()
565   "Scroll up, moving point to last message in thread if at end."
566   (interactive)
567   (condition-case nil
568       (scroll-up nil)
569     ((end-of-buffer) (notmuch-search-goto-last-thread))))
570
571 (defun notmuch-search-scroll-down ()
572   "Scroll down, moving point to first message in thread if at beginning."
573   (interactive)
574   ; I don't know why scroll-down doesn't signal beginning-of-buffer
575   ; the way that scroll-up signals end-of-buffer, but c'est la vie.
576   ;
577   ; So instead of trapping a signal we instead check whether the
578   ; window begins on the first line of the buffer and if so, move
579   ; directly to that position. (We have to count lines since the
580   ; window-start position is not the same as point-min due to the
581   ; invisible thread-ID characters on the first line.
582   (if (equal (count-lines (point-min) (window-start)) 1)
583       (goto-char (window-start))
584     (scroll-down nil)))
585
586 (defun notmuch-search-goto-last-thread (&optional arg)
587   "Move point to the last thread in the buffer."
588   (interactive "^P")
589   (end-of-buffer arg)
590   (forward-line -1))
591
592 ;;;###autoload
593 (defun notmuch-search-mode ()
594   "Major mode for searching mail with notmuch.
595
596 This buffer contains the results of a \"notmuch search\" of your
597 email archives. Each line in the buffer represents a single
598 thread giving a relative date for the thread and a subject.
599
600 Pressing RET on any line displays that thread. The '+' and '-'
601 keys can be used to add or remove tags from a thread. The 'a' key
602 is a convenience key for archiving a thread (removing the
603 \"inbox\" tag).
604
605 Other useful commands are `notmuch-search-filter' for filtering
606 the current search based on an additional query string,
607 `notmuch-search-filter-by-tag' for filtering to include only
608 messages with a given tag, and `notmuch-search' to execute a new,
609 global search.
610
611 \\{notmuch-search-mode-map}"
612   (interactive)
613   (kill-all-local-variables)
614   (make-local-variable 'notmuch-search-query-string)
615   (set (make-local-variable 'scroll-preserve-screen-position) t)
616   (add-to-invisibility-spec 'notmuch-search)
617   (use-local-map notmuch-search-mode-map)
618   (setq major-mode 'notmuch-search-mode
619         mode-name "notmuch-search")
620   (setq buffer-read-only t))
621
622 (defun notmuch-search-find-thread-id ()
623   (save-excursion
624     (beginning-of-line)
625     (let ((beg (point)))
626       (re-search-forward "[a-fA-F0-9]*")
627       (filter-buffer-substring beg (point)))))
628
629 (defun notmuch-search-markup-this-thread-id ()
630   (beginning-of-line)
631   (let ((beg (point)))
632     (re-search-forward "[a-fA-F0-9]*")
633     (forward-char)
634     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
635
636 (defun notmuch-search-markup-thread-ids ()
637   (save-excursion
638     (goto-char (point-min))
639     (while (not (eobp))
640       (notmuch-search-markup-this-thread-id)
641       (next-line))))
642
643 (defun notmuch-search-hide-thread-ids ()
644   (interactive)
645   (add-to-invisibility-spec 'notmuch-search)
646   (force-window-update)
647   (redisplay t))
648
649 (defun notmuch-search-show-thread-ids ()
650   (interactive)
651   (remove-from-invisibility-spec 'notmuch-search)
652   (force-window-update)
653   (redisplay t))
654
655 (defun notmuch-search-show-thread ()
656   (interactive)
657   (let ((thread-id (notmuch-search-find-thread-id)))
658     (forward-line)
659     (if (> (length thread-id) 0)
660         (notmuch-show thread-id (current-buffer))
661       (error "End of search results"))))
662
663 (defun notmuch-call-notmuch-process (&rest args)
664   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
665     (with-current-buffer error-buffer
666         (erase-buffer))
667     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
668         (point)
669       (progn
670         (with-current-buffer error-buffer
671           (let ((beg (point-min))
672                 (end (- (point-max) 1)))
673             (error (buffer-substring beg end))
674             ))))))
675
676 (defun notmuch-search-set-tags (tags)
677   (save-excursion
678     (end-of-line)
679     (re-search-backward "(")
680     (forward-char)
681     (let ((beg (point))
682           (inhibit-read-only t))
683       (re-search-forward ")")
684       (backward-char)
685       (let ((end (point)))
686         (delete-region beg end)
687         (insert (mapconcat  'identity tags " "))))))
688
689 (defun notmuch-search-get-tags ()
690   (save-excursion
691     (end-of-line)
692     (re-search-backward "(")
693     (let ((beg (+ (point) 1)))
694       (re-search-forward ")")
695       (let ((end (- (point) 1)))
696         (split-string (buffer-substring beg end))))))
697
698 (defun notmuch-search-add-tag (tag)
699   (interactive "sTag to add: ")
700   (notmuch-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
701   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
702
703 (defun notmuch-search-remove-tag (tag)
704   (interactive "sTag to remove: ")
705   (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
706   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
707
708 (defun notmuch-search-archive-thread ()
709   "Archive the current thread (remove its \"inbox\" tag).
710
711 This function advances point to the next line when finished."
712   (interactive)
713   (notmuch-search-remove-tag "inbox")
714   (forward-line))
715
716 (defun notmuch-search (query)
717   "Run \"notmuch search\" with the given query string and display results."
718   (interactive "sNotmuch search: ")
719   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
720     (switch-to-buffer buffer)
721     (notmuch-search-mode)
722     (set 'notmuch-search-query-string query)
723     (let ((proc (get-buffer-process (current-buffer)))
724           (inhibit-read-only t))
725       (if proc
726           (error "notmuch search process already running for query `%s'" query)
727         )
728       (erase-buffer)
729       (goto-char (point-min))
730       (save-excursion
731         (call-process "notmuch" nil t nil "search" query)
732         (notmuch-search-markup-thread-ids)
733         ))))
734
735 (defun notmuch-search-refresh-view ()
736   "Refresh the current view.
737
738 Kills the current buffer and runs a new search with the same
739 query string as the current search. If the current thread is in
740 the new search results, then point will be placed on the same
741 thread. Otherwise, point will be moved to attempt to be in the
742 same relative position within the new buffer."
743   (interactive)
744   (let ((here (point))
745         (thread (notmuch-search-find-thread-id))
746         (query notmuch-search-query-string))
747     (kill-this-buffer)
748     (notmuch-search query)
749     (goto-char (point-min))
750     (if (re-search-forward (concat "^" thread) nil t)
751         (beginning-of-line)
752       (goto-char here))))
753
754 (defun notmuch-search-filter (query)
755   "Filter the current search results based on an additional query string.
756
757 Runs a new search matching only messages that match both the
758 current search results AND the additional query string provided."
759   (interactive "sFilter search: ")
760   (notmuch-search (concat notmuch-search-query-string " and " query)))
761
762 (defun notmuch-search-filter-by-tag (tag)
763   "Filter the current search results based on a single tag.
764
765 Runs a new search matching only messages that match both the
766 current search results AND that are tagged with the given tag."
767   (interactive "sFilter by tag: ")
768   (notmuch-search (concat notmuch-search-query-string " and tag:" tag)))
769
770 (defun notmuch ()
771   "Run notmuch to display all mail with tag of 'inbox'"
772   (interactive)
773   (notmuch-search "tag:inbox"))
774
775 (provide 'notmuch)