Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: ZSH Autocomplete does not work if installed as a file #2955 #2956

Merged
merged 1 commit into from
May 14, 2024

Conversation

Maetveis
Copy link
Contributor

@Maetveis Maetveis commented May 14, 2024

Fixes: #2955

The #compdef directive shouldn't have double-quotes. (https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Autoloaded-files)

We need to execute the function the first time when its loaded as described by https://zsh.sourceforge.io/Doc/Release/Functions.html#Autoloading-Functions

[If the KSH_AUTOLOAD option is not set] the function body
(with no surrounding `funcname() {...}``) is taken to be the complete contents of the file.
This form allows the file to be used directly as an executable shell script.

If processing of the file results in the function being re-defined, the function itself is not re-executed. To force the shell to perform initialization and then call the function defined, the file should contain initialization code (which will be executed then discarded) in addition to a complete function definition (which will be retained for subsequent calls to the function), and a call to the shell function, including any arguments, at the end.

To keep direct sourcing possible, zsh_eval_context is used (https://zsh.sourceforge.io/Doc/Release/Parameters.html#Parameters-Set-By-The-Shell) to detect wether the script is being auto-loaded.

…nge#2955

The #compdef directive shouldn't have double-quotes.
(https://zsh.sourceforge.io/Doc/Release/Completion-System.html#Autoloaded-files)

We need to execute the function the first time when its loaded as
described by https://zsh.sourceforge.io/Doc/Release/Functions.html#Autoloading-Functions

> [If the KSH_AUTOLOAD option is not set] the function body
(with no surrounding `funcname() {...}``) is taken to be the complete
contents of the file.
This form allows the file to be used directly as an executable shell script.
If processing of the file results in the function being re-defined,
the function itself is not re-executed. To force the shell to perform
initialization and then call the function defined, the file should contain
initialization code (which will be executed then discarded) in addition
to a complete function definition (which will be retained for subsequent
calls to the function), and a call to the shell function,
including any arguments, at the end.

To keep direct sourcing possible still use `zsh_eval_context`
(https://zsh.sourceforge.io/Doc/Release/Parameters.html#Parameters-Set-By-The-Shell)
to detect wether the script is being auto-loaded.
@tlimoncelli
Copy link
Contributor

CC @whi-tw

Copy link
Contributor

@whi-tw whi-tw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Good catch, I've not spotted the behaviour myself, but that is definitely what the spec says!

Seems to work fine on my end :)

@tlimoncelli
Copy link
Contributor

Thanks! All the zsh users thank you too!

@tlimoncelli tlimoncelli merged commit ec241ee into StackExchange:main May 14, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Autocomplete does not work in ZSH when installed as a file
3 participants