From: Ping Yin Date: Fri, 02 May 2008 03:39:24 GMT Subject: [PATCH] Make words boundary for --color-words configurable Message-ID: <1209699564-2800-1-git-send-email-pkufranky@gmail.com> Previously --color-words only allow spaces as words boundary. However, just space is not enough. For example, when i rename a function from foo to bar, following example doesn't show as expected when using --color-words. ------------------ - if (foo(arg)) + if (bar(arg)) ------------------ It shows as "if (foo(arg))(foo(arg))". Actually, it's the best to show as "if (foobar(arg))". Here "r" and "g" represent "red" and "green" separately. This patch introduces a configuration diff.wordsboundary to make --color-words treat both spaces and characters in diff.wordsboundary as boundary characters. If we set diff.wordsboundary to "()", the example above will show as "if (foo(bar(arg))". It's much better, athough not the best, Signed-off-by: Ping Yin --- diff.c | 14 ++++++++++++-- 1 files changed, 12 insertions(+), 2 deletions(-) diff --git a/diff.c b/diff.c index 3632b55..2c37bb6 100644 --- a/diff.c +++ b/diff.c @@ -23,6 +23,7 @@ static int diff_rename_limit_default = 100; int diff_use_color_default = -1; static const char *external_diff_cmd_cfg; int diff_auto_refresh_index = 1; +static const char *diff_words_boundary = ""; static char diff_colors[][COLOR_MAXLEN] = { "\033[m", /* reset */ @@ -159,6 +160,10 @@ int git_diff_ui_config(const char *var, const char *value) external_diff_cmd_cfg = xstrdup(value); return 0; } + if (!strcmp(var, "diff.wordsboundary")) { + diff_words_boundary = value ? xstrdup(value) : ""; + return 0; + } if (!prefixcmp(var, "diff.")) { const char *ep = strrchr(var, '.'); @@ -434,6 +439,11 @@ static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len) } } +static int iswordsboundary(char c) +{ + return isspace(c) || !!strchr(diff_words_boundary, c); +} + /* this executes the word diff on the accumulated buffers */ static void diff_words_show(struct diff_words_data *diff_words) { @@ -448,7 +458,7 @@ static void diff_words_show(struct diff_words_data *diff_words) minus.ptr = xmalloc(minus.size); memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size); for (i = 0; i < minus.size; i++) - if (isspace(minus.ptr[i])) + if (iswordsboundary(minus.ptr[i])) minus.ptr[i] = '\n'; diff_words->minus.current = 0; @@ -456,7 +466,7 @@ static void diff_words_show(struct diff_words_data *diff_words) plus.ptr = xmalloc(plus.size); memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size); for (i = 0; i < plus.size; i++) - if (isspace(plus.ptr[i])) + if (iswordsboundary(plus.ptr[i])) plus.ptr[i] = '\n'; diff_words->plus.current = 0; -- 1.5.5.1.116.ge4b9c.dirty