'Run command in interpreter'

def run_command(self, cmd, new_prompt=True):

    if (cmd == 'exit()'):
        self.exit_flag = True
        self.write('\n')
        return
    special_pattern = '^%s    (?:r\\\')?(?:u\\\')?\\"?\\\'?([a-zA-Z0-9_\\.]+)'
    run_match = re.match((special_pattern % 'run'), cmd)
    help_match = re.match('^([a-zA-Z0-9_\\.]+)\\?$', cmd)
    cd_match = re.match('^\\!cd    \\"?\\\'?([a-zA-Z0-9_    \\.]+)', cmd)
    if help_match:
        cmd = ('help(%s)' % help_match.group(1))
    elif run_match:
        filename = guess_filename(run_match.groups()[0])
        cmd = ("runfile('%s',    args=None)" % remove_backslashes(filename))
    elif cd_match:
        cmd = ('import    os;    os.chdir(r"%s")' % cd_match.groups()[0].strip())
    xedit_match = re.match((special_pattern % 'xedit'), cmd)
    edit_match = re.match((special_pattern % 'edit'), cmd)
    clear_match = re.match('^clear    ([a-zA-Z0-9_,    ]+)', cmd)
    if xedit_match:
        filename = guess_filename(xedit_match.groups()[0])
        self.widget_proxy.edit(filename, external_editor=True)
    elif edit_match:
        filename = guess_filename(edit_match.groups()[0])
        if osp.isfile(filename):
            self.widget_proxy.edit(filename)
        else:
            self.stderr_write.write(('No    such    file    or    directory:    %s\n' % filename))
    elif clear_match:
        varnames = clear_match.groups()[0].replace('    ', '').split(',')
        for varname in varnames:
            try:
                self.namespace.pop(varname)
            except KeyError:
                pass
    elif cmd.startswith('!'):
        pipe = programs.run_shell_command(cmd[1:])
        txt_out = encoding.transcode(pipe.stdout.read().decode())
        txt_err = encoding.transcode(pipe.stderr.read().decode().rstrip())
        if txt_err:
            self.stderr_write.write(txt_err)
        if txt_out:
            self.stdout_write.write(txt_out)
        self.stdout_write.write('\n')
        self.more = False
    else:
        self.more = self.push(cmd)
    if new_prompt:
        self.widget_proxy.new_prompt((self.p2 if self.more else self.p1))
    if (not self.more):
        self.resetbuffer()
