This adds a "clear-screen" editing command to the emacs editing mode.
This is the same name as bash and zsh uses, and then I stopped looking.

The default binding of 'redraw' remains for ^L, for now anyway, so
you'll need to run
  $ bind ^L=clear-screen"
when testing.

$CLEARSTR can be set to an arbitrary character sequence for clearing the 
screen, should ^[[H^[[J not fit the bill. For example,
  $ CLEARSTR=$(clear) 
Not sure we want or need this, or what could possibly be a better name
for it.

Thoughts? OK?

/Alexander


Index: emacs.c
===================================================================
RCS file: /cvs/src/bin/ksh/emacs.c,v
retrieving revision 1.84
diff -u -p -r1.84 emacs.c
--- emacs.c     16 Jan 2018 17:17:18 -0000      1.84
+++ emacs.c     5 Jun 2018 22:03:49 -0000
@@ -146,6 +146,7 @@ static int  isu8cont(unsigned char);
 /* proto's for keybindings */
 static int     x_abort(int);
 static int     x_beg_hist(int);
+static int     x_clear_screen(int);
 static int     x_comp_comm(int);
 static int     x_comp_file(int);
 static int     x_complete(int);
@@ -202,6 +203,7 @@ static int  x_debug_info(int);
 static const struct x_ftab x_ftab[] = {
        { x_abort,              "abort",                        0 },
        { x_beg_hist,           "beginning-of-history",         0 },
+       { x_clear_screen,       "clear-screen",                 0 },
        { x_comp_comm,          "complete-command",             0 },
        { x_comp_file,          "complete-file",                0 },
        { x_complete,           "complete",                     0 },
@@ -1004,12 +1006,19 @@ x_draw_line(int c)
 {
        x_redraw(-1);
        return KSTD;
+}
 
+static int
+x_clear_screen(int c)
+{
+       x_redraw(-2);
+       return KSTD;
 }
 
-/* Redraw (part of) the line.  If limit is < 0, the everything is redrawn
- * on a NEW line, otherwise limit is the screen column up to which needs
- * redrawing.
+/* Redraw (part of) the line.
+ * A non-negative limit is the screen column up to which needs
+ * redrawing. A limit of -1 redraws on a new line, while a limit
+ * of -2 (attempts to) clear the screen.
  */
 static void
 x_redraw(int limit)
@@ -1018,9 +1027,15 @@ x_redraw(int limit)
        char    *cp;
 
        x_adj_ok = 0;
-       if (limit == -1)
+       if (limit == -2) {
+               char *clearstr = str_val(global("CLEARSTR"));
+               if (clearstr == null)
+                       clearstr = "\033[H\033[J";
+               x_e_puts(clearstr);
+       }
+       else if (limit == -1)
                x_e_putc('\n');
-       else
+       else if (limit >= 0)
                x_e_putc('\r');
        x_flush();
        if (xbp == xbuf) {
Index: ksh.1
===================================================================
RCS file: /cvs/src/bin/ksh/ksh.1,v
retrieving revision 1.200
diff -u -p -r1.200 ksh.1
--- ksh.1       30 May 2018 21:20:52 -0000      1.200
+++ ksh.1       5 Jun 2018 22:03:49 -0000
@@ -1345,6 +1345,8 @@ Also, the
 .Ic cd
 built-in command will display the resulting directory when a match is found
 in any search path other than the empty path.
+.It Ev CLEARSTR
+If set, overrides the default escape sequence to clear the screen.
 .It Ev COLUMNS
 Set to the number of columns on the terminal or window.
 Currently set to the
@@ -4690,6 +4692,12 @@ Moves the cursor to the beginning of the
 Uppercase the first character in the next
 .Ar n
 words, leaving the cursor past the end of the last word.
+.It clear-screen:
+Clears the screen and redraws the prompt and current input line.
+If the
+.Ev CLEARSTR
+parameter is set, it is used to clear the screen.
+Otherwise, a default escape sequence (^[[H^[2J) is used.
 .It comment: ^[#
 If the current line does not begin with a comment character, one is added at
 the beginning of the line and the line is entered (as if return had been

Reply via email to