def magic_matches(self, text):
        completion_log.debug("completing [{0}]".format(text))
        # print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
        # Get all shell magics now rather than statically, so magics loaded at
        # runtime show up too.
        lsm = self.shell.magics_manager.lsmagic()
        line_magics = lsm['line']
        cell_magics = lsm['cell']
        pre = self.magic_escape
        pre2 = pre + pre

        # Completion logic:
        # - user gives %%: only do cell magics
        # - user gives %: do both line and cell magics
        # - no prefix: do both
        # In other words, line magics are skipped if the user gives %%
        # explicitly
        bare_text = text.lstrip(pre)
        comp = [pre2 + m for m in cell_magics if m.startswith(bare_text)]
        if not text.startswith(pre2):
            comp += [pre + m for m in line_magics if m.startswith(bare_text)]
            # do not allow known shell commands to be prefixed
            # with '%' as if they were magic commands
            for i, x in enumerate(comp):
                original = x[len(pre):]
                if have_command_alias(original):
                    comp[i] = original
        return comp
    def smash_matcher(self, shell, event):
        completion_log.info('completing event: {0}'.format(event.__dict__))
        line = event.line

        if not line.strip():
            raise TryNext()
        first_word = line.split()[0]
        # NB: cannot use event.symbol here, it splits on '$'
        last_word = event.text_until_cursor.split()
        last_word = last_word[-1] if last_word else ''
        completion_log.info("first-word, last-word: {0}".format(
            [first_word, last_word]))
        if last_word.startswith('$'):
            return smash_env_complete(last_word)
        magic_command_alias = first_word.startswith('%') and \
            have_command_alias(first_word[1:])
        naked_command_alias = have_command_alias(first_word)
        results = []
        if naked_command_alias:
            completion_log.info('naked command alias detected')
            results += smash_bash_complete(line)[:self.MAX_MATCH]
        elif magic_command_alias:
            completion_log.info('magic command alias detected')
            results += smash_bash_complete(line[1:])[:self.MAX_MATCH]

        # can't do anything smarter? look for file matches.
        # this works by default if the last word contains os.path.sep,
        # but this doesn't necessarily work with the special "ed" alias
        # unless this sectiion is executed
        if not results:
            completion_log.info(('no results for completion, looking for '
                                 'file matches with "{0}"'.format(last_word)))
            results = self.smash.shell.Completer.file_matches(last_word)

        if results:
            completion_log.info("returning: {0}".format(results))
            return results
        else:
            completion_log.info("no results so far, raising trynext ")
            raise TryNext()