robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

robinmerge: 1dc9728daa1d8a2702

master
37.8 KiB1255 linesraw
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;
69  return grid->target;
70}
71
72schar_T schar_from_str(const char *str)
73{
74  if (str == NULL) {
75    return 0;
76  }
77  return 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{
85  assert(len < MAX_SCHAR_SIZE);
86  if (len <= 4) {
87    schar_T sc = 0;
88    memcpy((char *)&sc, buf, len);
89    return sc;
90  } else {
91    String str = { .data = (char *)buf, .size = len };
92
93    MHPutStatus status;
94    uint32_t idx = set_put_idx(glyph, &glyph_cache, str, &status);
95    assert(idx < 0xFFFFFF);
96#ifdef ORDER_BIG_ENDIAN
97    return idx + ((uint32_t)0xFF << 24);
98#else
99    return 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
114  if (glyph_cache.h.n_keys > (1<<21)) {
115    schar_cache_clear();
116    return true;
117  }
118  return false;
119}
120
121void schar_cache_clear(void)
122{
123  decor_check_invalid_glyphs();
124  set_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.
129  if (check_chars_options()) {
130    abort();
131  }
132}
133
134bool schar_high(schar_T sc)
135{
136#ifdef ORDER_BIG_ENDIAN
137  return ((sc & 0xFF000000) == 0xFF000000);
138#else
139  return ((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{
152  size_t len = schar_get_adv(&buf_out, sc);
153  *buf_out = NUL;
154  return len;
155}
156
157/// advance buf_out. do NOT set final NUL
158size_t schar_get_adv(char **buf_out, schar_T sc)
159{
160  size_t len;
161  if (schar_high(sc)) {
162    uint32_t idx = schar_idx(sc);
163    assert(idx < glyph_cache.h.n_keys);
164    len = strlen(&glyph_cache.keys[idx]);
165    memcpy(*buf_out, &glyph_cache.keys[idx], len);
166  } else {
167    len = strnlen((char *)&sc, 4);
168    memcpy(*buf_out, (char *)&sc, len);
169  }
170  *buf_out += len;
171  return len;
172}
173
174size_t schar_len(schar_T sc)
175{
176  if (schar_high(sc)) {
177    uint32_t idx = schar_idx(sc);
178    assert(idx < glyph_cache.h.n_keys);
179    return strlen(&glyph_cache.keys[idx]);
180  } else {
181    return strnlen((char *)&sc, 4);
182  }
183}
184
185int schar_cells(schar_T sc)
186{
187  // hot path
188#ifdef ORDER_BIG_ENDIAN
189  if (!(sc & 0x80FFFFFF)) {
190    return 1;
191  }
192#else
193  if (sc < 0x80) {
194    return 1;
195  }
196#endif
197
198  char sc_buf[MAX_SCHAR_SIZE];
199  schar_get(sc_buf, sc);
200  return 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{
206  assert(!(schar_high(sc) && schar_idx(sc) >= glyph_cache.h.n_keys));
207  return schar_high(sc) ? glyph_cache.keys[schar_idx(sc)] : *(char *)&sc;
208}
209
210int schar_get_first_codepoint(schar_T sc)
211{
212  char sc_buf[MAX_SCHAR_SIZE];
213  schar_get(sc_buf, sc);
214  return 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
221  return (!(sc & 0x80FFFFFF)) ? *(char *)&sc : NUL;
222#else
223  return (sc < 0x80) ? (char)sc : NUL;
224#endif
225}
226
227static bool schar_in_arabic_block(schar_T sc)
228{
229  char first_byte = schar_get_first_byte(sc);
230  return ((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{
236  char sc_buf[MAX_SCHAR_SIZE];
237  schar_get(sc_buf, sc);
238
239  *c0 = utf_ptr2char(sc_buf);
240  int len = utf_ptr2len(sc_buf);
241  if (*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{
250  int i = 0;
251
252  for (i = 0; i < cols; i++) {
253    // quickly skip over non-arabic text
254    if (schar_in_arabic_block(buf[i])) {
255      break;
256    }
257  }
258
259  if (i == cols) {
260    return;
261  }
262
263  int c0prev = 0;
264  int c0, c1;
265  schar_get_first_two_codepoints(buf[i], &c0, &c1);
266
267  for (; i < cols; i++) {
268    int c0next, c1next;
269    schar_get_first_two_codepoints(i + 1 < cols ? buf[i + 1] : 0, &c0next, &c1next);
270
271    if (!ARABIC_CHAR(c0)) {
272      goto next;
273    }
274
275    int c1new = c1;
276    int c0new = arabic_shape(c0, &c1new, c0next, c1next, c0prev);
277
278    if (c0new == c0 && c1new == c1) {
279      goto next;  // unchanged
280    }
281
282    char scbuf[MAX_SCHAR_SIZE];
283    schar_get(scbuf, buf[i]);
284
285    char scbuf_new[MAX_SCHAR_SIZE];
286    size_t len = (size_t)utf_char2bytes(c0new, scbuf_new);
287    if (c1new) {
288      len += (size_t)utf_char2bytes(c1new, scbuf_new + len);
289    }
290
291    int off = utf_char2len(c0) + (c1 ? utf_char2len(c1) : 0);
292    size_t rest = strlen(scbuf + off);
293    if (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)
297      rest -= (size_t)utf_cp_bounds(scbuf + off, scbuf + off + rest - 1).begin_off + 1;
298    }
299    memcpy(scbuf_new + len, scbuf + off, rest);
300    buf[i] = schar_from_buf(scbuf_new, len + rest);
301
302next:
303    c0prev = c0;
304    c0 = c0next;
305    c1 = 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{
313  for (int col = 0; col < width; col++) {
314    grid->chars[off + (size_t)col] = schar_from_ascii(' ');
315  }
316  int fill = valid ? 0 : -1;
317  memset(grid->attrs + off, fill, (size_t)width * sizeof(sattr_T));
318  memset(grid->vcols + off, -1, (size_t)width * sizeof(colnr_T));
319}
320
321void grid_invalidate(ScreenGrid *grid)
322{
323  memset(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{
328  return 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
337  if (grid->chars == NULL || row >= grid->rows || col >= grid->cols) {
338    return NUL;
339  }
340
341  size_t off = grid->line_offset[row] + (size_t)col;
342  if (attrp != NULL) {
343    *attrp = grid->attrs[off];
344  }
345  return 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{
365  int col = 0;
366  ScreenGrid *grid = grid_adjust(view, &row, &col);
367  screengrid_line_start(grid, row, col);
368}
369
370void screengrid_line_start(ScreenGrid *grid, int row, int col)
371{
372  grid_line_maxcol = grid->cols;
373  assert(grid_line_grid == NULL);
374  grid_line_row = row;
375  grid_line_grid = grid;
376  grid_line_coloff = col;
377  grid_line_first = (int)linebuf_size;
378  grid_line_maxcol = MIN(grid_line_maxcol, grid->cols - grid_line_coloff);
379  grid_line_last = 0;
380  grid_line_clear_to = 0;
381  grid_line_bg_attr = 0;
382  grid_line_clear_attr = 0;
383  grid_line_flags = 0;
384
385  assert((size_t)grid_line_maxcol <= linebuf_size);
386
387  if (full_screen && (rdb_flags & kOptRdbFlagInvalid)) {
388    assert(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.
391    memset(linebuf_char, 0xFF, sizeof(schar_T) * linebuf_size);
392    memset(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{
403  if (col < grid_line_maxcol) {
404    col += grid_line_coloff;
405    size_t off = grid_line_grid->line_offset[grid_line_row] + (size_t)col;
406    if (attr != NULL) {
407      *attr = grid_line_grid->attrs[off];
408    }
409    return grid_line_grid->chars[off];
410  } else {
411    // NUL is a very special value (right-half of double width), space is True Neutral™
412    return schar_from_ascii(' ');
413  }
414}
415
416void grid_line_put_schar(int col, schar_T schar, int attr)
417{
418  assert(grid_line_grid);
419  if (col >= grid_line_maxcol) {
420    return;
421  }
422
423  linebuf_char[col] = schar;
424  linebuf_attr[col] = attr;
425
426  grid_line_first = MIN(grid_line_first, col);
427  // TODO(bfredl): Y U NO DOUBLEWIDTH?
428  grid_line_last = MAX(grid_line_last, col + 1);
429  linebuf_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{
441  const char *ptr = text;
442  int len = textlen;
443
444  assert(grid_line_grid);
445
446  int start_col = col;
447
448  const int max_col = grid_line_maxcol;
449  while (col < max_col && (len < 0 || (int)(ptr - text) < len) && *ptr != NUL) {
450    // check if this is the first byte of a multibyte
451    int mbyte_blen;
452    if (len >= 0) {
453      int maxlen = (int)((text + len) - ptr);
454      mbyte_blen = utfc_ptr2len_len(ptr, maxlen);
455      if (mbyte_blen > maxlen) {
456        mbyte_blen = 1;
457      }
458    } else {
459      mbyte_blen = utfc_ptr2len(ptr);
460    }
461    int firstc;
462    schar_T schar = utfc_ptrlen2schar(ptr, mbyte_blen, &firstc);
463    int mbyte_cells = utf_ptr2cells_len(ptr, mbyte_blen);
464    if (mbyte_cells > 2 || schar == 0) {
465      mbyte_cells = 1;
466      schar = schar_from_char(0xFFFD);
467    }
468
469    if (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.
472      schar = schar_from_ascii('>');
473      mbyte_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 '>'.
478    if (ptr == text && col > grid_line_first && col < grid_line_last
479        && linebuf_char[col] == 0) {
480      linebuf_char[col - 1] = schar_from_ascii('>');
481    }
482
483    linebuf_char[col] = schar;
484    linebuf_attr[col] = attr;
485    linebuf_vcol[col] = -1;
486    if (mbyte_cells == 2) {
487      linebuf_char[col + 1] = 0;
488      linebuf_attr[col + 1] = attr;
489      linebuf_vcol[col + 1] = -1;
490    }
491
492    col += mbyte_cells;
493    ptr += mbyte_blen;
494  }
495
496  if (col > start_col) {
497    grid_line_first = MIN(grid_line_first, start_col);
498    grid_line_last = MAX(grid_line_last, col);
499  }
500
501  return col - start_col;
502}
503
504int grid_line_fill(int start_col, int end_col, schar_T sc, int attr)
505{
506  end_col = MIN(end_col, grid_line_maxcol);
507  if (start_col >= end_col) {
508    return end_col;
509  }
510
511  for (int col = start_col; col < end_col; col++) {
512    linebuf_char[col] = sc;
513    linebuf_attr[col] = attr;
514    linebuf_vcol[col] = -1;
515  }
516
517  grid_line_first = MIN(grid_line_first, start_col);
518  grid_line_last = MAX(grid_line_last, end_col);
519  return 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{
526  if (grid_line_first > start_col) {
527    grid_line_first = start_col;
528    grid_line_last = start_col;
529  }
530  grid_line_clear_to = end_col;
531  grid_line_bg_attr = bg_attr;
532  grid_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{
538  ui_grid_cursor_goto(grid_line_grid->handle, grid_line_row, col);
539}
540
541void grid_line_mirror(int width)
542{
543  grid_line_clear_to = MAX(grid_line_last, grid_line_clear_to);
544  if (grid_line_first >= grid_line_clear_to) {
545    return;
546  }
547  linebuf_mirror(&grid_line_first, &grid_line_last, &grid_line_clear_to, width);
548  grid_line_flags |= SLF_RIGHTLEFT;
549}
550
551void linebuf_mirror(int *firstp, int *lastp, int *clearp, int width)
552{
553  int first = *firstp;
554  int last = *lastp;
555
556  size_t n = (size_t)(last - first);
557  int mirror = width - 1;  // Mirrors are more fun than television.
558  schar_T *scratch_char = (schar_T *)linebuf_scratch;
559  memcpy(scratch_char + first, linebuf_char + first, n * sizeof(schar_T));
560  for (int col = first; col < last; col++) {
561    int rev = mirror - col;
562    if (col + 1 < last && scratch_char[col + 1] == 0) {
563      linebuf_char[rev - 1] = scratch_char[col];
564      linebuf_char[rev] = 0;
565      col++;
566    } else {
567      linebuf_char[rev] = scratch_char[col];
568    }
569  }
570
571  // for attr and vcol: assumes doublewidth chars are self-consistent
572  sattr_T *scratch_attr = (sattr_T *)linebuf_scratch;
573  memcpy(scratch_attr + first, linebuf_attr + first, n * sizeof(sattr_T));
574  for (int col = first; col < last; col++) {
575    linebuf_attr[mirror - col] = scratch_attr[col];
576  }
577
578  colnr_T *scratch_vcol = (colnr_T *)linebuf_scratch;
579  memcpy(scratch_vcol + first, linebuf_vcol + first, n * sizeof(colnr_T));
580  for (int col = first; col < last; col++) {
581    linebuf_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{
592  ScreenGrid *grid = grid_line_grid;
593  grid_line_grid = NULL;
594  grid_line_clear_to = MAX(grid_line_last, grid_line_clear_to);
595  assert(grid_line_clear_to <= grid_line_maxcol);
596  if (grid_line_first >= grid_line_clear_to) {
597    return;
598  }
599
600  grid_put_linebuf(grid, grid_line_row, grid_line_coloff, grid_line_first, grid_line_last,
601                   grid_line_clear_to, grid_line_bg_attr, grid_line_clear_attr, -1,
602                   grid_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{
610  if (grid_line_row < 0 || grid_line_row >= grid_line_grid->rows) {
611    if (rdb_flags & kOptRdbFlagInvalid) {
612      abort();
613    } else {
614      grid_line_grid = NULL;
615      return;
616    }
617  }
618  grid_line_flush();
619}
620
621void grid_clear(GridView *grid, int start_row, int end_row, int start_col, int end_col, int attr)
622{
623  for (int row = start_row; row < end_row; row++) {
624    grid_line_start(grid, row);
625    end_col = MIN(end_col, grid_line_maxcol);
626    if (grid_line_row >= grid_line_grid->rows || start_col >= end_col) {
627      grid_line_grid = NULL;  // TODO(bfredl): make callers behave instead
628      return;
629    }
630    grid_line_clear_end(start_col, end_col, attr, 0);
631    grid_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{
642  return (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,
667                      int bg_attr, int clear_attr, colnr_T last_vcol, int flags)
668{
669  bool redraw_next;                         // redraw_this for next character
670  bool clear_next = false;
671  assert(0 <= row && row < grid->rows);
672  // TODO(bfredl): check all callsites and eliminate
673  // Check for illegal col, just in case
674  if (endcol > grid->cols) {
675    endcol = grid->cols;
676  }
677
678  // Safety check. Avoids clang warnings down the call stack.
679  if (grid->chars == NULL || row >= grid->rows || coloff >= grid->cols) {
680    DLOG("invalid state, skipped");
681    return;
682  }
683
684  bool invalid_row = grid != &default_grid && grid_invalid_row(grid, row) && col == 0;
685  size_t off_to = grid->line_offset[row] + (size_t)coloff;
686  const 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 '>'.
690  if (col > 0 && grid->chars[off_to + (size_t)col] == 0) {
691    linebuf_char[col - 1] = schar_from_ascii('>');
692    linebuf_attr[col - 1] = grid->attrs[off_to + (size_t)col - 1];
693    col--;
694  }
695
696  int clear_start = endcol;
697  if (flags & SLF_RIGHTLEFT) {
698    clear_start = col;
699    col = endcol;
700    endcol = clear_width;
701    clear_width = col;
702  }
703
704  if (p_arshape && !p_tbidi && endcol > col) {
705    line_do_arabic_shape(linebuf_char + col, endcol - col);
706  }
707
708  if (bg_attr) {
709    for (int c = col; c < endcol; c++) {
710      linebuf_attr[c] = hl_combine_attr(bg_attr, linebuf_attr[c]);
711    }
712  }
713
714  redraw_next = grid_char_needs_redraw(grid, col, off_to + (size_t)col, endcol - col);
715
716  int start_dirty = -1;
717  int end_dirty = 0;
718
719  while (col < endcol) {
720    int char_cells = 1;  // 1: normal char
721                         // 2: occupies two display cells
722    if (col + 1 < endcol && linebuf_char[col + 1] == 0) {
723      char_cells = 2;
724    }
725    bool redraw_this = redraw_next;  // Does character need redraw?
726    size_t off = off_to + (size_t)col;
727    redraw_next = grid_char_needs_redraw(grid, col + char_cells,
728                                         off + (size_t)char_cells,
729                                         endcol - col - char_cells);
730
731    if (redraw_this) {
732      if (start_dirty == -1) {
733        start_dirty = col;
734      }
735      end_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
741      if (col + char_cells == endcol && off + (size_t)char_cells < max_off_to
742          && grid->chars[off + (size_t)char_cells] == NUL) {
743        clear_next = true;
744      }
745
746      grid->chars[off] = linebuf_char[col];
747      if (char_cells == 2) {
748        grid->chars[off + 1] = linebuf_char[col + 1];
749      }
750
751      grid->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.
754      if (char_cells == 2) {
755        grid->attrs[off + 1] = linebuf_attr[col];
756      }
757    }
758
759    grid->vcols[off] = linebuf_vcol[col];
760    if (char_cells == 2) {
761      grid->vcols[off + 1] = linebuf_vcol[col + 1];
762    }
763
764    col += char_cells;
765  }
766
767  if (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.
770    grid->chars[off_to + (size_t)col] = schar_from_ascii(' ');
771    end_dirty++;
772  }
773
774  // When clearing the left half of a double-wide char also clear the right half.
775  if (off_to + (size_t)clear_width < max_off_to
776      && grid->chars[off_to + (size_t)clear_width] == 0) {
777    clear_width++;
778  }
779
780  int clear_dirty_start = -1, clear_end = -1;
781  if (flags & SLF_RIGHTLEFT) {
782    for (col = clear_width - 1; col >= clear_start; col--) {
783      size_t off = off_to + (size_t)col;
784      grid->vcols[off] = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
785    }
786  }
787  clear_attr = hl_combine_attr(bg_attr, clear_attr);
788  // blank out the rest of the line
789  // TODO(bfredl): we could cache winline widths
790  for (col = clear_start; col < clear_width; col++) {
791    size_t off = off_to + (size_t)col;
792    if (grid->chars[off] != schar_from_ascii(' ')
793        || grid->attrs[off] != clear_attr
794        || rdb_flags & kOptRdbFlagNodelta) {
795      grid->chars[off] = schar_from_ascii(' ');
796      grid->attrs[off] = clear_attr;
797      if (clear_dirty_start == -1) {
798        clear_dirty_start = col;
799      }
800      clear_end = col + 1;
801    }
802    if (!(flags & SLF_RIGHTLEFT)) {
803      grid->vcols[off] = (flags & SLF_INC_VCOL) ? ++last_vcol : last_vcol;
804    }
805  }
806
807  if ((flags & SLF_RIGHTLEFT) && start_dirty != -1 && clear_dirty_start != -1) {
808    if (grid->throttled || clear_dirty_start >= start_dirty - 5) {
809      // cannot draw now or too small to be worth a separate "clear" event
810      start_dirty = clear_dirty_start;
811    } else {
812      ui_line(grid, row, invalid_row, coloff + clear_dirty_start, coloff + clear_dirty_start,
813              coloff + clear_end, clear_attr, flags & SLF_WRAP);
814    }
815    clear_end = end_dirty;
816  } else {
817    if (start_dirty == -1) {  // clear only
818      start_dirty = clear_dirty_start;
819      end_dirty = clear_dirty_start;
820    } else if (clear_end < end_dirty) {  // put only
821      clear_end = end_dirty;
822    } else {
823      end_dirty = endcol;
824    }
825  }
826
827  if (clear_end > start_dirty) {
828    if (!grid->throttled) {
829      ui_line(grid, row, invalid_row, coloff + start_dirty, coloff + end_dirty, coloff + clear_end,
830              clear_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"
834      if (clear_end > grid->dirty_col[row]) {
835        grid->dirty_col[row] = clear_end;
836      }
837    }
838  }
839}
840
841void grid_alloc(ScreenGrid *grid, int rows, int columns, bool copy, bool valid)
842{
843  int new_row;
844  ScreenGrid ngrid = *grid;
845  assert(rows >= 0 && columns >= 0);
846  size_t ncells = (size_t)rows * (size_t)columns;
847  ngrid.chars = xmalloc(ncells * sizeof(schar_T));
848  ngrid.attrs = xmalloc(ncells * sizeof(sattr_T));
849  ngrid.vcols = xmalloc(ncells * sizeof(colnr_T));
850  memset(ngrid.vcols, -1, ncells * sizeof(colnr_T));
851  ngrid.line_offset = xmalloc((size_t)rows * sizeof(*ngrid.line_offset));
852
853  ngrid.rows = rows;
854  ngrid.cols = columns;
855
856  for (new_row = 0; new_row < ngrid.rows; new_row++) {
857    ngrid.line_offset[new_row] = (size_t)new_row * (size_t)ngrid.cols;
858
859    grid_clear_line(&ngrid, ngrid.line_offset[new_row], columns, valid);
860
861    if (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).
866      if (new_row < grid->rows && grid->chars != NULL) {
867        int len = MIN(grid->cols, ngrid.cols);
868        memmove(ngrid.chars + ngrid.line_offset[new_row],
869                grid->chars + grid->line_offset[new_row],
870                (size_t)len * sizeof(schar_T));
871        memmove(ngrid.attrs + ngrid.line_offset[new_row],
872                grid->attrs + grid->line_offset[new_row],
873                (size_t)len * sizeof(sattr_T));
874        memmove(ngrid.vcols + ngrid.line_offset[new_row],
875                grid->vcols + grid->line_offset[new_row],
876                (size_t)len * sizeof(colnr_T));
877      }
878    }
879  }
880  grid_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.
885  if (linebuf_size < (size_t)columns) {
886    xfree(linebuf_char);
887    xfree(linebuf_attr);
888    xfree(linebuf_vcol);
889    xfree(linebuf_scratch);
890    linebuf_char = xmalloc((size_t)columns * sizeof(schar_T));
891    linebuf_attr = xmalloc((size_t)columns * sizeof(sattr_T));
892    linebuf_vcol = xmalloc((size_t)columns * sizeof(colnr_T));
893    linebuf_scratch = xmalloc((size_t)columns * sizeof(sscratch_T));
894    linebuf_size = (size_t)columns;
895  }
896}
897
898void grid_free(ScreenGrid *grid)
899{
900  xfree(grid->chars);
901  xfree(grid->attrs);
902  xfree(grid->vcols);
903  xfree(grid->line_offset);
904
905  grid->chars = NULL;
906  grid->attrs = NULL;
907  grid->vcols = NULL;
908  grid->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{
915  grid_free(&default_grid);
916  grid_free(&msg_grid);
917  XFREE_CLEAR(msg_grid.dirty_col);
918  xfree(linebuf_char);
919  xfree(linebuf_attr);
920  xfree(linebuf_vcol);
921  xfree(linebuf_scratch);
922  set_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{
933  GridView *grid = &wp->w_grid;
934  ScreenGrid *grid_allocated = &wp->w_grid_alloc;
935
936  int total_rows = wp->w_height_outer;
937  int total_cols = wp->w_width_outer;
938
939  bool want_allocation = ui_has(kUIMultigrid) || wp->w_floating;
940  bool has_allocation = (grid_allocated->chars != NULL);
941
942  if (wp->w_view_height > wp->w_lines_size) {
943    wp->w_lines_valid = 0;
944    xfree(wp->w_lines);
945    wp->w_lines = xcalloc((size_t)wp->w_view_height + 1, sizeof(wline_T));
946    wp->w_lines_size = wp->w_view_height;
947  }
948
949  bool was_resized = false;
950  if (want_allocation && (!has_allocation
951                          || grid_allocated->rows != total_rows
952                          || grid_allocated->cols != total_cols)) {
953    grid_alloc(grid_allocated, total_rows, total_cols, wp->w_grid_alloc.valid, false);
954    grid_allocated->valid = true;
955    if (wp->w_floating && wp->w_config.border) {
956      wp->w_redr_border = true;
957    }
958    was_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.
962    grid_free(grid_allocated);
963    grid_allocated->valid = false;
964    was_resized = true;
965  } else if (want_allocation && has_allocation && !wp->w_grid_alloc.valid) {
966    grid_invalidate(grid_allocated);
967    grid_allocated->valid = true;
968  }
969
970  if (want_allocation) {
971    grid->target = grid_allocated;
972    grid->row_offset = wp->w_winrow_off;
973    grid->col_offset = wp->w_wincol_off;
974  } else {
975    grid->target = &default_grid;
976    grid->row_offset = wp->w_winrow + wp->w_winrow_off;
977    grid->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)
984  if ((resizing_screen || was_resized) && want_allocation) {
985    ui_call_grid_resize(grid_allocated->handle,
986                        grid_allocated->cols, grid_allocated->rows);
987    ui_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{
994  static int last_grid_handle = DEFAULT_GRID_HANDLE;
995
996  // only assign a grid handle if not already
997  if (grid->handle == 0) {
998    grid->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{
1010  int j;
1011  unsigned temp;
1012
1013  if (line_count <= 0) {
1014    return;
1015  }
1016
1017  // Shift line_offset[] line_count down to reflect the inserted lines.
1018  // Clear the inserted lines.
1019  for (int i = 0; i < line_count; i++) {
1020    if (width != grid->cols) {
1021      // need to copy part of a line
1022      j = end - 1 - i;
1023      while ((j -= line_count) >= row) {
1024        linecopy(grid, j + line_count, j, col, width);
1025      }
1026      j += line_count;
1027      grid_clear_line(grid, grid->line_offset[j] + (size_t)col, width, false);
1028    } else {
1029      j = end - 1 - i;
1030      temp = (unsigned)grid->line_offset[j];
1031      while ((j -= line_count) >= row) {
1032        grid->line_offset[j + line_count] = grid->line_offset[j];
1033      }
1034      grid->line_offset[j + line_count] = temp;
1035      grid_clear_line(grid, temp, grid->cols, false);
1036    }
1037  }
1038
1039  if (!grid->throttled) {
1040    ui_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{
1050  int j;
1051  unsigned temp;
1052
1053  if (line_count <= 0) {
1054    return;
1055  }
1056
1057  // Now shift line_offset[] line_count up to reflect the deleted lines.
1058  // Clear the inserted lines.
1059  for (int i = 0; i < line_count; i++) {
1060    if (width != grid->cols) {
1061      // need to copy part of a line
1062      j = row + i;
1063      while ((j += line_count) <= end - 1) {
1064        linecopy(grid, j - line_count, j, col, width);
1065      }
1066      j -= line_count;
1067      grid_clear_line(grid, grid->line_offset[j] + (size_t)col, width, false);
1068    } else {
1069      // whole width, moving the line pointers is faster
1070      j = row + i;
1071      temp = (unsigned)grid->line_offset[j];
1072      while ((j += line_count) <= end - 1) {
1073        grid->line_offset[j - line_count] = grid->line_offset[j];
1074      }
1075      grid->line_offset[j - line_count] = temp;
1076      grid_clear_line(grid, temp, grid->cols, false);
1077    }
1078  }
1079
1080  if (!grid->throttled) {
1081    ui_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,
1087                                 int win_hl_attr_normal, BorderTextType bt, int overflow)
1088{
1089  int default_attr = hl_attr[bt == kBorderTextTitle ? HLF_BTITLE : HLF_BFOOTER];
1090  default_attr = hl_apply_winblend(winbl, default_attr);
1091  default_attr = hl_combine_attr(win_hl_attr_normal, default_attr);
1092
1093  if (overflow > 0) {
1094    grid_line_puts(1, "<", -1, default_attr);
1095    col += 1;
1096    overflow += 1;
1097  }
1098
1099  for (size_t i = 0; i < kv_size(vt);) {
1100    int attr = -1;
1101    char *text = next_virt_text_chunk(vt, &i, &attr);
1102    if (text == NULL) {
1103      break;
1104    }
1105    // Skip characters from the beginning when title overflows available width.
1106    if (overflow > 0) {
1107      int cells = (int)mb_string2cells(text);
1108      // Skip entire chunk if overflow is larger than chunk width.
1109      if (overflow >= cells) {
1110        overflow -= cells;
1111        continue;
1112      }
1113      // Skip partial characters within the chunk.
1114      char *p = text;
1115      while (*p && overflow > 0) {
1116        overflow -= utf_ptr2cells(p);
1117        p += utfc_ptr2len(p);
1118      }
1119      text = p;
1120    }
1121    if (attr == -1) {  // No highlight specified.
1122      attr = default_attr;
1123    } else {
1124      attr = hl_apply_winblend(winbl, attr);
1125      attr = hl_combine_attr(win_hl_attr_normal, attr);
1126    }
1127    col += grid_line_puts(col, text, -1, attr);
1128  }
1129}
1130
1131static int get_bordertext_col(int total_col, int text_width, AlignTextPos align)
1132{
1133  switch (align) {
1134  case kAlignLeft:
1135    return 1;
1136  case kAlignCenter:
1137    return MAX((total_col - text_width) / 2 + 1, 1);
1138  case kAlignRight:
1139    return MAX(total_col - text_width + 1, 1);
1140  }
1141  UNREACHABLE;
1142}
1143
1144/// draw border on floating window grid
1145void grid_draw_border(ScreenGrid *grid, WinConfig *config, int *adj, int winbl, int *hl_attr,
1146                      int win_hl_attr_normal)
1147{
1148  int default_adj[4] = { 1, 1, 1, 1 };
1149  if (adj == NULL) {
1150    adj = default_adj;
1151  }
1152  schar_T chars[8];
1153  int border_attrs[8];
1154  if (!hl_attr) {
1155    hl_attr = hl_attr_active;
1156  }
1157
1158  for (int i = 0; i < 8; i++) {
1159    chars[i] = schar_from_str(config->border_chars[i]);
1160    border_attrs[i] = hl_combine_attr(win_hl_attr_normal, config->border_attr[i]);
1161  }
1162
1163  int irow = grid->rows - adj[0] - adj[2];
1164  int icol = grid->cols - adj[1] - adj[3];
1165
1166  if (adj[0]) {
1167    screengrid_line_start(grid, 0, 0);
1168    if (adj[3]) {
1169      grid_line_put_schar(0, chars[0], border_attrs[0]);
1170    }
1171
1172    for (int i = 0; i < icol; i++) {
1173      grid_line_put_schar(i + adj[3], chars[1], border_attrs[1]);
1174    }
1175
1176    if (config->title) {
1177      int title_col = get_bordertext_col(icol, config->title_width, config->title_pos);
1178      grid_draw_bordertext(config->title_chunks, title_col, winbl, hl_attr,
1179                           win_hl_attr_normal, kBorderTextTitle,
1180                           config->title_width - icol);
1181    }
1182    if (adj[1]) {
1183      grid_line_put_schar(icol + adj[3], chars[2], border_attrs[2]);
1184    }
1185    grid_line_flush();
1186  }
1187
1188  for (int i = 0; i < irow; i++) {
1189    if (adj[3]) {
1190      screengrid_line_start(grid, i + adj[0], 0);
1191      grid_line_put_schar(0, chars[7], border_attrs[7]);
1192      grid_line_flush();
1193    }
1194    if (adj[1]) {
1195      int ic = (i == 0 && !adj[0] && chars[2]) ? 2 : 3;
1196      screengrid_line_start(grid, i + adj[0], 0);
1197      grid_line_put_schar(icol + adj[3], chars[ic], border_attrs[ic]);
1198      grid_line_flush();
1199    }
1200  }
1201
1202  if (adj[2]) {
1203    screengrid_line_start(grid, irow + adj[0], 0);
1204    if (adj[3]) {
1205      grid_line_put_schar(0, chars[6], border_attrs[6]);
1206    }
1207
1208    for (int i = 0; i < icol; i++) {
1209      int ic = (i == 0 && !adj[3] && chars[6]) ? 6 : 5;
1210      grid_line_put_schar(i + adj[3], chars[ic], border_attrs[ic]);
1211    }
1212
1213    if (config->footer) {
1214      int footer_col = get_bordertext_col(icol, config->footer_width, config->footer_pos);
1215      grid_draw_bordertext(config->footer_chunks, footer_col, winbl, hl_attr, win_hl_attr_normal,
1216                           kBorderTextFooter, config->footer_width - icol);
1217    }
1218    if (adj[1]) {
1219      grid_line_put_schar(icol + adj[3], chars[4], border_attrs[4]);
1220    }
1221    grid_line_flush();
1222  }
1223}
1224
1225static void linecopy(ScreenGrid *grid, int to, int from, int col, int width)
1226{
1227  unsigned off_to = (unsigned)(grid->line_offset[to] + (size_t)col);
1228  unsigned off_from = (unsigned)(grid->line_offset[from] + (size_t)col);
1229
1230  memmove(grid->chars + off_to, grid->chars + off_from, (size_t)width * sizeof(schar_T));
1231  memmove(grid->attrs + off_to, grid->attrs + off_from, (size_t)width * sizeof(sattr_T));
1232  memmove(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{
1237  FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
1238    if (wp->w_grid_alloc.handle == handle) {
1239      return wp;
1240    }
1241  }
1242  return NULL;
1243}
1244
1245/// Put a unicode character in a screen cell.
1246schar_T schar_from_char(int c)
1247{
1248  schar_T sc = 0;
1249  if (c >= 0x200000) {
1250    // TODO(bfredl): this must NEVER happen, even if the file contained overlong sequences
1251    c = 0xFFFD;
1252  }
1253  utf_char2bytes(c, (char *)&sc);
1254  return sc;
1255}