Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 2c / 8ca893e0c001286e205bbd5135e7f2b78d86bc
1 Return-Path: <jani@nikula.org>\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 CF703431FBD\r
6         for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:47:10 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id kV5TRnHCUHEK for <notmuch@notmuchmail.org>;\r
17         Tue, 29 Jan 2013 13:47:07 -0800 (PST)\r
18 Received: from mail-lb0-f176.google.com (mail-lb0-f176.google.com\r
19         [209.85.217.176]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 380AF431FC7\r
22         for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:46:59 -0800 (PST)\r
23 Received: by mail-lb0-f176.google.com with SMTP id s4so1340488lbc.35\r
24         for <notmuch@notmuchmail.org>; Tue, 29 Jan 2013 13:46:57 -0800 (PST)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=x-received:from:to:subject:date:message-id:x-mailer:in-reply-to\r
28         :references:in-reply-to:references:x-gm-message-state;\r
29         bh=4t2uEylr2N5obOfCLkTkGVzs1Ec2uAUIcZvFbmFI3dE=;\r
30         b=JbUXHFpBT+O/rDowxgsmN0sC+BQTxSPVd/1U23L3sWs7FU01J/X8AquL0Vb1gnHmNz\r
31         s/+Duoyupwnz9VXPdOf7BucMbWck21ed3d9gtO+ryeA9OLYh65siUm/88St2rLv6Dlg5\r
32         c8mdhaLUW/Uv5mv9jnFQNxANh5JTtpOX6G+CvydOt55srfCxtDzFv4XL4Ra2hMANoqnu\r
33         k6uMvXuwdUzR57wIzIk7nrIUER7wNDIK0sp5QWil1/Oh9KUinFqusbv/mKXlmkbLmtwg\r
34         IpUgclfYjqx0xbWa8UFiJoz5v3NJZ/II5WJqQofIC39Phgs2GIsyhnzYwLk94XmxGJZV\r
35         ZZKw==\r
36 X-Received: by 10.112.40.228 with SMTP id a4mr1071015lbl.26.1359496017633;\r
37         Tue, 29 Jan 2013 13:46:57 -0800 (PST)\r
38 Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
39         [80.223.81.27])\r
40         by mx.google.com with ESMTPS id ew10sm5593460lbb.10.2013.01.29.13.46.55\r
41         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
42         Tue, 29 Jan 2013 13:46:56 -0800 (PST)\r
43 From: Jani Nikula <jani@nikula.org>\r
44 To: notmuch@notmuchmail.org\r
45 Subject: [PATCH 5/6] cli: move config open/close to main() from subcommands\r
46 Date: Tue, 29 Jan 2013 23:46:23 +0200\r
47 Message-Id:\r
48  <b6b3571c9bb31621d17d61a9bf7a2401577fb213.1359495450.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.10.4\r
50 In-Reply-To: <cover.1359495450.git.jani@nikula.org>\r
51 References: <cover.1359495450.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1359495450.git.jani@nikula.org>\r
53 References: <cover.1359495450.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQlLVB+b+0L+3JL7lZ7hS8N6abYoVFZth5Zq7LX0apkxW2HFFrnhSuDuTzuRUUU6pyo1torh\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Tue, 29 Jan 2013 21:47:11 -0000\r
69 \r
70 This allows specifying config file as a top level argument to notmuch,\r
71 and generally makes it possible to override config file options in\r
72 main(), without having to touch the subcommands.\r
73 \r
74 This also makes notmuch config the talloc context for subcommands.\r
75 ---\r
76  notmuch-client.h  |   30 +++++++++++---------------\r
77  notmuch-config.c  |   40 +++++++----------------------------\r
78  notmuch-count.c   |   11 +++-------\r
79  notmuch-dump.c    |    7 +-----\r
80  notmuch-new.c     |   17 ++++++---------\r
81  notmuch-reply.c   |   15 +++++--------\r
82  notmuch-restore.c |   11 +++-------\r
83  notmuch-search.c  |   15 +++++--------\r
84  notmuch-setup.c   |   17 ++++++---------\r
85  notmuch-show.c    |   15 +++++--------\r
86  notmuch-tag.c     |   15 +++++--------\r
87  notmuch.c         |   61 +++++++++++++++++++++++++++--------------------------\r
88  12 files changed, 91 insertions(+), 163 deletions(-)\r
89 \r
90 diff --git a/notmuch-client.h b/notmuch-client.h\r
91 index b3dcb21..45749a6 100644\r
92 --- a/notmuch-client.h\r
93 +++ b/notmuch-client.h\r
94 @@ -150,6 +150,8 @@ chomp_newline (char *str)\r
95   */\r
96  extern int notmuch_format_version;\r
97  \r
98 +typedef struct _notmuch_config notmuch_config_t;\r
99 +\r
100  /* Commands that support structured output should support the\r
101   * following argument\r
102   *  { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 }\r
103 @@ -169,40 +171,34 @@ int\r
104  notmuch_crypto_cleanup (notmuch_crypto_t *crypto);\r
105  \r
106  int\r
107 -notmuch_count_command (void *ctx, int argc, char *argv[]);\r
108 -\r
109 -int\r
110 -notmuch_dump_command (void *ctx, int argc, char *argv[]);\r
111 +notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]);\r
112  \r
113  int\r
114 -notmuch_new_command (void *ctx, int argc, char *argv[]);\r
115 +notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]);\r
116  \r
117  int\r
118 -notmuch_reply_command (void *ctx, int argc, char *argv[]);\r
119 +notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]);\r
120  \r
121  int\r
122 -notmuch_restore_command (void *ctx, int argc, char *argv[]);\r
123 +notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]);\r
124  \r
125  int\r
126 -notmuch_search_command (void *ctx, int argc, char *argv[]);\r
127 +notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]);\r
128  \r
129  int\r
130 -notmuch_setup_command (void *ctx, int argc, char *argv[]);\r
131 +notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]);\r
132  \r
133  int\r
134 -notmuch_show_command (void *ctx, int argc, char *argv[]);\r
135 +notmuch_setup_command (notmuch_config_t *config, int argc, char *argv[]);\r
136  \r
137  int\r
138 -notmuch_tag_command (void *ctx, int argc, char *argv[]);\r
139 +notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]);\r
140  \r
141  int\r
142 -notmuch_search_tags_command (void *ctx, int argc, char *argv[]);\r
143 +notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]);\r
144  \r
145  int\r
146 -notmuch_cat_command (void *ctx, int argc, char *argv[]);\r
147 -\r
148 -int\r
149 -notmuch_config_command (void *ctx, int argc, char *argv[]);\r
150 +notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]);\r
151  \r
152  const char *\r
153  notmuch_time_relative_date (const void *ctx, time_t then);\r
154 @@ -243,8 +239,6 @@ json_quote_str (const void *ctx, const char *str);\r
155  \r
156  /* notmuch-config.c */\r
157  \r
158 -typedef struct _notmuch_config notmuch_config_t;\r
159 -\r
160  notmuch_config_t *\r
161  notmuch_config_open (void *ctx,\r
162                      const char *filename,\r
163 diff --git a/notmuch-config.c b/notmuch-config.c\r
164 index 247fbe4..48312e3 100644\r
165 --- a/notmuch-config.c\r
166 +++ b/notmuch-config.c\r
167 @@ -705,14 +705,8 @@ _item_split (char *item, char **group, char **key)\r
168  }\r
169  \r
170  static int\r
171 -notmuch_config_command_get (void *ctx, char *item)\r
172 +notmuch_config_command_get (notmuch_config_t *config, char *item)\r
173  {\r
174 -    notmuch_config_t *config;\r
175 -\r
176 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
177 -    if (config == NULL)\r
178 -       return 1;\r
179 -\r
180      if (strcmp(item, "database.path") == 0) {\r
181         printf ("%s\n", notmuch_config_get_database_path (config));\r
182      } else if (strcmp(item, "user.name") == 0) {\r
183 @@ -756,25 +750,17 @@ notmuch_config_command_get (void *ctx, char *item)\r
184         g_strfreev (value);\r
185      }\r
186  \r
187 -    notmuch_config_close (config);\r
188 -\r
189      return 0;\r
190  }\r
191  \r
192  static int\r
193 -notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])\r
194 +notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char *argv[])\r
195  {\r
196 -    notmuch_config_t *config;\r
197      char *group, *key;\r
198 -    int ret;\r
199  \r
200      if (_item_split (item, &group, &key))\r
201         return 1;\r
202  \r
203 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
204 -    if (config == NULL)\r
205 -       return 1;\r
206 -\r
207      /* With only the name of an item, we clear it from the\r
208       * configuration file.\r
209       *\r
210 @@ -795,23 +781,15 @@ notmuch_config_command_set (void *ctx, char *item, int argc, char *argv[])\r
211         break;\r
212      }\r
213  \r
214 -    ret = notmuch_config_save (config);\r
215 -    notmuch_config_close (config);\r
216 -\r
217 -    return ret;\r
218 +    return notmuch_config_save (config);\r
219  }\r
220  \r
221  static int\r
222 -notmuch_config_command_list (void *ctx)\r
223 +notmuch_config_command_list (notmuch_config_t *config)\r
224  {\r
225 -    notmuch_config_t *config;\r
226      char **groups;\r
227      size_t g, groups_length;\r
228  \r
229 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
230 -    if (config == NULL)\r
231 -       return 1;\r
232 -\r
233      groups = g_key_file_get_groups (config->key_file, &groups_length);\r
234      if (groups == NULL)\r
235         return 1;\r
236 @@ -841,13 +819,11 @@ notmuch_config_command_list (void *ctx)\r
237  \r
238      g_strfreev (groups);\r
239  \r
240 -    notmuch_config_close (config);\r
241 -\r
242      return 0;\r
243  }\r
244  \r
245  int\r
246 -notmuch_config_command (void *ctx, int argc, char *argv[])\r
247 +notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])\r
248  {\r
249      argc--; argv++; /* skip subcommand argument */\r
250  \r
251 @@ -862,16 +838,16 @@ notmuch_config_command (void *ctx, int argc, char *argv[])\r
252                      "one argument.\n");\r
253             return 1;\r
254         }\r
255 -       return notmuch_config_command_get (ctx, argv[1]);\r
256 +       return notmuch_config_command_get (config, argv[1]);\r
257      } else if (strcmp (argv[0], "set") == 0) {\r
258         if (argc < 2) {\r
259             fprintf (stderr, "Error: notmuch config set requires at least "\r
260                      "one argument.\n");\r
261             return 1;\r
262         }\r
263 -       return notmuch_config_command_set (ctx, argv[1], argc - 2, argv + 2);\r
264 +       return notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);\r
265      } else if (strcmp (argv[0], "list") == 0) {\r
266 -       return notmuch_config_command_list (ctx);\r
267 +       return notmuch_config_command_list (config);\r
268      }\r
269  \r
270      fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",\r
271 diff --git a/notmuch-count.c b/notmuch-count.c\r
272 index 61722ed..390794f 100644\r
273 --- a/notmuch-count.c\r
274 +++ b/notmuch-count.c\r
275 @@ -33,9 +33,8 @@ enum {\r
276  };\r
277  \r
278  int\r
279 -notmuch_count_command (void *ctx, int argc, char *argv[])\r
280 +notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])\r
281  {\r
282 -    notmuch_config_t *config;\r
283      notmuch_database_t *notmuch;\r
284      notmuch_query_t *query;\r
285      char *query_str;\r
286 @@ -62,22 +61,18 @@ notmuch_count_command (void *ctx, int argc, char *argv[])\r
287         return 1;\r
288      }\r
289  \r
290 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
291 -    if (config == NULL)\r
292 -       return 1;\r
293 -\r
294      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
295                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
296         return 1;\r
297  \r
298 -    query_str = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
299 +    query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
300      if (query_str == NULL) {\r
301         fprintf (stderr, "Out of memory.\n");\r
302         return 1;\r
303      }\r
304  \r
305      if (*query_str == '\0') {\r
306 -       query_str = talloc_strdup (ctx, "");\r
307 +       query_str = talloc_strdup (config, "");\r
308      }\r
309  \r
310      query = notmuch_query_create (notmuch, query_str);\r
311 diff --git a/notmuch-dump.c b/notmuch-dump.c\r
312 index 845a67e..2024e30 100644\r
313 --- a/notmuch-dump.c\r
314 +++ b/notmuch-dump.c\r
315 @@ -23,9 +23,8 @@\r
316  #include "string-util.h"\r
317  \r
318  int\r
319 -notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
320 +notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])\r
321  {\r
322 -    notmuch_config_t *config;\r
323      notmuch_database_t *notmuch;\r
324      notmuch_query_t *query;\r
325      FILE *output = stdout;\r
326 @@ -34,10 +33,6 @@ notmuch_dump_command (unused (void *ctx), int argc, char *argv[])\r
327      notmuch_tags_t *tags;\r
328      const char *query_str = "";\r
329  \r
330 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
331 -    if (config == NULL)\r
332 -       return 1;\r
333 -\r
334      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
335                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
336         return 1;\r
337 diff --git a/notmuch-new.c b/notmuch-new.c\r
338 index 4915418..faa33f1 100644\r
339 --- a/notmuch-new.c\r
340 +++ b/notmuch-new.c\r
341 @@ -840,9 +840,8 @@ _remove_directory (void *ctx,\r
342  }\r
343  \r
344  int\r
345 -notmuch_new_command (void *ctx, int argc, char *argv[])\r
346 +notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
347  {\r
348 -    notmuch_config_t *config;\r
349      notmuch_database_t *notmuch;\r
350      add_files_state_t add_files_state;\r
351      double elapsed;\r
352 @@ -875,10 +874,6 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
353         return 1;\r
354      }\r
355  \r
356 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
357 -    if (config == NULL)\r
358 -       return 1;\r
359 -\r
360      add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length);\r
361      add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length);\r
362      add_files_state.synchronize_flags = notmuch_config_get_maildir_synchronize_flags (config);\r
363 @@ -890,7 +885,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
364             return ret;\r
365      }\r
366  \r
367 -    dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");\r
368 +    dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch");\r
369  \r
370      if (stat (dot_notmuch_path, &st)) {\r
371         int count;\r
372 @@ -941,9 +936,9 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
373      add_files_state.removed_messages = add_files_state.renamed_messages = 0;\r
374      gettimeofday (&add_files_state.tv_start, NULL);\r
375  \r
376 -    add_files_state.removed_files = _filename_list_create (ctx);\r
377 -    add_files_state.removed_directories = _filename_list_create (ctx);\r
378 -    add_files_state.directory_mtimes = _filename_list_create (ctx);\r
379 +    add_files_state.removed_files = _filename_list_create (config);\r
380 +    add_files_state.removed_directories = _filename_list_create (config);\r
381 +    add_files_state.directory_mtimes = _filename_list_create (config);\r
382  \r
383      if (! debugger_is_active () && add_files_state.output_is_a_tty\r
384         && ! add_files_state.verbose) {\r
385 @@ -970,7 +965,7 @@ notmuch_new_command (void *ctx, int argc, char *argv[])\r
386  \r
387      gettimeofday (&tv_start, NULL);\r
388      for (f = add_files_state.removed_directories->head, i = 0; f && !interrupted; f = f->next, i++) {\r
389 -       ret = _remove_directory (ctx, notmuch, f->filename, &add_files_state);\r
390 +       ret = _remove_directory (config, notmuch, f->filename, &add_files_state);\r
391         if (ret)\r
392             goto DONE;\r
393         if (do_print_progress) {\r
394 diff --git a/notmuch-reply.c b/notmuch-reply.c\r
395 index 9da42b9..e151f78 100644\r
396 --- a/notmuch-reply.c\r
397 +++ b/notmuch-reply.c\r
398 @@ -702,9 +702,8 @@ enum {\r
399  };\r
400  \r
401  int\r
402 -notmuch_reply_command (void *ctx, int argc, char *argv[])\r
403 +notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])\r
404  {\r
405 -    notmuch_config_t *config;\r
406      notmuch_database_t *notmuch;\r
407      notmuch_query_t *query;\r
408      char *query_string;\r
409 @@ -752,21 +751,17 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
410         reply_format_func = notmuch_reply_format_headers_only;\r
411      } else if (format == FORMAT_JSON) {\r
412         reply_format_func = notmuch_reply_format_sprinter;\r
413 -       sp = sprinter_json_create (ctx, stdout);\r
414 +       sp = sprinter_json_create (config, stdout);\r
415      } else if (format == FORMAT_SEXP) {\r
416         reply_format_func = notmuch_reply_format_sprinter;\r
417 -       sp = sprinter_sexp_create (ctx, stdout);\r
418 +       sp = sprinter_sexp_create (config, stdout);\r
419      } else {\r
420         reply_format_func = notmuch_reply_format_default;\r
421      }\r
422  \r
423      notmuch_exit_if_unsupported_format ();\r
424  \r
425 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
426 -    if (config == NULL)\r
427 -       return 1;\r
428 -\r
429 -    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
430 +    query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
431      if (query_string == NULL) {\r
432         fprintf (stderr, "Out of memory\n");\r
433         return 1;\r
434 @@ -787,7 +782,7 @@ notmuch_reply_command (void *ctx, int argc, char *argv[])\r
435         return 1;\r
436      }\r
437  \r
438 -    if (reply_format_func (ctx, config, query, &params, reply_all, sp) != 0)\r
439 +    if (reply_format_func (config, config, query, &params, reply_all, sp) != 0)\r
440         return 1;\r
441  \r
442      notmuch_crypto_cleanup (&params.crypto);\r
443 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
444 index dd2507f..1419621 100644\r
445 --- a/notmuch-restore.c\r
446 +++ b/notmuch-restore.c\r
447 @@ -120,9 +120,8 @@ parse_sup_line (void *ctx, char *line,\r
448  }\r
449  \r
450  int\r
451 -notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
452 +notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])\r
453  {\r
454 -    notmuch_config_t *config;\r
455      notmuch_database_t *notmuch;\r
456      notmuch_bool_t accumulate = FALSE;\r
457      tag_op_flag_t flags = 0;\r
458 @@ -139,10 +138,6 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
459      int opt_index;\r
460      int input_format = DUMP_FORMAT_AUTO;\r
461  \r
462 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
463 -    if (config == NULL)\r
464 -       return 1;\r
465 -\r
466      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
467                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
468         return 1;\r
469 @@ -187,7 +182,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
470         return 1;\r
471      }\r
472  \r
473 -    tag_ops = tag_op_list_create (ctx);\r
474 +    tag_ops = tag_op_list_create (config);\r
475      if (tag_ops == NULL) {\r
476         fprintf (stderr, "Out of memory.\n");\r
477         return 1;\r
478 @@ -226,7 +221,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
479         if (line_ctx != NULL)\r
480             talloc_free (line_ctx);\r
481  \r
482 -       line_ctx = talloc_new (ctx);\r
483 +       line_ctx = talloc_new (config);\r
484         if (input_format == DUMP_FORMAT_SUP) {\r
485             ret = parse_sup_line (line_ctx, line, &query_string, tag_ops);\r
486         } else {\r
487 diff --git a/notmuch-search.c b/notmuch-search.c\r
488 index fac6663..e658639 100644\r
489 --- a/notmuch-search.c\r
490 +++ b/notmuch-search.c\r
491 @@ -290,9 +290,8 @@ enum {\r
492  };\r
493  \r
494  int\r
495 -notmuch_search_command (void *ctx, int argc, char *argv[])\r
496 +notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
497  {\r
498 -    notmuch_config_t *config;\r
499      notmuch_database_t *notmuch;\r
500      notmuch_query_t *query;\r
501      char *query_str;\r
502 @@ -349,20 +348,20 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
503  \r
504      switch (format_sel) {\r
505      case NOTMUCH_FORMAT_TEXT:\r
506 -       format = sprinter_text_create (ctx, stdout);\r
507 +       format = sprinter_text_create (config, stdout);\r
508         break;\r
509      case NOTMUCH_FORMAT_TEXT0:\r
510         if (output == OUTPUT_SUMMARY) {\r
511             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
512             return 1;\r
513         }\r
514 -       format = sprinter_text0_create (ctx, stdout);\r
515 +       format = sprinter_text0_create (config, stdout);\r
516         break;\r
517      case NOTMUCH_FORMAT_JSON:\r
518 -       format = sprinter_json_create (ctx, stdout);\r
519 +       format = sprinter_json_create (config, stdout);\r
520         break;\r
521      case NOTMUCH_FORMAT_SEXP:\r
522 -       format = sprinter_sexp_create (ctx, stdout);\r
523 +       format = sprinter_sexp_create (config, stdout);\r
524         break;\r
525      default:\r
526         /* this should never happen */\r
527 @@ -371,10 +370,6 @@ notmuch_search_command (void *ctx, int argc, char *argv[])\r
528  \r
529      notmuch_exit_if_unsupported_format ();\r
530  \r
531 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
532 -    if (config == NULL)\r
533 -       return 1;\r
534 -\r
535      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
536                                NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))\r
537         return 1;\r
538 diff --git a/notmuch-setup.c b/notmuch-setup.c\r
539 index 72d862a..475248b 100644\r
540 --- a/notmuch-setup.c\r
541 +++ b/notmuch-setup.c\r
542 @@ -120,12 +120,11 @@ parse_tag_list (void *ctx, char *response)\r
543  }\r
544  \r
545  int\r
546 -notmuch_setup_command (unused (void *ctx),\r
547 +notmuch_setup_command (notmuch_config_t *config,\r
548                        unused (int argc), unused (char *argv[]))\r
549  {\r
550      char *response = NULL;\r
551      size_t response_size = 0;\r
552 -    notmuch_config_t *config;\r
553      const char **old_other_emails;\r
554      size_t old_other_emails_len;\r
555      GPtrArray *other_emails;\r
556 @@ -146,8 +145,6 @@ notmuch_setup_command (unused (void *ctx),\r
557         chomp_newline (response);                               \\r
558      } while (0)\r
559  \r
560 -    config = notmuch_config_open (ctx, NULL, TRUE);\r
561 -\r
562      if (notmuch_config_is_new (config))\r
563         welcome_message_pre_setup ();\r
564  \r
565 @@ -167,16 +164,16 @@ notmuch_setup_command (unused (void *ctx),\r
566      for (i = 0; i < old_other_emails_len; i++) {\r
567         prompt ("Additional email address [%s]: ", old_other_emails[i]);\r
568         if (strlen (response))\r
569 -           g_ptr_array_add (other_emails, talloc_strdup (ctx, response));\r
570 +           g_ptr_array_add (other_emails, talloc_strdup (config, response));\r
571         else\r
572 -           g_ptr_array_add (other_emails, talloc_strdup (ctx,\r
573 +           g_ptr_array_add (other_emails, talloc_strdup (config,\r
574                                                          old_other_emails[i]));\r
575      }\r
576  \r
577      do {\r
578         prompt ("Additional email address [Press 'Enter' if none]: ");\r
579         if (strlen (response))\r
580 -           g_ptr_array_add (other_emails, talloc_strdup (ctx, response));\r
581 +           g_ptr_array_add (other_emails, talloc_strdup (config, response));\r
582      } while (strlen (response));\r
583      if (other_emails->len)\r
584         notmuch_config_set_user_other_email (config,\r
585 @@ -190,7 +187,7 @@ notmuch_setup_command (unused (void *ctx),\r
586      if (strlen (response)) {\r
587         const char *absolute_path;\r
588  \r
589 -       absolute_path = make_path_absolute (ctx, response);\r
590 +       absolute_path = make_path_absolute (config, response);\r
591         notmuch_config_set_database_path (config, absolute_path);\r
592      }\r
593  \r
594 @@ -201,7 +198,7 @@ notmuch_setup_command (unused (void *ctx),\r
595      prompt ("]: ");\r
596  \r
597      if (strlen (response)) {\r
598 -       GPtrArray *tags = parse_tag_list (ctx, response);\r
599 +       GPtrArray *tags = parse_tag_list (config, response);\r
600  \r
601         notmuch_config_set_new_tags (config, (const char **) tags->pdata,\r
602                                      tags->len);\r
603 @@ -217,7 +214,7 @@ notmuch_setup_command (unused (void *ctx),\r
604      prompt ("]: ");\r
605  \r
606      if (strlen (response)) {\r
607 -       GPtrArray *tags = parse_tag_list (ctx, response);\r
608 +       GPtrArray *tags = parse_tag_list (config, response);\r
609  \r
610         notmuch_config_set_search_exclude_tags (config,\r
611                                                 (const char **) tags->pdata,\r
612 diff --git a/notmuch-show.c b/notmuch-show.c\r
613 index 5ae5d7d..c2ec122 100644\r
614 --- a/notmuch-show.c\r
615 +++ b/notmuch-show.c\r
616 @@ -1056,9 +1056,8 @@ enum {\r
617  };\r
618  \r
619  int\r
620 -notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
621 +notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])\r
622  {\r
623 -    notmuch_config_t *config;\r
624      notmuch_database_t *notmuch;\r
625      notmuch_query_t *query;\r
626      char *query_string;\r
627 @@ -1176,11 +1175,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
628      else\r
629         params.entire_thread = FALSE;\r
630  \r
631 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
632 -    if (config == NULL)\r
633 -       return 1;\r
634 -\r
635 -    query_string = query_string_from_args (ctx, argc-opt_index, argv+opt_index);\r
636 +    query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);\r
637      if (query_string == NULL) {\r
638         fprintf (stderr, "Out of memory\n");\r
639         return 1;\r
640 @@ -1202,11 +1197,11 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
641      }\r
642  \r
643      /* Create structure printer. */\r
644 -    sprinter = format->new_sprinter(ctx, stdout);\r
645 +    sprinter = format->new_sprinter(config, stdout);\r
646  \r
647      /* If a single message is requested we do not use search_excludes. */\r
648      if (params.part >= 0)\r
649 -       ret = do_show_single (ctx, query, format, sprinter, &params);\r
650 +       ret = do_show_single (config, query, format, sprinter, &params);\r
651      else {\r
652         /* We always apply set the exclude flag. The\r
653          * exclude=true|false option controls whether or not we return\r
654 @@ -1225,7 +1220,7 @@ notmuch_show_command (void *ctx, unused (int argc), unused (char *argv[]))\r
655             params.omit_excluded = FALSE;\r
656         }\r
657  \r
658 -       ret = do_show (ctx, query, format, sprinter, &params);\r
659 +       ret = do_show (config, query, format, sprinter, &params);\r
660      }\r
661  \r
662      notmuch_crypto_cleanup (&params.crypto);\r
663 diff --git a/notmuch-tag.c b/notmuch-tag.c\r
664 index 148e856..0e73197 100644\r
665 --- a/notmuch-tag.c\r
666 +++ b/notmuch-tag.c\r
667 @@ -178,11 +178,10 @@ tag_file (void *ctx, notmuch_database_t *notmuch, tag_op_flag_t flags,\r
668  }\r
669  \r
670  int\r
671 -notmuch_tag_command (void *ctx, int argc, char *argv[])\r
672 +notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])\r
673  {\r
674      tag_op_list_t *tag_ops = NULL;\r
675      char *query_string = NULL;\r
676 -    notmuch_config_t *config;\r
677      notmuch_database_t *notmuch;\r
678      struct sigaction action;\r
679      tag_op_flag_t tag_flags = TAG_FLAG_NONE;\r
680 @@ -225,21 +224,17 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
681             return 1;\r
682         }\r
683      } else {\r
684 -       tag_ops = tag_op_list_create (ctx);\r
685 +       tag_ops = tag_op_list_create (config);\r
686         if (tag_ops == NULL) {\r
687             fprintf (stderr, "Out of memory.\n");\r
688             return 1;\r
689         }\r
690  \r
691 -       if (parse_tag_command_line (ctx, argc - opt_index, argv + opt_index,\r
692 +       if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,\r
693                                     &query_string, tag_ops))\r
694             return 1;\r
695      }\r
696  \r
697 -    config = notmuch_config_open (ctx, NULL, FALSE);\r
698 -    if (config == NULL)\r
699 -       return 1;\r
700 -\r
701      if (notmuch_database_open (notmuch_config_get_database_path (config),\r
702                                NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))\r
703         return 1;\r
704 @@ -248,9 +243,9 @@ notmuch_tag_command (void *ctx, int argc, char *argv[])\r
705         tag_flags |= TAG_FLAG_MAILDIR_SYNC;\r
706  \r
707      if (batch)\r
708 -       ret = tag_file (ctx, notmuch, tag_flags, input);\r
709 +       ret = tag_file (config, notmuch, tag_flags, input);\r
710      else\r
711 -       ret = tag_query (ctx, notmuch, query_string, tag_ops, tag_flags);\r
712 +       ret = tag_query (config, notmuch, query_string, tag_ops, tag_flags);\r
713  \r
714      notmuch_database_destroy (notmuch);\r
715  \r
716 diff --git a/notmuch.c b/notmuch.c\r
717 index 67d5772..f4bfeaa 100644\r
718 --- a/notmuch.c\r
719 +++ b/notmuch.c\r
720 @@ -22,56 +22,57 @@\r
721  \r
722  #include "notmuch-client.h"\r
723  \r
724 -typedef int (*command_function_t) (void *ctx, int argc, char *argv[]);\r
725 +typedef int (*command_function_t) (notmuch_config_t *config, int argc, char *argv[]);\r
726  \r
727  typedef struct command {\r
728      const char *name;\r
729      command_function_t function;\r
730 +    notmuch_bool_t create_config;\r
731      const char *arguments;\r
732      const char *summary;\r
733  } command_t;\r
734  \r
735  static int\r
736 -notmuch_help_command (void *ctx, int argc, char *argv[]);\r
737 +notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]);\r
738  \r
739  static int\r
740 -notmuch_command (void *ctx, int argc, char *argv[]);\r
741 +notmuch_command (notmuch_config_t *config, int argc, char *argv[]);\r
742  \r
743  static command_t commands[] = {\r
744 -    { NULL, notmuch_command,\r
745 +    { NULL, notmuch_command, TRUE,\r
746        NULL,\r
747        "Notmuch main command." },\r
748 -    { "setup", notmuch_setup_command,\r
749 +    { "setup", notmuch_setup_command, TRUE,\r
750        NULL,\r
751        "Interactively setup notmuch for first use." },\r
752 -    { "new", notmuch_new_command,\r
753 +    { "new", notmuch_new_command, FALSE,\r
754        "[options...]",\r
755        "Find and import new messages to the notmuch database." },\r
756 -    { "search", notmuch_search_command,\r
757 +    { "search", notmuch_search_command, FALSE,\r
758        "[options...] <search-terms> [...]",\r
759        "Search for messages matching the given search terms." },\r
760 -    { "show", notmuch_show_command,\r
761 +    { "show", notmuch_show_command, FALSE,\r
762        "<search-terms> [...]",\r
763        "Show all messages matching the search terms." },\r
764 -    { "count", notmuch_count_command,\r
765 +    { "count", notmuch_count_command, FALSE,\r
766        "[options...] <search-terms> [...]",\r
767        "Count messages matching the search terms." },\r
768 -    { "reply", notmuch_reply_command,\r
769 +    { "reply", notmuch_reply_command, FALSE,\r
770        "[options...] <search-terms> [...]",\r
771        "Construct a reply template for a set of messages." },\r
772 -    { "tag", notmuch_tag_command,\r
773 +    { "tag", notmuch_tag_command, FALSE,\r
774        "+<tag>|-<tag> [...] [--] <search-terms> [...]" ,\r
775        "Add/remove tags for all messages matching the search terms." },\r
776 -    { "dump", notmuch_dump_command,\r
777 +    { "dump", notmuch_dump_command, FALSE,\r
778        "[<filename>] [--] [<search-terms>]",\r
779        "Create a plain-text dump of the tags for each message." },\r
780 -    { "restore", notmuch_restore_command,\r
781 +    { "restore", notmuch_restore_command, FALSE,\r
782        "[--accumulate] [<filename>]",\r
783        "Restore the tags from the given dump file (see 'dump')." },\r
784 -    { "config", notmuch_config_command,\r
785 +    { "config", notmuch_config_command, FALSE,\r
786        "[get|set] <section>.<item> [value ...]",\r
787        "Get or set settings in the notmuch configuration file." },\r
788 -    { "help", notmuch_help_command,\r
789 +    { "help", notmuch_help_command, FALSE,\r
790        "[<command>]",\r
791        "This message, or more detailed help for the named command." }\r
792  };\r
793 @@ -155,7 +156,7 @@ exec_man (const char *page)\r
794  }\r
795  \r
796  static int\r
797 -notmuch_help_command (void *ctx, int argc, char *argv[])\r
798 +notmuch_help_command (notmuch_config_t *config, int argc, char *argv[])\r
799  {\r
800      command_t *command;\r
801  \r
802 @@ -178,7 +179,7 @@ notmuch_help_command (void *ctx, int argc, char *argv[])\r
803  \r
804      command = find_command (argv[0]);\r
805      if (command) {\r
806 -       char *page = talloc_asprintf (ctx, "notmuch-%s", command->name);\r
807 +       char *page = talloc_asprintf (config, "notmuch-%s", command->name);\r
808         exec_man (page);\r
809      }\r
810  \r
811 @@ -199,28 +200,23 @@ notmuch_help_command (void *ctx, int argc, char *argv[])\r
812   * to be more clever about this in the future.\r
813   */\r
814  static int\r
815 -notmuch_command (void *ctx, unused(int argc), unused(char *argv[]))\r
816 +notmuch_command (notmuch_config_t *config,\r
817 +                unused(int argc), unused(char *argv[]))\r
818  {\r
819 -    notmuch_config_t *config;\r
820      char *db_path;\r
821      struct stat st;\r
822  \r
823 -    config = notmuch_config_open (ctx, NULL, TRUE);\r
824 -\r
825      /* If the user has never configured notmuch, then run\r
826       * notmuch_setup_command which will give a nice welcome message,\r
827       * and interactively guide the user through the configuration. */\r
828 -    if (notmuch_config_is_new (config)) {\r
829 -       notmuch_config_close (config);\r
830 -       return notmuch_setup_command (ctx, 0, NULL);\r
831 -    }\r
832 +    if (notmuch_config_is_new (config))\r
833 +       return notmuch_setup_command (config, 0, NULL);\r
834  \r
835      /* Notmuch is already configured, but is there a database? */\r
836 -    db_path = talloc_asprintf (ctx, "%s/%s",\r
837 +    db_path = talloc_asprintf (config, "%s/%s",\r
838                                notmuch_config_get_database_path (config),\r
839                                ".notmuch");\r
840      if (stat (db_path, &st)) {\r
841 -       notmuch_config_close (config);\r
842         if (errno != ENOENT) {\r
843             fprintf (stderr, "Error looking for notmuch database at %s: %s\n",\r
844                      db_path, strerror (errno));\r
845 @@ -252,8 +248,6 @@ notmuch_command (void *ctx, unused(int argc), unused(char *argv[]))\r
846             notmuch_config_get_user_name (config),\r
847             notmuch_config_get_user_primary_email (config));\r
848  \r
849 -    notmuch_config_close (config);\r
850 -\r
851      return 0;\r
852  }\r
853  \r
854 @@ -264,6 +258,7 @@ main (int argc, char *argv[])\r
855      char *talloc_report;\r
856      const char *command_name = NULL;\r
857      command_t *command;\r
858 +    notmuch_config_t *config;\r
859      notmuch_bool_t print_help=FALSE, print_version=FALSE;\r
860      int opt_index;\r
861      int ret = 0;\r
862 @@ -308,7 +303,13 @@ main (int argc, char *argv[])\r
863         return 1;\r
864      }\r
865  \r
866 -    ret = (command->function)(local, argc - opt_index, argv + opt_index);\r
867 +    config = notmuch_config_open (local, NULL, command->create_config);\r
868 +    if (!config)\r
869 +       return 1;\r
870 +\r
871 +    ret = (command->function)(config, argc - opt_index, argv + opt_index);\r
872 +\r
873 +    notmuch_config_close (config);\r
874  \r
875      /* in the future support for this environment variable may\r
876       * be supplemented or replaced by command line arguments\r
877 -- \r
878 1.7.10.4\r
879 \r