And the fact that this only applies to certain commands, and not to output generated by ordinary expressions/statements, means it feels too ad-hoc to me.
..
but note that i'm looking for output capture, within GHCi, rather than redirection of a whole session. one could try,
alternatively, to redirect stdout to a tmp file, then read that
into a variable (:redir >tmpfile | commands | :redir END, in
vim). perhaps that approach would give better coverage,
i'm open to suggestions/opinions.

(actually, :redir =>variable | commands | :redir END)

that route does indeed look easier. i guess i was confused
because there seems to be no haskell function for redirect,
but the neccessary tools are available in GHC.Handle (why
not in the standard libs? i have often search for this..).

in fact, i can almost get by without even extending GHCi,
as far as command output capture is concerned (see below),
so i can focus on the :browse! part of the patches i sent.

great!-) the only problem being that the intermediate bindings
(for h,sto, and var) are still being echoed, and apparently
not to the redirected stdout. is there a way to avoid that
output?

btw, is there a wiki page collecting useful ghci :defs?

claus

----------------------------------------------
import GHC.Handle
import IO
import Data.Char

-- :def redir redir
--
-- :redir var :browse Data.Maybe
-- :redir out :?
--
-- problem: h/sto/var are still printed
redir varcmd =
 let file = "ghci.tmp"
     (var,_:cmd)=span (not . Data.Char.isSpace) varcmd
 in return $ unlines
     ["h <- openFile "++show file++" WriteMode"
     ,"sto <- hDuplicate stdout"
     ,"hDuplicateTo h stdout"
     ,"hClose h"
     ,cmd
     ,"hDuplicateTo sto stdout"
     ,var++" <- readFile "++show file
     ]
----------------------------------------------

$ ghcii.sh redir.hs

*Main> :def redir redir
Loading package haskell98 ... linking ... done.

*Main> :redir a :t id
{handle: ghci.tmp}
{handle: <stdout>}
"id :: a -> a\n"

*Main> a
"id :: a -> a\n"

*Main> :redir out :b Data.Maybe
{handle: ghci.tmp}
{handle: <stdout>}
"maybe :: b -> (a -> b) -> Maybe a -> b\ndata Maybe a = Nothing | Just a\nisJust 
:: Maybe a -> Bool\
nisNothing :: Maybe a -> Bool\nfromJust :: Maybe a -> a\nfromMaybe :: a -> Maybe a 
-> a\nmaybeToList
:: Maybe a -> [a]\nlistToMaybe :: [a] -> Maybe a\ncatMaybes :: [Maybe a] -> 
[a]\nmapMaybe :: (a ->
Maybe b) -> [a] -> [b]\n"

*Main> putStrLn out
maybe :: b -> (a -> b) -> Maybe a -> b
data Maybe a = Nothing | Just a
isJust :: Maybe a -> Bool
isNothing :: Maybe a -> Bool
fromJust :: Maybe a -> a
fromMaybe :: a -> Maybe a -> a
maybeToList :: Maybe a -> [a]
listToMaybe :: [a] -> Maybe a
catMaybes :: [Maybe a] -> [a]
mapMaybe :: (a -> Maybe b) -> [a] -> [b]

_______________________________________________
Cvs-ghc mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to