unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Edmondson <dme@dme.org>
To: notmuch@notmuchmail.org
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	[thread overview]
Message-ID: <20190829172441.6849-3-dme@dme.org> (raw)
In-Reply-To: <20190829172441.6849-1-dme@dme.org>

---
 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

  parent reply	other threads:[~2019-08-29 17:24 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-25 10:02 notmuch-emacs: WISH: command notmuch-show-message-up with key bindung u Gregor Zattler
2019-08-26 14:12 ` David Bremner
2019-08-29 14:16 ` Daniel Kahn Gillmor
2019-08-29 17:24 ` [PATCH v1 0/2] emacs/show: Add a binding to go up David Edmondson
2019-08-29 17:24   ` [PATCH v1 1/2] emacs: Stash the currently shown forest in a buffer local variable David Edmondson
2019-08-29 17:24   ` David Edmondson [this message]
2019-08-29 18:50     ` [PATCH v1 2/2] emacs: Bind "u" to jump to the parent message in a thread Tomi Ollila

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190829172441.6849-3-dme@dme.org \
    --to=dme@dme.org \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).