Skip to content

Commit c70022e

Browse files
committed
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 / e5d1d16). 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.
1 parent 6480ad9 commit c70022e

7 files changed

Lines changed: 51 additions & 31 deletions

File tree

‎src/MacVim/MMBackend.m‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,7 +1309,11 @@ - (oneway void)processInput:(int)msgid data:(in bycopy NSData *)data
13091309
isTerminating = YES;
13101310
getout(0);
13111311
} else if (UpdateCellSizeMsgID == msgid) {
1312-
// Immediately handle simple state updates to they can be reflected in Vim.
1312+
// Handle this immediately so that it is picked up whenever the run
1313+
// loop is updated (e.g. during ":sleep"), not only when waiting for
1314+
// keys. This does modify Vim state, but only the cached cell size
1315+
// and reporting it to :terminal jobs, the same as what happens when a
1316+
// terminal reports its cell size in handle_csi().
13131317
[self handleCellSize:data];
13141318
} else {
13151319
// First remove previous instances of this message from the input
@@ -2773,9 +2777,14 @@ - (void)handleCellSize:(NSData *)data
27732777
const void *bytes = [data bytes];
27742778

27752779
// Don't use gui.char_width/height because for simplicity we set those to
2776-
// 1. We store the cell size separately (it's only used for
2777-
// getcellpixels()).
2780+
// 1. We store the cell size separately and let update_cell_size() pick it
2781+
// up, which updates cell_width/cell_height. This is more similar to
2782+
// terminal Vim than GUI Vim, due to the async multi-process architecture
2783+
// of MacVim.
27782784
memcpy(&_cellSize, bytes, sizeof(NSSize));
2785+
#if defined(FEAT_IMAGE) || defined(FEAT_EVAL)
2786+
update_cell_size();
2787+
#endif
27792788
}
27802789

27812790
- (void)handleDropFiles:(NSData *)data

‎src/evalfunc.c‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6307,19 +6307,13 @@ f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
63076307
if (rettv_list_alloc(rettv) == FAIL)
63086308
return;
63096309

6310-
#if defined(FEAT_GUI)
6310+
// MacVim uses cell_width similar to terminal Vim.
6311+
#if defined(FEAT_GUI) && !defined(FEAT_GUI_MACVIM)
63116312
if (gui.in_use)
63126313
{
63136314
// success pixel size and no gui.
6314-
#ifdef FEAT_GUI_MACVIM
6315-
struct cellsize cs;
6316-
gui_mch_calc_cell_size(&cs);
6317-
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
6318-
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
6319-
#else
63206315
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_width);
63216316
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_height);
6322-
#endif
63236317
}
63246318
else
63256319
#endif

‎src/os_unix.c‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,20 +4475,6 @@ mch_report_winsize(int fd, int rows, int cols)
44754475
ws.ws_xpixel = cols * cell_width;
44764476
ws.ws_ypixel = rows * cell_height;
44774477

4478-
#if defined(FEAT_GUI) && defined(FEAT_GUI_MACVIM)
4479-
if (gui.in_use)
4480-
{
4481-
// calculate and set tty pixel size
4482-
struct cellsize cs;
4483-
gui_mch_calc_cell_size(&cs);
4484-
if (cs.cs_xpixel != -1)
4485-
{
4486-
ws.ws_xpixel = cols * cs.cs_xpixel;
4487-
ws.ws_ypixel = rows * cs.cs_ypixel;
4488-
}
4489-
}
4490-
#endif
4491-
44924478
retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
44934479
ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
44944480
# elif defined(TIOCSSIZE)

‎src/term.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,7 +3788,20 @@ update_cell_size(void)
37883788
{
37893789
# ifdef FEAT_GUI
37903790
if (gui.in_use)
3791+
{
3792+
# ifdef FEAT_GUI_MACVIM
3793+
// In MacVim, char_width/char_height are set to 1 always, and the cell
3794+
// size is updated asynchronously by the GUI, so we need to check
3795+
// whether we have a valid value first.
3796+
struct cellsize cs;
3797+
3798+
gui_mch_calc_cell_size(&cs);
3799+
if (cs.cs_xpixel > 0 && cs.cs_ypixel > 0)
3800+
set_cell_size(cs.cs_xpixel, cs.cs_ypixel);
3801+
# else
37913802
set_cell_size(gui.char_width, gui.char_height);
3803+
# endif
3804+
}
37923805
else
37933806
# endif
37943807
{

‎src/terminal.c‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5104,10 +5104,8 @@ parse_csi(
51045104
if (gui.in_use)
51055105
{
51065106
#ifdef FEAT_GUI_MACVIM
5107-
struct cellsize cs;
5108-
gui_mch_calc_cell_size(&cs);
5109-
x += wp->w_wincol * cs.cs_xpixel;
5110-
y += W_WINROW(wp) * cs.cs_ypixel;
5107+
x += wp->w_wincol * cell_width;
5108+
y += W_WINROW(wp) * cell_height;
51115109
#else
51125110
x += wp->w_wincol * gui.char_width;
51135111
y += W_WINROW(wp) * gui.char_height;

‎src/testdir/test_functions.vim‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5242,8 +5242,8 @@ func Test_getcellpixels_macvim()
52425242
" deliberate design decision. Right now the caller has to wait for MacVim
52435243
" to update the state before getcellpixels() will reflect the correct
52445244
" value, hence the need for multiple wait's here.
5245-
call WaitForAssert({-> assert_notequal(0, getcellpixels()[0], 'Uninitialized getcellpixels')})
5246-
call assert_equal([7, 13], getcellpixels()) " Default font is Menlo:h11
5245+
" Until then the default 8x16 is returned, like in a terminal.
5246+
call WaitForAssert({-> assert_equal([7, 13], getcellpixels())}) " Default font is Menlo:h11
52475247
set guifont=Menlo:h13
52485248
call WaitForAssert({-> assert_equal([8, 15], getcellpixels())})
52495249

‎src/testdir/test_macvim.vim‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,23 @@ func Test_macvim_invalid_options()
7777

7878
call assert_fails("let &fuoptions='abcdef'", 'E474:')
7979
endfunc
80+
81+
" Test that a job in a terminal window gets the correct cell size when it
82+
" queries it with CSI 16 t, after the font has changed.
83+
func Test_macvim_terminal_cellsize()
84+
CheckGui
85+
CheckFeature terminal
86+
87+
" MacVim updates the cell size asynchronously, so wait for it.
88+
set guifont=Menlo:h13
89+
call WaitForAssert({-> assert_equal([8, 15], getcellpixels())})
90+
91+
let buf = term_start(['sh', '-c',
92+
\ "stty raw -echo; printf '\\033[16t'; dd bs=64 count=1 2>/dev/null > Xcsireply"])
93+
call WaitForAssert({-> assert_equal(["\<Esc>[6;15;8t"],
94+
\ filereadable('Xcsireply') ? readfile('Xcsireply') : [])})
95+
96+
exe buf .. 'bwipe!'
97+
call delete('Xcsireply')
98+
set guifont&
99+
endfunc

0 commit comments

Comments
 (0)