zsh-users
 help / color / mirror / code / Atom feed
* pre-populate zle for next command?
@ 2017-08-31 17:38 Stephen Talley
  2017-08-31 19:33 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Talley @ 2017-08-31 17:38 UTC (permalink / raw)
  To: zsh-users

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

Is there any way for a command to pre-populate the the zle for the next
command?

Suppose I have, for example, a zsh function "buildcmd" that produces a
command line (based on supplied arguments, say) that the user would then be
able to edit in the zle before hitting enter to accept the line and execute
it.  The flow would be:

    % buildcmd --my --args<enter>
    % <output_from_buildcmd>

I know I could just do:

    % `buildcmd --my --args`<tab>

to achieve the same thing, but it's a bit more tedious than I'd like.

Ideally there'd be some hook (precmd?  accept-line?) that could check a
variable and pre-populate the zle:

  buildcmd() {
    zle_prepopulate="some command to edit"
  }

  precmd() {
    if [ -n "$zle_prepopulate" ]
    then
      zle -U "$zle_prepopulate"
    fi
  }

...but of course this doesn't quite work because the call to zle is not in
the context of a widget.

Is there a way?

Thanks!

Steve

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

* Re: pre-populate zle for next command?
  2017-08-31 17:38 pre-populate zle for next command? Stephen Talley
@ 2017-08-31 19:33 ` Mikael Magnusson
  2017-09-01  0:22   ` Stephen Talley
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2017-08-31 19:33 UTC (permalink / raw)
  To: Stephen Talley; +Cc: Zsh Users

On Thu, Aug 31, 2017 at 7:38 PM, Stephen Talley
<stephentalley@nerdysoftware.com> wrote:
> Is there any way for a command to pre-populate the the zle for the next
> command?
>
> Suppose I have, for example, a zsh function "buildcmd" that produces a
> command line (based on supplied arguments, say) that the user would then be
> able to edit in the zle before hitting enter to accept the line and execute
> it.  The flow would be:
>
>     % buildcmd --my --args<enter>
>     % <output_from_buildcmd>
>
> I know I could just do:
>
>     % `buildcmd --my --args`<tab>
>
> to achieve the same thing, but it's a bit more tedious than I'd like.
>
> Ideally there'd be some hook (precmd?  accept-line?) that could check a
> variable and pre-populate the zle:
>
>   buildcmd() {
>     zle_prepopulate="some command to edit"
>   }
>
>   precmd() {
>     if [ -n "$zle_prepopulate" ]
>     then
>       zle -U "$zle_prepopulate"
>     fi
>   }
>
> ...but of course this doesn't quite work because the call to zle is not in
> the context of a widget.
>
> Is there a way?

You can use print -z to push any string you like on the zle editor
stack, which effectively does what you want. (Ie, it is popped when
the next command line is to be entered, which is immediately). There's
no need to involve precmd or any hook, you can just call print -z
directly from your function.

-- 
Mikael Magnusson


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

* Re: pre-populate zle for next command?
  2017-08-31 19:33 ` Mikael Magnusson
@ 2017-09-01  0:22   ` Stephen Talley
  0 siblings, 0 replies; 3+ messages in thread
From: Stephen Talley @ 2017-09-01  0:22 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

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

Oh, excellent.  I knew there had to be an easy way to do this.

Thank you!

On 31 August 2017 at 15:33, Mikael Magnusson <mikachu@gmail.com> wrote:

> On Thu, Aug 31, 2017 at 7:38 PM, Stephen Talley
> <stephentalley@nerdysoftware.com> wrote:
> > Is there any way for a command to pre-populate the the zle for the next
> > command?
> >
> > Suppose I have, for example, a zsh function "buildcmd" that produces a
> > command line (based on supplied arguments, say) that the user would then
> be
> > able to edit in the zle before hitting enter to accept the line and
> execute
> > it.  The flow would be:
> >
> >     % buildcmd --my --args<enter>
> >     % <output_from_buildcmd>
> >
> > I know I could just do:
> >
> >     % `buildcmd --my --args`<tab>
> >
> > to achieve the same thing, but it's a bit more tedious than I'd like.
> >
> > Ideally there'd be some hook (precmd?  accept-line?) that could check a
> > variable and pre-populate the zle:
> >
> >   buildcmd() {
> >     zle_prepopulate="some command to edit"
> >   }
> >
> >   precmd() {
> >     if [ -n "$zle_prepopulate" ]
> >     then
> >       zle -U "$zle_prepopulate"
> >     fi
> >   }
> >
> > ...but of course this doesn't quite work because the call to zle is not
> in
> > the context of a widget.
> >
> > Is there a way?
>
> You can use print -z to push any string you like on the zle editor
> stack, which effectively does what you want. (Ie, it is popped when
> the next command line is to be entered, which is immediately). There's
> no need to involve precmd or any hook, you can just call print -z
> directly from your function.
>
> --
> Mikael Magnusson
>

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

end of thread, other threads:[~2017-09-01  0:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-31 17:38 pre-populate zle for next command? Stephen Talley
2017-08-31 19:33 ` Mikael Magnusson
2017-09-01  0:22   ` Stephen Talley

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