#else
import GHC.ConsoleHandler ( flushConsole )
import qualified System.Win32
-import System.FilePath
#endif
#ifdef USE_READLINE
import Control.Exception as Exception
-- import Control.Concurrent
+import System.FilePath
import qualified Data.ByteString.Char8 as BS
import Data.List
import Data.Maybe
is_tty <- hIsTerminalDevice stdin
when is_tty $ do
Readline.initialize
+
+ withGhcAppData
+ (\dir -> Readline.readHistory (dir </> "ghci_history"))
+ (return True)
+
Readline.setAttemptedCompletionFunction (Just completeWord)
--Readline.parseAndBind "set show-all-if-ambiguous 1"
}
#ifdef USE_READLINE
+ Readline.stifleHistory 100
+ withGhcAppData (\dir -> Readline.writeHistory (dir </> "ghci_history"))
+ (return True)
Readline.resetTerminal Nothing
#endif
return ()
+withGhcAppData :: (FilePath -> IO a) -> IO a -> IO a
+withGhcAppData right left = do
+ either_dir <- IO.try (getAppUserDataDirectory "ghc")
+ case either_dir of
+ Right dir -> right dir
+ _ -> left
+
+
runGHCi :: [(FilePath, Maybe Phase)] -> Maybe [String] -> GHCi ()
runGHCi paths maybe_exprs = do
- let read_dot_files = not opt_IgnoreDotGhci
+ let
+ read_dot_files = not opt_IgnoreDotGhci
- when (read_dot_files) $ do
- -- Read in ./.ghci.
- let file = "./.ghci"
- exists <- io (doesFileExist file)
- when exists $ do
- dir_ok <- io (checkPerms ".")
- file_ok <- io (checkPerms file)
+ current_dir = return (Just ".ghci")
+
+ app_user_dir = io $ withGhcAppData
+ (\dir -> return (Just (dir </> "ghci.conf")))
+ (return Nothing)
+
+ home_dir = do
+ either_dir <- io $ IO.try (getEnv "HOME")
+ case either_dir of
+ Right home -> return (Just (home </> ".ghci"))
+ _ -> return Nothing
+
+ sourceConfigFile :: FilePath -> GHCi ()
+ sourceConfigFile file = do
+ exists <- io $ doesFileExist file
+ when exists $ do
+ dir_ok <- io $ checkPerms (getDirectory file)
+ file_ok <- io $ checkPerms file
when (dir_ok && file_ok) $ do
- either_hdl <- io (IO.try (openFile "./.ghci" ReadMode))
- case either_hdl of
- Left _e -> return ()
- Right hdl -> runCommands (fileLoop hdl False False)
+ either_hdl <- io $ IO.try (openFile file ReadMode)
+ case either_hdl of
+ Left _e -> return ()
+ Right hdl -> runCommands (fileLoop hdl False False)
+ where
+ getDirectory f = case takeDirectory f of "" -> "."; d -> d
when (read_dot_files) $ do
- -- Read in $HOME/.ghci
- either_dir <- io (IO.try getHomeDirectory)
- case either_dir of
- Left _e -> return ()
- Right dir -> do
- cwd <- io (getCurrentDirectory)
- when (dir /= cwd) $ do
- let file = dir ++ "/.ghci"
- ok <- io (checkPerms file)
- when ok $ do
- either_hdl <- io (IO.try (openFile file ReadMode))
- case either_hdl of
- Left _e -> return ()
- Right hdl -> runCommands (fileLoop hdl False False)
+ cfgs0 <- sequence [ current_dir, app_user_dir, home_dir ]
+ cfgs <- io $ mapM canonicalizePath (catMaybes cfgs0)
+ mapM_ sourceConfigFile (nub cfgs)
+ -- nub, because we don't want to read .ghci twice if the
+ -- CWD is $HOME.
-- Perform a :load for files given on the GHCi command line
-- When in -e mode, if the load fails then we want to stop
splatSavedSession
case l of
Nothing -> return Nothing
+ Just "" -> return (Just "") -- Don't put empty lines in the history
Just l -> do
io (addHistory l)
str <- io $ consoleInputToUnicode True l
breakSwitch _session [] = do
io $ putStrLn "The break command requires at least one argument."
breakSwitch session (arg1:rest)
- | looksLikeModuleName arg1 = do
+ | looksLikeModuleName arg1 && not (null rest) = do
mod <- wantInterpretedModule arg1
breakByModule mod rest
| all isDigit arg1 = do