-- Information
-- {{{
--  Last Modified [ 2009-09-22 ]
-- File:    ~/.xmonad/xmonad.hs
-- Author:  kalkin-
-- Purpose  config file for the xmonad window manager
-- Notices: This file is actually kind of a haskell programm. So it's really
--          helpfull if you understand the basics of functional programming.
--
-- Documentation: http://xmonad.org/documentation.html
--
-- For more documentation about the used Extensions (the import stuff) look at
-- http://xmonad.org/xmonad-docs/xmonad-contrib/XMonad-Doc-Extending.html
--
-- Powered by:  A very big pack of PalMal, boredom and THE FORCE!
-- }}}

-- Some imports i need
import Data.Bits ((.|.))
import Data.Ratio
import Graphics.X11
import qualified Data.Map as M
import qualified XMonad.Actions.FlexibleResize as Flex
import qualified XMonad.StackSet as W
import System.IO
import XMonad
import XMonad.Actions.CopyWindow
import XMonad.Actions.NoBorders
import XMonad.Actions.WindowBringer
import XMonad.Hooks.DynamicLog   ( PP(..), dynamicLogWithPP, dzenColor, wrap, defaultPP )
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.SetWMName
import XMonad.Layout.Grid
import XMonad.Layout.NoBorders
import XMonad.Layout.ResizableTile
import XMonad.ManageHook
import XMonad.Operations
import XMonad.Util.Run

-- My statusbar
main :: IO()
statusBarCmd = "dzen2 -ta lr "


-- Merge standart keybindings with new ones
newKeys x  = M.union (keys defaultConfig x) (M.fromList (myKeys x))

-- my keubindings
myKeys x =  [ ((modMask x,               xK_a     ), sendMessage MirrorShrink)
            , ((modMask x,               xK_z     ), sendMessage MirrorExpand) 
            
            -- toggle borders on current focused window
            , ((modMask x  .|. shiftMask,  xK_g ),   withFocused toggleBorder)

            -- shows menu with all windows on workspaces and lets you go
            -- the the workspace where the choosen window is
            , ((modMask x,  xK_g ), gotoMenu)

            -- the same like above but instead going to a workpsace it brings
            -- the window to the current workspace
            , ((modMask x,  xK_b ), bringMenu)
            ]
            ++
             -- mod-[1..9] @@ Switch to workspace N
             -- mod-shift-[1..9] @@ Move client to workspace N
             -- mod-control-shift-[1..9] @@ Copy client to workspace NV
            [ ((m .|. modMask x, k), windows $ f i) | (i, k) <- zip (workspaces x) [xK_1 ..]
            , (f, m) <- [(W.view, 0), (W.shift, shiftMask), (copy, controlMask)]
            ]
            ++
            [((modMask x, xK_c     ), kill1) ]
            ++
            [((modMask x, xK_n     ), refresh) ] 

-- Mouse Bindings
myMouse  x = [ ((modMask x, button3), (\w -> focus w >> Flex.mouseResizeWindow w))]
newMouse x = M.union (mouseBindings defaultConfig x) (M.fromList (myMouse x))

-- My Layouts
myLayouts = avoidStruts(Grid ||| tiled ||| Mirror tiled ||| Full)
    where
     -- default tiling algorithm partitions the screen into two panes
     tiled   = ResizableTall 1 delta ratio []
 
     -- The default number of windows in the master pane
     nmaster = 1
 
     -- Default proportion of screen occupied by master pane
     ratio   = 1/2
 
     -- Percent of screen to increment by when resizing panes
     delta   = 3/100

main = do din <- spawnPipe statusBarCmd 
          xmonad $ defaultConfig
                  { 
                     keys                  = newKeys
                    , layoutHook            = myLayouts
                    , logHook               = dynamicLogWithPP $ robPP din
                    -- good for java apps like thinking rock and ect...
                    ,startupHook        = setWMName "LG3D"

                    -- use the windows/apple key for command key
                    , modMask               = mod4Mask
                    , mouseBindings         = newMouse
                    , terminal              = "uxterm -fn -*-terminus-medium-*-*-*-14-*-*-*-*-*-iso10646-* -e /usr/bin/screen -xRR everday"
                  }

-- some magic which does my the logging in the dzen bar.
robPP h = defaultPP 
                 { ppCurrent = wrap "^fg(#000000)^bg(#a6c292)^p(2)^i(/home/kalkin/dzen_bitmaps/has_win.xbm)" "^p(2)^fg()^bg()"
                  , ppLayout  = dzenColor "#80AA83" "" .
                                (\x -> case x of
                                         "ResizableTall" -> "^i(/home/kalkin/dzen_bitmaps/tall.xbm)"
                                         "Mirror ResizableTall" -> "^i(/home/kalkin/dzen_bitmaps/mtall.xbm)"
                                         "Full" -> "^i(/home/kalkin/dzen_bitmaps/full.xbm)"
                                         "Grid" -> "^i(/home/kalkin/dzen_bitmaps/grid.xbm)"

                                )
                  , ppTitle   = wrap "< " " >" 
                  , ppOutput   = hPutStrLn h
                  }

