ed

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

commit 13ed056e61ea2ba5177a8b9dc08e85d5387b9e06
parent 90fa396a8ec963a7eedce9e1434840b438a38824
Author: Byron Torres <b@torresjrjr.com>
Date:   Wed, 10 Jan 2024 00:53:31 +0000

add printmode()

Diffstat:
Mcommand.ha | 30+++++++++++++++++++++++++-----
Mparse.ha | 8++++----
Mprint.ha | 24+++++++++++++++++++-----
3 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/command.ha b/command.ha @@ -24,9 +24,9 @@ type CommandFn = *fn(*Session, *Command) (void | Error); type PrintMode = enum{ NONE, - LIST, + PLAIN, NUMBER, - PRINT, + LIST, }; type CmdError = !( @@ -122,6 +122,7 @@ fn cmd_append(s: *Session, cmd: *Command) (void | Error) = { }; s.buf.cursor = n + len(cmd.textinput); + printmode(s, cmd)?; }; fn cmd_change(s: *Session, cmd: *Command) (void | Error) = { @@ -155,6 +156,7 @@ fn cmd_change(s: *Session, cmd: *Command) (void | Error) = { a else a + len(cmd.textinput) - 1; + printmode(s, cmd)?; }; fn cmd_delete(s: *Session, cmd: *Command) (void | Error) = { @@ -177,6 +179,7 @@ fn cmd_delete(s: *Session, cmd: *Command) (void | Error) = { a - 1 else a; + printmode(s, cmd)?; }; fn cmd_edit(s: *Session, cmd: *Command) (void | Error) = { @@ -210,6 +213,8 @@ fn cmd_help(s: *Session, cmd: *Command) (void | Error) = { if (s.lasterror is Error) fmt::println(strerror(s.lasterror as Error))!; + + printmode(s, cmd)?; }; fn cmd_helpmode(s: *Session, cmd: *Command) (void | Error) = { @@ -221,6 +226,8 @@ fn cmd_helpmode(s: *Session, cmd: *Command) (void | Error) = { if (s.helpmode && s.lasterror is Error) fmt::println(strerror(s.lasterror as Error))!; + + printmode(s, cmd)?; }; fn cmd_insert(s: *Session, cmd: *Command) (void | Error) = { @@ -239,6 +246,7 @@ fn cmd_insert(s: *Session, cmd: *Command) (void | Error) = { n else n + len(cmd.textinput) - 1; + printmode(s, cmd)?; }; fn cmd_join(s: *Session, cmd: *Command) (void | Error) = { @@ -273,6 +281,8 @@ fn cmd_join(s: *Session, cmd: *Command) (void | Error) = { buf_delete(s.buf, a, b); buf_insert(s.buf, a, newline); + + printmode(s, cmd)?; }; fn cmd_mark(s: *Session, cmd: *Command) (void | Error) = { @@ -300,6 +310,8 @@ fn cmd_mark(s: *Session, cmd: *Command) (void | Error) = { }; s.buf.lines[n].mark = mark; + + printmode(s, cmd)?; }; fn cmd_list(s: *Session, cmd: *Command) (void | Error) = { @@ -313,7 +325,7 @@ fn cmd_list(s: *Session, cmd: *Command) (void | Error) = { )?; assert_nonzero(s, a)?; - printlistlns(s.buf, a, b)?; + printlist(s.buf, a, b)?; s.buf.cursor = b; }; @@ -361,6 +373,7 @@ fn cmd_move(s: *Session, cmd: *Command) (void | Error) = { buf_insert(s.buf, dest, lines...); s.buf.cursor = dest - 1 + len(lines); + printmode(s, cmd)?; }; fn cmd_number(s: *Session, cmd: *Command) (void | Error) = { @@ -374,7 +387,7 @@ fn cmd_number(s: *Session, cmd: *Command) (void | Error) = { )?; assert_nonzero(s, a)?; - printnumberlns(s.buf, a, b)?; + printnumber(s.buf, a, b)?; s.buf.cursor = b; }; @@ -390,7 +403,7 @@ fn cmd_print(s: *Session, cmd: *Command) (void | Error) = { )?; assert_nonzero(s, a)?; - printlns(s.buf, a, b)?; + printplain(s.buf, a, b)?; s.buf.cursor = b; }; @@ -401,6 +414,8 @@ fn cmd_prompt(s: *Session, cmd: *Command) (void | Error) = { assert_noaddrs(s, cmd.linenums)?; s.promptmode = !s.promptmode; + + printmode(s, cmd)?; }; fn cmd_quit(s: *Session, cmd: *Command) (void | Error) = { @@ -493,6 +508,8 @@ fn cmd_substitute(s: *Session, cmd: *Command) (void | Error) = { buf_delete(s.buf, i, i); buf_insert(s.buf, i, newline); }; + + printmode(s, cmd)?; }; fn cmd_copy(s: *Session, cmd: *Command) (void | Error) = { @@ -525,6 +542,7 @@ fn cmd_copy(s: *Session, cmd: *Command) (void | Error) = { buf_insert(s.buf, dest, lines...); s.buf.cursor = dest - 1 + len(lines); + printmode(s, cmd)?; }; fn cmd_undo(s: *Session, cmd: *Command) (void | Error) = void; @@ -581,6 +599,8 @@ fn cmd_linenumber(s: *Session, cmd: *Command) (void | Error) = { const n = get_linenum(cmd.linenums, addr_lastline(s.buf)); fmt::println(n)!; + + printmode(s, cmd)?; }; fn cmd_shellescape(s: *Session, cmd: *Command) (void | Error) = { diff --git a/parse.ha b/parse.ha @@ -444,12 +444,12 @@ fn scan_suffix(iter: *strings::iterator) PrintMode = { }; switch (r) { - case 'l' => - return PrintMode::LIST; + case 'p' => + return PrintMode::PLAIN; case 'n' => return PrintMode::NUMBER; - case 'p' => - return PrintMode::PRINT; + case 'l' => + return PrintMode::LIST; case => strings::prev(iter); return PrintMode::NONE; diff --git a/print.ha b/print.ha @@ -4,7 +4,21 @@ use strings; type Printer = *fn(*Buffer, size, size) (size | io::error); -fn printlns(buf: *Buffer, a: size, b: size) (size | io::error) = { +fn printmode(s: *Session, cmd: *Command) (size | io::error) = { + let n = s.buf.cursor; + switch (cmd.printmode) { + case PrintMode::NONE => + return 0z; + case PrintMode::PLAIN => + return printplain(s.buf, n, n)?; + case PrintMode::NUMBER => + return printnumber(s.buf, n, n)?; + case PrintMode::LIST => + return printlist(s.buf, n, n)?; + }; +}; + +fn printplain(buf: *Buffer, a: size, b: size) (size | io::error) = { let sz = 0z; for (let n = a; n <= b; n += 1) { sz += fmt::println(buf.lines[n].text)?; @@ -12,7 +26,7 @@ fn printlns(buf: *Buffer, a: size, b: size) (size | io::error) = { return sz; }; -fn printnumberlns(buf: *Buffer, a: size, b: size) (size | io::error) = { +fn printnumber(buf: *Buffer, a: size, b: size) (size | io::error) = { let sz = 0z; for (let n = a; n <= b; n += 1) { sz += fmt::printfln("{}\t{}", n, buf.lines[n].text)?; @@ -20,15 +34,15 @@ fn printnumberlns(buf: *Buffer, a: size, b: size) (size | io::error) = { return sz; }; -fn printlistlns(buf: *Buffer, a: size, b: size) (size | io::error) = { +fn printlist(buf: *Buffer, a: size, b: size) (size | io::error) = { let sz = 0z; for (let n = a; n <= b; n += 1) { - sz += printlistln(buf.lines[n].text)?; + sz += printlistline(buf.lines[n].text)?; }; return sz; }; -fn printlistln(text: str) (size | io::error) = { +fn printlistline(text: str) (size | io::error) = { // TODO: handle wrapping (see ed(1) > list command) const iter = strings::iter(text); let sz = 0z;