unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@linkov.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 44155@debbugs.gnu.org, schwab@linux-m68k.org
Subject: bug#44155: Print integers as characters
Date: Wed, 28 Oct 2020 21:41:46 +0200	[thread overview]
Message-ID: <87imaury51.fsf@mail.linkov.net> (raw)
In-Reply-To: <83h7qecps2.fsf@gnu.org> (Eli Zaretskii's message of "Wed, 28 Oct 2020 17:51:25 +0200")

[-- Attachment #1: Type: text/plain, Size: 1353 bytes --]

>> > Alternatively, we could print raw bytes in some special way.  But not
>> > treating them as characters sounds some subtlety that will be hard to
>> > explain.
>> 
>> The existing 'prin1-char' used as a reference implementation
>> doesn't print integers like 4194176 as characters, so the patch
>> does the same.
>
> I don't think it's right, FWIW.  Displaying something like \100 would
> be better, IMO.

Sorry, I don't understand why 4194176 could be printed as \100.

>> +@defvar integer-output-format
>> +This variable specifies how to print integer numbers.  The default is
>> +@code{nil}, meaning use the decimal format.  When bound to @code{t},
>> +print integers as characters when an integer represents a character
>> +(@pxref{Basic Char Syntax}).  When bound to the number @code{16},
>> +print non-negative integers in the hexadecimal format.
>
> This should mention the functions affected by the variable.
>
>> +** New variable 'integer-output-format' defines the format of integers.
>                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> "determines how to print integer values"
>
>> +When this variable is bound to the value 't', integers are printed by
>> +printing functions as characters when an integer represents a character.
>
> Please give at least one example of a function affected by this.

Ok, fixed:


[-- Attachment #2: integer-output-format-4.patch --]
[-- Type: text/x-diff, Size: 4991 bytes --]

diff --git a/doc/lispref/streams.texi b/doc/lispref/streams.texi
index 2cd61ad04f..08d8032e6f 100644
--- a/doc/lispref/streams.texi
+++ b/doc/lispref/streams.texi
@@ -902,3 +902,12 @@ Output Variables
 in the C function @code{sprintf}.  For further restrictions on what
 you can use, see the variable's documentation string.
 @end defvar
+
+@defvar integer-output-format
+This variable specifies how to print integer numbers.  The default is
+@code{nil}, meaning use the decimal format.  When bound to @code{t},
+print integers as characters when an integer represents a character
+(@pxref{Basic Char Syntax}).  When bound to the number @code{16},
+print non-negative integers in the hexadecimal format.
+This variable affects all print functions.
+@end defvar
diff --git a/etc/NEWS b/etc/NEWS
index 5e159480e0..202e449b16 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1641,6 +1641,12 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el.
 \f
 * Lisp Changes in Emacs 28.1
 
+** New variable 'integer-output-format' determines how to print integer values.
+When this variable is bound to the value 't', integers are printed by
+printing functions as characters when an integer represents a character.
+When bound to the number 16, non-negative integers are printed in the
+hexadecimal format.
+
 +++
 ** 'define-globalized-minor-mode' now takes a :predicate parameter.
 This can be used to control which major modes the minor mode should be
diff --git a/src/print.c b/src/print.c
index 53aa353769..7b3dc61065 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1908,8 +1908,31 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
     {
     case_Lisp_Int:
       {
-	int len = sprintf (buf, "%"pI"d", XFIXNUM (obj));
-	strout (buf, len, len, printcharfun);
+        int c;
+        intmax_t i;
+
+        if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj)
+            && (c = XFIXNUM (obj)) && ! CHAR_BYTE8_P (c))
+          {
+            printchar ('?', printcharfun);
+            if (escapeflag
+                && (c == ';' || c == '(' || c == ')' || c == '{' || c == '}'
+                    || c == '[' || c == ']' || c == '\"' || c == '\'' || c == '\\'))
+              printchar ('\\', printcharfun);
+            printchar (c, printcharfun);
+          }
+        else if (INTEGERP (Vinteger_output_format)
+                 && integer_to_intmax (Vinteger_output_format, &i)
+                 && i == 16 && Fnatnump (obj))
+          {
+            int len = sprintf (buf, "#x%"pI"x", (EMACS_UINT) XFIXNUM (obj));
+            strout (buf, len, len, printcharfun);
+          }
+        else
+          {
+            int len = sprintf (buf, "%"pI"d", XFIXNUM (obj));
+            strout (buf, len, len, printcharfun);
+          }
       }
       break;
 
@@ -2247,6 +2270,15 @@ syms_of_print (void)
 that represents the number without losing information.  */);
   Vfloat_output_format = Qnil;
 
+  DEFVAR_LISP ("integer-output-format", Vinteger_output_format,
+	       doc: /* The format used to print integers.
+When t, print characters from integers that represent a character.
+When a number 16, print non-negative integers in the hexadecimal format.
+Otherwise, by default print integers in the decimal format.
+This variable affects all print functions, for example, such function
+as `print'.  */);
+  Vinteger_output_format = Qnil;
+
   DEFVAR_LISP ("print-length", Vprint_length,
 	       doc: /* Maximum length of list to print before abbreviating.
 A value of nil means no limit.  See also `eval-expression-print-length'.  */);
diff --git a/test/src/print-tests.el b/test/src/print-tests.el
index eb9572dbdf..7b026b6b21 100644
--- a/test/src/print-tests.el
+++ b/test/src/print-tests.el
@@ -383,5 +383,25 @@ print-hash-table-test
       (let ((print-length 1))
         (format "%S" h))))))
 
+(print-tests--deftest print-integer-output-format ()
+  ;; Bug#44155.
+  (let ((integer-output-format t)
+        (syms (list ?? ?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\ ?Á)))
+    (should (equal (read (print-tests--prin1-to-string syms)) syms))
+    (should (equal (print-tests--prin1-to-string syms)
+                   (concat "(" (mapconcat #'prin1-char syms " ") ")"))))
+  (let ((integer-output-format t)
+        (syms (list -1 0 1 ?\120 4194175 4194176 (max-char) (1+ (max-char)))))
+    (should (equal (read (print-tests--prin1-to-string syms)) syms)))
+  (let ((integer-output-format 16)
+        (syms (list -1 0 1 most-positive-fixnum (1+ most-positive-fixnum))))
+    (should (equal (read (print-tests--prin1-to-string syms)) syms))
+    (should (equal (print-tests--prin1-to-string syms)
+                   (concat "(" (mapconcat
+                                (lambda (i)
+                                  (if (and (>= i 0) (<= i most-positive-fixnum))
+                                      (format "#x%x" i) (format "%d" i)))
+                                syms " ") ")")))))
+
 (provide 'print-tests)
 ;;; print-tests.el ends here

  reply	other threads:[~2020-10-28 19:41 UTC|newest]

Thread overview: 109+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-08 12:05 bug#43866: 26.3; italian postfix additions Francesco Potortì
2020-10-08 12:26 ` Eli Zaretskii
2020-10-08 12:34   ` Francesco Potortì
2020-10-08 12:39   ` Robert Pluim
2020-10-08 12:57     ` Eli Zaretskii
2020-10-08 13:54       ` Robert Pluim
2020-10-08 14:24         ` Robert Pluim
2020-10-08 14:32           ` Eli Zaretskii
2020-10-08 13:26     ` Francesco Potortì
2020-10-08 14:00       ` Robert Pluim
2020-10-13 20:07     ` Juri Linkov
2020-10-14  2:31       ` Eli Zaretskii
2020-10-14  8:07         ` Juri Linkov
2020-10-14 15:07           ` Eli Zaretskii
2020-10-14 19:40             ` Juri Linkov
2020-10-15  2:34               ` Eli Zaretskii
2020-10-19 20:45                 ` Juri Linkov
2020-10-19 23:12                   ` Stefan Kangas
2020-10-20 18:42                     ` Juri Linkov
2020-10-20 14:12                   ` Eli Zaretskii
2020-10-20 14:47                     ` Robert Pluim
2020-10-20 15:50                       ` Eli Zaretskii
2020-10-20 18:44                       ` Juri Linkov
2020-10-20 19:05                     ` Juri Linkov
2020-10-21  8:11                       ` Robert Pluim
2020-10-21 14:29                         ` Eli Zaretskii
2020-10-21 14:40                           ` Robert Pluim
2020-10-21 15:23                             ` Eli Zaretskii
2020-10-21 17:30                         ` Juri Linkov
2020-10-20 19:56                     ` Juri Linkov
2020-10-21 14:02                       ` Eli Zaretskii
2020-10-21 17:23                         ` Juri Linkov
2020-10-21 18:16                           ` Eli Zaretskii
2020-10-21 18:27                             ` Juri Linkov
2020-10-21 18:35                               ` Eli Zaretskii
2020-10-21 19:39                                 ` Juri Linkov
2020-10-22 12:59                                   ` Eli Zaretskii
2020-10-22 20:56                                     ` bug#44155: Print integers as characters Juri Linkov
2020-10-22 22:39                                       ` Andreas Schwab
2020-10-23  8:16                                         ` Juri Linkov
2020-10-23  8:32                                         ` Juri Linkov
2020-10-24 19:53                                           ` Juri Linkov
2020-10-25 17:22                                             ` Eli Zaretskii
2020-10-25 19:09                                               ` Juri Linkov
2020-10-25 19:53                                                 ` Eli Zaretskii
2020-10-27 20:08                                                   ` Juri Linkov
2020-10-28 15:51                                                     ` Eli Zaretskii
2020-10-28 19:41                                                       ` Juri Linkov [this message]
2020-10-29 14:20                                                         ` Eli Zaretskii
2020-10-29 21:00                                                           ` Juri Linkov
2020-10-30  7:35                                                             ` Eli Zaretskii
2020-10-31 20:11                                                               ` Juri Linkov
2020-10-31 23:27                                                                 ` Glenn Morris
2020-11-01  7:58                                                                   ` Juri Linkov
2020-11-01 15:13                                                                     ` Eli Zaretskii
2020-11-01 18:39                                                                       ` Juri Linkov
2020-11-01 18:51                                                                         ` Eli Zaretskii
2020-11-01 19:13                                                                           ` Juri Linkov
2020-11-01 19:41                                                                             ` Eli Zaretskii
2020-11-01 20:16                                                                               ` Juri Linkov
2020-11-01 12:03                                       ` Mattias Engdegård
2020-11-01 18:35                                         ` Juri Linkov
2020-11-01 20:52                                           ` Mattias Engdegård
2020-11-02 21:36                                             ` Juri Linkov
2020-11-02 23:03                                               ` Mattias Engdegård
2020-11-03  8:30                                                 ` Juri Linkov
2020-11-03 15:24                                                 ` Eli Zaretskii
2020-11-03 18:47                                                   ` Mattias Engdegård
2020-11-03 19:36                                                     ` Eli Zaretskii
2020-11-04 11:03                                                       ` Mattias Engdegård
2020-11-04 15:38                                                         ` Eli Zaretskii
2020-11-04 16:46                                                           ` Mattias Engdegård
2020-11-04 16:58                                                             ` Mattias Engdegård
2020-11-06 13:02                                                               ` Mattias Engdegård
2022-04-30 12:19                                     ` bug#43866: 26.3; italian postfix additions Lars Ingebrigtsen
2022-04-30 12:29                                       ` Eli Zaretskii
2022-04-30 14:49                                         ` Lars Ingebrigtsen
2022-04-30 15:26                                           ` Eli Zaretskii
2022-04-30 18:49                                             ` Lars Ingebrigtsen
2022-05-29 13:35                                               ` Lars Ingebrigtsen
2020-10-15  3:52         ` Richard Stallman
2020-10-14  4:38       ` Richard Stallman
2020-10-14  8:11         ` Juri Linkov
2020-10-14 10:43         ` Robert Pluim
2020-10-15  3:54           ` Richard Stallman
2020-10-14 14:56         ` Eli Zaretskii
2020-10-08 15:23 ` Mattias Engdegård
2020-10-08 15:35   ` Robert Pluim
2020-10-08 16:22     ` Francesco Potortì
2020-10-08 15:42   ` Eli Zaretskii
2020-10-08 16:10   ` Francesco Potortì
2020-10-08 17:18     ` Robert Pluim
2020-10-08 17:28       ` Francesco Potortì
2020-10-08 17:59       ` Mattias Engdegård
2020-10-08 19:55         ` Francesco Potortì
2020-10-09  4:42         ` Lars Ingebrigtsen
2020-10-09 11:26           ` Mattias Engdegård
2020-10-09 11:53             ` Thien-Thi Nguyen
2020-10-09 12:45               ` Robert Pluim
2020-10-09 14:31                 ` Eli Zaretskii
2020-10-09 14:48                   ` Robert Pluim
2020-10-09 15:04                     ` Eli Zaretskii
2020-10-10 20:54                       ` Lars Ingebrigtsen
2020-10-12  9:26                         ` Robert Pluim
2020-10-09 15:05                   ` Mattias Engdegård
2020-10-09 15:08                     ` Robert Pluim
2020-10-09 15:28                       ` Mattias Engdegård
2020-10-09 15:10                     ` Eli Zaretskii
2020-10-09 15:21                       ` Robert Pluim

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=87imaury51.fsf@mail.linkov.net \
    --to=juri@linkov.net \
    --cc=44155@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=schwab@linux-m68k.org \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).