ed

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

commit f78704ab0f94163d970896d6625c40d9e956edf1
parent d5d23ab6a6497a337e79f55b3a1e4d80a454ff2b
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat,  6 Jan 2024 13:03:45 +0000

add cmd_edit_forced; if block style

Diffstat:
Mcommand.ha | 47+++++++++++++++++++++++++++++------------------
1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/command.ha b/command.ha @@ -192,13 +192,7 @@ fn cmd_edit(s: *Session, cmd: *Command) (void | Error) = { return NoFilename; }; - const h = match (os::open(fname)) { - case let err: fs::error => - return err; - case let h: io::file => - yield h: io::handle; - }; - defer io::close(h)!; + const h = os::open(fname)?; defer io::close(h)!; buf_deleteall(s.buf); const (sz, _) = buf_read(s.buf, h, 0)?; @@ -208,7 +202,28 @@ fn cmd_edit(s: *Session, cmd: *Command) (void | Error) = { s.buf.cursor = len(s.buf.lines) - 1; }; -fn cmd_edit_forced(s: *Session, cmd: *Command) (void | Error) = void; +fn cmd_edit_forced(s: *Session, cmd: *Command) (void | Error) = { + assert_noaddrs(s, cmd.linenums)?; + + const fname = if (len(cmd.arg1) != 0) { + //free(s.buf.filename); + s.buf.filename = strings::dup(cmd.arg1); + yield cmd.arg1; + } else if (len(s.buf.filename) != 0) { + yield s.buf.filename; + } else { + return NoFilename; + }; + + const h = os::open(fname)?; defer io::close(h)!; + + buf_deleteall(s.buf); + const (sz, _) = buf_read(s.buf, h, 0)?; + if (!s.suppressmode) { + fmt::println(sz)!; + }; + s.buf.cursor = len(s.buf.lines) - 1; +}; fn cmd_filename(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; @@ -536,12 +551,10 @@ fn cmd_read(s: *Session, cmd: *Command) (void | Error) = { const fname = if (len(cmd.arg1) != 0) { s.buf.filename = cmd.arg1; yield cmd.arg1; + } else if (len(s.buf.filename) != 0) { + yield s.buf.filename; } else { - yield if (len(s.buf.filename) != 0) { - yield s.buf.filename; - } else { - return NoFilename; - }; + return NoFilename; }; const h = os::open(fname)?: io::handle; @@ -655,12 +668,10 @@ fn cmd_write(s: *Session, cmd: *Command) (void | Error) = { // free(s.buf.filename); s.buf.filename = strings::dup(cmd.arg1); yield s.buf.filename; + } else if (len(s.buf.filename) != 0) { + yield s.buf.filename; } else { - yield if (len(s.buf.filename) != 0) { - yield s.buf.filename; - } else { - return NoFilename; - }; + return NoFilename; }; const h = match (os::open(fname, fs::flag::WRONLY)) {