zsh-users
 help / color / mirror / code / Atom feed
* _gnu_generic for aliases
@ 2016-05-19  8:50 Paul Seyfert
  2016-06-02  5:30 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Seyfert @ 2016-05-19  8:50 UTC (permalink / raw)
  To: Zsh Users

Hi,

my situation is the following, I have aliases which are defined
somewhere in /etc/zprofile (i.e. changing where and how the aliases are
defined is not directly accessible to me and can affect other users).

I already wrote my own completion function for them:

_arguments '-c:configuration:->listconfigs'
case $state in
  (listconfigs)
    local -a theconfigs
    # just hard coded strings I don't want to remember/type/mistype
    theconfigs=(x256_777_May x375_324_Jun x942_113_Feb)
    _describe 'config' theconfigs
    ;;
esac

such that I can call the completion with

        fancyalias -c <TAB>

This already covers most of my usages of the alias, but I'd like to improve it
a little more. One of the executables is actually in the PATH so I could test
that the following does what it should do for the executable in the PATH

_arguments '-c:configuration:->listconfigs' ':::->whatelse'
case $state in
  (listconfigs)
    local -a theconfigs
    # just hard coded strings I don't want to remember/type/mistype
    theconfigs=(x256_777_May x375_324_Jun x942_113_Feb)
    _describe 'config' theconfigs
    ;;
  (whatelse)
    _gnu_generic
    ;;
esac

i.e. not only expand -c and its arguments, but also suggests completions on --<TAB>

This however does not work for the fancyalias.

I crosschecked that also

       compdef _gnu_generic fancyalias

does not result in any suggestions. My understanding is that the
_gnu_generic function does not know about the fancyalias / I cannot use
the fancyalias inside the _gnu_generic function.

Is there a way to make _gnu_generic work for aliases?

Thanks,
Paul


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

* Re: _gnu_generic for aliases
  2016-05-19  8:50 _gnu_generic for aliases Paul Seyfert
@ 2016-06-02  5:30 ` Bart Schaefer
  2016-06-02 10:20   ` [solved] " Paul Seyfert
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2016-06-02  5:30 UTC (permalink / raw)
  To: Zsh Users

On May 19, 10:50am, Paul Seyfert wrote:
}
} I crosschecked that also
} 
}        compdef _gnu_generic fancyalias
} 
} does not result in any suggestions. My understanding is that the
} _gnu_generic function does not know about the fancyalias / I cannot use
} the fancyalias inside the _gnu_generic function.
} 
} Is there a way to make _gnu_generic work for aliases?

Have you tried "setopt complete_aliases" ?

Unfortunately it's not easily possible to have it both ways, that is, to
treat an alias as something you can "compdef" and ALSO have the alias be
expanded to use the completion for the command it eventually becomes.

This might work a lot of the time:

setopt complete_aliases
_expand_alias_and_complete() {
  if [[ -o complete_aliases && -n $aliases[$words[1]] ]]; then
    words[1]=( $aliases[$words[1]] )
    _complete
  else
    return 1
  fi
}
zstyle ':completion:*' completer _complete __expand_alias_and_complete

Needs tweaking based on the rest of your current "completer" style, but
hopefully you get the idea.


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

* [solved] Re: _gnu_generic for aliases
  2016-06-02  5:30 ` Bart Schaefer
@ 2016-06-02 10:20   ` Paul Seyfert
  2016-06-03  8:05     ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Paul Seyfert @ 2016-06-02 10:20 UTC (permalink / raw)
  To: zsh-users

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

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

On 02.06.2016 07:30, Bart Schaefer wrote:
> On May 19, 10:50am, Paul Seyfert wrote:
> }
> } I crosschecked that also
> } 
> }        compdef _gnu_generic fancyalias
> } 
> } does not result in any suggestions. My understanding is that the
> } _gnu_generic function does not know about the fancyalias / I cannot use
> } the fancyalias inside the _gnu_generic function.
> } 
> } Is there a way to make _gnu_generic work for aliases?
> 
> Have you tried "setopt complete_aliases" ?

Yes,
I created a small test setup attached:
put "testme.sh" in /tmp/ start zsh with

NORMALPROMPT > zsh -f
defaultprompt > source testrc
defaultprompt > /tmp/testme.sh -<TAB><TAB>

works fine

defaultprompt > fancy -<TAB><TAB><TAB>

nothing.

> Unfortunately it's not easily possible to have it both ways, that is, to
> treat an alias as something you can "compdef" and ALSO have the alias be
> expanded to use the completion for the command it eventually becomes.
> 
> This might work a lot of the time:
> 
> setopt complete_aliases
> _expand_alias_and_complete() {
>   if [[ -o complete_aliases && -n $aliases[$words[1]] ]]; then
>     words[1]=( $aliases[$words[1]] )
>     _complete
>   else
>     return 1
>   fi
> }
> zstyle ':completion:*' completer _complete __expand_alias_and_complete

I have that block commented out in testrc. Once I include it, it works!

> 
> Needs tweaking based on the rest of your current "completer" style, but
> hopefully you get the idea.
> 

So far it seems to work as-is when enabeling all other completion
settings (I didn't put all in testrc).

Thanks!
Paul

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJXUAiCAAoJEPOmP9OjPXmroU8P/20/9V+MZVTYUzEfN8V+G/rG
4XyFcWKVSUTlKV2dnSjnUzk4W1+9nOLphakU5ytd8vcDjg9iyNYi7gQ1tombg+2q
kKtgizmdW08wn4IroDDdO2CQ2tx5C87y9GSw5/QI9YS/b9+0W2R8APTpHthuj4si
UwOSIXkGkIrYVSYBcYZjrW4LJarO6gDyUVZg4XKEunjLwLYbKALDGSKdBc8ewUcI
wiYeXiYGyn79wa+VjzGCOG80/uA9TJKjJG6pCbMvDyMVUH/DoUoWR4GKBWivOz3s
I6HHUcL4E8JHKucbckobiBNPxkx6Ei7c2o/hrT1kWPJrv3VQXnqfe6/yzgJzyjhM
zVFhAyZkqxPUV7cB4WXrMi6jHR7ZXB61PVnp6IncIAYxU1qvQiumy9tpDe8sdc8U
y0hSFzqbeI3fdabZvjX3Zmww91iBB8Iyhg7TCEKA7Dkgr04whAAcJvOeXsesLYf4
tHigU8g7VZWOBMV7TnxxJKxCiPTEd+uAv7OHJ7qUIjxDh1lX0+DbOfxY0dP5O9Xc
mz2/YbJzf13bKMw0RHXMwuI8uUGofNXDvBZy9Zr2ayPzBMXMmGRwV4paBPXdWrGw
1PC7eNad1Tm+cwpixRNU+2O5KW3EpOBAG57WSOQLpg2Td3DwBCAb/EJ40GA8soY7
+zFlbtfuZBNX8ULtRQk5
=d2KE
-----END PGP SIGNATURE-----

[-- Attachment #2: testme.sh --]
[-- Type: application/x-shellscript, Size: 314 bytes --]

[-- Attachment #3: testrc --]
[-- Type: text/plain, Size: 977 bytes --]


autoload -U compinit
compinit

setopt +o bashautolist
setopt +o listambiguous
setopt +o autolist

zstyle ':completion:*:default' menu select=0
zstyle ':completion:*:default' compstate last_prompt=1

alias "fancy"=". /tmp/testme.sh"

setopt complete_aliases

compdef _gnu_generic testme.sh
compdef _gnu_generic fancy

# Messages/warnings format
zstyle ':completion:*:descriptions' format $'%{\e[0;33m%}%d:%{\e[0m%}'
zstyle ':completion:*:messages' format $'%{\e[0;31m%}%d%{\e[0m%}'
zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for: %d%{\e[0m%}'
zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}'

#setopt complete_aliases
#_expand_alias_and_complete() {
#  if [[ -o complete_aliases && -n $aliases[$words[1]] ]]; then
#    words[1]=( $aliases[$words[1]] )
#    _complete
#  else
#    return 1
#  fi
#}
#zstyle ':completion:*' completer _complete _expand_alias_and_complete


setopt NO_BEEP

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

* Re: [solved] Re: _gnu_generic for aliases
  2016-06-02 10:20   ` [solved] " Paul Seyfert
@ 2016-06-03  8:05     ` Bart Schaefer
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2016-06-03  8:05 UTC (permalink / raw)
  To: zsh-users

On Jun 2, 12:20pm, Paul Seyfert wrote:
}
} defaultprompt > fancy -<TAB><TAB><TAB>
} 
} nothing.

Aha.

The _set_command utility doesn't attempt to do anything with aliases,
so it never finds a command to pass to _call_program as the thing to
run to generate the --help listing, so there's nothing for _arguments
to parse even though it *is* being called via _gnu_generic.

} > This might work a lot of the time:
} > 
} > _expand_alias_and_complete() {
[...]
} > }
} > zstyle ':completion:*' completer _complete __expand_alias_and_complete

Typo, should only be one leading underscore in _expand_alias_and_complete
(my error).

} I have that block commented out in testrc. Once I include it, it works!

That has to be accidental, because _expand_alias_and_complete should work
almost exactly like completion with complete_aliases NOT set.  I don't
think this is an actual solution.

I think _set_command needs tweaking to handle [[ -o complete_aliases ]]
but I can't look at this myself for a while.


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

end of thread, other threads:[~2016-06-03  8:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-19  8:50 _gnu_generic for aliases Paul Seyfert
2016-06-02  5:30 ` Bart Schaefer
2016-06-02 10:20   ` [solved] " Paul Seyfert
2016-06-03  8:05     ` Bart Schaefer

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