ed

[hare] The standard editor
Log | Files | Refs | README | LICENSE

commit 5998fc8c235ad32ad2fff480f91fc08272656bfb
parent da07e811f6362cbba6c4f843d777d28f4c9d7052
Author: Byron Torres <b@torresjrjr.com>
Date:   Thu, 18 Jan 2024 10:47:17 +0000

undo progress

Diffstat:
Mbuffer.ha | 20++++++++++++++------
Mfile.ha | 5+++++
Mhistory.ha | 9+++++++--
3 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/buffer.ha b/buffer.ha @@ -64,8 +64,12 @@ fn buf_delete(buf: *Buffer, a: size, b: size) void = { debug("buf_delete(): len('last changeseq')={}", len(buf.hist[len(buf.hist) - 1])); }; -fn buf_read(buf: *Buffer, src: io::handle, a: size) ((size, size) | io::error | encoding::utf8::invalid) = { - let ls: []*Line = []; +fn buf_read( + buf: *Buffer, + src: io::handle, + n: size, +) ((size, size) | io::error | encoding::utf8::invalid) = { + let lines: []*Line = []; let sz = 0z; for (true) { const bytes = match (bufio::read_line(src)?) { @@ -78,14 +82,18 @@ fn buf_read(buf: *Buffer, src: io::handle, a: size) ((size, size) | io::error | sz += len(bytes) + 1; // TODO: handle newlines better const text = strings::fromutf8(bytes)?; - append(ls, alloc(Line{ text = text, ... })); + append(lines, alloc(Line{ text = text, ... })); }; + const m = len(lines); + + insert(buf.lines[n + 1], lines...); + + hist_append(buf, (n + 1, m): Addition); - insert(buf.lines[a + 1], ls...); if (buf.written) buf.modified = true; - const lenls = len(ls); - return (sz, lenls); + + return (sz, m); }; fn buf_write(buf: *Buffer, dest: io::handle, a: size, b: size) (size | io::error) = { diff --git a/file.ha b/file.ha @@ -13,11 +13,16 @@ fn edit(s: *Session, cmd: *Command, forced: bool) (void | Error) = { return WarnBufferModified; }; + hist_clear(s.buf); + hist_newseq(s.buf); + const rd = os::open(filename(s, cmd, true)?)?; defer io::close(rd)!; buf_deleteall(s.buf); const (sz, _) = buf_read(s.buf, rd, 0)?; + hist_discardseq(s.buf); + if (!s.suppressmode) fmt::println(sz)!; diff --git a/history.ha b/history.ha @@ -16,11 +16,16 @@ type Deletion = (size, size); // There is no history to undo. type NoHistory = !void; +fn hist_clear(buf: *Buffer) void = { + delete(buf.hist[..]); +}; + fn hist_newseq(buf: *Buffer) void = { append(buf.hist, []: ChangeSeq); }; fn hist_discardseq(buf: *Buffer) void = { + debug("hist_discardseq()"); delete(buf.hist[len(buf.hist) - 1]); }; @@ -34,8 +39,8 @@ fn hist_undo(buf: *Buffer) (void | NoHistory) = { let seq = buf.hist[ len(buf.hist) - 1 ]; - for (let i = len(seq) - 1; i < len(seq); i -= 1) - match (seq[i]) { + for (let j = len(seq) - 1; j < len(seq); j -= 1) + match (seq[j]) { case let deln: Deletion => let (n, m) = deln; let lentrash = len(buf.trash);