[project @ 2001-03-27 16:55:03 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / InteractiveUI.hs
index 1c5c53d..cd531b2 100644 (file)
@@ -1,5 +1,5 @@
 -----------------------------------------------------------------------------
--- $Id: InteractiveUI.hs,v 1.54 2001/03/12 14:06:46 simonpj Exp $
+-- $Id: InteractiveUI.hs,v 1.58 2001/03/27 16:55:03 simonmar Exp $
 --
 -- GHC Interactive User Interface
 --
@@ -22,6 +22,7 @@ import Linker
 import Util
 import Name            ( Name )
 import Outputable
+import CmdLineOpts     ( DynFlag(..), dopt_unset )
 import Panic           ( GhcException(..) )
 import Config
 
@@ -82,26 +83,28 @@ helpText = "\
 \ Commands available from the prompt:\n\ 
 \\  
 \   <stmt>             evaluate/run <stmt>\n\ 
-\   :add <filename>     add a module to the current set\n\ 
 \   :cd <dir>          change directory to <dir>\n\ 
+\   :def <cmd> <expr>   define a command :<cmd>\n\ 
 \   :help, :?          display this list of commands\n\ 
 \   :load <filename>    load a module (and it dependents)\n\ 
 \   :module <mod>      set the context for expression evaluation to <mod>\n\ 
 \   :reload            reload the current module set\n\ 
 \   :set <option> ...  set options\n\ 
-\   :unset <option> ...        unset options\n\ 
+\   :undef <name>      undefine user-defined command :<name>\n\ 
 \   :type <expr>       show the type of <expr>\n\ 
+\   :unset <option> ...        unset options\n\ 
 \   :quit              exit GHCi\n\ 
 \   :!<command>                run the shell command <command>\n\ 
 \\ 
 \ Options for `:set' and `:unset':\n\ 
 \\ 
+\    +r                        revert top-level expressions after each evaluation\n\ 
 \    +s                 print timing/memory stats after each evaluation\n\ 
 \    +t                        print type after evaluation\n\ 
-\    +r                        revert top-level expressions after each evaluation\n\ 
 \    -<flags>          most GHC command line flags can also be set here\n\ 
 \                         (eg. -v2, -fglasgow-exts, etc.)\n\ 
 \"
+ --ToDo   :add <filename>     add a module to the current set\n\ 
 
 interactiveUI :: CmState -> Maybe FilePath -> [LibrarySpec] -> IO ()
 interactiveUI cmstate mod cmdline_libs = do
@@ -155,10 +158,12 @@ runGHCi = do
   case home of
    Left e  -> return ()
    Right dir -> do
-       dot_ghci <- io (IO.try (openFile (dir ++ "/.ghci") ReadMode))
-       case dot_ghci of
-          Left e -> return ()
-          Right hdl -> fileLoop hdl False
+       cwd <- io (getCurrentDirectory)
+       when (dir /= cwd) $ do
+         dot_ghci <- io (IO.try (openFile (dir ++ "/.ghci") ReadMode))
+         case dot_ghci of
+            Left e -> return ()
+            Right hdl -> fileLoop hdl False
 
   -- read commands from stdin
 #ifndef NO_READLINE
@@ -237,7 +242,6 @@ runCommand c =
    doCommand c
 
 doCommand (':' : command) = specialCommand command
-doCommand ('-':'-':_) = return False   -- comments, useful in scripts
 doCommand stmt
    = do timeIt (do stuff <- runStmt stmt; finishEvalExpr stuff)
         return False
@@ -251,7 +255,8 @@ runStmt stmt
  | otherwise
  = do st <- getGHCiState
       dflags <- io (getDynFlags)
-      (new_cmstate, names) <- io (cmRunStmt (cmstate st) dflags stmt)
+      let dflags' = dopt_unset dflags Opt_WarnUnusedBinds
+      (new_cmstate, names) <- io (cmRunStmt (cmstate st) dflags' stmt)
       setGHCiState st{cmstate = new_cmstate}
       return (Just names)
 
@@ -296,7 +301,7 @@ specialCommand str = do
                                       foldr1 (\a b -> a ++ ',':b) (map fst cs)
                                         ++ ")") >> return False)
 
-noArgs c = io (hPutStrLn stdout ("command `:" ++ c ++ "' takes no arguments"))
+noArgs c = throwDyn (OtherError ("command `" ++ c ++ "' takes no arguments"))
 
 -----------------------------------------------------------------------------
 -- Commands
@@ -318,6 +323,9 @@ setContext str
        setGHCiState st{cmstate=new_cmstate}
 
 changeDirectory :: String -> GHCi ()
+changeDirectory ('~':d) = do
+   tilde <- io (getEnv "HOME") -- will fail if HOME not defined
+   io (setCurrentDirectory (tilde ++ '/':d))
 changeDirectory d = io (setCurrentDirectory d)
 
 defineMacro :: String -> GHCi ()
@@ -342,14 +350,9 @@ defineMacro s = do
   (new_cmstate, maybe_hv) <- io (cmCompileExpr (cmstate st) dflags new_expr)
   setGHCiState st{cmstate = new_cmstate}
   case maybe_hv of
-       Nothing -> return ()
-       Just hv -> 
-         do funs <- io (unsafeCoerce# hv :: IO [HValue])
-            case funs of
-               [fun] -> io (writeIORef commands        
-                               ((macro_name, keepGoing (runMacro fun))
-                                : cmds))
-               _ -> throwDyn (OtherError "defineMacro: bizarre")
+     Nothing -> return ()
+     Just hv -> io (writeIORef commands --
+                   ((macro_name, keepGoing (runMacro hv)) : cmds))
 
 runMacro :: HValue{-String -> IO String-} -> String -> GHCi ()
 runMacro fun s = do