ed

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

commit 3c9de16b8ac47fbda677d1b9c52faac470f405ef
parent 18bb4f889430b5f59936ce12aa199e8bc37df527
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue,  9 Jan 2024 00:50:16 +0000

add print.ha

Diffstat:
MMakefile | 1+
Aprint.ha | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mutil.ha | 67-------------------------------------------------------------------
3 files changed, 69 insertions(+), 67 deletions(-)

diff --git a/Makefile b/Makefile @@ -11,6 +11,7 @@ source=\ global.ha \ interaction.ha \ parse.ha \ + print.ha \ util.ha \ all: ed diff --git a/print.ha b/print.ha @@ -0,0 +1,68 @@ +use fmt; +use io; +use strings; + +type Printer = *fn(*Buffer, size, size) (size | io::error); + +fn printlns(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)?; + }; + return sz; +}; + +fn printnumberlns(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)?; + }; + return sz; +}; + +fn printlistlns(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)?; + }; + return sz; +}; + +fn printlistln(text: str) (size | io::error) = { + // TODO: handle wrapping (see ed(1) > list command) + const iter = strings::iter(text); + let sz = 0z; + + for (true) { + const r = match (strings::next(&iter)) { + case void => + break; + case let r: rune => + yield r; + }; + + sz += switch (r) { + case '\\' => + yield fmt::print("\\\\")?; + case '\a' => + yield fmt::print("\\a")?; + case '\b' => + yield fmt::print("\\b")?; + case '\f' => + yield fmt::print("\\f")?; + case '\r' => + yield fmt::print("\\r")?; + case '\t' => + yield fmt::print("\\t")?; + case '\v' => + yield fmt::print("\\v")?; + case '$' => + yield fmt::print("\\$")?; + case => + yield fmt::print(r)?; + }; + }; + + sz += fmt::println('$')?; + return sz; +}; diff --git a/util.ha b/util.ha @@ -1,6 +1,4 @@ use fmt; -use io; -use strings; fn cmd_dumpbuffer(s: *Session, cmd: *Command) (void | Error) = { return dumpbuffer(s.buf); @@ -17,68 +15,3 @@ fn debug(fmtstr: str, args: fmt::field...) void = { fmt::errorf(fmtstr, args...)!; fmt::errorln("\x1b[m")!; }; - -type Printer = *fn(*Buffer, size, size) (size | io::error); - -fn printlns(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)?; - }; - return sz; -}; - -fn printnumberlns(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)?; - }; - return sz; -}; - -fn printlistlns(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)?; - }; - return sz; -}; - -fn printlistln(text: str) (size | io::error) = { - // TODO: handle wrapping (see ed(1) > list command) - const iter = strings::iter(text); - let sz = 0z; - - for (true) { - const r = match (strings::next(&iter)) { - case void => - break; - case let r: rune => - yield r; - }; - - sz += switch (r) { - case '\\' => - yield fmt::print("\\\\")?; - case '\a' => - yield fmt::print("\\a")?; - case '\b' => - yield fmt::print("\\b")?; - case '\f' => - yield fmt::print("\\f")?; - case '\r' => - yield fmt::print("\\r")?; - case '\t' => - yield fmt::print("\\t")?; - case '\v' => - yield fmt::print("\\v")?; - case '$' => - yield fmt::print("\\$")?; - case => - yield fmt::print(r)?; - }; - }; - - sz += fmt::println('$')?; - return sz; -};