1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#
# .xmonad/xmonad.hs
#
# Requires xmonad, xmonad-contrib and xmobar with xft
#
# trayer, urxvt, libnotify-bin, googlizer
#

import XMonad hiding ( (|||) )
import System.Exit
import System.IO
import Data.Ratio ((%))
import Data.List

import qualified XMonad.StackSet as W
import qualified Data.Map        as M

import XMonad.Actions.CycleWS
import XMonad.Actions.Search
import XMonad.Actions.WindowGo
import XMonad.Actions.Promote
import qualified XMonad.Actions.Submap as SM
import qualified XMonad.Actions.ConstrainedResize as SQR
import XMonad.Actions.CopyWindow
import XMonad.Actions.DynamicWorkspaces
import XMonad.Actions.RotSlaves
import XMonad.Actions.UpdatePointer
import XMonad.Actions.SinkAll
import qualified XMonad.Actions.FlexibleResize as FlexR
import XMonad.Actions.FloatKeys

import XMonad.Layout hiding ( (|||) )
import XMonad.Layout.LayoutModifier
import XMonad.Layout.LayoutCombinators
import XMonad.Layout.ResizableTile
import XMonad.Layout.LayoutHints
import XMonad.Layout.ToggleLayouts
import XMonad.Layout.NoBorders
import XMonad.Layout.TwoPane
import XMonad.Layout.Tabbed
import XMonad.Layout.IM
import XMonad.Layout.Grid
import XMonad.Layout.PerWorkspace
import XMonad.Layout.WindowNavigation
import XMonad.Layout.Named
import XMonad.Layout.DwmStyle
import qualified XMonad.Layout.Magnifier as Mag

import XMonad.Hooks.DynamicLog
import XMonad.Hooks.ManageDocks
import XMonad.Hooks.ManageHelpers
import XMonad.Hooks.EwmhDesktops
import XMonad.Hooks.UrgencyHook

import XMonad.Prompt
import XMonad.Prompt.Input
import XMonad.Prompt.RunOrRaise
import XMonad.Prompt.Shell
import XMonad.Prompt.Window
import XMonad.Prompt.AppLauncher as AL
import XMonad.Prompt.Layout

import XMonad.Util.Run
import XMonad.Util.EZConfig
import XMonad.Util.Scratchpad
import XMonad.Util.Font
import XMonad.Util.XSelection
import XMonad.Util.WorkspaceCompare
import XMonad.Util.WindowProperties
import XMonad.Util.NamedWindows (getName)

myTerminal      = "urxvt"
myBorderWidth   = 1
myModMask       = mod4Mask
myNumlockMask   = mod2Mask

myNormalBorderColor  = "snow4"
myFocusedBorderColor = "#61ce3c"

-- shell prompt theme
mySP = defaultXPConfig
       { font = "xft:DejaVu Sans Mono:pixelsize=14"
       , bgColor           = "#0c1021"
       , fgColor           = "#f8f8f8"
       , fgHLight          = "#f8f8f8"
       , bgHLight          = "steelblue3"
       , borderColor       = "DarkOrange"
       , promptBorderWidth = 1
       , position          = Top
       , height            = 22
       , defaultText       = []
       }

myAutoSP = mySP { autoComplete       = Just 1000 }
myWaitSP = mySP { autoComplete       = Just 1000000 }

delicious = searchEngine "delicious" "http://delicious.com/doitian/"
searchEngineMap method = M.fromList $
    [ ((0, xK_b), method delicious)
    , ((0, xK_d), method dictionary)
    , ((0, xK_g), method google)
    , ((0, xK_w), method wikipedia) ]

xmobarStrip :: String -> String
xmobarStrip = strip [] where
    strip keep x
      | null x                 = keep
      | "<fc="  `isPrefixOf` x = strip keep (drop 1 . dropWhile (/= '>') $ x)
      | "</fc>" `isPrefixOf` x = strip keep (drop 5  x)
      | '<' == head x          = strip (keep ++ "<") (tail x)
      | otherwise              = let (good,x') = span (/= '<') x
                                 in strip (keep ++ good) x'

data LibNotifyUrgencyHook = LibNotifyUrgencyHook deriving (Read, Show)

instance UrgencyHook LibNotifyUrgencyHook where
    urgencyHook LibNotifyUrgencyHook w = do
        name <- getName w
        ws <- gets windowset
        whenJust (W.findTag w ws) (flash name)
      where flash name index =
                safeSpawn "notify-send" (show name ++ " requests your attention on workspace " ++ index)

myXmobarPP h = xmobarPP
               { ppOutput = hPutStrLn h
               , ppCurrent = xmobarColor "#f8f8f8" "DodgerBlue4" . wrap " " " "
               , ppVisible = xmobarColor "#f8f8f8" "LightSkyBlue4" . wrap " " " "
               , ppUrgent = xmobarColor "#f8f8f8" "red4" . wrap " " " " . xmobarStrip
               , ppLayout = wrap " |" "" . xmobarColor "DarkOrange" "" . wrap " [" "] "
               , ppTitle = xmobarColor "#61ce3c" "" . shorten 50
               , ppSort = fmap (.scratchpadFilterOutWorkspace) getSortByIndex
               , ppSep = ""
               , ppWsSep = " | "
               }

myTheme :: Theme
myTheme = defaultTheme
          { fontName = "xft:DejaVu Sans YuanTi:pixelsize=14"
          , decoHeight = 22
          , decoWidth = 400
          , activeColor = "grey75"
          , inactiveColor = "grey30"
          , urgentColor = "grey30"
          , activeBorderColor = "#61ce3c"
          , inactiveBorderColor = "grey40"
          , urgentBorderColor = "red"
          , activeTextColor = "black"
          , inactiveTextColor = "grey80"
          , urgentTextColor = "grey80"
          }

myLayoutPrompt = inputPromptWithCompl myAutoSP "Layout"
                 (mkComplFunFromList' ["1.Tall", "2.Wide", "3.2Col", "4.2Row", "5.Tab"]) ?+ \l ->
                     sendMessage $ JumpToLayout $ drop 2 l

-- unused char
-- x, c, y, ', \, d
myKeys =  \conf -> mkKeymap conf $
    [ ("M-S-<Return>", spawn $ XMonad.terminal conf) -- terminal
    , ("M-`", scratchpadSpawnAction conf) -- quake terminal

    -- prompt
    , ("M-p r", shellPrompt mySP) -- shell prompt
    , ("M-p t", prompt (myTerminal ++ " -e") mySP) -- run in term
    , ("M-p g", windowPromptGoto myWaitSP) -- window go prompt
    , ("M-p b", windowPromptBring myWaitSP) -- window bring prompt
    , ("M-p d", AL.launchApp mySP { defaultText = "~" } "thunar" ) -- thunar prompt
    , ("M-g", windowPromptGoto myWaitSP)
    , ("M-b", windowPromptBring myWaitSP)
    , ("M-<Return>", runOrRaise "gmrun" (className =? "Gmrun")) -- gmrun

    , ("M-s", -- search b:delicious d:dictionary g:google w:wikipedia
       (SM.submap $ searchEngineMap $ promptSearch mySP)
       >> raise (className =? "Firefox"))
    , ("M-S-s", spawn "googlizer") -- search selection
    , ("M-C-s", promptSelection "gnome-open") -- search selection

    -- app
    , ("M-o", runOrRaiseNext "firefox" (className =? "Firefox")) -- firefox
    , ("M-S-o", spawn "firefox") -- new firefox
    , ("M-i", runOrRaiseNext "emacs" (className =? "Emacs")) --emacs
    , ("M-u", runOrRaiseNext "urxvt" (className =? "URxvt" <&&> appName /=? "scratchpad")) -- raise next terminal

    , ("M-c p", runOrRaiseNext "gksu synaptic" (className =? "Synaptic")) -- Synaptic
    , ("M-c t", runOrRaiseNext "urxvt -name htop -e htop" (appName =? "htop")) -- Top
    , ("M-c q", runOrRaiseNext "qq" (className =? "Qq")) --QQ
    , ("M-c M-C-S-q", spawn "gksu poweroff") -- PowerOff
    , ("M-c h", spawn "/home/ian/bin/xmonad_key.sh") -- Help
    , ("M-c i", spawn "/home/ian/bin/xp.sh") -- Window Info
    , ("M-c m", runOrRaiseNext "sonata" (className =? "Sonata")) -- Music Player
    , ("M-c x", spawn "xkill") -- Kill X app

    -- client
    , ("M-S-c", kill1) -- kill
    , ("M-C-c", kill) -- kill all
    , ("M-C-S-c", kill) -- kill all
    , ("M-S-<Backspace>", kill1) -- kill
    , ("M-S-=", windows copyToAll) -- copy to all
    , ("M-=",  killAllOtherCopies) -- kill others
    , ("M-C-S-=",  kill) -- kill all

    , ("M-C-<Tab>", rotSlavesUp) -- rotate slaves
    , ("M-<Tab>", windows W.focusDown) -- focus down
    , ("M-S-<Tab>", windows W.focusUp  ) -- focus up
    , ("M-<Page_Down>", windows W.focusDown) -- focus down
    , ("M-<Page_Up>", windows W.focusUp  ) -- focus up
    , ("M-m", windows W.focusMaster  ) -- focus master
    , ("M-z", focusUrgent) -- focus urgent
    , ("M-;", promote) -- promote to master
    , ("M-S-<Page_Down>", windows W.swapDown  ) -- swap down
    , ("M-S-<Page_Up>", windows W.swapUp    ) -- swap up

    , ("M-[", sendMessage Shrink) -- shrink master
    , ("M-]", sendMessage Expand) -- expand master
    , ("M-S-[", sendMessage MirrorShrink) -- shrink window in slave pane
    , ("M-S-]", sendMessage MirrorExpand) -- expand window in slave pane

    , ("M-h", sendMessage $ Go L) -- focus left
    , ("M-j", sendMessage $ Go D) -- focus down
    , ("M-k", sendMessage $ Go U) -- focus up
    , ("M-l", sendMessage $ Go R) -- focus right
    , ("M-S-h", sendMessage $ Swap L) -- swap left
    , ("M-S-j", sendMessage $ Swap D) -- swap down
    , ("M-S-k", sendMessage $ Swap U) -- swap up
    , ("M-S-l", sendMessage $ Swap R) -- swap right
    , ("M-C-h", sendMessage $ Move L) -- move left
    , ("M-C-j", sendMessage $ Move D) -- move down
    , ("M-C-k", sendMessage $ Move U) -- move up
    , ("M-C-l", sendMessage $ Move R) -- move right

    -- float
    , ("M-<L>", withFocused (keysMoveWindow (-20,0))) -- move float left
    , ("M-<R>", withFocused (keysMoveWindow (20,0))) -- move float right
    , ("M-<U>", withFocused (keysMoveWindow (0,-20))) -- move float up
    , ("M-<D>", withFocused (keysMoveWindow (0,20))) -- move float down
    , ("M-S-<L>", withFocused (keysResizeWindow (-20,0) (0,0))) --shrink float at right
    , ("M-S-<R>", withFocused (keysResizeWindow (20,0) (0,0))) --expand float at right
    , ("M-S-<D>", withFocused (keysResizeWindow (0,20) (0,0))) --expand float at bottom
    , ("M-S-<U>", withFocused (keysResizeWindow (0,-20) (0,0))) --shrink float at bottom
    , ("M-C-<L>", withFocused (keysResizeWindow (20,0) (1,0))) --expand float at left
    , ("M-C-<R>", withFocused (keysResizeWindow (-20,0) (1,0))) --shrink float at left
    , ("M-C-<U>", withFocused (keysResizeWindow (0,20) (0,1))) --expand float at top
    , ("M-C-<D>", withFocused (keysResizeWindow (0,-20) (0,1))) --shrink float at top

    -- layout
    , ("M-\\", sendMessage NextLayout) -- toggle layouts
    , ("M-S-\\", myLayoutPrompt) -- layout prompt
    , ("M-C-\\", setLayout $ XMonad.layoutHook conf) -- reset layout
    , ("M-f", sendMessage (Toggle "Full")) -- toggle Full
    , ("M-<Escape>", sendMessage ToggleStruts) -- toggle panel
    , ("M-t", withFocused $ windows . W.sink) -- sink focused window
    , ("M-S-t", sinkAll) -- sink all windows
    , ("M-C-[", sendMessage (IncMasterN 1)) -- increase master windows number
    , ("M-C-]", sendMessage (IncMasterN (-1))) --decrease master windows number

    -- system
    , ("M-C-S-q", io (exitWith ExitSuccess)) -- exit
    , ("M-S-q",  broadcastMessage ReleaseResources >> restart "xmonad" True) -- restart
    , ("M-q", refresh) -- refresh

    -- cycle through workspaces
    , ("M-n", selectWorkspace mySP) -- workspace prompt
    , ("M-S-n", withWorkspace mySP (windows . W.shift)) -- workspace shift prompt
    , ("M-C-n", withWorkspace mySP (windows . copy)) -- workspace copy prompt
    , ("M-C-S-n", renameWorkspace mySP) -- rename workspace
    , ("M-C-S-<Backspace>", removeWorkspace) -- delete empty workspace
    , ("M-/", toggleWSNoSP) -- toggle recently visited workspaces

    -- misc
    ]
    ++
    -- "M-[1..9,0,-]" -- Switch to workspace N
    -- "M-S-[1..9,0,-]" -- Move client to workspace N
    -- "M-C-[1..9,0,-]" -- Copy client to workspace N
    [("M-" ++ m ++ [k], windows $ f i)
        | (i, k) <- zip (XMonad.workspaces conf) (['1' .. '9'] ++ ['0', '-'])
        , (f, m) <- [ (W.greedyView, "")
                    , (W.shift, "S-")
                    , (copy, "C-")
                    ]
    ]
    ++
    -- "M-C-S-[1..9,0,-]" -- Move client to workspace N and follow
    [("M-C-S-" ++ [k], (windows $ W.shift i) >> (windows $ W.greedyView i))
        | (i, k) <- zip (XMonad.workspaces conf) (['1' .. '9'] ++ ['0', '-'])
    ]
    ++
    -- "M-{w,e,r}" -- Switch to physical/Xinerama screens 1, 2, or 3
    -- "M-S-{w,e,r}" -- Move client to screen 1, 2, or 3
    --
    [("M-" ++ m ++ k, screenWorkspace sc >>= flip whenJust (windows . f))
        | (k, sc) <- zip ["w", "e", "r"] [0..]
        , (f, m) <- [(W.view, ""), (W.shift, "S-")]]

    ++
    [ ("M-.", windows . W.greedyView =<< findWorkspace getSortByIndexNoSP Next HiddenNonEmptyWS 1) -- go to next workspace
    , ("M-,", windows . W.greedyView =<< findWorkspace getSortByIndexNoSP Prev HiddenNonEmptyWS 1) -- go to prev workspace
    , ("M-S-.", windows . W.shift =<< findWorkspace getSortByIndexNoSP Next HiddenNonEmptyWS 1) -- shift to next workspace
    , ("M-S-,", windows . W.shift =<< findWorkspace getSortByIndexNoSP Prev HiddenNonEmptyWS 1) -- shift to prev workspace
    -- move window to and focus HiddenNonEmpty wss except scratchpad
    , ("M-C-S-.", shiftAndView' Next) -- shift to next workspace and follow
    , ("M-C-S-,", shiftAndView' Prev) -- shift to prev workspace and follow
    ]
    where 
      getSortByIndexNoSP =
          fmap (.scratchpadFilterOutWorkspace) getSortByIndex
      shiftAndView' dir = findWorkspace getSortByIndexNoSP dir HiddenNonEmptyWS 1
                          >>= \t -> (windows . W.shift $ t) >> (windows . W.greedyView $ t)
      toggleWSNoSP = windows $ W.view =<< W.tag . head . scratchpadFilterOutWorkspace . W.hidden

myMouseBindings (XConfig {XMonad.modMask = modMask}) = M.fromList $
    [ ((modMask, button1), (\w -> focus w >> mouseMoveWindow w))
    , ((modMask, button2), (\w -> focus w >> windows W.swapMaster))
    , ((modMask, button3),               (\w -> focus w >> FlexR.mouseResizeWindow w))
    , ((modMask .|. shiftMask, button3), (\w -> focus w >> SQR.mouseResizeWindow w True ))
    -- you may also bind events to the mouse scroll wheel (button4 and button5)
    ]

------------------------------------------------------------------------
-- Layouts:
myLayout = ewmhDesktopsLayout
           $ avoidStruts
           $ configurableNavigation (navigateColor "snow1")
           $ toggleLayouts (noBorders Full)
           $ smartBorders
           $ onWorkspace "9.im" pidginLayout
           $ onWorkspace "0.qq" qqLayout
           $ onWorkspace "-" mgrid
           $ basicLayout
  where
    basicLayout  = tall ||| wide ||| twoCol ||| twoRow ||| tabs
    rtiled       = layoutHints $ ResizableTall nmaster delta ratio []
    tall         = named "Tall" $ deco $ rtiled
    wide         = named "Wide" $ deco $ Mirror rtiled
    twoPane      = layoutHints $ TwoPane delta ratio
    twoCol       = named "2Col" $ deco $ twoPane
    twoRow       = named "2Row" $ deco $ Mirror twoPane
    tabs         = named "Tab" $ layoutHints $ tabbed shrinkText myTheme
    deco         = dwmStyle shrinkText myTheme
    nmaster      = 1
    ratio        = 1/2
    delta        = 3/100
    pidginLayout = named "IM" $ (withIM (1%7) pidginRoster grid)
    qqLayout     = named "IM" $ (withIM (1%7) qqRoster grid)
    grid         = layoutHints Grid
    mgrid        = named "Grid" $ Mag.magnifiercz 1.15 $ grid
    pidginRoster = And (ClassName "Pidgin") (Role "buddy_list")
    qqRoster     = And (ClassName "Qq") (Title "QQ")


-- myWorkspaces = ["web", "irc", "code" ] ++ map show [4..9]
myWorkspaces    = ["1.sys","2.www","3.emacs","4.doc","5","6","7","8","9.im","0.qq","-"]
                   
myAdditionalManageHook = composeOne $
    [ transience ]
    ++
    [ className =? c                -?> doIgnore | c <- ignoreC ]
    ++
    [ className =? c                -?> doFloat | c <- floatC ]
    ++
    [ className =? c                -?> doCenterFloat | c <- centerFloatC ]
    -- ++
    -- [ resource  =? r                -?> doCenterFloat | r <- centerFloatR ]
    -- ++
    ++
    [ className =? "Firefox" <&&> resource =? r -?> doCenterFloat
      | r <- floatFF ]
    ++
    -- auto shift
    [ className =? c         -?> doShift t
      | (c, t) <- [ ("Pidgin", "9.im")
                  , ("Qq", "0.qq")
                  , ("Firefox", "2.www")
                  , ("Emacs", "3.emacs")
                  ]
    ]
    ++
    -- auto copy
    [ className =? c         -?> (ask >>= doF .  \w -> (copyWindow w t))
      | (c, t) <- [ ("Evince", "4.doc")
                  , ("Xpdf", "4.doc")
                  ]
    ]
    where
      unFloat = ask >>= doF . W.sink
      floatFF = [ "DTA", "Manager", "Extension", "Download",
                  "Dialog", "Browser", "Toplevel" ]
      floatC  = [ "Zenity", "Twhirl", "Gcalctool", "Airappinstaller", "Qt-dotnet.dll" ]
      ignoreC = [ "Do", "trayer" ]
      centerFloatC = [ "Gcolor2", "Sonata", "Stardict", "Update-manager" ]

myManageHook = manageDocks
               <+> (doF avoidMaster)
               <+> myAdditionalManageHook
               <+> composeAll [(isFullscreen --> doFullFloat)]
               <+> scratchpadManageHook (W.RationalRect 0 0 1 0.3)
               <+> manageHook defaultConfig

avoidMaster :: W.StackSet i l a s sd -> W.StackSet i l a s sd
avoidMaster = W.modify' $ \c -> case c of
    W.Stack t [] (r:rs) -> W.Stack t [r] rs
    otherwise           -> c

-- Whether focus follows the mouse pointer.
myFocusFollowsMouse :: Bool
myFocusFollowsMouse = True

------------------------------------------------------------------------
-- Startup hook

-- Perform an arbitrary action each time xmonad starts or is restarted
-- with mod-q.  Used by, e.g., XMonad.Layout.PerWorkspace to initialize
-- per-workspace layout choices.
--
-- By default, do nothing.
myStartupHook = return ()

------------------------------------------------------------------------
-- Now run xmonad with all the defaults we set up.

-- Run xmonad with the settings you specify. No need to modify this.
--

main = do
  xmobar <- spawnPipe "/usr/bin/xmobar /home/ian/.xmobarrc"
  xmonad $ withUrgencyHook LibNotifyUrgencyHook defaultConfig
             { terminal           = myTerminal
             , focusFollowsMouse  = myFocusFollowsMouse
             , borderWidth        = myBorderWidth
             , modMask            = myModMask
             , numlockMask        = myNumlockMask
             , workspaces         = myWorkspaces
             , normalBorderColor  = myNormalBorderColor
             , focusedBorderColor = myFocusedBorderColor

             -- key bindings
             , keys               = myKeys
             , mouseBindings      = myMouseBindings

             -- hooks, layouts
             , layoutHook         = myLayout
             , manageHook         = myManageHook
             , logHook            = ewmhDesktopsLogHook >> (dynamicLogWithPP $ myXmobarPP xmobar)
                                    >> updatePointer Nearest
             , startupHook        = myStartupHook
             }
             `additionalKeys`
             [ ((0, 0x1008ff18), AL.launchApp mySP { defaultText = "~" } "thunar" )
             , ((0, 0x1008ff26), spawn "mpc prev")
             , ((0, 0x1008ff27), spawn "mpc next")
             ]