Re: [PATCH 0/5] Notmuch Pick (WIP or contrib)
[notmuch-archives.git] / 9f / 89fa6563d9da7810eba6394d30b4016f8b0af1
1 Return-Path: <jan@ryngle.com>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 16D1C431FBF\r
6         for <notmuch@notmuchmail.org>; Tue, 24 Nov 2009 19:30:32 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id MZVYX--60eKu for <notmuch@notmuchmail.org>;\r
11         Tue, 24 Nov 2009 19:30:31 -0800 (PST)\r
12 Received: from mail-bw0-f224.google.com (mail-bw0-f224.google.com\r
13         [209.85.218.224])\r
14         by olra.theworths.org (Postfix) with ESMTP id 43124431FAE\r
15         for <notmuch@notmuchmail.org>; Tue, 24 Nov 2009 19:30:31 -0800 (PST)\r
16 Received: by bwz24 with SMTP id 24so5548847bwz.30\r
17         for <notmuch@notmuchmail.org>; Tue, 24 Nov 2009 19:30:30 -0800 (PST)\r
18 Received: by 10.204.157.24 with SMTP id z24mr6964174bkw.208.1259119830174;\r
19         Tue, 24 Nov 2009 19:30:30 -0800 (PST)\r
20 Received: from x61s.janakj (r2c34.net.upc.cz [62.245.66.34])\r
21         by mx.google.com with ESMTPS id 16sm1608458bwz.15.2009.11.24.19.30.29\r
22         (version=TLSv1/SSLv3 cipher=RC4-MD5);\r
23         Tue, 24 Nov 2009 19:30:29 -0800 (PST)\r
24 Received: by x61s.janakj (Postfix, from userid 1000)\r
25         id DB9D7440656; Wed, 25 Nov 2009 04:30:22 +0100 (CET)\r
26 From: Jan Janak <jan@ryngle.com>\r
27 To: notmuch@notmuchmail.org\r
28 Date: Wed, 25 Nov 2009 04:30:22 +0100\r
29 Message-Id: <1259119822-22593-2-git-send-email-jan@ryngle.com>\r
30 X-Mailer: git-send-email 1.6.3.3\r
31 In-Reply-To: <1259119822-22593-1-git-send-email-jan@ryngle.com>\r
32 References: <1258935056-9746-2-git-send-email-jan@ryngle.com>\r
33         <1259119822-22593-1-git-send-email-jan@ryngle.com>\r
34 Subject: [notmuch] [PATCH 2/2] search-tags: Add support for search-terms.\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.12\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Wed, 25 Nov 2009 03:30:32 -0000\r
48 \r
49 This patch adds support for search-terms to 'notmuch search-tags'. If\r
50 no search-term is provided then the command returns a list of all tags\r
51 from the database.\r
52 \r
53 If the user provides one or more search-terms as arguments then the\r
54 command collects tags from matching messages only.\r
55 \r
56 This could be used by functions in the Emacs mode to further limit the\r
57 list of tags offered for completion. For example, functions that remove\r
58 tags from message(s) could offer only tags present in the message(s).\r
59 \r
60 Signed-off-by: Jan Janak <jan@ryngle.com>\r
61 ---\r
62  notmuch-search-tags.c |   55 ++++++++++++++++++++++++++++++++++++------------\r
63  notmuch.c             |   13 +++++++----\r
64  2 files changed, 49 insertions(+), 19 deletions(-)\r
65 \r
66 diff --git a/notmuch-search-tags.c b/notmuch-search-tags.c\r
67 index 1201165..7a1305e 100644\r
68 --- a/notmuch-search-tags.c\r
69 +++ b/notmuch-search-tags.c\r
70 @@ -21,34 +21,31 @@\r
71  \r
72  #include "notmuch-client.h"\r
73  \r
74 -static int\r
75 -list_all_tags (notmuch_database_t* db)\r
76 +static void\r
77 +print_tags (notmuch_tags_t *tags)\r
78  {\r
79 -    notmuch_tags_t* tags;\r
80 -    const char* t;\r
81 +    const char *t;\r
82  \r
83 -    if ((tags = notmuch_database_get_all_tags (db)) == NULL) {\r
84 -       fprintf (stderr, "Error while obtaining tags from the database.\n");\r
85 -       return 1;\r
86 -    }\r
87 -\r
88 -    while((t = notmuch_tags_get (tags))) {\r
89 +    while ((t = notmuch_tags_get (tags))) {\r
90         printf ("%s\n", t);\r
91         notmuch_tags_advance (tags);\r
92      }\r
93 -\r
94 -    notmuch_tags_destroy (tags);\r
95 -    return 0;\r
96  }\r
97  \r
98  int\r
99  notmuch_search_tags_command (void *ctx, int argc, char *argv[])\r
100  {\r
101 +    notmuch_messages_t *msgs;\r
102 +    notmuch_tags_t *tags;\r
103      notmuch_config_t *config;\r
104      notmuch_database_t *db;\r
105 +    notmuch_query_t *query;\r
106 +    char *query_str;\r
107  \r
108 +    tags = NULL;\r
109      config = NULL;\r
110      db = NULL;\r
111 +    query = NULL;\r
112  \r
113      if ((config = notmuch_config_open (ctx, NULL, NULL)) == NULL) {\r
114         goto error;\r
115 @@ -60,12 +57,42 @@ notmuch_search_tags_command (void *ctx, int argc, char *argv[])\r
116         goto error;\r
117      }\r
118  \r
119 -    if (list_all_tags (db) != 0) goto error;\r
120 +    if (argc > 0) {\r
121 +       if ((query_str = query_string_from_args (ctx, argc, argv)) == NULL) {\r
122 +           fprintf (stderr, "Out of memory.\n");\r
123 +           goto error;\r
124 +       }\r
125 +\r
126 +       if (*query_str == '\0') {\r
127 +           fprintf (stderr, "Error: Invalid search string.\n");\r
128 +           goto error;\r
129 +       }\r
130 +\r
131 +       if ((query = notmuch_query_create (db, query_str)) == NULL) {\r
132 +           fprintf (stderr, "Out of memory\n");\r
133 +           goto error;\r
134 +       }\r
135 +\r
136 +\r
137 +       msgs = notmuch_query_search_messages (query);\r
138 +       if ((tags = notmuch_messages_collect_tags (msgs)) == NULL) goto error;\r
139 +    } else {\r
140 +       if ((tags = notmuch_database_get_all_tags (db)) == NULL) {\r
141 +           fprintf (stderr, "Error while getting tags from the database.\n");\r
142 +           goto error;\r
143 +       }\r
144 +    }\r
145 +\r
146 +    print_tags (tags);\r
147  \r
148 +    notmuch_tags_destroy (tags);\r
149 +    if (query) notmuch_query_destroy (query);\r
150      notmuch_database_close (db);\r
151      return 0;\r
152  \r
153  error:\r
154 +    if (tags) notmuch_tags_destroy (tags);\r
155 +    if (query) notmuch_query_destroy (query);\r
156      if (db) notmuch_database_close (db);\r
157      return 1;\r
158  }\r
159 diff --git a/notmuch.c b/notmuch.c\r
160 index c57bb5c..5b0284c 100644\r
161 --- a/notmuch.c\r
162 +++ b/notmuch.c\r
163 @@ -255,11 +255,14 @@ command_t commands[] = {\r
164        "\t\t\"notmuch restore\" command provides you a way to import\n"\r
165        "\t\tall of your tags (or labels as sup calls them)." },\r
166      { "search-tags", notmuch_search_tags_command,\r
167 -      NULL,\r
168 -      "List all tags found in the database.",\r
169 -      "\t\tThis command returns an alphabetically sorted list of all tags\n"\r
170 -      "\t\tthat are present in the database. In other words, the resulting\n"\r
171 -      "\t\tlist is a collection of all tags from all messages." },\r
172 +      "[<search-terms> [...] ]",\r
173 +      "\t\tList all tags found in the database or matching messages.",\r
174 +      "\t\tRun this command without any search-term(s) to obtain a list\n"\r
175 +      "\t\tof all tags found in the database. If you provide one or more\n"\r
176 +      "\t\tsearch-terms as argument(s) then the resulting list will\n"\r
177 +      "\t\tcontain tags only from messages that match the search-term(s).\n"\r
178 +      "\n"\r
179 +      "\t\tIn both cases the list will be alphabetically sorted." },\r
180      { "help", notmuch_help_command,\r
181        "[<command>]",\r
182        "\t\tThis message, or more detailed help for the named command.",\r
183 -- \r
184 1.6.3.3\r
185 \r