zsh-workers
 help / color / mirror / code / Atom feed
* shell script that replaces the command line
@ 2014-06-12 23:07 Dave Yost
  2014-06-13  6:40 ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Dave Yost @ 2014-06-12 23:07 UTC (permalink / raw)
  To: zsh-workers

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

Hi.

Can a shell function replace the command line contents? I know a widget can do it, but I can’t see a way for a shell function to do that.

The use case is my “d” command I’ve been using for years. It is a fancy shell function that runs a command in the background with all output to a file. Optionally, it can rerun the previously-backgrounded command again or echo it.

When I want to rerun a command, I want my d shell function to put the previously-backgrounded command in the BUFFER so I can edit it before rerunning it.

Thanks

Dave

0 Thu 13:33:58 DaveBook yost /Users/yost
201 Z% d c echo ha
[5] 4595
0 Thu 13:34:11 DaveBook yost /Users/yost
202 Z% 
[5]  + 4595 done       yostD_runToOutfileWithEpilogue
0 Thu 13:34:11 DaveBook yost /Users/yost
202 Z% cat out
=d= tbegin 2014-06-12T13:34:11 PDT Thu
=d= see    http://yost.com/computers/d/
=d= host   ip2
=d= uname  Darwin ip2 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
=d= path   /Users/yost/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin::
=d= wd     /Users/yost
=d= cmd    echo ha
=d==begin / - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \
ha
  0.01s user 0.02s system 143% cpu 0.024 total
=d==end   \ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - /
=d= status 0
=d= tend   2014-06-12T13:34:11 PDT Thu
========== =====================================================================
0 Thu 13:34:14 DaveBook yost /Users/yost
203 Z% d sp
echo ha
0 Thu 13:34:25 DaveBook yost /Users/yost
204 Z% d s
[ echo ha ]
[5] 4647
0 Thu 13:34:28 DaveBook yost /Users/yost
205 Z% 
[5]  + 4647 done       yostD_runToOutfileWithEpilogue
0 Thu 13:34:29 DaveBook yost /Users/yost
205 Z% 

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

* Re: shell script that replaces the command line
  2014-06-12 23:07 shell script that replaces the command line Dave Yost
@ 2014-06-13  6:40 ` Bart Schaefer
  2014-06-13  7:22   ` Dave Yost
  2014-06-13 21:34   ` Dave Yost
  0 siblings, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2014-06-13  6:40 UTC (permalink / raw)
  To: Dave Yost, zsh-workers

On Jun 12,  4:07pm, Dave Yost wrote:
} Subject: shell script that replaces the command line
}
} Can a shell function replace the command line contents? I know a
} widget can do it, but I can't see a way for a shell function to do
} that.

The "print -z ..." command loads the buffer stack, which is then popped
into the editor buffer at the next prompt (e.g., after the calling
function/script exits).


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

* Re: shell script that replaces the command line
  2014-06-13  6:40 ` Bart Schaefer
@ 2014-06-13  7:22   ` Dave Yost
  2014-06-13 21:34   ` Dave Yost
  1 sibling, 0 replies; 7+ messages in thread
From: Dave Yost @ 2014-06-13  7:22 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

It would be good if "buffer stack" were in the index of the doc, and it would be good if the PDF form of the doc had internal hyperlinks.

On Thu 2014-06-12, at 11:40 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Jun 12,  4:07pm, Dave Yost wrote:
> } Subject: shell script that replaces the command line
> }
> } Can a shell function replace the command line contents? I know a
> } widget can do it, but I can't see a way for a shell function to do
> } that.
> 
> The "print -z ..." command loads the buffer stack, which is then popped
> into the editor buffer at the next prompt (e.g., after the calling
> function/script exits).


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

* Re: shell script that replaces the command line
  2014-06-13  6:40 ` Bart Schaefer
  2014-06-13  7:22   ` Dave Yost
@ 2014-06-13 21:34   ` Dave Yost
  2014-06-13 22:02     ` Bart Schaefer
  2014-06-14  9:45     ` Mikael Magnusson
  1 sibling, 2 replies; 7+ messages in thread
From: Dave Yost @ 2014-06-13 21:34 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

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

Thanks.

This is OK,
0 Fri 0:08:54 DaveBook yost /Users/yost
215 Z% print -z echo foo
0 Fri 0:09:33 DaveBook yost /Users/yost
216 Z% echo foo

but there must be a way to replace the current command line rather than setting the contents of the next one, yes?

On Thu 2014-06-12, at 11:40 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Jun 12,  4:07pm, Dave Yost wrote:
> } Subject: shell script that replaces the command line
> }
> } Can a shell function replace the command line contents? I know a
> } widget can do it, but I can't see a way for a shell function to do
> } that.
> 
> The "print -z ..." command loads the buffer stack, which is then popped
> into the editor buffer at the next prompt (e.g., after the calling
> function/script exits).


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

* Re: shell script that replaces the command line
  2014-06-13 21:34   ` Dave Yost
@ 2014-06-13 22:02     ` Bart Schaefer
  2014-06-17  4:31       ` Dave Yost
  2014-06-14  9:45     ` Mikael Magnusson
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-06-13 22:02 UTC (permalink / raw)
  To: Dave Yost; +Cc: zsh-workers

On Jun 13,  2:34pm, Dave Yost wrote:
}
} but there must be a way to replace the current command line rather
} than setting the contents of the next one, yes?

Not from outside a widget, no.  Only widget functions have access to
the current editor state.

But I'm not clear on why you need to do so?  In your original example,
the "d" function would just "print -z previously-backgrounded-command"
and then exit, at which point a new prompt would appear and pop the
buffer stack so you'd have "previously-backgrounded-command" sitting
there ready to edit.

What part of the flow am I not seeing?

If what you want is for "d" to present a line for editing, wait for the
editor, and then continue execution after the editor returns, then what
you want is to call the "vared" command from "d".

example_d() {
  local foo="echo this is the command to edit"
  vared -e -h -p "Do your editing: " foo && eval $foo
}

There are a bunch of options to vared to manipulate the state in which
the editor starts/runs.


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

* Re: shell script that replaces the command line
  2014-06-13 21:34   ` Dave Yost
  2014-06-13 22:02     ` Bart Schaefer
@ 2014-06-14  9:45     ` Mikael Magnusson
  1 sibling, 0 replies; 7+ messages in thread
From: Mikael Magnusson @ 2014-06-14  9:45 UTC (permalink / raw)
  To: Dave Yost; +Cc: Bart Schaefer, zsh workers

On 13 June 2014 23:34, Dave Yost <Dave@yost.com> wrote:
> Thanks.
>
> This is OK,
> 0 Fri 0:08:54 DaveBook yost /Users/yost
> 215 Z% print -z echo foo
> 0 Fri 0:09:33 DaveBook yost /Users/yost
> 216 Z% echo foo
>
> but there must be a way to replace the current command line rather than setting the contents of the next one, yes?

You can most likely use zle -F to do what you want, but it would be
somewhat messy.

-- 
Mikael Magnusson


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

* Re: shell script that replaces the command line
  2014-06-13 22:02     ` Bart Schaefer
@ 2014-06-17  4:31       ` Dave Yost
  0 siblings, 0 replies; 7+ messages in thread
From: Dave Yost @ 2014-06-17  4:31 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

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

Thanks.

vared will do.

http://yost.com/computers/d

Dave

On Fri 2014-06-13, at 03:02 PM, Bart Schaefer <schaefer@brasslantern.com> wrote:

> On Jun 13,  2:34pm, Dave Yost wrote:
> }
> } but there must be a way to replace the current command line rather
> } than setting the contents of the next one, yes?
> 
> Not from outside a widget, no.  Only widget functions have access to
> the current editor state.
> 
> But I'm not clear on why you need to do so?  In your original example,
> the "d" function would just "print -z previously-backgrounded-command"
> and then exit, at which point a new prompt would appear and pop the
> buffer stack so you'd have "previously-backgrounded-command" sitting
> there ready to edit.
> 
> What part of the flow am I not seeing?
> 
> If what you want is for "d" to present a line for editing, wait for the
> editor, and then continue execution after the editor returns, then what
> you want is to call the "vared" command from "d".
> 
> example_d() {
>  local foo="echo this is the command to edit"
>  vared -e -h -p "Do your editing: " foo && eval $foo
> }
> 
> There are a bunch of options to vared to manipulate the state in which
> the editor starts/runs.


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

end of thread, other threads:[~2014-06-17  4:31 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-12 23:07 shell script that replaces the command line Dave Yost
2014-06-13  6:40 ` Bart Schaefer
2014-06-13  7:22   ` Dave Yost
2014-06-13 21:34   ` Dave Yost
2014-06-13 22:02     ` Bart Schaefer
2014-06-17  4:31       ` Dave Yost
2014-06-14  9:45     ` Mikael Magnusson

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