git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] --color-words: Make the word characters configurable

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
May 3, 2008, 14:03 UTC
Message-ID
<alpine.DEB.1.00.0805031501290.30431@racer>
In-Reply-To
<1209736766-8029-1-git-send-email-pkufranky@gmail.com>

Now, you can specify which characters are to be interpreted as word characters with "--color-words=A-Za-z", or by setting the config variable diff.wordCharacters.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
	I would have preferred an approach like this.
 Documentation/config.txt       |    6 ++++
 Documentation/diff-options.txt |    8 ++++-
 README                         |    2 +-
 diff.c                         |   64 +++++++++++++++++++++++++++++++++++++++-
 4 files changed, 77 insertions(+), 3 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 05bf2df..663d82b 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -546,6 +546,12 @@ diff.renames::
 	will enable basic rename detection.  If set to "copies" or
 	"copy", it will detect copies, as well.
 
+diff.wordcharacters::
+	This config setting overrides which characters are interpreted as
+	word characters by the --color-words option of linkgit:git-diff[1].
++
+The default is: all ASCII characters excluding NUL to SPACE.
+
 fetch.unpackLimit::
 	If the number of objects fetched over the git native
 	transfer is below this
diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt
index 13234fa..88ea5d4 100644
--- a/Documentation/diff-options.txt
+++ b/Documentation/diff-options.txt
@@ -93,8 +93,14 @@ endif::git-format-patch[]
 	Turn off colored diff, even when the configuration file
 	gives the default to color output.
 
---color-words::
+--color-words[=<set>]::
 	Show colored word diff, i.e. color words which have changed.
++
+If a set of characters is specified, it is interpreted as the range of
+word characters.  Example: "0-9A-Fa-f".  As a convenience, "[:alnum:]"
+and "[:alpha:]" expand to alpha-numeric and alpha characters,
+respectively.  This argument overrides the config setting
+'diff.wordCharacters'.
 
 --no-renames::
 	Turn off rename detection, even when the configuration
diff --git a/README b/README
index 548142c..0e325e2 100644
--- a/README
+++ b/README
@@ -4,7 +4,7 @@
 
 ////////////////////////////////////////////////////////////////
 
-"git" can mean anything, depending on your mood.
+"git" cann mean anything, depending on your mood.
 
  - random three-letter combination that is pronounceable, and not
    actually used by any common UNIX command.  The fact that it is a
diff --git a/diff.c b/diff.c
index 3632b55..3e8719c 100644
--- a/diff.c
+++ b/diff.c
@@ -23,6 +23,20 @@ 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 char word_character[256] = {
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+	1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+};
 
 static char diff_colors[][COLOR_MAXLEN] = {
 	"\033[m",	/* reset */
@@ -123,6 +137,44 @@ static int parse_funcname_pattern(const char *var, const char *ep, const char *v
 	return 0;
 }
 
+static void set_word_character_range(char start, char end)
+{
+	int i;
+	for (i = (unsigned char)start; i <= (unsigned char)end; i++)
+		word_character[i] = 1;
+}
+
+static int set_word_characters(const char *set)
+{
+	int previous_character = -1;
+
+	memset(word_character, 0, sizeof(word_character));
+
+	/* parse values like "0-9[:alnum:]" */
+	for (; *set; set++)
+		if (!prefixcmp(set, "[:alpha:]")) {
+			set_word_character_range('A', 'Z');
+			set_word_character_range('a', 'z');
+			previous_character = -1;
+			set += 8;
+		} else if (!prefixcmp(set, "[:alnum:]")) {
+			set_word_character_range('A', 'Z');
+			set_word_character_range('a', 'z');
+			set_word_character_range('0', '9');
+			previous_character = -1;
+			set += 8;
+		} else if (*set == '-' && previous_character >= 0) {
+			set++;
+			set_word_character_range(previous_character, *set);
+			previous_character = -1;
+		} else {
+			word_character[(unsigned int)*set] = 1;
+			previous_character = *set;
+		}
+
+	return 0;
+}
+
 /*
  * These are to give UI layer defaults.
  * The core-level commands such as git-diff-files should
@@ -179,6 +231,12 @@ int git_diff_basic_config(const char *var, const char *value)
 		return 0;
 	}
 
+	if (!strcmp(var, "diff.wordcharacters")) {
+		if (!value)
+			return config_error_nonbool(var);
+		return set_word_characters(value);
+	}
+
 	if (!prefixcmp(var, "diff.")) {
 		const char *ep = strrchr(var, '.');
 		if (ep != var + 4) {
@@ -456,7 +514,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 (!word_character[(unsigned char)plus.ptr[i]])
 			plus.ptr[i] = '\n';
 	diff_words->plus.current = 0;
 
@@ -2489,6 +2547,10 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 		DIFF_OPT_CLR(options, COLOR_DIFF);
 	else if (!strcmp(arg, "--color-words"))
 		options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
+	else if (!prefixcmp(arg, "--color-words=")) {
+		options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
+		set_word_characters(arg + 13);
+	}
 	else if (!strcmp(arg, "--exit-code"))
 		DIFF_OPT_SET(options, EXIT_WITH_STATUS);
 	else if (!strcmp(arg, "--quiet"))
-- 
1.5.5.1.266.g7cbb
Previous: Ping YinNext: Ping Yin
Message 80 of 92 in “Make words boundary for --color-words configurable”
  1. Make words boundary for --color-words configurablePing Yin, May 2, 2008
  2. Junio C HamanoMay 2, 2008
  3. Ping YinMay 2, 2008
  4. Make boundary characters for --color-words configurablePing Yin, May 2, 2008
  5. Ping YinMay 2, 2008
  6. Ping YinMay 2, 2008
  7. 0/5 Make boundary characters for --color-words configurablePing Yin, May 3, 2008
  8. 1/5 diff.c: Remove code redundancy in diff_words_showPing Yin, May 3, 2008
  9. 2/5 diff.c: Use show variable name in fn_out_diff_words_auxPing Yin, May 3, 2008
  10. 3/5 diff.c: Fix --color-words showing trailing deleted words at another linePing Yin, May 3, 2008
  11. 4/5 Make boundary characters for --color-words configurablePing Yin, May 3, 2008
  12. 5/5 fn_out_diff_words_aux: Handle common diff line more carefullyPing Yin, May 3, 2008
  13. Junio C HamanoMay 3, 2008
  14. Teemu LikonenMay 3, 2008
  15. Ping YinMay 4, 2008
  16. Johannes SchindelinMay 4, 2008
  17. Ping YinMay 4, 2008
  18. Junio C HamanoMay 4, 2008
  19. Jakub NarebskiMay 4, 2008
  20. Teemu LikonenMay 4, 2008
  21. Johannes SchindelinMay 5, 2008
  22. Ping YinMay 5, 2008
  23. Junio C HamanoMay 5, 2008
  24. Ping YinMay 5, 2008
  25. Ping YinMay 6, 2008
  26. Johannes SchindelinMay 6, 2008
  27. Ping YinMay 7, 2008
  28. Johannes SchindelinMay 7, 2008
  29. Ping YinMay 7, 2008
  30. Johannes SchindelinMay 7, 2008
  31. Ping YinMay 7, 2008
  32. Junio C HamanoMay 7, 2008
  33. Junio C HamanoMay 7, 2008
  34. Jeff KingMay 7, 2008
  35. Junio C HamanoMay 7, 2008
  36. Jeff KingMay 7, 2008
  37. Teemu LikonenMay 8, 2008
  38. Ping YinMay 10, 2008
  39. Teemu LikonenMay 10, 2008
  40. Ping YinMay 11, 2008
  41. Ping YinMay 11, 2008
  42. Junio C HamanoMay 11, 2008
  43. Ping YinMay 12, 2008
  44. Jakub NarebskiMay 12, 2008
  45. Junio C HamanoMay 12, 2008
  46. Jakub NarebskiMay 12, 2008
  47. Ping YinMay 13, 2008
  48. Ping YinMay 13, 2008
  49. Ping YinMay 10, 2008
  50. Johannes SchindelinMay 5, 2008
  51. Ping YinMay 5, 2008
  52. Junio C HamanoMay 3, 2008
  53. Ping YinMay 3, 2008
  54. Junio C HamanoMay 3, 2008
  55. Junio C HamanoMay 3, 2008
  56. 0/6 --color-words improvementPing Yin, May 4, 2008
  57. 1/6 diff.c: Remove code redundancy in diff_words_showPing Yin, May 4, 2008
  58. 2/6 fn_out_diff_words_aux: Use short variable namePing Yin, May 4, 2008
  59. 3/6 --color-words: Fix showing trailing deleted words at another linePing Yin, May 4, 2008
  60. 4/6 --color-words: Make non-word characters configurablePing Yin, May 4, 2008
  61. 5/6 fn_out_diff_words_aux: Handle common diff line more carefullyPing Yin, May 4, 2008
  62. 6/6 --color-words: Add test t4030Ping Yin, May 4, 2008
  63. Johannes SchindelinMay 4, 2008
  64. Ping YinMay 4, 2008
  65. Johannes SchindelinMay 5, 2008
  66. Ping YinMay 5, 2008
  67. Junio C HamanoMay 4, 2008
  68. Ping YinMay 4, 2008
  69. Johannes SchindelinMay 4, 2008
  70. Ping YinMay 4, 2008
  71. Johannes SchindelinMay 5, 2008
  72. Johannes SchindelinMay 4, 2008
  73. Ping YinMay 4, 2008
  74. Johannes SchindelinMay 5, 2008
  75. Johannes SchindelinMay 4, 2008
  76. Teemu LikonenMay 2, 2008
  77. Ping YinMay 3, 2008
  78. Dirk SüsserottMay 3, 2008
  79. Ping YinMay 3, 2008
  80. --color-words: Make the word characters configurableJohannes Schindelin, May 3, 2008
  81. Ping YinMay 3, 2008
  82. Johannes SchindelinMay 3, 2008
  83. Teemu LikonenMay 3, 2008
  84. Johannes SchindelinMay 4, 2008
  85. Junio C HamanoMay 3, 2008
  86. Johannes SchindelinMay 4, 2008
  87. Johannes SchindelinMay 2, 2008
  88. Teemu LikonenMay 2, 2008
  89. Ping YinMay 2, 2008
  90. Teemu LikonenMay 2, 2008
  91. Ping YinMay 2, 2008
  92. Jakub NarebskiMay 3, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.