diff options
| author | Basil L. Contovounesios | 2020-05-29 19:56:14 +0100 |
|---|---|---|
| committer | Basil L. Contovounesios | 2020-06-18 12:46:21 +0100 |
| commit | 0185d76e7426eb1b58a9b60b0d18e763ddf57dea (patch) | |
| tree | 68e6f560b1751902e98a241a3bf732b9fd596364 /doc | |
| parent | 97d1f672ac1529ac07a999405f630cb19a1010eb (diff) | |
| download | emacs-0185d76e7426eb1b58a9b60b0d18e763ddf57dea.tar.gz emacs-0185d76e7426eb1b58a9b60b0d18e763ddf57dea.zip | |
Fix and extend format-spec (bug#41758)
* lisp/format-spec.el: Use lexical-binding. Remove dependence on
subr-x.el.
(format-spec-make): Clarify docstring.
(format-spec--parse-modifiers): Rename to...
(format-spec--parse-flags): ...this and simplify. In particular,
don't bother parsing :space-pad which is redundant and unused.
(format-spec--pad): Remove, replacing with...
(format-spec--do-flags): ...this new helper function which performs
more of format-spec's supported text manipulation.
(format-spec): Autoload. Allow optional argument to take on special
values 'ignore' and 'delete' for more control over what happens when
a replacement for a format specification isn't provided. Bring back
proper support for a precision modifier similar to that of 'format'.
* lisp/battery.el (battery-format): Rewrite in terms of format-spec.
(battery-echo-area-format, battery-mode-line-format): Mention
support of format-spec syntax in docstrings.
* doc/lispref/strings.texi (Custom Format Strings):
* etc/NEWS: Document and announce these changes.
* lisp/dired-aux.el (dired-do-compress-to):
* lisp/erc/erc-match.el (erc-log-matches):
* lisp/erc/erc.el (erc-update-mode-line-buffer):
* lisp/gnus/gnus-sieve.el (gnus-sieve-update):
* lisp/gnus/gssapi.el (open-gssapi-stream):
* lisp/gnus/mail-source.el (mail-source-fetch-file)
(mail-source-fetch-directory, mail-source-fetch-pop)
(mail-source-fetch-imap):
* lisp/gnus/message.el (message-insert-formatted-citation-line):
* lisp/image-dired.el:
* lisp/net/eww.el:
* lisp/net/imap.el (imap-kerberos4-open, imap-gssapi-open)
(imap-shell-open):
* lisp/net/network-stream.el (network-stream-open-shell):
* lisp/obsolete/tls.el (open-tls-stream):
* lisp/textmodes/tex-mode.el:
Remove extraneous loads and autoloads of format-spec now that it is
autoloaded and simplify its uses where possible.
* test/lisp/battery-tests.el (battery-format): Test new format-spec
support.
* test/lisp/format-spec-tests.el (test-format-spec): Rename to...
(format-spec) ...this, extending test cases.
(test-format-unknown): Rename to...
(format-spec-unknown): ...this, extending test cases.
(test-format-modifiers): Rename to...
(format-spec-flags): ...this.
(format-spec-make, format-spec-parse-flags, format-spec-do-flags)
(format-spec-do-flags-truncate, format-spec-do-flags-pad)
(format-spec-do-flags-chop, format-spec-do-flags-case): New tests.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/strings.texi | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 4a7bda57c4e..2ef88b90254 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -1152,7 +1152,7 @@ The function @code{format-spec} described in this section performs a | |||
| 1152 | similar function to @code{format}, except it operates on format | 1152 | similar function to @code{format}, except it operates on format |
| 1153 | control strings that use arbitrary specification characters. | 1153 | control strings that use arbitrary specification characters. |
| 1154 | 1154 | ||
| 1155 | @defun format-spec template spec-alist &optional only-present | 1155 | @defun format-spec template spec-alist &optional ignore-missing |
| 1156 | This function returns a string produced from the format string | 1156 | This function returns a string produced from the format string |
| 1157 | @var{template} according to conversions specified in @var{spec-alist}, | 1157 | @var{template} according to conversions specified in @var{spec-alist}, |
| 1158 | which is an alist (@pxref{Association Lists}) of the form | 1158 | which is an alist (@pxref{Association Lists}) of the form |
| @@ -1185,12 +1185,15 @@ The order of specifications in @var{template} need not correspond to | |||
| 1185 | the order of associations in @var{spec-alist}. | 1185 | the order of associations in @var{spec-alist}. |
| 1186 | @end itemize | 1186 | @end itemize |
| 1187 | 1187 | ||
| 1188 | The optional argument @var{only-present} indicates how to handle | 1188 | The optional argument @var{ignore-missing} indicates how to handle |
| 1189 | specification characters in @var{template} that are not found in | 1189 | specification characters in @var{template} that are not found in |
| 1190 | @var{spec-alist}. If it is @code{nil} or omitted, the function | 1190 | @var{spec-alist}. If it is @code{nil} or omitted, the function |
| 1191 | signals an error. Otherwise, those format specifications and any | 1191 | signals an error; if it is @code{ignore}, those format specifications |
| 1192 | occurrences of @samp{%%} in @var{template} are left verbatim in the | 1192 | are left verbatim in the output, including their text properties, if |
| 1193 | output, including their text properties, if any. | 1193 | any; if it is @code{delete}, those format specifications are removed |
| 1194 | from the output; any other non-@code{nil} value is handled like | ||
| 1195 | @code{ignore}, but any occurrences of @samp{%%} are also left verbatim | ||
| 1196 | in the output. | ||
| 1194 | @end defun | 1197 | @end defun |
| 1195 | 1198 | ||
| 1196 | The syntax of format specifications accepted by @code{format-spec} is | 1199 | The syntax of format specifications accepted by @code{format-spec} is |
| @@ -1238,7 +1241,7 @@ the right rather than the left. | |||
| 1238 | 1241 | ||
| 1239 | @item < | 1242 | @item < |
| 1240 | This flag causes the substitution to be truncated on the left to the | 1243 | This flag causes the substitution to be truncated on the left to the |
| 1241 | given width, if specified. | 1244 | given width and precision, if specified. |
| 1242 | 1245 | ||
| 1243 | @item > | 1246 | @item > |
| 1244 | This flag causes the substitution to be truncated on the right to the | 1247 | This flag causes the substitution to be truncated on the right to the |
| @@ -1257,9 +1260,12 @@ The result of using contradictory flags (for instance, both upper and | |||
| 1257 | lower case) is undefined. | 1260 | lower case) is undefined. |
| 1258 | 1261 | ||
| 1259 | As is the case with @code{format}, a format specification can include | 1262 | As is the case with @code{format}, a format specification can include |
| 1260 | a width, which is a decimal number that appears after any flags. If a | 1263 | a width, which is a decimal number that appears after any flags, and a |
| 1261 | substitution contains fewer characters than its specified width, it is | 1264 | precision, which is a decimal-point @samp{.} followed by a decimal |
| 1262 | padded on the left: | 1265 | number that appears after any flags and width. |
| 1266 | |||
| 1267 | If a substitution contains fewer characters than its specified width, | ||
| 1268 | it is padded on the left: | ||
| 1263 | 1269 | ||
| 1264 | @example | 1270 | @example |
| 1265 | @group | 1271 | @group |
| @@ -1269,6 +1275,17 @@ padded on the left: | |||
| 1269 | @end group | 1275 | @end group |
| 1270 | @end example | 1276 | @end example |
| 1271 | 1277 | ||
| 1278 | If a substitution contains more characters than its specified | ||
| 1279 | precision, it is truncated on the right: | ||
| 1280 | |||
| 1281 | @example | ||
| 1282 | @group | ||
| 1283 | (format-spec "%.2a is truncated on the right" | ||
| 1284 | '((?a . "alpha"))) | ||
| 1285 | @result{} "al is truncated on the right" | ||
| 1286 | @end group | ||
| 1287 | @end example | ||
| 1288 | |||
| 1272 | Here is a more complicated example that combines several | 1289 | Here is a more complicated example that combines several |
| 1273 | aforementioned features: | 1290 | aforementioned features: |
| 1274 | 1291 | ||