patch 9.2.0580: xxd: binary output is not colored with -R

Commit: 
https://github.com/vim/vim/commit/f0cae9d5abba1ad97f8c78c8d3cbb0d1c46d5aff
Author: Hirohito Higashi <[email protected]>
Date:   Sun May 31 21:11:55 2026 +0000

    patch 9.2.0580: xxd: binary output is not colored with -R
    
    Problem:  With xxd the -R option colors the hex output but leaves the
              binary output produced by -b uncolored (Boris Verkhovskiy)
    Solution: Color the binary (bits) output per byte with the same colors as
              the hex output, update the documentation and add a test
              (Hirohito Higashi).
    
    fixes:  #20385
    closes: #20401
    
    Signed-off-by: Hirohito Higashi <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/runtime/doc/xxd.1 b/runtime/doc/xxd.1
index 48166e03c..d80c5e9c0 100644
--- a/runtime/doc/xxd.1
+++ b/runtime/doc/xxd.1
@@ -142,9 +142,9 @@ anywhere. Use the combination
 to read a bits dump instead of a hex dump.
 .TP
 .IR \-R " " when
-In the output the hex-value and the value are both colored with the same color
-depending on the hex-value. Mostly helping to differentiate printable and
-non-printable characters.
+In the output both the data column (hex, or bits with \-b) and the character
+column are colored with the same color depending on the byte value. Mostly
+helping to differentiate printable and non-printable characters.
 .I IwhenP
 is
 .BR never ", " always ", or " auto " (default: auto).
diff --git a/runtime/doc/xxd.man b/runtime/doc/xxd.man
index 8beba9c88..17a9dd14b 100644
--- a/runtime/doc/xxd.man
+++ b/runtime/doc/xxd.man
@@ -101,11 +101,12 @@ OPTIONS
               instead of a hex dump.
 
        -R when
-              In the output the hex-value and the value are both colored  with
-              the  same  color  depending  on the hex-value. Mostly helping to
-              differentiate printable and non-printable characters.   when  is
-              never,  always, or auto (default: auto).  When the $NO_COLOR en‐
-              vironment variable is set, colorization will be disabled.
+              In the output both the data column (hex, or bits  with  -b)  and
+              the  character  column are colored with the same color depending
+              on the byte value. Mostly helping to differentiate printable and
+              non-printable characters.  when is never, always, or  auto  (de‐
+              fault:  auto).   When the $NO_COLOR environment variable is set,
+              colorization will be disabled.
 
        -seek offset
               When used after -r: revert with <offset> added to file positions
diff --git a/src/testdir/test_xxd.vim b/src/testdir/test_xxd.vim
index 430a07ccf..990683b17 100644
--- a/src/testdir/test_xxd.vim
+++ b/src/testdir/test_xxd.vim
@@ -663,6 +663,22 @@ call writefile(data,'Xinput')
 
 endfunc
 
+func Test_xxd_color_bits()
+  " Binary output (-b) should be colored per byte like the hex output,
+  " see issue #20385.  Bytes cover the white/yellow/green/blue color groups.
+  let s:test = 1
+  call writefile(0z000941FF, 'Xxxdbits')
+
+  %d
+  exe '0r! ' . s:xxd_cmd . ' -b -R always -c 4 Xxxdbits'
+  $d
+  let expected = [
+      \ "00000000:  [1;37m00000000 [0m  [1;33m00001001 [0m  [1;32m01000001 [0m 
 [1;34m11111111 [0m   [1;37m. [0m [1;33m. [0m [1;32mA [0m [1;34m. [0m"]
+  call assert_equal(expected, getline(1, '$'), s:Mess(s:test))
+
+  call delete('Xxxdbits')
+endfunc
+
 func Test_xxd_color2()
   CheckScreendump
   CheckUnix
diff --git a/src/version.c b/src/version.c
index 95107bb28..e39f67aad 100644
--- a/src/version.c
+++ b/src/version.c
@@ -729,6 +729,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    580,
 /**/
     579,
 /**/
diff --git a/src/xxd/xxd.c b/src/xxd/xxd.c
index fb45f6c28..323d5a398 100644
--- a/src/xxd/xxd.c
+++ b/src/xxd/xxd.c
@@ -75,6 +75,7 @@
  * 19.03.2026  Add -t option to end output with terminating null
  * 25.03.2026  Fix color output issues
  * 26.04.2026  Use unsigned long for printing offsets
+ * 31.05.2026  Colorize binary output
  *
  * (c) 1990-1998 by Juergen Weigert ([email protected])
  *
@@ -155,7 +156,7 @@ extern void perror __P((char *));
 # endif
 #endif
 
-char version[] = "xxd 2026-04-26 by Juergen Weigert et al.";
+char version[] = "xxd 2026-05-31 by Juergen Weigert et al.";
 #ifdef WIN32
 char osver[] = " (Win32)";
 #else
@@ -1194,8 +1195,15 @@ main(int argc, char *argv[])
        }
       else /* hextype == HEX_BITS */
        {
+         if (color)
+           cur_color = get_color_char(e, ebcdic);
+
          for (i = 7; i >= 0; i--)
-           l[c++] = (e & (1 << i)) ? '1' : '0';
+           {
+             if (color)
+               colors[c] = cur_color;
+             l[c++] = (e & (1 << i)) ? '1' : '0';
+           }
        }
       if (e)
        nonzero++;

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/vim_dev/E1wTnUx-00GaQo-O8%40256bit.org.

Raspunde prin e-mail lui