module Yi.Command where
import Control.Applicative
import Control.Exception(SomeException)
import Control.Lens
import Control.Monad
import Control.Monad.Base
import Data.Binary
import Data.Default
import qualified Data.Text as T
import Data.Typeable
import System.Exit (ExitCode(..))
import Yi.Buffer
import Yi.Core (startSubprocess)
import Yi.Types (YiVariable)
import Yi.Editor
import Yi.Keymap
import Yi.MiniBuffer
import qualified Yi.Mode.Compilation as Compilation
import qualified Yi.Mode.Interactive as Interactive
import Yi.Monad
import Yi.Process
import qualified Yi.Rope as R
import Yi.UI.Common
import Yi.Utils
changeBufferNameE :: YiM ()
changeBufferNameE = withMinibufferFree "New buffer name:" strFun
where
strFun :: T.Text -> YiM ()
strFun = withCurrentBuffer . assign identA . MemBuffer
shellCommandE :: YiM ()
shellCommandE = withMinibufferFree "Shell command:" shellCommandV
shellCommandV :: T.Text -> YiM ()
shellCommandV cmd = do
(exitCode,cmdOut,cmdErr) <- liftBase $ runShellCommand (T.unpack cmd)
case exitCode of
ExitSuccess -> if length (filter (== '\n') cmdOut) > 17
then withEditor . void $
newBufferE (MemBuffer "Shell Command Output")
(R.fromString cmdOut)
else printMsg $ case T.pack cmdOut of
"" -> "(Shell command with no output)"
xs -> if T.last xs == '\n' then T.init xs else xs
ExitFailure _ -> printMsg $ T.pack cmdErr
newtype CabalBuffer = CabalBuffer {cabalBuffer :: Maybe BufferRef}
deriving (Default, Typeable, Binary)
instance YiVariable CabalBuffer
cabalConfigureE :: CommandArguments -> YiM ()
cabalConfigureE = cabalRun "configure" configureExit
configureExit :: Either SomeException ExitCode -> YiM ()
configureExit (Right ExitSuccess) = reloadProjectE "."
configureExit _ = return ()
reloadProjectE :: String -> YiM ()
reloadProjectE s = withUI $ \ui -> reloadProject ui s
buildRun :: T.Text -> [T.Text] -> (Either SomeException ExitCode -> YiM x) -> YiM ()
buildRun cmd args onExit = withOtherWindow $ do
b <- startSubprocess (T.unpack cmd) (T.unpack <$> args) onExit
maybeM deleteBuffer =<< cabalBuffer <$> getEditorDyn
putEditorDyn $ CabalBuffer $ Just b
withCurrentBuffer $ setMode Compilation.mode
return ()
makeBuild :: CommandArguments -> YiM ()
makeBuild (CommandArguments args) = buildRun "make" args (const $ return ())
cabalRun :: T.Text -> (Either SomeException 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.spawnProcess sh ["-i"]
searchSources :: String ::: RegexTag -> YiM ()
searchSources = grepFind (Doc "*.hs")
grepFind :: String ::: FilePatternTag -> String ::: RegexTag -> YiM ()
grepFind (Doc filePattern) (Doc searchedRegex) = withOtherWindow $ do
void $ startSubprocess "find" [".",
"-name", "_darcs", "-prune", "-o",
"-name", filePattern, "-exec", "grep", "-Hnie", searchedRegex, "{}", ";"] (const $ return ())
withCurrentBuffer $ setMode Compilation.mode
return ()