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

Activating liquidprompt clobbers completion options #641

Closed
nedbat opened this issue Jan 1, 2021 · 3 comments
Closed

Activating liquidprompt clobbers completion options #641

nedbat opened this issue Jan 1, 2021 · 3 comments
Assignees
Labels
Milestone

Comments

@nedbat
Copy link

nedbat commented Jan 1, 2021

When I set a zsh completion option, and then activate liquidprompt, my completion option is gone.

In my case, I have a git shortcut function, and I set the completion to be the same as for git:

g() { git "$@"; }
compdef g=git
source ~/liquidprompt/liquidprompt

Now the completion for "g" doesn't work properly. If I define the completion after the prompt, then it does work:

g() { git "$@"; }
source ~/liquidprompt/liquidprompt
compdef g=git

Shell: zsh 5.8 (x86_64-apple-darwin17.7.0)
Operating system: MacOS 10.13.6
Liquidprompt version: v2.0.0-beta.2

Possible Solution

Why invoke "compinit" when activating the prompt? Leave that out.

@nedbat nedbat added the bug label Jan 1, 2021
@nedbat nedbat changed the title Activating liquidprompt shouldn't run "compinit" Activating liquidprompt clobbers completion options Jan 1, 2021
@Rycieos
Copy link
Collaborator

Rycieos commented Jan 1, 2021

Referencing this line:
https://github.com/nojhan/liquidprompt/blob/f445eff684558e8bf200df2f3f897c09374b7d6c/liquidprompt#L374

I read the Zsh manual on this module: http://zsh.sourceforge.net/Doc/Release/Completion-System.html
Which describes calling compinit in the .zshrc or similar. So I didn't call compinit, but then you get this error when calling compdef:

lp_activate:20: command not found: compdef

So compinit must be called first.

My thought process was why not enable completion. But I didn't realize that running compinit after it was already called (and some configuration was done) would wipe all previous configuration.

So I see a few solutions:

  1. Only add this completion if the user has already called compinit. How would we test for that?
  2. Call compdef no matter what, and ignore errors.

Any ideas on how to detect the current state of the completion system?

Actually, it looks like a function:

$ type compdef
compdef not found
$ autoload -Uz compinit && compinit
$ type compdef
compdef is a shell function from /usr/share/zsh/5.5.1/functions/compinit

So I think this patch should work:

-        autoload -Uz compinit && compinit && compdef __lp_theme_zsh_complete lp_theme
+        # Enable the autocomplete if the autocomplete system is initialized.
+        __lp_is_function compdef && compdef __lp_theme_zsh_complete lp_theme 

Would that fix your issue?

@nedbat
Copy link
Author

nedbat commented Jan 1, 2021

That was fast! Thanks.

I tried your patch, and it fixes my issue, yes. I don't know what completion that compdef is enabling, so I can't confirm that it still enables it though.

@Rycieos
Copy link
Collaborator

Rycieos commented Jan 1, 2021

It's for the lp_theme function. Hitting <Tab> after should autocomplete default, or more options if you source another file like themes/powerline/powerline.theme.

@Rycieos Rycieos closed this as completed in 762018c Jan 1, 2021
@Rycieos Rycieos added this to the v2.0 milestone Jan 1, 2021
@Rycieos Rycieos self-assigned this Jan 1, 2021
pkkolos pushed a commit to pkkolos/liquidprompt that referenced this issue May 9, 2021
I had discovered while working on the completion support for lp_theme()
that calling compdef before compinit would error. So I assumed that
calling compinit first would be the right call. But actually calling
compinit removes all previously set completions, which is not what most
users want.

Rather than force users to turn on completion, instead detect if zsh has
completion enabled (by looking for the compdef function), and if so
adding our completion.

Fixes liquidprompt#641
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants