zsh-workers
 help / color / mirror / code / Atom feed
* What's the last command in prompt expansion?
@ 2009-12-14 22:53 Jörg Sommer
  2009-12-15 10:02 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Jörg Sommer @ 2009-12-14 22:53 UTC (permalink / raw)
  To: zsh-workers

Hi,

what should be the expansion of %? in the prompt, the return code of the
last command run at the prompt or the return code of the last command run
by zsh?

I've a function triggered via TRAPALARM that modifies PS1, by resetting
its value and running zle reset-prompt. After the first time this
function was executed the return value isn't the one of the last command,
e.g. it's not 1 after executing false.

Do I have to save the return value and put the value into PS1?

Regards, Jörg.
-- 
“Politics is for the moment, equations are forever”
            (Albert Einstein)


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

* Re: What's the last command in prompt expansion?
  2009-12-14 22:53 What's the last command in prompt expansion? Jörg Sommer
@ 2009-12-15 10:02 ` Peter Stephenson
  2009-12-15 10:23   ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2009-12-15 10:02 UTC (permalink / raw)
  To: zsh-workers

=?UTF-8?Q?J=C3=B6rg?= Sommer wrote:
> what should be the expansion of %? in the prompt, the return code of the
> last command run at the prompt or the return code of the last command run
> by zsh?

Neither, necessarily, it's more complicated than that.  Most
encapsulated commands---precmd, chpwd etc. etc., plus anything in
zle---save and restore the status.  However, anything considered by the
shell to be run as part of the main execution thread (a phrase I
deliberately haven't attempted to define in detail) will set the status
directly.

> I've a function triggered via TRAPALARM that modifies PS1, by resetting
> its value and running zle reset-prompt. After the first time this
> function was executed the return value isn't the one of the last command,
> e.g. it's not 1 after executing false.
> 
> Do I have to save the return value and put the value into PS1?

Traps don't save and restore the status.  This is deliberate, they are
supposed to be able to return a status to the surrounding environment.

Saving the status in TRAPALRM might be good enough for your purposes.

TRAPALRM() {
   integer stat=$?
   # ....
   return $stat
}

However, that might have side effects in some cases, since return values
from traps are special; I'd have to look in more detail to decide if
this is important with TRAPALRM, it may not be.  Also, since TRAPALRM is
usually executed at the command prompt maybe this isn't important.  (I
presume you're trying to trap keyboard inactivity so there isn't a
simple way of doing this with, say, the periodic function.)

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: What's the last command in prompt expansion?
  2009-12-15 10:02 ` Peter Stephenson
@ 2009-12-15 10:23   ` Peter Stephenson
  2009-12-18 16:45     ` Jörg Sommer
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2009-12-15 10:23 UTC (permalink / raw)
  To: zsh-workers

On Tue, 15 Dec 2009 10:02:31 +0000
Peter Stephenson <pws@csr.com> wrote:
> Traps don't save and restore the status.  This is deliberate, they are
> supposed to be able to return a status to the surrounding environment.

Actually, I've overstated the case here.  If a trap function doesn't
execute a "return" explicitly, the status should be maintained unless there
was an error.  So in general your TRAPALRM ought not to change it.

-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: What's the last command in prompt expansion?
  2009-12-15 10:23   ` Peter Stephenson
@ 2009-12-18 16:45     ` Jörg Sommer
  0 siblings, 0 replies; 4+ messages in thread
From: Jörg Sommer @ 2009-12-18 16:45 UTC (permalink / raw)
  To: zsh-workers

Hallo Peter,

Peter Stephenson <pws@csr.com> wrote:
> On Tue, 15 Dec 2009 10:02:31 +0000
> Peter Stephenson <pws@csr.com> wrote:
>> Traps don't save and restore the status.  This is deliberate, they are
>> supposed to be able to return a status to the surrounding environment.
>
> Actually, I've overstated the case here.  If a trap function doesn't
> execute a "return" explicitly, the status should be maintained unless there
> was an error.  So in general your TRAPALRM ought not to change it.

The problem is not the return value of TRAPALRM, but the change of PS1.

% zsh -f
ibook% PS1='%? %% '
0 % TRAPALRM() { PS1=$PS1; zle reset-prompt; TMOUT=0; }
0 % TMOUT=1; false
0 %

Bye, Jörg.
-- 
Es ist außerdem ein weit verbreiteter Irrtum das USENET ‚helfen‘ soll.
Tatsächlich wurde USENET nachweislich zur persönlichen Belustigung
seiner Erfinder geschaffen.
Jörg Klemenz <joerg@gmx.net>, <b4ai4o$1u8vmt$2@ID-21915.news.dfncis.de>


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

end of thread, other threads:[~2009-12-18 23:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-14 22:53 What's the last command in prompt expansion? Jörg Sommer
2009-12-15 10:02 ` Peter Stephenson
2009-12-15 10:23   ` Peter Stephenson
2009-12-18 16:45     ` Jörg Sommer

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