In the current codebase, for the original completion settings, the
pieces of the codes to determine the variable name and to access the
stored data are scattered. In this patch, we define functions to
access these variables. Those functions will be used in a coming
patch.
* This patch also resolves an inconsistent escaping of "$cmd": $cmd is
escaped as ${...//[^A-Za-z0-9_]/_} in some places, but it is escaped
as ${...//[^A-Za-z0-9_=]/_} in some other places. The latter leaves
the character "=" in the command name, which causes an issue because
"=" cannot be a part of a variable name. For example, the following
test case produces an error message:
$ COMP_WORDBREAKS=${COMP_WORDBREAKS//=}
$ _test1() { COMPREPLY=(); }
$ complete -vF _test1 cmd.v=1.0
$ _fzf_setup_completion path cmd.v=1.0
$ cmd.v=1.0 [TAB]
bash: _fzf_orig_completion_cmd_v=1_0: invalid variable name
The behavior of leaving "=" was present from the beginning when
saving the original completion is introduced in commit 91401514, and
this does not seem to be a specific reasoning. In this patch, we
replace "=" as well as the other non-identifier characters.
* Note: In this patch, the variable REPLY is used to return values
from functions. This design is to make it useful with the value
substitutions, a new Bash feature of the next release 5.3, which is
taken from mksh.
When bash-completion (and thus `_known_hosts_real()`) is / is not available this
will typically not change during the lifetime of a shell.
The only exception is if the user would unset `_known_hosts_real()`, but well,
that would be his problem.
So we can easily define `__fzf_list_hosts()` either using `_known_hosts_real()`
or using the old code, and avoid checking every time whether
`_known_hosts_real()` is defined.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
If defined, use bash-completions’s `_known_hosts_real()`-function to create the
list of hostnames.
This obviously requires bash-completion to be sourced before fzf.
If not defined, fall back to the previous code.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Just like it’s already done for `_fzf_compgen_path()` and `_fzf_compgen_dir()`
allow a user to easily define his own version of `__fzf_list_hosts()`.
Also add some documentation on the expected “interface” of such custom function.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
`__fzf_list_hosts()` seems like a function a user may want to override with some
custom code.
For that reason it should be kept as simple as possible, that is printing only
hostnames, one per line, optionally in some sorting.
The handling of adding a `username@` (which is then the same for each line), if
any, would unnecessarily complicate that for people who want to override the
function.
Therefore this commit moves that to the places where it's actually used (as of
now only `_fzf_complete_ssh()`).
This also saves any such handling for `_fzf_host_completion()`, where this isn’t
needed at all.
Right now it comes at a cost, namely an extra invocation of `awk` in the
`_fzf_complete_ssh()`-case.
However, it should be easily possible to improve `__fzf_list_hosts()` to no
longer need the final `awk` in the pipeline there.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
This commit causes all simple commands that are not built-ins or functions to be
invoked via `command` in order to protect them from alias substitution or from
accidentally taking functions of the same name.
It was decided to not “protect” `fzf` and `fzf-tmux` for now.
Maybe a better solution should be implemented for that in the future.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
* [bash] bring fzf’s own bash completion up to date
This orders and groups completed options and values in just as they appear in
the code respectively, for some option values, as they’d be printed in the
`--help`-output.
It does not add support for completion of `:` right after values that support an
optional `:some-further-value` postfix.
Neither does it add support for the `--option=value`-style.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
* [bash] drop unnecessary code in handling `history`
Presumably, the dropped code is not needed for any effect, thus drop it.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
---------
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
* [bash] return instead of not executing an if-block, when non-interactive
This should keep the code more readable, be less error prone (accidentally doing
something outside the if-block and aligns the code with what’s already done for
zsh.
`0` is returned, because it shall not be considered an error when the script is
(accidentally) sourced from a non-interactive shell.
If executed as a script (rather than sourced), the results are not specified by
POSIX but depend on the shell, with bash giving an error in that case.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
* [shell] exit immediately when called from non-interactive shell
The shell execution environment shouldn’t be modified at all, when called from a
non-interactive shell.
It shall be noted that the current check may become error prone for bash, namely
in case there should ever be a differentiation between `i` and `I` in the
special variable `-` and bash’s `nocasematch`-shell-option be used.
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
Take two.
* Avoid eval if the prefix contains `:=`
* This is not to evaluate variable assignment. e.g. ${FOO:=BAR}
* [zsh] Prevent `>(...)` form
* Suppress error message from prefix evaluation
* Stop completion when prefix evaluation failed
Thanks to @calestyo
When just checking whether a function is already defined or not, it’s not
necessary to print out it’s definition (should it be defined).
bash’s `declare` provides the `-F`-option (which implies `-f`), which should
give a minor performance improvement
Signed-off-by: Christoph Anton Mitterer <mail@christoph.anton.mitterer.name>
* Explicitly specify the list of fields for consistent experience
* Add fallback command for BusyBox (Close#3219)
* Apply `--header-lines=1` to show the column header
I forgot to add the "not _fzf" check into __fzf_orig_completion, so
invoking it twice would rewrite the _fzf_orig_completion_xxx variables
and then cause an endless loop when completion is requested.
Fixes: ef2c29d5d4 ("[bash-completion] Optimize __fzf_orig_completion_filter")
Commit d4ad4a25 slowed loading of completion.bash significantly (on my
laptop from 10 ms to 30 ms), then 54891d11 improved that (to 20 ms) but
it still stands out as the heavy part of my .bashrc.
Rewriting __fzf_orig_completion_filter to pure bash without forking to
sed/awk brings this back under 10 ms.
before:
$ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i'
Benchmark #1: bash --rcfile shell/completion.bash -i
Time (mean ± σ): 21.2 ms ± 0.3 ms [User: 24.9 ms, System: 6.4 ms]
Range (min … max): 20.7 ms … 23.3 ms 132 runs
after:
$ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i'
Benchmark #1: bash --rcfile shell/completion.bash -i
Time (mean ± σ): 9.6 ms ± 0.3 ms [User: 8.0 ms, System: 2.2 ms]
Range (min … max): 9.3 ms … 11.4 ms 298 runs
Fixes: d4ad4a25db ("[bash-completion] Fix default alias/variable completion")
Fixes: 54891d11e0 ("[bash-completion] Minor optimization")
This prevents mistakes like the one fixed by the previous commit, and
also speeds bash startup a tiny bit:
before:
$ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i'
Benchmark #1: bash --rcfile shell/completion.bash -i
Time (mean ± σ): 22.4 ms ± 0.6 ms [User: 28.7 ms, System: 7.8 ms]
Range (min … max): 21.7 ms … 25.2 ms 123 runs
after:
$ HISTFILE=/tmp/bashhist hyperfine 'bash --rcfile shell/completion.bash -i'
Benchmark #1: bash --rcfile shell/completion.bash -i
Time (mean ± σ): 21.2 ms ± 0.3 ms [User: 24.9 ms, System: 6.4 ms]
Range (min … max): 20.7 ms … 23.3 ms 132 runs
This doesn't look right:
$ complete | grep ' _.$'
complete _a
complete _v
The __fzf_orig_completion_filter invocation in _fzf_setup_completion
needs the /-F/ filter, just like all the other invocations.
Fixes: d4ad4a25db ("[bash-completion] Fix default alias/variable completion")
Requires latest tmux built from source (e.g. brew install tmux --HEAD)
Examples:
# 50%/50% width and height on the center of the screen
fzf-tmux -p
# 80%/80%
fzf-tmux -p80%
# 80%/40%
fzf-tmux -p80%,40%
# Separate -w and -h
fzf-tmux -w80% -h40%
# 80%/40% at position (0, 0)
fzf-tmux -w80% -h40% -x0 -y0
You can configure key bindings and fuzzy completion to open in tmux
popup window like so:
FZF_TMUX_OPTS='-p 80%'
To make it easier to write more complex fzf options. Although this
does not break backward compatibility, users are encouraged to update
their code accordingly.
# Before
_fzf_complete "FZF_ARG1 FZF_ARG2..." "$@" < <(
# Print candidates
)
# After
_fzf_complete FZF_ARG1 FZF_ARG2... -- "$@" < <(
# Print candidates
)