Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Matching anywhere in a full path
- X-seq: zsh-users 20165
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh Users <zsh-users@xxxxxxx>
- Subject: Re: Matching anywhere in a full path
- Date: Mon, 13 Apr 2015 22:13:03 -0700
- In-reply-to: <CAHYJk3S4mhhv1m0mrDJu2HQgHDcgtA6aUVq=GJSCuSP+vPxFZg@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CABZhJg9ZqCXZfJ1XJha36Oe3vLxCsHmB3fvoowGN7OLPPYZGLw@mail.gmail.com> <CAHYJk3S4mhhv1m0mrDJu2HQgHDcgtA6aUVq=GJSCuSP+vPxFZg@mail.gmail.com>
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