From: ryt <0x747972@gmail.com> Date: Mon, 12 Nov 2018 19:49:57 +0000 (+0100) Subject: Implement :set [all] commands X-Git-Tag: 2019-10-25~401^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=908a11fa1714b39d2f3eb5734b7f4f82fba33a09;p=neomutt Implement :set [all] commands --- diff --git a/config/dump.c b/config/dump.c index fd8f1e5c8..1b49c8ce3 100644 --- a/config/dump.c +++ b/config/dump.c @@ -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); diff --git a/config/dump.h b/config/dump.h index 26ddc7ec5..d09b7e961 100644 --- a/config/dump.h +++ b/config/dump.h @@ -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); diff --git a/icommands.c b/icommands.c index 1f39ee1ec..599f564c8 100644 --- a/icommands.c +++ b/icommands.c @@ -34,12 +34,15 @@ #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 5f96dd1d6..a48ac76b5 100644 --- 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 1cf57b8ca..ef07dd5be 100644 --- 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 }