diff --git a/buffer.py b/buffer.py index af2532c..add75b2 100644 --- a/buffer.py +++ b/buffer.py @@ -423,13 +423,83 @@ class IperlBuffer(Buffer): def readonly(self): return True -def get_ipython_name(parent): - if hasattr(parent, 'path'): - return '*IPython:%s*' % parent.name() - else: - return '*IPython*' +class IperlBuffer2(Buffer): + def get_ipython_name(parent): + if hasattr(parent, 'path'): + return '*IPython:%s*' % parent.name() + else: + return '*IPython*' + get_ipython_name = classmethod(get_ipython_name) + btype = 'ipython' + modename = 'ipython' + readre = re.compile('^([A-Z]+):(.*)\n$') + _name = '*IPython*' + def __init__(self, parent): + if hasattr(parent, 'path'): + self.parent = parent + else: + self.parent = None + Buffer.__init__(self) + f = open('/dev/null', 'w') + self.pipe = Popen(self.get_cmd(), stdin=PIPE, stdout=PIPE, stderr=f) + self.prompt = '***' + self.clear() + self.pipe_read() + self._name = get_ipython_name(parent) + def name(self): + return self._name + def get_cmd(self): + if self.parent: + return ('epython', '-p', self.parent.path) + else: + return ('epython',) + def pipe_readline(self): + line = self.pipe.stdout.readline() + m = self.readre.match(line) + if m: + return (m.group(1), m.group(2)) + else: + return (None, line.rstrip()) + def pipe_read(self): + lines = [] + while True: + (type_, value) = self.pipe_readline() + if type_ == 'PROMPT': + self.prompt = value.strip() + ' ' + break + value.rstrip() + if value: + lines.append(value) + if lines: + output = '\n'.join(lines) + '\n' + p = self.get_buffer_end() + self.insert_string(p, output, force=True) + def pipe_write(self, s): + self.pipe.stdin.write("%s\n" % s) + self.pipe.stdin.flush() + def pipe_read_completions(self): + (typ_, value) = self.pipe_readline() + assert typ_ == 'COMPLETIONS', '%r %r' % (typ_, value) + candidates = [x for x in value.split('|') if x] + return candidates + + def clear(self): + self.set_data('', force=True) + def changed(self): + return False + def close(self): + global ipython + ipython = None + def readonly(self): + return True class IpythonBuffer(Buffer): + def get_ipython_name(parent): + if hasattr(parent, 'path'): + return '*IPython:%s*' % parent.name() + else: + return '*IPython*' + get_ipython_name = classmethod(get_ipython_name) btype = 'ipython' modename = 'ipython' readre = re.compile('^([A-Z]+):(.*)\n$') diff --git a/mode/ipythonmini.py b/mode/ipythonmini.py index 6866c41..19c1e78 100644 --- a/mode/ipythonmini.py +++ b/mode/ipythonmini.py @@ -76,7 +76,7 @@ class IpythonPathStart(method.Method): if w.buffer.btype == 'ipython': b = w.buffer else: - name = buffer.get_ipython_name(w.buffer) + name = buffer.IpythonBuffer.get_ipython_name(w.buffer) if not a.has_buffer_name(name): b = buffer.IpythonBuffer(w.buffer) a.add_buffer(b)