handle :: ([WindowSpace] -> [WindowSpace]) -> Event -> X ()
handle f (ClientMessageEvent {
               ev_window = w,
               ev_message_type = mt,
               ev_data = d
        }) = withWindowSet $ \s -> do
        sort' <- getSortByIndex
        let ws = f $ sort' $ W.workspaces s

        a_cd <- getAtom "XMONAD_CURRENT_DESKTOP"
        a_d <- getAtom "XMONAD_WM_DESKTOP"
        a_aw <- getAtom "XMONAD_ACTIVE_WINDOW"
        a_cw <- getAtom "XMONAD_CLOSE_WINDOW"
        a_ignore <- mapM getAtom ["XMONAD_TIMER"]
        if mt == a_cd then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.view (W.tag (ws !! fi n))
            else
              trace $ "Bad XMONAD_CURRENT_DESKTOP with data[0]="++show n
        else if mt == a_d then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.shiftWin (W.tag (ws !! fi n)) w
            else
              trace $ "Bad XMONAD_DESKTOP with data[0]="++show n
        else if mt == a_aw then do
            windows $ W.focusWindow w
        else if mt == a_cw then do
            killWindow w
        else if mt `elem` a_ignore then do
            return ()
        else do
           return ()
handle :: ([WindowSpace] -> [WindowSpace]) -> Event -> X ()
handle f (ClientMessageEvent {
               ev_window = w,
               ev_message_type = mt,
               ev_data = d
        }) = withWindowSet $ \s -> do
        sort' <- getSortByIndex
        let ws = f $ sort' $ W.workspaces s

        a_cd <- getAtom "XMONAD_CURRENT_DESKTOP"
        a_d <- getAtom "XMONAD_WM_DESKTOP"
        a_aw <- getAtom "XMONAD_ACTIVE_WINDOW"
        a_cw <- getAtom "XMONAD_CLOSE_WINDOW"
        a_ignore <- mapM getAtom ["XMONAD_TIMER"]
        if mt == a_cd then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.view (W.tag (ws !! fi n))
            else
              trace $ "Bad XMONAD_CURRENT_DESKTOP with data[0]="++show n
        else if mt == a_d then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.shiftWin (W.tag (ws !! fi n)) w
            else
              trace $ "Bad XMONAD_DESKTOP with data[0]="++show n
        else if mt == a_aw then do
            windows $ W.focusWindow w
        else if mt == a_cw then do
            killWindow w
        else if mt `elem` a_ignore then do
            return ()
        else do
           return ()
handle f (ClientMessageEvent {
               ev_window = w,
               ev_message_type = mt,
               ev_data = d
        }) = withWindowSet $ \s -> do
        sort' <- getSortByIndex
        let ws = f $ sort' $ W.workspaces s

        a_cd <- getAtom "XMONAD_CURRENT_DESKTOP"
        a_d <- getAtom "XMONAD_WM_DESKTOP"
        a_aw <- getAtom "XMONAD_ACTIVE_WINDOW"
        a_cw <- getAtom "XMONAD_CLOSE_WINDOW"
        a_ignore <- mapM getAtom ["XMONAD_TIMER"]
        if mt == a_cd then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.view (W.tag (ws !! fi n))
            else
              trace $ "Bad XMONAD_CURRENT_DESKTOP with data[0]="++show n
        else if mt == a_d then do
            let n = head d
            if 0 <= n && fi n < length ws then
              windows $ W.shiftWin (W.tag (ws !! fi n)) w
            else
              trace $ "Bad XMONAD_DESKTOP with data[0]="++show n
        else if mt == a_aw then do
            windows $ W.focusWindow w
        else if mt == a_cw then do
            killWindow w
        else if mt `elem` a_ignore then do
            return ()
        else do
           return ()