module Yi.Command where
import Data.Binary
import System.Exit
( ExitCode( ExitSuccess,ExitFailure ) )
import Control.OldException
import Control.Monad.Trans (MonadIO (..))
import Prelude ()
import Yi.Prelude (discard)
import Yi.Core
import Yi.MiniBuffer
import qualified Yi.Mode.Compilation as Compilation
import Yi.Process
import Yi.Templates
( addTemplate
, templateNames
)
import Yi.UI.Common
import qualified Yi.Mode.Interactive as Interactive
import qualified Data.Rope as R
changeBufferNameE :: YiM ()
changeBufferNameE =
withMinibufferFree "New buffer name:" strFun
where
strFun :: String -> YiM ()
strFun = withBuffer . putA identA . Left
shellCommandE :: YiM ()
shellCommandE = do
withMinibufferFree "Shell command:" shellCommandV
shellCommandV :: String -> YiM ()
shellCommandV cmd = do
(cmdOut,cmdErr,exitCode) <- liftIO $ runShellCommand cmd
case exitCode of
ExitSuccess -> withEditor $ newBufferE (Left "Shell Command Output") (R.fromString cmdOut) >> return ()
ExitFailure _ -> msgEditor cmdErr
newtype CabalBuffer = CabalBuffer {cabalBuffer :: Maybe BufferRef}
deriving (Initializable, Typeable, Binary)
cabalConfigureE :: CommandArguments -> YiM ()
cabalConfigureE = cabalRun "configure" configureExit
configureExit :: Either Exception ExitCode -> YiM ()
configureExit (Right ExitSuccess) = reloadProjectE "."
configureExit _ = return ()
reloadProjectE :: String -> YiM ()
reloadProjectE s = withUI $ \ui -> reloadProject ui s
buildRun :: String -> [String] -> (Either Exception ExitCode -> YiM x) -> YiM ()
buildRun cmd args onExit = withOtherWindow $ do
b <- startSubprocess cmd args onExit
withEditor $ do
maybeM deleteBuffer =<< cabalBuffer <$> getDynamic
setDynamic $ CabalBuffer $ Just b
withBuffer0 $ setMode Compilation.mode
return ()
makeBuild :: CommandArguments -> YiM ()
makeBuild (CommandArguments args) = buildRun "make" args (const $ return ())
cabalRun :: String -> (Either Exception ExitCode -> YiM x) -> CommandArguments -> YiM ()
cabalRun cmd onExit (CommandArguments args) = buildRun "cabal" (cmd:args) onExit
cabalBuildE :: CommandArguments -> YiM ()
cabalBuildE = cabalRun "build" (const $ return ())
shell :: YiM BufferRef
shell = do
sh <- io shellFileName
Interactive.interactive sh ["-i"]
searchSources :: String ::: RegexTag -> YiM ()
searchSources = grepFind (Doc "*.hs")
grepFind :: String ::: FilePatternTag -> String ::: RegexTag -> YiM ()
grepFind (Doc filePattern) (Doc searchedRegex) = withOtherWindow $ do
discard $ startSubprocess "find" [".",
"-name", "_darcs", "-prune", "-o",
"-name", filePattern, "-exec", "grep", "-Hnie", searchedRegex, "{}", ";"] (const $ return ())
withBuffer $ setMode Compilation.mode
return ()
insertTemplate :: YiM ()
insertTemplate = withMinibuffer "template-name:" (const $ return templateNames) $ withEditor . addTemplate