]> granicus.if.org Git - neomutt/commitdiff
fix getting count of new messages for non-trivial query
authorKirill A. Shutemov <kirill@shutemov.name>
Fri, 11 Oct 2013 18:34:57 +0000 (11:34 -0700)
committerRichard Russon <rich@flatcap.org>
Mon, 4 Apr 2016 15:30:07 +0000 (16:30 +0100)
Currtly, we ask for count of new messages by appending tag:unread (or
whatever NotmuchUnreadTag has) to db query. It works if your query is
very simple: no 'OR', 'NOT', etc.

If you will ask new messages for query like
 "to:foo@example.com or to:bar@example.net"
mutt will query db for count of
 "to:foo@example.com or to:bar@example.net tag:unread"
and notmuch will understand it as
 "to:foo@example.com or (to:bar@example.net tag:unread)"
which, obviously, is not what you wanted.

Optimizations like
if (strstr(db_query, NotmuchUnreadTag))
*new = all ? *all : count_query(db, db_query);
doesn't work for the same reason.

Let's fix all this by construct query with proper parenthesis:
 "(to:foo@example.com or to:bar@example.net) tag:unread"

Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
mutt_notmuch.c

index bca3f39676ac1178d43cbabcc97f004ab5499c11..40192d550d611b6edf6ab3f15b98eaeecd5ea99d 100644 (file)
@@ -1623,21 +1623,12 @@ int nm_nonctx_get_count(char *path, int *all, int *new)
 
        /* new messages */
        if (new) {
-               if (strstr(db_query, NotmuchUnreadTag))
-                       *new = all ? *all : count_query(db, db_query);
-               else {
-                       size_t qsz = strlen(db_query)
-                                       + sizeof(" and tag:")
-                                       + strlen(NotmuchUnreadTag);
-                       char *qstr = safe_malloc(qsz);
-
-                       if (!qstr)
-                               goto done;
+               char *qstr;
 
-                       snprintf(qstr, qsz, "%s and tag:%s", db_query, NotmuchUnreadTag);
-                       *new = count_query(db, qstr);
-                       FREE(&qstr);
-               }
+               safe_asprintf(&qstr, "( %s ) tag:%s",
+                               db_query, NotmuchUnreadTag);
+               *new = count_query(db, qstr);
+               FREE(&qstr);
        }
 
        rc = 0;