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