[RFC] Several minor enhancements to the Emacs interface

Thomas Jost schnouki at schnouki.net
Tue May 31 07:47:27 PDT 2011


Hi list,

The thread regarding multiple sender identities seems to have been of
interest for several people, so I'd like to share a few other snippets
From my Emacs configuration file.

Please tell me what you think of them. If you like them, I'd be glad to
write some patches so they can be merged in notmuch.

*** HTML support ***
The current solution for opening HTML parts in a browser seems to be an
external script relying on the "munpack" executable:
http://notmuchmail.org/emacstips/#index8h2
However Emacs has everything that is needed. Here's a function that will
open text/html parts in a browser using nothing but pure elisp:

    (defun schnouki/notmuch-view-html ()
      "Open the HTML parts of a mail in a web browser."
      (interactive)
      (with-current-notmuch-show-message
       (let ((mm-handle (mm-dissect-buffer)))
         (notmuch-foreach-mime-part
          (lambda (p)
            (if (string-equal (mm-handle-media-type p) "text/html")
                (mm-display-external p (lambda ()
                                         (message "Opening web browser...")
                                         (browse-url-of-buffer)
                                         (bury-buffer)))))
          mm-handle))))

(Bound to "H" in notmuch-show-mode in my .emacs)


*** Filter by date ***
When displaying a search with lots of results (like tag:notmuch...), I'm
often only interested in what happened during the last few days. Here's
a little function, bound to "d" in notmuch-search-mode in my .emacs,
that asks for the number of days to display on restricts the search
accordingly:

    (defun schnouki/notmuch-search-filter-by-date (days)
      (interactive "NNumber of days to display: ")
      (let* ((now (current-time))
             (beg (time-subtract now (days-to-time days)))
             (filter
              (concat
               (format-time-string "%s.." beg)
               (format-time-string "%s" now))))
        (notmuch-search-filter filter)))

The funny thing is that it also works with decimal inputs: d 1.5 RET,
and here is the list of messages received during the last 36 hours :)

This is really basic and could be improved by using something like
calendar-read-date (or better: org-read-date) to select the bounds of
the search. Please tell me if you're interested.


*** Autorefresh notmuch-hello via D-Bus ***
I process my incoming mails with a massive shell scripts that does many
things: the obvious and essential ones (offlineimap, notmuch new, call
autotag script), and the optional and fancy ones (update desktop widget
with a unread messages counter, display a desktop notification with the
last unread messages, make a daily backup of the notmuch DB, etc.). A
recent addition was to tell Emacs to update the *notmuch-hello* buffer
by calling a D-Bus method. This requires Emacs 23 compiled with D-Bus
support. Here's the corresponding code:

    (require 'dbus)
    (defun schnouki/notmuch-dbus-notify ()
      (when (get-buffer "*notmuch-hello*")
        (message "Notmuch notify")
        (notmuch-hello-update t)))
     (dbus-register-method :session dbus-service-emacs dbus-path-emacs
                           dbus-service-emacs "NotmuchNotify"
                           'schnouki/notmuch-dbus-notify)

The shell script then runs the following command:

    dbus-send --session --dest="org.gnu.Emacs" \
        "/org/gnu/Emacs" "org.gnu.Emacs.NotmuchNotify"

Et voilà! *notmuch-hello* is updated.
(This probably shouldn't be integrated in notmuch because it has almost
nothing to do with notmuch itself, but it's still a cool hack and I'm
quite proud of it, so I just wanted to share it here :))


*** Automagical message signature selection ***
This function selects the signature to insert at the end of a message
according to the From header. It's based on a set of rules: when the
From header matches a rule, the content of the corresponding file is
inserted at the end of the message.

    (setq schnouki/message-signatures '(("me at work.tld" . "~/.signature/work")
                                        ("me at fsfe.org" . "~/.signature/fsfe")
                                        (".*"          . "~/.signature/normal"))
    (defun schnouki/choose-signature ()
      (let* ((from (message-fetch-field "From"))
             (sigfile
              (catch 'first-match
                (dolist (re-file schnouki/message-signatures)
                  (when (string-match-p (car re-file) from)
                    (throw 'first-match (cdr re-file)))))))
        (if sigfile
            (with-temp-buffer
              (insert-file-contents sigfile)
              (buffer-string)))))
    (setq message-signature 'schnouki/choose-signature)


*** Not done yet :) ***
notmuch-search-archive-thread and notmuch-show-archive-thread are nice,
but they only remove the "inbox" tag. When quickly browsing through
messages I don't wan't to read, I'd also want it to remove the "unread"
tag.
Right now I copy-pasted them in my .emacs and just added that manually,
but I'd like it better if the list of tags removed by *-archive-thread
could be customized. So I'll probably write a patch that adds a
"notmuch-archive-removed-tags" customization variable and updates the
*-archive-thread functions to take that into account.

If you're interested, all of these snippets (and some other ones) are
available in the mail section of my .emacs:
https://github.com/Schnouki/dotfiles/blob/master/emacs/init-50-mail.el

Please tell me what you think about all of that, and if there are
particular pieces that you would like to see included in notmuch :)

Regards,
-- 
Thomas/Schnouki
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 489 bytes
Desc: not available
URL: <http://notmuchmail.org/pipermail/notmuch/attachments/20110531/5526113b/attachment.pgp>


More information about the notmuch mailing list