[PATCH v2] emacs: `notmuch-show-buttonize-links' only `notmuch-show's a message if it exists

Pieter Praet pieter at praet.org
Mon Jan 16 08:51:59 PST 2012


* emacs/notmuch-show.el (notmuch-show-found-target-p): new predicate function
  that uses notmuch(1) 'count' to see if a query turns up any results.

* emacs/notmuch-show.el (notmuch-show-if-found): new function that only shows
  a message/thread if present in the database and otherwise returns an error.

* emacs/notmuch-show.el (notmuch-show-buttonize-links): some deduplication,
  and use new function `notmuch-show-if-found' instead of `notmuch-show'
  to prevent showing a blank screen for Message-Id's which aren't present
  in the database.

---

v2:  (corrections by David Edmondson [1])

 - `notmuch-show-found-target-p' : removed useless `substring'.
 - `notmuch-show-if-found' : remove redundant `format'.

... and added a missing closing paren due to badly splitting up the
original commit.

[1] id:"cun7h0r50v6.fsf at hotblack-desiato.hh.sledj.net"


 emacs/notmuch-show.el |   31 +++++++++++++++++++++++--------
 1 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index f101d76..7a79d06 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -803,14 +803,17 @@ a corresponding notmuch search."
   (save-excursion
     (goto-char start)
     (while (re-search-forward "id:\\(\"?\\)[^[:space:]\"]+\\1" end t)
-      ;; remove the overlay created by goto-address-mode
-      (remove-overlays (match-beginning 0) (match-end 0) 'goto-address t)
-      (make-text-button (match-beginning 0) (match-end 0)
-			'action `(lambda (arg)
-				   (notmuch-show ,(match-string-no-properties 0)))
-			'follow-link t
-			'help-echo "Mouse-1, RET: search for this message"
-			'face goto-address-mail-face))))
+      (let ((message-id (match-string-no-properties 0))
+	    (string-start (match-beginning 0))
+	    (string-end (match-end 0)))
+	;; remove the overlay created by goto-address-mode
+	(remove-overlays string-start string-end 'goto-address t)
+	(make-text-button string-start string-end
+			  'action `(lambda (arg)
+				     (notmuch-show-if-found ,message-id))
+			  'follow-link t
+			  'help-echo "Mouse-1, RET: search for this message"
+			  'face goto-address-mail-face)))))
 
 ;;;###autoload
 (defun notmuch-show (thread-id &optional parent-buffer query-context buffer-name crypto-switch)
@@ -903,6 +906,18 @@ thread id.  If a prefix is given, crypto processing is toggled."
     (notmuch-kill-this-buffer)
     (notmuch-show-worker thread-id parent-buffer query-context buffer-name process-crypto)))
 
+(defun notmuch-show-found-target-p (target)
+  (let ((args `("count" ,target)))
+    (> (string-to-number
+	(with-output-to-string
+	  (apply 'call-process notmuch-command nil standard-output nil args)))
+       0)))
+
+(defun notmuch-show-if-found (target &rest args)
+  (if (notmuch-show-found-target-p target)
+      (notmuch-show target args)
+    (error "Can't find target: %s" target)))
+
 (defvar notmuch-show-stash-map
   (let ((map (make-sparse-keymap)))
     (define-key map "c" 'notmuch-show-stash-cc)
-- 
1.7.8.1



More information about the notmuch mailing list