]> granicus.if.org Git - neomutt/commitdiff
Implement :set [all] commands
authorryt <0x747972@gmail.com>
Mon, 12 Nov 2018 19:49:57 +0000 (20:49 +0100)
committerRichard Russon <rich@flatcap.org>
Fri, 4 Jan 2019 01:19:02 +0000 (01:19 +0000)
config/dump.c
config/dump.h
icommands.c
init.c
main.c

index fd8f1e5c85f3d6b6d67376685a0f0f75612720d5..1b49c8ce3f2726fee334534f34de0c8c4e2fc34d 100644 (file)
@@ -151,9 +151,10 @@ struct HashElem **get_elem_list(struct ConfigSet *cs)
  * @param value   Current value of the config item
  * @param initial Initial value of the config item
  * @param flags   Flags, e.g. #CS_DUMP_ONLY_CHANGED
+ * @param fp      File pointer to write to
  */
-void dump_config_mutt(struct ConfigSet *cs, struct HashElem *he,
-                      struct Buffer *value, struct Buffer *initial, int flags)
+void dump_config_mutt(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value,
+                      struct Buffer *initial, int flags, FILE *fp)
 {
   const char *name = he->key.strkey;
 
@@ -161,16 +162,16 @@ void dump_config_mutt(struct ConfigSet *cs, struct HashElem *he,
   {
     if ((value->data[0] == 'y') || ((value->data[0] == '"') && (value->data[1] == 'y')))
     {
-      printf("%s is set\n", name);
+      fprintf(fp, "%s is set\n", name);
     }
     else
     {
-      printf("%s is unset\n", name);
+      fprintf(fp, "%s is unset\n", name);
     }
   }
   else
   {
-    printf("%s=%s\n", name, value->data);
+    fprintf(fp, "%s=%s\n", name, value->data);
   }
 }
 
@@ -181,9 +182,10 @@ void dump_config_mutt(struct ConfigSet *cs, struct HashElem *he,
  * @param value   Current value of the config item
  * @param initial Initial value of the config item
  * @param flags   Flags, e.g. #CS_DUMP_ONLY_CHANGED
+ * @param fp      File pointer to write to
  */
-void dump_config_neo(struct ConfigSet *cs, struct HashElem *he,
-                     struct Buffer *value, struct Buffer *initial, int flags)
+void dump_config_neo(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value,
+                     struct Buffer *initial, int flags, FILE *fp)
 {
   const char *name = he->key.strkey;
 
@@ -194,7 +196,7 @@ void dump_config_neo(struct ConfigSet *cs, struct HashElem *he,
   {
     const struct ConfigDef *cdef = he->data;
     const char *syn = (const char *) cdef->initial;
-    printf("# synonym: %s -> %s\n", name, syn);
+    fprintf(fp, "# synonym: %s -> %s\n", name, syn);
     return;
   }
 
@@ -202,31 +204,32 @@ void dump_config_neo(struct ConfigSet *cs, struct HashElem *he,
   bool show_value = !(flags & CS_DUMP_HIDE_VALUE);
 
   if (show_name && show_value)
-    printf("set ");
+    fprintf(fp, "set ");
   if (show_name)
-    printf("%s", name);
+    fprintf(fp, "%s", name);
   if (show_name && show_value)
-    printf(" = ");
+    fprintf(fp, " = ");
   if (show_value)
-    printf("%s", value->data);
+    fprintf(fp, "%s", value->data);
   if (show_name || show_value)
-    printf("\n");
+    fprintf(fp, "\n");
 
   if (flags & CS_DUMP_SHOW_DEFAULTS)
   {
     const struct ConfigSetType *cst = cs_get_type_def(cs, he->type);
     if (cst)
-      printf("# %s %s %s\n", cst->name, name, value->data);
+      fprintf(fp, "# %s %s %s\n", cst->name, name, value->data);
   }
 }
 
 /**
- * dump_config - Write all the config to stdout
+ * dump_config - Write all the config to a file
  * @param cs    ConfigSet to dump
  * @param style Output style, e.g. #CS_DUMP_STYLE_MUTT
  * @param flags Display flags, e.g. #CS_DUMP_ONLY_CHANGED
+ * @param fp    File to write config to
  */
-bool dump_config(struct ConfigSet *cs, int style, int flags)
+bool dump_config(struct ConfigSet *cs, int style, int flags, FILE *fp)
 {
   if (!cs)
     return false;
@@ -313,9 +316,9 @@ bool dump_config(struct ConfigSet *cs, int style, int flags)
     }
 
     if (style == CS_DUMP_STYLE_MUTT)
-      dump_config_mutt(cs, he, value, initial, flags);
+      dump_config_mutt(cs, he, value, initial, flags, fp);
     else
-      dump_config_neo(cs, he, value, initial, flags);
+      dump_config_neo(cs, he, value, initial, flags, fp);
   }
 
   FREE(&list);
index 26ddc7ec507ed9eb4c4774f46bdbe2598313128b..d09b7e961705286c217455d4ce3a94b7da4e743e 100644 (file)
@@ -39,9 +39,9 @@ struct ConfigSet;
 #define CS_DUMP_SHOW_DISABLED  (1 << 6) /**< Show disabled config items, too */
 #define CS_DUMP_SHOW_SYNONYMS  (1 << 7) /**< Show synonyms and the config items their linked to */
 
-void              dump_config_mutt(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value, struct Buffer *initial, int flags);
-void              dump_config_neo(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value, struct Buffer *initial, int flags);
-bool              dump_config(struct ConfigSet *cs, int style, int flags);
+void              dump_config_mutt(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value, struct Buffer *initial, int flags, FILE *fp);
+void              dump_config_neo(struct ConfigSet *cs, struct HashElem *he, struct Buffer *value, struct Buffer *initial, int flags, FILE *fp);
+bool              dump_config(struct ConfigSet *cs, int style, int flags, FILE *fp);
 int               elem_list_sort(const void *a, const void *b);
 size_t            escape_string(struct Buffer *buf, const char *src);
 struct HashElem **get_elem_list(struct ConfigSet *cs);
index 1f39ee1ec695dfb5402b0310a3c7a9fe6b932395..599f564c839cb48283a646cf213054c5ed75b917 100644 (file)
 #include "version.h"
 
 // clang-format off
+static enum CommandResult icmd_set(struct      Buffer *, struct Buffer *, unsigned long, struct Buffer *);
+
 /**
  * ICommandList - All available informational commands
  *
  * @note These commands take precendence over conventional mutt rc-lines
  */
 const struct ICommand ICommandList[] = {
+  { "set",      icmd_set,      0 },
   { NULL,       NULL,          0 },
 };
 // clang-format on
@@ -92,3 +95,46 @@ finish:
     FREE(&expn.data);
   return rc;
 }
+
+/**
+ * icmd_set - Parse 'set' command to display config - Implements ::icommand_t
+ */
+static enum CommandResult icmd_set(struct Buffer *buf, struct Buffer *s,
+                                   unsigned long data, struct Buffer *err)
+{
+  char tempfile[PATH_MAX];
+  mutt_mktemp(tempfile, sizeof(tempfile));
+
+  FILE *fpout = mutt_file_fopen(tempfile, "w");
+  if (!fpout)
+  {
+    mutt_buffer_addstr(err, _("Could not create temporary file"));
+    return MUTT_CMD_ERROR;
+  }
+
+  if (mutt_str_strcmp(s->data, "set all") == 0)
+  {
+    dump_config(Config, CS_DUMP_STYLE_NEO, 0, fpout);
+  }
+  else if (mutt_str_strcmp(s->data, "set") == 0)
+  {
+    dump_config(Config, CS_DUMP_STYLE_NEO, CS_DUMP_ONLY_CHANGED, fpout);
+  }
+  else
+  {
+    mutt_file_fclose(&fpout);
+    return MUTT_CMD_ERROR;
+  }
+
+  fflush(fpout);
+  mutt_file_fclose(&fpout);
+
+  struct Pager info = { 0 };
+  if (mutt_pager("set", tempfile, 0, &info) == -1)
+  {
+    mutt_buffer_addstr(err, _("Could not create temporary file"));
+    return MUTT_CMD_ERROR;
+  }
+
+  return MUTT_CMD_SUCCESS;
+}
diff --git a/init.c b/init.c
index 5f96dd1d6c6785a5e3d38d7aa76434187080a9bc..a48ac76b57a119397209ef8ca145d7a87f032b41 100644 (file)
--- a/init.c
+++ b/init.c
@@ -3212,7 +3212,7 @@ int mutt_query_variables(struct ListHead *queries)
       mutt_buffer_strcpy(value, tmp->data);
     }
 
-    dump_config_neo(Config, he, value, NULL, 0);
+    dump_config_neo(Config, he, value, NULL, 0, stdout);
   }
 
   mutt_buffer_free(&value);
diff --git a/main.c b/main.c
index 1cf57b8ca0142bc03054d328dbcd66d1852f02e1..ef07dd5bec3a47017313e04055c9fa1eb743e7ab 100644 (file)
--- a/main.c
+++ b/main.c
@@ -747,7 +747,8 @@ int main(int argc, char *argv[], char *envp[])
 
   if (dump_variables)
   {
-    dump_config(Config, CS_DUMP_STYLE_NEO, hide_sensitive ? CS_DUMP_HIDE_SENSITIVE : 0);
+    dump_config(Config, CS_DUMP_STYLE_NEO,
+                hide_sensitive ? CS_DUMP_HIDE_SENSITIVE : 0, stdout);
     goto main_ok; // TEST18: neomutt -D
   }