module Yi.Keymap.Vim.Ex.Commands.Edit (parse) where
import Control.Applicative (Alternative ((<|>)))
import Control.Monad (void, when)
import Data.Maybe (isJust)
import qualified Data.Text as T (Text, append, pack, unpack)
import qualified Data.Attoparsec.Text as P (anyChar, many1, space, string, try, option)
import Yi.Editor (MonadEditor (withEditor), newTabE)
import Yi.File (openNewFile)
import Yi.Keymap (Action (YiA))
import Yi.Keymap.Vim.Common (EventString)
import qualified Yi.Keymap.Vim.Ex.Commands.Common as Common (filenameComplete, impureExCommand, parse)
import Yi.Keymap.Vim.Ex.Types (ExCommand (cmdAction, cmdComplete, cmdShow))
parse :: EventString -> Maybe ExCommand
parse = Common.parse $ do
tab <- P.option Nothing $ Just <$> P.string "tab"
void $ P.try (P.string "edit") <|> P.string "e"
void $ P.many1 P.space
filename <- T.pack <$> P.many1 P.anyChar
return $! edit (isJust tab) filename
edit :: Bool -> T.Text -> ExCommand
edit tab f = Common.impureExCommand {
cmdShow = showEdit tab f
, cmdAction = YiA $ do
when tab $ withEditor newTabE
openNewFile $ T.unpack f
, cmdComplete = (fmap . fmap)
(showEdit tab) (Common.filenameComplete f)
}
showEdit :: Bool -> T.Text -> T.Text
showEdit tab f = (if tab then "tab" else "") `T.append` "edit " `T.append` f