Skip to content

Commit f46be37

Browse files
akinomyogascop
andcommitted
docs(api-and-naming): add rules for xfuncs and generators
scop#954 (comment) scop#964 scop#962 (comment) Co-authored-by: Ville Skyttä <[email protected]>
1 parent 0cd3bce commit f46be37

File tree

1 file changed

+73
-9
lines changed

1 file changed

+73
-9
lines changed

doc/api-and-naming.md

+73-9
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,17 @@ deprecated in.
4141
Due to its nature, bash-completion adds a number of functions and variables in
4242
the shell's environment.
4343

44-
| | `bash_completion` | `completions/*` |
45-
|:------------------------------------|:--------------------|:---------------------------------------------------------------------------|
46-
| public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` |
47-
| private non-local variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` |
48-
| private non-local mutable variables | `_comp__*_mut_*` | `_comp_cmd_${Command}__mut_${Data}` |
49-
| exporter function local variables | `_*` (not `_comp*`) | `_*` (not `_comp*`) |
50-
| public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) |
51-
| | | `_comp_xfunc_${Command}_${Utility}` (functions for use with `_comp_xfunc`) |
52-
| private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) |
44+
| | `bash_completion` | `completions/*` |
45+
|:------------------------------------|:------------------------|:--------------------------------------------------------------------------------------|
46+
| public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` |
47+
| private non-local variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` |
48+
| private non-local mutable variables | `_comp__*_mut_*` | `_comp_cmd_${Command}__mut_${Data}` |
49+
| exporter function local variables | `_*` (not `_comp*`) | `_*` (not `_comp*`) |
50+
| public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) |
51+
| | | `_comp_xfunc_${Command}_${Utility}` (functions for use with `_comp_xfunc`) |
52+
| | `_comp_compgen_${Name}` | `_comp_xfunc_${Command}_compgen_${Name}` (generators for use with `_comp_compgen -x`) |
53+
| private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) |
54+
| | | `_comp_cmd_${Command}__compgen_${Name}` (generators for use with `_comp_compgen -i`) |
5355

5456
`${Command}` refers to a command name (with characters not allowed in POSIX
5557
function or variable names replaced by an underscore), `${Config}` the name of
@@ -110,3 +112,65 @@ distinctive and clash free enough.
110112
It is known that a lot of functions and variables in the tree do not follow
111113
these naming rules yet. Things introduced after version 2.11 should, and we are
112114
evaluating our options for handling older ones.
115+
116+
## Exported functions (xfunc)
117+
118+
Exported functions (xfunc) are the functions defined in completion files for
119+
specific commands but exposed to other completion files. The xfuncs have the
120+
name `_comp_xfunc_CMD_NAME` where `CMD` is the name of the associated command,
121+
and `NAME` is the name of the utility. The other functions defined in specific
122+
completion files are considered private and should not be called outside the
123+
file.
124+
125+
The xfuncs can be called by `_comp_xfunc CMD NAME ARGS` from external files.
126+
The xfuncs are supposed to be directly called as `_comp_xfunc_CMD_NAME ARGS`
127+
from the same file where they are defined, or if they wrap a `_comp_cmd_NAME__*`
128+
function, that one should be called directly instead.
129+
130+
Note: The name `xfunc` was initially the name of a utility function, `_xfunc`,
131+
to call "eXternal FUNCtions" that are defined in other completion files. The
132+
concept is later extended to also mean "eXported".
133+
134+
## Generator functions
135+
136+
The generator functions, which have names of the form `_comp_compgen_NAME`, are
137+
used to generate completion candidates. A generator function is supposed to be
138+
called by `_comp_compgen [OPTS] NAME ARGS` where `OPTS = -aRl|-v var|-c cur|-C
139+
dir|-F sep` are the options to modify the behavior (see the code comment of
140+
`_comp_compgen` for details). When there are no `opts`, the generator function
141+
is supposed to be directly called as `_comp_compgen_NAME ARGS`. The result is
142+
stored in the target variable (which is `COMPREPLY` by default but can be
143+
specified by `-v var` in `OPTS`).
144+
145+
### Implementing a generator function
146+
147+
To implement a generator function, one should generate completion candidates by
148+
calling `_comp_compgen` or other generators. One should not directly modify or
149+
reference the target variable. When post-filtering is needed, store them in
150+
local arrays, filter them, and finally append them by `_comp_compgen -- -W
151+
"${arr[@]}"`.
152+
153+
A generator function should replace the existing content of the variable by
154+
default. When the appending behavior is favored, the caller should specify it
155+
through `_comp_compgen -a NAME`. The generator function do not need to process
156+
it because internal `_comp_compgen` calls automatically reflects the option
157+
`-a` specified to the outer calls of `_comp_compgen`.
158+
159+
The exit status is implementation-defined.
160+
161+
- The `_comp_compgen -- COMPGEN_ARGS` returns whether there is at least one
162+
completions. This is useful when one wants to reuse the array content with
163+
`"${tmp[@]}"` avoiding `nounset` error.
164+
- Some use other rules for the exit status. E.g., `help` and `usage` return
165+
whether there were options *before* filtering by cur. This is used for
166+
`_comp_compgen_help || _comp_compgen_usage`.
167+
168+
Whether to clear the target variable on the runtime error (when `-a` is not
169+
specified in `OPTS`) is implementation-defined. On the other hand, the
170+
generator function should not leave any side effects in the target variable on
171+
the usage error.
172+
173+
Exported generators are defined with the names `_comp_xfunc_CMD_compgen_NAME`
174+
and called by `_comp_compgen [opts] -x CMD NAME args`. Internal generators are
175+
defined with the names `_comp_cmd_CMD__compgen_NAME` and called by
176+
`_comp_compgen [opts] -i CMD NAME args`.

0 commit comments

Comments
 (0)