3 # notmuch-mutt - notmuch (of a) helper for Mutt
5 # Copyright: © 2011-2012 Stefano Zacchiroli <zack@upsilon.cc>
6 # License: GNU General Public License (GPL), version 3 or above
8 # See the bottom of this file for more documentation.
9 # A manpage can be obtained by running "pod2man notmuch-mutt > notmuch-mutt.1"
15 use Getopt::Long qw(:config no_getopt_compat);
17 use Mail::Box::Maildir;
19 use String::ShellQuote;
23 my $xdg_cache_dir = "$ENV{HOME}/.cache";
24 $xdg_cache_dir = $ENV{XDG_CACHE_HOME} if $ENV{XDG_CACHE_HOME};
25 my $cache_dir = "$xdg_cache_dir/notmuch/mutt";
28 # create an empty maildir (if missing) or empty an existing maildir"
29 sub empty_maildir($) {
31 rmtree($maildir) if (-d $maildir);
32 my $folder = new Mail::Box::Maildir(folder => $maildir,
37 # search($maildir, $remove_dups, $query)
38 # search mails according to $query with notmuch; store results in $maildir
40 my ($maildir, $remove_dups, $query) = @_;
43 $query = shell_quote($query);
46 $dup_option = "--duplicate=1";
49 empty_maildir($maildir);
50 system("notmuch search --output=files $dup_option $query"
51 . " | sed -e 's: :\\\\ :g'"
52 . " | xargs --no-run-if-empty ln -s -t $maildir/cur/");
56 my ($text, $default) = @_;
58 my $term = Term::ReadLine->new( "notmuch-mutt" );
59 my $histfile = "$cache_dir/history";
61 $term->ornaments( 0 );
62 $term->unbind_key( ord( "\t" ) );
64 $histfile = $ENV{MUTT_NOTMUCH_HISTFILE} if $ENV{MUTT_NOTMUCH_HISTFILE};
65 $term->ReadHistory($histfile) if (-r $histfile);
67 chomp($query = $term->readline($text, $default));
69 system("man", "notmuch-search-terms");
71 $term->WriteHistory($histfile);
77 sub get_message_id() {
78 my $mail = Mail::Internet->new(\*STDIN);
79 my $mid = $mail->head->get("message-id") or return undef;
80 $mid =~ /^<(.*)>$/; # get message-id value
84 sub search_action($$$@) {
85 my ($interactive, $results_dir, $remove_dups, @params) = @_;
88 search($results_dir, $remove_dups, join(' ', @params));
90 my $query = prompt("search ('?' for man): ", join(' ', @params));
92 search($results_dir, $remove_dups, $query);
97 sub thread_action($$@) {
98 my ($results_dir, $remove_dups, @params) = @_;
100 my $mid = get_message_id();
101 if (! defined $mid) {
102 empty_maildir($results_dir);
103 die "notmuch-mutt: cannot find Message-Id, abort.\n";
105 my $search_cmd = 'notmuch search --output=threads ' . shell_quote("id:$mid");
106 my $tid = `$search_cmd`; # get thread id
109 search($results_dir, $remove_dups, $tid);
113 my $mid = get_message_id();
114 defined $mid or die "notmuch-mutt: cannot find Message-Id, abort.\n";
116 system("notmuch", "tag", @_, "--", "id:$mid");
120 my %podflags = ( "verbose" => 1,
122 pod2usage(%podflags);
126 mkpath($cache_dir) unless (-d $cache_dir);
128 my $results_dir = "$cache_dir/results";
133 my $getopt = GetOptions(
134 "h|help" => \$help_needed,
135 "o|output-dir=s" => \$results_dir,
136 "p|prompt" => \$interactive,
137 "r|remove-dups" => \$remove_dups);
138 if (! $getopt || $#ARGV < 0) { die_usage() };
139 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);
141 foreach my $param (@params) {
142 $param =~ s/folder:=/folder:/g;
147 } elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) {
148 print STDERR "Error: no search term provided\n\n";
150 } elsif ($action eq "search") {
151 search_action($interactive, $results_dir, $remove_dups, @params);
152 } elsif ($action eq "thread") {
153 thread_action($results_dir, $remove_dups, @params);
154 } elsif ($action eq "tag") {
167 notmuch-mutt - notmuch (of a) helper for Mutt
173 =item B<notmuch-mutt> [I<OPTION>]... search [I<SEARCH-TERM>]...
175 =item B<notmuch-mutt> [I<OPTION>]... thread < I<MAIL>
177 =item B<notmuch-mutt> [I<OPTION>]... tag [I<TAGS>]... < I<MAIL>
183 notmuch-mutt is a frontend to the notmuch mail indexer capable of populating
184 a maildir with search results.
192 =item --output-dir DIR
194 Store search results as (symlink) messages under maildir DIR. Beware: DIR will
195 be overwritten. (Default: F<~/.cache/notmuch/mutt/results/>)
201 Instead of using command line search terms, prompt the user for them (only for
208 Remove emails with duplicate message-ids from search results. (Passes
209 --duplicate=1 to notmuch search command.) Note this can hide search
210 results if an email accidentally or maliciously uses the same message-id
211 as a different email.
217 Show usage information and exit.
221 =head1 INTEGRATION WITH MUTT
223 notmuch-mutt can be used to integrate notmuch with the Mutt mail user agent
224 (unsurprisingly, given the name). To that end, you should define macros like
225 the following in your Mutt configuration (usually one of: F<~/.muttrc>,
226 F</etc/Muttrc>, or a configuration snippet under F</etc/Muttrc.d/>):
229 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
230 <shell-escape>notmuch-mutt -r --prompt search<enter>\
231 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
232 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
233 "notmuch: search mail"
236 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
237 <pipe-message>notmuch-mutt -r thread<enter>\
238 <change-folder-readonly>`echo ${XDG_CACHE_HOME:-$HOME/.cache}/notmuch/mutt/results`<enter>\
239 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
240 "notmuch: reconstruct thread"
243 "<enter-command>set my_old_pipe_decode=\$pipe_decode my_old_wait_key=\$wait_key nopipe_decode nowait_key<enter>\
244 <pipe-message>notmuch-mutt tag -- -inbox<enter>\
245 <enter-command>set pipe_decode=\$my_old_pipe_decode wait_key=\$my_old_wait_key<enter>" \
246 "notmuch: remove message from inbox"
248 The first macro (activated by <F8>) prompts the user for notmuch search terms
249 and then jump to a temporary maildir showing search results. The second macro
250 (activated by <F9>) reconstructs the thread corresponding to the current mail
251 and show it as search results. The third macro (activated by <F6>) removes the
252 tag C<inbox> from the current message; by changing C<-inbox> this macro may be
253 customised to add or remove tags appropriate to the users notmuch work-flow.
255 To keep notmuch index current you should then periodically run C<notmuch
256 new>. Depending on your local mail setup, you might want to do that via cron,
257 as a hook triggered by mail retrieval, etc.
265 Copyright: (C) 2011-2012 Stefano Zacchiroli <zack@upsilon.cc>
267 License: GNU General Public License (GPL), version 3 or higher