-
Notifications
You must be signed in to change notification settings - Fork 66
/
editor.c
749 lines (681 loc) · 21.1 KB
/
editor.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
#include "editor.h"
#include <time.h>
#include <unistd.h>
static struct editorConfig E;
/* ====================== Syntax highlight color scheme ==================== */
static hlcolor hlscheme[] = {
HL_NORMAL_COLOR,
HL_ERROR_COLOR,
HL_COMMENT_COLOR,
HL_KEYWORD_COLOR,
HL_STRING_COLOR,
HL_NUMBER_COLOR,
HL_FUNCDEF_COLOR,
HL_LIB_COLOR
};
int is_separator(int c) {
return c == '\0' || isspace(c) || strchr(",.()+-/*=~%[];",c) != NULL;
}
/* Set every byte of row->hl (that corresponds to every character in the line)
* to the right syntax highlight type (HL_* defines). */
void editorUpdateSyntax(erow *row) {
int i, prev_sep, in_string;
char *p;
char *keywords[] = {
/* Keywords */
"function","if","while","for","end","in","do","local","break",
"then","pairs","return","else","elseif",
/* Libs (ending with dots) will be marked as HL_LIB */
"math.","table.","string.","mouse.","keyboard.",NULL
};
row->hl = realloc(row->hl,row->size);
memset(row->hl,HL_NORMAL,row->size);
/* Point to the first non-space char. */
p = row->chars;
i = 0; /* Current char offset */
while(*p && isspace(*p)) {
p++;
i++;
}
prev_sep = 1; /* Tell the parser if 'i' points to start of word. */
in_string = 0; /* Are we inside "" or '' ? */
while(*p) {
if (prev_sep && *p == '-' && *(p+1) == '-') {
/* From here to end is a comment */
memset(row->hl+i,HL_COMMENT,row->size-i);
return;
}
/* Handle "" and '' */
if (in_string) {
row->hl[i] = HL_STRING;
if (*p == '\\') {
row->hl[i+1] = HL_STRING;
p += 2; i += 2;
prev_sep = 0;
continue;
}
if (*p == in_string) in_string = 0;
p++; i++;
continue;
} else {
if (*p == '"' || *p == '\'') {
in_string = *p;
row->hl[i] = HL_STRING;
p++; i++;
prev_sep = 0;
continue;
}
}
/* Handle numbers */
if ((isdigit(*p) && (prev_sep || row->hl[i-1] == HL_NUMBER)) ||
(*p == '.' && i >0 && row->hl[i-1] == HL_NUMBER)) {
row->hl[i] = HL_NUMBER;
p++; i++;
prev_sep = 0;
continue;
}
/* Handle keywords and lib calls */
if (prev_sep) {
int j;
for (j = 0; keywords[j]; j++) {
int klen = strlen(keywords[j]);
int lib = keywords[j][klen-1] == '.';
if (!lib && !memcmp(p,keywords[j],klen) &&
is_separator(*(p+klen)))
{
/* Keyword */
memset(row->hl+i,HL_KEYWORD,klen);
p += klen;
i += klen;
break;
}
if (lib && !memcmp(p,keywords[j],klen)) {
/* Library call */
memset(row->hl+i,HL_LIB,klen);
p += klen;
i += klen;
while(!is_separator(*p)) {
row->hl[i] = HL_LIB;
p++;
i++;
}
break;
}
}
if (keywords[j] != NULL) {
prev_sep = 0;
continue; /* We had a keyword match */
}
}
/* Not special chars */
prev_sep = is_separator(*p);
p++; i++;
}
}
/* ======================= Editor rows implementation ======================= */
/* Insert a row at the specified position, shifting the other rows on the bottom
* if required. */
void editorInsertRow(int at, char *s) {
if (at > E.numrows) return;
E.row = realloc(E.row,sizeof(erow)*(E.numrows+1));
if (at != E.numrows)
memmove(E.row+at+1,E.row+at,sizeof(E.row[0])*(E.numrows-at));
E.row[at].size = strlen(s);
E.row[at].chars = strdup(s);
E.row[at].hl = NULL;
editorUpdateSyntax(E.row+at);
E.numrows++;
E.dirty++;
}
/* Free row's heap allocated stuff. */
void editorFreeRow(erow *row) {
free(row->chars);
free(row->hl);
}
/* Remove the row at the specified position, shifting the remainign on the
* top. */
void editorDelRow(int at) {
erow *row;
if (at >= E.numrows) return;
row = E.row+at;
editorFreeRow(row);
memmove(E.row+at,E.row+at+1,sizeof(E.row[0])*(E.numrows-at-1));
E.numrows--;
E.dirty++;
}
/* Turn the editor rows into a single heap-allocated string.
* Returns the pointer to the heap-allocated string and populate the
* integer pointed by 'buflen' with the size of the string, escluding
* the final nulterm. */
char *editorRowsToString(int *buflen) {
char *buf = NULL, *p;
int totlen = 0;
int j;
/* Compute count of bytes */
for (j = 0; j < E.numrows; j++)
totlen += E.row[j].size+1; /* +1 is for "\n" at end of every row */
*buflen = totlen;
totlen++; /* Also make space for nulterm */
p = buf = malloc(totlen);
for (j = 0; j < E.numrows; j++) {
memcpy(p,E.row[j].chars,E.row[j].size);
p += E.row[j].size;
*p = '\n';
p++;
}
*p = '\0';
return buf;
}
/* Insert a character at the specified position in a row, moving the remaining
* chars on the right if needed. */
void editorRowInsertChar(erow *row, int at, int c) {
if (at > row->size) {
/* Pad the string with spaces if the insert location is outside the
* current length by more than a single character. */
int padlen = at-row->size;
/* In the next line +2 means: new char and null term. */
row->chars = realloc(row->chars,row->size+padlen+2);
memset(row->chars+row->size,' ',padlen);
row->chars[row->size+padlen+1] = '\0';
row->size += padlen+1;
} else {
/* If we are in the middle of the string just make space for 1 new
* char plus the (already existing) null term. */
row->chars = realloc(row->chars,row->size+2);
memmove(row->chars+at+1,row->chars+at,row->size-at+1);
row->size++;
}
row->chars[at] = c;
editorUpdateSyntax(row);
E.dirty++;
}
/* Append the string 's' at the end of a row */
void editorRowAppendString(erow *row, char *s) {
int l = strlen(s);
row->chars = realloc(row->chars,row->size+l+1);
memcpy(row->chars+row->size,s,l);
row->size += l;
row->chars[row->size] = '\0';
editorUpdateSyntax(row);
E.dirty++;
}
void editorRowDelChar(erow *row, int at) {
if (row->size <= at) return;
memmove(row->chars+at,row->chars+at+1,row->size-at);
editorUpdateSyntax(row);
row->size--;
E.dirty++;
}
void editorInsertChar(int c) {
int filerow = E.rowoff+E.cy;
int filecol = E.coloff+E.cx;
erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
/* If the row where the cursor is currently located does not exist in our
* logical representaion of the file, add enough empty rows as needed. */
if (!row) {
while(E.numrows <= filerow)
editorInsertRow(E.numrows,"");
}
row = &E.row[filerow];
editorRowInsertChar(row,filecol,c);
if (E.cx == E.screencols-1)
E.coloff++;
else
E.cx++;
E.dirty++;
}
/* Inserting a newline is slightly complex as we have to handle inserting a
* newline in the middle of a line, splitting the line as needed. */
void editorInsertNewline(void) {
int filerow = E.rowoff+E.cy;
int filecol = E.coloff+E.cx;
erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
if (!row) {
if (filerow == E.numrows) {
editorInsertRow(filerow,"");
goto fixcursor;
}
return;
}
/* If the cursor is over the current line size, we want to conceptually
* think it's just over the last character. */
if (filecol >= row->size) filecol = row->size;
if (filecol == 0) {
editorInsertRow(filerow,"");
} else {
/* We are in the middle of a line. Split it between two rows. */
editorInsertRow(filerow+1,row->chars+filecol);
row = &E.row[filerow];
row->chars[filecol] = '\0';
row->size = filecol;
editorUpdateSyntax(row);
}
fixcursor:
if (E.cy == E.screenrows-1) {
E.rowoff++;
} else {
E.cy++;
}
E.cx = 0;
E.coloff = 0;
}
void editorDelChar() {
int filerow = E.rowoff+E.cy;
int filecol = E.coloff+E.cx;
erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
if (!row || (filecol == 0 && filerow == 0)) return;
if (filecol == 0) {
/* Handle the case of column 0, we need to move the current line
* on the right of the previous one. */
filecol = E.row[filerow-1].size;
editorRowAppendString(&E.row[filerow-1],row->chars);
editorDelRow(filerow);
row = NULL;
if (E.cy == 0)
E.rowoff--;
else
E.cy--;
E.cx = filecol;
if (E.cx >= E.screencols) {
int shift = (E.screencols-E.cx)+1;
E.cx -= shift;
E.coloff += shift;
}
} else {
editorRowDelChar(row,filecol-1);
if (E.cx == 0 && E.coloff)
E.coloff--;
else
E.cx--;
}
if (row) editorUpdateSyntax(row);
E.dirty++;
}
/* Program template used when the input file does not exist. */
char *editorTemplate[] = {
"function setup()",
" -- This function is called only once at startup.",
"end",
"",
"function draw()",
" -- This function is called at every frame refresh.",
" background(0,0,0)",
" fill(200,200,200,1)",
" text(WIDTH/2-150,HEIGHT/2,\"Press ESC to edit this program.\")",
"end",
NULL
};
/* Load the specified program in the editor memory and returns 0 on success
* or 1 on error. */
int editorOpen(char *filename) {
FILE *fp;
char line[1024];
E.dirty = 0;
free(E.filename);
E.filename = strdup(filename);
/* TODO: remove old program from rows. */
fp = fopen(filename,"r");
if (!fp) {
/* No such file, add a template. */
int j = 0;
while(editorTemplate[j])
editorInsertRow(E.numrows,editorTemplate[j++]);
return 1;
}
while(fgets(line,sizeof(line),fp) != NULL) {
int l = strlen(line);
if (l && (line[l-1] == '\n' || line[l-1] == '\r'))
line[l-1] = '\0';
editorInsertRow(E.numrows,line);
}
fclose(fp);
return 0;
}
int editorSave(char *filename) {
int len;
char *buf = editorRowsToString(&len);
FILE *fp;
fp = fopen(filename,"w");
if (!fp) {
free(buf);
return 1;
}
fwrite(buf,len,1,fp);
fclose(fp);
free(buf);
E.dirty = 0;
return 0;
}
/* ============================= Editor drawing ============================= */
void editorDrawCursor(void) {
int x = E.cx*FONT_KERNING;
int y = E.fb->height-((E.cy+1)*FONT_HEIGHT);
int charmargin = (FONT_WIDTH-FONT_KERNING)/2;
x += E.margin_left;
y -= E.margin_top;
if (!(E.cblink & 0x80)) drawBox(E.fb,x+charmargin,y,
x+charmargin+FONT_KERNING-1,y+FONT_HEIGHT-1,
165,165,255,128);
E.cblink += 4;
}
void editorDrawChars(void) {
int y,x;
erow *r;
char buf[32];
for (y = 0; y < E.screenrows; y++) {
int chary, filerow = E.rowoff+y;
if (filerow >= E.numrows) break;
chary = E.fb->height-((y+1)*FONT_HEIGHT);
chary -= E.margin_top;
r = &E.row[filerow];
snprintf(buf,sizeof(buf),"%d",filerow%1000);
bfWriteString(E.fb,0,chary,buf,strlen(buf),120,120,120,255);
for (x = 0; x < E.screencols; x++) {
int idx = x+E.coloff;
int charx;
hlcolor *color;
if (idx >= r->size) break;
charx = x*FONT_KERNING;
charx += E.margin_left;
color = hlscheme+r->hl[idx];
bfWriteChar(E.fb,charx,chary,r->chars[idx],
color->r,color->g,color->b,255);
}
}
if (E.err)
bfWriteString(E.fb,E.margin_left,10,E.err,strlen(E.err),0,0,0,255);
}
void editorDrawPowerOff(int x, int y) {
drawEllipse(E.fb,x,y,12,12,66,66,231,255);
drawEllipse(E.fb,x,y,7,7,165,165,255,255);
drawBox(E.fb,x-4,y,x+4,y+12,165,165,255,255);
drawBox(E.fb,x-2,y,x+2,y+14,66,66,231,255);
}
void editorDrawSaveIcon(int x, int y) {
drawBox(E.fb,x-12,y-12,x+12,y+12,66,66,231,255);
drawBox(E.fb,x-1,y+7,x+1,y+11,165,165,255,255);
drawEllipse(E.fb,x,y,4,4,165,165,255,255);
}
void editorDraw() {
drawBox(E.fb,0,0,E.fb->width-1,E.fb->height-1,165,165,255,255);
drawBox(E.fb,
E.margin_left,
E.margin_bottom,
E.fb->width-1-E.margin_right,
E.fb->height-1-E.margin_top,66,66,231,255);
editorDrawChars();
editorDrawCursor();
/* Show buttons */
editorDrawPowerOff(POWEROFF_BUTTON_X,POWEROFF_BUTTON_Y);
if (E.dirty) editorDrawSaveIcon(SAVE_BUTTON_X,SAVE_BUTTON_Y);
/* Show info about the current file */
bfWriteString(E.fb,E.margin_left,E.fb->height-E.margin_top+4,E.filename,
strlen(E.filename), 255,255,255,255);
}
/* ========================= Editor events handling ======================== */
/* As long as a key is pressed, we incremnet a counter in order to
* implement first pression of key and key repeating.
*
* This function returns if the key was just pressed or if it is repeating. */
#define KEY_REPEAT_PERIOD 2
#define KEY_REPEAT_PERIOD_FAST 1
#define KEY_REPEAT_DELAY 8
int pressed_or_repeated(int counter) {
int period;
if (counter > KEY_REPEAT_DELAY+(KEY_REPEAT_PERIOD*3)) {
period = KEY_REPEAT_PERIOD_FAST;
} else {
period = KEY_REPEAT_PERIOD;
}
if (counter > 1 && counter < KEY_REPEAT_DELAY) return 0;
return ((counter+period-1) % period) == 0;
}
void editorMouseClicked(int x, int y, int button) {
if (abs(x-POWEROFF_BUTTON_X) < 15 && abs(y-POWEROFF_BUTTON_Y) < 15 &&
button == 1)
{
exit(1);
} else if (abs(x-SAVE_BUTTON_X) < 15 && abs(y-SAVE_BUTTON_Y) < 15 &&
button == 1) {
if (editorSave(E.filename) == 0) E.dirty = 0;
} else if (x >= E.margin_left && x <= E.fb->width-1-E.margin_right &&
y >= E.margin_bottom && y <= E.fb->height-1-E.margin_top)
{
int realheight = E.fb->height - E.margin_top - E.margin_bottom;
int realy = y - E.margin_bottom;
int row = (realheight-realy)/FONT_HEIGHT;
int col = (x-E.margin_left)/FONT_KERNING;
int filerow = E.rowoff+row;
int filecol = E.coloff+col;
erow *r = (filerow >= E.numrows) ? NULL : &E.row[filerow];
E.cblink = 0;
if (filerow == E.numrows) {
E.cx = 0;
E.cy = filerow-E.rowoff;
} else if (r) {
if (filecol >= r->size)
E.cx = r->size-E.coloff;
else
E.cx = filecol-E.coloff;
E.cy = filerow-E.rowoff;
}
}
}
void editorMoveCursor(int key) {
int filerow = E.rowoff+E.cy;
int filecol = E.coloff+E.cx;
int rowlen;
erow *row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
switch(key) {
case SDLK_LEFT:
if (E.cx == 0) {
if (E.coloff) E.coloff--;
} else {
E.cx -= 1;
}
break;
case SDLK_RIGHT:
if (row && filecol < row->size) {
if (E.cx == E.screencols-1) {
E.coloff++;
} else {
E.cx += 1;
}
}
break;
case SDLK_UP:
if (E.cy == 0) {
if (E.rowoff) E.rowoff--;
} else {
E.cy -= 1;
}
break;
case SDLK_DOWN:
if (filerow < E.numrows) {
if (E.cy == E.screenrows-1) {
E.rowoff++;
} else {
E.cy += 1;
}
}
break;
}
/* Fix cx if the current line has not enough chars. */
filerow = E.rowoff+E.cy;
filecol = E.coloff+E.cx;
row = (filerow >= E.numrows) ? NULL : &E.row[filerow];
rowlen = row ? row->size : 0;
if (filecol > rowlen) {
E.cx -= filecol-rowlen;
if (E.cx < 0) {
E.coloff += E.cx;
E.cx = 0;
}
}
}
/* Scan the array of currently pressed keys, and return the matching entry
* if any, otherwise return one of the free entries. */
keyState *editorGetKeyState(int ksym) {
int free = -1;
for (int j = 0; j < KEY_MAX; j++) {
if (E.key[j].ksym == ksym) return &E.key[j];
if (E.key[j].ksym == 0) free = j;
}
if (free == -1) return NULL;
E.key[free].ksym = ksym;
E.key[free].counter = 0;
return &E.key[free];
}
int editorEvents(void) {
SDL_Event event;
int j, ksym;
time_t idletime;
/* Sleep 0.25 seconds if no body is pressing any key for a few seconds.
* This way we can save tons of energy when in editing mode and
* the user is thinking or away from keyboard. */
idletime = time(NULL)-E.lastevent;
if (idletime > 5) {
usleep((idletime < 60) ? 50000 : 250000);
E.cblink = 0;
}
while (SDL_PollEvent(&event)) {
E.lastevent = time(NULL);
switch(event.type) {
/* Text inserted. */
case SDL_TEXTINPUT:
for (int j = 0; event.text.text[j]; j++)
editorInsertChar(event.text.text[j]);
break;
/* Key pressed */
case SDL_KEYDOWN:
ksym = event.key.keysym.sym;
switch(ksym) {
case SDLK_ESCAPE:
return 1;
break;
default:
{
keyState *ks = editorGetKeyState(ksym);
if (ks) ks->counter = 1;
}
break;
}
break;
/* Key released */
case SDL_KEYUP:
ksym = event.key.keysym.sym;
keyState *ks = editorGetKeyState(ksym);
if (ks) {
ks->counter = 0;
ks->ksym = 0;
}
break;
/* Mouse click */
case SDL_MOUSEBUTTONDOWN:
editorMouseClicked(event.motion.x, E.fb->height-1-event.motion.y,
event.button.button);
break;
case SDL_QUIT:
exit(0);
break;
}
}
/* Convert events into actions */
for (j = 0; j < KEY_MAX; j++) {
int i;
if (pressed_or_repeated(E.key[j].counter)) {
E.lastevent = time(NULL);
E.cblink = 0;
int sym = E.key[j].ksym;
switch(sym) {
case SDLK_LEFT:
case SDLK_RIGHT:
case SDLK_UP:
case SDLK_DOWN:
editorMoveCursor(sym);
break;
case SDLK_BACKSPACE:
editorDelChar();
break;
case SDLK_RETURN:
editorInsertNewline();
break;
case SDLK_HOME:
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LGUI:
case SDLK_RGUI:
case SDLK_CAPSLOCK:
case SDLK_MODE:
/* Ignored */
break;
case SDLK_TAB:
for (i = 0; i < 4; i++)
editorInsertChar(' ');
break;
default:
/* Avoid repetition for special characters. */
if (sym == SDLK_UNKNOWN) {
E.key[j].counter = 0;
E.key[j].ksym = 0;
}
break;
}
}
if (E.key[j].counter) E.key[j].counter++; /* auto repeat counter */
}
/* Call the draw function at every iteration. */
editorDraw();
/* Refresh the screen */
presentFrameBuffer(E.fb);
return 0;
}
void editorSetError(const char *err, int line) {
erow *row;
free(E.err);
E.err = strdup(err);
E.errline = line;
row = (line >= E.numrows) ? NULL : &E.row[line];
if (row) memset(row->hl,HL_ERROR,row->size);
}
void editorClearError(void) {
erow *row;
if (E.err) {
row = (E.errline >= E.numrows) ? NULL : &E.row[E.errline];
if (row) editorUpdateSyntax(row);
}
free(E.err);
E.err = NULL;
E.errline = 0;
}
int editorFileWasModified(void) {
return E.dirty;
}
void editorRun(void) {
E.lastevent = time(NULL);
SDL_setFramerate(&E.fb->fps_mgr,EDITOR_FPS);
while(!editorEvents());
}
void initEditor(frameBuffer *fb, int mt, int mb, int ml, int mr) {
E.fb = fb;
E.cx = 0;
E.cy = 0;
E.cblink = 0;
E.rowoff = 0;
E.coloff = 0;
E.numrows = 0;
E.row = NULL;
E.margin_top = mt;
E.margin_bottom = mb;
E.margin_left = ml;
E.margin_right = mr;
E.screencols = (fb->width-E.margin_left-E.margin_right) / FONT_KERNING;
E.screenrows = (fb->height-E.margin_top-E.margin_bottom) / FONT_HEIGHT;
E.dirty = 0;
E.filename = NULL;
memset(E.key,0,sizeof(E.key));
}