aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2015-08-18 23:04:58 -0700
committerPaul Eggert2015-08-18 23:14:08 -0700
commit67de1b6fa752df913ae00537234d1a18bca2543f (patch)
treeabb61f8f7eed292dbcc61ebca1b912ade02dde4b /src
parent85bc107458601e305445d7ec6f5b209c01f5db0c (diff)
downloademacs-67de1b6fa752df913ae00537234d1a18bca2543f.tar.gz
emacs-67de1b6fa752df913ae00537234d1a18bca2543f.zip
New q flag for ‘format’
* doc/lispref/processes.texi (Sentinels): Don't hardwire grave quoting style in example. * doc/lispref/strings.texi (Formatting Strings): * etc/NEWS: Document new q flag. * src/editfns.c (Fformat): Implement it.
Diffstat (limited to 'src')
-rw-r--r--src/editfns.c78
1 files changed, 67 insertions, 11 deletions
diff --git a/src/editfns.c b/src/editfns.c
index ed57d8aee09..0e1b0c8f01d 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -3822,7 +3822,7 @@ specifiers, as follows:
3822 3822
3823 %<flags><width><precision>character 3823 %<flags><width><precision>character
3824 3824
3825where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+ 3825where flags is [+ #-0q]+, width is [0-9]+, and precision is .[0-9]+
3826 3826
3827The + flag character inserts a + before any positive number, while a 3827The + flag character inserts a + before any positive number, while a
3828space inserts a space before any positive number; these flags only 3828space inserts a space before any positive number; these flags only
@@ -3835,6 +3835,9 @@ The # flag means to use an alternate display form for %o, %x, %X, %e,
3835for %e, %f, and %g, it causes a decimal point to be included even if 3835for %e, %f, and %g, it causes a decimal point to be included even if
3836the precision is zero. 3836the precision is zero.
3837 3837
3838The q flag means to quote the printed representation as per
3839‘text-quoting-style’. E.g., "%qs" is equivalent to "‘%s’".
3840
3838The width specifier supplies a lower limit for the length of the 3841The width specifier supplies a lower limit for the length of the
3839printed representation. The padding, if any, normally goes on the 3842printed representation. The padding, if any, normally goes on the
3840left, but it goes on the right if the - flag is present. The padding 3843left, but it goes on the right if the - flag is present. The padding
@@ -3973,11 +3976,12 @@ usage: (format STRING &rest OBJECTS) */)
3973 digits to print after the '.' for floats, or the max. 3976 digits to print after the '.' for floats, or the max.
3974 number of chars to print from a string. */ 3977 number of chars to print from a string. */
3975 3978
3976 bool minus_flag = 0; 3979 bool minus_flag = false;
3977 bool plus_flag = 0; 3980 bool plus_flag = false;
3978 bool space_flag = 0; 3981 bool space_flag = false;
3979 bool sharp_flag = 0; 3982 bool sharp_flag = false;
3980 bool zero_flag = 0; 3983 bool zero_flag = false;
3984 bool quote_flag = false;
3981 ptrdiff_t field_width; 3985 ptrdiff_t field_width;
3982 bool precision_given; 3986 bool precision_given;
3983 uintmax_t precision = UINTMAX_MAX; 3987 uintmax_t precision = UINTMAX_MAX;
@@ -3988,11 +3992,12 @@ usage: (format STRING &rest OBJECTS) */)
3988 { 3992 {
3989 switch (*++format) 3993 switch (*++format)
3990 { 3994 {
3991 case '-': minus_flag = 1; continue; 3995 case '-': minus_flag = true; continue;
3992 case '+': plus_flag = 1; continue; 3996 case '+': plus_flag = true; continue;
3993 case ' ': space_flag = 1; continue; 3997 case ' ': space_flag = true; continue;
3994 case '#': sharp_flag = 1; continue; 3998 case '#': sharp_flag = true; continue;
3995 case '0': zero_flag = 1; continue; 3999 case '0': zero_flag = true; continue;
4000 case 'q': quote_flag = true; continue;
3996 } 4001 }
3997 break; 4002 break;
3998 } 4003 }
@@ -4121,6 +4126,20 @@ usage: (format STRING &rest OBJECTS) */)
4121 if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n])) 4126 if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n]))
4122 convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes); 4127 convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes);
4123 4128
4129 if (quote_flag)
4130 {
4131 convbytes += 2;
4132 if (quoting_style == CURVE_QUOTING_STYLE)
4133 {
4134 if (!multibyte)
4135 {
4136 multibyte = true;
4137 goto retry;
4138 }
4139 convbytes += 4;
4140 }
4141 }
4142
4124 padding = width < field_width ? field_width - width : 0; 4143 padding = width < field_width ? field_width - width : 0;
4125 4144
4126 if (max_bufsize - padding <= convbytes) 4145 if (max_bufsize - padding <= convbytes)
@@ -4128,6 +4147,27 @@ usage: (format STRING &rest OBJECTS) */)
4128 convbytes += padding; 4147 convbytes += padding;
4129 if (convbytes <= buf + bufsize - p) 4148 if (convbytes <= buf + bufsize - p)
4130 { 4149 {
4150
4151 if (quote_flag)
4152 {
4153 switch (quoting_style)
4154 {
4155 case CURVE_QUOTING_STYLE:
4156 memcpy (p, uLSQM, 3);
4157 p += 3;
4158 break;
4159
4160 case GRAVE_QUOTING_STYLE:
4161 *p++ = '`';
4162 break;
4163
4164 case STRAIGHT_QUOTING_STYLE:
4165 *p++ = '\'';
4166 break;
4167 }
4168 nchars++;
4169 }
4170
4131 if (! minus_flag) 4171 if (! minus_flag)
4132 { 4172 {
4133 memset (p, ' ', padding); 4173 memset (p, ' ', padding);
@@ -4157,6 +4197,22 @@ usage: (format STRING &rest OBJECTS) */)
4157 nchars += padding; 4197 nchars += padding;
4158 } 4198 }
4159 4199
4200 if (quote_flag)
4201 {
4202 switch (quoting_style)
4203 {
4204 case CURVE_QUOTING_STYLE:
4205 memcpy (p, uRSQM, 3);
4206 p += 3;
4207 break;
4208
4209 default:
4210 *p++ = '\'';
4211 break;
4212 }
4213 nchars++;
4214 }
4215
4160 /* If this argument has text properties, record where 4216 /* If this argument has text properties, record where
4161 in the result string it appears. */ 4217 in the result string it appears. */
4162 if (string_intervals (args[n])) 4218 if (string_intervals (args[n]))