X-Git-Url: https://git.distorted.org.uk/~mdw/tig/blobdiff_plain/0001fc3426ae001a0bd465dcd19a20e2005cdfb6..5dcf8064ca55dfc77c3fcd18cdf6432ed0f51a94:/tig.c diff --git a/tig.c b/tig.c index d3702c4..fb7a9a6 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) @@ -1186,7 +1192,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 +1213,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 +1257,20 @@ 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) + view->ops->select(view, line); + + return view->ops->draw(view, line, lineno, selected); } static void @@ -1674,16 +1690,20 @@ 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)); + regerror(regex_err, view->regex, buf, sizeof(buf)); report("Search failed: %s", buf); return; } @@ -2189,7 +2209,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; @@ -2198,17 +2218,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); } @@ -2328,10 +2338,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; @@ -2399,18 +2413,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, }; @@ -2550,7 +2576,7 @@ tree_enter(struct view *view, struct line *line) size_t origlen = pathlen; char *basename = data + SIZEOF_TREE_ATTR; - if (string_format_from(opt_path, &pathlen, "%s/", basename)) { + if (!string_format_from(opt_path, &pathlen, "%s/", basename)) { opt_path[origlen] = 0; return TRUE; } @@ -2590,12 +2616,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 @@ -2615,6 +2653,7 @@ static struct view_ops blob_ops = { blob_read, pager_enter, pager_grep, + pager_select, }; @@ -2633,7 +2672,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; @@ -2648,9 +2687,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); @@ -2875,19 +2912,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, };