From 24598fed7c365133c463e7781faee547c0b2bf46 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 16 Feb 2021 00:07:03 +0000 Subject: [PATCH] Add new 'nobackslashctrl' shell option; other minor editor tweaks The following emacs editor 'feature' kept making me want to go back to bash. I forget a backslash escape in a command somewhere. So I go back to insert it. I type the \, then want to go forward. My right arrow key, instead of moving the cursor, then replaces my backslash with garbage. Why? The backslash escapes the following control character at the editor level and inserts it literally. The vi editor has a variant of this which is much less harmful. It only works in insert mode and the backslash only escapes the next kill or erase character. In both editors, this feature is completely redundant with the 'stty lnext' character which is ^V by default -- and works better as well because it also escapes ^C, ^J (linefeed) and ^M (Return). [In fact, you could even issue 'stty lnext \\' and get a much more consistent version of this feature on any shell. You have to type two backslashes to enter one, but it won't kill your cursor keys.] If it were up to me alone, I'd simply remove this misfeature from both editors. However, it is long-standing documented behaviour. It's in the 1995 book. Plus, POSIX specifies the vi variant of it. So, this adds a shell option instead. It was quite trivial to do. Now I can 'set --nobackslashctrl' in my ~/.kshrc. What a relief! Note: To keep .kshrc compatibile with older ksh versions, use: command set --nobackslashctrl 2>/dev/null src/cmd/ksh93/include/shell.h, src/cmd/ksh93/data/options.c: - Add new SH_NOBACKSLCTRL/"nobackslashctrl" long-form option. The "no" prefix shows it to the user as "backslashctrl" which is on by default. This avoids unexpectedly changing historic behaviour. src/cmd/ksh93/edit/emacs.c: ed_emacsread(), src/cmd/ksh93/edit/vi.c: getline(): - Only set the flag for special backslash handling if SH_NOBACKSLCTRL is off. src/cmd/ksh93/sh.1, src/cmd/ksh93/data/builtins.c: - Document the new option (as "backslashctrl", on by default). Other minor tweaks: src/cmd/ksh93/edit/edit.c: - ed_setup(): Add fallback #error if no tput method is set. This should never be triggered; it's to catch future editing mistakes. - escape(): cntl('\t') is nonsense as '\t' is already a control character, so change this to just '\t'. - xcommands(): Let's enable the ^X^D command for debugging information on non-release builds. src/cmd/ksh93/features/cmds: - The tput feature tests assumed a functioning terminal in $TERM. However, for all we know we might be compiling with no tty and TERM=dumb. The tput commands won't work then. So set TERM=ansi to use a standard default. --- NEWS | 11 +++++++++++ src/cmd/ksh93/data/builtins.c | 4 ++++ src/cmd/ksh93/data/options.c | 1 + src/cmd/ksh93/edit/edit.c | 2 ++ src/cmd/ksh93/edit/emacs.c | 6 +++--- src/cmd/ksh93/edit/vi.c | 2 +- src/cmd/ksh93/features/cmds | 4 ++++ src/cmd/ksh93/include/shell.h | 1 + src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/sh.1 | 25 +++++++++++++++++++++++-- 10 files changed, 51 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 8fc15785e..e1602b007 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,17 @@ For full details, see the git log at: https://github.com/ksh93/ksh Any uppercase BUG_* names are modernish shell bug IDs. +2021-02-15: + +- A new 'backslashctrl' shell option has been added. It is on by default. + Turning it off (set +o backslashctrl or set --nobackslashctrl) disables the + special escaping behaviour of the backslash character in the emacs and vi + built-in editors. Particularly in the emacs editor, this makes it much easier + to go back, insert a forgotten backslash into a command, and then continue + editing without having your next cursor key replace your backslash with + garbage. Note that Ctrl+V (or whatever other character was set using + 'stty lnext') always escapes all control characters in either editing mode. + 2021-02-14: - Due to a deficiency in some UNIX veriants, the 'sleep' built-in command could diff --git a/src/cmd/ksh93/data/builtins.c b/src/cmd/ksh93/data/builtins.c index 9d8ae86d3..bc4571045 100644 --- a/src/cmd/ksh93/data/builtins.c +++ b/src/cmd/ksh93/data/builtins.c @@ -204,6 +204,10 @@ const char sh_set[] = "off. This can be repeated to enable/disable multiple options. " "The value of \aoption\a must be one of the following:]{" "[+allexport?Equivalent to \b-a\b.]" + "[+backslashctrl?The backslash character \b\\\b escapes the " + "next control character in the \bemacs\b built-in " + "editor and the next \aerase\a or \akill\a character " + "in the \bvi\b built-in editor. On by default.]" "[+bgnice?Runs background jobs at lower priorities.]" #if SHOPT_BRACEPAT "[+braceexpand?Equivalent to \b-B\b.] " diff --git a/src/cmd/ksh93/data/options.c b/src/cmd/ksh93/data/options.c index c4e35aa32..f1e3019d2 100644 --- a/src/cmd/ksh93/data/options.c +++ b/src/cmd/ksh93/data/options.c @@ -31,6 +31,7 @@ const Shtable_t shtab_options[] = { "allexport", SH_ALLEXPORT, + "nobackslashctrl", SH_NOBACKSLCTRL, "bgnice", SH_BGNICE, #if SHOPT_BRACEPAT "braceexpand", SH_BRACEEXPAND, diff --git a/src/cmd/ksh93/edit/edit.c b/src/cmd/ksh93/edit/edit.c index 35d0f182d..0e85c671e 100644 --- a/src/cmd/ksh93/edit/edit.c +++ b/src/cmd/ksh93/edit/edit.c @@ -785,6 +785,8 @@ void ed_setup(register Edit_t *ep, int fd, int reedit) sh_trap(".sh.subscript=$(" _pth_tput " cuu1 2>/dev/null)",0); #elif _tput_termcap sh_trap(".sh.subscript=$(" _pth_tput " up 2>/dev/null)",0); +#else +#error no tput method #endif if(pp=nv_getval(SH_SUBSCRNOD)) strncpy(CURSOR_UP,pp,sizeof(CURSOR_UP)-1); diff --git a/src/cmd/ksh93/edit/emacs.c b/src/cmd/ksh93/edit/emacs.c index 007f7fec1..cefb4b033 100644 --- a/src/cmd/ksh93/edit/emacs.c +++ b/src/cmd/ksh93/edit/emacs.c @@ -378,7 +378,7 @@ int ed_emacsread(void *context, int fd,char *buff,int scend, int reedit) } for(i= ++eol; i>cur; i--) out[i] = out[i-1]; - backslash = (c == '\\'); + backslash = (c == '\\' && !sh_isoption(SH_NOBACKSLCTRL)); out[cur++] = c; draw(ep,APPEND); continue; @@ -1010,7 +1010,7 @@ static int escape(register Emacs_t* ep,register genchar *out,int count) if(ep->ed->e_tabcount==1) { ep->ed->e_tabcount=2; - ed_ungetchar(ep->ed,cntl('\t')); + ed_ungetchar(ep->ed,'\t'); return(-1); } beep(); @@ -1229,7 +1229,7 @@ static void xcommands(register Emacs_t *ep,int count) show_info(ep,hbuf); return; } -# if 0 /* debugging, modify as required */ +# if !AST_ksh_release /* debugging, modify as required */ case cntl('D'): /* ^X^D show debugging info */ { char debugbuf[MAXLINE]; diff --git a/src/cmd/ksh93/edit/vi.c b/src/cmd/ksh93/edit/vi.c index 0f31b57a6..cb6d7dea7 100644 --- a/src/cmd/ksh93/edit/vi.c +++ b/src/cmd/ksh93/edit/vi.c @@ -1583,7 +1583,7 @@ static void getline(register Vi_t* vp,register int mode) mode = APPEND; max_virt = last_virt+3; } - backslash = (c == '\\'); + backslash = (c == '\\' && !sh_isoption(SH_NOBACKSLCTRL)); append(vp,c, mode); break; } diff --git a/src/cmd/ksh93/features/cmds b/src/cmd/ksh93/features/cmds index 1554c68d9..87d211c0d 100644 --- a/src/cmd/ksh93/features/cmds +++ b/src/cmd/ksh93/features/cmds @@ -7,6 +7,8 @@ pth ed fail{ pth tput tput_terminfo note{ does tput support terminfo codes }end run{ + TERM=ansi + export TERM case ${_pth_tput-} in \"/*/tput\") tput=`echo "${_pth_tput}" | sed 's/^"//; s/"$//'` @@ -21,6 +23,8 @@ tput_terminfo note{ does tput support terminfo codes }end run{ }end tput_termcap note{ does tput support termcap codes }end run{ + TERM=ansi + export TERM case ${_pth_tput-} in \"/*/tput\") tput=`echo "${_pth_tput}" | sed 's/^"//; s/"$//'` diff --git a/src/cmd/ksh93/include/shell.h b/src/cmd/ksh93/include/shell.h index 491d54eee..b1db8d9ae 100644 --- a/src/cmd/ksh93/include/shell.h +++ b/src/cmd/ksh93/include/shell.h @@ -132,6 +132,7 @@ typedef union Shnode_u Shnode_t; #endif #define SH_POSIX 46 #define SH_MULTILINE 47 +#define SH_NOBACKSLCTRL 48 #define SH_LOGIN_SHELL 67 #define SH_NOUSRPROFILE 79 /* internal use only */ #define SH_COMMANDLINE 0x100 /* bit flag for invocation-only options ('set -o' cannot change them) */ diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 83344971d..ce73daf45 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -20,7 +20,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.0.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2021-02-14" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2021-02-15" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2021 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh.1 b/src/cmd/ksh93/sh.1 index 548b84c13..f5bd3aa2f 100644 --- a/src/cmd/ksh93/sh.1 +++ b/src/cmd/ksh93/sh.1 @@ -4964,7 +4964,9 @@ Multiply parameter of next command by 4. .PP .TP 10 .BI \e -Escape next character. +If the +.B backslashctrl +shell option is on (which is the default setting), this escapes the next character. Editing characters, the user's erase, kill and interrupt (normally .BR ^? ) @@ -4976,6 +4978,9 @@ The .B \e removes the next character's editing features (if any). +See also +.I lnext +which is not subject to any shell option. .PP .TP 10 .B M-^V @@ -5074,7 +5079,9 @@ On some systems the \f3viraw\fP option may be required for this to work. .TP 10 .BI \e -Escape the next +If the +.B backslashctrl +shell option is on (which is the default setting), this escapes the next .I erase or .I kill @@ -7004,6 +7011,20 @@ The following argument can be one of the following option names: Same as .BR \-a . .TP 8 +.B backslashctrl +The backslash character +.B \\\\ +escapes the next control character in the +.B emacs +built-in editor and the next +.I erase +or +.I kill +character in the +.B vi +built-in editor. +On by default. +.TP 8 .B bgnice All background jobs are run at a lower priority. This is the default mode.