Skip to content

Commit

Permalink
feat(_comp_compgen): support -U var to unlocal var
Browse files Browse the repository at this point in the history
When `-U var` is specified for a builtin `compgen` call, variable
`var` is unlocalized by `_comp_unlocal` just before storing the result
to the target array.  When `-U var` is specified for a generator call,
variable `var` is unlocalized by `_comp_unlocal` just before calling
the generator function `_comp_compgen_G2` (where `G2` is the generator
name)..

A generator should basically define local variables with the names
starting with `_`.  However, a generator sometimes needs to use local
variable names that do not start with `_`.  When the child generator
call with a variable name (such as `local var; _comp_compgen -v var`)
is used within the generator, the local variable can unexpectedly mask
a local variable of the upper call.

For example, the following call fails to obtain the result of
generator `mygen1` because the array `arr` is masked by the same name
of a local variable in `_comp_compgen_mygen1`.

  # generator with a problem
  _comp_compgen_mygen1()
  {
      local -a arr=(1 2 3)
      _comp_compgen -av arr -- -W '4 5 6'
      _comp_compgen_set "${arr[@]/#p}"
  }

  _comp_compgen -v arr mygen1 # fails to get the result in array `arr`

To avoid this, a generator that defines a local variable that does not
start with `_` can use the option `-U var` to unlocalize the variable
on assigning the final result.

  # properly designed generator
  _comp_compgen_mygen1()
  {
      local -a arr=(1 2 3)
      _comp_compgen -av arr -- -W '4 5 6'
      _comp_compgen -U arr set "${arr[@]/#p}"
  }
  • Loading branch information
akinomyoga committed Aug 20, 2023
1 parent 201239c commit b603535
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 64 deletions.
Loading

0 comments on commit b603535

Please sign in to comment.