Skip to content

Commit

Permalink
Highlight all matching searches on a line
Browse files Browse the repository at this point in the history
Fixes #493
  • Loading branch information
jonas committed Apr 23, 2016
1 parent 3ce0631 commit d3ea0a5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Improvements:

- Use .mailmap to show canonical name and email addresses, off by default.
Add `set mailmap = yes` to `~/.tigrc` to enable. (GH #411)
- Highlight search results, configurable via `search-result` color.
- Highlight search results, configurable via `search-result` color. (GH #493)
- Wrap around when searching, configurable via `wrap-search` setting.
- Populate `%(file)` with file names from diff stat. (GH #404)
- `tig --merge` implies `--boundary` similar to gitk.
Expand Down
18 changes: 10 additions & 8 deletions src/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -570,26 +570,28 @@ draw_view_line_search_result(struct view *view, unsigned int lineno)
size_t bufsize = view->width * 4;
char *buf = malloc(bufsize);
regmatch_t pmatch[1];
int i;
regoff_t bufpos = 0;

if (!buf || mvwinnstr(view->win, lineno, 0, buf, bufsize) == ERR ||
regexec(view->regex, buf, ARRAY_SIZE(pmatch), pmatch, 0)) {
if (!buf || mvwinnstr(view->win, lineno, 0, buf, bufsize) == ERR) {
free(buf);
return;
}

for (i = 0; i < ARRAY_SIZE(pmatch); i++) {
regoff_t start = pmatch[i].rm_so;
while (bufpos < bufsize && !regexec(view->regex, buf + bufpos, ARRAY_SIZE(pmatch), pmatch, 0)) {
regoff_t start = pmatch[0].rm_so;
regoff_t end = pmatch[0].rm_eo;

if (start == -1)
if (start == -1 || end <= 0 || end <= start)
continue;

mvwchgat(view->win, lineno,
utf8_width_of(buf, start, -1),
utf8_width_of(buf + start, pmatch[i].rm_eo - start, -1),
utf8_width_of(buf, bufpos + start, -1),
utf8_width_of(buf + bufpos + start, end - start, -1),
get_view_attr(view, LINE_SEARCH_RESULT),
get_view_color(view, LINE_SEARCH_RESULT),
NULL);

bufpos += end;
}

free(buf);
Expand Down

0 comments on commit d3ea0a5

Please sign in to comment.