forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.c
400 lines (350 loc) · 10.1 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
#include <stdlib.h>
#include "vis.h"
#include "util.h"
static VisWin *vis_window_new_text(Vis *vis, Text *text);
static void vis_window_free(VisWin *win);
static void vis_window_split_internal(Vis *vis, const char *filename);
static void vis_windows_invalidate(Vis *vis, size_t start, size_t end);
static void vis_window_draw(VisWin *win);
static void vis_search_forward(Vis *vis, Regex *regex);
static void vis_search_backward(Vis *vis, Regex *regex);
static void vis_windows_arrange_horizontal(Vis *vis);
static void vis_windows_arrange_vertical(Vis *vis);
static void vis_window_resize(VisWin *win, int width, int height) {
window_resize(win->win, width, height - 1);
wresize(win->statuswin, 1, width);
win->width = width;
win->height = height;
}
static void vis_window_move(VisWin *win, int x, int y) {
window_move(win->win, x, y);
mvwin(win->statuswin, y + win->height - 1, x);
}
static void vis_window_statusbar_draw(VisWin *win) {
size_t line, col;
if (win->vis->statusbar) {
window_cursor_getxy(win->win, &line, &col);
win->vis->statusbar(win->statuswin, win->vis->win == win,
text_filename(win->text), line, col);
}
}
static void vis_window_cursor_moved(Win *win, void *data) {
vis_window_statusbar_draw(data);
}
void vis_statusbar_set(Vis *vis, vis_statusbar_t statusbar) {
vis->statusbar = statusbar;
}
size_t vis_line_goto(Vis *vis, size_t lineno) {
size_t pos = text_pos_by_lineno(vis->win->text, lineno);
window_cursor_to(vis->win->win, pos);
return pos;
}
static void vis_search_forward(Vis *vis, Regex *regex) {
VisWin *win = vis->win;
int pos = window_cursor_get(win->win) + 1;
int end = text_size(win->text);
RegexMatch match[1];
bool found = false;
if (text_search_forward(win->text, pos, end - pos, regex, 1, match, 0)) {
pos = 0;
end = window_cursor_get(win->win);
if (!text_search_forward(win->text, pos, end, regex, 1, match, 0))
found = true;
} else {
found = true;
}
if (found)
window_cursor_to(win->win, match[0].start);
}
static void vis_search_backward(Vis *vis, Regex *regex) {
VisWin *win = vis->win;
int pos = 0;
int end = window_cursor_get(win->win);
RegexMatch match[1];
bool found = false;
if (text_search_backward(win->text, pos, end, regex, 1, match, 0)) {
pos = window_cursor_get(win->win) + 1;
end = text_size(win->text);
if (!text_search_backward(win->text, pos, end - pos, regex, 1, match, 0))
found = true;
} else {
found = true;
}
if (found)
window_cursor_to(win->win, match[0].start);
}
void vis_search(Vis *vis, const char *s, int direction) {
Regex *regex = text_regex_new();
if (!regex)
return;
if (!text_regex_compile(regex, s, REG_EXTENDED)) {
if (direction >= 0)
vis_search_forward(vis, regex);
else
vis_search_backward(vis, regex);
}
text_regex_free(regex);
}
void vis_snapshot(Vis *vis) {
text_snapshot(vis->win->text);
}
void vis_undo(Vis *vis) {
VisWin *win = vis->win;
// TODO window invalidation
if (text_undo(win->text))
window_draw(win->win);
}
void vis_redo(Vis *vis) {
VisWin *win = vis->win;
// TODO window invalidation
if (text_redo(win->text))
window_draw(win->win);
}
static void vis_windows_arrange_horizontal(Vis *vis) {
int n = 0, x = 0, y = 0;
for (VisWin *win = vis->windows; win; win = win->next)
n++;
int height = vis->height / n;
for (VisWin *win = vis->windows; win; win = win->next) {
vis_window_resize(win, vis->width, win->next ? height : vis->height - y);
vis_window_move(win, x, y);
y += height;
}
}
static void vis_windows_arrange_vertical(Vis *vis) {
int n = 0, x = 0, y = 0;
for (VisWin *win = vis->windows; win; win = win->next)
n++;
int width = vis->width / n;
for (VisWin *win = vis->windows; win; win = win->next) {
vis_window_resize(win, win->next ? width : vis->width - x, vis->height);
vis_window_move(win, x, y);
x += width;
}
}
static void vis_window_split_internal(Vis *vis, const char *filename) {
VisWin *sel = vis->win;
if (filename) {
// TODO check that this file isn't already open
vis_window_new(vis, filename);
} else if (sel) {
VisWin *win = vis_window_new_text(vis, sel->text);
win->text = sel->text;
window_syntax_set(win->win, window_syntax_get(sel->win));
window_cursor_to(win->win, window_cursor_get(sel->win));
}
}
void vis_window_split(Vis *vis, const char *filename) {
vis_window_split_internal(vis, filename);
vis->windows_arrange = vis_windows_arrange_horizontal;
vis_draw(vis);
}
void vis_window_vsplit(Vis *vis, const char *filename) {
vis_window_split_internal(vis, filename);
vis->windows_arrange = vis_windows_arrange_vertical;
vis_draw(vis);
}
void vis_resize(Vis *vis, int width, int height) {
vis->width = width;
vis->height = height;
vis_draw(vis);
}
void vis_window_next(Vis *vis) {
VisWin *sel = vis->win;
if (!sel)
return;
vis->win = vis->win->next;
if (!vis->win)
vis->win = vis->windows;
vis_window_statusbar_draw(sel);
vis_window_statusbar_draw(vis->win);
}
void vis_window_prev(Vis *vis) {
VisWin *sel = vis->win;
if (!sel)
return;
vis->win = vis->win->prev;
if (!vis->win)
for (vis->win = vis->windows; vis->win->next; vis->win = vis->win->next);
vis_window_statusbar_draw(sel);
vis_window_statusbar_draw(vis->win);
}
void vis_mark_set(Vis *vis, Mark mark, size_t pos) {
text_mark_set(vis->win->text, mark, pos);
}
void vis_mark_goto(Vis *vis, Mark mark) {
VisWin *win = vis->win;
size_t pos = text_mark_get(win->text, mark);
if (pos != (size_t)-1)
window_cursor_to(win->win, pos);
}
void vis_mark_clear(Vis *vis, Mark mark) {
text_mark_clear(vis->win->text, mark);
}
static void vis_windows_invalidate(Vis *vis, size_t start, size_t end) {
for (VisWin *win = vis->windows; win; win = win->next) {
if (vis->win != win && vis->win->text == win->text) {
Filerange view = window_viewport_get(win->win);
if ((view.start <= start && start <= view.end) ||
(view.start <= end && end <= view.end))
vis_window_draw(win);
}
}
}
bool vis_syntax_load(Vis *vis, Syntax *syntaxes, Color *colors) {
bool success = true;
vis->syntaxes = syntaxes;
for (Syntax *syn = syntaxes; syn && syn->name; syn++) {
if (regcomp(&syn->file_regex, syn->file, REG_EXTENDED|REG_NOSUB|REG_ICASE|REG_NEWLINE))
success = false;
Color *color = colors;
for (int j = 0; j < LENGTH(syn->rules); j++) {
SyntaxRule *rule = &syn->rules[j];
if (!rule->rule)
break;
if (rule->color.fg == 0 && color && color->fg != 0)
rule->color = *color++;
if (rule->color.attr == 0)
rule->color.attr = A_NORMAL;
if (rule->color.fg != 0)
rule->color.attr |= COLOR_PAIR(vis_color_reserve(rule->color.fg, rule->color.bg));
if (regcomp(&rule->regex, rule->rule, REG_EXTENDED|rule->cflags))
success = false;
}
}
return success;
}
void vis_syntax_unload(Vis *vis) {
for (Syntax *syn = vis->syntaxes; syn && syn->name; syn++) {
regfree(&syn->file_regex);
for (int j = 0; j < LENGTH(syn->rules); j++) {
SyntaxRule *rule = &syn->rules[j];
if (!rule->rule)
break;
regfree(&rule->regex);
}
}
vis->syntaxes = NULL;
}
static void vis_window_draw(VisWin *win) {
// TODO window_draw draw should restore cursor position
window_draw(win->win);
window_cursor_to(win->win, window_cursor_get(win->win));
}
void vis_draw(Vis *vis) {
erase();
wnoutrefresh(stdscr);
vis->windows_arrange(vis);
for (VisWin *win = vis->windows; win; win = win->next) {
if (vis->win != win)
vis_window_draw(win);
}
vis_window_draw(vis->win);
}
void vis_update(Vis *vis) {
for (VisWin *win = vis->windows; win; win = win->next) {
if (vis->win != win) {
wnoutrefresh(win->statuswin);
window_update(win->win);
}
}
wnoutrefresh(vis->win->statuswin);
window_update(vis->win->win);
}
static void vis_window_free(VisWin *win) {
if (!win)
return;
window_free(win->win);
if (win->statuswin)
delwin(win->statuswin);
// XXX: open in other windows
text_free(win->text);
free(win);
}
static VisWin *vis_window_new_text(Vis *vis, Text *text) {
VisWin *win = calloc(1, sizeof(VisWin));
if (!win)
return NULL;
win->vis = vis;
win->text = text;
win->win = window_new(win->text);
win->statuswin = newwin(1, vis->width, 0, 0);
if (!win->win || !win->statuswin) {
vis_window_free(win);
return NULL;
}
window_cursor_watch(win->win, vis_window_cursor_moved, win);
if (vis->windows)
vis->windows->prev = win;
win->next = vis->windows;
win->prev = NULL;
vis->windows = win;
vis->win = win;
return win;
}
bool vis_window_new(Vis *vis, const char *filename) {
Text *text = text_load(filename);
if (!text)
return false;
VisWin *win = vis_window_new_text(vis, text);
if (!win) {
text_free(text);
return false;
}
if (filename) {
for (Syntax *syn = vis->syntaxes; syn && syn->name; syn++) {
if (!regexec(&syn->file_regex, filename, 0, NULL, 0)) {
window_syntax_set(win->win, syn);
break;
}
}
}
vis_draw(vis);
return true;
}
Vis *vis_new(int width, int height) {
Vis *vis = calloc(1, sizeof(Vis));
if (!vis)
return NULL;
vis->width = width;
vis->height = height;
vis->windows_arrange = vis_windows_arrange_horizontal;
return vis;
}
void vis_free(Vis *vis) {
for (VisWin *next, *win = vis->windows; win; win = next) {
next = win->next;
vis_window_free(win);
}
free(vis);
}
void vis_insert_key(Vis *vis, const char *c, size_t len) {
Win *win = vis->win->win;
size_t start = window_cursor_get(win);
window_insert_key(win, c, len);
vis_windows_invalidate(vis, start, start + len);
}
void vis_replace_key(Vis *vis, const char *c, size_t len) {
Win *win = vis->win->win;
size_t start = window_cursor_get(win);
window_replace_key(win, c, len);
vis_windows_invalidate(vis, start, start + 6);
}
void vis_backspace_key(Vis *vis) {
Win *win = vis->win->win;
size_t end = window_cursor_get(win);
size_t start = window_backspace_key(win);
vis_windows_invalidate(vis, start, end);
}
void vis_delete_key(Vis *vis) {
size_t start = window_delete_key(vis->win->win);
vis_windows_invalidate(vis, start, start + 6);
}
void vis_insert(Vis *vis, size_t pos, const char *c, size_t len) {
text_insert_raw(vis->win->text, pos, c, len);
vis_windows_invalidate(vis, pos, pos + len);
}
void vis_delete(Vis *vis, size_t pos, size_t len) {
text_delete(vis->win->text, pos, len);
vis_windows_invalidate(vis, pos, pos + len);
}