public function search($mailbox, $criteria, $return_uid = false, $items = array())
    {
        $old_sel = $this->selected;

        if (!$this->select($mailbox)) {
            return new rcube_result_index($mailbox);
        }

        // return empty result when folder is empty and we're just after SELECT
        if ($old_sel != $mailbox && !$this->data['EXISTS']) {
            return new rcube_result_index($mailbox, '* SEARCH');
        }

        // If ESEARCH is supported always use ALL
        // but not when items are specified or using simple id2uid search
        if (empty($items) && preg_match('/[^0-9]/', $criteria)) {
            $items = array('ALL');
        }

        $esearch  = empty($items) ? false : $this->getCapability('ESEARCH');
        $criteria = trim($criteria);
        $params   = '';

        // RFC4731: ESEARCH
        if (!empty($items) && $esearch) {
            $params .= 'RETURN (' . implode(' ', $items) . ')';
        }

        if (!empty($criteria)) {
            $params .= ($params ? ' ' : '') . $criteria;
        }
        else {
            $params .= 'ALL';
        }

        list($code, $response) = $this->execute($return_uid ? 'UID SEARCH' : 'SEARCH',
            array($params));

        if ($code != self::ERROR_OK) {
            $response = null;
        }

        return new rcube_result_index($mailbox, $response);
    }