zsh-users
 help / color / mirror / code / Atom feed
* Re: accept-line question
       [not found] ` <E00A4521-8A4E-486C-BB2E-5D8D80639156@yost.com>
@ 2015-04-02 20:58   ` Mikael Magnusson
  2015-04-02 21:30     ` Bart Schaefer
  2015-04-02 21:50   ` Dave Yost
  1 sibling, 1 reply; 6+ messages in thread
From: Mikael Magnusson @ 2015-04-02 20:58 UTC (permalink / raw)
  To: Dave Yost; +Cc: Zsh Users

On Thu, Apr 2, 2015 at 7:12 PM, Dave Yost <Dave@yost.com> wrote:
> Sorry, that email was flawed. Let me try again.
>
> Below, after sourcing a script, I type ^a then ^b.
> The ^a works as expected, but not the ^b
>
> There must be something more I have to do besides “accept-line”. But what?
>
> 0 Thu 9:59:38 yost DaveBook ~
> 206 Z% cat accept-line-test.zsh
> function xxx {
>   BUFFER="$1"
>   zle -R
>   zle accept-line
> }
>
> function x1 {
>   xxx echo\ 1
> }
>
> function x2 {
>   xxx echo\ 2
>   xxx echo\ 3
> }
>
> zle -N x1
> zle -N x2
>
> bindkey ^a x1
> bindkey ^b x2
> 0 Thu 9:59:47 yost DaveBook ~
> 207 Z% source accept-line-test.zsh
> 0 Thu 9:59:54 yost DaveBook ~
> 208 Z% echo 1
> 1
> 0 Thu 9:59:55 yost DaveBook ~
> 209 Z% echo 3
> 3
> 0 Thu 9:59:59 yost DaveBook ~
> 210 Z%
>
> The output I want from ^b is
>
> 0 Wed 0:30:03 yost DaveBook ~
> 240 Z% echo 2
> 2
> 0 Wed 0:30:04 yost DaveBook ~
> 241 Z% echo 3
> 3
> 0 Wed 0:30:04 yost DaveBook ~
> 242 Z%
>
> Extra credit:
>
> Is there a way to write a function x3 that calls x2, such that I can issue x3 from the command line get the result above instead of the result below?
>
> 0 Thu 10:04:32 yost DaveBook ~
> 216 Z% x2
> xxx:zle:3: widgets can only be called when ZLE is active
> xxx:zle:3: widgets can only be called when ZLE is active
> 1 Thu 10:05:55 yost DaveBook ~
> 217 Z%

AFAICT, Bart's answer still applies to the above, so just read that again.

-- 
Mikael Magnusson


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

* Re: accept-line question
  2015-04-02 20:58   ` Mikael Magnusson
@ 2015-04-02 21:30     ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2015-04-02 21:30 UTC (permalink / raw)
  To: Zsh Users

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

On Apr 2, 2015 1:59 PM, "Mikael Magnusson" <mikachu@gmail.com> wrote:
>
> AFAICT, Bart's answer still applies to the above, so just read that again.

Yes, except please mentally correct my "now how" typo to "NOT how" ...

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

* Re: accept-line question
       [not found] ` <E00A4521-8A4E-486C-BB2E-5D8D80639156@yost.com>
  2015-04-02 20:58   ` Mikael Magnusson
@ 2015-04-02 21:50   ` Dave Yost
  2015-04-02 22:40     ` Bart Schaefer
  1 sibling, 1 reply; 6+ messages in thread
From: Dave Yost @ 2015-04-02 21:50 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

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

That works.

I do want the executed command to be inserted into history.

I searched the manual in vain for a way to do that. Any chance you could illuminate further?

Thanks

Dave

> Bart Schaefer wrote:
> 
> That's now how it works; accept-line in x1 does not run the command and
> then resume ZLE inside the x2 function, and even if it did it would not
> reprint the prompt in that way.
> 
> All "accept-line" does is assert that the buffer is now in its final
> state, so that after ZLE finishes, the regular shell parser should read
> that line as the input.  However, ZLE does not finish until the current
> active widget has also finished, so if accept-line is run again it can
> still change the previously-accepted buffer.
> 
> You'd need something like this:
> 
> --- 8< ---
> execute-now() {
>   zle -I
>   eval "$BUFFER"
>   BUFFER=
>   zle -R
> }
> 
> zle -N execute-now
> --- 8< ---
> 
> Then replace any "zle accept-line" with "zle execute-now".
> 
> However, *that* does not push the buffer onto the history stack, so even
> more may be necessary depending on what you intend to accomplish.
> 
> -- 
> Barton E. Schaefer
> 
> 
>> On 2015-04-02, at 10:12 AM, Dave Yost <Dave@Yost.com> wrote:
>> 
>> Sorry, that email was flawed. Let me try again.
>> 
>> Below, after sourcing a script, I type ^a then ^b.
>> The ^a works as expected, but not the ^b
>> 
>> There must be something more I have to do besides “accept-line”. But what?
>> 
>> 0 Thu 9:59:38 yost DaveBook ~
>> 206 Z% cat accept-line-test.zsh 
>> function xxx {
>>   BUFFER="$1"
>>   zle -R
>>   zle accept-line
>> }
>> 
>> function x1 {
>>   xxx echo\ 1
>> }
>> 
>> function x2 {
>>   xxx echo\ 2
>>   xxx echo\ 3
>> }
>> 
>> zle -N x1
>> zle -N x2
>> 
>> bindkey ^a x1
>> bindkey ^b x2
>> 0 Thu 9:59:47 yost DaveBook ~
>> 207 Z% source accept-line-test.zsh
>> 0 Thu 9:59:54 yost DaveBook ~
>> 208 Z% echo 1
>> 1
>> 0 Thu 9:59:55 yost DaveBook ~
>> 209 Z% echo 3
>> 3
>> 0 Thu 9:59:59 yost DaveBook ~
>> 210 Z% 
>> 
>> The output I want from ^b is
>> 
>> 0 Wed 0:30:03 yost DaveBook ~
>> 240 Z% echo 2
>> 2
>> 0 Wed 0:30:04 yost DaveBook ~
>> 241 Z% echo 3
>> 3
>> 0 Wed 0:30:04 yost DaveBook ~
>> 242 Z% 
>> 
>> Extra credit:
>> 
>> Is there a way to write a function x3 that calls x2, such that I can issue x3 from the command line get the result above instead of the result below?
>> 
>> 0 Thu 10:04:32 yost DaveBook ~
>> 216 Z% x2
>> xxx:zle:3: widgets can only be called when ZLE is active
>> xxx:zle:3: widgets can only be called when ZLE is active
>> 1 Thu 10:05:55 yost DaveBook ~
>> 217 Z% 
>> 
>> Thanks
>> 
> 

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

* Re: accept-line question
  2015-04-02 21:50   ` Dave Yost
@ 2015-04-02 22:40     ` Bart Schaefer
  2015-04-02 23:19       ` Mikael Magnusson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2015-04-02 22:40 UTC (permalink / raw)
  To: Zsh Users

On Thu, Apr 2, 2015 at 2:50 PM, Dave Yost <Dave@yost.com> wrote:
> That works.
>
> I do want the executed command to be inserted into history.
>
> I searched the manual in vain for a way to do that. Any chance you could
> illuminate further?

Try this:

--- 8< ---
execute-now() {
  zle -I
  print -S "$BUFFER"
  eval "$BUFFER"
  BUFFER=
  zle -R
}
--- 8< ---

Remaining glitches will be that $? / $pipestatus won't be correct when
redrawing the prompt or executing a subsequent command.  I can't
immediately think of a workaround.


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

* Re: accept-line question
  2015-04-02 22:40     ` Bart Schaefer
@ 2015-04-02 23:19       ` Mikael Magnusson
  0 siblings, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2015-04-02 23:19 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Users

(don't write your reply up here)
On Fri, Apr 3, 2015 at 12:40 AM, Bart Schaefer
<schaefer@brasslantern.com> wrote:
> On Thu, Apr 2, 2015 at 2:50 PM, Dave Yost <Dave@yost.com> wrote:
>> That works.
>>
>> I do want the executed command to be inserted into history.
>>
>> I searched the manual in vain for a way to do that. Any chance you could
>> illuminate further?
>
> Try this:
>
> --- 8< ---
> execute-now() {
>   zle -I
>   print -S "$BUFFER"
>   eval "$BUFFER"
>   BUFFER=
>   zle -R
> }
> --- 8< ---
>
> Remaining glitches will be that $? / $pipestatus won't be correct when
> redrawing the prompt or executing a subsequent command.  I can't
> immediately think of a workaround.

This mostly works, the only caveat is that the commands are executed
in the reverse order:
xxx() {
 zle push-input
 BUFFER="$1"
 zle -U $'\n'
}
x1() { xxx 'echo 1' }
x2() { xxx 'echo 2'; xxx 'echo 3' }
zle -N x{1,2}

Executing x2 will print 3 and then 2 because of how stacks work.

-- 
Mikael Magnusson


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

* Re: accept-line question
       [not found] <FF62FDC6-BDD8-4BF3-AD44-F36E834D3B4F@yost.com>
@ 2015-04-01 15:39 ` Bart Schaefer
       [not found] ` <E00A4521-8A4E-486C-BB2E-5D8D80639156@yost.com>
  1 sibling, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2015-04-01 15:39 UTC (permalink / raw)
  To: zsh-users

[redirecting to zsh-users as others may find this interesting]

On Apr 1, 12:36am, Dave Yost wrote:
} 
} There must be something more I have to do besides "accept-line". But what?
} 
} I type control-a after sourcing my script, and it doesn't work as I expect.
} 
} 0 Wed 0:29:51 yost DaveBook ~
} 238 Z% cat accept-line-test.zsh
} function x1 {
}   BUFFER="$1"
}   zle -R
}   zle accept-line
} }
} 
} function x2 {
}   x1 echo\ 1
}   x2 echo\ 2
} }
} 
} zle -N xxx
} 
} bindkey ^a xxx

There is something missing here, because "zle -N xxx" creates a widget
that calls the function "xxx", but you don't show a function named that.

Also x2 as shown is calling itself recursively, which will eventually
die of stack overflow.

} The output I want is
} 
} 0 Wed 0:30:03 yost DaveBook ~
} 240 Z% echo 1
} 1
} 0 Wed 0:30:04 yost DaveBook ~
} 241 Z% echo 2
} 2
} 0 Wed 0:30:04 yost DaveBook ~
} 242 Z% 

That's now how it works; accept-line in x1 does not run the command and
then resume ZLE inside the x2 function, and even if it did it would not
reprint the prompt in that way.

All "accept-line" does is assert that the buffer is now in its final
state, so that after ZLE finishes, the regular shell parser should read
that line as the input.  However, ZLE does not finish until the current
active widget has also finished, so if accept-line is run again it can
still change the previously-accepted buffer.

You'd need something like this:

--- 8< ---
execute-now() {
  zle -I
  eval "$BUFFER"
  BUFFER=
  zle -R
}

zle -N execute-now
--- 8< ---

Then replace any "zle accept-line" with "zle execute-now".

However, *that* does not push the buffer onto the history stack, so even
more may be necessary depending on what you intend to accomplish.

-- 
Barton E. Schaefer


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

end of thread, other threads:[~2015-04-02 23:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <FF62FDC6-BDD8-4BF3-AD44-F36E834D3B4F@yost.com>
2015-04-01 15:39 ` accept-line question Bart Schaefer
     [not found] ` <E00A4521-8A4E-486C-BB2E-5D8D80639156@yost.com>
2015-04-02 20:58   ` Mikael Magnusson
2015-04-02 21:30     ` Bart Schaefer
2015-04-02 21:50   ` Dave Yost
2015-04-02 22:40     ` Bart Schaefer
2015-04-02 23:19       ` 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).