forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.c
1915 lines (1720 loc) · 50.7 KB
/
vis.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
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <ctype.h>
#include <time.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pwd.h>
#include <libgen.h>
#include <termkey.h>
#include "vis.h"
#include "text-util.h"
#include "text-motions.h"
#include "text-objects.h"
#include "util.h"
#include "vis-core.h"
#include "sam.h"
const MarkDef vis_marks[] = {
[VIS_MARK_SELECTION_START] = { '<', VIS_HELP("Last selection start") },
[VIS_MARK_SELECTION_END] = { '>', VIS_HELP("Last selection end") },
};
const RegisterDef vis_registers[] = {
[VIS_REG_DEFAULT] = { '"', VIS_HELP("Unnamed register") },
[VIS_REG_ZERO] = { '0', VIS_HELP("Yank register") },
[VIS_REG_1] = { '1', VIS_HELP("1st sub-expression match") },
[VIS_REG_2] = { '2', VIS_HELP("2nd sub-expression match") },
[VIS_REG_3] = { '3', VIS_HELP("3rd sub-expression match") },
[VIS_REG_4] = { '4', VIS_HELP("4th sub-expression match") },
[VIS_REG_5] = { '5', VIS_HELP("5th sub-expression match") },
[VIS_REG_6] = { '6', VIS_HELP("6th sub-expression match") },
[VIS_REG_7] = { '7', VIS_HELP("7th sub-expression match") },
[VIS_REG_8] = { '8', VIS_HELP("8th sub-expression match") },
[VIS_REG_9] = { '9', VIS_HELP("9th sub-expression match") },
[VIS_REG_AMPERSAND] = { '&', VIS_HELP("Last regex match") },
[VIS_REG_BLACKHOLE] = { '_', VIS_HELP("/dev/null register") },
[VIS_REG_CLIPBOARD] = { '*', VIS_HELP("System clipboard register, see vis-clipboard(1)") },
[VIS_REG_DOT] = { '.', VIS_HELP("Last inserted text") },
[VIS_REG_SEARCH] = { '/', VIS_HELP("Last search pattern") },
[VIS_REG_COMMAND] = { ':', VIS_HELP("Last :-command") },
[VIS_REG_SHELL] = { '!', VIS_HELP("Last shell command given to either <, >, |, or !") },
};
static Macro *macro_get(Vis *vis, enum VisRegister);
static void macro_replay(Vis *vis, const Macro *macro);
static void macro_replay_internal(Vis *vis, const Macro *macro);
static void vis_keys_push(Vis *vis, const char *input, size_t pos, bool record);
bool vis_event_emit(Vis *vis, enum VisEvents id, ...) {
if (!vis->event)
return true;
if (!vis->initialized) {
vis->initialized = true;
vis->ui->init(vis->ui, vis);
if (vis->event->init)
vis->event->init(vis);
}
va_list ap;
va_start(ap, id);
bool ret = true;
switch (id) {
case VIS_EVENT_INIT:
break;
case VIS_EVENT_START:
if (vis->event->start)
vis->event->start(vis);
break;
case VIS_EVENT_FILE_OPEN:
case VIS_EVENT_FILE_SAVE_PRE:
case VIS_EVENT_FILE_SAVE_POST:
case VIS_EVENT_FILE_CLOSE:
{
File *file = va_arg(ap, File*);
if (file->internal)
break;
if (id == VIS_EVENT_FILE_OPEN && vis->event->file_open) {
vis->event->file_open(vis, file);
} else if (id == VIS_EVENT_FILE_SAVE_PRE && vis->event->file_save_pre) {
const char *path = va_arg(ap, const char*);
ret = vis->event->file_save_pre(vis, file, path);
} else if (id == VIS_EVENT_FILE_SAVE_POST && vis->event->file_save_post) {
const char *path = va_arg(ap, const char*);
vis->event->file_save_post(vis, file, path);
} else if (id == VIS_EVENT_FILE_CLOSE && vis->event->file_close) {
vis->event->file_close(vis, file);
}
break;
}
case VIS_EVENT_WIN_OPEN:
case VIS_EVENT_WIN_CLOSE:
case VIS_EVENT_WIN_HIGHLIGHT:
case VIS_EVENT_WIN_STATUS:
{
Win *win = va_arg(ap, Win*);
if (win->file->internal && id != VIS_EVENT_WIN_STATUS)
break;
if (vis->event->win_open && id == VIS_EVENT_WIN_OPEN) {
vis->event->win_open(vis, win);
} else if (vis->event->win_close && id == VIS_EVENT_WIN_CLOSE) {
vis->event->win_close(vis, win);
} else if (vis->event->win_highlight && id == VIS_EVENT_WIN_HIGHLIGHT) {
vis->event->win_highlight(vis, win);
} else if (vis->event->win_status && id == VIS_EVENT_WIN_STATUS) {
vis->event->win_status(vis, win);
}
break;
}
case VIS_EVENT_QUIT:
if (vis->event->quit)
vis->event->quit(vis);
break;
}
va_end(ap);
return ret;
}
/** window / file handling */
static void file_free(Vis *vis, File *file) {
if (!file)
return;
if (file->refcount > 1) {
--file->refcount;
return;
}
vis_event_emit(vis, VIS_EVENT_FILE_CLOSE, file);
text_free(file->text);
free((char*)file->name);
if (file->prev)
file->prev->next = file->next;
if (file->next)
file->next->prev = file->prev;
if (vis->files == file)
vis->files = file->next;
free(file);
}
static File *file_new_text(Vis *vis, Text *text) {
File *file = calloc(1, sizeof(*file));
if (!file)
return NULL;
file->fd = -1;
file->text = text;
file->stat = text_stat(text);
if (vis->files)
vis->files->prev = file;
file->next = vis->files;
vis->files = file;
return file;
}
static char *absolute_path(const char *name) {
if (!name)
return NULL;
char *copy1 = strdup(name);
char *copy2 = strdup(name);
char *path_absolute = NULL;
char path_normalized[PATH_MAX] = "";
if (!copy1 || !copy2)
goto err;
char *dir = dirname(copy1);
char *base = basename(copy2);
if (!(path_absolute = realpath(dir, NULL)))
goto err;
snprintf(path_normalized, sizeof(path_normalized)-1, "%s/%s",
path_absolute, base);
err:
free(copy1);
free(copy2);
free(path_absolute);
return path_normalized[0] ? strdup(path_normalized) : NULL;
}
static File *file_new(Vis *vis, const char *name) {
char *name_absolute = NULL;
if (name) {
if (!(name_absolute = absolute_path(name)))
return NULL;
File *existing = NULL;
/* try to detect whether the same file is already open in another window
* TODO: do this based on inodes */
for (File *file = vis->files; file; file = file->next) {
if (file->name && strcmp(file->name, name_absolute) == 0) {
existing = file;
break;
}
}
if (existing) {
free(name_absolute);
return existing;
}
}
File *file = NULL;
Text *text = text_load(name);
if (!text && name && errno == ENOENT)
text = text_load(NULL);
if (!text)
goto err;
if (!(file = file_new_text(vis, text)))
goto err;
file->name = name_absolute;
vis_event_emit(vis, VIS_EVENT_FILE_OPEN, file);
return file;
err:
free(name_absolute);
text_free(text);
file_free(vis, file);
return NULL;
}
static File *file_new_internal(Vis *vis, const char *filename) {
File *file = file_new(vis, filename);
if (file) {
file->refcount = 1;
file->internal = true;
}
return file;
}
void file_name_set(File *file, const char *name) {
if (name == file->name)
return;
free((char*)file->name);
file->name = absolute_path(name);
}
const char *file_name_get(File *file) {
/* TODO: calculate path relative to working directory, cache result */
if (!file->name)
return NULL;
char cwd[PATH_MAX];
if (!getcwd(cwd, sizeof cwd))
return file->name;
const char *path = strstr(file->name, cwd);
if (path != file->name)
return file->name;
size_t cwdlen = strlen(cwd);
return file->name[cwdlen] == '/' ? file->name+cwdlen+1 : file->name;
}
void vis_window_status(Win *win, const char *status) {
win->ui->status(win->ui, status);
}
void window_selection_save(Win *win) {
File *file = win->file;
Filerange sel = view_cursors_selection_get(view_cursors(win->view));
file->marks[VIS_MARK_SELECTION_START] = text_mark_set(file->text, sel.start);
file->marks[VIS_MARK_SELECTION_END] = text_mark_set(file->text, sel.end);
}
static void window_free(Win *win) {
if (!win)
return;
Vis *vis = win->vis;
for (Win *other = vis->windows; other; other = other->next) {
if (other->parent == win)
other->parent = NULL;
}
if (vis->ui)
vis->ui->window_free(win->ui);
view_free(win->view);
for (size_t i = 0; i < LENGTH(win->modes); i++)
map_free(win->modes[i].bindings);
ringbuf_free(win->jumplist);
free(win->lexer_name);
free(win);
}
static void window_draw_colorcolumn(Win *win) {
View *view = win->view;
int cc = view_colorcolumn_get(view);
if (cc <= 0)
return;
CellStyle style = win->ui->style_get(win->ui, UI_STYLE_COLOR_COLUMN);
size_t lineno = 0;
int line_cols = 0; /* Track the number of columns we've passed on each line */
bool line_cc_set = false; /* Has the colorcolumn attribute been set for this line yet */
int width = view_width_get(view);
for (Line *l = view_lines_first(view); l; l = l->next) {
if (l->lineno != lineno) {
line_cols = 0;
line_cc_set = false;
lineno = l->lineno;
}
if (line_cc_set)
continue;
line_cols += width;
/* This screen line contains the cell we want to highlight */
if (line_cols >= cc) {
l->cells[(cc - 1) % width].style = style;
line_cc_set = true;
}
}
}
static void window_draw_cursorline(Win *win) {
Vis *vis = win->vis;
View *view = win->view;
enum UiOption options = view_options_get(view);
if (!(options & UI_OPTION_CURSOR_LINE))
return;
if (vis->mode->visual || vis->win != win)
return;
if (view_cursors_multiple(view))
return;
int width = view_width_get(view);
CellStyle style = win->ui->style_get(win->ui, UI_STYLE_CURSOR_LINE);
Cursor *cursor = view_cursors_primary_get(view);
size_t lineno = view_cursors_line_get(cursor)->lineno;
for (Line *l = view_lines_first(view); l; l = l->next) {
if (l->lineno == lineno) {
for (int x = 0; x < width; x++) {
l->cells[x].style.attr |= style.attr;
l->cells[x].style.bg = style.bg;
}
} else if (l->lineno > lineno) {
break;
}
}
}
static void window_draw_selection(View *view, Cursor *cur, CellStyle *style) {
Filerange sel = view_cursors_selection_get(cur);
if (!text_range_valid(&sel))
return;
Line *start_line; int start_col;
Line *end_line; int end_col;
view_coord_get(view, sel.start, &start_line, NULL, &start_col);
view_coord_get(view, sel.end, &end_line, NULL, &end_col);
if (!start_line && !end_line)
return;
if (!start_line) {
start_line = view_lines_first(view);
start_col = 0;
}
if (!end_line) {
end_line = view_lines_last(view);
end_col = end_line->width;
}
for (Line *l = start_line; l != end_line->next; l = l->next) {
int col = (l == start_line) ? start_col : 0;
int end = (l == end_line) ? end_col : l->width;
while (col < end) {
if (cell_color_equal(l->cells[col].style.fg, style->bg)) {
CellStyle old = l->cells[col].style;
l->cells[col].style.fg = old.bg;
l->cells[col].style.bg = old.fg;
} else {
l->cells[col].style.bg = style->bg;
}
col++;
}
}
}
static void window_draw_cursor_matching(Win *win, Cursor *cur, CellStyle *style) {
if (win->vis->mode->visual)
return;
Line *line_match; int col_match;
size_t pos = view_cursors_pos(cur);
size_t pos_match = text_bracket_match_symbol(win->file->text, pos, "(){}[]\"'`");
if (pos == pos_match)
return;
if (!view_coord_get(win->view, pos_match, &line_match, NULL, &col_match))
return;
if (cell_color_equal(line_match->cells[col_match].style.fg, style->fg)) {
CellStyle old = line_match->cells[col_match].style;
line_match->cells[col_match].style.fg = old.bg;
line_match->cells[col_match].style.bg = old.fg;
} else {
line_match->cells[col_match].style.bg = style->bg;
}
}
static void window_draw_cursor(Win *win, Cursor *cur, CellStyle *style, CellStyle *sel_style) {
if (win->vis->win != win)
return;
Line *line = view_cursors_line_get(cur);
int col = view_cursors_cell_get(cur);
if (!line || col == -1)
return;
line->cells[col].style = *style;
window_draw_cursor_matching(win, cur, sel_style);
return;
}
static void window_draw_cursors(Win *win) {
View *view = win->view;
Filerange viewport = view_viewport_get(view);
bool multiple_cursors = view_cursors_multiple(view);
Cursor *cursor = view_cursors_primary_get(view);
CellStyle style_cursor = win->ui->style_get(win->ui, UI_STYLE_CURSOR);
CellStyle style_cursor_primary = win->ui->style_get(win->ui, UI_STYLE_CURSOR_PRIMARY);
CellStyle style_selection = win->ui->style_get(win->ui, UI_STYLE_SELECTION);
for (Cursor *c = view_cursors_prev(cursor); c; c = view_cursors_prev(c)) {
window_draw_selection(win->view, c, &style_selection);
size_t pos = view_cursors_pos(c);
if (pos < viewport.start)
break;
window_draw_cursor(win, c, &style_cursor, &style_selection);
}
window_draw_selection(win->view, cursor, &style_selection);
window_draw_cursor(win, cursor, multiple_cursors ? &style_cursor_primary : &style_cursor, &style_selection);
for (Cursor *c = view_cursors_next(cursor); c; c = view_cursors_next(c)) {
window_draw_selection(win->view, c, &style_selection);
size_t pos = view_cursors_pos(c);
if (pos > viewport.end)
break;
window_draw_cursor(win, c, &style_cursor, &style_selection);
}
}
static void window_draw_eof(Win *win) {
View *view = win->view;
if (view_width_get(view) == 0)
return;
CellStyle style = win->ui->style_get(win->ui, UI_STYLE_EOF);
for (Line *l = view_lines_last(view)->next; l; l = l->next) {
strcpy(l->cells[0].data, "~");
l->cells[0].style = style;
}
}
void vis_window_draw(Win *win) {
if (!win->ui || !view_update(win->view))
return;
Vis *vis = win->vis;
vis_event_emit(vis, VIS_EVENT_WIN_HIGHLIGHT, win);
window_draw_colorcolumn(win);
window_draw_cursorline(win);
window_draw_cursors(win);
window_draw_eof(win);
vis_event_emit(vis, VIS_EVENT_WIN_STATUS, win);
}
void vis_window_invalidate(Win *win) {
for (Win *w = win->vis->windows; w; w = w->next) {
if (w->file == win->file)
view_draw(w->view);
}
}
Win *window_new_file(Vis *vis, File *file, enum UiOption options) {
Win *win = calloc(1, sizeof(Win));
if (!win)
return NULL;
win->vis = vis;
win->file = file;
win->jumplist = ringbuf_alloc(31);
win->view = view_new(file->text);
win->ui = vis->ui->window_new(vis->ui, win, options);
if (!win->jumplist || !win->view || !win->ui) {
window_free(win);
return NULL;
}
file->refcount++;
view_tabwidth_set(win->view, vis->tabwidth);
if (vis->windows)
vis->windows->prev = win;
win->next = vis->windows;
vis->windows = win;
vis->win = win;
vis->ui->window_focus(win->ui);
for (size_t i = 0; i < LENGTH(win->modes); i++)
win->modes[i].parent = &vis_modes[i];
vis_event_emit(vis, VIS_EVENT_WIN_OPEN, win);
return win;
}
bool vis_window_reload(Win *win) {
const char *name = win->file->name;
if (!name)
return false; /* can't reload unsaved file */
/* temporarily unset file name, otherwise file_new returns the same File */
win->file->name = NULL;
File *file = file_new(win->vis, name);
win->file->name = name;
if (!file)
return false;
file_free(win->vis, win->file);
file->refcount = 1;
win->file = file;
view_reload(win->view, file->text);
return true;
}
bool vis_window_split(Win *original) {
Win *win = window_new_file(original->vis, original->file, UI_OPTION_STATUSBAR);
if (!win)
return false;
for (size_t i = 0; i < LENGTH(win->modes); i++) {
if (original->modes[i].bindings)
win->modes[i].bindings = map_new();
if (win->modes[i].bindings)
map_copy(win->modes[i].bindings, original->modes[i].bindings);
}
win->file = original->file;
view_options_set(win->view, view_options_get(original->view));
view_cursor_to(win->view, view_cursor_get(original->view));
return true;
}
void vis_window_focus(Win *win) {
if (!win)
return;
Vis *vis = win->vis;
vis->win = win;
vis->ui->window_focus(win->ui);
}
void vis_window_next(Vis *vis) {
Win *sel = vis->win;
if (!sel)
return;
vis_window_focus(sel->next ? sel->next : vis->windows);
}
void vis_window_prev(Vis *vis) {
Win *sel = vis->win;
if (!sel)
return;
sel = sel->prev;
if (!sel)
for (sel = vis->windows; sel->next; sel = sel->next);
vis_window_focus(sel);
}
int vis_window_width_get(const Win *win) {
return win->ui->window_width(win->ui);
}
int vis_window_height_get(const Win *win) {
return win->ui->window_height(win->ui);
}
void vis_draw(Vis *vis) {
for (Win *win = vis->windows; win; win = win->next)
view_draw(win->view);
}
void vis_redraw(Vis *vis) {
vis->ui->redraw(vis->ui);
}
void vis_update(Vis *vis) {
vis->ui->draw(vis->ui);
}
void vis_suspend(Vis *vis) {
vis->ui->suspend(vis->ui);
}
bool vis_window_new(Vis *vis, const char *filename) {
File *file = file_new(vis, filename);
if (!file)
return false;
Win *win = window_new_file(vis, file, UI_OPTION_STATUSBAR);
if (!win) {
file_free(vis, file);
return false;
}
return true;
}
bool vis_window_new_fd(Vis *vis, int fd) {
if (fd == -1)
return false;
if (!vis_window_new(vis, NULL))
return false;
vis->win->file->fd = fd;
return true;
}
bool vis_window_closable(Win *win) {
if (!win || !text_modified(win->file->text))
return true;
return win->file->refcount > 1;
}
void vis_window_swap(Win *a, Win *b) {
if (a == b || !a || !b)
return;
Vis *vis = a->vis;
Win *tmp = a->next;
a->next = b->next;
b->next = tmp;
if (a->next)
a->next->prev = a;
if (b->next)
b->next->prev = b;
tmp = a->prev;
a->prev = b->prev;
b->prev = tmp;
if (a->prev)
a->prev->next = a;
if (b->prev)
b->prev->next = b;
if (vis->windows == a)
vis->windows = b;
else if (vis->windows == b)
vis->windows = a;
vis->ui->window_swap(a->ui, b->ui);
if (vis->win == a)
vis_window_focus(b);
else if (vis->win == b)
vis_window_focus(a);
}
void vis_window_close(Win *win) {
if (!win)
return;
Vis *vis = win->vis;
vis_event_emit(vis, VIS_EVENT_WIN_CLOSE, win);
file_free(vis, win->file);
if (win->prev)
win->prev->next = win->next;
if (win->next)
win->next->prev = win->prev;
if (vis->windows == win)
vis->windows = win->next;
if (vis->win == win)
vis->win = win->next ? win->next : win->prev;
if (win == vis->message_window)
vis->message_window = NULL;
window_free(win);
if (vis->win)
vis->ui->window_focus(vis->win->ui);
vis_draw(vis);
}
Vis *vis_new(Ui *ui, VisEvent *event) {
if (!ui)
return NULL;
Vis *vis = calloc(1, sizeof(Vis));
if (!vis)
return NULL;
vis->exit_status = -1;
vis->ui = ui;
vis->tabwidth = 8;
vis->expandtab = false;
vis->change_colors = true;
vis->registers[VIS_REG_BLACKHOLE].type = REGISTER_BLACKHOLE;
vis->registers[VIS_REG_CLIPBOARD].type = REGISTER_CLIPBOARD;
array_init(&vis->operators);
array_init(&vis->motions);
array_init(&vis->textobjects);
array_init(&vis->bindings);
array_init(&vis->actions_user);
action_reset(&vis->action);
buffer_init(&vis->input_queue);
if (!(vis->command_file = file_new_internal(vis, NULL)))
goto err;
if (!(vis->search_file = file_new_internal(vis, NULL)))
goto err;
if (!(vis->error_file = file_new_internal(vis, NULL)))
goto err;
if (!(vis->actions = map_new()))
goto err;
if (!(vis->keymap = map_new()))
goto err;
if (!sam_init(vis))
goto err;
struct passwd *pw;
char *shell = getenv("SHELL");
if ((!shell || !*shell) && (pw = getpwuid(getuid())))
shell = pw->pw_shell;
if (!shell || !*shell)
shell = "/bin/sh";
if (!(vis->shell = strdup(shell)))
goto err;
vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL];
vis->event = event;
if (event) {
if (event->mode_insert_input)
vis_modes[VIS_MODE_INSERT].input = event->mode_insert_input;
if (event->mode_replace_input)
vis_modes[VIS_MODE_REPLACE].input = event->mode_replace_input;
}
return vis;
err:
vis_free(vis);
return NULL;
}
void vis_free(Vis *vis) {
if (!vis)
return;
vis_event_emit(vis, VIS_EVENT_QUIT);
vis->event = NULL;
while (vis->windows)
vis_window_close(vis->windows);
file_free(vis, vis->command_file);
file_free(vis, vis->search_file);
file_free(vis, vis->error_file);
for (int i = 0; i < LENGTH(vis->registers); i++)
register_release(&vis->registers[i]);
vis->ui->free(vis->ui);
if (vis->usercmds) {
const char *name;
while (map_first(vis->usercmds, &name) && vis_cmd_unregister(vis, name));
}
map_free(vis->usercmds);
map_free(vis->cmds);
if (vis->options) {
const char *name;
while (map_first(vis->options, &name) && vis_option_unregister(vis, name));
}
map_free(vis->options);
map_free(vis->actions);
map_free(vis->keymap);
buffer_release(&vis->input_queue);
for (int i = 0; i < VIS_MODE_INVALID; i++)
map_free(vis_modes[i].bindings);
array_release_full(&vis->operators);
array_release_full(&vis->motions);
array_release_full(&vis->textobjects);
while (array_length(&vis->bindings))
vis_binding_free(vis, array_get_ptr(&vis->bindings, 0));
array_release(&vis->bindings);
while (array_length(&vis->actions_user))
vis_action_free(vis, array_get_ptr(&vis->actions_user, 0));
array_release(&vis->actions_user);
free(vis->shell);
free(vis);
}
void vis_insert(Vis *vis, size_t pos, const char *data, size_t len) {
text_insert(vis->win->file->text, pos, data, len);
vis_window_invalidate(vis->win);
}
void vis_insert_key(Vis *vis, const char *data, size_t len) {
for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) {
size_t pos = view_cursors_pos(c);
vis_insert(vis, pos, data, len);
view_cursors_scroll_to(c, pos + len);
}
}
void vis_replace(Vis *vis, size_t pos, const char *data, size_t len) {
Text *txt = vis->win->file->text;
Iterator it = text_iterator_get(txt, pos);
int chars = text_char_count(data, len);
for (char c; chars-- > 0 && text_iterator_byte_get(&it, &c) && c != '\r' && c != '\n'; )
text_iterator_char_next(&it, NULL);
text_delete(txt, pos, it.pos - pos);
vis_insert(vis, pos, data, len);
}
void vis_replace_key(Vis *vis, const char *data, size_t len) {
for (Cursor *c = view_cursors(vis->win->view); c; c = view_cursors_next(c)) {
size_t pos = view_cursors_pos(c);
vis_replace(vis, pos, data, len);
view_cursors_scroll_to(c, pos + len);
}
}
void vis_delete(Vis *vis, size_t pos, size_t len) {
text_delete(vis->win->file->text, pos, len);
vis_window_invalidate(vis->win);
}
bool vis_action_register(Vis *vis, const KeyAction *action) {
return map_put(vis->actions, action->name, action);
}
bool vis_keymap_add(Vis *vis, const char *key, const char *mapping) {
return map_put(vis->keymap, key, mapping);
}
void vis_keymap_disable(Vis *vis) {
vis->keymap_disabled = true;
}
static void window_jumplist_add(Win *win, size_t pos) {
Mark mark = text_mark_set(win->file->text, pos);
if (mark && win->jumplist)
ringbuf_add(win->jumplist, (void*)mark);
}
static void window_jumplist_invalidate(Win *win) {
if (win->jumplist)
ringbuf_invalidate(win->jumplist);
}
void vis_do(Vis *vis) {
Win *win = vis->win;
File *file = win->file;
Text *txt = file->text;
View *view = win->view;
Action *a = &vis->action;
if (a->op == &vis_operators[VIS_OP_FILTER] && !vis->mode->visual)
vis_mode_switch(vis, VIS_MODE_VISUAL_LINE);
int count = MAX(a->count, 1);
if (a->op == &vis_operators[VIS_OP_MODESWITCH])
count = 1; /* count should apply to inserted text not motion */
bool repeatable = a->op && !vis->macro_operator && !vis->win->parent;
bool multiple_cursors = view_cursors_multiple(view);
bool linewise = !(a->type & CHARWISE) && (
a->type & LINEWISE || (a->movement && a->movement->type & LINEWISE) ||
vis->mode == &vis_modes[VIS_MODE_VISUAL_LINE]);
for (Cursor *cursor = view_cursors(view), *next; cursor; cursor = next) {
next = view_cursors_next(cursor);
size_t pos = view_cursors_pos(cursor);
Register *reg = multiple_cursors ? view_cursors_register(cursor) : a->reg;
if (!reg)
reg = &vis->registers[file->internal ? VIS_REG_PROMPT : VIS_REG_DEFAULT];
OperatorContext c = {
.count = count,
.pos = pos,
.newpos = EPOS,
.range = text_range_empty(),
.reg = reg,
.linewise = linewise,
.arg = &a->arg,
.context = a->op ? a->op->context : NULL,
};
bool err = false;
if (a->movement) {
size_t start = pos;
for (int i = 0; i < count; i++) {
size_t pos_prev = pos;
if (a->movement->txt)
pos = a->movement->txt(txt, pos);
else if (a->movement->cur)
pos = a->movement->cur(cursor);
else if (a->movement->file)
pos = a->movement->file(vis, file, pos);
else if (a->movement->vis)
pos = a->movement->vis(vis, txt, pos);
else if (a->movement->view)
pos = a->movement->view(vis, view);
else if (a->movement->win)
pos = a->movement->win(vis, win, pos);
else if (a->movement->user)
pos = a->movement->user(vis, win, a->movement->data, pos);
if (pos == EPOS || a->movement->type & IDEMPOTENT || pos == pos_prev) {
err = a->movement->type & COUNT_EXACT;
break;
}
}
if (err) {
repeatable = false;
continue; // break?
}
if (pos == EPOS) {
c.range.start = start;
c.range.end = start;
pos = start;
} else {
c.range = text_range_new(start, pos);
c.newpos = pos;
}
if (!a->op) {
if (a->movement->type & CHARWISE)
view_cursors_scroll_to(cursor, pos);
else
view_cursors_to(cursor, pos);
if (vis->mode->visual)
c.range = view_cursors_selection_get(cursor);
if (a->movement->type & JUMP)
window_jumplist_add(win, pos);
else
window_jumplist_invalidate(win);
} else if (a->movement->type & INCLUSIVE && c.range.end > start) {
c.range.end = text_char_next(txt, c.range.end);
} else if (linewise && (a->movement->type & LINEWISE_INCLUSIVE)) {
c.range.end = text_char_next(txt, c.range.end);
}
} else if (a->textobj) {
if (vis->mode->visual)
c.range = view_cursors_selection_get(cursor);
else
c.range.start = c.range.end = pos;
for (int i = 0; i < count; i++) {
Filerange r = text_range_empty();
if (a->textobj->txt)
r = a->textobj->txt(txt, pos);
else if (a->textobj->vis)
r = a->textobj->vis(vis, txt, pos);
else if (a->textobj->user)
r = a->textobj->user(vis, win, a->textobj->data, pos);
if (!text_range_valid(&r))
break;
if (a->textobj->type & TEXTOBJECT_DELIMITED_OUTER) {
r.start--;
r.end++;
}
if (vis->mode->visual || (i > 0 && !(a->textobj->type & TEXTOBJECT_NON_CONTIGUOUS)))
c.range = text_range_union(&c.range, &r);
else
c.range = r;
if (i < count - 1) {
if (a->textobj->type & TEXTOBJECT_EXTEND_BACKWARD) {
pos = c.range.start;
if ((a->textobj->type & TEXTOBJECT_DELIMITED_INNER) && pos > 0)
pos--;
} else {
pos = c.range.end;
if (a->textobj->type & TEXTOBJECT_DELIMITED_INNER)
pos++;
}
}
}
} else if (vis->mode->visual) {
c.range = view_cursors_selection_get(cursor);
if (!text_range_valid(&c.range))
c.range.start = c.range.end = pos;
}
if (linewise && vis->mode != &vis_modes[VIS_MODE_VISUAL])
c.range = text_range_linewise(txt, &c.range);
if (vis->mode->visual) {
view_cursors_selection_set(cursor, &c.range);
if (vis->mode == &vis_modes[VIS_MODE_VISUAL] || a->textobj)
view_cursors_selection_sync(cursor);
}
if (a->op) {
size_t pos = a->op->func(vis, txt, &c);
if (pos == EPOS) {
view_cursors_dispose(cursor);
} else if (pos <= text_size(txt)) {
/* moving the cursor will affect the selection.
* because we want to be able to later restore
* the old selection we update it again before
* leaving visual mode.
*/
Filerange sel = view_cursors_selection_get(cursor);
view_cursors_to(cursor, pos);
if (vis->mode->visual) {
if (sel.start == EPOS && sel.end == EPOS)
sel = c.range;
else if (sel.start == EPOS)
sel = text_range_new(c.range.start, sel.end);
else if (sel.end == EPOS)
sel = text_range_new(c.range.start, sel.start);
if (vis->mode == &vis_modes[VIS_MODE_VISUAL_LINE])
sel = text_range_linewise(txt, &sel);
if (!text_range_contains(&sel, pos)) {
Filerange cur = text_range_new(pos, pos);
sel = text_range_union(&sel, &cur);
}
view_cursors_selection_set(cursor, &sel);
}
}
}
}
if (a->op) {
/* we do not support visual repeat, still do something resonable */
if (vis->mode->visual && !a->movement && !a->textobj)
a->movement = &vis_motions[VIS_MOVE_NOP];
/* operator implementations must not change the mode,
* they might get called multiple times (once for every cursor)