-/* Output like sprintf to a buffer of specified size.
+/* Output like sprintf to a buffer of specified size. -*- coding: utf-8 -*-
Also takes args differently: pass one pointer to the end
of the format string in addition to the format string itself.
- Copyright (C) 1985, 2001-2015 Free Software Foundation, Inc.
+ Copyright (C) 1985, 2001-2016 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
ignored %s and %c conversions. (See below for the detailed documentation of
what is supported.) However, this is okay, as this function is supposed to
be called from `error' and similar functions, and thus does not need to
- support features beyond those in `Fformat', which is used by `error' on the
- Lisp level. */
+ support features beyond those in `Fformat_message', which is used
+ by `error' on the Lisp level. */
-/* This function supports the following %-sequences in the `format'
- argument:
+/* In the FORMAT argument this function supports ` and ' as directives
+ that output left and right quotes as per ‘text-quoting style’. It
+ also supports the following %-sequences:
%s means print a string argument.
- %S is silently treated as %s, for loose compatibility with `Fformat'.
+ %S is treated as %s, for loose compatibility with `Fformat_message'.
%d means print a `signed int' argument in decimal.
%o means print an `unsigned int' argument in octal.
%x means print an `unsigned int' argument in hex.
/* Buffer we have got with malloc. */
char *big_buffer = NULL;
+ enum text_quoting_style quoting_style = text_quoting_style ();
ptrdiff_t tem = -1;
char *string;
char fixed_buffer[20]; /* Default buffer for small formatting. */
/* Loop until end of format string or buffer full. */
while (fmt < format_end && bufsize > 0)
{
- if (*fmt == '%') /* Check for a '%' character */
+ char const *fmt0 = fmt;
+ char fmtchar = *fmt++;
+ if (fmtchar == '%')
{
ptrdiff_t size_bound = 0;
ptrdiff_t width; /* Columns occupied by STRING on display. */
int maxmlen = max (max (1, pDlen), max (pIlen, pMlen));
int mlen;
- fmt++;
/* Copy this one %-spec into fmtcpy. */
string = fmtcpy;
*string++ = '%';
}
}
- {
- /* Just some character; Copy it if the whole multi-byte form
- fit in the buffer. */
- char *save_bufptr = bufptr;
-
- do { *bufptr++ = *fmt++; }
- while (fmt < format_end && --bufsize > 0 && !CHAR_HEAD_P (*fmt));
- if (!CHAR_HEAD_P (*fmt))
- {
- /* Truncate, but return value that will signal to caller
- that the buffer was too small. */
- *save_bufptr = 0;
- break;
- }
- }
- };
+ char const *src;
+ ptrdiff_t srclen;
+ if (quoting_style == CURVE_QUOTING_STYLE && fmtchar == '`')
+ src = uLSQM, srclen = sizeof uLSQM - 1;
+ else if (quoting_style == CURVE_QUOTING_STYLE && fmtchar == '\'')
+ src = uRSQM, srclen = sizeof uRSQM - 1;
+ else if (quoting_style == STRAIGHT_QUOTING_STYLE && fmtchar == '`')
+ src = "'", srclen = 1;
+ else
+ {
+ while (fmt < format_end && !CHAR_HEAD_P (*fmt))
+ fmt++;
+ src = fmt0, srclen = fmt - fmt0;
+ }
+
+ if (bufsize < srclen)
+ {
+ /* Truncate, but return value that will signal to caller
+ that the buffer was too small. */
+ do
+ *bufptr++ = '\0';
+ while (--bufsize != 0);
+ }
+ else
+ {
+ do
+ *bufptr++ = *src++;
+ while (--srclen != 0);
+ }
+ }
/* If we had to malloc something, free it. */
xfree (big_buffer);
/* Format to an unbounded buffer BUF. This is like sprintf, except it
is not limited to returning an 'int' so it doesn't have a silly 2
GiB limit on typical 64-bit hosts. However, it is limited to the
- Emacs-style formats that doprnt supports.
+ Emacs-style formats that doprnt supports, and it requotes ` and '
+ as per ‘text-quoting-style’.
Return the number of bytes put into BUF, excluding the terminating
'\0'. */
return nbytes;
}
-#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
+#if HAVE_MODULES || (defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT)
/* Format to buffer *BUF of positive size *BUFSIZE, reallocating *BUF
and updating *BUFSIZE if the buffer is too small, and otherwise