Skip to content

Commit 1083cae

Browse files
mikoto2000chrisbra
authored andcommitted
patch 9.1.0854: cannot get terminal cell size
Problem: cannot get terminal cell size Solution: add getcellpixels() function to return xpixel * ypixel cell size on terminal Unix (mikoto2000) closes: #16004 Signed-off-by: mikoto2000 <mikoto2000@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
1 parent 6fbf63d commit 1083cae

File tree

10 files changed

+112
-5
lines changed

10 files changed

+112
-5
lines changed

runtime/doc/builtin.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 10
1+
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 11
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -222,6 +222,7 @@ getbufline({buf}, {lnum} [, {end}])
222222
getbufoneline({buf}, {lnum}) String line {lnum} of buffer {buf}
223223
getbufvar({buf}, {varname} [, {def}])
224224
any variable {varname} in buffer {buf}
225+
getcellpixels() List get character cell pixel size
225226
getcellwidths() List get character cell width overrides
226227
getchangelist([{buf}]) List list of change list items
227228
getchar([{expr}]) Number or String
@@ -3786,6 +3787,15 @@ getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
37863787
Return type: any, depending on {varname}
37873788

37883789

3790+
getcellpixels() *getcellpixels()*
3791+
Returns a |List| of terminal cell pixel size.
3792+
List format is [xpixels, ypixels].
3793+
Only works on Unix. For gVim and on other systems,
3794+
returns [-1, -1].
3795+
3796+
Return type: list<Number>
3797+
3798+
37893799
getcellwidths() *getcellwidths()*
37903800
Returns a |List| of cell widths of character ranges overridden
37913801
by |setcellwidths()|. The format is equal to the argument of

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7838,6 +7838,7 @@ getbufinfo() builtin.txt /*getbufinfo()*
78387838
getbufline() builtin.txt /*getbufline()*
78397839
getbufoneline() builtin.txt /*getbufoneline()*
78407840
getbufvar() builtin.txt /*getbufvar()*
7841+
getcellpixels() builtin.txt /*getcellpixels()*
78417842
getcellwidths() builtin.txt /*getcellwidths()*
78427843
getchangelist() builtin.txt /*getchangelist()*
78437844
getchar() builtin.txt /*getchar()*

runtime/doc/usr_41.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*usr_41.txt* For Vim version 9.1. Last change: 2024 Oct 05
1+
*usr_41.txt* For Vim version 9.1. Last change: 2024 Nov 11
22

33
VIM USER MANUAL - by Bram Moolenaar
44

@@ -778,6 +778,7 @@ String manipulation: *string-functions*
778778
strdisplaywidth() size of string when displayed, deals with tabs
779779
setcellwidths() set character cell width overrides
780780
getcellwidths() get character cell width overrides
781+
getcellpixels() get character cell pixel size
781782
reverse() reverse the order of characters in a string
782783
substitute() substitute a pattern match with a string
783784
submatch() get a specific match in ":s" and substitute()
@@ -1394,6 +1395,7 @@ Various: *various-functions*
13941395
did_filetype() check if a FileType autocommand was used
13951396
diff() diff two Lists of strings
13961397
eventhandler() check if invoked by an event handler
1398+
getcellpixels() get List of cell pixel size
13971399
getpid() get process ID of Vim
13981400
getscriptinfo() get list of sourced vim scripts
13991401
getimstatus() check if IME status is active

runtime/doc/version9.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*version9.txt* For Vim version 9.1. Last change: 2024 Nov 06
1+
*version9.txt* For Vim version 9.1. Last change: 2024 Nov 11
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -41621,6 +41621,7 @@ Functions: ~
4162141621
|foreach()| apply function to List items
4162241622
|getcmdcomplpat()| Shell command line completion
4162341623
|getcmdprompt()| get prompt for input()/confirm()
41624+
|getcellpixels()| get List of terminal cell pixel size
4162441625
|getregion()| get a region of text from a buffer
4162541626
|getregionpos()| get a list of positions for a region
4162641627
|id()| get unique identifier for a Dict, List, Object,

src/evalfunc.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,6 +2077,14 @@ static funcentry_T global_functions[] =
20772077
ret_string, f_getbufoneline},
20782078
{"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any,
20792079
ret_any, f_getbufvar},
2080+
{"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+
},
20802088
{"getcellwidths", 0, 0, 0, NULL,
20812089
ret_list_any, f_getcellwidths},
20822090
{"getchangelist", 0, 1, FEARG_1, arg1_buffer,

src/os_unix.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,6 +4348,68 @@ 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+
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
4362+
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
4363+
}
4364+
#endif
4365+
4366+
/*
4367+
* Try to get the current terminal cell size.
4368+
* If faile get cell size, fallback 5x10 pixel.
4369+
*/
4370+
void
4371+
mch_calc_cell_size(struct cellsize *cs_out)
4372+
{
4373+
#if defined(FEAT_GUI)
4374+
if (!gui.in_use)
4375+
{
4376+
#endif
4377+
// get current tty size.
4378+
struct winsize ws;
4379+
int fd = 1;
4380+
int retval = -1;
4381+
retval = ioctl(fd, TIOCGWINSZ, &ws);
4382+
# ifdef FEAT_EVAL
4383+
ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
4384+
# endif
4385+
if (retval == -1)
4386+
{
4387+
cs_out->cs_xpixel = -1;
4388+
cs_out->cs_ypixel = -1;
4389+
return;
4390+
}
4391+
4392+
// calculate parent tty's pixel per cell.
4393+
int x_cell_size = ws.ws_xpixel / ws.ws_col;
4394+
int y_cell_size = ws.ws_ypixel / ws.ws_row;
4395+
4396+
// calculate current tty's pixel
4397+
cs_out->cs_xpixel = x_cell_size;
4398+
cs_out->cs_ypixel = y_cell_size;
4399+
4400+
# ifdef FEAT_EVAL
4401+
ch_log(NULL, "Got cell pixel size with TIOCGWINSZ: %d x %d", x_cell_size, y_cell_size);
4402+
# endif
4403+
#if defined(FEAT_GUI)
4404+
}
4405+
else
4406+
{
4407+
cs_out->cs_xpixel = -1;
4408+
cs_out->cs_ypixel = -1;
4409+
}
4410+
#endif
4411+
}
4412+
43514413
#if defined(FEAT_TERMINAL) || defined(PROTO)
43524414
/*
43534415
* Report the windows size "rows" and "cols" to tty "fd".
@@ -4367,8 +4429,13 @@ mch_report_winsize(int fd, int rows, int cols)
43674429

43684430
ws.ws_col = cols;
43694431
ws.ws_row = rows;
4370-
ws.ws_xpixel = cols * 5;
4371-
ws.ws_ypixel = rows * 10;
4432+
4433+
// calcurate and set tty pixel size
4434+
struct cellsize cs;
4435+
mch_calc_cell_size(&cs);
4436+
ws.ws_xpixel = cols * cs.cs_xpixel;
4437+
ws.ws_ypixel = rows * cs.cs_ypixel;
4438+
43724439
retval = ioctl(tty_fd, TIOCSWINSZ, &ws);
43734440
ch_log(NULL, "ioctl(TIOCSWINSZ) %s", retval == 0 ? "success" : "failed");
43744441
# elif defined(TIOCSSIZE)

src/os_unix.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,9 @@ int mch_rename(const char *src, const char *dest);
488488

489489
// We have three kinds of ACL support.
490490
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)
491+
492+
struct cellsize {
493+
unsigned int cs_xpixel;
494+
unsigned int cs_ypixel;
495+
};
496+

src/proto/os_unix.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,6 @@ 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);
95+
void mch_calc_cell_size(struct cellsize *cs_out);
9496
/* vim: set ft=c : */

src/testdir/test_utf8.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ func Test_setcellwidths()
273273
bwipe!
274274
endfunc
275275

276+
" Pixel size of a cell is terminal-dependent, so in the test, only the list and size 2 are checked.
277+
func Test_getcellpixels()
278+
" Not yet Windows-compatible
279+
CheckNotMSWindows
280+
let cellpixels = getcellpixels()
281+
call assert_equal(2, len(cellpixels))
282+
endfunc
283+
276284
func Test_getcellwidths()
277285
call setcellwidths([])
278286
call assert_equal([], getcellwidths())

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+
854,
707709
/**/
708710
853,
709711
/**/

0 commit comments

Comments
 (0)