Skip to content

Commit a73dfc2

Browse files
mikoto2000chrisbra
authored andcommitted
patch 9.1.0871: getcellpixels() can be further improved
Problem: getcellpixels() can be further improved Solution: Fix floating point exception, implement getcellpixels() in the UI (mikoto2000) closes: #16059 Signed-off-by: mikoto2000 <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent bd4614f commit a73dfc2

8 files changed

Lines changed: 82 additions & 50 deletions

File tree

runtime/doc/builtin.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 14
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 18
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3790,8 +3790,13 @@ getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
37903790
getcellpixels() *getcellpixels()*
37913791
Returns a |List| of terminal cell pixel size.
37923792
List format is [xpixels, ypixels].
3793-
Only works on (terminal) Unix. For gVim, on other systems and
3794-
on failure returns [].
3793+
3794+
Only works on Unix (terminal and gVim) and Windows (gVim only).
3795+
Returns [] on other systems or on failure.
3796+
Note that there could be variations across different terminals.
3797+
On macOS, system Terminal.app returns sizes in points (before
3798+
Retina scaling), whereas third-party terminals return raw pixel
3799+
sizes (post Retina scaling).
37953800

37963801
Return type: list<any>
37973802

src/evalfunc.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static void f_funcref(typval_T *argvars, typval_T *rettv);
6161
static void f_function(typval_T *argvars, typval_T *rettv);
6262
static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
6363
static void f_get(typval_T *argvars, typval_T *rettv);
64+
static void f_getcellpixels(typval_T *argvars, typval_T *rettv);
6465
static void f_getchangelist(typval_T *argvars, typval_T *rettv);
6566
static void f_getcharpos(typval_T *argvars, typval_T *rettv);
6667
static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
@@ -2078,13 +2079,7 @@ static funcentry_T global_functions[] =
20782079
{"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any,
20792080
ret_any, f_getbufvar},
20802081
{"getcellpixels", 0, 0, 0, NULL,
2081-
ret_list_any,
2082-
#if (defined(UNIX) || defined(VMS)) && (defined(FEAT_EVAL) || defined(PROTO))
2083-
f_getcellpixels
2084-
#else
2085-
NULL
2086-
#endif
2087-
},
2082+
ret_list_any, f_getcellpixels},
20882083
{"getcellwidths", 0, 0, 0, NULL,
20892084
ret_list_any, f_getcellwidths},
20902085
{"getchangelist", 0, 1, FEARG_1, arg1_buffer,
@@ -5216,6 +5211,45 @@ f_get(typval_T *argvars, typval_T *rettv)
52165211
copy_tv(tv, rettv);
52175212
}
52185213

5214+
/*
5215+
* "getcellpixels()" function
5216+
*/
5217+
static void
5218+
f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
5219+
{
5220+
if (rettv_list_alloc(rettv) == FAIL)
5221+
return;
5222+
5223+
#if defined(FEAT_GUI)
5224+
if (gui.in_use)
5225+
{
5226+
// success pixel size and no gui.
5227+
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_width);
5228+
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_height);
5229+
}
5230+
else
5231+
#endif
5232+
{
5233+
struct cellsize cs;
5234+
#if defined(UNIX)
5235+
mch_calc_cell_size(&cs);
5236+
#else
5237+
// Non-Unix CUIs are not supported, so set this to -1x-1.
5238+
cs.cs_xpixel = -1;
5239+
cs.cs_ypixel = -1;
5240+
#endif
5241+
5242+
// failed get pixel size.
5243+
if (cs.cs_xpixel == -1)
5244+
return;
5245+
5246+
// success pixel size and no gui.
5247+
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
5248+
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
5249+
}
5250+
5251+
}
5252+
52195253
/*
52205254
* "getchangelist()" function
52215255
*/

src/os_unix.c

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,32 +4348,6 @@ mch_get_shellsize(void)
43484348
return OK;
43494349
}
43504350

4351-
#if defined(FEAT_EVAL) || defined(PROTO)
4352-
void
4353-
f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
4354-
{
4355-
struct cellsize cs;
4356-
mch_calc_cell_size(&cs);
4357-
4358-
if (rettv_list_alloc(rettv) == FAIL)
4359-
return;
4360-
4361-
// failed get pixel size.
4362-
if (cs.cs_xpixel == -1)
4363-
return;
4364-
4365-
#if defined(FEAT_GUI)
4366-
// gui return [].
4367-
if (gui.in_use)
4368-
return;
4369-
#endif
4370-
4371-
// success pixel size and no gui.
4372-
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
4373-
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
4374-
}
4375-
#endif
4376-
43774351
/*
43784352
* Try to get the current terminal cell size.
43794353
* On failure, returns -1x-1
@@ -4391,7 +4365,7 @@ mch_calc_cell_size(struct cellsize *cs_out)
43914365
ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
43924366
#endif
43934367

4394-
if (retval == -1)
4368+
if (retval == -1 || ws.ws_col == 0 || ws.ws_row == 0)
43954369
{
43964370
cs_out->cs_xpixel = -1;
43974371
cs_out->cs_ypixel = -1;

src/os_unix.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,3 @@ int mch_rename(const char *src, const char *dest);
489489
// We have three kinds of ACL support.
490490
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
491491

492-
// Defined as signed, to return -1 on error
493-
struct cellsize {
494-
int cs_xpixel;
495-
int cs_ypixel;
496-
};
497-

src/proto/os_unix.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,5 @@ void xsmp_close(void);
9191
void stop_timeout(void);
9292
volatile sig_atomic_t *start_timeout(long msec);
9393
void delete_timer(void);
94-
void f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv);
9594
void mch_calc_cell_size(struct cellsize *cs_out);
9695
/* vim: set ft=c : */

src/structs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5099,3 +5099,11 @@ typedef struct
50995099

51005100
#define KEYVALUE_ENTRY(k, v) \
51015101
{(k), {((char_u *)v), STRLEN_LITERAL(v)}}
5102+
5103+
#if defined(UNIX) || defined(MSWIN)
5104+
// Defined as signed, to return -1 on error
5105+
struct cellsize {
5106+
int cs_xpixel;
5107+
int cs_ypixel;
5108+
};
5109+
#endif

src/testdir/test_functions.vim

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4160,10 +4160,9 @@ func Test_slice()
41604160
endfunc
41614161

41624162

4163-
" Test for getcellpixels()
4163+
" Test for getcellpixels() for unix system
41644164
" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
4165-
func Test_getcellpixels()
4166-
" Not yet Windows-compatible
4165+
func Test_getcellpixels_for_unix()
41674166
CheckNotMSWindows
41684167
CheckRunVimInTerminal
41694168

@@ -4180,13 +4179,30 @@ func Test_getcellpixels()
41804179
call StopVimInTerminal(buf)
41814180
endfunc
41824181

4182+
" Test for getcellpixels() for windows system
4183+
" Windows terminal vim is not support. check return `[]`.
4184+
func Test_getcellpixels_for_windows()
4185+
CheckMSWindows
4186+
CheckRunVimInTerminal
4187+
4188+
let buf = RunVimInTerminal('', #{rows: 6})
4189+
4190+
" write getcellpixels() result to current buffer.
4191+
call term_sendkeys(buf, ":redi @\"\<CR>")
4192+
call term_sendkeys(buf, ":echo getcellpixels()\<CR>")
4193+
call term_sendkeys(buf, ":redi END\<CR>")
4194+
call term_sendkeys(buf, "P")
4195+
4196+
call WaitForAssert({-> assert_match("\[\]", term_getline(buf, 3))}, 1000)
4197+
4198+
call StopVimInTerminal(buf)
4199+
endfunc
4200+
41834201
" Test for getcellpixels() on gVim
41844202
func Test_getcellpixels_gui()
4185-
" Not yet Windows-compatible
4186-
CheckNotMSWindows
41874203
if has("gui_running")
41884204
let cellpixels = getcellpixels()
4189-
call assert_equal(0, len(cellpixels))
4205+
call assert_equal(2, len(cellpixels))
41904206
endif
41914207
endfunc
41924208

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
871,
707709
/**/
708710
870,
709711
/**/

0 commit comments

Comments
 (0)