]> code.delx.au - gnu-emacs/commitdiff
New q flag for ‘format’
authorPaul Eggert <eggert@Penguin.CS.UCLA.EDU>
Wed, 19 Aug 2015 06:04:58 +0000 (23:04 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Wed, 19 Aug 2015 06:14:08 +0000 (23:14 -0700)
* 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
doc/lispref/strings.texi
etc/NEWS
src/editfns.c

index 2bc6a1843c488234bbbf85ab9393f7fa344257a2..98b3dfb9e3a22715903f24dc761855d96f9659f8 100644 (file)
@@ -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: #<process shell> had the event `killed'
+     @print{} Process: #<process shell> had the event ‘killed’
      @result{} #<process shell>
 @end group
 @end smallexample
index 30933387b20c25aa823c302a824ec884810aba6a..8de1473b83d80099ed2d3da9e69e22f6296eed97 100644 (file)
@@ -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"))
index ec3d25c4870de8cf0fa431a61dad3924539f93b9..6058f22146430ad9c66ad57da3eaab9364e3e213 100644 (file)
--- 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
index ed57d8aee091a4d7f0d4bee2de424decb95565b1..0e1b0c8f01d8b83e5a4768318163a43bdf9325ac 100644 (file)
@@ -3822,7 +3822,7 @@ specifiers, as follows:
 
   %<flags><width><precision>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]))