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

Re: Matching anywhere in a full path



On Apr 13, 11:10pm, Mikael Magnusson wrote:
} Subject: Re: Matching anywhere in a full path
}
} On Mon, Apr 13, 2015 at 10:06 PM, Jesper Nygards
} <jesper.nygards@xxxxxxxxx> wrote:
} > In this particular example, I realize I could pass the filter string as a
} > restriction to the find command, and I could also filter the array "hlist"
} > after the paths are generated, but that is not so simple in my real
} > command. Is there a way to specify in a more "zsh like" way that when using
} > _gen-result, the whole path should be examined for a match?
} 
} If you have an array foo, and a string bar, you can return all
} elements that contain $bar by doing
} ${(M)foo:#*$bar*}
} (Without the (M), the matching elements will be removed).
} Eg, compadd -- ${hlist:#*$filter*}
} If this is already what you mean by "that is not so simple in my real
} command", then your example is too simplified :).

I have to agree with Mikael here -- either you have an array to filter,
in which case one of ${(M)...:#...} or ${...:#...} should do the job,
or you haven't really given us enough details to give you an answer.

There are some additional considerations, though:

Given "foo" on the line and "/etc/foo" and /etc/baz/foo.txt" as possible
completions, you probably will need to call "compadd -U" to allow ZLE to
remove "foo" before replacing it with each full path.  Otherwise the
internal comparison rules take over and "foo" will have to be a prefix
of the strings passed to "compadd".

The alternative is to set a different prefix and suffix for each of the
possible strings.  E.g.:

_gen-result() {
    local -a hlist
    hlist=("${(@f)$(find /etc/ -type f)}") # This is just a stand-in for my
real function
    if [[ -n $words[CURRENT] ]]
    then
      local maybe
      for maybe in $hlist
      do
        PREFIX=${maybe%$words[CURRENT]*} SUFFIX=${maybe#*$words[CURRENT]}
        [[ $PREFIX != $SUFFIX ]] && compadd -- $maybe
      done
    else
      compadd -- $hlist
    fi
}

-- 
Barton E. Schaefer



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