Subject: bug#44155: Print integers as characters

From: Juri Linkov <[email-masked]>
Date: Sun, 01 Nov 2020 20:35:33 +0200

> reopen 44155
> stop
>
> I don't mind the basic idea, but I'm reopening the bug since it looks
> like there is some unfinished business.  Hope you don't mind.

Thanks for bringing a fresh perspective to this feature request.

>> When t, print characters from integers that represent a character.
>
> In what way does 't' suggest a character? Wouldn't something like 'character' be more suggestive?
> The variable isn't named 'print-integers-as-chars'.

As the most frequent usage pattern, 't' is more convenient to use in code:

  (let ((integer-output-format t))

whereas this would be uglier and harder to type with:

  (let ((integer-output-format 'character))

>> When a number 16, print non-negative integers in the hexadecimal format.
>
> Doesn't work for bignums:
>
> (let ((integer-output-format 16))
>   (print 394583945873948753948539845))
>
> 394583945873948753948539845

Yes, this is known current limitation.

> This must be a bug since there is no reason why bignums should be treated specially.
> In general we try hard not to.

I agree, support for big numbers should be added as well.

> Since there is a read syntax for binary and octal numbers as well, why not permit 2 and 8?
> (And why not print negative numbers in the selected radix?)

2 and 8 could be added as well.

> And C0/C1 controls aren't printed well:
>
> (let ((integer-output-format t))
>   (print 10)
>   (print 127))
>
> ?
>
>
> ?^?
>
> I strongly suggest that the controls that have special escapes, like
> \n, use them.

prin1-char uses more readable format, is this better?

(prin1-char 10) ?\C-j
(prin1-char 127) ?\C-?

Or should 10 be printed as '?\n'?

> What to use for the rest depends on the user's preference really --
> for example, 31 might be printed as 31, ?\037, #o37 or #x1f.

Maybe more user choices should be supported by the variable?

> Whether to print 32 as ?‹SPACE› or ?\s is a matter of taste.

?\s is less error-prone.

> For that matter, the variable name should perhaps start with 'print-'
> like other variables that control printing.  Maybe we should separate
> the default radix and print integers as characters?  Thus, we'd have:

The variable name was modeled after the similar variable float-output-format.

> print-integer-radix -- 2, 8, 16, 10 or nil (which means 10)
>
> print-integers-as-characters -- nil or t

What should be printed when both variables are bound to non-default values,
e.g. print-integers-as-characters to t, and print-integer-radix to 16?
Maybe to print with character syntax and the given radix, e.g. '?\x1f'.