From a443cca9ae030389cdf310c65e486b47c518d58d 2008-08-04 18:27:46 From: gvaroquaux Date: 2008-08-04 18:27:46 Subject: [PATCH] Make the wx frontend well-behaved under windows. --- diff --git a/IPython/frontend/linefrontendbase.py b/IPython/frontend/linefrontendbase.py index e18200b..b89d46d 100644 --- a/IPython/frontend/linefrontendbase.py +++ b/IPython/frontend/linefrontendbase.py @@ -19,11 +19,9 @@ import re import IPython - from frontendbase import FrontEndBase from IPython.kernel.core.interpreter import Interpreter - def common_prefix(strings): ref = strings[0] prefix = '' @@ -156,8 +154,9 @@ class LineFrontEndBase(FrontEndBase): if self.is_complete(cleaned_buffer): self.execute(cleaned_buffer, raw_string=current_buffer) else: - self.write(self._get_indent_string(current_buffer[:-1])) - if current_buffer.rstrip().endswith(':'): + self.write(self._get_indent_string( + current_buffer[:-1])) + if current_buffer[:-1].split('\n')[-1].rstrip().endswith(':'): self.write('\t') diff --git a/IPython/frontend/wx/console_widget.py b/IPython/frontend/wx/console_widget.py index e8dc363..aab01d9 100644 --- a/IPython/frontend/wx/console_widget.py +++ b/IPython/frontend/wx/console_widget.py @@ -23,16 +23,17 @@ import wx import wx.stc as stc from wx.py import editwindow +import sys +LINESEP = '\n' +if sys.platform == 'win32': + LINESEP = '\n\r' import re # FIXME: Need to provide an API for non user-generated display on the # screen: this should not be editable by the user. -if wx.Platform == '__WXMSW__': - _DEFAULT_SIZE = 80 -else: - _DEFAULT_SIZE = 10 +_DEFAULT_SIZE = 10 _DEFAULT_STYLE = { 'stdout' : 'fore:#0000FF', @@ -94,7 +95,6 @@ class ConsoleWidget(editwindow.EditWindow): # The color of the carret (call _apply_style() after setting) carret_color = 'BLACK' - #-------------------------------------------------------------------------- # Public API #-------------------------------------------------------------------------- @@ -114,6 +114,8 @@ class ConsoleWidget(editwindow.EditWindow): def configure_scintilla(self): + self.SetEOLMode(stc.STC_EOL_LF) + # Ctrl"+" or Ctrl "-" can be used to zoomin/zoomout the text inside # the widget self.CmdKeyAssign(ord('+'), stc.STC_SCMOD_CTRL, stc.STC_CMD_ZOOMIN) @@ -206,6 +208,7 @@ class ConsoleWidget(editwindow.EditWindow): text = self.title_pat.sub('', text) segments = self.color_pat.split(text) segment = segments.pop(0) + self.GotoPos(self.GetLength()) self.StartStyling(self.GetLength(), 0xFF) self.AppendText(segment) @@ -223,7 +226,7 @@ class ConsoleWidget(editwindow.EditWindow): self.GotoPos(self.GetLength()) wx.Yield() - + def new_prompt(self, prompt): """ Prints a prompt at start of line, and move the start of the @@ -250,8 +253,10 @@ class ConsoleWidget(editwindow.EditWindow): def get_current_edit_buffer(self): """ Returns the text in current edit buffer. """ - return self.GetTextRange(self.current_prompt_pos, - self.GetLength()) + current_edit_buffer = self.GetTextRange(self.current_prompt_pos, + self.GetLength()) + current_edit_buffer = current_edit_buffer.replace(LINESEP, '\n') + return current_edit_buffer #-------------------------------------------------------------------------- @@ -293,7 +298,7 @@ class ConsoleWidget(editwindow.EditWindow): buf.append(symbol.ljust(max_len)) pos += 1 else: - buf.append(symbol.rstrip() +'\n') + buf.append(symbol.rstrip() + '\n') pos = 1 self.write(''.join(buf)) self.new_prompt(self.prompt % (self.last_result['number'] + 1)) @@ -316,12 +321,6 @@ class ConsoleWidget(editwindow.EditWindow): self.ScrollLines(maxrange) - def _on_enter(self): - """ Called when the return key is hit. - """ - pass - - def _on_key_down(self, event, skip=True): """ Key press callback used for correcting behavior for console-like interfaces: the cursor is constraint to be after @@ -354,6 +353,12 @@ class ConsoleWidget(editwindow.EditWindow): catched = True self.CallTipCancel() self.write('\n') + # Under windows scintilla seems to be doing funny stuff to the + # line returns here, but get_current_edit_buffer filters this + # out. + if sys.platform == 'win32': + self.replace_current_edit_buffer( + self.get_current_edit_buffer()) self._on_enter() elif event.KeyCode == wx.WXK_HOME: @@ -384,7 +389,11 @@ class ConsoleWidget(editwindow.EditWindow): catched = True if skip and not catched: - event.Skip() + # Put the cursor back in the edit region + if self.GetCurrentPos() < self.current_prompt_pos: + self.GotoPos(self.current_prompt_pos) + else: + event.Skip() return catched diff --git a/IPython/frontend/wx/wx_frontend.py b/IPython/frontend/wx/wx_frontend.py index c5577c4..75f6132 100644 --- a/IPython/frontend/wx/wx_frontend.py +++ b/IPython/frontend/wx/wx_frontend.py @@ -44,6 +44,8 @@ class WxController(PrefilterFrontEnd, ConsoleWidget): output_prompt = \ '\x01\x1b[0;31m\x02Out[\x01\x1b[1;31m\x02%i\x01\x1b[0;31m\x02]: \x01\x1b[0m\x02' + + debug = True #-------------------------------------------------------------------------- # Public API @@ -68,7 +70,6 @@ class WxController(PrefilterFrontEnd, ConsoleWidget): background=_ERROR_BG) - def do_completion(self): """ Do code completion. """ @@ -240,6 +241,11 @@ class WxController(PrefilterFrontEnd, ConsoleWidget): else: ConsoleWidget._on_key_up(self, event, skip=skip) + def _on_enter(self): + if self.debug: + import sys + print >>sys.__stdout__, repr(self.get_current_edit_buffer()) + PrefilterFrontEnd._on_enter(self) def _set_title(self, title): return self.Parent.SetTitle(title)