Using notmuch-emacs along with other folder-based agents

Little by little I’ve started to leave my mutt niche in order to start using notmuch-emacs MUA. All in all, I can see the see the speed advantage of using notmuch, among other little perks. Nevertheless, the approach of tagging emails instead of organizing them in IMAP folders makes the use of notmuch a little non-portable for others folder based user agents. I wouldn’t mind that, but I like receiving my emails on my mobile phone and as far as I know there is no such thing as a mobile notmuch MUA.

In this blog post I’ll share my approach for using a notmuch based agent, as notmuch-emacs, along with other folder based agents, as Thunderbird or K-9 Mail. The first thing to do is to define the layout of our IMAP folders. In our examples we’ll use something like this:

$ ls -1 ~/Maildir
archive
cur
debian
debian.devel
debian.haskell
debian.haskell.commits
debian.haskell.maintainers
debian.haskell.maintainers.junk
debian.misc
debian.project
new
tmp

So, here I have my inbox folder along with an archive maildir and a hierarchy of folders containing Debian mailing lists. In my folder based mobile agent, K-9, I won’t be bothered with notifications of new mailing list emails, as the agent is configured to only check for new emails in the inbox folder. On the other hand, in a default notmuch setup each time I run notmuch new the database will be filled with all the emails in all the folders and will tag the new ones with inbox. Now, when I’m using notmuch-emacs, I like that my inbox emails be only the ones in my inbox folder. Besides, I’ll also like to add a tag that matches the name of the folder the email is in. In notmuch language:

#!/bin/sh

notmuch new

notmuch tag +archive -inbox "path:archive/**"
notmuch tag +debian -inbox "path:debian/**"
notmuch tag +debian-devel -inbox "path:debian.devel/**"
notmuch tag +debian-project -inbox "path:debian.project/**"
notmuch tag +debian-haskell -inbox "path:debian.haskell/**"
notmuch tag +debian-haskell-commits -inbox "path:debian.haskell.commits/**"
notmuch tag +debian-haskell-maintainers -inbox "path:debian.haskell.maintainers/**"
notmuch tag +debian-haskell-maintainers-junk -inbox -unread "path:debian.haskell.maintainers.junk/**"
notmuch tag +debian-misc -inbox "path:debian.misc/**"

With this method we can use a folder based approach with notmuch tags. Let’s move to another topic. In the notmuch world, archiving an email means removing the inbox tag. If we only do this, all the archived emails will still be in our inbox folder, so what we’ll like to do is to move to the archive folder all the emails from the inbox folder that don’t have the inbox tag. In other words:

function archive() {
    if [ $# -ne 2 ]; then
       echo "Incorrent parameters. Send INBOX and ARCHIVE"
       return 1
    fi

    for e in $(notmuch search --output=files "path:$1/**" and not tag:inbox); do
        mv $e $2
    done

    return 0
}

archive cur ${MAILDIR}/archive/cur

Finally, one little detail is left: spam. In a folder based agent, we’ll usually have a spam folder. In the notmuch world, when we tag an email with spam, a decent client won’t show it in the results of notmuch searches, but it will remain in the folder it was found. So, what we have to do is to move all the spam tagged email to our spam folder.

for e in $(notmuch search --output=files tag:spam and not "path:spam/**"); do
    mv $e $MAILDIR/spam/cur
done

So, if we cron this little script we’ll be able to use a notmuch based MUA along with an IMAP folder based one. Enjoy!


Posted on June 24, 2015
Tags: notmuch

Comments? Sure! Just send me an email with it and I'll happily include it here.