diff options
| author | Basil L. Contovounesios | 2020-05-28 00:53:42 +0100 |
|---|---|---|
| committer | Basil L. Contovounesios | 2020-06-02 14:58:25 +0100 |
| commit | b07e3b1d97e73c5cf0cd60edf4838b555530bbf0 (patch) | |
| tree | 6d0265225716fe97e25a2483e763236cce842238 /lisp | |
| parent | 0260d2d2dbb2607e7310bdb518b7b6c0f58f5f98 (diff) | |
| download | emacs-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.el | 49 |
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. |
| 32 | FORMAT is a string containing `format'-like specs like \"su - %u %k\", | 32 | FORMAT is a string containing `format'-like specs like \"su - %u %k\". |
| 33 | while SPECIFICATION is an alist mapping from format spec characters | 33 | SPECIFICATION is an alist mapping format specification characters |
| 34 | to values. | 34 | to their substitutions. |
| 35 | 35 | ||
| 36 | For instance: | 36 | For 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 | ||
| 42 | Each format spec can have modifiers, where \"%<010b\" means \"if | 42 | Each %-spec may contain optional flag and width modifiers, as |
| 43 | the expansion is shorter than ten characters, zero-pad it, and if | 43 | follows: |
| 44 | it's longer, chop off characters from the left side\". | ||
| 45 | 44 | ||
| 46 | The following modifiers are allowed: | 45 | %<flags><width>character |
| 47 | 46 | ||
| 48 | * 0: Use zero-padding. | 47 | The 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 | ||
| 55 | Any text properties on a %-spec itself are propagated to the text | 49 | * 0: Pad to the width, if given, with zeros instead of spaces. |
| 56 | that 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 | ||
| 58 | If ONLY-PRESENT, format spec characters not present in | 56 | The width modifier behaves like the corresponding one in `format' |
| 59 | SPECIFICATION are ignored, and the \"%\" characters are left | 57 | when applied to %s. |
| 60 | where they are, including \"%%\" strings." | 58 | |
| 59 | For example, \"%<010b\" means \"substitute into the output the | ||
| 60 | value associated with ?b in SPECIFICATION, either padding it with | ||
| 61 | leading zeros or truncating leading characters until it's ten | ||
| 62 | characters wide\". | ||
| 63 | |||
| 64 | Any text properties of FORMAT are copied to the result, with any | ||
| 65 | text properties of a %-spec itself copied to its substitution. | ||
| 66 | |||
| 67 | ONLY-PRESENT indicates how to handle %-spec characters not | ||
| 68 | present in SPECIFICATION. If it is nil or omitted, emit an | ||
| 69 | error; otherwise leave those %-specs and any occurrences of | ||
| 70 | \"%%\" in FORMAT verbatim in the result, including their text | ||
| 71 | properties, 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)) |