Commit b8a331a4 authored by phercek's avatar phercek
Browse files

alow macros to redefine builtin GHCi commands (implements #3084)

parent f314da8e
...@@ -127,7 +127,6 @@ builtin_commands = [ ...@@ -127,7 +127,6 @@ builtin_commands = [
("def", keepGoing (defineMacro False), completeExpression), ("def", keepGoing (defineMacro False), completeExpression),
("def!", keepGoing (defineMacro True), completeExpression), ("def!", keepGoing (defineMacro True), completeExpression),
("delete", keepGoing deleteCmd, noCompletion), ("delete", keepGoing deleteCmd, noCompletion),
("e", keepGoing editFile, completeFilename),
("edit", keepGoing editFile, completeFilename), ("edit", keepGoing editFile, completeFilename),
("etags", keepGoing createETagsFileCmd, completeFilename), ("etags", keepGoing createETagsFileCmd, completeFilename),
("force", keepGoing forceCmd, completeExpression), ("force", keepGoing forceCmd, completeExpression),
...@@ -753,9 +752,12 @@ lookupCommand str = do ...@@ -753,9 +752,12 @@ lookupCommand str = do
Nothing -> BadCommand Nothing -> BadCommand
lookupCommand' :: String -> IO (Maybe Command) lookupCommand' :: String -> IO (Maybe Command)
lookupCommand' str = do lookupCommand' ":" = return Nothing
lookupCommand' str' = do
macros <- readIORef macros_ref macros <- readIORef macros_ref
let cmds = builtin_commands ++ macros let{ (str, cmds) = case str' of
':' : rest -> (rest, builtin_commands)
_ -> (str', macros ++ builtin_commands) }
-- look for exact match first, then the first prefix match -- look for exact match first, then the first prefix match
return $ case [ c | c <- cmds, str == cmdName c ] of return $ case [ c | c <- cmds, str == cmdName c ] of
c:_ -> Just c c:_ -> Just c
...@@ -934,6 +936,8 @@ chooseEditFile = ...@@ -934,6 +936,8 @@ chooseEditFile =
fromTarget _ = Nothing -- when would we get a module target? fromTarget _ = Nothing -- when would we get a module target?
defineMacro :: Bool{-overwrite-} -> String -> GHCi () defineMacro :: Bool{-overwrite-} -> String -> GHCi ()
defineMacro _ (':':_) =
io $ putStrLn "macro name cannot start with a colon"
defineMacro overwrite s = do defineMacro overwrite s = do
let (macro_name, definition) = break isSpace s let (macro_name, definition) = break isSpace s
macros <- io (readIORef macros_ref) macros <- io (readIORef macros_ref)
...@@ -1627,9 +1631,13 @@ ghciCompleteWord line@(left,_) = case firstWord of ...@@ -1627,9 +1631,13 @@ ghciCompleteWord line@(left,_) = case firstWord of
Nothing -> return completeFilename Nothing -> return completeFilename
completeCmd = wrapCompleter " " $ \w -> do completeCmd = wrapCompleter " " $ \w -> do
cmds <- liftIO $ readIORef macros_ref macros <- liftIO $ readIORef macros_ref
return (filter (w `isPrefixOf`) (map (':':) let macro_names = map (':':) . map cmdName $ macros
(map cmdName (builtin_commands ++ cmds)))) let command_names = map (':':) . map cmdName $ builtin_commands
let{ candidates = case w of
':' : ':' : _ -> map (':':) command_names
_ -> nub $ macro_names ++ command_names }
return $ filter (w `isPrefixOf`) candidates
completeMacro = wrapIdentCompleter $ \w -> do completeMacro = wrapIdentCompleter $ \w -> do
cmds <- liftIO $ readIORef macros_ref cmds <- liftIO $ readIORef macros_ref
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment