2018-05-21 15:19:38 +02:00
|
|
|
= Command Parsing
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
Kakoune commands, either loaded from a script or written in the command
|
|
|
|
prompt, are parsed according to the following rules:
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Basic parsing
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- Commands are terminated by a `;` or an end of line.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- Words (command names and parameters) are delimited by whitespaces.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Quoted Strings
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
If a word starts with `'`, `"`, or `%X` with `X` a _non-nestable_ punctuation
|
2022-10-20 14:02:25 +02:00
|
|
|
character (see <<command-parsing#balanced-strings,Balanced Strings>> below for
|
|
|
|
nestable characters), it is parsed as a quoted string whose delimiter is,
|
|
|
|
respectively, `'`, `"`, or `X`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
A quoted string contains every character (including whitespaces). Doubling
|
|
|
|
a closing delimiter escapes it. Thus, for example, entering two closing
|
|
|
|
delimiters at the end of a quoted string will render one of the characters
|
|
|
|
literally; that is, it will be considered as part of the quoted string's
|
|
|
|
content.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
Inside double quotes, `%`-strings are processed unless the `%` is escaped by
|
|
|
|
doubling it. Double quotes inside these nested strings must also be escaped.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
2018-05-21 15:19:38 +02:00
|
|
|
No other escaping takes place in quoted strings.
|
|
|
|
|
|
|
|
=== Quoted Strings Examples
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `'foo'` contains *foo*.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `foo'bar'` is read verbatim, so it contains *foo'bar'*.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
|
|
|
- `foo%|bar|` is read verbatim, so it contains *foo%|bar|*.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `'foo''bar'` is a single word whose content is *foo'bar*.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
- `"baz"""` is a single word whose content is *baz"*.
|
|
|
|
|
2019-01-31 14:41:58 +01:00
|
|
|
- `%|foo||bar|` is a single word whose content is *foo|bar*.
|
|
|
|
|
|
|
|
- `"foo %|""bar| %%,baz,"` is a single word whose content is *foo "bar %,baz,*.
|
|
|
|
|
2018-05-21 15:19:38 +02:00
|
|
|
== Balanced Strings
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
If a word starts with `%X` with `X` a _nestable_ punctuation character (one
|
|
|
|
of `(`, `[`, `{` and `<`), it is parsed as a balanced string whose closing
|
|
|
|
delimiter matches that of its opening delimiter (respectively, `)`, `]`,
|
|
|
|
`}`, and `>`).
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-10-20 14:03:13 +02:00
|
|
|
There is no way to escape the opening and closing characters, even if they
|
|
|
|
are nested inside some other kind of string. For example, this will **not**
|
|
|
|
work, because the `{` in the map command interferes with the delimiters for the
|
|
|
|
hook's command block:
|
|
|
|
|
|
|
|
----
|
|
|
|
# DOES NOT WORK
|
|
|
|
hook global WinSetOption filetype=latex %{
|
|
|
|
map window user b 'o\begin{'
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
You can solve this by using a different outer delimiter:
|
|
|
|
|
|
|
|
----
|
|
|
|
hook global WinSetOption filetype=latex %[
|
|
|
|
# Note different delimiter used -----^
|
|
|
|
map window user b o\begin{
|
|
|
|
]
|
|
|
|
----
|
|
|
|
|
|
|
|
...or just by including matching delimiters inside comments:
|
|
|
|
|
|
|
|
----
|
|
|
|
hook global WinSetOption filetype=latex %{
|
|
|
|
map window user b o\begin{ # matched pair to keep Kakoune happy: }
|
|
|
|
}
|
|
|
|
----
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
=== Balanced Strings Examples
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `%{foo}` contains *foo*.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `%{foo\{bar}}` contains *foo\{bar}*.
|
2019-01-31 14:41:58 +01:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `foo%{bar}` contains *foo%{bar}*.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- `"foo %{bar}"` is a single word whose content is *foo bar*.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
== Non-Quoted words
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
Other words are non-quoted. Non-quoted words are terminated by either a
|
|
|
|
whitespace or a `;`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
If they start with a `\` followed by a `%`, `'`, or `"`, then that leading
|
|
|
|
`\` escapes those characters and is discarded.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
If a whitespace or `;` is preceded by a `\`, then the `\` is discarded, and
|
|
|
|
the whitespace or `;` becomes part of the word. Any other `\` is treated
|
|
|
|
as a literal `\`.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
== Typed Expansions
|
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
Quoted and Balanced strings starting with `%` might have an optional
|
|
|
|
alphabetic *expansion type* between the `%` and their delimiter (which is
|
|
|
|
always a punctuation character). This *expansion type* defines how the
|
|
|
|
string's content is going to be expanded. Rules for expanding and escaping
|
|
|
|
expansion types are the same as for `%`-strings.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
|
|
|
- If the *expansion type* is empty, the string content is used verbatim.
|
|
|
|
|
|
|
|
- If the *expansion type* is one of `sh`, `reg`, `opt`, `val` or `arg`,
|
2022-05-10 18:53:03 +02:00
|
|
|
the string is expanded as described in <<expansions#,`:doc expansions`>>.
|
2018-05-21 15:19:38 +02:00
|
|
|
|
2022-05-10 18:53:03 +02:00
|
|
|
- For any other *expansion type*, a parsing error is raised.
|