ed

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

commit c45768710a8a811b5111ef547cd956d19a4bf78c
parent ab50cec3ff7bf1cf13f3a28deaf12ede76a6a4d0
Author: Byron Torres <b@torresjrjr.com>
Date:   Sun,  7 Jan 2024 21:40:08 +0000

tidy command.ha

Diffstat:
Mcommand.ha | 282+++++++++++++++++++++++++++++++++++++------------------------------------------
1 file changed, 132 insertions(+), 150 deletions(-)

diff --git a/command.ha b/command.ha @@ -115,9 +115,8 @@ fn cmd_append(s: *Session, cmd: *Command) (void | Error) = { const n = get_linenum(cmd.linenums, s.buf.cursor); for (let i = 0z; i < len(cmd.textinput); i += 1) { - const l = alloc(Line{ text = cmd.textinput[i], ... }); - //debug("cmd_append(): l.text=<{}>", l.text); - buf_insert(s.buf, n + 1 + i, l); + const line = alloc(Line{ text = cmd.textinput[i], ... }); + buf_insert(s.buf, n + 1 + i, line); }; s.buf.cursor = n + len(cmd.textinput); @@ -142,20 +141,18 @@ fn cmd_change(s: *Session, cmd: *Command) (void | Error) = { buf_delete(s.buf, a, b); for (let i = 0z; i < len(cmd.textinput); i += 1) { - const l = alloc(Line{ text = cmd.textinput[i], ... }); - //debug("cmd_append(): l.text=<{}>", l.text); - buf_insert(s.buf, a + i, l); + const line = alloc(Line{ text = cmd.textinput[i], ... }); + buf_insert(s.buf, a + i, line); }; - s.buf.cursor = if (len(cmd.textinput) == 0) { - yield if (len(s.buf.lines) == a) { - yield a - 1; - } else { - yield a; - }; - } else { - yield a + len(cmd.textinput) - 1; - }; + s.buf.cursor = + if (len(cmd.textinput) == 0) + if (len(s.buf.lines) == a) + a - 1 + else + a + else + a + len(cmd.textinput) - 1; }; fn cmd_delete(s: *Session, cmd: *Command) (void | Error) = { @@ -170,13 +167,14 @@ fn cmd_delete(s: *Session, cmd: *Command) (void | Error) = { assert_nonzero(s, a)?; buf_delete(s.buf, a, b); - s.buf.cursor = if (len(s.buf.lines) == 1) { - yield 0; - } else if (len(s.buf.lines) == a) { - yield a - 1; - } else { - yield a; - }; + + s.buf.cursor = + if (len(s.buf.lines) == 1) + 0 + else if (len(s.buf.lines) == a) + a - 1 + else + a; }; fn cmd_edit(s: *Session, cmd: *Command) (void | Error) = { @@ -187,46 +185,48 @@ fn cmd_edit(s: *Session, cmd: *Command) (void | Error) = { return BufferModified; }; - 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 fname = + if (len(cmd.arg1) != 0) { + 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) { + + if (!s.suppressmode) fmt::println(sz)!; - }; + s.buf.cursor = len(s.buf.lines) - 1; }; 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 fname = + if (len(cmd.arg1) != 0) { + 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) { + + if (!s.suppressmode) fmt::println(sz)!; - }; + s.buf.cursor = len(s.buf.lines) - 1; }; @@ -235,17 +235,11 @@ fn cmd_filename(s: *Session, cmd: *Command) (void | Error) = { assert_noaddrs(s, cmd.linenums)?; - if (cmd.arg1 != "") { - //debug("cmd_filename(): cmd.arg1 != 0"); - //free(s.buf.filename); - //debug("cmd_filename(): freed"); - debug("cmd_filename(): s.buf.filename = dup(cmd.arg1)"); + if (cmd.arg1 != "") s.buf.filename = strings::dup(cmd.arg1); - }; - if (s.buf.filename == "") { + if (s.buf.filename == "") return NoFilename; - }; fmt::println(s.buf.filename)!; }; @@ -262,19 +256,20 @@ fn cmd_help(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; assert_noaddrs(s, cmd.linenums)?; - if (s.lasterror is Error) { + + if (s.lasterror is Error) fmt::println(strerror(s.lasterror as Error))!; - }; }; fn cmd_helpmode(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; assert_noaddrs(s, cmd.linenums)?; + s.helpmode = !s.helpmode; - if (s.helpmode && s.lasterror is Error) { + + if (s.helpmode && s.lasterror is Error) fmt::println(strerror(s.lasterror as Error))!; - }; }; fn cmd_insert(s: *Session, cmd: *Command) (void | Error) = { @@ -284,9 +279,8 @@ fn cmd_insert(s: *Session, cmd: *Command) (void | Error) = { const n = if (n == 0) 1z else n; for (let i = 0z; i < len(cmd.textinput); i += 1) { - const l = alloc(Line{ text = cmd.textinput[i], ... }); - //debug("cmd_insert(): l.text=<{}>", l.text); - buf_insert(s.buf, n + i, l); + const line = alloc(Line{ text = cmd.textinput[i], ... }); + buf_insert(s.buf, n + i, line); }; s.buf.cursor = @@ -306,18 +300,16 @@ fn cmd_join(s: *Session, cmd: *Command) (void | Error) = { addr_nextline(s.buf, s.buf.cursor), )?; assert_nonzero(s, a)?; - - if (a == b) { + if (a == b) return; - }; let ls: []str = []; let mark = NUL; for (let n = a; n <= b; n += 1) { - const l = s.buf.lines[n]; - append(ls, l.text); + const line = s.buf.lines[n]; + append(ls, line.text); if (mark == NUL) { - mark = l.mark; + mark = line.mark; }; }; @@ -338,33 +330,24 @@ fn cmd_mark(s: *Session, cmd: *Command) (void | Error) = { const n = get_linenum(cmd.linenums, s.buf.cursor); assert_nonzero(s, n)?; + // TODO: improve this const mark = strings::torunes(cmd.arg1)[0]; - debug("cmd_mark(): mark={}", mark); - :search { - debug("cmd_mark(): search A"); - for (let i = 0z; i < len(s.buf.trash); i += 1) { - debug("cmd_mark(): search A i={}", i); - if (s.buf.trash[i].mark == mark) { - debug("cmd_mark(): search A i={} true", i); - s.buf.trash[i].mark = NUL; - yield :search; - }; - }; - debug("cmd_mark(): search B"); + :clearmark { for (let i = 0z; i < len(s.buf.lines); i += 1) { - debug("cmd_mark(): search B i={}", i); if (s.buf.lines[i].mark == mark) { - debug("cmd_mark(): search B i={} true", i); s.buf.lines[i].mark = NUL; - yield :search; + yield :clearmark; + }; + }; + for (let i = 0z; i < len(s.buf.trash); i += 1) { + if (s.buf.trash[i].mark == mark) { + s.buf.trash[i].mark = NUL; + yield :clearmark; }; }; - debug("cmd_mark(): search C"); }; - debug("cmd_mark(): search D"); - s.buf.lines[n].mark = mark; }; @@ -378,9 +361,9 @@ fn cmd_list(s: *Session, cmd: *Command) (void | Error) = { s.buf.cursor, )?; assert_nonzero(s, a)?; - //debug("cmd_list(): (a, b)=({}, {})", a, b); printlistlns(s.buf, a, b)?; + s.buf.cursor = b; }; @@ -397,39 +380,36 @@ fn cmd_move(s: *Session, cmd: *Command) (void | Error) = { // TODO: parse this properly in parse.ha? const iter = strings::iter(cmd.arg1); - const n = match (scan_addr(&iter)) { - case let addr: Address => - const n = match (exec_addr(s, addr)) { - case let n: size => - yield n; - case InvalidAddress => - return InvalidDestination; - }; - yield n + 1; // like insert + const addr = match (scan_addr(&iter)) { + case let a: Address => + yield a; case void => return InvalidAddress; }; - debug("cmd_move(): n={}", n); - - if (a < n && n <= b) { + const n = match (exec_addr(s, addr)) { + case let n: size => + yield n + 1; // like insert + case InvalidAddress => return InvalidDestination; }; - if (n == b + 1) { + if (a < n && n <= b) + return InvalidDestination; + + if (n == b + 1) return; - }; - const dest = if (n > b) { - yield n - (1 + b - a); - } else { - yield n; - }; + const dest = + if (n > b) + n - (1 + b - a) + else + n; - const ls = alloc(s.buf.lines[a..b+1]...); defer free(ls); // TODO: ? + const lines = alloc(s.buf.lines[a..b+1]...); defer free(lines); // TODO: mem? buf_delete(s.buf, a, b); - buf_insert(s.buf, dest, ls...); + buf_insert(s.buf, dest, lines...); - s.buf.cursor = dest - 1 + len(ls); + s.buf.cursor = dest - 1 + len(lines); }; fn cmd_number(s: *Session, cmd: *Command) (void | Error) = { @@ -442,9 +422,9 @@ fn cmd_number(s: *Session, cmd: *Command) (void | Error) = { s.buf.cursor, )?; assert_nonzero(s, a)?; - //debug("cmd_number(): (a, b)=({}, {})", a, b); printnumberlns(s.buf, a, b)?; + s.buf.cursor = b; }; @@ -460,6 +440,7 @@ fn cmd_print(s: *Session, cmd: *Command) (void | Error) = { assert_nonzero(s, a)?; printlns(s.buf, a, b)?; + s.buf.cursor = b; }; @@ -467,6 +448,7 @@ fn cmd_prompt(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; assert_noaddrs(s, cmd.linenums)?; + s.promptmode = !s.promptmode; }; @@ -487,22 +469,24 @@ fn cmd_read(s: *Session, cmd: *Command) (void | Error) = { s.warned = false; const n = get_linenum(cmd.linenums, s.buf.cursor); - 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 { - return NoFilename; - }; + 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 { + return NoFilename; + }; const h = os::open(fname)?: io::handle; - defer io::close(h)!; + defer io::close(h)!; const (sz, _) = buf_read(s.buf, h, n)?; - if (!s.suppressmode) { + + if (!s.suppressmode) fmt::println(sz)!; - }; + s.buf.cursor = len(s.buf.lines) - 1; }; @@ -527,11 +511,10 @@ fn cmd_substitute(s: *Session, cmd: *Command) (void | Error) = { for (let i = a; i <= b; i += 1) { const old = s.buf.lines[i].text; const results = regex::findall(&regex, old); - defer regex::result_freeall(results); + defer regex::result_freeall(results); - if (len(results) == 0) { + if (len(results) == 0) continue; - }; let iter = strings::iter(s.buf.lines[i].text); let new = memio::dynamic(); defer io::close(&new)!; @@ -571,23 +554,23 @@ fn cmd_copy(s: *Session, cmd: *Command) (void | Error) = { // TODO: parse this properly in parse.ha? const iter = strings::iter(cmd.arg1); - const dest = match (scan_addr(&iter)) { - case let addr: Address => - yield 1 + (match (exec_addr(s, addr)) { - case let n: size => - yield n; - case InvalidAddress => - return InvalidDestination; - }); + const addr = match (scan_addr(&iter)) { + case let a: Address => + yield a; case void => return InvalidAddress; }; - debug("cmd_copy(): dest={}", dest); + const dest = match (exec_addr(s, addr)) { + case let n: size => + yield n + 1; + case InvalidAddress => + return InvalidDestination; + }; - const ls = alloc(s.buf.lines[a..b+1]...); defer free(ls); // TODO: ? - buf_insert(s.buf, dest, ls...); + const lines = alloc(s.buf.lines[a..b+1]...); defer free(lines); // TODO: ? + buf_insert(s.buf, dest, lines...); - s.buf.cursor = dest - 1 + len(ls); + s.buf.cursor = dest - 1 + len(lines); }; fn cmd_undo(s: *Session, cmd: *Command) (void | Error) = void; @@ -611,15 +594,15 @@ fn cmd_write(s: *Session, cmd: *Command) (void | Error) = { )?; assert_nonzero(s, a)?; - const fname = if (len(cmd.arg1) != 0) { - // 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 { - return NoFilename; - }; + const fname = + if (len(cmd.arg1) != 0) { + s.buf.filename = strings::dup(cmd.arg1); + yield s.buf.filename; + } else if (len(s.buf.filename) != 0) { + yield s.buf.filename; + } else { + return NoFilename; + }; const h = match (os::open(fname, fs::flag::WRONLY)) { case let err: fs::error => @@ -632,12 +615,12 @@ fn cmd_write(s: *Session, cmd: *Command) (void | Error) = { case let h: io::file => yield h: io::handle; }; - defer io::close(h)!; + defer io::close(h)!; const sz = buf_write(s.buf, h, a, b)?; - if (!s.suppressmode) { + + if (!s.suppressmode) fmt::println(sz)!; - }; }; fn cmd_linenumber(s: *Session, cmd: *Command) (void | Error) = { @@ -702,13 +685,11 @@ fn cmd_shellescape(s: *Session, cmd: *Command) (void | Error) = { io::close(pipe.0)!; exec::wait(&proc)!; - if (preview) { + if (preview) fmt::println(shcmdline)!; - }; - fmt::print(strings::fromutf8(data)!)!; - if (!s.suppressmode) { + io::write(os::stdout, data)!; + if (!s.suppressmode) fmt::println("!")!; - }; s.prev_shcmd = shcmdline; }; @@ -723,5 +704,6 @@ fn cmd_null(s: *Session, cmd: *Command) (void | Error) = { assert_nonzero(s, n)?; fmt::println(s.buf.lines[n].text)!; + s.buf.cursor = n; };