zsh-users
 help / color / mirror / code / Atom feed
* Conditionally complete on space.
@ 2017-08-17 22:34 Grant Taylor
  2017-08-18  1:07 ` Daniel Shahaf
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Taylor @ 2017-08-17 22:34 UTC (permalink / raw)
  To: Zsh Users List

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

Is it possible to have ZLE conditionally complete on a space?

I know that I can 'bindkey " " expand-or-complete' to get space to 
complete like tab.  However, this does not play well when I want a true 
space.

Thus I want to conditionally complete if there is not a space proceeding 
the cursor's current position.  If there is a space proceeding the 
cursor, then put a space and not call expand-or-complete.

I am guessing that I will need to modify expand-or-complete's behavior 
via modifying _main_complete, or wrap _main_complete with something else 
to do the conditional logic and then call _main_complete, and update zle 
so that expand-or-complete calls the wrapper instead of _main_complete.

Should such be possible?  Or am I waisting my time?



-- 
Grant. . . .
unix || die


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 3717 bytes --]

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

* Re: Conditionally complete on space.
  2017-08-17 22:34 Conditionally complete on space Grant Taylor
@ 2017-08-18  1:07 ` Daniel Shahaf
  2017-08-18  4:32   ` Grant Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2017-08-18  1:07 UTC (permalink / raw)
  To: Grant Taylor; +Cc: Zsh Users List

Grant Taylor wrote on Thu, 17 Aug 2017 16:34 -0600:
> Thus I want to conditionally complete if there is not a space proceeding 
> the cursor's current position.  If there is a space proceeding the 
> cursor, then put a space and not call expand-or-complete.

Define a custom widget:

    maybe-complete-on-space() {
      if [[ $LBUFFER[-1] == ' ' ]]; then
        zle self-insert
      else
        zle expand-or-complete
      fi
    }
    zle -N maybe-complete-on-space

and bind it:

    bindkey ' ' maybe-complete-on-space

This implements the behaviour you specified, but I suspect you meant the
'if' and the 'else' behaviours to be the other way around.

> I am guessing that I will need to modify expand-or-complete's behavior 
> via modifying _main_complete, or wrap _main_complete with something else 
> to do the conditional logic and then call _main_complete, and update zle 
> so that expand-or-complete calls the wrapper instead of _main_complete.

You aren't changing the behaviour of completion, you are just changing
the method of invoking it, so the solution was in zshzle(1)'s domain,
not in zshcompsys(1)'s.

Cheers,

Daniel


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

* Re: Conditionally complete on space.
  2017-08-18  1:07 ` Daniel Shahaf
@ 2017-08-18  4:32   ` Grant Taylor
  2017-08-18  5:07     ` Daniel Shahaf
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Taylor @ 2017-08-18  4:32 UTC (permalink / raw)
  To: zsh-users

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

On 08/17/2017 07:07 PM, Daniel Shahaf wrote:
> Define a custom widget:
> 
>     maybe-complete-on-space() {
>       if [[ $LBUFFER[-1] == ' ' ]]; then
>         zle self-insert
>       else
>         zle expand-or-complete
>       fi
>     }
>     zle -N maybe-complete-on-space
> 
> and bind it:
> 
>     bindkey ' ' maybe-complete-on-space

Thank you Daniel!

That does seem very close to what I want.

> This implements the behaviour you specified, but I suspect you meant the
> 'if' and the 'else' behaviours to be the other way around.

Nope, you got what I wanted the first time.

> You aren't changing the behaviour of completion, you are just changing
> the method of invoking it, so the solution was in zshzle(1)'s domain,
> not in zshcompsys(1)'s.

Good to know.

I did find a couple of oddities when I was testing as you provided:

1)  I need to hit space an extra time after completion.  I.e. "sip   "
results in "sipcalc  ".  -  It seems as if the second space is
swallowed.  (I do not consider this a problem and am happy to live with it.

2)  I seem to be stuck in a loop if I try to complete "kil" into "kill"
because "killall" is also matched.

I tried playing with recexact to allow the exact match, "kill", to be
accepted.  But I'm obviously missing something.

I will play with it more as time permits tomorrow.

Thank you again Daniel.



-- 
Grant. . . .
unix || die


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 3717 bytes --]

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

* Re: Conditionally complete on space.
  2017-08-18  4:32   ` Grant Taylor
@ 2017-08-18  5:07     ` Daniel Shahaf
  2017-08-18 17:13       ` Grant Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Shahaf @ 2017-08-18  5:07 UTC (permalink / raw)
  To: Grant Taylor; +Cc: zsh-users

Grant Taylor wrote on Thu, 17 Aug 2017 22:32 -0600:
> 1)  I need to hit space an extra time after completion.  I.e. "sip   "
> results in "sipcalc  ".  -  It seems as if the second space is
> swallowed.  (I do not consider this a problem and am happy to live with it.

That doesn't seem to have anything to do with the widget:

    $ zsh -f
    % autoload compinit
    % compinit
    % sipcalc(){}
    % sipc<TAB><space>

results in BUFFER="sipcalc " with just one space.

> 2)  I seem to be stuck in a loop if I try to complete "kil" into "kill"
> because "killall" is also matched.

You can always <Ctrl+V><Space> to enter a literal space.

The REC_EXACT option works if you change expand-or-complete
to complete-word.  (In this case you may want to add _expand
to your 'completer' zstyle.)

Cheers,

Daniel


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

* Re: Conditionally complete on space.
  2017-08-18  5:07     ` Daniel Shahaf
@ 2017-08-18 17:13       ` Grant Taylor
  2017-08-18 17:19         ` Grant Taylor
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Taylor @ 2017-08-18 17:13 UTC (permalink / raw)
  To: zsh-users

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

On 08/17/2017 11:07 PM, Daniel Shahaf wrote:
> That doesn't seem to have anything to do with the widget:
> 
>      $ zsh -f
>      % autoload compinit
>      % compinit
>      % sipcalc(){}
>      % sipc<TAB><space>
> 
> results in BUFFER="sipcalc " with just one space.

Well I'll be....

I never noticed it before, but it sure is there.  -  I guess that means 
that I won't mind it henceforth either.  :-)

> You can always <Ctrl+V><Space> to enter a literal space.

Ya, I was doing that during my diagnostics.

My goal is to overload space so that I don't need to hit special keys.  ;-)

> The REC_EXACT option works if you change expand-or-complete
> to complete-word.  (In this case you may want to add _expand
> to your 'completer' zstyle.)

You've gotten me a LOT closer.  And some brute force hacking (less than 
gracefully) got me to what I think I want.

maybe-expand-or-complete() {
	if [[ $LBUFFER[-1] != ' ' && -z "$JUSTCOMPLETED" ]]; then
		zle expand-or-complete
		if [[ $LBUFFER[-1] != ' ' ]]; then
			JUSTCOMPLETED=true
		fi
	else
		unset JUSTCOMPLETED
		zle self-insert
	fi
}
zle -N maybe-expand-or-complete
bindkey ' ' maybe-expand-or-complete
setopt recexact

The key was to retain state between maybe-expand-or-complete() 
executions via JUSTCOMPLETED so as to not call expand-or-complete 
successively when there were multiple matches.

Thanks to your help Daniel, I believe I have something that I can start 
using and deal with any annoyances as they come up.  :-)



-- 
Grant. . . .
unix || die


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 3717 bytes --]

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

* Re: Conditionally complete on space.
  2017-08-18 17:13       ` Grant Taylor
@ 2017-08-18 17:19         ` Grant Taylor
  0 siblings, 0 replies; 6+ messages in thread
From: Grant Taylor @ 2017-08-18 17:19 UTC (permalink / raw)
  To: zsh-users

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

On 08/18/2017 11:13 AM, Grant Taylor wrote:
> Thanks to your help Daniel, I believe I have something that I can start 
> using and deal with any annoyances as they come up.  :-)

As sure as I sent the email, I stubbed my toe on something else.

"ipc" expands to ipcalc ipcmk ipcrm ipcs.  Adding "a" and hitting space 
would not complete ipcalc with my last script

Slightly tweaking things does:

maybe-expand-or-complete() {
	if [[ $LBUFFER[-1] != ' ' && "$JUSTCOMPLETED" != $LBUFFER ]]; then
		zle expand-or-complete
		if [[ $LBUFFER[-1] != ' ' ]]; then
			JUSTCOMPLETED=$LBUFFER
		fi
	else
		unset JUSTCOMPLETED
		zle self-insert
	fi
}
zle -N maybe-expand-or-complete
bindkey ' ' maybe-expand-or-complete
setopt recexact

This version checks to see if the last completion matches the current 
line buffer.  If they differ, expand-or-complete.

Thank you again Daniel.



-- 
Grant. . . .
unix || die


[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 3717 bytes --]

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

end of thread, other threads:[~2017-08-18 17:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-17 22:34 Conditionally complete on space Grant Taylor
2017-08-18  1:07 ` Daniel Shahaf
2017-08-18  4:32   ` Grant Taylor
2017-08-18  5:07     ` Daniel Shahaf
2017-08-18 17:13       ` Grant Taylor
2017-08-18 17:19         ` Grant Taylor

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