ed

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

commit 8071aefcd32dcb1d1e909021b30343cac097e255
parent 38188ef19073b17473e208dbd791c19caf3fcee3
Author: Byron Torres <b@torresjrjr.com>
Date:   Tue, 13 Dec 2022 00:29:06 +0000

add buf_insert(), cmd_join()

Diffstat:
Mbuffer.ha | 5+++++
Mcommand.ha | 34+++++++++++++++++++++++++++++++++-
Mparse.ha | 2+-
3 files changed, 39 insertions(+), 2 deletions(-)

diff --git a/buffer.ha b/buffer.ha @@ -17,6 +17,11 @@ type line = struct { globalmark: bool, }; +fn buf_insert(buf: *buffer, n: size, ls: *line...) void = { + debug("buf_insert(n={}), len(ls)={}", n, len(ls)); + insert(buf.lines[n], ls...); +}; + fn buf_deleteall(buf: *buffer) void = { buf_wipetrash(buf); diff --git a/command.ha b/command.ha @@ -205,7 +205,39 @@ fn cmd_helpmode(s: *session, cmd: *command) (void | error) = { fn cmd_insert(s: *session, cmd: *command) (void | error) = void; -fn cmd_join(s: *session, cmd: *command) (void | error) = void; +fn cmd_join(s: *session, cmd: *command) (void | error) = { + const (a, b) = get_range( + s, + &cmd.linenums, + s.buf.cursor, + addr_nextline(&s.buf, s.buf.cursor), + )?; + assert_nonzero(s, a)?; + + if (a == b) { + return; + }; + + let ls: []str = []; + let mark = '\x00'; + for (let n = a; n <= b; n += 1) { + const l = s.buf.lines[n]; + append(ls, l.text); + if (mark == '\x00') { + mark = l.mark; + }; + }; + + const newtext = strings::concat(ls...); + const newline = alloc(line { + text = newtext, + mark = mark, + ... + }); + + buf_delete(&s.buf, a, b); + buf_insert(&s.buf, a, newline); +}; fn cmd_mark(s: *session, cmd: *command) (void | error) = { const n = get_linenum(cmd.linenums, s.buf.cursor); diff --git a/parse.ha b/parse.ha @@ -219,7 +219,7 @@ fn scan_cmdfn(iter: *strings::iterator) commandfn = { // case 'g' => return &cmd_global; case 'h' => return &cmd_help; // case 'i' => return &cmd_insert; -// case 'j' => return &cmd_join; + case 'j' => return &cmd_join; case 'k' => return &cmd_mark; // case 'l' => return &cmd_list; // case 'm' => return &cmd_move;