-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c
393 lines (325 loc) · 8.98 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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include "mem.h"
#include "macros.h"
#include "pos.h"
#include "region.h"
#include "list.h"
#include "buffer.h"
#include "window.h"
#include "ncurses.h"
void window_calc_rects(
window *topleft,
const unsigned screentop,
const unsigned screenwidth,
const unsigned screenheight)
{
unsigned nwins_h = 0;
window *win_h;
for(win_h = topleft; win_h; win_h = win_h->neighbours.right, nwins_h++);
const unsigned nvlines = nwins_h + 1;
const unsigned winspace_v = screenwidth - nvlines;
unsigned winwidth = winspace_v / nwins_h;
unsigned i_h;
for(i_h = 0, win_h = topleft; win_h; win_h = win_h->neighbours.right, i_h++){
unsigned nwins_v = 0;
window *win_v;
for(win_v = win_h; win_v; win_v = win_v->neighbours.below)
nwins_v++;
/* final win gets any left over cols */
if(!win_h->neighbours.right)
winwidth += winspace_v % nwins_h;
const unsigned nhlines = nwins_v - 1;
const unsigned winspace_h = screenheight - nhlines;
const unsigned winheight = winspace_h / nwins_v;
unsigned i_v;
for(i_v = 0, win_v = win_h; win_v; win_v = win_v->neighbours.below, i_v++){
rect_t r = {
.x = i_h * (winwidth + 1),
.y = i_v * (winheight + 1) + screentop,
.w = winwidth,
.h = winheight,
};
/* final win gets any left over lines */
if(!win_v->neighbours.below)
r.h += winspace_h % nwins_v;
win_v->screen_coord = r;
}
}
}
window *window_next(window *cur)
{
#define RET_IF(x) if(x) return x
RET_IF(cur->neighbours.below);
RET_IF(cur->neighbours.above);
RET_IF(cur->neighbours.right);
RET_IF(cur->neighbours.left);
#undef RET_IF
return NULL;
}
#define window_top_bottom_left_right(H, V) \
for(;;){ \
int changed = 0; \
for(; win->neighbours.H; changed = 1, win = win->neighbours.H); \
for(; win->neighbours.V; changed = 1, win = win->neighbours.V); \
if(!changed) \
break; \
} \
\
return win
window *window_topleftmost(window *win)
{
window_top_bottom_left_right(left, above);
}
window *window_bottomrightmost(window *win)
{
window_top_bottom_left_right(right, below);
}
void window_evict(window *const evictee)
{
if(evictee->neighbours.above){
/* we are just a child in the chain */
assert(!evictee->neighbours.left);
assert(!evictee->neighbours.right);
window *above = evictee->neighbours.above;
assert(above->neighbours.below == evictee);
above->neighbours.below = evictee->neighbours.below;
if(evictee->neighbours.below)
evictee->neighbours.below->neighbours.above = above;
evictee->neighbours.above = evictee->neighbours.below = NULL;
return;
}
/* we are a top-most chain.
* remove ourselves from the left-right hierarchy
* if we have a child below, it takes our place.
*/
window *const child = evictee->neighbours.below;
if(evictee->neighbours.left){
window *left = evictee->neighbours.left;
assert(left->neighbours.right == evictee);
window *target = child ? child : evictee->neighbours.right;
left->neighbours.right = target;
if(target)
target->neighbours.left = left;
}
if(evictee->neighbours.right){
window *right = evictee->neighbours.right;
window *target = child ? child : evictee->neighbours.left;
right->neighbours.left = target;
if(target)
target->neighbours.right = right;
}
evictee->neighbours.left = evictee->neighbours.right = NULL;
evictee->neighbours.above = evictee->neighbours.below = NULL;
if(child){
assert(child->neighbours.above == evictee);
child->neighbours.above = NULL;
}
}
bool neighbour_is_vertical(enum neighbour n)
{
switch(n){
case neighbour_up:
case neighbour_down:
return true;
case neighbour_left:
case neighbour_right:
break;
}
return false;
}
void window_add_neighbour(window *to, enum neighbour dir, window *new)
{
window_evict(new);
switch(dir){
case neighbour_left:
case neighbour_right:
{
window *topmost;
for(topmost = to; topmost->neighbours.above; topmost = topmost->neighbours.above);
window **pdest = (dir == neighbour_right
? &topmost->neighbours.right
: &topmost->neighbours.left);
window *dest = *pdest;
if(!dest){
*pdest = new;
if(dir == neighbour_right)
new->neighbours.left = topmost;
else
new->neighbours.right = topmost;
}else{
/* walk down until we find an entry where we left */
while(dest->neighbours.below
&& dest->ui_start.y <= new->ui_start.y)
{
dest = dest->neighbours.below;
}
window *replace = dest->neighbours.below;
dest->neighbours.below = new;
if(replace)
replace->neighbours.above = new;
new->neighbours.above = dest;
new->neighbours.below = replace;
}
break;
}
case neighbour_down:
{
window *evicted = to->neighbours.below;
to->neighbours.below = new;
new->neighbours.above = to;
new->neighbours.below = evicted;
if(evicted)
evicted->neighbours.above = new;
break;
}
case neighbour_up:
{
window *up = to->neighbours.above;
if(up){
/* insert in the chain */
new->neighbours.above = up;
up->neighbours.below = new;
to->neighbours.above = new;
new->neighbours.below = to;
}else{
/* rewrite left and right pointers */
new->neighbours.left = to->neighbours.left;
to->neighbours.left = NULL;
new->neighbours.right = to->neighbours.right;
to->neighbours.right = NULL;
if(new->neighbours.right)
new->neighbours.right->neighbours.left = new;
if(new->neighbours.left)
new->neighbours.left->neighbours.right = new;
new->neighbours.below = to;
to->neighbours.above = new;
}
break;
}
}
}
window *window_new(buffer_t *b)
{
window *win = umalloc(sizeof *win);
win->buf = retain(b);
win->ui_pos = &win->ui_npos;
win->ui_mode = UI_NORMAL;
win->ui_paren = (point_t){ -1, -1 };
return win;
}
void window_free(window *w)
{
buffer_release(w->buf);
free(w);
}
void window_togglev(window *win, bool corner_toggle)
{
if(corner_toggle){
point_t *alt = window_uipos_alt(win);
int tmp = win->ui_pos->x;
win->ui_pos->x = alt->x;
alt->x = tmp;
}else{
win->ui_pos = (win->ui_pos == &win->ui_npos)
? &win->ui_vpos
: &win->ui_npos;
}
}
list_t *window_current_line(const window *w, bool create)
{
return list_seek(w->buf->head, w->ui_pos->y, create);
}
char *window_current_word(const window *w)
{
return list_word_at(w->buf->head, w->ui_pos);
}
char *window_current_fname(const window *w)
{
return list_fname_at(w->buf->head, w->ui_pos);
}
point_t window_toscreen(const window *win, point_t const *pt)
{
list_t *l = list_seek(win->buf->head, win->ui_pos->y, 0);
int xoff = 0;
if(l && l->len_line > 0)
for(int x = MIN((unsigned)win->ui_pos->x, l->len_line - 1);
x >= 0;
x--)
xoff += nc_charlen(l->line[x]) - 1;
return (point_t){
win->screen_coord.x + pt->x - win->ui_start.x + xoff,
win->screen_coord.y + pt->y - win->ui_start.y
};
}
int window_setmode(window *win, enum buf_mode m)
{
if(!m || (m & (m - 1))){
return -1;
}else{
if(win->ui_mode & UI_INSERT_ANY)
win->buf->prev_insert = *win->ui_pos;
if(m & UI_VISUAL_ANY){
if((win->ui_mode & UI_VISUAL_ANY) == 0){
/* from non-visual to visual */
*window_uipos_alt(win) = *win->ui_pos;
}
}else{
/* from a visual, save state */
win->buf->prev_visual.mode = win->ui_mode;
win->buf->prev_visual.npos = *win->ui_pos;
win->buf->prev_visual.vpos = *window_uipos_alt(win);
}
win->ui_mode = m;
return 0;
}
}
void window_inschar(window *win, char ch)
{
if(win->ui_mode != UI_INSERT_COL || isnewline(ch)){
/* can't col-insert a newline, revert */
if(win->ui_mode == UI_INSERT_COL)
window_setmode(win, UI_INSERT);
buffer_inscolchar(win->buf, ch, 1, win->ui_pos);
}else{
buffer_inscolchar(win->buf, ch, win->buf->col_insert_height, win->ui_pos);
}
}
point_t *window_uipos_alt(window *win)
{
if(win->ui_pos == &win->ui_vpos)
return &win->ui_npos;
return &win->ui_vpos;
}
void window_replace_buffer(window *win, buffer_t *buf)
{
buffer_release(win->buf);
win->buf = retain(buf);
win->ui_pos->y = MIN(win->ui_pos->y, list_count(buf->head));
}
bool window_replace_fname(window *win, const char *fname, const char **const err)
{
buffer_t *buf = NULL;
buffer_new_fname(&buf, fname, err);
if(!*err){
window_replace_buffer(win, buf);
buffer_release(buf);
}
return !*err;
}
bool window_reload_buffer(window *win, const char **const err)
{
const char *fname = buffer_fname(win->buf);
if(!fname)
return false;
/* need to update the buffer's list, not the window,
* since there may be multiple windows with the old buffer */
buffer_replace_fname(win->buf, fname, err);
return !*err;
}
void window_smartindent(window *win)
{
buffer_smartindent(win->buf, &win->ui_pos->x, win->ui_pos->y);
}