zsh-users
 help / color / mirror / code / Atom feed
* Howto to print ENTER to zle-buffer?
@ 2016-12-13 15:01 Andy Spiegl
  2016-12-13 16:12 ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Spiegl @ 2016-12-13 15:01 UTC (permalink / raw)
  To: zsh-users

Hi, I want to run a command put something like this at the end of my .zshrc:

  print -z "command-to-run args"
  zle accept-line

But I get the (understandable) error:
 /home/andy/.zshrc:zle:822: widgets can only be called when ZLE is active

Can zsh be told somehow to "press enter"?

Thanks a lot,
 Andy


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-13 15:01 Howto to print ENTER to zle-buffer? Andy Spiegl
@ 2016-12-13 16:12 ` Bart Schaefer
  2016-12-13 16:47   ` Andy Spiegl
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2016-12-13 16:12 UTC (permalink / raw)
  To: zsh-users; +Cc: Andy Spiegl

On Dec 13,  4:01pm, Andy Spiegl wrote:
}
}   print -z "command-to-run args"
}   zle accept-line
} 
} Can zsh be told somehow to "press enter"?

If you're doing this in a startup file, you probably want

    print -S "command-to-run args"
    command-to-run args

That is, explicitly place the command into the history, and then run
it directly.

If for some reason you really need the command executed within ZLE,
you will have to create a zle-line-init widget that will accept the
line and then remove or update itself that so it doesn't run it a
second time.


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-13 16:12 ` Bart Schaefer
@ 2016-12-13 16:47   ` Andy Spiegl
  2016-12-13 17:30     ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Spiegl @ 2016-12-13 16:47 UTC (permalink / raw)
  To: zsh-users

> If for some reason you really need the command executed within ZLE,
Actually, yes.  It's for sort of a session-management I'm trying to write.

> you will have to create a zle-line-init widget that will accept the
> line and then remove or update itself that so it doesn't run it a
> second time.
Uffda, sounds complicated.  Could you help me with that a bit, please?

Thanks so much!
 Andy

-- 
 If riding in an airplane is flying, then riding in a boat is swimming.


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-13 16:47   ` Andy Spiegl
@ 2016-12-13 17:30     ` Bart Schaefer
  2016-12-13 19:29       ` Andy Spiegl
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2016-12-13 17:30 UTC (permalink / raw)
  To: zsh-users

On Dec 13,  5:47pm, Andy Spiegl wrote:
}
} > If for some reason you really need the command executed within ZLE,
} Actually, yes.  It's for sort of a session-management I'm trying to write.

Usually better if you say stuff like this up front, it helps to have
context when trying to answer a question!

Given this -- and assuming you can target zsh-5.3 -- I'd go with:

autoload -Uz add-zle-hook-widget
session-startup {
  command-to-run args	# your thing here
  add-zle-hook-widget -d line-init $WIDGET
}
add-zle-hook-widget line-init session-startup


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-13 17:30     ` Bart Schaefer
@ 2016-12-13 19:29       ` Andy Spiegl
  2016-12-14  1:03         ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Spiegl @ 2016-12-13 19:29 UTC (permalink / raw)
  To: zsh-users

> Usually better if you say stuff like this up front, it helps to have
> context when trying to answer a question!
You are right, sorry.  I had thought about it but then didn't want to
clutter up my (presumably) simple question with too much side information.

> Given this -- and assuming you can target zsh-5.3 -- I'd go with:
I'm still stuck with 5.2.  Guess I have to wait a while then. :-(

> autoload -Uz add-zle-hook-widget
> session-startup {
>   command-to-run args	# your thing here
>   add-zle-hook-widget -d line-init $WIDGET
> }
> add-zle-hook-widget line-init session-startup
Looks very smart but I'm not sure I understand how this works.
You define the function "session-startup" which is then hooked
to zle such that it is run after initialization?  How does it show
up in the zle buffer then?  No "print -z" anymore?

Thanks!
 Andy


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-13 19:29       ` Andy Spiegl
@ 2016-12-14  1:03         ` Bart Schaefer
  2016-12-15 18:20           ` Andy Spiegl
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2016-12-14  1:03 UTC (permalink / raw)
  To: zsh-users

On Dec 13,  8:29pm, Andy Spiegl wrote:
} Subject: Re: Howto to print ENTER to zle-buffer?
}
} > Given this -- and assuming you can target zsh-5.3 -- I'd go with:
} I'm still stuck with 5.2.  Guess I have to wait a while then. :-(

No, it can still be done; you'll just have to be sure that your
zle-line-init widget doesn't conflict with one the user already has.

Actually if you grab Functions/Misc/add-zle-hook-widget from 5.3 and
make it part of your session manager, it should work in 5.2.

} You define the function "session-startup"

Which I typo'd, I left out the () between the function name and the
open brace ...

} which is then hooked
} to zle such that it is run after initialization?  How does it show
} up in the zle buffer then?  No "print -z" anymore?

I won't show up in the ZLE buffer, instead it will just execute at
the time zle starts up.  I don't know why you'd need it to appear in
the buffer if it's just going to be accept-line'd immediately, but
you can do that like this:

session-startup() {
  zle -U $'command-to-run args\n'
  add-zle-hook-widget -d line-init $WIDGET
}

To "backport" that to 5.2 without add-zle-hook-widget:

zle-line-init() {
  if [[ -n $functions[session-startup] ]]
  then
    session-startup
    unfunction session-startup
  fi
  # Do whatever the user's zle-line-init did
}
zle -N zle-line-init

session-startup() {
  zle -U $'command-to-run args\n'
}


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-14  1:03         ` Bart Schaefer
@ 2016-12-15 18:20           ` Andy Spiegl
  2016-12-16  2:29             ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Andy Spiegl @ 2016-12-15 18:20 UTC (permalink / raw)
  To: zsh-users

Hi Bart,

> No, it can still be done; you'll just have to be sure that your
> zle-line-init widget doesn't conflict with one the user already has.

You are the BEST!  So cool, now it works just as I want it.
Thanks so much!!!

> I don't know why you'd need it to appear in the buffer if it's just
> going to be accept-line'd immediately, ...
Because I want these shells to look exactly the way it looked before it
was closed.  Sometimes keep shells open as a reminder - with the last
executed command visible.

Thanks a lot!
 Andy


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

* Re: Howto to print ENTER to zle-buffer?
  2016-12-15 18:20           ` Andy Spiegl
@ 2016-12-16  2:29             ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2016-12-16  2:29 UTC (permalink / raw)
  To: zsh-users

On Dec 15,  7:20pm, Andy Spiegl wrote:
}
} > I don't know why you'd need it to appear in the buffer if it's just
} > going to be accept-line'd immediately, ...
} Because I want these shells to look exactly the way it looked before it
} was closed.  Sometimes keep shells open as a reminder - with the last
} executed command visible.

Hmm, be careful with that -- if the last executed command was something
destructive, like "rm" or an output redirection, re-executing it might
clobber the wrong thing.


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

end of thread, other threads:[~2016-12-16  2:28 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-12-13 15:01 Howto to print ENTER to zle-buffer? Andy Spiegl
2016-12-13 16:12 ` Bart Schaefer
2016-12-13 16:47   ` Andy Spiegl
2016-12-13 17:30     ` Bart Schaefer
2016-12-13 19:29       ` Andy Spiegl
2016-12-14  1:03         ` Bart Schaefer
2016-12-15 18:20           ` Andy Spiegl
2016-12-16  2:29             ` 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).