aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/strings.texi
diff options
context:
space:
mode:
authorGlenn Morris2020-06-07 09:03:59 -0700
committerGlenn Morris2020-06-07 09:03:59 -0700
commitfa6d56529f9ebaedaf62ecbf8887ffecca7dce12 (patch)
tree922e12ffbf813534647a96788b9a5fb77b4f2288 /doc/lispref/strings.texi
parent6cb557e7a58d7f1c2e9afceeba45d0ced2aff214 (diff)
parent35661ef943d261f6c43e93e475ddd8516f4a4b62 (diff)
downloademacs-fa6d56529f9ebaedaf62ecbf8887ffecca7dce12.tar.gz
emacs-fa6d56529f9ebaedaf62ecbf8887ffecca7dce12.zip
Merge from origin/emacs-27
35661ef943 (origin/emacs-27) Fix typo in "(elisp) Type Keywords" 1af0e95fec Gnus nnir-summary-line-format has no effect dd366b5d3b Improve documentation of 'window-text-pixel-size' fbd49f969e * src/xdisp.c (Fwindow_text_pixel_size): Doc fix. (Bug#41... d8593fd19f Minor improvements to EDE and EIEIO manuals 3916e63f9e Have Fido mode also imitate Ido mode in ignore-case options cc35b197c7 Update package-menu-quick-help bf09106256 Improve documentation of 'sort-subr' 73749efa13 Update Ukrainian transliteration 30a7ee505a Fix Arabic shaping when eww/shr fill the text to be rendered 7d323f07c0 Silence some byte-compiler warnings in tests cf473e742f * test/lisp/battery-tests.el: New file. b07e3b1d97 Improve format-spec documentation (bug#41571) # Conflicts: # test/lisp/emacs-lisp/package-tests.el
Diffstat (limited to 'doc/lispref/strings.texi')
-rw-r--r--doc/lispref/strings.texi176
1 files changed, 176 insertions, 0 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi
index 70c3b3cf4be..4a7bda57c4e 100644
--- a/doc/lispref/strings.texi
+++ b/doc/lispref/strings.texi
@@ -28,6 +28,7 @@ keyboard character events.
28* Text Comparison:: Comparing characters or strings. 28* Text Comparison:: Comparing characters or strings.
29* String Conversion:: Converting to and from characters and strings. 29* String Conversion:: Converting to and from characters and strings.
30* Formatting Strings:: @code{format}: Emacs's analogue of @code{printf}. 30* Formatting Strings:: @code{format}: Emacs's analogue of @code{printf}.
31* Custom Format Strings:: Formatting custom @code{format} specifications.
31* Case Conversion:: Case conversion functions. 32* Case Conversion:: Case conversion functions.
32* Case Tables:: Customizing case conversion. 33* Case Tables:: Customizing case conversion.
33@end menu 34@end menu
@@ -1122,6 +1123,181 @@ may be problematic; for example, @samp{%d} and @samp{%g} can mishandle
1122NaNs and can lose precision and type, and @samp{#x%x} and @samp{#o%o} 1123NaNs and can lose precision and type, and @samp{#x%x} and @samp{#o%o}
1123can mishandle negative integers. @xref{Input Functions}. 1124can mishandle negative integers. @xref{Input Functions}.
1124 1125
1126The functions described in this section accept a fixed set of
1127specification characters. The next section describes a function
1128@code{format-spec} which can accept custom specification characters,
1129such as @samp{%a} or @samp{%z}.
1130
1131@node Custom Format Strings
1132@section Custom Format Strings
1133@cindex custom format string
1134@cindex custom @samp{%}-sequence in format
1135
1136Sometimes it is useful to allow users and Lisp programs alike to
1137control how certain text is generated via custom format control
1138strings. For example, a format string could control how to display
1139someone's forename, surname, and email address. Using the function
1140@code{format} described in the previous section, the format string
1141could be something like @w{@code{"%s %s <%s>"}}. This approach
1142quickly becomes impractical, however, as it can be unclear which
1143specification character corresponds to which piece of information.
1144
1145A more convenient format string for such cases would be something like
1146@w{@code{"%f %l <%e>"}}, where each specification character carries
1147more semantic information and can easily be rearranged relative to
1148other specification characters, making such format strings more easily
1149customizable by the user.
1150
1151The function @code{format-spec} described in this section performs a
1152similar function to @code{format}, except it operates on format
1153control strings that use arbitrary specification characters.
1154
1155@defun format-spec template spec-alist &optional only-present
1156This function returns a string produced from the format string
1157@var{template} according to conversions specified in @var{spec-alist},
1158which is an alist (@pxref{Association Lists}) of the form
1159@w{@code{(@var{letter} . @var{replacement})}}. Each specification
1160@code{%@var{letter}} in @var{template} will be replaced by
1161@var{replacement} when formatting the resulting string.
1162
1163The characters in @var{template}, other than the format
1164specifications, are copied directly into the output, including their
1165text properties, if any. Any text properties of the format
1166specifications are copied to their replacements.
1167
1168Using an alist to specify conversions gives rise to some useful
1169properties:
1170
1171@itemize @bullet
1172@item
1173If @var{spec-alist} contains more unique @var{letter} keys than there
1174are unique specification characters in @var{template}, the unused keys
1175are simply ignored.
1176@item
1177If @var{spec-alist} contains more than one association with the same
1178@var{letter}, the closest one to the start of the list is used.
1179@item
1180If @var{template} contains the same specification character more than
1181once, then the same @var{replacement} found in @var{spec-alist} is
1182used as a basis for all of that character's substitutions.
1183@item
1184The order of specifications in @var{template} need not correspond to
1185the order of associations in @var{spec-alist}.
1186@end itemize
1187
1188The optional argument @var{only-present} indicates how to handle
1189specification characters in @var{template} that are not found in
1190@var{spec-alist}. If it is @code{nil} or omitted, the function
1191signals an error. Otherwise, those format specifications and any
1192occurrences of @samp{%%} in @var{template} are left verbatim in the
1193output, including their text properties, if any.
1194@end defun
1195
1196The syntax of format specifications accepted by @code{format-spec} is
1197similar, but not identical, to that accepted by @code{format}. In
1198both cases, a format specification is a sequence of characters
1199beginning with @samp{%} and ending with an alphabetic letter such as
1200@samp{s}.
1201
1202Unlike @code{format}, which assigns specific meanings to a fixed set
1203of specification characters, @code{format-spec} accepts arbitrary
1204specification characters and treats them all equally. For example:
1205
1206@example
1207@group
1208(setq my-site-info
1209 (list (cons ?s system-name)
1210 (cons ?t (symbol-name system-type))
1211 (cons ?c system-configuration)
1212 (cons ?v emacs-version)
1213 (cons ?e invocation-name)
1214 (cons ?p (number-to-string (emacs-pid)))
1215 (cons ?a user-mail-address)
1216 (cons ?n user-full-name)))
1217
1218(format-spec "%e %v (%c)" my-site-info)
1219 @result{} "emacs 27.1 (x86_64-pc-linux-gnu)"
1220
1221(format-spec "%n <%a>" my-site-info)
1222 @result{} "Emacs Developers <emacs-devel@@gnu.org>"
1223@end group
1224@end example
1225
1226A format specification can include any number of the following flag
1227characters immediately after the @samp{%} to modify aspects of the
1228substitution.
1229
1230@table @samp
1231@item 0
1232This flag causes any padding specified by the width to consist of
1233@samp{0} characters instead of spaces.
1234
1235@item -
1236This flag causes any padding specified by the width to be inserted on
1237the right rather than the left.
1238
1239@item <
1240This flag causes the substitution to be truncated on the left to the
1241given width, if specified.
1242
1243@item >
1244This flag causes the substitution to be truncated on the right to the
1245given width, if specified.
1246
1247@item ^
1248This flag converts the substituted text to upper case (@pxref{Case
1249Conversion}).
1250
1251@item _
1252This flag converts the substituted text to lower case (@pxref{Case
1253Conversion}).
1254@end table
1255
1256The result of using contradictory flags (for instance, both upper and
1257lower case) is undefined.
1258
1259As 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
1261substitution contains fewer characters than its specified width, it is
1262padded on the left:
1263
1264@example
1265@group
1266(format-spec "%8a is padded on the left with spaces"
1267 '((?a . "alpha")))
1268 @result{} " alpha is padded on the left with spaces"
1269@end group
1270@end example
1271
1272Here is a more complicated example that combines several
1273aforementioned features:
1274
1275@example
1276@group
1277(setq my-battery-info
1278 (list (cons ?p "73") ; Percentage
1279 (cons ?L "Battery") ; Status
1280 (cons ?t "2:23") ; Remaining time
1281 (cons ?c "24330") ; Capacity
1282 (cons ?r "10.6"))) ; Rate of discharge
1283
1284(format-spec "%>^-3L : %3p%% (%05t left)" my-battery-info)
1285 @result{} "BAT : 73% (02:23 left)"
1286
1287(format-spec "%>^-3L : %3p%% (%05t left)"
1288 (cons (cons ?L "AC")
1289 my-battery-info))
1290 @result{} "AC : 73% (02:23 left)"
1291@end group
1292@end example
1293
1294As the examples in this section illustrate, @code{format-spec} is
1295often used for selectively formatting an assortment of different
1296pieces of information. This is useful in programs that provide
1297user-customizable format strings, as the user can choose to format
1298with a regular syntax and in any desired order only a subset of the
1299information that the program makes available.
1300
1125@node Case Conversion 1301@node Case Conversion
1126@section Case Conversion in Lisp 1302@section Case Conversion in Lisp
1127@cindex upper case 1303@cindex upper case