Re: ksh "clear-screen" for vi mode
On 20 Sep 06:14, Todd C. Miller wrote: > On Sun, 20 Sep 2020 05:39:02 +0200, Theo Buehler wrote: > > > This works and appears to match bash's behavior in that it only works > > in normal mode. I would slightly prefer to also add the command to the > > nonstandard vi commands in the switch around line 650 to have it > > available from insert mode as well. This would match zsh's behavior. > > Sure, I was comparing it to bash so didn't support insert mode. > I agree that it's more useful to have it available there too. > > - todd > > Index: bin/ksh/ksh.1 > === > RCS file: /cvs/src/bin/ksh/ksh.1,v > retrieving revision 1.209 > diff -u -p -u -r1.209 ksh.1 > --- bin/ksh/ksh.1 7 Jul 2020 10:33:58 - 1.209 > +++ bin/ksh/ksh.1 20 Sep 2020 12:12:01 - > @@ -5053,6 +5053,12 @@ Erases previous character. > .It ^J | ^M > End of line. > The current line is read, parsed, and executed by the shell. > +.It ^L > +Clear screen. > +The screen is cleared if the > +.Ev TERM > +parameter is set and the terminal supports clearing the screen. > +The prompt string and the current line are redrawn. > .It ^V > Literal next. > The next character typed is not treated specially (can be used > Index: bin/ksh/vi.c > === > RCS file: /cvs/src/bin/ksh/vi.c,v > retrieving revision 1.56 > diff -u -p -u -r1.56 vi.c > --- bin/ksh/vi.c 15 Mar 2018 16:51:29 - 1.56 > +++ bin/ksh/vi.c 20 Sep 2020 12:02:38 - > @@ -14,12 +14,14 @@ > #include > #include > #include > +#ifndef SMALL > +# include > +# include > +#endif > > #include "sh.h" > #include "edit.h" > > -#define CTRL(c) (c & 0x1f) > - > struct edstate { > char*cbuf; /* main buffer to build the command line */ > int cbufsize; /* number of bytes allocated for cbuf */ > @@ -52,8 +54,9 @@ static int Backword(int); > static int Endword(int); > static int grabhist(int, int); > static int grabsearch(int, int, int, char *); > +static void do_clear_screen(void); > static void redraw_line(int); > -static void refresh(int); > +static void refresh_line(int); > static int outofwin(void); > static void rewindow(void); > static int newcol(int, int); > @@ -271,9 +274,9 @@ vi_hook(int ch) > case 0: > if (state == VLIT) { > es->cursor--; > - refresh(0); > + refresh_line(0); > } else > - refresh(insert != 0); > + refresh_line(insert != 0); > break; > case 1: > return 1; > @@ -298,7 +301,7 @@ vi_hook(int ch) > return -1; > } else if (putbuf("?", 1, 0) != 0) > return -1; > - refresh(0); > + refresh_line(0); > } > } > } > @@ -310,7 +313,7 @@ vi_hook(int ch) > vi_error(); > } else > es->cbuf[es->cursor++] = ch; > - refresh(1); > + refresh_line(1); > state = VNORMAL; > break; > > @@ -375,7 +378,7 @@ vi_hook(int ch) > if (!srchpat[0]) { > vi_error(); > state = VNORMAL; > - refresh(0); > + refresh_line(0); > return 0; > } > } else { > @@ -392,17 +395,17 @@ vi_hook(int ch) > } while (srchlen > 0 && > isu8cont(locpat[srchlen])); > es->cursor = es->linelen; > - refresh(0); > + refresh_line(0); > return 0; > } > restore_cbuf(); > state = VNORMAL; > - refresh(0); > + refresh_line(0); > } else if (ch == edchars.kill) { > srchlen = 0; > es->linelen = 1; > es->cursor = 1; > - refresh(0); > + refresh_line(0); > return 0; > } else if (ch == edchars.werase) { > struct edstate new_es, *save_es; > @@ -421,7 +424,7 @@ vi_hook(int ch) >
Re: ksh "clear-screen" for vi mode
On 20 Sep 05:39, Theo Buehler wrote: > On Sat, Sep 19, 2020 at 03:50:52PM -0600, Todd C. Miller wrote: > > The vi and emacs edit code are completely separate. Try the following > > diff. I had to rename a few things to avoid clashing with ncurses.h. > > This works and appears to match bash's behavior in that it only works > in normal mode. I would slightly prefer to also add the command to the > nonstandard vi commands in the switch around line 650 to have it > available from insert mode as well. This would match zsh's behavior. That would be really great. > > Either way, ok tb >
Re: ksh "clear-screen" for vi mode
On Sun, Sep 20, 2020 at 08:19:24AM -0600, Todd C. Miller wrote: > Does this look better? I don't think we need to refer to the emacs > clear-screen command in both cases; once should be sufficient. > > - todd > i think it reads fine. jmc > Index: bin/ksh/ksh.1 > === > RCS file: /cvs/src/bin/ksh/ksh.1,v > retrieving revision 1.209 > diff -u -p -u -r1.209 ksh.1 > --- bin/ksh/ksh.1 7 Jul 2020 10:33:58 - 1.209 > +++ bin/ksh/ksh.1 20 Sep 2020 14:16:50 - > @@ -5053,6 +5053,13 @@ Erases previous character. > .It ^J | ^M > End of line. > The current line is read, parsed, and executed by the shell. > +.It ^L > +Clear the screen (if possible) and redraw the current line. > +See the > +.Em clear-screen > +command in > +.Sx Emacs editing mode > +for more information. > .It ^V > Literal next. > The next character typed is not treated specially (can be used > @@ -5510,7 +5517,9 @@ Miscellaneous vi commands > .Bl -tag -width Ds > .It ^J and ^M > The current line is read, parsed, and executed by the shell. > -.It ^L and ^R > +.It ^L > +Clear the screen (if possible) and redraw the current line. > +.It ^R > Redraw the current line. > .It Xo > .Oo Ar n Oc Ns \&. > Index: bin/ksh/vi.c > === > RCS file: /cvs/src/bin/ksh/vi.c,v > retrieving revision 1.56 > diff -u -p -u -r1.56 vi.c > --- bin/ksh/vi.c 15 Mar 2018 16:51:29 - 1.56 > +++ bin/ksh/vi.c 20 Sep 2020 12:02:38 - > @@ -14,12 +14,14 @@ > #include > #include > #include > +#ifndef SMALL > +# include > +# include > +#endif > > #include "sh.h" > #include "edit.h" > > -#define CTRL(c) (c & 0x1f) > - > struct edstate { > char*cbuf; /* main buffer to build the command line */ > int cbufsize; /* number of bytes allocated for cbuf */ > @@ -52,8 +54,9 @@ static int Backword(int); > static int Endword(int); > static int grabhist(int, int); > static int grabsearch(int, int, int, char *); > +static void do_clear_screen(void); > static void redraw_line(int); > -static void refresh(int); > +static void refresh_line(int); > static int outofwin(void); > static void rewindow(void); > static int newcol(int, int); > @@ -271,9 +274,9 @@ vi_hook(int ch) > case 0: > if (state == VLIT) { > es->cursor--; > - refresh(0); > + refresh_line(0); > } else > - refresh(insert != 0); > + refresh_line(insert != 0); > break; > case 1: > return 1; > @@ -298,7 +301,7 @@ vi_hook(int ch) > return -1; > } else if (putbuf("?", 1, 0) != 0) > return -1; > - refresh(0); > + refresh_line(0); > } > } > } > @@ -310,7 +313,7 @@ vi_hook(int ch) > vi_error(); > } else > es->cbuf[es->cursor++] = ch; > - refresh(1); > + refresh_line(1); > state = VNORMAL; > break; > > @@ -375,7 +378,7 @@ vi_hook(int ch) > if (!srchpat[0]) { > vi_error(); > state = VNORMAL; > - refresh(0); > + refresh_line(0); > return 0; > } > } else { > @@ -392,17 +395,17 @@ vi_hook(int ch) > } while (srchlen > 0 && > isu8cont(locpat[srchlen])); > es->cursor = es->linelen; > - refresh(0); > + refresh_line(0); > return 0; > } > restore_cbuf(); > state = VNORMAL; > - refresh(0); > + refresh_line(0); > } else if (ch == edchars.kill) { > srchlen = 0; > es->linelen = 1; > es->cursor = 1; > - refresh(0); > + refresh_line(0); > return 0; > } else if (ch == edchars.werase) { > struct edstate new_es, *save_es; > @@ -421,7 +424,7 @@ vi_hook(int ch) >
Re: ksh "clear-screen" for vi mode
Does this look better? I don't think we need to refer to the emacs clear-screen command in both cases; once should be sufficient. - todd Index: bin/ksh/ksh.1 === RCS file: /cvs/src/bin/ksh/ksh.1,v retrieving revision 1.209 diff -u -p -u -r1.209 ksh.1 --- bin/ksh/ksh.1 7 Jul 2020 10:33:58 - 1.209 +++ bin/ksh/ksh.1 20 Sep 2020 14:16:50 - @@ -5053,6 +5053,13 @@ Erases previous character. .It ^J | ^M End of line. The current line is read, parsed, and executed by the shell. +.It ^L +Clear the screen (if possible) and redraw the current line. +See the +.Em clear-screen +command in +.Sx Emacs editing mode +for more information. .It ^V Literal next. The next character typed is not treated specially (can be used @@ -5510,7 +5517,9 @@ Miscellaneous vi commands .Bl -tag -width Ds .It ^J and ^M The current line is read, parsed, and executed by the shell. -.It ^L and ^R +.It ^L +Clear the screen (if possible) and redraw the current line. +.It ^R Redraw the current line. .It Xo .Oo Ar n Oc Ns \&. Index: bin/ksh/vi.c === RCS file: /cvs/src/bin/ksh/vi.c,v retrieving revision 1.56 diff -u -p -u -r1.56 vi.c --- bin/ksh/vi.c15 Mar 2018 16:51:29 - 1.56 +++ bin/ksh/vi.c20 Sep 2020 12:02:38 - @@ -14,12 +14,14 @@ #include #include #include +#ifndef SMALL +# include +# include +#endif #include "sh.h" #include "edit.h" -#define CTRL(c)(c & 0x1f) - struct edstate { char*cbuf; /* main buffer to build the command line */ int cbufsize; /* number of bytes allocated for cbuf */ @@ -52,8 +54,9 @@ static intBackword(int); static int Endword(int); static int grabhist(int, int); static int grabsearch(int, int, int, char *); +static voiddo_clear_screen(void); static voidredraw_line(int); -static voidrefresh(int); +static voidrefresh_line(int); static int outofwin(void); static voidrewindow(void); static int newcol(int, int); @@ -271,9 +274,9 @@ vi_hook(int ch) case 0: if (state == VLIT) { es->cursor--; - refresh(0); + refresh_line(0); } else - refresh(insert != 0); + refresh_line(insert != 0); break; case 1: return 1; @@ -298,7 +301,7 @@ vi_hook(int ch) return -1; } else if (putbuf("?", 1, 0) != 0) return -1; - refresh(0); + refresh_line(0); } } } @@ -310,7 +313,7 @@ vi_hook(int ch) vi_error(); } else es->cbuf[es->cursor++] = ch; - refresh(1); + refresh_line(1); state = VNORMAL; break; @@ -375,7 +378,7 @@ vi_hook(int ch) if (!srchpat[0]) { vi_error(); state = VNORMAL; - refresh(0); + refresh_line(0); return 0; } } else { @@ -392,17 +395,17 @@ vi_hook(int ch) } while (srchlen > 0 && isu8cont(locpat[srchlen])); es->cursor = es->linelen; - refresh(0); + refresh_line(0); return 0; } restore_cbuf(); state = VNORMAL; - refresh(0); + refresh_line(0); } else if (ch == edchars.kill) { srchlen = 0; es->linelen = 1; es->cursor = 1; - refresh(0); + refresh_line(0); return 0; } else if (ch == edchars.werase) { struct edstate new_es, *save_es; @@ -421,7 +424,7 @@ vi_hook(int ch) es->linelen -= char_len((unsigned char)locpat[i]); srchlen = n; es->cursor = es->linelen; - refresh(0); +
Re: ksh "clear-screen" for vi mode
On Sun, Sep 20, 2020 at 06:14:22AM -0600, Todd C. Miller wrote: > On Sun, 20 Sep 2020 05:39:02 +0200, Theo Buehler wrote: > > > This works and appears to match bash's behavior in that it only works > > in normal mode. I would slightly prefer to also add the command to the > > nonstandard vi commands in the switch around line 650 to have it > > available from insert mode as well. This would match zsh's behavior. > > Sure, I was comparing it to bash so didn't support insert mode. > I agree that it's more useful to have it available there too. Works as expected, I cannot spot any regression either. OK kn, but the could need further tweaks. > Index: bin/ksh/ksh.1 > === > RCS file: /cvs/src/bin/ksh/ksh.1,v > retrieving revision 1.209 > diff -u -p -u -r1.209 ksh.1 > --- bin/ksh/ksh.1 7 Jul 2020 10:33:58 - 1.209 > +++ bin/ksh/ksh.1 20 Sep 2020 12:12:01 - > @@ -5053,6 +5053,12 @@ Erases previous character. > .It ^J | ^M > End of line. > The current line is read, parsed, and executed by the shell. > +.It ^L > +Clear screen. > +The screen is cleared if the > +.Ev TERM > +parameter is set and the terminal supports clearing the screen. > +The prompt string and the current line are redrawn. This duplicates the `clear-screen' documentation in the "Emacs editing mode" paragraph, perhaps just refer to that? Otherwise it seems harder than necessary to grep for the same functionality in two places. Also, at the end of "Vi editing mode" there's another mention of `^L' which needs updating now. > .It ^V > Literal next. > The next character typed is not treated specially (can be used
Re: ksh "clear-screen" for vi mode
On Sun, 20 Sep 2020 05:39:02 +0200, Theo Buehler wrote: > This works and appears to match bash's behavior in that it only works > in normal mode. I would slightly prefer to also add the command to the > nonstandard vi commands in the switch around line 650 to have it > available from insert mode as well. This would match zsh's behavior. Sure, I was comparing it to bash so didn't support insert mode. I agree that it's more useful to have it available there too. - todd Index: bin/ksh/ksh.1 === RCS file: /cvs/src/bin/ksh/ksh.1,v retrieving revision 1.209 diff -u -p -u -r1.209 ksh.1 --- bin/ksh/ksh.1 7 Jul 2020 10:33:58 - 1.209 +++ bin/ksh/ksh.1 20 Sep 2020 12:12:01 - @@ -5053,6 +5053,12 @@ Erases previous character. .It ^J | ^M End of line. The current line is read, parsed, and executed by the shell. +.It ^L +Clear screen. +The screen is cleared if the +.Ev TERM +parameter is set and the terminal supports clearing the screen. +The prompt string and the current line are redrawn. .It ^V Literal next. The next character typed is not treated specially (can be used Index: bin/ksh/vi.c === RCS file: /cvs/src/bin/ksh/vi.c,v retrieving revision 1.56 diff -u -p -u -r1.56 vi.c --- bin/ksh/vi.c15 Mar 2018 16:51:29 - 1.56 +++ bin/ksh/vi.c20 Sep 2020 12:02:38 - @@ -14,12 +14,14 @@ #include #include #include +#ifndef SMALL +# include +# include +#endif #include "sh.h" #include "edit.h" -#define CTRL(c)(c & 0x1f) - struct edstate { char*cbuf; /* main buffer to build the command line */ int cbufsize; /* number of bytes allocated for cbuf */ @@ -52,8 +54,9 @@ static intBackword(int); static int Endword(int); static int grabhist(int, int); static int grabsearch(int, int, int, char *); +static voiddo_clear_screen(void); static voidredraw_line(int); -static voidrefresh(int); +static voidrefresh_line(int); static int outofwin(void); static voidrewindow(void); static int newcol(int, int); @@ -271,9 +274,9 @@ vi_hook(int ch) case 0: if (state == VLIT) { es->cursor--; - refresh(0); + refresh_line(0); } else - refresh(insert != 0); + refresh_line(insert != 0); break; case 1: return 1; @@ -298,7 +301,7 @@ vi_hook(int ch) return -1; } else if (putbuf("?", 1, 0) != 0) return -1; - refresh(0); + refresh_line(0); } } } @@ -310,7 +313,7 @@ vi_hook(int ch) vi_error(); } else es->cbuf[es->cursor++] = ch; - refresh(1); + refresh_line(1); state = VNORMAL; break; @@ -375,7 +378,7 @@ vi_hook(int ch) if (!srchpat[0]) { vi_error(); state = VNORMAL; - refresh(0); + refresh_line(0); return 0; } } else { @@ -392,17 +395,17 @@ vi_hook(int ch) } while (srchlen > 0 && isu8cont(locpat[srchlen])); es->cursor = es->linelen; - refresh(0); + refresh_line(0); return 0; } restore_cbuf(); state = VNORMAL; - refresh(0); + refresh_line(0); } else if (ch == edchars.kill) { srchlen = 0; es->linelen = 1; es->cursor = 1; - refresh(0); + refresh_line(0); return 0; } else if (ch == edchars.werase) { struct edstate new_es, *save_es; @@ -421,7 +424,7 @@ vi_hook(int ch) es->linelen -= char_len((unsigned char)locpat[i]); srchlen = n; es->cur
Re: ksh "clear-screen" for vi mode
On Sun, Sep 20, 2020 at 05:39:02AM +0200, Theo Buehler wrote: > On Sat, Sep 19, 2020 at 03:50:52PM -0600, Todd C. Miller wrote: > > The vi and emacs edit code are completely separate. Try the following > > diff. I had to rename a few things to avoid clashing with ncurses.h. > > This works and appears to match bash's behavior in that it only works > in normal mode. I would slightly prefer to also add the command to the > nonstandard vi commands in the switch around line 650 to have it > available from insert mode as well. This would match zsh's behavior. Exactly. > Either way, ok tb OK kn for this diff just works and everything else is extra.
Re: ksh "clear-screen" for vi mode
On Sat, Sep 19, 2020 at 03:50:52PM -0600, Todd C. Miller wrote: > The vi and emacs edit code are completely separate. Try the following > diff. I had to rename a few things to avoid clashing with ncurses.h. This works and appears to match bash's behavior in that it only works in normal mode. I would slightly prefer to also add the command to the nonstandard vi commands in the switch around line 650 to have it available from insert mode as well. This would match zsh's behavior. Either way, ok tb
Re: ksh "clear-screen" for vi mode
The vi and emacs edit code are completely separate. Try the following diff. I had to rename a few things to avoid clashing with ncurses.h. - todd Index: bin/ksh/vi.c === RCS file: /cvs/src/bin/ksh/vi.c,v retrieving revision 1.56 diff -u -p -u -r1.56 vi.c --- bin/ksh/vi.c15 Mar 2018 16:51:29 - 1.56 +++ bin/ksh/vi.c19 Sep 2020 21:48:31 - @@ -14,12 +14,14 @@ #include #include #include +#ifndef SMALL +# include +# include +#endif #include "sh.h" #include "edit.h" -#define CTRL(c)(c & 0x1f) - struct edstate { char*cbuf; /* main buffer to build the command line */ int cbufsize; /* number of bytes allocated for cbuf */ @@ -52,8 +54,9 @@ static intBackword(int); static int Endword(int); static int grabhist(int, int); static int grabsearch(int, int, int, char *); +static voiddo_clear_screen(void); static voidredraw_line(int); -static voidrefresh(int); +static voidrefresh_line(int); static int outofwin(void); static voidrewindow(void); static int newcol(int, int); @@ -271,9 +274,9 @@ vi_hook(int ch) case 0: if (state == VLIT) { es->cursor--; - refresh(0); + refresh_line(0); } else - refresh(insert != 0); + refresh_line(insert != 0); break; case 1: return 1; @@ -298,7 +301,7 @@ vi_hook(int ch) return -1; } else if (putbuf("?", 1, 0) != 0) return -1; - refresh(0); + refresh_line(0); } } } @@ -310,7 +313,7 @@ vi_hook(int ch) vi_error(); } else es->cbuf[es->cursor++] = ch; - refresh(1); + refresh_line(1); state = VNORMAL; break; @@ -375,7 +378,7 @@ vi_hook(int ch) if (!srchpat[0]) { vi_error(); state = VNORMAL; - refresh(0); + refresh_line(0); return 0; } } else { @@ -392,17 +395,17 @@ vi_hook(int ch) } while (srchlen > 0 && isu8cont(locpat[srchlen])); es->cursor = es->linelen; - refresh(0); + refresh_line(0); return 0; } restore_cbuf(); state = VNORMAL; - refresh(0); + refresh_line(0); } else if (ch == edchars.kill) { srchlen = 0; es->linelen = 1; es->cursor = 1; - refresh(0); + refresh_line(0); return 0; } else if (ch == edchars.werase) { struct edstate new_es, *save_es; @@ -421,7 +424,7 @@ vi_hook(int ch) es->linelen -= char_len((unsigned char)locpat[i]); srchlen = n; es->cursor = es->linelen; - refresh(0); + refresh_line(0); return 0; } else { if (srchlen == SRCHLEN - 1) @@ -446,7 +449,7 @@ vi_hook(int ch) es->cbuf[es->linelen++] = ch; } es->cursor = es->linelen; - refresh(0); + refresh_line(0); } return 0; } @@ -459,15 +462,15 @@ vi_hook(int ch) switch (vi_cmd(argc1, curcmd)) { case -1: vi_error(); - refresh(0); + refresh_line(0); break; case 0: if (insert != 0) inslen = 0; - refresh(insert != 0); + refresh_line(
ksh "clear-screen" for vi mode
Hi everyone, I would like to add a clear screen feature for the vi mode of ksh. I have seen that it has been done for the emacs mode last year and wanned to adapt that code to vi. But it isn't quite a success for now because it's hard for me to get were the clear screen operation is actually written in emacs.c and it is also probably due to the fact that I'm not really used to C. Here is a sum up of my understanding of it : It's clear that the important function is x_redraw, but more precisely according to my understanding of it, it clears the screen when x_bs is called which itself calls x_adjust and I guess that x_adjust is eventually clearing the screen, but I'm not quite sure that this is what is happening. So here are my two main questions : 1. Do you think it is a good approach? Or should I try to understand how to clear the screen by reading another code which is maybe simpler? 2. Am I understanding the clear-screen feature of emacs? Thanks in advance for all your responses, and advices, Best, Rhylx