+ for (i = 0; i < ARRAY_SIZE(key_table); i++)
+ if (!strcasecmp(key_table[i].name, name))
+ return key_table[i].value;
+
+ if (strlen(name) == 1 && isprint(*name))
+ return (int) *name;
+
+ return ERR;
+}
+
+static char *
+get_key(enum request request)
+{
+ static char buf[BUFSIZ];
+ static char key_char[] = "'X'";
+ size_t pos = 0;
+ char *sep = " ";
+ int i;
+
+ buf[pos] = 0;
+
+ for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) {
+ struct keybinding *keybinding = &default_keybindings[i];
+ char *seq = NULL;
+ int key;
+
+ if (keybinding->request != request)
+ continue;
+
+ for (key = 0; key < ARRAY_SIZE(key_table); key++)
+ if (key_table[key].value == keybinding->alias)
+ seq = key_table[key].name;
+
+ if (seq == NULL &&
+ keybinding->alias < 127 &&
+ isprint(keybinding->alias)) {
+ key_char[1] = (char) keybinding->alias;
+ seq = key_char;
+ }
+
+ if (!seq)
+ seq = "'?'";
+
+ if (!string_format_from(buf, &pos, "%s%s", sep, seq))
+ return "Too many keybindings!";
+ sep = ", ";
+ }
+
+ return buf;
+}
+
+
+/*
+ * User config file handling.
+ */
+
+static struct int_map color_map[] = {
+#define COLOR_MAP(name) { #name, STRING_SIZE(#name), COLOR_##name }
+ COLOR_MAP(DEFAULT),
+ COLOR_MAP(BLACK),
+ COLOR_MAP(BLUE),
+ COLOR_MAP(CYAN),
+ COLOR_MAP(GREEN),
+ COLOR_MAP(MAGENTA),
+ COLOR_MAP(RED),
+ COLOR_MAP(WHITE),
+ COLOR_MAP(YELLOW),
+};
+
+#define set_color(color, name) \
+ set_from_int_map(color_map, ARRAY_SIZE(color_map), color, name, strlen(name))
+
+static struct int_map attr_map[] = {
+#define ATTR_MAP(name) { #name, STRING_SIZE(#name), A_##name }
+ ATTR_MAP(NORMAL),
+ ATTR_MAP(BLINK),
+ ATTR_MAP(BOLD),
+ ATTR_MAP(DIM),
+ ATTR_MAP(REVERSE),
+ ATTR_MAP(STANDOUT),
+ ATTR_MAP(UNDERLINE),
+};
+
+#define set_attribute(attr, name) \
+ set_from_int_map(attr_map, ARRAY_SIZE(attr_map), attr, name, strlen(name))
+
+static int config_lineno;
+static bool config_errors;
+static char *config_msg;
+
+/* Wants: object fgcolor bgcolor [attr] */
+static int
+option_color_command(int argc, char *argv[])
+{
+ struct line_info *info;
+
+ if (argc != 3 && argc != 4) {
+ config_msg = "Wrong number of arguments given to color command";
+ return ERR;
+ }
+
+ info = get_line_info(argv[0], strlen(argv[0]));
+ if (!info) {
+ config_msg = "Unknown color name";
+ return ERR;
+ }
+
+ if (set_color(&info->fg, argv[1]) == ERR ||
+ set_color(&info->bg, argv[2]) == ERR) {
+ config_msg = "Unknown color";
+ return ERR;
+ }
+
+ if (argc == 4 && set_attribute(&info->attr, argv[3]) == ERR) {
+ config_msg = "Unknown attribute";
+ return ERR;
+ }
+
+ return OK;
+}
+
+/* Wants: name = value */
+static int
+option_set_command(int argc, char *argv[])
+{
+ if (argc != 3) {
+ config_msg = "Wrong number of arguments given to set command";
+ return ERR;
+ }
+
+ if (strcmp(argv[1], "=")) {
+ config_msg = "No value assigned";
+ return ERR;
+ }
+
+ if (!strcmp(argv[0], "show-rev-graph")) {
+ opt_rev_graph = (!strcmp(argv[2], "1") ||
+ !strcmp(argv[2], "true") ||
+ !strcmp(argv[2], "yes"));
+ return OK;
+ }
+
+ if (!strcmp(argv[0], "line-number-interval")) {
+ opt_num_interval = atoi(argv[2]);
+ return OK;
+ }
+
+ if (!strcmp(argv[0], "tab-size")) {
+ opt_tab_size = atoi(argv[2]);
+ return OK;
+ }
+
+ if (!strcmp(argv[0], "commit-encoding")) {
+ char *arg = argv[2];
+ int delimiter = *arg;
+ int i;
+
+ switch (delimiter) {
+ case '"':
+ case '\'':
+ for (arg++, i = 0; arg[i]; i++)
+ if (arg[i] == delimiter) {
+ arg[i] = 0;
+ break;
+ }
+ default:
+ string_copy(opt_encoding, arg);
+ return OK;
+ }
+ }
+
+ config_msg = "Unknown variable name";
+ return ERR;
+}
+
+/* Wants: mode request key */
+static int
+option_bind_command(int argc, char *argv[])
+{
+ enum request request;
+ int keymap;
+ int key;
+
+ if (argc != 3) {
+ config_msg = "Wrong number of arguments given to bind command";
+ return ERR;
+ }
+
+ if (set_keymap(&keymap, argv[0]) == ERR) {
+ config_msg = "Unknown key map";
+ return ERR;
+ }
+
+ key = get_key_value(argv[1]);
+ if (key == ERR) {
+ config_msg = "Unknown key";
+ return ERR;
+ }
+
+ request = get_request(argv[2]);
+ if (request == REQ_UNKNOWN) {
+ config_msg = "Unknown request name";
+ return ERR;
+ }
+
+ add_keybinding(keymap, request, key);
+
+ return OK;
+}
+
+static int
+set_option(char *opt, char *value)
+{
+ char *argv[16];
+ int valuelen;
+ int argc = 0;
+
+ /* Tokenize */
+ while (argc < ARRAY_SIZE(argv) && (valuelen = strcspn(value, " \t"))) {
+ argv[argc++] = value;
+
+ value += valuelen;
+ if (!*value)
+ break;
+
+ *value++ = 0;
+ while (isspace(*value))
+ value++;
+ }
+
+ if (!strcmp(opt, "color"))
+ return option_color_command(argc, argv);
+
+ if (!strcmp(opt, "set"))
+ return option_set_command(argc, argv);
+
+ if (!strcmp(opt, "bind"))
+ return option_bind_command(argc, argv);
+
+ config_msg = "Unknown option command";
+ return ERR;
+}
+
+static int
+read_option(char *opt, int optlen, char *value, int valuelen)
+{
+ int status = OK;
+
+ config_lineno++;
+ config_msg = "Internal error";
+
+ /* Check for comment markers, since read_properties() will
+ * only ensure opt and value are split at first " \t". */
+ optlen = strcspn(opt, "#");
+ if (optlen == 0)
+ return OK;
+
+ if (opt[optlen] != 0) {
+ config_msg = "No option value";
+ status = ERR;
+
+ } else {
+ /* Look for comment endings in the value. */
+ int len = strcspn(value, "#");
+
+ if (len < valuelen) {
+ valuelen = len;
+ value[valuelen] = 0;
+ }
+
+ status = set_option(opt, value);
+ }
+
+ if (status == ERR) {
+ fprintf(stderr, "Error on line %d, near '%.*s': %s\n",
+ config_lineno, optlen, opt, config_msg);
+ config_errors = TRUE;
+ }
+
+ /* Always keep going if errors are encountered. */
+ return OK;
+}
+
+static int
+load_options(void)
+{
+ char *home = getenv("HOME");
+ char buf[SIZEOF_STR];
+ FILE *file;
+
+ config_lineno = 0;
+ config_errors = FALSE;
+
+ if (!home || !string_format(buf, "%s/.tigrc", home))
+ return ERR;
+
+ /* It's ok that the file doesn't exist. */
+ file = fopen(buf, "r");
+ if (!file)
+ return OK;
+
+ if (read_properties(file, " \t", read_option) == ERR ||
+ config_errors == TRUE)
+ fprintf(stderr, "Errors while loading %s.\n", buf);
+
+ return OK;
+}
+
+
+/*
+ * The viewer
+ */
+
+struct view;
+struct view_ops;
+
+/* The display array of active views and the index of the current view. */
+static struct view *display[2];
+static unsigned int current_view;
+
+#define foreach_displayed_view(view, i) \
+ for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
+
+#define displayed_views() (display[1] != NULL ? 2 : 1)
+
+/* Current head and commit ID */
+static char ref_blob[SIZEOF_REF] = "";
+static char ref_commit[SIZEOF_REF] = "HEAD";
+static char ref_head[SIZEOF_REF] = "HEAD";
+
+struct view {
+ const char *name; /* View name */
+ const char *cmd_fmt; /* Default command line format */
+ const char *cmd_env; /* Command line set via environment */
+ const char *id; /* Points to either of ref_{head,commit,blob} */
+
+ struct view_ops *ops; /* View operations */
+
+ enum keymap keymap; /* What keymap does this view have */
+
+ char cmd[SIZEOF_STR]; /* Command buffer */
+ char ref[SIZEOF_REF]; /* Hovered commit reference */
+ char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
+
+ int height, width; /* The width and height of the main window */
+ WINDOW *win; /* The main window */
+ WINDOW *title; /* The title window living below the main window */
+
+ /* Navigation */
+ unsigned long offset; /* Offset of the window top */
+ unsigned long lineno; /* Current line number */
+
+ /* Searching */
+ char grep[SIZEOF_STR]; /* Search string */
+ regex_t regex; /* Pre-compiled regex */
+
+ /* If non-NULL, points to the view that opened this view. If this view
+ * is closed tig will switch back to the parent view. */
+ struct view *parent;
+
+ /* Buffering */
+ unsigned long lines; /* Total number of lines */
+ struct line *line; /* Line index */
+ unsigned long line_size;/* Total number of allocated lines */
+ unsigned int digits; /* Number of digits in the lines member. */
+
+ /* Loading */
+ FILE *pipe;
+ time_t start_time;
+};
+
+struct view_ops {
+ /* What type of content being displayed. Used in the title bar. */
+ const char *type;
+ /* Draw one line; @lineno must be < view->height. */
+ bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
+ /* Read one line; updates view->line. */
+ bool (*read)(struct view *view, char *data);
+ /* Depending on view, change display based on current line. */
+ bool (*enter)(struct view *view, struct line *line);
+ /* Search for regex in a line. */
+ bool (*grep)(struct view *view, struct line *line);
+};
+
+static struct view_ops pager_ops;
+static struct view_ops main_ops;
+static struct view_ops tree_ops;
+static struct view_ops blob_ops;
+
+#define VIEW_STR(name, cmd, env, ref, ops, map) \
+ { name, cmd, #env, ref, ops, map}
+
+#define VIEW_(id, name, ops, ref) \
+ VIEW_STR(name, TIG_##id##_CMD, TIG_##id##_CMD, ref, ops, KEYMAP_##id)