/
window.c
7186 lines (6479 loc) · 193 KB
/
window.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
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <assert.h>
#include <inttypes.h>
#include <stdbool.h>
#include "nvim/api/private/helpers.h"
#include "nvim/ascii.h"
#include "nvim/buffer.h"
#include "nvim/charset.h"
#include "nvim/cursor.h"
#include "nvim/diff.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
#include "nvim/ex_cmds.h"
#include "nvim/ex_cmds2.h"
#include "nvim/ex_docmd.h"
#include "nvim/ex_eval.h"
#include "nvim/ex_getln.h"
#include "nvim/file_search.h"
#include "nvim/fileio.h"
#include "nvim/fold.h"
#include "nvim/garray.h"
#include "nvim/getchar.h"
#include "nvim/globals.h"
#include "nvim/hashtab.h"
#include "nvim/main.h"
#include "nvim/mark.h"
#include "nvim/match.h"
#include "nvim/memline.h"
#include "nvim/memory.h"
#include "nvim/message.h"
#include "nvim/mouse.h"
#include "nvim/move.h"
#include "nvim/normal.h"
#include "nvim/option.h"
#include "nvim/os/os.h"
#include "nvim/os_unix.h"
#include "nvim/path.h"
#include "nvim/plines.h"
#include "nvim/quickfix.h"
#include "nvim/regexp.h"
#include "nvim/screen.h"
#include "nvim/search.h"
#include "nvim/state.h"
#include "nvim/strings.h"
#include "nvim/syntax.h"
#include "nvim/terminal.h"
#include "nvim/ui.h"
#include "nvim/ui_compositor.h"
#include "nvim/undo.h"
#include "nvim/vim.h"
#include "nvim/window.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "window.c.generated.h"
#endif
#define NOWIN (win_T *)-1 // non-existing window
#define ROWS_AVAIL (Rows - p_ch - tabline_height() - global_stl_height())
/// flags for win_enter_ext()
typedef enum {
WEE_UNDO_SYNC = 0x01,
WEE_CURWIN_INVALID = 0x02,
WEE_TRIGGER_NEW_AUTOCMDS = 0x04,
WEE_TRIGGER_ENTER_AUTOCMDS = 0x08,
WEE_TRIGGER_LEAVE_AUTOCMDS = 0x10,
} wee_flags_T;
static char *m_onlyone = N_("Already only one window");
/// @return the current window, unless in the cmdline window and "prevwin" is
/// set, then return "prevwin".
win_T *prevwin_curwin(void)
FUNC_ATTR_WARN_UNUSED_RESULT
{
// In cmdwin, the alternative buffer should be used.
return is_in_cmdwin() && prevwin != NULL ? prevwin : curwin;
}
/// all CTRL-W window commands are handled here, called from normal_cmd().
///
/// @param xchar extra char from ":wincmd gx" or NUL
void do_window(int nchar, long Prenum, int xchar)
{
long Prenum1;
win_T *wp;
char_u *ptr;
linenr_T lnum = -1;
int type = FIND_DEFINE;
size_t len;
char cbuf[40];
Prenum1 = Prenum == 0 ? 1 : Prenum;
#define CHECK_CMDWIN \
do { \
if (cmdwin_type != 0) { \
emsg(_(e_cmdwin)); \
return; \
} \
} while (0)
switch (nchar) {
// split current window in two parts, horizontally
case 'S':
case Ctrl_S:
case 's':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
// When splitting the quickfix window open a new buffer in it,
// don't replicate the quickfix buffer.
if (bt_quickfix(curbuf)) {
goto newwindow;
}
(void)win_split((int)Prenum, 0);
break;
// split current window in two parts, vertically
case Ctrl_V:
case 'v':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
// When splitting the quickfix window open a new buffer in it,
// don't replicate the quickfix buffer.
if (bt_quickfix(curbuf)) {
goto newwindow;
}
(void)win_split((int)Prenum, WSP_VERT);
break;
// split current window and edit alternate file
case Ctrl_HAT:
case '^':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
if (buflist_findnr(Prenum == 0 ? curwin->w_alt_fnum : Prenum) == NULL) {
if (Prenum == 0) {
emsg(_(e_noalt));
} else {
semsg(_("E92: Buffer %" PRId64 " not found"), (int64_t)Prenum);
}
break;
}
if (!curbuf_locked() && win_split(0, 0) == OK) {
(void)buflist_getfile(Prenum == 0 ? curwin->w_alt_fnum : Prenum,
(linenr_T)0, GETF_ALT, false);
}
break;
// open new window
case Ctrl_N:
case 'n':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
newwindow:
if (Prenum) {
// window height
vim_snprintf(cbuf, sizeof(cbuf) - 5, "%" PRId64, (int64_t)Prenum);
} else {
cbuf[0] = NUL;
}
if (nchar == 'v' || nchar == Ctrl_V) {
xstrlcat(cbuf, "v", sizeof(cbuf));
}
xstrlcat(cbuf, "new", sizeof(cbuf));
do_cmdline_cmd(cbuf);
break;
// quit current window
case Ctrl_Q:
case 'q':
reset_VIsual_and_resel(); // stop Visual mode
cmd_with_count("quit", (char_u *)cbuf, sizeof(cbuf), Prenum);
do_cmdline_cmd(cbuf);
break;
// close current window
case Ctrl_C:
case 'c':
reset_VIsual_and_resel(); // stop Visual mode
cmd_with_count("close", (char_u *)cbuf, sizeof(cbuf), Prenum);
do_cmdline_cmd(cbuf);
break;
// close preview window
case Ctrl_Z:
case 'z':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
do_cmdline_cmd("pclose");
break;
// cursor to preview window
case 'P':
wp = NULL;
FOR_ALL_WINDOWS_IN_TAB(wp2, curtab) {
if (wp2->w_p_pvw) {
wp = wp2;
break;
}
}
if (wp == NULL) {
emsg(_("E441: There is no preview window"));
} else {
win_goto(wp);
}
break;
// close all but current window
case Ctrl_O:
case 'o':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
cmd_with_count("only", (char_u *)cbuf, sizeof(cbuf), Prenum);
do_cmdline_cmd(cbuf);
break;
// cursor to next window with wrap around
case Ctrl_W:
case 'w':
// cursor to previous window with wrap around
case 'W':
CHECK_CMDWIN;
if (ONE_WINDOW && Prenum != 1) { // just one window
beep_flush();
} else {
if (Prenum) { // go to specified window
for (wp = firstwin; --Prenum > 0;) {
if (wp->w_next == NULL) {
break;
} else {
wp = wp->w_next;
}
}
} else {
if (nchar == 'W') { // go to previous window
wp = curwin->w_prev;
if (wp == NULL) {
wp = lastwin; // wrap around
}
while (wp != NULL && wp->w_floating
&& !wp->w_float_config.focusable) {
wp = wp->w_prev;
}
} else { // go to next window
wp = curwin->w_next;
while (wp != NULL && wp->w_floating
&& !wp->w_float_config.focusable) {
wp = wp->w_next;
}
if (wp == NULL) {
wp = firstwin; // wrap around
}
}
}
win_goto(wp);
}
break;
// cursor to window below
case 'j':
case K_DOWN:
case Ctrl_J:
CHECK_CMDWIN;
win_goto_ver(false, Prenum1);
break;
// cursor to window above
case 'k':
case K_UP:
case Ctrl_K:
CHECK_CMDWIN;
win_goto_ver(true, Prenum1);
break;
// cursor to left window
case 'h':
case K_LEFT:
case Ctrl_H:
case K_BS:
CHECK_CMDWIN;
win_goto_hor(true, Prenum1);
break;
// cursor to right window
case 'l':
case K_RIGHT:
case Ctrl_L:
CHECK_CMDWIN;
win_goto_hor(false, Prenum1);
break;
// move window to new tab page
case 'T':
if (one_window()) {
msg(_(m_onlyone));
} else {
tabpage_T *oldtab = curtab;
tabpage_T *newtab;
// First create a new tab with the window, then go back to
// the old tab and close the window there.
wp = curwin;
if (win_new_tabpage((int)Prenum, NULL) == OK
&& valid_tabpage(oldtab)) {
newtab = curtab;
goto_tabpage_tp(oldtab, true, true);
if (curwin == wp) {
win_close(curwin, false, false);
}
if (valid_tabpage(newtab)) {
goto_tabpage_tp(newtab, true, true);
apply_autocmds(EVENT_TABNEWENTERED, NULL, NULL, false, curbuf);
}
}
}
break;
// cursor to top-left window
case 't':
case Ctrl_T:
win_goto(firstwin);
break;
// cursor to bottom-right window
case 'b':
case Ctrl_B:
win_goto(lastwin_nofloating());
break;
// cursor to last accessed (previous) window
case 'p':
case Ctrl_P:
if (!win_valid(prevwin)) {
beep_flush();
} else {
win_goto(prevwin);
}
break;
// exchange current and next window
case 'x':
case Ctrl_X:
CHECK_CMDWIN;
win_exchange(Prenum);
break;
// rotate windows downwards
case Ctrl_R:
case 'r':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
win_rotate(false, (int)Prenum1); // downwards
break;
// rotate windows upwards
case 'R':
CHECK_CMDWIN;
reset_VIsual_and_resel(); // stop Visual mode
win_rotate(true, (int)Prenum1); // upwards
break;
// move window to the very top/bottom/left/right
case 'K':
case 'J':
case 'H':
case 'L':
CHECK_CMDWIN;
win_totop((int)Prenum,
((nchar == 'H' || nchar == 'L') ? WSP_VERT : 0)
| ((nchar == 'H' || nchar == 'K') ? WSP_TOP : WSP_BOT));
break;
// make all windows the same height
case '=':
win_equal(NULL, false, 'b');
break;
// increase current window height
case '+':
win_setheight(curwin->w_height + (int)Prenum1);
break;
// decrease current window height
case '-':
win_setheight(curwin->w_height - (int)Prenum1);
break;
// set current window height
case Ctrl__:
case '_':
win_setheight(Prenum ? (int)Prenum : Rows-1);
break;
// increase current window width
case '>':
win_setwidth(curwin->w_width + (int)Prenum1);
break;
// decrease current window width
case '<':
win_setwidth(curwin->w_width - (int)Prenum1);
break;
// set current window width
case '|':
win_setwidth(Prenum != 0 ? (int)Prenum : Columns);
break;
// jump to tag and split window if tag exists (in preview window)
case '}':
CHECK_CMDWIN;
if (Prenum) {
g_do_tagpreview = Prenum;
} else {
g_do_tagpreview = p_pvh;
}
FALLTHROUGH;
case ']':
case Ctrl_RSB:
CHECK_CMDWIN;
// Keep visual mode, can select words to use as a tag.
if (Prenum) {
postponed_split = Prenum;
} else {
postponed_split = -1;
}
if (nchar != '}') {
g_do_tagpreview = 0;
}
// Execute the command right here, required when
// "wincmd ]" was used in a function.
do_nv_ident(Ctrl_RSB, NUL);
break;
// edit file name under cursor in a new window
case 'f':
case 'F':
case Ctrl_F:
wingotofile:
CHECK_CMDWIN;
ptr = grab_file_name(Prenum1, &lnum);
if (ptr != NULL) {
tabpage_T *oldtab = curtab;
win_T *oldwin = curwin;
setpcmark();
if (win_split(0, 0) == OK) {
RESET_BINDING(curwin);
if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) {
// Failed to open the file, close the window opened for it.
win_close(curwin, false, false);
goto_tabpage_win(oldtab, oldwin);
} else if (nchar == 'F' && lnum >= 0) {
curwin->w_cursor.lnum = lnum;
check_cursor_lnum();
beginline(BL_SOL | BL_FIX);
}
}
xfree(ptr);
}
break;
// Go to the first occurrence of the identifier under cursor along path in a
// new window -- webb
case 'i': // Go to any match
case Ctrl_I:
type = FIND_ANY;
FALLTHROUGH;
case 'd': // Go to definition, using 'define'
case Ctrl_D:
CHECK_CMDWIN;
if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0) {
break;
}
find_pattern_in_path(ptr, 0, len, true, Prenum == 0,
type, Prenum1, ACTION_SPLIT, 1, MAXLNUM);
curwin->w_set_curswant = TRUE;
break;
// Quickfix window only: view the result under the cursor in a new split.
case K_KENTER:
case CAR:
if (bt_quickfix(curbuf)) {
qf_view_result(true);
}
break;
// CTRL-W g extended commands
case 'g':
case Ctrl_G:
CHECK_CMDWIN;
no_mapping++;
if (xchar == NUL) {
xchar = plain_vgetc();
}
LANGMAP_ADJUST(xchar, true);
no_mapping--;
(void)add_to_showcmd(xchar);
switch (xchar) {
case '}':
xchar = Ctrl_RSB;
if (Prenum) {
g_do_tagpreview = Prenum;
} else {
g_do_tagpreview = p_pvh;
}
FALLTHROUGH;
case ']':
case Ctrl_RSB:
// Keep visual mode, can select words to use as a tag.
if (Prenum) {
postponed_split = Prenum;
} else {
postponed_split = -1;
}
// Execute the command right here, required when
// "wincmd g}" was used in a function.
do_nv_ident('g', xchar);
break;
case 'f': // CTRL-W gf: "gf" in a new tab page
case 'F': // CTRL-W gF: "gF" in a new tab page
cmdmod.tab = tabpage_index(curtab) + 1;
nchar = xchar;
goto wingotofile;
case 't': // CTRL-W gt: go to next tab page
goto_tabpage((int)Prenum);
break;
case 'T': // CTRL-W gT: go to previous tab page
goto_tabpage(-(int)Prenum1);
break;
case TAB: // CTRL-W g<Tab>: go to last used tab page
if (!goto_tabpage_lastused()) {
beep_flush();
}
break;
case 'e':
if (curwin->w_floating || !ui_has(kUIMultigrid)) {
beep_flush();
break;
}
FloatConfig config = FLOAT_CONFIG_INIT;
config.width = curwin->w_width;
config.height = curwin->w_height;
config.external = true;
Error err = ERROR_INIT;
if (!win_new_float(curwin, false, config, &err)) {
emsg(err.msg);
api_clear_error(&err);
beep_flush();
}
break;
default:
beep_flush();
break;
}
break;
default:
beep_flush();
break;
}
}
static void cmd_with_count(char *cmd, char_u *bufp, size_t bufsize, int64_t Prenum)
{
size_t len = STRLCPY(bufp, cmd, bufsize);
if (Prenum > 0 && len < bufsize) {
vim_snprintf((char *)bufp + len, bufsize - len, "%" PRId64, Prenum);
}
}
void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err)
{
win_T *win = find_window_by_handle(window, err);
buf_T *buf = find_buffer_by_handle(buffer, err);
tabpage_T *tab = win_find_tabpage(win);
if (!win || !buf) {
return;
}
if (noautocmd) {
block_autocmds();
}
switchwin_T switchwin;
if (switch_win_noblock(&switchwin, win, tab, false) == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to switch to window %d",
window);
}
try_start();
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
if (!try_end(err) && result == FAIL) {
api_set_error(err,
kErrorTypeException,
"Failed to set buffer %d",
buffer);
}
// If window is not current, state logic will not validate its cursor.
// So do it now.
validate_cursor();
restore_win_noblock(&switchwin, false);
if (noautocmd) {
unblock_autocmds();
}
}
/// Create a new float.
///
/// @param wp if NULL, allocate a new window, otherwise turn existing window into a float.
/// It must then already belong to the current tabpage!
/// @param last make the window the last one in the window list.
/// Only used when allocating the autocommand window.
/// @param config must already have been validated!
win_T *win_new_float(win_T *wp, bool last, FloatConfig fconfig, Error *err)
{
if (wp == NULL) {
wp = win_alloc(last ? lastwin : lastwin_nofloating(), false);
win_init(wp, curwin, 0);
} else {
assert(!last);
assert(!wp->w_floating);
if (firstwin == wp && lastwin_nofloating() == wp) {
// last non-float
api_set_error(err, kErrorTypeException,
"Cannot change last window into float");
return NULL;
} else if (!win_valid(wp)) {
api_set_error(err, kErrorTypeException,
"Cannot change window from different tabpage into float");
return NULL;
}
int dir;
winframe_remove(wp, &dir, NULL);
XFREE_CLEAR(wp->w_frame);
(void)win_comp_pos(); // recompute window positions
win_remove(wp, NULL);
win_append(lastwin_nofloating(), wp);
}
wp->w_floating = 1;
wp->w_status_height = 0;
wp->w_hsep_height = 0;
wp->w_vsep_width = 0;
win_config_float(wp, fconfig);
win_set_inner_size(wp);
wp->w_pos_changed = true;
redraw_later(wp, VALID);
return wp;
}
void win_set_minimal_style(win_T *wp)
{
wp->w_p_nu = false;
wp->w_p_rnu = false;
wp->w_p_cul = false;
wp->w_p_cuc = false;
wp->w_p_spell = false;
wp->w_p_list = false;
// Hide EOB region: use " " fillchar and cleared highlighting
if (wp->w_p_fcs_chars.eob != ' ') {
char_u *old = wp->w_p_fcs;
wp->w_p_fcs = ((*old == NUL)
? (char_u *)xstrdup("eob: ")
: concat_str(old, (char_u *)",eob: "));
xfree(old);
}
if (wp->w_hl_ids[HLF_EOB] != -1) {
char_u *old = wp->w_p_winhl;
wp->w_p_winhl = ((*old == NUL)
? (char_u *)xstrdup("EndOfBuffer:")
: concat_str(old, (char_u *)",EndOfBuffer:"));
xfree(old);
}
// signcolumn: use 'auto'
if (wp->w_p_scl[0] != 'a' || STRLEN(wp->w_p_scl) >= 8) {
xfree(wp->w_p_scl);
wp->w_p_scl = (char_u *)xstrdup("auto");
}
// foldcolumn: use '0'
if (wp->w_p_fdc[0] != '0') {
xfree(wp->w_p_fdc);
wp->w_p_fdc = (char_u *)xstrdup("0");
}
// colorcolumn: cleared
if (wp->w_p_cc != NULL && *wp->w_p_cc != NUL) {
xfree(wp->w_p_cc);
wp->w_p_cc = (char_u *)xstrdup("");
}
}
void win_config_float(win_T *wp, FloatConfig fconfig)
{
wp->w_width = MAX(fconfig.width, 1);
wp->w_height = MAX(fconfig.height, 1);
if (fconfig.relative == kFloatRelativeCursor) {
fconfig.relative = kFloatRelativeWindow;
fconfig.row += curwin->w_wrow;
fconfig.col += curwin->w_wcol;
fconfig.window = curwin->handle;
}
bool change_external = fconfig.external != wp->w_float_config.external;
bool change_border = (fconfig.border != wp->w_float_config.border
|| memcmp(fconfig.border_hl_ids,
wp->w_float_config.border_hl_ids,
sizeof fconfig.border_hl_ids));
wp->w_float_config = fconfig;
bool has_border = wp->w_floating && wp->w_float_config.border;
for (int i = 0; i < 4; i++) {
int new_adj = has_border && wp->w_float_config.border_chars[2 * i + 1][0];
if (new_adj != wp->w_border_adj[i]) {
change_border = true;
wp->w_border_adj[i] = new_adj;
}
}
if (!ui_has(kUIMultigrid)) {
wp->w_height = MIN(wp->w_height,
Rows - 1 - (wp->w_border_adj[0] + wp->w_border_adj[2]));
wp->w_width = MIN(wp->w_width,
Columns - (wp->w_border_adj[1] + wp->w_border_adj[3]));
}
win_set_inner_size(wp);
must_redraw = MAX(must_redraw, VALID);
wp->w_pos_changed = true;
if (change_external || change_border) {
wp->w_hl_needs_update = true;
redraw_later(wp, NOT_VALID);
}
// compute initial position
if (wp->w_float_config.relative == kFloatRelativeWindow) {
int row = wp->w_float_config.row;
int col = wp->w_float_config.col;
Error dummy = ERROR_INIT;
win_T *parent = find_window_by_handle(wp->w_float_config.window, &dummy);
if (parent) {
row += parent->w_winrow;
col += parent->w_wincol;
ScreenGrid *grid = &parent->w_grid;
int row_off = 0, col_off = 0;
screen_adjust_grid(&grid, &row_off, &col_off);
row += row_off;
col += col_off;
}
api_clear_error(&dummy);
if (wp->w_float_config.bufpos.lnum >= 0) {
pos_T pos = { wp->w_float_config.bufpos.lnum + 1,
wp->w_float_config.bufpos.col, 0 };
int trow, tcol, tcolc, tcole;
textpos2screenpos(wp, &pos, &trow, &tcol, &tcolc, &tcole, true);
row += trow - 1;
col += tcol - 1;
}
wp->w_winrow = row;
wp->w_wincol = col;
} else {
wp->w_winrow = fconfig.row;
wp->w_wincol = fconfig.col;
}
// changing border style while keeping border only requires redrawing border
if (fconfig.border) {
wp->w_redr_border = true;
redraw_later(wp, VALID);
}
}
void win_check_anchored_floats(win_T *win)
{
for (win_T *wp = lastwin; wp && wp->w_floating; wp = wp->w_prev) {
// float might be anchored to moved window
if (wp->w_float_config.relative == kFloatRelativeWindow
&& wp->w_float_config.window == win->handle) {
wp->w_pos_changed = true;
}
}
}
/// Return the number of fold columns to display
int win_fdccol_count(win_T *wp)
{
const char *fdc = (const char *)wp->w_p_fdc;
// auto:<NUM>
if (strncmp(fdc, "auto", 4) == 0) {
const int fdccol = fdc[4] == ':' ? fdc[5] - '0' : 1;
int needed_fdccols = getDeepestNesting(wp);
return MIN(fdccol, needed_fdccols);
} else {
return fdc[0] - '0';
}
}
void ui_ext_win_position(win_T *wp)
{
if (!wp->w_floating) {
ui_call_win_pos(wp->w_grid_alloc.handle, wp->handle, wp->w_winrow,
wp->w_wincol, wp->w_width, wp->w_height);
return;
}
FloatConfig c = wp->w_float_config;
if (!c.external) {
ScreenGrid *grid = &default_grid;
float row = c.row, col = c.col;
if (c.relative == kFloatRelativeWindow) {
Error dummy = ERROR_INIT;
win_T *win = find_window_by_handle(c.window, &dummy);
if (win) {
grid = &win->w_grid;
int row_off = 0, col_off = 0;
screen_adjust_grid(&grid, &row_off, &col_off);
row += row_off;
col += col_off;
if (c.bufpos.lnum >= 0) {
pos_T pos = { c.bufpos.lnum+1, c.bufpos.col, 0 };
int trow, tcol, tcolc, tcole;
textpos2screenpos(win, &pos, &trow, &tcol, &tcolc, &tcole, true);
row += trow-1;
col += tcol-1;
}
}
api_clear_error(&dummy);
}
wp->w_grid_alloc.zindex = wp->w_float_config.zindex;
if (ui_has(kUIMultigrid)) {
String anchor = cstr_to_string(float_anchor_str[c.anchor]);
ui_call_win_float_pos(wp->w_grid_alloc.handle, wp->handle, anchor,
grid->handle, row, col, c.focusable,
wp->w_grid_alloc.zindex);
} else {
// TODO(bfredl): ideally, compositor should work like any multigrid UI
// and use standard win_pos events.
bool east = c.anchor & kFloatAnchorEast;
bool south = c.anchor & kFloatAnchorSouth;
int comp_row = (int)row - (south ? wp->w_height_outer : 0);
int comp_col = (int)col - (east ? wp->w_width_outer : 0);
comp_row += grid->comp_row;
comp_col += grid->comp_col;
comp_row = MAX(MIN(comp_row, Rows - wp->w_height_outer - 1), 0);
comp_col = MAX(MIN(comp_col, Columns - wp->w_width_outer), 0);
wp->w_winrow = comp_row;
wp->w_wincol = comp_col;
bool valid = (wp->w_redr_type == 0);
ui_comp_put_grid(&wp->w_grid_alloc, comp_row, comp_col,
wp->w_height_outer, wp->w_width_outer, valid, false);
ui_check_cursor_grid(wp->w_grid_alloc.handle);
wp->w_grid_alloc.focusable = wp->w_float_config.focusable;
if (!valid) {
wp->w_grid_alloc.valid = false;
redraw_later(wp, NOT_VALID);
}
}
} else {
ui_call_win_external_pos(wp->w_grid_alloc.handle, wp->handle);
}
}
void ui_ext_win_viewport(win_T *wp)
{
if ((wp == curwin || ui_has(kUIMultigrid)) && wp->w_viewport_invalid) {
int botline = wp->w_botline;
int line_count = wp->w_buffer->b_ml.ml_line_count;
if (botline == line_count+1 && wp->w_empty_rows == 0) {
// TODO(bfredl): The might be more cases to consider, like how does this
// interact with incomplete final line? Diff filler lines?
botline = wp->w_buffer->b_ml.ml_line_count;
}
ui_call_win_viewport(wp->w_grid_alloc.handle, wp->handle, wp->w_topline-1,
botline, wp->w_cursor.lnum-1, wp->w_cursor.col,
line_count);
wp->w_viewport_invalid = false;
}
}
/*
* split the current window, implements CTRL-W s and :split
*
* "size" is the height or width for the new window, 0 to use half of current
* height or width.
*
* "flags":
* WSP_ROOM: require enough room for new window
* WSP_VERT: vertical split.
* WSP_TOP: open window at the top-left of the shell (help window).
* WSP_BOT: open window at the bottom-right of the shell (quickfix window).
* WSP_HELP: creating the help window, keep layout snapshot
*
* return FAIL for failure, OK otherwise
*/
int win_split(int size, int flags)
{
// When the ":tab" modifier was used open a new tab page instead.
if (may_open_tabpage() == OK) {
return OK;
}
// Add flags from ":vertical", ":topleft" and ":botright".
flags |= cmdmod.split;
if ((flags & WSP_TOP) && (flags & WSP_BOT)) {
emsg(_("E442: Can't split topleft and botright at the same time"));
return FAIL;
}
// When creating the help window make a snapshot of the window layout.
// Otherwise clear the snapshot, it's now invalid.
if (flags & WSP_HELP) {
make_snapshot(SNAP_HELP_IDX);
} else {
clear_snapshot(curtab, SNAP_HELP_IDX);
}
return win_split_ins(size, flags, NULL, 0);
}
/*
* When "new_wp" is NULL: split the current window in two.
* When "new_wp" is not NULL: insert this window at the far
* top/left/right/bottom.
* return FAIL for failure, OK otherwise
*/
int win_split_ins(int size, int flags, win_T *new_wp, int dir)
{
win_T *wp = new_wp;
win_T *oldwin;
int new_size = size;
int i;
int need_status = 0;
bool do_equal = false;
int needed;
int available;
int oldwin_height = 0;
int layout;
frame_T *frp, *curfrp, *frp2, *prevfrp;
int before;
int minheight;
int wmh1;
int hsep_height;
bool did_set_fraction = false;
// aucmd_win should always remain floating
if (new_wp != NULL && new_wp == aucmd_win) {
return FAIL;
}
if (flags & WSP_TOP) {
oldwin = firstwin;
} else if (flags & WSP_BOT || curwin->w_floating) {
// can't split float, use last nonfloating window instead
oldwin = lastwin_nofloating();
} else {
oldwin = curwin;
}
bool new_in_layout = (new_wp == NULL || new_wp->w_floating);
// add a status line when p_ls == 1 and splitting the first window
if (one_nonfloat() && p_ls == 1 && oldwin->w_status_height == 0) {
if (oldwin->w_height <= p_wmh && new_in_layout) {
emsg(_(e_noroom));
return FAIL;
}
need_status = STATUS_HEIGHT;
}