commit 53c09efc48bacae49767a042a283722af95b2ca7 (HEAD, refs/heads/show-next-thread)
Author: David Edmondson <dme@dme.org>
Date:   Tue Jan 31 08:29:45 2012 +0000

    emacs: Rewhack the `notmuch-show-mode' movement commands.

        Modified   emacs/notmuch-show.el
diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index de9421e..bdc8007 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -125,6 +125,16 @@ indentation."
                 (const :tag "View interactively"
                        notmuch-show-interactively-view-part)))
 
+(defcustom notmuch-show-exit-at-end-p t
+  "Should movement commands automatically exit to the results?"
+  :group 'notmuch-show
+  :type 'boolean)
+
+(defcustom notmuch-show-open-next-thread-p t
+  "Should the next thread be opened automatically?"
+  :group 'notmuch-show
+  :type 'boolean)
+
 (defmacro with-current-notmuch-show-message (&rest body)
   "Evaluate body with current buffer set to the text of current message"
   `(save-excursion
@@ -1020,6 +1030,7 @@ buffer."
     ;; Set the header line to the subject of the first open message.
     (setq header-line-format (notmuch-show-strip-re (notmuch-show-get-subject)))
 
+    ;; Mark the displayed message as read.
     (notmuch-show-mark-read)))
 
 (defun notmuch-show-refresh-view (&optional crypto-switch)
@@ -1155,6 +1166,9 @@ All currently available key bindings:
   (cdr (notmuch-show-message-extent)))
 
 (defun notmuch-show-goto-message-next ()
+  "Move point to the next message.
+
+Return `t' if there was a next message, else return `nil'."
   (let ((start (point)))
     (notmuch-show-move-to-message-bottom)
     (if (not (eobp))
@@ -1163,6 +1177,9 @@ All currently available key bindings:
       nil)))
 
 (defun notmuch-show-goto-message-previous ()
+  "Move point to the previous message.
+
+Return `t' if there was a previous message, else return `nil'."
   (notmuch-show-move-to-message-top)
   (if (bobp)
       nil
@@ -1319,6 +1336,11 @@ current window), advance to the next open message."
       ;; This is not the last message - move to the next visible one.
       (notmuch-show-next-open-message))
 
+     ((not (= (point) (point-max)))
+      ;; This is the last message, but the cursor is not at the end of
+      ;; the buffer. Move it there.
+      (goto-char (point-max)))
+
      (t
       ;; This is the last message - change the return value
       (setq ret t)))
@@ -1391,19 +1413,17 @@ any effects from previous calls to
   (with-current-notmuch-show-message
    (notmuch-mua-new-forward-message prompt-for-sender)))
 
-(defun notmuch-show-next-message (&optional pop-at-end)
-  "Show the next message.
-
-If a prefix argument is given and this is the last message in the
-thread, navigate to the next thread in the parent search buffer."
+(defun notmuch-show-next-message ()
+  "Show the next message."
   (interactive "P")
   (if (notmuch-show-goto-message-next)
+      ;; There is another message. Mark it as read, because we are now
+      ;; showing it, and adjust the display.
       (progn
        (notmuch-show-mark-read)
        (notmuch-show-message-adjust))
-    (if pop-at-end
-       (notmuch-show-next-thread)
-      (goto-char (point-max)))))
+    ;; There is no other message.
+    (notmuch-show-next-thread)))
 
 (defun notmuch-show-previous-message ()
   "Show the previous message."
@@ -1412,23 +1432,23 @@ thread, navigate to the next thread in the parent search buffer."
   (notmuch-show-mark-read)
   (notmuch-show-message-adjust))
 
-(defun notmuch-show-next-open-message (&optional pop-at-end)
-  "Show the next open message.
-
-If a prefix argument is given and this is the last open message
-in the thread, navigate to the next thread in the parent search
-buffer."
-  (interactive "P")
-  (let (r)
-    (while (and (setq r (notmuch-show-goto-message-next))
+(defun notmuch-show-next-open-message ()
+  "Show the next open message."
+  (interactive)
+  (let (found-one)
+    ;; Move to the next open message.
+    (while (and (notmuch-show-goto-message-next)
                (not (notmuch-show-message-visible-p))))
-    (if r
+    (setq found-one (notmuch-show-message-visible-p))
+
+    (if found-one
+       ;; There is another open message. Mark it as read, because we
+       ;; are now showing it, and adjust the display.
        (progn
          (notmuch-show-mark-read)
          (notmuch-show-message-adjust))
-      (if pop-at-end
-         (notmuch-show-next-thread)
-       (goto-char (point-max))))))
+      ;; There is no other open message.
+      (notmuch-show-next-thread))))
 
 (defun notmuch-show-previous-open-message ()
   "Show the previous open message."
@@ -1591,31 +1611,38 @@ added."
   (interactive)
   (notmuch-show-tag-thread-internal tag t))
 
-(defun notmuch-show-next-thread (&optional show-next)
-  "Move to the next item in the search results, if any."
-  (interactive "P")
-  (let ((parent-buffer notmuch-show-parent-buffer))
-    (notmuch-kill-this-buffer)
-    (when parent-buffer
-      (switch-to-buffer parent-buffer)
-      (notmuch-search-next-thread)
-      (if show-next
-         (notmuch-search-show-thread)))))
+(defun notmuch-show-next-thread (&optional just-exit)
+  "Move to the next item in the search results, if any.
 
-(defun notmuch-show-archive-thread (&optional unarchive)
-  "Archive each message in thread.
+If JUST-EXIT is non-nil, don't open the next thread or move the
+cursor along."
+  (interactive)
+  (when (or notmuch-show-exit-at-end-p
+           just-exit)
+    ;; We are expected to exit the current buffer. Look for the parent
+    ;; buffer and check that it is alive.
+    (let ((parent-buffer notmuch-show-parent-buffer))
+      (notmuch-kill-this-buffer)
+      (when (buffer-live-p parent-buffer)
+       ;; The parent buffer is alive. Switch to it.
+       (switch-to-buffer parent-buffer)
+       (unless just-exit
+         ;; Move to the next thread.
+         (notmuch-search-next-thread)
+         ;; We are expected to open the next thread. Do so.
+         (if notmuch-show-open-next-thread-p
+             (notmuch-search-show-thread)))))))
 
-If a prefix argument is given, the messages will be
-\"unarchived\" (ie. the \"inbox\" tag will be added instead of
-removed).
+(defun notmuch-show-archive-thread (&optional unarchive)
+  "Archive messages in the current thread.
 
-Archive each message currently 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.
+Remove the \"inbox\" tag from the messages currently shown.  If
+UNARCHIVE is non-nil, add the \"inbox\" tag rather than removing
+it.
 
-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
+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."
   (interactive "P")
   (if unarchive
@@ -1623,16 +1650,19 @@ buffer."
     (notmuch-show-remove-tag-thread "inbox")))
 
 (defun notmuch-show-archive-thread-then-next ()
-  "Archive each message in thread, then show next thread from search."
+  "Archive each message in the current thread, then show the next
+thread."
   (interactive)
   (notmuch-show-archive-thread)
-  (notmuch-show-next-thread t))
+  (notmuch-show-next-thread))
 
 (defun notmuch-show-archive-thread-then-exit ()
-  "Archive each message in thread, then exit back to search results."
+  "Archive each message in the current thread, then exit to the
+search results."
   (interactive)
   (notmuch-show-archive-thread)
-  (notmuch-show-next-thread))
+  ;; Don't open the next thread or move the cursor.
+  (notmuch-show-next-thread t))
 
 (defun notmuch-show-archive-message (&optional unarchive)
   "Archive the current message.
@@ -1646,10 +1676,11 @@ removed)."
     (notmuch-show-remove-tag "inbox")))
 
 (defun notmuch-show-archive-message-then-next ()
-  "Archive the current message, then show the next open message in the current thread."
+  "Archive the current message, then show the next open message
+in the current thread."
   (interactive)
   (notmuch-show-archive-message)
-  (notmuch-show-next-open-message t))
+  (notmuch-show-next-open-message))
 
 (defun notmuch-show-stash-cc ()
   "Copy CC field of current message to kill-ring."

[back] 

Generated by David Edmondson using scpaste at Tue Jan 31 14:31:43 2012. GMT. (original)