robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1/// @file popupmenu.c 2/// 3/// Popup menu (PUM) 4 5#include <assert.h> 6#include <stdbool.h> 7#include <stdint.h> 8#include <string.h> 9 10#include "nvim/api/buffer.h" 11#include "nvim/api/private/defs.h" 12#include "nvim/api/private/helpers.h" 13#include "nvim/api/vim.h" 14#include "nvim/api/win_config.h" 15#include "nvim/ascii_defs.h" 16#include "nvim/autocmd.h" 17#include "nvim/buffer.h" 18#include "nvim/buffer_defs.h" 19#include "nvim/charset.h" 20#include "nvim/cmdexpand.h" 21#include "nvim/decoration.h" 22#include "nvim/drawscreen.h" 23#include "nvim/errors.h" 24#include "nvim/eval/typval.h" 25#include "nvim/ex_cmds.h" 26#include "nvim/ex_cmds_defs.h" 27#include "nvim/fuzzy.h" 28#include "nvim/garray.h" 29#include "nvim/garray_defs.h" 30#include "nvim/gettext_defs.h" 31#include "nvim/globals.h" 32#include "nvim/grid.h" 33#include "nvim/grid_defs.h" 34#include "nvim/highlight.h" 35#include "nvim/highlight_defs.h" 36#include "nvim/highlight_group.h" 37#include "nvim/input.h" 38#include "nvim/insexpand.h" 39#include "nvim/keycodes.h" 40#include "nvim/mbyte.h" 41#include "nvim/memory.h" 42#include "nvim/memory_defs.h" 43#include "nvim/menu.h" 44#include "nvim/message.h" 45#include "nvim/mouse.h" 46#include "nvim/move.h" 47#include "nvim/option.h" 48#include "nvim/option_defs.h" 49#include "nvim/option_vars.h" 50#include "nvim/plines.h" 51#include "nvim/popupmenu.h" 52#include "nvim/pos_defs.h" 53#include "nvim/state_defs.h" 54#include "nvim/strings.h" 55#include "nvim/syntax.h" 56#include "nvim/types_defs.h" 57#include "nvim/ui.h" 58#include "nvim/ui_compositor.h" 59#include "nvim/ui_defs.h" 60#include "nvim/vim_defs.h" 61#include "nvim/window.h" 62#include "nvim/winfloat.h" 63 64static pumitem_T * pum_array = NULL ;// items of displayed pum 65static int pum_size ;// nr of items in "pum_array" 66static int pum_selected ;// index of selected item or -1 67static int pum_first = 0 ;// index of top item 68 69static int pum_height ;// nr of displayed pum items 70static int pum_width ;// width of displayed pum items 71static int pum_base_width ;// width of pum items base 72static int pum_kind_width ;// width of pum items kind column 73static int pum_extra_width ;// width of extra stuff 74static int pum_scrollbar ;// one when scrollbar present, else zero 75static bool pum_rl ;// true when popupmenu is drawn 'rightleft' 76 77static int pum_anchor_grid ;// grid where position is defined 78static int pum_row ;// top row of pum 79static int pum_col ;// left column of pum, right column if 'rightleft' 80static int pum_win_row_offset ;// The row offset needed to convert to window relative coordinates 81static int pum_win_col_offset ;// The column offset needed to convert to window relative coordinates 82static int pum_left_col ;// left column of pum, before padding or scrollbar 83static int pum_right_col ;// right column of pum, after padding or scrollbar 84static bool pum_above ;// pum is drawn above cursor line 85 86static bool pum_is_visible = false; 87static bool pum_is_drawn = false; 88static bool pum_external = false; 89static bool pum_invalid = false;// the screen was just cleared 90 91#include "popupmenu.c.generated.h" 92#define PUM_DEF_HEIGHT 10 93 94static void pum_compute_size (void ) 95{ 96// Compute the width of the widest match and the widest extra. 97pum_base_width = 0 ; 98pum_kind_width = 0 ; 99pum_extra_width = 0 ; 100for (int i = 0 ;i < pum_size ;i ++ ) { 101if (pum_array [i ].pum_text != NULL ) { 102int w = vim_strsize (pum_array [i ].pum_text ); 103if (pum_base_width < w ) { 104pum_base_width = w ; 105 } 106 } 107if (pum_array [i ].pum_kind != NULL ) { 108int w = vim_strsize (pum_array [i ].pum_kind )+ 1 ; 109if (pum_kind_width < w ) { 110pum_kind_width = w ; 111 } 112 } 113if (pum_array [i ].pum_extra != NULL ) { 114int w = vim_strsize (pum_array [i ].pum_extra )+ 1 ; 115if (pum_extra_width < w ) { 116pum_extra_width = w ; 117 } 118 } 119 } 120} 121 122/// Calculate vertical placement for popup menu. 123/// Sets pum_row and pum_height based on available space. 124static void pum_compute_vertical_placement (int size ,win_T * target_win ,int pum_win_row , 125int above_row ,int below_row ,int pum_border_size ) 126{ 127int context_lines ; 128 129// Figure out the size and position of the pum. 130pum_height = MIN (size ,PUM_DEF_HEIGHT ); 131if (p_ph > 0 && pum_height > p_ph ) { 132pum_height = (int )p_ph ; 133 } 134 135// Put the pum below "pum_win_row" if possible. 136// If there are few lines decide on where there is more room. 137if (pum_win_row + 2 + pum_border_size >=below_row - pum_height 138&& pum_win_row - above_row > (below_row - above_row ) /2 ) { 139// pum above "pum_win_row" 140pum_above = true; 141 142if ((State & MODE_CMDLINE )&& target_win == NULL ) { 143// For cmdline pum, no need for context lines unless target_win is set 144context_lines = 0 ; 145 }else { 146// Leave two lines of context if possible 147context_lines = MIN (2 ,target_win -> w_wrow - target_win -> w_cline_row ); 148 } 149 150if (pum_win_row >=size + context_lines ) { 151pum_row = pum_win_row - size - context_lines ; 152pum_height = size ; 153 }else { 154pum_row = 0 ; 155pum_height = pum_win_row - context_lines ; 156 } 157if (p_ph > 0 && pum_height > p_ph ) { 158pum_row += pum_height - (int )p_ph ; 159pum_height = (int )p_ph ; 160 } 161 162if (pum_border_size > 0 && pum_border_size + pum_row + pum_height >=pum_win_row ) { 163if (pum_row < 2 ) { 164pum_height -= pum_border_size ; 165 }else { 166pum_row -= pum_border_size ; 167 } 168 } 169 }else { 170// pum below "pum_win_row" 171pum_above = false; 172 173if ((State & MODE_CMDLINE )&& target_win == NULL ) { 174// for cmdline pum, no need for context lines unless target_win is set 175context_lines = 0 ; 176 }else { 177// Leave three lines of context if possible 178validate_cheight (target_win ); 179int cline_visible_offset = target_win -> w_cline_row + 180target_win -> w_cline_height - target_win -> w_wrow ; 181context_lines = MIN (3 ,cline_visible_offset ); 182 } 183 184pum_row = pum_win_row + context_lines ; 185pum_height = MIN (below_row - pum_row ,size ); 186if (p_ph > 0 && pum_height > p_ph ) { 187pum_height = (int )p_ph ; 188 } 189 190if (pum_row + pum_height + pum_border_size >=cmdline_row ) { 191pum_height -= pum_border_size ; 192 } 193 } 194 195// If there is a preview window above avoid drawing over it. 196if (above_row > 0 && pum_row < above_row && pum_height > above_row ) { 197pum_row = above_row ; 198pum_height = pum_win_row - above_row ; 199 } 200} 201 202/// Try to set "pum_width" so that it fits within available_width. 203/// Returns true if pum_width was successfully set, FALSE otherwise. 204static bool set_pum_width_aligned_with_cursor (int width ,int available_width ) 205{ 206bool end_padding = true; 207 208if (width < p_pw ) { 209width = (int )p_pw ; 210end_padding = false; 211 } 212if (p_pmw > 0 && width > p_pmw ) { 213width = (int )p_pmw ; 214end_padding = false; 215 } 216 217pum_width = width + (end_padding && width >=p_pw ?1 :0 ); 218return available_width >=pum_width ; 219} 220 221/// Calculate horizontal placement for popup menu. Sets pum_col and pum_width 222/// based on cursor position and available space. 223static void pum_compute_horizontal_placement (win_T * target_win ,int cursor_col ,int border_width ) 224{ 225int max_col = MAX (Columns ,target_win ? (target_win -> w_wincol + target_win -> w_view_width ) :0 ); 226int desired_width = pum_base_width + pum_kind_width + pum_extra_width ; 227int available_width ; 228 229if (pum_rl ) { 230available_width = cursor_col - pum_scrollbar + 1 - border_width ; 231 }else { 232available_width = max_col - cursor_col - pum_scrollbar - border_width ; 233 } 234 235// Align pum with "cursor_col" 236pum_col = cursor_col ; 237if (set_pum_width_aligned_with_cursor (desired_width ,available_width )) { 238return ; 239 } 240 241// Show the pum truncated, provided it is at least as wide as 'pum_width' 242if (available_width > p_pw ) { 243pum_width = available_width ; 244return ; 245 } 246 247// Truncated pum is no longer aligned with "cursor_col" 248if (pum_rl ) { 249available_width = max_col - pum_scrollbar - border_width ; 250 }else { 251available_width += cursor_col ; 252 } 253 254if (available_width > p_pw ) { 255pum_width = (int )p_pw + 1 ;// Truncate beyond 'pum_width' 256if (pum_rl ) { 257pum_col = pum_width + pum_scrollbar + border_width ; 258 }else { 259pum_col = max_col - pum_width - pum_scrollbar - border_width ; 260 } 261return ; 262 } 263 264// Not enough room anywhere, use what we have 265if (pum_rl ) { 266pum_col = max_col - 1 ; 267 }else { 268pum_col = 0 ; 269 } 270pum_width = max_col - pum_scrollbar - border_width ; 271} 272 273static inline int pum_border_width (void ) 274{ 275if (* p_pumborder == NUL || strequal (p_pumborder ,opt_winborder_values [7 ])) { 276return 0 ;// No border 277 } 278// Shadow (1) only has right+bottom, others (2) have full border 279return strequal (p_pumborder ,opt_winborder_values [3 ]) ?1 :2 ; 280} 281 282/// Show the popup menu with items "array[size]". 283/// "array" must remain valid until pum_undisplay() is called! 284/// When possible the leftmost character is aligned with cursor column. 285/// The menu appears above the screen line "row" or at "row" + "height" - 1. 286/// 287/// @param array 288/// @param size 289/// @param selected index of initially selected item, -1 if out of range 290/// @param array_changed if true, array contains different items since last call 291/// if false, a new item is selected, but the array 292/// is the same 293/// @param cmd_startcol only for cmdline mode: column of completed match 294void pum_display (pumitem_T * array ,int size ,int selected ,bool array_changed ,int cmd_startcol ) 295{ 296int redo_count = 0 ; 297int pum_win_row ; 298int cursor_col ; 299 300if (!pum_is_visible ) { 301// To keep the code simple, we only allow changing the 302// draw mode when the popup menu is not being displayed 303pum_external = ui_has (kUIPopupmenu ) 304|| ((State & MODE_CMDLINE )&& ui_has (kUIWildmenu )); 305 } 306 307pum_rl = (State & MODE_CMDLINE )== 0 && curwin -> w_p_rl ; 308 309int border_width = pum_border_width (); 310do { 311// Mark the pum as visible already here, 312// to avoid that must_redraw is set when 'cursorcolumn' is on. 313pum_is_visible = true; 314pum_is_drawn = true; 315validate_cursor_col (curwin ); 316int above_row = 0 ; 317int below_row = MAX (cmdline_row ,curwin -> w_winrow + curwin -> w_view_height ); 318if (State & MODE_CMDLINE ) { 319below_row = cmdline_row ; 320 } 321win_T * target_win = (State & MODE_CMDLINE ) ?cmdline_win :curwin ; 322pum_win_row_offset = 0 ; 323pum_win_col_offset = 0 ; 324 325// wildoptions=pum 326if (State & MODE_CMDLINE ) { 327pum_win_row = cmdline_win ?cmdline_win -> w_wrow :ui_has (kUICmdline ) ?0 :cmdline_row ; 328cursor_col = (cmdline_win ?cmdline_win -> w_config ._cmdline_offset :0 )+ cmd_startcol ; 329cursor_col %=cmdline_win ?cmdline_win -> w_view_width :Columns ; 330pum_anchor_grid = ui_has (kUICmdline ) ?-1 :DEFAULT_GRID_HANDLE ; 331 }else { 332// anchor position: the start of the completed word 333pum_win_row = curwin -> w_wrow ; 334int wcol = curwin -> w_wcol ; 335// w_wcol does not account for text concealed before the cursor; 336// shift by the offset win_line() recorded for the cursor line so the 337// menu lines up with the visible text. 338if (curwin -> w_p_cole > 0 && conceal_cursor_line (curwin )) { 339wcol -= curwin -> w_wcol_conceal_off ; 340if (wcol < 0 ) { 341wcol = 0 ; 342 } 343 } 344if (pum_rl ) { 345cursor_col = curwin -> w_view_width - wcol - 1 ; 346 }else { 347cursor_col = wcol ; 348 } 349 } 350 351if (target_win != NULL ) { 352// ext_popupmenu should always anchor to the default grid when multigrid is disabled 353pum_anchor_grid = target_win -> w_grid .target -> handle ; 354pum_win_row += target_win -> w_grid .row_offset ; 355cursor_col += target_win -> w_grid .col_offset ; 356if (target_win -> w_grid .target != & default_grid ) { 357pum_win_row += target_win -> w_winrow ; 358cursor_col += target_win -> w_wincol ; 359if (!ui_has (kUIMultigrid )) { 360pum_anchor_grid = DEFAULT_GRID_HANDLE ; 361 }else { 362pum_win_row_offset = target_win -> w_winrow ; 363pum_win_col_offset = target_win -> w_wincol ; 364 } 365 } 366 } 367 368if (pum_external ) { 369if (array_changed ) { 370Arena arena = ARENA_EMPTY ; 371Array arr = arena_array (& arena , (size_t )size ); 372for (int i = 0 ;i < size ;i ++ ) { 373Array item = arena_array (& arena ,4 ); 374ADD_C (item ,CSTR_AS_OBJ (array [i ].pum_text )); 375ADD_C (item ,CSTR_AS_OBJ (array [i ].pum_kind )); 376ADD_C (item ,CSTR_AS_OBJ (array [i ].pum_extra )); 377ADD_C (item ,CSTR_AS_OBJ (array [i ].pum_info )); 378ADD_C (arr ,ARRAY_OBJ (item )); 379 } 380ui_call_popupmenu_show (arr ,selected ,pum_win_row - pum_win_row_offset , 381cursor_col - pum_win_col_offset , 382pum_anchor_grid ); 383arena_mem_free (arena_finish (& arena )); 384 }else { 385ui_call_popupmenu_select (selected ); 386return ; 387 } 388 } 389 390win_T * pvwin = NULL ; 391FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 392if (wp -> w_p_pvw ) { 393pvwin = wp ; 394break ; 395 } 396 } 397 398if (pvwin != NULL ) { 399if (pvwin -> w_winrow < curwin -> w_winrow ) { 400above_row = pvwin -> w_winrow + pvwin -> w_height ; 401 }else if (pvwin -> w_winrow > curwin -> w_winrow + curwin -> w_height ) { 402below_row = pvwin -> w_winrow ; 403 } 404 } 405 406// Figure out the vertical size and position of the pum. 407pum_compute_vertical_placement (size ,target_win ,pum_win_row ,above_row ,below_row , 408border_width ); 409 410// don't display when we only have room for one line 411if (border_width == 0 && (pum_height < 1 || (pum_height == 1 && size > 1 ))) { 412return ; 413 } 414 415pum_array = array ; 416// Set "pum_size" before returning so that pum_set_event_info() gets the correct size. 417pum_size = size ; 418 419if (pum_external ) { 420return ; 421 } 422 423pum_compute_size (); 424 425// if there are more items than room we need a scrollbar 426pum_scrollbar = (pum_height < size ) ?1 :0 ; 427 428// Figure out the horizontal size and position of the pum. 429pum_compute_horizontal_placement (target_win ,cursor_col ,border_width ); 430 431// Set selected item and redraw. If the window size changed need to redo 432// the positioning. Limit this to two times, when there is not much 433// room the window size will keep changing. 434 }while (pum_set_selected (selected ,redo_count )&& ++ redo_count <=2 ); 435 436pum_grid .zindex = (State & MODE_CMDLINE ) ?kZIndexCmdlinePopupMenu :kZIndexPopupMenu ; 437pum_redraw (); 438} 439 440/// Computes attributes of text on the popup menu. 441/// Returns attributes for every cell, or NULL if all attributes are the same. 442static int * pum_compute_text_attrs (char * text ,hlf_T hlf ,int user_hlattr ) 443{ 444if (* text == NUL || (hlf != HLF_PSI && hlf != HLF_PNI ) 445|| (win_hl_attr (curwin ,HLF_PMSI )== win_hl_attr (curwin ,HLF_PSI ) 446&& win_hl_attr (curwin ,HLF_PMNI )== win_hl_attr (curwin ,HLF_PNI ))) { 447return NULL ; 448 } 449 450char * leader = (State & MODE_CMDLINE ) ?cmdline_compl_pattern () 451 :ins_compl_leader (); 452if (leader == NULL || * leader == NUL ) { 453return NULL ; 454 } 455 456int * attrs = xmalloc (sizeof (int )* (size_t )vim_strsize (text )); 457bool in_fuzzy = (State & MODE_CMDLINE ) ?cmdline_compl_is_fuzzy () 458 : (get_cot_flags ()& kOptCotFlagFuzzy )!= 0 ; 459size_t leader_len = strlen (leader ); 460 461garray_T * ga = NULL ; 462int matched_len = -1 ; 463 464if (in_fuzzy ) { 465ga = fuzzy_match_str_with_pos (text ,leader ); 466if (!ga ) { 467xfree (attrs ); 468return NULL ; 469 } 470 } 471 472const char * ptr = text ; 473int cell_idx = 0 ; 474uint32_t char_pos = 0 ; 475bool is_select = hlf == HLF_PSI ; 476 477while (* ptr != NUL ) { 478int new_attr = win_hl_attr (curwin , (int )hlf ); 479 480if (ga != NULL ) { 481// Handle fuzzy matching 482for (int i = 0 ;i < ga -> ga_len ;i ++ ) { 483if (char_pos == ((uint32_t * )ga -> ga_data )[i ]) { 484new_attr = win_hl_attr (curwin ,is_select ?HLF_PMSI :HLF_PMNI ); 485new_attr = hl_combine_attr (win_hl_attr (curwin ,HLF_PMNI ),new_attr ); 486new_attr = hl_combine_attr (win_hl_attr (curwin , (int )hlf ),new_attr ); 487break ; 488 } 489 } 490 }else { 491if (matched_len < 0 && mb_strnicmp (ptr ,leader ,leader_len )== 0 ) { 492matched_len = (int )leader_len ; 493 } 494if (matched_len > 0 ) { 495new_attr = win_hl_attr (curwin ,is_select ?HLF_PMSI :HLF_PMNI ); 496new_attr = hl_combine_attr (win_hl_attr (curwin ,HLF_PMNI ),new_attr ); 497new_attr = hl_combine_attr (win_hl_attr (curwin , (int )hlf ),new_attr ); 498matched_len -- ; 499 } 500 } 501 502new_attr = hl_combine_attr (win_hl_attr (curwin ,HLF_PNI ),new_attr ); 503 504if (user_hlattr > 0 ) { 505new_attr = hl_combine_attr (new_attr ,user_hlattr ); 506 } 507 508int char_cells = utf_ptr2cells (ptr ); 509for (int i = 0 ;i < char_cells ;i ++ ) { 510attrs [cell_idx + i ]= new_attr ; 511 } 512cell_idx += char_cells ; 513 514MB_PTR_ADV (ptr ); 515char_pos ++ ; 516 } 517 518if (ga != NULL ) { 519ga_clear (ga ); 520xfree (ga ); 521 } 522return attrs ; 523} 524 525/// Displays text on the popup menu with specific attributes. 526static void pum_grid_puts_with_attrs (int col ,int cells ,const char * text ,int textlen , 527const int * attrs ) 528{ 529const int col_start = col ; 530const char * ptr = text ; 531 532// Render text with proper attributes 533while (* ptr != NUL && (textlen < 0 || ptr < text + textlen )) { 534int char_len = utfc_ptr2len (ptr ); 535int attr = attrs [pum_rl ? (col_start + cells - col - 1 ) : (col - col_start )]; 536grid_line_puts (col ,ptr ,char_len ,attr ); 537col += utf_ptr2cells (ptr ); 538ptr += char_len ; 539 } 540} 541 542static inline void pum_align_order (int * order ) 543{ 544bool is_default = cia_flags == 0 ; 545order [0 ]= is_default ?CPT_ABBR :cia_flags /100 ; 546order [1 ]= is_default ?CPT_KIND : (cia_flags /10 ) %10 ; 547order [2 ]= is_default ?CPT_MENU :cia_flags %10 ; 548} 549 550static inline char * pum_get_item (int index ,int type ) 551{ 552switch (type ) { 553case CPT_ABBR : 554return pum_array [index ].pum_text ; 555case CPT_KIND : 556return pum_array [index ].pum_kind ; 557case CPT_MENU : 558return pum_array [index ].pum_extra ; 559 } 560return NULL ; 561} 562 563static inline int pum_user_attr_combine (int idx ,int type ,int attr ) 564{ 565int user_attr []= { 566pum_array [idx ].pum_user_abbr_hlattr , 567pum_array [idx ].pum_user_kind_hlattr , 568 }; 569return user_attr [type ]> 0 ?hl_combine_attr (attr ,user_attr [type ]) :attr ; 570} 571 572/// Redraw the popup menu, using "pum_first" and "pum_selected". 573void pum_redraw (void ) 574{ 575int row = 0 ; 576int attr_scroll = win_hl_attr (curwin ,HLF_PSB ); 577int attr_thumb = win_hl_attr (curwin ,HLF_PST ); 578char * p = NULL ; 579int thumb_pos = 0 ; 580int thumb_height = 1 ; 581int n ; 582const schar_T fcs_trunc = pum_rl ?curwin -> w_p_fcs_chars .truncrl 583 :curwin -> w_p_fcs_chars .trunc ; 584 585// "word" "kind" "extra text" 586const hlf_T hlfsNorm [3 ]= {HLF_PNI ,HLF_PNK ,HLF_PNX }; 587const hlf_T hlfsSel [3 ]= {HLF_PSI ,HLF_PSK ,HLF_PSX }; 588 589int grid_width = pum_width ; 590int col_off = 0 ; 591bool extra_space = false; 592if (pum_rl ) { 593col_off = pum_width - 1 ; 594assert (!(State & MODE_CMDLINE )); 595int win_end_col = W_ENDCOL (curwin ); 596if (pum_col < win_end_col - 1 ) { 597grid_width += 1 ; 598extra_space = true; 599 } 600 }else { 601int min_col = 0 ; 602if (pum_col > min_col ) { 603grid_width += 1 ; 604col_off = 1 ; 605extra_space = true; 606 } 607 } 608WinConfig fconfig = WIN_CONFIG_INIT ; 609int border_width = pum_border_width (); 610int border_attr = 0 ; 611schar_T border_char = 0 ; 612schar_T fill_char = schar_from_ascii (' ' ); 613bool has_border = border_width > 0 ; 614// setup popup menu border if 'pumborder' option is set 615if (border_width > 0 ) { 616Error err = ERROR_INIT ; 617if (!parse_winborder (& fconfig ,p_pumborder ,& err )) { 618if (ERROR_SET (& err )) { 619emsg (err .msg ); 620 } 621api_clear_error (& err ); 622return ; 623 } 624 625// Shadow style: only adds border on right and bottom edges 626if (strequal (p_pumborder ,opt_winborder_values [3 ])) { 627fconfig .shadow = true; 628int blend = SYN_GROUP_STATIC ("PmenuShadow" ); 629int through = SYN_GROUP_STATIC ("PmenuShadowThrough" ); 630fconfig .border_hl_ids [2 ]= through ; 631fconfig .border_hl_ids [3 ]= blend ; 632fconfig .border_hl_ids [4 ]= blend ; 633fconfig .border_hl_ids [5 ]= blend ; 634fconfig .border_hl_ids [6 ]= through ; 635 } 636 637// convert border highlight IDs to attributes, use PmenuBorder as default 638for (int i = 0 ;i < 8 ;i ++ ) { 639int attr = hl_attr_active [HLF_PBR ]; 640if (fconfig .border_hl_ids [i ]) { 641attr = hl_get_ui_attr (-1 ,HLF_PBR ,fconfig .border_hl_ids [i ], false); 642 } 643fconfig .border_attr [i ]= attr ; 644 } 645api_clear_error (& err ); 646if (pum_scrollbar ) { 647border_char = schar_from_str (fconfig .border_chars [3 ]); 648border_attr = fconfig .border_attr [3 ]; 649 } 650 } 651 652if (pum_scrollbar > 0 && (!fconfig .border || fconfig .shadow )) { 653grid_width ++ ; 654if (pum_rl ) { 655col_off ++ ; 656 } 657 } 658pum_grid .blending = p_pb > 0 || fconfig .shadow ; 659grid_assign_handle (& pum_grid ); 660 661pum_left_col = pum_col - col_off ; 662pum_right_col = pum_left_col + grid_width ; 663bool moved = ui_comp_put_grid (& pum_grid ,pum_row ,pum_left_col , 664pum_height + border_width ,grid_width + border_width , false, 665 true); 666bool invalid_grid = moved || pum_invalid ; 667pum_invalid = false; 668must_redraw_pum = false; 669 670if (!pum_grid .chars || pum_grid .rows != pum_height + border_width 671|| pum_grid .cols != grid_width + border_width ) { 672grid_alloc (& pum_grid ,pum_height + border_width ,grid_width + border_width , 673 !invalid_grid , false); 674ui_call_grid_resize (pum_grid .handle ,pum_grid .cols ,pum_grid .rows ); 675 }else if (invalid_grid ) { 676grid_invalidate (& pum_grid ); 677 } 678if (ui_has (kUIMultigrid )) { 679pum_pos_ui_flush (true); 680 } 681 682int scroll_range = pum_size - pum_height ; 683if (fconfig .border ) { 684grid_draw_border (& pum_grid ,& fconfig ,NULL ,0 ,NULL ,0 ); 685if (!fconfig .shadow ) { 686row ++ ; 687col_off ++ ; 688 } 689 } 690 691// Never display more than we have 692pum_first = MIN (pum_first ,scroll_range ); 693 694if (pum_scrollbar ) { 695thumb_height = pum_height * pum_height /pum_size ; 696if (thumb_height == 0 ) { 697thumb_height = 1 ; 698 } 699thumb_pos = (pum_first * (pum_height - thumb_height )+ scroll_range /2 ) /scroll_range ; 700 } 701 702for (int i = 0 ;i < pum_height ;i ++ ) { 703int idx = i + pum_first ; 704const bool selected = idx == pum_selected ; 705const hlf_T * const hlfs = selected ?hlfsSel :hlfsNorm ; 706const int trunc_attr = win_hl_attr (curwin ,selected ?HLF_PSI :HLF_PNI ); 707hlf_T hlf = hlfs [0 ];// start with "word" highlight 708int attr = win_hl_attr (curwin , (int )hlf ); 709attr = hl_combine_attr (win_hl_attr (curwin ,HLF_PNI ),attr ); 710 711screengrid_line_start (& pum_grid ,row ,0 ); 712 713// prepend a space if there is room 714if (extra_space ) { 715if (pum_rl ) { 716grid_line_puts (col_off + 1 ," " ,1 ,attr ); 717 }else { 718grid_line_puts (col_off - 1 ," " ,1 ,attr ); 719 } 720 } 721 722// Display each entry, use two spaces for a Tab. 723// Do this 3 times and order from p_cia 724int grid_col = col_off ; 725int totwidth = 0 ; 726bool need_fcs_trunc = false; 727int order [3 ]; 728int items_width_array [3 ]= {pum_base_width ,pum_kind_width ,pum_extra_width }; 729pum_align_order (order ); 730int basic_width = items_width_array [order [0 ]];// first item width 731bool last_isabbr = order [2 ]== CPT_ABBR ; 732int orig_attr = -1 ; 733 734for (int j = 0 ;j < 3 ;j ++ ) { 735int item_type = order [j ]; 736hlf = hlfs [item_type ]; 737attr = win_hl_attr (curwin , (int )hlf ); 738attr = hl_combine_attr (win_hl_attr (curwin ,HLF_PNI ),attr ); 739orig_attr = attr ; 740if (item_type < 2 ) {// try combine attr with user custom 741attr = pum_user_attr_combine (idx ,item_type ,attr ); 742 } 743int width = 0 ; 744char * s = NULL ; 745p = pum_get_item (idx ,item_type ); 746 747const bool next_isempty = j + 1 >=3 || pum_get_item (idx ,order [j + 1 ])== NULL ; 748 749if (p != NULL ) { 750for (;;MB_PTR_ADV (p )) { 751if (s == NULL ) { 752s = p ; 753 } 754int w = ptr2cells (p ); 755if (* p != NUL && * p != TAB && totwidth + w <=pum_width ) { 756width += w ; 757continue ; 758 } 759const int width_limit = pum_width ; 760 761// Display the text that fits or comes before a Tab. 762// First convert it to printable characters. 763char saved = * p ; 764 765if (saved != NUL ) { 766* p = NUL ; 767 } 768char * st = transstr (s , true); 769if (saved != NUL ) { 770* p = saved ; 771 } 772 773int * attrs = NULL ; 774if (item_type == CPT_ABBR ) { 775attrs = pum_compute_text_attrs (st ,hlf , 776pum_array [idx ].pum_user_abbr_hlattr ); 777 } 778 779if (pum_rl ) { 780char * rt = reverse_text (st ); 781char * rt_start = rt ; 782int cells = (int )mb_string2cells (rt ); 783int pad = next_isempty ?0 :2 ; 784if (width_limit - totwidth < cells + pad ) { 785need_fcs_trunc = true; 786 } 787 788// only draw the text that fits 789if (grid_col - cells < col_off - width_limit ) { 790do { 791cells -= utf_ptr2cells (rt ); 792MB_PTR_ADV (rt ); 793 }while (grid_col - cells < col_off - width_limit ); 794 795if (grid_col - cells > col_off - width_limit ) { 796// Most left character requires 2-cells but only 1 cell is available on 797// screen. Put a '<' on the left of the pum item. 798* (-- rt )= '<' ; 799cells ++ ; 800 } 801 } 802 803if (attrs == NULL ) { 804grid_line_puts (grid_col - cells + 1 ,rt ,-1 ,attr ); 805 }else { 806pum_grid_puts_with_attrs (grid_col - cells + 1 ,cells ,rt ,-1 ,attrs ); 807 } 808 809xfree (rt_start ); 810xfree (st ); 811grid_col -= width ; 812 }else { 813int cells = (int )mb_string2cells (st ); 814int pad = next_isempty ?0 :2 ; 815if (width_limit - totwidth < cells + pad ) { 816need_fcs_trunc = true; 817 } 818if (need_fcs_trunc ) { 819int available_cells = width_limit - totwidth ; 820// Find the truncation point by counting display cells 821char * p_end = st ; 822int displayed = 0 ; 823while (* p_end != NUL ) { 824int char_cells = utf_ptr2cells (p_end ); 825if (displayed + char_cells > available_cells ) { 826break ; 827 } 828displayed += char_cells ; 829MB_PTR_ADV (p_end ); 830 } 831* p_end = NUL ; 832cells = displayed ; 833width = displayed ; 834 } 835 836if (attrs == NULL ) { 837grid_line_puts (grid_col ,st ,-1 ,attr ); 838 }else { 839pum_grid_puts_with_attrs (grid_col ,cells ,st ,-1 ,attrs ); 840 } 841 842xfree (st ); 843grid_col += width ; 844 } 845 846if (attrs != NULL ) { 847XFREE_CLEAR (attrs ); 848 } 849 850if (* p != TAB ) { 851break ; 852 } 853 854// Display two spaces for a Tab. 855if (pum_rl ) { 856grid_line_puts (grid_col - 1 ," " ,2 ,attr ); 857grid_col -= 2 ; 858 }else { 859grid_line_puts (grid_col ," " ,2 ,attr ); 860grid_col += 2 ; 861 } 862totwidth += 2 ; 863s = NULL ;// start text at next char 864width = 0 ; 865 } 866 } 867 868if (j > 0 ) { 869n = items_width_array [order [1 ]]+ (last_isabbr ?0 :1 ); 870 }else { 871n = order [j ]== CPT_ABBR ?1 :0 ; 872 } 873 874// Stop when there is nothing more to display. 875if ((j == 2 ) 876|| (next_isempty && (j == 1 || (j == 0 && pum_get_item (idx ,order [j + 2 ])== NULL ))) 877|| (basic_width + n >=pum_width )) { 878break ; 879 } 880 881if (pum_rl ) { 882grid_line_fill (col_off - basic_width - n + 1 ,grid_col + 1 , 883schar_from_ascii (' ' ),orig_attr ); 884grid_col = col_off - basic_width - n ; 885 }else { 886grid_line_fill (grid_col ,col_off + basic_width + n , 887schar_from_ascii (' ' ),orig_attr ); 888grid_col = col_off + basic_width + n ; 889 } 890totwidth = basic_width + n ; 891 } 892 893if (pum_rl ) { 894const int lcol = col_off - pum_width + 1 ; 895grid_line_fill (lcol ,grid_col + 1 ,schar_from_ascii (' ' ),orig_attr ); 896if (need_fcs_trunc ) { 897linebuf_char [lcol ]= fcs_trunc != NUL ?fcs_trunc :schar_from_ascii ('<' ); 898linebuf_attr [lcol ]= trunc_attr ; 899if (pum_width > 1 && linebuf_char [lcol + 1 ]== NUL ) { 900linebuf_char [lcol + 1 ]= schar_from_ascii (' ' ); 901 } 902 } 903 }else { 904const int rcol = col_off + pum_width ; 905grid_line_fill (grid_col ,rcol ,schar_from_ascii (' ' ),orig_attr ); 906if (need_fcs_trunc ) { 907if (pum_width > 1 && linebuf_char [rcol - 1 ]== NUL ) { 908linebuf_char [rcol - 2 ]= schar_from_ascii (' ' ); 909 } 910linebuf_char [rcol - 1 ]= fcs_trunc != NUL ?fcs_trunc :schar_from_ascii ('>' ); 911linebuf_attr [rcol - 1 ]= trunc_attr ; 912 } 913 } 914 915if (pum_scrollbar > 0 ) { 916bool thumb = i >=thumb_pos && i < thumb_pos + thumb_height ; 917int scrollbar_col = col_off + (pum_rl ?- pum_width :pum_width ); 918bool use_border_style = has_border && !fconfig .shadow ; 919grid_line_put_schar (scrollbar_col , 920 (use_border_style && !thumb ) ?border_char :fill_char , 921thumb ?attr_thumb : (use_border_style ?border_attr :attr_scroll )); 922 } 923grid_line_flush (); 924row ++ ; 925 } 926} 927 928/// Set the informational text in the preview buffer when the completion 929/// item does not include a dedicated preview or popup window. 930/// 931/// @param[in] win Window containing buffer where the text will be set. 932/// @param[in] info Informational text to display in the preview buffer. 933/// @param[in] lnum Where to start the text. Incremented for each added line. 934/// @param[out] max_width Maximum width of the displayed text. 935static void pum_preview_set_text (win_T * win ,char * info ,linenr_T * lnum ,int * max_width ) 936{ 937Error err = ERROR_INIT ; 938Arena arena = ARENA_EMPTY ; 939Array replacement = ARRAY_DICT_INIT ; 940buf_T * buf = win -> w_buffer ; 941buf -> b_p_ma = true; 942 943// Iterate through the string line by line by temporarily replacing newlines with NUL 944for (char * curr = info ,* next ;curr ;curr = next ?next + 1 :NULL ) { 945if ((next = strchr (curr ,'\n' ))) { 946* next = NUL ;// Temporarily replace the newline with a string terminator 947 } 948// Only skip if this is an empty line AND it's the last line 949if (* curr == NUL && !next ) { 950break ; 951 } 952// Temporarily disable 'wrap' to avoid 'showbreak/linebreak' 953// inflating the result when the window is narrow. 954bool save_wrap = win -> w_p_wrap ; 955win -> w_p_wrap = false; 956int line_width = win_linetabsize (win ,0 ,curr ,MAXCOL ); 957win -> w_p_wrap = save_wrap ; 958* max_width = MAX (* max_width ,line_width ); 959ADD (replacement ,STRING_OBJ (cstr_to_string (curr ))); 960 (* lnum )++ ; 961 962if (next ) { 963* next = '\n' ; 964 } 965 } 966 967int original_textlock = textlock ; 968textlock = 0 ; 969nvim_buf_set_lines (0 ,buf -> handle ,0 ,-1 , false,replacement ,& arena ,& err ); 970textlock = original_textlock ; 971if (ERROR_SET (& err )) { 972emsg (err .msg ); 973api_clear_error (& err ); 974 } 975arena_mem_free (arena_finish (& arena )); 976api_free_array (replacement ); 977buf -> b_p_ma = false; 978} 979 980/// adjust floating info preview window position 981static bool pum_adjust_info_position (win_T * wp ,int width ) 982{ 983int border_width = pum_border_width (); 984int col = pum_col + pum_width + 1 + MAX (border_width ,pum_scrollbar ); 985// TODO(glepnir): support config align border by using completepopup 986// align menu 987int right_extra = Columns - col ; 988int left_extra = pum_col - 2 ; 989 990int max_extra = MAX (right_extra ,left_extra ); 991// Close info window if there's insufficient space 992// TODO(glepnir): Replace the hardcoded value (10) with values from the 'completepopup' width/height options. 993if (max_extra < 10 ) { 994wp -> w_config .hide = true; 995return false; 996 } 997 998if (right_extra > width ) {// place in right 999wp -> w_config .width = width ; 1000wp -> w_config .col = col - 1 ; 1001 }else if (left_extra > width ) {// place in left 1002wp -> w_config .width = width ; 1003wp -> w_config .col = pum_col - wp -> w_config .width - 1 ; 1004 }else {// either width is enough just use the biggest one. 1005const bool place_in_right = right_extra > left_extra ; 1006wp -> w_config .width = max_extra ; 1007wp -> w_config .col = place_in_right ?col - 1 :pum_col - wp -> w_config .width - 1 ; 1008 } 1009wp -> w_config .anchor = 0 ;// NW: align top of info window with top of pum 1010linenr_T count = wp -> w_buffer -> b_ml .ml_line_count ; 1011wp -> w_view_width = wp -> w_config .width ; 1012wp -> w_config .height = plines_m_win (wp ,wp -> w_topline ,count ,Rows ); 1013wp -> w_config .row = pum_row ; 1014wp -> w_config .hide = false; 1015win_config_float (wp ,wp -> w_config ); 1016return true; 1017} 1018 1019/// Used for nvim__complete_set 1020/// 1021/// @param selected the selected compl item. 1022/// @param info Info string. 1023/// @return a win_T pointer. 1024win_T * pum_set_info (int selected ,char * info ) 1025{ 1026if (!pum_is_visible || !compl_match_curr_select (selected )) { 1027return NULL ; 1028 } 1029block_autocmds (); 1030RedrawingDisabled ++ ; 1031no_u_sync ++ ; 1032win_T * wp = win_float_find (kWinInfo ); 1033if (wp == NULL ) { 1034wp = win_float_special (false, true,kWinInfo ); 1035if (!wp ) { 1036return NULL ; 1037 } 1038wp -> w_topline = 1 ; 1039wp -> w_p_wfb = true; 1040 } 1041linenr_T lnum = 0 ; 1042int max_info_width = 0 ; 1043pum_preview_set_text (wp ,info ,& lnum ,& max_info_width ); 1044no_u_sync -- ; 1045RedrawingDisabled -- ; 1046redraw_later (wp ,UPD_NOT_VALID ); 1047 1048if (!pum_adjust_info_position (wp ,max_info_width )) { 1049wp = NULL ; 1050 } 1051unblock_autocmds (); 1052return wp ; 1053} 1054 1055/// Set the index of the currently selected item. The menu will scroll when 1056/// necessary. When "n" is out of range don't scroll. 1057/// This may be repeated when the preview window is used: 1058/// "repeat" == 0: open preview window normally 1059/// "repeat" == 1: open preview window but don't set the size 1060/// "repeat" == 2: don't open preview window 1061/// 1062/// @param n 1063/// @param repeat 1064/// 1065/// @returns true when the window was resized and the location of the popup 1066/// menu must be recomputed. 1067static bool pum_set_selected (int n ,int repeat ) 1068{ 1069bool resized = false; 1070int context = pum_height /2 ; 1071int prev_selected = pum_selected ; 1072 1073pum_selected = n ; 1074int scroll_offset = pum_selected - pum_height ; 1075unsigned cur_cot_flags = get_cot_flags (); 1076bool use_float = (cur_cot_flags & kOptCotFlagPopup )!= 0 ; 1077 1078// Close the floating preview window if 'selected' is -1, indicating a return to the original 1079// state. It is also closed when the selected item has no corresponding info item. 1080if (use_float && (pum_selected < 0 || pum_array [pum_selected ].pum_info == NULL )) { 1081win_T * wp = win_float_find (kWinInfo ); 1082if (wp ) { 1083wp -> w_config .hide = true; 1084win_config_float (wp ,wp -> w_config ); 1085 } 1086 } 1087 1088if ((pum_selected >=0 )&& (pum_selected < pum_size )) { 1089if (pum_first > pum_selected - 4 ) { 1090// scroll down; when we did a jump it's probably a PageUp then 1091// scroll a whole page 1092if (pum_first > pum_selected - 2 ) { 1093pum_first -= pum_height - 2 ; 1094if (pum_first < 0 ) { 1095pum_first = 0 ; 1096 }else if (pum_first > pum_selected ) { 1097pum_first = pum_selected ; 1098 } 1099 }else { 1100pum_first = pum_selected ; 1101 } 1102 }else if (pum_first < scroll_offset + 5 ) { 1103// scroll up; when we did a jump it's probably a PageDown then 1104// scroll a whole page 1105if (pum_first < scroll_offset + 3 ) { 1106pum_first = MAX (pum_first + pum_height - 2 ,scroll_offset + 1 ); 1107 }else { 1108pum_first = scroll_offset + 1 ; 1109 } 1110 } 1111 1112// Give a few lines of context when possible. 1113context = MIN (context ,3 ); 1114 1115if (pum_height > 2 ) { 1116if (pum_first > pum_selected - context ) { 1117pum_first = MAX (pum_selected - context ,0 );// scroll down 1118 }else if (pum_first < pum_selected + context - pum_height + 1 ) { 1119pum_first = pum_selected + context - pum_height + 1 ;// up 1120 } 1121 } 1122// adjust for the number of lines displayed 1123pum_first = MIN (pum_first ,pum_size - pum_height ); 1124 1125// Show extra info in the preview window if there is something and 1126// 'completeopt' contains "preview" or "popup". 1127// Skip this when tried twice already. 1128// Skip this also when there is not much room. 1129// Skip this for command-window when 'completeopt' contains "preview". 1130// NOTE: Be very careful not to sync undo! 1131if ((pum_array [pum_selected ].pum_info != NULL ) 1132&& (Rows > 10 ) 1133&& (repeat <=1 ) 1134&& (cur_cot_flags & (kOptCotFlagPreview |kOptCotFlagPopup )) 1135&& !((cur_cot_flags & kOptCotFlagPreview )&& cmdwin_buf != NULL )) { 1136win_T * curwin_save = curwin ; 1137tabpage_T * curtab_save = curtab ; 1138 1139if (use_float ) { 1140block_autocmds (); 1141 } 1142 1143// Open a preview window. 3 lines by default. Prefer 1144// 'previewheight' if set and smaller. 1145g_do_tagpreview = 3 ; 1146 1147if ((p_pvh > 0 )&& (p_pvh < g_do_tagpreview )) { 1148g_do_tagpreview = (int )p_pvh ; 1149 } 1150RedrawingDisabled ++ ; 1151// Prevent undo sync here, if an autocommand syncs undo weird 1152// things can happen to the undo tree. 1153no_u_sync ++ ; 1154 1155if (!use_float ) { 1156resized = prepare_tagpreview (false, false); 1157 }else { 1158win_T * wp = win_float_find (kWinInfo ); 1159if (wp ) { 1160win_enter (wp , false); 1161 }else { 1162wp = win_float_special (true, true,kWinInfo ); 1163if (wp ) { 1164resized = true; 1165 } 1166 } 1167 } 1168 1169no_u_sync -- ; 1170RedrawingDisabled -- ; 1171g_do_tagpreview = 0 ; 1172 1173if (curwin -> w_p_pvw || curwin -> w_kind == kWinInfo ) { 1174int res = OK ; 1175if (!resized 1176&& (curbuf -> b_nwindows == 1 ) 1177&& (curbuf -> b_fname == NULL ) 1178&& bt_nofile (curbuf ) 1179&& (curbuf -> b_p_bh [0 ]== 'w' )) { 1180// Already a "wipeout" buffer, make it empty. 1181buf_clear (); 1182 }else { 1183// Don't want to sync undo in the current buffer. 1184no_u_sync ++ ; 1185res = do_ecmd (0 ,NULL ,NULL ,NULL ,ECMD_ONE ,0 ,NULL ); 1186no_u_sync -- ; 1187 1188if (res == OK ) { 1189// Edit a new, empty buffer. Set options for a "wipeout" 1190// buffer. 1191set_option_value_give_err (kOptSwapfile ,BOOLEAN_OBJ (false),OPT_LOCAL ); 1192set_option_value_give_err (kOptBuflisted ,BOOLEAN_OBJ (false),OPT_LOCAL ); 1193set_option_value_give_err (kOptBuftype ,STATIC_CSTR_AS_OBJ ("nofile" ),OPT_LOCAL ); 1194set_option_value_give_err (kOptBufhidden ,STATIC_CSTR_AS_OBJ ("wipe" ),OPT_LOCAL ); 1195set_option_value_give_err (kOptDiff ,BOOLEAN_OBJ (false),OPT_LOCAL ); 1196 } 1197 } 1198 1199if (res == OK ) { 1200linenr_T lnum = 0 ; 1201int max_info_width = 0 ; 1202pum_preview_set_text (curwin ,pum_array [pum_selected ].pum_info ,& lnum ,& max_info_width ); 1203// Increase the height of the preview window to show the 1204// text, but no more than 'previewheight' lines. 1205if (repeat == 0 && !use_float ) { 1206lnum = MIN (lnum , (linenr_T )p_pvh ); 1207 1208if (curwin -> w_height < lnum ) { 1209win_setheight ((int )lnum ); 1210resized = true; 1211 } 1212 } 1213 1214curbuf -> b_changed = false; 1215curbuf -> b_p_ma = false; 1216if (pum_selected != prev_selected ) { 1217curwin -> w_topline = 1 ; 1218 }else if (curwin -> w_topline > curbuf -> b_ml .ml_line_count ) { 1219curwin -> w_topline = curbuf -> b_ml .ml_line_count ; 1220 } 1221curwin -> w_cursor .lnum = 1 ; 1222curwin -> w_cursor .col = 0 ; 1223 1224if (use_float ) { 1225// adjust floating window by actually height and max info text width 1226if (!pum_adjust_info_position (curwin ,max_info_width )&& win_valid (curwin_save )) { 1227win_enter (curwin_save , false); 1228 } 1229 } 1230 1231if ((curwin != curwin_save && win_valid (curwin_save )) 1232|| (curtab != curtab_save && valid_tabpage (curtab_save ))) { 1233if (curtab != curtab_save && valid_tabpage (curtab_save )) { 1234goto_tabpage_tp (curtab_save , false, false); 1235 } 1236 1237// When the first completion is done and the preview 1238// window is not resized, skip the preview window's 1239// status line redrawing. 1240if (ins_compl_active ()&& !resized ) { 1241curwin -> w_redr_status = false; 1242 } 1243 1244// Return cursor to where we were 1245validate_cursor (curwin ); 1246redraw_later (curwin ,UPD_SOME_VALID ); 1247 1248// When the preview window was resized we need to 1249// update the view on the buffer. Only go back to 1250// the window when needed, otherwise it will always be 1251// redrawn. 1252if (resized && win_valid (curwin_save )) { 1253no_u_sync ++ ; 1254win_enter (curwin_save , true); 1255no_u_sync -- ; 1256update_topline (curwin ); 1257 } 1258 1259const bool save_pum_is_drawn = pum_is_drawn ; 1260 1261// Update the screen before drawing the popup menu. 1262// Enable updating the status lines. 1263// TODO(bfredl): can simplify, get rid of the flag munging? 1264// or at least eliminate extra redraw before win_enter()? 1265pum_is_visible = false; 1266pum_is_drawn = false; 1267update_screen (); 1268pum_is_visible = true; 1269pum_is_drawn = save_pum_is_drawn ; 1270 1271if (!resized && win_valid (curwin_save )) { 1272no_u_sync ++ ; 1273win_enter (curwin_save , true); 1274no_u_sync -- ; 1275 } 1276 1277// May need to update the screen again when there are 1278// autocommands involved. 1279pum_is_visible = false; 1280pum_is_drawn = false; 1281update_screen (); 1282pum_is_visible = true; 1283pum_is_drawn = save_pum_is_drawn ; 1284 } 1285 } 1286 } 1287 1288if (use_float ) { 1289unblock_autocmds (); 1290 } 1291 } 1292 } 1293 1294return resized ; 1295} 1296 1297/// Undisplay the popup menu (later). 1298void pum_undisplay (bool immediate ) 1299{ 1300pum_is_visible = false; 1301pum_array = NULL ; 1302must_redraw_pum = false; 1303 1304if (immediate ) { 1305pum_check_clear (); 1306 } 1307} 1308 1309void pum_check_clear (void ) 1310{ 1311if (!pum_is_visible && pum_is_drawn ) { 1312if (pum_external ) { 1313ui_call_popupmenu_hide (); 1314 }else { 1315ui_comp_remove_grid (& pum_grid ); 1316if (ui_has (kUIMultigrid )) { 1317ui_call_win_close (pum_grid .handle ); 1318ui_call_grid_destroy (pum_grid .handle ); 1319 } 1320// TODO(bfredl): consider keeping float grids allocated. 1321grid_free (& pum_grid ); 1322 } 1323pum_is_drawn = false; 1324pum_external = false; 1325win_float_close (kWinInfo ); 1326 } 1327} 1328 1329/// Clear the popup menu. Currently only resets the offset to the first 1330/// displayed item. 1331void pum_clear (void ) 1332{ 1333pum_first = 0 ; 1334} 1335 1336/// @return true if the popup menu is displayed. 1337bool pum_visible (void ) 1338{ 1339return pum_is_visible ; 1340} 1341 1342/// @return true if the popup menu is displayed and drawn on the grid. 1343bool pum_drawn (void ) 1344{ 1345return pum_visible ()&& !pum_external ; 1346} 1347 1348/// Screen was cleared, need to redraw next time 1349void pum_invalidate (void ) 1350{ 1351pum_invalid = true; 1352} 1353 1354void pum_recompose (void ) 1355{ 1356ui_comp_compose_grid (& pum_grid ); 1357} 1358 1359void pum_ext_select_item (int item ,bool insert ,bool finish ) 1360{ 1361if (!pum_visible ()|| item < -1 || item >=pum_size ) { 1362return ; 1363 } 1364pum_want .active = true; 1365pum_want .item = item ; 1366pum_want .insert = insert ; 1367pum_want .finish = finish ; 1368} 1369 1370/// Gets the height of the menu. 1371/// 1372/// @return the height of the popup menu, the number of entries visible. 1373/// Only valid when pum_visible() returns true! 1374int pum_get_height (void ) 1375{ 1376if (pum_external ) { 1377int ui_pum_height = ui_pum_get_height (); 1378if (ui_pum_height ) { 1379return ui_pum_height ; 1380 } 1381 } 1382return pum_height ; 1383} 1384 1385/// Add size information about the pum to "dict". 1386void pum_set_event_info (dict_T * dict ) 1387{ 1388if (!pum_visible ()) { 1389return ; 1390 } 1391double w ,h ,r ,c ; 1392if (!ui_pum_get_pos (& w ,& h ,& r ,& c )) { 1393w = (double )pum_width ; 1394h = (double )pum_height ; 1395r = (double )pum_row ; 1396c = (double )pum_col ; 1397 } 1398tv_dict_add_float (dict ,S_LEN ("height" ),h ); 1399tv_dict_add_float (dict ,S_LEN ("width" ),w ); 1400tv_dict_add_float (dict ,S_LEN ("row" ),r ); 1401tv_dict_add_float (dict ,S_LEN ("col" ),c ); 1402tv_dict_add_nr (dict ,S_LEN ("size" ),pum_size ); 1403tv_dict_add_bool (dict ,S_LEN ("scrollbar" ), 1404pum_scrollbar ?kBoolVarTrue :kBoolVarFalse ); 1405} 1406 1407static void pum_position_at_mouse (int min_width ) 1408{ 1409int min_row = 0 ; 1410int min_col = 0 ; 1411int max_row = Rows ; 1412int max_col = Columns ; 1413int grid = mouse_grid ; 1414int row = mouse_row ; 1415int col = mouse_col ; 1416pum_win_row_offset = 0 ; 1417pum_win_col_offset = 0 ; 1418 1419if (ui_has (kUIMultigrid )&& grid == 0 ) { 1420mouse_find_win_outer (& grid ,& row ,& col ); 1421 } 1422if (grid > 1 ) { 1423win_T * wp = get_win_by_grid_handle (grid ); 1424if (wp != NULL ) { 1425row += wp -> w_winrow ; 1426col += wp -> w_wincol ; 1427pum_win_row_offset = wp -> w_winrow ; 1428pum_win_col_offset = wp -> w_wincol ; 1429 1430if (wp -> w_view_height > 0 || wp -> w_view_width > 0 ) { 1431// When the user has requested a different grid size, let the popupmenu extend to the size 1432// of it. 1433max_row = MAX (Rows - wp -> w_winrow ,wp -> w_winrow + wp -> w_view_height ); 1434max_col = MAX (Columns - wp -> w_wincol ,wp -> w_wincol + wp -> w_view_width ); 1435 } 1436 } 1437 } 1438if (pum_grid .handle != 0 && grid == pum_grid .handle ) { 1439// Repositioning the menu by right-clicking on itself 1440row += pum_row ; 1441col += pum_left_col ; 1442 }else { 1443pum_anchor_grid = grid ; 1444 } 1445 1446// Both width and height are 1 for shadow border, otherwise 2 1447int border_width = pum_border_width (); 1448int border_height = border_width ; 1449if (max_row - row > pum_size + border_height || max_row - row > row - min_row ) { 1450// Enough space below the mouse row, 1451// or there is more space below the mouse row than above. 1452pum_above = false; 1453pum_row = row + 1 ; 1454if (pum_height + border_height > max_row - pum_row ) { 1455pum_height = max_row - pum_row - border_height ; 1456 } 1457 }else { 1458// Show above the mouse row, reduce height if it does not fit. 1459pum_above = true; 1460pum_row = row - pum_size - border_height ; 1461if (pum_row < min_row ) { 1462pum_height += pum_row - min_row ; 1463pum_row = min_row ; 1464 } 1465 } 1466 1467if (pum_rl ) { 1468if (col - min_col + 1 >=pum_base_width + border_width 1469|| col - min_col + 1 > min_width + border_width ) { 1470// Enough space to show at mouse column. 1471pum_col = col ; 1472 }else { 1473// Not enough space, left align with window. 1474pum_col = min_col + MIN (pum_base_width + border_width ,min_width + border_width )- 1 ; 1475 } 1476pum_width = pum_col - min_col + 1 - border_width ; 1477 }else { 1478if (max_col - col >=pum_base_width + border_width 1479|| max_col - col > min_width + border_width ) { 1480// Enough space to show at mouse column. 1481pum_col = col ; 1482 }else { 1483// Not enough space, right align with window. 1484pum_col = max_col - MIN (pum_base_width + border_width ,min_width + border_width ); 1485 } 1486pum_width = max_col - pum_col - border_width ; 1487 } 1488 1489pum_width = MIN (pum_width ,pum_base_width + 1 ); 1490} 1491 1492/// Select the pum entry at the mouse position. 1493static void pum_select_mouse_pos (void ) 1494{ 1495int grid = mouse_grid ; 1496int row = mouse_row ; 1497int col = mouse_col ; 1498 1499if (grid == 0 ) { 1500mouse_find_win_outer (& grid ,& row ,& col ); 1501 } 1502 1503if (grid == pum_grid .handle ) { 1504// Offset by 1 when border width is 2 (non-shadow border) 1505int border_offset = pum_border_width ()== 2 ?1 :0 ; 1506int item = row - border_offset ; 1507pum_selected = (item >=0 && item < pum_height ) ?item :-1 ; 1508return ; 1509 } 1510 1511if (grid != pum_anchor_grid 1512|| col < pum_left_col - pum_win_col_offset 1513|| col >=pum_right_col - pum_win_col_offset ) { 1514pum_selected = -1 ; 1515return ; 1516 } 1517 1518int idx = row - (pum_row - pum_win_row_offset ); 1519 1520if (idx < 0 || idx >=pum_height ) { 1521pum_selected = -1 ; 1522 }else if (* pum_array [idx ].pum_text != NUL ) { 1523pum_selected = idx ; 1524 } 1525} 1526 1527/// Execute the currently selected popup menu item. 1528static void pum_execute_menu (vimmenu_T * menu ,int mode ) 1529{ 1530int idx = 0 ; 1531exarg_T ea ; 1532 1533for (vimmenu_T * mp = menu -> children ;mp != NULL ;mp = mp -> next ) { 1534if ((mp -> modes & mp -> enabled & mode )&& idx ++ == pum_selected ) { 1535CLEAR_FIELD (ea ); 1536execute_menu (& ea ,mp ,-1 ); 1537break ; 1538 } 1539 } 1540} 1541 1542/// Open the terminal version of the popup menu and don't return until it is closed. 1543void pum_show_popupmenu (vimmenu_T * menu ) 1544{ 1545pum_undisplay (true); 1546pum_size = 0 ; 1547int mode = get_menu_mode_flag (); 1548 1549for (vimmenu_T * mp = menu -> children ;mp != NULL ;mp = mp -> next ) { 1550if (menu_is_separator (mp -> dname )|| (mp -> modes & mp -> enabled & mode )) { 1551pum_size ++ ; 1552 } 1553 } 1554 1555// When there are only Terminal mode menus, using "popup Edit" results in 1556// pum_size being zero. 1557if (pum_size <=0 ) { 1558emsg (_ (e_menu_only_exists_in_another_mode )); 1559return ; 1560 } 1561 1562int idx = 0 ; 1563pumitem_T * array = (pumitem_T * )xcalloc ((size_t )pum_size ,sizeof (pumitem_T )); 1564 1565for (vimmenu_T * mp = menu -> children ;mp != NULL ;mp = mp -> next ) { 1566char * s = NULL ; 1567// Make a copy of the text, the menu may be redefined in a callback. 1568if (menu_is_separator (mp -> dname )) { 1569s = "" ; 1570 }else if (mp -> modes & mp -> enabled & mode ) { 1571s = mp -> dname ; 1572 } 1573if (s != NULL ) { 1574s = xstrdup (s ); 1575array [idx ++ ].pum_text = s ; 1576 } 1577 } 1578 1579pum_array = array ; 1580pum_compute_size (); 1581pum_scrollbar = 0 ; 1582pum_height = pum_size ; 1583pum_rl = curwin -> w_p_rl ; 1584pum_position_at_mouse (20 ); 1585 1586pum_selected = -1 ; 1587pum_first = 0 ; 1588if (!p_mousemev ) { 1589// Pretend 'mousemoveevent' is set. 1590ui_call_option_set (STATIC_CSTR_AS_STRING ("mousemoveevent" ),BOOLEAN_OBJ (true)); 1591 } 1592 1593while (true) { 1594pum_is_visible = true; 1595pum_is_drawn = true; 1596pum_grid .zindex = kZIndexCmdlinePopupMenu ;// show above cmdline area #23275 1597pum_redraw (); 1598setcursor_mayforce (curwin , true); 1599 1600int c = vgetc (); 1601 1602// Bail out when typing Esc, CTRL-C or some callback or <expr> mapping 1603// closed the popup menu. 1604if (c == ESC || c == Ctrl_C || pum_array == NULL ) { 1605break ; 1606 }else if (c == CAR || c == NL ) { 1607// enter: select current item, if any, and close 1608pum_execute_menu (menu ,mode ); 1609break ; 1610 }else if (c == 'k' || c == K_UP || c == K_MOUSEUP ) { 1611// cursor up: select previous item 1612while (pum_selected > 0 ) { 1613pum_selected -- ; 1614if (* array [pum_selected ].pum_text != NUL ) { 1615break ; 1616 } 1617 } 1618 }else if (c == 'j' || c == K_DOWN || c == K_MOUSEDOWN ) { 1619// cursor down: select next item 1620while (pum_selected < pum_size - 1 ) { 1621pum_selected ++ ; 1622if (* array [pum_selected ].pum_text != NUL ) { 1623break ; 1624 } 1625 } 1626 }else if (c == K_RIGHTMOUSE ) { 1627// Right mouse down: reposition the menu. 1628vungetc (c ); 1629break ; 1630 }else if (c == K_LEFTDRAG || c == K_RIGHTDRAG || c == K_MOUSEMOVE ) { 1631// mouse moved: select item in the mouse row 1632pum_select_mouse_pos (); 1633 }else if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM || c == K_RIGHTRELEASE ) { 1634// left mouse click: select clicked item, if any, and close; 1635// right mouse release: select clicked item, close if any 1636pum_select_mouse_pos (); 1637if (pum_selected >=0 ) { 1638pum_execute_menu (menu ,mode ); 1639break ; 1640 } 1641if (c == K_LEFTMOUSE || c == K_LEFTMOUSE_NM ) { 1642break ; 1643 } 1644 } 1645 } 1646 1647for (idx = 0 ;idx < pum_size ;idx ++ ) { 1648xfree (array [idx ].pum_text ); 1649 } 1650xfree (array ); 1651pum_undisplay (true); 1652if (!p_mousemev ) { 1653ui_call_option_set (STATIC_CSTR_AS_STRING ("mousemoveevent" ),BOOLEAN_OBJ (false)); 1654 } 1655} 1656 1657void pum_make_popup (const char * path_name ,int use_mouse_pos ) 1658{ 1659if (!use_mouse_pos ) { 1660// Hack: set mouse position at the cursor so that the menu pops up 1661// around there. 1662mouse_row = curwin -> w_grid .row_offset + curwin -> w_wrow ; 1663mouse_col = curwin -> w_grid .col_offset 1664+ (curwin -> w_p_rl ?curwin -> w_view_width - curwin -> w_wcol - 1 1665 :curwin -> w_wcol ); 1666if (ui_has (kUIMultigrid )) { 1667mouse_grid = curwin -> w_grid .target -> handle ; 1668 }else if (curwin -> w_grid .target != & default_grid ) { 1669mouse_grid = 0 ; 1670mouse_row += curwin -> w_winrow ; 1671mouse_col += curwin -> w_wincol ; 1672 } 1673 } 1674 1675vimmenu_T * menu = menu_find (path_name ); 1676if (menu != NULL ) { 1677pum_show_popupmenu (menu ); 1678 } 1679} 1680 1681void pum_pos_ui_flush (bool force ) 1682{ 1683if (force || (ui_has (kUIMultigrid )&& pum_is_drawn && !pum_external 1684&& pum_grid .handle != 0 && pum_grid .pending_comp_index_update )) { 1685const char * anchor = pum_above ?"SW" :"NW" ; 1686int row_off = pum_above ?- pum_height :0 ; 1687ui_call_win_float_pos (pum_grid .handle ,-1 ,cstr_as_string (anchor ),pum_anchor_grid , 1688pum_row - row_off - pum_win_row_offset ,pum_left_col - pum_win_col_offset , 1689 false,pum_grid .zindex , (int )pum_grid .comp_index ,pum_grid .comp_row , 1690pum_grid .comp_col ); 1691pum_grid .pending_comp_index_update = false; 1692 } 1693}