-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Util.Dmenu
-- Description :  Convenient bindings to dmenu.
-- Copyright   :  (c) Spencer Janssen <spencerjanssen@gmail.com>
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Spencer Janssen <spencerjanssen@gmail.com>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A convenient binding to dmenu.
--
-- Requires the process-1.0 package
--
-----------------------------------------------------------------------------

module XMonad.Util.Dmenu (
                -- * Usage
                -- $usage
                dmenu, dmenuXinerama, dmenuMap, menu, menuArgs, menuMap, menuMapArgs
               ) where

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

-- $usage
-- You can use this module with the following in your Config.hs file:
--
-- > import XMonad.Util.Dmenu
--
-- These functions block xmonad's event loop until dmenu exits; this means that
-- programs will not be able to open new windows and you will not be able to
-- change workspaces or input focus until you have responded to the prompt one
-- way or another.

-- %import XMonad.Util.Dmenu

-- | Starts dmenu on the current screen. Requires this patch to dmenu:
-- <http://www.jcreigh.com/dmenu/dmenu-3.2-xinerama.patch>
dmenuXinerama :: [String] -> X String
dmenuXinerama :: [String] -> X String
dmenuXinerama [String]
opts = do
    Int
curscreen <-
      forall a b. (Integral a, Num b) => a -> b
fromIntegral forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. Screen i l a sid sd -> sid
W.screen forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i l a sid sd. StackSet i l a sid sd -> Screen i l a sid sd
W.current forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
gets XState
-> StackSet String (Layout Window) Window ScreenId ScreenDetail
windowset :: X Int
    String
_ <-
      forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
runProcessWithInput String
"dmenu" [String
"-xs", forall a. Show a => a -> String
show (Int
curscreenforall a. Num a => a -> a -> a
+Int
1)] ([String] -> String
unlines [String]
opts)
    forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m String
menuArgs String
"dmenu" [String
"-xs", forall a. Show a => a -> String
show (Int
curscreenforall a. Num a => a -> a -> a
+Int
1)] [String]
opts

-- | Run dmenu to select an option from a list.
dmenu :: MonadIO m => [String] -> m String
dmenu :: forall (m :: * -> *). MonadIO m => [String] -> m String
dmenu = forall (m :: * -> *). MonadIO m => String -> [String] -> m String
menu String
"dmenu"

-- | like 'dmenu' but also takes the command to run.
menu :: MonadIO m => String -> [String] -> m String
menu :: forall (m :: * -> *). MonadIO m => String -> [String] -> m String
menu String
menuCmd = forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m String
menuArgs String
menuCmd []

-- | Like 'menu' but also takes a list of command line arguments.
menuArgs :: MonadIO m => String -> [String] -> [String] -> m String
menuArgs :: forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m String
menuArgs String
menuCmd [String]
args [String]
opts = forall a. (a -> Bool) -> [a] -> [a]
filter (forall a. Eq a => a -> a -> Bool
/=Char
'\n') forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
  forall (m :: * -> *).
MonadIO m =>
String -> [String] -> String -> m String
runProcessWithInput String
menuCmd [String]
args ([String] -> String
unlines [String]
opts)

-- | Like 'dmenuMap' but also takes the command to run.
menuMap :: MonadIO m => String -> M.Map String a -> m (Maybe a)
menuMap :: forall (m :: * -> *) a.
MonadIO m =>
String -> Map String a -> m (Maybe a)
menuMap String
menuCmd = forall (m :: * -> *) a.
MonadIO m =>
String -> [String] -> Map String a -> m (Maybe a)
menuMapArgs String
menuCmd []

-- | Like 'menuMap' but also takes a list of command line arguments.
menuMapArgs :: MonadIO m => String -> [String] -> M.Map String a ->
               m (Maybe a)
menuMapArgs :: forall (m :: * -> *) a.
MonadIO m =>
String -> [String] -> Map String a -> m (Maybe a)
menuMapArgs String
menuCmd [String]
args Map String a
selectionMap = do
  String
selection <- [String] -> m String
menuFunction (forall k a. Map k a -> [k]
M.keys Map String a
selectionMap)
  forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup String
selection Map String a
selectionMap
      where
        menuFunction :: [String] -> m String
menuFunction = forall (m :: * -> *).
MonadIO m =>
String -> [String] -> [String] -> m String
menuArgs String
menuCmd [String]
args

-- | Run dmenu to select an entry from a map based on the key.
dmenuMap :: MonadIO m => M.Map String a -> m (Maybe a)
dmenuMap :: forall (m :: * -> *) a. MonadIO m => Map String a -> m (Maybe a)
dmenuMap = forall (m :: * -> *) a.
MonadIO m =>
String -> Map String a -> m (Maybe a)
menuMap String
"dmenu"