{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK show-extensions #-}

-- |
-- Module      :  Yi.Keymap.Vim.Ex.Eval
-- License     :  GPL-2
-- Maintainer  :  yi-devel@googlegroups.com
-- Stability   :  experimental
-- Portability :  portable

module Yi.Keymap.Vim.Ex.Eval
    ( exEvalE
    , exEvalY
    ) where

import           Control.Monad          (void)
import           Data.Monoid            ((<>))
import qualified Data.Text              as T (unpack)
import           Yi.Editor              (EditorM, MonadEditor (withEditor), withCurrentBuffer)
import           Yi.Keymap              (Action (BufferA, EditorA, YiA), YiM)
import           Yi.Keymap.Vim.Common   (EventString (_unEv))
import           Yi.Keymap.Vim.Ex.Types (ExCommand (cmdAction), evStringToExCommand)

exEvalE :: [EventString -> Maybe ExCommand] -> EventString -> EditorM ()
exEvalE :: [EventString -> Maybe ExCommand] -> EventString -> EditorM ()
exEvalE [EventString -> Maybe ExCommand]
cmds EventString
cmdString = (EditorM () -> EditorM ())
-> (YiM () -> EditorM ())
-> [EventString -> Maybe ExCommand]
-> EventString
-> EditorM ()
forall (m :: * -> *).
MonadEditor m =>
(EditorM () -> m ())
-> (YiM () -> m ())
-> [EventString -> Maybe ExCommand]
-> EventString
-> m ()
evalHelper EditorM () -> EditorM ()
forall a. a -> a
id (EditorM () -> YiM () -> EditorM ()
forall a b. a -> b -> a
const (EditorM () -> YiM () -> EditorM ())
-> EditorM () -> YiM () -> EditorM ()
forall a b. (a -> b) -> a -> b
$ [Char] -> EditorM ()
forall a. HasCallStack => [Char] -> a
error [Char]
msg) [EventString -> Maybe ExCommand]
cmds EventString
cmdString
    where msg :: [Char]
msg = Text -> [Char]
T.unpack (Text -> [Char]) -> (EventString -> Text) -> EventString -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EventString -> Text
_unEv (EventString -> [Char]) -> EventString -> [Char]
forall a b. (a -> b) -> a -> b
$ EventString
"exEvalE got impure command" EventString -> EventString -> EventString
forall a. Semigroup a => a -> a -> a
<> EventString
cmdString

exEvalY :: [EventString -> Maybe ExCommand] -> EventString -> YiM ()
exEvalY :: [EventString -> Maybe ExCommand] -> EventString -> YiM ()
exEvalY = (EditorM () -> YiM ())
-> (YiM () -> YiM ())
-> [EventString -> Maybe ExCommand]
-> EventString
-> YiM ()
forall (m :: * -> *).
MonadEditor m =>
(EditorM () -> m ())
-> (YiM () -> m ())
-> [EventString -> Maybe ExCommand]
-> EventString
-> m ()
evalHelper EditorM () -> YiM ()
forall (m :: * -> *) a. MonadEditor m => EditorM a -> m a
withEditor YiM () -> YiM ()
forall a. a -> a
id

evalHelper :: MonadEditor m =>
    (EditorM () -> m ()) -> (YiM () -> m ()) ->
    [EventString -> Maybe ExCommand] -> EventString -> m ()
evalHelper :: (EditorM () -> m ())
-> (YiM () -> m ())
-> [EventString -> Maybe ExCommand]
-> EventString
-> m ()
evalHelper EditorM () -> m ()
pureHandler YiM () -> m ()
impureHandler [EventString -> Maybe ExCommand]
cmds EventString
cmdString =
    case [EventString -> Maybe ExCommand] -> EventString -> Maybe ExCommand
evStringToExCommand [EventString -> Maybe ExCommand]
cmds EventString
cmdString of
        Just ExCommand
cmd -> case ExCommand -> Action
cmdAction ExCommand
cmd of
            BufferA BufferM a
actionB -> EditorM () -> m ()
pureHandler (EditorM () -> m ()) -> EditorM () -> m ()
forall a b. (a -> b) -> a -> b
$ BufferM () -> EditorM ()
forall (m :: * -> *) a. MonadEditor m => BufferM a -> m a
withCurrentBuffer (BufferM a -> BufferM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void BufferM a
actionB)
            EditorA EditorM a
actionE -> EditorM () -> m ()
pureHandler (EditorM a -> EditorM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void EditorM a
actionE)
            YiA YiM a
actionY -> YiM () -> m ()
impureHandler (YiM a -> YiM ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void YiM a
actionY)
        Maybe ExCommand
_ -> () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()