zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: Polite output from traps with zle active
@ 2000-11-02 11:20 Peter Stephenson
  2000-11-02 17:30 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2000-11-02 11:20 UTC (permalink / raw)
  To: Zsh hackers list

In parallel to Sven's changes for traps, here is something I have wanted
for some time:  the ability for traps to keep the line editing display neat
in the same way that e.g. information from exiting background jobs has
always done.  It alters zle -R to allow it to be called when zle is not
active --- it simply returns zero --- and adds `zle -I' (invalidate), which
simply calls trashzle().  Both are allowed when zle is not active (and do
nothing) because there is no good reason for forcing the onus of deciding
whether the display needs to be cleared and refreshed onto the writer of the
trap function.  So a prototype is:

TRAPUSR1() {
  zle -I
  print Hello, I am output
  zle -R
}

It's possible there are extra safeguards I should have added but missed.
If anyone thinks this should be done a different way, now would be a good
time to mention it.

I probably won't commit this at the moment, anyway, because my zle_main.c
has Sven's trap changes mixed in it --- maybe they can eventually go in
together.

Index: Src/Zle/zle_main.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_main.c,v
retrieving revision 1.13
diff -u -r1.13 zle_main.c
--- Src/Zle/zle_main.c	2000/10/22 02:34:19	1.13
+++ Src/Zle/zle_main.c	2000/11/02 11:08:56
@@ -1083,7 +1092,7 @@
 static struct builtin bintab[] = {
     BUILTIN("bindkey", 0, bin_bindkey, 0, -1, 0, "evaMldDANmrsLR", NULL),
     BUILTIN("vared",   0, bin_vared,   1,  7, 0, NULL,             NULL),
-    BUILTIN("zle",     0, bin_zle,     0, -1, 0, "lDANCLmMgGcRaU", NULL),
+    BUILTIN("zle",     0, bin_zle,     0, -1, 0, "lDANCLmMgGcRaUI", NULL),
 };
 
 /* The order of the entries in this table has to match the *HOOK
Index: Src/Zle/zle_thingy.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_thingy.c,v
retrieving revision 1.2
diff -u -r1.2 zle_thingy.c
--- Src/Zle/zle_thingy.c	2000/07/13 09:11:54	1.2
+++ Src/Zle/zle_thingy.c	2000/11/02 11:08:56
@@ -339,6 +339,7 @@
 	{ 'R', bin_zle_refresh, 0, -1 },
 	{ 'M', bin_zle_mesg, 1, 1 },
 	{ 'U', bin_zle_unget, 1, 1 },
+	{ 'I', bin_zle_invalidate, 0, 0 },
 	{ 0,   bin_zle_call, 0, -1 },
     };
     struct opn const *op, *opp;
@@ -396,10 +397,8 @@
     char *s = statusline;
     int sl = statusll, ocl = clearlist;
 
-    if (!zleactive) {
-	zwarnnam(name, "can only be called from widget function", NULL, 0);
-	return 1;
-    }
+    if (!zleactive)
+	return 0;
     statusline = NULL;
     statusll = 0;
     if (*args) {
@@ -654,6 +653,14 @@
     if (saveflag)
 	zmod = modsave;
     return ret;
+}
+
+/**/
+static int
+bin_zle_invalidate(char *name, char **args, char *ops, char func)
+{
+    trashzle();
+    return 0;
 }
 
 /*******************/
Index: Doc/Zsh/mod_zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/mod_zle.yo,v
retrieving revision 1.8
diff -u -r1.8 mod_zle.yo
--- Doc/Zsh/mod_zle.yo	2000/09/11 07:08:28	1.8
+++ Doc/Zsh/mod_zle.yo	2000/11/02 11:08:56
@@ -198,6 +198,7 @@
 xitem(tt(zle) tt(-R) [ tt(-c) ] [ var(display-string) ] [ var(string) ... ])
 xitem(tt(zle) tt(-M) var(string))
 xitem(tt(zle) tt(-U) var(string))
+xitem(tt(zle) tt(-I))
 xitem(tt(zle) var(widget) tt([ -n) var(num) tt(]) tt([ -N ]) var(args) ...)
 item(tt(zle))(
 The tt(zle) builtin performs a number of different actions concerning
@@ -263,6 +264,12 @@
 Note that this option is only useful for widgets that do not exit
 immediately after using it because the strings displayed will be erased 
 immediately after return from the widget.
+
+This command can safely be called outside user defined widgets; if zle is
+active, the display will be refreshed, while if zle is not active, the
+command has no effect.  This is useful for trap functions, which may be
+called while zle is active, in order to tidy up the display on exit.
+In this case there will usually be no other arguments.
 )
 item(tt(-M) var(string))(
 As with the tt(-R) option, the var(string) will be displayed below the 
@@ -280,6 +287,27 @@
 the last string pushed onto the stack will be processed first.  However,
 the characters in each var(string) will be processed in the order in which
 they appear in the string.
+)
+item(tt(-I))(
+Unusually, this option is only useful em(outside) ordinary widget functions.
+It invalidates the current zle display in preparation for output; usually
+this will be from a trap function.  As it has no effect if zle is not
+active, a safe idiom for ensuring that output from traps interacts cleanly
+with text being edited is:
+
+example(TRAPUSR1() {
+    # Invalidate zle display
+  zle -I
+    # Show output
+  print Hello
+    # Refresh display
+  zle -R
+})
+
+Note that there are better ways of manipulating the display from within zle
+widgets.  In general, the trap function may need to test whether zle is
+loaded before using this method; if it is not, there is no point in loading
+it specially since the line editor will not be active.
 )
 item(var(widget) tt([ -n) var(num) tt(]) tt([ -N ]) var(args) ...)(
 Invoke the specified widget.  This can only be done when ZLE is

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

* Re: PATCH: Polite output from traps with zle active
  2000-11-02 11:20 PATCH: Polite output from traps with zle active Peter Stephenson
@ 2000-11-02 17:30 ` Bart Schaefer
  2000-11-03 11:09   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2000-11-02 17:30 UTC (permalink / raw)
  To: Peter Stephenson, Zsh hackers list

On Nov 2, 11:20am, Peter Stephenson wrote:
} Subject: PATCH: Polite output from traps with zle active
}
} TRAPUSR1() {
}   zle -I
}   print Hello, I am output
}   zle -R
} }
} 
} It's possible there are extra safeguards I should have added but missed.
} If anyone thinks this should be done a different way, now would be a good
} time to mention it.

It's too bad that the trap writer has to call `zle -R' as well as `zle -I'.
What happens if you accidentally call only `zle -I'?

I can see how the zsh trap handler can't possibly know in advance whether
the user-defined trap will produce output, and therefore shouldn't do a
trashzle() on its own -- but surely it could detect whether zle has been
trashed and do the zrefresh() automatically?

If there really is some valid use for trashing zle without refreshing it,
perhaps another parameter to `zle -I' (or even something like `zle +I')
could be used to suppress the eventual zrefresh().

} It alters zle -R to allow it to be called when zle is not active ---
} it simply returns zero --- and adds `zle -I' (invalidate), which
} simply calls trashzle(). Both are allowed when zle is not active (and
} do nothing) because there is no good reason for forcing the onus of
} deciding whether the display needs to be cleared and refreshed onto
} the writer of the trap function.

It'd also be nice if `zle -R' and `zle -I' returned zero when zle is
active and nonzero otherwise, just as `zle' with no arguments does.
There may not be much use for this, since probably one most often wants
to test for zle *before* invalidating it, but there is some benefit to
consistency.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: PATCH: Polite output from traps with zle active
  2000-11-02 17:30 ` Bart Schaefer
@ 2000-11-03 11:09   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2000-11-03 11:09 UTC (permalink / raw)
  To: Zsh hackers list

Bart instructed:
> It's too bad that the trap writer has to call `zle -R' as well as `zle -I'.
> What happens if you accidentally call only `zle -I'?
> 
> It'd also be nice if `zle -R' and `zle -I' returned zero when zle is
> active and nonzero otherwise, just as `zle' with no arguments does.

This looks like it works.  This is on top of everything else (still
uncommitted): the signals.c hunk just about fails to conflict when any of
Sven's trap work.

--- Doc/Zsh/mod_zle.yo.zrt2	Fri Nov  3 10:57:42 2000
+++ Doc/Zsh/mod_zle.yo	Fri Nov  3 11:02:33 2000
@@ -267,9 +267,8 @@
 
 This command can safely be called outside user defined widgets; if zle is
 active, the display will be refreshed, while if zle is not active, the
-command has no effect.  This is useful for trap functions, which may be
-called while zle is active, in order to tidy up the display on exit.
-In this case there will usually be no other arguments.
+command has no effect.  In this case there will usually be no other
+arguments.  The status is zero if zle was active, else one.
 )
 item(tt(-M) var(string))(
 As with the tt(-R) option, the var(string) will be displayed below the 
@@ -291,23 +290,24 @@
 item(tt(-I))(
 Unusually, this option is only useful em(outside) ordinary widget functions.
 It invalidates the current zle display in preparation for output; usually
-this will be from a trap function.  As it has no effect if zle is not
-active, a safe idiom for ensuring that output from traps interacts cleanly
-with text being edited is:
+this will be from a trap function.  It has no effect if zle is not
+active.  When a trap exits, the shell checks to see if the display needs
+restoring, hence the following will print output in such a way as not to
+disturb the line being edited:
 
 example(TRAPUSR1() {
     # Invalidate zle display
   zle -I
     # Show output
   print Hello
-    # Refresh display
-  zle -R
 })
 
 Note that there are better ways of manipulating the display from within zle
 widgets.  In general, the trap function may need to test whether zle is
 loaded before using this method; if it is not, there is no point in loading
 it specially since the line editor will not be active.
+
+The status is zero if zle was active, else one.
 )
 item(var(widget) tt([ -n) var(num) tt(]) tt([ -N ]) var(args) ...)(
 Invoke the specified widget.  This can only be done when ZLE is
--- Src/Zle/zle_thingy.c.zrt2	Fri Nov  3 11:00:00 2000
+++ Src/Zle/zle_thingy.c	Fri Nov  3 11:00:30 2000
@@ -398,7 +398,7 @@
     int sl = statusll, ocl = clearlist;
 
     if (!zleactive)
-	return 0;
+	return 1;
     statusline = NULL;
     statusll = 0;
     if (*args) {
@@ -659,8 +659,11 @@
 static int
 bin_zle_invalidate(char *name, char **args, char *ops, char func)
 {
-    trashzle();
-    return 0;
+    if (zleactive) {
+	trashzle();
+	return 0;
+    } else
+	return 1;
 }
 
 /*******************/
--- Src/signals.c.zrt2	Fri Nov  3 10:57:12 2000
+++ Src/signals.c	Fri Nov  3 10:57:24 2000
@@ -955,6 +955,13 @@
 	    breaks = loops;
     }
 
+    /*
+     * If zle was running while the trap was executed, see if we
+     * need to restore the display.
+     */
+    if (zleactive && resetneeded)
+	zrefresh();
+
     if (*sigtr != ZSIG_IGNORED)
 	*sigtr &= ~ZSIG_IGNORED;
 }

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

end of thread, other threads:[~2000-11-03 11:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-11-02 11:20 PATCH: Polite output from traps with zle active Peter Stephenson
2000-11-02 17:30 ` Bart Schaefer
2000-11-03 11:09   ` Peter Stephenson

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