module Yi.History where
import Yi.Buffer
import Data.List
import Data.Accessor.Container
import Yi.Dynamic
import Yi.Editor
import qualified Data.Map as M
import Yi.Prelude
import Prelude ()
type Histories = M.Map String History
instance (Typeable k, Typeable v) => Initializable (M.Map k v) where
initial = M.empty
data History = History {_historyCurrent :: Int,
_historyContents :: [String],
_historyPrefix :: String}
deriving (Show, Typeable)
instance Initializable History where
initial = (History (1) [] "")
dynKeyA :: (Initializable v, Ord k) => k -> Accessor (M.Map k v) v
dynKeyA = mapDefault initial
miniBuffer :: String
miniBuffer = "minibuffer"
historyUp :: EditorM ()
historyUp = historyMove miniBuffer 1
historyDown :: EditorM ()
historyDown = historyMove miniBuffer (1)
historyStart :: EditorM ()
historyStart = historyStartGen miniBuffer
historyStartGen :: String -> EditorM ()
historyStartGen ident = do
(History _cur cont pref) <- getA (dynKeyA ident . dynA)
putA (dynKeyA ident . dynA) (History 0 (nub ("":cont)) pref)
debugHist
historyFinish :: EditorM ()
historyFinish = historyFinishGen miniBuffer (withBuffer0 elemsB)
historyFinishGen :: String -> EditorM String -> EditorM ()
historyFinishGen ident getCurValue = do
(History _cur cont pref) <- getA (dynKeyA ident . dynA)
curValue <- getCurValue
let cont' = dropWhile (curValue==) . dropWhile null $ cont
length curValue `seq`
cont' `seq`
putA (dynKeyA ident . dynA) $ History (1) (curValue:cont') pref
debugHist :: EditorM ()
debugHist = return ()
historyFind :: [String] -> Int -> Int -> Int -> String -> Int
historyFind cont len cur delta pref =
case (next < 0, next >= len) of
(True,_) -> next
(_,True) -> next
(_,_) -> if isPrefixOf pref (cont !! next)
then next
else historyFind cont len cur deltaLarger pref
where
next = cur + delta
deltaLarger = delta + signum delta
historyMove :: String -> Int -> EditorM ()
historyMove ident delta = (withBuffer0 . replaceBufferContent) =<< historyMoveGen ident delta (withBuffer0 elemsB)
historyMoveGen :: String -> Int -> EditorM String -> EditorM String
historyMoveGen ident delta getCurValue = do
(History cur cont pref) <- getA (dynKeyA ident . dynA)
curValue <- getCurValue
let len = length cont
next = historyFind cont len cur delta pref
nextValue = cont !! next
case (next < 0, next >= len) of
(True, _) -> do printMsg $ "end of " ++ ident ++ " history, no next item."
return curValue
(_, True) -> do printMsg $ "beginning of " ++ ident ++ " history, no previous item."
return curValue
(_,_) -> do
putA (dynKeyA ident . dynA) (History next (take cur cont ++ [curValue] ++ drop (cur+1) cont) pref)
debugHist
return nextValue
historyPrefixSet :: String -> EditorM ()
historyPrefixSet pref = historyPrefixSet' miniBuffer pref
historyPrefixSet' :: String -> String -> EditorM ()
historyPrefixSet' ident pref = do
(History cur cont _pref) <- getA (dynKeyA ident . dynA)
putA (dynKeyA ident . dynA) (History cur cont pref)
return ()