zsh-users
 help / color / mirror / code / Atom feed
* Matching anywhere in a full path
@ 2015-04-13 20:06 Jesper Nygårds
  2015-04-13 21:10 ` Mikael Magnusson
  2015-04-14  1:30 ` Jan Larres
  0 siblings, 2 replies; 5+ messages in thread
From: Jesper Nygårds @ 2015-04-13 20:06 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1406 bytes --]

I am working on an idea with a completion function that generates file
paths, absolute or relative. Below is a simplified version of what I am
trying to do. I have replaced what I'm really using to generate the paths
with a call to "find", just to make it simple to see what I am stumbling on.

Here's how I've set up the function:

_gen-result() {
    local -a hlist
    hlist=("${(@f)$(find /etc/ -type f)}") # This is just a stand-in for my
real function
    compadd -- $hlist
}

zle -C gen-comp menu-complete _gen-result
bindkey '\ee' gen-comp

I works well. Typing "ls <\ee>" I get the files found below /etc.

Here's what I don't know how to solve: I would like to type some string on
the command line, and I want to have that string working as a filter for
the suggestions, matching anywhere in the path. Say I have "/etc/foo",
"/etc/bar" and /etc/baz/foo.txt", and I type "ls foo <\ee>", I only want
"/etc/foo" and "/etc/baz/foo.txt" to be suggested. In other words, I want
the completion to match against the string anywhere in the full path.

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?

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Matching anywhere in a full path
  2015-04-13 20:06 Matching anywhere in a full path Jesper Nygårds
@ 2015-04-13 21:10 ` Mikael Magnusson
  2015-04-14  5:13   ` Bart Schaefer
  2015-04-14  7:09   ` Jesper Nygårds
  2015-04-14  1:30 ` Jan Larres
  1 sibling, 2 replies; 5+ messages in thread
From: Mikael Magnusson @ 2015-04-13 21:10 UTC (permalink / raw)
  To: Jesper Nygårds; +Cc: Zsh Users

On Mon, Apr 13, 2015 at 10:06 PM, Jesper Nygårds
<jesper.nygards@gmail.com> wrote:
> I am working on an idea with a completion function that generates file
> paths, absolute or relative. Below is a simplified version of what I am
> trying to do. I have replaced what I'm really using to generate the paths
> with a call to "find", just to make it simple to see what I am stumbling on.
>
> Here's how I've set up the function:
>
> _gen-result() {
>     local -a hlist
>     hlist=("${(@f)$(find /etc/ -type f)}") # This is just a stand-in for my
> real function
>     compadd -- $hlist
> }
>
> zle -C gen-comp menu-complete _gen-result
> bindkey '\ee' gen-comp
>
> I works well. Typing "ls <\ee>" I get the files found below /etc.
>
> Here's what I don't know how to solve: I would like to type some string on
> the command line, and I want to have that string working as a filter for
> the suggestions, matching anywhere in the path. Say I have "/etc/foo",
> "/etc/bar" and /etc/baz/foo.txt", and I type "ls foo <\ee>", I only want
> "/etc/foo" and "/etc/baz/foo.txt" to be suggested. In other words, I want
> the completion to match against the string anywhere in the full path.
>
> 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 :).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Matching anywhere in a full path
  2015-04-13 20:06 Matching anywhere in a full path Jesper Nygårds
  2015-04-13 21:10 ` Mikael Magnusson
@ 2015-04-14  1:30 ` Jan Larres
  1 sibling, 0 replies; 5+ messages in thread
From: Jan Larres @ 2015-04-14  1:30 UTC (permalink / raw)
  To: zsh-users

On 14/04/15 08:06, Jesper Nygårds wrote:
> Here's what I don't know how to solve: I would like to type some string on
> the command line, and I want to have that string working as a filter for
> the suggestions, matching anywhere in the path. Say I have "/etc/foo",
> "/etc/bar" and /etc/baz/foo.txt", and I type "ls foo <\ee>", I only want
> "/etc/foo" and "/etc/baz/foo.txt" to be suggested. In other words, I want
> the completion to match against the string anywhere in the full path.

I'm not sure completion is the best way to handle this. I would recommend
something like fzf instead: https://github.com/junegunn/fzf

-Jan


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Matching anywhere in a full path
  2015-04-13 21:10 ` Mikael Magnusson
@ 2015-04-14  5:13   ` Bart Schaefer
  2015-04-14  7:09   ` Jesper Nygårds
  1 sibling, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2015-04-14  5:13 UTC (permalink / raw)
  To: Zsh Users

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@gmail.com> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Matching anywhere in a full path
  2015-04-13 21:10 ` Mikael Magnusson
  2015-04-14  5:13   ` Bart Schaefer
@ 2015-04-14  7:09   ` Jesper Nygårds
  1 sibling, 0 replies; 5+ messages in thread
From: Jesper Nygårds @ 2015-04-14  7:09 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 269 bytes --]

Yes, I realize now that I, as they say, misunderstood my question. The
obvious solution is of course to do the filtering within my completer
function. I was thinking on the wrong level when it comes to how the zsh
completion should be configured.

Thanks for the tips.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-04-14  7:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-13 20:06 Matching anywhere in a full path Jesper Nygårds
2015-04-13 21:10 ` Mikael Magnusson
2015-04-14  5:13   ` Bart Schaefer
2015-04-14  7:09   ` Jesper Nygårds
2015-04-14  1:30 ` Jan Larres

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).