]> granicus.if.org Git - neomutt/commitdiff
notmuch: count flagged messages
authorAustin Ray <austin@austinray.io>
Wed, 31 Oct 2018 00:55:07 +0000 (20:55 -0400)
committerRichard Russon <rich@flatcap.org>
Sun, 4 Nov 2018 00:57:08 +0000 (00:57 +0000)
`nm_nonctx_get_count(...)` did not count flagged messages. This means
they could not display flagged message counts in the sidebar _unless_ it
was the active mailbox.

This commit introduces NmFlaggedTag, mirrored from NmUnreadTag, and will
query the database for flagged messages.

browser.c
doc/manual.xml.head
init.h
mailbox.c
notmuch/mutt_notmuch.c
notmuch/mutt_notmuch.h
sidebar.c

index 6e9187a9cbc4b258d3b16ada81a75c9c53584ce5..64a3e328b38a7c317be55dc14664fa8f43fbd8d8 100644 (file)
--- a/browser.c
+++ b/browser.c
@@ -943,7 +943,7 @@ static int examine_vfolders(struct Menu *menu, struct BrowserState *state)
   {
     if (nm_path_probe(np->m->path, NULL) == MUTT_NOTMUCH)
     {
-      nm_nonctx_get_count(np->m->path, &np->m->msg_count, &np->m->msg_unread);
+      nm_nonctx_get_count(np->m);
       add_folder(menu, state, np->m->path, np->m->desc, NULL, np->m, NULL);
       continue;
     }
index 886a2f818d9e9fbe8421e672a8827f0c0aaea403..dd000e36e17503346f058e54382ebe9821c914a2 100644 (file)
@@ -14555,6 +14555,12 @@ virtual-mailboxes "My INBOX" "notmuch://?query=tag:inbox"
             Restricts number of messages/threads in the result. The default
             limit is nm_db_limit.
           </para>
+          <para>
+            Due to a limitation with <literal>libnotmuch</literal>, unread and
+            flagged message count may be inaccurate with limit statements.
+            <literal>libnotmuch</literal> cannot return a specific tag count
+            within the first X messages of a query.
+          </para>
           <para>
             <emphasis role="bold">type=&lt;threads|messages&gt;</emphasis>
           </para>
diff --git a/init.h b/init.h
index 4d02d0576dbc3729e81551e88675b071a1fd0f2a..847e95790e45617ebe081df09e71a9a877ee0bbc 100644 (file)
--- a/init.h
+++ b/init.h
@@ -2189,6 +2189,13 @@ struct ConfigDef MuttVars[] = {
   ** variable is used to count unread messages in DB only. All other NeoMutt commands
   ** use standard (e.g. maildir) flags.
   */
+  { "nm_flagged_tag", DT_STRING, R_NONE, &NmFlaggedTag, IP "flagged" },
+  /*
+  ** .pp
+  ** This variable specifies notmuch tag which is used for flagged messages. The
+  ** variable is used to count flagged messages in DB only. All other NeoMutt commands
+  ** use standard (e.g. maildir) flags.
+  */
 #endif
 #ifdef USE_NNTP
   { "nntp_authenticators", DT_STRING, R_NONE, &NntpAuthenticators, 0 },
index 9b5774485432ebf10bc387275397fce07f3a6ff3..429f93ae95c42cfccfd106b49097688b4f8687f9 100644 (file)
--- a/mailbox.c
+++ b/mailbox.c
@@ -382,7 +382,7 @@ static void mailbox_check(struct Mailbox *m, struct stat *ctx_sb, bool check_sta
         m->msg_count = 0;
         m->msg_unread = 0;
         m->msg_flagged = 0;
-        nm_nonctx_get_count(m->path, &m->msg_count, &m->msg_unread);
+        nm_nonctx_get_count(m);
         if (m->msg_unread > 0)
         {
           MailboxCount++;
index df154c67a768b9a8538f46ab9da2cd2c754e814c..4701aa44eeb248a21ab6e7ac8d869da88d8360e4 100644 (file)
@@ -81,7 +81,8 @@ char *NmQueryType; ///< Config: (notmuch) Default query type: 'threads' or 'mess
 int NmQueryWindowCurrentPosition; ///< Config: (notmuch) Position of current search window
 char *NmQueryWindowTimebase; ///< Config: (notmuch) Units for the time duration
 char *NmRecordTags; ///< Config: (notmuch) Tags to apply to the 'record' mailbox (sent mail)
-char *NmUnreadTag; ///< Config: (notmuch) Tag to use for unread messages
+char *NmUnreadTag;  ///< Config: (notmuch) Tag to use for unread messages
+char *NmFlaggedTag; ///< Config: (notmuch) Tag to use for flagged messages
 
 /**
  * string_to_query_type - Lookup a query type
@@ -1497,11 +1498,12 @@ done:
 
 /**
  * count_query - Count the results of a query
- * @param db   Notmuch database
- * @param qstr Query to execute
- * @retval num Numner of results
+ * @param db    Notmuch database
+ * @param qstr  Query to execute
+ * @param limit Maximum number of results
+ * @retval num Number of results
  */
-static unsigned int count_query(notmuch_database_t *db, const char *qstr)
+static unsigned int count_query(notmuch_database_t *db, const char *qstr, int limit)
 {
   notmuch_query_t *q = notmuch_query_create(db, qstr);
   if (!q)
@@ -1522,6 +1524,9 @@ static unsigned int count_query(notmuch_database_t *db, const char *qstr)
   notmuch_query_destroy(q);
   mutt_debug(1, "nm: count '%s', result=%d\n", qstr, res);
 
+  if (limit > 0 && res > limit)
+    res = limit;
+
   return res;
 }
 
@@ -1882,17 +1887,15 @@ int nm_update_filename(struct Mailbox *m, const char *old, const char *new, stru
 
 /**
  * nm_nonctx_get_count - Perform some queries without an open database
- * @param path Notmuch database path
- * @param all  Count of all emails
- * @param new  Count of new emails
+ * @param m Mailbox to examine
  * @retval  0 Success
  * @retval -1 Failure
  */
-int nm_nonctx_get_count(char *path, int *all, int *new)
+int nm_nonctx_get_count(struct Mailbox *m)
 {
   struct UrlQueryString *item = NULL;
   struct Url url = { U_UNKNOWN };
-  char *url_holder = mutt_str_strdup(path);
+  char *url_holder = mutt_str_strdup(m->path);
   char *db_filename = NULL, *db_query = NULL;
   notmuch_database_t *db = NULL;
   int rc = -1;
@@ -1901,7 +1904,7 @@ int nm_nonctx_get_count(char *path, int *all, int *new)
 
   if (url_parse(&url, url_holder) < 0)
   {
-    mutt_error(_("failed to parse notmuch uri: %s"), path);
+    mutt_error(_("failed to parse notmuch uri: %s"), m->path);
     goto done;
   }
 
@@ -1937,24 +1940,20 @@ int nm_nonctx_get_count(char *path, int *all, int *new)
     goto done;
 
   /* all emails */
-  if (all)
-  {
-    *all = count_query(db, db_query);
+  m->msg_count = count_query(db, db_query, limit);
 
-    // If we have a non-zero limit, we shouldn't exceed it.
-    if (limit > 0 && *all > limit)
-      *all = limit;
-  }
+  // holder variable for extending query to unread/flagged
+  char *qstr = NULL;
 
-  /* new messages */
-  if (new)
-  {
-    char *qstr = NULL;
+  // unread messages
+  safe_asprintf(&qstr, "( %s ) tag:%s", db_query, NmUnreadTag);
+  m->msg_unread = count_query(db, qstr, limit);
+  FREE(&qstr);
 
-    safe_asprintf(&qstr, "( %s ) tag:%s", db_query, NmUnreadTag);
-    *new = count_query(db, qstr);
-    FREE(&qstr);
-  }
+  // flagged messages
+  safe_asprintf(&qstr, "( %s ) tag:%s", db_query, NmFlaggedTag);
+  m->msg_flagged = count_query(db, qstr, limit);
+  FREE(&qstr);
 
   rc = 0;
 done:
index f057afcb9844f6d102979943859086b8e178791e..ba12760b2b6de4502df1d28d4dccb578db01da5b 100644 (file)
@@ -51,6 +51,7 @@ extern int   NmQueryWindowCurrentPosition;
 extern char *NmQueryWindowTimebase;
 extern char *NmRecordTags;
 extern char *NmUnreadTag;
+extern char *NmFlaggedTag;
 
 extern struct MxOps mx_notmuch_ops;
 
@@ -61,7 +62,7 @@ char *nm_email_get_folder        (struct Email *e);
 void  nm_db_longrun_done            (struct Mailbox *m);
 void  nm_db_longrun_init            (struct Mailbox *m, bool writable);
 bool  nm_message_is_still_queried(struct Mailbox *m, struct Email *e);
-int   nm_nonctx_get_count        (char *path, int *all, int *new);
+int   nm_nonctx_get_count        (struct Mailbox *m);
 bool  nm_normalize_uri           (const char *uri, char *buf, size_t buflen);
 void  nm_parse_type_from_query   (struct NmMboxData *mdata, char *buf);
 int   nm_path_probe              (const char *path, const struct stat *st);
index cf6192eb7c4a29de71baf9d8bcfb92d8cf9d40fe..cc4f15af3ec2197a2c584b5250a533906166b9dd 100644 (file)
--- a/sidebar.c
+++ b/sidebar.c
@@ -876,7 +876,7 @@ static void draw_sidebar(int num_rows, int num_cols, int div_width)
     {
 #ifdef USE_NOTMUCH
       if (m->magic == MUTT_NOTMUCH)
-        nm_nonctx_get_count(m->realpath, &m->msg_count, &m->msg_unread);
+        nm_nonctx_get_count(m);
       else
 #endif
       {