diff --git a/src/cg/06-textEditor.cg b/src/cg/06-textEditor.cg index daf420d..9540e8f 100644 --- a/src/cg/06-textEditor.cg +++ b/src/cg/06-textEditor.cg @@ -12,6 +12,33 @@ textEditor = name \ { state = state.{ lines = newLines, cursorCol = state.cursorCol + 1 }, emit = [] }; + insertMode = state \ + stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol }; + newUndoStack = [stateSnapshot, ...state.undoStack]; + { + state = state.{ + undoStack = newUndoStack, + mode = Insert + }, + emit = [] + }; + + escape = state \ { state = state.{ mode = Normal }, emit = [] }; + + undo = state \ state.undoStack + | [] \ { state = state, emit = [] } + | [prev, ...rest] \ ( + stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol }; + + { state = state.{ lines = prev.lines, cursorCol = prev.cursorCol, cursorRow = prev.cursorRow, undoStack = rest, redoStack = [stateSnapshot, ...state.redoStack] }, emit = [] }); + + redo = state \ state.redoStack + | [] \ { state = state, emit = [] } + | [prev, ...rest] \ ( + stateSnapshot = { lines = state.lines, cursorRow = state.cursorRow, cursorCol = state.cursorCol }; + + { state = state.{ lines = prev.lines, cursorCol = prev.cursorCol, cursorRow = prev.cursorRow, undoStack = [stateSnapshot, ...state.undoStack], redoStack = rest }, emit = [] }); + backspace = state \ state.cursorCol == 0 | True \ ( @@ -84,6 +111,8 @@ textEditor = name \ cursorCol = 0, scrollX = 0, scrollY = 0, + undoStack = [], + redoStack = [], mode = Normal # Normal | Insert | Visual }, @@ -110,9 +139,17 @@ textEditor = name \ | Key { key = "i" } \ (state.mode | Insert \ insertChar "i" state - | Normal \ { state = state.{ mode = state.mode | Normal \ Insert | _ \ Insert } }) + | Normal \ insertMode state) + + | Key { key = "u" } \ (state.mode + | Insert \ insertChar "u" state + | Normal \ undo state) - | Key { key = "Escape" } \ { state = state.{ mode = state.mode | Insert \ Normal | _ \ Normal } } + | Key { key = "r", ctrl = True } \ (state.mode + | Insert \ insertChar "R" state + | Normal \ redo state) + + | Key { key = "Escape" } \ escape state | Key { key = "Backspace" } \ (state.mode | Insert \ backspace state @@ -130,8 +167,6 @@ textEditor = name \ | _ \ { state = state, emit = [] }, view = state emit \ - _ = debug! "view" state; - buffer = map (l \ text l) state.lines; scale = 2;