All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 # mutt-notmuch - notmuch (of a) helper for Mutt
5 # Copyright: © 2011 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 mutt-notmuch > mutt-notmuch.1"
17 use Mail::Box::Maildir;
21 # create an empty maildir (if missing) or empty an existing maildir"
22 sub empty_maildir($) {
24 rmtree($maildir) if (-d $maildir);
25 my $folder = new Mail::Box::Maildir(folder => $maildir,
30 # search($maildir, $query)
31 # search mails according to $query with notmuch; store results in $maildir
33 my ($maildir, $query) = @_;
35 empty_maildir($maildir);
36 system("notmuch search --output=files $query"
37 . " | sed -e 's: :\\\\ :g'"
38 . " | xargs --no-run-if-empty ln -s -t $maildir/cur/");
41 sub search_action($$@) {
42 my ($interactive, $results_dir, @params) = @_;
45 search($results_dir, join(' ', @params));
50 print "search ('?' for man): ";
51 chomp($query = <STDIN>);
53 system("man notmuch");
54 } elsif ($query eq "") {
55 $done = 1; # quit doing nothing
57 search($results_dir, $query);
64 sub thread_action(@) {
65 my ($results_dir, @params) = @_;
67 my $mail = Mail::Internet->new(\*STDIN);
68 $mail->head->get('message-id') =~ /^<(.*)>$/; # get message-id
70 my $tid = `notmuch search --output=threads id:$mid`;# get thread id
73 search($results_dir, $tid);
77 my %podflags = ( "verbose" => 1,
83 my $results_dir = "$ENV{HOME}/.cache/mutt_results";
87 my $getopt = GetOptions(
88 "h|help" => \$help_needed,
89 "o|output-dir=s" => \$results_dir,
90 "p|prompt" => \$interactive);
91 if (! $getopt || $#ARGV < 0) { die_usage() };
92 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);
96 } elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) {
97 print STDERR "Error: no search term provided\n\n";
99 } elsif ($action eq "search") {
100 search_action($interactive, $results_dir, @params);
101 } elsif ($action eq "thread") {
102 thread_action($results_dir, @params);
114 mutt-notmuch - notmuch (of a) helper for Mutt
120 =item B<mutt-notmuch> [I<OPTION>]... search [I<SEARCH-TERM>]...
122 =item B<mutt-notmuch> [I<OPTION>]... thread < I<MAIL>
128 mutt-notmuch is a frontend to the notmuch mail indexer capable of populating
129 maildir with search results.
137 =item --output-dir DIR
139 Store search results as (symlink) messages under maildir DIR. Beware: DIR will
140 be overwritten. (Default: F<~/.cache/mutt_results/>)
146 Instead of using command line search terms, prompt the user for them (only for
153 Show usage information and exit.
157 =head1 INTEGRATION WITH MUTT
159 mutt-notmuch can be used to integrate notmuch with the Mutt mail user agent
160 (unsurprisingly, given the name). To that end, you should define the following
161 macros in your F<~/.muttrc> (replacing F<~/bin/mutt-notmuch> for the actual
162 location of mutt-notmuch on your system):
165 "<enter-command>unset wait_key<enter><shell-escape>~/bin/mutt-notmuch --prompt search<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
166 "search mail (using notmuch)"
168 "<enter-command>unset wait_key<enter><pipe-message>~/bin/mutt-notmuch thread<enter><change-folder-readonly>~/.cache/mutt_results<enter><enter-command>set wait_key<enter>" \
169 "search and reconstruct owning thread (using notmuch)"
171 The first macro (activated by <F8>) will prompt the user for notmuch search
172 terms and then jump to a temporary maildir showing search results. The second
173 macro (activated by <F9>) will reconstruct the thread corresponding to the
174 current mail and show it as search results.
176 To keep notmuch index current you should then periodically run C<notmuch
177 new>. Depending on your local mail setup, you might want to do that via cron,
178 as a hook triggered by mail retrieval, etc.
186 Copyright: (C) 2011 Stefano Zacchiroli <zack@upsilon.cc>
188 License: GNU General Public License (GPL), version 3 or higher