Refactoring: helper for mkXPromptWithReturn and mkXPromptWithModes

This commit is contained in:
Michael Sloan
2019-01-23 14:11:58 -08:00
parent 1706160b14
commit 41a2db5563

View File

@@ -471,40 +471,18 @@ getCurrentCompletions = gets currentCompletions
-- module. -- module.
mkXPromptWithReturn :: XPrompt p => p -> XPConfig -> ComplFunction -> (String -> X a) -> X (Maybe a) mkXPromptWithReturn :: XPrompt p => p -> XPConfig -> ComplFunction -> (String -> X a) -> X (Maybe a)
mkXPromptWithReturn t conf compl action = do mkXPromptWithReturn t conf compl action = do
XConf { display = d, theRoot = rw } <- ask st' <- mkXPromptImplementation (showXPrompt t) conf (XPSingleMode compl (XPT t))
s <- gets $ screenRect . W.screenDetail . W.current . windowset if successful st'
hist <- io readHistory then do
w <- io $ createWin d rw conf s let selectedCompletion =
io $ selectInput d w $ exposureMask .|. keyPressMask case alwaysHighlight (config st') of
gc <- io $ createGC d w -- When alwaysHighlight is True, autocompletion is
io $ setGraphicsExposures d gc False -- handled with indexes.
fs <- initXMF (font conf) False -> command st'
numlock <- gets $ X.numberlockMask -- When it is false, it is handled depending on the
let hs = fromMaybe [] $ M.lookup (showXPrompt t) hist -- prompt buffer's value.
om = (XPSingleMode compl (XPT t)) --operation mode True -> fromMaybe (command st') $ highlightedCompl st'
st = initState d rw w s om gc fs hs conf numlock Just <$> action selectedCompletion
st' <- io $ execStateT runXP st
releaseXMF fs
io $ freeGC d gc
if successful st' then do
let
prune = take (historySize conf)
io $ writeHistory $ M.insertWith
(\xs ys -> prune . historyFilter conf $ xs ++ ys)
(showXPrompt t)
(prune $ historyFilter conf [command st'])
hist
-- we need to apply historyFilter before as well, since
-- otherwise the filter would not be applied if
-- there is no history
--When alwaysHighlight is True, autocompletion is handled with indexes.
--When it is false, it is handled depending on the prompt buffer's value
let selectedCompletion = case alwaysHighlight (config st') of
False -> command st'
True -> fromMaybe (command st') $ highlightedCompl st'
Just <$> action selectedCompletion
else return Nothing else return Nothing
-- | Creates a prompt given: -- | Creates a prompt given:
@@ -532,6 +510,26 @@ mkXPrompt t conf compl action = mkXPromptWithReturn t conf compl action >> retur
-- that means that this prompt overrides the value `alwaysHighlight` for its configuration to True. -- that means that this prompt overrides the value `alwaysHighlight` for its configuration to True.
mkXPromptWithModes :: [XPType] -> XPConfig -> X () mkXPromptWithModes :: [XPType] -> XPConfig -> X ()
mkXPromptWithModes modes conf = do mkXPromptWithModes modes conf = do
let defaultMode = head modes
modeStack = W.Stack { W.focus = defaultMode -- Current mode
, W.up = []
, W.down = tail modes -- Other modes
}
om = XPMultipleModes modeStack
st' <- mkXPromptImplementation (showXPrompt defaultMode) conf { alwaysHighlight = True } om
if successful st'
then do
case operationMode st' of
XPMultipleModes ms -> let
action = modeAction $ W.focus ms
in action (command st') $ (fromMaybe "" $ highlightedCompl st')
_ -> error "The impossible occurred: This prompt runs with multiple modes but they could not be found." --we are creating a prompt with multiple modes, so its operationMode should have been constructed with XPMultipleMode
else return ()
-- Internal function used to implement 'mkXPromptWithReturn' and
-- 'mkXPromptWithModes'.
mkXPromptImplementation :: String -> XPConfig -> XPOperationMode -> X XPState
mkXPromptImplementation historyKey conf om = do
XConf { display = d, theRoot = rw } <- ask XConf { display = d, theRoot = rw } <- ask
s <- gets $ screenRect . W.screenDetail . W.current . windowset s <- gets $ screenRect . W.screenDetail . W.current . windowset
hist <- io readHistory hist <- io readHistory
@@ -541,38 +539,25 @@ mkXPromptWithModes modes conf = do
io $ setGraphicsExposures d gc False io $ setGraphicsExposures d gc False
fs <- initXMF (font conf) fs <- initXMF (font conf)
numlock <- gets $ X.numberlockMask numlock <- gets $ X.numberlockMask
let let hs = fromMaybe [] $ M.lookup historyKey hist
defaultMode = head modes st = initState d rw w s om gc fs hs conf numlock
hs = fromMaybe [] $ M.lookup (showXPrompt defaultMode) hist
modeStack = W.Stack{ W.focus = defaultMode --current mode
, W.up = []
, W.down = tail modes --other modes
}
st = initState d rw w s (XPMultipleModes modeStack) gc fs hs conf { alwaysHighlight = True} numlock
st' <- io $ execStateT runXP st st' <- io $ execStateT runXP st
releaseXMF fs releaseXMF fs
io $ freeGC d gc io $ freeGC d gc
if successful st' then do when (successful st') $ do
let let prune = take (historySize conf)
prune = take (historySize conf) io $ writeHistory $
M.insertWith
-- insert into history the buffers value
io $ writeHistory $ M.insertWith
(\xs ys -> prune . historyFilter conf $ xs ++ ys) (\xs ys -> prune . historyFilter conf $ xs ++ ys)
(showXPrompt defaultMode) historyKey
-- We need to apply historyFilter before as well, since
-- otherwise the filter would not be applied if there is no
-- history
(prune $ historyFilter conf [command st']) (prune $ historyFilter conf [command st'])
hist hist
return st'
case operationMode st' of
XPMultipleModes ms -> let
action = modeAction $ W.focus ms
in action (command st') $ (fromMaybe "" $ highlightedCompl st')
_ -> error "The impossible occurred: This prompt runs with multiple modes but they could not be found." --we are creating a prompt with multiple modes, so its operationMode should have been constructed with XPMultipleMode
else
return ()
-- | Removes numlock and capslock from a keymask. -- | Removes numlock and capslock from a keymask.
-- Duplicate of cleanMask from core, but in the -- Duplicate of cleanMask from core, but in the