emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] master 7c9ac11: Add customizable to display raw bytes as h


From: Eli Zaretskii
Subject: [Emacs-diffs] master 7c9ac11: Add customizable to display raw bytes as hex
Date: Thu, 1 Jun 2017 14:17:10 -0400 (EDT)

branch: master
commit 7c9ac111c5e5d92e620b666893993d5dc562e483
Author: Eli Zaretskii <address@hidden>
Commit: Eli Zaretskii <address@hidden>

    Add customizable to display raw bytes as hex
    
    * src/xdisp.c (get_next_display_element): Dispatch used format string
    for unprintables based on new display-raw-bytes-as-hex variable.
    (display-raw-bytes-as-hex): New variable.  (Bug#27122)
    
    * lisp/cus-start.el: Add defcustom form for display-raw-bytes-as-hex.
    
    * doc/emacs/display.texi: Document the new variable.
    * etc/NEWS: Mention display-raw-bytes-as-hex.
    
    * test/manual/redisplay-testsuite.el (test-redisplay-5-toggle)
    (test-redisplay-5): New tests.
    (test-redisplay): Call test-redisplay-5.
---
 doc/emacs/display.texi             |  6 ++++++
 etc/NEWS                           |  4 ++++
 lisp/cus-start.el                  |  1 +
 src/xdisp.c                        | 16 +++++++++++++---
 test/manual/redisplay-testsuite.el | 27 ++++++++++++++++++++++++++-
 5 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/doc/emacs/display.texi b/doc/emacs/display.texi
index a0d0792..c4554eb 100644
--- a/doc/emacs/display.texi
+++ b/doc/emacs/display.texi
@@ -1763,3 +1763,9 @@ itself, in pixels; the default is 2.
 in text that is hard to read.  Call the function
 @code{tty-suppress-bold-inverse-default-colors} with a address@hidden
 argument to suppress the effect of bold-face in this case.
+
address@hidden display-raw-bytes-as-hex
+  Raw bytes are displayed in octal format by default, for example a
+byte with a decimal value of 128 is displayed as @code{\200}.  To
+change display to the hexadecimal format of @code{\x80}, set the
+variable @code{display-raw-bytes-as-hex} to @code{t}.
diff --git a/etc/NEWS b/etc/NEWS
index 43e7897..055de8c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -364,6 +364,10 @@ large integers from being displayed as characters.
 ** Two new commands for finding the source code of Emacs Lisp
 libraries: 'find-library-other-window' and 'find-library-other-frame'.
 
++++
+** The new variable 'display-raw-bytes-as-hex' allows to change the
+display of raw bytes from octal to hex.
+
 
 * Editing Changes in Emacs 26.1
 
diff --git a/lisp/cus-start.el b/lisp/cus-start.el
index 4253d40..744fe7f 100644
--- a/lisp/cus-start.el
+++ b/lisp/cus-start.el
@@ -583,6 +583,7 @@ since it could result in memory overflow and make Emacs 
crash."
                       (const :tag "Fit (t)" :value t)
                       (const :tag "Grow only" :value grow-only))
              "25.1")
+            (display-raw-bytes-as-hex display boolean "26.1")
             ;; xfaces.c
             (scalable-fonts-allowed display boolean "22.1")
             ;; xfns.c
diff --git a/src/xdisp.c b/src/xdisp.c
index eaa701e..53210e5 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -7055,7 +7055,7 @@ get_next_display_element (struct it *it)
             translated too.
 
             Non-printable characters and raw-byte characters are also
-            translated to octal form.  */
+            translated to octal or hexadecimal form.  */
          if (((c < ' ' || c == 127) /* ASCII control chars.  */
               ? (it->area != TEXT_AREA
                  /* In mode line, treat \n, \t like other crl chars.  */
@@ -7162,9 +7162,12 @@ get_next_display_element (struct it *it)
                int len, i;
 
                if (CHAR_BYTE8_P (c))
-                 /* Display \200 instead of \17777600.  */
+                 /* Display \200 or \x80 instead of \17777600.  */
                  c = CHAR_TO_BYTE8 (c);
-               len = sprintf (str, "%03o", c + 0u);
+               const char *format_string = display_raw_bytes_as_hex
+                                           ? "x%02x"
+                                           : "%03o";
+               len = sprintf (str, format_string, c + 0u);
 
                XSETINT (it->ctl_chars[0], escape_glyph);
                for (i = 0; i < len; i++)
@@ -32231,6 +32234,13 @@ display table takes effect; in this case, Emacs does 
not consult
   /* Initialize to t, since we need to disable reordering until
      loadup.el successfully loads charprop.el.  */
   redisplay__inhibit_bidi = true;
+
+  DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
+    doc: /* Non-nil means display raw bytes in hexadecimal format.
+The default is to use octal format (\200) whereas hexadecimal (\x80)
+may be more familar to users.  */);
+  display_raw_bytes_as_hex = false;
+
 }
 
 
diff --git a/test/manual/redisplay-testsuite.el 
b/test/manual/redisplay-testsuite.el
index 694d55a..2175cba 100644
--- a/test/manual/redisplay-testsuite.el
+++ b/test/manual/redisplay-testsuite.el
@@ -34,7 +34,8 @@
     (setq overlay (make-overlay opoint (point)))
     (while props
       (overlay-put overlay (car props) (cadr props))
-      (setq props (cddr props)))))
+      (setq props (cddr props)))
+    overlay))
 
 (defun test-redisplay-1 ()
   (insert "Test 1: Displaying adjacent and overlapping overlays:\n\n")
@@ -293,6 +294,29 @@ static unsigned char x_bits[] = {0xff, 0x81, 0xbd, 0xa5, 
0xa5, 0xbd, 0x81, 0xff
 
   (insert "\n"))
 
+(defvar test-redisplay-5-expected-overlay nil)
+(defvar test-redisplay-5-result-overlay nil)
+
+(defun test-redisplay-5-toggle (_event)
+  (interactive "e")
+  (setq display-raw-bytes-as-hex (not display-raw-bytes-as-hex))
+  (let ((label (if display-raw-bytes-as-hex "\\x80" "\\200")))
+    (overlay-put test-redisplay-5-expected-overlay 'display
+                 (propertize label 'face 'escape-glyph))))
+
+(defun test-redisplay-5 ()
+  (insert "Test 5: Display of raw bytes:\n\n")
+  (insert "  Expected: ")
+  (setq test-redisplay-5-expected-overlay
+        (test-insert-overlay " " 'display
+                             (propertize "\\200" 'face 'escape-glyph)))
+  (insert "\n    Result: ")
+  (setq test-redisplay-5-result-overlay
+        (test-insert-overlay " " 'display "\200"))
+  (insert "\n\n")
+  (insert-button "Toggle between octal and hex display"
+                 'action 'test-redisplay-5-toggle))
+
 
 (defun test-redisplay ()
   (interactive)
@@ -309,5 +333,6 @@ static unsigned char x_bits[] = {0xff, 0x81, 0xbd, 0xa5, 
0xa5, 0xbd, 0x81, 0xff
     (test-redisplay-2)
     (test-redisplay-3)
     (test-redisplay-4)
+    (test-redisplay-5)
     (goto-char (point-min))))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]