X-Git-Url: https://git.distorted.org.uk/~mdw/tig/blobdiff_plain/904e68d878402cde03164a9706d828a6bafa25ce..3c571d672aa2edbe8afc04cf452ea323eef96f6c:/tig.c diff --git a/tig.c b/tig.c index e78c26c..e48af7e 100644 --- a/tig.c +++ b/tig.c @@ -145,16 +145,22 @@ set_from_int_map(struct int_map *map, size_t map_size, */ static inline void -string_ncopy(char *dst, const char *src, size_t dstlen) +string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen) { - strncpy(dst, src, dstlen - 1); - dst[dstlen - 1] = 0; + if (srclen > dstlen - 1) + srclen = dstlen - 1; + strncpy(dst, src, srclen); + dst[srclen] = 0; } -/* Shorthand for safely copying into a fixed buffer. */ +/* Shorthands for safely copying into a fixed buffer. */ + #define string_copy(dst, src) \ - string_ncopy(dst, src, sizeof(dst)) + string_ncopy_do(dst, sizeof(dst), src, sizeof(dst)) + +#define string_ncopy(dst, src, srclen) \ + string_ncopy_do(dst, sizeof(dst), src, srclen) static char * chomp_string(char *name) @@ -655,6 +661,10 @@ init_colors(void) struct line { enum line_type type; + + /* State flags */ + unsigned int selected:1; + void *data; /* User data */ }; @@ -675,7 +685,7 @@ static struct keybinding default_keybindings[] = { { 'd', REQ_VIEW_DIFF }, { 'l', REQ_VIEW_LOG }, { 't', REQ_VIEW_TREE }, - { 'b', REQ_VIEW_BLOB }, + { 'f', REQ_VIEW_BLOB }, { 'p', REQ_VIEW_PAGER }, { 'h', REQ_VIEW_HELP }, @@ -1186,7 +1196,7 @@ struct view { /* Searching */ char grep[SIZEOF_STR]; /* Search string */ - regex_t regex; /* Pre-compiled regex */ + 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. */ @@ -1207,13 +1217,15 @@ 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); + bool (*draw)(struct view *view, struct line *line, unsigned int lineno, bool selected); /* 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); + /* Select line */ + void (*select)(struct view *view, struct line *line); }; static struct view_ops pager_ops; @@ -1249,12 +1261,26 @@ static struct view views[] = { static bool draw_view_line(struct view *view, unsigned int lineno) { + struct line *line; + bool selected = (view->offset + lineno == view->lineno); + assert(view_is_displayed(view)); if (view->offset + lineno >= view->lines) return FALSE; - return view->ops->draw(view, &view->line[view->offset + lineno], lineno); + line = &view->line[view->offset + lineno]; + + if (selected) { + line->selected = TRUE; + view->ops->select(view, line); + } else if (line->selected) { + line->selected = FALSE; + wmove(view->win, lineno, 0); + wclrtoeol(view->win); + } + + return view->ops->draw(view, line, lineno, selected); } static void @@ -1549,13 +1575,8 @@ move_view(struct view *view, enum request request, bool redraw) assert(0 <= view->lineno && view->lineno < view->lines); /* Repaint the old "current" line if we be scrolling */ - if (ABS(steps) < view->height) { - int prev_lineno = view->lineno - steps - view->offset; - - wmove(view->win, prev_lineno, 0); - wclrtoeol(view->win); - draw_view_line(view, prev_lineno); - } + if (ABS(steps) < view->height) + draw_view_line(view, view->lineno - steps - view->offset); /* Check whether the view needs to be scrolled */ if (view->lineno < view->offset || @@ -1611,9 +1632,6 @@ find_next_line(struct view *view, unsigned long lineno, struct line *line) unsigned long old_lineno = view->lineno - view->offset; view->lineno = lineno; - - wmove(view->win, old_lineno, 0); - wclrtoeol(view->win); draw_view_line(view, old_lineno); draw_view_line(view, view->lineno - view->offset); @@ -1674,17 +1692,21 @@ search_view(struct view *view, enum request request, const char *search) { int regex_err; - if (*view->grep) { - regfree(&view->regex); + if (view->regex) { + regfree(view->regex); *view->grep = 0; + } else { + view->regex = calloc(1, sizeof(*view->regex)); + if (!view->regex) + return; } - regex_err = regcomp(&view->regex, search, REG_EXTENDED); + regex_err = regcomp(view->regex, search, REG_EXTENDED); if (regex_err != 0) { char buf[SIZEOF_STR] = "unknown error"; - regerror(regex_err, &view->regex, buf, sizeof(buf)); - report("Search failed: %s", buf);; + regerror(regex_err, view->regex, buf, sizeof(buf)); + report("Search failed: %s", buf); return; } @@ -1731,8 +1753,7 @@ begin_update(struct view *view) if (strcmp(view->vid, view->id)) opt_path[0] = 0; - if (snprintf(view->cmd, sizeof(view->cmd), format, id, opt_path) - >= sizeof(view->cmd)) + if (!string_format(view->cmd, format, id, opt_path)) return FALSE; } else { @@ -2190,7 +2211,7 @@ view_driver(struct view *view, enum request request) */ static bool -pager_draw(struct view *view, struct line *line, unsigned int lineno) +pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selected) { char *text = line->data; enum line_type type = line->type; @@ -2199,17 +2220,7 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno) wmove(view->win, lineno, 0); - if (view->offset + lineno == view->lineno) { - if (type == LINE_COMMIT) { - string_copy(view->ref, text + 7); - string_copy(ref_commit, view->ref); - - } else if (type == LINE_TREE_DIR || type == LINE_TREE_FILE) { - strncpy(view->ref, text + STRING_SIZE("100644 blob "), 41); - view->ref[40] = 0; - string_copy(ref_blob, view->ref); - } - + if (selected) { type = LINE_CURSOR; wchgat(view->win, -1, 0, type, NULL); } @@ -2329,10 +2340,14 @@ add_pager_refs(struct view *view, struct line *line) if (!is_tag && view == VIEW(REQ_VIEW_DIFF)) { try_add_describe_ref: + /* Add -g "fake" reference. */ if (!add_describe_ref(buf, &bufpos, commit_id, sep)) return; } + if (bufpos == 0) + return; + if (!realloc_lines(view, view->line_size + 1)) return; @@ -2400,18 +2415,30 @@ pager_grep(struct view *view, struct line *line) if (!*text) return FALSE; - if (regexec(&view->regex, text, 1, &pmatch, 0) == REG_NOMATCH) + if (regexec(view->regex, text, 1, &pmatch, 0) == REG_NOMATCH) return FALSE; return TRUE; } +static void +pager_select(struct view *view, struct line *line) +{ + if (line->type == LINE_COMMIT) { + char *text = line->data; + + string_copy(view->ref, text + STRING_SIZE("commit ")); + string_copy(ref_commit, view->ref); + } +} + static struct view_ops pager_ops = { "line", pager_draw, pager_read, pager_enter, pager_grep, + pager_select, }; @@ -2452,6 +2479,7 @@ tree_read(struct view *view, char *text) char buf[SIZEOF_STR]; unsigned long pos; enum line_type type; + bool first_read = view->lines == 0; if (textlen <= SIZEOF_TREE_ATTR) return FALSE; @@ -2459,10 +2487,9 @@ tree_read(struct view *view, char *text) type = text[STRING_SIZE("100644 ")] == 't' ? LINE_TREE_DIR : LINE_TREE_FILE; - /* The first time around ... */ - if (!view->lines) { + if (first_read) { /* Add path info line */ - if (snprintf(buf, sizeof(buf), "Directory path /%s", opt_path) < sizeof(buf) && + if (string_format(buf, "Directory path /%s", opt_path) && realloc_lines(view, view->line_size + 1) && pager_read(view, buf)) view->line[view->lines - 1].type = LINE_DEFAULT; @@ -2471,7 +2498,7 @@ tree_read(struct view *view, char *text) /* Insert "link" to parent directory. */ if (*opt_path && - snprintf(buf, sizeof(buf), TREE_UP_FORMAT, view->ref) < sizeof(buf) && + string_format(buf, TREE_UP_FORMAT, view->ref) && realloc_lines(view, view->line_size + 1) && pager_read(view, buf)) view->line[view->lines - 1].type = LINE_TREE_DIR; @@ -2518,6 +2545,10 @@ tree_read(struct view *view, char *text) if (!pager_read(view, text)) return FALSE; + /* Move the current line to the first tree entry. */ + if (first_read) + view->lineno++; + view->line[view->lines - 1].type = type; return TRUE; } @@ -2544,9 +2575,13 @@ tree_enter(struct view *view, struct line *line) } else { size_t pathlen = strlen(opt_path); + size_t origlen = pathlen; char *basename = data + SIZEOF_TREE_ATTR; - string_format_from(opt_path, &pathlen, "%s/", basename); + if (!string_format_from(opt_path, &pathlen, "%s/", basename)) { + opt_path[origlen] = 0; + return TRUE; + } } /* Trees and subtrees share the same ID, so they are not not @@ -2583,12 +2618,24 @@ tree_enter(struct view *view, struct line *line) return TRUE; } +static void +tree_select(struct view *view, struct line *line) +{ + if (line->type == LINE_TREE_DIR || line->type == LINE_TREE_FILE) { + char *text = line->data; + + string_ncopy(view->ref, text + STRING_SIZE("100644 blob "), 40); + string_copy(ref_blob, view->ref); + } +} + static struct view_ops tree_ops = { "file", pager_draw, tree_read, tree_enter, pager_grep, + tree_select, }; static bool @@ -2608,6 +2655,7 @@ static struct view_ops blob_ops = { blob_read, pager_enter, pager_grep, + pager_select, }; @@ -2626,7 +2674,7 @@ struct commit { }; static bool -main_draw(struct view *view, struct line *line, unsigned int lineno) +main_draw(struct view *view, struct line *line, unsigned int lineno, bool selected) { char buf[DATE_COLS + 1]; struct commit *commit = line->data; @@ -2641,9 +2689,7 @@ main_draw(struct view *view, struct line *line, unsigned int lineno) wmove(view->win, lineno, col); - if (view->offset + lineno == view->lineno) { - string_copy(view->ref, commit->id); - string_copy(ref_commit, view->ref); + if (selected) { type = LINE_CURSOR; wattrset(view->win, get_line_attr(type)); wchgat(view->win, -1, 0, type, NULL); @@ -2868,19 +2914,29 @@ main_grep(struct view *view, struct line *line) return FALSE; } - if (regexec(&view->regex, text, 1, &pmatch, 0) != REG_NOMATCH) + if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH) return TRUE; } return FALSE; } +static void +main_select(struct view *view, struct line *line) +{ + struct commit *commit = line->data; + + string_copy(view->ref, commit->id); + string_copy(ref_commit, view->ref); +} + static struct view_ops main_ops = { "commit", main_draw, main_read, main_enter, main_grep, + main_select, }; @@ -3416,7 +3472,7 @@ main(int argc, char *argv[]) if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) { opt_iconv = iconv_open(opt_codeset, opt_encoding); - if (opt_iconv == (iconv_t) -1) + if (opt_iconv == ICONV_NONE) die("Failed to initialize character set conversion"); }