|
| 1 | +# API and naming |
| 2 | + |
| 3 | +## General API conventions |
| 4 | + |
| 5 | +Most of the functions in bash-completion generate completions and directly |
| 6 | +inject them to the `COMPREPLY` array variable, as required for completions to |
| 7 | +work. |
| 8 | + |
| 9 | +Most other functions make use of "output" variables, i.e. assign values to |
| 10 | +them. The most common one of these is named `ret`. Consult the commentary |
| 11 | +before each function in the source to find out the specific names. |
| 12 | +`local`izing output variables before invoking a function that populates them |
| 13 | +is the caller's responsibility. |
| 14 | +Note that if calling multiple functions that assign output to the same variable |
| 15 | +during one completion function run, each result should be copied to another |
| 16 | +variable between the calls to avoid it possibly being overwritten and lost on |
| 17 | +the next call. Also, the variables should also be ensured to be clear before |
| 18 | +each call that references the value, variable name, or their existence, |
| 19 | +typically by `unset -v`ing them when multiple such calls are used, |
| 20 | +to avoid them interfering with each other. |
| 21 | + |
| 22 | +## Naming |
| 23 | + |
| 24 | +Due to its nature, bash-completion adds a number of functions and variables in |
| 25 | +the shell's environment. |
| 26 | + |
| 27 | +| | `bash_completion` | `completions/*` | |
| 28 | +|:------------------------------------|:--------------------|:---------------------------------------------------------------------------| |
| 29 | +| public configuration variables | `BASH_COMPLETION_*` | `BASH_COMPLETION_CMD_${Command^^}_${Config^^}` | |
| 30 | +| private non-local variables | `_comp__*` | `_comp_cmd_${Command}__${Data}` | |
| 31 | +| private non-local mutable variables | `_comp__*_mut_*` | `_comp_cmd_${Command}__mut_${Data}` | |
| 32 | +| exporter function local variables | `_*` (not `_comp*`) | `_*` (not `_comp*`) | |
| 33 | +| public/exported functions | `_comp_*` | `_comp_cmd_${Command}` (functions for `complete -F`) | |
| 34 | +| | | `_comp_xfunc_${Command}_${Utility}` (functions for use with `_comp_xfunc`) | |
| 35 | +| private/internal functions | `_comp__*` | `_comp_cmd_${Command}__${Utility}` (utility functions) | |
| 36 | + |
| 37 | +`${Command}` refers to a command name (with characters not allowed in POSIX |
| 38 | +function or variable names replaced by an underscore), `${Config}` the name of |
| 39 | +a configurable thing, `^^` means uppercase, `${Data}` is an identifier for the |
| 40 | +data contained in the variable, and `${Utility}` describes the typical usage of |
| 41 | +the function. |
| 42 | + |
| 43 | +Variables and functions affecting multiple completions are usually defined |
| 44 | +in the main `bash_completion` file and do not require any additional files to |
| 45 | +be sourced. Variables and functions in command specific completion files in |
| 46 | +`completions/*` follow a slightly different naming scheme; they include |
| 47 | +`cmd` in their name as well as the name of the command. |
| 48 | + |
| 49 | +Public configuration variables are shell ones that affect the runtime behavior |
| 50 | +of various completions. As a rule of thumb, we lean towards not providing |
| 51 | +customizability but rather strive to provide great completion behavior out of |
| 52 | +the box. But there are some, see [configuration](configuration.md). |
| 53 | + |
| 54 | +Variables and functions whose name contains a double underscore (`__`) anywhere |
| 55 | +in their name are private implementation details, not part of the stable API, |
| 56 | +and not intended to be used outside of their defining context. Internally, the |
| 57 | +double underscores serve as privacy scope delimiters; there can be more than one |
| 58 | +pair of them in a name, and functions and variables are intended to reference |
| 59 | +and call other functions and variables within that scope, one level deep, |
| 60 | +sharing a common prefix. For example, a function named `_comp_foo` is "allowed" |
| 61 | +to access `_comp_foo__*` where `*` does not contain any double underscores, |
| 62 | +i.e. it should not access `_comp_foo__something__*` despite the common prefix. |
| 63 | + |
| 64 | +Private non-local variables are considered readonly by default. When a |
| 65 | +completion function needs to change variables for e.g. caching purposes, the |
| 66 | +variables should contain the infix `*_mut_*` anywhere in their names. This is |
| 67 | +needed to tell the test framework to allow these variables changing. |
| 68 | +Nevertheless, the completion results should be consistent among different calls |
| 69 | +and unaffected by the state of the cache variables when it is called. |
| 70 | + |
| 71 | +Internal local variables of functions that "export" their results using a |
| 72 | +variable name that is passed in start with an underscore and do not start with |
| 73 | +`_comp`. The variable names that are passed in for this purpose must not start |
| 74 | +with an underscore. |
| 75 | + |
| 76 | +Functions with names prefixed with `_comp_xfunc_` are intended to be used |
| 77 | +through the `_comp_xfunc` function from files other than the one they are |
| 78 | +defined in. From the same file they can be used directly using their complete |
| 79 | +name. |
| 80 | + |
| 81 | +Function names start with an underscore in order to avoid them being |
| 82 | +included in completions of command names. (Except naturally when a command |
| 83 | +starting with an underscore is being completed.) The underscore prefix does |
| 84 | +not have anything to do with whether the thing is considered public or |
| 85 | +private in the API, nor anything else really. |
| 86 | + |
| 87 | +The `BASH_COMPLETION_` prefix provides a namespace and makes it clear what |
| 88 | +these variables relate to. The `_comp` in other names serves a similar purpose, |
| 89 | +but because these are used a lot in the code (unlike the public configuration |
| 90 | +variables), using something shorter is beneficial. We hope and believe this is |
| 91 | +distinctive and clash free enough. |
| 92 | + |
| 93 | +It is known that a lot of functions and variables in the tree do not follow |
| 94 | +these naming rules yet. Things introduced after version 2.11 should, and we are |
| 95 | +evaluating our options for handling older ones. |
0 commit comments