aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorBasil L. Contovounesios2020-05-28 00:53:42 +0100
committerBasil L. Contovounesios2020-06-02 14:58:25 +0100
commitb07e3b1d97e73c5cf0cd60edf4838b555530bbf0 (patch)
tree6d0265225716fe97e25a2483e763236cce842238 /lisp
parent0260d2d2dbb2607e7310bdb518b7b6c0f58f5f98 (diff)
downloademacs-b07e3b1d97e73c5cf0cd60edf4838b555530bbf0.tar.gz
emacs-b07e3b1d97e73c5cf0cd60edf4838b555530bbf0.zip
Improve format-spec documentation (bug#41571)
* doc/lispref/text.texi (Interpolated Strings): Move from here... * doc/lispref/strings.texi (Custom Format Strings): ...to here, renaming the node and clarifying the documentation. (Formatting Strings): End node with sentence referring to the next one. * lisp/format-spec.el (format-spec): Clarify docstring.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/format-spec.el49
1 files changed, 30 insertions, 19 deletions
diff --git a/lisp/format-spec.el b/lisp/format-spec.el
index f418cea4259..9278bd74c42 100644
--- a/lisp/format-spec.el
+++ b/lisp/format-spec.el
@@ -29,35 +29,46 @@
29 29
30(defun format-spec (format specification &optional only-present) 30(defun format-spec (format specification &optional only-present)
31 "Return a string based on FORMAT and SPECIFICATION. 31 "Return a string based on FORMAT and SPECIFICATION.
32FORMAT is a string containing `format'-like specs like \"su - %u %k\", 32FORMAT is a string containing `format'-like specs like \"su - %u %k\".
33while SPECIFICATION is an alist mapping from format spec characters 33SPECIFICATION is an alist mapping format specification characters
34to values. 34to their substitutions.
35 35
36For instance: 36For instance:
37 37
38 (format-spec \"su - %u %l\" 38 (format-spec \"su - %u %l\"
39 `((?u . ,(user-login-name)) 39 \\=`((?u . ,(user-login-name))
40 (?l . \"ls\"))) 40 (?l . \"ls\")))
41 41
42Each format spec can have modifiers, where \"%<010b\" means \"if 42Each %-spec may contain optional flag and width modifiers, as
43the expansion is shorter than ten characters, zero-pad it, and if 43follows:
44it's longer, chop off characters from the left side\".
45 44
46The following modifiers are allowed: 45 %<flags><width>character
47 46
48* 0: Use zero-padding. 47The following flags are allowed:
49* -: Pad to the right.
50* ^: Upper-case the expansion.
51* _: Lower-case the expansion.
52* <: Limit the length by removing chars from the left.
53* >: Limit the length by removing chars from the right.
54 48
55Any text properties on a %-spec itself are propagated to the text 49* 0: Pad to the width, if given, with zeros instead of spaces.
56that it generates. 50* -: Pad to the width, if given, on the right instead of the left.
51* <: Truncate to the width, if given, on the left.
52* >: Truncate to the width, if given, on the right.
53* ^: Convert to upper case.
54* _: Convert to lower case.
57 55
58If ONLY-PRESENT, format spec characters not present in 56The width modifier behaves like the corresponding one in `format'
59SPECIFICATION are ignored, and the \"%\" characters are left 57when applied to %s.
60where they are, including \"%%\" strings." 58
59For example, \"%<010b\" means \"substitute into the output the
60value associated with ?b in SPECIFICATION, either padding it with
61leading zeros or truncating leading characters until it's ten
62characters wide\".
63
64Any text properties of FORMAT are copied to the result, with any
65text properties of a %-spec itself copied to its substitution.
66
67ONLY-PRESENT indicates how to handle %-spec characters not
68present in SPECIFICATION. If it is nil or omitted, emit an
69error; otherwise leave those %-specs and any occurrences of
70\"%%\" in FORMAT verbatim in the result, including their text
71properties, if any."
61 (with-temp-buffer 72 (with-temp-buffer
62 (insert format) 73 (insert format)
63 (goto-char (point-min)) 74 (goto-char (point-min))