Skip to content

Commit

Permalink
New variable integer-output-format to print integers as characters (b…
Browse files Browse the repository at this point in the history
…ug#44155)

* doc/lispref/streams.texi (Output Variables): Add integer-output-format.

* src/print.c (print_object): In case of Lisp_Int, print integers
as characters when Vinteger_output_format is Qt, and in hex format
when Vinteger_output_format is 16.
(Vinteger_output_format): New variable.

* test/src/print-tests.el (print-integer-output-format): New test.
  • Loading branch information
link0ff committed Oct 31, 2020
1 parent c307c96 commit 5d9e456
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
8 changes: 8 additions & 0 deletions doc/lispref/streams.texi
Original file line number Diff line number Diff line change
Expand Up @@ -902,3 +902,11 @@ variable. The string should hold a @samp{%}-specification to be used
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.
@end defvar
6 changes: 6 additions & 0 deletions etc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,12 @@ ledit.el, lmenu.el, lucid.el and old-whitespace.el.

* 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
Expand Down
34 changes: 32 additions & 2 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
{
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 && !NILP (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;

Expand Down Expand Up @@ -2247,6 +2270,13 @@ A value of nil means to use the shortest notation
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. */);
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'. */);
Expand Down
20 changes: 20 additions & 0 deletions test/src/print-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -383,5 +383,25 @@ otherwise, use a different charset."
(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

0 comments on commit 5d9e456

Please sign in to comment.