From 50c8f55ad5ac077486b12bf2865a657534dcc693 Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Mon, 23 Nov 2026 17:58:39 -0800 Subject: [PATCH] Support OSI 16 in hosted terminal to match upstream Upstream Vim refactored the cell pixel code, and now supports responding to CSI 16 calls from hosted terminal (see v9.2.1343 / e5d1d16ae3). Add that support to MacVim too and add a test for that (upstream didn't add a test for this). Also, use this opportunity to refactor the code so MacVim has less special case ifdefs. MacVim's handling of this simply works more similar to terminal Vim than GUI Vim due to the async nature of MacVim's multi-process GUI. We can just reuse the terminal Vim codepath instead of having to add special case ifdef everywhere, and use update_cell_size() to do most of the work by updating a centralized location whenever we receive a new size from the GUI frontend. --- src/MacVim/MMBackend.m | 15 ++++++++++++--- src/evalfunc.c | 10 ++-------- src/os_unix.c | 14 -------------- src/term.c | 13 +++++++++++++ src/terminal.c | 6 ++---- src/testdir/test_functions.vim | 4 ++-- src/testdir/test_macvim.vim | 20 ++++++++++++++++++++ 7 files changed, 51 insertions(+), 31 deletions(-) diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index d58eae8476..391c54d640 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -1309,7 +1309,11 @@ - (oneway void)processInput:(int)msgid data:(in bycopy NSData *)data isTerminating = YES; getout(0); } else if (UpdateCellSizeMsgID == msgid) { - // Immediately handle simple state updates to they can be reflected in Vim. + // Handle this immediately so that it is picked up whenever the run + // loop is updated (e.g. during ":sleep"), not only when waiting for + // keys. This does modify Vim state, but only the cached cell size + // and reporting it to :terminal jobs, the same as what happens when a + // terminal reports its cell size in handle_csi(). [self handleCellSize:data]; } else { // First remove previous instances of this message from the input @@ -2773,9 +2777,14 @@ - (void)handleCellSize:(NSData *)data const void *bytes = [data bytes]; // Don't use gui.char_width/height because for simplicity we set those to - // 1. We store the cell size separately (it's only used for - // getcellpixels()). + // 1. We store the cell size separately and let update_cell_size() pick it + // up, which updates cell_width/cell_height. This is more similar to + // terminal Vim than GUI Vim, due to the async multi-process architecture + // of MacVim. memcpy(&_cellSize, bytes, sizeof(NSSize)); +#if defined(FEAT_IMAGE) || defined(FEAT_EVAL) + update_cell_size(); +#endif } - (void)handleDropFiles:(NSData *)data diff --git a/src/evalfunc.c b/src/evalfunc.c index e49a03f317..40d2a6281b 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -6307,19 +6307,13 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv) if (rettv_list_alloc(rettv) == FAIL) return; -#if defined(FEAT_GUI) + // MacVim uses cell_width similar to terminal Vim. +#if defined(FEAT_GUI) && !defined(FEAT_GUI_MACVIM) if (gui.in_use) { // success pixel size and no gui. -#ifdef FEAT_GUI_MACVIM - struct cellsize cs; - gui_mch_calc_cell_size(&cs); - list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel); - list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel); -#else list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_width); list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_height); -#endif } else #endif diff --git a/src/os_unix.c b/src/os_unix.c index 5727436710..623f3bbf10 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4475,20 +4475,6 @@ mch_report_winsize(int fd, int rows, int cols) ws.ws_xpixel = cols * cell_width; ws.ws_ypixel = rows * cell_height; -#if defined(FEAT_GUI) && defined(FEAT_GUI_MACVIM) - if (gui.in_use) - { - // calculate and set tty pixel size - struct cellsize cs; - gui_mch_calc_cell_size(&cs); - if (cs.cs_xpixel != -1) - { - ws.ws_xpixel = cols * cs.cs_xpixel; - ws.ws_ypixel = rows * cs.cs_ypixel; - } - } -#endif - retval = ioctl(tty_fd, TIOCSWINSZ, &ws); ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed"); # elif defined(TIOCSSIZE) diff --git a/src/term.c b/src/term.c index 1f508688a5..621055aaea 100644 --- a/src/term.c +++ b/src/term.c @@ -3788,7 +3788,20 @@ update_cell_size(void) { # ifdef FEAT_GUI if (gui.in_use) + { +# ifdef FEAT_GUI_MACVIM + // In MacVim, char_width/char_height are set to 1 always, and the cell + // size is updated asynchronously by the GUI, so we need to check + // whether we have a valid value first. + struct cellsize cs; + + gui_mch_calc_cell_size(&cs); + if (cs.cs_xpixel > 0 && cs.cs_ypixel > 0) + set_cell_size(cs.cs_xpixel, cs.cs_ypixel); +# else set_cell_size(gui.char_width, gui.char_height); +# endif + } else # endif { diff --git a/src/terminal.c b/src/terminal.c index b379328db4..8290e6cc34 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -5104,10 +5104,8 @@ parse_csi( if (gui.in_use) { #ifdef FEAT_GUI_MACVIM - struct cellsize cs; - gui_mch_calc_cell_size(&cs); - x += wp->w_wincol * cs.cs_xpixel; - y += W_WINROW(wp) * cs.cs_ypixel; + x += wp->w_wincol * cell_width; + y += W_WINROW(wp) * cell_height; #else x += wp->w_wincol * gui.char_width; y += W_WINROW(wp) * gui.char_height; diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim index 8db0f906a5..1c897a6bdc 100644 --- a/src/testdir/test_functions.vim +++ b/src/testdir/test_functions.vim @@ -5242,8 +5242,8 @@ func Test_getcellpixels_macvim() " deliberate design decision. Right now the caller has to wait for MacVim " to update the state before getcellpixels() will reflect the correct " value, hence the need for multiple wait's here. - call WaitForAssert({-> assert_notequal(0, getcellpixels()[0], 'Uninitialized getcellpixels')}) - call assert_equal([7, 13], getcellpixels()) " Default font is Menlo:h11 + " Until then the default 8x16 is returned, like in a terminal. + call WaitForAssert({-> assert_equal([7, 13], getcellpixels())}) " Default font is Menlo:h11 set guifont=Menlo:h13 call WaitForAssert({-> assert_equal([8, 15], getcellpixels())}) diff --git a/src/testdir/test_macvim.vim b/src/testdir/test_macvim.vim index b0da61b3f7..b772ac0f87 100644 --- a/src/testdir/test_macvim.vim +++ b/src/testdir/test_macvim.vim @@ -77,3 +77,23 @@ func Test_macvim_invalid_options() call assert_fails("let &fuoptions='abcdef'", 'E474:') endfunc + +" Test that a job in a terminal window gets the correct cell size when it +" queries it with CSI 16 t, after the font has changed. +func Test_macvim_terminal_cellsize() + CheckGui + CheckFeature terminal + + " MacVim updates the cell size asynchronously, so wait for it. + set guifont=Menlo:h13 + call WaitForAssert({-> assert_equal([8, 15], getcellpixels())}) + + let buf = term_start(['sh', '-c', + \ "stty raw -echo; printf '\\033[16t'; dd bs=64 count=1 2>/dev/null > Xcsireply"]) + call WaitForAssert({-> assert_equal(["\[6;15;8t"], + \ filereadable('Xcsireply') ? readfile('Xcsireply') : [])}) + + exe buf .. 'bwipe!' + call delete('Xcsireply') + set guifont& +endfunc