From 67de1b6fa752df913ae00537234d1a18bca2543f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 18 Aug 2015 23:04:58 -0700 Subject: [PATCH] =?utf8?q?New=20q=20flag=20for=20=E2=80=98format=E2=80=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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. --- doc/lispref/processes.texi | 4 +- doc/lispref/strings.texi | 9 +++-- etc/NEWS | 13 +++++-- src/editfns.c | 78 ++++++++++++++++++++++++++++++++------ 4 files changed, 84 insertions(+), 20 deletions(-) diff --git a/doc/lispref/processes.texi b/doc/lispref/processes.texi index 2bc6a1843c4..98b3dfb9e3a 100644 --- a/doc/lispref/processes.texi +++ b/doc/lispref/processes.texi @@ -1720,13 +1720,13 @@ sentinel, the eventual call to the sentinel will use the new one. @group (defun msg-me (process event) (princ - (format "Process: %s had the event `%s'" process event))) + (format "Process: %s had the event ‘%s’" process event))) (set-process-sentinel (get-process "shell") 'msg-me) @result{} msg-me @end group @group (kill-process (get-process "shell")) - @print{} Process: # had the event `killed' + @print{} Process: # had the event ‘killed’ @result{} # @end group @end smallexample diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 30933387b20..8de1473b83d 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi @@ -936,7 +936,7 @@ where curved single quotes stand for themselves: (format "The name of this buffer is ‘%s’." (buffer-name)) @result{} "The name of this buffer is ‘strings.texi’." -(format "The buffer object prints as ‘%s’." (current-buffer)) +(format "The buffer object prints as %qs." (current-buffer)) @result{} "The buffer object prints as ‘strings.texi’." (format "The octal value of %d is %o, @@ -1011,13 +1011,16 @@ specifier, if any, to be inserted on the right rather than the left. If both @samp{-} and @samp{0} are present, the @samp{0} flag is ignored. + The flag @samp{q} quotes the printed representation as per the +variable @samp{text-quoting-style} described below. + @example @group (format "%06d is padded on the left with zeros" 123) @result{} "000123 is padded on the left with zeros" -(format "%-6d is padded on the right" 123) - @result{} "123 is padded on the right" +(format "%q-6d is padded on the right" 123) + @result{} "‘123 ’ is padded on the right" (format "The word ‘%-7s’ actually has %d letters in it." "foo" (length "foo")) diff --git a/etc/NEWS b/etc/NEWS index ec3d25c4870..6058f221464 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -911,13 +911,18 @@ in many contexts where it's a no-op, ‘format’ is no longer required to create a string, and may return its first argument if the argument already has the correct value. ++++ +** New ‘format’ flag ‘q’ +The new ‘q’ flag causes ‘format’ to quote the output representation as +per the value of ‘text quoting-style’. E.g., (format "%qs failed" +"foo") might return "‘foo’ failed". + +++ ** substitute-command-keys now replaces quotes. That is, it converts documentation strings' quoting style as per the -value of ‘text-quoting-style’ as described above. Doc strings in -source code can use either curved quotes or grave accent and -apostrophe. As before, isolated apostrophes and characters preceded -by \= are output as-is. +value of ‘text-quoting-style’. Doc strings in source code can use +either curved quotes or grave accent and apostrophe. As before, +isolated apostrophes and characters preceded by \= are output as-is. +++ ** The character classes [:alpha:] and [:alnum:] in regular expressions 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: %character -where flags is [+ #-0]+, width is [0-9]+, and precision is .[0-9]+ +where flags is [+ #-0q]+, width is [0-9]+, and precision is .[0-9]+ The + flag character inserts a + before any positive number, while a space 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, for %e, %f, and %g, it causes a decimal point to be included even if the precision is zero. +The q flag means to quote the printed representation as per +‘text-quoting-style’. E.g., "%qs" is equivalent to "‘%s’". + The width specifier supplies a lower limit for the length of the printed representation. The padding, if any, normally goes on the left, but it goes on the right if the - flag is present. The padding @@ -3973,11 +3976,12 @@ usage: (format STRING &rest OBJECTS) */) digits to print after the '.' for floats, or the max. number of chars to print from a string. */ - bool minus_flag = 0; - bool plus_flag = 0; - bool space_flag = 0; - bool sharp_flag = 0; - bool zero_flag = 0; + bool minus_flag = false; + bool plus_flag = false; + bool space_flag = false; + bool sharp_flag = false; + bool zero_flag = false; + bool quote_flag = false; ptrdiff_t field_width; bool precision_given; uintmax_t precision = UINTMAX_MAX; @@ -3988,11 +3992,12 @@ usage: (format STRING &rest OBJECTS) */) { switch (*++format) { - case '-': minus_flag = 1; continue; - case '+': plus_flag = 1; continue; - case ' ': space_flag = 1; continue; - case '#': sharp_flag = 1; continue; - case '0': zero_flag = 1; continue; + case '-': minus_flag = true; continue; + case '+': plus_flag = true; continue; + case ' ': space_flag = true; continue; + case '#': sharp_flag = true; continue; + case '0': zero_flag = true; continue; + case 'q': quote_flag = true; continue; } break; } @@ -4121,6 +4126,20 @@ usage: (format STRING &rest OBJECTS) */) if (convbytes && multibyte && ! STRING_MULTIBYTE (args[n])) convbytes = count_size_as_multibyte (SDATA (args[n]), nbytes); + if (quote_flag) + { + convbytes += 2; + if (quoting_style == CURVE_QUOTING_STYLE) + { + if (!multibyte) + { + multibyte = true; + goto retry; + } + convbytes += 4; + } + } + padding = width < field_width ? field_width - width : 0; if (max_bufsize - padding <= convbytes) @@ -4128,6 +4147,27 @@ usage: (format STRING &rest OBJECTS) */) convbytes += padding; if (convbytes <= buf + bufsize - p) { + + if (quote_flag) + { + switch (quoting_style) + { + case CURVE_QUOTING_STYLE: + memcpy (p, uLSQM, 3); + p += 3; + break; + + case GRAVE_QUOTING_STYLE: + *p++ = '`'; + break; + + case STRAIGHT_QUOTING_STYLE: + *p++ = '\''; + break; + } + nchars++; + } + if (! minus_flag) { memset (p, ' ', padding); @@ -4157,6 +4197,22 @@ usage: (format STRING &rest OBJECTS) */) nchars += padding; } + if (quote_flag) + { + switch (quoting_style) + { + case CURVE_QUOTING_STYLE: + memcpy (p, uRSQM, 3); + p += 3; + break; + + default: + *p++ = '\''; + break; + } + nchars++; + } + /* If this argument has text properties, record where in the result string it appears. */ if (string_intervals (args[n])) -- 2.39.2