[PATCH v1 2/2] emacs: Bind "u" to jump to the parent message in a thread

Subject: [PATCH v1 2/2] emacs: Bind "u" to jump to the parent message in a thread

Date: Thu, 29 Aug 2019 18:24:41 +0100

To: notmuch@notmuchmail.org

Cc:

From: David Edmondson


---
 emacs/notmuch-show.el | 55 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 1e3834f2..7c50d2cd 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -1173,6 +1173,60 @@ is t, hide the part initially and show the button."
   (setq notmuch-show--forest forest)
   (mapc (lambda (thread) (notmuch-show-insert-thread thread 0)) forest))
 
+(defun notmuch-show--find-parent-in-thread-node (thread-node id)
+  (let* ((msg (car thread-node))
+	 (reply-nodes (cadr thread-node))
+	 ;; Is one of the replies to this message the one we are looking
+	 ;; for?
+	 (candidate
+	  (catch 'found
+	    (mapc (lambda (reply-node)
+		    (let* ((reply-msg (car reply-node))
+			   (reply-id (plist-get reply-msg :id)))
+		      (when (string= reply-id id)
+			(throw 'found (plist-get msg :id)))))
+		  reply-nodes)
+	    nil)))
+    (if candidate
+	candidate
+      ;; Otherwise recurse down.
+      (notmuch-show--find-parent-in-thread reply-nodes id))))
+
+(defun notmuch-show--find-parent-in-thread (thread id)
+  (let ((msg (catch 'found
+	       (mapc (lambda (thread-node)
+		       (setq msg (notmuch-show--find-parent-in-thread-node thread-node id))
+		       (when msg
+			 (throw 'found msg)))
+		     thread)
+	       nil)))
+    msg))
+
+(defun notmuch-show--find-parent-in-thread-set (thread-set id)
+  (let ((msg (catch 'found
+	       (mapc (lambda (thread)
+		       (setq msg (notmuch-show--find-parent-in-thread thread id))
+		       (when msg
+			 (throw 'found msg)))
+		     thread-set)
+	       nil)))
+    msg))
+
+(defun notmuch-show--find-parent ()
+  "Find the parent of the current message."
+  (notmuch-show--find-parent-in-thread-set notmuch-show--forest
+					   (notmuch-show-get-message-id t)))
+
+(defun notmuch-show--show-parent ()
+  "Jump to the parent of the current message, opening it if necessary."
+  (interactive)
+  (let ((parent (notmuch-show--find-parent)))
+    (unless parent
+      (error "The current message has no parent."))
+    (notmuch-show-goto-message (notmuch-id-to-query parent))
+    (unless (notmuch-show-message-visible-p)
+      (notmuch-show-toggle-message))))
+
 (defvar notmuch-id-regexp
   (concat
    ;; Match the id: prefix only if it begins a word (to disallow, for
@@ -1516,6 +1570,7 @@ reset based on the original query."
     (define-key map "t" 'toggle-truncate-lines)
     (define-key map "." 'notmuch-show-part-map)
     (define-key map "B" 'notmuch-show-browse-urls)
+    (define-key map "u" 'notmuch-show--show-parent)
     map)
   "Keymap for \"notmuch show\" buffers.")
 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
-- 
2.22.0

_______________________________________________
notmuch mailing list
notmuch@notmuchmail.org
https://notmuchmail.org/mailman/listinfo/notmuch

Thread: