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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
|
const es = @import("root");
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const Buffer = @This();
const Config = es.Config;
const File = std.fs.File;
const Editor = es.Editor;
const Highlight = es.Highlight;
const Row = es.Row;
const Syntax = es.Syntax;
allocator: Allocator,
file_path: ?[]u8,
short_name: []u8,
rows: ArrayList(Row),
cx: usize,
cy: usize,
rx: usize,
rowoff: usize,
coloff: usize,
edited: bool,
config: Config,
syntax: ?Syntax,
pub fn init(allocator: Allocator, name: []const u8) !Buffer {
const name_owned = try allocator.dupe(u8, name);
errdefer allocator.free(name_owned);
// TODO: buffer-specific config support
const config = try Config.readConfig(allocator);
return Buffer{
.allocator = allocator,
.file_path = null,
.short_name = name_owned,
.rows = ArrayList(Row).init(allocator),
.cx = 0,
.cy = 0,
.rx = 0,
.rowoff = 0,
.coloff = 0,
.edited = false,
.config = config,
.syntax = null,
};
}
pub fn deinit(self: Buffer) void {
if (self.file_path) |file_path| {
self.allocator.free(file_path);
}
self.allocator.free(self.short_name);
for (self.rows.items) |row| {
row.deinit();
}
self.rows.deinit();
}
pub fn appendRow(self: *Buffer, data: []const u8) !void {
try self.insertRow(self.rows.items.len, data);
}
pub fn backwardChar(self: *Buffer) void {
if (self.cx == 0) {
if (self.cy == 0) {
return;
} else {
self.cy -= 1;
self.cx = self.rows.items[self.cy].data.items.len;
}
} else {
self.cx -= 1;
}
}
pub fn backwardDeleteChar(self: *Buffer) !void {
if (self.cx == 0 and self.cy == 0) {
return;
}
self.backwardChar();
return self.deleteChar();
}
pub fn backwardDeleteWord(self: *Buffer) !void {
const start = self.findBackwardWordStart();
if (start == self.cx) {
return self.backwardDeleteChar();
} else {
var row = &self.rows.items[self.cy];
row.data.replaceRangeAssumeCapacity(start, self.cx - start, &.{});
self.cx = start;
return row.update(self);
}
}
pub fn backwardParagraph(self: *Buffer) void {
if (self.cy == 0) {
return;
}
self.cy -= 1;
while (self.cy > 0) : (self.cy -= 1) {
// TODO: I'm lazy but also consider lines of only whitespace
if (self.rows.items[self.cy].data.items.len == 0) {
break;
}
}
self.cx = 0;
}
pub fn backwardWord(self: *Buffer) void {
const target = self.findBackwardWordStart();
if (target == self.cx) {
self.backwardChar();
} else {
self.cx = target;
}
}
pub fn beginningOfBuffer(self: *Buffer) void {
self.cx = 0;
self.cy = 0;
}
pub fn cleanWhiteSpace(self: *Buffer) !void {
for (self.rows.items) |*row| {
try row.cleanWhiteSpace(self);
}
if (self.cy == self.rows.items.len) {
return;
}
const row = self.rows.items[self.cy];
if (self.cx > row.data.items.len) {
self.cx = row.data.items.len;
self.rx = row.cxToRx(self.config, self.cx);
}
}
pub fn deleteChar(self: *Buffer) !void {
if (self.cy == self.rows.items.len) {
return;
}
if (self.cx < self.rows.items[self.cy].data.items.len) {
self.edited = true;
try self.rows.items[self.cy].deleteChar(self, self.cx);
} else {
if (self.cy == self.rows.items.len - 1) {
return;
}
self.edited = true;
try self.rows.items[self.cy].appendString(self, self.rows.items[self.cy + 1].data.items);
self.deleteRow(self.cy + 1);
}
}
pub fn deleteAllRows(self: *Buffer) void {
if (self.rows.items.len == 0) {
return;
}
self.edited = true;
while (self.rows.popOrNull()) |row| {
row.deinit();
}
}
pub fn deleteWord(self: *Buffer) !void {
const end = self.findForwardWordEnd();
if (end == self.cx) {
return self.deleteChar();
} else {
var row = &self.rows.items[self.cy];
row.data.replaceRangeAssumeCapacity(self.cx, end - self.cx, &.{});
return row.update(self);
}
}
pub fn deleteRow(self: *Buffer, at: usize) void {
if (at == self.rows.items.len) {
return;
}
self.edited = true;
self.rows.orderedRemove(at).deinit();
var i = at;
while (i < self.rows.items.len) : (i += 1) {
self.rows.items[i].idx -= 1;
}
if (self.cy == self.rows.items.len) {
self.cx = 0;
} else {
self.cx = @min(self.cx, self.rows.items[self.cy].data.items.len);
}
}
pub fn drawRows(self: Buffer, writer: anytype, screenrows: usize, screencols: usize) !void {
const line_num_digits = self.lineNumberDigits();
var y: usize = 0;
while (y < screenrows) : (y += 1) {
const filerow = y + self.rowoff;
if (filerow < self.rows.items.len) {
const row = self.rows.items[filerow];
try writer.writeAll(Highlight.line_no.asString());
try printWithLeftPadding(self.allocator, writer, "{}", line_num_digits - 1, ' ', .{filerow + 1});
try writer.writeByte(' ');
if (row.rdata.items.len >= self.coloff) {
try writer.writeAll("\x1b[m");
var last_hl = Highlight.normal;
var x: usize = 0;
while (x < screencols - line_num_digits) : (x += 1) {
const rx = x + self.coloff;
if (rx >= row.rdata.items.len) {
break;
}
const char = row.rdata.items[rx];
if (std.ascii.isControl(char)) {
const sym = [_]u8{if (char <= 26) char + '@' else '?'};
try writer.print("\x1b[7m{s}\x1b[m", .{&sym});
last_hl = Highlight.none;
} else {
const hl = if (rx > self.config.line_limit) Highlight.overlong_line else row.hldata.items[rx];
if (hl != last_hl) {
try writer.writeAll(hl.asString());
last_hl = hl;
}
try writer.writeByte(char);
}
}
}
} else if (self.rows.items.len == 0 and y == screenrows / 3) {
const welcome_data = try std.fmt.allocPrint(
self.allocator,
"ES -- version {}",
.{es.conf.es_version},
);
defer self.allocator.free(welcome_data);
var welcome = welcome_data;
if (welcome.len > screencols - 1) {
welcome = welcome[0..(screencols - 1)];
}
const padding = (screencols - welcome.len - 1) / 2;
try writer.print("{s}~{s}", .{ Highlight.line_no.asString(), Highlight.normal.asString() });
try printWithLeftPadding(self.allocator, writer, "{s}", welcome.len + padding, ' ', .{welcome});
} else {
try writer.print("{s}~", .{Highlight.line_no.asString()});
}
try writer.writeAll("\x1b[K\r\n");
}
}
pub fn drawStatusBar(self: Buffer, writer: anytype, screencols: usize) !void {
try writer.writeAll("\x1b[m\x1b[7m");
const name = if (self.short_name.len > 20)
try std.fmt.allocPrint(
self.allocator,
"...{s}",
.{self.short_name[self.short_name.len - 17 ..]},
)
else
try self.allocator.dupe(u8, self.short_name);
defer self.allocator.free(name);
const modified = if (self.isDirty())
@as([]const u8, " (modified)")
else
@as([]const u8, "");
var lbuf = try std.fmt.allocPrint(self.allocator, "{s}{s}", .{ name, modified });
defer self.allocator.free(lbuf);
var rbuf = try std.fmt.allocPrint(self.allocator, " {s} | {}/{}", .{
if (self.syntax) |syntax| syntax.name else "Fundamental",
self.cy + 1,
self.rows.items.len,
});
defer self.allocator.free(rbuf);
const rlen = if (rbuf.len > screencols) screencols else rbuf.len;
const llen = if (lbuf.len > screencols - rlen) screencols - rlen else lbuf.len;
try writer.writeAll(lbuf[0..llen]);
try writer.writeByteNTimes(' ', screencols - llen - rlen);
try writer.writeAll(rbuf[0..rlen]);
try writer.writeAll("\x1b[m\r\n");
}
pub fn endOfBuffer(self: *Buffer) void {
self.cx = 0;
self.cy = self.rows.items.len;
}
pub fn findBackwardWordStart(self: Buffer) usize {
if (self.cy == self.rows.items.len) {
return 0;
}
const chars = self.rows.items[self.cy].data.items;
var start = self.cx;
// First we skip non-word
while (start > 0) : (start -= 1) {
if (std.ascii.isAlphanumeric(chars[start - 1])) {
break;
}
}
// Then we skip word
while (start > 0) : (start -= 1) {
if (!std.ascii.isAlphanumeric(chars[start - 1])) {
break;
}
}
return start;
}
pub fn findForwardWordEnd(self: Buffer) usize {
if (self.cy == self.rows.items.len) {
return 0;
}
const chars = self.rows.items[self.cy].data.items;
var end = self.cx;
// First we skip non-word
while (end < chars.len) : (end += 1) {
if (std.ascii.isAlphanumeric(chars[end])) {
break;
}
}
// Then we skip word
while (end < chars.len) : (end += 1) {
if (!std.ascii.isAlphanumeric(chars[end])) {
break;
}
}
return end;
}
pub fn forwardChar(self: *Buffer) void {
if (self.rows.items.len == self.cy) {
return;
}
if (self.cx == self.rows.items[self.cy].data.items.len) {
self.cx = 0;
self.cy += 1;
} else {
self.cx += 1;
}
}
pub fn forwardParagraph(self: *Buffer) void {
if (self.cy == self.rows.items.len) {
return;
}
self.cy += 1;
while (self.cy < self.rows.items.len) : (self.cy += 1) {
// TODO: I'm lazy but also consider whitespace-only lines
if (self.rows.items[self.cy].data.items.len == 0) {
break;
}
}
self.cx = 0;
}
pub fn forwardWord(self: *Buffer) void {
const end = self.findForwardWordEnd();
if (end == self.cx) {
self.forwardChar();
} else {
self.cx = end;
}
}
// TODO: Make use of the callback feature to go to the final line while typing in.
pub fn goToLine(self: *Buffer, editor: *Editor) !void {
if (try editor.prompt(self.allocator, "Goto line")) |line_str| {
defer self.allocator.free(line_str);
const line = std.fmt.parseUnsigned(usize, line_str, 0) catch |err| {
try editor.setStatusMessage("Couldn't parse '{s}' as an integer: {}", .{ line_str, err });
return;
};
self.cy = std.math.clamp(line, 1, self.rows.items.len + 1) - 1;
if (self.cy == self.rows.items.len) {
self.cx = 0;
} else {
self.cx = @min(self.cx, self.rows.items[self.cy].data.items.len);
}
}
}
pub fn indent(self: *Buffer) !void {
if (self.config.hard_tabs) {
return self.insertChar('\t');
} else {
return self.insertNChars(' ', self.config.tab_stop - self.cx % self.config.tab_stop);
}
}
pub fn insertChar(self: *Buffer, char: u8) !void {
if (self.cy == self.rows.items.len) {
try self.insertRow(self.rows.items.len, "");
}
try self.rows.items[self.cy].insertChar(self, self.cx, char);
self.cx += 1;
self.edited = true;
}
pub fn insertNChars(self: *Buffer, char: u8, count: usize) !void {
var remaining = count;
while (remaining > 0) : (remaining -= 1) {
try self.insertChar(char);
}
}
pub fn insertNewline(self: *Buffer) !void {
self.edited = true;
if (self.cx == 0) {
try self.insertRow(self.cy, "");
self.cy += 1;
return;
}
var row = &self.rows.items[self.cy];
const indentation_size = row.indentationSize();
const indentation = row.data.items[0..indentation_size];
try self.insertRow(self.cy + 1, indentation);
row = &self.rows.items[self.cy];
try self.rows.items[self.cy + 1].appendString(self, row.data.items[self.cx..]);
try row.data.resize(self.cx);
try row.update(self);
self.cx = indentation.len;
self.cy += 1;
}
pub fn insertRow(self: *Buffer, at: usize, data: []const u8) !void {
var row = try Row.init(self.allocator, at, data);
errdefer row.deinit();
try self.rows.insert(at, row);
var i: usize = at + 1;
while (i < self.rows.items.len) : (i += 1) {
self.rows.items[i].idx += 1;
}
try self.rows.items[at].update(self);
self.edited = true;
}
pub fn isDirty(self: Buffer) bool {
if (self.short_name.len > 0) {
if (self.short_name[0] == '*' and self.short_name[self.short_name.len-1] == '*') {
return false;
}
}
return self.edited;
}
pub fn killLine(self: *Buffer) !void {
if (self.cy == self.rows.items.len) {
return;
}
var row = &self.rows.items[self.cy];
if (self.cx == row.data.items.len) {
return self.deleteChar();
} else {
self.edited = true;
try row.data.resize(self.cx);
return row.update(self);
}
}
pub fn lineNumberDigits(self: Buffer) usize {
if (self.rows.items.len == 0) {
return 2;
}
return 2 + std.math.log10(self.rows.items.len);
}
pub fn moveBeginningOfLine(self: *Buffer) void {
self.cx = 0;
}
pub fn moveEndOfLine(self: *Buffer) void {
if (self.rows.items.len == self.cy) {
self.cx = 0;
} else {
self.cx = self.rows.items[self.cy].data.items.len;
}
}
pub fn nextLine(self: *Buffer) void {
if (self.rows.items.len == self.cy) {
return;
}
self.cy += 1;
if (self.rows.items.len == self.cy) {
self.cx = 0;
} else {
self.cx = self.rows.items[self.cy].rxToCx(self.config, self.rx);
}
}
pub fn pageDown(self: *Buffer, editor: Editor) void {
const screenrows = editor.screenrows;
self.cy = std.math.clamp(
self.rowoff + screenrows - 1 - self.config.page_overlap,
0,
self.rows.items.len,
);
var i: usize = 0;
while (i < screenrows) : (i += 1) {
self.nextLine();
}
}
pub fn pageUp(self: *Buffer, editor: Editor) void {
const screenrows = editor.screenrows;
self.cy = @min(self.rows.items.len, self.rowoff + self.config.page_overlap);
var i: usize = 0;
while (i < screenrows) : (i += 1) {
self.previousLine();
}
}
pub fn previousLine(self: *Buffer) void {
if (self.cy == 0) {
return;
}
self.cy -= 1;
self.cx = self.rows.items[self.cy].rxToCx(self.config, self.rx);
}
pub fn recenterTopBottom(self: *Buffer, editor: *Editor) !void {
// TODO: Currently only recenters
try editor.refreshWindowSize();
const screenrows = editor.screenrows;
if (self.cy >= screenrows / 2) {
self.rowoff = self.cy - screenrows / 2;
} else {
self.rowoff = 0;
}
}
pub fn save(self: *Buffer, editor: *Editor) !void {
if (self.file_path == null) {
const fname = (try editor.prompt(self.allocator, "Save as")) orelse {
return;
};
defer self.allocator.free(fname);
const file_path = try es.files.resolvePath(self.allocator, fname);
errdefer self.allocator.free(file_path);
const file_name = std.fs.path.basename(file_path);
const short_name = try editor.getUniqueBufferName(self.allocator, file_name);
errdefer self.allocator.free(short_name);
self.allocator.free(self.short_name);
self.short_name = short_name;
self.file_path = file_path;
// TODO: Do I want to do this?
// try self.selectSyntaxHighlighting();
}
// TODO: Add a config value for this
try self.cleanWhiteSpace();
const file_path = self.file_path.?;
if (std.fs.path.dirname(file_path)) |dirname| {
var res_dir = std.fs.openDirAbsolute(dirname, .{});
if (res_dir) |*dir| {
dir.close();
} else |_| {
const prompt = try std.fmt.allocPrint(
self.allocator,
"Create parent directory '{s}'?",
.{dirname},
);
defer self.allocator.free(prompt);
if (try editor.promptYN(prompt)) {
std.fs.cwd().makePath(dirname) catch |err| {
try editor.setStatusMessage(
"Cannot create parent directory '{s}': {}",
.{ dirname, err },
);
};
} else {
return;
}
}
}
const tmp_path = try std.fmt.allocPrint(
self.allocator,
"{s}~{}",
.{ file_path, std.time.milliTimestamp() },
);
defer self.allocator.free(tmp_path);
const tmp_file = std.fs.createFileAbsolute(tmp_path, .{ .truncate = true }) catch |err| {
try editor.setStatusMessage(
"Cannot open tempfile '{s}' for writing: {}",
.{ tmp_path, err },
);
return;
};
defer tmp_file.close();
const mode = statFileMode(file_path) catch |err| {
try editor.setStatusMessage("Couldn't stat file '{s}': {}", .{ file_path, err });
return;
};
tmp_file.chmod(mode) catch |err| {
try editor.setStatusMessage("Couldn't chmod tempfile '{s}': {}", .{ tmp_path, err });
return;
};
// TODO: chown
self.writeToFile(tmp_file) catch |err| {
try editor.setStatusMessage("Couldn't write to tempfile '{s}': {}", .{ tmp_path, err });
return;
};
std.fs.renameAbsolute(tmp_path, file_path) catch |err| {
try editor.setStatusMessage(
"Couldn't move '{s}' to '{s}': {}",
.{ tmp_path, file_path, err },
);
return;
};
try editor.setStatusMessage("Saved to '{s}'", .{file_path});
self.edited = false;
}
pub fn scroll(self: *Buffer, screenrows: usize, screencols: usize) void {
self.rx = 0;
if (self.cy < self.rows.items.len) {
self.rx = self.rows.items[self.cy].cxToRx(self.config, self.cx);
}
if (self.cy < self.rowoff) {
self.rowoff = self.cy;
} else if (self.cy >= self.rowoff + screenrows) {
self.rowoff = self.cy + 1 - screenrows;
}
if (self.rx < self.coloff) {
self.coloff = self.rx;
} else if (self.rx + self.lineNumberDigits() >= self.coloff + screencols) {
self.coloff = self.lineNumberDigits() + self.rx + 1 - screencols;
}
}
pub fn selectSyntaxHighlighting(self: *Buffer) !void {
self.syntax = null;
const name = if (self.file_path) |file_path|
std.fs.path.basename(file_path)
else
self.short_name;
self.syntax = Syntax.chooseSyntax(name);
if (self.syntax == null) {
if (std.mem.lastIndexOfScalar(u8, name, '.')) |idx| {
self.syntax = Syntax.chooseSyntax(name[idx..]);
}
}
}
pub fn unindent(self: *Buffer) !void {
if (self.cy == self.rows.items.len) {
return;
}
// Find the end of starting whitespace
const row = &self.rows.items[self.cy];
var indentation_size = row.indentationSize();
if (indentation_size == 0) {
return;
}
const indentation_rsize = row.cxToRx(self.config, indentation_size);
const desired_rsize = indentation_rsize - 1 - (indentation_rsize - 1) % self.config.tab_stop;
const desired_size = row.rxToCx(self.config, desired_rsize);
if (self.cx < indentation_size - desired_size) {
self.cx = 0;
} else {
self.cx -= indentation_size - desired_size;
}
// TODO: ArrayList needs orderedRemoveNItems
while (indentation_size > desired_size) : (indentation_size -= 1) {
try row.deleteChar(self, desired_size);
}
}
fn printWithLeftPadding(
allocator: Allocator,
writer: anytype,
comptime fmt: []const u8,
width: usize,
padding: u8,
args: anytype,
) !void {
const unpadded = try std.fmt.allocPrint(allocator, fmt, args);
defer allocator.free(unpadded);
std.debug.assert(unpadded.len <= width);
try writer.writeByteNTimes(padding, width - unpadded.len);
try writer.writeAll(unpadded);
}
fn statFileMode(name: []const u8) !File.Mode {
const file = std.fs.cwd().openFile(name, .{}) catch |err| switch (err) {
error.FileNotFound => return 0o644,
else => return err,
};
defer file.close();
return (try file.stat()).mode;
}
fn writeToFile(self: Buffer, file: File) !void {
var buffered_writer = std.io.bufferedWriter(file.writer());
const writer = buffered_writer.writer();
for (self.rows.items) |row| {
try writer.writeAll(row.data.items);
try writer.writeByte('\n');
}
try buffered_writer.flush();
}
|