robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
e0e7f65b74
master
1#include <assert.h> 2#include <inttypes.h> 3#include <stdbool.h> 4#include <stddef.h> 5#include <stdint.h> 6#include <stdlib.h> 7#include <string.h> 8 9#include "klib/kvec.h" 10#include "nvim/api/private/converter.h" 11#include "nvim/api/private/defs.h" 12#include "nvim/api/private/helpers.h" 13#include "nvim/api/private/validate.h" 14#include "nvim/api/ui.h" 15#include "nvim/assert_defs.h" 16#include "nvim/autocmd.h" 17#include "nvim/autocmd_defs.h" 18#include "nvim/channel.h" 19#include "nvim/channel_defs.h" 20#include "nvim/errors.h" 21#include "nvim/eval/typval.h" 22#include "nvim/eval/vars.h" 23#include "nvim/event/defs.h" 24#include "nvim/event/loop.h" 25#include "nvim/event/multiqueue.h" 26#include "nvim/event/wstream.h" 27#include "nvim/globals.h" 28#include "nvim/grid.h" 29#include "nvim/highlight.h" 30#include "nvim/macros_defs.h" 31#include "nvim/main.h" 32#include "nvim/map_defs.h" 33#include "nvim/mbyte.h" 34#include "nvim/memory.h" 35#include "nvim/memory_defs.h" 36#include "nvim/msgpack_rpc/channel.h" 37#include "nvim/msgpack_rpc/channel_defs.h" 38#include "nvim/msgpack_rpc/packer.h" 39#include "nvim/msgpack_rpc/packer_defs.h" 40#include "nvim/option.h" 41#include "nvim/types_defs.h" 42#include "nvim/ui.h" 43 44#ifdef MSWIN 45# include "nvim/os/os_win_console.h" 46#endif 47 48#define BUF_POS (ui ) ((size_t)((ui)->packer.ptr - (ui)->packer.startptr)) 49 50#include "api/ui.c.generated.h" 51#include "ui_events_remote.generated.h" // IWYU pragma: export 52 53// TODO(bfredl): just make UI:s owned by their channels instead 54static PMap (uint64_t )connected_uis = MAP_INIT ; 55 56/// Gets the UI attached to the given channel, or sets an error message on `err`. 57static RemoteUI * get_ui_or_err (uint64_t chan_id ,Error * err ) 58{ 59RemoteUI * ui = pmap_get (uint64_t )(& connected_uis ,chan_id ); 60if (ui == NULL && err != NULL ) { 61api_set_error (err ,kErrorTypeException ,"UI not attached to channel: %" PRId64 ,chan_id ); 62 } 63return ui ; 64} 65 66static char * mpack_array_dyn16 (char ** buf ) 67{ 68mpack_w (buf ,0xdc ); 69char * pos = * buf ; 70mpack_w2 (buf ,0xFFEF ); 71return pos ; 72} 73 74static void mpack_str_small (char ** buf ,const char * str ,size_t len ) 75{ 76assert (len < 0x20 ); 77mpack_w (buf ,0xa0 |len ); 78memcpy (* buf ,str ,len ); 79* buf += len ; 80} 81 82static void remote_ui_destroy (RemoteUI * ui ) 83FUNC_ATTR_NONNULL_ALL 84{ 85xfree (ui -> packer .startptr ); 86XFREE_CLEAR (ui -> term_name ); 87xfree (ui ); 88} 89 90/// Removes the client on the given channel from the list of UIs. 91/// 92/// @param err if non-NULL and there is no UI on the channel, set an error 93/// @param send_error_exit send an "error_exit" event with 0 status first 94void remote_ui_disconnect (uint64_t channel_id ,Error * err ,bool send_error_exit ) 95{ 96RemoteUI * ui = get_ui_or_err (channel_id ,err ); 97if (!ui ) { 98return ; 99 } 100if (send_error_exit ) { 101MAXSIZE_TEMP_ARRAY (args ,1 ); 102ADD_C (args ,INTEGER_OBJ (0 )); 103push_call (ui ,"error_exit" ,args ); 104ui_flush_buf (ui , false); 105 } 106pmap_del (uint64_t )(& connected_uis ,channel_id ,NULL ); 107ui_detach_impl (ui ,channel_id ); 108Channel * chan = find_channel (channel_id ); 109if (chan && chan -> rpc .ui == ui ) { 110chan -> rpc .ui = NULL ; 111 } 112 113remote_ui_destroy (ui ); 114} 115 116/// Detaches the UI on channel `chan` (server keeps running). Sets `channel.detach`, sends an 117/// "error_exit" UI event, and closes the channel. 118/// 119/// @param chan UI channel to disconnect. 120/// @param[out] err Error details, if any. 121void ui_detach_channel (uint64_t chan ,Error * err ) 122{ 123Channel * c = find_channel (chan ); 124VALIDATE (c != NULL ,"%s" ,e_invchan , { 125return ; 126 }); 127#ifdef MSWIN 128bool detach_stdio = c -> streamtype == kChannelStreamStdio ; 129#endif 130// Prevent server self-exit on channel-close. 131c -> detach = true; 132// Server-side UI detach. Doesn't close the channel. 133remote_ui_disconnect (chan ,err , true); 134if (ERROR_SET (err )) { 135return ; 136 } 137// Server-side channel close. 138channel_close (chan ,kChannelPartAll ,NULL ); 139#ifdef MSWIN 140if (detach_stdio ) { 141// After UI/channel detach, move this server off the parent's console so it 142// survives terminal closure and still has working CONIN$/CONOUT$. 143os_swap_to_hidden_console (); 144 } 145#endif 146} 147 148/// Detaches every UI except `keep_chan` (server keeps running). 149/// 150/// @param keep_chan UI channel to preserve. 151/// @return Number of UIs detached. 152size_t ui_detach_others (uint64_t keep_chan ) 153{ 154// Snapshot the channel ids first: ui_detach_channel() mutates connected_uis. 155kvec_t (uint64_t )chans = KV_INITIAL_VALUE ; 156uint64_t chan ; 157map_foreach_key (& connected_uis ,chan , { 158if (chan != keep_chan ) { 159kv_push (chans ,chan ); 160 } 161 }); 162 163size_t detached = 0 ; 164for (size_t i = 0 ;i < kv_size (chans );i ++ ) { 165Error err = ERROR_INIT ; 166ui_detach_channel (kv_A (chans ,i ),& err ); 167if (ERROR_SET (& err )) { 168api_clear_error (& err ); 169 }else { 170detached ++ ; 171 } 172 } 173 174kv_destroy (chans ); 175return detached ; 176} 177 178#ifdef EXITFREE 179void remote_ui_free_all_mem (void ) 180{ 181RemoteUI * ui ; 182map_foreach_value (& connected_uis ,ui , { 183remote_ui_destroy (ui ); 184 }); 185map_destroy (uint64_t ,& connected_uis ); 186} 187#endif 188 189/// Wait until UI has connected. 190void remote_ui_wait_for_attach (void ) 191{ 192LOOP_PROCESS_EVENTS_UNTIL (& main_loop ,main_loop .events ,-1 ,ui_active ()); 193} 194 195/// Activates UI events on the channel. 196/// 197/// Entry point of all UI clients. Allows |--embed| to continue startup. 198/// Implies that the client is ready to show the UI. Adds the client to the 199/// list of UIs. |nvim_list_uis()| 200/// 201/// @note If multiple UI clients are attached, the global screen dimensions 202/// degrade to the smallest client. E.g. if client A requests 80x40 but 203/// client B requests 200x100, the global screen has size 80x40. 204/// 205/// @param channel_id 206/// @param width Requested screen columns 207/// @param height Requested screen rows 208/// @param options |ui-option| map 209/// @param[out] err Error details, if any 210void nvim_ui_attach (uint64_t channel_id ,Integer width ,Integer height ,Dict options ,Error * err ) 211FUNC_API_SINCE (1 )FUNC_API_REMOTE_ONLY 212{ 213if (map_has (uint64_t ,& connected_uis ,channel_id )) { 214api_set_error (err ,kErrorTypeException , 215"UI already attached to channel: %" PRId64 ,channel_id ); 216return ; 217 } 218if (!ui_can_attach_more ()) { 219api_set_error (err ,kErrorTypeException ,"Maximum UI count reached" ); 220return ; 221 } 222 223if (width <=0 || height <=0 ) { 224api_set_error (err ,kErrorTypeValidation ,"Expected width > 0 and height > 0" ); 225return ; 226 } 227RemoteUI * ui = xcalloc (1 ,sizeof (RemoteUI )); 228ui -> channel_id = channel_id ; 229ui -> width = (int )width ; 230ui -> height = (int )height ; 231ui -> pum_row = -1.0 ; 232ui -> pum_col = -1.0 ; 233ui -> rgb = true; 234CLEAR_FIELD (ui -> ui_ext ); 235 236for (size_t i = 0 ;i < options .size ;i ++ ) { 237ui_set_option (ui , true,options .items [i ].key ,options .items [i ].value ,err ); 238if (ERROR_SET (err )) { 239xfree (ui ); 240return ; 241 } 242 } 243 244if (ui -> ui_ext [kUIHlState ]|| ui -> ui_ext [kUIMultigrid ]) { 245ui -> ui_ext [kUILinegrid ]= true; 246 } 247 248if (ui -> ui_ext [kUIMessages ]) { 249// This uses attribute indices, so ext_linegrid is needed. 250ui -> ui_ext [kUILinegrid ]= true; 251// Cmdline uses the messages area, so it should be externalized too. 252ui -> ui_ext [kUICmdline ]= true; 253 } 254 255ui -> cur_event = NULL ; 256ui -> hl_id = 0 ; 257ui -> client_col = -1 ; 258ui -> nevents_pos = NULL ; 259ui -> nevents = 0 ; 260ui -> flushed_events = false; 261ui -> incomplete_event = false; 262ui -> ncalls_pos = NULL ; 263ui -> ncalls = 0 ; 264ui -> ncells_pending = 0 ; 265ui -> packer = (PackerBuffer ) { 266 .startptr = NULL , 267 .ptr = NULL , 268 .endptr = NULL , 269 .packer_flush = ui_flush_callback , 270 .anydata = ui , 271 }; 272ui -> wildmenu_active = false; 273 274pmap_put (uint64_t )(& connected_uis ,channel_id ,ui ); 275current_ui = channel_id ; 276ui_attach_impl (ui ,channel_id ); 277 278Channel * chan = find_channel (channel_id ); 279if (chan ) { 280chan -> rpc .ui = ui ; 281 } 282 283may_trigger_vim_suspend_resume (false); 284} 285 286/// @deprecated 287void ui_attach (uint64_t channel_id ,Integer width ,Integer height ,Boolean enable_rgb ,Error * err ) 288FUNC_API_DEPRECATED_SINCE (1 ) 289{ 290MAXSIZE_TEMP_DICT (opts ,1 ); 291PUT_C (opts ,"rgb" ,BOOLEAN_OBJ (enable_rgb )); 292nvim_ui_attach (channel_id ,width ,height ,opts ,err ); 293} 294 295/// Tells the nvim server if focus was gained or lost by the GUI 296void nvim_ui_set_focus (uint64_t channel_id ,Boolean gained ,Error * error ) 297FUNC_API_SINCE (11 )FUNC_API_REMOTE_ONLY 298{ 299if (!get_ui_or_err (channel_id ,error )) { 300return ; 301 } 302 303if (gained ) { 304current_ui = channel_id ; 305may_trigger_vim_suspend_resume (false); 306 } 307 308do_autocmd_focusgained ((bool )gained ); 309} 310 311/// Deactivates UI events on the channel. 312/// 313/// Removes the client from the list of UIs. |nvim_list_uis()| 314/// 315/// @param channel_id 316/// @param[out] err Error details, if any 317void nvim_ui_detach (uint64_t channel_id ,Error * err ) 318FUNC_API_SINCE (1 )FUNC_API_REMOTE_ONLY 319{ 320remote_ui_disconnect (channel_id ,err , false); 321} 322 323// Send a connect UI event to the UI on the given channel 324void remote_ui_connect (uint64_t channel_id ,char * server_addr ,Error * err ) 325{ 326RemoteUI * ui = get_ui_or_err (channel_id ,err ); 327if (!ui ) { 328return ; 329 } 330 331MAXSIZE_TEMP_ARRAY (args ,1 ); 332ADD_C (args ,CSTR_AS_OBJ (server_addr )); 333 334push_call (ui ,"connect" ,args ); 335} 336 337// TODO(bfredl): use me to detach a specific ui from the server 338void remote_ui_stop (RemoteUI * ui ) 339{ 340} 341 342void nvim_ui_try_resize (uint64_t channel_id ,Integer width ,Integer height ,Error * err ) 343FUNC_API_SINCE (1 )FUNC_API_REMOTE_ONLY 344{ 345RemoteUI * ui = get_ui_or_err (channel_id ,err ); 346if (!ui ) { 347return ; 348 } 349 350if (width <=0 || height <=0 ) { 351api_set_error (err ,kErrorTypeValidation ,"Expected width > 0 and height > 0" ); 352return ; 353 } 354 355ui -> width = (int )width ; 356ui -> height = (int )height ; 357ui_refresh (); 358} 359 360void nvim_ui_set_option (uint64_t channel_id ,String name ,Object value ,Error * error ) 361FUNC_API_SINCE (1 )FUNC_API_REMOTE_ONLY 362{ 363RemoteUI * ui = get_ui_or_err (channel_id ,error ); 364if (!ui ) { 365return ; 366 } 367 368ui_set_option (ui , false,name ,value ,error ); 369} 370 371static void ui_set_option (RemoteUI * ui ,bool init ,String name ,Object value ,Error * err ) 372{ 373if (strequal (name .data ,"override" )) { 374VALIDATE_T ("override" ,kObjectTypeBoolean ,value .type , { 375return ; 376 }); 377ui -> override = value .data .boolean ; 378return ; 379 } 380 381if (strequal (name .data ,"rgb" )) { 382VALIDATE_T ("rgb" ,kObjectTypeBoolean ,value .type , { 383return ; 384 }); 385ui -> rgb = value .data .boolean ; 386// A little drastic, but only takes effect for legacy uis. For linegrid UI 387// only changes metadata for nvim_list_uis(), no refresh needed. 388if (!init && !ui -> ui_ext [kUILinegrid ]) { 389ui_refresh (); 390 } 391return ; 392 } 393 394if (strequal (name .data ,"term_name" )) { 395VALIDATE_T ("term_name" ,kObjectTypeString ,value .type , { 396return ; 397 }); 398set_tty_option ("term" ,string_to_cstr (value .data .string )); 399ui -> term_name = string_to_cstr (value .data .string ); 400return ; 401 } 402 403if (strequal (name .data ,"term_colors" )) { 404VALIDATE_T ("term_colors" ,kObjectTypeInteger ,value .type , { 405return ; 406 }); 407t_colors = (int )value .data .integer ; 408ui -> term_colors = (int )value .data .integer ; 409return ; 410 } 411 412if (strequal (name .data ,"stdin_fd" )) { 413VALIDATE_T ("stdin_fd" ,kObjectTypeInteger ,value .type , { 414return ; 415 }); 416VALIDATE_INT ((value .data .integer >=0 ),"stdin_fd" ,value .data .integer , { 417return ; 418 }); 419VALIDATE ((starting == NO_SCREEN ),"%s" ,"stdin_fd can only be used with first attached UI" , { 420return ; 421 }); 422 423stdin_fd = (int )value .data .integer ; 424return ; 425 } 426 427if (strequal (name .data ,"stdin_tty" )) { 428VALIDATE_T ("stdin_tty" ,kObjectTypeBoolean ,value .type , { 429return ; 430 }); 431if (ui -> channel_id == CHAN_STDIO ) { 432stdin_isatty = value .data .boolean ; 433 } 434ui -> stdin_tty = value .data .boolean ; 435return ; 436 } 437 438if (strequal (name .data ,"stdout_tty" )) { 439VALIDATE_T ("stdout_tty" ,kObjectTypeBoolean ,value .type , { 440return ; 441 }); 442if (ui -> channel_id == CHAN_STDIO ) { 443stdout_isatty = value .data .boolean ; 444 } 445ui -> stdout_tty = value .data .boolean ; 446return ; 447 } 448 449// LEGACY: Deprecated option, use `ext_cmdline` instead. 450bool is_popupmenu = strequal (name .data ,"popupmenu_external" ); 451 452for (UIExtension i = 0 ;i < kUIExtCount ;i ++ ) { 453if (strequal (name .data ,ui_ext_names [i ]) 454|| (i == kUIPopupmenu && is_popupmenu )) { 455VALIDATE_EXP ((value .type == kObjectTypeBoolean ),name .data ,"Boolean" , 456api_typename (value .type ), { 457return ; 458 }); 459bool boolval = value .data .boolean ; 460if (!init && i == kUILinegrid && boolval != ui -> ui_ext [i ]) { 461// There shouldn't be a reason for a UI to do this ever 462// so explicitly don't support this. 463api_set_error (err ,kErrorTypeValidation ,"ext_linegrid option cannot be changed" ); 464 } 465ui -> ui_ext [i ]= boolval ; 466if (!init ) { 467ui_set_ext_option (ui ,i ,boolval ); 468 } 469return ; 470 } 471 } 472 473VALIDATE_S (false,"UI option" ,name .data , { 474return ; 475 }); 476} 477 478/// Tell Nvim to resize a grid. Triggers a grid_resize event with the requested 479/// grid size or the maximum size if it exceeds size limits. 480/// 481/// On invalid grid handle, fails with error. 482/// 483/// @param channel_id 484/// @param grid The handle of the grid to be changed. 485/// @param width The new requested width. 486/// @param height The new requested height. 487/// @param[out] err Error details, if any 488void nvim_ui_try_resize_grid (uint64_t channel_id ,Integer grid ,Integer width ,Integer height , 489Error * err ) 490FUNC_API_SINCE (6 )FUNC_API_REMOTE_ONLY 491{ 492if (!get_ui_or_err (channel_id ,err )) { 493return ; 494 } 495 496if (grid == DEFAULT_GRID_HANDLE ) { 497nvim_ui_try_resize (channel_id ,width ,height ,err ); 498 }else { 499ui_grid_resize ((handle_T )grid , (int )width , (int )height ,err ); 500 } 501} 502 503/// Tells Nvim the number of elements displaying in the popupmenu, to decide 504/// [<PageUp>] and [<PageDown>] movement. 505/// 506/// @param channel_id 507/// @param height Popupmenu height, must be greater than zero. 508/// @param[out] err Error details, if any 509void nvim_ui_pum_set_height (uint64_t channel_id ,Integer height ,Error * err ) 510FUNC_API_SINCE (6 )FUNC_API_REMOTE_ONLY 511{ 512RemoteUI * ui = get_ui_or_err (channel_id ,err ); 513if (!ui ) { 514return ; 515 } 516 517if (height <=0 ) { 518api_set_error (err ,kErrorTypeValidation ,"Expected pum height > 0" ); 519return ; 520 } 521 522if (!ui -> ui_ext [kUIPopupmenu ]) { 523api_set_error (err ,kErrorTypeValidation ,"UI must support the ext_popupmenu option" ); 524return ; 525 } 526 527ui -> pum_nlines = (int )height ; 528} 529 530/// Tells Nvim the geometry of the popupmenu, to align floating windows with an 531/// external popup menu. 532/// 533/// Note that this method is not to be confused with |nvim_ui_pum_set_height()|, 534/// which sets the number of visible items in the popup menu, while this 535/// function sets the bounding box of the popup menu, including visual 536/// elements such as borders and sliders. Floats need not use the same font 537/// size, nor be anchored to exact grid corners, so one can set floating-point 538/// numbers to the popup menu geometry. 539/// 540/// @param channel_id 541/// @param width Popupmenu width. 542/// @param height Popupmenu height. 543/// @param row Popupmenu row. 544/// @param col Popupmenu height. 545/// @param[out] err Error details, if any. 546void nvim_ui_pum_set_bounds (uint64_t channel_id ,Float width ,Float height ,Float row ,Float col , 547Error * err ) 548FUNC_API_SINCE (7 )FUNC_API_REMOTE_ONLY 549{ 550RemoteUI * ui = get_ui_or_err (channel_id ,err ); 551if (!ui ) { 552return ; 553 } 554 555if (!ui -> ui_ext [kUIPopupmenu ]) { 556api_set_error (err ,kErrorTypeValidation , 557"UI must support the ext_popupmenu option" ); 558return ; 559 } 560 561if (width <=0 ) { 562api_set_error (err ,kErrorTypeValidation ,"Expected width > 0" ); 563return ; 564 }else if (height <=0 ) { 565api_set_error (err ,kErrorTypeValidation ,"Expected height > 0" ); 566return ; 567 } 568 569ui -> pum_row = (double )row ; 570ui -> pum_col = (double )col ; 571ui -> pum_width = (double )width ; 572ui -> pum_height = (double )height ; 573ui -> pum_pos = true; 574} 575 576static void flush_event (RemoteUI * ui ) 577{ 578if (ui -> cur_event ) { 579mpack_w2 (& ui -> ncalls_pos ,1 + ui -> ncalls ); 580ui -> cur_event = NULL ; 581ui -> ncalls_pos = NULL ; 582ui -> ncalls = 0 ; 583 } 584} 585 586static void ui_alloc_buf (RemoteUI * ui ) 587{ 588ui -> packer .startptr = alloc_block (); 589ui -> packer .ptr = ui -> packer .startptr ; 590ui -> packer .endptr = ui -> packer .startptr + UI_BUF_SIZE ; 591} 592 593static void prepare_call (RemoteUI * ui ,const char * name ) 594{ 595if (ui -> packer .startptr 596&& (BUF_POS (ui )> UI_BUF_SIZE - EVENT_BUF_SIZE || ui -> ncells_pending >=500 )) { 597ui_flush_buf (ui , false); 598 } 599 600if (ui -> packer .startptr == NULL ) { 601ui_alloc_buf (ui ); 602 } 603 604// To optimize data transfer (especially for "grid_line"), we bundle adjacent 605// calls to same method together, so only add a new call entry if the last 606// method call is different from "name" 607 608if (!ui -> cur_event || !strequal (ui -> cur_event ,name )) { 609char ** buf = & ui -> packer .ptr ; 610if (!ui -> nevents_pos ) { 611// [2, "redraw", [...]] 612mpack_array (buf ,3 ); 613mpack_uint (buf ,2 ); 614mpack_str_small (buf ,S_LEN ("redraw" )); 615ui -> nevents_pos = mpack_array_dyn16 (buf ); 616assert (ui -> cur_event == NULL ); 617 } 618flush_event (ui ); 619ui -> cur_event = name ; 620ui -> ncalls_pos = mpack_array_dyn16 (buf ); 621mpack_str_small (buf ,name ,strlen (name )); 622ui -> nevents ++ ; 623ui -> ncalls = 1 ; 624 }else { 625ui -> ncalls ++ ; 626 } 627} 628 629/// Pushes data into RemoteUI, to be consumed later by remote_ui_flush(). 630static void push_call (RemoteUI * ui ,const char * name ,Array args ) 631{ 632prepare_call (ui ,name ); 633mpack_object_array (args ,& ui -> packer ); 634} 635 636static void ui_flush_callback (PackerBuffer * packer ) 637{ 638RemoteUI * ui = packer -> anydata ; 639ui_flush_buf (ui , true); 640ui_alloc_buf (ui ); 641} 642 643void remote_ui_grid_clear (RemoteUI * ui ,Integer grid ) 644{ 645MAXSIZE_TEMP_ARRAY (args ,1 ); 646if (ui -> ui_ext [kUILinegrid ]) { 647ADD_C (args ,INTEGER_OBJ (grid )); 648 } 649const char * name = ui -> ui_ext [kUILinegrid ] ?"grid_clear" :"clear" ; 650push_call (ui ,name ,args ); 651} 652 653void remote_ui_grid_resize (RemoteUI * ui ,Integer grid ,Integer width ,Integer height ) 654{ 655MAXSIZE_TEMP_ARRAY (args ,3 ); 656if (ui -> ui_ext [kUILinegrid ]) { 657ADD_C (args ,INTEGER_OBJ (grid )); 658 }else { 659ui -> client_col = -1 ;// force cursor update 660 } 661ADD_C (args ,INTEGER_OBJ (width )); 662ADD_C (args ,INTEGER_OBJ (height )); 663const char * name = ui -> ui_ext [kUILinegrid ] ?"grid_resize" :"resize" ; 664push_call (ui ,name ,args ); 665} 666 667void remote_ui_grid_scroll (RemoteUI * ui ,Integer grid ,Integer top ,Integer bot ,Integer left , 668Integer right ,Integer rows ,Integer cols ) 669{ 670if (ui -> ui_ext [kUILinegrid ]) { 671MAXSIZE_TEMP_ARRAY (args ,7 ); 672ADD_C (args ,INTEGER_OBJ (grid )); 673ADD_C (args ,INTEGER_OBJ (top )); 674ADD_C (args ,INTEGER_OBJ (bot )); 675ADD_C (args ,INTEGER_OBJ (left )); 676ADD_C (args ,INTEGER_OBJ (right )); 677ADD_C (args ,INTEGER_OBJ (rows )); 678ADD_C (args ,INTEGER_OBJ (cols )); 679push_call (ui ,"grid_scroll" ,args ); 680 }else { 681MAXSIZE_TEMP_ARRAY (args ,4 ); 682ADD_C (args ,INTEGER_OBJ (top )); 683ADD_C (args ,INTEGER_OBJ (bot - 1 )); 684ADD_C (args ,INTEGER_OBJ (left )); 685ADD_C (args ,INTEGER_OBJ (right - 1 )); 686push_call (ui ,"set_scroll_region" ,args ); 687 688kv_size (args )= 0 ; 689ADD_C (args ,INTEGER_OBJ (rows )); 690push_call (ui ,"scroll" ,args ); 691 692// some clients have "clear" being affected by scroll region, so reset it. 693kv_size (args )= 0 ; 694ADD_C (args ,INTEGER_OBJ (0 )); 695ADD_C (args ,INTEGER_OBJ (ui -> height - 1 )); 696ADD_C (args ,INTEGER_OBJ (0 )); 697ADD_C (args ,INTEGER_OBJ (ui -> width - 1 )); 698push_call (ui ,"set_scroll_region" ,args ); 699 } 700} 701 702void remote_ui_default_colors_set (RemoteUI * ui ,Integer rgb_fg ,Integer rgb_bg ,Integer rgb_sp , 703Integer cterm_fg ,Integer cterm_bg ) 704{ 705if (!ui -> ui_ext [kUITermColors ]) { 706HL_SET_DEFAULT_COLORS (rgb_fg ,rgb_bg ,rgb_sp ); 707 } 708MAXSIZE_TEMP_ARRAY (args ,5 ); 709ADD_C (args ,INTEGER_OBJ (rgb_fg )); 710ADD_C (args ,INTEGER_OBJ (rgb_bg )); 711ADD_C (args ,INTEGER_OBJ (rgb_sp )); 712ADD_C (args ,INTEGER_OBJ (cterm_fg )); 713ADD_C (args ,INTEGER_OBJ (cterm_bg )); 714push_call (ui ,"default_colors_set" ,args ); 715 716// Deprecated 717if (!ui -> ui_ext [kUILinegrid ]) { 718kv_size (args )= 0 ; 719ADD_C (args ,INTEGER_OBJ (ui -> rgb ?rgb_fg :cterm_fg - 1 )); 720push_call (ui ,"update_fg" ,args ); 721 722kv_size (args )= 0 ; 723ADD_C (args ,INTEGER_OBJ (ui -> rgb ?rgb_bg :cterm_bg - 1 )); 724push_call (ui ,"update_bg" ,args ); 725 726kv_size (args )= 0 ; 727ADD_C (args ,INTEGER_OBJ (ui -> rgb ?rgb_sp :-1 )); 728push_call (ui ,"update_sp" ,args ); 729 } 730} 731 732void remote_ui_hl_attr_define (RemoteUI * ui ,Integer id ,HlAttrs rgb_attrs ,HlAttrs cterm_attrs , 733Array info ) 734{ 735if (!ui -> ui_ext [kUILinegrid ]) { 736return ; 737 } 738 739MAXSIZE_TEMP_ARRAY (args ,4 ); 740ADD_C (args ,INTEGER_OBJ (id )); 741MAXSIZE_TEMP_DICT (rgb ,HLATTRS_DICT_SIZE ); 742MAXSIZE_TEMP_DICT (cterm ,HLATTRS_DICT_SIZE ); 743hlattrs2dict (& rgb ,NULL ,rgb_attrs , true, false); 744hlattrs2dict (& cterm ,NULL ,rgb_attrs , false, false); 745 746// URLs are not added in hlattrs2dict since they are used only by UIs and not by the highlight 747// system. So we add them here. 748if (rgb_attrs .url >=0 ) { 749const char * url = hl_get_url ((uint32_t )rgb_attrs .url ); 750PUT_C (rgb ,"url" ,CSTR_AS_OBJ (url )); 751 } 752 753ADD_C (args ,DICT_OBJ (rgb )); 754ADD_C (args ,DICT_OBJ (cterm )); 755 756if (ui -> ui_ext [kUIHlState ]) { 757ADD_C (args ,ARRAY_OBJ (info )); 758 }else { 759ADD_C (args ,ARRAY_OBJ ((Array )ARRAY_DICT_INIT )); 760 } 761 762push_call (ui ,"hl_attr_define" ,args ); 763} 764 765void remote_ui_highlight_set (RemoteUI * ui ,int id ) 766{ 767if (ui -> hl_id == id ) { 768return ; 769 } 770 771ui -> hl_id = id ; 772MAXSIZE_TEMP_DICT (dict ,HLATTRS_DICT_SIZE ); 773hlattrs2dict (& dict ,NULL ,syn_attr2entry (id ),ui -> rgb , false); 774MAXSIZE_TEMP_ARRAY (args ,1 ); 775ADD_C (args ,DICT_OBJ (dict )); 776push_call (ui ,"highlight_set" ,args ); 777} 778 779/// "true" cursor used only for input focus 780void remote_ui_grid_cursor_goto (RemoteUI * ui ,Integer grid ,Integer row ,Integer col ) 781{ 782if (ui -> ui_ext [kUILinegrid ]) { 783MAXSIZE_TEMP_ARRAY (args ,3 ); 784ADD_C (args ,INTEGER_OBJ (grid )); 785ADD_C (args ,INTEGER_OBJ (row )); 786ADD_C (args ,INTEGER_OBJ (col )); 787push_call (ui ,"grid_cursor_goto" ,args ); 788 }else { 789ui -> cursor_row = row ; 790ui -> cursor_col = col ; 791remote_ui_cursor_goto (ui ,row ,col ); 792 } 793} 794 795/// emulated cursor used both for drawing and for input focus 796void remote_ui_cursor_goto (RemoteUI * ui ,Integer row ,Integer col ) 797{ 798if (ui -> client_row == row && ui -> client_col == col ) { 799return ; 800 } 801ui -> client_row = row ; 802ui -> client_col = col ; 803MAXSIZE_TEMP_ARRAY (args ,2 ); 804ADD_C (args ,INTEGER_OBJ (row )); 805ADD_C (args ,INTEGER_OBJ (col )); 806push_call (ui ,"cursor_goto" ,args ); 807} 808 809void remote_ui_put (RemoteUI * ui ,const char * cell ) 810{ 811ui -> client_col ++ ; 812MAXSIZE_TEMP_ARRAY (args ,1 ); 813ADD_C (args ,CSTR_AS_OBJ (cell )); 814push_call (ui ,"put" ,args ); 815} 816 817void remote_ui_raw_line (RemoteUI * ui ,Integer grid ,Integer row ,Integer startcol ,Integer endcol , 818Integer clearcol ,Integer clearattr ,LineFlags flags ,const schar_T * chunk , 819const sattr_T * attrs ) 820{ 821// If MAX_SCHAR_SIZE is made larger, we need to refactor implementation below 822// to not only use FIXSTR (only up to 0x20 bytes) 823STATIC_ASSERT (MAX_SCHAR_SIZE - 1 < 0x20 ,"SCHAR doesn't fit in fixstr" ); 824 825if (ui -> ui_ext [kUILinegrid ]) { 826prepare_call (ui ,"grid_line" ); 827 828char ** buf = & ui -> packer .ptr ; 829mpack_array (buf ,5 ); 830mpack_uint (buf , (uint32_t )grid ); 831mpack_uint (buf , (uint32_t )row ); 832mpack_uint (buf , (uint32_t )startcol ); 833char * lenpos = mpack_array_dyn16 (buf ); 834 835uint32_t repeat = 0 ; 836size_t ncells = (size_t )(endcol - startcol ); 837int last_hl = -1 ; 838uint32_t nelem = 0 ; 839bool was_space = false; 840for (size_t i = 0 ;i < ncells ;i ++ ) { 841repeat ++ ; 842if (i == ncells - 1 || attrs [i ]!= attrs [i + 1 ]|| chunk [i ]!= chunk [i + 1 ]) { 843if ( 844// Close to overflowing the redraw buffer. Finish this event, flush, 845// and start a new "grid_line" event at the current position. 846// For simplicity leave place for the final "clear" element as well, 847// hence the factor of 2 in the check. 848UI_BUF_SIZE - BUF_POS (ui )< 2 * (1 + 2 + MAX_SCHAR_SIZE + 5 + 5 )+ 1 849// Also if there is a lot of packed cells, pass them off to the UI to 850// let it start processing them. 851|| ui -> ncells_pending >=500 ) { 852// If the last chunk was all spaces, add an empty clearing chunk, 853// so it's clear that the last chunk wasn't a clearing chunk. 854if (was_space ) { 855nelem ++ ; 856ui -> ncells_pending += 1 ; 857mpack_array (buf ,3 ); 858mpack_str_small (buf ,S_LEN (" " )); 859mpack_uint (buf , (uint32_t )clearattr ); 860mpack_uint (buf ,0 ); 861 } 862mpack_w2 (& lenpos ,nelem ); 863// We only ever set the wrap field on the final "grid_line" event for the line. 864mpack_bool (buf , false); 865ui_flush_buf (ui , false); 866 867prepare_call (ui ,"grid_line" ); 868mpack_array (buf ,5 ); 869mpack_uint (buf , (uint32_t )grid ); 870mpack_uint (buf , (uint32_t )row ); 871mpack_uint (buf , (uint32_t )startcol + (uint32_t )i - repeat + 1 ); 872lenpos = mpack_array_dyn16 (buf ); 873nelem = 0 ; 874last_hl = -1 ; 875 } 876uint32_t csize = (repeat > 1 ) ?3 : ((attrs [i ]!= last_hl ) ?2 :1 ); 877nelem ++ ; 878mpack_array (buf ,csize ); 879char * size_byte = (* buf )++ ; 880size_t len = schar_get_adv (buf ,chunk [i ]); 881* size_byte = (char )(0xa0 |len ); 882if (csize >=2 ) { 883mpack_uint (buf , (uint32_t )attrs [i ]); 884if (csize >=3 ) { 885mpack_uint (buf ,repeat ); 886 } 887 } 888ui -> ncells_pending += MIN (repeat ,2 ); 889last_hl = attrs [i ]; 890repeat = 0 ; 891was_space = chunk [i ]== schar_from_ascii (' ' ); 892 } 893 } 894// If the last chunk was all spaces, add a clearing chunk even if there are 895// no more cells to clear, so there is no ambiguity about what to clear. 896if (endcol < clearcol || was_space ) { 897nelem ++ ; 898ui -> ncells_pending += 1 ; 899mpack_array (buf ,3 ); 900mpack_str_small (buf ,S_LEN (" " )); 901mpack_uint (buf , (uint32_t )clearattr ); 902mpack_uint (buf , (uint32_t )(clearcol - endcol )); 903 } 904mpack_w2 (& lenpos ,nelem ); 905mpack_bool (buf ,flags & kLineFlagWrap ); 906 }else { 907for (int i = 0 ;i < endcol - startcol ;i ++ ) { 908remote_ui_cursor_goto (ui ,row ,startcol + i ); 909remote_ui_highlight_set (ui ,attrs [i ]); 910char sc_buf [MAX_SCHAR_SIZE ]; 911schar_get (sc_buf ,chunk [i ]); 912remote_ui_put (ui ,sc_buf ); 913if (utf_ambiguous_width (sc_buf )) { 914ui -> client_col = -1 ;// force cursor update 915 } 916 } 917if (endcol < clearcol ) { 918remote_ui_cursor_goto (ui ,row ,endcol ); 919remote_ui_highlight_set (ui , (int )clearattr ); 920// legacy eol_clear was only ever used with cleared attributes 921// so be on the safe side 922if (clearattr == 0 && clearcol == Columns ) { 923Array args = ARRAY_DICT_INIT ; 924push_call (ui ,"eol_clear" ,args ); 925 }else { 926for (Integer c = endcol ;c < clearcol ;c ++ ) { 927remote_ui_put (ui ," " ); 928 } 929 } 930 } 931 } 932} 933 934/// Flush the internal packing buffer to the client. 935/// 936/// This might happen multiple times before the actual ui_flush, if the 937/// total redraw size is large! 938static void ui_flush_buf (RemoteUI * ui ,bool incomplete_event ) 939{ 940if (!ui -> packer .startptr || !BUF_POS (ui )) { 941return ; 942 } 943ui -> incomplete_event = incomplete_event ; 944 945flush_event (ui ); 946if (ui -> nevents_pos != NULL ) { 947mpack_w2 (& ui -> nevents_pos ,ui -> nevents ); 948ui -> nevents = 0 ; 949ui -> nevents_pos = NULL ; 950 } 951 952WBuffer * buf = wstream_new_buffer (ui -> packer .startptr ,BUF_POS (ui ),1 ,free_block ); 953rpc_write_raw (ui -> channel_id ,buf ); 954 955ui -> packer .startptr = NULL ; 956ui -> packer .ptr = NULL ; 957 958// we have sent events to the client, but possibly not yet the final "flush" event. 959ui -> flushed_events = true; 960ui -> ncells_pending = 0 ; 961} 962 963/// An intentional flush (vsync) when Nvim is finished redrawing the screen 964/// 965/// Clients can know this happened by a final "flush" event at the end of the 966/// "redraw" batch. 967void remote_ui_flush (RemoteUI * ui ) 968{ 969if (ui -> nevents > 0 || ui -> flushed_events ) { 970if (!ui -> ui_ext [kUILinegrid ]) { 971remote_ui_cursor_goto (ui ,ui -> cursor_row ,ui -> cursor_col ); 972 } 973push_call (ui ,"flush" , (Array )ARRAY_DICT_INIT ); 974ui_flush_buf (ui , false); 975ui -> flushed_events = false; 976 } 977} 978 979void remote_ui_ui_send (RemoteUI * ui ,String content ) 980{ 981if (!ui -> stdout_tty ) { 982return ; 983 } 984 985MAXSIZE_TEMP_ARRAY (args ,1 ); 986ADD_C (args ,STRING_OBJ (content )); 987push_call (ui ,"ui_send" ,args ); 988} 989 990void remote_ui_flush_pending_data (RemoteUI * ui ) 991{ 992ui_flush_buf (ui , false); 993} 994 995static Array translate_contents (RemoteUI * ui ,Array contents ,Arena * arena ) 996{ 997Array new_contents = arena_array (arena ,contents .size ); 998for (size_t i = 0 ;i < contents .size ;i ++ ) { 999Array item = contents .items [i ].data .array ; 1000Array new_item = arena_array (arena ,2 ); 1001int attr = (int )item .items [0 ].data .integer ; 1002if (attr ) { 1003Dict rgb_attrs = arena_dict (arena ,HLATTRS_DICT_SIZE ); 1004hlattrs2dict (& rgb_attrs ,NULL ,syn_attr2entry (attr ),ui -> rgb , false); 1005ADD_C (new_item ,DICT_OBJ (rgb_attrs )); 1006 }else { 1007ADD_C (new_item ,DICT_OBJ ((Dict )ARRAY_DICT_INIT )); 1008 } 1009ADD_C (new_item ,item .items [1 ]); 1010ADD_C (new_contents ,ARRAY_OBJ (new_item )); 1011 } 1012return new_contents ; 1013} 1014 1015static Array translate_firstarg (RemoteUI * ui ,Array args ,Arena * arena ) 1016{ 1017Array new_args = arena_array (arena ,args .size ); 1018Array contents = args .items [0 ].data .array ; 1019 1020ADD_C (new_args ,ARRAY_OBJ (translate_contents (ui ,contents ,arena ))); 1021for (size_t i = 1 ;i < args .size ;i ++ ) { 1022ADD_C (new_args ,args .items [i ]); 1023 } 1024return new_args ; 1025} 1026 1027void remote_ui_event (RemoteUI * ui ,char * name ,Array args ) 1028{ 1029Arena arena = ARENA_EMPTY ; 1030if (!ui -> ui_ext [kUILinegrid ]) { 1031// the representation of highlights in cmdline changed, translate back 1032// never consumes args 1033if (strequal (name ,"cmdline_show" )) { 1034Array new_args = translate_firstarg (ui ,args ,& arena ); 1035push_call (ui ,name ,new_args ); 1036 gotofree_ret ; 1037 }else if (strequal (name ,"cmdline_block_show" )) { 1038Array block = args .items [0 ].data .array ; 1039Array new_block = arena_array (& arena ,block .size ); 1040for (size_t i = 0 ;i < block .size ;i ++ ) { 1041ADD_C (new_block ,ARRAY_OBJ (translate_contents (ui ,block .items [i ].data .array ,& arena ))); 1042 } 1043MAXSIZE_TEMP_ARRAY (new_args ,1 ); 1044ADD_C (new_args ,ARRAY_OBJ (new_block )); 1045push_call (ui ,name ,new_args ); 1046 gotofree_ret ; 1047 }else if (strequal (name ,"cmdline_block_append" )) { 1048Array new_args = translate_firstarg (ui ,args ,& arena ); 1049push_call (ui ,name ,new_args ); 1050 gotofree_ret ; 1051 } 1052 } 1053 1054// Back-compat: translate popupmenu_xx to legacy wildmenu_xx. 1055if (ui -> ui_ext [kUIWildmenu ]) { 1056if (strequal (name ,"popupmenu_show" )) { 1057ui -> wildmenu_active = (args .items [4 ].data .integer == -1 ) 1058|| !ui -> ui_ext [kUIPopupmenu ]; 1059if (ui -> wildmenu_active ) { 1060Array items = args .items [0 ].data .array ; 1061Array new_items = arena_array (& arena ,items .size ); 1062for (size_t i = 0 ;i < items .size ;i ++ ) { 1063ADD_C (new_items ,items .items [i ].data .array .items [0 ]); 1064 } 1065MAXSIZE_TEMP_ARRAY (new_args ,1 ); 1066ADD_C (new_args ,ARRAY_OBJ (new_items )); 1067push_call (ui ,"wildmenu_show" ,new_args ); 1068if (args .items [1 ].data .integer != -1 ) { 1069kv_size (new_args )= 0 ; 1070ADD_C (new_args ,args .items [1 ]); 1071push_call (ui ,"wildmenu_select" ,new_args ); 1072 } 1073 gotofree_ret ; 1074 } 1075 }else if (strequal (name ,"popupmenu_select" )) { 1076if (ui -> wildmenu_active ) { 1077name = "wildmenu_select" ; 1078 } 1079 }else if (strequal (name ,"popupmenu_hide" )) { 1080if (ui -> wildmenu_active ) { 1081name = "wildmenu_hide" ; 1082 } 1083 } 1084 } 1085 1086push_call (ui ,name ,args ); 1087return ; 1088 1089free_ret : 1090arena_mem_free (arena_finish (& arena )); 1091} 1092 1093/// Sends arbitrary data to a UI. Use this instead of |nvim_chan_send()| or `io.stdout:write()`, if 1094/// you really want to write to the |TUI| host terminal. 1095/// 1096/// Emits a "ui_send" event to all UIs with the "stdout_tty" |ui-option| set. UIs are expected to 1097/// write the received data to a connected TTY if one exists. 1098/// 1099/// @param channel_id 1100/// @param content Content to write to the TTY 1101/// @param[out] err Error details, if any 1102void nvim_ui_send (uint64_t channel_id ,String content ,Error * err ) 1103FUNC_API_SINCE (14 ) 1104{ 1105ui_call_ui_send (content ); 1106}