ed

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

commit debc52be43ee6c77c9c3448d5939a3990ff9b32e
parent 001cb7ccdde7bff7d48911fe53bfd4aa2fa799eb
Author: Byron Torres <b@torresjrjr.com>
Date:   Sun,  7 Jan 2024 20:12:23 +0000

tidy execute.ha

Diffstat:
Mexecute.ha | 64+++++++++++++++++++++++++---------------------------------------
1 file changed, 25 insertions(+), 39 deletions(-)

diff --git a/execute.ha b/execute.ha @@ -1,38 +1,28 @@ // Executes the Session's .cmd Command. fn execute(s: *Session, cmd: *Command) (void | Error) = { - // TODO: move this into the cmd_* functions? match (exec_addrs(s, cmd)) { case void => void; - case let err: CmdError => - errormsg(s, err); - return err; + case let e: CmdError => + errormsg(s, e); // TODO: move up into caller? + return e; }; - match (lookupcmd(cmd.name)(s, cmd)) { case void => void; - case let err: Error => - errormsg(s, err); - return err; + case let e: Error => + errormsg(s, e); // TODO: move up into caller? + return e; }; - //debug("execute(): end"); }; fn exec_addrs(s: *Session, cmd: *Command) (void | CmdError) = { for (let i = 0z; i < len(cmd.addrs); i += 1) { const addr = cmd.addrs[i]; let n = exec_addr(s, addr)?; - //debug("exec_addrs(): offs={}", addr.lineoffset); - n = n + addr.lineoffset: size; // beware of negatives - //debug("exec_addrs(): n={}", n); + n = n + addr.lineoffset: size; // TODO: beware of negatives append(cmd.linenums, n); - for (let j = 0z; j < len(cmd.linenums); j += 1) { - void; //debug("exec_addrs(): cmd.linenums[{}]={}", j, cmd.linenums[j]); - }; - - if (addr.setcurrentline) { + if (addr.setcurrentline) s.buf.cursor = n; - }; }; }; @@ -44,8 +34,8 @@ fn exec_addr(s: *Session, addr: Address) (size | CmdError) = { return s.buf.cursor; case LastLine => return addr_lastline(s.buf); - case let m: rune => - return addr_mark(s.buf, m)?; + case let mark: rune => + return addr_mark(s.buf, mark)?; case let rad: RegexAddr => return addr_regex(s.buf, rad, s.buf.cursor)?; }; @@ -53,37 +43,33 @@ fn exec_addr(s: *Session, addr: Address) (size | CmdError) = { fn get_range(s: *Session, lns: *[]size, a: size, b: size) ((size, size) | InvalidAddress) = { - //debug("get_range(): len(lns)={}", len(lns)); - const (a, b) = if (len(lns) == 0) { - yield (a, b); - } else if (len(lns) == 1) { - yield (lns[0], lns[0]); - } else { - yield (lns[ len(lns) - 2 ], lns[ len(lns) - 1 ]); - }; - //debug("get_range(): (a, b)=({}, {})", a, b); - if (a < 0 || a > b || b >= len(s.buf.lines)) { + const (a, b) = + if (len(lns) == 0) + (a, b) + else if (len(lns) == 1) + (lns[0], lns[0]) + else + (lns[len(lns) - 2], lns[len(lns) - 1]); + + if (a < 0 || a > b || b >= len(s.buf.lines)) return InvalidAddress; - }; + return (a, b); }; fn get_linenum(lns: []size, n: size) size = { - if (len(lns) == 0) { - return n; - } else { + if (len(lns) == 0) + return n + else return lns[len(lns)-1]; - }; }; fn assert_noaddrs(s: *Session, lns: []size) (void | UnexpectedAddress) = { - if (len(lns) != 0) { + if (len(lns) != 0) return UnexpectedAddress; - }; }; fn assert_nonzero(s: *Session, n: size) (void | InvalidAddress) = { - if (n < 1) { + if (n < 1) return InvalidAddress; - }; };