aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorBasil L. Contovounesios2020-05-29 19:56:14 +0100
committerBasil L. Contovounesios2020-06-18 12:46:21 +0100
commit0185d76e7426eb1b58a9b60b0d18e763ddf57dea (patch)
tree68e6f560b1751902e98a241a3bf732b9fd596364 /doc
parent97d1f672ac1529ac07a999405f630cb19a1010eb (diff)
downloademacs-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.texi35
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
1152similar function to @code{format}, except it operates on format 1152similar function to @code{format}, except it operates on format
1153control strings that use arbitrary specification characters. 1153control 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
1156This function returns a string produced from the format string 1156This 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},
1158which is an alist (@pxref{Association Lists}) of the form 1158which is an alist (@pxref{Association Lists}) of the form
@@ -1185,12 +1185,15 @@ The order of specifications in @var{template} need not correspond to
1185the order of associations in @var{spec-alist}. 1185the order of associations in @var{spec-alist}.
1186@end itemize 1186@end itemize
1187 1187
1188The optional argument @var{only-present} indicates how to handle 1188The optional argument @var{ignore-missing} indicates how to handle
1189specification characters in @var{template} that are not found in 1189specification 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
1191signals an error. Otherwise, those format specifications and any 1191signals an error; if it is @code{ignore}, those format specifications
1192occurrences of @samp{%%} in @var{template} are left verbatim in the 1192are left verbatim in the output, including their text properties, if
1193output, including their text properties, if any. 1193any; if it is @code{delete}, those format specifications are removed
1194from the output; any other non-@code{nil} value is handled like
1195@code{ignore}, but any occurrences of @samp{%%} are also left verbatim
1196in the output.
1194@end defun 1197@end defun
1195 1198
1196The syntax of format specifications accepted by @code{format-spec} is 1199The 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 <
1240This flag causes the substitution to be truncated on the left to the 1243This flag causes the substitution to be truncated on the left to the
1241given width, if specified. 1244given width and precision, if specified.
1242 1245
1243@item > 1246@item >
1244This flag causes the substitution to be truncated on the right to the 1247This 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
1257lower case) is undefined. 1260lower case) is undefined.
1258 1261
1259As is the case with @code{format}, a format specification can include 1262As is the case with @code{format}, a format specification can include
1260a width, which is a decimal number that appears after any flags. If a 1263a width, which is a decimal number that appears after any flags, and a
1261substitution contains fewer characters than its specified width, it is 1264precision, which is a decimal-point @samp{.} followed by a decimal
1262padded on the left: 1265number that appears after any flags and width.
1266
1267If a substitution contains fewer characters than its specified width,
1268it 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
1278If a substitution contains more characters than its specified
1279precision, 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
1272Here is a more complicated example that combines several 1289Here is a more complicated example that combines several
1273aforementioned features: 1290aforementioned features:
1274 1291