module Yi.Keymap.Vim2.Search
( doVimSearch
, continueVimSearch
) where
import Prelude ()
import Yi.Prelude
import Data.Maybe (maybe, listToMaybe)
import Yi.Buffer
import Yi.Editor
import Yi.Search
doVimSearch :: Maybe String -> [SearchOption] -> Direction -> EditorM ()
doVimSearch Nothing _ dir = do
mbRegex <- getRegexE
case mbRegex of
Just regex -> withBuffer0 $ continueVimSearch (regex, dir)
Nothing -> printMsg "No previous search pattern"
doVimSearch (Just needle) opts dir =
searchInit needle dir opts >>= withBuffer0 . continueVimSearch
continueVimSearch :: (SearchExp, Direction) -> BufferM ()
continueVimSearch (searchExp, dir) = do
mp <- savingPointB $ do
moveB Character dir
rs <- regexB dir searchExp
moveB Document (reverseDir dir)
ls <- regexB dir searchExp
return $ listToMaybe $ rs ++ ls
maybe (return ()) (moveTo . regionFirst') mp
regionFirst' :: Region -> Point
regionFirst' r = Point $ min a b
where a = fromPoint $ regionStart r
b = fromPoint $ regionEnd r