using notmuch programmatically from emacs

Alan Schmitt alan.schmitt at polytechnique.org
Mon Jul 21 08:10:42 PDT 2014


On 2014-07-18 21:40, David Bremner <david at tethera.net> writes:

> Alan Schmitt <alan.schmitt at polytechnique.org> writes:
>
>> Hello,
>>
>> I sometimes have to find a message knowing only its message id. I know
>> how to use a notmuch search in emacs to find the message, then use
>> another function to display it in gnus, but I would like to do it
>> directly without going through the *notmuch-search* buffer.
>>
>> Right now I'm doing the following:
>>
>> #+begin_src emacs-lisp
>>   (defun as/msgid-to-gnus (msgid)
>>     "Search for the MSGID using notmuch, then open the message with
>>   gnus."
>>     (let ((file (shell-command-to-string (concat "notmuch search --output=files 'id:" msgid "'"))))
>
> The structured output formats (sexp, json) include file names, so you
> should be able to use something like the following
>
> (defun notmuch-query-get-message-filenames (&rest search-terms)
>   "Return a list of message-ids of messages that match SEARCH-TERMS"
>   (notmuch-query-map-threads
>    (lambda (msg) (plist-get msg :filename))
>    (notmuch-query-get-threads search-terms)))
>
> This is based on the example at the bottom of notmuch-query.el

Thanks a lot, this put me on the right track. I had to slightly modify
your example as it returns every message in the thread (and not just
the matching one). So I first test for id, and the filter the list to
return only the matching message. Here is the code I wrote.

#+begin_src emacs-lisp
  (defun notmuch-query-get-message-filenames (msgid)
    "Return the message filename where the message id matches MSGID
  and `nil' if no such message is found"
    (car (remove nil
                 (notmuch-query-map-threads
                  (lambda (msg) (if (equal (plist-get msg :id) msgid)
                                    (plist-get msg :filename)
                                  nil))
                  (notmuch-query-get-threads (list (concat "id:" msgid)))))))
#+end_src

Thanks again,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 494 bytes
Desc: not available
URL: <http://notmuchmail.org/pipermail/notmuch/attachments/20140721/920d527c/attachment.pgp>


More information about the notmuch mailing list