robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1// Low-level functions to manipulate individual character cells on the 2// screen grid. 3// 4// Most of the routines in this file perform screen (grid) manipulations. The 5// given operation is performed physically on the screen. The corresponding 6// change is also made to the internal screen image. In this way, the editor 7// anticipates the effect of editing changes on the appearance of the screen. 8// That way, when we call update_screen() a complete redraw isn't usually 9// necessary. Another advantage is that we can keep adding code to anticipate 10// screen changes, and in the meantime, everything still works. 11// 12// The grid_*() functions write to the screen and handle updating grid->lines[]. 13 14#include <assert.h> 15#include <limits.h> 16#include <stdint.h> 17#include <stdlib.h> 18#include <string.h> 19 20#include "nvim/api/private/defs.h" 21#include "nvim/arabic.h" 22#include "nvim/ascii_defs.h" 23#include "nvim/buffer_defs.h" 24#include "nvim/decoration.h" 25#include "nvim/globals.h" 26#include "nvim/grid.h" 27#include "nvim/highlight.h" 28#include "nvim/log.h" 29#include "nvim/map_defs.h" 30#include "nvim/mbyte.h" 31#include "nvim/memory.h" 32#include "nvim/message.h" 33#include "nvim/option_vars.h" 34#include "nvim/optionstr.h" 35#include "nvim/types_defs.h" 36#include "nvim/ui.h" 37#include "nvim/ui_defs.h" 38 39#include "grid.c.generated.h" 40 41// temporary buffer for rendering a single screenline, so it can be 42// compared with previous contents to calculate smallest delta. 43// Per-cell attributes 44static size_t linebuf_size = 0 ; 45 46// Used to cache glyphs which doesn't fit an a sizeof(schar_T) length UTF-8 string. 47// Then it instead stores an index into glyph_cache.keys[] which is a flat char array. 48// The hash part is used by schar_from_buf() to quickly lookup glyphs which already 49// has been interned. schar_get() should used to convert a schar_T value 50// back to a string buffer. 51// 52// The maximum byte size of a glyph is MAX_SCHAR_SIZE (including the final NUL). 53static Set (glyph )glyph_cache = SET_INIT ; 54 55/// Determine if dedicated window grid should be used or the default_grid 56/// 57/// If UI did not request multigrid support, draw all windows on the 58/// default_grid. 59/// 60/// NB: this function can only been used with window grids in a context where 61/// win_grid_alloc already has been called! 62/// 63/// If the default_grid is used, adjust window relative positions to global 64/// screen positions. 65ScreenGrid * grid_adjust (GridView * grid ,int * row_off ,int * col_off ) 66{ 67* row_off += grid -> row_offset ; 68* col_off += grid -> col_offset ; 69return grid -> target ; 70} 71 72schar_T schar_from_str (const char * str ) 73{ 74if (str == NULL ) { 75return 0 ; 76 } 77return schar_from_buf (str ,strlen (str )); 78} 79 80/// @param buf need not be NUL terminated, but may not contain embedded NULs. 81/// 82/// caller must ensure len < MAX_SCHAR_SIZE (not =, as NUL needs a byte) 83schar_T schar_from_buf (const char * buf ,size_t len ) 84{ 85assert (len < MAX_SCHAR_SIZE ); 86if (len <=4 ) { 87schar_T sc = 0 ; 88memcpy ((char * )& sc ,buf ,len ); 89return sc ; 90 }else { 91String str = { .data = (char * )buf , .size = len }; 92 93MHPutStatus status ; 94uint32_t idx = set_put_idx (glyph ,& glyph_cache ,str ,& status ); 95assert (idx < 0xFFFFFF ); 96#ifdef ORDER_BIG_ENDIAN 97return idx + ((uint32_t )0xFF <<24 ); 98#else 99return 0xFF + (idx <<8 ); 100#endif 101 } 102} 103 104/// Check if cache is full, and if it is, clear it. 105/// 106/// This should normally only be called in update_screen() 107/// 108/// @return true if cache was clered, and all your screen buffers now are hosed 109/// and you need to use UPD_CLEAR 110bool schar_cache_clear_if_full (void ) 111{ 112// note: critical max is really (1<<24)-1. This gives us some marginal 113// until next time update_screen() is called 114if (glyph_cache .h .n_keys > (1 <<21 )) { 115schar_cache_clear (); 116return true; 117 } 118return false; 119} 120 121void schar_cache_clear (void ) 122{ 123decor_check_invalid_glyphs (); 124set_clear (glyph ,& glyph_cache ); 125 126// for char options we have stored the original strings. Regenerate 127// the parsed schar_T values with the new clean cache. 128// This must not return an error as cell widths have not changed. 129if (check_chars_options ()) { 130abort (); 131 } 132} 133 134bool schar_high (schar_T sc ) 135{ 136#ifdef ORDER_BIG_ENDIAN 137return ((sc & 0xFF000000 )== 0xFF000000 ); 138#else 139return ((sc & 0xFF )== 0xFF ); 140#endif 141} 142 143#ifdef ORDER_BIG_ENDIAN 144# define schar_idx (sc ) (sc & (0x00FFFFFF)) 145#else 146# define schar_idx (sc ) (sc >> 8) 147#endif 148 149/// sets final NUL 150size_t schar_get (char * buf_out ,schar_T sc ) 151{ 152size_t len = schar_get_adv (& buf_out ,sc ); 153* buf_out = NUL ; 154return len ; 155} 156 157/// advance buf_out. do NOT set final NUL 158size_t schar_get_adv (char ** buf_out ,schar_T sc ) 159{ 160size_t len ; 161if (schar_high (sc )) { 162uint32_t idx = schar_idx (sc ); 163assert (idx < glyph_cache .h .n_keys ); 164len = strlen (& glyph_cache .keys [idx ]); 165memcpy (* buf_out ,& glyph_cache .keys [idx ],len ); 166 }else { 167len = strnlen ((char * )& sc ,4 ); 168memcpy (* buf_out , (char * )& sc ,len ); 169 } 170* buf_out += len ; 171return len ; 172} 173 174size_t schar_len (schar_T sc ) 175{ 176if (schar_high (sc )) { 177uint32_t idx = schar_idx (sc ); 178assert (idx < glyph_cache .h .n_keys ); 179return strlen (& glyph_cache .keys [idx ]); 180 }else { 181return strnlen ((char * )& sc ,4 ); 182 } 183} 184 185int schar_cells (schar_T sc ) 186{ 187// hot path 188#ifdef ORDER_BIG_ENDIAN 189if (!(sc & 0x80FFFFFF )) { 190return 1 ; 191 } 192#else 193if (sc < 0x80 ) { 194return 1 ; 195 } 196#endif 197 198char sc_buf [MAX_SCHAR_SIZE ]; 199schar_get (sc_buf ,sc ); 200return ptr2cells (sc_buf ); 201} 202 203/// gets first raw UTF-8 byte of an schar 204static char schar_get_first_byte (schar_T sc ) 205{ 206assert (!(schar_high (sc )&& schar_idx (sc ) >=glyph_cache .h .n_keys )); 207return schar_high (sc ) ?glyph_cache .keys [schar_idx (sc )] :* (char * )& sc ; 208} 209 210int schar_get_first_codepoint (schar_T sc ) 211{ 212char sc_buf [MAX_SCHAR_SIZE ]; 213schar_get (sc_buf ,sc ); 214return utf_ptr2char (sc_buf ); 215} 216 217/// @return ascii char or NUL if not ascii 218char schar_get_ascii (schar_T sc ) 219{ 220#ifdef ORDER_BIG_ENDIAN 221return (!(sc & 0x80FFFFFF )) ?* (char * )& sc :NUL ; 222#else 223return (sc < 0x80 ) ? (char )sc :NUL ; 224#endif 225} 226 227static bool schar_in_arabic_block (schar_T sc ) 228{ 229char first_byte = schar_get_first_byte (sc ); 230return ((uint8_t )first_byte & 0xFE )== 0xD8 ; 231} 232 233/// Get the first two codepoints of an schar, or NUL when not available 234static void schar_get_first_two_codepoints (schar_T sc ,int * c0 ,int * c1 ) 235{ 236char sc_buf [MAX_SCHAR_SIZE ]; 237schar_get (sc_buf ,sc ); 238 239* c0 = utf_ptr2char (sc_buf ); 240int len = utf_ptr2len (sc_buf ); 241if (* c0 == NUL ) { 242* c1 = NUL ; 243 }else { 244* c1 = utf_ptr2char (sc_buf + len ); 245 } 246} 247 248void line_do_arabic_shape (schar_T * buf ,int cols ) 249{ 250int i = 0 ; 251 252for (i = 0 ;i < cols ;i ++ ) { 253// quickly skip over non-arabic text 254if (schar_in_arabic_block (buf [i ])) { 255break ; 256 } 257 } 258 259if (i == cols ) { 260return ; 261 } 262 263int c0prev = 0 ; 264int c0 ,c1 ; 265schar_get_first_two_codepoints (buf [i ],& c0 ,& c1 ); 266 267for (;i < cols ;i ++ ) { 268int c0next ,c1next ; 269schar_get_first_two_codepoints (i + 1 < cols ?buf [i + 1 ] :0 ,& c0next ,& c1next ); 270 271if (!ARABIC_CHAR (c0 )) { 272 gotonext ; 273 } 274 275int c1new = c1 ; 276int c0new = arabic_shape (c0 ,& c1new ,c0next ,c1next ,c0prev ); 277 278if (c0new == c0 && c1new == c1 ) { 279 gotonext ;// unchanged 280 } 281 282char scbuf [MAX_SCHAR_SIZE ]; 283schar_get (scbuf ,buf [i ]); 284 285char scbuf_new [MAX_SCHAR_SIZE ]; 286size_t len = (size_t )utf_char2bytes (c0new ,scbuf_new ); 287if (c1new ) { 288len += (size_t )utf_char2bytes (c1new ,scbuf_new + len ); 289 } 290 291int off = utf_char2len (c0 )+ (c1 ?utf_char2len (c1 ) :0 ); 292size_t rest = strlen (scbuf + off ); 293if (rest + len + 1 > MAX_SCHAR_SIZE ) { 294// Too bigly, discard one code-point. 295// This should be enough as c0 cannot grow more than from 2 to 4 bytes 296// (base arabic to extended arabic) 297rest -= (size_t )utf_cp_bounds (scbuf + off ,scbuf + off + rest - 1 ).begin_off + 1 ; 298 } 299memcpy (scbuf_new + len ,scbuf + off ,rest ); 300buf [i ]= schar_from_buf (scbuf_new ,len + rest ); 301 302next : 303c0prev = c0 ; 304c0 = c0next ; 305c1 = c1next ; 306 } 307} 308 309/// clear a line in the grid starting at "off" until "width" characters 310/// are cleared. 311void grid_clear_line (ScreenGrid * grid ,size_t off ,int width ,bool valid ) 312{ 313for (int col = 0 ;col < width ;col ++ ) { 314grid -> chars [off + (size_t )col ]= schar_from_ascii (' ' ); 315 } 316int fill = valid ?0 :-1 ; 317memset (grid -> attrs + off ,fill , (size_t )width * sizeof (sattr_T )); 318memset (grid -> vcols + off ,-1 , (size_t )width * sizeof (colnr_T )); 319} 320 321void grid_invalidate (ScreenGrid * grid ) 322{ 323memset (grid -> attrs ,-1 ,sizeof (sattr_T )* (size_t )grid -> rows * (size_t )grid -> cols ); 324} 325 326static bool grid_invalid_row (ScreenGrid * grid ,int row ) 327{ 328return grid -> attrs [grid -> line_offset [row ]]< 0 ; 329} 330 331/// Get a single character directly from grid.chars 332/// 333/// @param[out] attrp set to the character's attribute (optional) 334schar_T grid_getchar (ScreenGrid * grid ,int row ,int col ,int * attrp ) 335{ 336// safety check 337if (grid -> chars == NULL || row >=grid -> rows || col >=grid -> cols ) { 338return NUL ; 339 } 340 341size_t off = grid -> line_offset [row ]+ (size_t )col ; 342if (attrp != NULL ) { 343* attrp = grid -> attrs [off ]; 344 } 345return grid -> chars [off ]; 346} 347 348static ScreenGrid * grid_line_grid = NULL ; 349static int grid_line_row = -1 ; 350static int grid_line_coloff = 0 ; 351static int grid_line_maxcol = 0 ; 352static int grid_line_first = INT_MAX ; 353static int grid_line_last = 0 ; 354static int grid_line_clear_to = 0 ; 355static int grid_line_bg_attr = 0 ; 356static int grid_line_clear_attr = 0 ; 357static int grid_line_flags = 0 ; 358 359/// Start a group of grid_line_puts calls that builds a single grid line. 360/// 361/// Must be matched with a grid_line_flush call before moving to 362/// another line. 363void grid_line_start (GridView * view ,int row ) 364{ 365int col = 0 ; 366ScreenGrid * grid = grid_adjust (view ,& row ,& col ); 367screengrid_line_start (grid ,row ,col ); 368} 369 370void screengrid_line_start (ScreenGrid * grid ,int row ,int col ) 371{ 372grid_line_maxcol = grid -> cols ; 373assert (grid_line_grid == NULL ); 374grid_line_row = row ; 375grid_line_grid = grid ; 376grid_line_coloff = col ; 377grid_line_first = (int )linebuf_size ; 378grid_line_maxcol = MIN (grid_line_maxcol ,grid -> cols - grid_line_coloff ); 379grid_line_last = 0 ; 380grid_line_clear_to = 0 ; 381grid_line_bg_attr = 0 ; 382grid_line_clear_attr = 0 ; 383grid_line_flags = 0 ; 384 385assert ((size_t )grid_line_maxcol <=linebuf_size ); 386 387if (full_screen && (rdb_flags & kOptRdbFlagInvalid )) { 388assert (linebuf_char ); 389// Current batch must not depend on previous contents of linebuf_char. 390// Set invalid values which will cause assertion failures later if they are used. 391memset (linebuf_char ,0xFF ,sizeof (schar_T )* linebuf_size ); 392memset (linebuf_attr ,0xFF ,sizeof (sattr_T )* linebuf_size ); 393 } 394} 395 396/// Get present char from current rendered screen line 397/// 398/// This indicates what already is on screen, not the pending render buffer. 399/// 400/// @return char or space if out of bounds 401schar_T grid_line_getchar (int col ,int * attr ) 402{ 403if (col < grid_line_maxcol ) { 404col += grid_line_coloff ; 405size_t off = grid_line_grid -> line_offset [grid_line_row ]+ (size_t )col ; 406if (attr != NULL ) { 407* attr = grid_line_grid -> attrs [off ]; 408 } 409return grid_line_grid -> chars [off ]; 410 }else { 411// NUL is a very special value (right-half of double width), space is True Neutral™ 412return schar_from_ascii (' ' ); 413 } 414} 415 416void grid_line_put_schar (int col ,schar_T schar ,int attr ) 417{ 418assert (grid_line_grid ); 419if (col >=grid_line_maxcol ) { 420return ; 421 } 422 423linebuf_char [col ]= schar ; 424linebuf_attr [col ]= attr ; 425 426grid_line_first = MIN (grid_line_first ,col ); 427// TODO(bfredl): Y U NO DOUBLEWIDTH? 428grid_line_last = MAX (grid_line_last ,col + 1 ); 429linebuf_vcol [col ]= -1 ; 430} 431 432/// Put string "text" at "col" position relative to the grid line from the 433/// recent grid_line_start() call. 434/// 435/// @param textlen length of string or -1 to use strlen(text) 436/// Note: only outputs within one row! 437/// 438/// @return number of grid cells used 439int grid_line_puts (int col ,const char * text ,int textlen ,int attr ) 440{ 441const char * ptr = text ; 442int len = textlen ; 443 444assert (grid_line_grid ); 445 446int start_col = col ; 447 448const int max_col = grid_line_maxcol ; 449while (col < max_col && (len < 0 || (int )(ptr - text )< len )&& * ptr != NUL ) { 450// check if this is the first byte of a multibyte 451int mbyte_blen ; 452if (len >=0 ) { 453int maxlen = (int )((text + len )- ptr ); 454mbyte_blen = utfc_ptr2len_len (ptr ,maxlen ); 455if (mbyte_blen > maxlen ) { 456mbyte_blen = 1 ; 457 } 458 }else { 459mbyte_blen = utfc_ptr2len (ptr ); 460 } 461int firstc ; 462schar_T schar = utfc_ptrlen2schar (ptr ,mbyte_blen ,& firstc ); 463int mbyte_cells = utf_ptr2cells_len (ptr ,mbyte_blen ); 464if (mbyte_cells > 2 || schar == 0 ) { 465mbyte_cells = 1 ; 466schar = schar_from_char (0xFFFD ); 467 } 468 469if (col + mbyte_cells > max_col ) { 470// Only 1 cell left, but character requires 2 cells: 471// display a '>' in the last column to avoid wrapping. 472schar = schar_from_ascii ('>' ); 473mbyte_cells = 1 ; 474 } 475 476// When at the start of the text and overwriting the right half of a 477// two-cell character in the same grid, truncate that into a '>'. 478if (ptr == text && col > grid_line_first && col < grid_line_last 479&& linebuf_char [col ]== 0 ) { 480linebuf_char [col - 1 ]= schar_from_ascii ('>' ); 481 } 482 483linebuf_char [col ]= schar ; 484linebuf_attr [col ]= attr ; 485linebuf_vcol [col ]= -1 ; 486if (mbyte_cells == 2 ) { 487linebuf_char [col + 1 ]= 0 ; 488linebuf_attr [col + 1 ]= attr ; 489linebuf_vcol [col + 1 ]= -1 ; 490 } 491 492col += mbyte_cells ; 493ptr += mbyte_blen ; 494 } 495 496if (col > start_col ) { 497grid_line_first = MIN (grid_line_first ,start_col ); 498grid_line_last = MAX (grid_line_last ,col ); 499 } 500 501return col - start_col ; 502} 503 504int grid_line_fill (int start_col ,int end_col ,schar_T sc ,int attr ) 505{ 506end_col = MIN (end_col ,grid_line_maxcol ); 507if (start_col >=end_col ) { 508return end_col ; 509 } 510 511for (int col = start_col ;col < end_col ;col ++ ) { 512linebuf_char [col ]= sc ; 513linebuf_attr [col ]= attr ; 514linebuf_vcol [col ]= -1 ; 515 } 516 517grid_line_first = MIN (grid_line_first ,start_col ); 518grid_line_last = MAX (grid_line_last ,end_col ); 519return end_col ; 520} 521 522/// @param bg_attr applies to both the buffered line and the columns to clear 523/// @param clear_attr applies only to the columns to clear 524void grid_line_clear_end (int start_col ,int end_col ,int bg_attr ,int clear_attr ) 525{ 526if (grid_line_first > start_col ) { 527grid_line_first = start_col ; 528grid_line_last = start_col ; 529 } 530grid_line_clear_to = end_col ; 531grid_line_bg_attr = bg_attr ; 532grid_line_clear_attr = clear_attr ; 533} 534 535/// move the cursor to a position in a currently rendered line. 536void grid_line_cursor_goto (int col ) 537{ 538ui_grid_cursor_goto (grid_line_grid -> handle ,grid_line_row ,col ); 539} 540 541void grid_line_mirror (int width ) 542{ 543grid_line_clear_to = MAX (grid_line_last ,grid_line_clear_to ); 544if (grid_line_first >=grid_line_clear_to ) { 545return ; 546 } 547linebuf_mirror (& grid_line_first ,& grid_line_last ,& grid_line_clear_to ,width ); 548grid_line_flags |=SLF_RIGHTLEFT ; 549} 550 551void linebuf_mirror (int * firstp ,int * lastp ,int * clearp ,int width ) 552{ 553int first = * firstp ; 554int last = * lastp ; 555 556size_t n = (size_t )(last - first ); 557int mirror = width - 1 ;// Mirrors are more fun than television. 558schar_T * scratch_char = (schar_T * )linebuf_scratch ; 559memcpy (scratch_char + first ,linebuf_char + first ,n * sizeof (schar_T )); 560for (int col = first ;col < last ;col ++ ) { 561int rev = mirror - col ; 562if (col + 1 < last && scratch_char [col + 1 ]== 0 ) { 563linebuf_char [rev - 1 ]= scratch_char [col ]; 564linebuf_char [rev ]= 0 ; 565col ++ ; 566 }else { 567linebuf_char [rev ]= scratch_char [col ]; 568 } 569 } 570 571// for attr and vcol: assumes doublewidth chars are self-consistent 572sattr_T * scratch_attr = (sattr_T * )linebuf_scratch ; 573memcpy (scratch_attr + first ,linebuf_attr + first ,n * sizeof (sattr_T )); 574for (int col = first ;col < last ;col ++ ) { 575linebuf_attr [mirror - col ]= scratch_attr [col ]; 576 } 577 578colnr_T * scratch_vcol = (colnr_T * )linebuf_scratch ; 579memcpy (scratch_vcol + first ,linebuf_vcol + first ,n * sizeof (colnr_T )); 580for (int col = first ;col < last ;col ++ ) { 581linebuf_vcol [mirror - col ]= scratch_vcol [col ]; 582 } 583 584* firstp = width - * clearp ; 585* clearp = width - first ; 586* lastp = width - last ; 587} 588 589/// End a group of grid_line_puts calls and send the screen buffer to the UI layer. 590void grid_line_flush (void ) 591{ 592ScreenGrid * grid = grid_line_grid ; 593grid_line_grid = NULL ; 594grid_line_clear_to = MAX (grid_line_last ,grid_line_clear_to ); 595assert (grid_line_clear_to <=grid_line_maxcol ); 596if (grid_line_first >=grid_line_clear_to ) { 597return ; 598 } 599 600grid_put_linebuf (grid ,grid_line_row ,grid_line_coloff ,grid_line_first ,grid_line_last , 601grid_line_clear_to ,grid_line_bg_attr ,grid_line_clear_attr ,-1 , 602grid_line_flags ); 603} 604 605/// flush grid line but only if on a valid row 606/// 607/// This is a stopgap until message.c has been refactored to behave 608void grid_line_flush_if_valid_row (void ) 609{ 610if (grid_line_row < 0 || grid_line_row >=grid_line_grid -> rows ) { 611if (rdb_flags & kOptRdbFlagInvalid ) { 612abort (); 613 }else { 614grid_line_grid = NULL ; 615return ; 616 } 617 } 618grid_line_flush (); 619} 620 621void grid_clear (GridView * grid ,int start_row ,int end_row ,int start_col ,int end_col ,int attr ) 622{ 623for (int row = start_row ;row < end_row ;row ++ ) { 624grid_line_start (grid ,row ); 625end_col = MIN (end_col ,grid_line_maxcol ); 626if (grid_line_row >=grid_line_grid -> rows || start_col >=end_col ) { 627grid_line_grid = NULL ;// TODO(bfredl): make callers behave instead 628return ; 629 } 630grid_line_clear_end (start_col ,end_col ,attr ,0 ); 631grid_line_flush (); 632 } 633} 634 635/// Check whether the given character needs redrawing: 636/// - the (first byte of the) character is different 637/// - the attributes are different 638/// - the character is multi-byte and the next byte is different 639/// - the character is two cells wide and the second cell differs. 640static int grid_char_needs_redraw (ScreenGrid * grid ,int col ,size_t off_to ,int cols ) 641{ 642return (cols > 0 643&& ((linebuf_char [col ]!= grid -> chars [off_to ] 644|| linebuf_attr [col ]!= grid -> attrs [off_to ] 645|| (cols > 1 && linebuf_char [col + 1 ]== 0 646&& linebuf_char [col + 1 ]!= grid -> chars [off_to + 1 ])) 647|| rdb_flags & kOptRdbFlagNodelta )); 648} 649 650/// Move one buffered line to the window grid, but only the characters that 651/// have actually changed. Handle insert/delete character. 652/// 653/// @param coloff gives the first column on the grid for this line. 654/// @param endcol gives the columns where valid characters are. 655/// @param clear_width see SLF_RIGHTLEFT. 656/// @param clear_attr combined with "bg_attr" for the columns to clear. 657/// @param flags can have bits: 658/// - SLF_RIGHTLEFT rightleft text, like a window with 'rightleft' option set: 659/// - When false, clear columns "endcol" to "clear_width". 660/// - When true, clear columns "col" to "endcol". 661/// - SLF_WRAP hint to UI that "row" contains a line wrapped into the next row. 662/// - SLF_INC_VCOL: 663/// - When false, use "last_vcol" for grid->vcols[] of the columns to clear. 664/// - When true, use an increasing sequence starting from "last_vcol + 1" for 665/// grid->vcols[] of the columns to clear. 666void grid_put_linebuf (ScreenGrid * grid ,int row ,int coloff ,int col ,int endcol ,int clear_width , 667int bg_attr ,int clear_attr ,colnr_T last_vcol ,int flags ) 668{ 669bool redraw_next ;// redraw_this for next character 670bool clear_next = false; 671assert (0 <=row && row < grid -> rows ); 672// TODO(bfredl): check all callsites and eliminate 673// Check for illegal col, just in case 674if (endcol > grid -> cols ) { 675endcol = grid -> cols ; 676 } 677 678// Safety check. Avoids clang warnings down the call stack. 679if (grid -> chars == NULL || row >=grid -> rows || coloff >=grid -> cols ) { 680DLOG ("invalid state, skipped" ); 681return ; 682 } 683 684bool invalid_row = grid != & default_grid && grid_invalid_row (grid ,row )&& col == 0 ; 685size_t off_to = grid -> line_offset [row ]+ (size_t )coloff ; 686const size_t max_off_to = grid -> line_offset [row ]+ (size_t )grid -> cols ; 687 688// When at the start of the text and overwriting the right half of a 689// two-cell character in the same grid, truncate that into a '>'. 690if (col > 0 && grid -> chars [off_to + (size_t )col ]== 0 ) { 691linebuf_char [col - 1 ]= schar_from_ascii ('>' ); 692linebuf_attr [col - 1 ]= grid -> attrs [off_to + (size_t )col - 1 ]; 693col -- ; 694 } 695 696int clear_start = endcol ; 697if (flags & SLF_RIGHTLEFT ) { 698clear_start = col ; 699col = endcol ; 700endcol = clear_width ; 701clear_width = col ; 702 } 703 704if (p_arshape && !p_tbidi && endcol > col ) { 705line_do_arabic_shape (linebuf_char + col ,endcol - col ); 706 } 707 708if (bg_attr ) { 709for (int c = col ;c < endcol ;c ++ ) { 710linebuf_attr [c ]= hl_combine_attr (bg_attr ,linebuf_attr [c ]); 711 } 712 } 713 714redraw_next = grid_char_needs_redraw (grid ,col ,off_to + (size_t )col ,endcol - col ); 715 716int start_dirty = -1 ; 717int end_dirty = 0 ; 718 719while (col < endcol ) { 720int char_cells = 1 ;// 1: normal char 721// 2: occupies two display cells 722if (col + 1 < endcol && linebuf_char [col + 1 ]== 0 ) { 723char_cells = 2 ; 724 } 725bool redraw_this = redraw_next ;// Does character need redraw? 726size_t off = off_to + (size_t )col ; 727redraw_next = grid_char_needs_redraw (grid ,col + char_cells , 728off + (size_t )char_cells , 729endcol - col - char_cells ); 730 731if (redraw_this ) { 732if (start_dirty == -1 ) { 733start_dirty = col ; 734 } 735end_dirty = col + char_cells ; 736// When writing a single-width character over a double-width 737// character and at the end of the redrawn text, need to clear out 738// the right half of the old character. 739// Also required when writing the right half of a double-width 740// char over the left half of an existing one 741if (col + char_cells == endcol && off + (size_t )char_cells < max_off_to 742&& grid -> chars [off + (size_t )char_cells ]== NUL ) { 743clear_next = true; 744 } 745 746grid -> chars [off ]= linebuf_char [col ]; 747if (char_cells == 2 ) { 748grid -> chars [off + 1 ]= linebuf_char [col + 1 ]; 749 } 750 751grid -> attrs [off ]= linebuf_attr [col ]; 752// For simplicity set the attributes of second half of a 753// double-wide character equal to the first half. 754if (char_cells == 2 ) { 755grid -> attrs [off + 1 ]= linebuf_attr [col ]; 756 } 757 } 758 759grid -> vcols [off ]= linebuf_vcol [col ]; 760if (char_cells == 2 ) { 761grid -> vcols [off + 1 ]= linebuf_vcol [col + 1 ]; 762 } 763 764col += char_cells ; 765 } 766 767if (clear_next ) { 768// Clear the second half of a double-wide character of which the left 769// half was overwritten with a single-wide character. 770grid -> chars [off_to + (size_t )col ]= schar_from_ascii (' ' ); 771end_dirty ++ ; 772 } 773 774// When clearing the left half of a double-wide char also clear the right half. 775if (off_to + (size_t )clear_width < max_off_to 776&& grid -> chars [off_to + (size_t )clear_width ]== 0 ) { 777clear_width ++ ; 778 } 779 780int clear_dirty_start = -1 ,clear_end = -1 ; 781if (flags & SLF_RIGHTLEFT ) { 782for (col = clear_width - 1 ;col >=clear_start ;col -- ) { 783size_t off = off_to + (size_t )col ; 784grid -> vcols [off ]= (flags & SLF_INC_VCOL ) ?++ last_vcol :last_vcol ; 785 } 786 } 787clear_attr = hl_combine_attr (bg_attr ,clear_attr ); 788// blank out the rest of the line 789// TODO(bfredl): we could cache winline widths 790for (col = clear_start ;col < clear_width ;col ++ ) { 791size_t off = off_to + (size_t )col ; 792if (grid -> chars [off ]!= schar_from_ascii (' ' ) 793|| grid -> attrs [off ]!= clear_attr 794|| rdb_flags & kOptRdbFlagNodelta ) { 795grid -> chars [off ]= schar_from_ascii (' ' ); 796grid -> attrs [off ]= clear_attr ; 797if (clear_dirty_start == -1 ) { 798clear_dirty_start = col ; 799 } 800clear_end = col + 1 ; 801 } 802if (!(flags & SLF_RIGHTLEFT )) { 803grid -> vcols [off ]= (flags & SLF_INC_VCOL ) ?++ last_vcol :last_vcol ; 804 } 805 } 806 807if ((flags & SLF_RIGHTLEFT )&& start_dirty != -1 && clear_dirty_start != -1 ) { 808if (grid -> throttled || clear_dirty_start >=start_dirty - 5 ) { 809// cannot draw now or too small to be worth a separate "clear" event 810start_dirty = clear_dirty_start ; 811 }else { 812ui_line (grid ,row ,invalid_row ,coloff + clear_dirty_start ,coloff + clear_dirty_start , 813coloff + clear_end ,clear_attr ,flags & SLF_WRAP ); 814 } 815clear_end = end_dirty ; 816 }else { 817if (start_dirty == -1 ) {// clear only 818start_dirty = clear_dirty_start ; 819end_dirty = clear_dirty_start ; 820 }else if (clear_end < end_dirty ) {// put only 821clear_end = end_dirty ; 822 }else { 823end_dirty = endcol ; 824 } 825 } 826 827if (clear_end > start_dirty ) { 828if (!grid -> throttled ) { 829ui_line (grid ,row ,invalid_row ,coloff + start_dirty ,coloff + end_dirty ,coloff + clear_end , 830clear_attr ,flags & SLF_WRAP ); 831 }else if (grid -> dirty_col ) { 832// TODO(bfredl): really get rid of the extra pseudo terminal in message.c 833// by using a linebuf_char copy for "throttled message line" 834if (clear_end > grid -> dirty_col [row ]) { 835grid -> dirty_col [row ]= clear_end ; 836 } 837 } 838 } 839} 840 841void grid_alloc (ScreenGrid * grid ,int rows ,int columns ,bool copy ,bool valid ) 842{ 843int new_row ; 844ScreenGrid ngrid = * grid ; 845assert (rows >=0 && columns >=0 ); 846size_t ncells = (size_t )rows * (size_t )columns ; 847ngrid .chars = xmalloc (ncells * sizeof (schar_T )); 848ngrid .attrs = xmalloc (ncells * sizeof (sattr_T )); 849ngrid .vcols = xmalloc (ncells * sizeof (colnr_T )); 850memset (ngrid .vcols ,-1 ,ncells * sizeof (colnr_T )); 851ngrid .line_offset = xmalloc ((size_t )rows * sizeof (* ngrid .line_offset )); 852 853ngrid .rows = rows ; 854ngrid .cols = columns ; 855 856for (new_row = 0 ;new_row < ngrid .rows ;new_row ++ ) { 857ngrid .line_offset [new_row ]= (size_t )new_row * (size_t )ngrid .cols ; 858 859grid_clear_line (& ngrid ,ngrid .line_offset [new_row ],columns ,valid ); 860 861if (copy ) { 862// If the screen is not going to be cleared, copy as much as 863// possible from the old screen to the new one and clear the rest 864// (used when resizing the window at the "--more--" prompt or when 865// executing an external command, for the GUI). 866if (new_row < grid -> rows && grid -> chars != NULL ) { 867int len = MIN (grid -> cols ,ngrid .cols ); 868memmove (ngrid .chars + ngrid .line_offset [new_row ], 869grid -> chars + grid -> line_offset [new_row ], 870 (size_t )len * sizeof (schar_T )); 871memmove (ngrid .attrs + ngrid .line_offset [new_row ], 872grid -> attrs + grid -> line_offset [new_row ], 873 (size_t )len * sizeof (sattr_T )); 874memmove (ngrid .vcols + ngrid .line_offset [new_row ], 875grid -> vcols + grid -> line_offset [new_row ], 876 (size_t )len * sizeof (colnr_T )); 877 } 878 } 879 } 880grid_free (grid ); 881* grid = ngrid ; 882 883// Share a single scratch buffer for all grids, by 884// ensuring it is as wide as the widest grid. 885if (linebuf_size < (size_t )columns ) { 886xfree (linebuf_char ); 887xfree (linebuf_attr ); 888xfree (linebuf_vcol ); 889xfree (linebuf_scratch ); 890linebuf_char = xmalloc ((size_t )columns * sizeof (schar_T )); 891linebuf_attr = xmalloc ((size_t )columns * sizeof (sattr_T )); 892linebuf_vcol = xmalloc ((size_t )columns * sizeof (colnr_T )); 893linebuf_scratch = xmalloc ((size_t )columns * sizeof (sscratch_T )); 894linebuf_size = (size_t )columns ; 895 } 896} 897 898void grid_free (ScreenGrid * grid ) 899{ 900xfree (grid -> chars ); 901xfree (grid -> attrs ); 902xfree (grid -> vcols ); 903xfree (grid -> line_offset ); 904 905grid -> chars = NULL ; 906grid -> attrs = NULL ; 907grid -> vcols = NULL ; 908grid -> line_offset = NULL ; 909} 910 911#ifdef EXITFREE 912/// Doesn't allow reinit, so must only be called by free_all_mem! 913void grid_free_all_mem (void ) 914{ 915grid_free (& default_grid ); 916grid_free (& msg_grid ); 917XFREE_CLEAR (msg_grid .dirty_col ); 918xfree (linebuf_char ); 919xfree (linebuf_attr ); 920xfree (linebuf_vcol ); 921xfree (linebuf_scratch ); 922set_destroy (glyph ,& glyph_cache ); 923} 924#endif 925 926/// (Re)allocates a window grid if size changed while in ext_multigrid mode. 927/// Updates size, offsets and handle for the grid regardless. 928/// 929/// If "doclear" is true, don't try to copy from the old grid rather clear the 930/// resized grid. 931void win_grid_alloc (win_T * wp ) 932{ 933GridView * grid = & wp -> w_grid ; 934ScreenGrid * grid_allocated = & wp -> w_grid_alloc ; 935 936int total_rows = wp -> w_height_outer ; 937int total_cols = wp -> w_width_outer ; 938 939bool want_allocation = ui_has (kUIMultigrid )|| wp -> w_floating ; 940bool has_allocation = (grid_allocated -> chars != NULL ); 941 942if (wp -> w_view_height > wp -> w_lines_size ) { 943wp -> w_lines_valid = 0 ; 944xfree (wp -> w_lines ); 945wp -> w_lines = xcalloc ((size_t )wp -> w_view_height + 1 ,sizeof (wline_T )); 946wp -> w_lines_size = wp -> w_view_height ; 947 } 948 949bool was_resized = false; 950if (want_allocation && (!has_allocation 951|| grid_allocated -> rows != total_rows 952|| grid_allocated -> cols != total_cols )) { 953grid_alloc (grid_allocated ,total_rows ,total_cols ,wp -> w_grid_alloc .valid , false); 954grid_allocated -> valid = true; 955if (wp -> w_floating && wp -> w_config .border ) { 956wp -> w_redr_border = true; 957 } 958was_resized = true; 959 }else if (!want_allocation && has_allocation ) { 960// Single grid mode, all rendering will be redirected to default_grid. 961// Only keep track of the size and offset of the window. 962grid_free (grid_allocated ); 963grid_allocated -> valid = false; 964was_resized = true; 965 }else if (want_allocation && has_allocation && !wp -> w_grid_alloc .valid ) { 966grid_invalidate (grid_allocated ); 967grid_allocated -> valid = true; 968 } 969 970if (want_allocation ) { 971grid -> target = grid_allocated ; 972grid -> row_offset = wp -> w_winrow_off ; 973grid -> col_offset = wp -> w_wincol_off ; 974 }else { 975grid -> target = & default_grid ; 976grid -> row_offset = wp -> w_winrow + wp -> w_winrow_off ; 977grid -> col_offset = wp -> w_wincol + wp -> w_wincol_off ; 978 } 979 980// send grid resize event if: 981// - a grid was just resized 982// - screen_resize was called and all grid sizes must be sent 983// - the UI wants multigrid event (necessary) 984if ((resizing_screen || was_resized )&& want_allocation ) { 985ui_call_grid_resize (grid_allocated -> handle , 986grid_allocated -> cols ,grid_allocated -> rows ); 987ui_check_cursor_grid (grid_allocated -> handle ); 988 } 989} 990 991/// assign a handle to the grid. The grid need not be allocated. 992void grid_assign_handle (ScreenGrid * grid ) 993{ 994static int last_grid_handle = DEFAULT_GRID_HANDLE ; 995 996// only assign a grid handle if not already 997if (grid -> handle == 0 ) { 998grid -> handle = ++ last_grid_handle ; 999 } 1000} 1001 1002/// insert lines on the screen and move the existing lines down 1003/// 'line_count' is the number of lines to be inserted. 1004/// 'end' is the line after the scrolled part. Normally it is Rows. 1005/// 'col' is the column from with we start inserting. 1006// 1007/// 'row', 'col' and 'end' are relative to the start of the region. 1008void grid_ins_lines (ScreenGrid * grid ,int row ,int line_count ,int end ,int col ,int width ) 1009{ 1010int j ; 1011unsigned temp ; 1012 1013if (line_count <=0 ) { 1014return ; 1015 } 1016 1017// Shift line_offset[] line_count down to reflect the inserted lines. 1018// Clear the inserted lines. 1019for (int i = 0 ;i < line_count ;i ++ ) { 1020if (width != grid -> cols ) { 1021// need to copy part of a line 1022j = end - 1 - i ; 1023while ((j -= line_count ) >=row ) { 1024linecopy (grid ,j + line_count ,j ,col ,width ); 1025 } 1026j += line_count ; 1027grid_clear_line (grid ,grid -> line_offset [j ]+ (size_t )col ,width , false); 1028 }else { 1029j = end - 1 - i ; 1030temp = (unsigned )grid -> line_offset [j ]; 1031while ((j -= line_count ) >=row ) { 1032grid -> line_offset [j + line_count ]= grid -> line_offset [j ]; 1033 } 1034grid -> line_offset [j + line_count ]= temp ; 1035grid_clear_line (grid ,temp ,grid -> cols , false); 1036 } 1037 } 1038 1039if (!grid -> throttled ) { 1040ui_call_grid_scroll (grid -> handle ,row ,end ,col ,col + width ,- line_count ,0 ); 1041 } 1042} 1043 1044/// delete lines on the screen and move lines up. 1045/// 'end' is the line after the scrolled part. Normally it is Rows. 1046/// When scrolling region used 'off' is the offset from the top for the region. 1047/// 'row' and 'end' are relative to the start of the region. 1048void grid_del_lines (ScreenGrid * grid ,int row ,int line_count ,int end ,int col ,int width ) 1049{ 1050int j ; 1051unsigned temp ; 1052 1053if (line_count <=0 ) { 1054return ; 1055 } 1056 1057// Now shift line_offset[] line_count up to reflect the deleted lines. 1058// Clear the inserted lines. 1059for (int i = 0 ;i < line_count ;i ++ ) { 1060if (width != grid -> cols ) { 1061// need to copy part of a line 1062j = row + i ; 1063while ((j += line_count ) <=end - 1 ) { 1064linecopy (grid ,j - line_count ,j ,col ,width ); 1065 } 1066j -= line_count ; 1067grid_clear_line (grid ,grid -> line_offset [j ]+ (size_t )col ,width , false); 1068 }else { 1069// whole width, moving the line pointers is faster 1070j = row + i ; 1071temp = (unsigned )grid -> line_offset [j ]; 1072while ((j += line_count ) <=end - 1 ) { 1073grid -> line_offset [j - line_count ]= grid -> line_offset [j ]; 1074 } 1075grid -> line_offset [j - line_count ]= temp ; 1076grid_clear_line (grid ,temp ,grid -> cols , false); 1077 } 1078 } 1079 1080if (!grid -> throttled ) { 1081ui_call_grid_scroll (grid -> handle ,row ,end ,col ,col + width ,line_count ,0 ); 1082 } 1083} 1084 1085/// @param overflow Number of cells to skip. 1086static void grid_draw_bordertext (VirtText vt ,int col ,int winbl ,const int * hl_attr , 1087int win_hl_attr_normal ,BorderTextType bt ,int overflow ) 1088{ 1089int default_attr = hl_attr [bt == kBorderTextTitle ?HLF_BTITLE :HLF_BFOOTER ]; 1090default_attr = hl_apply_winblend (winbl ,default_attr ); 1091default_attr = hl_combine_attr (win_hl_attr_normal ,default_attr ); 1092 1093if (overflow > 0 ) { 1094grid_line_puts (1 ,"<" ,-1 ,default_attr ); 1095col += 1 ; 1096overflow += 1 ; 1097 } 1098 1099for (size_t i = 0 ;i < kv_size (vt );) { 1100int attr = -1 ; 1101char * text = next_virt_text_chunk (vt ,& i ,& attr ); 1102if (text == NULL ) { 1103break ; 1104 } 1105// Skip characters from the beginning when title overflows available width. 1106if (overflow > 0 ) { 1107int cells = (int )mb_string2cells (text ); 1108// Skip entire chunk if overflow is larger than chunk width. 1109if (overflow >=cells ) { 1110overflow -= cells ; 1111continue ; 1112 } 1113// Skip partial characters within the chunk. 1114char * p = text ; 1115while (* p && overflow > 0 ) { 1116overflow -= utf_ptr2cells (p ); 1117p += utfc_ptr2len (p ); 1118 } 1119text = p ; 1120 } 1121if (attr == -1 ) {// No highlight specified. 1122attr = default_attr ; 1123 }else { 1124attr = hl_apply_winblend (winbl ,attr ); 1125attr = hl_combine_attr (win_hl_attr_normal ,attr ); 1126 } 1127col += grid_line_puts (col ,text ,-1 ,attr ); 1128 } 1129} 1130 1131static int get_bordertext_col (int total_col ,int text_width ,AlignTextPos align ) 1132{ 1133switch (align ) { 1134case kAlignLeft : 1135return 1 ; 1136case kAlignCenter : 1137return MAX ((total_col - text_width ) /2 + 1 ,1 ); 1138case kAlignRight : 1139return MAX (total_col - text_width + 1 ,1 ); 1140 } 1141UNREACHABLE ; 1142} 1143 1144/// draw border on floating window grid 1145void grid_draw_border (ScreenGrid * grid ,WinConfig * config ,int * adj ,int winbl ,int * hl_attr , 1146int win_hl_attr_normal ) 1147{ 1148int default_adj [4 ]= {1 ,1 ,1 ,1 }; 1149if (adj == NULL ) { 1150adj = default_adj ; 1151 } 1152schar_T chars [8 ]; 1153int border_attrs [8 ]; 1154if (!hl_attr ) { 1155hl_attr = hl_attr_active ; 1156 } 1157 1158for (int i = 0 ;i < 8 ;i ++ ) { 1159chars [i ]= schar_from_str (config -> border_chars [i ]); 1160border_attrs [i ]= hl_combine_attr (win_hl_attr_normal ,config -> border_attr [i ]); 1161 } 1162 1163int irow = grid -> rows - adj [0 ]- adj [2 ]; 1164int icol = grid -> cols - adj [1 ]- adj [3 ]; 1165 1166if (adj [0 ]) { 1167screengrid_line_start (grid ,0 ,0 ); 1168if (adj [3 ]) { 1169grid_line_put_schar (0 ,chars [0 ],border_attrs [0 ]); 1170 } 1171 1172for (int i = 0 ;i < icol ;i ++ ) { 1173grid_line_put_schar (i + adj [3 ],chars [1 ],border_attrs [1 ]); 1174 } 1175 1176if (config -> title ) { 1177int title_col = get_bordertext_col (icol ,config -> title_width ,config -> title_pos ); 1178grid_draw_bordertext (config -> title_chunks ,title_col ,winbl ,hl_attr , 1179win_hl_attr_normal ,kBorderTextTitle , 1180config -> title_width - icol ); 1181 } 1182if (adj [1 ]) { 1183grid_line_put_schar (icol + adj [3 ],chars [2 ],border_attrs [2 ]); 1184 } 1185grid_line_flush (); 1186 } 1187 1188for (int i = 0 ;i < irow ;i ++ ) { 1189if (adj [3 ]) { 1190screengrid_line_start (grid ,i + adj [0 ],0 ); 1191grid_line_put_schar (0 ,chars [7 ],border_attrs [7 ]); 1192grid_line_flush (); 1193 } 1194if (adj [1 ]) { 1195int ic = (i == 0 && !adj [0 ]&& chars [2 ]) ?2 :3 ; 1196screengrid_line_start (grid ,i + adj [0 ],0 ); 1197grid_line_put_schar (icol + adj [3 ],chars [ic ],border_attrs [ic ]); 1198grid_line_flush (); 1199 } 1200 } 1201 1202if (adj [2 ]) { 1203screengrid_line_start (grid ,irow + adj [0 ],0 ); 1204if (adj [3 ]) { 1205grid_line_put_schar (0 ,chars [6 ],border_attrs [6 ]); 1206 } 1207 1208for (int i = 0 ;i < icol ;i ++ ) { 1209int ic = (i == 0 && !adj [3 ]&& chars [6 ]) ?6 :5 ; 1210grid_line_put_schar (i + adj [3 ],chars [ic ],border_attrs [ic ]); 1211 } 1212 1213if (config -> footer ) { 1214int footer_col = get_bordertext_col (icol ,config -> footer_width ,config -> footer_pos ); 1215grid_draw_bordertext (config -> footer_chunks ,footer_col ,winbl ,hl_attr ,win_hl_attr_normal , 1216kBorderTextFooter ,config -> footer_width - icol ); 1217 } 1218if (adj [1 ]) { 1219grid_line_put_schar (icol + adj [3 ],chars [4 ],border_attrs [4 ]); 1220 } 1221grid_line_flush (); 1222 } 1223} 1224 1225static void linecopy (ScreenGrid * grid ,int to ,int from ,int col ,int width ) 1226{ 1227unsigned off_to = (unsigned )(grid -> line_offset [to ]+ (size_t )col ); 1228unsigned off_from = (unsigned )(grid -> line_offset [from ]+ (size_t )col ); 1229 1230memmove (grid -> chars + off_to ,grid -> chars + off_from , (size_t )width * sizeof (schar_T )); 1231memmove (grid -> attrs + off_to ,grid -> attrs + off_from , (size_t )width * sizeof (sattr_T )); 1232memmove (grid -> vcols + off_to ,grid -> vcols + off_from , (size_t )width * sizeof (colnr_T )); 1233} 1234 1235win_T * get_win_by_grid_handle (handle_T handle ) 1236{ 1237FOR_ALL_WINDOWS_IN_TAB (wp ,curtab ) { 1238if (wp -> w_grid_alloc .handle == handle ) { 1239return wp ; 1240 } 1241 } 1242return NULL ; 1243} 1244 1245/// Put a unicode character in a screen cell. 1246schar_T schar_from_char (int c ) 1247{ 1248schar_T sc = 0 ; 1249if (c >=0x200000 ) { 1250// TODO(bfredl): this must NEVER happen, even if the file contained overlong sequences 1251c = 0xFFFD ; 1252 } 1253utf_char2bytes (c , (char * )& sc ); 1254return sc ; 1255}