Skip to content

Commit

Permalink
patch 9.1.0871: getcellpixels() can be further improved
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
mikoto2000 authored and chrisbra committed Nov 18, 2024
1 parent bd4614f commit a73dfc2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 50 deletions.
11 changes: 8 additions & 3 deletions runtime/doc/builtin.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 14
*builtin.txt* For Vim version 9.1. Last change: 2024 Nov 18


VIM REFERENCE MANUAL by Bram Moolenaar
Expand Down Expand Up @@ -3790,8 +3790,13 @@ getbufvar({buf}, {varname} [, {def}]) *getbufvar()*
getcellpixels() *getcellpixels()*
Returns a |List| of terminal cell pixel size.
List format is [xpixels, ypixels].
Only works on (terminal) Unix. For gVim, on other systems and
on failure returns [].

Only works on Unix (terminal and gVim) and Windows (gVim only).
Returns [] on other systems or on failure.
Note that there could be variations across different terminals.
On macOS, system Terminal.app returns sizes in points (before
Retina scaling), whereas third-party terminals return raw pixel
sizes (post Retina scaling).

Return type: list<any>

Expand Down
48 changes: 41 additions & 7 deletions src/evalfunc.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static void f_funcref(typval_T *argvars, typval_T *rettv);
static void f_function(typval_T *argvars, typval_T *rettv);
static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
static void f_get(typval_T *argvars, typval_T *rettv);
static void f_getcellpixels(typval_T *argvars, typval_T *rettv);
static void f_getchangelist(typval_T *argvars, typval_T *rettv);
static void f_getcharpos(typval_T *argvars, typval_T *rettv);
static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
Expand Down Expand Up @@ -2078,13 +2079,7 @@ static funcentry_T global_functions[] =
{"getbufvar", 2, 3, FEARG_1, arg3_buffer_string_any,
ret_any, f_getbufvar},
{"getcellpixels", 0, 0, 0, NULL,
ret_list_any,
#if (defined(UNIX) || defined(VMS)) && (defined(FEAT_EVAL) || defined(PROTO))
f_getcellpixels
#else
NULL
#endif
},
ret_list_any, f_getcellpixels},
{"getcellwidths", 0, 0, 0, NULL,
ret_list_any, f_getcellwidths},
{"getchangelist", 0, 1, FEARG_1, arg1_buffer,
Expand Down Expand Up @@ -5216,6 +5211,45 @@ f_get(typval_T *argvars, typval_T *rettv)
copy_tv(tv, rettv);
}

/*
* "getcellpixels()" function
*/
static void
f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
{
if (rettv_list_alloc(rettv) == FAIL)
return;

#if defined(FEAT_GUI)
if (gui.in_use)
{
// success pixel size and no gui.
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_width);
list_append_number(rettv->vval.v_list, (varnumber_T)gui.char_height);
}
else
#endif
{
struct cellsize cs;
#if defined(UNIX)
mch_calc_cell_size(&cs);
#else
// Non-Unix CUIs are not supported, so set this to -1x-1.
cs.cs_xpixel = -1;
cs.cs_ypixel = -1;
#endif

// failed get pixel size.
if (cs.cs_xpixel == -1)
return;

// success pixel size and no gui.
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
}

}

/*
* "getchangelist()" function
*/
Expand Down
28 changes: 1 addition & 27 deletions src/os_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4348,32 +4348,6 @@ mch_get_shellsize(void)
return OK;
}

#if defined(FEAT_EVAL) || defined(PROTO)
void
f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv)
{
struct cellsize cs;
mch_calc_cell_size(&cs);

if (rettv_list_alloc(rettv) == FAIL)
return;

// failed get pixel size.
if (cs.cs_xpixel == -1)
return;

#if defined(FEAT_GUI)
// gui return [].
if (gui.in_use)
return;
#endif

// success pixel size and no gui.
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_xpixel);
list_append_number(rettv->vval.v_list, (varnumber_T)cs.cs_ypixel);
}
#endif

/*
* Try to get the current terminal cell size.
* On failure, returns -1x-1
Expand All @@ -4391,7 +4365,7 @@ mch_calc_cell_size(struct cellsize *cs_out)
ch_log(NULL, "ioctl(TIOCGWINSZ) %s", retval == 0 ? "success" : "failed");
#endif

if (retval == -1)
if (retval == -1 || ws.ws_col == 0 || ws.ws_row == 0)
{
cs_out->cs_xpixel = -1;
cs_out->cs_ypixel = -1;
Expand Down
6 changes: 0 additions & 6 deletions src/os_unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,3 @@ int mch_rename(const char *src, const char *dest);
// We have three kinds of ACL support.
#define HAVE_ACL (HAVE_POSIX_ACL || HAVE_SOLARIS_ACL || HAVE_AIX_ACL)

// Defined as signed, to return -1 on error
struct cellsize {
int cs_xpixel;
int cs_ypixel;
};

1 change: 0 additions & 1 deletion src/proto/os_unix.pro
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,5 @@ void xsmp_close(void);
void stop_timeout(void);
volatile sig_atomic_t *start_timeout(long msec);
void delete_timer(void);
void f_getcellpixels(typval_T *argvars UNUSED, typval_T *rettv);
void mch_calc_cell_size(struct cellsize *cs_out);
/* vim: set ft=c : */
8 changes: 8 additions & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -5099,3 +5099,11 @@ typedef struct

#define KEYVALUE_ENTRY(k, v) \
{(k), {((char_u *)v), STRLEN_LITERAL(v)}}

#if defined(UNIX) || defined(MSWIN)
// Defined as signed, to return -1 on error
struct cellsize {
int cs_xpixel;
int cs_ypixel;
};
#endif
28 changes: 22 additions & 6 deletions src/testdir/test_functions.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4160,10 +4160,9 @@ func Test_slice()
endfunc


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

Expand All @@ -4180,13 +4179,30 @@ func Test_getcellpixels()
call StopVimInTerminal(buf)
endfunc

" Test for getcellpixels() for windows system
" Windows terminal vim is not support. check return `[]`.
func Test_getcellpixels_for_windows()
CheckMSWindows
CheckRunVimInTerminal

let buf = RunVimInTerminal('', #{rows: 6})

" write getcellpixels() result to current buffer.
call term_sendkeys(buf, ":redi @\"\<CR>")
call term_sendkeys(buf, ":echo getcellpixels()\<CR>")
call term_sendkeys(buf, ":redi END\<CR>")
call term_sendkeys(buf, "P")

call WaitForAssert({-> assert_match("\[\]", term_getline(buf, 3))}, 1000)

call StopVimInTerminal(buf)
endfunc

" Test for getcellpixels() on gVim
func Test_getcellpixels_gui()
" Not yet Windows-compatible
CheckNotMSWindows
if has("gui_running")
let cellpixels = getcellpixels()
call assert_equal(0, len(cellpixels))
call assert_equal(2, len(cellpixels))
endif
endfunc

Expand Down
2 changes: 2 additions & 0 deletions src/version.c
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,8 @@ static char *(features[]) =

static int included_patches[] =
{ /* Add new patch number below this line */
/**/
871,
/**/
870,
/**/
Expand Down

0 comments on commit a73dfc2

Please sign in to comment.