UIAttach, UIDetach

This commit is contained in:
Rui Abreu Ferreira
2017-06-21 16:59:52 +01:00
committed by Justin M. Keyes
parent 426399c2c4
commit e9cf515888
8 changed files with 60 additions and 15 deletions

View File

@@ -280,8 +280,8 @@ Name triggered by ~
Startup and exit Startup and exit
|VimEnter| after doing all the startup stuff |VimEnter| after doing all the startup stuff
|GUIEnter| after starting the GUI successfully |UIAttach| after a new UI attaches
|GUIFailed| after starting the GUI failed |UIDetach| after a UI detaches
|TermResponse| after the terminal response to t_RV is received |TermResponse| after the terminal response to t_RV is received
|QuitPre| when using `:quit`, before deciding whether to exit |QuitPre| when using `:quit`, before deciding whether to exit
|ExitPre| when using a command that may make Vim exit |ExitPre| when using a command that may make Vim exit
@@ -805,19 +805,14 @@ FuncUndefined When a user function is used but it isn't
NOTE: When writing Vim scripts a better NOTE: When writing Vim scripts a better
alternative is to use an autoloaded function. alternative is to use an autoloaded function.
See |autoload-functions|. See |autoload-functions|.
*GUIEnter* {Nvim} *UIAttach*
GUIEnter After starting the GUI successfully, and after UIAttach After a new UI connects to nvim and successfully
opening the window. It is triggered before calls |nvim_ui_attach|. Sets chan in |v:event| with
VimEnter when using gvim. Can be used to the channel id or 0 if using the internal UI.
position the window from a gvimrc file: > {Nvim} *UIDetach*
:autocmd GUIEnter * winpos 100 50 UIDetach After a UI detaches from nvim. Sets chan in |v:event|
< *GUIFailed* with the channel id or 0 if using the internal UI.
GUIFailed After starting the GUI failed. Vim may *InsertChange*
continue to run in the terminal, if possible
(only on Unix and alikes, when connecting the
X server fails). You may want to quit Vim: >
:autocmd GUIFailed * qall
< *InsertChange*
InsertChange When typing <Insert> while in Insert or InsertChange When typing <Insert> while in Insert or
Replace mode. The |v:insertmode| variable Replace mode. The |v:insertmode| variable
indicates the new mode. indicates the new mode.

View File

@@ -28,6 +28,8 @@ Environment Variables ~
Events ~ Events ~
*EncodingChanged* Never fired; 'encoding' is always "utf-8". *EncodingChanged* Never fired; 'encoding' is always "utf-8".
*FileEncoding* Never fired; equivalent to |EncodingChanged|. *FileEncoding* Never fired; equivalent to |EncodingChanged|.
*GUIEnter* Never fired; Use |UIAttach| instead.
*GUIFailed* Never fired.
Keycodes ~ Keycodes ~
*<MouseDown>* Use <ScrollWheelUp> instead. *<MouseDown>* Use <ScrollWheelUp> instead.

View File

@@ -154,6 +154,8 @@ Events:
|TermClose| |TermClose|
|TermOpen| |TermOpen|
|TextYankPost| |TextYankPost|
|UIAttach|
|UIDetach|
|VimResume| |VimResume|
|VimSuspend| |VimSuspend|

View File

@@ -19,6 +19,8 @@
#include "nvim/highlight.h" #include "nvim/highlight.h"
#include "nvim/screen.h" #include "nvim/screen.h"
#include "nvim/window.h" #include "nvim/window.h"
#include "nvim/fileio.h"
#include "nvim/eval.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "api/ui.c.generated.h" # 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); pmap_put(uint64_t)(connected_uis, channel_id, ui);
ui_attach_impl(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 /// @deprecated
@@ -196,6 +204,12 @@ void nvim_ui_detach(uint64_t channel_id, Error *err)
return; return;
} }
remote_ui_disconnect(channel_id); 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);
} }

View File

@@ -96,6 +96,8 @@ return {
'TextChangedI', -- text was modified in Insert mode(no popup) 'TextChangedI', -- text was modified in Insert mode(no popup)
'TextChangedP', -- text was modified in Insert mode(popup) 'TextChangedP', -- text was modified in Insert mode(popup)
'TextYankPost', -- after a yank or delete was done (y, d, c) '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 'User', -- user defined autocommand
'VimEnter', -- after starting Vim 'VimEnter', -- after starting Vim
'VimLeave', -- before exiting Vim 'VimLeave', -- before exiting Vim
@@ -123,5 +125,7 @@ return {
TabNewEntered=true, TabNewEntered=true,
TermClose=true, TermClose=true,
TermOpen=true, TermOpen=true,
UIAttach=true,
UIDetach=true,
}, },
} }

View File

@@ -172,6 +172,7 @@ static Channel *channel_alloc(ChannelStreamType type)
chan->refcount = 1; chan->refcount = 1;
chan->exit_status = -1; chan->exit_status = -1;
chan->streamtype = type; chan->streamtype = type;
assert(chan->id <= VARNUMBER_MAX);
pmap_put(uint64_t)(channels, chan->id, chan); pmap_put(uint64_t)(channels, chan->id, chan);
return chan; return chan;
} }
@@ -190,6 +191,7 @@ void channel_create_event(Channel *chan, const char *ext_source)
source = (const char *)IObuff; source = (const char *)IObuff;
} }
assert(chan->id <= VARNUMBER_MAX);
Dictionary info = channel_info(chan->id); Dictionary info = channel_info(chan->id);
typval_T tv = TV_INITIAL_VALUE; typval_T tv = TV_INITIAL_VALUE;
// TODO(bfredl): do the conversion in one step. Also would be nice // TODO(bfredl): do the conversion in one step. Also would be nice

View File

@@ -17,6 +17,8 @@
#include "nvim/ui_bridge.h" #include "nvim/ui_bridge.h"
#include "nvim/ugrid.h" #include "nvim/ugrid.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/fileio.h"
#include "nvim/eval.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS #ifdef INCLUDE_GENERATED_DECLARATIONS
# include "ui_bridge.c.generated.h" # 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); uv_mutex_unlock(&rv->mutex);
ui_attach_impl(&rv->bridge); 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; 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 // Detach bridge first, so that "stop" is the last event the TUI loop
// receives from the main thread. #8041 // receives from the main thread. #8041
ui_detach_impl(b); 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; UIBridgeData *bridge = (UIBridgeData *)b;
bool stopped = bridge->stopped = false; bool stopped = bridge->stopped = false;
UI_BRIDGE_CALL(b, stop, 1, b); UI_BRIDGE_CALL(b, stop, 1, b);

View File

@@ -6,6 +6,7 @@ local eval = helpers.eval
local meths = helpers.meths local meths = helpers.meths
local request = helpers.request local request = helpers.request
local pcall_err = helpers.pcall_err local pcall_err = helpers.pcall_err
local command = helpers.command
describe('nvim_ui_attach()', function() describe('nvim_ui_attach()', function()
before_each(function() before_each(function()
@@ -34,4 +35,13 @@ describe('nvim_ui_attach()', function()
eq('UI already attached to channel: 1', eq('UI already attached to channel: 1',
pcall_err(request, 'nvim_ui_attach', 40, 10, { rgb=false })) pcall_err(request, 'nvim_ui_attach', 40, 10, { rgb=false }))
end) end)
it('autocmds UIAttach/Detach set v:event', function()
local screen = Screen.new()
command('autocmd UIAttach * :let g:ui_attach_v_event = deepcopy(v:event)')
command('autocmd UIDetach * :let g:ui_detach_v_event = deepcopy(v:event)')
screen:attach()
assert.same({chan=1}, eval('g:ui_attach_v_event'))
screen:detach()
assert.same({chan=1}, eval('g:ui_detach_v_event'))
end)
end) end)