##// END OF EJS Templates
Merge pull request #3089 from minrk/stdin...
Merge pull request #3089 from minrk/stdin add stdin to the notebook

File last commit:

r5390:c82649ea
r10380:c3a90443 merge
Show More
wx_frontend.py
602 lines | 23.1 KiB | text/x-python | PythonLexer
Gael Varoquaux
Update wx frontend.
r1349 # encoding: utf-8 -*- test-case-name:
# FIXME: Need to add tests.
gvaroquaux
More tests....
r1460 # ipython1.frontend.wx.tests.test_wx_frontend -*-
Gael Varoquaux
Update wx frontend.
r1349
"""Classes to provide a Wx frontend to the
Gael Varoquaux
Traceback capture now working.
r1360 IPython.kernel.core.interpreter.
Gael Varoquaux
Update wx frontend.
r1349
gvaroquaux
Clean up code, names, and docstrings.
r1455 This class inherits from ConsoleWidget, that provides a console-like
widget to provide a text-rendering widget suitable for a terminal.
Gael Varoquaux
Update wx frontend.
r1349 """
__docformat__ = "restructuredtext en"
#-------------------------------------------------------------------------------
Matthias BUSSONNIER
update copyright to 2011/20xx-2011...
r5390 # Copyright (C) 2008-2011 The IPython Development Team
Gael Varoquaux
Update wx frontend.
r1349 #
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# Imports
#-------------------------------------------------------------------------------
gvaroquaux
Clean up code, names, and docstrings.
r1455 # Major library imports
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 import re
Gael Varoquaux
Calltips are now working.
r1375 import __builtin__
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 import sys
from threading import Lock
gvaroquaux
Clean up code, names, and docstrings.
r1455 import wx
from wx import stc
# Ipython-specific imports.
Gael Varoquaux
Take in account remarks by Fernando on code review
r1947 from IPython.frontend.process import PipedProcess
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 from console_widget import ConsoleWidget, _COMPLETE_BUFFER_MARKER, \
_ERROR_MARKER, _INPUT_MARKER
Gael Varoquaux
Traceback capture now working.
r1360 from IPython.frontend.prefilterfrontend import PrefilterFrontEnd
Gael Varoquaux
Update wx frontend.
r1349
gvaroquaux
Clean up code, names, and docstrings.
r1455 #-------------------------------------------------------------------------------
Gael Varoquaux
Update wx frontend.
r1349 # Classes to implement the Wx frontend
#-------------------------------------------------------------------------------
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 class WxController(ConsoleWidget, PrefilterFrontEnd):
gvaroquaux
Clean up code, names, and docstrings.
r1455 """Classes to provide a Wx frontend to the
IPython.kernel.core.interpreter.
This class inherits from ConsoleWidget, that provides a console-like
widget to provide a text-rendering widget suitable for a terminal.
"""
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # Print debug info on what is happening to the console.
gvaroquaux
Clean up the test application for the wx frontend.
r1475 debug = False
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # The title of the terminal, as captured through the ANSI escape
# sequences.
def _set_title(self, title):
return self.Parent.SetTitle(title)
def _get_title(self):
return self.Parent.GetTitle()
title = property(_get_title, _set_title)
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462
# The buffer being edited.
Gael Varoquaux
Add color feedback for multiline entry.
r1473 # We are duplicating the definition here because of multiple
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 # inheritence
def _set_input_buffer(self, string):
Gael Varoquaux
Add color feedback for multiline entry.
r1473 ConsoleWidget._set_input_buffer(self, string)
self._colorize_input_buffer()
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462
def _get_input_buffer(self):
""" Returns the text in current edit buffer.
"""
return ConsoleWidget._get_input_buffer(self)
input_buffer = property(_get_input_buffer, _set_input_buffer)
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 #--------------------------------------------------------------------------
# Private Attributes
#--------------------------------------------------------------------------
# A flag governing the behavior of the input. Can be:
#
Bernardo B. Marques
remove all trailling spaces
r4872 # 'readline' for readline-like behavior with a prompt
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # and an edit buffer.
gvaroquaux
Make help() more robust.
r1480 # 'raw_input' similar to readline, but triggered by a raw-input
# call. Can be used by subclasses to act differently.
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # 'subprocess' for sending the raw input directly to a
# subprocess.
# 'buffering' for buffering of the input, that will be used
# when the input state switches back to another state.
_input_state = 'readline'
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # Attribute to store reference to the pipes of a subprocess, if we
# are running any.
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _running_process = False
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
# A queue for writing fast streams to the screen without flooding the
# event loop
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _out_buffer = []
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # A lock to lock the _out_buffer to make sure we don't empty it
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # while it is being swapped
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 _out_buffer_lock = Lock()
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
Gael Varoquaux
Fix a completion crasher (index error during completion)....
r1624 # The different line markers used to higlight the prompts.
Gael Varoquaux
Isolate the displayhook created by ipython0. This fixes a test not...
r1472 _markers = dict()
Gael Varoquaux
Update wx frontend.
r1349 #--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872
Gael Varoquaux
Update wx frontend.
r1349 def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
gvaroquaux
Misspelling in a docstring.
r1630 size=wx.DefaultSize,
style=wx.CLIP_CHILDREN|wx.WANTS_CHARS,
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 styledef=None,
Gael Varoquaux
Update wx frontend.
r1349 *args, **kwds):
""" Create Shell instance.
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893
Parameters
-----------
styledef : dict, optional
styledef is the dictionary of options used to define the
style.
Gael Varoquaux
Update wx frontend.
r1349 """
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 if styledef is not None:
self.style = styledef
Gael Varoquaux
Update wx frontend.
r1349 ConsoleWidget.__init__(self, parent, id, pos, size, style)
gvaroquaux
Tweak the debug mode....
r1484 PrefilterFrontEnd.__init__(self, **kwds)
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Fix raw_input.
r1654 # Stick in our own raw_input:
self.ipython0.raw_input = self.raw_input
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # A time for flushing the write buffer
BUFFER_FLUSH_TIMER_ID = 100
self._buffer_flush_timer = wx.Timer(self, BUFFER_FLUSH_TIMER_ID)
wx.EVT_TIMER(self, BUFFER_FLUSH_TIMER_ID, self._buffer_flush)
Gael Varoquaux
Nice background color for already entered code....
r1374
gvaroquaux
Tweak the debug mode....
r1484 if 'debug' in kwds:
self.debug = kwds['debug']
kwds.pop('debug')
Gael Varoquaux
Isolate the displayhook created by ipython0. This fixes a test not...
r1472 # Inject self in namespace, for debug
if self.debug:
self.shell.user_ns['self'] = self
gvaroquaux
Fix raw_input.
r1654 # Inject our own raw_input in namespace
self.shell.user_ns['raw_input'] = self.raw_input
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Fix bug in raw_input that was requiring a manditory argument, whereas it...
r1660 def raw_input(self, prompt=''):
gvaroquaux
Clean up code, names, and docstrings.
r1455 """ A replacement from python's raw_input.
"""
self.new_prompt(prompt)
gvaroquaux
Make help() more robust.
r1480 self._input_state = 'raw_input'
Gael Varoquaux
Fix traceback on exit.
r1489 if hasattr(self, '_cursor'):
Bernardo B. Marques
remove all trailling spaces
r4872 del self._cursor
gvaroquaux
Tweak the debug mode....
r1484 self.SetCursor(wx.StockCursor(wx.CURSOR_CROSS))
gvaroquaux
Clean up code, names, and docstrings.
r1455 self.__old_on_enter = self._on_enter
gvaroquaux
Trying a different approach for raw_input: starting a second event loop...
r1655 event_loop = wx.EventLoop()
gvaroquaux
Clean up code, names, and docstrings.
r1455 def my_on_enter():
gvaroquaux
Trying a different approach for raw_input: starting a second event loop...
r1655 event_loop.Exit()
gvaroquaux
Clean up code, names, and docstrings.
r1455 self._on_enter = my_on_enter
gvaroquaux
Trying a different approach for raw_input: starting a second event loop...
r1655 # XXX: Running a separate event_loop. Ugly.
Bernardo B. Marques
remove all trailling spaces
r4872 event_loop.Run()
gvaroquaux
Clean up code, names, and docstrings.
r1455 self._on_enter = self.__old_on_enter
self._input_state = 'buffering'
gvaroquaux
Tweak the debug mode....
r1484 self._cursor = wx.BusyCursor()
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 return self.input_buffer.rstrip('\n')
gvaroquaux
Clean up code, names, and docstrings.
r1455
def system_call(self, command_string):
self._input_state = 'subprocess'
gvaroquaux
Apply the same hack used for raw_input to system calls.
r1656 event_loop = wx.EventLoop()
def _end_system_call():
self._input_state = 'buffering'
self._running_process = False
event_loop.Exit()
Bernardo B. Marques
remove all trailling spaces
r4872 self._running_process = PipedProcess(command_string,
gvaroquaux
Clean up code, names, and docstrings.
r1455 out_callback=self.buffered_write,
gvaroquaux
Apply the same hack used for raw_input to system calls.
r1656 end_callback = _end_system_call)
gvaroquaux
Clean up code, names, and docstrings.
r1455 self._running_process.start()
gvaroquaux
Apply the same hack used for raw_input to system calls.
r1656 # XXX: Running a separate event_loop. Ugly.
Bernardo B. Marques
remove all trailling spaces
r4872 event_loop.Run()
gvaroquaux
Clean up code, names, and docstrings.
r1455 # Be sure to flush the buffer.
self._buffer_flush(event=None)
Gael Varoquaux
Calltips are now working.
r1375 def do_calltip(self):
gvaroquaux
Clean up code, names, and docstrings.
r1455 """ Analyse current and displays useful calltip for it.
"""
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
Bernardo B. Marques
remove all trailling spaces
r4872 print >>sys.__stdout__, "do_calltip"
Gael Varoquaux
More calltip fixing.
r1382 separators = re.compile('[\s\{\}\[\]\(\)\= ,:]')
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 symbol = self.input_buffer
Gael Varoquaux
More calltip fixing.
r1382 symbol_string = separators.split(symbol)[-1]
Gael Varoquaux
Calltips are now working.
r1375 base_symbol_string = symbol_string.split('.')[0]
if base_symbol_string in self.shell.user_ns:
symbol = self.shell.user_ns[base_symbol_string]
elif base_symbol_string in self.shell.user_global_ns:
symbol = self.shell.user_global_ns[base_symbol_string]
elif base_symbol_string in __builtin__.__dict__:
symbol = __builtin__.__dict__[base_symbol_string]
else:
return False
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 try:
gvaroquaux
Tweak the debug mode....
r1484 for name in symbol_string.split('.')[1:] + ['__doc__']:
symbol = getattr(symbol, name)
Gael Varoquaux
Tweak calltip code.
r1381 self.AutoCompCancel()
gvaroquaux
Catter for empty docstrings (bug introduced 2 checkins ago).
r1659 # Check that the symbol can indeed be converted to a string:
symbol += ''
gvaroquaux
Remove one more Yield, to make the frontend more robust under Vista.
r1657 wx.CallAfter(self.CallTipShow, self.GetCurrentPos(), symbol)
Gael Varoquaux
More calltip fixing.
r1382 except:
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 # The retrieve symbol couldn't be converted to a string
pass
Gael Varoquaux
Calltips are now working.
r1375
Gael Varoquaux
Proper tab completion that actually completes.
r1379
gvaroquaux
Clean up code, names, and docstrings.
r1455 def _popup_completion(self, create=False):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Updates the popup completion menu if it exists. If create is
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 true, open the menu.
"""
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
Bernardo B. Marques
remove all trailling spaces
r4872 print >>sys.__stdout__, "_popup_completion"
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 line = self.input_buffer
Gael Varoquaux
Fix a completion crasher (index error during completion)....
r1624 if (self.AutoCompActive() and line and not line[-1] == '.') \
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 or create==True:
suggestion, completions = self.complete(line)
if completions:
Gael Varoquaux
BUG: Integrate bug fixes from Enthought
r1887 offset = len(self._get_completion_text(line))
Gael Varoquaux
Tweak calltip code.
r1381 self.pop_completion(completions, offset=offset)
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 if self.debug:
Bernardo B. Marques
remove all trailling spaces
r4872 print >>sys.__stdout__, completions
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
gvaroquaux
Clean up code, names, and docstrings.
r1455 def buffered_write(self, text):
""" A write method for streams, that caches the stream in order
to avoid flooding the event loop.
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447
gvaroquaux
Clean up code, names, and docstrings.
r1455 This can be called outside of the main loop, in separate
threads.
Gael Varoquaux
Deal with raw_input.
r1388 """
gvaroquaux
Clean up code, names, and docstrings.
r1455 self._out_buffer_lock.acquire()
self._out_buffer.append(text)
self._out_buffer_lock.release()
if not self._buffer_flush_timer.IsRunning():
Bernardo B. Marques
remove all trailling spaces
r4872 wx.CallAfter(self._buffer_flush_timer.Start,
gvaroquaux
Tweaks to make line-display faster.
r1503 milliseconds=100, oneShot=True)
gvaroquaux
Clean up code, names, and docstrings.
r1455
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 def clear_screen(self):
""" Empty completely the widget.
"""
self.ClearAll()
self.new_prompt(self.input_prompt_template.substitute(
number=(self.last_result['number'] + 1)))
Gael Varoquaux
IPythonX: terminate the mainloop when exiting, to close the child windows...
r1732 #--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872 # LineFrontEnd interface
Gael Varoquaux
IPythonX: terminate the mainloop when exiting, to close the child windows...
r1732 #--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872
Gael Varoquaux
IPythonX: terminate the mainloop when exiting, to close the child windows...
r1732 def execute(self, python_string, raw_string=None):
self._input_state = 'buffering'
self.CallTipCancel()
self._cursor = wx.BusyCursor()
if raw_string is None:
raw_string = python_string
end_line = self.current_prompt_line \
+ max(1, len(raw_string.split('\n'))-1)
for i in range(self.current_prompt_line, end_line):
if i in self._markers:
self.MarkerDeleteHandle(self._markers[i])
self._markers[i] = self.MarkerAdd(i, _COMPLETE_BUFFER_MARKER)
# Use a callafter to update the display robustly under windows
def callback():
self.GotoPos(self.GetLength())
Bernardo B. Marques
remove all trailling spaces
r4872 PrefilterFrontEnd.execute(self, python_string,
Gael Varoquaux
IPythonX: terminate the mainloop when exiting, to close the child windows...
r1732 raw_string=raw_string)
wx.CallAfter(callback)
Gael Varoquaux
Add an execute_command to the linefrontendbase API. This method...
r1712 def execute_command(self, command, hidden=False):
""" Execute a command, not only in the model, but also in the
view.
"""
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 # XXX: This method needs to be integrated in the base fronted
# interface
Gael Varoquaux
Add an execute_command to the linefrontendbase API. This method...
r1712 if hidden:
return self.shell.execute(command)
else:
# XXX: we are not storing the input buffer previous to the
# execution, as this forces us to run the execution
# input_buffer a yield, which is not good.
##current_buffer = self.shell.control.input_buffer
command = command.rstrip()
if len(command.split('\n')) > 1:
# The input command is several lines long, we need to
# force the execution to happen
command += '\n'
cleaned_command = self.prefilter_input(command)
self.input_buffer = command
# Do not use wx.Yield() (aka GUI.process_events()) to avoid
# recursive yields.
self.ProcessEvent(wx.PaintEvent())
self.write('\n')
if not self.is_complete(cleaned_command + '\n'):
self._colorize_input_buffer()
self.render_error('Incomplete or invalid input')
self.new_prompt(self.input_prompt_template.substitute(
number=(self.last_result['number'] + 1)))
return False
self._on_enter()
return True
Bernardo B. Marques
remove all trailling spaces
r4872 def save_output_hooks(self):
Gael Varoquaux
Add color feedback for multiline entry.
r1473 self.__old_raw_input = __builtin__.raw_input
PrefilterFrontEnd.save_output_hooks(self)
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391
def capture_output(self):
gvaroquaux
Tweaks to make line-display faster.
r1503 self.SetLexer(stc.STC_LEX_NULL)
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391 PrefilterFrontEnd.capture_output(self)
gvaroquaux
Fix raw_input.
r1654 __builtin__.raw_input = self.raw_input
Bernardo B. Marques
remove all trailling spaces
r4872
Gael Varoquaux
Add demo app. Add callback for exit to the ipython0 code.
r1391 def release_output(self):
Gael Varoquaux
Deal with raw_input.
r1388 __builtin__.raw_input = self.__old_raw_input
Gael Varoquaux
Isolate the displayhook created by ipython0. This fixes a test not...
r1472 PrefilterFrontEnd.release_output(self)
gvaroquaux
Tweaks to make line-display faster.
r1503 self.SetLexer(stc.STC_LEX_PYTHON)
Gael Varoquaux
Nice background color for already entered code....
r1374
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
def after_execute(self):
PrefilterFrontEnd.after_execute(self)
gvaroquaux
Clean up code, names, and docstrings.
r1455 # Clear the wait cursor
Gael Varoquaux
Improve tab-completion.
r1373 if hasattr(self, '_cursor'):
del self._cursor
gvaroquaux
Tweak the debug mode....
r1484 self.SetCursor(wx.StockCursor(wx.CURSOR_CHAR))
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437
Gael Varoquaux
Tracebacks in red.
r1384 def show_traceback(self):
start_line = self.GetCurrentLine()
PrefilterFrontEnd.show_traceback(self)
gvaroquaux
Tweak to have windows show tracebacks.
r1651 self.ProcessEvent(wx.PaintEvent())
#wx.Yield()
Gael Varoquaux
Tracebacks in red.
r1384 for i in range(start_line, self.GetCurrentLine()):
Gael Varoquaux
Isolate the displayhook created by ipython0. This fixes a test not...
r1472 self._markers[i] = self.MarkerAdd(i, _ERROR_MARKER)
Gael Varoquaux
Tracebacks in red.
r1384
Bernardo B. Marques
remove all trailling spaces
r4872
Gael Varoquaux
Update wx frontend.
r1349 #--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872 # FrontEndBase interface
gvaroquaux
Capture errors in user's input lines and raises meaningfull exceptions.
r1639 #--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Capture errors in user's input lines and raises meaningfull exceptions.
r1639 def render_error(self, e):
start_line = self.GetCurrentLine()
self.write('\n' + e + '\n')
for i in range(start_line, self.GetCurrentLine()):
self._markers[i] = self.MarkerAdd(i, _ERROR_MARKER)
#--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872 # ConsoleWidget interface
Gael Varoquaux
Update wx frontend.
r1349 #--------------------------------------------------------------------------
gvaroquaux
Clean up code, names, and docstrings.
r1455
def new_prompt(self, prompt):
""" Display a new prompt, and start a new input buffer.
"""
self._input_state = 'readline'
ConsoleWidget.new_prompt(self, prompt)
Gael Varoquaux
Add color feedback for multiline entry.
r1473 i = self.current_prompt_line
self._markers[i] = self.MarkerAdd(i, _INPUT_MARKER)
gvaroquaux
Clean up code, names, and docstrings.
r1455
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 def continuation_prompt(self, *args, **kwargs):
# Avoid multiple inheritence, be explicit about which
# parent method class gets called
return ConsoleWidget.continuation_prompt(self, *args, **kwargs)
Gael Varoquaux
More tests of the frontend. Improve the ease of testing.
r1458 def write(self, *args, **kwargs):
# Avoid multiple inheritence, be explicit about which
# parent method class gets called
Gael Varoquaux
MERGE Laurent's tweaks. Clean up'
r1893 return ConsoleWidget.write(self, *args, **kwargs)
Gael Varoquaux
More tests of the frontend. Improve the ease of testing.
r1458
Gael Varoquaux
Update wx frontend.
r1349 def _on_key_down(self, event, skip=True):
""" Capture the character events, let the parent
widget handle them, and put our logic afterward.
"""
Gael Varoquaux
Fix for Unicode characters when executing processes. Also fix typo in...
r1451 # FIXME: This method needs to be broken down in smaller ones.
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 current_line_num = self.GetCurrentLine()
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 key_code = event.GetKeyCode()
if key_code in (ord('c'), ord('C')) and event.ControlDown():
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # Capture Control-C
if self._input_state == 'subprocess':
if self.debug:
print >>sys.__stderr__, 'Killing running process'
gvaroquaux
Fix a race in killing subprocesses.
r1648 if hasattr(self._running_process, 'process'):
self._running_process.process.kill()
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 elif self._input_state == 'buffering':
if self.debug:
gvaroquaux
Clean up code, names, and docstrings.
r1455 print >>sys.__stderr__, 'Raising KeyboardInterrupt'
raise KeyboardInterrupt
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # XXX: We need to make really sure we
# get back to a prompt.
Gael Varoquaux
Proper redirection of keystrokes to subprocesses.
r1450 elif self._input_state == 'subprocess' and (
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 ( key_code <256 and not event.ControlDown() )
Bernardo B. Marques
remove all trailling spaces
r4872 or
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 ( key_code in (ord('d'), ord('D')) and
Gael Varoquaux
Proper redirection of keystrokes to subprocesses.
r1450 event.ControlDown())):
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 # We are running a process, we redirect keys.
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 ConsoleWidget._on_key_down(self, event, skip=skip)
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 char = chr(key_code)
Gael Varoquaux
Proper redirection of keystrokes to subprocesses.
r1450 # Deal with some inconsistency in wx keycodes:
if char == '\r':
char = '\n'
elif not event.ShiftDown():
char = char.lower()
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 if event.ControlDown() and key_code in (ord('d'), ord('D')):
Gael Varoquaux
Proper redirection of keystrokes to subprocesses.
r1450 char = '\04'
self._running_process.process.stdin.write(char)
gvaroquaux
Make process execution work under windows.
r1449 self._running_process.process.stdin.flush()
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 elif key_code in (ord('('), 57, 53):
Gael Varoquaux
First cut of subprocess execution with redirection of stdin/stdout.
r1437 # Calltips
Gael Varoquaux
Tweak calltip code.
r1381 event.Skip()
self.do_calltip()
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 elif self.AutoCompActive() and not key_code == ord('\t'):
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 event.Skip()
Bernardo B. Marques
remove all trailling spaces
r4872 if key_code in (wx.WXK_BACK, wx.WXK_DELETE):
gvaroquaux
Clean up code, names, and docstrings.
r1455 wx.CallAfter(self._popup_completion, create=True)
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 elif not key_code in (wx.WXK_UP, wx.WXK_DOWN, wx.WXK_LEFT,
Gael Varoquaux
Better tab-completion when the autocomp menu is displayed.
r1493 wx.WXK_RIGHT, wx.WXK_ESCAPE):
gvaroquaux
Clean up code, names, and docstrings.
r1455 wx.CallAfter(self._popup_completion)
Gael Varoquaux
Update wx frontend.
r1349 else:
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 # Up history
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 if key_code == wx.WXK_UP and (
event.ControlDown() or
current_line_num == self.current_prompt_line
):
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 new_buffer = self.get_history_previous(
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 self.input_buffer)
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 if new_buffer is not None:
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 self.input_buffer = new_buffer
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 if self.GetCurrentLine() > self.current_prompt_line:
# Go to first line, for seemless history up.
self.GotoPos(self.current_prompt_pos)
# Down history
Gael Varoquaux
BUG: Correct some trailing wx2.6 incompatibilities
r2002 elif key_code == wx.WXK_DOWN and (
Gael Varoquaux
BUG: Make the frontend compatible with wxPython 2.6
r2001 event.ControlDown() or
current_line_num == self.LineCount -1
):
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 new_buffer = self.get_history_next()
if new_buffer is not None:
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 self.input_buffer = new_buffer
Gael Varoquaux
Make "help()" work.
r1390 # Tab-completion
Gael Varoquaux
BUG: Correct some trailing wx2.6 incompatibilities
r2002 elif key_code == ord('\t'):
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 current_line, current_line_num = self.CurLine
Bernardo B. Marques
remove all trailling spaces
r4872 if not re.match(r'^%s\s*$' % self.continuation_prompt(),
Gael Varoquaux
BUG: Fix handling of non-tab completion tab keys
r1993 current_line):
gvaroquaux
Abstract completion mechanism outside of wx-specific code.
r1463 self.complete_current_input()
Gael Varoquaux
Better tab-completion when the autocomp menu is displayed.
r1493 if self.AutoCompActive():
wx.CallAfter(self._popup_completion, create=True)
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 else:
event.Skip()
Gael Varoquaux
BUG: Correct some trailing wx2.6 incompatibilities
r2002 elif key_code == wx.WXK_BACK:
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 # If characters where erased, check if we have to
# remove a line.
# XXX: What about DEL?
Gael Varoquaux
Remove forgotten debug code
r1895 # FIXME: This logics should be in ConsoleWidget, as it is
# independant of IPython
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 current_line, _ = self.CurLine
current_pos = self.GetCurrentPos()
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 current_line_num = self.LineFromPosition(current_pos)
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 current_col = self.GetColumn(current_pos)
len_prompt = len(self.continuation_prompt())
if ( current_line.startswith(self.continuation_prompt())
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 and current_col == len_prompt):
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 new_lines = []
for line_num, line in enumerate(
self.input_buffer.split('\n')):
if (line_num + self.current_prompt_line ==
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 current_line_num):
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 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)
Gael Varoquaux
Correct small history bug. Add busy cursor.
r1371 else:
ConsoleWidget._on_key_down(self, event, skip=skip)
Bernardo B. Marques
remove all trailling spaces
r4872
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Improve tab-completion.
r1373 def _on_key_up(self, event, skip=True):
gvaroquaux
Clean up code, names, and docstrings.
r1455 """ Called when any key is released.
"""
Gael Varoquaux
BUG: Correct some trailing wx2.6 incompatibilities
r2002 if event.GetKeyCode() in (59, ord('.')):
Gael Varoquaux
Improve tab-completion.
r1373 # Intercepting '.'
event.Skip()
Gael Varoquaux
Fix a completion crasher (index error during completion)....
r1624 wx.CallAfter(self._popup_completion, create=True)
Gael Varoquaux
Improve tab-completion.
r1373 else:
ConsoleWidget._on_key_up(self, event, skip=skip)
Bernardo B. Marques
remove all trailling spaces
r4872 # Make sure the continuation_prompts are always followed by a
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 # whitespace
new_lines = []
if self._input_state == 'readline':
Gael Varoquaux
ENH: Add continuation prompts
r1884 position = self.GetCurrentPos()
Gael Varoquaux
Take in account remarks by Fernando on code review
r1947 continuation_prompt = self.continuation_prompt()[:-1]
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 for line in self.input_buffer.split('\n'):
Gael Varoquaux
Take in account remarks by Fernando on code review
r1947 if not line == continuation_prompt:
Gael Varoquaux
ENH: Backspace now deletes continuation lines
r1894 new_lines.append(line)
self.input_buffer = '\n'.join(new_lines)
Gael Varoquaux
ENH: Add continuation prompts
r1884 self.GotoPos(position)
Gael Varoquaux
Improve tab-completion.
r1373
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447
gvaroquaux
Make the wx frontend well-behaved under windows.
r1436 def _on_enter(self):
gvaroquaux
Clean up code, names, and docstrings.
r1455 """ Called on return key down, in readline input_state.
"""
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 last_line_num = self.LineFromPosition(self.GetLength())
current_line_num = self.LineFromPosition(self.GetCurrentPos())
new_line_pos = (last_line_num - current_line_num)
gvaroquaux
Make the wx frontend well-behaved under windows.
r1436 if self.debug:
gvaroquaux
More code reuse between GUI-independant frontend and Wx frontend: getting...
r1462 print >>sys.__stdout__, repr(self.input_buffer)
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 self.write('\n', refresh=False)
# Under windows scintilla seems to be doing funny
# stuff to the line returns here, but the getter for
# input_buffer filters this out.
if sys.platform == 'win32':
self.input_buffer = self.input_buffer
Gael Varoquaux
Take in account remarks by Fernando on code review
r1947 old_prompt_num = self.current_prompt_pos
Bernardo B. Marques
remove all trailling spaces
r4872 has_executed = PrefilterFrontEnd._on_enter(self,
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 new_line_pos=new_line_pos)
Gael Varoquaux
Take in account remarks by Fernando on code review
r1947 if old_prompt_num == self.current_prompt_pos:
Bernardo B. Marques
remove all trailling spaces
r4872 # No execution has happened
Gael Varoquaux
ENH: Enter adds lines at the right position
r1896 self.GotoPos(self.GetLineEndPosition(current_line_num + 1))
return has_executed
Gael Varoquaux
Update wx frontend.
r1349
Gael Varoquaux
Clean up the title-setting code.
r1393
gvaroquaux
Clean up code, names, and docstrings.
r1455 #--------------------------------------------------------------------------
gvaroquaux
Tweaks to make line-display faster.
r1503 # EditWindow API
#--------------------------------------------------------------------------
def OnUpdateUI(self, event):
Bernardo B. Marques
remove all trailling spaces
r4872 """ Override the OnUpdateUI of the EditWindow class, to prevent
gvaroquaux
Tweaks to make line-display faster.
r1503 syntax highlighting both for faster redraw, and for more
consistent look and feel.
"""
if not self._input_state == 'readline':
ConsoleWidget.OnUpdateUI(self, event)
#--------------------------------------------------------------------------
gvaroquaux
Clean up code, names, and docstrings.
r1455 # Private API
#--------------------------------------------------------------------------
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 def _buffer_flush(self, event):
""" Called by the timer to flush the write buffer.
Bernardo B. Marques
remove all trailling spaces
r4872
gvaroquaux
Bind Ctrl-C to kill process, when in process execution.
r1447 This is always called in the mainloop, by the wx timer.
"""
self._out_buffer_lock.acquire()
_out_buffer = self._out_buffer
self._out_buffer = []
self._out_buffer_lock.release()
gvaroquaux
Make process execution work under windows.
r1449 self.write(''.join(_out_buffer), refresh=False)
gvaroquaux
Tweaks to make line-display faster.
r1503
Gael Varoquaux
Clean up the title-setting code.
r1393
Gael Varoquaux
Add color feedback for multiline entry.
r1473 def _colorize_input_buffer(self):
""" Keep the input buffer lines at a bright color.
"""
gvaroquaux
Make help() more robust.
r1480 if not self._input_state in ('readline', 'raw_input'):
gvaroquaux
Clean up the test application for the wx frontend.
r1475 return
gvaroquaux
Tweak the line colorisation for windows.
r1478 end_line = self.GetCurrentLine()
if not sys.platform == 'win32':
end_line += 1
Gael Varoquaux
Add color feedback for multiline entry.
r1473 for i in range(self.current_prompt_line, end_line):
if i in self._markers:
self.MarkerDeleteHandle(self._markers[i])
self._markers[i] = self.MarkerAdd(i, _INPUT_MARKER)
Gael Varoquaux
Clean up the title-setting code.
r1393
Gael Varoquaux
Update wx frontend.
r1349 if __name__ == '__main__':
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(300,250))
self._sizer = wx.BoxSizer(wx.VERTICAL)
Gael Varoquaux
Minor bug.
r1389 self.shell = WxController(self)
Gael Varoquaux
Update wx frontend.
r1349 self._sizer.Add(self.shell, 1, wx.EXPAND)
self.SetSizer(self._sizer)
self.SetAutoLayout(1)
self.Show(True)
app = wx.PySimpleApp()
frame = MainWindow(None, wx.ID_ANY, 'Ipython')
frame.shell.SetFocus()
Gael Varoquaux
Usability tweaks. Better auto tab completion. Terminal size more...
r1380 frame.SetSize((680, 460))
Gael Varoquaux
Update wx frontend.
r1349 self = frame.shell
app.MainLoop()