-----------------------------------------------------------------------------
-- |
-- Module      :  XMonad.Prompt.XMonad
-- Description :  A prompt for running XMonad commands.
-- Copyright   :  (C) 2007 Andrea Rossato
-- License     :  BSD3
--
-- Maintainer  :  andrea.rossato@unibz.it
-- Stability   :  unstable
-- Portability :  unportable
--
-- A prompt for running XMonad commands
--
-----------------------------------------------------------------------------

module XMonad.Prompt.XMonad (
                             -- * Usage
                             -- $usage
                             xmonadPrompt,
                             xmonadPromptC,
                             xmonadPromptCT,
                             XMonad,
                              ) where

import XMonad
import XMonad.Prompt
import XMonad.Actions.Commands (defaultCommands)
import XMonad.Prelude (fromMaybe)

-- $usage
-- You can use this module with the following in your @xmonad.hs@:
--
-- > import XMonad.Prompt
-- > import XMonad.Prompt.XMonad
--
-- in your keybindings add:
--
-- >   , ((modm .|. controlMask, xK_x), xmonadPrompt def)
--
-- For detailed instruction on editing the key binding see
-- <https://xmonad.org/TUTORIAL.html#customizing-xmonad the tutorial>.

newtype XMonad = XMonad String

instance XPrompt XMonad where
    showXPrompt :: XMonad -> String
showXPrompt (XMonad String
str) = String
str forall a. Semigroup a => a -> a -> a
<> String
": "

xmonadPrompt :: XPConfig -> X ()
xmonadPrompt :: XPConfig -> X ()
xmonadPrompt XPConfig
c = do
    [(String, X ())]
cmds <- X [(String, X ())]
defaultCommands
    [(String, X ())] -> XPConfig -> X ()
xmonadPromptC [(String, X ())]
cmds XPConfig
c

-- | An xmonad prompt with a custom command list
xmonadPromptC :: [(String, X ())] -> XPConfig -> X ()
xmonadPromptC :: [(String, X ())] -> XPConfig -> X ()
xmonadPromptC = String -> [(String, X ())] -> XPConfig -> X ()
xmonadPromptCT String
"XMonad"

-- | An xmonad prompt with a custom command list and a custom title
xmonadPromptCT :: String -> [(String, X ())] -> XPConfig -> X ()
xmonadPromptCT :: String -> [(String, X ())] -> XPConfig -> X ()
xmonadPromptCT String
title' [(String, X ())]
commands XPConfig
c =
    forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> XMonad
XMonad String
title') XPConfig
c (XPConfig -> [String] -> ComplFunction
mkComplFunFromList' XPConfig
c (forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(String, X ())]
commands)) forall a b. (a -> b) -> a -> b
$
        forall a. a -> Maybe a -> a
fromMaybe (forall (m :: * -> *) a. Monad m => a -> m a
return ()) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a b. Eq a => a -> [(a, b)] -> Maybe b
`lookup` [(String, X ())]
commands)