Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
tfendin opened this issue Jul 13, 2022 · 5 comments
Open

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

tfendin opened this issue Jul 13, 2022 · 5 comments

Comments

@tfendin
Copy link

tfendin commented Jul 13, 2022

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
Copy link
Owner

sio commented Jul 13, 2022

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
Copy link
Author

tfendin commented Jul 13, 2022

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
Copy link
Owner

sio commented Jul 13, 2022

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

@tfendin
Copy link
Author

tfendin commented Jul 13, 2022

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
Copy link
Owner

sio commented Jul 13, 2022

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:

  • Implement path expansion that respects multiple starting directories from $CDPATH
  • Update compatibility wrappers for scop/bash-completion

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants