From c2fab0bd95fbd840d353c1753c730bd27f1ea6c2 2009-03-30 00:15:21 From: Gael Varoquaux Date: 2009-03-30 00:15:21 Subject: [PATCH] ENH: Backspace now deletes continuation lines --- diff --git a/IPython/frontend/wx/console_widget.py b/IPython/frontend/wx/console_widget.py index c63bc19..35c2d9c 100644 --- a/IPython/frontend/wx/console_widget.py +++ b/IPython/frontend/wx/console_widget.py @@ -242,7 +242,9 @@ class ConsoleWidget(editwindow.EditWindow): def continuation_prompt(self): - """Returns the current continuation prompt. + """ Returns the current continuation prompt. + We need to implement this method here to deal with the + ascii escape sequences cleaning up. """ # ASCII-less prompt ascii_less = ''.join(self.color_pat.split(self.last_prompt)[2::2]) @@ -271,21 +273,6 @@ class ConsoleWidget(editwindow.EditWindow): return self.GetSize()[0]/self.GetCharWidth() - - #-------------------------------------------------------------------------- - # EditWindow API - #-------------------------------------------------------------------------- - - def OnUpdateUI(self, event): - """ Override the OnUpdateUI of the EditWindow class, to prevent - syntax highlighting both for faster redraw, and for more - consistent look and feel. - """ - - #-------------------------------------------------------------------------- - # Styling API - #-------------------------------------------------------------------------- - def configure_scintilla(self): p = self.style @@ -461,7 +448,18 @@ class ConsoleWidget(editwindow.EditWindow): self.SetEdgeMode(stc.STC_EDGE_LINE) self.SetEdgeColumn(88) - + + #-------------------------------------------------------------------------- + # EditWindow API + #-------------------------------------------------------------------------- + + def OnUpdateUI(self, event): + """ Override the OnUpdateUI of the EditWindow class, to prevent + syntax highlighting both for faster redraw, and for more + consistent look and feel. + """ + + #-------------------------------------------------------------------------- # Private API #-------------------------------------------------------------------------- @@ -572,6 +570,7 @@ class ConsoleWidget(editwindow.EditWindow): self._keep_cursor_in_buffer() + # XXX: I need to avoid the problem of having an empty glass; def _keep_cursor_in_buffer(self, pos=None): """ Checks if the cursor is where it is allowed to be. If not, put it back. @@ -602,7 +601,7 @@ class ConsoleWidget(editwindow.EditWindow): # Jump the continuation prompt continuation_prompt = self.continuation_prompt() if ( line.startswith(continuation_prompt) - and line_pos < len(continuation_prompt)+1): + and line_pos < len(continuation_prompt)): if line_pos < 2: # We are at the beginning of the line, trying to move # forward: jump forward. diff --git a/IPython/frontend/wx/wx_frontend.py b/IPython/frontend/wx/wx_frontend.py index cdb6742..e6745d5 100644 --- a/IPython/frontend/wx/wx_frontend.py +++ b/IPython/frontend/wx/wx_frontend.py @@ -459,8 +459,36 @@ class WxController(ConsoleWidget, PrefilterFrontEnd): wx.CallAfter(self._popup_completion, create=True) else: event.Skip() + elif event.KeyCode == wx.WXK_BACK: + # If characters where erased, check if we have to + # remove a line. + # XXX: What about DEL? + current_line, _ = self.CurLine + current_pos = self.GetCurrentPos() + current_line_number = self.LineFromPosition(current_pos) + current_col = self.GetColumn(current_pos) + len_prompt = len(self.continuation_prompt()) + if ( current_line.startswith(self.continuation_prompt()) + and current_col == len_prompt): + print 'BACK', current_line, self.current_prompt_line, \ + current_line_number + new_lines = [] + for line_num, line in enumerate( + self.input_buffer.split('\n')): + if (line_num + self.current_prompt_line == + current_line_number): + new_lines.append(line[len_prompt:]) + else: + new_lines.append('\n'+line) + # The first character is '\n', due to the above + # code: + self.input_buffer = ''.join(new_lines)[1:] + self.GotoPos(current_pos - 1 - len_prompt) + else: + ConsoleWidget._on_key_down(self, event, skip=skip) else: ConsoleWidget._on_key_down(self, event, skip=skip) + def _on_key_up(self, event, skip=True): @@ -472,11 +500,15 @@ class WxController(ConsoleWidget, PrefilterFrontEnd): wx.CallAfter(self._popup_completion, create=True) else: ConsoleWidget._on_key_up(self, event, skip=skip) - if (self.input_buffer.split('\n')[-1] == self.continuation_prompt() - and self._input_state == 'readline'): - # Make sure the continuation_prompt is followed by a whitespace + # Make sure the continuation_prompts are always followed by a + # whitespace + new_lines = [] + if self._input_state == 'readline': position = self.GetCurrentPos() - self.input_buffer += ' ' + for line in self.input_buffer.split('\n'): + if not line == self.continuation_prompt()[:-1]: + new_lines.append(line) + self.input_buffer = '\n'.join(new_lines) self.GotoPos(position)