sio / bash-complete-partial-path

Enhanced file path completion in bash (like in zsh)
Apache License 2.0
54 stars 2 forks source link

cd completion, add candidates from directories in $CDPATH #20

Open tfendin opened 2 years ago

tfendin commented 2 years ago

I use $CDPATH to quickly cd or pushd to some interesting directories. However, the completion I get from _bcpp --dirs only considers directories in $PWD as candidates for completion. I'd love it if I also got candidates from other directories specified in $CDPATH as well.

sio commented 2 years ago

Thank you for the suggestion!

What's the default bash behavior in this regard? Does it offer path completion for directories in $CDPATH (without _bcpp)? Quick test on bash 4.4 (what I have at hand) shows that default bash completion returns only filenames in $PWD, not in other $CDPATH elements. Bash documentation also mentions that $CDPATH completion requires custom handling.

Would you be willing to implement this feature?

tfendin commented 2 years ago

That was a bit strange, I can't remember having this issue before switching to the awesome bccp :) I could get it to work on a vanilla bash in Red Hat (see below). I can try to create a PR sometime in the near future.

~ $ mkdir -p proj/projectA
~ $ mkdir -p anotherdir/subdir
~ $ export CDPATH=:$HOME/proj
~ $ cd anotherdir/
~/anotherdir $ cd <CTRL-i>
projectA/  subdir/
~/anotherdir $ cd p<CTRL-i>rojectA/
/home/tobfen/proj/projectA
~/proj/projectA $ 
~/proj/projectA $ complete -p cd
complete -o nospace -F _cd cd
~/proj/projectA $ cat /etc/redhat-release
Red Hat Enterprise Linux release 8.5 (Ootpa)
~/proj/projectA $ bash --version
GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
~/proj/projectA $
sio commented 2 years ago

Could you add the output of type _cd from that RedHat machine? I guess it's loaded from scop/bash-completion

tfendin commented 2 years ago

It seems that you are right

~ $ type _cd
_cd is a function
_cd ()
{
    local cur prev words cword;
    _init_completion || return;
    local IFS='
' i j k;
    compopt -o filenames;
    if [[ -z "${CDPATH:-}" || "$cur" == ?(.)?(.)/* ]]; then
        _filedir -d;
        return;
    fi;
    local -r mark_dirs=$(_rl_enabled mark-directories && echo y);
    local -r mark_symdirs=$(_rl_enabled mark-symlinked-directories && echo y);
    for i in ${CDPATH//:/'
'};
    do
        k="${#COMPREPLY[@]}";
        for j in $( compgen -d -- $i/$cur );
        do
            if [[ ( -n $mark_symdirs && -h $j || -n $mark_dirs && ! -h $j ) && ! -d ${j#$i/} ]]; then
                j+="/";
            fi;
            COMPREPLY[k++]=${j#$i/};
        done;
    done;
    _filedir -d;
    if [[ ${#COMPREPLY[@]} -eq 1 ]]; then
        i=${COMPREPLY[0]};
        if [[ "$i" == "$cur" && $i != "*/" ]]; then
            COMPREPLY[0]="${i}/";
        fi;
    fi;
    return
}
~ $ grep ^_cd\( /usr/share/bash-completion/bash_completion
_cd()
~ $ rpm -q --whatprovides /usr/share/bash-completion/bash_completion
bash-completion-2.7-5.el8.noarch
~ $ rpm -q --queryformat '%{Name} %{URL}\n' bash-completion-2.7-5.el8.noarch
bash-completion https://github.com/scop/bash-completion
sio commented 2 years ago

Thanks for confirming. Currently bcpp hacks into _filedir function to extend scop/bash-completion, but it appears that when $CDPATH is in use they do not call that function. That means this issue breaks into two:

If you start working on this, do not hesitate to ask me any questions. I will be glad to help.

Existing functionality is pretty thoroughly covered by the test suite, so don't be afraid to introduce regressions - automation should alert you about them. Run test suite locally with make test and/or in GitHub actions when you push to your fork of this repo. Don't worry about FreeBSD/macos tests failing - they are executed by another CI platform that requires extra configuration.