ed

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

commit d525d4b5711b5d5b1a1205c92a208840e14a033a
parent 596f6d1fa4c51ecce0dd6c73fe185fcf1bdbcfa0
Author: Byron Torres <b@torresjrjr.com>
Date:   Sat, 10 Dec 2022 01:34:12 +0000

commandtype -> commandfn

Diffstat:
Mcommand.ha | 68++------------------------------------------------------------------
Mparse.ha | 105+++++++++++++++++++++++++++----------------------------------------------------
Mutil.ha | 4++++
3 files changed, 42 insertions(+), 135 deletions(-)

diff --git a/command.ha b/command.ha @@ -48,71 +48,7 @@ fn execute(s: *session) (void | error) = { exec_addrs(s, &s.cmd)?; defer delete(s.cmd.linenums[..]); // TODO: write finish_cmd() - switch (s.cmd.cmdtype) { - case commandtype::APPEND => - fmt::errorfln("TODO: append")!; - case commandtype::CHANGE => - fmt::errorfln("TODO: change")!; - case commandtype::DELETE => - cmd_delete(s, &s.cmd): void; - case commandtype::EDIT => - cmd_edit(s, &s.cmd): void; - case commandtype::EDIT_FORCED => - fmt::errorfln("TODO: edit_forced")!; - case commandtype::FILENAME => - cmd_filename(s, &s.cmd): void; - case commandtype::GLOBAL => - fmt::errorfln("TODO: global")!; - case commandtype::GLOBAL_INTERACTIVE => - fmt::errorfln("TODO: global_interactive")!; - case commandtype::HELP => - cmd_help(s, &s.cmd): void; - case commandtype::HELPMODE => - cmd_helpmode(s, &s.cmd): void; - case commandtype::INSERT => - fmt::errorfln("TODO: insert")!; - case commandtype::JOIN => - fmt::errorfln("TODO: join")!; - case commandtype::MARK => - fmt::errorfln("TODO: mark")!; - case commandtype::LIST => - fmt::errorfln("TODO: list")!; - case commandtype::MOVE => - fmt::errorfln("TODO: move")!; - case commandtype::NUMBER => - cmd_number(s, &s.cmd): void; - case commandtype::PRINT => - cmd_print(s, &s.cmd): void; - case commandtype::PROMPT=> - cmd_prompt(s, &s.cmd): void; - case commandtype::QUIT => - fmt::errorfln("TODO: quit")!; - case commandtype::READ => - cmd_read(s, &s.cmd); - case commandtype::SUBSTITUTE => - fmt::errorfln("TODO: substitute")!; - case commandtype::COPY => - fmt::errorfln("TODO: copy")!; - case commandtype::UNDO => - fmt::errorfln("TODO: undo")!; - case commandtype::GLOBAL_INVERSE => - fmt::errorfln("TODO: global_inverse")!; - case commandtype::GLOBAL_INVERSE_INTERACTIVE => - fmt::errorfln("TODO: global_inverse_interactive")!; - case commandtype::WRITE => - cmd_write(s, &s.cmd): void; - case commandtype::LINE_NUMBER => - cmd_linenumber(s, &s.cmd); - case commandtype::SHELL_ESCAPE => - fmt::errorfln("TODO: shell_escape")!; - case commandtype::NULL => - cmd_null(s, &s.cmd): void; - - case commandtype::DEBUG_NUMBER => - dumpbuffer(&s.buf); - case => - fmt::errorfln("DEBUG: Unknown command")!; - }; + s.cmd.cmdfn(s, &s.cmd)?; }; fn get_range(s: *session, lns: *[]size, a: size, b: size) ((size, size) | badaddress) = { @@ -151,7 +87,7 @@ fn cmd_prompt(s: *session, cmd: *command) (void | error) = { s.promptmode = !s.promptmode; }; -fn cmd_linenumber(s: *session, cmd: *command) void = { +fn cmd_linenumber(s: *session, cmd: *command) (void | error) = { const n = get_linenum(cmd.linenums, addr_lastline(&s.buf)); fmt::println(n)!; }; diff --git a/parse.ha b/parse.ha @@ -7,47 +7,14 @@ use strings; type command = struct { addrs: []address, linenums: []size, - cmdtype: commandtype, + cmdfn: commandfn, suffix: suffix, arg: str, input: []str, subcmds: []command, }; -type commandtype = enum { - APPEND, - CHANGE, - DELETE, - EDIT, - EDIT_FORCED, - FILENAME, - GLOBAL, - GLOBAL_INTERACTIVE, - HELP, - HELPMODE, - INSERT, - JOIN, - MARK, - LIST, - MOVE, - NUMBER, - PRINT, - PROMPT, - QUIT, - QUIT_FORCED, - READ, - SUBSTITUTE, - COPY, - UNDO, - GLOBAL_INVERSE, - GLOBAL_INVERSE_INTERACTIVE, - WRITE, - LINE_NUMBER, - SHELL_ESCAPE, - NULL, - - DEBUG_NUMBER, -}; +type commandfn = *fn(_: *session, _: *command) (void | error); type suffix = enum { NONE, @@ -65,7 +32,7 @@ fn parse(cmd: *command, input: str) bool = { const iter = strings::iter(input); cmd.addrs = scan_addrs(&iter); - cmd.cmdtype = scan_cmdtype(&iter); + cmd.cmdfn = scan_cmdfn(&iter); cmd.arg = scan_arg(&iter); return true; @@ -246,46 +213,46 @@ fn scan_offset(iter: *strings::iterator) int = { }; }; -fn scan_cmdtype(iter: *strings::iterator) commandtype = { +fn scan_cmdfn(iter: *strings::iterator) commandfn = { let r = match (strings::next(iter)) { case void => - return commandtype::NULL; + return &cmd_null; case let r: rune => yield r; }; switch (r) { - case '=' => return commandtype::LINE_NUMBER; - case 'E' => return commandtype::EDIT_FORCED; - case 'G' => return commandtype::GLOBAL_INTERACTIVE; - case 'H' => return commandtype::HELPMODE; - case 'P' => return commandtype::PROMPT; - case 'Q' => return commandtype::QUIT_FORCED; - case 'V' => return commandtype::GLOBAL_INVERSE_INTERACTIVE; - case 'a' => return commandtype::APPEND; - case 'c' => return commandtype::CHANGE; - case 'd' => return commandtype::DELETE; - case 'e' => return commandtype::EDIT; - case 'f' => return commandtype::FILENAME; - case 'g' => return commandtype::GLOBAL; - case 'h' => return commandtype::HELP; - case 'i' => return commandtype::INSERT; - case 'j' => return commandtype::JOIN; - case 'k' => return commandtype::MARK; - case 'l' => return commandtype::LIST; - case 'm' => return commandtype::MOVE; - case 'n' => return commandtype::NUMBER; - case 'p' => return commandtype::PRINT; - case 'q' => return commandtype::QUIT; - case 'r' => return commandtype::READ; - case 's' => return commandtype::SUBSTITUTE; - case 't' => return commandtype::COPY; - case 'u' => return commandtype::UNDO; - case 'v' => return commandtype::GLOBAL_INVERSE; - case 'w' => return commandtype::WRITE; - - case 'b' => return commandtype::DEBUG_NUMBER; - case => abort("Invalid command"); + case '=' => return &cmd_linenumber; +// case 'E' => return &cmd_edit_forced; +// case 'G' => return &cmd_global_interactive; + case 'H' => return &cmd_helpmode; + case 'P' => return &cmd_prompt; +// case 'Q' => return &cmd_quit_forced; +// case 'V' => return &cmd_global_inverse_interactive; +// case 'a' => return &cmd_append; +// case 'c' => return &cmd_change; + case 'd' => return &cmd_delete; + case 'e' => return &cmd_edit; + case 'f' => return &cmd_filename; +// case 'g' => return &cmd_global; + case 'h' => return &cmd_help; +// case 'i' => return &cmd_insert; +// case 'j' => return &cmd_join; +// case 'k' => return &cmd_mark; +// case 'l' => return &cmd_list; +// case 'm' => return &cmd_move; + case 'n' => return &cmd_number; + case 'p' => return &cmd_print; +// case 'q' => return &cmd_quit; +// case 'r' => return &cmd_read; +// case 's' => return &cmd_substitute; +// case 't' => return &cmd_copy; +// case 'u' => return &cmd_undo; +// case 'v' => return &cmd_global_inverse; + case 'w' => return &cmd_write; + + case 'b' => return &cmd_dumpbuffer; + case => abort("Invalid command (unimplemented?)"); }; }; diff --git a/util.ha b/util.ha @@ -1,5 +1,9 @@ use fmt; +fn cmd_dumpbuffer(s: *session, cmd: *command) (void | error) = { + return dumpbuffer(&s.buf); +}; + fn dumpbuffer(buf: *buffer) void = { for (let n = 1z; n < len(buf.lines); n += 1) { fmt::printfln("{}\t{}", n, buf.lines[n].text)!;