diff options
| author | Juri Linkov | 2020-10-31 22:11:02 +0200 |
|---|---|---|
| committer | Juri Linkov | 2020-10-31 22:11:02 +0200 |
| commit | 5d9e456c3ed3dcefe6bf48e24a1a8f275fc887cb (patch) | |
| tree | fc7e141fbde3a524969741ecbc91614c67550503 /src | |
| parent | c307c9648d541338814fe541389ea8c7a1cf50a5 (diff) | |
| download | emacs-5d9e456c3ed3dcefe6bf48e24a1a8f275fc887cb.tar.gz emacs-5d9e456c3ed3dcefe6bf48e24a1a8f275fc887cb.zip | |
New variable integer-output-format to print integers as characters (bug#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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/print.c | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/print.c b/src/print.c index 53aa353769b..fa65a3cb268 100644 --- a/src/print.c +++ b/src/print.c | |||
| @@ -1908,8 +1908,31 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag) | |||
| 1908 | { | 1908 | { |
| 1909 | case_Lisp_Int: | 1909 | case_Lisp_Int: |
| 1910 | { | 1910 | { |
| 1911 | int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); | 1911 | int c; |
| 1912 | strout (buf, len, len, printcharfun); | 1912 | intmax_t i; |
| 1913 | |||
| 1914 | if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj) | ||
| 1915 | && (c = XFIXNUM (obj))) | ||
| 1916 | { | ||
| 1917 | printchar ('?', printcharfun); | ||
| 1918 | if (escapeflag | ||
| 1919 | && (c == ';' || c == '(' || c == ')' || c == '{' || c == '}' | ||
| 1920 | || c == '[' || c == ']' || c == '\"' || c == '\'' || c == '\\')) | ||
| 1921 | printchar ('\\', printcharfun); | ||
| 1922 | printchar (c, printcharfun); | ||
| 1923 | } | ||
| 1924 | else if (INTEGERP (Vinteger_output_format) | ||
| 1925 | && integer_to_intmax (Vinteger_output_format, &i) | ||
| 1926 | && i == 16 && !NILP (Fnatnump (obj))) | ||
| 1927 | { | ||
| 1928 | int len = sprintf (buf, "#x%"pI"x", (EMACS_UINT) XFIXNUM (obj)); | ||
| 1929 | strout (buf, len, len, printcharfun); | ||
| 1930 | } | ||
| 1931 | else | ||
| 1932 | { | ||
| 1933 | int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); | ||
| 1934 | strout (buf, len, len, printcharfun); | ||
| 1935 | } | ||
| 1913 | } | 1936 | } |
| 1914 | break; | 1937 | break; |
| 1915 | 1938 | ||
| @@ -2247,6 +2270,13 @@ A value of nil means to use the shortest notation | |||
| 2247 | that represents the number without losing information. */); | 2270 | that represents the number without losing information. */); |
| 2248 | Vfloat_output_format = Qnil; | 2271 | Vfloat_output_format = Qnil; |
| 2249 | 2272 | ||
| 2273 | DEFVAR_LISP ("integer-output-format", Vinteger_output_format, | ||
| 2274 | doc: /* The format used to print integers. | ||
| 2275 | When t, print characters from integers that represent a character. | ||
| 2276 | When a number 16, print non-negative integers in the hexadecimal format. | ||
| 2277 | Otherwise, by default print integers in the decimal format. */); | ||
| 2278 | Vinteger_output_format = Qnil; | ||
| 2279 | |||
| 2250 | DEFVAR_LISP ("print-length", Vprint_length, | 2280 | DEFVAR_LISP ("print-length", Vprint_length, |
| 2251 | doc: /* Maximum length of list to print before abbreviating. | 2281 | doc: /* Maximum length of list to print before abbreviating. |
| 2252 | A value of nil means no limit. See also `eval-expression-print-length'. */); | 2282 | A value of nil means no limit. See also `eval-expression-print-length'. */); |