static enum status_code prompt_toggle(struct view *view, const char *argv[], enum view_flag *flags) { const char *option = argv[1]; size_t optionlen = option ? strlen(option) : 0; struct prompt_toggle *toggle; struct view_column *column; if (!option) return error("%s", "No option name given to :toggle"); if (enum_equals_static("sort-field", option, optionlen) || enum_equals_static("sort-order", option, optionlen)) { if (!view_has_flags(view, VIEW_SORTABLE)) { return error("Sorting is not yet supported for the %s view", view->name); } else { bool sort_field = enum_equals_static("sort-field", option, optionlen); struct sort_state *sort = &view->sort; sort_view(view, sort_field); return success("set %s = %s", option, sort_field ? view_column_name(get_sort_field(view)) : sort->reverse ? "descending" : "ascending"); } } toggle = find_prompt_toggle(option_toggles, ARRAY_SIZE(option_toggles), "", option, optionlen); if (toggle) return prompt_toggle_option(view, argv, "", toggle, flags); #define DEFINE_COLUMN_OPTIONS_TOGGLE(name, type, flags) \ { #name, #type, flags, &opt->name }, #define DEFINE_COLUMN_OPTIONS_CHECK(name, id, options) \ if (column->type == VIEW_COLUMN_##id) { \ struct name##_options *opt = &column->opt.name; \ struct prompt_toggle toggles[] = { \ options(DEFINE_COLUMN_OPTIONS_TOGGLE) \ }; \ toggle = find_prompt_toggle(toggles, ARRAY_SIZE(toggles), #name, option, optionlen); \ if (toggle) \ return prompt_toggle_option(view, argv, #name, toggle, flags); \ } for (column = view->columns; column; column = column->next) { COLUMN_OPTIONS(DEFINE_COLUMN_OPTIONS_CHECK); } return error("`:toggle %s` not supported", option); }
static enum view_flag prompt_toggle(struct view *view, const char *argv[], char msg[SIZEOF_STR]) { struct prompt_toggle option_toggles[] = { #define TOGGLE_OPTIONS(name, type, flags) { #name, #type, flags, &opt_ ## name }, OPTION_INFO(TOGGLE_OPTIONS) }; const char *option = argv[1]; size_t optionlen = option ? strlen(option) : 0; struct prompt_toggle *toggle; if (!option) { string_format_size(msg, SIZEOF_STR, "%s", "No option name given to :toggle"); return VIEW_NO_FLAGS; } if (enum_equals_static("sort-field", option, optionlen) || enum_equals_static("sort-order", option, optionlen)) { if (!view_has_flags(view, VIEW_SORTABLE)) { report("Sorting is not yet supported for the %s view", view->name); } else { bool sort_field = enum_equals_static("sort-field", option, optionlen); struct sort_state *sort = &view->sort; sort_view(view, sort_field); string_format_size(msg, SIZEOF_STR, "set %s = %s", option, sort_field ? enum_name(view_column_type_map->entries[get_sort_field(view)]) : sort->reverse ? "descending" : "ascending"); } return VIEW_NO_FLAGS; } toggle = find_prompt_toggle(option_toggles, ARRAY_SIZE(option_toggles), option, optionlen); if (toggle) return prompt_toggle_option(view, argv, toggle, msg); string_format_size(msg, SIZEOF_STR, "`:toggle %s` not supported", option); return VIEW_NO_FLAGS; }
enum request run_prompt_command(struct view *view, const char *argv[]) { enum request request; const char *cmd = argv[0]; size_t cmdlen = cmd ? strlen(cmd) : 0; if (!cmd) return REQ_NONE; if (string_isnumber(cmd)) { int lineno = view->pos.lineno + 1; if (parse_int(&lineno, cmd, 0, view->lines + 1) == SUCCESS) { if (!lineno) lineno = 1; select_view_line(view, lineno - 1); report_clear(); } else { report("Unable to parse '%s' as a line number", cmd); } } else if (iscommit(cmd)) { int lineno; if (!(view->ops->column_bits & view_column_bit(ID))) { report("Jumping to commits is not supported by the %s view", view->name); return REQ_NONE; } for (lineno = 0; lineno < view->lines; lineno++) { struct view_column_data column_data = {}; struct line *line = &view->line[lineno]; if (view->ops->get_column_data(view, line, &column_data) && column_data.id && !strncasecmp(column_data.id, cmd, cmdlen)) { string_ncopy(view->env->search, cmd, cmdlen); select_view_line(view, lineno); report_clear(); return REQ_NONE; } } report("Unable to find commit '%s'", view->env->search); return REQ_NONE; } else if (cmdlen > 1 && (cmd[0] == '/' || cmd[0] == '?')) { char search[SIZEOF_STR]; if (!argv_to_string(argv, search, sizeof(search), " ")) { report("Failed to copy search string"); return REQ_NONE; } if (!strcmp(search + 1, view->env->search)) return cmd[0] == '/' ? REQ_FIND_NEXT : REQ_FIND_PREV; string_ncopy(view->env->search, search + 1, strlen(search + 1)); return cmd[0] == '/' ? REQ_SEARCH : REQ_SEARCH_BACK; } else if (cmdlen > 1 && cmd[0] == '!') { struct view *next = &pager_view; bool copied; /* Trim the leading '!'. */ argv[0] = cmd + 1; copied = argv_format(view->env, &next->argv, argv, FALSE, TRUE); argv[0] = cmd; if (!copied) { report("Argument formatting failed"); } else { /* When running random commands, initially show the * command in the title. However, it maybe later be * overwritten if a commit line is selected. */ argv_to_string(next->argv, next->ref, sizeof(next->ref), " "); next->dir = NULL; open_pager_view(view, OPEN_PREPARED | OPEN_WITH_STDERR); } } else if (!strcmp(cmd, "save-display")) { const char *path = argv[1] ? argv[1] : "tig-display.txt"; if (!save_display(path)) report("Failed to save screen to %s", path); else report("Saved screen to %s", path); } else if (!strcmp(cmd, "exec")) { struct run_request req = { view->keymap, {}, argv + 1 }; enum status_code code = parse_run_request_flags(&req.flags, argv + 1); if (code != SUCCESS) { report("Failed to execute command: %s", get_status_message(code)); } else { return exec_run_request(view, &req); } } else if (!strcmp(cmd, "toggle")) { enum view_flag flags = VIEW_NO_FLAGS; enum status_code code = prompt_toggle(view, argv, &flags); const char *action = get_status_message(code); if (code != SUCCESS) { report("%s", action); return REQ_NONE; } prompt_update_display(flags); if (*action) report("%s", action); } else if (!strcmp(cmd, "script")) { if (is_script_executing()) { report("Scripts cannot be run from scripts"); } else if (!open_script(argv[1])) { report("Failed to open %s", argv[1]); } } else { struct key key = {}; enum status_code code; enum view_flag flags = VIEW_NO_FLAGS; /* Try :<key> */ key.modifiers.multibytes = 1; string_ncopy(key.data.bytes, cmd, cmdlen); request = get_keybinding(view->keymap, &key, 1); if (request != REQ_NONE) return request; /* Try :<command> */ request = get_request(cmd); if (request != REQ_UNKNOWN) return request; code = set_option(argv[0], argv_size(argv + 1), &argv[1]); if (code != SUCCESS) { report("%s", get_status_message(code)); return REQ_NONE; } if (!strcmp(cmd, "set")) { struct prompt_toggle *toggle; toggle = find_prompt_toggle(option_toggles, ARRAY_SIZE(option_toggles), "", argv[1], strlen(argv[1])); if (toggle) flags = toggle->flags; } if (flags) { prompt_update_display(flags); } else { request = view_can_refresh(view) ? REQ_REFRESH : REQ_SCREEN_REDRAW; if (!strcmp(cmd, "color")) init_colors(); resize_display(); redraw_display(TRUE); } } return REQ_NONE; }