]> git.notmuchmail.org Git - notmuch/blobdiff - notmuch.el
notmuch.el: Make magic space bar advance to next unread messages.
[notmuch] / notmuch.el
index a6cb82360949206cf2993a23af5f68a5c7e74991..81c7b4475a0e38d2867db1ff29f6f92f0ee04fd1 100644 (file)
     (define-key map "b" 'notmuch-show-toggle-body-read-visible)
     (define-key map "c" 'notmuch-show-toggle-citations-visible)
     (define-key map "h" 'notmuch-show-toggle-headers-visible)
-    (define-key map "n" 'notmuch-show-mark-read-then-next-message)
+    (define-key map "n" 'notmuch-show-next-message)
     (define-key map "p" 'notmuch-show-previous-message)
     (define-key map "q" 'kill-this-buffer)
     (define-key map "s" 'notmuch-show-toggle-signatures-visible)
     (define-key map "x" 'kill-this-buffer)
     (define-key map "+" 'notmuch-show-add-tag)
     (define-key map "-" 'notmuch-show-remove-tag)
+    (define-key map " " 'notmuch-show-advance-marking-read-and-archiving)
     map)
   "Keymap for \"notmuch show\" buffers.")
 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
 
 (defun notmuch-show-remove-tag (tag)
   (interactive "sTag to remove: ")
-  (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
-  (notmuch-show-set-tags (delete tag (notmuch-show-get-tags))))
+  (let ((tags (notmuch-show-get-tags)))
+    (if (member tag tags)
+       (progn
+         (notmuch-call-notmuch-process "tag" (concat "-" tag) (concat "id:" (notmuch-show-get-message-id)))
+         (notmuch-show-set-tags (delete tag tags))))))
 
 (defun notmuch-show-archive-thread ()
-  "Archive each message currrently shown by removing the \"inbox\" tag from each.
+  "Archive each message in thread, and show next thread from search.
+
+Archive each message currrently shown by removing the \"inbox\"
+tag from each. Then kill this buffer and show the next thread
+from the search from which this thread was originally shown.
 
-This command is safe from any race condition of new messages
+Note: This command is safe from any race condition of new messages
 being delivered to the same thread. It does not archive the
 entire thread, but only the messages shown in the current
 buffer."
@@ -112,7 +120,13 @@ buffer."
       (if (not (eobp))
          (forward-char))
       (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
-         (goto-char (point-max))))))
+         (goto-char (point-max)))))
+  (let ((parent-buffer notmuch-show-parent-buffer))
+    (kill-this-buffer)
+    (if parent-buffer
+       (progn
+         (switch-to-buffer parent-buffer)
+         (notmuch-search-show-thread)))))
 
 (defun notmuch-show-move-to-current-message-summary-line ()
   "Move to the beginning of the one-line summary of the current message.
@@ -136,14 +150,50 @@ by searching backward)."
 (defun notmuch-show-next-message ()
   "Advance to the beginning of the next message in the buffer.
 
-Moves to the beginning of the current message if already on the
-last message in the buffer."
+Moves to the end of the buffer if already on the last message in
+the buffer."
   (interactive)
   (notmuch-show-move-to-current-message-summary-line)
-  (re-search-forward notmuch-show-message-begin-regexp nil t)
-  (notmuch-show-move-to-current-message-summary-line)
+  (if (re-search-forward notmuch-show-message-begin-regexp nil t)
+      (notmuch-show-move-to-current-message-summary-line)
+    (goto-char (point-max)))
   (recenter 0))
 
+(defun notmuch-show-find-next-message ()
+  "Returns the position of the next message in the buffer.
+
+Or the end of the buffer if already within the last message in
+the buffer."
+  ; save-excursion doesn't save our window position
+  ; save-window-excursion doesn't save point
+  ; Looks like we have to use both.
+  (save-excursion
+    (save-window-excursion
+      (notmuch-show-next-message)
+      (point))))
+
+(defun notmuch-show-next-unread-message ()
+  "Advance to the beginning of the next unread message in the buffer.
+
+Does nothing if there are no more unread messages past the
+current point."
+  (while (and (not (eobp))
+             (not (member "unread" (notmuch-show-get-tags))))
+    (notmuch-show-next-message)))
+
+(defun notmuch-show-find-next-unread-message ()
+  "Returns the position of the next message in the buffer.
+
+Returns the current point if there are no more unread messages
+past the current point."
+  ; save-excursion doesn't save our window position
+  ; save-window-excursion doesn't save point
+  ; Looks like we have to use both.
+  (save-excursion
+    (save-window-excursion
+      (notmuch-show-next-unread-message)
+      (point))))
+
 (defun notmuch-show-previous-message ()
   "Backup to the beginning of the previous message in the buffer.
 
@@ -161,12 +211,40 @@ simply move to the beginning of the current message."
          ))
     (recenter 0)))
 
-(defun notmuch-show-mark-read-then-next-message ()
-  "Remove uread tag from current message, then advance to next message."
+(defun notmuch-show-mark-read-then-next-unread-message ()
+  "Remove unread tag from current message, then advance to next unread message."
+  (interactive)
+  (notmuch-show-remove-tag "unread")
+  (notmuch-show-next-unread-message))
+
+(defun notmuch-show-advance-marking-read-and-archiving ()
+  "Advance through buffer, marking read and archiving.
+
+This command is intended to be one of the simplest ways to
+process a thread of email. It does the following:
+
+If the current message in the thread is not yet fully visible,
+scroll by a near screenful to read more of the message.
+
+Otherwise, (the end of the current message is already within the
+current window), remove the \"unread\" tag from the current
+message and advance to the next message.
+
+Finally, if there is no further message to advance to, and this
+last message is already read, then archive the entire current
+thread, (remove the \"inbox\" tag from each message). Also kill
+this buffer, and display the next thread from the search from
+which this thread was originally shown."
   (interactive)
-  (if (member "unread" (notmuch-show-get-tags))
-      (notmuch-show-remove-tag "unread"))
-  (notmuch-show-next-message))
+  (let ((next (notmuch-show-find-next-unread-message))
+       (unread (member "unread" (notmuch-show-get-tags))))
+    (if (and (not unread)
+            (equal next (point)))
+       (notmuch-show-archive-thread)
+      (if (and (> next (window-end))
+              (< next (point-max)))
+         (scroll-up nil)
+       (notmuch-show-mark-read-then-next-unread-message)))))
 
 (defun notmuch-show-markup-citations-region (beg end)
   (goto-char beg)
@@ -322,12 +400,17 @@ view, (remove the \"inbox\" tag from each), with
        mode-name "notmuch-show")
   (setq buffer-read-only t))
 
-(defun notmuch-show (thread-id)
-  "Run \"notmuch show\" with the given thread ID and display results."
+(defun notmuch-show (thread-id &optional parent-buffer)
+  "Run \"notmuch show\" with the given thread ID and display results.
+
+The optional PARENT-BUFFER is the notmuch-search buffer from
+which this notmuch-show command was executed, (so that the next
+thread from that buffer can be show when done with this one)."
   (interactive "sNotmuch show: ")
   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
     (switch-to-buffer buffer)
     (notmuch-show-mode)
+    (set (make-local-variable 'notmuch-show-parent-buffer) parent-buffer)
     (let ((proc (get-buffer-process (current-buffer)))
          (inhibit-read-only t))
       (if proc
@@ -339,6 +422,7 @@ view, (remove the \"inbox\" tag from each), with
        (call-process "notmuch" nil t nil "show" thread-id)
        (notmuch-show-markup-messages)
        )
+      (notmuch-show-next-unread-message)
       )))
 
 (defvar notmuch-search-mode-map
@@ -352,7 +436,7 @@ view, (remove the \"inbox\" tag from each), with
     (define-key map "s" 'notmuch-search)
     (define-key map "t" 'notmuch-search-filter-by-tag)
     (define-key map "x" 'kill-this-buffer)
-    (define-key map "\r" 'notmuch-search-show-thread)
+    (define-key map (kbd "RET") 'notmuch-search-show-thread)
     (define-key map "+" 'notmuch-search-add-tag)
     (define-key map "-" 'notmuch-search-remove-tag)
     (define-key map "<" 'beginning-of-buffer)
@@ -369,7 +453,7 @@ view, (remove the \"inbox\" tag from each), with
   "Move point to the last thread in the buffer."
   (interactive "^P")
   (end-of-buffer arg)
-  (beginning-of-line))
+  (forward-line -1))
 
 ;;;###autoload
 (defun notmuch-search-mode ()
@@ -436,7 +520,11 @@ global search.
 
 (defun notmuch-search-show-thread ()
   (interactive)
-  (notmuch-show (notmuch-search-find-thread-id)))
+  (let ((thread-id (notmuch-search-find-thread-id)))
+    (forward-line)
+    (if (> (length thread-id) 0)
+       (notmuch-show thread-id (current-buffer))
+      (error "End of search results"))))
 
 (defun notmuch-call-notmuch-process (&rest args)
   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
@@ -489,7 +577,7 @@ global search.
 This function advances point to the next line when finished."
   (interactive)
   (notmuch-search-remove-tag "inbox")
-  (next-line))
+  (forward-line))
 
 (defun notmuch-search (query)
   "Run \"notmuch search\" with the given query string and display results."
@@ -508,12 +596,6 @@ This function advances point to the next line when finished."
       (save-excursion
        (call-process "notmuch" nil t nil "search" query)
        (notmuch-search-markup-thread-ids)
-        ; A well-behaved program ends its output with a newline, but we
-        ; don't actually want the blank line at the end of the file.
-       (goto-char (point-max))
-       (if (looking-at "^$")
-           (delete-backward-char 1)
-         )
        ))))
 
 (defun notmuch-search-refresh-view ()