zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: $ZLE_LINE_ABORTED
@ 2010-09-07 10:44 Peter Stephenson
  2010-09-07 11:46 ` Nikolai Weibull
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Stephenson @ 2010-09-07 10:44 UTC (permalink / raw)
  To: Zsh Hackers' List

Here's what I think is a useful one-line change.  If there's an error in
the line editor, including ^G, the line being edited gets copied to the
variable ZLE_LINE_ABORTED.  I'm forever aborting what I'm doing and
realising I need something on the line after all.

You can do this:

recover-line() {
  LBUFFER=$ZLE_LINE_ABORTED
  RBUFFER=
}
zle -N recover-line

Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.60
diff -p -u -r1.60 params.yo
--- Doc/Zsh/params.yo	14 Jun 2010 11:58:26 -0000	1.60
+++ Doc/Zsh/params.yo	7 Sep 2010 10:40:43 -0000
@@ -1448,6 +1448,13 @@ item(tt(ZDOTDIR))(
 The directory to search for shell startup files (.zshrc, etc),
 if not tt($HOME).
 )
+vindex(ZLE_LINE_ABORTED)
+item(tt(ZLE_LINE_ABORTED))(
+This parameter is set by the line editor when an error occurs.  It
+contains the line that was being edited at the point of the error.
+`tt(print -zr -- $ZLE_LINE_ABORTED)' can be used to recover the line.
+Only the most recent line of this kind is remembered.
+)
 vindex(ZLE_REMOVE_SUFFIX_CHARS)
 vindex(ZLE_SPACE_SUFFIX_CHARS)
 xitem(tt(ZLE_REMOVE_SUFFIX_CHARS))
Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.87
diff -p -u -r1.87 zle.yo
--- Doc/Zsh/zle.yo	6 Sep 2010 08:50:05 -0000	1.87
+++ Doc/Zsh/zle.yo	7 Sep 2010 10:40:43 -0000
@@ -2057,7 +2057,8 @@ tindex(send-break)
 item(tt(send-break) (^G ESC-^G) (unbound) (unbound))(
 Abort the current editor function, e.g. tt(execute-named-command), or the
 editor itself, e.g. if you are in tt(vared). Otherwise abort the parsing of
-the current line.
+the current line.  The aborted line is available in the shell variable
+tt(ZLE_LINE_ABORTED).
 )
 tindex(run-help)
 item(tt(run-help) (ESC-H ESC-h) (unbound) (unbound))(
Index: Src/Zle/zle_utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_utils.c,v
retrieving revision 1.56
diff -p -u -r1.56 zle_utils.c
--- Src/Zle/zle_utils.c	22 Mar 2010 19:49:07 -0000	1.56
+++ Src/Zle/zle_utils.c	7 Sep 2010 10:40:43 -0000
@@ -967,6 +967,7 @@ showmsg(char const *msg)
 int
 handlefeep(UNUSED(char **args))
 {
+    setsparam("ZLE_LINE_ABORTED", zlegetline(NULL, NULL));
     zbeep();
     region_active = 0;
     return 0;

-- 
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] 8+ messages in thread

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 10:44 PATCH: $ZLE_LINE_ABORTED Peter Stephenson
@ 2010-09-07 11:46 ` Nikolai Weibull
  2010-09-07 12:02   ` Peter Stephenson
  0 siblings, 1 reply; 8+ messages in thread
From: Nikolai Weibull @ 2010-09-07 11:46 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh Hackers' List

On Tue, Sep 7, 2010 at 12:44, Peter Stephenson <Peter.Stephenson@csr.com> wrote:
> Here's what I think is a useful one-line change.  If there's an error in
> the line editor, including ^G, the line being edited gets copied to the
> variable ZLE_LINE_ABORTED.

Does this include pressing ^C at the “correct A to B” prompt?


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

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 11:46 ` Nikolai Weibull
@ 2010-09-07 12:02   ` Peter Stephenson
  2010-09-07 14:31     ` Greg Klanderman
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Stephenson @ 2010-09-07 12:02 UTC (permalink / raw)
  To: Zsh Hackers' List

On Tue, 7 Sep 2010 13:46:55 +0200
Nikolai Weibull <now@bitwi.se> wrote:
> On Tue, Sep 7, 2010 at 12:44, Peter Stephenson
> <Peter.Stephenson@csr.com> wrote:
> > Here's what I think is a useful one-line change.  If there's an
> > error in the line editor, including ^G, the line being edited gets
> > copied to the variable ZLE_LINE_ABORTED.
> 
> Does this include pressing ^C at the “correct A to B” prompt?

No, that's all done after the line manager has exited.  It's a nuisance the
line doesn't appear in the history (or anywhere else), but it's an entirely
different problem.  Could save the last successfully edited line (whatever
happens to it later) in yet another variable...  Oh, actually, you can do
that yourself...  The following seems to handle both cases.  It would be
more logical to unset ZLE_LINE_ABORTED in zle-line-init.  This works for
the spelling correction part even without the last patch.

zle-line-finish() {
   typeset -g ZLE_LINE_EDITED=$BUFFER
   # This is only called on a successful exit, so we don't need
   # the last aborted line any more...
   unset ZLE_LINE_ABORTED
}
recover-line() {
   if [[ -n $ZLE_LINE_ABORTED ]]; then
     LBUFFER=$ZLE_LINE_ABORTED
     RBUFFER=
   elif [[ -n $ZLE_LINE_EDITED ]]; then
     LBUFFER=$ZLE_LINE_EDITED
     RBUFFER=
   fi
}
zle -N zle-line-finish
zle -N recover-line

-- 
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] 8+ messages in thread

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 12:02   ` Peter Stephenson
@ 2010-09-07 14:31     ` Greg Klanderman
  2010-09-07 14:44       ` Peter Stephenson
  0 siblings, 1 reply; 8+ messages in thread
From: Greg Klanderman @ 2010-09-07 14:31 UTC (permalink / raw)
  To: zsh-workers

Cool, I was just wanting something like this the other day.. is there
a way to capture the line when exiting zle with C-c as well?

Here's how I'm using it, btw:

function _zle-line-init () {
  if [[ -n ${ZLE_LINE_ABORTED-} ]] ; then
      zle copy-region-as-kill "$ZLE_LINE_ABORTED"
      unset ZLE_LINE_ABORTED
  fi
}

thanks,
Greg


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

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 14:31     ` Greg Klanderman
@ 2010-09-07 14:44       ` Peter Stephenson
  2010-09-07 14:51         ` Greg Klanderman
  0 siblings, 1 reply; 8+ messages in thread
From: Peter Stephenson @ 2010-09-07 14:44 UTC (permalink / raw)
  To: zsh-workers

On Tue, 07 Sep 2010 10:31:41 -0400
Greg Klanderman <gak@klanderman.net> wrote:
> Cool, I was just wanting something like this the other day.. is there
> a way to capture the line when exiting zle with C-c as well?

Hmm... I'm wondering if I put it in the right place.  sendbreak can abort
other things than the full line edit, and I already claimed that "this
parameter is set by the line editor when an error occurs", which as you
spotted isn't necessarily true.

Index: Doc/Zsh/params.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/params.yo,v
retrieving revision 1.60
diff -p -u -r1.60 params.yo
--- Doc/Zsh/params.yo	14 Jun 2010 11:58:26 -0000	1.60
+++ Doc/Zsh/params.yo	7 Sep 2010 14:42:46 -0000
@@ -1448,6 +1448,13 @@ item(tt(ZDOTDIR))(
 The directory to search for shell startup files (.zshrc, etc),
 if not tt($HOME).
 )
+vindex(ZLE_LINE_ABORTED)
+item(tt(ZLE_LINE_ABORTED))(
+This parameter is set by the line editor when an error occurs.  It
+contains the line that was being edited at the point of the error.
+`tt(print -zr -- $ZLE_LINE_ABORTED)' can be used to recover the line.
+Only the most recent line of this kind is remembered.
+)
 vindex(ZLE_REMOVE_SUFFIX_CHARS)
 vindex(ZLE_SPACE_SUFFIX_CHARS)
 xitem(tt(ZLE_REMOVE_SUFFIX_CHARS))
Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.87
diff -p -u -r1.87 zle.yo
--- Doc/Zsh/zle.yo	6 Sep 2010 08:50:05 -0000	1.87
+++ Doc/Zsh/zle.yo	7 Sep 2010 14:42:46 -0000
@@ -2057,7 +2057,8 @@ tindex(send-break)
 item(tt(send-break) (^G ESC-^G) (unbound) (unbound))(
 Abort the current editor function, e.g. tt(execute-named-command), or the
 editor itself, e.g. if you are in tt(vared). Otherwise abort the parsing of
-the current line.
+the current line; in this case the aborted line is available in the shell
+variable tt(ZLE_LINE_ABORTED).
 )
 tindex(run-help)
 item(tt(run-help) (ESC-H ESC-h) (unbound) (unbound))(
Index: Src/Zle/zle_main.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_main.c,v
retrieving revision 1.124
diff -p -u -r1.124 zle_main.c
--- Src/Zle/zle_main.c	31 Jul 2010 19:36:54 -0000	1.124
+++ Src/Zle/zle_main.c	7 Sep 2010 14:42:46 -0000
@@ -1226,6 +1226,9 @@ zleread(char **lp, char **rp, int flags,
 
     zlecore();
 
+    if (errflag)
+	setsparam("ZLE_LINE_ABORTED", zlegetline(NULL, NULL));
+
     if (done && !exit_pending && !errflag &&
 	(initthingy = rthingy_nocreate("zle-line-finish"))) {
 	int saverrflag = errflag;

-- 
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] 8+ messages in thread

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 14:44       ` Peter Stephenson
@ 2010-09-07 14:51         ` Greg Klanderman
  2010-09-12 19:49           ` Mikael Magnusson
  0 siblings, 1 reply; 8+ messages in thread
From: Greg Klanderman @ 2010-09-07 14:51 UTC (permalink / raw)
  To: zsh-workers


Wow that was fast!  Seems to be working here, will continue to play
with it..

greg


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

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-07 14:51         ` Greg Klanderman
@ 2010-09-12 19:49           ` Mikael Magnusson
  2010-09-12 19:51             ` Mikael Magnusson
  0 siblings, 1 reply; 8+ messages in thread
From: Mikael Magnusson @ 2010-09-12 19:49 UTC (permalink / raw)
  To: zsh-workers

On 7 September 2010 16:51, Greg Klanderman <gak@klanderman.net> wrote:
>
> Wow that was fast!  Seems to be working here, will continue to play
> with it..

I played with it a bit now, and came up with this

#the intent here is to insert syntax errors back on the command line, since
#those aren't saved in history, for example entering "do;"
function _zle_line_init() {
  if [[ -n $ZLE_LINE_SAVED && $ZLE_LINE_SAVED[1] != " " &&
$ZLE_LINE_SAVED != $history[$((HISTCMD-1))] ]]; then
    LBUFFER=$ZLE_LINE_SAVED
    zle -R
  fi
  unset ZLE_LINE_SAVED
}

#like pws said, if we accept a line, we don't need the aborted one anymore
function _zle_line_finish() {
  ZLE_LINE_SAVED=$BUFFER
  unset ZLE_LINE_ABORTED
}


#this puts back the aborted line when you try to go up history, can be
bound to other
#things too but haven't got around to that yet (ie up-line-or-search).
should probably
#also check the current line is empty first too.
function _recover_line_or_else() {
  if [[ -n $ZLE_LINE_SAVED ]]; then
    LBUFFER+=$ZLE_LINE_SAVED
    unset ZLE_LINE_SAVED
  elif [[ -n $ZLE_LINE_ABORTED ]]; then
    LBUFFER+=$ZLE_LINE_ABORTED
    unset ZLE_LINE_ABORTED
  else
    zle .$WIDGET
  fi
}
zle -N up-history _recover_line_or_else

-- 
Mikael Magnusson


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

* Re: PATCH: $ZLE_LINE_ABORTED
  2010-09-12 19:49           ` Mikael Magnusson
@ 2010-09-12 19:51             ` Mikael Magnusson
  0 siblings, 0 replies; 8+ messages in thread
From: Mikael Magnusson @ 2010-09-12 19:51 UTC (permalink / raw)
  To: zsh-workers

On 12 September 2010 21:49, Mikael Magnusson <mikachu@gmail.com> wrote:
> #the intent here is to insert syntax errors back on the command line, since
> #those aren't saved in history, for example entering "do;"
> function _zle_line_init() {
>  if [[ -n $ZLE_LINE_SAVED && $ZLE_LINE_SAVED[1] != " " &&
> $ZLE_LINE_SAVED != $history[$((HISTCMD-1))] ]]; then

I had print -s $ZLE_LINE_SAVED here for a while, but it didn't work
well, that's what the [1] != " " is there from (emulating
histignorespace), forgot to remove it.

-- 
Mikael Magnusson


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

end of thread, other threads:[~2010-09-12 19:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-07 10:44 PATCH: $ZLE_LINE_ABORTED Peter Stephenson
2010-09-07 11:46 ` Nikolai Weibull
2010-09-07 12:02   ` Peter Stephenson
2010-09-07 14:31     ` Greg Klanderman
2010-09-07 14:44       ` Peter Stephenson
2010-09-07 14:51         ` Greg Klanderman
2010-09-12 19:49           ` Mikael Magnusson
2010-09-12 19:51             ` 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).