ed

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

commit 6cb1f03ea0722b123261051d9de277762e4a3fc1
parent c8814150deb48370b8f758135b378599cac727e9
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue,  9 Jan 2024 23:13:53 +0000

refine filename

Diffstat:
Mcommand.ha | 8+++-----
Mfile.ha | 17+----------------
2 files changed, 4 insertions(+), 21 deletions(-)

diff --git a/command.ha b/command.ha @@ -192,8 +192,7 @@ fn cmd_filename(s: *Session, cmd: *Command) (void | Error) = { assert_noaddrs(s, cmd.linenums)?; - const fname = filename(s, cmd, true)?; - fmt::println(fname)!; + fmt::println(filename(s, cmd, true)?)!; }; fn cmd_global(s: *Session, cmd: *Command) (void | Error) = { @@ -424,8 +423,7 @@ fn cmd_read(s: *Session, cmd: *Command) (void | Error) = { const rd: io::handle = if (!strings::hasprefix(cmd.arg1, "!")) { - const fname = filename(s, cmd, false)?; - yield os::open(fname)?; + yield os::open(filename(s, cmd, false)?)?; } else { let shcmdline = strings::cut(cmd.arg1, "!").1; let shcmd = exec::cmd("sh", "-c", shcmdline)?; @@ -553,7 +551,7 @@ fn cmd_write(s: *Session, cmd: *Command) (void | Error) = { let proc: (void | exec::process) = void; const wr: io::handle = if (!strings::hasprefix(cmd.arg1, "!")) { - yield openwritefile(filename(s, cmd, false)?)?; + yield os::create(filename(s, cmd, false)?, 0o644)?; } else { let shcmdline = strings::cut(cmd.arg1, "!").1; let shcmd = exec::cmd("sh", "-c", shcmdline)?; diff --git a/file.ha b/file.ha @@ -13,8 +13,7 @@ fn edit(s: *Session, cmd: *Command, forced: bool) (void | Error) = { return WarnBufferModified; }; - const fname = filename(s, cmd, true)?; - const rd = os::open(fname)?; defer io::close(rd)!; + const rd = os::open(filename(s, cmd, true)?)?; defer io::close(rd)!; buf_deleteall(s.buf); const (sz, _) = buf_read(s.buf, rd, 0)?; @@ -36,17 +35,3 @@ fn filename(s: *Session, cmd: *Command, remember: bool) (str | NoFilename) = { return NoFilename; }; - -fn openwritefile(fname: str) (io::file | fs::error) = { - match (os::open(fname, fs::flag::WRONLY)) { - case let h: io::file => - return h; - case let e: fs::error => - match (e) { - case errors::noentry => - return os::create(fname, 0o644)?; - case => - return e; - }; - }; -};