-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
drawscreen.c
2904 lines (2601 loc) · 91.9 KB
/
drawscreen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// drawscreen.c: Code for updating all the windows on the screen.
// This is the top level, drawline.c is the middle and grid.c the lower level.
// update_screen() is the function that updates all windows and status lines.
// It is called from the main loop when must_redraw is non-zero. It may be
// called from other places when an immediate screen update is needed.
//
// The part of the buffer that is displayed in a window is set with:
// - w_topline (first buffer line in window)
// - w_topfill (filler lines above the first line)
// - w_leftcol (leftmost window cell in window),
// - w_skipcol (skipped window cells of first line)
//
// Commands that only move the cursor around in a window, do not need to take
// action to update the display. The main loop will check if w_topline is
// valid and update it (scroll the window) when needed.
//
// Commands that scroll a window change w_topline and must call
// check_cursor() to move the cursor into the visible part of the window, and
// call redraw_later(wp, UPD_VALID) to have the window displayed by update_screen()
// later.
//
// Commands that change text in the buffer must call changed_bytes() or
// changed_lines() to mark the area that changed and will require updating
// later. The main loop will call update_screen(), which will update each
// window that shows the changed buffer. This assumes text above the change
// can remain displayed as it is. Text after the change may need updating for
// scrolling, folding and syntax highlighting.
//
// Commands that change how a window is displayed (e.g., setting 'list') or
// invalidate the contents of a window in another way (e.g., change fold
// settings), must call redraw_later(wp, UPD_NOT_VALID) to have the whole window
// redisplayed by update_screen() later.
//
// Commands that change how a buffer is displayed (e.g., setting 'tabstop')
// must call redraw_curbuf_later(UPD_NOT_VALID) to have all the windows for the
// buffer redisplayed by update_screen() later.
//
// Commands that change highlighting and possibly cause a scroll too must call
// redraw_later(wp, UPD_SOME_VALID) to update the whole window but still use
// scrolling to avoid redrawing everything. But the length of displayed lines
// must not change, use UPD_NOT_VALID then.
//
// Commands that move the window position must call redraw_later(wp, UPD_NOT_VALID).
// TODO(neovim): should minimize redrawing by scrolling when possible.
//
// Commands that change everything (e.g., resizing the screen) must call
// redraw_all_later(UPD_NOT_VALID) or redraw_all_later(UPD_CLEAR).
//
// Things that are handled indirectly:
// - When messages scroll the screen up, msg_scrolled will be set and
// update_screen() called to redraw.
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "klib/kvec.h"
#include "nvim/api/private/defs.h"
#include "nvim/ascii_defs.h"
#include "nvim/autocmd.h"
#include "nvim/autocmd_defs.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/cmdexpand.h"
#include "nvim/cursor.h"
#include "nvim/decoration.h"
#include "nvim/decoration_defs.h"
#include "nvim/decoration_provider.h"
#include "nvim/diff.h"
#include "nvim/digraph.h"
#include "nvim/drawline.h"
#include "nvim/drawscreen.h"
#include "nvim/eval.h"
#include "nvim/ex_getln.h"
#include "nvim/fold.h"
#include "nvim/fold_defs.h"
#include "nvim/getchar.h"
#include "nvim/gettext_defs.h"
#include "nvim/globals.h"
#include "nvim/grid.h"
#include "nvim/grid_defs.h"
#include "nvim/highlight.h"
#include "nvim/highlight_defs.h"
#include "nvim/highlight_group.h"
#include "nvim/insexpand.h"
#include "nvim/marktree_defs.h"
#include "nvim/match.h"
#include "nvim/mbyte.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/normal_defs.h"
#include "nvim/option.h"
#include "nvim/option_vars.h"
#include "nvim/os/os_defs.h"
#include "nvim/plines.h"
#include "nvim/popupmenu.h"
#include "nvim/pos_defs.h"
#include "nvim/profile.h"
#include "nvim/regexp.h"
#include "nvim/search.h"
#include "nvim/spell.h"
#include "nvim/state.h"
#include "nvim/state_defs.h"
#include "nvim/statusline.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/terminal.h"
#include "nvim/types_defs.h"
#include "nvim/ui.h"
#include "nvim/ui_compositor.h"
#include "nvim/ui_defs.h"
#include "nvim/version.h"
#include "nvim/vim_defs.h"
#include "nvim/window.h"
/// corner value flags for hsep_connected and vsep_connected
typedef enum {
WC_TOP_LEFT = 0,
WC_TOP_RIGHT,
WC_BOTTOM_LEFT,
WC_BOTTOM_RIGHT,
} WindowCorner;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "drawscreen.c.generated.h"
#endif
static bool redraw_popupmenu = false;
static bool msg_grid_invalid = false;
static bool resizing_autocmd = false;
/// Check if the cursor line needs to be redrawn because of 'concealcursor'.
///
/// When cursor is moved at the same time, both lines will be redrawn regardless.
void conceal_check_cursor_line(void)
{
bool should_conceal = conceal_cursor_line(curwin);
if (curwin->w_p_cole <= 0 || conceal_cursor_used == should_conceal) {
return;
}
redrawWinline(curwin, curwin->w_cursor.lnum);
// Need to recompute cursor column, e.g., when starting Visual mode
// without concealing.
curs_columns(curwin, true);
}
/// Resize default_grid to Rows and Columns.
///
/// Allocate default_grid.chars[] and other grid arrays.
///
/// There may be some time between setting Rows and Columns and (re)allocating
/// default_grid arrays. This happens when starting up and when
/// (manually) changing the screen size. Always use default_grid.rows and
/// default_grid.cols to access items in default_grid.chars[]. Use Rows and
/// Columns for positioning text etc. where the final size of the screen is
/// needed.
///
/// @return whether resizing has been done
bool default_grid_alloc(void)
{
static bool resizing = false;
// It's possible that we produce an out-of-memory message below, which
// will cause this function to be called again. To break the loop, just
// return here.
if (resizing) {
return false;
}
resizing = true;
// Allocation of the screen buffers is done only when the size changes and
// when Rows and Columns have been set and we have started doing full
// screen stuff.
if ((default_grid.chars != NULL
&& Rows == default_grid.rows
&& Columns == default_grid.cols)
|| Rows == 0
|| Columns == 0
|| (!full_screen && default_grid.chars == NULL)) {
resizing = false;
return false;
}
// We're changing the size of the screen.
// - Allocate new arrays for default_grid
// - Move lines from the old arrays into the new arrays, clear extra
// lines (unless the screen is going to be cleared).
// - Free the old arrays.
//
// If anything fails, make grid arrays NULL, so we don't do anything!
// Continuing with the old arrays may result in a crash, because the
// size is wrong.
grid_alloc(&default_grid, Rows, Columns, true, true);
stl_clear_click_defs(tab_page_click_defs, tab_page_click_defs_size);
tab_page_click_defs = stl_alloc_click_defs(tab_page_click_defs, Columns,
&tab_page_click_defs_size);
default_grid.comp_height = Rows;
default_grid.comp_width = Columns;
default_grid.row_offset = 0;
default_grid.col_offset = 0;
default_grid.handle = DEFAULT_GRID_HANDLE;
resizing = false;
return true;
}
void screenclear(void)
{
msg_check_for_delay(false);
if (starting == NO_SCREEN || default_grid.chars == NULL) {
return;
}
// blank out the default grid
for (int i = 0; i < default_grid.rows; i++) {
grid_clear_line(&default_grid, default_grid.line_offset[i],
default_grid.cols, true);
}
ui_call_grid_clear(1); // clear the display
ui_comp_set_screen_valid(true);
ns_hl_fast = -1;
clear_cmdline = false;
mode_displayed = false;
redraw_all_later(UPD_NOT_VALID);
redraw_cmdline = true;
redraw_tabline = true;
redraw_popupmenu = true;
pum_invalidate();
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_floating) {
wp->w_redr_type = UPD_CLEAR;
}
}
if (must_redraw == UPD_CLEAR) {
must_redraw = UPD_NOT_VALID; // no need to clear again
}
compute_cmdrow();
msg_row = cmdline_row; // put cursor on last line for messages
msg_col = 0;
msg_scrolled = 0; // can't scroll back
msg_didany = false;
msg_didout = false;
if (HL_ATTR(HLF_MSG) > 0 && msg_use_grid() && msg_grid.chars) {
grid_invalidate(&msg_grid);
msg_grid_validate();
msg_grid_invalid = false;
clear_cmdline = true;
}
}
/// Set dimensions of the Nvim application "screen".
void screen_resize(int width, int height)
{
// Avoid recursiveness, can happen when setting the window size causes
// another window-changed signal.
if (updating_screen || resizing_screen) {
return;
}
if (width < 0 || height < 0) { // just checking...
return;
}
if (State == MODE_HITRETURN || State == MODE_SETWSIZE) {
// postpone the resizing
State = MODE_SETWSIZE;
return;
}
resizing_screen = true;
Rows = height;
Columns = width;
check_screensize();
int max_p_ch = Rows - min_rows() + 1;
if (!ui_has(kUIMessages) && p_ch > 0 && p_ch > max_p_ch) {
p_ch = max_p_ch ? max_p_ch : 1;
}
height = Rows;
width = Columns;
p_lines = Rows;
p_columns = Columns;
ui_call_grid_resize(1, width, height);
int retry_count = 0;
resizing_autocmd = true;
// In rare cases, autocommands may have altered Rows or Columns,
// so retry to check if we need to allocate the screen again.
while (default_grid_alloc()) {
// win_new_screensize will recompute floats position, but tell the
// compositor to not redraw them yet
ui_comp_set_screen_valid(false);
if (msg_grid.chars) {
msg_grid_invalid = true;
}
RedrawingDisabled++;
win_new_screensize(); // fit the windows in the new sized screen
comp_col(); // recompute columns for shown command and ruler
RedrawingDisabled--;
// Do not apply autocommands more than 3 times to avoid an endless loop
// in case applying autocommands always changes Rows or Columns.
if (++retry_count > 3) {
break;
}
apply_autocmds(EVENT_VIMRESIZED, NULL, NULL, false, curbuf);
}
resizing_autocmd = false;
redraw_all_later(UPD_CLEAR);
if (State != MODE_ASKMORE && State != MODE_EXTERNCMD && State != MODE_CONFIRM) {
screenclear();
}
if (starting != NO_SCREEN) {
maketitle();
changed_line_abv_curs();
invalidate_botline(curwin);
// We only redraw when it's needed:
// - While at the more prompt or executing an external command, don't
// redraw, but position the cursor.
// - While editing the command line, only redraw that. TODO: lies
// - in Ex mode, don't redraw anything.
// - Otherwise, redraw right now, and position the cursor.
if (State == MODE_ASKMORE || State == MODE_EXTERNCMD || State == MODE_CONFIRM
|| exmode_active) {
if (msg_grid.chars) {
msg_grid_validate();
}
// TODO(bfredl): sometimes messes up the output. Implement clear+redraw
// also for the pager? (or: what if the pager was just a modal window?)
ui_comp_set_screen_valid(true);
repeat_message();
} else {
if (curwin->w_p_scb) {
do_check_scrollbind(true);
}
if (State & MODE_CMDLINE) {
redraw_popupmenu = false;
update_screen();
redrawcmdline();
if (pum_drawn()) {
cmdline_pum_display(false);
}
} else {
update_topline(curwin);
if (pum_drawn()) {
// TODO(bfredl): ins_compl_show_pum wants to redraw the screen first.
// For now make sure the nested update_screen() won't redraw the
// pum at the old position. Try to untangle this later.
redraw_popupmenu = false;
ins_compl_show_pum();
}
update_screen();
if (redrawing()) {
setcursor();
}
}
}
ui_flush();
}
resizing_screen = false;
}
/// Check if the new Nvim application "screen" dimensions are valid.
/// Correct it if it's too small or way too big.
void check_screensize(void)
{
// Limit Rows and Columns to avoid an overflow in Rows * Columns.
if (Rows < min_rows()) {
// need room for one window and command line
Rows = min_rows();
} else if (Rows > 1000) {
Rows = 1000;
}
if (Columns < MIN_COLUMNS) {
Columns = MIN_COLUMNS;
} else if (Columns > 10000) {
Columns = 10000;
}
}
/// Return true if redrawing should currently be done.
bool redrawing(void)
{
return !RedrawingDisabled
&& !(p_lz && char_avail() && !KeyTyped && !do_redraw);
}
/// Redraw the parts of the screen that is marked for redraw.
///
/// Most code shouldn't call this directly, rather use redraw_later() and
/// and redraw_all_later() to mark parts of the screen as needing a redraw.
int update_screen(void)
{
static bool still_may_intro = true;
if (still_may_intro) {
if (!may_show_intro()) {
redraw_later(firstwin, UPD_NOT_VALID);
still_may_intro = false;
}
}
bool is_stl_global = global_stl_height() > 0;
// Don't do anything if the screen structures are (not yet) valid.
// A VimResized autocmd can invoke redrawing in the middle of a resize,
// which would bypass the checks in screen_resize for popupmenu etc.
if (resizing_autocmd || !default_grid.chars) {
return FAIL;
}
// May have postponed updating diffs.
if (need_diff_redraw) {
diff_redraw(true);
}
// Postpone the redrawing when it's not needed and when being called
// recursively.
if (!redrawing() || updating_screen) {
return FAIL;
}
int type = must_redraw;
// must_redraw is reset here, so that when we run into some weird
// reason to redraw while busy redrawing (e.g., asynchronous
// scrolling), or update_topline() in win_update() will cause a
// scroll, or a decoration provider requires a redraw, the screen
// will be redrawn later or in win_update().
must_redraw = 0;
updating_screen = true;
display_tick++; // let syntax code know we're in a next round of
// display updating
// glyph cache full, very rare
if (schar_cache_clear_if_full()) {
// must use CLEAR, as the contents of screen buffers cannot be
// compared to their previous state here.
// TODO(bfredl): if start to cache schar_T values in places (like fcs/lcs)
// we need to revalidate these here as well!
type = MAX(type, UPD_CLEAR);
}
// Tricky: vim code can reset msg_scrolled behind our back, so need
// separate bookkeeping for now.
if (msg_did_scroll) {
msg_did_scroll = false;
msg_scrolled_at_flush = 0;
}
if (type >= UPD_CLEAR || !default_grid.valid) {
ui_comp_set_screen_valid(false);
}
// if the screen was scrolled up when displaying a message, scroll it down
if (msg_scrolled || msg_grid_invalid) {
clear_cmdline = true;
int valid = MAX(Rows - msg_scrollsize(), 0);
if (msg_grid.chars) {
// non-displayed part of msg_grid is considered invalid.
for (int i = 0; i < MIN(msg_scrollsize(), msg_grid.rows); i++) {
grid_clear_line(&msg_grid, msg_grid.line_offset[i],
msg_grid.cols, i < p_ch);
}
}
msg_grid.throttled = false;
bool was_invalidated = false;
// UPD_CLEAR is already handled
if (type == UPD_NOT_VALID && !ui_has(kUIMultigrid) && msg_scrolled) {
was_invalidated = ui_comp_set_screen_valid(false);
for (int i = valid; i < Rows - p_ch; i++) {
grid_clear_line(&default_grid, default_grid.line_offset[i],
Columns, false);
}
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_floating) {
continue;
}
if (W_ENDROW(wp) > valid) {
// TODO(bfredl): too pessimistic. type could be UPD_NOT_VALID
// only because windows that are above the separator.
wp->w_redr_type = MAX(wp->w_redr_type, UPD_NOT_VALID);
}
if (!is_stl_global && W_ENDROW(wp) + wp->w_status_height > valid) {
wp->w_redr_status = true;
}
}
if (is_stl_global && Rows - p_ch - 1 > valid) {
curwin->w_redr_status = true;
}
}
msg_grid_set_pos(Rows - (int)p_ch, false);
msg_grid_invalid = false;
if (was_invalidated) {
// screen was only invalid for the msgarea part.
// @TODO(bfredl): using the same "valid" flag
// for both messages and floats moving is bit of a mess.
ui_comp_set_screen_valid(true);
}
msg_scrolled = 0;
msg_scrolled_at_flush = 0;
msg_grid_scroll_discount = 0;
need_wait_return = false;
}
win_ui_flush(true);
msg_ext_check_clear();
// reset cmdline_row now (may have been changed temporarily)
compute_cmdrow();
bool hl_changed = false;
// Check for changed highlighting
if (need_highlight_changed) {
highlight_changed();
hl_changed = true;
}
if (type == UPD_CLEAR) { // first clear screen
screenclear(); // will reset clear_cmdline
// and set UPD_NOT_VALID for each window
cmdline_screen_cleared(); // clear external cmdline state
type = UPD_NOT_VALID;
// must_redraw may be set indirectly, avoid another redraw later
must_redraw = 0;
} else if (!default_grid.valid) {
grid_invalidate(&default_grid);
default_grid.valid = true;
}
// might need to clear space on default_grid for the message area.
if (type == UPD_NOT_VALID && clear_cmdline && !ui_has(kUIMessages)) {
grid_clear(&default_grid, Rows - (int)p_ch, Rows, 0, Columns, 0);
}
ui_comp_set_screen_valid(true);
decor_providers_start();
// "start" callback could have changed highlights for global elements
if (win_check_ns_hl(NULL)) {
redraw_cmdline = true;
redraw_tabline = true;
}
if (clear_cmdline) { // going to clear cmdline (done below)
msg_check_for_delay(false);
}
// Force redraw when width of 'number' or 'relativenumber' column
// changes.
// TODO(bfredl): special casing curwin here is SÅ JÄVLA BULL.
// Either this should be done for all windows or not at all.
if (curwin->w_redr_type < UPD_NOT_VALID
&& curwin->w_nrwidth != ((curwin->w_p_nu || curwin->w_p_rnu || *curwin->w_p_stc)
? number_width(curwin) : 0)) {
curwin->w_redr_type = UPD_NOT_VALID;
}
// Redraw the tab pages line if needed.
if (redraw_tabline || type >= UPD_NOT_VALID) {
update_window_hl(curwin, type >= UPD_NOT_VALID);
FOR_ALL_TABS(tp) {
if (tp != curtab) {
update_window_hl(tp->tp_curwin, type >= UPD_NOT_VALID);
}
}
draw_tabline();
}
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
// Correct stored syntax highlighting info for changes in each displayed
// buffer. Each buffer must only be done once.
update_window_hl(wp, type >= UPD_NOT_VALID || hl_changed);
buf_T *buf = wp->w_buffer;
if (buf->b_mod_set) {
if (buf->b_mod_tick_syn < display_tick
&& syntax_present(wp)) {
syn_stack_apply_changes(buf);
buf->b_mod_tick_syn = display_tick;
}
if (buf->b_mod_tick_decor < display_tick) {
decor_providers_invoke_buf(buf);
buf->b_mod_tick_decor = display_tick;
}
}
}
// Go from top to bottom through the windows, redrawing the ones that need it.
bool did_one = false;
screen_search_hl.rm.regprog = NULL;
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
if (wp->w_redr_type == UPD_CLEAR && wp->w_floating && wp->w_grid_alloc.chars) {
grid_invalidate(&wp->w_grid_alloc);
wp->w_redr_type = UPD_NOT_VALID;
}
win_check_ns_hl(wp);
// reallocate grid if needed.
win_grid_alloc(wp);
if (wp->w_redr_border || wp->w_redr_type >= UPD_NOT_VALID) {
win_redr_border(wp);
}
if (wp->w_redr_type != 0) {
if (!did_one) {
did_one = true;
start_search_hl();
}
win_update(wp);
}
// redraw status line and window bar after the window to minimize cursor movement
if (wp->w_redr_status) {
win_redr_winbar(wp);
win_redr_status(wp);
}
}
end_search_hl();
// May need to redraw the popup menu.
if (pum_drawn() && must_redraw_pum) {
win_check_ns_hl(curwin);
pum_redraw();
}
win_check_ns_hl(NULL);
// Reset b_mod_set and b_signcols.resized flags. Going through all windows is
// probably faster than going through all buffers (there could be many buffers).
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
wp->w_buffer->b_mod_set = false;
wp->w_buffer->b_signcols.resized = false;
}
updating_screen = false;
// Clear or redraw the command line. Done last, because scrolling may
// mess up the command line.
if (clear_cmdline || redraw_cmdline || redraw_mode) {
showmode();
}
// May put up an introductory message when not editing a file
if (still_may_intro) {
intro_message(false);
}
decor_providers_invoke_end();
// either cmdline is cleared, not drawn or mode is last drawn
cmdline_was_last_drawn = false;
return OK;
}
/// Prepare for 'hlsearch' highlighting.
void start_search_hl(void)
{
if (!p_hls || no_hlsearch) {
return;
}
end_search_hl(); // just in case it wasn't called before
last_pat_prog(&screen_search_hl.rm);
// Set the time limit to 'redrawtime'.
screen_search_hl.tm = profile_setlimit(p_rdt);
}
/// Clean up for 'hlsearch' highlighting.
void end_search_hl(void)
{
if (screen_search_hl.rm.regprog == NULL) {
return;
}
vim_regfree(screen_search_hl.rm.regprog);
screen_search_hl.rm.regprog = NULL;
}
static void win_redr_bordertext(win_T *wp, VirtText vt, int col)
{
int used_col = col;
int width = wp->w_config.width;
bool truncate = false;
char *new_text = NULL;
for (size_t i = 0; i < kv_size(vt);) {
int attr = 0;
char *text = next_virt_text_chunk(vt, &i, &attr);
if (text == NULL) {
break;
}
attr = hl_apply_winblend(wp, attr);
int cell = (int)mb_string2cells(text);
int shift = used_col + cell - width;
if (shift > 1) {
truncate = true;
if (used_col > col) {
int move_start = col + shift - 1;
int start_pos = col;
for (int j = move_start; j < used_col; j++) {
grid_line_put_schar(start_pos, linebuf_char[j], linebuf_attr[j]);
start_pos++;
}
used_col -= (shift - 1);
} else {
new_text = xmemdupz(text + shift - 1, strlen(text) - (size_t)shift + 1);
}
}
used_col += grid_line_puts(used_col, new_text ? new_text : text, -1, attr);
if (new_text) {
xfree(new_text);
new_text = NULL;
}
}
if (truncate) {
grid_line_put_schar(col, schar_from_ascii('<'), linebuf_attr[col]);
}
}
static int win_get_bordertext_col(int total_col, int text_width, AlignTextPos align)
{
switch (align) {
case kAlignLeft:
return 1;
case kAlignCenter:
return MAX((total_col - text_width) / 2 + 1, 1);
case kAlignRight:
return MAX(total_col - text_width + 1, 1);
}
UNREACHABLE;
}
static void win_redr_border(win_T *wp)
{
wp->w_redr_border = false;
if (!(wp->w_floating && wp->w_config.border)) {
return;
}
ScreenGrid *grid = &wp->w_grid_alloc;
schar_T chars[8];
for (int i = 0; i < 8; i++) {
chars[i] = schar_from_str(wp->w_config.border_chars[i]);
}
int *attrs = wp->w_config.border_attr;
int *adj = wp->w_border_adj;
int irow = wp->w_height_inner + wp->w_winbar_height;
int icol = wp->w_width_inner;
if (adj[0]) {
grid_line_start(grid, 0);
if (adj[3]) {
grid_line_put_schar(0, chars[0], attrs[0]);
}
for (int i = 0; i < icol; i++) {
grid_line_put_schar(i + adj[3], chars[1], attrs[1]);
}
if (wp->w_config.title) {
int title_col = win_get_bordertext_col(icol, wp->w_config.title_width,
wp->w_config.title_pos);
win_redr_bordertext(wp, wp->w_config.title_chunks, title_col);
}
if (adj[1]) {
grid_line_put_schar(icol + adj[3], chars[2], attrs[2]);
}
grid_line_flush();
}
for (int i = 0; i < irow; i++) {
if (adj[3]) {
grid_line_start(grid, i + adj[0]);
grid_line_put_schar(0, chars[7], attrs[7]);
grid_line_flush();
}
if (adj[1]) {
int ic = (i == 0 && !adj[0] && chars[2]) ? 2 : 3;
grid_line_start(grid, i + adj[0]);
grid_line_put_schar(icol + adj[3], chars[ic], attrs[ic]);
grid_line_flush();
}
}
if (adj[2]) {
grid_line_start(grid, irow + adj[0]);
if (adj[3]) {
grid_line_put_schar(0, chars[6], attrs[6]);
}
for (int i = 0; i < icol; i++) {
int ic = (i == 0 && !adj[3] && chars[6]) ? 6 : 5;
grid_line_put_schar(i + adj[3], chars[ic], attrs[ic]);
}
if (wp->w_config.footer) {
int footer_col = win_get_bordertext_col(icol, wp->w_config.footer_width,
wp->w_config.footer_pos);
win_redr_bordertext(wp, wp->w_config.footer_chunks, footer_col);
}
if (adj[1]) {
grid_line_put_schar(icol + adj[3], chars[4], attrs[4]);
}
grid_line_flush();
}
}
/// Set cursor to its position in the current window.
void setcursor(void)
{
setcursor_mayforce(false);
}
/// Set cursor to its position in the current window.
/// @param force when true, also when not redrawing.
void setcursor_mayforce(bool force)
{
if (force || redrawing()) {
validate_cursor(curwin);
ScreenGrid *grid = &curwin->w_grid;
int row = curwin->w_wrow;
int col = curwin->w_wcol;
if (curwin->w_p_rl) {
// With 'rightleft' set and the cursor on a double-wide character,
// position it on the leftmost column.
col = curwin->w_width_inner - curwin->w_wcol
- ((utf_ptr2cells(get_cursor_pos_ptr()) == 2
&& vim_isprintc(gchar_cursor())) ? 2 : 1);
}
grid_adjust(&grid, &row, &col);
ui_grid_cursor_goto(grid->handle, row, col);
}
}
/// Show current cursor info in ruler and various other places
///
/// @param always if false, only show ruler if position has changed.
void show_cursor_info_later(bool force)
{
int state = get_real_state();
int empty_line = (State & MODE_INSERT) == 0
&& *ml_get_buf(curwin->w_buffer, curwin->w_cursor.lnum) == NUL;
// Only draw when something changed.
validate_virtcol(curwin);
if (force
|| curwin->w_cursor.lnum != curwin->w_stl_cursor.lnum
|| curwin->w_cursor.col != curwin->w_stl_cursor.col
|| curwin->w_virtcol != curwin->w_stl_virtcol
|| curwin->w_cursor.coladd != curwin->w_stl_cursor.coladd
|| curwin->w_topline != curwin->w_stl_topline
|| curwin->w_buffer->b_ml.ml_line_count != curwin->w_stl_line_count
|| curwin->w_topfill != curwin->w_stl_topfill
|| empty_line != curwin->w_stl_empty
|| reg_recording != curwin->w_stl_recording
|| state != curwin->w_stl_state
|| (VIsual_active && VIsual_mode != curwin->w_stl_visual_mode)) {
if (curwin->w_status_height || global_stl_height()) {
curwin->w_redr_status = true;
} else {
redraw_cmdline = true;
}
if (*p_wbr != NUL || *curwin->w_p_wbr != NUL) {
curwin->w_redr_status = true;
}
if ((p_icon && (stl_syntax & STL_IN_ICON))
|| (p_title && (stl_syntax & STL_IN_TITLE))) {
need_maketitle = true;
}
}
curwin->w_stl_cursor = curwin->w_cursor;
curwin->w_stl_virtcol = curwin->w_virtcol;
curwin->w_stl_empty = (char)empty_line;
curwin->w_stl_topline = curwin->w_topline;
curwin->w_stl_line_count = curwin->w_buffer->b_ml.ml_line_count;
curwin->w_stl_topfill = curwin->w_topfill;
curwin->w_stl_recording = reg_recording;
curwin->w_stl_state = state;
if (VIsual_active) {
curwin->w_stl_visual_mode = VIsual_mode;
}
}
/// @return true when postponing displaying the mode message: when not redrawing
/// or inside a mapping.
bool skip_showmode(void)
{
// Call char_avail() only when we are going to show something, because it
// takes a bit of time. redrawing() may also call char_avail().
if (global_busy || msg_silent != 0 || !redrawing() || (char_avail() && !KeyTyped)) {
redraw_mode = true; // show mode later
return true;
}
return false;
}
/// Show the current mode and ruler.
///
/// If clear_cmdline is true, clear the rest of the cmdline.
/// If clear_cmdline is false there may be a message there that needs to be
/// cleared only if a mode is shown.
/// If redraw_mode is true show or clear the mode.
/// @return the length of the message (0 if no message).
int showmode(void)
{
int length = 0;
if (ui_has(kUIMessages) && clear_cmdline) {
msg_ext_clear(true);
}
// don't make non-flushed message part of the showmode
msg_ext_ui_flush();
msg_grid_validate();
bool do_mode = ((p_smd && msg_silent == 0)
&& ((State & MODE_TERMINAL)
|| (State & MODE_INSERT)
|| restart_edit != NUL
|| VIsual_active));
bool can_show_mode = (p_ch != 0 || ui_has(kUIMessages));
if ((do_mode || reg_recording != 0) && can_show_mode) {
if (skip_showmode()) {
return 0; // show mode later
}
bool nwr_save = need_wait_return;
// wait a bit before overwriting an important message
msg_check_for_delay(false);
// if the cmdline is more than one line high, erase top lines
bool need_clear = clear_cmdline;
if (clear_cmdline && cmdline_row < Rows - 1) {
msg_clr_cmdline(); // will reset clear_cmdline
}
// Position on the last line in the window, column 0
msg_pos_mode();
int attr = HL_ATTR(HLF_CM); // Highlight mode
// When the screen is too narrow to show the entire mode message,
// avoid scrolling and truncate instead.
msg_no_more = true;
int save_lines_left = lines_left;
lines_left = 0;
if (do_mode) {