diff --git a/src/cg/01-stdlib.cg b/src/cg/01-stdlib.cg index b05de06..0686984 100644 --- a/src/cg/01-stdlib.cg +++ b/src/cg/01-stdlib.cg @@ -44,6 +44,11 @@ insertCharAt = text pos char \ after = slice text pos (len text); before & char & after; +deleteCharAt = text pos \ + before = slice text 0 pos; + after = slice text (pos + 1) (len text); + before & after; + # updateAt updateAt = i f list \ mapWithIndex (x j \ (i == j | True \ f x | False \ x) diff --git a/src/cg/06-textEditor.cg b/src/cg/06-textEditor.cg index ae12a39..da95b59 100644 --- a/src/cg/06-textEditor.cg +++ b/src/cg/06-textEditor.cg @@ -93,9 +93,16 @@ textEditor = name \ to = motion | Word \ nextWordStart state.lines from.row from.col | BackWord \ prevWordStart state.lines from.row from.col - | WholeLine \ from; + | WholeLine \ from + | Cursor \ from; { from = from, to = to }; + deleteChar = state \ + line = nth state.cursorRow state.lines ~ unwrapOr []; + newLine = deleteCharAt line state.cursorCol; + newLines = updateAt state.cursorRow (_ \ newLine) state.lines; + { state = state.{ lines = newLines, emit = [] } }; + deleteLine = state \ newLines = [...(take (state.cursorRow) state.lines), ...(drop (state.cursorRow + 1) state.lines)]; { state = state.{ lines = newLines, emit = [] } }; @@ -114,6 +121,7 @@ textEditor = name \ newState = operator | Delete \ (motion | WholeLine \ deleteLine state + | Cursor \ deleteChar state | _ \ { state = deleteRange state target.from target.to, emit = [] }); { state = clampCursor newState.state.{ undoStack = [stateSnapshot, ...state.undoStack], pending = None, lastAction = Some action }, emit = newState.emit }; @@ -311,6 +319,7 @@ textEditor = name \ | Key { key = "u", ctrl = True } \ scrollHalfUp state ctx | Key { key = "u" } \ undo state | Key { key = "r", ctrl = True } \ redo state + | Key { key = "x" } \ applyOperator Delete Cursor state | Key { key = "d" } \ (state.pending | Some Delete \ applyOperator Delete WholeLine state | _ \ { state = state.{ pending = Some Delete }, emit = [] })