-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathte.c
598 lines (518 loc) · 13.3 KB
/
te.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xft/Xft.h>
#include <assert.h>
struct X11
{
int fd;
Display *dpy;
int screen;
Window root;
Window win;
GC gc;
unsigned long col_fg, col_bg;
int w, h;
XftFont* font;
XftDraw* fdraw;
int font_width, font_height;
XftColor fcol_fg, fcol_bg, fcol_hi;
int cx, cy;
};
struct X11 x11;
enum Mode {
Normal,
Insert,
Command
};
enum Mode mode;
typedef struct Line Line;
struct Line {
char* str;
Line* prev;
Line* next;
};
Line *buf_start;
Line *buf_end;
Line *curr;
Line *view_start;
Line *view_end;
typedef struct Gap Gap;
struct Gap {
char buf[200];
int lptr, rptr;
int end;
};
Gap gap;
char command[50];
char nc_partial;
char file[50];
bool
x11_setup(struct X11 *x11)
{
Colormap cmap;
XColor color;
x11->dpy = XOpenDisplay(NULL);
if (x11->dpy == NULL)
{
fprintf(stderr, "Cannot open display\n");
return false;
}
x11->screen = DefaultScreen(x11->dpy);
x11->root = RootWindow(x11->dpy, x11->screen);
x11->fd = ConnectionNumber(x11->dpy);
x11->font = XftFontOpenName(x11->dpy, x11->screen,
"Monospace:size=22");
if (x11->font == NULL)
{
fprintf(stderr, "Could not load font\n");
return false;
}
x11->font_height = x11->font->height;
XGlyphInfo ext;
XftTextExtents8(x11->dpy, x11->font, (FcChar8 *)"m", 1, &ext);
x11->font_width = ext.width + 2;
cmap = DefaultColormap(x11->dpy, x11->screen);
if (!XAllocNamedColor(x11->dpy, cmap, "white", &color, &color))
{
fprintf(stderr, "Could not load bg color\n");
return false;
}
x11->col_bg = color.pixel;
if (!XAllocNamedColor(x11->dpy, cmap, "black", &color, &color))
{
fprintf(stderr, "Could not load fg color\n");
return false;
}
x11->col_fg = color.pixel;
// init XftColor for use with text
if (XftColorAllocName(x11->dpy,
DefaultVisual(x11->dpy, x11->screen),
cmap,
"black", &x11->fcol_fg) == False)
{
fprintf(stderr, "Could not load font fg color\n");
return false;
}
if (XftColorAllocName(x11->dpy,
DefaultVisual(x11->dpy, x11->screen),
cmap,
"white", &x11->fcol_bg) == False)
{
fprintf(stderr, "Could not load font bg color\n");
return false;
}
if (XftColorAllocName(x11->dpy,
DefaultVisual(x11->dpy, x11->screen),
cmap,
"gray", &x11->fcol_hi) == False)
{
fprintf(stderr, "Could not load font hi color\n");
return false;
}
x11->w = 80 * x11->font_width;
x11->h = 25 * x11->font_height;
x11->win = XCreateSimpleWindow(x11->dpy, x11->root,
0, 0,
x11->w, x11->h,
2, x11->col_fg, x11->col_bg);
/* allow receiving mouse events */
XSelectInput(x11->dpy, x11->win,
KeyReleaseMask|KeyPressMask|ExposureMask|ButtonPressMask);
XStoreName(x11->dpy, x11->win, "te");
XMapWindow(x11->dpy, x11->win);
x11->gc = XCreateGC(x11->dpy, x11->win, 0, NULL);
// get real size since window is mapped already
XWindowAttributes wa;
XGetWindowAttributes(x11->dpy, x11->win, &wa);
x11->w = wa.width;
x11->h = wa.height;
x11->cx = x11->cy = 0;
// init draw for xft drawing
x11->fdraw = XftDrawCreate(x11->dpy, x11->win,
DefaultVisual(x11->dpy, x11->screen), cmap);
if (x11->fdraw == NULL)
{
fprintf(stderr, "Could not create xft draw \n");
return false;
}
XSync(x11->dpy, False);
return true;
}
void
x11_redraw()
{
// background
XftDrawRect(x11.fdraw, &x11.fcol_bg, 0, 0, x11.w, x11.h);
// cursor
XftDrawRect(x11.fdraw, &x11.fcol_hi, 20 + x11.cx*x11.font_width,
x11.cy*x11.font_height, x11.font_width, x11.font_height);
int numlines = x11.h / x11.font_height;
Line *c = view_start;
for (int i = 0; i < numlines; i++) {
// less content
if (c == NULL)
break;
// only for the current line being edited
// draw from gap buffer
if ((mode == Insert) && (c == curr)) {
XftDrawString8(x11.fdraw, &x11.fcol_fg, x11.font,
20, 30 + i * x11.font_height,
(XftChar8 *)gap.buf, gap.lptr);
XftDrawString8(x11.fdraw, &x11.fcol_fg, x11.font,
20 + x11.font_width*gap.lptr, 30 + i * x11.font_height,
(XftChar8 *)gap.buf+gap.rptr, gap.end-gap.rptr);
} else {
XftDrawString8(x11.fdraw, &x11.fcol_fg, x11.font,
20, 30 + i * x11.font_height,
(XftChar8 *)c->str, strlen(c->str)-1);
}
c = c->next;
}
XftDrawRect(x11.fdraw, &x11.fcol_bg, 0, numlines*x11.font_height,
x11.w, x11.font_height);
if (mode == Command) {
char* cmd_prefix = "->:";
int len = 3;
XftDrawString8(x11.fdraw, &x11.fcol_fg, x11.font,
20, 30 + numlines * x11.font_height,
(XftChar8 *)cmd_prefix, len);
XftDrawString8(x11.fdraw, &x11.fcol_fg, x11.font,
20 + len*x11.font_width, 30 + numlines * x11.font_height,
(XftChar8 *)command, strlen(command));
}
}
void
curr_to_gap()
{
int x = x11.cx;
int size = strlen(curr->str);
int gapsize = 40;
memcpy(gap.buf, curr->str, x);
gap.lptr = x;
gap.rptr = x + gapsize;
memcpy(gap.buf+gap.rptr, curr->str+x, size - x);
gap.end = size + gapsize;
free(curr->str);
}
void
gap_to_curr()
{
int size = gap.lptr + (gap.end - gap.rptr);
curr->str = (char*)malloc(sizeof(char) * size);
memcpy(curr->str, gap.buf, gap.lptr);
memcpy(curr->str + gap.lptr, gap.buf + gap.rptr, gap.end - gap.rptr);
}
void
delete_char()
{
if (gap.lptr > 0){
gap.lptr--;
x11.cx--;
}
}
void
self_insert(char k)
{
// we have enough space in the gap
if (gap.lptr < gap.rptr) {
gap.buf[gap.lptr] = k;
gap.lptr+=1;
}
// TODO: else adjust the gap
}
void
insert_newline()
{
Line *c = (Line *)malloc(sizeof(Line));
c->str = (char *)malloc(2 * sizeof(char));
c->str[0] = '\n';
c->str[1] = '\0';
c->next = curr->next;
c->prev = curr;
c->next->prev = c;
curr->next = c;
curr = c;
}
void
delete_line()
{
Line *nc = curr->next;
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
free(curr->str);
free(curr);
curr = nc;
}
void
forward_char(int n)
{
if (n == 1) {
// we can move forward
if (strlen(curr->str) != x11.cx + 2) {
x11.cx += 1;
}
} else if (n == -1) {
if (x11.cx != 0) {
x11.cx -= 1;
}
}
}
void
forward_line(int n)
{
if (n == 1) {
// we can move forward
if (curr->next != NULL) {
x11.cy += 1;
if (curr == view_end) {
view_start = view_start->next;
view_end = view_end->next;
x11.cy -= 1;
}
curr = curr->next;
}
} else if (n == -1) {
if (curr->prev != NULL) {
x11.cy -= 1;
if (curr == view_start) {
view_start = view_start->prev;
view_end = view_end->prev;
x11.cy = 0;
}
curr = curr->prev;
}
}
}
void
forward_view(int n)
{
int numlines = x11.h / x11.font_height;
Line *p = view_start;
for (int i = 0; i < numlines; i++) {
if (!p)
break;
if (n == 1) {
p = p->next;
} else if (n == -1) {
p = p->prev;
}
}
// if we are not able to move a page full
if (p == NULL)
return;
view_start = p;
// find out new p end
int i, j = 0;
Line *c = view_start;
for (i = 0; i < numlines; i++) {
if (!p)
break;
p = p->next;
if (j < x11.cy) {
c = c->next;
j++;
}
}
view_end = p ? p : buf_end;
curr = (i > j) ? c : buf_end;
x11.cy = (i > j) ? x11.cy : i;
}
void
write_file()
{
FILE* fd = fopen(file, "w");
Line *c = buf_start;
while (c) {
fputs(c->str, fd);
c = c->next;
}
fclose(fd);
}
void
execute_command()
{
if (strcmp(command, "q") == 0) {
exit(0);
} else if (strcmp(command, "w") == 0) {
write_file();
}
return;
}
void
x11_key(XKeyEvent *ev)
{
char buf[32];
KeySym ksym;
int cs;
XLookupString(ev, buf, sizeof buf, &ksym, 0);
if (mode == Normal) {
switch (ksym) {
case XK_Left:
case XK_h:
forward_char(-1);
break;
case XK_Right:
case XK_l:
forward_char(1);
break;
case XK_Up:
case XK_k:
forward_line(-1);
break;
case XK_Down:
case XK_j:
forward_line(1);
break;
case XK_i:
mode = Insert;
curr_to_gap();
break;
case XK_a:
x11.cx += 1;
mode = Insert;
curr_to_gap();
break;
case XK_o:
x11.cy += 1;
x11.cx = 0;
insert_newline();
mode = Insert;
curr_to_gap();
break;
case XK_colon:
mode = Command;
break;
case XK_Next:
forward_view(1);
break;
case XK_Prior:
forward_view(-1);
break;
case XK_d:
if (nc_partial == '\0')
nc_partial = 'd';
else if (nc_partial == 'd') {
delete_line();
nc_partial = '\0';
}
}
} else if (mode == Insert) {
switch (ksym) {
case XK_Escape:
mode = Normal;
gap_to_curr();
break;
case XK_BackSpace:
delete_char();
break;
// list of printable characters from X key sym definitions
// they are already nicely organized in a single contiguous block
// See: https://www.cl.cam.ac.uk/~mgk25/ucs/keysymdef.h
case XK_space ... XK_asciitilde:
x11.cx += 1;
self_insert(buf[0]);
break;
}
} else if (mode == Command) {
switch (ksym) {
case XK_space ... XK_asciitilde:
cs = strlen(command);
command[cs] = buf[0];
command[cs+1] = '\0';
break;
case XK_Return:
execute_command();
command[0] = '\0';
mode = Normal;
break;
case XK_BackSpace:
cs = strlen(command);
command[cs-1] = '\0';
break;
case XK_Escape:
command[0] = '\0';
mode = Normal;
break;
}
}
x11_redraw();
}
void
load_file()
{
FILE* fp = fopen(file, "r");
if (fp == NULL) {
printf("Unable to open file: %s!\n", file);
exit(1);
}
char buf[200];
int size;
Line *p, *c;
// for the very first entry, prev is NULL
p = NULL;
while(true) {
fgets(buf, 200, fp);
size = strlen(buf);
// EOF, nothing more to read
if (size == 0) {
break;
}
// we don't deal with very long lines rn
assert(size < 200);
c = (Line *)malloc(sizeof(Line));
c->str = (char *)malloc((size+1) * sizeof(char));
memcpy(c->str, buf, size);
c->str[size] = '\0';
// pointer to very first entry
if (buf_start == NULL)
buf_start = c;
c->prev = p;
if (p)
p->next = c;
p = c;
buf[0] = '\0';
}
// next pointer for the very last entry
c->next = NULL;
// pointer to very last item
buf_end = c;
fclose(fp);
}
int
main(int argc, char** argv)
{
if (argc != 2) {
printf("Usage: te <filename>\n");
exit(1);
}
strcpy(file, argv[1]);
if (!x11_setup(&x11))
return 1;
buf_start = NULL;
buf_end = NULL;
mode = Normal;
load_file();
curr = buf_start;
view_start = buf_start;
// bit of a hack to setup the initial view end
forward_view(1); forward_view(-1);
command[0] = '\0';
nc_partial = '\0';
XEvent ev;
for (;;)
{
XNextEvent(x11.dpy, &ev);
switch (ev.type)
{
case Expose:
x11_redraw(x11);
break;
case KeyPress:
x11_key(&ev.xkey);
break;
}
}
}