import code, re, string, StringIO, sys import buffer, color, completer, lex, method, mode, mode.mini, mode.consolemini, window from subprocess import Popen, PIPE, STDOUT from mode.python import PythonGrammar from point import Point class IpythonExec(method.Method): def _execute(self, w, **vargs): if w.application.completion_window_is_open(): w.application.close_completion_buffer() s = w.buffer.make_string() w.mode.history[-1] = s w.mode.history.append('') w.buffer.set_data('') w.mode.hindex = len(w.mode.history) - 1 b = w.mode.get_ipython() a = w.application if a.window().buffer is not b: a.switch_buffer(b) p = a.get_mini_buffer_prompt() b.insert_string(b.get_buffer_end(), p + s + '\n', force=True) b.pipe_write("ENTER:%s" % s) w.mode.read_sync() class IpythonTab(method.Method): def execute(self, w, **vargs): a = w.application s = w.buffer.make_string() x2 = w.logical_cursor().x if not s or s[:x2].isspace(): w.insert_string_at_cursor(' ' * w.mode.tabwidth) return r = re.compile('^[a-zA-Z0-9_.]$') line = s x1 = x2 while x1 > 0 and r.match(s[x1 - 1]): x1 -= 1 word = line[x1:x2] b = w.mode.get_ipython() b.pipe_write("COMPLETE:%s" % word) candidates = b.pipe_read_completions() w.mode.read_sync() if candidates: s = completer.find_common_string(candidates) w.buffer.delete(Point(x1, 0), Point(x2, 0), force=True) w.insert_string_at_cursor(s) mode.mini.use_completion_window(a, s, candidates) elif a.completion_window_is_open(): a.close_completion_buffer() class IpythonStart(method.Method): '''Evaluate python expressions (for advanced use and debugging only)''' def execute(self, w, **vargs): a = w.application if not a.has_buffer_name('*IPython*'): b = buffer.IpythonBuffer(None) a.add_buffer(b) window.Window(b, a) b = a.bufferlist.get_buffer_by_name('*IPython*') self.main_buffer = b if a.window().buffer is not b: a.switch_buffer(b) f = lambda x: None w.application.open_mini_buffer('*** ', f, self, None, 'ipythonmini') class IpythonPathStart(method.Method): '''xyz''' def execute(self, w, **vargs): a = w.application if w.buffer.btype == 'ipython': b = w.buffer else: name = buffer.get_ipython_name(w.buffer) if not a.has_buffer_name(name): b = buffer.IpythonBuffer(w.buffer) a.add_buffer(b) window.Window(b, a) else: b = a.get_buffer_by_name(name) self.main_buffer = b if a.window().buffer is not b: a.switch_buffer(b) f = lambda x: None w.application.open_mini_buffer('*** ', f, self, None, 'ipythonmini') class IpythonPageUp(mode.consolemini.ConsolePageUp): subbuf = '*IPython*' class IpythonPageDown(mode.consolemini.ConsolePageDown): subbuf = '*IPython*' class IpythonGotoBeginning(mode.consolemini.ConsoleGotoBeginning): subbuf = '*IPython*' class IpythonGotoEnd(mode.consolemini.ConsoleGotoEnd): subbuf = '*IPython*' class IpythonMini(mode.Fundamental): modename = 'IpythonMini' actions = [IpythonExec, IpythonTab, IpythonStart, IpythonPathStart, IpythonPageUp, IpythonPageDown, IpythonGotoBeginning, IpythonGotoEnd] def get_ipython(self): return self.window.buffer.method.main_buffer def read_sync(self): b = self.get_ipython() b.pipe_read() self.window.application.set_mini_buffer_prompt(b.prompt) def __init__(self, w): mode.Fundamental.__init__(self, w) self.history = [''] self.hindex = 0 b = self.window.buffer.method.main_buffer assert hasattr(b, 'pipe') self.window.application.set_mini_buffer_prompt(b.prompt) self.add_bindings('ipython-exec', ('RETURN',)) self.add_bindings('console-clear', ('C-l',)) self.add_bindings('console-cancel', ('C-]',)) self.add_bindings('console-history-prev', ('C-p', 'UP')) self.add_bindings('console-history-next', ('C-n', 'DOWN')) self.add_bindings('ipython-tab', ('TAB',)) self.add_bindings('ipython-page-up', ('M-v',)) self.add_bindings('ipython-page-down', ('C-v',)) self.add_bindings('ipython-goto-beginning', ('M-<',)) self.add_bindings('ipython-goto-end', ('M->',)) for c in string.letters + string.digits + string.punctuation: self.add_binding('insert-string-%s' % c, c) install = IpythonMini.install