[PATCH] notmuch-mutt: add --whole-thread flag

2012-10-09 Thread Stefan Hajnoczi
It can be useful to manipulate tags on an entire thread instead of just
a single message.  For example:

  # Mark entire thread unread
  notmuch-mutt tag --whole-thread -- -unread < MAIL

Add the --whole-thread flag which first looks up the thread for a given
message and then applies the tag action on the entire thread.

Signed-off-by: Stefan Hajnoczi 
---
Warning: I don't really know Perl :)

It would be nice if notmuch's query syntax could match threads.  Then
core notmuch CLI could perform this operation in a single command:

  # Mark entire thread unread given a single Message-ID
  notmuch tag thread-contains(id:1...@foo.com) -- -unread

Since the core cannot do this query today this patch bolts it on top for
"notmuch-mutt tag".

 contrib/notmuch-mutt/notmuch-mutt | 38 +++---
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/contrib/notmuch-mutt/notmuch-mutt 
b/contrib/notmuch-mutt/notmuch-mutt
index d14709d..8529940 100755
--- a/contrib/notmuch-mutt/notmuch-mutt
+++ b/contrib/notmuch-mutt/notmuch-mutt
@@ -125,6 +125,14 @@ sub get_message_id() {
 return $1;
 }
 
+sub get_thread_id($) {
+my ($mid) = @_;
+my $search_cmd = 'notmuch search --output=threads '
+. shell_quote("id:$mid");
+`$search_cmd` =~ /^thread:(.*)$/;
+return $1;
+}
+
 sub search_action($$$@) {
 my ($interactive, $results_dir, $remove_dups, @params) = @_;
 
@@ -141,20 +149,26 @@ sub search_action($$$@) {
 sub thread_action($$@) {
 my ($results_dir, $remove_dups, @params) = @_;
 
-my $mid = get_message_id();
-my $search_cmd = 'notmuch search --output=threads ' . 
shell_quote("id:$mid");
-my $tid = `$search_cmd`;   # get thread id
-chomp($tid);
+my $tid = get_thread_id(get_message_id());
 
-search($results_dir, $remove_dups, $tid);
+search($results_dir, $remove_dups, "thread:$tid");
 }
 
-sub tag_action(@) {
+sub tag_action($@) {
+my ($whole_thread, @params) = @_;
 my $mid = get_message_id();
 
-system("notmuch tag "
-  . shell_quote(join(' ', @_))
-  . " id:$mid");
+if ($whole_thread) {
+   my $tid = get_thread_id($mid);
+
+   system("notmuch tag "
+   . shell_quote(join(' ', @params))
+   . " thread:$tid");
+} else {
+   system("notmuch tag "
+   . shell_quote(join(' ', @params))
+   . " id:$mid");
+}
 }
 
 sub die_usage() {
@@ -170,12 +184,14 @@ sub main() {
 my $interactive = 0;
 my $help_needed = 0;
 my $remove_dups = 0;
+my $whole_thread = 0;
 
 my $getopt = GetOptions(
"h|help" => \$help_needed,
"o|output-dir=s" => \$results_dir,
"p|prompt" => \$interactive,
-   "r|remove-dups" => \$remove_dups);
+   "r|remove-dups" => \$remove_dups,
+   "w|whole-thread" => \$whole_thread);
 if (! $getopt || $#ARGV < 0) { die_usage() };
 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);
 
@@ -193,7 +209,7 @@ sub main() {
 } elsif ($action eq "thread") {
thread_action($results_dir, $remove_dups, @params);
 } elsif ($action eq "tag") {
-   tag_action(@params);
+   tag_action($whole_thread, @params);
 } else {
die_usage();
 }
-- 
1.7.11.4

___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


[PATCH] notmuch-mutt: add --whole-thread flag

2012-10-09 Thread Stefan Hajnoczi
It can be useful to manipulate tags on an entire thread instead of just
a single message.  For example:

  # Mark entire thread unread
  notmuch-mutt tag --whole-thread -- -unread < MAIL

Add the --whole-thread flag which first looks up the thread for a given
message and then applies the tag action on the entire thread.

Signed-off-by: Stefan Hajnoczi 
---
Warning: I don't really know Perl :)

It would be nice if notmuch's query syntax could match threads.  Then
core notmuch CLI could perform this operation in a single command:

  # Mark entire thread unread given a single Message-ID
  notmuch tag thread-contains(id:1234 at foo.com) -- -unread

Since the core cannot do this query today this patch bolts it on top for
"notmuch-mutt tag".

 contrib/notmuch-mutt/notmuch-mutt | 38 +++---
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/contrib/notmuch-mutt/notmuch-mutt 
b/contrib/notmuch-mutt/notmuch-mutt
index d14709d..8529940 100755
--- a/contrib/notmuch-mutt/notmuch-mutt
+++ b/contrib/notmuch-mutt/notmuch-mutt
@@ -125,6 +125,14 @@ sub get_message_id() {
 return $1;
 }

+sub get_thread_id($) {
+my ($mid) = @_;
+my $search_cmd = 'notmuch search --output=threads '
+. shell_quote("id:$mid");
+`$search_cmd` =~ /^thread:(.*)$/;
+return $1;
+}
+
 sub search_action($$$@) {
 my ($interactive, $results_dir, $remove_dups, @params) = @_;

@@ -141,20 +149,26 @@ sub search_action($$$@) {
 sub thread_action($$@) {
 my ($results_dir, $remove_dups, @params) = @_;

-my $mid = get_message_id();
-my $search_cmd = 'notmuch search --output=threads ' . 
shell_quote("id:$mid");
-my $tid = `$search_cmd`;   # get thread id
-chomp($tid);
+my $tid = get_thread_id(get_message_id());

-search($results_dir, $remove_dups, $tid);
+search($results_dir, $remove_dups, "thread:$tid");
 }

-sub tag_action(@) {
+sub tag_action($@) {
+my ($whole_thread, @params) = @_;
 my $mid = get_message_id();

-system("notmuch tag "
-  . shell_quote(join(' ', @_))
-  . " id:$mid");
+if ($whole_thread) {
+   my $tid = get_thread_id($mid);
+
+   system("notmuch tag "
+   . shell_quote(join(' ', @params))
+   . " thread:$tid");
+} else {
+   system("notmuch tag "
+   . shell_quote(join(' ', @params))
+   . " id:$mid");
+}
 }

 sub die_usage() {
@@ -170,12 +184,14 @@ sub main() {
 my $interactive = 0;
 my $help_needed = 0;
 my $remove_dups = 0;
+my $whole_thread = 0;

 my $getopt = GetOptions(
"h|help" => \$help_needed,
"o|output-dir=s" => \$results_dir,
"p|prompt" => \$interactive,
-   "r|remove-dups" => \$remove_dups);
+   "r|remove-dups" => \$remove_dups,
+   "w|whole-thread" => \$whole_thread);
 if (! $getopt || $#ARGV < 0) { die_usage() };
 my ($action, @params) = ($ARGV[0], @ARGV[1..$#ARGV]);

@@ -193,7 +209,7 @@ sub main() {
 } elsif ($action eq "thread") {
thread_action($results_dir, $remove_dups, @params);
 } elsif ($action eq "tag") {
-   tag_action(@params);
+   tag_action($whole_thread, @params);
 } else {
die_usage();
 }
-- 
1.7.11.4