aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias EngdegÄrd2020-11-02 23:37:16 +0100
committerMattias EngdegÄrd2020-11-06 13:55:32 +0100
commit0c5eb1c7e798fdf16c3f2694285fe0d18367c6ea (patch)
tree5fc075ff856b1c770d48dc34f836dd89de700793 /src
parent527413fb2ff8c073d89ee2d22d38a67c74678b27 (diff)
downloademacs-0c5eb1c7e798fdf16c3f2694285fe0d18367c6ea.tar.gz
emacs-0c5eb1c7e798fdf16c3f2694285fe0d18367c6ea.zip
Reduce integer-output-format to print-integers-as-characters
The variable now only controls whether characters are printed, not the radix. Control chars are printed in human-readable syntax only when special escapes such as ?\n are available. Spaces, formatting and combining chars are excluded (bug#44155). Done in collaboration with Juri Linkov. * src/character.c (graphic_base_p): * src/print.c (named_escape): New functions. (print_object): Change semantics as described above. (syms_of_print): Rename integer-output-format. Update doc string. * doc/lispref/streams.texi (Output Variables): * etc/NEWS: * test/src/print-tests.el (print-integers-as-characters): Rename and update according to new semantics. The test now passes.
Diffstat (limited to 'src')
-rw-r--r--src/character.c21
-rw-r--r--src/character.h1
-rw-r--r--src/print.c64
3 files changed, 64 insertions, 22 deletions
diff --git a/src/character.c b/src/character.c
index 5860f6a0c8c..00b73293a3f 100644
--- a/src/character.c
+++ b/src/character.c
@@ -982,6 +982,27 @@ printablep (int c)
982 || gen_cat == UNICODE_CATEGORY_Cn)); /* unassigned */ 982 || gen_cat == UNICODE_CATEGORY_Cn)); /* unassigned */
983} 983}
984 984
985/* Return true if C is graphic character that can be printed independently. */
986bool
987graphic_base_p (int c)
988{
989 Lisp_Object category = CHAR_TABLE_REF (Vunicode_category_table, c);
990 if (! FIXNUMP (category))
991 return false;
992 EMACS_INT gen_cat = XFIXNUM (category);
993
994 return (!(gen_cat == UNICODE_CATEGORY_Mn /* mark, nonspacing */
995 || gen_cat == UNICODE_CATEGORY_Mc /* mark, combining */
996 || gen_cat == UNICODE_CATEGORY_Me /* mark, enclosing */
997 || gen_cat == UNICODE_CATEGORY_Zs /* separator, space */
998 || gen_cat == UNICODE_CATEGORY_Zl /* separator, line */
999 || gen_cat == UNICODE_CATEGORY_Zp /* separator, paragraph */
1000 || gen_cat == UNICODE_CATEGORY_Cc /* other, control */
1001 || gen_cat == UNICODE_CATEGORY_Cs /* other, surrogate */
1002 || gen_cat == UNICODE_CATEGORY_Cf /* other, format */
1003 || gen_cat == UNICODE_CATEGORY_Cn)); /* other, unassigned */
1004}
1005
985/* Return true if C is a horizontal whitespace character, as defined 1006/* Return true if C is a horizontal whitespace character, as defined
986 by https://www.unicode.org/reports/tr18/tr18-19.html#blank. */ 1007 by https://www.unicode.org/reports/tr18/tr18-19.html#blank. */
987bool 1008bool
diff --git a/src/character.h b/src/character.h
index af5023f77cc..cbf43097ae2 100644
--- a/src/character.h
+++ b/src/character.h
@@ -583,6 +583,7 @@ extern bool alphanumericp (int);
583extern bool graphicp (int); 583extern bool graphicp (int);
584extern bool printablep (int); 584extern bool printablep (int);
585extern bool blankp (int); 585extern bool blankp (int);
586extern bool graphic_base_p (int);
586 587
587/* Look up the element in char table OBJ at index CH, and return it as 588/* Look up the element in char table OBJ at index CH, and return it as
588 an integer. If the element is not a character, return CH itself. */ 589 an integer. If the element is not a character, return CH itself. */
diff --git a/src/print.c b/src/print.c
index fa65a3cb268..9ff331fb8e1 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1848,6 +1848,24 @@ print_vectorlike (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag,
1848 return true; 1848 return true;
1849} 1849}
1850 1850
1851static char
1852named_escape (int i)
1853{
1854 switch (i)
1855 {
1856 case '\b': return 'b';
1857 case '\t': return 't';
1858 case '\n': return 'n';
1859 case '\f': return 'f';
1860 case '\r': return 'r';
1861 case ' ': return 's';
1862 /* \a, \v, \e and \d are excluded from printing as escapes since
1863 they are somewhat rare as characters and more likely to be
1864 plain integers. */
1865 }
1866 return 0;
1867}
1868
1851static void 1869static void
1852print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag) 1870print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1853{ 1871{
@@ -1908,29 +1926,30 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag)
1908 { 1926 {
1909 case_Lisp_Int: 1927 case_Lisp_Int:
1910 { 1928 {
1911 int c; 1929 EMACS_INT i = XFIXNUM (obj);
1912 intmax_t i; 1930 char escaped_name;
1913 1931
1914 if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj) 1932 if (print_integers_as_characters && i >= 0 && i <= MAX_UNICODE_CHAR
1915 && (c = XFIXNUM (obj))) 1933 && ((escaped_name = named_escape (i))
1934 || graphic_base_p (i)))
1916 { 1935 {
1917 printchar ('?', printcharfun); 1936 printchar ('?', printcharfun);
1918 if (escapeflag 1937 if (escaped_name)
1919 && (c == ';' || c == '(' || c == ')' || c == '{' || c == '}' 1938 {
1920 || c == '[' || c == ']' || c == '\"' || c == '\'' || c == '\\')) 1939 printchar ('\\', printcharfun);
1940 i = escaped_name;
1941 }
1942 else if (escapeflag
1943 && (i == ';' || i == '\"' || i == '\'' || i == '\\'
1944 || i == '(' || i == ')'
1945 || i == '{' || i == '}'
1946 || i == '[' || i == ']'))
1921 printchar ('\\', printcharfun); 1947 printchar ('\\', printcharfun);
1922 printchar (c, printcharfun); 1948 printchar (i, 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 } 1949 }
1931 else 1950 else
1932 { 1951 {
1933 int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); 1952 int len = sprintf (buf, "%"pI"d", i);
1934 strout (buf, len, len, printcharfun); 1953 strout (buf, len, len, printcharfun);
1935 } 1954 }
1936 } 1955 }
@@ -2270,12 +2289,13 @@ A value of nil means to use the shortest notation
2270that represents the number without losing information. */); 2289that represents the number without losing information. */);
2271 Vfloat_output_format = Qnil; 2290 Vfloat_output_format = Qnil;
2272 2291
2273 DEFVAR_LISP ("integer-output-format", Vinteger_output_format, 2292 DEFVAR_BOOL ("print-integers-as-characters", print_integers_as_characters,
2274 doc: /* The format used to print integers. 2293 doc: /* Non-nil means integers are printed using characters syntax.
2275When t, print characters from integers that represent a character. 2294Only independent graphic characters, and control characters with named
2276When a number 16, print non-negative integers in the hexadecimal format. 2295escape sequences such as newline, are printed this way. Other
2277Otherwise, by default print integers in the decimal format. */); 2296integers, including those corresponding to raw bytes, are printed
2278 Vinteger_output_format = Qnil; 2297as numbers the usual way. */);
2298 print_integers_as_characters = Qnil;
2279 2299
2280 DEFVAR_LISP ("print-length", Vprint_length, 2300 DEFVAR_LISP ("print-length", Vprint_length,
2281 doc: /* Maximum length of list to print before abbreviating. 2301 doc: /* Maximum length of list to print before abbreviating.