diff --git a/IPython/gui/wx/ipython_view.py b/IPython/gui/wx/ipython_view.py index e359f2a..767192e 100644 --- a/IPython/gui/wx/ipython_view.py +++ b/IPython/gui/wx/ipython_view.py @@ -109,7 +109,7 @@ class WxConsoleView(stc.StyledTextCtrl): def __init__(self,parent,prompt,intro="",background_color="BLACK", pos=wx.DefaultPosition, ID = -1, size=wx.DefaultSize, - style=0): + style=0, autocomplete_mode = 'IPYTHON'): ''' Initialize console view. @@ -121,6 +121,9 @@ class WxConsoleView(stc.StyledTextCtrl): @param background_color: Can be BLACK or WHITE @type background_color: string @param other: init param of styledTextControl (can be used as-is) + @param autocomplete_mode: Can be 'IPYTHON' or 'STC' + 'IPYTHON' show autocompletion the ipython way + 'STC" show it scintilla text control way ''' stc.StyledTextCtrl.__init__(self, parent, ID, pos, size, style) @@ -212,9 +215,11 @@ class WxConsoleView(stc.StyledTextCtrl): self.write(intro) self.setPrompt(prompt) self.showPrompt() + + self.autocomplete_mode = autocomplete_mode self.Bind(wx.EVT_KEY_DOWN, self._onKeypress, self) - + def asyncWrite(self, text): ''' Write given text to buffer in an asynchroneous way. @@ -353,33 +358,52 @@ class WxConsoleView(stc.StyledTextCtrl): def writeHistory(self,history): self.removeFromTo(self.getCurrentPromptStart(),self.getCurrentLineEnd()) self.changeLine(history) + + def setCompletionMethod(self, completion): + if completion in ['IPYTHON','STC']: + self.autocomplete_mode = completion + else: + raise AttributeError + + def getCompletionMethod(self, completion): + return self.autocomplete_mode def writeCompletion(self, possibilities): - max_len = len(max(possibilities,key=len)) - max_symbol =' '*max_len - - #now we check how much symbol we can put on a line... - cursor_pos = self.getCursorPos() - test_buffer = max_symbol + ' '*4 - current_lines = self.GetLineCount() - - allowed_symbols = 80/len(test_buffer) - if allowed_symbols == 0: - allowed_symbols = 1 + if self.autocomplete_mode == 'IPYTHON': + max_len = len(max(possibilities,key=len)) + max_symbol =' '*max_len + + #now we check how much symbol we can put on a line... + cursor_pos = self.getCursorPos() + test_buffer = max_symbol + ' '*4 + current_lines = self.GetLineCount() + + allowed_symbols = 80/len(test_buffer) + if allowed_symbols == 0: + allowed_symbols = 1 + + pos = 1 + buf = '' + for symbol in possibilities: + #buf += symbol+'\n'#*spaces) + if pos self.getCurrentPromptStart(): + event.Skip() return True - - elif event.GetKeyCode() == wx.WXK_BACK: - self.moveCursorOnNewValidKey() - if self.getCursorPos() > self.getCurrentPromptStart(): + + if skip: + if event.GetKeyCode() not in [wx.WXK_PAGEUP,wx.WXK_PAGEDOWN] and event.Modifiers == wx.MOD_NONE: + self.moveCursorOnNewValidKey() + event.Skip() - return True - - if skip: - if event.GetKeyCode() not in [wx.WXK_PAGEUP,wx.WXK_PAGEDOWN] and event.Modifiers == wx.MOD_NONE: - self.moveCursorOnNewValidKey() - + return True + return False + else: event.Skip() - return True - return False - + def OnUpdateUI(self, evt): # check for matching braces braceAtCaret = -1 @@ -515,12 +543,24 @@ class IPShellWidget(wx.Panel): background_color=background_color) self.cout.write = self.text_ctrl.asyncWrite + + self.completion_option = wx.CheckBox(self, -1, "Scintilla completion") + self.completion_option.SetValue(False) + option_text = wx.StaticText(self,-1,'Options:') self.text_ctrl.Bind(wx.EVT_KEY_DOWN, self.keyPress) - + self.Bind(wx.EVT_CHECKBOX, self.evtCheckOptionCompletion, self.completion_option) + ### making the layout of the panel ### sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(self.text_ctrl, 1, wx.EXPAND) + option_sizer = wx.BoxSizer(wx.HORIZONTAL) + sizer.Add(option_sizer,0) + option_sizer.AddMany([(10,15), + option_text, + (20,15), + self.completion_option + ]) self.SetAutoLayout(True) sizer.Fit(self) sizer.SetSizeHints(self) @@ -630,12 +670,20 @@ class IPShellWidget(wx.Panel): autocompletions, etc. ''' if event.GetKeyCode() == ord('C'): - if event.Modifiers == wx.MOD_CONTROL: + if event.Modifiers == wx.MOD_CONTROL or event.Modifiers == wx.MOD_ALT: if self.cur_state == 'WAIT_END_OF_EXECUTION': #we raise an exception inside the IPython thread container self.IP.ce.raise_exc(KeyboardInterrupt) return + #let this before 'wx.WXK_RETURN' because we have to put 'IDLE' + #mode if AutoComp has been set as inactive + if self.cur_state == 'COMPLETING': + if not self.text_ctrl.AutoCompActive(): + self.cur_state = 'IDLE' + else: + event.Skip() + if event.KeyCode == wx.WXK_RETURN: if self.cur_state == 'IDLE': #we change the state ot the state machine @@ -663,7 +711,7 @@ class IPShellWidget(wx.Panel): if self.cur_state == 'WAITING_USER_INPUT': event.Skip() - + if self.cur_state == 'IDLE': if event.KeyCode == wx.WXK_UP: history = self.IP.historyBack() @@ -680,17 +728,30 @@ class IPShellWidget(wx.Panel): return completed, possibilities = self.IP.complete(self.text_ctrl.getCurrentLine()) if len(possibilities) > 1: - cur_slice = self.text_ctrl.getCurrentLine() - self.text_ctrl.write('\n') - self.text_ctrl.writeCompletion(possibilities) - self.text_ctrl.write('\n') - - self.text_ctrl.showPrompt() - self.text_ctrl.write(cur_slice) - self.text_ctrl.changeLine(completed or cur_slice) - + if self.text_ctrl.autocomplete_mode == 'IPYTHON': + cur_slice = self.text_ctrl.getCurrentLine() + self.text_ctrl.write('\n') + self.text_ctrl.writeCompletion(possibilities) + self.text_ctrl.write('\n') + + self.text_ctrl.showPrompt() + self.text_ctrl.write(cur_slice) + self.text_ctrl.changeLine(completed or cur_slice) + else: + self.cur_state = 'COMPLETING' + self.text_ctrl.writeCompletion(possibilities) + else: + self.text_ctrl.changeLine(completed or cur_slice) return event.Skip() + + #------------------------ Option Section --------------------------------- + def evtCheckOptionCompletion(self, event): + if event.IsChecked(): + self.text_ctrl.setCompletionMethod('STC') + else: + self.text_ctrl.setCompletionMethod('IPYTHON') + self.text_ctrl.SetFocus() #------------------------ Hook Section ----------------------------------- def updateHistoryTracker(self,command_line):