module Yi.Keymap.Vim.Ex.Commands.Substitute (parse) where
import Control.Applicative (Alternative ((<|>)), (<$>))
import Control.Monad (void)
import Data.Monoid ((<>))
import qualified Data.Text as T (cons, snoc)
import qualified Text.ParserCombinators.Parsec as P (char, many, noneOf, oneOf, string, try)
import Yi.Buffer.Adjusted
import Yi.Editor (EditorM, closeBufferAndWindowE, printMsg, withCurrentBuffer)
import Yi.Keymap (Action (EditorA), Keymap)
import Yi.Keymap.Keys (char, choice, (?>>!))
import Yi.Keymap.Vim.Common (EventString)
import qualified Yi.Keymap.Vim.Ex.Commands.Common as Common (parse, pureExCommand)
import Yi.Keymap.Vim.Ex.Types (ExCommand (cmdAction, cmdShow))
import Yi.MiniBuffer (spawnMinibufferE)
import Yi.Regex (makeSearchOptsM)
import qualified Yi.Rope as R (YiString, fromString, length, null, toText, toString)
import Yi.Search
parse :: EventString -> Maybe ExCommand
parse = Common.parse $ do
percents <- P.many (P.char '%')
void $ P.try (P.string "substitute") <|> P.string "s"
delimiter <- P.oneOf "!@#$%^&*()[]{}<>/.,~';:?-="
from <- R.fromString <$> P.many (P.noneOf [delimiter])
void $ P.char delimiter
to <- R.fromString <$> P.many (P.noneOf [delimiter])
void $ P.char delimiter
flagChars <- P.many (P.oneOf "gic")
return $! substitute from to delimiter
('g' `elem` flagChars)
('i' `elem` flagChars)
('c' `elem` flagChars)
(not $ null percents)
substitute :: R.YiString -> R.YiString -> Char -> Bool -> Bool -> Bool -> Bool -> ExCommand
substitute from to delimiter global caseInsensitive confirm allLines = Common.pureExCommand {
cmdShow = (if allLines then "%" else "")
<> "substitute"
<> (delimiter `T.cons` R.toText from)
<> (delimiter `T.cons` R.toText to)
`T.snoc` delimiter
<> (if confirm then "c" else "")
<> (if caseInsensitive then "i" else "")
<> (if global then "g" else "")
, cmdAction = EditorA $ do
let opts = QuoteRegex : if caseInsensitive then [IgnoreCase] else []
regex <- if R.null from
then getRegexE
else return . (either (const Nothing) Just)
. makeSearchOptsM opts . R.toString $ from
case regex of
Nothing -> printMsg "No previous search pattern"
Just regex' -> if confirm
then substituteConfirm regex' to global allLines
else withCurrentBuffer $ do
let replace = void $ regionOfB Line
>>= searchAndRepRegion0 regex' to global
if allLines
then withEveryLineB replace
else replace
moveToSol
}
substituteConfirm :: SearchExp -> R.YiString -> Bool -> Bool -> EditorM ()
substituteConfirm regex to global allLines = do
setRegexE regex
regions <- withCurrentBuffer $ findMatches regex global allLines
substituteMatch to 0 False regions
findMatches :: SearchExp -> Bool -> Bool -> BufferM [Region]
findMatches regex global allLines = do
lns <- if allLines
then do lineCount <- lineCountB
lineRegions [1..lineCount]
else return <$> regionOfB Line
let f = if global then id else take 1
concat <$> mapM (fmap f . regexRegionB regex) lns
lineRegions :: [Int] -> BufferM [Region]
lineRegions = mapM $ \ln -> gotoLn ln >> regionOfB Line
offsetRegion :: Int -> Region -> Region
offsetRegion k reg = mkRegion (regionStart reg + k') (regionEnd reg + k')
where k' = fromIntegral k
substituteMatch :: R.YiString -> Int -> Bool -> [Region] -> EditorM ()
substituteMatch _ _ _ [] = resetRegexE
substituteMatch to co autoAll (m:ms) = do
let m' = offsetRegion co m
withCurrentBuffer . moveTo $ regionStart m'
len <- withCurrentBuffer $ R.length <$> readRegionB m'
let diff = R.length to len
tex = "replace with " <> R.toText to <> " (y/n/a/q)?"
if autoAll
then do withCurrentBuffer $ replaceRegionB m' to
substituteMatch to (co + diff) True ms
else void . spawnMinibufferE tex . const $ askKeymap to co (co + diff) m ms
askKeymap :: R.YiString -> Int -> Int -> Region -> [Region] -> Keymap
askKeymap to co co' m ms = choice [
char 'n' ?>>! cleanUp >> substituteMatch to co False ms
, char 'a' ?>>! do cleanUp
replace
substituteMatch to co' True ms
, char 'y' ?>>! do cleanUp
replace
substituteMatch to co' False ms
, char 'q' ?>>! cleanUp >> resetRegexE
]
where cleanUp = closeBufferAndWindowE
replace = withCurrentBuffer $ replaceRegionB (offsetRegion co m) to