-----------------------------------------------------------------------------
-- |
-- 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 String -> String -> String
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 =
    XMonad -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
forall p.
XPrompt p =>
p -> XPConfig -> ComplFunction -> (String -> X ()) -> X ()
mkXPrompt (String -> XMonad
XMonad String
title') XPConfig
c (XPConfig -> [String] -> ComplFunction
mkComplFunFromList' XPConfig
c (((String, X ()) -> String) -> [(String, X ())] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map (String, X ()) -> String
forall a b. (a, b) -> a
fst [(String, X ())]
commands)) ((String -> X ()) -> X ()) -> (String -> X ()) -> X ()
forall a b. (a -> b) -> a -> b
$
        X () -> Maybe (X ()) -> X ()
forall a. a -> Maybe a -> a
fromMaybe (() -> X ()
forall a. a -> X a
forall (m :: * -> *) a. Monad m => a -> m a
return ()) (Maybe (X ()) -> X ())
-> (String -> Maybe (X ())) -> String -> X ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String -> [(String, X ())] -> Maybe (X ())
forall a b. Eq a => a -> [(a, b)] -> Maybe b
`lookup` [(String, X ())]
commands)