def __cbManualSearch(self, result, tag, mbox, query, uid,
                         searchResults=None):
        """
        Apply the search filter to a set of messages. Send the response to the
        client.

        @type result: L{list} of L{tuple} of (L{int}, provider of
            L{imap4.IMessage})
        @param result: A list two tuples of messages with their sequence ids,
            sorted by the ids in descending order.

        @type tag: L{str}
        @param tag: A command tag.

        @type mbox: Provider of L{imap4.IMailbox}
        @param mbox: The searched mailbox.

        @type query: L{list}
        @param query: A list representing the parsed form of the search query.

        @param uid: A flag indicating whether the search is over message
            sequence numbers or UIDs.

        @type searchResults: L{list}
        @param searchResults: The search results so far or L{None} if no
            results yet.
        """
        if searchResults is None:
            searchResults = []
        i = 0

        # result is a list of tuples (sequenceId, Message)
        lastSequenceId = result and result[-1][0]
        lastMessageId = result and result[-1][1].getUID()
        for (i, (msgId, msg)) in list(zip(range(5), result)):
            # searchFilter and singleSearchStep will mutate the query.  Dang.
            # Copy it here or else things will go poorly for subsequent
            # messages.
            if self._searchFilter(copy.deepcopy(query), msgId, msg,
                                  lastSequenceId, lastMessageId):
                if uid:
                    searchResults.append(intToBytes(msg.getUID()))
                else:
                    searchResults.append(intToBytes(msgId))

        if i == 4:
            from twisted.internet import reactor
            reactor.callLater(
                0, self.__cbManualSearch, list(result[5:]), tag, mbox, query, uid,
                searchResults)
        else:
            if searchResults:
                self.sendUntaggedResponse(b'SEARCH ' + b' '.join(searchResults))
            self.sendPositiveResponse(tag, b'SEARCH completed')

        Apply the search filter to a set of messages. Send the response to the
        client.

        @type result: L{list} of L{tuple} of (L{int}, provider of
            L{imap4.IMessage})
        @param result: A list two tuples of messages with their sequence ids,
            sorted by the ids in descending order.

        @type tag: L{str}
        @param tag: A command tag.

        @type mbox: Provider of L{imap4.IMailbox}
        @param mbox: The searched mailbox.

        @type query: L{list}
        @param query: A list representing the parsed form of the search query.

        @param uid: A flag indicating whether the search is over message
            sequence numbers or UIDs.

        @type searchResults: L{list}
        @param searchResults: The search results so far or L{None} if no
            results yet.
        
stackoverflow/venv/lib/python3.6/site-packages/twisted/mail/imap4.py