Skip to content

Commit e4c3511

Browse files
Rewrite match function leveraging Either monad.
1 parent 82a6ced commit e4c3511

1 file changed

Lines changed: 13 additions & 15 deletions

File tree

Core.hs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -444,21 +444,19 @@ genericJumpToMatch :: Lookup a
444444
-> (Int -> HState -> Int)
445445
-> IO ()
446446
genericJumpToMatch re sw k sel = do
447-
mmsg <- modifyHS \st -> let
448-
info = case re of
449-
Just s -> Just (st { searchFw = sw }, s, sw)
450-
_ -> listToMaybe [ (st, s, st.searchFw == sw) | s <- st.searchHist ]
451-
in flip (maybe (st, Just "No previous search.")) info
452-
\ (st', p, forwards) -> do
453-
let (fs, cur, m) = k st
454-
l = if forwards then [cur+1 .. m-1] ++ [0 .. cur]
455-
else [cur-1, cur-2 .. 0] ++ [m-1, m-2 .. cur]
456-
case matches p of
457-
Just match ->
458-
case [ i | i <- l, match $ extract (fs ! i) ] of
459-
i:_ -> (st' { cursor = sel i st }, Nothing)
460-
_ -> (st', Just "No match found.")
461-
_ -> (st', Just "Invalid ERE search pattern.")
447+
mmsg <- modifyHS \st -> either ((st,) . Just) (,Nothing) do
448+
(st', pat, forwards) <- case re of
449+
Just pat -> Right (st { searchFw = sw }, pat, sw)
450+
_ -> case st.searchHist of
451+
pat:_ -> Right (st, pat, st.searchFw == sw)
452+
_ -> Left "No previous search."
453+
let (fs, cur, m) = k st
454+
l = if forwards then [cur+1 .. m-1] ++ [0 .. cur]
455+
else [cur-1, cur-2 .. 0] ++ [m-1, m-2 .. cur]
456+
match <- maybe (Left "Invalid ERE search pattern.") Right $ matches pat
457+
case [ i | i <- l, match $ extract (fs ! i) ] of
458+
i:_ -> Right st' { cursor = sel i st }
459+
_ -> Left "No match found."
462460
whenJust mmsg \msg -> putMessage [plainSeg msg]
463461

464462
------------------------------------------------------------------------

0 commit comments

Comments
 (0)