Skip to content

Commit

Permalink
Highlight matching regex pattern in pager, via option `color-pager-re…
Browse files Browse the repository at this point in the history
…gex`

Any line matching the regex will be drawn in a new color ↔ `color-match`.
  • Loading branch information
psprint committed Jul 21, 2022
1 parent 232af7d commit a92e5cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/tig/line.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct ref;
*/

#define LINE_INFO(_) \
_(COLOR_MATCH, ""), \
_(DIFF_HEADER, "diff --"), \
_(DIFF_DEL_FILE, "--- "), \
_(DIFF_ADD_FILE, "+++ "), \
Expand Down
26 changes: 26 additions & 0 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,38 @@ diff_common_highlight(struct view *view, const char *text, enum line_type type)
bool
diff_common_read(struct view *view, const char *data, struct diff_state *state)
{
const char *regex_txt;
regex_t regex;
regmatch_t pmatch[10];
int regex_flags = REG_EXTENDED, regex_err;
enum line_type type = get_line_type(data);

/* ADD2 and DEL2 are only valid in combined diff hunks */
if (!state->combined_diff && (type == LINE_DIFF_ADD2 || type == LINE_DIFF_DEL2))
type = LINE_DEFAULT;


if (type == LINE_DEFAULT)
{
/* Parse all compiler message lines, ie. such as:
*
* {file}:{line}:{col}: (note|warning|error):{message}
*
*/
if (opt_color_pager_regex != NULL && *opt_color_pager_regex)
regex_txt = opt_color_pager_regex;
else
/* A reasonable default - match GCC errors */
regex_txt = "([^:]+):([0-9]+):(|([0-9]+):)[ \t]+(note|warning|error): .*$";
regex_err = regcomp(&regex, regex_txt, regex_flags);

if (!regex_err)
regex_err = regexec(&regex, data, 8, pmatch, 0);

if (!regex_err)
type = LINE_COLOR_MATCH;
regfree(&regex);
}
/* DEL_FILE, ADD_FILE and START are only valid outside diff chunks */
if (state->reading_diff_chunk) {
if (type == LINE_DIFF_DEL_FILE || type == LINE_DIFF_START)
Expand Down
1 change: 1 addition & 0 deletions tigrc
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ color palette-12 white default bold
color palette-13 red default bold
color graph-commit blue default
color search-result black yellow
color color-match yellow default bold

# Mappings for colors read from git configuration.
# Set to "no" to disable.
Expand Down

0 comments on commit a92e5cb

Please sign in to comment.