enum status_code format_ref_formats(struct ref_format **formats, char buf[], size_t bufsize) { const struct enum_map *map = reference_type_map; char name[SIZEOF_STR]; enum reference_type type; size_t bufpos = 0; const char *sep = ""; for (type = 0; type < map->size; type++) { struct ref_format *format = formats[type]; if (!format) continue; if (!enum_name_copy(name, sizeof(name), map->entries[type].name) || !string_nformat(buf, bufsize, &bufpos, "%s%s%s%s", sep, format->start, name, format->end)) return error("No space left in buffer"); sep = " "; } return SUCCESS; }
static enum view_flag prompt_toggle_option(struct view *view, const char *argv[], struct prompt_toggle *toggle, char msg[SIZEOF_STR]) { char name[SIZEOF_STR]; enum_name_copy(name, toggle->name, strlen(toggle->name)); if (!strcmp(toggle->type, "bool")) { bool *opt = toggle->opt; *opt = !*opt; string_format_size(msg, SIZEOF_STR, "set %s = %s", name, *opt ? "yes" : "no"); } else if (!strncmp(toggle->type, "enum", 4)) { const char *type = toggle->type + STRING_SIZE("enum "); enum author *opt = toggle->opt; const struct enum_map *map = find_enum_map(type); *opt = (*opt + 1) % map->size; string_format_size(msg, SIZEOF_STR, "set %s = %s", name, enum_name(map->entries[*opt])); } else if (!strcmp(toggle->type, "int")) { const char *arg = argv[2] ? argv[2] : "1"; int diff = atoi(arg); int *opt = toggle->opt; if (!diff) diff = *arg == '-' ? -1 : 1; if (opt == &opt_diff_context && diff < 0) { if (!*opt) { report("Diff context cannot be less than zero"); return VIEW_NO_FLAGS; } if (*opt < -diff) diff = -*opt; } if (opt == &opt_title_overflow) { *opt = *opt ? -*opt : 50; if (*opt < 0) { string_format_size(msg, SIZEOF_STR, "set %s = no", name); return toggle->flags; } } *opt += diff; string_format_size(msg, SIZEOF_STR, "set %s = %d", name, *opt); } else if (!strcmp(toggle->type, "double")) { const char *arg = argv[2] ? argv[2] : "1.0"; double *opt = toggle->opt; int sign = 1; double diff; if (*arg == '-') { sign = -1; arg++; } if (parse_step(&diff, arg) != SUCCESS) diff = strtod(arg, NULL); *opt += sign * diff; string_format_size(msg, SIZEOF_STR, "set %s = %.2f", name, *opt); } else if (!strcmp(toggle->type, "const char **")) { const char ***opt = toggle->opt; bool found = TRUE; int i; for (i = 2; argv[i]; i++) { if (!find_arg(*opt, argv[i])) { found = FALSE; break; } } if (found) { int next, pos; for (next = 0, pos = 0; (*opt)[pos]; pos++) { const char *arg = (*opt)[pos]; if (find_arg(argv + 2, arg)) { free((void *) arg); continue; } (*opt)[next++] = arg; } (*opt)[next] = NULL; } else if (!argv_copy(opt, argv + 2)) { report("Failed to append arguments"); return VIEW_NO_FLAGS; } } else { die("Unsupported `:toggle %s` (%s)", name, toggle->type); } return toggle->flags; }