[PATCH] cli: Hooks for tag-command

2012-07-17 Thread Dominik Peteler
hello,

I attached some modifications which I made to notmuch. Comes with man pages and 
test.

regards

dominik



There are two hooks:
 * pre-tag: Run before tagging
 * post-tag: Run after

This allows users to react on changes of tags. For example,
you might want to move a message to a special Maildir
depending on its notmuch tags.
---
 man/man1/notmuch-tag.1   | 17 +
 man/man5/notmuch-hooks.5 | 23 +++
 notmuch-tag.c| 25 +
 test/hooks   | 36 
 4 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/man/man1/notmuch-tag.1 b/man/man1/notmuch-tag.1
index d810e1b..8d8b7b2 100644
--- a/man/man1/notmuch-tag.1
+++ b/man/man1/notmuch-tag.1
@@ -4,6 +4,7 @@ notmuch-tag \- add/remove tags for all messages matching the 
search terms
 
 .SH SYNOPSIS
 .B notmuch tag
+.RB "[" --no-hooks "]"
 .RI  "+<" tag> "|\-<" tag "> [...] [\-\-] <" search-term ">..."
 
 .SH DESCRIPTION
@@ -29,6 +30,22 @@ updates the maildir flags according to tag changes if the
 configuration option is enabled. See \fBnotmuch-config\fR(1) for
 details.
 
+The
+.B tag
+command supports hooks. See  \fBnotmuch-hooks(5)\fR
+for more details on hooks.
+
+Supported options for
+.B tag
+include
+.RS 4
+.TP 4
+.BR \-\-no\-hooks
+
+Prevents hooks from being run.
+.RE
+.RE
+
 .SH SEE ALSO
 
 \fBnotmuch\fR(1), \fBnotmuch-config\fR(1), \fBnotmuch-count\fR(1),
diff --git a/man/man5/notmuch-hooks.5 b/man/man5/notmuch-hooks.5
index b914a29..7399627 100644
--- a/man/man5/notmuch-hooks.5
+++ b/man/man5/notmuch-hooks.5
@@ -38,6 +38,29 @@ the scan or import.
 Typically this hook is used to perform additional query\-based tagging on the
 imported messages.
 .RE
+.RS 4
+.TP 4
+.B pre\-tag
+This hook is invoked by the
+.B tag
+command before tagging messages. If this
+hook exits with a non-zero status, notmuch will abort further processing of the
+.B tag
+command.
+
+Typically this hook is used for syncing the Maildir with notmuch tags.
+.RE
+.RS 4
+.TP 4
+.B post\-tag
+This hook is invoked by the
+.B tag
+command after messages have been tagged. The hook will not be run if there 
have been any errors during
+the tagging.
+
+Typically this hook is used for syncing the Maildir with notmuch tags.
+.RE
+
 
 .SH SEE ALSO
 
diff --git a/notmuch-tag.c b/notmuch-tag.c
index 7d18639..e98d3a0 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -174,9 +174,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 int tag_ops_count = 0;
 char *query_string;
 notmuch_config_t *config;
+const char *db_path;
 notmuch_database_t *notmuch;
 struct sigaction action;
 notmuch_bool_t synchronize_flags;
+notmuch_bool_t run_hooks = TRUE;
 int i;
 int ret;
 
@@ -198,11 +200,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 }
 
 for (i = 0; i < argc; i++) {
-   if (strcmp (argv[i], "--") == 0) {
+   if (strcmp (argv[i], "--no-hooks") == 0) {
+   run_hooks = FALSE;
+   } else if (strcmp (argv[i], "--") == 0) {
i++;
break;
-   }
-   if (argv[i][0] == '+' || argv[i][0] == '-') {
+   } else if (argv[i][0] == '+' || argv[i][0] == '-') {
tag_ops[tag_ops_count].tag = argv[i] + 1;
tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
tag_ops_count++;
@@ -229,7 +232,15 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 if (config == NULL)
return 1;
 
-if (notmuch_database_open (notmuch_config_get_database_path (config),
+db_path = notmuch_config_get_database_path (config);
+
+if (run_hooks) {
+   ret = notmuch_run_hook (db_path, "pre-tag");
+   if (ret)
+   return ret;
+}
+
+if (notmuch_database_open (db_path,
   NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
return 1;
 
@@ -239,5 +250,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 
 notmuch_database_destroy (notmuch);
 
+if (run_hooks) {
+   ret = notmuch_run_hook (db_path, "post-tag");
+   if (ret)
+   return ret;
+}
+
 return ret;
 }
diff --git a/test/hooks b/test/hooks
index 77e8569..ae857cc 100755
--- a/test/hooks
+++ b/test/hooks
@@ -31,6 +31,7 @@ rm_hooks () {
 # add a message to generate mail dir and database
 add_message
 
+# {pre,post}-new hooks
 test_begin_subtest "pre-new is run"
 rm_hooks
 generate_message
@@ -101,4 +102,39 @@ EOF
 chmod +x "${HOOK_DIR}/pre-new"
 test_expect_code 1 "hook execution failure" "notmuch new"
 
+
+
+# {pre,post}-tag hooks
+test_begin_subtest "pre-tag is run"
+rm_hooks
+generate_message
+create_echo_hook "pre-tag" expected output
+notmuch tag +foo -- '*' > /dev/null
+test_expect_equal_file expected output
+
+test_begin_subtest "post-tag is run"
+rm_hooks
+generate_message
+create_echo_hook "post-tag" expected output
+notmuch tag +foo -- '*'  > /dev/null
+test_expect_equal_file expected output
+
+

[PATCH] emacs: Fix notmuch-message-mark-replied.

2012-07-17 Thread Tomi Ollila
On Sun, Jul 08 2012, Mark Walters wrote:

> On Sun, 03 Jun 2012, Ingo Lohmar  wrote:
>> notmuch-message-mark-replied used "apply" to change message tags
>> according to notmuch-message-replied-tags after sending a reply.  This
>> works if the latter is a single-element list.  But with the recently
>> changed format of tag changes, it breaks for multiple-element lists.
>> Use "funcall" to properly pass the list of tag changes as a single
>> argument.
>
> This looks correct to me: the bug is still in current master and this
> does fix it.
>
> As Jamie says, it would be nice to have a test. Unfortunately, that is
> beyond my test/emacs skills. 
>
> Note this bug does not occur with default configuration but the option
> is a defcustom option, and following the example in that option's
> documentation will cause the problem.
>
> Since this patch has been around for over a month I think it should
> probably be applied.

I just tested this, without the patch setting multiple tags in reply
fails with pretty ugly error message. with the patch setting one or
multiple tags works. 
The fix is trivial (and obvious if one looks at it).

I remove the needs-review tag.

>
> Best wishes
>
> Mark

Tomi

>
>> ---
>>  emacs/notmuch-message.el |2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/emacs/notmuch-message.el b/emacs/notmuch-message.el
>> index 5964caa..d3738bf 100644
>> --- a/emacs/notmuch-message.el
>> +++ b/emacs/notmuch-message.el
>> @@ -45,7 +45,7 @@ the \"inbox\" and \"todo\", you would set
>>  (concat "+" str)
>>str))
>>notmuch-message-replied-tags)))
>> -(apply 'notmuch-tag (notmuch-id-to-query (car (car rep))) tags)
>> +(funcall 'notmuch-tag (notmuch-id-to-query (car (car rep))) tags)
>>  
>>  (add-hook 'message-send-hook 'notmuch-message-mark-replied)
>>  
>> -- 
>> 1.7.10


Notmuch scripts (again), now with more usenet

2012-07-17 Thread c...@webprojekty.cz
On Sun, Jul 15, 2012 at 10:36:54AM -0600, David Bremner wrote:
> ccx at webprojekty.cz writes:
> 
> > I hope it's now in the form acceptable for inclusion to contrib.
> 
> Dear Jan;
> 
> Without looking too much at the details I think this is reasonable to
> include in contrib, as a set of examples if nothing else. Still, I'm
> curious about a few things. Are there actual users (other than the
> author)?  What would we do about bug reports? Will you follow the list,
> or would we explicitely tell people to contact you directly?

I might not always have time to read the entirety of the ML, so it would
be great if people CC me when posting there.

> I guess some of the python experts might want to discuss the coding
> style of notmuch-new.py. pylint goes ballistic, for whatever that is
> worth.

That's mainly because of tabs used for indentation, which used to be
considered a viable alternative, but is now considered obsolete. I
reformatted the code to conform to pep8 guidelines.

> For the shell stuff, I noticed lots of unquoted parameter expansion;
> also at some point you make a temporary directory using $$
> directly rather than using e.g. mktemp.

Zsh does not word-split parameter expansion by default, this is
intentional. The temporary directories are created in xdg cache dir,
which should not be susceptible to symlink attacks etc. and the pid
actually helps to correlate the directories to processes, eg. when you
want to autoclean after searches that got killed for some reason.

> What did you have in mind for a license? since there is no linking here,
> it does not have to be GPL3+, but some standard Free license is needed I
> think; since I'm also maintaining the Debian package, I don't want to
> have to remove your stuff before uploading to Debian.

You can consider it public domain. I guess I should add wtfpl text to my
repo. :-)

> d

Thanks very much for your rewiev.
-- next part --
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: 
<http://notmuchmail.org/pipermail/notmuch/attachments/20120717/d0cd0af7/attachment.pgp>


[PATCH] cli: Hooks for tag-command

2012-07-17 Thread Dominik Peteler
hello,

I attached some modifications which I made to notmuch. Comes with man pages and 
test.

regards

dominik



There are two hooks:
 * pre-tag: Run before tagging
 * post-tag: Run after

This allows users to react on changes of tags. For example,
you might want to move a message to a special Maildir
depending on its notmuch tags.
---
 man/man1/notmuch-tag.1   | 17 +
 man/man5/notmuch-hooks.5 | 23 +++
 notmuch-tag.c| 25 +
 test/hooks   | 36 
 4 files changed, 97 insertions(+), 4 deletions(-)

diff --git a/man/man1/notmuch-tag.1 b/man/man1/notmuch-tag.1
index d810e1b..8d8b7b2 100644
--- a/man/man1/notmuch-tag.1
+++ b/man/man1/notmuch-tag.1
@@ -4,6 +4,7 @@ notmuch-tag \- add/remove tags for all messages matching the 
search terms

 .SH SYNOPSIS
 .B notmuch tag
+.RB "[" --no-hooks "]"
 .RI  "+<" tag> "|\-<" tag "> [...] [\-\-] <" search-term ">..."

 .SH DESCRIPTION
@@ -29,6 +30,22 @@ updates the maildir flags according to tag changes if the
 configuration option is enabled. See \fBnotmuch-config\fR(1) for
 details.

+The
+.B tag
+command supports hooks. See  \fBnotmuch-hooks(5)\fR
+for more details on hooks.
+
+Supported options for
+.B tag
+include
+.RS 4
+.TP 4
+.BR \-\-no\-hooks
+
+Prevents hooks from being run.
+.RE
+.RE
+
 .SH SEE ALSO

 \fBnotmuch\fR(1), \fBnotmuch-config\fR(1), \fBnotmuch-count\fR(1),
diff --git a/man/man5/notmuch-hooks.5 b/man/man5/notmuch-hooks.5
index b914a29..7399627 100644
--- a/man/man5/notmuch-hooks.5
+++ b/man/man5/notmuch-hooks.5
@@ -38,6 +38,29 @@ the scan or import.
 Typically this hook is used to perform additional query\-based tagging on the
 imported messages.
 .RE
+.RS 4
+.TP 4
+.B pre\-tag
+This hook is invoked by the
+.B tag
+command before tagging messages. If this
+hook exits with a non-zero status, notmuch will abort further processing of the
+.B tag
+command.
+
+Typically this hook is used for syncing the Maildir with notmuch tags.
+.RE
+.RS 4
+.TP 4
+.B post\-tag
+This hook is invoked by the
+.B tag
+command after messages have been tagged. The hook will not be run if there 
have been any errors during
+the tagging.
+
+Typically this hook is used for syncing the Maildir with notmuch tags.
+.RE
+

 .SH SEE ALSO

diff --git a/notmuch-tag.c b/notmuch-tag.c
index 7d18639..e98d3a0 100644
--- a/notmuch-tag.c
+++ b/notmuch-tag.c
@@ -174,9 +174,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 int tag_ops_count = 0;
 char *query_string;
 notmuch_config_t *config;
+const char *db_path;
 notmuch_database_t *notmuch;
 struct sigaction action;
 notmuch_bool_t synchronize_flags;
+notmuch_bool_t run_hooks = TRUE;
 int i;
 int ret;

@@ -198,11 +200,12 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 }

 for (i = 0; i < argc; i++) {
-   if (strcmp (argv[i], "--") == 0) {
+   if (strcmp (argv[i], "--no-hooks") == 0) {
+   run_hooks = FALSE;
+   } else if (strcmp (argv[i], "--") == 0) {
i++;
break;
-   }
-   if (argv[i][0] == '+' || argv[i][0] == '-') {
+   } else if (argv[i][0] == '+' || argv[i][0] == '-') {
tag_ops[tag_ops_count].tag = argv[i] + 1;
tag_ops[tag_ops_count].remove = (argv[i][0] == '-');
tag_ops_count++;
@@ -229,7 +232,15 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])
 if (config == NULL)
return 1;

-if (notmuch_database_open (notmuch_config_get_database_path (config),
+db_path = notmuch_config_get_database_path (config);
+
+if (run_hooks) {
+   ret = notmuch_run_hook (db_path, "pre-tag");
+   if (ret)
+   return ret;
+}
+
+if (notmuch_database_open (db_path,
   NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much))
return 1;

@@ -239,5 +250,11 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])

 notmuch_database_destroy (notmuch);

+if (run_hooks) {
+   ret = notmuch_run_hook (db_path, "post-tag");
+   if (ret)
+   return ret;
+}
+
 return ret;
 }
diff --git a/test/hooks b/test/hooks
index 77e8569..ae857cc 100755
--- a/test/hooks
+++ b/test/hooks
@@ -31,6 +31,7 @@ rm_hooks () {
 # add a message to generate mail dir and database
 add_message

+# {pre,post}-new hooks
 test_begin_subtest "pre-new is run"
 rm_hooks
 generate_message
@@ -101,4 +102,39 @@ EOF
 chmod +x "${HOOK_DIR}/pre-new"
 test_expect_code 1 "hook execution failure" "notmuch new"

+
+
+# {pre,post}-tag hooks
+test_begin_subtest "pre-tag is run"
+rm_hooks
+generate_message
+create_echo_hook "pre-tag" expected output
+notmuch tag +foo -- '*' > /dev/null
+test_expect_equal_file expected output
+
+test_begin_subtest "post-tag is run"
+rm_hooks
+generate_message
+create_echo_hook "post-tag" expected output
+notmuch tag +foo -- '*'  > /dev/null
+test_expect_equal_file expected output
+
+test_begin_sub

Re: Notmuch scripts (again), now with more usenet

2012-07-17 Thread ccx
On Sun, Jul 15, 2012 at 10:36:54AM -0600, David Bremner wrote:
> c...@webprojekty.cz writes:
> 
> > I hope it's now in the form acceptable for inclusion to contrib.
> 
> Dear Jan;
> 
> Without looking too much at the details I think this is reasonable to
> include in contrib, as a set of examples if nothing else. Still, I'm
> curious about a few things. Are there actual users (other than the
> author)?  What would we do about bug reports? Will you follow the list,
> or would we explicitely tell people to contact you directly?

I might not always have time to read the entirety of the ML, so it would
be great if people CC me when posting there.

> I guess some of the python experts might want to discuss the coding
> style of notmuch-new.py. pylint goes ballistic, for whatever that is
> worth.

That's mainly because of tabs used for indentation, which used to be
considered a viable alternative, but is now considered obsolete. I
reformatted the code to conform to pep8 guidelines.

> For the shell stuff, I noticed lots of unquoted parameter expansion;
> also at some point you make a temporary directory using $$
> directly rather than using e.g. mktemp.

Zsh does not word-split parameter expansion by default, this is
intentional. The temporary directories are created in xdg cache dir,
which should not be susceptible to symlink attacks etc. and the pid
actually helps to correlate the directories to processes, eg. when you
want to autoclean after searches that got killed for some reason.

> What did you have in mind for a license? since there is no linking here,
> it does not have to be GPL3+, but some standard Free license is needed I
> think; since I'm also maintaining the Debian package, I don't want to
> have to remove your stuff before uploading to Debian.

You can consider it public domain. I guess I should add wtfpl text to my
repo. :-)

> d

Thanks very much for your rewiev.


signature.asc
Description: Digital signature
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch


Re: [PATCH] emacs: Fix notmuch-message-mark-replied.

2012-07-17 Thread Tomi Ollila
On Sun, Jul 08 2012, Mark Walters wrote:

> On Sun, 03 Jun 2012, Ingo Lohmar  wrote:
>> notmuch-message-mark-replied used "apply" to change message tags
>> according to notmuch-message-replied-tags after sending a reply.  This
>> works if the latter is a single-element list.  But with the recently
>> changed format of tag changes, it breaks for multiple-element lists.
>> Use "funcall" to properly pass the list of tag changes as a single
>> argument.
>
> This looks correct to me: the bug is still in current master and this
> does fix it.
>
> As Jamie says, it would be nice to have a test. Unfortunately, that is
> beyond my test/emacs skills. 
>
> Note this bug does not occur with default configuration but the option
> is a defcustom option, and following the example in that option's
> documentation will cause the problem.
>
> Since this patch has been around for over a month I think it should
> probably be applied.

I just tested this, without the patch setting multiple tags in reply
fails with pretty ugly error message. with the patch setting one or
multiple tags works. 
The fix is trivial (and obvious if one looks at it).

I remove the needs-review tag.

>
> Best wishes
>
> Mark

Tomi

>
>> ---
>>  emacs/notmuch-message.el |2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/emacs/notmuch-message.el b/emacs/notmuch-message.el
>> index 5964caa..d3738bf 100644
>> --- a/emacs/notmuch-message.el
>> +++ b/emacs/notmuch-message.el
>> @@ -45,7 +45,7 @@ the \"inbox\" and \"todo\", you would set
>>  (concat "+" str)
>>str))
>>notmuch-message-replied-tags)))
>> -(apply 'notmuch-tag (notmuch-id-to-query (car (car rep))) tags)
>> +(funcall 'notmuch-tag (notmuch-id-to-query (car (car rep))) tags)
>>  
>>  (add-hook 'message-send-hook 'notmuch-message-mark-replied)
>>  
>> -- 
>> 1.7.10
___
notmuch mailing list
notmuch@notmuchmail.org
http://notmuchmail.org/mailman/listinfo/notmuch