aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2023-02-27 16:31:05 +0100
committerJustin M. Keyes <justinkz@gmail.com>2023-02-27 19:50:59 +0100
commitce597235a26839826de88ecd8b949ec54c310fbd (patch)
tree145d03f0f47afef4fa7df2c6d1976de9c97049c3
parent7f424e2b65779c59fc0cac3cc7508ba2ec07f200 (diff)
downloadrneovim-ce597235a26839826de88ecd8b949ec54c310fbd.tar.gz
rneovim-ce597235a26839826de88ecd8b949ec54c310fbd.tar.bz2
rneovim-ce597235a26839826de88ecd8b949ec54c310fbd.zip
feat(ui): restore has('gui_running')
Problem: has('gui_running') is still common in the wild and our answer has changed over time, causing frustration. https://github.com/vimpostor/vim-tpipeline/commit/95a6ccbe9f33bc42dd4cee45731d8bc3fbcd92d1 Solution: Use stdin_tty/stdout_tty to decide if a UI is (not) a GUI.
-rw-r--r--runtime/doc/news.txt3
-rw-r--r--src/nvim/api/ui.c1
-rw-r--r--src/nvim/eval/funcs.c4
-rw-r--r--src/nvim/ui.c16
-rw-r--r--src/nvim/ui.h1
-rw-r--r--test/functional/terminal/tui_spec.lua4
-rw-r--r--test/functional/vimscript/has_spec.lua21
-rw-r--r--test/helpers.lua2
8 files changed, 43 insertions, 9 deletions
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt
index 4c9cbf9189..90e17d3678 100644
--- a/runtime/doc/news.txt
+++ b/runtime/doc/news.txt
@@ -181,6 +181,9 @@ The following new APIs or features were added.
• |nvim_list_uis()| reports all |ui-option| fields.
+• Vim's `has('gui_running')` is now supported as a way for plugins to check if
+ a GUI (not the |TUI|) is attached to Nvim. |has()|
+
==============================================================================
CHANGED FEATURES *news-changes*
diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c
index 00fd2781ff..a8f5d2e070 100644
--- a/src/nvim/api/ui.c
+++ b/src/nvim/api/ui.c
@@ -352,7 +352,6 @@ static void ui_set_option(UI *ui, bool init, String name, Object value, Error *e
});
stdin_fd = (int)value.data.integer;
- ui->stdin_fd = (int)value.data.integer;
return;
}
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
index 1baf96e281..d97f7b6d35 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -3228,7 +3228,9 @@ static void f_has(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
}
if (!n) {
- if (STRNICMP(name, "patch", 5) == 0) {
+ if (STRNICMP(name, "gui_running", 11) == 0) {
+ n = ui_gui_attached();
+ } else if (STRNICMP(name, "patch", 5) == 0) {
if (name[5] == '-'
&& strlen(name) >= 11
&& ascii_isdigit(name[6])
diff --git a/src/nvim/ui.c b/src/nvim/ui.c
index 6c95579b47..ce1a57350a 100644
--- a/src/nvim/ui.c
+++ b/src/nvim/ui.c
@@ -134,6 +134,7 @@ void ui_free_all_mem(void)
}
#endif
+/// Returns true if any `rgb=true` UI is attached.
bool ui_rgb_attached(void)
{
if (!headless_mode && p_tgc) {
@@ -147,6 +148,18 @@ bool ui_rgb_attached(void)
return false;
}
+/// Returns true if a GUI is attached.
+bool ui_gui_attached(void)
+{
+ for (size_t i = 0; i < ui_count; i++) {
+ bool tui = uis[i]->stdin_tty || uis[i]->stdout_tty;
+ if (!tui) {
+ return true;
+ }
+ }
+ return false;
+}
+
/// Returns true if any UI requested `override=true`.
bool ui_override(void)
{
@@ -599,11 +612,10 @@ Array ui_array(void)
PUT(info, "rgb", BOOLEAN_OBJ(ui->rgb));
PUT(info, "override", BOOLEAN_OBJ(ui->override));
- // TUI fields.
+ // TUI fields. (`stdin_fd` is intentionally omitted.)
PUT(info, "term_name", STRING_OBJ(cstr_to_string(ui->term_name)));
PUT(info, "term_background", STRING_OBJ(cstr_to_string(ui->term_background)));
PUT(info, "term_colors", INTEGER_OBJ(ui->term_colors));
- PUT(info, "stdin_fd", INTEGER_OBJ(ui->stdin_fd));
PUT(info, "stdin_tty", BOOLEAN_OBJ(ui->stdin_tty));
PUT(info, "stdout_tty", BOOLEAN_OBJ(ui->stdout_tty));
diff --git a/src/nvim/ui.h b/src/nvim/ui.h
index c1377f56b4..dc0ccc73ea 100644
--- a/src/nvim/ui.h
+++ b/src/nvim/ui.h
@@ -107,7 +107,6 @@ struct ui_t {
char *term_name;
char *term_background;
int term_colors;
- int stdin_fd;
bool stdin_tty;
bool stdout_tty;
diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua
index 9db80e0db2..76ea6256a1 100644
--- a/test/functional/terminal/tui_spec.lua
+++ b/test/functional/terminal/tui_spec.lua
@@ -1399,6 +1399,8 @@ describe('TUI', function()
end)
it('in nvim_list_uis()', function()
+ -- $TERM in :terminal.
+ local exp_term = is_os('bsd') and 'builtin_xterm' or 'xterm-256color'
local expected = {
{
chan = 1,
@@ -1418,7 +1420,7 @@ describe('TUI', function()
stdout_tty = true,
term_background = '',
term_colors = 256,
- term_name = 'xterm-256color', -- $TERM in :terminal.
+ term_name = exp_term,
width = 50
},
}
diff --git a/test/functional/vimscript/has_spec.lua b/test/functional/vimscript/has_spec.lua
index 2e26d603b3..78a761d370 100644
--- a/test/functional/vimscript/has_spec.lua
+++ b/test/functional/vimscript/has_spec.lua
@@ -1,8 +1,11 @@
local helpers = require('test.functional.helpers')(after_each)
-local eq = helpers.eq
+local Screen = require('test.functional.ui.screen')
local clear = helpers.clear
+local connect = helpers.connect
+local eq = helpers.eq
local funcs = helpers.funcs
local is_os = helpers.is_os
+local nvim_prog = helpers.nvim_prog
describe('has()', function()
before_each(clear)
@@ -69,8 +72,22 @@ describe('has()', function()
end
end)
+ it('"gui_running"', function()
+ eq(0, funcs.has('gui_running'))
+ local tui = Screen.new(50,15)
+ local gui_session = connect(funcs.serverstart())
+ local gui = Screen.new(50,15)
+ eq(0, funcs.has('gui_running'))
+ tui:attach({ext_linegrid=true, rgb=true, stdin_tty=true, stdout_tty=true})
+ gui:attach({ext_multigrid=true, rgb=true}, gui_session)
+ eq(1, funcs.has('gui_running'))
+ tui:detach()
+ eq(1, funcs.has('gui_running'))
+ gui:detach()
+ eq(0, funcs.has('gui_running'))
+ end)
+
it('does not change v:shell_error', function()
- local nvim_prog = helpers.nvim_prog
funcs.system({nvim_prog, '-es', '+73cquit'})
funcs.has('python3') -- use a call whose implementation shells out
eq(73, funcs.eval('v:shell_error'))
diff --git a/test/helpers.lua b/test/helpers.lua
index d45536b42b..117b6b4aaa 100644
--- a/test/helpers.lua
+++ b/test/helpers.lua
@@ -312,7 +312,7 @@ function module.is_os(s)
or s == 'bsd') then
error('unknown platform: '..tostring(s))
end
- return ((s == 'win' and module.sysname():find('windows'))
+ return not not ((s == 'win' and module.sysname():find('windows'))
or (s == 'mac' and module.sysname() == 'darwin')
or (s == 'freebsd' and module.sysname() == 'freebsd')
or (s == 'openbsd' and module.sysname() == 'openbsd')