module Rasa.Internal.Directive
(
bufDo
, focusDo
, exit
, addBuffer
, addBufferThen
, nextBuf
, prevBuf
, overRange
, replaceRange
, deleteRange
, insertAt
, sizeOf
) where
import Rasa.Internal.Text
import Rasa.Internal.Editor
import Rasa.Internal.Action
import Rasa.Internal.Range
import Rasa.Internal.Buffer as B
import Control.Lens
import qualified Data.Text as T
focusDo :: BufAction a -> Action a
focusDo = Action . zoom focusedBuf . getBufAction
bufDo :: Monoid a => BufAction a -> Action a
bufDo = Action . zoom (buffers . traverse) . getBufAction
addBuffer :: T.Text -> Action ()
addBuffer txt = buffers <>= [newBuffer txt]
addBufferThen :: T.Text -> BufAction a -> Action a
addBufferThen txt act = do
buffers <>= [newBuffer txt]
fmap (!! 0) . Action . zoom (buffers._last) . fmap (:[]) . getBufAction $ act
exit :: Action ()
exit = exiting .= True
nextBuf :: Action ()
nextBuf = do
numBuffers <- use (buffers.to length)
focused %= (`mod` numBuffers) . (+1)
prevBuf :: Action ()
prevBuf = do
numBuffers <- use (buffers.to length)
focused %= (`mod` numBuffers) . subtract 1
deleteRange :: Range -> BufAction ()
deleteRange r = range r.asText .= ""
replaceRange :: Range -> T.Text -> BufAction ()
replaceRange r txt = range r.asText .= txt
insertAt :: Coord -> T.Text -> BufAction ()
insertAt c txt = range (Range c c).asText .= txt
overRange :: Range -> (T.Text -> T.Text) -> BufAction ()
overRange r f = range r.asText %= f