To: vim_dev@googlegroups.com Subject: Patch 8.0.0739 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.0.0739 Problem: Terminal resizing doesn't work well. Solution: Resize the terminal to the Vim window and the other way around. Avoid mapping typed keys. Set the environment properly. Files: src/terminal.c, src/os_unix.c, src/structs.h *** ../vim-8.0.0738/src/terminal.c 2017-07-19 23:20:15.719053810 +0200 --- src/terminal.c 2017-07-20 22:50:59.073088603 +0200 *************** *** 184,189 **** --- 184,191 ---- opt.jo_io_buf[PART_OUT] = curbuf->b_fnum; opt.jo_io_buf[PART_ERR] = curbuf->b_fnum; opt.jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT)); + opt.jo_term_rows = rows; + opt.jo_term_cols = cols; term->tl_job = job_start(argvars, &opt); } *************** *** 267,273 **** --- 269,279 ---- update_screen(0); setcursor(); out_flush(); + ++no_mapping; + ++allow_keys; c = vgetc(); + --no_mapping; + --allow_keys; /* Catch keys that need to be handled as in Normal mode. */ switch (c) *************** *** 331,336 **** --- 337,349 ---- term_update_lines(wp); } + static void + position_cursor(win_T *wp, VTermPos *pos) + { + wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1)); + wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1)); + } + #ifdef WIN3264 /************************************** *************** *** 486,492 **** } static int ! handle_moverect(VTermRect dest, VTermRect src, void *user) { term_T *term = (term_T *)user; --- 499,505 ---- } static int ! handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user) { term_T *term = (term_T *)user; *************** *** 496,502 **** } static int ! handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user) { term_T *term = (term_T *)user; win_T *wp; --- 509,519 ---- } static int ! handle_movecursor( ! VTermPos pos, ! VTermPos oldpos UNUSED, ! int visible UNUSED, ! void *user) { term_T *term = (term_T *)user; win_T *wp; *************** *** 506,514 **** { if (wp->w_buffer == term->tl_buffer) { ! /* TODO: limit to window size? */ ! wp->w_wrow = pos.row; ! wp->w_wcol = pos.col; if (wp == curwin) is_current = TRUE; } --- 523,529 ---- { if (wp->w_buffer == term->tl_buffer) { ! position_cursor(wp, &pos); if (wp == curwin) is_current = TRUE; } *************** *** 527,534 **** handle_resize(int rows, int cols, void *user) { term_T *term = (term_T *)user; - /* TODO: handle terminal resize. */ redraw_buf_later(term->tl_buffer, NOT_VALID); return 1; } --- 542,558 ---- handle_resize(int rows, int cols, void *user) { term_T *term = (term_T *)user; + win_T *wp; + + FOR_ALL_WINDOWS(wp) + { + if (wp->w_buffer == term->tl_buffer) + { + win_setheight_win(rows, wp); + win_setwidth_win(cols, wp); + } + } redraw_buf_later(term->tl_buffer, NOT_VALID); return 1; } *************** *** 648,657 **** --- 672,694 ---- int vterm_cols; VTerm *vterm = wp->w_buffer->b_term->tl_vterm; VTermScreen *screen = vterm_obtain_screen(vterm); + VTermState *state = vterm_obtain_state(vterm); VTermPos pos; vterm_get_size(vterm, &vterm_rows, &vterm_cols); + if (*wp->w_p_tms == NUL + && (vterm_rows != wp->w_height || vterm_cols != wp->w_width)) + { + vterm_set_size(vterm, wp->w_height, wp->w_width); + /* Get the size again, in case setting the didn't work. */ + vterm_get_size(vterm, &vterm_rows, &vterm_cols); + } + + /* The cursor may have been moved when resizing. */ + vterm_state_get_cursorpos(state, &pos); + position_cursor(wp, &pos); + /* TODO: Only redraw what changed. */ for (pos.row = 0; pos.row < wp->w_height; ++pos.row) { *** ../vim-8.0.0738/src/os_unix.c 2017-06-22 22:37:53.592267703 +0200 --- src/os_unix.c 2017-07-20 22:56:58.730555104 +0200 *************** *** 4054,4091 **** #if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL) static void ! set_child_environment(void) { # ifdef HAVE_SETENV char envbuf[50]; # else static char envbuf_Rows[20]; static char envbuf_Columns[20]; # endif /* Simulate to have a dumb terminal (for now) */ # ifdef HAVE_SETENV ! setenv("TERM", "dumb", 1); ! sprintf((char *)envbuf, "%ld", Rows); setenv("ROWS", (char *)envbuf, 1); ! sprintf((char *)envbuf, "%ld", Rows); setenv("LINES", (char *)envbuf, 1); ! sprintf((char *)envbuf, "%ld", Columns); setenv("COLUMNS", (char *)envbuf, 1); # else /* * Putenv does not copy the string, it has to remain valid. * Use a static array to avoid losing allocated memory. */ ! putenv("TERM=dumb"); ! sprintf(envbuf_Rows, "ROWS=%ld", Rows); putenv(envbuf_Rows); ! sprintf(envbuf_Rows, "LINES=%ld", Rows); ! putenv(envbuf_Rows); ! sprintf(envbuf_Columns, "COLUMNS=%ld", Columns); putenv(envbuf_Columns); # endif } #endif int --- 4054,4101 ---- #if !defined(USE_SYSTEM) || defined(FEAT_JOB_CHANNEL) static void ! set_child_environment(long rows, long columns, char *term) { # ifdef HAVE_SETENV char envbuf[50]; # else + static char envbuf_TERM[30]; static char envbuf_Rows[20]; + static char envbuf_Lines[20]; static char envbuf_Columns[20]; # endif /* Simulate to have a dumb terminal (for now) */ # ifdef HAVE_SETENV ! setenv("TERM", term, 1); ! sprintf((char *)envbuf, "%ld", rows); setenv("ROWS", (char *)envbuf, 1); ! sprintf((char *)envbuf, "%ld", rows); setenv("LINES", (char *)envbuf, 1); ! sprintf((char *)envbuf, "%ld", columns); setenv("COLUMNS", (char *)envbuf, 1); # else /* * Putenv does not copy the string, it has to remain valid. * Use a static array to avoid losing allocated memory. */ ! vim_snprintf(envbuf_Term, sizeof(envbuf_Term), "TERM=%s", term); ! putenv(envbuf_Term); ! vim_snprintf(envbuf_Rows, sizeof(envbuf_Rows), "ROWS=%ld", rows); putenv(envbuf_Rows); ! vim_snprintf(envbuf_Lines, sizeof(envbuf_Lines), "LINES=%ld", rows); ! putenv(envbuf_Lines); ! vim_snprintf(envbuf_Columns, sizeof(envbuf_Columns), ! "COLUMNS=%ld", columns); putenv(envbuf_Columns); # endif } + + static void + set_default_child_environment(void) + { + set_child_environment(Rows, Columns, "dumb"); + } #endif int *************** *** 4417,4423 **** # endif } # endif ! set_child_environment(); /* * stderr is only redirected when using the GUI, so that a --- 4427,4433 ---- # endif } # endif ! set_default_child_environment(); /* * stderr is only redirected when using the GUI, so that a *************** *** 5090,5096 **** #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) void ! mch_start_job(char **argv, job_T *job, jobopt_T *options UNUSED) { pid_t pid; int fd_in[2]; /* for stdin */ --- 5100,5106 ---- #if defined(FEAT_JOB_CHANNEL) || defined(PROTO) void ! mch_start_job(char **argv, job_T *job, jobopt_T *options) { pid_t pid; int fd_in[2]; /* for stdin */ *************** *** 5200,5206 **** (void)setsid(); # endif ! set_child_environment(); if (use_null_for_in || use_null_for_out || use_null_for_err) null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0); --- 5210,5224 ---- (void)setsid(); # endif ! # ifdef FEAT_TERMINAL ! if (options->jo_term_rows > 0) ! set_child_environment( ! (long)options->jo_term_rows, ! (long)options->jo_term_cols, ! "xterm"); ! else ! # endif ! set_default_child_environment(); if (use_null_for_in || use_null_for_out || use_null_for_err) null_fd = open("/dev/null", O_RDWR | O_EXTRA, 0); *** ../vim-8.0.0738/src/structs.h 2017-07-15 14:03:53.927047056 +0200 --- src/structs.h 2017-07-20 22:52:40.460374437 +0200 *************** *** 1732,1737 **** --- 1732,1743 ---- int jo_id; char_u jo_soe_buf[NUMBUFLEN]; char_u *jo_stoponexit; + + #ifdef FEAT_TERMINAL + /* when non-zero run the job in a terminal window of this size */ + int jo_term_rows; + int jo_term_cols; + #endif } jobopt_T; *** ../vim-8.0.0738/src/version.c 2017-07-19 23:20:15.719053810 +0200 --- src/version.c 2017-07-20 22:45:11.555535871 +0200 *************** *** 771,772 **** --- 771,774 ---- { /* Add new patch number below this line */ + /**/ + 739, /**/ -- Press any key to continue, press any other key to quit. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///