mirror of
https://github.com/xmonad/xmonad-contrib.git
synced 2025-08-13 11:16:01 -07:00
add autoComplete option to XMonad.Prompt
Maybe this will get Gwern one step closer to a complete Ratpoison binding.
This commit is contained in:
@@ -52,6 +52,7 @@ import XMonad.Util.Font
|
|||||||
import XMonad.Util.XSelection (getSelection)
|
import XMonad.Util.XSelection (getSelection)
|
||||||
|
|
||||||
import Control.Arrow ((&&&))
|
import Control.Arrow ((&&&))
|
||||||
|
import Control.Concurrent (threadDelay)
|
||||||
import Control.Monad.Reader
|
import Control.Monad.Reader
|
||||||
import Control.Monad.State
|
import Control.Monad.State
|
||||||
import Control.Applicative ((<$>))
|
import Control.Applicative ((<$>))
|
||||||
@@ -98,11 +99,13 @@ data XPConfig =
|
|||||||
, fgHLight :: String -- ^ Font color of a highlighted completion entry
|
, fgHLight :: String -- ^ Font color of a highlighted completion entry
|
||||||
, bgHLight :: String -- ^ Background color of a highlighted completion entry
|
, bgHLight :: String -- ^ Background color of a highlighted completion entry
|
||||||
, borderColor :: String -- ^ Border color
|
, borderColor :: String -- ^ Border color
|
||||||
, promptBorderWidth :: !Dimension -- ^ Border width
|
, promptBorderWidth :: !Dimension -- ^ Border width
|
||||||
, position :: XPPosition -- ^ Position: 'Top' or 'Bottom'
|
, position :: XPPosition -- ^ Position: 'Top' or 'Bottom'
|
||||||
, height :: !Dimension -- ^ Window height
|
, height :: !Dimension -- ^ Window height
|
||||||
, historySize :: !Int -- ^ The number of history entries to be saved
|
, historySize :: !Int -- ^ The number of history entries to be saved
|
||||||
, defaultText :: String -- ^ The text by default in the prompt line
|
, defaultText :: String -- ^ The text by default in the prompt line
|
||||||
|
, autoComplete :: Maybe Int -- ^ Just x: if only one completion remains, auto-select it,
|
||||||
|
-- and delay by x microseconds
|
||||||
} deriving (Show, Read)
|
} deriving (Show, Read)
|
||||||
|
|
||||||
data XPType = forall p . XPrompt p => XPT p
|
data XPType = forall p . XPrompt p => XPT p
|
||||||
@@ -171,6 +174,7 @@ defaultXPConfig =
|
|||||||
, height = 18
|
, height = 18
|
||||||
, historySize = 256
|
, historySize = 256
|
||||||
, defaultText = []
|
, defaultText = []
|
||||||
|
, autoComplete = Nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
type ComplFunction = String -> IO [String]
|
type ComplFunction = String -> IO [String]
|
||||||
@@ -303,6 +307,25 @@ completionHandle _ ks (KeyEvent {ev_event_type = t, ev_state = m})
|
|||||||
-- some other event: go back to main loop
|
-- some other event: go back to main loop
|
||||||
completionHandle _ k e = handle k e
|
completionHandle _ k e = handle k e
|
||||||
|
|
||||||
|
tryAutoComplete :: XP Bool
|
||||||
|
tryAutoComplete = do
|
||||||
|
ac <- gets (autoComplete . config)
|
||||||
|
case ac of
|
||||||
|
Just d -> do cs <- getCompletions
|
||||||
|
case cs of
|
||||||
|
[c] -> runCompleted c d >> return True
|
||||||
|
_ -> return False
|
||||||
|
Nothing -> return False
|
||||||
|
where runCompleted cmd delay = do
|
||||||
|
st <- get
|
||||||
|
let new_command = nextCompletion (xptype st) (command st) [cmd]
|
||||||
|
modify $ \s -> s { command = "autocompleting..." }
|
||||||
|
updateWindows
|
||||||
|
io $ threadDelay delay
|
||||||
|
modify $ \s -> s { command = new_command }
|
||||||
|
historyPush
|
||||||
|
return True
|
||||||
|
|
||||||
-- KeyPresses
|
-- KeyPresses
|
||||||
|
|
||||||
data Direction = Prev | Next deriving (Eq,Show,Read)
|
data Direction = Prev | Next deriving (Eq,Show,Read)
|
||||||
@@ -343,7 +366,8 @@ keyPressHandle _ (_,s)
|
|||||||
| s == "" = eventLoop handle
|
| s == "" = eventLoop handle
|
||||||
| otherwise = do insertString (decodeInput s)
|
| otherwise = do insertString (decodeInput s)
|
||||||
updateWindows
|
updateWindows
|
||||||
eventLoop handle
|
completed <- tryAutoComplete
|
||||||
|
unless completed $ eventLoop handle
|
||||||
|
|
||||||
-- KeyPress and State
|
-- KeyPress and State
|
||||||
|
|
||||||
|
@@ -45,6 +45,15 @@ import XMonad.Actions.WindowBringer
|
|||||||
-- > , ((modMask x .|. shiftMask, xK_g ), windowPromptGoto defaultXPConfig)
|
-- > , ((modMask x .|. shiftMask, xK_g ), windowPromptGoto defaultXPConfig)
|
||||||
-- > , ((modMask x .|. shiftMask, xK_b ), windowPromptBring defaultXPConfig)
|
-- > , ((modMask x .|. shiftMask, xK_b ), windowPromptBring defaultXPConfig)
|
||||||
--
|
--
|
||||||
|
-- The autoComplete option is a handy complement here:
|
||||||
|
--
|
||||||
|
-- > , ((modMask x .|. shiftMask, xK_g ), windowPromptGoto
|
||||||
|
-- > defaultXPConfig { autoComplete = Just 500000 } )
|
||||||
|
--
|
||||||
|
-- The \'500000\' is the number of microseconds to pause before sending you to
|
||||||
|
-- your new window. This is useful so that you don't accidentally send some
|
||||||
|
-- keystrokes to the selected client.
|
||||||
|
--
|
||||||
-- For detailed instruction on editing the key binding see
|
-- For detailed instruction on editing the key binding see
|
||||||
-- "XMonad.Doc.Extending#Editing_key_bindings".
|
-- "XMonad.Doc.Extending#Editing_key_bindings".
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user