zsh-users
 help / color / mirror / code / Atom feed
* help for writing GNU stow completion
@ 2019-08-06 17:04 Aurélien
  2019-08-15 23:36 ` dana
  0 siblings, 1 reply; 5+ messages in thread
From: Aurélien @ 2019-08-06 17:04 UTC (permalink / raw)
  To: zsh-users

Hello,

I started working on a completion script for GNU Stow. The work in
progress version is visible on github
(https://github.com/zsh-users/zsh/pull/36/files). It works, but I'm
stuck on the evaluation of a variable like "$HOME"....

Context: GNU Stow is a symbolic link manager, which is based on packages
stored in a directory (the '--dir' option). The completion allows to
propose the packages present in this directory.

From the command line, when I try to complete something like this:

    % stow --dir=$HOME/.dotfiles/

I get: 'no packages found in $HOME/.dotfiles/'

It seems that "$HOME" is not evaluated, and it is the same with "~". Is
there a way to evaluate these elements?

Thank's


-- 
Aur??lien

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

* Re: help for writing GNU stow completion
  2019-08-06 17:04 help for writing GNU stow completion Aurélien
@ 2019-08-15 23:36 ` dana
  2019-08-16  1:28   ` Daniel Shahaf
  2019-08-17  7:48   ` Aurélien
  0 siblings, 2 replies; 5+ messages in thread
From: dana @ 2019-08-15 23:36 UTC (permalink / raw)
  To: Aurélien; +Cc: zsh-users

On 6 Aug 2019, at 12:04, Aurélien <orel_jf@yahoo.fr> wrote:
> From the command line, when I try to complete something like this:
>
>    % stow --dir=$HOME/.dotfiles/
>
> I get: 'no packages found in $HOME/.dotfiles/'
>
> It seems that "$HOME" is not evaluated, and it is the same with "~". Is
> there a way to evaluate these elements?

Sorry for the late reply (haven't been paying attention to the ML much
recently), but if you still need help with this:

Completion functions get arguments on the command line in a 'raw' form,
exactly as they're given, including unexpanded parameters, leading tildes,
quotes, &c. That's why you get a literal $HOME instead of its value.

You can use the (Q) expansion flag to strip quotes, which is a semi-common
thing in completion functions when they need to take user input from the
command line, but there's no flag for anything fancier, and most functions
don't seem to bother with it. But if you wanted to, something like this is
probably the best way...?

  local -a stow_pkg_list
  eval set -A stow_pkg_list $1
  [[ -n $stow_pkg_list ]] && stow_pkg_list=( $stow_pkg_list/*(-/N:t) )

It's not perfectly accurate, though; for example, because of how _arguments
breaks up optargs, this would treat `--dir=~/foo` and `--dir ~/foo` the same,
even though the tilde would not actually be expanded before passing it to stow
in the former case (unless magic_equal_subst was enabled)

dana


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

* Re: help for writing GNU stow completion
  2019-08-15 23:36 ` dana
@ 2019-08-16  1:28   ` Daniel Shahaf
  2019-08-16  1:54     ` dana
  2019-08-17  7:48   ` Aurélien
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel Shahaf @ 2019-08-16  1:28 UTC (permalink / raw)
  To: dana, Aurélien; +Cc: zsh-users

dana wrote on Fri, 16 Aug 2019 01:12 +00:00:
> something like this is probably the best way...?
> 
>   local -a stow_pkg_list
>   eval set -A stow_pkg_list $1

This would run the code being typed on the command line in cases
such as «--dir *(+f)» and «--dir $(sudo pwd)», wouldn't it?

>   [[ -n $stow_pkg_list ]] && stow_pkg_list=( $stow_pkg_list/*(-/N:t) )

I think you want ${^stow_pkg_list} in the second instance, don't you?

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

* Re: help for writing GNU stow completion
  2019-08-16  1:28   ` Daniel Shahaf
@ 2019-08-16  1:54     ` dana
  0 siblings, 0 replies; 5+ messages in thread
From: dana @ 2019-08-16  1:54 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Aurélien, zsh-users

On 15 Aug 2019, at 20:28, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> This would run the code being typed on the command line in cases
> such as «--dir *(+f)» and «--dir $(sudo pwd)», wouldn't it?

Yeah, i guess it would.

On 15 Aug 2019, at 20:28, Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> I think you want ${^stow_pkg_list} in the second instance, don't you?

I was assuming that you'd only ever expect a single argument. If the user did
something like `stow --dir=$arr` or `stow --dir=$( print a b c)`, only the
first element of the expansion would be the optarg; any others would probably
be operands.

But then of course it's possible for someone to do like `--dir={a,b,c}` or
`--dir=$^foo`, and at that point i don't think there's *anything* sane you
could do from inside that helper function.

Stuff like that is probably why most functions don't try to do this

dana


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

* Re: help for writing GNU stow completion
  2019-08-15 23:36 ` dana
  2019-08-16  1:28   ` Daniel Shahaf
@ 2019-08-17  7:48   ` Aurélien
  1 sibling, 0 replies; 5+ messages in thread
From: Aurélien @ 2019-08-17  7:48 UTC (permalink / raw)
  To: dana; +Cc: zsh-users

Le 16/08/2019 ?? 01:36, dana a ??crit??:
> You can use the (Q) expansion flag to strip quotes, which is a semi-common
> thing in completion functions when they need to take user input from the
> command line, but there's no flag for anything fancier, and most functions
> don't seem to bother with it. But if you wanted to, something like this is
> probably the best way...?
> 
>   local -a stow_pkg_list
>   eval set -A stow_pkg_list $1
>   [[ -n $stow_pkg_list ]] && stow_pkg_list=( $stow_pkg_list/*(-/N:t) )
> 
> It's not perfectly accurate, though; for example, because of how _arguments
> breaks up optargs, this would treat `--dir=~/foo` and `--dir ~/foo` the same,
> even though the tilde would not actually be expanded before passing it to stow
> in the former case (unless magic_equal_subst was enabled)
> 

Knowing that this option only accepts one value and after a few tests,
it seems that 'eval' is the right solution, . My function now looks like
this:

  local stow_dir
  local -a stow_pkg_list

  eval set -A stow_dir $1
  [[ -n $stow_dir ]] && stow_pkg_list=( $stow_dir/*(-/N:t) )

  if [[ ${#stow_pkg_list} -gt 0 ]]; then
    _values -C "packages from $stow_dir" ${stow_pkg_list[@]}
  else
    _message "no packages found in $stow_dir"
  fi

and the completion of parameters such as'$HOME' or'~/' works well !

I pushed my modifications on github. Thank you for the answers :-)

-- 
Aur??lien

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

end of thread, other threads:[~2019-08-17  7:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-06 17:04 help for writing GNU stow completion Aurélien
2019-08-15 23:36 ` dana
2019-08-16  1:28   ` Daniel Shahaf
2019-08-16  1:54     ` dana
2019-08-17  7:48   ` Aurélien

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).