-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathTabManager.c
461 lines (417 loc) · 12.6 KB
/
TabManager.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
#include "Prototypes.h"
//#needs Buffer
/*{
struct Jump_ {
int x;
int y;
TabPage* page;
Jump* prev;
};
struct TabPageClass_ {
ObjectClass super;
};
struct TabPage_ {
Object super;
char* name;
Buffer* buffer;
};
struct TabManager_ {
Vector* items;
Jump* jumps;
int x;
int y;
int w;
int h;
int tabOffset;
int currentPage;
int width;
int forceTabSize;
bool redrawBar;
bool bufferModified;
};
extern TabPageClass TabPageType;
extern bool CRT_hasColors;
}*/
static const char* TabManager_Untitled = "<untitled>";
void Jump_purge(Jump* jump) {
while (jump) {
Jump* prev = jump->prev;
free(jump);
jump = prev;
}
}
TabPageClass TabPageType = {
.super = {
.size = sizeof(TabPage),
.delete = TabPage_delete,
.equals = Object_equals
}
};
TabPage* TabPage_new(char* name, Buffer* buffer) {
TabPage* this = Alloc(TabPage);
if (name) {
this->name = strdup(name);
} else {
this->name = NULL;
}
this->buffer = buffer;
return this;
}
void TabPage_delete(Object* super) {
TabPage* this = (TabPage*) super;
TabManager_releaseLock(this->name);
free(this->name);
if (this->buffer) {
Buffer_delete(this->buffer);
}
free(this);
}
TabManager* TabManager_new(int x, int y, int w, int h, int tabOffset) {
TabManager* this = (TabManager*) calloc(sizeof(TabManager), 1);
this->x = x;
this->y = y;
this->w = w;
this->h = h;
this->tabOffset = tabOffset;
this->width = 0;
this->items = Vector_new(ClassAs(TabPage, Object), true, DEFAULT_SIZE);
this->currentPage = 0;
this->redrawBar = true;
this->bufferModified = false;
this->forceTabSize = 0;
return this;
}
void TabManager_delete(TabManager* this) {
Vector_delete(this->items);
Jump_purge(this->jumps);
free(this);
}
void TabManager_autosaveAll(TabManager* this) {
int items = Vector_size(this->items);
for (int i = 0; i < items; i++) {
TabPage* page = (TabPage*) Vector_get(this->items, i);
Buffer_autosave(page->buffer, true);
}
}
void TabManager_moveTabLeft(TabManager* this) {
int curr = this->currentPage;
if (curr > 0) {
TabPage* page = (TabPage*) Vector_take(this->items, curr);
Vector_insert(this->items, curr - 1, page);
this->currentPage--;
this->redrawBar = true;
}
}
void TabManager_moveTabRight(TabManager* this) {
int curr = this->currentPage;
if (curr < Vector_size(this->items) - 1) {
TabPage* page = (TabPage*) Vector_take(this->items, curr);
Vector_insert(this->items, curr + 1, page);
this->currentPage++;
this->redrawBar = true;
}
}
static inline int TabManager_nameLength(const char* name) {
int len;
if (name) {
const char* base = strrchr(name, '/');
if (!base) {
base = name;
}
len = strlen(base) + 4;
} else {
len = strlen(TabManager_Untitled) + 4;
}
return len;
}
int TabManager_add(TabManager* this, char* name, Buffer* buffer) {
Vector_add(this->items, TabPage_new(name, buffer));
this->width += TabManager_nameLength(name);
return Vector_size(this->items) - 1;
}
void TabManager_removeCurrent(TabManager* this) {
const char* name = TabManager_current(this)->name;
this->width -= TabManager_nameLength(name);
assert(Vector_size(this->items) > 1);
Vector_remove(this->items, this->currentPage);
TabManager_setPage(this, this->currentPage);
}
TabPage* TabManager_current(TabManager* this) {
return (TabPage*) Vector_get(this->items, this->currentPage);
}
void TabManager_printStatus(TabManager* this, const char* text) {
int lines, cols;
Display_getScreenSize(&cols, &lines);
Display_attrset(CRT_colors[CurrentTabColor]);
Display_mvhline(lines - 1, this->tabOffset, ' ', cols - this->tabOffset);
Display_printAt(lines - 1, this->tabOffset, "%s", text);
this->redrawBar = false;
}
static inline void TabManager_drawBar(TabManager* this, int width) {
int lines, cols;
Display_getScreenSize(&cols, &lines);
int items = Vector_size(this->items);
const int current = this->currentPage;
assert(current < items);
int x = this->x + this->tabOffset;
Display_attrset(CRT_colors[TabColor]);
Display_mvhline(lines - 1, x, ' ', cols - x);
int tabWidth = 15;
for (int i = 0; i < items; i++) {
TabPage* page = (TabPage*) Vector_get(this->items, i);
int currentColor = ((page->buffer && page->buffer->readOnly) ? CRT_colors[CurrentTabROColor] : CRT_colors[CurrentTabColor]);
char modified;
const char* label = TabManager_Untitled;
if (page->buffer) {
modified = page->buffer->modified ? '*' : ' ';
if (page->buffer->fileName)
label = page->buffer->fileName;
} else {
modified = ' ';
if (page->name)
label = page->name;
}
Display_attrset(CRT_colors[TabColor]);
Display_printAt(this->y + this->h - 1, x+1, "│");
char* base = strrchr(label, '/');
if (i == current) {
Display_attrset(currentColor);
Display_setWindowTitle(label);
tabWidth = 30;
int offset = strlen(label) - (tabWidth - 2);
if (offset > 0) {
label += offset;
}
} else {
tabWidth = 15;
if (base) label = base + 1;
}
if (x+1 < cols-1) {
Display_printAt(this->y + this->h - 1, x+2, "%c", modified);
if (i == current && base > label) {
int lenToSlash = base - label + 1;
Display_attrset(CRT_colors[CurrentTabShadeColor]);
Display_writeAtn(this->y + this->h - 1, x+3, label, MIN(lenToSlash, cols-x-1));
Display_attrset(currentColor);
Display_writeAtn(this->y + this->h - 1, x+3+lenToSlash, label + lenToSlash, MIN(tabWidth - lenToSlash, cols-x-1));
} else {
Display_writeAtn(this->y + this->h - 1, x+3, label, MIN(tabWidth-2, cols-x-1));
}
const int lenLabel = strlen(label);
if (lenLabel < tabWidth) {
Display_writeAtn(this->y + this->h - 1, x+3+lenLabel, " ", MIN(tabWidth - lenLabel, cols-x-1));
}
}
x += tabWidth;
}
Display_attrset(CRT_colors[NormalColor]);
this->redrawBar = false;
}
Buffer* TabManager_getBuffer(TabManager* this, int pageNr) {
TabPage* page = (TabPage*) Vector_get(this->items, pageNr);
if (!page) {
return NULL;
}
if (page->buffer) {
if (page->buffer->modified != this->bufferModified) {
this->redrawBar = true;
this->bufferModified = page->buffer->modified;
}
} else {
page->buffer = Buffer_new(this->x, this->y, this->w, this->h-1, page->name, false, this);
if (this->forceTabSize != 0) {
page->buffer->tabSize = this->forceTabSize;
}
if (page->name && !TabManager_checkLock(this, page->name)) {
page->buffer->modified = true;
this->bufferModified = true;
} else {
this->bufferModified = false;
}
}
return page->buffer;
}
Buffer* TabManager_draw(TabManager* this, int width) {
Buffer* buffer = TabManager_getBuffer(this, this->currentPage);
if (this->redrawBar)
TabManager_drawBar(this, width);
Buffer_draw(buffer);
return buffer;
}
/**
* This is like TabManager_draw, but it never loads new files,
* it only shows what's already loaded. This is used in the
* error handler of the script engine, to avoid an endless
* loop in case of errors when loading a file.
*/
void TabManager_redraw(TabManager* this, int width) {
TabPage* page = (TabPage*) Vector_get(this->items, this->currentPage);
if (!page || !page->buffer) {
return;
}
if (this->redrawBar)
TabManager_drawBar(this, width);
Buffer_draw(page->buffer);
}
bool TabManager_checkLock(TabManager* this, char* fileName) {
if (!fileName)
return true;
char* lockFileName = Files_encodePathAsFileName(fileName);
bool exists = Files_existsHome("lock/%s", lockFileName);
if (exists) {
char question[1024];
sprintf(question, "Looks like %s is already open in another instance. Open anyway?", fileName);
return (TabManager_question(this, question, "yn") == 0);
} else {
FILE* lfd = Files_openHome("w", "lock/%s", lockFileName);
if (lfd)
fclose(lfd);
else
return false;
}
free(lockFileName);
return true;
}
void TabManager_releaseLock(char* fileName) {
if (!fileName)
return;
char* lockFileName = Files_encodePathAsFileName(fileName);
Files_deleteHome("lock/%s", lockFileName);
free(lockFileName);
}
void TabManager_resize(TabManager* this, int w, int h) {
this->w = w;
this->h = h;
int items = Vector_size(this->items);
for (int i = 0; i < items; i++) {
TabPage* page = (TabPage*) Vector_get(this->items, i);
if (page->buffer)
Buffer_resize(page->buffer, this->w, this->h - 1);
}
this->redrawBar = true;
}
int TabManager_find(TabManager* this, char* name) {
int items = Vector_size(this->items);
for (int i = 0; i < items; i++) {
TabPage* page = (TabPage*) Vector_get(this->items, i);
if (String_eq(page->name, name))
return i;
}
return -1;
}
inline void TabManager_refreshCurrent(TabManager* this) {
TabPage* page = (TabPage*) Vector_get(this->items, this->currentPage);
if (page->buffer) {
Buffer_refresh(page->buffer);
this->bufferModified = page->buffer->modified;
}
this->redrawBar = true;
}
void TabManager_previousPage(TabManager* this) {
this->currentPage--;
if (this->currentPage == -1)
this->currentPage = Vector_size(this->items) - 1;
TabManager_refreshCurrent(this);
}
void TabManager_nextPage(TabManager* this) {
this->currentPage++;
int items = Vector_size(this->items);
if (this->currentPage == items)
this->currentPage = 0;
TabManager_refreshCurrent(this);
}
void TabManager_setPage(TabManager* this, int i) {
assert(i >= 0);
if (i >= Vector_size(this->items))
i = 0;
this->currentPage = i;
TabManager_refreshCurrent(this);
}
void TabManager_markJump(TabManager* this) {
Jump* jump = calloc(sizeof(Jump), 1);
Buffer* buffer = TabManager_getBuffer(this, this->currentPage);
jump->x = buffer->x;
jump->y = buffer->y;
jump->page = TabManager_current(this);
jump->prev = this->jumps;
this->jumps = jump;
}
void TabManager_jumpBack(TabManager* this) {
Jump* jump = this->jumps;
if (!jump)
return;
this->jumps = jump->prev;
int idx = Vector_indexOf(this->items, jump->page);
if (idx == -1) {
free(jump);
//TabManager_jumpBack(this);
return;
}
TabManager_setPage(this, idx);
Buffer_goto(TabManager_current(this)->buffer, jump->x, jump->y, true);
free(jump);
}
int TabManager_getPageCount(TabManager* this) {
return Vector_size(this->items);
}
char* TabManager_getPageName(TabManager* this, int i) {
TabPage* page = (TabPage*) Vector_get(this->items, i);
if (page->buffer) {
return page->buffer->fileName;
} else {
return page->name;
}
}
void TabManager_load(TabManager* this, char* fileName, int limit) {
FILE* fd = Files_openHome("r", fileName, NULL);
if (fd) {
char line[4097];
while (!feof(fd)) {
char* ok = fgets(line, 4096, fd);
if (ok) {
char* enter = strrchr(line, '\n');
if (enter) *enter = '\0';
if (*line == '\0') continue;
if (TabManager_find(this, (char*) line) == -1 && access(line, F_OK) == 0) {
TabManager_add(this, (char*) line, NULL);
limit--;
if (!limit) break;
}
}
}
fclose(fd);
}
}
void TabManager_save(TabManager* this, char* fileName) {
FILE* fd = Files_openHome("w", fileName, NULL);
if (fd) {
int items = Vector_size(this->items);
for (int i = 0; i < items; i++) {
char* name = TabManager_getPageName(this, i);
if (name)
fprintf(fd, "%s\n", name);
}
fclose(fd);
}
}
int TabManager_size(TabManager* this) {
return Vector_size(this->items);
}
int TabManager_question(TabManager* this, char* question, char* options) {
int lines, cols;
Display_getScreenSize(&cols, &lines);
Display_attrset(CRT_colors[StatusColor]);
Display_printAt(lines - 1, 0, "%s [%s]", question, options);
Display_clearToEol();
Display_attrset(CRT_colors[NormalColor]);
Display_refresh();
bool code;
int opt;
char* which;
Display_beep();
do {
opt = Display_getch(&code);
} while (code || !(which = strchr(options, tolower(opt))));
TabManager_refreshCurrent(this);
return which - options;
}