Git development
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Ping Yin <pkufranky@gmail.com>
Cc: git@vger.kernel.org, gitster@pobox.com
Subject: [PATCH] --color-words: Make the word characters configurable
Date: Sat, 3 May 2008 15:03:17 +0100 (BST)	[thread overview]
Message-ID: <alpine.DEB.1.00.0805031501290.30431@racer> (raw)
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

  parent reply	other threads:[~2008-05-03 14:04 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-02  3:39 [PATCH] Make words boundary for --color-words configurable Ping Yin
2008-05-02  3:54 ` Junio C Hamano
2008-05-02  4:28   ` Ping Yin
2008-05-02 13:59     ` [PATCH] Make boundary characters " Ping Yin
2008-05-02 14:26       ` Ping Yin
2008-05-02 14:27         ` Ping Yin
2008-05-03 11:57         ` [PATCH v2 0/5] " Ping Yin
2008-05-03 11:57           ` [PATCH v2 1/5] diff.c: Remove code redundancy in diff_words_show Ping Yin
2008-05-03 11:57             ` [PATCH v2 2/5] diff.c: Use show variable name in fn_out_diff_words_aux Ping Yin
2008-05-03 11:57               ` [PATCH v2 3/5] diff.c: Fix --color-words showing trailing deleted words at another line Ping Yin
2008-05-03 11:57                 ` [PATCH v2 4/5] Make boundary characters for --color-words configurable Ping Yin
2008-05-03 11:57                   ` [PATCH v2 5/5] fn_out_diff_words_aux: Handle common diff line more carefully Ping Yin
2008-05-03 18:18                   ` [PATCH v2 4/5] Make boundary characters for --color-words configurable Junio C Hamano
2008-05-03 18:41                     ` Teemu Likonen
2008-05-04  0:32                     ` Ping Yin
2008-05-04  9:44                       ` Johannes Schindelin
2008-05-04 16:35                         ` Ping Yin
2008-05-04 20:16                           ` Junio C Hamano
2008-05-04 20:47                             ` Jakub Narebski
2008-05-04 21:27                               ` Teemu Likonen
2008-05-05 12:14                               ` Johannes Schindelin
2008-05-05  1:40                             ` Ping Yin
2008-05-05  5:00                               ` Junio C Hamano
2008-05-05 12:10                                 ` Ping Yin
2008-05-06  0:40                                   ` Ping Yin
2008-05-06  8:55                                     ` Johannes Schindelin
2008-05-07  1:15                                       ` Ping Yin
2008-05-07 11:24                                         ` Johannes Schindelin
2008-05-07 12:19                                           ` Ping Yin
2008-05-07 13:10                                             ` Johannes Schindelin
2008-05-07 14:11                                               ` Ping Yin
2008-05-07 19:13                                           ` Junio C Hamano
2008-05-07 19:33                                             ` Junio C Hamano
2008-05-07 19:45                                             ` Jeff King
2008-05-07 20:02                                               ` Junio C Hamano
2008-05-07 22:04                                                 ` Jeff King
2008-05-08 10:34                                             ` Teemu Likonen
2008-05-10  9:02                                               ` Ping Yin
2008-05-10  9:14                                                 ` Teemu Likonen
2008-05-11 13:16                                                 ` Ping Yin
2008-05-11 13:27                                                   ` Ping Yin
2008-05-11 16:27                                                   ` Junio C Hamano
2008-05-12 16:31                                                     ` Ping Yin
2008-05-12 18:57                                                       ` Jakub Narebski
2008-05-12 19:17                                                         ` Junio C Hamano
2008-05-12 19:57                                                           ` Jakub Narebski
2008-05-13  1:37                                                           ` Ping Yin
2008-05-13  1:42                                                         ` Ping Yin
2008-05-10  8:20                                             ` Ping Yin
2008-05-05 11:51                           ` Johannes Schindelin
2008-05-05 12:02                             ` Ping Yin
2008-05-03 18:01                 ` [PATCH v2 3/5] diff.c: Fix --color-words showing trailing deleted words at another line Junio C Hamano
2008-05-03 12:01               ` [PATCH v2 2/5] diff.c: Use show variable name in fn_out_diff_words_aux Ping Yin
2008-05-03 17:47               ` Junio C Hamano
2008-05-03 18:20             ` [PATCH v2 1/5] diff.c: Remove code redundancy in diff_words_show Junio C Hamano
2008-05-04  4:20           ` [PATCH v3 0/6] --color-words improvement Ping Yin
2008-05-04  4:20             ` [PATCH v3 1/6] diff.c: Remove code redundancy in diff_words_show Ping Yin
2008-05-04  4:20               ` [PATCH v3 2/6] fn_out_diff_words_aux: Use short variable name Ping Yin
2008-05-04  4:20                 ` [PATCH v3 3/6] --color-words: Fix showing trailing deleted words at another line Ping Yin
2008-05-04  4:20                   ` [PATCH v3 4/6] --color-words: Make non-word characters configurable Ping Yin
2008-05-04  4:20                     ` [PATCH v3 5/6] fn_out_diff_words_aux: Handle common diff line more carefully Ping Yin
2008-05-04  4:20                       ` [PATCH v3 6/6] --color-words: Add test t4030 Ping Yin
2008-05-04  9:54                       ` [PATCH v3 5/6] fn_out_diff_words_aux: Handle common diff line more carefully Johannes Schindelin
2008-05-04 16:53                         ` Ping Yin
2008-05-05 12:11                           ` Johannes Schindelin
2008-05-05 14:18                             ` Ping Yin
2008-05-04  6:45                     ` [PATCH v3 4/6] --color-words: Make non-word characters configurable Junio C Hamano
2008-05-04  7:04                       ` Ping Yin
2008-05-04  9:52                   ` [PATCH v3 3/6] --color-words: Fix showing trailing deleted words at another line Johannes Schindelin
2008-05-04 16:48                     ` Ping Yin
2008-05-05 12:10                       ` Johannes Schindelin
2008-05-04  9:47                 ` [PATCH v3 2/6] fn_out_diff_words_aux: Use short variable name Johannes Schindelin
2008-05-04 16:39                   ` Ping Yin
2008-05-05 12:05                     ` Johannes Schindelin
2008-05-04  9:46               ` [PATCH v3 1/6] diff.c: Remove code redundancy in diff_words_show Johannes Schindelin
2008-05-02 14:36       ` [PATCH] Make boundary characters for --color-words configurable Teemu Likonen
2008-05-03  0:22         ` Ping Yin
2008-05-03 13:22           ` Dirk Süsserott
2008-05-03 13:57             ` Ping Yin
2008-05-03 14:03       ` Johannes Schindelin [this message]
2008-05-03 14:13         ` [PATCH] --color-words: Make the word characters configurable Ping Yin
2008-05-03 14:23           ` Johannes Schindelin
2008-05-03 14:43         ` Teemu Likonen
2008-05-04  9:18           ` Johannes Schindelin
2008-05-03 17:43         ` Junio C Hamano
2008-05-04  9:25           ` Johannes Schindelin
2008-05-02  7:45 ` [PATCH] Make words boundary for --color-words configurable Johannes Schindelin
2008-05-02  8:14   ` Teemu Likonen
2008-05-02  9:23     ` Ping Yin
2008-05-02 10:01     ` Teemu Likonen
2008-05-02  9:28   ` Ping Yin
2008-05-03  0:18   ` Jakub Narebski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.1.00.0805031501290.30431@racer \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pkufranky@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox