diff options
| author | Philipp Stephani | 2017-06-01 00:09:43 +0200 |
|---|---|---|
| committer | Philipp Stephani | 2017-06-02 00:22:13 +0200 |
| commit | 0dd1bbb0bb228acab21b8e16f2f2a0b5a17b19ab (patch) | |
| tree | 279fbd070724c1d04945b69db32eb69957274e72 /doc | |
| parent | 404273aeacba39833ae3a38ce6764cc7a636e9d9 (diff) | |
| download | emacs-0dd1bbb0bb228acab21b8e16f2f2a0b5a17b19ab.tar.gz emacs-0dd1bbb0bb228acab21b8e16f2f2a0b5a17b19ab.zip | |
Implement field numbers in format strings
A field number explicitly specifies the argument to be formatted.
This is especially important for potential localization work, since
grammars of various languages dictate different word orders.
* src/editfns.c (Fformat): Update documentation.
(styled_format): Implement field numbers.
* doc/lispref/strings.texi (Formatting Strings): Document field numbers.
* lisp/emacs-lisp/bytecomp.el (byte-compile-format-warn): Adapt.
* test/src/editfns-tests.el (format-with-field): New unit test.
Diffstat (limited to 'doc')
| -rw-r--r-- | doc/lispref/strings.texi | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/doc/lispref/strings.texi b/doc/lispref/strings.texi index 9436a96ead4..526b1fb4ebc 100644 --- a/doc/lispref/strings.texi +++ b/doc/lispref/strings.texi | |||
| @@ -864,7 +864,8 @@ below, as the first argument, and the string as the second, like this: | |||
| 864 | (format "%s" @var{arbitrary-string}) | 864 | (format "%s" @var{arbitrary-string}) |
| 865 | @end example | 865 | @end example |
| 866 | 866 | ||
| 867 | If @var{string} contains more than one format specification, the | 867 | If @var{string} contains more than one format specification and none |
| 868 | of the format specifications contain an explicit field number, the | ||
| 868 | format specifications correspond to successive values from | 869 | format specifications correspond to successive values from |
| 869 | @var{objects}. Thus, the first format specification in @var{string} | 870 | @var{objects}. Thus, the first format specification in @var{string} |
| 870 | uses the first such value, the second format specification uses the | 871 | uses the first such value, the second format specification uses the |
| @@ -961,6 +962,25 @@ operation} error. | |||
| 961 | @end group | 962 | @end group |
| 962 | @end example | 963 | @end example |
| 963 | 964 | ||
| 965 | @cindex field numbers in format spec | ||
| 966 | A specification can have a @dfn{field number}, which is a decimal | ||
| 967 | number after the initial @samp{%}, followed by a literal dollar sign | ||
| 968 | @samp{$}. If you provide a field number, then the argument to be | ||
| 969 | printed corresponds to the given field number instead of the next | ||
| 970 | argument. Field numbers start at 1. | ||
| 971 | |||
| 972 | You can mix specifications with and without field numbers. A | ||
| 973 | specification without a field number that follows a specification with | ||
| 974 | a field number will convert the argument after the one specified by | ||
| 975 | the field number: | ||
| 976 | |||
| 977 | @example | ||
| 978 | (format "First argument %2$s, then %s, then %1$s" 1 2 3) | ||
| 979 | @result{} "First argument 2, then 3, then 1" | ||
| 980 | @end example | ||
| 981 | |||
| 982 | You can't use field numbers in a @samp{%%} specification. | ||
| 983 | |||
| 964 | @cindex field width | 984 | @cindex field width |
| 965 | @cindex padding | 985 | @cindex padding |
| 966 | A specification can have a @dfn{width}, which is a decimal number | 986 | A specification can have a @dfn{width}, which is a decimal number |
| @@ -996,9 +1016,14 @@ is not truncated. | |||
| 996 | @end group | 1016 | @end group |
| 997 | @end example | 1017 | @end example |
| 998 | 1018 | ||
| 1019 | If you want to use both a field number and a width, place the field | ||
| 1020 | number before the width. For example, in @samp{%2$7s}, @samp{2} is | ||
| 1021 | the field number and @samp{7} is the width. | ||
| 1022 | |||
| 999 | @cindex flags in format specifications | 1023 | @cindex flags in format specifications |
| 1000 | Immediately after the @samp{%} and before the optional width | 1024 | After the @samp{%} and before the optional width specifier, you can |
| 1001 | specifier, you can also put certain @dfn{flag characters}. | 1025 | also put certain @dfn{flag characters}. The flag characters need to |
| 1026 | come directly after a potential field number. | ||
| 1002 | 1027 | ||
| 1003 | The flag @samp{+} inserts a plus sign before a positive number, so | 1028 | The flag @samp{+} inserts a plus sign before a positive number, so |
| 1004 | that it always has a sign. A space character as flag inserts a space | 1029 | that it always has a sign. A space character as flag inserts a space |