Module Name:    src
Committed By:   kre
Date:           Wed Jun 28 13:46:06 UTC 2017

Modified Files:
        src/bin/sh: histedit.c myhistedit.h sh.1 var.c var.h

Log Message:
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).


To generate a diff of this commit:
cvs rdiff -u -r1.51 -r1.52 src/bin/sh/histedit.c
cvs rdiff -u -r1.12 -r1.13 src/bin/sh/myhistedit.h
cvs rdiff -u -r1.155 -r1.156 src/bin/sh/sh.1
cvs rdiff -u -r1.61 -r1.62 src/bin/sh/var.c
cvs rdiff -u -r1.33 -r1.34 src/bin/sh/var.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/bin/sh/histedit.c
diff -u src/bin/sh/histedit.c:1.51 src/bin/sh/histedit.c:1.52
--- src/bin/sh/histedit.c:1.51	Tue Jun 27 23:27:03 2017
+++ src/bin/sh/histedit.c	Wed Jun 28 13:46:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: histedit.c,v 1.51 2017/06/27 23:27:03 christos Exp $	*/
+/*	$NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)histedit.c	8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: histedit.c,v 1.51 2017/06/27 23:27:03 christos Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.52 2017/06/28 13:46:06 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -134,7 +134,8 @@ histedit(void)
 			if (el != NULL) {
 				if (hist)
 					el_set(el, EL_HIST, history, hist);
-				el_set(el, EL_PROMPT_ESC, getprompt, L'\1');
+
+				set_prompt_lit(lookupvar("PSlit"));
 				el_set(el, EL_SIGNAL, 1);
 				el_set(el, EL_ALIAS_TEXT, alias_text, NULL);
 				el_set(el, EL_ADDFN, "rl-complete",
@@ -175,6 +176,27 @@ bad:
 }
 
 void
+set_prompt_lit(const char *lit_ch)
+{
+	wchar_t wc;
+
+	if (!(iflag && editing && el))
+		return;
+
+	if (lit_ch == NULL) {
+		el_set(el, EL_PROMPT, getprompt);
+		return;
+	}
+
+	mbtowc(&wc, NULL, 1);		/* state init */
+
+	if (mbtowc(&wc, lit_ch, strlen(lit_ch)) <= 0)
+		el_set(el, EL_PROMPT, getprompt);
+	else
+		el_set(el, EL_PROMPT_ESC, getprompt, (int)wc);
+}
+
+void
 set_editrc(const char *fname)
 {
 	if (iflag && editing && el)

Index: src/bin/sh/myhistedit.h
diff -u src/bin/sh/myhistedit.h:1.12 src/bin/sh/myhistedit.h:1.13
--- src/bin/sh/myhistedit.h:1.12	Tue Jun 27 02:22:08 2017
+++ src/bin/sh/myhistedit.h	Wed Jun 28 13:46:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: myhistedit.h,v 1.12 2017/06/27 02:22:08 kre Exp $	*/
+/*	$NetBSD: myhistedit.h,v 1.13 2017/06/28 13:46:06 kre Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -42,6 +42,7 @@ void sethistsize(const char *);
 void setterm(const char *);
 int inputrc(int, char **);
 void set_editrc(const char *);
+void set_prompt_lit(const char *);
 int not_fcnumber(char *);
 int str_to_event(const char *, int);
 

Index: src/bin/sh/sh.1
diff -u src/bin/sh/sh.1:1.155 src/bin/sh/sh.1:1.156
--- src/bin/sh/sh.1:1.155	Tue Jun 27 12:43:44 2017
+++ src/bin/sh/sh.1	Wed Jun 28 13:46:06 2017
@@ -1,4 +1,4 @@
-.\"	$NetBSD: sh.1,v 1.155 2017/06/27 12:43:44 kre Exp $
+.\"	$NetBSD: sh.1,v 1.156 2017/06/28 13:46:06 kre Exp $
 .\" Copyright (c) 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
 .\"
@@ -31,7 +31,7 @@
 .\"
 .\"	@(#)sh.1	8.6 (Berkeley) 5/4/95
 .\"
-.Dd June 17, 2017
+.Dd June 28, 2017
 .Dt SH 1
 .ds flags abCEeFfhnuvxIimpqV
 .Os
@@ -2756,9 +2756,109 @@ for the commands that can be given to co
 .Xr editline 7
 in the file named by the
 .Ev EDITRC
-parameter, or using
+parameter,
+or a file used with the
+.Ic inputrc
+built-in command,
+or using
 .Xr editline 7 Ap s
 configuration command line.
+.Pp
+When command line editing is enabled, the
+.Xr editline 7
+functions control printing of the
+.Ev PS1
+and
+.Ev PS2
+prompts when required.
+As, in this mode, the command line editor needs to
+keep track of what characters are in what position on
+the command line, care needs to be taken when setting
+the prompts.
+Normal printing characters are handled automatically,
+however mode setting sequences, which do not actually display
+on the terminal, need to be identified to
+.Xr editline 7 .
+This is done, when needed, by choosing a character that
+is not needed anywhere in the prompt, including in the mode
+setting sequences, any single character is acceptable,
+and assigning it to the shell parameter
+.Ev PSlit .
+Then that character should be used, in pairs, in the
+prompt string.
+Between each pair of
+.Ev PSlit
+characters are mode setting sequences, which affect the printing
+attributes of the following (normal) characters of the prompt,
+but do not themselves appear visibly, nor change the terminal's
+cursor position.
+.Pp
+Each such sequence, that is
+.Ev PSlit
+character, mode setting character sequence, and another
+.Ev PSlit
+character, must currently be followed by at least one following
+normal prompt character, or it will be ignored.
+That is, a
+.Ev PSlit
+character cannot be the final character of
+.Ev PS1
+or
+.Ev PS2 ,
+nor may two
+.Ev PSlit
+delimited sequences appear adjacent to each other.
+Each sequence can contain as many mode altering sequences as are
+required however.
+Only the first character from
+.Ev PSlit
+will be used.
+When set
+.Ev PSlit
+should usually be set to a string containing just one
+character, then it can simply be embedded in
+.Ev PS1
+(or
+.Ev PS2 )
+as in
+.Dl PS1="${PSlit}mset${PSlit}XYZ${PSlit}mclr${PSlit}ABC"
+The prompt visible will be
+.Dq XYZABC
+with the
+.Dq XYZ
+part shown according as defined by the mode setting characters
+.Dq mset ,
+and then cleared again by
+.Dq mclr .
+See
+.Xr tput 1
+for one method to generate appropriate mode sequences.
+Note that both parts, XYZ and ABC, must each contain at least one
+character.
+.Pp
+If
+.Ev PSlit
+is unset, which is its initial state, or set to a null string,
+no literal character will be defined,
+and all characters of the prompt strings will be assumed
+to be visible characters (which includes spaces etc.)
+To allow smooth use of prompts, without needing redefinition, when
+.Xr editline 7
+is disabled, the character chosen should be one which will be
+ignored by the terminal if received, as when
+.Xr edlitline 7
+is not in use, the prompt strings are simply written to the terminal.
+For example, setting:
+.Bd -compact -literal -offset left
+  PSlit="$(printf\ '\e1')"
+  PS1="${PSlit}$(tput\ bold\ blink)${PSlit}\e$${PSlit}$(tput\ sgr0)${PSlit}\ "
+.Ed
+will arrange for the primary prompt to be a bold blinking dollar sign,
+if supported by the current terminal, followed by an (ordinary) space,
+and, as the SOH (Control-A) character ('\e1') will not normally affect
+a terminal, this same prompt will usually work with
+.Xr editline 7
+enabled or disabled.
 .Sh ENVIRONMENT
 .Bl -tag -width MAILCHECK
 .It Ev CDPATH
@@ -2868,6 +2968,22 @@ The secondary prompt string, which defau
 Output before each line when execution trace (set -x) is enabled,
 defaults to
 .Dq + \  .
+.It Ev PSlit
+Defines the character which may be embedded in pairs, in
+.Ev PS1
+or
+.Ev PS2
+to indicate to
+.Xr editline 7
+that the characters between each pair of occurrences of the
+.Ev PSlit
+character will not appear in the visible prompt, and will not
+cause the terminal's cursor to change position, but rather set terminal
+attributes for the following prompt character(s) at least one of
+which must be present.
+See
+.Sx Command Line Editing
+above for more information.
 .It Ev TERM
 The default terminal setting for the shell.
 This is inherited by

Index: src/bin/sh/var.c
diff -u src/bin/sh/var.c:1.61 src/bin/sh/var.c:1.62
--- src/bin/sh/var.c:1.61	Tue Jun 27 02:22:08 2017
+++ src/bin/sh/var.c	Wed Jun 28 13:46:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.61 2017/06/27 02:22:08 kre Exp $	*/
+/*	$NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.61 2017/06/27 02:22:08 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.62 2017/06/28 13:46:06 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -96,6 +96,7 @@ struct localvar *localvars;
 struct var vhistsize;
 struct var vterm;
 struct var editrc;
+struct var ps_lit;
 #endif
 struct var vifs;
 struct var vmail;
@@ -142,6 +143,8 @@ const struct varinit varinit[] = {
 	   { .set_func= setterm } },
 	{ &editrc, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"EDITRC=",
 	   { .set_func= set_editrc } },
+	{ &ps_lit, 	VSTRFIXED|VTEXTFIXED|VUNSET,	"PSlit=",
+	   { .set_func= set_prompt_lit } },
 #endif
 	{ &voptind,	VSTRFIXED|VTEXTFIXED|VNOFUNC,	"OPTIND=1",
 	   { .set_func= getoptsreset } },

Index: src/bin/sh/var.h
diff -u src/bin/sh/var.h:1.33 src/bin/sh/var.h:1.34
--- src/bin/sh/var.h:1.33	Tue Jun 27 02:22:08 2017
+++ src/bin/sh/var.h	Wed Jun 28 13:46:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.h,v 1.33 2017/06/27 02:22:08 kre Exp $	*/
+/*	$NetBSD: var.h,v 1.34 2017/06/28 13:46:06 kre Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -89,11 +89,12 @@ extern struct var vps1;
 extern struct var vps2;
 extern struct var vps4;
 extern struct var line_num;
-extern struct var editrc;
 #ifndef SMALL
+extern struct var editrc;
 extern struct var vterm;
 extern struct var vtermcap;
 extern struct var vhistsize;
+extern struct var ps_lit;
 #endif
 
 extern int line_number;

Reply via email to