Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: 3.1.5: Re: New-line in prompt



"Owen M. Astley" wrote:
> I am trying to get a new line (\n) in a prompt (actually I only want the
> new line if the current one is too long).  Has anybody got any idea about
> how to do this?  %{\n%} works but, as the man page says, it shouldn't
> change the cursor position (for obvious reasons if you use it).

Getting a newline in a prompt is a standard question, the info below is
taken from the FAQ.

However, the other bit, adding a newline if the current line is too
long, is a different kettle of fish.  It's possible to add a test flag
so you can put in arbitrary text if the line has reached a certain
length.  With the patch to 3.1.5 at the end, you can do e.g.

PS1='any old stuff%(30l.
.)some more stuff'

and the prompt will break after the first bit if and only if 30
characters have been printed on the current line.  The patch correctly
takes account of tabs, termcap sequences, etc. (took a bit of internal
fiddling to get this to work with print -P).  Currently it subtracts
any complete lines already wrapped around, e.g. if you have printed 82
characters on an 80 column terminal it claims the width is 2.

Wish I'd thought of this before, I can envisage using it myself as an
alternative to truncation.


3.12: How do I get a newline in my prompt?

  You can place a literal newline in quotes, i.e.

    PROMPT="Hi Joe,
    what now?%# "

  If you have the bad taste to set the option cshjunkiequotes, which
  inhibits such behaviour, you will have to bracket this with
  `unsetopt cshjunkiequotes' and `setopt cshjunkiequotes', or put it
  in your .zshrc before the option is set.

  Arguably the prompt code should handle `print'-like escapes.  Feel
  free to write this :-).  Otherwise, you can use

    PROMPT=$(print "Hi Joe,\nwhat now?%# ")

  in your initialisation file.


*** Doc/Zsh/prompt.yo.cnt	Sun Mar 22 20:41:59 1998
--- Doc/Zsh/prompt.yo	Mon Nov  9 14:59:11 1998
***************
*** 175,180 ****
--- 175,182 ----
  sitem(tt(?))(True if the exit status of the last command was var(n).)
  sitem(tt(#))(True if the effective uid of the current process is var(n).)
  sitem(tt(g))(True if the effective gid of the current process is var(n).)
+ sitem(tt(l))(True if at least var(n) characters have already been
+ printed on the current line.)
  sitem(tt(L))(True if the tt(SHLVL) parameter is at least var(n).)
  sitem(tt(S))(True if the tt(SECONDS) parameter is at least var(n).)
  sitem(tt(v))(True if the array tt(psvar) has at least var(n) elements.)
*** Src/prompt.c.cnt	Wed Apr 29 23:42:51 1998
--- Src/prompt.c	Mon Nov  9 15:52:02 1998
***************
*** 71,76 ****
--- 71,80 ----
  
  static char *bp;
  
+ /* Position of the start of the current line in the buffer */
+ 
+ static char *bufline;
+ 
  /* bp1 is an auxilliary pointer into the buffer, which when non-NULL is *
   * moved whenever the buffer is reallocated.  It is used when data is   *
   * being temporarily held in the buffer.                                */
***************
*** 95,104 ****
  
  static char *rstring, *Rstring;
  
- /* If non-zero, Inpar, Outpar and Nularg can be added to the buffer. */
- 
- static int nonsp;
- 
  /* Perform prompt expansion on a string, putting the result in a *
   * permanently-allocated string.  If ns is non-zero, this string *
   * may have embedded Inpar and Outpar, which indicate a toggling *
--- 99,104 ----
***************
*** 130,138 ****
  
      rstring = rs;
      Rstring = Rs;
-     nonsp = ns;
      fm = s;
!     bp = buf = zalloc(bufspc = 256);
      bp1 = NULL;
      trunclen = 0;
      putpromptchar(1, '\0');
--- 130,137 ----
  
      rstring = rs;
      Rstring = Rs;
      fm = s;
!     bp = bufline = buf = zalloc(bufspc = 256);
      bp1 = NULL;
      trunclen = 0;
      putpromptchar(1, '\0');
***************
*** 140,145 ****
--- 139,153 ----
      if(dontcount)
  	*bp++ = Outpar;
      *bp = 0;
+     if (!ns) {
+ 	/* If zero, Inpar, Outpar and Nularg should be removed. */
+ 	for (bp = buf; *bp; bp++) {
+ 	    if (*bp == Meta)
+ 		bp++;
+ 	    else if (*bp == Inpar || *bp == Outpar || *bp == Nularg)
+ 		chuck(bp);
+ 	}
+     }
      return buf;
  }
  
***************
*** 224,229 ****
--- 232,243 ----
  		    if (getegid() == arg)
  			test = 1;
  		    break;
+ 		case 'l':
+ 		    *bp = '\0';
+ 		    countprompt(bufline, &t0, 0);
+ 		    if (t0 >= arg)
+ 			test = 1;
+ 		    break;
  		case 'L':
  		    if (shlvl >= arg)
  			test = 1;
***************
*** 436,448 ****
  		    return 0;
  		break;
  	    case '{': /*}*/
! 		if (!dontcount++ && nonsp) {
  		    addbufspc(1);
  		    *bp++ = Inpar;
  		}
  		break;
  	    case /*{*/ '}':
! 		if (dontcount && !--dontcount && nonsp) {
  		    addbufspc(1);
  		    *bp++ = Outpar;
  		}
--- 450,462 ----
  		    return 0;
  		break;
  	    case '{': /*}*/
! 		if (!dontcount++) {
  		    addbufspc(1);
  		    *bp++ = Inpar;
  		}
  		break;
  	    case /*{*/ '}':
! 		if (dontcount && !--dontcount) {
  		    addbufspc(1);
  		    *bp++ = Outpar;
  		}
***************
*** 604,609 ****
--- 618,625 ----
  	c ^= 32;
      }
      *bp++ = c;
+     if (c == '\n' && !dontcount)
+ 	bufline = bp;
  }
  
  /* Make sure there is room for `need' more characters in the buffer. */
***************
*** 684,695 ****
  	    tputs(tcstr[cap], 1, putshout);
  	    break;
  	case 1:
! 	    if (!dontcount && nonsp) {
  		addbufspc(1);
  		*bp++ = Inpar;
  	    }
  	    tputs(tcstr[cap], 1, putstr);
! 	    if (!dontcount && nonsp) {
  		int glitch = 0;
  
  		if (cap == TCSTANDOUTBEG || cap == TCSTANDOUTEND)
--- 700,711 ----
  	    tputs(tcstr[cap], 1, putshout);
  	    break;
  	case 1:
! 	    if (!dontcount) {
  		addbufspc(1);
  		*bp++ = Inpar;
  	    }
  	    tputs(tcstr[cap], 1, putstr);
! 	    if (!dontcount) {
  		int glitch = 0;
  
  		if (cap == TCSTANDOUTBEG || cap == TCSTANDOUTEND)

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxx>       Tel: +39 050 844536
WWW:  http://www.ifh.de/~pws/
Dipartimento di Fisica, Via Buonarotti 2, 56100 Pisa, Italy



Messages sorted by: Reverse Date, Date, Thread, Author