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