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

-- |
-- Module      :  Yi.Mode.IReader
-- License     :  GPL-2
-- Maintainer  :  yi-devel@googlegroups.com
-- Stability   :  experimental
-- Portability :  portable
--
-- A simple text mode; it does very little besides define a comment
-- syntax. We have it as a separate mode so users can bind the
-- commands to this mode specifically.

module Yi.Mode.IReader where

import Lens.Micro.Platform   ((%~))
import Data.Char      (intToDigit)
import Data.Text      ()
import Yi.Buffer.Misc
import Yi.Editor      (printMsg, withCurrentBuffer)
import Yi.IReader
import Yi.Keymap      (YiM, topKeymapA)
import Yi.Keymap.Keys (choice, important, metaCh, (?>>!))
import Yi.Mode.Common (anyExtension, fundamentalMode)

abstract :: Mode syntax
abstract :: Mode syntax
abstract = Mode syntax
forall syntax. Mode syntax
fundamentalMode { modeApplies :: FilePath -> YiString -> Bool
modeApplies = [FilePath] -> FilePath -> YiString -> Bool
forall a. [FilePath] -> FilePath -> a -> Bool
anyExtension [FilePath
"irtxt"]
                           , modeKeymap :: KeymapSet -> KeymapSet
modeKeymap = (Keymap -> Identity Keymap) -> KeymapSet -> Identity KeymapSet
Lens' KeymapSet Keymap
topKeymapA ((Keymap -> Identity Keymap) -> KeymapSet -> Identity KeymapSet)
-> (Keymap -> Keymap) -> KeymapSet -> KeymapSet
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Keymap -> Keymap
ikeys }
  where
    ikeys :: Keymap -> Keymap
ikeys = Keymap -> Keymap -> Keymap
forall (f :: * -> *) w e a.
MonadInteract f w e =>
f a -> f a -> f a
important (Keymap -> Keymap -> Keymap) -> Keymap -> Keymap -> Keymap
forall a b. (a -> b) -> a -> b
$ [Keymap] -> Keymap
forall (m :: * -> *) w e a.
(MonadInteract m w e, MonadFail m) =>
[m a] -> m a
choice [Keymap]
m
    m :: [Keymap]
m = [ Char -> Event
metaCh Char
'`' Event -> YiM () -> Keymap
forall (m :: * -> *) a x.
(MonadInteract m Action Event, YiAction a x, Show x) =>
Event -> a -> m ()
?>>! YiM ()
saveAsNewArticle
        , Char -> Event
metaCh Char
'0' Event -> YiM () -> Keymap
forall (m :: * -> *) a x.
(MonadInteract m Action Event, YiAction a x, Show x) =>
Event -> a -> m ()
?>>! YiM ()
deleteAndNextArticle
        ]
        [Keymap] -> [Keymap] -> [Keymap]
forall a. [a] -> [a] -> [a]
++ (Int -> Keymap) -> [Int] -> [Keymap]
forall a b. (a -> b) -> [a] -> [b]
map (\Int
x -> Char -> Event
metaCh (Int -> Char
intToDigit Int
x) Event -> YiM () -> Keymap
forall (m :: * -> *) a x.
(MonadInteract m Action Event, YiAction a x, Show x) =>
Event -> a -> m ()
?>>! Int -> YiM ()
saveAndNextArticle Int
x) [Int
1..Int
9]

ireaderMode :: Mode syntax
ireaderMode :: Mode syntax
ireaderMode = Mode syntax
forall syntax. Mode syntax
abstract { modeName :: Text
modeName = Text
"interactive reading of text" }

ireadMode ::  YiM ()
ireadMode :: YiM ()
ireadMode = do
  BufferM () -> YiM ()
forall (m :: * -> *) a. MonadEditor m => BufferM a -> m a
withCurrentBuffer (BufferM () -> YiM ()) -> BufferM () -> YiM ()
forall a b. (a -> b) -> a -> b
$ AnyMode -> BufferM ()
setAnyMode (AnyMode -> BufferM ()) -> AnyMode -> BufferM ()
forall a b. (a -> b) -> a -> b
$ Mode Any -> AnyMode
forall syntax. Mode syntax -> AnyMode
AnyMode Mode Any
forall syntax. Mode syntax
ireaderMode
  YiM ()
nextArticle
  Text -> YiM ()
forall (m :: * -> *). MonadEditor m => Text -> m ()
printMsg Text
"M-` new; M-0 delete; M-[1-9]: save w/higher priority"