Esempio n. 1
0
 def _on_key_down(self, event, skip=True):
     """ Capture the character events, let the parent
         widget handle them, and put our logic afterward.
     """
     # FIXME: This method needs to be broken down in smaller ones.
     current_line_number = self.GetCurrentLine()
     if event.KeyCode in (ord('c'), ord('C')) and event.ControlDown():
         # Capture Control-C
         if self._input_state == 'subprocess':
             if self.debug:
                 print >> sys.__stderr__, 'Killing running process'
             if hasattr(self._running_process, 'process'):
                 self._running_process.process.kill()
         elif self._input_state == 'buffering':
             if self.debug:
                 print >> sys.__stderr__, 'Raising KeyboardInterrupt'
             raise KeyboardInterrupt
             # XXX: We need to make really sure we
             # get back to a prompt.
     elif self._input_state == 'subprocess' and (
         (event.KeyCode < 256 and not event.ControlDown()) or
         (event.KeyCode in (ord('d'), ord('D')) and event.ControlDown())):
         #  We are running a process, we redirect keys.
         ConsoleWidget._on_key_down(self, event, skip=skip)
         char = chr(event.KeyCode)
         # Deal with some inconsistency in wx keycodes:
         if char == '\r':
             char = '\n'
         elif not event.ShiftDown():
             char = char.lower()
         if event.ControlDown() and event.KeyCode in (ord('d'), ord('D')):
             char = '\04'
         self._running_process.process.stdin.write(char)
         self._running_process.process.stdin.flush()
     elif event.KeyCode in (ord('('), 57, 53):
         # Calltips
         event.Skip()
         self.do_calltip()
     elif self.AutoCompActive() and not event.KeyCode == ord('\t'):
         event.Skip()
         if event.KeyCode in (wx.WXK_BACK, wx.WXK_DELETE):
             wx.CallAfter(self._popup_completion, create=True)
         elif not event.KeyCode in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT,
                                    wx.WXK_RIGHT, wx.WXK_ESCAPE):
             wx.CallAfter(self._popup_completion)
     else:
         # Up history
         if event.KeyCode == wx.WXK_UP and (
             (current_line_number == self.current_prompt_line
              and event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN))
                 or event.ControlDown()):
             new_buffer = self.get_history_previous(self.input_buffer)
             if new_buffer is not None:
                 self.input_buffer = new_buffer
                 if self.GetCurrentLine() > self.current_prompt_line:
                     # Go to first line, for seemless history up.
                     self.GotoPos(self.current_prompt_pos)
         # Down history
         elif event.KeyCode == wx.WXK_DOWN and (
             (current_line_number == self.LineCount - 1 and event.Modifiers
              in (wx.MOD_NONE, wx.MOD_WIN)) or event.ControlDown()):
             new_buffer = self.get_history_next()
             if new_buffer is not None:
                 self.input_buffer = new_buffer
         # Tab-completion
         elif event.KeyCode == ord('\t'):
             current_line, current_line_number = self.CurLine
             if not re.match(r'^\s*$', current_line):
                 self.complete_current_input()
                 if self.AutoCompActive():
                     wx.CallAfter(self._popup_completion, create=True)
             else:
                 event.Skip()
         else:
             ConsoleWidget._on_key_down(self, event, skip=skip)
Esempio n. 2
0
 def _on_key_down(self, event, skip=True):
     """ Capture the character events, let the parent
         widget handle them, and put our logic afterward.
     """
     # FIXME: This method needs to be broken down in smaller ones.
     current_line_number = self.GetCurrentLine()
     if event.KeyCode in (ord("c"), ord("C")) and event.ControlDown():
         # Capture Control-C
         if self._input_state == "subprocess":
             if self.debug:
                 print >>sys.__stderr__, "Killing running process"
             if hasattr(self._running_process, "process"):
                 self._running_process.process.kill()
         elif self._input_state == "buffering":
             if self.debug:
                 print >>sys.__stderr__, "Raising KeyboardInterrupt"
             raise KeyboardInterrupt
             # XXX: We need to make really sure we
             # get back to a prompt.
     elif self._input_state == "subprocess" and (
         (event.KeyCode < 256 and not event.ControlDown())
         or (event.KeyCode in (ord("d"), ord("D")) and event.ControlDown())
     ):
         #  We are running a process, we redirect keys.
         ConsoleWidget._on_key_down(self, event, skip=skip)
         char = chr(event.KeyCode)
         # Deal with some inconsistency in wx keycodes:
         if char == "\r":
             char = "\n"
         elif not event.ShiftDown():
             char = char.lower()
         if event.ControlDown() and event.KeyCode in (ord("d"), ord("D")):
             char = "\04"
         self._running_process.process.stdin.write(char)
         self._running_process.process.stdin.flush()
     elif event.KeyCode in (ord("("), 57, 53):
         # Calltips
         event.Skip()
         self.do_calltip()
     elif self.AutoCompActive() and not event.KeyCode == ord("\t"):
         event.Skip()
         if event.KeyCode in (wx.WXK_BACK, wx.WXK_DELETE):
             wx.CallAfter(self._popup_completion, create=True)
         elif not event.KeyCode in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT, wx.WXK_RIGHT, wx.WXK_ESCAPE):
             wx.CallAfter(self._popup_completion)
     else:
         # Up history
         if event.KeyCode == wx.WXK_UP and (
             (current_line_number == self.current_prompt_line and event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN))
             or event.ControlDown()
         ):
             new_buffer = self.get_history_previous(self.input_buffer)
             if new_buffer is not None:
                 self.input_buffer = new_buffer
                 if self.GetCurrentLine() > self.current_prompt_line:
                     # Go to first line, for seemless history up.
                     self.GotoPos(self.current_prompt_pos)
         # Down history
         elif event.KeyCode == wx.WXK_DOWN and (
             (current_line_number == self.LineCount - 1 and event.Modifiers in (wx.MOD_NONE, wx.MOD_WIN))
             or event.ControlDown()
         ):
             new_buffer = self.get_history_next()
             if new_buffer is not None:
                 self.input_buffer = new_buffer
         # Tab-completion
         elif event.KeyCode == ord("\t"):
             current_line, current_line_number = self.CurLine
             if not re.match(r"^\s*$", current_line):
                 self.complete_current_input()
                 if self.AutoCompActive():
                     wx.CallAfter(self._popup_completion, create=True)
             else:
                 event.Skip()
         else:
             ConsoleWidget._on_key_down(self, event, skip=skip)
Esempio n. 3
0
 def _on_key_down(self, event, skip=True):
     """ Capture the character events, let the parent
         widget handle them, and put our logic afterward.
     """
     # FIXME: This method needs to be broken down in smaller ones.
     current_line_num = self.GetCurrentLine()
     key_code = event.GetKeyCode()
     if key_code in (ord('c'), ord('C')) and event.ControlDown():
         # Capture Control-C
         if self._input_state == 'subprocess':
             if self.debug:
                 print >> sys.__stderr__, 'Killing running process'
             if hasattr(self._running_process, 'process'):
                 self._running_process.process.kill()
         elif self._input_state == 'buffering':
             if self.debug:
                 print >> sys.__stderr__, 'Raising KeyboardInterrupt'
             raise KeyboardInterrupt
             # XXX: We need to make really sure we
             # get back to a prompt.
     elif self._input_state == 'subprocess' and (
         (key_code < 256 and not event.ControlDown()) or
         (key_code in (ord('d'), ord('D')) and event.ControlDown())):
         #  We are running a process, we redirect keys.
         ConsoleWidget._on_key_down(self, event, skip=skip)
         char = chr(key_code)
         # Deal with some inconsistency in wx keycodes:
         if char == '\r':
             char = '\n'
         elif not event.ShiftDown():
             char = char.lower()
         if event.ControlDown() and key_code in (ord('d'), ord('D')):
             char = '\04'
         self._running_process.process.stdin.write(char)
         self._running_process.process.stdin.flush()
     elif key_code in (ord('('), 57, 53):
         # Calltips
         event.Skip()
         self.do_calltip()
     elif self.AutoCompActive() and not key_code == ord('\t'):
         event.Skip()
         if key_code in (wx.WXK_BACK, wx.WXK_DELETE):
             wx.CallAfter(self._popup_completion, create=True)
         elif not key_code in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT,
                               wx.WXK_RIGHT, wx.WXK_ESCAPE):
             wx.CallAfter(self._popup_completion)
     else:
         # Up history
         if key_code == wx.WXK_UP and (event.ControlDown()
                                       or current_line_num
                                       == self.current_prompt_line):
             new_buffer = self.get_history_previous(self.input_buffer)
             if new_buffer is not None:
                 self.input_buffer = new_buffer
                 if self.GetCurrentLine() > self.current_prompt_line:
                     # Go to first line, for seemless history up.
                     self.GotoPos(self.current_prompt_pos)
         # Down history
         elif key_code == wx.WXK_DOWN and (event.ControlDown()
                                           or current_line_num
                                           == self.LineCount - 1):
             new_buffer = self.get_history_next()
             if new_buffer is not None:
                 self.input_buffer = new_buffer
         # Tab-completion
         elif key_code == ord('\t'):
             current_line, current_line_num = self.CurLine
             if not re.match(r'^%s\s*$' % self.continuation_prompt(),
                             current_line):
                 self.complete_current_input()
                 if self.AutoCompActive():
                     wx.CallAfter(self._popup_completion, create=True)
             else:
                 event.Skip()
         elif key_code == wx.WXK_BACK:
             # If characters where erased, check if we have to
             # remove a line.
             # XXX: What about DEL?
             # FIXME: This logics should be in ConsoleWidget, as it is
             # independant of IPython
             current_line, _ = self.CurLine
             current_pos = self.GetCurrentPos()
             current_line_num = 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):
                 new_lines = []
                 for line_num, line in enumerate(
                         self.input_buffer.split('\n')):
                     if (line_num +
                             self.current_prompt_line == current_line_num):
                         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)
Esempio n. 4
0
 def _on_key_down(self, event, skip=True):
     """ Capture the character events, let the parent
         widget handle them, and put our logic afterward.
     """
     # FIXME: This method needs to be broken down in smaller ones.
     current_line_num = self.GetCurrentLine()
     key_code = event.GetKeyCode()
     if key_code in (ord('c'), ord('C')) and event.ControlDown():
         # Capture Control-C
         if self._input_state == 'subprocess':
             if self.debug:
                 print >>sys.__stderr__, 'Killing running process'
             if hasattr(self._running_process, 'process'):
                 self._running_process.process.kill()
         elif self._input_state == 'buffering':
             if self.debug:
                 print >>sys.__stderr__, 'Raising KeyboardInterrupt'
             raise KeyboardInterrupt
             # XXX: We need to make really sure we
             # get back to a prompt.
     elif self._input_state == 'subprocess' and (
             ( key_code <256 and not event.ControlDown() )
                 or 
             ( key_code in (ord('d'), ord('D')) and
               event.ControlDown())):
         #  We are running a process, we redirect keys.
         ConsoleWidget._on_key_down(self, event, skip=skip)
         char = chr(key_code)
         # Deal with some inconsistency in wx keycodes:
         if char == '\r':
             char = '\n'
         elif not event.ShiftDown():
             char = char.lower()
         if event.ControlDown() and key_code in (ord('d'), ord('D')):
             char = '\04'
         self._running_process.process.stdin.write(char)
         self._running_process.process.stdin.flush()
     elif key_code in (ord('('), 57, 53):
         # Calltips
         event.Skip()
         self.do_calltip()
     elif self.AutoCompActive() and not key_code == ord('\t'):
         event.Skip()
         if key_code in (wx.WXK_BACK, wx.WXK_DELETE): 
             wx.CallAfter(self._popup_completion, create=True)
         elif not key_code in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT,
                         wx.WXK_RIGHT, wx.WXK_ESCAPE):
             wx.CallAfter(self._popup_completion)
     else:
         # Up history
         if key_code == wx.WXK_UP and (
                         event.ControlDown() or
                         current_line_num == self.current_prompt_line
                 ):
             new_buffer = self.get_history_previous(
                                         self.input_buffer)
             if new_buffer is not None:
                 self.input_buffer = new_buffer
                 if self.GetCurrentLine() > self.current_prompt_line:
                     # Go to first line, for seemless history up.
                     self.GotoPos(self.current_prompt_pos)
         # Down history
         elif key_code == wx.WXK_DOWN and (
                         event.ControlDown() or
                         current_line_num == self.LineCount -1
                 ):
             new_buffer = self.get_history_next()
             if new_buffer is not None:
                 self.input_buffer = new_buffer
         # Tab-completion
         elif key_code == ord('\t'):
             current_line, current_line_num = self.CurLine
             if not re.match(r'^%s\s*$' % self.continuation_prompt(), 
                                                         current_line):
                 self.complete_current_input()
                 if self.AutoCompActive():
                     wx.CallAfter(self._popup_completion, create=True)
             else:
                 event.Skip()
         elif key_code == wx.WXK_BACK:
             # If characters where erased, check if we have to
             # remove a line.
             # XXX: What about DEL?
             # FIXME: This logics should be in ConsoleWidget, as it is
             # independant of IPython
             current_line, _ = self.CurLine
             current_pos = self.GetCurrentPos()
             current_line_num = 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):
                 new_lines = []
                 for line_num, line in enumerate(
                                 self.input_buffer.split('\n')):
                     if (line_num + self.current_prompt_line ==
                                         current_line_num):
                         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)