Prompt: added nextCompletion and commandToComplete methods to fine tune prompts' completion functions

This commit is contained in:
Andrea Rossato
2008-02-16 11:37:23 +00:00
parent 8a793ce064
commit 99f6944c3d

View File

@@ -105,6 +105,8 @@ instance Show XPType where
instance XPrompt XPType where instance XPrompt XPType where
showXPrompt = show showXPrompt = show
nextCompletion (XPT t) c l = nextCompletion t c l
commandToComplete (XPT t) c = commandToComplete t c
-- | The class prompt types must be an instance of. In order to -- | The class prompt types must be an instance of. In order to
-- create a prompt you need to create a data type, without parameters, -- create a prompt you need to create a data type, without parameters,
@@ -119,6 +121,12 @@ instance XPrompt XPType where
class XPrompt t where class XPrompt t where
showXPrompt :: t -> String showXPrompt :: t -> String
nextCompletion :: t -> String -> [String] -> String
nextCompletion t c l = newCommand t c l
commandToComplete :: t -> String -> String
commandToComplete _ c = getLastWord c
data XPPosition = Top data XPPosition = Top
| Bottom | Bottom
deriving (Show,Read) deriving (Show,Read)
@@ -255,7 +263,7 @@ completionHandle c (ks,_) (KeyEvent {ev_event_type = t})
case c of case c of
[] -> do updateWindows [] -> do updateWindows
eventLoop handle eventLoop handle
l -> do let new_command = newCommand (command st) l l -> do let new_command = nextCompletion (xptype st) (command st) l --newCommand (command st) l
modify $ \s -> s { command = new_command, offset = length new_command } modify $ \s -> s { command = new_command, offset = length new_command }
redrawWindows c redrawWindows c
eventLoop (completionHandle c) eventLoop (completionHandle c)
@@ -267,20 +275,6 @@ 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
-- | Given a completion and a list of possible completions, returns the
-- index of the next completion in the list
newIndex :: String -> [String] -> Int
newIndex com cl =
case elemIndex (getLastWord com) cl of
Just i -> if i >= length cl - 1 then 0 else i + 1
Nothing -> 0
-- | Given a completion and a list of possible completions, returns the
-- the next completion in the list
newCommand :: String -> [String] -> String
newCommand com cl =
skipLastWord com ++ (cl !! (newIndex com cl))
-- KeyPresses -- KeyPresses
data Direction = Prev | Next deriving (Eq,Show,Read) data Direction = Prev | Next deriving (Eq,Show,Read)
@@ -503,7 +497,7 @@ printPrompt drw = do
getCompletions :: XP [String] getCompletions :: XP [String]
getCompletions = do getCompletions = do
s <- get s <- get
io $ (completionFunction s) (getLastWord $ command s) io $ (completionFunction s) (commandToComplete (xptype s) (command s))
`catch` \_ -> return [] `catch` \_ -> return []
setComplWin :: Window -> ComplWindowDim -> XP () setComplWin :: Window -> ComplWindowDim -> XP ()
@@ -619,7 +613,7 @@ printComplString :: Display -> Drawable -> GC -> String -> String
-> Position -> Position -> String -> XP () -> Position -> Position -> String -> XP ()
printComplString d drw gc fc bc x y s = do printComplString d drw gc fc bc x y s = do
st <- get st <- get
if s == getLastWord (command st) if s == commandToComplete (xptype st) (command st)
then printStringXMF d drw (fontS st) gc then printStringXMF d drw (fontS st) gc
(fgHLight $ config st) (bgHLight $ config st) x y s (fgHLight $ config st) (bgHLight $ config st) x y s
else printStringXMF d drw (fontS st) gc fc bc x y s else printStringXMF d drw (fontS st) gc fc bc x y s
@@ -711,6 +705,20 @@ splitInSubListsAt _ [] = []
splitInSubListsAt i x = f : splitInSubListsAt i rest splitInSubListsAt i x = f : splitInSubListsAt i rest
where (f,rest) = splitAt i x where (f,rest) = splitAt i x
-- | Given a completion and a list of possible completions, returns the
-- the next completion in the list
newCommand :: XPrompt t => t -> String -> [String] -> String
newCommand t com cl =
skipLastWord com ++ (cl !! (newIndex t com cl))
-- | Given a completion and a list of possible completions, returns the
-- index of the next completion in the list
newIndex :: XPrompt t => t -> String -> [String] -> Int
newIndex t com cl =
case elemIndex (commandToComplete t com) cl of
Just i -> if i >= length cl - 1 then 0 else i + 1
Nothing -> 0
-- | Gets the last word of a string or the whole string if formed by -- | Gets the last word of a string or the whole string if formed by
-- only one word -- only one word
getLastWord :: String -> String getLastWord :: String -> String