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