packages feed

zwirn-0.2.3.1: app/zwirnmill/Editor/Undo.hs

module Editor.Undo where

import Editor.Core
import Editor.Util
import Lens.Micro

pushUndo :: EditorState -> EditorState
pushUndo es =
  let entry = UndoEntry (es ^. esZipper) (es ^. esSelection)
   in es
        & esUndoStack %~ (entry :) . take 200
        & esRedoStack .~ []
        & esUnsaved .~ True

undo :: EditorState -> EditorState
undo es = case es ^. esUndoStack of
  [] -> clearSelection $ setMessage "Nothing to undo." es
  (e : rest) ->
    let redoEntry = UndoEntry (es ^. esZipper) (es ^. esSelection)
     in es
          & esZipper .~ undoZipper e
          & esUndoStack .~ rest
          & esRedoStack %~ (redoEntry :)
          & esSelection .~ undoSelection e
          & esUnsaved .~ True
          & esMessage ?~ "Undo."

redo :: EditorState -> EditorState
redo es = case es ^. esRedoStack of
  [] -> setMessage "Nothing to redo." es
  (e : rest) ->
    let undoEntry = UndoEntry (es ^. esZipper) (es ^. esSelection)
     in es
          & esRedoStack .~ rest
          & esUndoStack %~ (undoEntry :)
          & esSelection .~ undoSelection e
          & esUnsaved .~ True
          & esMessage ?~ "Redo."