zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: random attribute stuff
@ 2008-05-06  9:15 Peter Stephenson
  2008-05-06 14:34 ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2008-05-06  9:15 UTC (permalink / raw)
  To: Zsh hackers list

This is supposed to replace ad hoc code that fixes up text attributes
after the right prompt to use the usual settextattributes() function.
However, having seen the change, it's more ad hoc than I hoped... still,
that's mostly because it now handles changes in colour which it didn't
before.

I'm not completely convinced of the utility of this chunk of code.  It
seems to be designed so that after a right prompt is output everything
is the same as after the normal prompt.  But both before and after this
patch if I let the right prompt leave, say, underline turned on then
when I play with history lines the main line appears underlined, too.  I
don't think it's ever been a good idea to leave effects turned on after
prompts.

Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.129
diff -u -r1.129 zsh.h
--- Src/zsh.h	2 May 2008 22:48:58 -0000	1.129
+++ Src/zsh.h	6 May 2008 09:06:02 -0000
@@ -1983,6 +1983,8 @@
 #define TXT_ATTR_OFF_ON_SHIFT 6
 #define TXT_ATTR_OFF_FROM_ON(attr)	\
     (((attr) & TXT_ATTR_ON_MASK) << TXT_ATTR_OFF_ON_SHIFT)
+#define TXT_ATTR_ON_FROM_OFF(attr)	\
+    (((attr) & TXT_ATTR_OFF_MASK) >> TXT_ATTR_OFF_ON_SHIFT)
 /*
  * Indicates to zle_refresh.c that the character entry is an
  * index into the list of multiword symbols.
Index: Src/Zle/zle_refresh.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_refresh.c,v
retrieving revision 1.65
diff -u -r1.65 zle_refresh.c
--- Src/Zle/zle_refresh.c	1 May 2008 10:58:25 -0000	1.65
+++ Src/Zle/zle_refresh.c	6 May 2008 09:06:03 -0000
@@ -1869,29 +1869,44 @@
 
     /* output the right-prompt if appropriate */
 	if (put_rpmpt && !iln && !oput_rpmpt) {
+	    int attrchange;
+
 	    moveto(0, winw - 1 - rpromptw);
 	    zputs(rpromptbuf, shout);
 	    vcs = winw - 1;
 	/* reset character attributes to that set by the main prompt */
 	    txtchange = pmpt_attr;
-	    if (txtchangeisset(txtchange, TXTNOBOLDFACE) &&
-		(rpmpt_attr & TXTBOLDFACE))
-		tsetcap(TCALLATTRSOFF, 0);
-	    if (txtchangeisset(txtchange, TXTNOSTANDOUT) &&
-		(rpmpt_attr & TXTSTANDOUT))
-		tsetcap(TCSTANDOUTEND, 0);
-	    if (txtchangeisset(txtchange, TXTNOUNDERLINE) &&
-		(rpmpt_attr & TXTUNDERLINE))
-		tsetcap(TCUNDERLINEEND, 0);
-	    if (txtchangeisset(txtchange, TXTBOLDFACE) &&
-		(rpmpt_attr & TXTNOBOLDFACE))
-		tsetcap(TCBOLDFACEBEG, 0);
-	    if (txtchangeisset(txtchange, TXTSTANDOUT) &&
-		(rpmpt_attr & TXTNOSTANDOUT))
-		tsetcap(TCSTANDOUTBEG, 0);
-	    if (txtchangeisset(txtchange, TXTUNDERLINE) &&
-		(rpmpt_attr & TXTNOUNDERLINE))
-		tsetcap(TCUNDERLINEBEG, 0);
+	    /*
+	     * Keep attributes that have actually changed,
+	     * which are ones off in rpmpt_attr and on in
+	     * pmpt_attr, and vice versa.
+	     */
+	    attrchange = txtchange &
+		(TXT_ATTR_OFF_FROM_ON(rpmpt_attr) |
+		 TXT_ATTR_ON_FROM_OFF(rpmpt_attr));
+	    /*
+	     * Careful in case the colour changed.
+	     */
+	    if (txtchangeisset(txtchange, TXTFGCOLOUR) &&
+		(!txtchangeisset(rpmpt_attr, TXTFGCOLOUR) ||
+		 ((txtchange ^ rpmpt_attr) & TXT_ATTR_FG_COL_MASK)))
+	    {
+		attrchange |=
+		    txtchange & (TXTFGCOLOUR | TXT_ATTR_FG_COL_MASK);
+	    }
+	    if (txtchangeisset(txtchange, TXTBGCOLOUR) &&
+		(!txtchangeisset(rpmpt_attr, TXTBGCOLOUR) ||
+		 ((txtchange ^ rpmpt_attr) & TXT_ATTR_BG_COL_MASK)))
+	    {
+		attrchange |=
+		    txtchange & (TXTBGCOLOUR | TXT_ATTR_BG_COL_MASK);
+	    }
+	    /*
+	     * Now feed these changes into the usual function,
+	     * if necessary.
+	     */
+	    if (attrchange)
+		settextattributes(attrchange);
 	}
     }
 


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


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

* Re: PATCH: random attribute stuff
  2008-05-06  9:15 PATCH: random attribute stuff Peter Stephenson
@ 2008-05-06 14:34 ` Bart Schaefer
  2008-05-06 14:42   ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2008-05-06 14:34 UTC (permalink / raw)
  To: Zsh hackers list

On May 6, 10:15am, Peter Stephenson wrote:
}
} I'm not completely convinced of the utility of this chunk of code.  It
} seems to be designed so that after a right prompt is output everything
} is the same as after the normal prompt.

I think that would be correct, because the rprompt is printed last and
then the cursor is merely moved to the end of the normal prompt.

} But both before and after this patch if I let the right prompt leave,
} say, underline turned on then when I play with history lines the main
} line appears underlined, too. I don't think it's ever been a good idea
} to leave effects turned on after prompts.

Some people want the entire command line to look one way and the output
from the command to look another way, so they can easily tell what's a
command and what isn't when scrolling backward through buffered output.
Leaving effects turned on after the prompt is the only way to do that;
at some point it was decided that it was better to have the prompt help
out with it, than to force people to trick the shell into outputting a
terminal control sequence without using the prompt escapes.


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

* Re: PATCH: random attribute stuff
  2008-05-06 14:34 ` Bart Schaefer
@ 2008-05-06 14:42   ` Peter Stephenson
  2008-05-06 14:58     ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2008-05-06 14:42 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> Some people want the entire command line to look one way and the output
> from the command to look another way, so they can easily tell what's a
> command and what isn't when scrolling backward through buffered output.
> Leaving effects turned on after the prompt is the only way to do that;
> at some point it was decided that it was better to have the prompt help
> out with it, than to force people to trick the shell into outputting a
> terminal control sequence without using the prompt escapes.

Right, but I'm not convinced it actually works.

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


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

* Re: PATCH: random attribute stuff
  2008-05-06 14:42   ` Peter Stephenson
@ 2008-05-06 14:58     ` Bart Schaefer
  2008-05-06 15:10       ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2008-05-06 14:58 UTC (permalink / raw)
  To: Zsh hackers list

On May 6,  3:42pm, Peter Stephenson wrote:
}
} Bart Schaefer wrote:
} > Some people want the entire command line to look one way and the output
} > from the command to look another way, so they can easily tell what's a
} > command and what isn't when scrolling backward through buffered output.
} 
} Right, but I'm not convinced it actually works.

It almost certainly *won't* work except in the most simple cases now that
ZLE is doing its own highlighting effects.  People are going to have to
decide whether they want that badly enough to use zle_highlight=(none).

Inevitably someone will complain that ZLE should put back whatever effect
was there before when it turns on/off its own effects.


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

* Re: PATCH: random attribute stuff
  2008-05-06 14:58     ` Bart Schaefer
@ 2008-05-06 15:10       ` Peter Stephenson
  2008-05-06 15:38         ` Bart Schaefer
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2008-05-06 15:10 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> On May 6,  3:42pm, Peter Stephenson wrote:
> }
> } Bart Schaefer wrote:
> } > Some people want the entire command line to look one way and the output
> } > from the command to look another way, so they can easily tell what's a
> } > command and what isn't when scrolling backward through buffered output.
> } 
> } Right, but I'm not convinced it actually works.
> 
> It almost certainly *won't* work except in the most simple cases now that
> ZLE is doing its own highlighting effects.  People are going to have to
> decide whether they want that badly enough to use zle_highlight=(none).

I tried it without any highlighting.

> Inevitably someone will complain that ZLE should put back whatever effect
> was there before when it turns on/off its own effects.

That's probably doable in principle by tying the highlighting state to
what came in with the prompt, but (i) I don't have a lot of interest in
doing this since it's quite a complicated way of propagating a hack to
get particular effects that's getting hackier as the highlighting code
develops (ii) as I said, it doesn't seem to work very well anyway.

It ought to be (but might turn out not to be) fairly easy to add a
"default" element to zle_highlight that causes the given attributes to
be used as the default set for the command line, which would probably
work a lot better, and then deprecate any reliance on what the prompt
happened to produce.

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


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

* Re: PATCH: random attribute stuff
  2008-05-06 15:10       ` Peter Stephenson
@ 2008-05-06 15:38         ` Bart Schaefer
  2008-05-06 15:48           ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Bart Schaefer @ 2008-05-06 15:38 UTC (permalink / raw)
  To: Zsh hackers list

On May 6,  4:10pm, Peter Stephenson wrote:
}
} > It almost certainly *won't* work except in the most simple cases now that
} > ZLE is doing its own highlighting effects.  People are going to have to
} > decide whether they want that badly enough to use zle_highlight=(none).
} 
} I tried it without any highlighting.

It works for me in xterm if I e.g. assign PS1="$PS1%U" ... in fact for
the most part even the active region highlight doesn't screw it up, you
appear to be doing a good job of turning off only the seuqnces that you
previously turned on.  (I did not try more complicated effects than %U.)

(It's a bit odd that %U persists into the right prompt unless turned off
with PRS1="%u$RPS1" but it has always been that way.)
 
} It ought to be (but might turn out not to be) fairly easy to add a
} "default" element to zle_highlight that causes the given attributes to
} be used as the default set for the command line, which would probably
} work a lot better, and then deprecate any reliance on what the prompt
} happened to produce.

Sounds reasonable to me, but could get ugly if a widget were to change
it on the fly -- do you trap that and reprint the entire buffer, or do
you allow blotches of different attributes as areas are selectively
repainted, or do you make that setting read-only in widgets?


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

* Re: PATCH: random attribute stuff
  2008-05-06 15:38         ` Bart Schaefer
@ 2008-05-06 15:48           ` Peter Stephenson
  2008-05-06 18:16             ` Peter Stephenson
  0 siblings, 1 reply; 11+ messages in thread
From: Peter Stephenson @ 2008-05-06 15:48 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote:
> } It ought to be (but might turn out not to be) fairly easy to add a
> } "default" element to zle_highlight that causes the given attributes to
> } be used as the default set for the command line, which would probably
> } work a lot better, and then deprecate any reliance on what the prompt
> } happened to produce.
> 
> Sounds reasonable to me, but could get ugly if a widget were to change
> it on the fly -- do you trap that and reprint the entire buffer, or do
> you allow blotches of different attributes as areas are selectively
> repainted, or do you make that setting read-only in widgets?

It's likely to be implement in zle_refresh.c, so the answer is probably
that it changes at the next refresh, which I think ought to work OK.

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


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

* Re: PATCH: random attribute stuff
  2008-05-06 15:48           ` Peter Stephenson
@ 2008-05-06 18:16             ` Peter Stephenson
  2008-05-07 15:44               ` Oliver Kiddle
  2008-05-07 15:49               ` Oliver Kiddle
  0 siblings, 2 replies; 11+ messages in thread
From: Peter Stephenson @ 2008-05-06 18:16 UTC (permalink / raw)
  To: Zsh hackers list

On Tue, 06 May 2008 16:48:52 +0100
Peter Stephenson <pws@csr.com> wrote:
> Bart Schaefer wrote:
> > } It ought to be (but might turn out not to be) fairly easy to add a
> > } "default" element to zle_highlight that causes the given attributes to
> > } be used as the default set for the command line, which would probably
> > } work a lot better, and then deprecate any reliance on what the prompt
> > } happened to produce.
> > 
> > Sounds reasonable to me, but could get ugly if a widget were to change
> > it on the fly -- do you trap that and reprint the entire buffer, or do
> > you allow blotches of different attributes as areas are selectively
> > repainted, or do you make that setting read-only in widgets?
> 
> It's likely to be implement in zle_refresh.c, so the answer is probably
> that it changes at the next refresh, which I think ought to work OK.

I'm sending it in quite a hurry since I discovered there's a nasty
allocation problem: moveto() can be called by trashzle() outside
zrefresh(), in which case the buffer for colour sequences isn't allocated.
So if you have turn an attribute off to output raw newlines, say, then
you're in trouble.  Showed up with the patch because you're still
highlighting at the end of the text (which means I haven't tried hard
enough to find the point at which it needs to turn attributes off) but was
there since I added the configuration for colour sequences.

Most likely remaining problem is that I haven't tracked down some
optimisation which is likely to leave you with a blotchy display with the
default highlighting.

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.68
diff -u -r1.68 zle.yo
--- Doc/Zsh/zle.yo	1 May 2008 11:33:03 -0000	1.68
+++ Doc/Zsh/zle.yo	6 May 2008 18:11:56 -0000
@@ -2097,6 +2097,10 @@
 startitem()
 cindex(region, highlighting)
 cindex(highlighting, region)
+item(tt(default))(
+Any text within the command line not affected by any other highlighting.
+Text outside the editable area of the command line is not affected.
+)
 item(tt(isearch))(
 When one of the incremental history search widgets is active, the
 area of the command line matched by the search string or pattern.
Index: Src/Zle/zle_refresh.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_refresh.c,v
retrieving revision 1.66
diff -u -r1.66 zle_refresh.c
--- Src/Zle/zle_refresh.c	6 May 2008 09:23:08 -0000	1.66
+++ Src/Zle/zle_refresh.c	6 May 2008 18:11:57 -0000
@@ -203,11 +203,12 @@
 
 
 /*
- * Attributes used for highlighting special (unprintable) characters
+ * Attributes used by default on the command line, and
+ * attributes for highlighting special (unprintable) characters
  * displayed on screen.
  */
 
-static int special_atr_on;
+static int default_atr_on, special_atr_on;
 
 /* Flags for the region_highlight structure */
 enum {
@@ -503,6 +504,31 @@
 }
 
 
+/* Allocate buffer for colour code composition */
+
+static void
+set_colseq_buf(void)
+{
+    int lenfg, lenbg, len;
+
+    lenfg = strlen(fg_bg_sequences[COL_SEQ_FG].def);
+    /* always need 1 character for non-default code */
+    if (lenfg < 1)
+	lenfg = 1;
+    lenfg += strlen(fg_bg_sequences[COL_SEQ_FG].start) +
+	strlen(fg_bg_sequences[COL_SEQ_FG].end);
+
+    lenbg = strlen(fg_bg_sequences[COL_SEQ_BG].def);
+    /* always need 1 character for non-default code */
+    if (lenbg < 1)
+	lenbg = 1;
+    lenbg += strlen(fg_bg_sequences[COL_SEQ_BG].start) +
+	strlen(fg_bg_sequences[COL_SEQ_BG].end);
+
+    len = lenfg > lenbg ? lenfg : lenbg;
+    colseq_buf = (char *)zalloc(len+1);
+}
+
 /*
  * Parse the variable zle_highlight to decide how to highlight characters
  * and regions.  Set defaults for anything not explicitly covered.
@@ -516,30 +542,30 @@
     int special_atr_on_set = 0;
     int region_atr_on_set = 0;
     int isearch_atr_on_set = 0;
-    int lenfg, lenbg, len;
     struct region_highlight *rhp;
 
-    special_atr_on = 0;
+    special_atr_on = default_atr_on = 0;
     if (!region_highlights) {
 	region_highlights = (struct region_highlight *)
 	    zshcalloc(N_SPECIAL_HIGHLIGHTS*sizeof(struct region_highlight));
 	n_region_highlights = N_SPECIAL_HIGHLIGHTS;
     } else {
-	region_highlights->atr = 0;
+	for (rhp = region_highlights;
+	     rhp < region_highlights + N_SPECIAL_HIGHLIGHTS;
+	     rhp++) {
+	    rhp->atr = 0;
+	}
     }
 
     if (atrs) {
 	for (; *atrs; atrs++) {
 	    if (!strcmp(*atrs, "none")) {
 		/* reset attributes for consistency... usually unnecessary */
-		special_atr_on = 0;
+		special_atr_on = default_atr_on = 0;
 		special_atr_on_set = region_atr_on_set =
 		    isearch_atr_on_set = 1;
-		for (rhp = region_highlights;
-		     rhp < region_highlights + N_SPECIAL_HIGHLIGHTS;
-		     rhp++) {
-		    rhp->atr = 0;
-		}
+	    } else if (strpfx("default:", *atrs)) {
+		match_highlight(*atrs + 8, &default_atr_on);
 	    } else if (strpfx("special:", *atrs)) {
 		match_highlight(*atrs + 8, &special_atr_on);
 		special_atr_on_set = 1;
@@ -573,23 +599,7 @@
     if (!isearch_atr_on_set)
 	region_highlights[1].atr = TXTUNDERLINE;
 
-    /* Allocate buffer for colour code composition */
-    lenfg = strlen(fg_bg_sequences[COL_SEQ_FG].def);
-    /* always need 1 character for non-default code */
-    if (lenfg < 1)
-	lenfg = 1;
-    lenfg += strlen(fg_bg_sequences[COL_SEQ_FG].start) +
-	strlen(fg_bg_sequences[COL_SEQ_FG].end);
-
-    lenbg = strlen(fg_bg_sequences[COL_SEQ_BG].def);
-    /* always need 1 character for non-default code */
-    if (lenbg < 1)
-	lenbg = 1;
-    lenbg += strlen(fg_bg_sequences[COL_SEQ_BG].start) +
-	strlen(fg_bg_sequences[COL_SEQ_BG].end);
-
-    len = lenfg > lenbg ? lenfg : lenbg;
-    colseq_buf = (char *)zalloc(len+1);
+    set_colseq_buf();
 }
 
 
@@ -597,8 +607,10 @@
 static void
 zle_free_highlight(void)
 {
+    DPUTS(!colseq_buf, "Freeing colour sequence buffer without alloc");
     /* Free buffer for colour code composition */
     free(colseq_buf);
+    colseq_buf = NULL;
 }
 
 /*
@@ -615,63 +627,59 @@
 {
     int arrsize = n_region_highlights;
     char **retarr, **arrp;
+    struct region_highlight *rhp;
 
     /* region_highlights may not have been set yet */
-    if (!arrsize)
-	arrsize = N_SPECIAL_HIGHLIGHTS;
-    arrp = retarr = (char **)zhalloc(arrsize*sizeof(char *));
-    /* ignore NULL termination */
-    arrsize--;
-    if (arrsize) {
-	struct region_highlight *rhp;
-
-	/* ignore special highlighting */
-	for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS;
-	     arrsize--;
-	     rhp++, arrp++) {
-	    char digbuf1[DIGBUFSIZE], digbuf2[DIGBUFSIZE];
-	    int atrlen = 0, alloclen, done1;
-	    const struct highlight *hp;
+    if (arrsize)
+	arrsize -= N_SPECIAL_HIGHLIGHTS;
+    arrp = retarr = (char **)zhalloc((arrsize+1)*sizeof(char *));
 
-	    sprintf(digbuf1, "%d", rhp->start);
-	    sprintf(digbuf2, "%d", rhp->end);
-
-	    for (hp = highlights; hp->name; hp++) {
+    /* ignore special highlighting */
+    for (rhp = region_highlights + N_SPECIAL_HIGHLIGHTS;
+	 arrsize--;
+	 rhp++, arrp++) {
+	char digbuf1[DIGBUFSIZE], digbuf2[DIGBUFSIZE];
+	int atrlen = 0, alloclen, done1;
+	const struct highlight *hp;
+
+	sprintf(digbuf1, "%d", rhp->start);
+	sprintf(digbuf2, "%d", rhp->end);
+
+	for (hp = highlights; hp->name; hp++) {
+	    if (hp->mask_on & rhp->atr) {
+		if (atrlen)
+		    atrlen++; /* comma */
+		atrlen += strlen(hp->name);
+	    }
+	}
+	if (atrlen == 0)
+	    atrlen = 4; /* none */
+	alloclen = atrlen + strlen(digbuf1) + strlen(digbuf2) +
+	    3; /* 2 spaces, 1 0 */
+	if (rhp->flags & ZRH_PREDISPLAY)
+	    alloclen += 2; /* "P " */
+	*arrp = (char *)zhalloc(alloclen * sizeof(char));
+	/*
+	 * On input we allow a space after the flags.
+	 * I haven't put a space here because I think it's
+	 * marginally easier to have the output always split
+	 * into three words, and then check the first to
+	 * see if there are flags.  However, it's arguable.
+	 */
+	sprintf(*arrp, "%s%s %s ",
+		(rhp->flags & ZRH_PREDISPLAY) ? "P" : "",
+		digbuf1, digbuf2);
+	if (atrlen) {
+	    for (hp = highlights, done1 = 0; hp->name; hp++) {
 		if (hp->mask_on & rhp->atr) {
-		    if (atrlen)
-			atrlen++; /* comma */
-		    atrlen += strlen(hp->name);
+		    if (done1)
+			strcat(*arrp, ",");
+		    strcat(*arrp, hp->name);
+		    done1 = 1;
 		}
 	    }
-	    if (atrlen == 0)
-		atrlen = 4; /* none */
-	    alloclen = atrlen + strlen(digbuf1) + strlen(digbuf2) +
-		3; /* 2 spaces, 1 0 */
-	    if (rhp->flags & ZRH_PREDISPLAY)
-		alloclen += 2; /* "P " */
-	    *arrp = (char *)zhalloc(alloclen * sizeof(char));
-	    /*
-	     * On input we allow a space after the flags.
-	     * I haven't put a space here because I think it's
-	     * marginally easier to have the output always split
-	     * into three words, and then check the first to
-	     * see if there are flags.  However, it's arguable.
-	     */
-	    sprintf(*arrp, "%s%s %s ", 
-		    (rhp->flags & ZRH_PREDISPLAY) ? "P" : "",
-		    digbuf1, digbuf2);
-	    if (atrlen) {
-		for (hp = highlights, done1 = 0; hp->name; hp++) {
-		    if (hp->mask_on & rhp->atr) {
-			if (done1)
-			    strcat(*arrp, ",");
-			strcat(*arrp, hp->name);
-			done1 = 1;
-		    }
-		}
-	    } else
-		strcat(*arrp, "none");
-	}
+	} else
+	    strcat(*arrp, "none");
     }
     *arrp = '\0';
     return retarr;
@@ -748,6 +756,34 @@
 }
 
 
+/* The last attributes that were on. */
+static int lastatr;
+
+/*
+ * Clear the last attributes that we set:  used when we're going
+ * to be outputting stuff that shouldn't show up as text.
+ */
+static void
+clearattributes(void)
+{
+    if (lastatr) {
+	settextattributes(TXT_ATTR_OFF_FROM_ON(lastatr));
+	lastatr = 0;
+    }
+}
+
+/*
+ * Output a termcap capability, clearing any text attributes so
+ * as not to mess up the display.
+ */
+
+static void
+tcoutclear(int cap)
+{
+    clearattributes();
+    tcout(cap);
+}
+
 /*
  * Output the character.  This must come from the new video
  * buffer, nbuf, since we access the multiword buffer nmwbuf
@@ -767,7 +803,6 @@
      * This differs from *curatrp, which is an optimisation for
      * writing lots of stuff at once.
      */
-    static int lastatr;
 #ifdef MULTIBYTE_SUPPORT
     mbstate_t mbstate;
     int i;
@@ -1093,6 +1128,12 @@
 		   int use_termcap)
 {
     char *ptr;
+    int do_free;
+
+    if ((do_free = (colseq_buf == NULL))) {
+	/* This can happen when moving the cursor in trashzle() */
+	set_colseq_buf();
+    }
     /*
      * If we're not restoring the default, and either have a
      * colour value that is too large for ANSI, or have been told
@@ -1122,6 +1163,11 @@
 	*ptr++ = colour + '0';
     strcpy(ptr, fg_bg_sequences[fg_bg].end);
     tputs(colseq_buf, 1, putshout);
+
+    if (do_free) {
+	free(colseq_buf);
+	colseq_buf = NULL;
+    }
 }
 
 /**/
@@ -1310,7 +1356,7 @@
 
 	    nbuf[vln] = obuf[vln];
 	    moveto(nlnct, 0);
-	    tcout(TCCLEAREOD);
+	    tcoutclear(TCCLEAREOD);
 	    moveto(ovln, ovcs);
 	    nbuf[vln] = nb;
 	} else {
@@ -1370,7 +1416,7 @@
 
         if (!clearflag) {
             if (tccan(TCCLEAREOD))
-                tcout(TCCLEAREOD);
+                tcoutclear(TCCLEAREOD);
             else
                 cleareol = 1;   /* request: clear to end of line */
 	    if (listshown > 0)
@@ -1429,7 +1475,7 @@
     rpms.s = nbuf[rpms.ln = 0] + lpromptw;
     rpms.sen = *nbuf + winw;
     for (t = tmpline, tmppos = 0; tmppos < tmpll; t++, tmppos++) {
-	int base_atr_on = 0, base_atr_off = 0, ireg;
+	int base_atr_on = default_atr_on, base_atr_off = 0, ireg;
 	int all_atr_on, all_atr_off;
 	struct region_highlight *rhp;
 	/*
@@ -1445,9 +1491,10 @@
 		offset = predisplaylen; /* increment over it */
 	    if (rhp->start + offset <= tmppos &&
 		tmppos < rhp->end + offset) {
-		if (base_atr_on & (TXTFGCOLOUR|TXTBGCOLOUR)) {
-		    /* keep colour already set */
-		    base_atr_on |= rhp->atr & ~TXT_ATTR_COLOUR_ON_MASK;
+		if (rhp->atr & (TXTFGCOLOUR|TXTBGCOLOUR)) {
+		    /* override colour with later entry */
+		    base_atr_on = (base_atr_on & ~TXT_ATTR_ON_VALUES_MASK) |
+			rhp->atr;
 		} else {
 		    /* no colour set yet */
 		    base_atr_on |= rhp->atr;
@@ -2021,7 +2068,7 @@
     if (cleareol && !nllen && !(hasam && ln < nlnct - 1)
 	&& tccan(TCCLEAREOL)) {
 	moveto(ln, 0);
-	tcout(TCCLEAREOL);
+	tcoutclear(TCCLEAREOL);
 	return;	
     }
 
@@ -2161,7 +2208,7 @@
 
 	    /* if we can finish quickly, do so */
 	    if ((col_cleareol >= 0) && (ccs >= col_cleareol)) {
-		tcout(TCCLEAREOL);
+		tcoutclear(TCCLEAREOL);
 		return;
 	    }
 
@@ -2195,7 +2242,7 @@
 		zwrite(nl, i);
 		vcs += i;
 		if (col_cleareol >= 0)
-		    tcout(TCCLEAREOL);
+		    tcoutclear(TCCLEAREOL);
 		return;
 	    }
 
@@ -2505,7 +2552,7 @@
 mod_export int
 clearscreen(UNUSED(char **args))
 {
-    tcout(TCCLEARSCREEN);
+    tcoutclear(TCCLEARSCREEN);
     resetneeded = 1;
     clearflag = 0;
     return 0;
@@ -2823,7 +2870,7 @@
 	}
 	if (!vp->chr) {
 	    if (tccan(TCCLEAREOL))
-		tcout(TCCLEAREOL);
+		tcoutclear(TCCLEAREOL);
 	    else
 		for (; refreshop++->chr; vcs++)
 		    zputc(&zr_sp);



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


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

* Re: PATCH: random attribute stuff
  2008-05-06 18:16             ` Peter Stephenson
@ 2008-05-07 15:44               ` Oliver Kiddle
  2008-05-07 15:49               ` Oliver Kiddle
  1 sibling, 0 replies; 11+ messages in thread
From: Oliver Kiddle @ 2008-05-07 15:44 UTC (permalink / raw)
  To: Zsh hackers list

Peter wrote:
> > > } It ought to be (but might turn out not to be) fairly easy to add a
> > > } "default" element to zle_highlight that causes the given attributes to

This is definitely useful. Leaving attributes on at then end of the
prompt is something I always used to do. It still works except that it
all gets messed up by menu completion. I'm not sure about coloured
completion listings because I don't use them. In any case, it was enough
that I stopped leaving attributes on after the prompt.

I've attached a patch which changes prompt_oliver_setup to use
zle_highlight. This fixes things for the interaction with
region_highlight from my bracket matching widget but is still broken
when I use menu completion. It is especially broken if the bold
attribute is set: I get >= symbols around the matches.

Given that zsh now has internal knowledge that "green" is ESC [32m and
so on, would it be wise to allow this in prompt escapes: %A{fg=green}
perhaps? Or maybe support a prompt element in zle_highlight. I've only
ever used single colour prompts. Support for more than 16 colours would
also be good. SGI's terminal supported them 15 years ago.

Oliver

Index: prompt_oliver_setup
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Prompts/prompt_oliver_setup,v
retrieving revision 1.3
diff -u -r1.3 prompt_oliver_setup
--- prompt_oliver_setup	31 Jan 2002 15:30:42 -0000	1.3
+++ prompt_oliver_setup	7 May 2008 15:24:23 -0000
@@ -23,21 +23,22 @@
 
   [[ "${(t)pcolour}" != assoc* ]] && typeset -Ag pcolour
   [[ "${(t)tcolour}" != assoc* ]] && typeset -Ag tcolour
-  local pcol=${1:-${pcolour[${HOST:=`hostname`}]:-yellow}}
+  local pcol=${1:-${pcolour[${HOST:=`hostname`}]:-bold}}
   local pcolr=$fg[${pcol#bold}]
-  [[ $pcol = bold* ]] && pcolr=$bold_color$pcolr
+  [[ $pcol = bold* ]] && pcolr=%B$pcolr
   
-  local tcol=${2:-${tcolour[$HOST]:-white}}
-  local tcolr=$reset_color$fg[${tcol#bold}]
-  [[ $tcol = bold* ]] && tcolr=$tcolr$bold_color
+  local tcol=${2:-${tcolour[$HOST]}}
+  local tcolr="fg=${tcol#bold}"
+  [[ $tcol = bold* ]] && tcolr=bold,$tcolr
   
   local a host="%m:" user="%n "
   [[ $HOST == (${(j(|))~normal_hosts}) ]] && host=""
-  [[ ${USER:-`whoami`} == (root|${(j(|))~normal_users}) ]] && user=""
+  [[ $LOGNAME == (root|${(j(|))~normal_users}) ]] && user=""
 
-  PS1="%{$pcolr%}$user$host%~%"'$((COLUMNS-12))'"(l.$prompt_newline.
)[%h%1(j.%%%j.)%0(?..:%?)]%# %{$tcolr%}"
+  PS1="%{$pcolr%}$user$host%~%"'$((COLUMNS-12))'"(l.$prompt_newline.
)[%h%1(j.%%%j.)%0(?..:%?)]%# %{$reset_color%}"
   RPS2='<%^'
   PS2=''
+  zle_highlight[(r)default:*]=default:$tcolr
 }
 
 prompt_oliver_setup "$@"



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

* Re: PATCH: random attribute stuff
  2008-05-06 18:16             ` Peter Stephenson
  2008-05-07 15:44               ` Oliver Kiddle
@ 2008-05-07 15:49               ` Oliver Kiddle
  2008-05-07 15:55                 ` Peter Stephenson
  1 sibling, 1 reply; 11+ messages in thread
From: Oliver Kiddle @ 2008-05-07 15:49 UTC (permalink / raw)
  To: Zsh hackers list

This is definitely useful. Leaving attributes on at then end of the
prompt is something I always used to do. It still works except that it
all gets messed up by menu completion. I'm not sure about coloured
completion listings because I don't use them. In any case, it was enough
that I stopped leaving attributes on after the prompt.

I've attached a patch which changes prompt_oliver_setup to use
zle_highlight. This fixes things for the interaction with
region_highlight from my bracket matching widget but is still broken
when I use menu completion. It is especially broken if the bold
attribute is set: I get >= symbols around the matches.

Given that zsh now has internal knowledge that "green" is ESC [32m and
so on, would it be wise to allow this in prompt escapes: %A{fg=green}
perhaps? Or maybe support a prompt element in zle_highlight. I've only
ever used single colour prompts. Support for more than 16 colours would
also be good. SGI's terminal supported them 15 years ago.

Oliver

Index: prompt_oliver_setup
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Prompts/prompt_oliver_setup,v
retrieving revision 1.3
diff -u -r1.3 prompt_oliver_setup
--- prompt_oliver_setup	31 Jan 2002 15:30:42 -0000	1.3
+++ prompt_oliver_setup	7 May 2008 15:24:23 -0000
@@ -23,21 +23,22 @@
 
   [[ "${(t)pcolour}" != assoc* ]] && typeset -Ag pcolour
   [[ "${(t)tcolour}" != assoc* ]] && typeset -Ag tcolour
-  local pcol=${1:-${pcolour[${HOST:=`hostname`}]:-yellow}}
+  local pcol=${1:-${pcolour[${HOST:=`hostname`}]:-bold}}
   local pcolr=$fg[${pcol#bold}]
-  [[ $pcol = bold* ]] && pcolr=$bold_color$pcolr
+  [[ $pcol = bold* ]] && pcolr=%B$pcolr
   
-  local tcol=${2:-${tcolour[$HOST]:-white}}
-  local tcolr=$reset_color$fg[${tcol#bold}]
-  [[ $tcol = bold* ]] && tcolr=$tcolr$bold_color
+  local tcol=${2:-${tcolour[$HOST]}}
+  local tcolr="fg=${tcol#bold}"
+  [[ $tcol = bold* ]] && tcolr=bold,$tcolr
   
   local a host="%m:" user="%n "
   [[ $HOST == (${(j(|))~normal_hosts}) ]] && host=""
-  [[ ${USER:-`whoami`} == (root|${(j(|))~normal_users}) ]] && user=""
+  [[ $LOGNAME == (root|${(j(|))~normal_users}) ]] && user=""
 
-  PS1="%{$pcolr%}$user$host%~%"'$((COLUMNS-12))'"(l.$prompt_newline.
)[%h%1(j.%%%j.)%0(?..:%?)]%# %{$tcolr%}"
+  PS1="%{$pcolr%}$user$host%~%"'$((COLUMNS-12))'"(l.$prompt_newline.
)[%h%1(j.%%%j.)%0(?..:%?)]%# %{$reset_color%}"
   RPS2='<%^'
   PS2=''
+  zle_highlight[(r)default:*]=default:$tcolr
 }
 
 prompt_oliver_setup "$@"



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

* Re: PATCH: random attribute stuff
  2008-05-07 15:49               ` Oliver Kiddle
@ 2008-05-07 15:55                 ` Peter Stephenson
  0 siblings, 0 replies; 11+ messages in thread
From: Peter Stephenson @ 2008-05-07 15:55 UTC (permalink / raw)
  To: Zsh hackers list

Oliver Kiddle wrote:
> Given that zsh now has internal knowledge that "green" is ESC [32m and
> so on, would it be wise to allow this in prompt escapes: %A{fg=green}
> perhaps?

Yes, something along those lines is likely to happen.

> Support for more than 16 colours would also be good.

You can use up to 256 if you use the number.

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


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

end of thread, other threads:[~2008-05-07 16:18 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-06  9:15 PATCH: random attribute stuff Peter Stephenson
2008-05-06 14:34 ` Bart Schaefer
2008-05-06 14:42   ` Peter Stephenson
2008-05-06 14:58     ` Bart Schaefer
2008-05-06 15:10       ` Peter Stephenson
2008-05-06 15:38         ` Bart Schaefer
2008-05-06 15:48           ` Peter Stephenson
2008-05-06 18:16             ` Peter Stephenson
2008-05-07 15:44               ` Oliver Kiddle
2008-05-07 15:49               ` Oliver Kiddle
2008-05-07 15:55                 ` 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).