From 8f5be32912920c2c5eca7084885c0e2498540311 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Sun, 25 Mar 2012 15:55:44 +0200 Subject: [PATCH] contrib: new utility mutt-notmuch for Mutt integration --- contrib/mutt-notmuch/Makefile | 12 ++ contrib/mutt-notmuch/README | 58 +++++++++ contrib/mutt-notmuch/mutt-notmuch | 234 +++++++++++++++++++++++++++++++++++ contrib/mutt-notmuch/mutt-notmuch.1 | 210 +++++++++++++++++++++++++++++++ 4 files changed, 514 insertions(+), 0 deletions(-) create mode 100644 contrib/mutt-notmuch/Makefile create mode 100644 contrib/mutt-notmuch/README create mode 100755 contrib/mutt-notmuch/mutt-notmuch create mode 100644 contrib/mutt-notmuch/mutt-notmuch.1 diff --git a/contrib/mutt-notmuch/Makefile b/contrib/mutt-notmuch/Makefile new file mode 100644 index 0000000..b78953d --- /dev/null +++ b/contrib/mutt-notmuch/Makefile @@ -0,0 +1,12 @@ +NAME = mutt-notmuch + +all: $(NAME) $(NAME).1 + +$(NAME).1: $(NAME) + pod2man $< > $@ + +README.html: README + markdown $< > $@ + +clean: + rm -f mutt-notmuch.1 README.html diff --git a/contrib/mutt-notmuch/README b/contrib/mutt-notmuch/README new file mode 100644 index 0000000..e72aa91 --- /dev/null +++ b/contrib/mutt-notmuch/README @@ -0,0 +1,58 @@ +mutt-notmuch: Notmuch (of a) helper for Mutt +============================================ + +mutt-notmuch provide integration among the [Mutt] [1] mail user agent and the +[Notmuch] [2] mail indexer. + +mutt-notmuch offer two basic integration features. The first one is the ability +of stating a **search query interactively** and then jump to a fresh Maildir +containing its search results only. The second one is the ability to +**reconstruct threads on the fly** starting from the currently highlighted +mail, which comes handy when a thread has been split across different maildirs, +archived, or the like. + +mutt-notmuch enables to trigger mail searches via a Mutt macro (F8 being my +choice) and reconstruct threads via another (F9). Check the manpage for the +2-liner configuration snippet for your ~/.muttrc. + +A [blog style introduction] [3] to mutt-notmuch is available and includes some +more rationale for its existence. + +Arguably, some of the logics of mutt-notmuch could disappear by adding support +for a --output=symlinks flag to notmuch. + + +[1]: http://www.mutt.org/ +[2]: http://notmuchmail.org/ +[3]: http://upsilon.cc/~zack/blog/posts/2011/01/how_to_use_Notmuch_with_Mutt/ + + +Requirements +------------ + +To *run* mutt-notmuch you will need Perl with the following libraries: + +- Mail::Box + (Debian package: libmail-box-perl) +- Mail::Internet + (Debian package: libmailtools-perl) +- String::ShellQuote + (Debian package: libstring-shellquote-perl) +- Term::ReadLine + (Debian package: libterm-readline-gnu-perl) + +To *build* mutt-notmuch documentation you will need: + +- pod2man (coming with Perl) to generate the manpage +- markdown to generate README.html out of this file + + +License +------- + +mutt-notmuch is copyright (C) 2011-2012 Stefano Zacchiroli . + +mutt-notmuch is released under the terms of the GNU General Public License +(GPL), version 3 or above. A copy of the license is available online at +. + diff --git a/contrib/mutt-notmuch/mutt-notmuch b/contrib/mutt-notmuch/mutt-notmuch new file mode 100755 index 0000000..31cb539 --- /dev/null +++ b/contrib/mutt-notmuch/mutt-notmuch @@ -0,0 +1,234 @@ +#!/usr/bin/perl -w +# +# mutt-notmuch - notmuch (of a) helper for Mutt +# +# Copyright: © 2011-2012 Stefano Zacchiroli +# License: GNU General Public License (GPL), version 3 or above +# +# See the bottom of this file for more documentation. +# A manpage can be obtained by running "pod2man mutt-notmuch > mutt-notmuch.1" + +use strict; +use warnings; + +use File::Path; +use Getopt::Long qw(:config no_getopt_compat); +use Mail::Internet; +use Mail::Box::Maildir; +use Pod::Usage; +use String::ShellQuote; +use Term::ReadLine; + + +# create an empty maildir (if missing) or empty an existing maildir" +sub empty_maildir($) { + my ($maildir) = (@_); + rmtree($maildir) if (-d $maildir); + my $folder = new Mail::Box::Maildir(folder => $maildir, + create => 1); + $folder->close(); +} + +# search($maildir, $query) +# search mails according to $query with notmuch; store results in $maildir +sub search($$) { + my ($maildir, $query) = @_; + $query = shell_quote($query); + + empty_maildir($maildir); + system("notmuch search --output=files $query" + . " | sed -e 's: :\\\\ :g'" + . " | xargs --no-run-if-empty ln -s -t $maildir/cur/"); +} + +sub prompt($$) { + my ($text, $default) = @_; + my $query = ""; + my $term = Term::ReadLine->new( "mutt-notmuch" ); + $term->ornaments( 0 ); + $term->unbind_key( ord( "\t" ) ); + $term->MinLine( 3 ); + if ($ENV{MUTT_NOTMUCH_HISTFILE} && -r "$ENV{MUTT_NOTMUCH_HISTFILE}") { + $term->ReadHistory("$ENV{MUTT_NOTMUCH_HISTFILE}"); + } elsif (-r "$ENV{HOME}/.mutt-notmuch.history") { + $term->ReadHistory("$ENV{HOME}/.mutt-notmuch.history"); + } + while (1) { + chomp($query = $term->readline($text, $default)); + if ($query eq "?") { + system("man", "notmuch"); + } else { + if (!$ENV{MUTT_NOTMUCH_HISTFILE} || + !$term->WriteHistory("$ENV{MUTT_NOTMUCH_HISTFILE}")) { + $term->WriteHistory("$ENV{HOME}/.mutt-notmuch.history"); + } + return $query; + } + } +} + +sub get_message_id() { + my $mail = Mail::Internet->new(\*STDIN); + $mail->head->get("message-id") =~ /^<(.*)>$/; # get message-id + return $1; +} + +sub search_action($$@) { + my ($interactive, $results_dir, @params) = @_; + + if (! $interactive) { + search($results_dir, join(' ', @params)); + } else { + my $query = prompt("search ('?' for man): ", join(' ', @params)); + if ($query ne "") { + search($results_dir,$query); + } + } +} + +sub thread_action(@) { + my ($results_dir, @params) = @_; + my $mid = get_message_id(); + my $tid = `notmuch search --output=threads id:$mid`;# get thread id + chomp($tid); + + search($results_dir, $tid); +} + +sub tag_action(@) { + my $mid = get_message_id(); + + system("notmuch tag " + . shell_quote(join(' ', @_)) + . " id:$mid"); +} + +sub die_usage() { + my %podflags = ( "verbose" => 1, + "exitval" => 2 ); + pod2usage(%podflags); +} + +sub main() { + my $results_dir = "$ENV{HOME}/.cache/mutt_results"; + my $interactive = 0; + my $help_needed = 0; + + my $getopt = GetOptions( + "h|help" => \$help_needed, + "o|output-dir=s" => \$results_dir, + "p|prompt" => \$interactive); + if (! $getopt || $#ARGV < 0) { die_usage() }; + my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]); + + foreach my $param (@params) { + $param =~ s/folder:=/folder:/g; + } + + if ($help_needed) { + die_usage(); + } elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) { + print STDERR "Error: no search term provided\n\n"; + die_usage(); + } elsif ($action eq "search") { + search_action($interactive, $results_dir, @params); + } elsif ($action eq "thread") { + thread_action($results_dir, @params); + } elsif ($action eq "tag") { + tag_action(@params); + } else { + die_usage(); + } +} + +main(); + +__END__ + +=head1 NAME + +mutt-notmuch - notmuch (of a) helper for Mutt + +=head1 SYNOPSIS + +=over + +=item B [I