Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight matching regex pattern in pager, via option color-pager-regex #1249

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions include/tig/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ typedef struct view_column *view_settings;
_(vertical_split, enum vertical_split, VIEW_RESET_DISPLAY | VIEW_DIFF_LIKE) \
_(wrap_lines, bool, VIEW_DIFF_LIKE) \
_(wrap_search, bool, VIEW_NO_FLAGS) \
_(color_pager_regex, const char *, VIEW_NO_FLAGS) \

#define DEFINE_OPTION_EXTERNS(name, type, flags) extern type opt_##name;
OPTION_INFO(DEFINE_OPTION_EXTERNS)
Expand Down
16 changes: 16 additions & 0 deletions src/diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,28 @@ 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 && opt_color_pager_regex != NULL && *opt_color_pager_regex)
{
regex_err = regcomp(&regex, opt_color_pager_regex, 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
2 changes: 2 additions & 0 deletions tigrc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ set mouse = no # Enable mouse support?
set mouse-scroll = 3 # Number of lines to scroll via the mouse
set mouse-wheel-cursor = no # Prefer moving the cursor to scrolling the view?
set pgrp = no # Make tig process-group leader
set color-pager-regex = ""

# User-defined commands
# ---------------------
Expand Down Expand Up @@ -447,6 +448,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 magenta default bold

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