Skip to content

Commit

Permalink
Simplify searching in view lines by defining grep_text utility
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas committed Feb 17, 2009
1 parent 6125c6c commit 436674f
Showing 1 changed file with 35 additions and 82 deletions.
117 changes: 35 additions & 82 deletions tig.c
Expand Up @@ -2502,6 +2502,19 @@ move_view(struct view *view, enum request request)

static void search_view(struct view *view, enum request request);

static bool
grep_text(struct view *view, const char *text[])
{
regmatch_t pmatch;
size_t i;

for (i = 0; text[i]; i++)
if (*text[i] &&
regexec(view->regex, text[i], 1, &pmatch, 0) != REG_NOMATCH)
return TRUE;
return FALSE;
}

static void
select_view_line(struct view *view, unsigned long lineno)
{
Expand Down Expand Up @@ -3612,16 +3625,9 @@ pager_request(struct view *view, enum request request, struct line *line)
static bool
pager_grep(struct view *view, struct line *line)
{
regmatch_t pmatch;
char *text = line->data;

if (!*text)
return FALSE;

if (regexec(view->regex, text, 1, &pmatch, 0) == REG_NOMATCH)
return FALSE;
const char *text[] = { line->data, NULL };

return TRUE;
return grep_text(view, text);
}

static void
Expand Down Expand Up @@ -4580,24 +4586,15 @@ blame_grep(struct view *view, struct line *line)
{
struct blame *blame = line->data;
struct blame_commit *commit = blame->commit;
regmatch_t pmatch;

#define MATCH(text, on) \
(on && *text && regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)

if (commit) {
if (MATCH(commit->title, 1) ||
MATCH(commit->author, opt_author) ||
MATCH(commit->id, opt_date))
return TRUE;

if (MATCH(mkdate(&commit->time), 1))
return TRUE;
}

return MATCH(blame->text, 1);
const char *text[] = {
blame->text,
commit ? commit->title : "",
commit ? commit->id : "",
commit && opt_author ? commit->author : "",
commit && opt_date ? mkdate(&commit->time) : "",
};

#undef MATCH
return grep_text(view, text);
}

static void
Expand Down Expand Up @@ -5357,29 +5354,12 @@ static bool
status_grep(struct view *view, struct line *line)
{
struct status *status = line->data;
enum { S_STATUS, S_NAME, S_END } state;
char buf[2] = "?";
regmatch_t pmatch;

if (!status)
return FALSE;

for (state = S_STATUS; state < S_END; state++) {
const char *text;
if (status) {
const char buf[2] = { status->status, 0 };
const char *text[] = { status->new.name, buf, NULL };

switch (state) {
case S_NAME: text = status->new.name; break;
case S_STATUS:
buf[0] = status->status;
text = buf;
break;

default:
return FALSE;
}

if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
return TRUE;
return grep_text(view, text);
}

return FALSE;
Expand Down Expand Up @@ -6045,7 +6025,7 @@ grep_refs(struct ref **refs, regex_t *regex)
regmatch_t pmatch;
size_t i = 0;

if (!refs)
if (!opt_show_refs || !refs)
return FALSE;
do {
if (regexec(regex, refs[i]->name, 1, &pmatch, 0) != REG_NOMATCH)
Expand All @@ -6059,41 +6039,14 @@ static bool
main_grep(struct view *view, struct line *line)
{
struct commit *commit = line->data;
enum { S_TITLE, S_AUTHOR, S_DATE, S_REFS, S_END } state;
regmatch_t pmatch;

for (state = S_TITLE; state < S_END; state++) {
const char *text;

switch (state) {
case S_TITLE: text = commit->title; break;
case S_AUTHOR:
if (!opt_author)
continue;
text = commit->author;
break;
case S_DATE:
if (!opt_date)
continue;
text = mkdate(&commit->time);
if (!text)
continue;
break;
case S_REFS:
if (!opt_show_refs)
continue;
if (grep_refs(commit->refs, view->regex) == TRUE)
return TRUE;
continue;
default:
return FALSE;
}

if (regexec(view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
return TRUE;
}
const char *text[] = {
commit->title,
opt_author ? commit->author : "",
opt_date ? mkdate(&commit->time) : "",
NULL
};

return FALSE;
return grep_text(view, text) || grep_refs(commit->refs, view->regex);
}

static void
Expand Down

0 comments on commit 436674f

Please sign in to comment.