Skip to content

Commit

Permalink
Merge pull request #26 from ethercrow/open_in_tabs
Browse files Browse the repository at this point in the history
Commandline option '-p' for opening multiple files in tabs.
  • Loading branch information
jeffwheeler committed Jun 23, 2012
2 parents 93dc335 + bf30c8c commit 1946e65
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions yi/src/library/Yi/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Prelude ()

import Control.Monad.Error
import Data.Char
import Data.List (intercalate)
import Data.List (intercalate, length)
import Distribution.Text (display)
import System.Console.GetOpt
import System.Exit
Expand Down Expand Up @@ -77,6 +77,7 @@ data Opts = Help
| SelfCheck
| GhcOption String
| Debug
| OpenInTabs

-- | List of editors for which we provide an emulation.
editors :: [(String,Config -> Config)]
Expand All @@ -95,11 +96,18 @@ options =
, Option ['l'] ["line"] (ReqArg LineNo "NUM") "Start on line number"
, Option [] ["as"] (ReqArg EditorNm "EDITOR") editorHelp
, Option [] ["ghc-option"] (ReqArg GhcOption "OPTION") "Specify option to pass to ghc when compiling configuration file"
, Option [openInTabsShort] [openInTabsLong] (NoArg OpenInTabs) "Open files in tabs"
] where frontendHelp = ("Select frontend, which can be one of:\n"
++ intercalate ", " frontendNames)
editorHelp = ("Start with editor keymap, where editor is one of:\n"
++ (intercalate ", " . fmap fst) editors)

openInTabsShort :: Char
openInTabsShort = 'p'

openInTabsLong :: String
openInTabsLong = "open-in-tabs"

-- | usage string.
usage, versinfo :: String
usage = usageInfo ("Usage: yi [option...] [file]") options
Expand All @@ -110,12 +118,15 @@ versinfo = "yi " ++ display version
do_args :: Config -> [String] -> Either Err (Config, ConsoleConfig)
do_args cfg args =
case (getOpt (ReturnInOrder File) options args) of
(o, [], []) -> foldM getConfig (cfg, defaultConsoleConfig) o
(os, [], []) -> foldM (getConfig shouldOpenInTabs) (cfg, defaultConsoleConfig) os
(_, _, errs) -> fail (concat errs)
where
shouldOpenInTabs = ("--" ++ openInTabsLong) `elem` args
|| ('-':[openInTabsShort]) `elem` args

-- | Update the default configuration based on a command-line option.
getConfig :: (Config, ConsoleConfig) -> Opts -> Either Err (Config, ConsoleConfig)
getConfig (cfg,cfgcon) opt =
getConfig :: Bool -> (Config, ConsoleConfig) -> Opts -> Either Err (Config, ConsoleConfig)
getConfig shouldOpenInTabs (cfg, cfgcon) opt =
case opt of
Frontend f -> case lookup f availableFrontends of
Just frontEnd -> return (cfg { startFrontEnd = frontEnd }, cfgcon)
Expand All @@ -126,13 +137,19 @@ getConfig (cfg,cfgcon) opt =
LineNo l -> case startActions cfg of
x : xs -> return (cfg { startActions = x:makeAction (gotoLn (read l)):xs }, cfgcon)
[] -> fail "The `-l' option must come after a file argument"
File filename -> prependAction (editFile filename)

File filename -> if shouldOpenInTabs && (length (startActions cfg) > 0) then
prependActions [YiA (editFile filename), EditorA newTabE]
else
prependAction (editFile filename)

EditorNm emul -> case lookup (fmap toLower emul) editors of
Just modifyCfg -> return $ (modifyCfg cfg, cfgcon)
Nothing -> fail $ "Unknown emulation: " ++ show emul
GhcOption ghcOpt -> return (cfg, cfgcon { ghcOptions = ghcOptions cfgcon ++ [ghcOpt] })
_ -> return (cfg, cfgcon)
where
where
prependActions as = return $ (cfg { startActions = (fmap makeAction as) ++ startActions cfg }, cfgcon)
prependAction a = return $ (cfg { startActions = makeAction a : startActions cfg}, cfgcon)

-- ---------------------------------------------------------------------
Expand Down

0 comments on commit 1946e65

Please sign in to comment.