Files
xmonad-contrib/Commands.hs
Devin Mullins beaead5256 change Dmenu functions to return IO/X (Maybe String)
dmenu exits with code 1 when you hit Escape, and I wanna create a contrib that
takes advantage of that.

This required changes in four contribs (Commands, DirectoryPrompt, ShellPrompt,
and WorkspaceDir), and might require changes in users' Configs. Also, I'm not
sure some of the changes I made to the client code are very Haskelly. Would
appreciate input there.
2007-10-06 07:09:59 +00:00

113 lines
4.7 KiB
Haskell

-----------------------------------------------------------------------------
-- |
-- Module : XMonadContrib.Commands
-- Copyright : (c) David Glasser 2007
-- License : BSD3
--
-- Maintainer : glasser@mit.edu
-- Stability : stable
-- Portability : portable
--
-- Allows you to run internal xmonad commands (X () actions) using
-- a dmenu menu in addition to key bindings. Requires dmenu and
-- the Dmenu XMonadContrib module.
--
-----------------------------------------------------------------------------
module XMonadContrib.Commands (
-- * Usage
-- $usage
commandMap,
runCommand,
runCommand',
workspaceCommands,
screenCommands,
defaultCommands
) where
import XMonad
import Operations
import StackSet hiding (workspaces)
import XMonadContrib.Dmenu (dmenu)
import {-# SOURCE #-} Config (workspaces,possibleLayouts)
import qualified Data.Map as M
import System.Exit
import Data.Maybe
-- $usage
--
-- To use, modify your Config.hs to:
--
-- > import XMonadContrib.Commands
--
-- and add a keybinding to the runCommand action:
--
-- > , ((modMask .|. controlMask, xK_y), runCommand commands)
--
-- and define the list commands:
--
-- > commands :: [(String, X ())]
-- > commands = defaultCommands
--
-- A popup menu of internal xmonad commands will appear. You can
-- change the commands by changing the contents of the list
-- 'commands'. (If you like it enough, you may even want to get rid
-- of many of your other key bindings!)
-- %def commands :: [(String, X ())]
-- %def commands = defaultCommands
-- %import XMonadContrib.Commands
-- %keybind , ((modMask .|. controlMask, xK_y), runCommand commands)
commandMap :: [(String, X ())] -> M.Map String (X ())
commandMap c = M.fromList c
workspaceCommands :: [(String, X ())]
workspaceCommands = [((m ++ show i), windows $ f i)
| i <- workspaces
, (f, m) <- [(view, "view"), (shift, "shift")]
]
screenCommands :: [(String, X ())]
screenCommands = [((m ++ show sc), screenWorkspace (fromIntegral sc) >>= flip whenJust (windows . f))
| sc <- [0, 1]::[Int] -- TODO: adapt to screen changes
, (f, m) <- [(view, "screen"), (shift, "screen-to-")]
]
defaultCommands :: [(String, X ())]
defaultCommands = workspaceCommands ++ screenCommands
++ [ ("shrink" , sendMessage Shrink )
, ("expand" , sendMessage Expand )
, ("next-layout" , sendMessage NextLayout )
, ("previous-layout" , sendMessage PrevLayout )
, ("default-layout" , setLayout (head possibleLayouts) )
, ("restart-wm" , sr >> restart Nothing True )
, ("restart-wm-no-resume", sr >> restart Nothing False )
, ("xterm" , spawn "xterm" )
, ("run" , spawn "exe=`dmenu_path | dmenu -b` && exec $exe" )
, ("kill" , kill )
, ("refresh" , refresh )
, ("focus-up" , windows $ focusUp )
, ("focus-down" , windows $ focusDown )
, ("swap-up" , windows $ swapUp )
, ("swap-down" , windows $ swapDown )
, ("swap-master" , windows $ swapMaster )
, ("sink" , withFocused $ windows . sink )
, ("quit-wm" , io $ exitWith ExitSuccess )
]
where sr = broadcastMessage ReleaseResources
runCommand :: [(String, X ())] -> X ()
runCommand cl = do
let m = commandMap cl
choice <- dmenu (M.keys m)
case choice of
Just selection -> fromMaybe (return ()) (M.lookup selection m)
Nothing -> return ()
runCommand' :: String -> X ()
runCommand' c = do
let m = commandMap defaultCommands
fromMaybe (return ()) (M.lookup c m)