robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1#include <assert.h> 2#include <stdbool.h> 3#include <stdlib.h> 4#include <string.h> 5 6#include "klib/kvec.h" 7#include "nvim/api/private/defs.h" 8#include "nvim/api/private/helpers.h" 9#include "nvim/api/vim.h" 10#include "nvim/api/win_config.h" 11#include "nvim/ascii_defs.h" 12#include "nvim/autocmd.h" 13#include "nvim/buffer.h" 14#include "nvim/buffer_defs.h" 15#include "nvim/charset.h" 16#include "nvim/decoration.h" 17#include "nvim/decoration_defs.h" 18#include "nvim/drawscreen.h" 19#include "nvim/errors.h" 20#include "nvim/fileio.h" 21#include "nvim/globals.h" 22#include "nvim/grid.h" 23#include "nvim/grid_defs.h" 24#include "nvim/highlight.h" 25#include "nvim/highlight_defs.h" 26#include "nvim/highlight_group.h" 27#include "nvim/lua/executor.h" 28#include "nvim/macros_defs.h" 29#include "nvim/mbyte.h" 30#include "nvim/memory.h" 31#include "nvim/message.h" 32#include "nvim/mouse.h" 33#include "nvim/move.h" 34#include "nvim/option.h" 35#include "nvim/option_defs.h" 36#include "nvim/option_vars.h" 37#include "nvim/optionstr.h" 38#include "nvim/os/fs.h" 39#include "nvim/plines.h" 40#include "nvim/pos_defs.h" 41#include "nvim/strings.h" 42#include "nvim/types_defs.h" 43#include "nvim/ui.h" 44#include "nvim/ui_defs.h" 45#include "nvim/vim_defs.h" 46#include "nvim/window.h" 47#include "nvim/winfloat.h" 48 49#include "options_keysets.generated.h" 50#include "winfloat.c.generated.h" 51 52/// Creates a new float, or transforms an existing window to a float. 53/// 54/// @param wp if NULL, allocate a new window, otherwise turn existing window into a float. 55/// @param last make the window the last one in the window list. 56/// Only used when allocating the autocommand window. 57/// @param config must already have been validated! 58win_T * win_new_float (win_T * wp ,bool last ,WinConfig fconfig ,Error * err ) 59{ 60if (wp == NULL ) { 61tabpage_T * tp = NULL ; 62win_T * tp_last = last ?lastwin :lastwin_nofloating (NULL ); 63if (fconfig .window != 0 ) { 64assert (!last ); 65win_T * parent_wp = find_window_by_handle (fconfig .window ,err ); 66if (!parent_wp ) { 67return NULL ; 68 } 69tp = win_find_tabpage (parent_wp ); 70if (!tp ) { 71return NULL ; 72 } 73tp_last = lastwin_nofloating (tp == curtab ?NULL :tp ); 74 } 75wp = win_alloc (tp_last , false); 76win_init (wp ,curwin ,0 ); 77if (wp -> w_p_wbr != NULL && fconfig .height == 1 ) { 78if (wp -> w_p_wbr != empty_string_option ) { 79free_string_option (wp -> w_p_wbr ); 80 } 81wp -> w_p_wbr = empty_string_option ; 82 } 83if (wp -> w_p_stl && wp -> w_p_stl != empty_string_option ) { 84free_string_option (wp -> w_p_stl ); 85wp -> w_p_stl = empty_string_option ; 86 } 87 }else { 88assert (!last ); 89assert (!wp -> w_floating ); 90tabpage_T * win_tp = win_find_tabpage (wp ); 91assert (win_tp ); 92if ((win_tp == curtab && firstwin == wp && lastwin_nofloating (NULL )== wp ) 93|| (win_tp != curtab && win_tp -> tp_firstwin == wp && lastwin_nofloating (win_tp )== wp )) { 94api_set_error (err ,kErrorTypeException ,"Cannot change last window into float" ); 95return NULL ; 96 } 97tabpage_T * tp = win_tp == curtab ?NULL :win_tp ; 98int dir ; 99winframe_remove (wp ,& dir ,tp ,NULL ); 100XFREE_CLEAR (wp -> w_frame ); 101win_remove (wp ,tp ); 102if (win_tp == curtab ) { 103last_status (false);// may need to remove last status line 104win_comp_pos ();// recompute window positions 105 } 106win_append (lastwin_nofloating (tp ),wp ,tp ); 107 } 108wp -> w_floating = true; 109wp -> w_status_height = wp -> w_p_stl && * wp -> w_p_stl != NUL 110&& (p_ls == 1 || p_ls == 2 ) ?STATUS_HEIGHT :0 ; 111wp -> w_winbar_height = 0 ; 112wp -> w_hsep_height = 0 ; 113wp -> w_vsep_width = 0 ; 114 115win_config_float (wp ,fconfig ); 116redraw_later (wp ,UPD_VALID ); 117return wp ; 118} 119 120void win_set_minimal_style (win_T * wp ) 121{ 122wp -> w_p_nu = false; 123wp -> w_p_rnu = false; 124wp -> w_p_cul = false; 125wp -> w_p_cuc = false; 126wp -> w_p_spell = false; 127wp -> w_p_list = false; 128 129// Hide EOB region: use " " fillchar and cleared highlighting 130if (wp -> w_p_fcs_chars .eob != ' ' ) { 131char * old = wp -> w_p_fcs ; 132wp -> w_p_fcs = ((* old == NUL ) 133 ?xstrdup ("eob: " ) 134 :concat_str (old ,",eob: " )); 135free_string_option (old ); 136 } 137 138// TODO(bfredl): this could use a highlight namespace directly, 139// and avoid peculiarities around window options 140char * old = wp -> w_p_winhl ; 141wp -> w_p_winhl = ((* old == NUL ) 142 ?xstrdup ("EndOfBuffer:" ) 143 :concat_str (old ,",EndOfBuffer:" )); 144free_string_option (old ); 145parse_winhl_opt (NULL ,wp ); 146 147// signcolumn: use 'auto' 148if (wp -> w_p_scl [0 ]!= 'a' || strlen (wp -> w_p_scl ) >=8 ) { 149free_string_option (wp -> w_p_scl ); 150wp -> w_p_scl = xstrdup ("auto" ); 151 } 152 153// foldcolumn: use '0' 154if (wp -> w_p_fdc [0 ]!= '0' ) { 155free_string_option (wp -> w_p_fdc ); 156wp -> w_p_fdc = xstrdup ("0" ); 157 } 158 159// colorcolumn: cleared 160if (wp -> w_p_cc != NULL && * wp -> w_p_cc != NUL ) { 161free_string_option (wp -> w_p_cc ); 162wp -> w_p_cc = xstrdup ("" ); 163 } 164 165// statuscolumn: cleared 166if (wp -> w_p_stc != NULL && * wp -> w_p_stc != NUL ) { 167free_string_option (wp -> w_p_stc ); 168wp -> w_p_stc = empty_string_option ; 169 } 170 171// statusline: cleared (for floating windows) 172if (wp -> w_floating && wp -> w_p_stl != NULL && * wp -> w_p_stl != NUL ) { 173free_string_option (wp -> w_p_stl ); 174wp -> w_p_stl = empty_string_option ; 175if (wp -> w_status_height > 0 ) { 176win_config_float (wp ,wp -> w_config ); 177 } 178 } 179} 180 181int win_border_height (win_T * wp ) 182{ 183return wp -> w_border_adj [0 ]+ wp -> w_border_adj [2 ]; 184} 185 186int win_border_width (win_T * wp ) 187{ 188return wp -> w_border_adj [1 ]+ wp -> w_border_adj [3 ]; 189} 190 191void win_config_float (win_T * wp ,WinConfig fconfig ) 192{ 193// Process statusline changes before applying new height from config 194bool show_stl = * wp -> w_p_stl != NUL && (p_ls == 1 || p_ls == 2 ); 195if (wp -> w_status_height && !show_stl ) { 196win_remove_status_line (wp , false); 197 }else if (wp -> w_status_height == 0 && show_stl ) { 198wp -> w_status_height = STATUS_HEIGHT ; 199 } 200 201wp -> w_width = MAX (fconfig .width ,1 ); 202wp -> w_height = MAX (fconfig .height ,1 ); 203 204if (fconfig .relative == kFloatRelativeCursor ) { 205validate_cursor (curwin ); 206fconfig .relative = kFloatRelativeWindow ; 207fconfig .row += curwin -> w_wrow ; 208fconfig .col += curwin -> w_wcol ; 209fconfig .window = curwin -> handle ; 210 }else if (fconfig .relative == kFloatRelativeMouse ) { 211int row = mouse_row ; 212int col = mouse_col ; 213int grid = mouse_grid ; 214win_T * mouse_win = mouse_find_win_inner (& grid ,& row ,& col ); 215if (mouse_win != NULL ) { 216fconfig .relative = kFloatRelativeWindow ; 217fconfig .row += row ; 218fconfig .col += col ; 219fconfig .window = mouse_win -> handle ; 220 } 221 } 222 223bool change_external = fconfig .external != wp -> w_config .external ; 224bool change_border = (fconfig .border != wp -> w_config .border 225|| memcmp (fconfig .border_hl_ids , 226wp -> w_config .border_hl_ids , 227sizeof fconfig .border_hl_ids )!= 0 ); 228 229merge_win_config (& wp -> w_config ,fconfig ); 230 231bool has_border = wp -> w_floating && wp -> w_config .border ; 232for (int i = 0 ;i < 4 ;i ++ ) { 233int new_adj = has_border && wp -> w_config .border_chars [2 * i + 1 ][0 ]; 234if (new_adj != wp -> w_border_adj [i ]) { 235change_border = true; 236wp -> w_border_adj [i ]= new_adj ; 237 } 238 } 239 240if (!ui_has (kUIMultigrid )) { 241int above_ch = wp -> w_config .zindex < kZIndexMessages ? (int )p_ch :0 ; 242wp -> w_height = MIN (wp -> w_height ,Rows - win_border_height (wp )- above_ch ); 243wp -> w_width = MIN (wp -> w_width ,Columns - win_border_width (wp )); 244 } 245 246win_set_inner_size (wp , true); 247set_must_redraw (UPD_VALID ); 248wp -> w_redr_status = wp -> w_status_height ; 249wp -> w_pos_changed = true; 250if (change_external || change_border ) { 251wp -> w_hl_needs_update = true; 252redraw_later (wp ,UPD_NOT_VALID ); 253 } 254 255// compute initial position 256if (wp -> w_config .relative == kFloatRelativeWindow ) { 257int row = (int )wp -> w_config .row ; 258int col = (int )wp -> w_config .col ; 259Error dummy = ERROR_INIT ; 260win_T * parent = find_window_by_handle (wp -> w_config .window ,& dummy ); 261if (parent ) { 262row += parent -> w_winrow ; 263col += parent -> w_wincol ; 264grid_adjust (& parent -> w_grid ,& row ,& col ); 265if (wp -> w_config .bufpos .lnum >=0 ) { 266pos_T pos = {MIN (wp -> w_config .bufpos .lnum + 1 ,parent -> w_buffer -> b_ml .ml_line_count ), 267wp -> w_config .bufpos .col ,0 }; 268int trow ,tcol ,tcolc ,tcole ; 269textpos2screenpos (parent ,& pos ,& trow ,& tcol ,& tcolc ,& tcole , true); 270row += trow - 1 ; 271col += tcol - 1 ; 272 } 273 } 274api_clear_error (& dummy ); 275wp -> w_winrow = row ; 276wp -> w_wincol = col ; 277 }else { 278wp -> w_winrow = (int )fconfig .row ; 279wp -> w_wincol = (int )fconfig .col ; 280 } 281 282// changing border style while keeping border only requires redrawing border 283if (fconfig .border ) { 284wp -> w_redr_border = true; 285redraw_later (wp ,UPD_VALID ); 286 } 287} 288 289static int float_zindex_cmp (const void * a ,const void * b ) 290{ 291int za = (* (win_T ** )a )-> w_config .zindex ; 292int zb = (* (win_T ** )b )-> w_config .zindex ; 293return za == zb ?0 :za < zb ?1 :-1 ; 294} 295 296void win_float_remove (bool bang ,int count ) 297{ 298kvec_t (win_T * )float_win_arr = KV_INITIAL_VALUE ; 299for (win_T * wp = lastwin ;wp && wp -> w_floating ;wp = wp -> w_prev ) { 300if (wp -> w_config .hide || wp -> w_p_wp ) { 301continue ; 302 } 303kv_push (float_win_arr ,wp ); 304 } 305if (float_win_arr .size > 0 ) { 306qsort (float_win_arr .items ,float_win_arr .size ,sizeof (win_T * ),float_zindex_cmp ); 307 } 308for (size_t i = 0 ;i < float_win_arr .size ;i ++ ) { 309win_T * wp = float_win_arr .items [i ]; 310if (win_valid (wp )&& win_close (wp , false, false)== FAIL ) { 311break ; 312 } 313if (!bang ) { 314count -- ; 315if (count == 0 ) { 316break ; 317 } 318 } 319 } 320kv_destroy (float_win_arr ); 321} 322 323void win_check_anchored_floats (win_T * win ) 324{ 325for (win_T * wp = lastwin ;wp && wp -> w_floating ;wp = wp -> w_prev ) { 326// float might be anchored to moved window 327if (wp -> w_config .relative == kFloatRelativeWindow 328&& wp -> w_config .window == win -> handle ) { 329wp -> w_pos_changed = true; 330 } 331 } 332} 333 334void win_float_update_statusline (void ) 335{ 336for (win_T * wp = lastwin ;wp && wp -> w_floating ;wp = wp -> w_prev ) { 337bool has_status = wp -> w_status_height > 0 ; 338bool should_show = * wp -> w_p_stl != NUL && (p_ls == 1 || p_ls == 2 ); 339if (should_show != has_status ) { 340win_config_float (wp ,wp -> w_config ); 341 } 342 } 343} 344 345void win_float_anchor_laststatus (void ) 346{ 347FOR_ALL_WINDOWS_IN_TAB (win ,curtab ) { 348if (win -> w_config .relative == kFloatRelativeLaststatus ) { 349win -> w_pos_changed = true; 350 } 351 } 352} 353 354void win_reconfig_floats (void ) 355{ 356for (win_T * wp = lastwin ;wp && wp -> w_floating ;wp = wp -> w_prev ) { 357win_config_float (wp ,wp -> w_config ); 358 } 359} 360 361/// Return true if "win" is floating window in the current tab page. 362/// 363/// @param win window to check 364bool win_float_valid (const win_T * win ) 365FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT 366{ 367if (win == NULL ) { 368return false; 369 } 370 371FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 372if (wp == win ) { 373return wp -> w_floating ; 374 } 375 } 376return false; 377} 378 379/// Find the floating window of the given kind in the current tabpage. 380/// 381/// @param kind the window kind to match 382/// 383/// @return the matching window, or NULL 384win_T * win_float_find (WinKind kind ) 385FUNC_ATTR_WARN_UNUSED_RESULT 386{ 387for (win_T * wp = lastwin ;wp && wp -> w_floating ;wp = wp -> w_prev ) { 388if (wp -> w_kind == kind ) { 389return wp ; 390 } 391 } 392return NULL ; 393} 394 395/// Select an alternative window to `win` (assumed floating) in tabpage `tp`. 396/// 397/// Useful for finding a window to switch to if `win` is the current window, but is then closed or 398/// moved to a different tabpage. 399/// 400/// @param tp `win`'s original tabpage, or NULL for current. 401win_T * win_float_find_altwin (const win_T * win ,const tabpage_T * tp ) 402FUNC_ATTR_NONNULL_ARG (1 ) 403{ 404win_T * wp = prevwin ; 405if (tp == NULL ) { 406return (win_valid (wp )&& wp != win && wp -> w_config .focusable 407&& !wp -> w_config .hide ) ?wp :firstwin ; 408 } 409 410assert (tp != curtab ); 411wp = tabpage_win_valid (tp ,tp -> tp_prevwin ) ?tp -> tp_prevwin :tp -> tp_firstwin ; 412return (wp != win && wp -> w_config .focusable && !wp -> w_config .hide ) ?wp :tp -> tp_firstwin ; 413} 414 415/// Abort win_float_special(): emit any error, tear down the half-built window, unblock autocmds. 416static inline win_T * win_float_special_fail (win_T * wp ,Error * err ) 417{ 418if (ERROR_SET (err )) { 419emsg (err -> msg ); 420api_clear_error (err ); 421 } 422if (wp ) { 423win_remove (wp ,NULL ); 424win_free (wp ,NULL ); 425 } 426unblock_autocmds (); 427return NULL ; 428} 429 430/// Snapshot a 'previewpopup' preview's anchor: the current cursor's screen cell. 431/// Call before the preview win is "entered" (then it becomes `curwin`). 432void win_float_anchor_preview (win_T * wp ) 433FUNC_ATTR_NONNULL_ALL 434{ 435wp -> w_wantline = curwin -> w_winrow + curwin -> w_wrow ; 436wp -> w_wantcol = curwin -> w_wincol + curwin -> w_wcol ; 437} 438 439/// Creates a special floatwin: a 'previewpopup' preview or a pum-info popup. 440/// 441/// @param enter Enter the new window. 442/// @param new_buf Give the window a new scratch buffer. 443/// @param kind kWinPreview or kWinInfo. 444win_T * win_float_special (bool enter ,bool new_buf ,WinKind kind ) 445{ 446WinConfig config = WIN_CONFIG_INIT ; 447config .col = curwin -> w_wcol ; 448config .row = curwin -> w_wrow ; 449config .relative = kFloatRelativeEditor ; 450config .focusable = false; 451config .mouse = true; 452config .anchor = 0 ;// NW 453config .noautocmd = true; 454config .hide = true; 455config .style = kWinStyleMinimal ; 456Error err = ERROR_INIT ; 457bool preview = kind == kWinPreview ; 458if (preview && !win_previewpopup_config (p_pvp ,& config )) { 459emsg (_ (e_invarg )); 460return NULL ; 461 } 462 463block_autocmds (); 464win_T * wp = win_new_float (NULL , false,config ,& err ); 465if (!wp ) { 466return win_float_special_fail (wp ,& err ); 467 } 468 469if (new_buf ) { 470Buffer b = nvim_create_buf (false, true,& err ); 471if (!b ) { 472return win_float_special_fail (wp ,& err ); 473 } 474buf_T * buf = find_buffer_by_handle (b ,& err ); 475if (!buf ) { 476return win_float_special_fail (wp ,& err ); 477 } 478buf -> b_p_bl = false;// unlist 479set_option_direct_for (kOptBufhidden ,STATIC_CSTR_AS_OBJ ("wipe" ),OPT_LOCAL ,0 , 480kOptScopeBuf ,buf ); 481win_set_buf (wp ,buf ,& err ); 482if (ERROR_SET (& err )) { 483return win_float_special_fail (wp ,& err ); 484 } 485 } 486unblock_autocmds (); 487wp -> w_p_diff = false; 488wp -> w_p_wrap = true;// 'wrap' is default on 489wp -> w_p_so = 0 ;// 'scrolloff' zero 490 491if (preview ) { 492wp -> w_p_pvw = true; 493RESET_BINDING (wp ); 494win_float_anchor_preview (wp ); 495 } 496wp -> w_kind = kind ; 497 498if (enter ) { 499win_enter (wp , false); 500 } 501return wp ; 502} 503 504/// Closes a specified floating window used for previews or popups. 505/// Searches for and closes a floating window based on given criteria. 506/// 507/// @param kind floating window preview kind. 508void win_float_close (WinKind kind ) 509{ 510win_T * wp = win_float_find (kind ); 511if (wp ) { 512win_close (wp , false, false); 513 } 514} 515 516/// Re-layout a 'previewpopup' window for its current content: title, size, and anchored position. 517/// No-op if `wp` is not a floating 'previewwindow'. 518void win_float_update_preview (win_T * wp ) 519FUNC_ATTR_NONNULL_ALL 520{ 521if (wp -> w_kind != kWinPreview || !wp -> w_floating || !wp -> w_buffer ) { 522return ; 523 } 524 525// Title is the previewed file's name, only known once it is loaded. 526if (wp -> w_config .border && wp -> w_buffer -> b_fname != NULL ) { 527char dirname [MAXPATHL ]; 528os_dirname (dirname ,MAXPATHL ); 529shorten_buf_fname (wp -> w_buffer ,dirname , false); 530 531clear_virttext (& wp -> w_config .title_chunks ); 532Error err = ERROR_INIT ; 533parse_bordertext (CSTR_AS_OBJ (wp -> w_buffer -> b_fname ),kBorderTextTitle ,& wp -> w_config ,& err ); 534api_clear_error (& err ); 535wp -> w_config .title_pos = kAlignCenter ; 536 } 537 538int border_h = win_border_height (wp ); 539int border_w = win_border_width (wp ); 540int row = MIN (MAX (wp -> w_wantline ,0 ),Rows - 1 ); 541int col = MIN (MAX (wp -> w_wantcol ,0 ),Columns - 1 ); 542 543OptKeyDict_pvp * v = opt_keyset (p_pvp ,kOptPreviewpopup ,NULL ); 544int want_w = HAS_KEY (v ,pvp ,width ) ? (int )v -> width :0 ; 545int want_h = HAS_KEY (v ,pvp ,height ) ? (int )v -> height :0 ; 546 547// If a dimension is not given in 'previewpopup', autosize to the content. 548linenr_T last = wp -> w_buffer -> b_ml .ml_line_count ; 549if (want_w == 0 ) { 550want_w = MAX (win_max_displaywidth (wp ,1 ,last ,Columns ),1 ); 551 } 552want_w = MIN (want_w ,Columns ); 553 554if (want_h == 0 ) { 555int saved_view_width = wp -> w_view_width ; 556wp -> w_view_width = want_w ; 557want_h = MAX (plines_m_win (wp ,1 ,last ,Rows ),1 ); 558wp -> w_view_width = saved_view_width ; 559 } 560want_h = MIN (want_h ,Rows ); 561 562int need_h = want_h + border_h ; 563int need_w = want_w + border_w ; 564int space_below = Rows - row - 1 - (int )p_ch ; 565int space_above = row - 1 ; 566int space_right = Columns - col - 1 ; 567int space_left = col ; 568// Prefer below/right; flip only if this side can't fit but the other can. 569bool below = space_below >=need_h || space_above < need_h ; 570bool right = space_right >=need_w || space_left < need_w ; 571int max_height = (below ?space_below :space_above )- border_h ; 572int max_width = (right ?space_right :space_left )- border_w ; 573 574wp -> w_config .height = MAX (1 ,MIN (want_h ,max_height )); 575wp -> w_config .width = MAX (1 ,MIN (want_w ,max_width )); 576wp -> w_config .anchor = (below ?0 :kFloatAnchorSouth ) | (right ?0 :kFloatAnchorEast ); 577wp -> w_config .row = row + (below ?1 :0 ); 578wp -> w_config .col = col + (right ?1 :0 ); 579wp -> w_config .hide = false; 580win_config_float (wp ,wp -> w_config ); 581} 582 583/// Applies a 'previewpopup' value ("height:N,width:N,border:style") to `config`. 584/// 585/// @return false on an invalid value; caller emits E474. 586bool win_previewpopup_config (const char * value ,WinConfig * config ) 587FUNC_ATTR_NONNULL_ALL 588{ 589OptKeyDict_pvp * v = opt_keyset (value ,kOptPreviewpopup ,NULL ); 590 591if ((HAS_KEY (v ,pvp ,height )&& v -> height < 1 )|| (HAS_KEY (v ,pvp ,width )&& v -> width < 1 )) { 592return false; 593 } 594config -> height = HAS_KEY (v ,pvp ,height ) ? (int )v -> height :0 ; 595config -> width = HAS_KEY (v ,pvp ,width ) ? (int )v -> width :0 ; 596 597// Use 'previewpopup' border if set, else fallback to 'winborder'. 598char * border = NULL ; 599if (HAS_KEY (v ,pvp ,border )) { 600border = v -> border ; 601 }else if (* p_winborder != NUL ) { 602border = p_winborder ; 603 } 604if (border != NULL ) { 605Error err = ERROR_INIT ; 606bool ok = parse_winborder (config ,border ,& err ); 607api_clear_error (& err ); 608if (!ok ) { 609return false; 610 } 611 } 612 613if (!config -> border ) { 614config -> title = false; 615 } 616return true; 617}