From 9c6008d3f6b60859e39c9738eb5466969bf6c514 Mon Sep 17 00:00:00 2001 From: Wu Zhenyu Date: Fri, 19 Jan 2024 22:37:13 +0800 Subject: [PATCH 1/2] feat: Iterate directories to search bash-completion --- zsh-bash-completions-fallback.plugin.zsh | 34 +++++++++++++++++------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/zsh-bash-completions-fallback.plugin.zsh b/zsh-bash-completions-fallback.plugin.zsh index a56d11a..6a10165 100644 --- a/zsh-bash-completions-fallback.plugin.zsh +++ b/zsh-bash-completions-fallback.plugin.zsh @@ -1,6 +1,17 @@ [[ -o interactive ]] || return 0 _bash_completions_getter_path=${0:A:h}/bash-completions-getter.sh +_bash_completions_dirs=( + ~/.local/share + ${(@s/:/)${XDG_DATA_DIRS}} # in NixOS, it contains /run/current-system/sw/share + /data/data/com.termux/files/usr/share # Android Termux + ~/.local/state/nix/profile/share # Nix + /home/linuxbrew/.linuxbrew/share # Homebrew for Linux + /opt/share # Homebrew for arm macOS + /usr/local/share # Homebrew for x86 macOS + /${MINGW_ARCH:-mingw64}/share # Windows Msys2 + /usr/share +) function _bash_completions_fallback_completer { emulate -L zsh @@ -63,17 +74,12 @@ function _bash_completions_fetch_supported_commands { setopt extended_glob typeset_silent no_short_loops unsetopt nomatch - local bash_completions=${ZSH_BASH_COMPLETIONS_FALLBACK_PATH:-${${(@s/:/)${XDG_DATA_DIRS:-/usr/share}}[1]}/bash-completion} - local -a dirs=( - ${BASH_COMPLETION_USER_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion}/completions - ) - - for dir in ${(@s/:/)${XDG_DATA_DIRS:-/usr/local/share:/usr/share}}; do + local dir dirs=($BASH_COMPLETION_USER_DIR) + for dir in "${_bash_completions_dirs[@]}"; do dirs+=("$dir/bash-completion/completions") done - dirs+=("$bash_completions/completions") - + local c for dir in "${dirs[@]}"; do for c in "$dir"/*; do [ ! -f "$c" ] && continue @@ -91,7 +97,17 @@ function _bash_completions_fetch_supported_commands { } function _bash_completions_load { - local bash_completions=${ZSH_BASH_COMPLETIONS_FALLBACK_PATH:-${${(@s/:/)${XDG_DATA_DIRS:-/usr/share}}[1]}/bash-completion} + local dir bash_completions + if [ -d "$ZSH_BASH_COMPLETIONS_FALLBACK_PATH" ]; then + bash_completions=$ZSH_BASH_COMPLETIONS_FALLBACK_PATH + else + for dir in "${_bash_completions_dirs[@]}"; do + if [ -d $dir/bash-completion ]; then + bash_completions=$dir/bash-completion + break + fi + done + fi local reserved_words=( "do" "done" From d9ad97999eab26bb7b3851af407f03762f68ffee Mon Sep 17 00:00:00 2001 From: Wu Zhenyu Date: Fri, 19 Jan 2024 23:21:14 +0800 Subject: [PATCH 2/2] Delete bash_completions and ZSH_BASH_COMPLETIONS_FALLBACK_PATH --- README.md | 3 --- zsh-bash-completions-fallback.plugin.zsh | 16 ---------------- 2 files changed, 19 deletions(-) diff --git a/README.md b/README.md index bc3e99b..091ea11 100644 --- a/README.md +++ b/README.md @@ -105,9 +105,6 @@ Configuration This script defines the following global variables. You may override their default values only after having loaded this script into your ZSH session. -* `ZSH_BASH_COMPLETIONS_FALLBACK_PATH` overrides the default bash completions - path that is set to `/usr/share/bash-completion` by default. - * `ZSH_BASH_COMPLETIONS_FALLBACK_REPLACE_ALL` set (to any value) to allow to replace all the zsh completions, even if we already have one for the given command. diff --git a/zsh-bash-completions-fallback.plugin.zsh b/zsh-bash-completions-fallback.plugin.zsh index 6a10165..b06855b 100644 --- a/zsh-bash-completions-fallback.plugin.zsh +++ b/zsh-bash-completions-fallback.plugin.zsh @@ -97,17 +97,6 @@ function _bash_completions_fetch_supported_commands { } function _bash_completions_load { - local dir bash_completions - if [ -d "$ZSH_BASH_COMPLETIONS_FALLBACK_PATH" ]; then - bash_completions=$ZSH_BASH_COMPLETIONS_FALLBACK_PATH - else - for dir in "${_bash_completions_dirs[@]}"; do - if [ -d $dir/bash-completion ]; then - bash_completions=$dir/bash-completion - break - fi - done - fi local reserved_words=( "do" "done" @@ -138,11 +127,6 @@ function _bash_completions_load { "typeset" ) - if ! [ -f /etc/bash_completion ] && - ! [ -f "$bash_completions/bash_completion" ]; then - return 1; - fi - local -a -U _bash_completions_commands=() _bash_completions_fetch_supported_commands