Download raw body.
bin/ksh: fix emacs/vi mode using wrong terminal width
tech@,
this is clear re-submission of my patch to ksh to fix terminal width
detecion.
xterm and other terminal emulators resize the pty after starting the
shell. x_init() queries the terminal size at startup before the resize
occurs, so x_cols stays at the default 80 columns.
SIGWINCH arrives, but check_sigwinch() isn't called until after command
entry. This makes the line editor think the screen is 80 columns, which
makes it show '<' and truncate input before the actual terminal edge on
large windows, and causes weird behavior on small ones.
Call check_sigwinch() when entering edit mode and on EINTR in x_getc().
The callback lets emacs/vi mode recalculate display width and redraw.
Ok? Tests?
diff --git bin/ksh/edit.c bin/ksh/edit.c
index 3ed69e5375d..83bab8ffbad 100644
--- bin/ksh/edit.c
+++ bin/ksh/edit.c
@@ -25,7 +25,8 @@ X_chars edchars;
static void x_sigwinch(int);
volatile sig_atomic_t got_sigwinch;
-static void check_sigwinch(void);
+void check_sigwinch(void);
+void (*x_resize_cb)(void); /* callback for resize during edit */
static int x_file_glob(int, const char *, int, char ***);
static int x_command_glob(int, const char *, int, char ***);
@@ -58,7 +59,7 @@ x_sigwinch(int sig)
got_sigwinch = 1;
}
-static void
+void
check_sigwinch(void)
{
if (got_sigwinch) {
@@ -75,11 +76,16 @@ check_sigwinch(void)
* see the change cause the environ doesn't change.
*/
if (ws.ws_col) {
+ int old_cols = x_cols;
+
x_cols = ws.ws_col < MIN_COLS ? MIN_COLS :
ws.ws_col;
if ((vp = typeset("COLUMNS", 0, 0, 0, 0)))
setint(vp, (int64_t) ws.ws_col);
+
+ if (x_cols != old_cols && x_resize_cb)
+ (*x_resize_cb)();
}
if (ws.ws_row && (vp = typeset("LINES", 0, 0, 0, 0)))
setint(vp, (int64_t) ws.ws_row);
@@ -120,12 +126,14 @@ x_getc(void)
char c;
int n;
- while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR)
+ while ((n = blocking_read(STDIN_FILENO, &c, 1)) < 0 && errno == EINTR) {
+ check_sigwinch();
if (trap) {
x_mode(false);
runtraps(0);
x_mode(true);
}
+ }
if (n != 1)
return -1;
return (int) (unsigned char) c;
diff --git bin/ksh/edit.h bin/ksh/edit.h
index 1d573e1f6fb..abcecc667e0 100644
--- bin/ksh/edit.h
+++ bin/ksh/edit.h
@@ -39,6 +39,8 @@ extern X_chars edchars;
#define XCF_COMMAND_FILE (XCF_COMMAND|XCF_FILE)
/* edit.c */
+void check_sigwinch(void);
+extern void (*x_resize_cb)(void);
int x_getc(void);
void x_flush(void);
int x_putc(int);
diff --git bin/ksh/emacs.c bin/ksh/emacs.c
index 3c9b0074d81..fa18ecd3a14 100644
--- bin/ksh/emacs.c
+++ bin/ksh/emacs.c
@@ -105,6 +105,7 @@ static int xlp_valid;
/* end from 4.9 edit.h } */
static int x_tty; /* are we on a tty? */
static int x_bind_quiet; /* be quiet when binding keys */
+static void x_emacs_resize(void);
static int (*x_last_command)(int);
static char **x_histp; /* history position */
@@ -286,6 +287,8 @@ x_emacs(char *buf, size_t len)
xmp = NULL;
x_histp = histptr + 1;
+ x_resize_cb = x_emacs_resize;
+ check_sigwinch();
xx_cols = x_cols;
x_col = promptlen(prompt, &p);
prompt_skip = p - prompt;
@@ -316,8 +319,11 @@ x_emacs(char *buf, size_t len)
x_last_command = NULL;
while (1) {
x_flush();
- if ((at = x_e_getu8(line, at)) < 0)
+ if ((at = x_e_getu8(line, at)) < 0) {
+ x_resize_cb = NULL;
return 0;
+ }
+
ntries++;
if (x_arg == -1) {
@@ -378,6 +384,7 @@ x_emacs(char *buf, size_t len)
x_last_command = NULL;
break;
case KEOL:
+ x_resize_cb = NULL;
ret = xep - xbuf;
return (ret);
break;
@@ -2162,4 +2169,37 @@ x_lastcp(void)
return (xlp);
}
+static void
+x_emacs_resize(void)
+{
+ /* clear old content before resizing */
+ x_putc('\r');
+ x_putc('\033');
+ x_putc('[');
+ x_putc('J');
+
+ xx_cols = x_cols;
+ x_col = promptlen(prompt, NULL);
+ if (x_col > xx_cols)
+ x_col = x_col - (x_col / xx_cols) * xx_cols;
+ x_displen = xx_cols - 2 - x_col;
+ if (x_displen < 1) {
+ x_col = 0;
+ x_displen = xx_cols - 2;
+ prompt_redraw = 0;
+ } else {
+ xbp = xbuf;
+ xlp_valid = false;
+ if (xcp <= x_lastcp()) {
+ x_redraw(0);
+ x_flush();
+ return;
+ }
+ }
+
+ x_col = 0;
+ x_displen = xx_cols - 2;
+ x_adjust();
+}
+
#endif /* EMACS */
diff --git bin/ksh/vi.c bin/ksh/vi.c
index 28be809036d..def7aeeae24 100644
--- bin/ksh/vi.c
+++ bin/ksh/vi.c
@@ -75,6 +75,8 @@ static void vi_error(void);
static void vi_macro_reset(void);
static int x_vi_putbuf(const char *, size_t);
static int isu8cont(unsigned char);
+static void x_vi_resize(void);
+static void calc_winsize(void);
#define C_ 0x1 /* a valid command that isn't a M_, E_, U_ */
#define M_ 0x2 /* movement command (h, l, etc.) */
@@ -201,6 +203,8 @@ x_vi(char *buf, size_t len)
int c;
vi_reset(buf, len > LINE ? LINE : len);
+ x_resize_cb = x_vi_resize;
+ check_sigwinch();
vi_pprompt(1);
x_flush();
while (1) {
@@ -242,6 +246,7 @@ x_vi(char *buf, size_t len)
x_flush();
}
+ x_resize_cb = NULL;
x_putc('\r'); x_putc('\n'); x_flush();
if (c == -1 || len <= (size_t)es->linelen)
@@ -1424,8 +1429,6 @@ free_edstate(struct edstate *old)
static void
edit_reset(char *buf, size_t len)
{
- const char *p;
-
es = &ebuf;
es->cbuf = buf;
es->cbufsize = len;
@@ -1436,6 +1439,17 @@ edit_reset(char *buf, size_t len)
es->cursor = undo->cursor = 0;
es->winleft = undo->winleft = 0;
+ calc_winsize();
+ win = 0;
+ morec = ' ';
+ holdlen = 0;
+}
+
+static void
+calc_winsize(void)
+{
+ const char *p;
+
cur_col = pwidth = promptlen(prompt, &p);
prompt_skip = p - prompt;
if (pwidth > x_cols - 3 - MIN_EDIT_SPACE) {
@@ -1452,9 +1466,6 @@ edit_reset(char *buf, size_t len)
(void) memset(wbuf[0], ' ', wbuf_len);
(void) memset(wbuf[1], ' ', wbuf_len);
winwidth = x_cols - pwidth - 3;
- win = 0;
- morec = ' ';
- holdlen = 0;
}
/*
@@ -2300,4 +2311,29 @@ isu8cont(unsigned char c)
{
return !Flag(FVISHOW8) && (c & (0x80 | 0x40)) == 0x80;
}
+
+static void
+x_vi_resize(void)
+{
+ int cur = 0, col = 0;
+
+ /* clear old content before resizing */
+ x_putc('\r');
+ x_putc('\033');
+ x_putc('[');
+ x_putc('J');
+
+ calc_winsize();
+
+ while (cur < es->linelen)
+ col = newcol((unsigned char) es->cbuf[cur++], col);
+ if (col <= winwidth)
+ es->winleft = 0;
+ else if (outofwin())
+ rewindow();
+
+ redraw_line(0, 0);
+ refresh_line(insert != 0);
+ x_flush();
+}
#endif /* VI */
bin/ksh: fix emacs/vi mode using wrong terminal width