#
# mutt-notmuch - notmuch (of a) helper for Mutt
#
-# Copyright: © 2011 Stefano Zacchiroli <zack@upsilon.cc>
+# Copyright: © 2011 Stefano Zacchiroli <zack@upsilon.cc>
# License: GNU General Public License (GPL), version 3 or above
#
# See the bottom of this file for more documentation.
my ($maildir) = (@_);
rmtree($maildir) if (-d $maildir);
my $folder = new Mail::Box::Maildir(folder => $maildir,
- create => 1);
+ create => 1);
$folder->close();
}
empty_maildir($maildir);
system("notmuch search --output=files $query"
- . " | sed -e 's: :\\\\ :g'"
- . " | xargs --no-run-if-empty ln -s -t $maildir/cur/");
+ . " | sed -e 's: :\\\\ :g'"
+ . " | xargs --no-run-if-empty ln -s -t $maildir/cur/");
+}
+
+sub prompt($) {
+ my ($text) = @_;
+ my $query = "";
+ while (1) {
+ print $text;
+ chomp($query = <STDIN>);
+ if ($query eq "?") {
+ system("man notmuch");
+ } else {
+ 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));
+ search($results_dir, join(' ', @params));
} else {
- my $query = "";
- my $done = 0;
- while (! $done) {
- print "search ('?' for man): ";
- chomp($query = <STDIN>);
- if ($query eq "?") {
- system("man notmuch");
- } elsif ($query eq "") {
- $done = 1; # quit doing nothing
- } else {
- search($results_dir, $query);
- $done = 1;
- }
- }
+ my $query = prompt("search ('?' for man): ");
+ if ($query ne "") {
+ search($results_dir,$query);
+ }
}
}
sub thread_action(@) {
my ($results_dir, @params) = @_;
-
- my $mail = Mail::Internet->new(\*STDIN);
- $mail->head->get('message-id') =~ /^<(.*)>$/; # get message-id
- my $mid = $1;
+ 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 ".join(' ', @_)." id:$mid");
+}
+
sub die_usage() {
my %podflags = ( "verbose" => 1,
- "exitval" => 2 );
+ "exitval" => 2 );
pod2usage(%podflags);
}
my $help_needed = 0;
my $getopt = GetOptions(
- "h|help" => \$help_needed,
- "o|output-dir=s" => \$results_dir,
- "p|prompt" => \$interactive);
+ "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]);
if ($help_needed) {
- die_usage();
+ die_usage();
} elsif ($action eq "search" && $#ARGV == 0 && ! $interactive) {
- print STDERR "Error: no search term provided\n\n";
- die_usage();
+ print STDERR "Error: no search term provided\n\n";
+ die_usage();
} elsif ($action eq "search") {
- search_action($interactive, $results_dir, @params);
+ search_action($interactive, $results_dir, @params);
} elsif ($action eq "thread") {
- thread_action($results_dir, @params);
+ thread_action($results_dir, @params);
+ } elsif ($action eq "tag") {
+ tag_action(@params);
} else {
- die_usage();
+ die_usage();
}
}
=item B<mutt-notmuch> [I<OPTION>]... thread < I<MAIL>
+=item B<mutt-notmuch> [I<OPTION>]... tag [I<TAGS>]... < I<MAIL>
+
=back
=head1 DESCRIPTION
macro index <F9> \
"<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>" \
"search and reconstruct owning thread (using notmuch)"
+ macro index <F6> \
+ "<enter-command>unset wait_key<enter><pipe-message>~/bin/mutt-notmuch tag -inbox<enter>" \
+ "remove message from inbox (using notmuch)"
The first macro (activated by <F8>) will prompt the user for notmuch search
terms and then jump to a temporary maildir showing search results. The second
macro (activated by <F9>) will reconstruct the thread corresponding to the
-current mail and show it as search results.
+current mail and show it as search results. The third macro (activated by <F6>)
+removes the tag C<inbox> from the current message; by changing C<-inbox> this
+macro may be customised to add or remove tags appropriate to the users notmuch
+work-flow.
To keep notmuch index current you should then periodically run C<notmuch
new>. Depending on your local mail setup, you might want to do that via cron,