From e9cf515888705640ebd754483349f2bf84c32255 Mon Sep 17 00:00:00 2001 From: Rui Abreu Ferreira Date: Wed, 21 Jun 2017 16:59:52 +0100 Subject: UIAttach, UIDetach --- src/nvim/api/ui.c | 14 ++++++++++++++ src/nvim/auevents.lua | 4 ++++ src/nvim/channel.c | 2 ++ src/nvim/ui_bridge.c | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) (limited to 'src') diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index ada26a2a07..acf0404c31 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -19,6 +19,8 @@ #include "nvim/highlight.h" #include "nvim/screen.h" #include "nvim/window.h" +#include "nvim/fileio.h" +#include "nvim/eval.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/ui.c.generated.h" @@ -169,6 +171,12 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height, pmap_put(uint64_t)(connected_uis, channel_id, ui); ui_attach_impl(ui); + + dict_T *dict = get_vim_var_dict(VV_EVENT); + tv_dict_add_nr(dict, S_LEN("chan"), (long)channel_id); + tv_dict_set_keys_readonly(dict); + apply_autocmds(EVENT_UIATTACH, NULL, NULL, false, curbuf); + tv_dict_clear(dict); } /// @deprecated @@ -196,6 +204,12 @@ void nvim_ui_detach(uint64_t channel_id, Error *err) return; } remote_ui_disconnect(channel_id); + + dict_T *dict = get_vim_var_dict(VV_EVENT); + tv_dict_add_nr(dict, S_LEN("chan"), (long)channel_id); + tv_dict_set_keys_readonly(dict); + apply_autocmds(EVENT_UIDETACH, NULL, NULL, false, curbuf); + tv_dict_clear(dict); } diff --git a/src/nvim/auevents.lua b/src/nvim/auevents.lua index 12fc8fd02a..1505b984ad 100644 --- a/src/nvim/auevents.lua +++ b/src/nvim/auevents.lua @@ -96,6 +96,8 @@ return { 'TextChangedI', -- text was modified in Insert mode(no popup) 'TextChangedP', -- text was modified in Insert mode(popup) 'TextYankPost', -- after a yank or delete was done (y, d, c) + 'UIAttach', -- after a UI attached + 'UIDetach', -- after a UI detaches 'User', -- user defined autocommand 'VimEnter', -- after starting Vim 'VimLeave', -- before exiting Vim @@ -123,5 +125,7 @@ return { TabNewEntered=true, TermClose=true, TermOpen=true, + UIAttach=true, + UIDetach=true, }, } diff --git a/src/nvim/channel.c b/src/nvim/channel.c index 104c79efd9..f9102fa0e2 100644 --- a/src/nvim/channel.c +++ b/src/nvim/channel.c @@ -172,6 +172,7 @@ static Channel *channel_alloc(ChannelStreamType type) chan->refcount = 1; chan->exit_status = -1; chan->streamtype = type; + assert(chan->id <= VARNUMBER_MAX); pmap_put(uint64_t)(channels, chan->id, chan); return chan; } @@ -190,6 +191,7 @@ void channel_create_event(Channel *chan, const char *ext_source) source = (const char *)IObuff; } + assert(chan->id <= VARNUMBER_MAX); Dictionary info = channel_info(chan->id); typval_T tv = TV_INITIAL_VALUE; // TODO(bfredl): do the conversion in one step. Also would be nice diff --git a/src/nvim/ui_bridge.c b/src/nvim/ui_bridge.c index 91cd458702..be76a8b047 100644 --- a/src/nvim/ui_bridge.c +++ b/src/nvim/ui_bridge.c @@ -17,6 +17,8 @@ #include "nvim/ui_bridge.h" #include "nvim/ugrid.h" #include "nvim/api/private/helpers.h" +#include "nvim/fileio.h" +#include "nvim/eval.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ui_bridge.c.generated.h" @@ -86,6 +88,13 @@ UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler) uv_mutex_unlock(&rv->mutex); ui_attach_impl(&rv->bridge); + + dict_T *dict = get_vim_var_dict(VV_EVENT); + tv_dict_add_nr(dict, S_LEN("chan"), 0); + tv_dict_set_keys_readonly(dict); + apply_autocmds(EVENT_UIATTACH, NULL, NULL, false, curbuf); + tv_dict_clear(dict); + return &rv->bridge; } @@ -107,6 +116,13 @@ static void ui_bridge_stop(UI *b) // Detach bridge first, so that "stop" is the last event the TUI loop // receives from the main thread. #8041 ui_detach_impl(b); + + dict_T *dict = get_vim_var_dict(VV_EVENT); + tv_dict_add_nr(dict, S_LEN("chan"), 0); + tv_dict_set_keys_readonly(dict); + apply_autocmds(EVENT_UIDETACH, NULL, NULL, false, curbuf); + tv_dict_clear(dict); + UIBridgeData *bridge = (UIBridgeData *)b; bool stopped = bridge->stopped = false; UI_BRIDGE_CALL(b, stop, 1, b); -- cgit From 6dd56d09025c960ef36952e3d4a1f77779782c52 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 11 Sep 2019 18:44:20 -0700 Subject: UIAttach, UIDetach doc: ginit.vim, gvimrc fix #3656 --- src/nvim/api/ui.c | 18 ++---------------- src/nvim/aucmd.c | 22 +++++++++++++++++++++- src/nvim/aucmd.h | 2 ++ src/nvim/ex_cmds2.c | 2 -- src/nvim/ex_cmds2.h | 21 ++++++++++----------- src/nvim/ui.c | 12 ++++++++++-- src/nvim/ui_bridge.c | 18 ++---------------- src/nvim/ui_compositor.c | 2 +- 8 files changed, 48 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/nvim/api/ui.c b/src/nvim/api/ui.c index acf0404c31..75ee05761b 100644 --- a/src/nvim/api/ui.c +++ b/src/nvim/api/ui.c @@ -19,8 +19,6 @@ #include "nvim/highlight.h" #include "nvim/screen.h" #include "nvim/window.h" -#include "nvim/fileio.h" -#include "nvim/eval.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "api/ui.c.generated.h" @@ -59,7 +57,7 @@ void remote_ui_disconnect(uint64_t channel_id) pmap_del(uint64_t)(connected_uis, channel_id); xfree(ui->data); ui->data = NULL; // Flag UI as "stopped". - ui_detach_impl(ui); + ui_detach_impl(ui, channel_id); xfree(ui); } @@ -170,13 +168,7 @@ void nvim_ui_attach(uint64_t channel_id, Integer width, Integer height, ui->data = data; pmap_put(uint64_t)(connected_uis, channel_id, ui); - ui_attach_impl(ui); - - dict_T *dict = get_vim_var_dict(VV_EVENT); - tv_dict_add_nr(dict, S_LEN("chan"), (long)channel_id); - tv_dict_set_keys_readonly(dict); - apply_autocmds(EVENT_UIATTACH, NULL, NULL, false, curbuf); - tv_dict_clear(dict); + ui_attach_impl(ui, channel_id); } /// @deprecated @@ -204,12 +196,6 @@ void nvim_ui_detach(uint64_t channel_id, Error *err) return; } remote_ui_disconnect(channel_id); - - dict_T *dict = get_vim_var_dict(VV_EVENT); - tv_dict_add_nr(dict, S_LEN("chan"), (long)channel_id); - tv_dict_set_keys_readonly(dict); - apply_autocmds(EVENT_UIDETACH, NULL, NULL, false, curbuf); - tv_dict_clear(dict); } diff --git a/src/nvim/aucmd.c b/src/nvim/aucmd.c index 3bb0fcec3b..5188f96a5d 100644 --- a/src/nvim/aucmd.c +++ b/src/nvim/aucmd.c @@ -7,11 +7,32 @@ #include "nvim/main.h" #include "nvim/ui.h" #include "nvim/aucmd.h" +#include "nvim/eval.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "aucmd.c.generated.h" #endif +void do_autocmd_uiattach(uint64_t chanid, bool attached) +{ + static bool recursive = false; + + if (recursive) { + return; // disallow recursion + } + recursive = true; + + dict_T *dict = get_vim_var_dict(VV_EVENT); + assert(chanid < VARNUMBER_MAX); + tv_dict_add_nr(dict, S_LEN("chan"), (varnumber_T)chanid); + tv_dict_set_keys_readonly(dict); + apply_autocmds(attached ? EVENT_UIATTACH : EVENT_UIDETACH, + NULL, NULL, false, curbuf); + tv_dict_clear(dict); + + recursive = false; +} + static void focusgained_event(void **argv) { bool *gainedp = argv[0]; @@ -38,4 +59,3 @@ static void do_autocmd_focusgained(bool gained) NULL, NULL, false, curbuf); recursive = false; } - diff --git a/src/nvim/aucmd.h b/src/nvim/aucmd.h index 6570ba7a92..9a4dd79a78 100644 --- a/src/nvim/aucmd.h +++ b/src/nvim/aucmd.h @@ -1,6 +1,8 @@ #ifndef NVIM_AUCMD_H #define NVIM_AUCMD_H +#include + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "aucmd.h.generated.h" #endif diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index affdda0386..25528e667f 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -3111,8 +3111,6 @@ int do_source(char_u *fname, int check_other, int is_vimrc) } if (is_vimrc == DOSO_VIMRC) { vimrc_found(fname_exp, (char_u *)"MYVIMRC"); - } else if (is_vimrc == DOSO_GVIMRC) { - vimrc_found(fname_exp, (char_u *)"MYGVIMRC"); } #ifdef USE_CRNL diff --git a/src/nvim/ex_cmds2.h b/src/nvim/ex_cmds2.h index 0a164cdeb8..f85ea94ed6 100644 --- a/src/nvim/ex_cmds2.h +++ b/src/nvim/ex_cmds2.h @@ -7,19 +7,18 @@ typedef void (*DoInRuntimepathCB)(char_u *, void *); -/* - * flags for check_changed() - */ -#define CCGD_AW 1 /* do autowrite if buffer was changed */ -#define CCGD_MULTWIN 2 /* check also when several wins for the buf */ -#define CCGD_FORCEIT 4 /* ! used */ -#define CCGD_ALLBUF 8 /* may write all buffers */ -#define CCGD_EXCMD 16 /* may suggest using ! */ +// +// flags for check_changed() +// +#define CCGD_AW 1 // do autowrite if buffer was changed +#define CCGD_MULTWIN 2 // check also when several wins for the buf +#define CCGD_FORCEIT 4 // ! used +#define CCGD_ALLBUF 8 // may write all buffers +#define CCGD_EXCMD 16 // may suggest using ! -/* last argument for do_source() */ +// last argument for do_source() #define DOSO_NONE 0 -#define DOSO_VIMRC 1 /* loading vimrc file */ -#define DOSO_GVIMRC 2 /* loading gvimrc file */ +#define DOSO_VIMRC 1 // loading vimrc file #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ex_cmds2.h.generated.h" diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 25077675bf..4096bc1e8e 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -9,6 +9,7 @@ #include "nvim/vim.h" #include "nvim/log.h" +#include "nvim/aucmd.h" #include "nvim/ui.h" #include "nvim/charset.h" #include "nvim/cursor.h" @@ -268,7 +269,7 @@ void ui_busy_stop(void) } } -void ui_attach_impl(UI *ui) +void ui_attach_impl(UI *ui, uint64_t chanid) { if (ui_count == MAX_UI_COUNT) { abort(); @@ -292,9 +293,14 @@ void ui_attach_impl(UI *ui) ui_send_all_hls(ui); } ui_refresh(); + + bool is_compositor = (ui == uis[0]); + if (!is_compositor) { + do_autocmd_uiattach(chanid, true); + } } -void ui_detach_impl(UI *ui) +void ui_detach_impl(UI *ui, uint64_t chanid) { size_t shift_index = MAX_UI_COUNT; @@ -326,6 +332,8 @@ void ui_detach_impl(UI *ui) if (!ui->ui_ext[kUIMultigrid] && !ui->ui_ext[kUIFloatDebug]) { ui_comp_detach(ui); } + + do_autocmd_uiattach(chanid, false); } void ui_set_ext_option(UI *ui, UIExtension ext, bool active) diff --git a/src/nvim/ui_bridge.c b/src/nvim/ui_bridge.c index be76a8b047..a64e691b6d 100644 --- a/src/nvim/ui_bridge.c +++ b/src/nvim/ui_bridge.c @@ -17,8 +17,6 @@ #include "nvim/ui_bridge.h" #include "nvim/ugrid.h" #include "nvim/api/private/helpers.h" -#include "nvim/fileio.h" -#include "nvim/eval.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "ui_bridge.c.generated.h" @@ -87,13 +85,7 @@ UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler) } uv_mutex_unlock(&rv->mutex); - ui_attach_impl(&rv->bridge); - - dict_T *dict = get_vim_var_dict(VV_EVENT); - tv_dict_add_nr(dict, S_LEN("chan"), 0); - tv_dict_set_keys_readonly(dict); - apply_autocmds(EVENT_UIATTACH, NULL, NULL, false, curbuf); - tv_dict_clear(dict); + ui_attach_impl(&rv->bridge, 0); return &rv->bridge; } @@ -115,13 +107,7 @@ static void ui_bridge_stop(UI *b) { // Detach bridge first, so that "stop" is the last event the TUI loop // receives from the main thread. #8041 - ui_detach_impl(b); - - dict_T *dict = get_vim_var_dict(VV_EVENT); - tv_dict_add_nr(dict, S_LEN("chan"), 0); - tv_dict_set_keys_readonly(dict); - apply_autocmds(EVENT_UIDETACH, NULL, NULL, false, curbuf); - tv_dict_clear(dict); + ui_detach_impl(b, 0); UIBridgeData *bridge = (UIBridgeData *)b; bool stopped = bridge->stopped = false; diff --git a/src/nvim/ui_compositor.c b/src/nvim/ui_compositor.c index f91442642d..1af027d415 100644 --- a/src/nvim/ui_compositor.c +++ b/src/nvim/ui_compositor.c @@ -83,7 +83,7 @@ void ui_comp_init(void) kv_push(layers, &default_grid); curgrid = &default_grid; - ui_attach_impl(compositor); + ui_attach_impl(compositor, 0); } void ui_comp_syn_init(void) -- cgit From 44d45e29ea4a632e66ac63d25d355a5a4c7178bc Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Wed, 11 Sep 2019 22:29:28 -0700 Subject: API/nvim_list_uis(): include "chan" field for TUI Even though it's always zero currently, it's less confusing if all UIs have the same fields. --- src/nvim/ui.c | 4 +--- src/nvim/ui_bridge.c | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 4096bc1e8e..9ff2381189 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -484,9 +484,7 @@ Array ui_array(void) PUT(info, ui_ext_names[j], BOOLEAN_OBJ(ui->ui_ext[j])); } } - if (ui->inspect) { - ui->inspect(ui, &info); - } + ui->inspect(ui, &info); ADD(all_uis, DICTIONARY_OBJ(info)); } return all_uis; diff --git a/src/nvim/ui_bridge.c b/src/nvim/ui_bridge.c index a64e691b6d..9a1988739c 100644 --- a/src/nvim/ui_bridge.c +++ b/src/nvim/ui_bridge.c @@ -63,6 +63,7 @@ UI *ui_bridge_attach(UI *ui, ui_main_fn ui_main, event_scheduler scheduler) rv->bridge.set_icon = ui_bridge_set_icon; rv->bridge.option_set = ui_bridge_option_set; rv->bridge.raw_line = ui_bridge_raw_line; + rv->bridge.inspect = ui_bridge_inspect; rv->scheduler = scheduler; for (UIExtension i = 0; (int)i < kUIExtCount; i++) { @@ -215,3 +216,8 @@ static void ui_bridge_option_set_event(void **argv) api_free_object(value); xfree(argv[3]); } + +static void ui_bridge_inspect(UI *ui, Dictionary *info) +{ + PUT(*info, "chan", INTEGER_OBJ(0)); +} -- cgit From 589f612adfea22b688618fa90e011f5494ca5204 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 12 Sep 2019 16:08:22 -0700 Subject: rename: UIAttach/UIDetach => UIEnter/UILeave "enter"/"leave" is more conventional for Vim events, and "attach"/"detach" distinction does not gain much. --- src/nvim/aucmd.c | 4 ++-- src/nvim/auevents.lua | 8 ++++---- src/nvim/ui.c | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/nvim/aucmd.c b/src/nvim/aucmd.c index 5188f96a5d..a9a7d834a4 100644 --- a/src/nvim/aucmd.c +++ b/src/nvim/aucmd.c @@ -13,7 +13,7 @@ # include "aucmd.c.generated.h" #endif -void do_autocmd_uiattach(uint64_t chanid, bool attached) +void do_autocmd_uienter(uint64_t chanid, bool attached) { static bool recursive = false; @@ -26,7 +26,7 @@ void do_autocmd_uiattach(uint64_t chanid, bool attached) assert(chanid < VARNUMBER_MAX); tv_dict_add_nr(dict, S_LEN("chan"), (varnumber_T)chanid); tv_dict_set_keys_readonly(dict); - apply_autocmds(attached ? EVENT_UIATTACH : EVENT_UIDETACH, + apply_autocmds(attached ? EVENT_UIENTER : EVENT_UILEAVE, NULL, NULL, false, curbuf); tv_dict_clear(dict); diff --git a/src/nvim/auevents.lua b/src/nvim/auevents.lua index 1505b984ad..c808af37b1 100644 --- a/src/nvim/auevents.lua +++ b/src/nvim/auevents.lua @@ -96,8 +96,8 @@ return { 'TextChangedI', -- text was modified in Insert mode(no popup) 'TextChangedP', -- text was modified in Insert mode(popup) 'TextYankPost', -- after a yank or delete was done (y, d, c) - 'UIAttach', -- after a UI attached - 'UIDetach', -- after a UI detaches + 'UIEnter', -- after UI attaches + 'UILeave', -- after UI detaches 'User', -- user defined autocommand 'VimEnter', -- after starting Vim 'VimLeave', -- before exiting Vim @@ -125,7 +125,7 @@ return { TabNewEntered=true, TermClose=true, TermOpen=true, - UIAttach=true, - UIDetach=true, + UIEnter=true, + UILeave=true, }, } diff --git a/src/nvim/ui.c b/src/nvim/ui.c index 9ff2381189..79fa8b8223 100644 --- a/src/nvim/ui.c +++ b/src/nvim/ui.c @@ -296,7 +296,7 @@ void ui_attach_impl(UI *ui, uint64_t chanid) bool is_compositor = (ui == uis[0]); if (!is_compositor) { - do_autocmd_uiattach(chanid, true); + do_autocmd_uienter(chanid, true); } } @@ -333,7 +333,7 @@ void ui_detach_impl(UI *ui, uint64_t chanid) ui_comp_detach(ui); } - do_autocmd_uiattach(chanid, false); + do_autocmd_uienter(chanid, false); } void ui_set_ext_option(UI *ui, UIExtension ext, bool active) -- cgit From 492ac04f7e0ffcc5011d3604b234d149f8b0bb91 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 12 Sep 2019 16:43:52 -0700 Subject: UIEnter/UILeave: fire for embedder UI, builtin TUI Before this, --embed UIs (without --headless) would not trigger UIEnter. For TUI, maybe UIEnter isn't useful, but: - It is less "surprising"/special. - Makes documentation simpler. - When TUI becomes a coprocess, it will happen anyway. --- src/nvim/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/nvim/main.c b/src/nvim/main.c index bca8991b0f..e7835ccbf1 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -12,6 +12,7 @@ #include "nvim/ascii.h" #include "nvim/vim.h" #include "nvim/main.h" +#include "nvim/aucmd.h" #include "nvim/buffer.h" #include "nvim/charset.h" #include "nvim/diff.h" @@ -351,7 +352,7 @@ int main(int argc, char **argv) bool use_remote_ui = (embedded_mode && !headless_mode); bool use_builtin_ui = (!headless_mode && !embedded_mode && !silent_mode); if (use_remote_ui || use_builtin_ui) { - TIME_MSG("waiting for UI to make request"); + TIME_MSG("waiting for UI"); if (use_remote_ui) { remote_ui_wait_for_attach(); } else { @@ -537,6 +538,10 @@ int main(int argc, char **argv) set_vim_var_nr(VV_VIM_DID_ENTER, 1L); apply_autocmds(EVENT_VIMENTER, NULL, NULL, false, curbuf); TIME_MSG("VimEnter autocommands"); + if (use_remote_ui || use_builtin_ui) { + do_autocmd_uienter(use_remote_ui ? CHAN_STDIO : 0, true); + TIME_MSG("UIEnter autocommands"); + } // Adjust default register name for "unnamed" in 'clipboard'. Can only be // done after the clipboard is available and all initial commands that may -- cgit