From 813264ca0d49d271b919f57a3f6d830dc8034ec8 Mon Sep 17 00:00:00 2001 From: Don Stewart Date: Wed, 7 Mar 2007 06:50:07 +0100 Subject: refactoring. less code darcs-hash:20070307055007-9c5c1-2dc9d077773b25f22954c41856e670f3b5583e9f --- W.hs | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'W.hs') diff --git a/W.hs b/W.hs index 5bacfea..deed0e6 100644 --- a/W.hs +++ b/W.hs @@ -20,64 +20,42 @@ import System.IO import Graphics.X11.Xlib import Control.Monad.State --- -- | WState, the window manager state. -- Just the display, width, height and a window list --- data WState = WState { display :: Display , screenWidth :: !Int , screenHeight :: !Int - , windows :: Windows + , windows :: !Windows } type Windows = [Window] -- | The W monad, a StateT transformer over IO encapuslating the window -- manager state --- -newtype W a = W (StateT WState IO a) +newtype W a = W { unW :: StateT WState IO a } deriving (Functor, Monad, MonadIO) -- | Run the W monad, given a chunk of W monad code, and an initial state -- Return the result, and final state --- -runW :: W a -> WState -> IO (a, WState) -runW (W m) = runStateT m +runW :: WState -> W a -> IO (a, WState) +runW st a = runStateT (unW a) st -withIO :: (forall b. (a -> IO b) -> IO b) -> (a -> W c) -> W c -withIO f g = do - s <- W get - (y, t) <- io (f (flip runW s . g)) - W (put t) - return y - --- -- | Lift an IO action into the W monad --- io :: IO a -> W a io = liftIO --- -- | Lift an IO action into the W monad, discarding any result --- io_ :: IO a -> W () io_ f = liftIO f >> return () --- --- | A 'trace' for the W monad --- +-- | A 'trace' for the W monad. Logs a string to stderr. The result may +-- be found in your .xsession-errors file trace :: String -> W () trace msg = io $ do hPutStrLn stderr msg hFlush stderr --- --- | Run an action forever --- -forever :: (Monad m) => m a -> m b -forever a = a >> forever a - -- --------------------------------------------------------------------- -- Getting at the window manager state @@ -104,3 +82,18 @@ setWindows x = W (modify (\s -> s {windows = x})) -- | Modify the current window list modifyWindows :: (Windows -> Windows) -> W () modifyWindows f = W (modify (\s -> s {windows = f (windows s)})) + +-- --------------------------------------------------------------------- +-- Generic utilities + +-- | Run an action forever +forever :: (Monad m) => m a -> m b +forever a = a >> forever a + +-- | Add an element onto the end of a list +snoc :: [a] -> a -> [a] +snoc xs x = xs ++ [x] + +-- | Rotate a list one element +rotate [] = [] +rotate (x:xs) = xs `snoc` x -- cgit v1.2.3