From 85a2182911eb350249727e319012d57ecf212744 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Thu, 27 Jun 2019 00:36:16 -0400 Subject: notmuch: Try harder to find thread ID when showing tree In some cases, for example showing a message based on an ID, notmuch-show-thread-id is set to the message ID. This means that the context for the tree is restricted to the message. I find navigating such a tree inconvenient, so avoid this by getting the corresponding thread ID with 'notmuch search' if notmuch-show-thread-id doesn't look like a proper thread ID (i.e. "thread:*"). --- lisp/km-mail.el | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'lisp') diff --git a/lisp/km-mail.el b/lisp/km-mail.el index a63c8bc..38e9eee 100644 --- a/lisp/km-mail.el +++ b/lisp/km-mail.el @@ -57,12 +57,39 @@ (mark-whole-buffer) (call-interactively #'notmuch-search-archive-thread)) +(defun km/notmuch-thread-id-from-message-id (message-id) + (let ((threads (with-temp-buffer + (call-process "notmuch" nil t nil + "search" "--format=sexp" "--output=threads" + message-id) + (goto-char (point-min)) + (read (current-buffer))))) + (cl-case (length threads) + (0 + (user-error "No thread found for %S" message-id)) + (1 + (concat "thread:" (car threads))) + (t + (error "Got multiple threads for %S" message-id))))) + ;;;###autoload (defun km/notmuch-tree-from-show-current-query (&optional ignore-context) (interactive "P") - (let ((notmuch-show-query-context (and (not ignore-context) - notmuch-show-query-context))) - (call-interactively #'notmuch-tree-from-show-current-query))) + (let* ((mid (or (notmuch-show-get-message-id) + (error "No message ID found"))) + (tid (if (and notmuch-show-thread-id + ;; notmuch's variant works with + ;; notmuch-show-thread-id ... + (string-prefix-p "thread:" notmuch-show-thread-id)) + notmuch-show-thread-id + ;; ... but there are cases where this is set to the + ;; message ID, leading to the tree result that is + ;; always narrowed to the message. Try harder to get + ;; the actual thread ID. + (km/notmuch-thread-id-from-message-id mid))) + (notmuch-show-query-context (and (not ignore-context) + notmuch-show-query-context))) + (notmuch-tree tid notmuch-show-query-context mid))) ;;;###autoload (defun km/notmuch-show-at-point () -- cgit v1.2.3