'Return the next possible completion for \'text\'.'

def complete(self, text, state):

    if (state == 0):
        try:
            import readline
        except ImportError:
            import pyreadline as readline
        origline = readline.get_line_buffer()
        begidx = readline.get_begidx()
        endidx = readline.get_endidx()
        if (begidx > 0):
            (cmd, args, foo) = self.parseline(origline)
            if (cmd == ''):
                compfunc = self.completedefault
            else:
                try:
                    compfunc = getattr(self, ('complete_' + cmd.lower()))
                except AttributeError:
                    try:
                        compfunc = self.ctx.lookup_compfunction(cmd)
                    except AttributeError:
                        compfunc = self.completedefault
        else:
            compfunc = self.completenames
        arglist = [item.strip() for item in origline.strip().split()]
        comp_state = self.get_compstate(text, arglist)
        self.completion_matches = compfunc(text, origline, arglist, comp_state, begidx, endidx)
    try:
        return self.completion_matches[state]
    except IndexError:
        return None
