Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: completion within a function



On Tue, Dec 29, 2020 at 1:21 PM Ray Andrews <rayandrews@xxxxxxxxxxx> wrote:
>
> This snippet pretty well does it, mind I'm still interested in trying to
> use completion:
> >     {
> >         local dirs=( $1*(N) )

You don't need completion for that.

  local dirs=( $1*(-/N) )
  [[ $#dirs -eq 1 ]] && [[ -d $dirs[1] ]] && cd "$dirs[1]"
  # else, fall through to n_list():

will do everything your example is doing.  (-) means follow symlinks
if there are any, and (/) means the result must be a directory.

With extendedglob set, file expansion (globbing) can do
case-insensitive matching and approximate matching as well.

> >         for ((aa = 1; aa <= $#dirs; aa++)); do
> >             ! [ -d "$dirs[$aa]" ] && dirs[$aa]=''
> >         done

If you must do it in a loop for some reason:

  for ((aa = 1; aa <= $#dirs; )); do
    if [[ -d $dirs[aa] ]]; then ((aa++))
    else dirs[aa]=()
    fi
  done

Then you don't need dirs=($dirs) later.




Messages sorted by: Reverse Date, Date, Thread, Author