beginEditingSnippetE :: EditorM () -> Snippet -> EditorM ()
beginEditingSnippetE escapeAction snip = do
    withCurrentBuffer (deleteB unitWord Backward)
    Point origin <- withCurrentBuffer pointB
    filenameValue <- withCurrentBuffer (gets identString)
    let editState0 =
            (\(EditState x vars) ->
                EditState x (M.insert filename (DefaultValue (R.fromText filenameValue)) vars))
            (initialEditState snip)
    withCurrentBuffer (putBufferDyn editState0)
    oldKeymap <- withCurrentBuffer (gets (withMode0 modeKeymap))

    withCurrentBuffer $ do
        let (offset, s) = renderSnippet snip editState0
        insertN s
        moveTo (Point (origin + offset))

    let go SEEscape = do
            withCurrentBuffer (modifyMode $ modeKeymapA .~ oldKeymap)
            escapeAction
        go action = withCurrentBuffer $ do
            editState <- getBufferDyn

            let nextEditState = advanceEditState editState action
                (_, prevS) = renderSnippet snip editState
            moveTo (Point origin)
            deleteN (R.length prevS)

            let (offset, s) = renderSnippet snip nextEditState
            insertN s
            moveTo (Point (origin + offset))

            case nextEditState of
                EditState (Just _, _) _ -> putBufferDyn nextEditState
                _ -> modifyMode $ modeKeymapA .~ oldKeymap
    withCurrentBuffer $ modifyMode $ modeKeymapA .~ topKeymapA .~ choice
        [ printableChar >>=! go . SEInsertChar
        , Event KEsc [] ?>>! go SEEscape
        , Event KTab [] ?>>! go SENext
        , Event KBS [] ?>>! go SEBackSpace
        , Event (KASCII 'h') [MCtrl] ?>>! go SEBackSpace
        , Event (KASCII '[') [MCtrl] ?>>! go SEEscape
        , Event (KASCII 'i') [MCtrl] ?>>! go SENext
        ]
beginEditingSnippetE :: EditorM () -> Snippet -> EditorM ()
beginEditingSnippetE escapeAction snip = do
    withCurrentBuffer (deleteB unitWord Backward)
    Point origin <- withCurrentBuffer pointB
    filenameValue <- withCurrentBuffer (gets identString)
    let editState0 =
            (\(EditState x vars) ->
                EditState x (M.insert filename (DefaultValue (R.fromText filenameValue)) vars))
            (initialEditState snip)
    withCurrentBuffer (putBufferDyn editState0)
    oldKeymap <- withCurrentBuffer (gets (withMode0 modeKeymap))

    withCurrentBuffer $ do
        let (offset, s) = renderSnippet snip editState0
        insertN s
        moveTo (Point (origin + offset))

    let go SEEscape = do
            withCurrentBuffer (modifyMode $ modeKeymapA .~ oldKeymap)
            escapeAction
        go action = withCurrentBuffer $ do
            editState <- getBufferDyn

            let nextEditState = advanceEditState editState action
                (_, prevS) = renderSnippet snip editState
            moveTo (Point origin)
            deleteN (R.length prevS)

            let (offset, s) = renderSnippet snip nextEditState
            insertN s
            moveTo (Point (origin + offset))

            case nextEditState of
                EditState (Just _, _) _ -> putBufferDyn nextEditState
                _ -> modifyMode $ modeKeymapA .~ oldKeymap
    withCurrentBuffer $ modifyMode $ modeKeymapA .~ topKeymapA .~ choice
        [ printableChar >>=! go . SEInsertChar
        , Event KEsc [] ?>>! go SEEscape
        , Event KTab [] ?>>! go SENext
        , Event KBS [] ?>>! go SEBackSpace
        , Event (KASCII 'h') [MCtrl] ?>>! go SEBackSpace
        , Event (KASCII '[') [MCtrl] ?>>! go SEEscape
        , Event (KASCII 'i') [MCtrl] ?>>! go SENext
        ]
beginEditingSnippetE escapeAction snip = do
    withCurrentBuffer (deleteB unitWord Backward)
    Point origin <- withCurrentBuffer pointB
    filenameValue <- withCurrentBuffer (gets identString)
    let editState0 =
            (\(EditState x vars) ->
                EditState x (M.insert filename (DefaultValue (R.fromText filenameValue)) vars))
            (initialEditState snip)
    withCurrentBuffer (putBufferDyn editState0)
    oldKeymap <- withCurrentBuffer (gets (withMode0 modeKeymap))

    withCurrentBuffer $ do
        let (offset, s) = renderSnippet snip editState0
        insertN s
        moveTo (Point (origin + offset))

    let go SEEscape = do
            withCurrentBuffer (modifyMode $ modeKeymapA .~ oldKeymap)
            escapeAction
        go action = withCurrentBuffer $ do
            editState <- getBufferDyn

            let nextEditState = advanceEditState editState action
                (_, prevS) = renderSnippet snip editState
            moveTo (Point origin)
            deleteN (R.length prevS)

            let (offset, s) = renderSnippet snip nextEditState
            insertN s
            moveTo (Point (origin + offset))

            case nextEditState of
                EditState (Just _, _) _ -> putBufferDyn nextEditState
                _ -> modifyMode $ modeKeymapA .~ oldKeymap
    withCurrentBuffer $ modifyMode $ modeKeymapA .~ topKeymapA .~ choice
        [ printableChar >>=! go . SEInsertChar
        , Event KEsc [] ?>>! go SEEscape
        , Event KTab [] ?>>! go SENext
        , Event KBS [] ?>>! go SEBackSpace
        , Event (KASCII 'h') [MCtrl] ?>>! go SEBackSpace
        , Event (KASCII '[') [MCtrl] ?>>! go SEEscape
        , Event (KASCII 'i') [MCtrl] ?>>! go SENext
        ]