goUnmatchedB :: Direction -> Char -> Char -> BufferM ()
goUnmatchedB dir cStart' cStop' = getLineAndCol >>= \position ->
    stepB >> readB >>= go position (0::Int)
    where
        go pos opened c
           | c == cStop && opened == 0 = return ()
           | c == cStop       = goIfNotEofSof pos (opened-1)
           | c == cStart      = goIfNotEofSof pos (opened+1)
           | otherwise        = goIfNotEofSof pos  opened
        goIfNotEofSof pos opened = atEof >>= \eof -> atSof >>= \sof ->
            if not eof && not sof
                then stepB >> readB >>= go pos opened
                else gotoLn (fst pos) >> moveToColB (snd pos)
        (stepB, cStart, cStop) | dir == Forward = (rightB, cStart', cStop')
                               | otherwise      = (leftB, cStop', cStart')

-----------------------------------------------------------------------
-- Queries

-- | Return true if the current point is the start of a line
goUnmatchedB :: Direction -> Char -> Char -> BufferM ()
goUnmatchedB dir cStart' cStop' = getLineAndCol >>= \position ->
    stepB >> readB >>= go position (0::Int)
    where
        go pos opened c
           | c == cStop && opened == 0 = return ()
           | c == cStop       = goIfNotEofSof pos (opened-1)
           | c == cStart      = goIfNotEofSof pos (opened+1)
           | otherwise        = goIfNotEofSof pos  opened
        goIfNotEofSof pos opened = atEof >>= \eof -> atSof >>= \sof ->
            if not eof && not sof
                then stepB >> readB >>= go pos opened
                else gotoLn (fst pos) >> moveToColB (snd pos)
        (stepB, cStart, cStop) | dir == Forward = (rightB, cStart', cStop')
                               | otherwise      = (leftB, cStop', cStart')

-----------------------------------------------------------------------
-- Queries

-- | Return true if the current point is the start of a line
goUnmatchedB dir cStart' cStop' = getLineAndCol >>= \position ->
    stepB >> readB >>= go position (0::Int)
    where
        go pos opened c
           | c == cStop && opened == 0 = return ()
           | c == cStop       = goIfNotEofSof pos (opened-1)
           | c == cStart      = goIfNotEofSof pos (opened+1)
           | otherwise        = goIfNotEofSof pos  opened
        goIfNotEofSof pos opened = atEof >>= \eof -> atSof >>= \sof ->
            if not eof && not sof
                then stepB >> readB >>= go pos opened
                else gotoLn (fst pos) >> moveToColB (snd pos)
        (stepB, cStart, cStop) | dir == Forward = (rightB, cStart', cStop')
                               | otherwise      = (leftB, cStop', cStart')

-----------------------------------------------------------------------
-- Queries

-- | Return true if the current point is the start of a line