##// END OF EJS Templates
minor improvements to text placement in qtconsole...
MinRK -
Show More
@@ -1,1815 +1,1815 b''
1 """ An abstract base class for console-type widgets.
1 """ An abstract base class for console-type widgets.
2 """
2 """
3 #-----------------------------------------------------------------------------
3 #-----------------------------------------------------------------------------
4 # Imports
4 # Imports
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6
6
7 # Standard library imports
7 # Standard library imports
8 import os
8 import os
9 from os.path import commonprefix
9 from os.path import commonprefix
10 import re
10 import re
11 import sys
11 import sys
12 from textwrap import dedent
12 from textwrap import dedent
13 from unicodedata import category
13 from unicodedata import category
14
14
15 # System library imports
15 # System library imports
16 from IPython.external.qt import QtCore, QtGui
16 from IPython.external.qt import QtCore, QtGui
17
17
18 # Local imports
18 # Local imports
19 from IPython.config.configurable import LoggingConfigurable
19 from IPython.config.configurable import LoggingConfigurable
20 from IPython.frontend.qt.rich_text import HtmlExporter
20 from IPython.frontend.qt.rich_text import HtmlExporter
21 from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
21 from IPython.frontend.qt.util import MetaQObjectHasTraits, get_font
22 from IPython.utils.text import columnize
22 from IPython.utils.text import columnize
23 from IPython.utils.traitlets import Bool, Enum, Integer, Unicode
23 from IPython.utils.traitlets import Bool, Enum, Integer, Unicode
24 from ansi_code_processor import QtAnsiCodeProcessor
24 from ansi_code_processor import QtAnsiCodeProcessor
25 from completion_widget import CompletionWidget
25 from completion_widget import CompletionWidget
26 from kill_ring import QtKillRing
26 from kill_ring import QtKillRing
27
27
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 # Functions
29 # Functions
30 #-----------------------------------------------------------------------------
30 #-----------------------------------------------------------------------------
31
31
32 def is_letter_or_number(char):
32 def is_letter_or_number(char):
33 """ Returns whether the specified unicode character is a letter or a number.
33 """ Returns whether the specified unicode character is a letter or a number.
34 """
34 """
35 cat = category(char)
35 cat = category(char)
36 return cat.startswith('L') or cat.startswith('N')
36 return cat.startswith('L') or cat.startswith('N')
37
37
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39 # Classes
39 # Classes
40 #-----------------------------------------------------------------------------
40 #-----------------------------------------------------------------------------
41
41
42 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):
42 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget):
43 """ An abstract base class for console-type widgets. This class has
43 """ An abstract base class for console-type widgets. This class has
44 functionality for:
44 functionality for:
45
45
46 * Maintaining a prompt and editing region
46 * Maintaining a prompt and editing region
47 * Providing the traditional Unix-style console keyboard shortcuts
47 * Providing the traditional Unix-style console keyboard shortcuts
48 * Performing tab completion
48 * Performing tab completion
49 * Paging text
49 * Paging text
50 * Handling ANSI escape codes
50 * Handling ANSI escape codes
51
51
52 ConsoleWidget also provides a number of utility methods that will be
52 ConsoleWidget also provides a number of utility methods that will be
53 convenient to implementors of a console-style widget.
53 convenient to implementors of a console-style widget.
54 """
54 """
55 __metaclass__ = MetaQObjectHasTraits
55 __metaclass__ = MetaQObjectHasTraits
56
56
57 #------ Configuration ------------------------------------------------------
57 #------ Configuration ------------------------------------------------------
58
58
59 ansi_codes = Bool(True, config=True,
59 ansi_codes = Bool(True, config=True,
60 help="Whether to process ANSI escape codes."
60 help="Whether to process ANSI escape codes."
61 )
61 )
62 buffer_size = Integer(500, config=True,
62 buffer_size = Integer(500, config=True,
63 help="""
63 help="""
64 The maximum number of lines of text before truncation. Specifying a
64 The maximum number of lines of text before truncation. Specifying a
65 non-positive number disables text truncation (not recommended).
65 non-positive number disables text truncation (not recommended).
66 """
66 """
67 )
67 )
68 gui_completion = Bool(False, config=True,
68 gui_completion = Bool(False, config=True,
69 help="""
69 help="""
70 Use a list widget instead of plain text output for tab completion.
70 Use a list widget instead of plain text output for tab completion.
71 """
71 """
72 )
72 )
73 # NOTE: this value can only be specified during initialization.
73 # NOTE: this value can only be specified during initialization.
74 kind = Enum(['plain', 'rich'], default_value='plain', config=True,
74 kind = Enum(['plain', 'rich'], default_value='plain', config=True,
75 help="""
75 help="""
76 The type of underlying text widget to use. Valid values are 'plain',
76 The type of underlying text widget to use. Valid values are 'plain',
77 which specifies a QPlainTextEdit, and 'rich', which specifies a
77 which specifies a QPlainTextEdit, and 'rich', which specifies a
78 QTextEdit.
78 QTextEdit.
79 """
79 """
80 )
80 )
81 # NOTE: this value can only be specified during initialization.
81 # NOTE: this value can only be specified during initialization.
82 paging = Enum(['inside', 'hsplit', 'vsplit', 'custom', 'none'],
82 paging = Enum(['inside', 'hsplit', 'vsplit', 'custom', 'none'],
83 default_value='inside', config=True,
83 default_value='inside', config=True,
84 help="""
84 help="""
85 The type of paging to use. Valid values are:
85 The type of paging to use. Valid values are:
86
86
87 'inside' : The widget pages like a traditional terminal.
87 'inside' : The widget pages like a traditional terminal.
88 'hsplit' : When paging is requested, the widget is split
88 'hsplit' : When paging is requested, the widget is split
89 horizontally. The top pane contains the console, and the
89 horizontally. The top pane contains the console, and the
90 bottom pane contains the paged text.
90 bottom pane contains the paged text.
91 'vsplit' : Similar to 'hsplit', except that a vertical splitter
91 'vsplit' : Similar to 'hsplit', except that a vertical splitter
92 used.
92 used.
93 'custom' : No action is taken by the widget beyond emitting a
93 'custom' : No action is taken by the widget beyond emitting a
94 'custom_page_requested(str)' signal.
94 'custom_page_requested(str)' signal.
95 'none' : The text is written directly to the console.
95 'none' : The text is written directly to the console.
96 """)
96 """)
97
97
98 font_family = Unicode(config=True,
98 font_family = Unicode(config=True,
99 help="""The font family to use for the console.
99 help="""The font family to use for the console.
100 On OSX this defaults to Monaco, on Windows the default is
100 On OSX this defaults to Monaco, on Windows the default is
101 Consolas with fallback of Courier, and on other platforms
101 Consolas with fallback of Courier, and on other platforms
102 the default is Monospace.
102 the default is Monospace.
103 """)
103 """)
104 def _font_family_default(self):
104 def _font_family_default(self):
105 if sys.platform == 'win32':
105 if sys.platform == 'win32':
106 # Consolas ships with Vista/Win7, fallback to Courier if needed
106 # Consolas ships with Vista/Win7, fallback to Courier if needed
107 return 'Consolas'
107 return 'Consolas'
108 elif sys.platform == 'darwin':
108 elif sys.platform == 'darwin':
109 # OSX always has Monaco, no need for a fallback
109 # OSX always has Monaco, no need for a fallback
110 return 'Monaco'
110 return 'Monaco'
111 else:
111 else:
112 # Monospace should always exist, no need for a fallback
112 # Monospace should always exist, no need for a fallback
113 return 'Monospace'
113 return 'Monospace'
114
114
115 font_size = Integer(config=True,
115 font_size = Integer(config=True,
116 help="""The font size. If unconfigured, Qt will be entrusted
116 help="""The font size. If unconfigured, Qt will be entrusted
117 with the size of the font.
117 with the size of the font.
118 """)
118 """)
119
119
120 # Whether to override ShortcutEvents for the keybindings defined by this
120 # Whether to override ShortcutEvents for the keybindings defined by this
121 # widget (Ctrl+n, Ctrl+a, etc). Enable this if you want this widget to take
121 # widget (Ctrl+n, Ctrl+a, etc). Enable this if you want this widget to take
122 # priority (when it has focus) over, e.g., window-level menu shortcuts.
122 # priority (when it has focus) over, e.g., window-level menu shortcuts.
123 override_shortcuts = Bool(False)
123 override_shortcuts = Bool(False)
124
124
125 #------ Signals ------------------------------------------------------------
125 #------ Signals ------------------------------------------------------------
126
126
127 # Signals that indicate ConsoleWidget state.
127 # Signals that indicate ConsoleWidget state.
128 copy_available = QtCore.Signal(bool)
128 copy_available = QtCore.Signal(bool)
129 redo_available = QtCore.Signal(bool)
129 redo_available = QtCore.Signal(bool)
130 undo_available = QtCore.Signal(bool)
130 undo_available = QtCore.Signal(bool)
131
131
132 # Signal emitted when paging is needed and the paging style has been
132 # Signal emitted when paging is needed and the paging style has been
133 # specified as 'custom'.
133 # specified as 'custom'.
134 custom_page_requested = QtCore.Signal(object)
134 custom_page_requested = QtCore.Signal(object)
135
135
136 # Signal emitted when the font is changed.
136 # Signal emitted when the font is changed.
137 font_changed = QtCore.Signal(QtGui.QFont)
137 font_changed = QtCore.Signal(QtGui.QFont)
138
138
139 #------ Protected class variables ------------------------------------------
139 #------ Protected class variables ------------------------------------------
140
140
141 # When the control key is down, these keys are mapped.
141 # When the control key is down, these keys are mapped.
142 _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left,
142 _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left,
143 QtCore.Qt.Key_F : QtCore.Qt.Key_Right,
143 QtCore.Qt.Key_F : QtCore.Qt.Key_Right,
144 QtCore.Qt.Key_A : QtCore.Qt.Key_Home,
144 QtCore.Qt.Key_A : QtCore.Qt.Key_Home,
145 QtCore.Qt.Key_P : QtCore.Qt.Key_Up,
145 QtCore.Qt.Key_P : QtCore.Qt.Key_Up,
146 QtCore.Qt.Key_N : QtCore.Qt.Key_Down,
146 QtCore.Qt.Key_N : QtCore.Qt.Key_Down,
147 QtCore.Qt.Key_H : QtCore.Qt.Key_Backspace,
147 QtCore.Qt.Key_H : QtCore.Qt.Key_Backspace,
148 QtCore.Qt.Key_D : QtCore.Qt.Key_Delete, }
148 QtCore.Qt.Key_D : QtCore.Qt.Key_Delete, }
149 if not sys.platform == 'darwin':
149 if not sys.platform == 'darwin':
150 # On OS X, Ctrl-E already does the right thing, whereas End moves the
150 # On OS X, Ctrl-E already does the right thing, whereas End moves the
151 # cursor to the bottom of the buffer.
151 # cursor to the bottom of the buffer.
152 _ctrl_down_remap[QtCore.Qt.Key_E] = QtCore.Qt.Key_End
152 _ctrl_down_remap[QtCore.Qt.Key_E] = QtCore.Qt.Key_End
153
153
154 # The shortcuts defined by this widget. We need to keep track of these to
154 # The shortcuts defined by this widget. We need to keep track of these to
155 # support 'override_shortcuts' above.
155 # support 'override_shortcuts' above.
156 _shortcuts = set(_ctrl_down_remap.keys() +
156 _shortcuts = set(_ctrl_down_remap.keys() +
157 [ QtCore.Qt.Key_C, QtCore.Qt.Key_G, QtCore.Qt.Key_O,
157 [ QtCore.Qt.Key_C, QtCore.Qt.Key_G, QtCore.Qt.Key_O,
158 QtCore.Qt.Key_V ])
158 QtCore.Qt.Key_V ])
159
159
160 #---------------------------------------------------------------------------
160 #---------------------------------------------------------------------------
161 # 'QObject' interface
161 # 'QObject' interface
162 #---------------------------------------------------------------------------
162 #---------------------------------------------------------------------------
163
163
164 def __init__(self, parent=None, **kw):
164 def __init__(self, parent=None, **kw):
165 """ Create a ConsoleWidget.
165 """ Create a ConsoleWidget.
166
166
167 Parameters:
167 Parameters:
168 -----------
168 -----------
169 parent : QWidget, optional [default None]
169 parent : QWidget, optional [default None]
170 The parent for this widget.
170 The parent for this widget.
171 """
171 """
172 QtGui.QWidget.__init__(self, parent)
172 QtGui.QWidget.__init__(self, parent)
173 LoggingConfigurable.__init__(self, **kw)
173 LoggingConfigurable.__init__(self, **kw)
174
174
175 # Create the layout and underlying text widget.
175 # Create the layout and underlying text widget.
176 layout = QtGui.QStackedLayout(self)
176 layout = QtGui.QStackedLayout(self)
177 layout.setContentsMargins(0, 0, 0, 0)
177 layout.setContentsMargins(0, 0, 0, 0)
178 self._control = self._create_control()
178 self._control = self._create_control()
179 self._page_control = None
179 self._page_control = None
180 self._splitter = None
180 self._splitter = None
181 if self.paging in ('hsplit', 'vsplit'):
181 if self.paging in ('hsplit', 'vsplit'):
182 self._splitter = QtGui.QSplitter()
182 self._splitter = QtGui.QSplitter()
183 if self.paging == 'hsplit':
183 if self.paging == 'hsplit':
184 self._splitter.setOrientation(QtCore.Qt.Horizontal)
184 self._splitter.setOrientation(QtCore.Qt.Horizontal)
185 else:
185 else:
186 self._splitter.setOrientation(QtCore.Qt.Vertical)
186 self._splitter.setOrientation(QtCore.Qt.Vertical)
187 self._splitter.addWidget(self._control)
187 self._splitter.addWidget(self._control)
188 layout.addWidget(self._splitter)
188 layout.addWidget(self._splitter)
189 else:
189 else:
190 layout.addWidget(self._control)
190 layout.addWidget(self._control)
191
191
192 # Create the paging widget, if necessary.
192 # Create the paging widget, if necessary.
193 if self.paging in ('inside', 'hsplit', 'vsplit'):
193 if self.paging in ('inside', 'hsplit', 'vsplit'):
194 self._page_control = self._create_page_control()
194 self._page_control = self._create_page_control()
195 if self._splitter:
195 if self._splitter:
196 self._page_control.hide()
196 self._page_control.hide()
197 self._splitter.addWidget(self._page_control)
197 self._splitter.addWidget(self._page_control)
198 else:
198 else:
199 layout.addWidget(self._page_control)
199 layout.addWidget(self._page_control)
200
200
201 # Initialize protected variables. Some variables contain useful state
201 # Initialize protected variables. Some variables contain useful state
202 # information for subclasses; they should be considered read-only.
202 # information for subclasses; they should be considered read-only.
203 self._append_before_prompt_pos = 0
203 self._append_before_prompt_pos = 0
204 self._ansi_processor = QtAnsiCodeProcessor()
204 self._ansi_processor = QtAnsiCodeProcessor()
205 self._completion_widget = CompletionWidget(self._control)
205 self._completion_widget = CompletionWidget(self._control)
206 self._continuation_prompt = '> '
206 self._continuation_prompt = '> '
207 self._continuation_prompt_html = None
207 self._continuation_prompt_html = None
208 self._executing = False
208 self._executing = False
209 self._filter_drag = False
209 self._filter_drag = False
210 self._filter_resize = False
210 self._filter_resize = False
211 self._html_exporter = HtmlExporter(self._control)
211 self._html_exporter = HtmlExporter(self._control)
212 self._input_buffer_executing = ''
212 self._input_buffer_executing = ''
213 self._input_buffer_pending = ''
213 self._input_buffer_pending = ''
214 self._kill_ring = QtKillRing(self._control)
214 self._kill_ring = QtKillRing(self._control)
215 self._prompt = ''
215 self._prompt = ''
216 self._prompt_html = None
216 self._prompt_html = None
217 self._prompt_pos = 0
217 self._prompt_pos = 0
218 self._prompt_sep = ''
218 self._prompt_sep = ''
219 self._reading = False
219 self._reading = False
220 self._reading_callback = None
220 self._reading_callback = None
221 self._tab_width = 8
221 self._tab_width = 8
222 self._text_completing_pos = 0
222 self._text_completing_pos = 0
223
223
224 # Set a monospaced font.
224 # Set a monospaced font.
225 self.reset_font()
225 self.reset_font()
226
226
227 # Configure actions.
227 # Configure actions.
228 action = QtGui.QAction('Print', None)
228 action = QtGui.QAction('Print', None)
229 action.setEnabled(True)
229 action.setEnabled(True)
230 printkey = QtGui.QKeySequence(QtGui.QKeySequence.Print)
230 printkey = QtGui.QKeySequence(QtGui.QKeySequence.Print)
231 if printkey.matches("Ctrl+P") and sys.platform != 'darwin':
231 if printkey.matches("Ctrl+P") and sys.platform != 'darwin':
232 # Only override the default if there is a collision.
232 # Only override the default if there is a collision.
233 # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX.
233 # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX.
234 printkey = "Ctrl+Shift+P"
234 printkey = "Ctrl+Shift+P"
235 action.setShortcut(printkey)
235 action.setShortcut(printkey)
236 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
236 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
237 action.triggered.connect(self.print_)
237 action.triggered.connect(self.print_)
238 self.addAction(action)
238 self.addAction(action)
239 self.print_action = action
239 self.print_action = action
240
240
241 action = QtGui.QAction('Save as HTML/XML', None)
241 action = QtGui.QAction('Save as HTML/XML', None)
242 action.setShortcut(QtGui.QKeySequence.Save)
242 action.setShortcut(QtGui.QKeySequence.Save)
243 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
243 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
244 action.triggered.connect(self.export_html)
244 action.triggered.connect(self.export_html)
245 self.addAction(action)
245 self.addAction(action)
246 self.export_action = action
246 self.export_action = action
247
247
248 action = QtGui.QAction('Select All', None)
248 action = QtGui.QAction('Select All', None)
249 action.setEnabled(True)
249 action.setEnabled(True)
250 selectall = QtGui.QKeySequence(QtGui.QKeySequence.SelectAll)
250 selectall = QtGui.QKeySequence(QtGui.QKeySequence.SelectAll)
251 if selectall.matches("Ctrl+A") and sys.platform != 'darwin':
251 if selectall.matches("Ctrl+A") and sys.platform != 'darwin':
252 # Only override the default if there is a collision.
252 # Only override the default if there is a collision.
253 # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX.
253 # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX.
254 selectall = "Ctrl+Shift+A"
254 selectall = "Ctrl+Shift+A"
255 action.setShortcut(selectall)
255 action.setShortcut(selectall)
256 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
256 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
257 action.triggered.connect(self.select_all)
257 action.triggered.connect(self.select_all)
258 self.addAction(action)
258 self.addAction(action)
259 self.select_all_action = action
259 self.select_all_action = action
260
260
261 self.increase_font_size = QtGui.QAction("Bigger Font",
261 self.increase_font_size = QtGui.QAction("Bigger Font",
262 self,
262 self,
263 shortcut=QtGui.QKeySequence.ZoomIn,
263 shortcut=QtGui.QKeySequence.ZoomIn,
264 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
264 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
265 statusTip="Increase the font size by one point",
265 statusTip="Increase the font size by one point",
266 triggered=self._increase_font_size)
266 triggered=self._increase_font_size)
267 self.addAction(self.increase_font_size)
267 self.addAction(self.increase_font_size)
268
268
269 self.decrease_font_size = QtGui.QAction("Smaller Font",
269 self.decrease_font_size = QtGui.QAction("Smaller Font",
270 self,
270 self,
271 shortcut=QtGui.QKeySequence.ZoomOut,
271 shortcut=QtGui.QKeySequence.ZoomOut,
272 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
272 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
273 statusTip="Decrease the font size by one point",
273 statusTip="Decrease the font size by one point",
274 triggered=self._decrease_font_size)
274 triggered=self._decrease_font_size)
275 self.addAction(self.decrease_font_size)
275 self.addAction(self.decrease_font_size)
276
276
277 self.reset_font_size = QtGui.QAction("Normal Font",
277 self.reset_font_size = QtGui.QAction("Normal Font",
278 self,
278 self,
279 shortcut="Ctrl+0",
279 shortcut="Ctrl+0",
280 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
280 shortcutContext=QtCore.Qt.WidgetWithChildrenShortcut,
281 statusTip="Restore the Normal font size",
281 statusTip="Restore the Normal font size",
282 triggered=self.reset_font)
282 triggered=self.reset_font)
283 self.addAction(self.reset_font_size)
283 self.addAction(self.reset_font_size)
284
284
285
285
286
286
287 def eventFilter(self, obj, event):
287 def eventFilter(self, obj, event):
288 """ Reimplemented to ensure a console-like behavior in the underlying
288 """ Reimplemented to ensure a console-like behavior in the underlying
289 text widgets.
289 text widgets.
290 """
290 """
291 etype = event.type()
291 etype = event.type()
292 if etype == QtCore.QEvent.KeyPress:
292 if etype == QtCore.QEvent.KeyPress:
293
293
294 # Re-map keys for all filtered widgets.
294 # Re-map keys for all filtered widgets.
295 key = event.key()
295 key = event.key()
296 if self._control_key_down(event.modifiers()) and \
296 if self._control_key_down(event.modifiers()) and \
297 key in self._ctrl_down_remap:
297 key in self._ctrl_down_remap:
298 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
298 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
299 self._ctrl_down_remap[key],
299 self._ctrl_down_remap[key],
300 QtCore.Qt.NoModifier)
300 QtCore.Qt.NoModifier)
301 QtGui.qApp.sendEvent(obj, new_event)
301 QtGui.qApp.sendEvent(obj, new_event)
302 return True
302 return True
303
303
304 elif obj == self._control:
304 elif obj == self._control:
305 return self._event_filter_console_keypress(event)
305 return self._event_filter_console_keypress(event)
306
306
307 elif obj == self._page_control:
307 elif obj == self._page_control:
308 return self._event_filter_page_keypress(event)
308 return self._event_filter_page_keypress(event)
309
309
310 # Make middle-click paste safe.
310 # Make middle-click paste safe.
311 elif etype == QtCore.QEvent.MouseButtonRelease and \
311 elif etype == QtCore.QEvent.MouseButtonRelease and \
312 event.button() == QtCore.Qt.MidButton and \
312 event.button() == QtCore.Qt.MidButton and \
313 obj == self._control.viewport():
313 obj == self._control.viewport():
314 cursor = self._control.cursorForPosition(event.pos())
314 cursor = self._control.cursorForPosition(event.pos())
315 self._control.setTextCursor(cursor)
315 self._control.setTextCursor(cursor)
316 self.paste(QtGui.QClipboard.Selection)
316 self.paste(QtGui.QClipboard.Selection)
317 return True
317 return True
318
318
319 # Manually adjust the scrollbars *after* a resize event is dispatched.
319 # Manually adjust the scrollbars *after* a resize event is dispatched.
320 elif etype == QtCore.QEvent.Resize and not self._filter_resize:
320 elif etype == QtCore.QEvent.Resize and not self._filter_resize:
321 self._filter_resize = True
321 self._filter_resize = True
322 QtGui.qApp.sendEvent(obj, event)
322 QtGui.qApp.sendEvent(obj, event)
323 self._adjust_scrollbars()
323 self._adjust_scrollbars()
324 self._filter_resize = False
324 self._filter_resize = False
325 return True
325 return True
326
326
327 # Override shortcuts for all filtered widgets.
327 # Override shortcuts for all filtered widgets.
328 elif etype == QtCore.QEvent.ShortcutOverride and \
328 elif etype == QtCore.QEvent.ShortcutOverride and \
329 self.override_shortcuts and \
329 self.override_shortcuts and \
330 self._control_key_down(event.modifiers()) and \
330 self._control_key_down(event.modifiers()) and \
331 event.key() in self._shortcuts:
331 event.key() in self._shortcuts:
332 event.accept()
332 event.accept()
333
333
334 # Ensure that drags are safe. The problem is that the drag starting
334 # Ensure that drags are safe. The problem is that the drag starting
335 # logic, which determines whether the drag is a Copy or Move, is locked
335 # logic, which determines whether the drag is a Copy or Move, is locked
336 # down in QTextControl. If the widget is editable, which it must be if
336 # down in QTextControl. If the widget is editable, which it must be if
337 # we're not executing, the drag will be a Move. The following hack
337 # we're not executing, the drag will be a Move. The following hack
338 # prevents QTextControl from deleting the text by clearing the selection
338 # prevents QTextControl from deleting the text by clearing the selection
339 # when a drag leave event originating from this widget is dispatched.
339 # when a drag leave event originating from this widget is dispatched.
340 # The fact that we have to clear the user's selection is unfortunate,
340 # The fact that we have to clear the user's selection is unfortunate,
341 # but the alternative--trying to prevent Qt from using its hardwired
341 # but the alternative--trying to prevent Qt from using its hardwired
342 # drag logic and writing our own--is worse.
342 # drag logic and writing our own--is worse.
343 elif etype == QtCore.QEvent.DragEnter and \
343 elif etype == QtCore.QEvent.DragEnter and \
344 obj == self._control.viewport() and \
344 obj == self._control.viewport() and \
345 event.source() == self._control.viewport():
345 event.source() == self._control.viewport():
346 self._filter_drag = True
346 self._filter_drag = True
347 elif etype == QtCore.QEvent.DragLeave and \
347 elif etype == QtCore.QEvent.DragLeave and \
348 obj == self._control.viewport() and \
348 obj == self._control.viewport() and \
349 self._filter_drag:
349 self._filter_drag:
350 cursor = self._control.textCursor()
350 cursor = self._control.textCursor()
351 cursor.clearSelection()
351 cursor.clearSelection()
352 self._control.setTextCursor(cursor)
352 self._control.setTextCursor(cursor)
353 self._filter_drag = False
353 self._filter_drag = False
354
354
355 # Ensure that drops are safe.
355 # Ensure that drops are safe.
356 elif etype == QtCore.QEvent.Drop and obj == self._control.viewport():
356 elif etype == QtCore.QEvent.Drop and obj == self._control.viewport():
357 cursor = self._control.cursorForPosition(event.pos())
357 cursor = self._control.cursorForPosition(event.pos())
358 if self._in_buffer(cursor.position()):
358 if self._in_buffer(cursor.position()):
359 text = event.mimeData().text()
359 text = event.mimeData().text()
360 self._insert_plain_text_into_buffer(cursor, text)
360 self._insert_plain_text_into_buffer(cursor, text)
361
361
362 # Qt is expecting to get something here--drag and drop occurs in its
362 # Qt is expecting to get something here--drag and drop occurs in its
363 # own event loop. Send a DragLeave event to end it.
363 # own event loop. Send a DragLeave event to end it.
364 QtGui.qApp.sendEvent(obj, QtGui.QDragLeaveEvent())
364 QtGui.qApp.sendEvent(obj, QtGui.QDragLeaveEvent())
365 return True
365 return True
366
366
367 return super(ConsoleWidget, self).eventFilter(obj, event)
367 return super(ConsoleWidget, self).eventFilter(obj, event)
368
368
369 #---------------------------------------------------------------------------
369 #---------------------------------------------------------------------------
370 # 'QWidget' interface
370 # 'QWidget' interface
371 #---------------------------------------------------------------------------
371 #---------------------------------------------------------------------------
372
372
373 def sizeHint(self):
373 def sizeHint(self):
374 """ Reimplemented to suggest a size that is 80 characters wide and
374 """ Reimplemented to suggest a size that is 80 characters wide and
375 25 lines high.
375 25 lines high.
376 """
376 """
377 font_metrics = QtGui.QFontMetrics(self.font)
377 font_metrics = QtGui.QFontMetrics(self.font)
378 margin = (self._control.frameWidth() +
378 margin = (self._control.frameWidth() +
379 self._control.document().documentMargin()) * 2
379 self._control.document().documentMargin()) * 2
380 style = self.style()
380 style = self.style()
381 splitwidth = style.pixelMetric(QtGui.QStyle.PM_SplitterWidth)
381 splitwidth = style.pixelMetric(QtGui.QStyle.PM_SplitterWidth)
382
382
383 # Note 1: Despite my best efforts to take the various margins into
383 # Note 1: Despite my best efforts to take the various margins into
384 # account, the width is still coming out a bit too small, so we include
384 # account, the width is still coming out a bit too small, so we include
385 # a fudge factor of one character here.
385 # a fudge factor of one character here.
386 # Note 2: QFontMetrics.maxWidth is not used here or anywhere else due
386 # Note 2: QFontMetrics.maxWidth is not used here or anywhere else due
387 # to a Qt bug on certain Mac OS systems where it returns 0.
387 # to a Qt bug on certain Mac OS systems where it returns 0.
388 width = font_metrics.width(' ') * 81 + margin
388 width = font_metrics.width(' ') * 81 + margin
389 width += style.pixelMetric(QtGui.QStyle.PM_ScrollBarExtent)
389 width += style.pixelMetric(QtGui.QStyle.PM_ScrollBarExtent)
390 if self.paging == 'hsplit':
390 if self.paging == 'hsplit':
391 width = width * 2 + splitwidth
391 width = width * 2 + splitwidth
392
392
393 height = font_metrics.height() * 25 + margin
393 height = font_metrics.height() * 25 + margin
394 if self.paging == 'vsplit':
394 if self.paging == 'vsplit':
395 height = height * 2 + splitwidth
395 height = height * 2 + splitwidth
396
396
397 return QtCore.QSize(width, height)
397 return QtCore.QSize(width, height)
398
398
399 #---------------------------------------------------------------------------
399 #---------------------------------------------------------------------------
400 # 'ConsoleWidget' public interface
400 # 'ConsoleWidget' public interface
401 #---------------------------------------------------------------------------
401 #---------------------------------------------------------------------------
402
402
403 def can_copy(self):
403 def can_copy(self):
404 """ Returns whether text can be copied to the clipboard.
404 """ Returns whether text can be copied to the clipboard.
405 """
405 """
406 return self._control.textCursor().hasSelection()
406 return self._control.textCursor().hasSelection()
407
407
408 def can_cut(self):
408 def can_cut(self):
409 """ Returns whether text can be cut to the clipboard.
409 """ Returns whether text can be cut to the clipboard.
410 """
410 """
411 cursor = self._control.textCursor()
411 cursor = self._control.textCursor()
412 return (cursor.hasSelection() and
412 return (cursor.hasSelection() and
413 self._in_buffer(cursor.anchor()) and
413 self._in_buffer(cursor.anchor()) and
414 self._in_buffer(cursor.position()))
414 self._in_buffer(cursor.position()))
415
415
416 def can_paste(self):
416 def can_paste(self):
417 """ Returns whether text can be pasted from the clipboard.
417 """ Returns whether text can be pasted from the clipboard.
418 """
418 """
419 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
419 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
420 return bool(QtGui.QApplication.clipboard().text())
420 return bool(QtGui.QApplication.clipboard().text())
421 return False
421 return False
422
422
423 def clear(self, keep_input=True):
423 def clear(self, keep_input=True):
424 """ Clear the console.
424 """ Clear the console.
425
425
426 Parameters:
426 Parameters:
427 -----------
427 -----------
428 keep_input : bool, optional (default True)
428 keep_input : bool, optional (default True)
429 If set, restores the old input buffer if a new prompt is written.
429 If set, restores the old input buffer if a new prompt is written.
430 """
430 """
431 if self._executing:
431 if self._executing:
432 self._control.clear()
432 self._control.clear()
433 else:
433 else:
434 if keep_input:
434 if keep_input:
435 input_buffer = self.input_buffer
435 input_buffer = self.input_buffer
436 self._control.clear()
436 self._control.clear()
437 self._show_prompt()
437 self._show_prompt()
438 if keep_input:
438 if keep_input:
439 self.input_buffer = input_buffer
439 self.input_buffer = input_buffer
440
440
441 def copy(self):
441 def copy(self):
442 """ Copy the currently selected text to the clipboard.
442 """ Copy the currently selected text to the clipboard.
443 """
443 """
444 self._control.copy()
444 self._control.copy()
445
445
446 def cut(self):
446 def cut(self):
447 """ Copy the currently selected text to the clipboard and delete it
447 """ Copy the currently selected text to the clipboard and delete it
448 if it's inside the input buffer.
448 if it's inside the input buffer.
449 """
449 """
450 self.copy()
450 self.copy()
451 if self.can_cut():
451 if self.can_cut():
452 self._control.textCursor().removeSelectedText()
452 self._control.textCursor().removeSelectedText()
453
453
454 def execute(self, source=None, hidden=False, interactive=False):
454 def execute(self, source=None, hidden=False, interactive=False):
455 """ Executes source or the input buffer, possibly prompting for more
455 """ Executes source or the input buffer, possibly prompting for more
456 input.
456 input.
457
457
458 Parameters:
458 Parameters:
459 -----------
459 -----------
460 source : str, optional
460 source : str, optional
461
461
462 The source to execute. If not specified, the input buffer will be
462 The source to execute. If not specified, the input buffer will be
463 used. If specified and 'hidden' is False, the input buffer will be
463 used. If specified and 'hidden' is False, the input buffer will be
464 replaced with the source before execution.
464 replaced with the source before execution.
465
465
466 hidden : bool, optional (default False)
466 hidden : bool, optional (default False)
467
467
468 If set, no output will be shown and the prompt will not be modified.
468 If set, no output will be shown and the prompt will not be modified.
469 In other words, it will be completely invisible to the user that
469 In other words, it will be completely invisible to the user that
470 an execution has occurred.
470 an execution has occurred.
471
471
472 interactive : bool, optional (default False)
472 interactive : bool, optional (default False)
473
473
474 Whether the console is to treat the source as having been manually
474 Whether the console is to treat the source as having been manually
475 entered by the user. The effect of this parameter depends on the
475 entered by the user. The effect of this parameter depends on the
476 subclass implementation.
476 subclass implementation.
477
477
478 Raises:
478 Raises:
479 -------
479 -------
480 RuntimeError
480 RuntimeError
481 If incomplete input is given and 'hidden' is True. In this case,
481 If incomplete input is given and 'hidden' is True. In this case,
482 it is not possible to prompt for more input.
482 it is not possible to prompt for more input.
483
483
484 Returns:
484 Returns:
485 --------
485 --------
486 A boolean indicating whether the source was executed.
486 A boolean indicating whether the source was executed.
487 """
487 """
488 # WARNING: The order in which things happen here is very particular, in
488 # WARNING: The order in which things happen here is very particular, in
489 # large part because our syntax highlighting is fragile. If you change
489 # large part because our syntax highlighting is fragile. If you change
490 # something, test carefully!
490 # something, test carefully!
491
491
492 # Decide what to execute.
492 # Decide what to execute.
493 if source is None:
493 if source is None:
494 source = self.input_buffer
494 source = self.input_buffer
495 if not hidden:
495 if not hidden:
496 # A newline is appended later, but it should be considered part
496 # A newline is appended later, but it should be considered part
497 # of the input buffer.
497 # of the input buffer.
498 source += '\n'
498 source += '\n'
499 elif not hidden:
499 elif not hidden:
500 self.input_buffer = source
500 self.input_buffer = source
501
501
502 # Execute the source or show a continuation prompt if it is incomplete.
502 # Execute the source or show a continuation prompt if it is incomplete.
503 complete = self._is_complete(source, interactive)
503 complete = self._is_complete(source, interactive)
504 if hidden:
504 if hidden:
505 if complete:
505 if complete:
506 self._execute(source, hidden)
506 self._execute(source, hidden)
507 else:
507 else:
508 error = 'Incomplete noninteractive input: "%s"'
508 error = 'Incomplete noninteractive input: "%s"'
509 raise RuntimeError(error % source)
509 raise RuntimeError(error % source)
510 else:
510 else:
511 if complete:
511 if complete:
512 self._append_plain_text('\n')
512 self._append_plain_text('\n')
513 self._input_buffer_executing = self.input_buffer
513 self._input_buffer_executing = self.input_buffer
514 self._executing = True
514 self._executing = True
515 self._prompt_finished()
515 self._prompt_finished()
516
516
517 # The maximum block count is only in effect during execution.
517 # The maximum block count is only in effect during execution.
518 # This ensures that _prompt_pos does not become invalid due to
518 # This ensures that _prompt_pos does not become invalid due to
519 # text truncation.
519 # text truncation.
520 self._control.document().setMaximumBlockCount(self.buffer_size)
520 self._control.document().setMaximumBlockCount(self.buffer_size)
521
521
522 # Setting a positive maximum block count will automatically
522 # Setting a positive maximum block count will automatically
523 # disable the undo/redo history, but just to be safe:
523 # disable the undo/redo history, but just to be safe:
524 self._control.setUndoRedoEnabled(False)
524 self._control.setUndoRedoEnabled(False)
525
525
526 # Perform actual execution.
526 # Perform actual execution.
527 self._execute(source, hidden)
527 self._execute(source, hidden)
528
528
529 else:
529 else:
530 # Do this inside an edit block so continuation prompts are
530 # Do this inside an edit block so continuation prompts are
531 # removed seamlessly via undo/redo.
531 # removed seamlessly via undo/redo.
532 cursor = self._get_end_cursor()
532 cursor = self._get_end_cursor()
533 cursor.beginEditBlock()
533 cursor.beginEditBlock()
534 cursor.insertText('\n')
534 cursor.insertText('\n')
535 self._insert_continuation_prompt(cursor)
535 self._insert_continuation_prompt(cursor)
536 cursor.endEditBlock()
536 cursor.endEditBlock()
537
537
538 # Do not do this inside the edit block. It works as expected
538 # Do not do this inside the edit block. It works as expected
539 # when using a QPlainTextEdit control, but does not have an
539 # when using a QPlainTextEdit control, but does not have an
540 # effect when using a QTextEdit. I believe this is a Qt bug.
540 # effect when using a QTextEdit. I believe this is a Qt bug.
541 self._control.moveCursor(QtGui.QTextCursor.End)
541 self._control.moveCursor(QtGui.QTextCursor.End)
542
542
543 return complete
543 return complete
544
544
545 def export_html(self):
545 def export_html(self):
546 """ Shows a dialog to export HTML/XML in various formats.
546 """ Shows a dialog to export HTML/XML in various formats.
547 """
547 """
548 self._html_exporter.export()
548 self._html_exporter.export()
549
549
550 def _get_input_buffer(self, force=False):
550 def _get_input_buffer(self, force=False):
551 """ The text that the user has entered entered at the current prompt.
551 """ The text that the user has entered entered at the current prompt.
552
552
553 If the console is currently executing, the text that is executing will
553 If the console is currently executing, the text that is executing will
554 always be returned.
554 always be returned.
555 """
555 """
556 # If we're executing, the input buffer may not even exist anymore due to
556 # If we're executing, the input buffer may not even exist anymore due to
557 # the limit imposed by 'buffer_size'. Therefore, we store it.
557 # the limit imposed by 'buffer_size'. Therefore, we store it.
558 if self._executing and not force:
558 if self._executing and not force:
559 return self._input_buffer_executing
559 return self._input_buffer_executing
560
560
561 cursor = self._get_end_cursor()
561 cursor = self._get_end_cursor()
562 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
562 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
563 input_buffer = cursor.selection().toPlainText()
563 input_buffer = cursor.selection().toPlainText()
564
564
565 # Strip out continuation prompts.
565 # Strip out continuation prompts.
566 return input_buffer.replace('\n' + self._continuation_prompt, '\n')
566 return input_buffer.replace('\n' + self._continuation_prompt, '\n')
567
567
568 def _set_input_buffer(self, string):
568 def _set_input_buffer(self, string):
569 """ Sets the text in the input buffer.
569 """ Sets the text in the input buffer.
570
570
571 If the console is currently executing, this call has no *immediate*
571 If the console is currently executing, this call has no *immediate*
572 effect. When the execution is finished, the input buffer will be updated
572 effect. When the execution is finished, the input buffer will be updated
573 appropriately.
573 appropriately.
574 """
574 """
575 # If we're executing, store the text for later.
575 # If we're executing, store the text for later.
576 if self._executing:
576 if self._executing:
577 self._input_buffer_pending = string
577 self._input_buffer_pending = string
578 return
578 return
579
579
580 # Remove old text.
580 # Remove old text.
581 cursor = self._get_end_cursor()
581 cursor = self._get_end_cursor()
582 cursor.beginEditBlock()
582 cursor.beginEditBlock()
583 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
583 cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor)
584 cursor.removeSelectedText()
584 cursor.removeSelectedText()
585
585
586 # Insert new text with continuation prompts.
586 # Insert new text with continuation prompts.
587 self._insert_plain_text_into_buffer(self._get_prompt_cursor(), string)
587 self._insert_plain_text_into_buffer(self._get_prompt_cursor(), string)
588 cursor.endEditBlock()
588 cursor.endEditBlock()
589 self._control.moveCursor(QtGui.QTextCursor.End)
589 self._control.moveCursor(QtGui.QTextCursor.End)
590
590
591 input_buffer = property(_get_input_buffer, _set_input_buffer)
591 input_buffer = property(_get_input_buffer, _set_input_buffer)
592
592
593 def _get_font(self):
593 def _get_font(self):
594 """ The base font being used by the ConsoleWidget.
594 """ The base font being used by the ConsoleWidget.
595 """
595 """
596 return self._control.document().defaultFont()
596 return self._control.document().defaultFont()
597
597
598 def _set_font(self, font):
598 def _set_font(self, font):
599 """ Sets the base font for the ConsoleWidget to the specified QFont.
599 """ Sets the base font for the ConsoleWidget to the specified QFont.
600 """
600 """
601 font_metrics = QtGui.QFontMetrics(font)
601 font_metrics = QtGui.QFontMetrics(font)
602 self._control.setTabStopWidth(self.tab_width * font_metrics.width(' '))
602 self._control.setTabStopWidth(self.tab_width * font_metrics.width(' '))
603
603
604 self._completion_widget.setFont(font)
604 self._completion_widget.setFont(font)
605 self._control.document().setDefaultFont(font)
605 self._control.document().setDefaultFont(font)
606 if self._page_control:
606 if self._page_control:
607 self._page_control.document().setDefaultFont(font)
607 self._page_control.document().setDefaultFont(font)
608
608
609 self.font_changed.emit(font)
609 self.font_changed.emit(font)
610
610
611 font = property(_get_font, _set_font)
611 font = property(_get_font, _set_font)
612
612
613 def paste(self, mode=QtGui.QClipboard.Clipboard):
613 def paste(self, mode=QtGui.QClipboard.Clipboard):
614 """ Paste the contents of the clipboard into the input region.
614 """ Paste the contents of the clipboard into the input region.
615
615
616 Parameters:
616 Parameters:
617 -----------
617 -----------
618 mode : QClipboard::Mode, optional [default QClipboard::Clipboard]
618 mode : QClipboard::Mode, optional [default QClipboard::Clipboard]
619
619
620 Controls which part of the system clipboard is used. This can be
620 Controls which part of the system clipboard is used. This can be
621 used to access the selection clipboard in X11 and the Find buffer
621 used to access the selection clipboard in X11 and the Find buffer
622 in Mac OS. By default, the regular clipboard is used.
622 in Mac OS. By default, the regular clipboard is used.
623 """
623 """
624 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
624 if self._control.textInteractionFlags() & QtCore.Qt.TextEditable:
625 # Make sure the paste is safe.
625 # Make sure the paste is safe.
626 self._keep_cursor_in_buffer()
626 self._keep_cursor_in_buffer()
627 cursor = self._control.textCursor()
627 cursor = self._control.textCursor()
628
628
629 # Remove any trailing newline, which confuses the GUI and forces the
629 # Remove any trailing newline, which confuses the GUI and forces the
630 # user to backspace.
630 # user to backspace.
631 text = QtGui.QApplication.clipboard().text(mode).rstrip()
631 text = QtGui.QApplication.clipboard().text(mode).rstrip()
632 self._insert_plain_text_into_buffer(cursor, dedent(text))
632 self._insert_plain_text_into_buffer(cursor, dedent(text))
633
633
634 def print_(self, printer = None):
634 def print_(self, printer = None):
635 """ Print the contents of the ConsoleWidget to the specified QPrinter.
635 """ Print the contents of the ConsoleWidget to the specified QPrinter.
636 """
636 """
637 if (not printer):
637 if (not printer):
638 printer = QtGui.QPrinter()
638 printer = QtGui.QPrinter()
639 if(QtGui.QPrintDialog(printer).exec_() != QtGui.QDialog.Accepted):
639 if(QtGui.QPrintDialog(printer).exec_() != QtGui.QDialog.Accepted):
640 return
640 return
641 self._control.print_(printer)
641 self._control.print_(printer)
642
642
643 def prompt_to_top(self):
643 def prompt_to_top(self):
644 """ Moves the prompt to the top of the viewport.
644 """ Moves the prompt to the top of the viewport.
645 """
645 """
646 if not self._executing:
646 if not self._executing:
647 prompt_cursor = self._get_prompt_cursor()
647 prompt_cursor = self._get_prompt_cursor()
648 if self._get_cursor().blockNumber() < prompt_cursor.blockNumber():
648 if self._get_cursor().blockNumber() < prompt_cursor.blockNumber():
649 self._set_cursor(prompt_cursor)
649 self._set_cursor(prompt_cursor)
650 self._set_top_cursor(prompt_cursor)
650 self._set_top_cursor(prompt_cursor)
651
651
652 def redo(self):
652 def redo(self):
653 """ Redo the last operation. If there is no operation to redo, nothing
653 """ Redo the last operation. If there is no operation to redo, nothing
654 happens.
654 happens.
655 """
655 """
656 self._control.redo()
656 self._control.redo()
657
657
658 def reset_font(self):
658 def reset_font(self):
659 """ Sets the font to the default fixed-width font for this platform.
659 """ Sets the font to the default fixed-width font for this platform.
660 """
660 """
661 if sys.platform == 'win32':
661 if sys.platform == 'win32':
662 # Consolas ships with Vista/Win7, fallback to Courier if needed
662 # Consolas ships with Vista/Win7, fallback to Courier if needed
663 fallback = 'Courier'
663 fallback = 'Courier'
664 elif sys.platform == 'darwin':
664 elif sys.platform == 'darwin':
665 # OSX always has Monaco
665 # OSX always has Monaco
666 fallback = 'Monaco'
666 fallback = 'Monaco'
667 else:
667 else:
668 # Monospace should always exist
668 # Monospace should always exist
669 fallback = 'Monospace'
669 fallback = 'Monospace'
670 font = get_font(self.font_family, fallback)
670 font = get_font(self.font_family, fallback)
671 if self.font_size:
671 if self.font_size:
672 font.setPointSize(self.font_size)
672 font.setPointSize(self.font_size)
673 else:
673 else:
674 font.setPointSize(QtGui.qApp.font().pointSize())
674 font.setPointSize(QtGui.qApp.font().pointSize())
675 font.setStyleHint(QtGui.QFont.TypeWriter)
675 font.setStyleHint(QtGui.QFont.TypeWriter)
676 self._set_font(font)
676 self._set_font(font)
677
677
678 def change_font_size(self, delta):
678 def change_font_size(self, delta):
679 """Change the font size by the specified amount (in points).
679 """Change the font size by the specified amount (in points).
680 """
680 """
681 font = self.font
681 font = self.font
682 size = max(font.pointSize() + delta, 1) # minimum 1 point
682 size = max(font.pointSize() + delta, 1) # minimum 1 point
683 font.setPointSize(size)
683 font.setPointSize(size)
684 self._set_font(font)
684 self._set_font(font)
685
685
686 def _increase_font_size(self):
686 def _increase_font_size(self):
687 self.change_font_size(1)
687 self.change_font_size(1)
688
688
689 def _decrease_font_size(self):
689 def _decrease_font_size(self):
690 self.change_font_size(-1)
690 self.change_font_size(-1)
691
691
692 def select_all(self):
692 def select_all(self):
693 """ Selects all the text in the buffer.
693 """ Selects all the text in the buffer.
694 """
694 """
695 self._control.selectAll()
695 self._control.selectAll()
696
696
697 def _get_tab_width(self):
697 def _get_tab_width(self):
698 """ The width (in terms of space characters) for tab characters.
698 """ The width (in terms of space characters) for tab characters.
699 """
699 """
700 return self._tab_width
700 return self._tab_width
701
701
702 def _set_tab_width(self, tab_width):
702 def _set_tab_width(self, tab_width):
703 """ Sets the width (in terms of space characters) for tab characters.
703 """ Sets the width (in terms of space characters) for tab characters.
704 """
704 """
705 font_metrics = QtGui.QFontMetrics(self.font)
705 font_metrics = QtGui.QFontMetrics(self.font)
706 self._control.setTabStopWidth(tab_width * font_metrics.width(' '))
706 self._control.setTabStopWidth(tab_width * font_metrics.width(' '))
707
707
708 self._tab_width = tab_width
708 self._tab_width = tab_width
709
709
710 tab_width = property(_get_tab_width, _set_tab_width)
710 tab_width = property(_get_tab_width, _set_tab_width)
711
711
712 def undo(self):
712 def undo(self):
713 """ Undo the last operation. If there is no operation to undo, nothing
713 """ Undo the last operation. If there is no operation to undo, nothing
714 happens.
714 happens.
715 """
715 """
716 self._control.undo()
716 self._control.undo()
717
717
718 #---------------------------------------------------------------------------
718 #---------------------------------------------------------------------------
719 # 'ConsoleWidget' abstract interface
719 # 'ConsoleWidget' abstract interface
720 #---------------------------------------------------------------------------
720 #---------------------------------------------------------------------------
721
721
722 def _is_complete(self, source, interactive):
722 def _is_complete(self, source, interactive):
723 """ Returns whether 'source' can be executed. When triggered by an
723 """ Returns whether 'source' can be executed. When triggered by an
724 Enter/Return key press, 'interactive' is True; otherwise, it is
724 Enter/Return key press, 'interactive' is True; otherwise, it is
725 False.
725 False.
726 """
726 """
727 raise NotImplementedError
727 raise NotImplementedError
728
728
729 def _execute(self, source, hidden):
729 def _execute(self, source, hidden):
730 """ Execute 'source'. If 'hidden', do not show any output.
730 """ Execute 'source'. If 'hidden', do not show any output.
731 """
731 """
732 raise NotImplementedError
732 raise NotImplementedError
733
733
734 def _prompt_started_hook(self):
734 def _prompt_started_hook(self):
735 """ Called immediately after a new prompt is displayed.
735 """ Called immediately after a new prompt is displayed.
736 """
736 """
737 pass
737 pass
738
738
739 def _prompt_finished_hook(self):
739 def _prompt_finished_hook(self):
740 """ Called immediately after a prompt is finished, i.e. when some input
740 """ Called immediately after a prompt is finished, i.e. when some input
741 will be processed and a new prompt displayed.
741 will be processed and a new prompt displayed.
742 """
742 """
743 pass
743 pass
744
744
745 def _up_pressed(self, shift_modifier):
745 def _up_pressed(self, shift_modifier):
746 """ Called when the up key is pressed. Returns whether to continue
746 """ Called when the up key is pressed. Returns whether to continue
747 processing the event.
747 processing the event.
748 """
748 """
749 return True
749 return True
750
750
751 def _down_pressed(self, shift_modifier):
751 def _down_pressed(self, shift_modifier):
752 """ Called when the down key is pressed. Returns whether to continue
752 """ Called when the down key is pressed. Returns whether to continue
753 processing the event.
753 processing the event.
754 """
754 """
755 return True
755 return True
756
756
757 def _tab_pressed(self):
757 def _tab_pressed(self):
758 """ Called when the tab key is pressed. Returns whether to continue
758 """ Called when the tab key is pressed. Returns whether to continue
759 processing the event.
759 processing the event.
760 """
760 """
761 return False
761 return False
762
762
763 #--------------------------------------------------------------------------
763 #--------------------------------------------------------------------------
764 # 'ConsoleWidget' protected interface
764 # 'ConsoleWidget' protected interface
765 #--------------------------------------------------------------------------
765 #--------------------------------------------------------------------------
766
766
767 def _append_custom(self, insert, input, before_prompt=False):
767 def _append_custom(self, insert, input, before_prompt=False):
768 """ A low-level method for appending content to the end of the buffer.
768 """ A low-level method for appending content to the end of the buffer.
769
769
770 If 'before_prompt' is enabled, the content will be inserted before the
770 If 'before_prompt' is enabled, the content will be inserted before the
771 current prompt, if there is one.
771 current prompt, if there is one.
772 """
772 """
773 # Determine where to insert the content.
773 # Determine where to insert the content.
774 cursor = self._control.textCursor()
774 cursor = self._control.textCursor()
775 if before_prompt and not self._executing:
775 if before_prompt and (self._reading or not self._executing):
776 cursor.setPosition(self._append_before_prompt_pos)
776 cursor.setPosition(self._append_before_prompt_pos)
777 else:
777 else:
778 cursor.movePosition(QtGui.QTextCursor.End)
778 cursor.movePosition(QtGui.QTextCursor.End)
779 start_pos = cursor.position()
779 start_pos = cursor.position()
780
780
781 # Perform the insertion.
781 # Perform the insertion.
782 result = insert(cursor, input)
782 result = insert(cursor, input)
783
783
784 # Adjust the prompt position if we have inserted before it. This is safe
784 # Adjust the prompt position if we have inserted before it. This is safe
785 # because buffer truncation is disabled when not executing.
785 # because buffer truncation is disabled when not executing.
786 if before_prompt and not self._executing:
786 if before_prompt and not self._executing:
787 diff = cursor.position() - start_pos
787 diff = cursor.position() - start_pos
788 self._append_before_prompt_pos += diff
788 self._append_before_prompt_pos += diff
789 self._prompt_pos += diff
789 self._prompt_pos += diff
790
790
791 return result
791 return result
792
792
793 def _append_html(self, html, before_prompt=False):
793 def _append_html(self, html, before_prompt=False):
794 """ Appends HTML at the end of the console buffer.
794 """ Appends HTML at the end of the console buffer.
795 """
795 """
796 self._append_custom(self._insert_html, html, before_prompt)
796 self._append_custom(self._insert_html, html, before_prompt)
797
797
798 def _append_html_fetching_plain_text(self, html, before_prompt=False):
798 def _append_html_fetching_plain_text(self, html, before_prompt=False):
799 """ Appends HTML, then returns the plain text version of it.
799 """ Appends HTML, then returns the plain text version of it.
800 """
800 """
801 return self._append_custom(self._insert_html_fetching_plain_text,
801 return self._append_custom(self._insert_html_fetching_plain_text,
802 html, before_prompt)
802 html, before_prompt)
803
803
804 def _append_plain_text(self, text, before_prompt=False):
804 def _append_plain_text(self, text, before_prompt=False):
805 """ Appends plain text, processing ANSI codes if enabled.
805 """ Appends plain text, processing ANSI codes if enabled.
806 """
806 """
807 self._append_custom(self._insert_plain_text, text, before_prompt)
807 self._append_custom(self._insert_plain_text, text, before_prompt)
808
808
809 def _cancel_text_completion(self):
809 def _cancel_text_completion(self):
810 """ If text completion is progress, cancel it.
810 """ If text completion is progress, cancel it.
811 """
811 """
812 if self._text_completing_pos:
812 if self._text_completing_pos:
813 self._clear_temporary_buffer()
813 self._clear_temporary_buffer()
814 self._text_completing_pos = 0
814 self._text_completing_pos = 0
815
815
816 def _clear_temporary_buffer(self):
816 def _clear_temporary_buffer(self):
817 """ Clears the "temporary text" buffer, i.e. all the text following
817 """ Clears the "temporary text" buffer, i.e. all the text following
818 the prompt region.
818 the prompt region.
819 """
819 """
820 # Select and remove all text below the input buffer.
820 # Select and remove all text below the input buffer.
821 cursor = self._get_prompt_cursor()
821 cursor = self._get_prompt_cursor()
822 prompt = self._continuation_prompt.lstrip()
822 prompt = self._continuation_prompt.lstrip()
823 while cursor.movePosition(QtGui.QTextCursor.NextBlock):
823 while cursor.movePosition(QtGui.QTextCursor.NextBlock):
824 temp_cursor = QtGui.QTextCursor(cursor)
824 temp_cursor = QtGui.QTextCursor(cursor)
825 temp_cursor.select(QtGui.QTextCursor.BlockUnderCursor)
825 temp_cursor.select(QtGui.QTextCursor.BlockUnderCursor)
826 text = temp_cursor.selection().toPlainText().lstrip()
826 text = temp_cursor.selection().toPlainText().lstrip()
827 if not text.startswith(prompt):
827 if not text.startswith(prompt):
828 break
828 break
829 else:
829 else:
830 # We've reached the end of the input buffer and no text follows.
830 # We've reached the end of the input buffer and no text follows.
831 return
831 return
832 cursor.movePosition(QtGui.QTextCursor.Left) # Grab the newline.
832 cursor.movePosition(QtGui.QTextCursor.Left) # Grab the newline.
833 cursor.movePosition(QtGui.QTextCursor.End,
833 cursor.movePosition(QtGui.QTextCursor.End,
834 QtGui.QTextCursor.KeepAnchor)
834 QtGui.QTextCursor.KeepAnchor)
835 cursor.removeSelectedText()
835 cursor.removeSelectedText()
836
836
837 # After doing this, we have no choice but to clear the undo/redo
837 # After doing this, we have no choice but to clear the undo/redo
838 # history. Otherwise, the text is not "temporary" at all, because it
838 # history. Otherwise, the text is not "temporary" at all, because it
839 # can be recalled with undo/redo. Unfortunately, Qt does not expose
839 # can be recalled with undo/redo. Unfortunately, Qt does not expose
840 # fine-grained control to the undo/redo system.
840 # fine-grained control to the undo/redo system.
841 if self._control.isUndoRedoEnabled():
841 if self._control.isUndoRedoEnabled():
842 self._control.setUndoRedoEnabled(False)
842 self._control.setUndoRedoEnabled(False)
843 self._control.setUndoRedoEnabled(True)
843 self._control.setUndoRedoEnabled(True)
844
844
845 def _complete_with_items(self, cursor, items):
845 def _complete_with_items(self, cursor, items):
846 """ Performs completion with 'items' at the specified cursor location.
846 """ Performs completion with 'items' at the specified cursor location.
847 """
847 """
848 self._cancel_text_completion()
848 self._cancel_text_completion()
849
849
850 if len(items) == 1:
850 if len(items) == 1:
851 cursor.setPosition(self._control.textCursor().position(),
851 cursor.setPosition(self._control.textCursor().position(),
852 QtGui.QTextCursor.KeepAnchor)
852 QtGui.QTextCursor.KeepAnchor)
853 cursor.insertText(items[0])
853 cursor.insertText(items[0])
854
854
855 elif len(items) > 1:
855 elif len(items) > 1:
856 current_pos = self._control.textCursor().position()
856 current_pos = self._control.textCursor().position()
857 prefix = commonprefix(items)
857 prefix = commonprefix(items)
858 if prefix:
858 if prefix:
859 cursor.setPosition(current_pos, QtGui.QTextCursor.KeepAnchor)
859 cursor.setPosition(current_pos, QtGui.QTextCursor.KeepAnchor)
860 cursor.insertText(prefix)
860 cursor.insertText(prefix)
861 current_pos = cursor.position()
861 current_pos = cursor.position()
862
862
863 if self.gui_completion:
863 if self.gui_completion:
864 cursor.movePosition(QtGui.QTextCursor.Left, n=len(prefix))
864 cursor.movePosition(QtGui.QTextCursor.Left, n=len(prefix))
865 self._completion_widget.show_items(cursor, items)
865 self._completion_widget.show_items(cursor, items)
866 else:
866 else:
867 cursor.beginEditBlock()
867 cursor.beginEditBlock()
868 self._append_plain_text('\n')
868 self._append_plain_text('\n')
869 self._page(self._format_as_columns(items))
869 self._page(self._format_as_columns(items))
870 cursor.endEditBlock()
870 cursor.endEditBlock()
871
871
872 cursor.setPosition(current_pos)
872 cursor.setPosition(current_pos)
873 self._control.moveCursor(QtGui.QTextCursor.End)
873 self._control.moveCursor(QtGui.QTextCursor.End)
874 self._control.setTextCursor(cursor)
874 self._control.setTextCursor(cursor)
875 self._text_completing_pos = current_pos
875 self._text_completing_pos = current_pos
876
876
877 def _context_menu_make(self, pos):
877 def _context_menu_make(self, pos):
878 """ Creates a context menu for the given QPoint (in widget coordinates).
878 """ Creates a context menu for the given QPoint (in widget coordinates).
879 """
879 """
880 menu = QtGui.QMenu(self)
880 menu = QtGui.QMenu(self)
881
881
882 self.cut_action = menu.addAction('Cut', self.cut)
882 self.cut_action = menu.addAction('Cut', self.cut)
883 self.cut_action.setEnabled(self.can_cut())
883 self.cut_action.setEnabled(self.can_cut())
884 self.cut_action.setShortcut(QtGui.QKeySequence.Cut)
884 self.cut_action.setShortcut(QtGui.QKeySequence.Cut)
885
885
886 self.copy_action = menu.addAction('Copy', self.copy)
886 self.copy_action = menu.addAction('Copy', self.copy)
887 self.copy_action.setEnabled(self.can_copy())
887 self.copy_action.setEnabled(self.can_copy())
888 self.copy_action.setShortcut(QtGui.QKeySequence.Copy)
888 self.copy_action.setShortcut(QtGui.QKeySequence.Copy)
889
889
890 self.paste_action = menu.addAction('Paste', self.paste)
890 self.paste_action = menu.addAction('Paste', self.paste)
891 self.paste_action.setEnabled(self.can_paste())
891 self.paste_action.setEnabled(self.can_paste())
892 self.paste_action.setShortcut(QtGui.QKeySequence.Paste)
892 self.paste_action.setShortcut(QtGui.QKeySequence.Paste)
893
893
894 menu.addSeparator()
894 menu.addSeparator()
895 menu.addAction(self.select_all_action)
895 menu.addAction(self.select_all_action)
896
896
897 menu.addSeparator()
897 menu.addSeparator()
898 menu.addAction(self.export_action)
898 menu.addAction(self.export_action)
899 menu.addAction(self.print_action)
899 menu.addAction(self.print_action)
900
900
901 return menu
901 return menu
902
902
903 def _control_key_down(self, modifiers, include_command=False):
903 def _control_key_down(self, modifiers, include_command=False):
904 """ Given a KeyboardModifiers flags object, return whether the Control
904 """ Given a KeyboardModifiers flags object, return whether the Control
905 key is down.
905 key is down.
906
906
907 Parameters:
907 Parameters:
908 -----------
908 -----------
909 include_command : bool, optional (default True)
909 include_command : bool, optional (default True)
910 Whether to treat the Command key as a (mutually exclusive) synonym
910 Whether to treat the Command key as a (mutually exclusive) synonym
911 for Control when in Mac OS.
911 for Control when in Mac OS.
912 """
912 """
913 # Note that on Mac OS, ControlModifier corresponds to the Command key
913 # Note that on Mac OS, ControlModifier corresponds to the Command key
914 # while MetaModifier corresponds to the Control key.
914 # while MetaModifier corresponds to the Control key.
915 if sys.platform == 'darwin':
915 if sys.platform == 'darwin':
916 down = include_command and (modifiers & QtCore.Qt.ControlModifier)
916 down = include_command and (modifiers & QtCore.Qt.ControlModifier)
917 return bool(down) ^ bool(modifiers & QtCore.Qt.MetaModifier)
917 return bool(down) ^ bool(modifiers & QtCore.Qt.MetaModifier)
918 else:
918 else:
919 return bool(modifiers & QtCore.Qt.ControlModifier)
919 return bool(modifiers & QtCore.Qt.ControlModifier)
920
920
921 def _create_control(self):
921 def _create_control(self):
922 """ Creates and connects the underlying text widget.
922 """ Creates and connects the underlying text widget.
923 """
923 """
924 # Create the underlying control.
924 # Create the underlying control.
925 if self.kind == 'plain':
925 if self.kind == 'plain':
926 control = QtGui.QPlainTextEdit()
926 control = QtGui.QPlainTextEdit()
927 elif self.kind == 'rich':
927 elif self.kind == 'rich':
928 control = QtGui.QTextEdit()
928 control = QtGui.QTextEdit()
929 control.setAcceptRichText(False)
929 control.setAcceptRichText(False)
930
930
931 # Install event filters. The filter on the viewport is needed for
931 # Install event filters. The filter on the viewport is needed for
932 # mouse events and drag events.
932 # mouse events and drag events.
933 control.installEventFilter(self)
933 control.installEventFilter(self)
934 control.viewport().installEventFilter(self)
934 control.viewport().installEventFilter(self)
935
935
936 # Connect signals.
936 # Connect signals.
937 control.cursorPositionChanged.connect(self._cursor_position_changed)
937 control.cursorPositionChanged.connect(self._cursor_position_changed)
938 control.customContextMenuRequested.connect(
938 control.customContextMenuRequested.connect(
939 self._custom_context_menu_requested)
939 self._custom_context_menu_requested)
940 control.copyAvailable.connect(self.copy_available)
940 control.copyAvailable.connect(self.copy_available)
941 control.redoAvailable.connect(self.redo_available)
941 control.redoAvailable.connect(self.redo_available)
942 control.undoAvailable.connect(self.undo_available)
942 control.undoAvailable.connect(self.undo_available)
943
943
944 # Hijack the document size change signal to prevent Qt from adjusting
944 # Hijack the document size change signal to prevent Qt from adjusting
945 # the viewport's scrollbar. We are relying on an implementation detail
945 # the viewport's scrollbar. We are relying on an implementation detail
946 # of Q(Plain)TextEdit here, which is potentially dangerous, but without
946 # of Q(Plain)TextEdit here, which is potentially dangerous, but without
947 # this functionality we cannot create a nice terminal interface.
947 # this functionality we cannot create a nice terminal interface.
948 layout = control.document().documentLayout()
948 layout = control.document().documentLayout()
949 layout.documentSizeChanged.disconnect()
949 layout.documentSizeChanged.disconnect()
950 layout.documentSizeChanged.connect(self._adjust_scrollbars)
950 layout.documentSizeChanged.connect(self._adjust_scrollbars)
951
951
952 # Configure the control.
952 # Configure the control.
953 control.setAttribute(QtCore.Qt.WA_InputMethodEnabled, True)
953 control.setAttribute(QtCore.Qt.WA_InputMethodEnabled, True)
954 control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
954 control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
955 control.setReadOnly(True)
955 control.setReadOnly(True)
956 control.setUndoRedoEnabled(False)
956 control.setUndoRedoEnabled(False)
957 control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
957 control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
958 return control
958 return control
959
959
960 def _create_page_control(self):
960 def _create_page_control(self):
961 """ Creates and connects the underlying paging widget.
961 """ Creates and connects the underlying paging widget.
962 """
962 """
963 if self.kind == 'plain':
963 if self.kind == 'plain':
964 control = QtGui.QPlainTextEdit()
964 control = QtGui.QPlainTextEdit()
965 elif self.kind == 'rich':
965 elif self.kind == 'rich':
966 control = QtGui.QTextEdit()
966 control = QtGui.QTextEdit()
967 control.installEventFilter(self)
967 control.installEventFilter(self)
968 control.setReadOnly(True)
968 control.setReadOnly(True)
969 control.setUndoRedoEnabled(False)
969 control.setUndoRedoEnabled(False)
970 control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
970 control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
971 return control
971 return control
972
972
973 def _event_filter_console_keypress(self, event):
973 def _event_filter_console_keypress(self, event):
974 """ Filter key events for the underlying text widget to create a
974 """ Filter key events for the underlying text widget to create a
975 console-like interface.
975 console-like interface.
976 """
976 """
977 intercepted = False
977 intercepted = False
978 cursor = self._control.textCursor()
978 cursor = self._control.textCursor()
979 position = cursor.position()
979 position = cursor.position()
980 key = event.key()
980 key = event.key()
981 ctrl_down = self._control_key_down(event.modifiers())
981 ctrl_down = self._control_key_down(event.modifiers())
982 alt_down = event.modifiers() & QtCore.Qt.AltModifier
982 alt_down = event.modifiers() & QtCore.Qt.AltModifier
983 shift_down = event.modifiers() & QtCore.Qt.ShiftModifier
983 shift_down = event.modifiers() & QtCore.Qt.ShiftModifier
984
984
985 #------ Special sequences ----------------------------------------------
985 #------ Special sequences ----------------------------------------------
986
986
987 if event.matches(QtGui.QKeySequence.Copy):
987 if event.matches(QtGui.QKeySequence.Copy):
988 self.copy()
988 self.copy()
989 intercepted = True
989 intercepted = True
990
990
991 elif event.matches(QtGui.QKeySequence.Cut):
991 elif event.matches(QtGui.QKeySequence.Cut):
992 self.cut()
992 self.cut()
993 intercepted = True
993 intercepted = True
994
994
995 elif event.matches(QtGui.QKeySequence.Paste):
995 elif event.matches(QtGui.QKeySequence.Paste):
996 self.paste()
996 self.paste()
997 intercepted = True
997 intercepted = True
998
998
999 #------ Special modifier logic -----------------------------------------
999 #------ Special modifier logic -----------------------------------------
1000
1000
1001 elif key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
1001 elif key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
1002 intercepted = True
1002 intercepted = True
1003
1003
1004 # Special handling when tab completing in text mode.
1004 # Special handling when tab completing in text mode.
1005 self._cancel_text_completion()
1005 self._cancel_text_completion()
1006
1006
1007 if self._in_buffer(position):
1007 if self._in_buffer(position):
1008 # Special handling when a reading a line of raw input.
1008 # Special handling when a reading a line of raw input.
1009 if self._reading:
1009 if self._reading:
1010 self._append_plain_text('\n')
1010 self._append_plain_text('\n')
1011 self._reading = False
1011 self._reading = False
1012 if self._reading_callback:
1012 if self._reading_callback:
1013 self._reading_callback()
1013 self._reading_callback()
1014
1014
1015 # If the input buffer is a single line or there is only
1015 # If the input buffer is a single line or there is only
1016 # whitespace after the cursor, execute. Otherwise, split the
1016 # whitespace after the cursor, execute. Otherwise, split the
1017 # line with a continuation prompt.
1017 # line with a continuation prompt.
1018 elif not self._executing:
1018 elif not self._executing:
1019 cursor.movePosition(QtGui.QTextCursor.End,
1019 cursor.movePosition(QtGui.QTextCursor.End,
1020 QtGui.QTextCursor.KeepAnchor)
1020 QtGui.QTextCursor.KeepAnchor)
1021 at_end = len(cursor.selectedText().strip()) == 0
1021 at_end = len(cursor.selectedText().strip()) == 0
1022 single_line = (self._get_end_cursor().blockNumber() ==
1022 single_line = (self._get_end_cursor().blockNumber() ==
1023 self._get_prompt_cursor().blockNumber())
1023 self._get_prompt_cursor().blockNumber())
1024 if (at_end or shift_down or single_line) and not ctrl_down:
1024 if (at_end or shift_down or single_line) and not ctrl_down:
1025 self.execute(interactive = not shift_down)
1025 self.execute(interactive = not shift_down)
1026 else:
1026 else:
1027 # Do this inside an edit block for clean undo/redo.
1027 # Do this inside an edit block for clean undo/redo.
1028 cursor.beginEditBlock()
1028 cursor.beginEditBlock()
1029 cursor.setPosition(position)
1029 cursor.setPosition(position)
1030 cursor.insertText('\n')
1030 cursor.insertText('\n')
1031 self._insert_continuation_prompt(cursor)
1031 self._insert_continuation_prompt(cursor)
1032 cursor.endEditBlock()
1032 cursor.endEditBlock()
1033
1033
1034 # Ensure that the whole input buffer is visible.
1034 # Ensure that the whole input buffer is visible.
1035 # FIXME: This will not be usable if the input buffer is
1035 # FIXME: This will not be usable if the input buffer is
1036 # taller than the console widget.
1036 # taller than the console widget.
1037 self._control.moveCursor(QtGui.QTextCursor.End)
1037 self._control.moveCursor(QtGui.QTextCursor.End)
1038 self._control.setTextCursor(cursor)
1038 self._control.setTextCursor(cursor)
1039
1039
1040 #------ Control/Cmd modifier -------------------------------------------
1040 #------ Control/Cmd modifier -------------------------------------------
1041
1041
1042 elif ctrl_down:
1042 elif ctrl_down:
1043 if key == QtCore.Qt.Key_G:
1043 if key == QtCore.Qt.Key_G:
1044 self._keyboard_quit()
1044 self._keyboard_quit()
1045 intercepted = True
1045 intercepted = True
1046
1046
1047 elif key == QtCore.Qt.Key_K:
1047 elif key == QtCore.Qt.Key_K:
1048 if self._in_buffer(position):
1048 if self._in_buffer(position):
1049 cursor.movePosition(QtGui.QTextCursor.EndOfLine,
1049 cursor.movePosition(QtGui.QTextCursor.EndOfLine,
1050 QtGui.QTextCursor.KeepAnchor)
1050 QtGui.QTextCursor.KeepAnchor)
1051 if not cursor.hasSelection():
1051 if not cursor.hasSelection():
1052 # Line deletion (remove continuation prompt)
1052 # Line deletion (remove continuation prompt)
1053 cursor.movePosition(QtGui.QTextCursor.NextBlock,
1053 cursor.movePosition(QtGui.QTextCursor.NextBlock,
1054 QtGui.QTextCursor.KeepAnchor)
1054 QtGui.QTextCursor.KeepAnchor)
1055 cursor.movePosition(QtGui.QTextCursor.Right,
1055 cursor.movePosition(QtGui.QTextCursor.Right,
1056 QtGui.QTextCursor.KeepAnchor,
1056 QtGui.QTextCursor.KeepAnchor,
1057 len(self._continuation_prompt))
1057 len(self._continuation_prompt))
1058 self._kill_ring.kill_cursor(cursor)
1058 self._kill_ring.kill_cursor(cursor)
1059 intercepted = True
1059 intercepted = True
1060
1060
1061 elif key == QtCore.Qt.Key_L:
1061 elif key == QtCore.Qt.Key_L:
1062 self.prompt_to_top()
1062 self.prompt_to_top()
1063 intercepted = True
1063 intercepted = True
1064
1064
1065 elif key == QtCore.Qt.Key_O:
1065 elif key == QtCore.Qt.Key_O:
1066 if self._page_control and self._page_control.isVisible():
1066 if self._page_control and self._page_control.isVisible():
1067 self._page_control.setFocus()
1067 self._page_control.setFocus()
1068 intercepted = True
1068 intercepted = True
1069
1069
1070 elif key == QtCore.Qt.Key_U:
1070 elif key == QtCore.Qt.Key_U:
1071 if self._in_buffer(position):
1071 if self._in_buffer(position):
1072 start_line = cursor.blockNumber()
1072 start_line = cursor.blockNumber()
1073 if start_line == self._get_prompt_cursor().blockNumber():
1073 if start_line == self._get_prompt_cursor().blockNumber():
1074 offset = len(self._prompt)
1074 offset = len(self._prompt)
1075 else:
1075 else:
1076 offset = len(self._continuation_prompt)
1076 offset = len(self._continuation_prompt)
1077 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1077 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1078 QtGui.QTextCursor.KeepAnchor)
1078 QtGui.QTextCursor.KeepAnchor)
1079 cursor.movePosition(QtGui.QTextCursor.Right,
1079 cursor.movePosition(QtGui.QTextCursor.Right,
1080 QtGui.QTextCursor.KeepAnchor, offset)
1080 QtGui.QTextCursor.KeepAnchor, offset)
1081 self._kill_ring.kill_cursor(cursor)
1081 self._kill_ring.kill_cursor(cursor)
1082 intercepted = True
1082 intercepted = True
1083
1083
1084 elif key == QtCore.Qt.Key_Y:
1084 elif key == QtCore.Qt.Key_Y:
1085 self._keep_cursor_in_buffer()
1085 self._keep_cursor_in_buffer()
1086 self._kill_ring.yank()
1086 self._kill_ring.yank()
1087 intercepted = True
1087 intercepted = True
1088
1088
1089 elif key in (QtCore.Qt.Key_Backspace, QtCore.Qt.Key_Delete):
1089 elif key in (QtCore.Qt.Key_Backspace, QtCore.Qt.Key_Delete):
1090 if key == QtCore.Qt.Key_Backspace:
1090 if key == QtCore.Qt.Key_Backspace:
1091 cursor = self._get_word_start_cursor(position)
1091 cursor = self._get_word_start_cursor(position)
1092 else: # key == QtCore.Qt.Key_Delete
1092 else: # key == QtCore.Qt.Key_Delete
1093 cursor = self._get_word_end_cursor(position)
1093 cursor = self._get_word_end_cursor(position)
1094 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1094 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1095 self._kill_ring.kill_cursor(cursor)
1095 self._kill_ring.kill_cursor(cursor)
1096 intercepted = True
1096 intercepted = True
1097
1097
1098 #------ Alt modifier ---------------------------------------------------
1098 #------ Alt modifier ---------------------------------------------------
1099
1099
1100 elif alt_down:
1100 elif alt_down:
1101 if key == QtCore.Qt.Key_B:
1101 if key == QtCore.Qt.Key_B:
1102 self._set_cursor(self._get_word_start_cursor(position))
1102 self._set_cursor(self._get_word_start_cursor(position))
1103 intercepted = True
1103 intercepted = True
1104
1104
1105 elif key == QtCore.Qt.Key_F:
1105 elif key == QtCore.Qt.Key_F:
1106 self._set_cursor(self._get_word_end_cursor(position))
1106 self._set_cursor(self._get_word_end_cursor(position))
1107 intercepted = True
1107 intercepted = True
1108
1108
1109 elif key == QtCore.Qt.Key_Y:
1109 elif key == QtCore.Qt.Key_Y:
1110 self._kill_ring.rotate()
1110 self._kill_ring.rotate()
1111 intercepted = True
1111 intercepted = True
1112
1112
1113 elif key == QtCore.Qt.Key_Backspace:
1113 elif key == QtCore.Qt.Key_Backspace:
1114 cursor = self._get_word_start_cursor(position)
1114 cursor = self._get_word_start_cursor(position)
1115 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1115 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1116 self._kill_ring.kill_cursor(cursor)
1116 self._kill_ring.kill_cursor(cursor)
1117 intercepted = True
1117 intercepted = True
1118
1118
1119 elif key == QtCore.Qt.Key_D:
1119 elif key == QtCore.Qt.Key_D:
1120 cursor = self._get_word_end_cursor(position)
1120 cursor = self._get_word_end_cursor(position)
1121 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1121 cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor)
1122 self._kill_ring.kill_cursor(cursor)
1122 self._kill_ring.kill_cursor(cursor)
1123 intercepted = True
1123 intercepted = True
1124
1124
1125 elif key == QtCore.Qt.Key_Delete:
1125 elif key == QtCore.Qt.Key_Delete:
1126 intercepted = True
1126 intercepted = True
1127
1127
1128 elif key == QtCore.Qt.Key_Greater:
1128 elif key == QtCore.Qt.Key_Greater:
1129 self._control.moveCursor(QtGui.QTextCursor.End)
1129 self._control.moveCursor(QtGui.QTextCursor.End)
1130 intercepted = True
1130 intercepted = True
1131
1131
1132 elif key == QtCore.Qt.Key_Less:
1132 elif key == QtCore.Qt.Key_Less:
1133 self._control.setTextCursor(self._get_prompt_cursor())
1133 self._control.setTextCursor(self._get_prompt_cursor())
1134 intercepted = True
1134 intercepted = True
1135
1135
1136 #------ No modifiers ---------------------------------------------------
1136 #------ No modifiers ---------------------------------------------------
1137
1137
1138 else:
1138 else:
1139 if shift_down:
1139 if shift_down:
1140 anchormode = QtGui.QTextCursor.KeepAnchor
1140 anchormode = QtGui.QTextCursor.KeepAnchor
1141 else:
1141 else:
1142 anchormode = QtGui.QTextCursor.MoveAnchor
1142 anchormode = QtGui.QTextCursor.MoveAnchor
1143
1143
1144 if key == QtCore.Qt.Key_Escape:
1144 if key == QtCore.Qt.Key_Escape:
1145 self._keyboard_quit()
1145 self._keyboard_quit()
1146 intercepted = True
1146 intercepted = True
1147
1147
1148 elif key == QtCore.Qt.Key_Up:
1148 elif key == QtCore.Qt.Key_Up:
1149 if self._reading or not self._up_pressed(shift_down):
1149 if self._reading or not self._up_pressed(shift_down):
1150 intercepted = True
1150 intercepted = True
1151 else:
1151 else:
1152 prompt_line = self._get_prompt_cursor().blockNumber()
1152 prompt_line = self._get_prompt_cursor().blockNumber()
1153 intercepted = cursor.blockNumber() <= prompt_line
1153 intercepted = cursor.blockNumber() <= prompt_line
1154
1154
1155 elif key == QtCore.Qt.Key_Down:
1155 elif key == QtCore.Qt.Key_Down:
1156 if self._reading or not self._down_pressed(shift_down):
1156 if self._reading or not self._down_pressed(shift_down):
1157 intercepted = True
1157 intercepted = True
1158 else:
1158 else:
1159 end_line = self._get_end_cursor().blockNumber()
1159 end_line = self._get_end_cursor().blockNumber()
1160 intercepted = cursor.blockNumber() == end_line
1160 intercepted = cursor.blockNumber() == end_line
1161
1161
1162 elif key == QtCore.Qt.Key_Tab:
1162 elif key == QtCore.Qt.Key_Tab:
1163 if not self._reading:
1163 if not self._reading:
1164 if self._tab_pressed():
1164 if self._tab_pressed():
1165 # real tab-key, insert four spaces
1165 # real tab-key, insert four spaces
1166 cursor.insertText(' '*4)
1166 cursor.insertText(' '*4)
1167 intercepted = True
1167 intercepted = True
1168
1168
1169 elif key == QtCore.Qt.Key_Left:
1169 elif key == QtCore.Qt.Key_Left:
1170
1170
1171 # Move to the previous line
1171 # Move to the previous line
1172 line, col = cursor.blockNumber(), cursor.columnNumber()
1172 line, col = cursor.blockNumber(), cursor.columnNumber()
1173 if line > self._get_prompt_cursor().blockNumber() and \
1173 if line > self._get_prompt_cursor().blockNumber() and \
1174 col == len(self._continuation_prompt):
1174 col == len(self._continuation_prompt):
1175 self._control.moveCursor(QtGui.QTextCursor.PreviousBlock,
1175 self._control.moveCursor(QtGui.QTextCursor.PreviousBlock,
1176 mode=anchormode)
1176 mode=anchormode)
1177 self._control.moveCursor(QtGui.QTextCursor.EndOfBlock,
1177 self._control.moveCursor(QtGui.QTextCursor.EndOfBlock,
1178 mode=anchormode)
1178 mode=anchormode)
1179 intercepted = True
1179 intercepted = True
1180
1180
1181 # Regular left movement
1181 # Regular left movement
1182 else:
1182 else:
1183 intercepted = not self._in_buffer(position - 1)
1183 intercepted = not self._in_buffer(position - 1)
1184
1184
1185 elif key == QtCore.Qt.Key_Right:
1185 elif key == QtCore.Qt.Key_Right:
1186 original_block_number = cursor.blockNumber()
1186 original_block_number = cursor.blockNumber()
1187 cursor.movePosition(QtGui.QTextCursor.Right,
1187 cursor.movePosition(QtGui.QTextCursor.Right,
1188 mode=anchormode)
1188 mode=anchormode)
1189 if cursor.blockNumber() != original_block_number:
1189 if cursor.blockNumber() != original_block_number:
1190 cursor.movePosition(QtGui.QTextCursor.Right,
1190 cursor.movePosition(QtGui.QTextCursor.Right,
1191 n=len(self._continuation_prompt),
1191 n=len(self._continuation_prompt),
1192 mode=anchormode)
1192 mode=anchormode)
1193 self._set_cursor(cursor)
1193 self._set_cursor(cursor)
1194 intercepted = True
1194 intercepted = True
1195
1195
1196 elif key == QtCore.Qt.Key_Home:
1196 elif key == QtCore.Qt.Key_Home:
1197 start_line = cursor.blockNumber()
1197 start_line = cursor.blockNumber()
1198 if start_line == self._get_prompt_cursor().blockNumber():
1198 if start_line == self._get_prompt_cursor().blockNumber():
1199 start_pos = self._prompt_pos
1199 start_pos = self._prompt_pos
1200 else:
1200 else:
1201 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1201 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1202 QtGui.QTextCursor.KeepAnchor)
1202 QtGui.QTextCursor.KeepAnchor)
1203 start_pos = cursor.position()
1203 start_pos = cursor.position()
1204 start_pos += len(self._continuation_prompt)
1204 start_pos += len(self._continuation_prompt)
1205 cursor.setPosition(position)
1205 cursor.setPosition(position)
1206 if shift_down and self._in_buffer(position):
1206 if shift_down and self._in_buffer(position):
1207 cursor.setPosition(start_pos, QtGui.QTextCursor.KeepAnchor)
1207 cursor.setPosition(start_pos, QtGui.QTextCursor.KeepAnchor)
1208 else:
1208 else:
1209 cursor.setPosition(start_pos)
1209 cursor.setPosition(start_pos)
1210 self._set_cursor(cursor)
1210 self._set_cursor(cursor)
1211 intercepted = True
1211 intercepted = True
1212
1212
1213 elif key == QtCore.Qt.Key_Backspace:
1213 elif key == QtCore.Qt.Key_Backspace:
1214
1214
1215 # Line deletion (remove continuation prompt)
1215 # Line deletion (remove continuation prompt)
1216 line, col = cursor.blockNumber(), cursor.columnNumber()
1216 line, col = cursor.blockNumber(), cursor.columnNumber()
1217 if not self._reading and \
1217 if not self._reading and \
1218 col == len(self._continuation_prompt) and \
1218 col == len(self._continuation_prompt) and \
1219 line > self._get_prompt_cursor().blockNumber():
1219 line > self._get_prompt_cursor().blockNumber():
1220 cursor.beginEditBlock()
1220 cursor.beginEditBlock()
1221 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1221 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
1222 QtGui.QTextCursor.KeepAnchor)
1222 QtGui.QTextCursor.KeepAnchor)
1223 cursor.removeSelectedText()
1223 cursor.removeSelectedText()
1224 cursor.deletePreviousChar()
1224 cursor.deletePreviousChar()
1225 cursor.endEditBlock()
1225 cursor.endEditBlock()
1226 intercepted = True
1226 intercepted = True
1227
1227
1228 # Regular backwards deletion
1228 # Regular backwards deletion
1229 else:
1229 else:
1230 anchor = cursor.anchor()
1230 anchor = cursor.anchor()
1231 if anchor == position:
1231 if anchor == position:
1232 intercepted = not self._in_buffer(position - 1)
1232 intercepted = not self._in_buffer(position - 1)
1233 else:
1233 else:
1234 intercepted = not self._in_buffer(min(anchor, position))
1234 intercepted = not self._in_buffer(min(anchor, position))
1235
1235
1236 elif key == QtCore.Qt.Key_Delete:
1236 elif key == QtCore.Qt.Key_Delete:
1237
1237
1238 # Line deletion (remove continuation prompt)
1238 # Line deletion (remove continuation prompt)
1239 if not self._reading and self._in_buffer(position) and \
1239 if not self._reading and self._in_buffer(position) and \
1240 cursor.atBlockEnd() and not cursor.hasSelection():
1240 cursor.atBlockEnd() and not cursor.hasSelection():
1241 cursor.movePosition(QtGui.QTextCursor.NextBlock,
1241 cursor.movePosition(QtGui.QTextCursor.NextBlock,
1242 QtGui.QTextCursor.KeepAnchor)
1242 QtGui.QTextCursor.KeepAnchor)
1243 cursor.movePosition(QtGui.QTextCursor.Right,
1243 cursor.movePosition(QtGui.QTextCursor.Right,
1244 QtGui.QTextCursor.KeepAnchor,
1244 QtGui.QTextCursor.KeepAnchor,
1245 len(self._continuation_prompt))
1245 len(self._continuation_prompt))
1246 cursor.removeSelectedText()
1246 cursor.removeSelectedText()
1247 intercepted = True
1247 intercepted = True
1248
1248
1249 # Regular forwards deletion:
1249 # Regular forwards deletion:
1250 else:
1250 else:
1251 anchor = cursor.anchor()
1251 anchor = cursor.anchor()
1252 intercepted = (not self._in_buffer(anchor) or
1252 intercepted = (not self._in_buffer(anchor) or
1253 not self._in_buffer(position))
1253 not self._in_buffer(position))
1254
1254
1255 # Don't move the cursor if Control/Cmd is pressed to allow copy-paste
1255 # Don't move the cursor if Control/Cmd is pressed to allow copy-paste
1256 # using the keyboard in any part of the buffer. Also, permit scrolling
1256 # using the keyboard in any part of the buffer. Also, permit scrolling
1257 # with Page Up/Down keys. Finally, if we're executing, don't move the
1257 # with Page Up/Down keys. Finally, if we're executing, don't move the
1258 # cursor (if even this made sense, we can't guarantee that the prompt
1258 # cursor (if even this made sense, we can't guarantee that the prompt
1259 # position is still valid due to text truncation).
1259 # position is still valid due to text truncation).
1260 if not (self._control_key_down(event.modifiers(), include_command=True)
1260 if not (self._control_key_down(event.modifiers(), include_command=True)
1261 or key in (QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown)
1261 or key in (QtCore.Qt.Key_PageUp, QtCore.Qt.Key_PageDown)
1262 or (self._executing and not self._reading)):
1262 or (self._executing and not self._reading)):
1263 self._keep_cursor_in_buffer()
1263 self._keep_cursor_in_buffer()
1264
1264
1265 return intercepted
1265 return intercepted
1266
1266
1267 def _event_filter_page_keypress(self, event):
1267 def _event_filter_page_keypress(self, event):
1268 """ Filter key events for the paging widget to create console-like
1268 """ Filter key events for the paging widget to create console-like
1269 interface.
1269 interface.
1270 """
1270 """
1271 key = event.key()
1271 key = event.key()
1272 ctrl_down = self._control_key_down(event.modifiers())
1272 ctrl_down = self._control_key_down(event.modifiers())
1273 alt_down = event.modifiers() & QtCore.Qt.AltModifier
1273 alt_down = event.modifiers() & QtCore.Qt.AltModifier
1274
1274
1275 if ctrl_down:
1275 if ctrl_down:
1276 if key == QtCore.Qt.Key_O:
1276 if key == QtCore.Qt.Key_O:
1277 self._control.setFocus()
1277 self._control.setFocus()
1278 intercept = True
1278 intercept = True
1279
1279
1280 elif alt_down:
1280 elif alt_down:
1281 if key == QtCore.Qt.Key_Greater:
1281 if key == QtCore.Qt.Key_Greater:
1282 self._page_control.moveCursor(QtGui.QTextCursor.End)
1282 self._page_control.moveCursor(QtGui.QTextCursor.End)
1283 intercepted = True
1283 intercepted = True
1284
1284
1285 elif key == QtCore.Qt.Key_Less:
1285 elif key == QtCore.Qt.Key_Less:
1286 self._page_control.moveCursor(QtGui.QTextCursor.Start)
1286 self._page_control.moveCursor(QtGui.QTextCursor.Start)
1287 intercepted = True
1287 intercepted = True
1288
1288
1289 elif key in (QtCore.Qt.Key_Q, QtCore.Qt.Key_Escape):
1289 elif key in (QtCore.Qt.Key_Q, QtCore.Qt.Key_Escape):
1290 if self._splitter:
1290 if self._splitter:
1291 self._page_control.hide()
1291 self._page_control.hide()
1292 self._control.setFocus()
1292 self._control.setFocus()
1293 else:
1293 else:
1294 self.layout().setCurrentWidget(self._control)
1294 self.layout().setCurrentWidget(self._control)
1295 return True
1295 return True
1296
1296
1297 elif key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return,
1297 elif key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return,
1298 QtCore.Qt.Key_Tab):
1298 QtCore.Qt.Key_Tab):
1299 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
1299 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
1300 QtCore.Qt.Key_PageDown,
1300 QtCore.Qt.Key_PageDown,
1301 QtCore.Qt.NoModifier)
1301 QtCore.Qt.NoModifier)
1302 QtGui.qApp.sendEvent(self._page_control, new_event)
1302 QtGui.qApp.sendEvent(self._page_control, new_event)
1303 return True
1303 return True
1304
1304
1305 elif key == QtCore.Qt.Key_Backspace:
1305 elif key == QtCore.Qt.Key_Backspace:
1306 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
1306 new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress,
1307 QtCore.Qt.Key_PageUp,
1307 QtCore.Qt.Key_PageUp,
1308 QtCore.Qt.NoModifier)
1308 QtCore.Qt.NoModifier)
1309 QtGui.qApp.sendEvent(self._page_control, new_event)
1309 QtGui.qApp.sendEvent(self._page_control, new_event)
1310 return True
1310 return True
1311
1311
1312 return False
1312 return False
1313
1313
1314 def _format_as_columns(self, items, separator=' '):
1314 def _format_as_columns(self, items, separator=' '):
1315 """ Transform a list of strings into a single string with columns.
1315 """ Transform a list of strings into a single string with columns.
1316
1316
1317 Parameters
1317 Parameters
1318 ----------
1318 ----------
1319 items : sequence of strings
1319 items : sequence of strings
1320 The strings to process.
1320 The strings to process.
1321
1321
1322 separator : str, optional [default is two spaces]
1322 separator : str, optional [default is two spaces]
1323 The string that separates columns.
1323 The string that separates columns.
1324
1324
1325 Returns
1325 Returns
1326 -------
1326 -------
1327 The formatted string.
1327 The formatted string.
1328 """
1328 """
1329 # Calculate the number of characters available.
1329 # Calculate the number of characters available.
1330 width = self._control.viewport().width()
1330 width = self._control.viewport().width()
1331 char_width = QtGui.QFontMetrics(self.font).width(' ')
1331 char_width = QtGui.QFontMetrics(self.font).width(' ')
1332 displaywidth = max(10, (width / char_width) - 1)
1332 displaywidth = max(10, (width / char_width) - 1)
1333
1333
1334 return columnize(items, separator, displaywidth)
1334 return columnize(items, separator, displaywidth)
1335
1335
1336 def _get_block_plain_text(self, block):
1336 def _get_block_plain_text(self, block):
1337 """ Given a QTextBlock, return its unformatted text.
1337 """ Given a QTextBlock, return its unformatted text.
1338 """
1338 """
1339 cursor = QtGui.QTextCursor(block)
1339 cursor = QtGui.QTextCursor(block)
1340 cursor.movePosition(QtGui.QTextCursor.StartOfBlock)
1340 cursor.movePosition(QtGui.QTextCursor.StartOfBlock)
1341 cursor.movePosition(QtGui.QTextCursor.EndOfBlock,
1341 cursor.movePosition(QtGui.QTextCursor.EndOfBlock,
1342 QtGui.QTextCursor.KeepAnchor)
1342 QtGui.QTextCursor.KeepAnchor)
1343 return cursor.selection().toPlainText()
1343 return cursor.selection().toPlainText()
1344
1344
1345 def _get_cursor(self):
1345 def _get_cursor(self):
1346 """ Convenience method that returns a cursor for the current position.
1346 """ Convenience method that returns a cursor for the current position.
1347 """
1347 """
1348 return self._control.textCursor()
1348 return self._control.textCursor()
1349
1349
1350 def _get_end_cursor(self):
1350 def _get_end_cursor(self):
1351 """ Convenience method that returns a cursor for the last character.
1351 """ Convenience method that returns a cursor for the last character.
1352 """
1352 """
1353 cursor = self._control.textCursor()
1353 cursor = self._control.textCursor()
1354 cursor.movePosition(QtGui.QTextCursor.End)
1354 cursor.movePosition(QtGui.QTextCursor.End)
1355 return cursor
1355 return cursor
1356
1356
1357 def _get_input_buffer_cursor_column(self):
1357 def _get_input_buffer_cursor_column(self):
1358 """ Returns the column of the cursor in the input buffer, excluding the
1358 """ Returns the column of the cursor in the input buffer, excluding the
1359 contribution by the prompt, or -1 if there is no such column.
1359 contribution by the prompt, or -1 if there is no such column.
1360 """
1360 """
1361 prompt = self._get_input_buffer_cursor_prompt()
1361 prompt = self._get_input_buffer_cursor_prompt()
1362 if prompt is None:
1362 if prompt is None:
1363 return -1
1363 return -1
1364 else:
1364 else:
1365 cursor = self._control.textCursor()
1365 cursor = self._control.textCursor()
1366 return cursor.columnNumber() - len(prompt)
1366 return cursor.columnNumber() - len(prompt)
1367
1367
1368 def _get_input_buffer_cursor_line(self):
1368 def _get_input_buffer_cursor_line(self):
1369 """ Returns the text of the line of the input buffer that contains the
1369 """ Returns the text of the line of the input buffer that contains the
1370 cursor, or None if there is no such line.
1370 cursor, or None if there is no such line.
1371 """
1371 """
1372 prompt = self._get_input_buffer_cursor_prompt()
1372 prompt = self._get_input_buffer_cursor_prompt()
1373 if prompt is None:
1373 if prompt is None:
1374 return None
1374 return None
1375 else:
1375 else:
1376 cursor = self._control.textCursor()
1376 cursor = self._control.textCursor()
1377 text = self._get_block_plain_text(cursor.block())
1377 text = self._get_block_plain_text(cursor.block())
1378 return text[len(prompt):]
1378 return text[len(prompt):]
1379
1379
1380 def _get_input_buffer_cursor_prompt(self):
1380 def _get_input_buffer_cursor_prompt(self):
1381 """ Returns the (plain text) prompt for line of the input buffer that
1381 """ Returns the (plain text) prompt for line of the input buffer that
1382 contains the cursor, or None if there is no such line.
1382 contains the cursor, or None if there is no such line.
1383 """
1383 """
1384 if self._executing:
1384 if self._executing:
1385 return None
1385 return None
1386 cursor = self._control.textCursor()
1386 cursor = self._control.textCursor()
1387 if cursor.position() >= self._prompt_pos:
1387 if cursor.position() >= self._prompt_pos:
1388 if cursor.blockNumber() == self._get_prompt_cursor().blockNumber():
1388 if cursor.blockNumber() == self._get_prompt_cursor().blockNumber():
1389 return self._prompt
1389 return self._prompt
1390 else:
1390 else:
1391 return self._continuation_prompt
1391 return self._continuation_prompt
1392 else:
1392 else:
1393 return None
1393 return None
1394
1394
1395 def _get_prompt_cursor(self):
1395 def _get_prompt_cursor(self):
1396 """ Convenience method that returns a cursor for the prompt position.
1396 """ Convenience method that returns a cursor for the prompt position.
1397 """
1397 """
1398 cursor = self._control.textCursor()
1398 cursor = self._control.textCursor()
1399 cursor.setPosition(self._prompt_pos)
1399 cursor.setPosition(self._prompt_pos)
1400 return cursor
1400 return cursor
1401
1401
1402 def _get_selection_cursor(self, start, end):
1402 def _get_selection_cursor(self, start, end):
1403 """ Convenience method that returns a cursor with text selected between
1403 """ Convenience method that returns a cursor with text selected between
1404 the positions 'start' and 'end'.
1404 the positions 'start' and 'end'.
1405 """
1405 """
1406 cursor = self._control.textCursor()
1406 cursor = self._control.textCursor()
1407 cursor.setPosition(start)
1407 cursor.setPosition(start)
1408 cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
1408 cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor)
1409 return cursor
1409 return cursor
1410
1410
1411 def _get_word_start_cursor(self, position):
1411 def _get_word_start_cursor(self, position):
1412 """ Find the start of the word to the left the given position. If a
1412 """ Find the start of the word to the left the given position. If a
1413 sequence of non-word characters precedes the first word, skip over
1413 sequence of non-word characters precedes the first word, skip over
1414 them. (This emulates the behavior of bash, emacs, etc.)
1414 them. (This emulates the behavior of bash, emacs, etc.)
1415 """
1415 """
1416 document = self._control.document()
1416 document = self._control.document()
1417 position -= 1
1417 position -= 1
1418 while position >= self._prompt_pos and \
1418 while position >= self._prompt_pos and \
1419 not is_letter_or_number(document.characterAt(position)):
1419 not is_letter_or_number(document.characterAt(position)):
1420 position -= 1
1420 position -= 1
1421 while position >= self._prompt_pos and \
1421 while position >= self._prompt_pos and \
1422 is_letter_or_number(document.characterAt(position)):
1422 is_letter_or_number(document.characterAt(position)):
1423 position -= 1
1423 position -= 1
1424 cursor = self._control.textCursor()
1424 cursor = self._control.textCursor()
1425 cursor.setPosition(position + 1)
1425 cursor.setPosition(position + 1)
1426 return cursor
1426 return cursor
1427
1427
1428 def _get_word_end_cursor(self, position):
1428 def _get_word_end_cursor(self, position):
1429 """ Find the end of the word to the right the given position. If a
1429 """ Find the end of the word to the right the given position. If a
1430 sequence of non-word characters precedes the first word, skip over
1430 sequence of non-word characters precedes the first word, skip over
1431 them. (This emulates the behavior of bash, emacs, etc.)
1431 them. (This emulates the behavior of bash, emacs, etc.)
1432 """
1432 """
1433 document = self._control.document()
1433 document = self._control.document()
1434 end = self._get_end_cursor().position()
1434 end = self._get_end_cursor().position()
1435 while position < end and \
1435 while position < end and \
1436 not is_letter_or_number(document.characterAt(position)):
1436 not is_letter_or_number(document.characterAt(position)):
1437 position += 1
1437 position += 1
1438 while position < end and \
1438 while position < end and \
1439 is_letter_or_number(document.characterAt(position)):
1439 is_letter_or_number(document.characterAt(position)):
1440 position += 1
1440 position += 1
1441 cursor = self._control.textCursor()
1441 cursor = self._control.textCursor()
1442 cursor.setPosition(position)
1442 cursor.setPosition(position)
1443 return cursor
1443 return cursor
1444
1444
1445 def _insert_continuation_prompt(self, cursor):
1445 def _insert_continuation_prompt(self, cursor):
1446 """ Inserts new continuation prompt using the specified cursor.
1446 """ Inserts new continuation prompt using the specified cursor.
1447 """
1447 """
1448 if self._continuation_prompt_html is None:
1448 if self._continuation_prompt_html is None:
1449 self._insert_plain_text(cursor, self._continuation_prompt)
1449 self._insert_plain_text(cursor, self._continuation_prompt)
1450 else:
1450 else:
1451 self._continuation_prompt = self._insert_html_fetching_plain_text(
1451 self._continuation_prompt = self._insert_html_fetching_plain_text(
1452 cursor, self._continuation_prompt_html)
1452 cursor, self._continuation_prompt_html)
1453
1453
1454 def _insert_html(self, cursor, html):
1454 def _insert_html(self, cursor, html):
1455 """ Inserts HTML using the specified cursor in such a way that future
1455 """ Inserts HTML using the specified cursor in such a way that future
1456 formatting is unaffected.
1456 formatting is unaffected.
1457 """
1457 """
1458 cursor.beginEditBlock()
1458 cursor.beginEditBlock()
1459 cursor.insertHtml(html)
1459 cursor.insertHtml(html)
1460
1460
1461 # After inserting HTML, the text document "remembers" it's in "html
1461 # After inserting HTML, the text document "remembers" it's in "html
1462 # mode", which means that subsequent calls adding plain text will result
1462 # mode", which means that subsequent calls adding plain text will result
1463 # in unwanted formatting, lost tab characters, etc. The following code
1463 # in unwanted formatting, lost tab characters, etc. The following code
1464 # hacks around this behavior, which I consider to be a bug in Qt, by
1464 # hacks around this behavior, which I consider to be a bug in Qt, by
1465 # (crudely) resetting the document's style state.
1465 # (crudely) resetting the document's style state.
1466 cursor.movePosition(QtGui.QTextCursor.Left,
1466 cursor.movePosition(QtGui.QTextCursor.Left,
1467 QtGui.QTextCursor.KeepAnchor)
1467 QtGui.QTextCursor.KeepAnchor)
1468 if cursor.selection().toPlainText() == ' ':
1468 if cursor.selection().toPlainText() == ' ':
1469 cursor.removeSelectedText()
1469 cursor.removeSelectedText()
1470 else:
1470 else:
1471 cursor.movePosition(QtGui.QTextCursor.Right)
1471 cursor.movePosition(QtGui.QTextCursor.Right)
1472 cursor.insertText(' ', QtGui.QTextCharFormat())
1472 cursor.insertText(' ', QtGui.QTextCharFormat())
1473 cursor.endEditBlock()
1473 cursor.endEditBlock()
1474
1474
1475 def _insert_html_fetching_plain_text(self, cursor, html):
1475 def _insert_html_fetching_plain_text(self, cursor, html):
1476 """ Inserts HTML using the specified cursor, then returns its plain text
1476 """ Inserts HTML using the specified cursor, then returns its plain text
1477 version.
1477 version.
1478 """
1478 """
1479 cursor.beginEditBlock()
1479 cursor.beginEditBlock()
1480 cursor.removeSelectedText()
1480 cursor.removeSelectedText()
1481
1481
1482 start = cursor.position()
1482 start = cursor.position()
1483 self._insert_html(cursor, html)
1483 self._insert_html(cursor, html)
1484 end = cursor.position()
1484 end = cursor.position()
1485 cursor.setPosition(start, QtGui.QTextCursor.KeepAnchor)
1485 cursor.setPosition(start, QtGui.QTextCursor.KeepAnchor)
1486 text = cursor.selection().toPlainText()
1486 text = cursor.selection().toPlainText()
1487
1487
1488 cursor.setPosition(end)
1488 cursor.setPosition(end)
1489 cursor.endEditBlock()
1489 cursor.endEditBlock()
1490 return text
1490 return text
1491
1491
1492 def _insert_plain_text(self, cursor, text):
1492 def _insert_plain_text(self, cursor, text):
1493 """ Inserts plain text using the specified cursor, processing ANSI codes
1493 """ Inserts plain text using the specified cursor, processing ANSI codes
1494 if enabled.
1494 if enabled.
1495 """
1495 """
1496 cursor.beginEditBlock()
1496 cursor.beginEditBlock()
1497 if self.ansi_codes:
1497 if self.ansi_codes:
1498 for substring in self._ansi_processor.split_string(text):
1498 for substring in self._ansi_processor.split_string(text):
1499 for act in self._ansi_processor.actions:
1499 for act in self._ansi_processor.actions:
1500
1500
1501 # Unlike real terminal emulators, we don't distinguish
1501 # Unlike real terminal emulators, we don't distinguish
1502 # between the screen and the scrollback buffer. A screen
1502 # between the screen and the scrollback buffer. A screen
1503 # erase request clears everything.
1503 # erase request clears everything.
1504 if act.action == 'erase' and act.area == 'screen':
1504 if act.action == 'erase' and act.area == 'screen':
1505 cursor.select(QtGui.QTextCursor.Document)
1505 cursor.select(QtGui.QTextCursor.Document)
1506 cursor.removeSelectedText()
1506 cursor.removeSelectedText()
1507
1507
1508 # Simulate a form feed by scrolling just past the last line.
1508 # Simulate a form feed by scrolling just past the last line.
1509 elif act.action == 'scroll' and act.unit == 'page':
1509 elif act.action == 'scroll' and act.unit == 'page':
1510 cursor.insertText('\n')
1510 cursor.insertText('\n')
1511 cursor.endEditBlock()
1511 cursor.endEditBlock()
1512 self._set_top_cursor(cursor)
1512 self._set_top_cursor(cursor)
1513 cursor.joinPreviousEditBlock()
1513 cursor.joinPreviousEditBlock()
1514 cursor.deletePreviousChar()
1514 cursor.deletePreviousChar()
1515
1515
1516 format = self._ansi_processor.get_format()
1516 format = self._ansi_processor.get_format()
1517 cursor.insertText(substring, format)
1517 cursor.insertText(substring, format)
1518 else:
1518 else:
1519 cursor.insertText(text)
1519 cursor.insertText(text)
1520 cursor.endEditBlock()
1520 cursor.endEditBlock()
1521
1521
1522 def _insert_plain_text_into_buffer(self, cursor, text):
1522 def _insert_plain_text_into_buffer(self, cursor, text):
1523 """ Inserts text into the input buffer using the specified cursor (which
1523 """ Inserts text into the input buffer using the specified cursor (which
1524 must be in the input buffer), ensuring that continuation prompts are
1524 must be in the input buffer), ensuring that continuation prompts are
1525 inserted as necessary.
1525 inserted as necessary.
1526 """
1526 """
1527 lines = text.splitlines(True)
1527 lines = text.splitlines(True)
1528 if lines:
1528 if lines:
1529 cursor.beginEditBlock()
1529 cursor.beginEditBlock()
1530 cursor.insertText(lines[0])
1530 cursor.insertText(lines[0])
1531 for line in lines[1:]:
1531 for line in lines[1:]:
1532 if self._continuation_prompt_html is None:
1532 if self._continuation_prompt_html is None:
1533 cursor.insertText(self._continuation_prompt)
1533 cursor.insertText(self._continuation_prompt)
1534 else:
1534 else:
1535 self._continuation_prompt = \
1535 self._continuation_prompt = \
1536 self._insert_html_fetching_plain_text(
1536 self._insert_html_fetching_plain_text(
1537 cursor, self._continuation_prompt_html)
1537 cursor, self._continuation_prompt_html)
1538 cursor.insertText(line)
1538 cursor.insertText(line)
1539 cursor.endEditBlock()
1539 cursor.endEditBlock()
1540
1540
1541 def _in_buffer(self, position=None):
1541 def _in_buffer(self, position=None):
1542 """ Returns whether the current cursor (or, if specified, a position) is
1542 """ Returns whether the current cursor (or, if specified, a position) is
1543 inside the editing region.
1543 inside the editing region.
1544 """
1544 """
1545 cursor = self._control.textCursor()
1545 cursor = self._control.textCursor()
1546 if position is None:
1546 if position is None:
1547 position = cursor.position()
1547 position = cursor.position()
1548 else:
1548 else:
1549 cursor.setPosition(position)
1549 cursor.setPosition(position)
1550 line = cursor.blockNumber()
1550 line = cursor.blockNumber()
1551 prompt_line = self._get_prompt_cursor().blockNumber()
1551 prompt_line = self._get_prompt_cursor().blockNumber()
1552 if line == prompt_line:
1552 if line == prompt_line:
1553 return position >= self._prompt_pos
1553 return position >= self._prompt_pos
1554 elif line > prompt_line:
1554 elif line > prompt_line:
1555 cursor.movePosition(QtGui.QTextCursor.StartOfBlock)
1555 cursor.movePosition(QtGui.QTextCursor.StartOfBlock)
1556 prompt_pos = cursor.position() + len(self._continuation_prompt)
1556 prompt_pos = cursor.position() + len(self._continuation_prompt)
1557 return position >= prompt_pos
1557 return position >= prompt_pos
1558 return False
1558 return False
1559
1559
1560 def _keep_cursor_in_buffer(self):
1560 def _keep_cursor_in_buffer(self):
1561 """ Ensures that the cursor is inside the editing region. Returns
1561 """ Ensures that the cursor is inside the editing region. Returns
1562 whether the cursor was moved.
1562 whether the cursor was moved.
1563 """
1563 """
1564 moved = not self._in_buffer()
1564 moved = not self._in_buffer()
1565 if moved:
1565 if moved:
1566 cursor = self._control.textCursor()
1566 cursor = self._control.textCursor()
1567 cursor.movePosition(QtGui.QTextCursor.End)
1567 cursor.movePosition(QtGui.QTextCursor.End)
1568 self._control.setTextCursor(cursor)
1568 self._control.setTextCursor(cursor)
1569 return moved
1569 return moved
1570
1570
1571 def _keyboard_quit(self):
1571 def _keyboard_quit(self):
1572 """ Cancels the current editing task ala Ctrl-G in Emacs.
1572 """ Cancels the current editing task ala Ctrl-G in Emacs.
1573 """
1573 """
1574 if self._text_completing_pos:
1574 if self._text_completing_pos:
1575 self._cancel_text_completion()
1575 self._cancel_text_completion()
1576 else:
1576 else:
1577 self.input_buffer = ''
1577 self.input_buffer = ''
1578
1578
1579 def _page(self, text, html=False):
1579 def _page(self, text, html=False):
1580 """ Displays text using the pager if it exceeds the height of the
1580 """ Displays text using the pager if it exceeds the height of the
1581 viewport.
1581 viewport.
1582
1582
1583 Parameters:
1583 Parameters:
1584 -----------
1584 -----------
1585 html : bool, optional (default False)
1585 html : bool, optional (default False)
1586 If set, the text will be interpreted as HTML instead of plain text.
1586 If set, the text will be interpreted as HTML instead of plain text.
1587 """
1587 """
1588 line_height = QtGui.QFontMetrics(self.font).height()
1588 line_height = QtGui.QFontMetrics(self.font).height()
1589 minlines = self._control.viewport().height() / line_height
1589 minlines = self._control.viewport().height() / line_height
1590 if self.paging != 'none' and \
1590 if self.paging != 'none' and \
1591 re.match("(?:[^\n]*\n){%i}" % minlines, text):
1591 re.match("(?:[^\n]*\n){%i}" % minlines, text):
1592 if self.paging == 'custom':
1592 if self.paging == 'custom':
1593 self.custom_page_requested.emit(text)
1593 self.custom_page_requested.emit(text)
1594 else:
1594 else:
1595 self._page_control.clear()
1595 self._page_control.clear()
1596 cursor = self._page_control.textCursor()
1596 cursor = self._page_control.textCursor()
1597 if html:
1597 if html:
1598 self._insert_html(cursor, text)
1598 self._insert_html(cursor, text)
1599 else:
1599 else:
1600 self._insert_plain_text(cursor, text)
1600 self._insert_plain_text(cursor, text)
1601 self._page_control.moveCursor(QtGui.QTextCursor.Start)
1601 self._page_control.moveCursor(QtGui.QTextCursor.Start)
1602
1602
1603 self._page_control.viewport().resize(self._control.size())
1603 self._page_control.viewport().resize(self._control.size())
1604 if self._splitter:
1604 if self._splitter:
1605 self._page_control.show()
1605 self._page_control.show()
1606 self._page_control.setFocus()
1606 self._page_control.setFocus()
1607 else:
1607 else:
1608 self.layout().setCurrentWidget(self._page_control)
1608 self.layout().setCurrentWidget(self._page_control)
1609 elif html:
1609 elif html:
1610 self._append_plain_html(text)
1610 self._append_plain_html(text)
1611 else:
1611 else:
1612 self._append_plain_text(text)
1612 self._append_plain_text(text)
1613
1613
1614 def _prompt_finished(self):
1614 def _prompt_finished(self):
1615 """ Called immediately after a prompt is finished, i.e. when some input
1615 """ Called immediately after a prompt is finished, i.e. when some input
1616 will be processed and a new prompt displayed.
1616 will be processed and a new prompt displayed.
1617 """
1617 """
1618 self._control.setReadOnly(True)
1618 self._control.setReadOnly(True)
1619 self._prompt_finished_hook()
1619 self._prompt_finished_hook()
1620
1620
1621 def _prompt_started(self):
1621 def _prompt_started(self):
1622 """ Called immediately after a new prompt is displayed.
1622 """ Called immediately after a new prompt is displayed.
1623 """
1623 """
1624 # Temporarily disable the maximum block count to permit undo/redo and
1624 # Temporarily disable the maximum block count to permit undo/redo and
1625 # to ensure that the prompt position does not change due to truncation.
1625 # to ensure that the prompt position does not change due to truncation.
1626 self._control.document().setMaximumBlockCount(0)
1626 self._control.document().setMaximumBlockCount(0)
1627 self._control.setUndoRedoEnabled(True)
1627 self._control.setUndoRedoEnabled(True)
1628
1628
1629 # Work around bug in QPlainTextEdit: input method is not re-enabled
1629 # Work around bug in QPlainTextEdit: input method is not re-enabled
1630 # when read-only is disabled.
1630 # when read-only is disabled.
1631 self._control.setReadOnly(False)
1631 self._control.setReadOnly(False)
1632 self._control.setAttribute(QtCore.Qt.WA_InputMethodEnabled, True)
1632 self._control.setAttribute(QtCore.Qt.WA_InputMethodEnabled, True)
1633
1633
1634 if not self._reading:
1634 if not self._reading:
1635 self._executing = False
1635 self._executing = False
1636 self._prompt_started_hook()
1636 self._prompt_started_hook()
1637
1637
1638 # If the input buffer has changed while executing, load it.
1638 # If the input buffer has changed while executing, load it.
1639 if self._input_buffer_pending:
1639 if self._input_buffer_pending:
1640 self.input_buffer = self._input_buffer_pending
1640 self.input_buffer = self._input_buffer_pending
1641 self._input_buffer_pending = ''
1641 self._input_buffer_pending = ''
1642
1642
1643 self._control.moveCursor(QtGui.QTextCursor.End)
1643 self._control.moveCursor(QtGui.QTextCursor.End)
1644
1644
1645 def _readline(self, prompt='', callback=None):
1645 def _readline(self, prompt='', callback=None):
1646 """ Reads one line of input from the user.
1646 """ Reads one line of input from the user.
1647
1647
1648 Parameters
1648 Parameters
1649 ----------
1649 ----------
1650 prompt : str, optional
1650 prompt : str, optional
1651 The prompt to print before reading the line.
1651 The prompt to print before reading the line.
1652
1652
1653 callback : callable, optional
1653 callback : callable, optional
1654 A callback to execute with the read line. If not specified, input is
1654 A callback to execute with the read line. If not specified, input is
1655 read *synchronously* and this method does not return until it has
1655 read *synchronously* and this method does not return until it has
1656 been read.
1656 been read.
1657
1657
1658 Returns
1658 Returns
1659 -------
1659 -------
1660 If a callback is specified, returns nothing. Otherwise, returns the
1660 If a callback is specified, returns nothing. Otherwise, returns the
1661 input string with the trailing newline stripped.
1661 input string with the trailing newline stripped.
1662 """
1662 """
1663 if self._reading:
1663 if self._reading:
1664 raise RuntimeError('Cannot read a line. Widget is already reading.')
1664 raise RuntimeError('Cannot read a line. Widget is already reading.')
1665
1665
1666 if not callback and not self.isVisible():
1666 if not callback and not self.isVisible():
1667 # If the user cannot see the widget, this function cannot return.
1667 # If the user cannot see the widget, this function cannot return.
1668 raise RuntimeError('Cannot synchronously read a line if the widget '
1668 raise RuntimeError('Cannot synchronously read a line if the widget '
1669 'is not visible!')
1669 'is not visible!')
1670
1670
1671 self._reading = True
1671 self._reading = True
1672 self._show_prompt(prompt, newline=False)
1672 self._show_prompt(prompt, newline=False)
1673
1673
1674 if callback is None:
1674 if callback is None:
1675 self._reading_callback = None
1675 self._reading_callback = None
1676 while self._reading:
1676 while self._reading:
1677 QtCore.QCoreApplication.processEvents()
1677 QtCore.QCoreApplication.processEvents()
1678 return self._get_input_buffer(force=True).rstrip('\n')
1678 return self._get_input_buffer(force=True).rstrip('\n')
1679
1679
1680 else:
1680 else:
1681 self._reading_callback = lambda: \
1681 self._reading_callback = lambda: \
1682 callback(self._get_input_buffer(force=True).rstrip('\n'))
1682 callback(self._get_input_buffer(force=True).rstrip('\n'))
1683
1683
1684 def _set_continuation_prompt(self, prompt, html=False):
1684 def _set_continuation_prompt(self, prompt, html=False):
1685 """ Sets the continuation prompt.
1685 """ Sets the continuation prompt.
1686
1686
1687 Parameters
1687 Parameters
1688 ----------
1688 ----------
1689 prompt : str
1689 prompt : str
1690 The prompt to show when more input is needed.
1690 The prompt to show when more input is needed.
1691
1691
1692 html : bool, optional (default False)
1692 html : bool, optional (default False)
1693 If set, the prompt will be inserted as formatted HTML. Otherwise,
1693 If set, the prompt will be inserted as formatted HTML. Otherwise,
1694 the prompt will be treated as plain text, though ANSI color codes
1694 the prompt will be treated as plain text, though ANSI color codes
1695 will be handled.
1695 will be handled.
1696 """
1696 """
1697 if html:
1697 if html:
1698 self._continuation_prompt_html = prompt
1698 self._continuation_prompt_html = prompt
1699 else:
1699 else:
1700 self._continuation_prompt = prompt
1700 self._continuation_prompt = prompt
1701 self._continuation_prompt_html = None
1701 self._continuation_prompt_html = None
1702
1702
1703 def _set_cursor(self, cursor):
1703 def _set_cursor(self, cursor):
1704 """ Convenience method to set the current cursor.
1704 """ Convenience method to set the current cursor.
1705 """
1705 """
1706 self._control.setTextCursor(cursor)
1706 self._control.setTextCursor(cursor)
1707
1707
1708 def _set_top_cursor(self, cursor):
1708 def _set_top_cursor(self, cursor):
1709 """ Scrolls the viewport so that the specified cursor is at the top.
1709 """ Scrolls the viewport so that the specified cursor is at the top.
1710 """
1710 """
1711 scrollbar = self._control.verticalScrollBar()
1711 scrollbar = self._control.verticalScrollBar()
1712 scrollbar.setValue(scrollbar.maximum())
1712 scrollbar.setValue(scrollbar.maximum())
1713 original_cursor = self._control.textCursor()
1713 original_cursor = self._control.textCursor()
1714 self._control.setTextCursor(cursor)
1714 self._control.setTextCursor(cursor)
1715 self._control.ensureCursorVisible()
1715 self._control.ensureCursorVisible()
1716 self._control.setTextCursor(original_cursor)
1716 self._control.setTextCursor(original_cursor)
1717
1717
1718 def _show_prompt(self, prompt=None, html=False, newline=True):
1718 def _show_prompt(self, prompt=None, html=False, newline=True):
1719 """ Writes a new prompt at the end of the buffer.
1719 """ Writes a new prompt at the end of the buffer.
1720
1720
1721 Parameters
1721 Parameters
1722 ----------
1722 ----------
1723 prompt : str, optional
1723 prompt : str, optional
1724 The prompt to show. If not specified, the previous prompt is used.
1724 The prompt to show. If not specified, the previous prompt is used.
1725
1725
1726 html : bool, optional (default False)
1726 html : bool, optional (default False)
1727 Only relevant when a prompt is specified. If set, the prompt will
1727 Only relevant when a prompt is specified. If set, the prompt will
1728 be inserted as formatted HTML. Otherwise, the prompt will be treated
1728 be inserted as formatted HTML. Otherwise, the prompt will be treated
1729 as plain text, though ANSI color codes will be handled.
1729 as plain text, though ANSI color codes will be handled.
1730
1730
1731 newline : bool, optional (default True)
1731 newline : bool, optional (default True)
1732 If set, a new line will be written before showing the prompt if
1732 If set, a new line will be written before showing the prompt if
1733 there is not already a newline at the end of the buffer.
1733 there is not already a newline at the end of the buffer.
1734 """
1734 """
1735 # Save the current end position to support _append*(before_prompt=True).
1735 # Save the current end position to support _append*(before_prompt=True).
1736 cursor = self._get_end_cursor()
1736 cursor = self._get_end_cursor()
1737 self._append_before_prompt_pos = cursor.position()
1737 self._append_before_prompt_pos = cursor.position()
1738
1738
1739 # Insert a preliminary newline, if necessary.
1739 # Insert a preliminary newline, if necessary.
1740 if newline and cursor.position() > 0:
1740 if newline and cursor.position() > 0:
1741 cursor.movePosition(QtGui.QTextCursor.Left,
1741 cursor.movePosition(QtGui.QTextCursor.Left,
1742 QtGui.QTextCursor.KeepAnchor)
1742 QtGui.QTextCursor.KeepAnchor)
1743 if cursor.selection().toPlainText() != '\n':
1743 if cursor.selection().toPlainText() != '\n':
1744 self._append_plain_text('\n')
1744 self._append_plain_text('\n')
1745
1745
1746 # Write the prompt.
1746 # Write the prompt.
1747 self._append_plain_text(self._prompt_sep)
1747 self._append_plain_text(self._prompt_sep)
1748 if prompt is None:
1748 if prompt is None:
1749 if self._prompt_html is None:
1749 if self._prompt_html is None:
1750 self._append_plain_text(self._prompt)
1750 self._append_plain_text(self._prompt)
1751 else:
1751 else:
1752 self._append_html(self._prompt_html)
1752 self._append_html(self._prompt_html)
1753 else:
1753 else:
1754 if html:
1754 if html:
1755 self._prompt = self._append_html_fetching_plain_text(prompt)
1755 self._prompt = self._append_html_fetching_plain_text(prompt)
1756 self._prompt_html = prompt
1756 self._prompt_html = prompt
1757 else:
1757 else:
1758 self._append_plain_text(prompt)
1758 self._append_plain_text(prompt)
1759 self._prompt = prompt
1759 self._prompt = prompt
1760 self._prompt_html = None
1760 self._prompt_html = None
1761
1761
1762 self._prompt_pos = self._get_end_cursor().position()
1762 self._prompt_pos = self._get_end_cursor().position()
1763 self._prompt_started()
1763 self._prompt_started()
1764
1764
1765 #------ Signal handlers ----------------------------------------------------
1765 #------ Signal handlers ----------------------------------------------------
1766
1766
1767 def _adjust_scrollbars(self):
1767 def _adjust_scrollbars(self):
1768 """ Expands the vertical scrollbar beyond the range set by Qt.
1768 """ Expands the vertical scrollbar beyond the range set by Qt.
1769 """
1769 """
1770 # This code is adapted from _q_adjustScrollbars in qplaintextedit.cpp
1770 # This code is adapted from _q_adjustScrollbars in qplaintextedit.cpp
1771 # and qtextedit.cpp.
1771 # and qtextedit.cpp.
1772 document = self._control.document()
1772 document = self._control.document()
1773 scrollbar = self._control.verticalScrollBar()
1773 scrollbar = self._control.verticalScrollBar()
1774 viewport_height = self._control.viewport().height()
1774 viewport_height = self._control.viewport().height()
1775 if isinstance(self._control, QtGui.QPlainTextEdit):
1775 if isinstance(self._control, QtGui.QPlainTextEdit):
1776 maximum = max(0, document.lineCount() - 1)
1776 maximum = max(0, document.lineCount() - 1)
1777 step = viewport_height / self._control.fontMetrics().lineSpacing()
1777 step = viewport_height / self._control.fontMetrics().lineSpacing()
1778 else:
1778 else:
1779 # QTextEdit does not do line-based layout and blocks will not in
1779 # QTextEdit does not do line-based layout and blocks will not in
1780 # general have the same height. Therefore it does not make sense to
1780 # general have the same height. Therefore it does not make sense to
1781 # attempt to scroll in line height increments.
1781 # attempt to scroll in line height increments.
1782 maximum = document.size().height()
1782 maximum = document.size().height()
1783 step = viewport_height
1783 step = viewport_height
1784 diff = maximum - scrollbar.maximum()
1784 diff = maximum - scrollbar.maximum()
1785 scrollbar.setRange(0, maximum)
1785 scrollbar.setRange(0, maximum)
1786 scrollbar.setPageStep(step)
1786 scrollbar.setPageStep(step)
1787
1787
1788 # Compensate for undesirable scrolling that occurs automatically due to
1788 # Compensate for undesirable scrolling that occurs automatically due to
1789 # maximumBlockCount() text truncation.
1789 # maximumBlockCount() text truncation.
1790 if diff < 0 and document.blockCount() == document.maximumBlockCount():
1790 if diff < 0 and document.blockCount() == document.maximumBlockCount():
1791 scrollbar.setValue(scrollbar.value() + diff)
1791 scrollbar.setValue(scrollbar.value() + diff)
1792
1792
1793 def _cursor_position_changed(self):
1793 def _cursor_position_changed(self):
1794 """ Clears the temporary buffer based on the cursor position.
1794 """ Clears the temporary buffer based on the cursor position.
1795 """
1795 """
1796 if self._text_completing_pos:
1796 if self._text_completing_pos:
1797 document = self._control.document()
1797 document = self._control.document()
1798 if self._text_completing_pos < document.characterCount():
1798 if self._text_completing_pos < document.characterCount():
1799 cursor = self._control.textCursor()
1799 cursor = self._control.textCursor()
1800 pos = cursor.position()
1800 pos = cursor.position()
1801 text_cursor = self._control.textCursor()
1801 text_cursor = self._control.textCursor()
1802 text_cursor.setPosition(self._text_completing_pos)
1802 text_cursor.setPosition(self._text_completing_pos)
1803 if pos < self._text_completing_pos or \
1803 if pos < self._text_completing_pos or \
1804 cursor.blockNumber() > text_cursor.blockNumber():
1804 cursor.blockNumber() > text_cursor.blockNumber():
1805 self._clear_temporary_buffer()
1805 self._clear_temporary_buffer()
1806 self._text_completing_pos = 0
1806 self._text_completing_pos = 0
1807 else:
1807 else:
1808 self._clear_temporary_buffer()
1808 self._clear_temporary_buffer()
1809 self._text_completing_pos = 0
1809 self._text_completing_pos = 0
1810
1810
1811 def _custom_context_menu_requested(self, pos):
1811 def _custom_context_menu_requested(self, pos):
1812 """ Shows a context menu at the given QPoint (in widget coordinates).
1812 """ Shows a context menu at the given QPoint (in widget coordinates).
1813 """
1813 """
1814 menu = self._context_menu_make(pos)
1814 menu = self._context_menu_make(pos)
1815 menu.exec_(self._control.mapToGlobal(pos))
1815 menu.exec_(self._control.mapToGlobal(pos))
@@ -1,728 +1,732 b''
1 from __future__ import print_function
1 from __future__ import print_function
2
2
3 # Standard library imports
3 # Standard library imports
4 from collections import namedtuple
4 from collections import namedtuple
5 import sys
5 import sys
6 import time
6 import time
7 import uuid
7 import uuid
8
8
9 # System library imports
9 # System library imports
10 from pygments.lexers import PythonLexer
10 from pygments.lexers import PythonLexer
11 from IPython.external import qt
11 from IPython.external import qt
12 from IPython.external.qt import QtCore, QtGui
12 from IPython.external.qt import QtCore, QtGui
13
13
14 # Local imports
14 # Local imports
15 from IPython.core.inputsplitter import InputSplitter, transform_classic_prompt
15 from IPython.core.inputsplitter import InputSplitter, transform_classic_prompt
16 from IPython.core.oinspect import call_tip
16 from IPython.core.oinspect import call_tip
17 from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin
17 from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin
18 from IPython.utils.traitlets import Bool, Instance, Unicode
18 from IPython.utils.traitlets import Bool, Instance, Unicode
19 from bracket_matcher import BracketMatcher
19 from bracket_matcher import BracketMatcher
20 from call_tip_widget import CallTipWidget
20 from call_tip_widget import CallTipWidget
21 from completion_lexer import CompletionLexer
21 from completion_lexer import CompletionLexer
22 from history_console_widget import HistoryConsoleWidget
22 from history_console_widget import HistoryConsoleWidget
23 from pygments_highlighter import PygmentsHighlighter
23 from pygments_highlighter import PygmentsHighlighter
24
24
25
25
26 class FrontendHighlighter(PygmentsHighlighter):
26 class FrontendHighlighter(PygmentsHighlighter):
27 """ A PygmentsHighlighter that understands and ignores prompts.
27 """ A PygmentsHighlighter that understands and ignores prompts.
28 """
28 """
29
29
30 def __init__(self, frontend):
30 def __init__(self, frontend):
31 super(FrontendHighlighter, self).__init__(frontend._control.document())
31 super(FrontendHighlighter, self).__init__(frontend._control.document())
32 self._current_offset = 0
32 self._current_offset = 0
33 self._frontend = frontend
33 self._frontend = frontend
34 self.highlighting_on = False
34 self.highlighting_on = False
35
35
36 def highlightBlock(self, string):
36 def highlightBlock(self, string):
37 """ Highlight a block of text. Reimplemented to highlight selectively.
37 """ Highlight a block of text. Reimplemented to highlight selectively.
38 """
38 """
39 if not self.highlighting_on:
39 if not self.highlighting_on:
40 return
40 return
41
41
42 # The input to this function is a unicode string that may contain
42 # The input to this function is a unicode string that may contain
43 # paragraph break characters, non-breaking spaces, etc. Here we acquire
43 # paragraph break characters, non-breaking spaces, etc. Here we acquire
44 # the string as plain text so we can compare it.
44 # the string as plain text so we can compare it.
45 current_block = self.currentBlock()
45 current_block = self.currentBlock()
46 string = self._frontend._get_block_plain_text(current_block)
46 string = self._frontend._get_block_plain_text(current_block)
47
47
48 # Decide whether to check for the regular or continuation prompt.
48 # Decide whether to check for the regular or continuation prompt.
49 if current_block.contains(self._frontend._prompt_pos):
49 if current_block.contains(self._frontend._prompt_pos):
50 prompt = self._frontend._prompt
50 prompt = self._frontend._prompt
51 else:
51 else:
52 prompt = self._frontend._continuation_prompt
52 prompt = self._frontend._continuation_prompt
53
53
54 # Only highlight if we can identify a prompt, but make sure not to
54 # Only highlight if we can identify a prompt, but make sure not to
55 # highlight the prompt.
55 # highlight the prompt.
56 if string.startswith(prompt):
56 if string.startswith(prompt):
57 self._current_offset = len(prompt)
57 self._current_offset = len(prompt)
58 string = string[len(prompt):]
58 string = string[len(prompt):]
59 super(FrontendHighlighter, self).highlightBlock(string)
59 super(FrontendHighlighter, self).highlightBlock(string)
60
60
61 def rehighlightBlock(self, block):
61 def rehighlightBlock(self, block):
62 """ Reimplemented to temporarily enable highlighting if disabled.
62 """ Reimplemented to temporarily enable highlighting if disabled.
63 """
63 """
64 old = self.highlighting_on
64 old = self.highlighting_on
65 self.highlighting_on = True
65 self.highlighting_on = True
66 super(FrontendHighlighter, self).rehighlightBlock(block)
66 super(FrontendHighlighter, self).rehighlightBlock(block)
67 self.highlighting_on = old
67 self.highlighting_on = old
68
68
69 def setFormat(self, start, count, format):
69 def setFormat(self, start, count, format):
70 """ Reimplemented to highlight selectively.
70 """ Reimplemented to highlight selectively.
71 """
71 """
72 start += self._current_offset
72 start += self._current_offset
73 super(FrontendHighlighter, self).setFormat(start, count, format)
73 super(FrontendHighlighter, self).setFormat(start, count, format)
74
74
75
75
76 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
76 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
77 """ A Qt frontend for a generic Python kernel.
77 """ A Qt frontend for a generic Python kernel.
78 """
78 """
79
79
80 # The text to show when the kernel is (re)started.
80 # The text to show when the kernel is (re)started.
81 banner = Unicode()
81 banner = Unicode()
82
82
83 # An option and corresponding signal for overriding the default kernel
83 # An option and corresponding signal for overriding the default kernel
84 # interrupt behavior.
84 # interrupt behavior.
85 custom_interrupt = Bool(False)
85 custom_interrupt = Bool(False)
86 custom_interrupt_requested = QtCore.Signal()
86 custom_interrupt_requested = QtCore.Signal()
87
87
88 # An option and corresponding signals for overriding the default kernel
88 # An option and corresponding signals for overriding the default kernel
89 # restart behavior.
89 # restart behavior.
90 custom_restart = Bool(False)
90 custom_restart = Bool(False)
91 custom_restart_kernel_died = QtCore.Signal(float)
91 custom_restart_kernel_died = QtCore.Signal(float)
92 custom_restart_requested = QtCore.Signal()
92 custom_restart_requested = QtCore.Signal()
93
93
94 # Whether to automatically show calltips on open-parentheses.
94 # Whether to automatically show calltips on open-parentheses.
95 enable_calltips = Bool(True, config=True,
95 enable_calltips = Bool(True, config=True,
96 help="Whether to draw information calltips on open-parentheses.")
96 help="Whether to draw information calltips on open-parentheses.")
97
97
98 # Emitted when a user visible 'execute_request' has been submitted to the
98 # Emitted when a user visible 'execute_request' has been submitted to the
99 # kernel from the FrontendWidget. Contains the code to be executed.
99 # kernel from the FrontendWidget. Contains the code to be executed.
100 executing = QtCore.Signal(object)
100 executing = QtCore.Signal(object)
101
101
102 # Emitted when a user-visible 'execute_reply' has been received from the
102 # Emitted when a user-visible 'execute_reply' has been received from the
103 # kernel and processed by the FrontendWidget. Contains the response message.
103 # kernel and processed by the FrontendWidget. Contains the response message.
104 executed = QtCore.Signal(object)
104 executed = QtCore.Signal(object)
105
105
106 # Emitted when an exit request has been received from the kernel.
106 # Emitted when an exit request has been received from the kernel.
107 exit_requested = QtCore.Signal(object)
107 exit_requested = QtCore.Signal(object)
108
108
109 # Protected class variables.
109 # Protected class variables.
110 _CallTipRequest = namedtuple('_CallTipRequest', ['id', 'pos'])
110 _CallTipRequest = namedtuple('_CallTipRequest', ['id', 'pos'])
111 _CompletionRequest = namedtuple('_CompletionRequest', ['id', 'pos'])
111 _CompletionRequest = namedtuple('_CompletionRequest', ['id', 'pos'])
112 _ExecutionRequest = namedtuple('_ExecutionRequest', ['id', 'kind'])
112 _ExecutionRequest = namedtuple('_ExecutionRequest', ['id', 'kind'])
113 _input_splitter_class = InputSplitter
113 _input_splitter_class = InputSplitter
114 _local_kernel = False
114 _local_kernel = False
115 _highlighter = Instance(FrontendHighlighter)
115 _highlighter = Instance(FrontendHighlighter)
116
116
117 #---------------------------------------------------------------------------
117 #---------------------------------------------------------------------------
118 # 'object' interface
118 # 'object' interface
119 #---------------------------------------------------------------------------
119 #---------------------------------------------------------------------------
120
120
121 def __init__(self, *args, **kw):
121 def __init__(self, *args, **kw):
122 super(FrontendWidget, self).__init__(*args, **kw)
122 super(FrontendWidget, self).__init__(*args, **kw)
123 # FIXME: remove this when PySide min version is updated past 1.0.7
123 # FIXME: remove this when PySide min version is updated past 1.0.7
124 # forcefully disable calltips if PySide is < 1.0.7, because they crash
124 # forcefully disable calltips if PySide is < 1.0.7, because they crash
125 if qt.QT_API == qt.QT_API_PYSIDE:
125 if qt.QT_API == qt.QT_API_PYSIDE:
126 import PySide
126 import PySide
127 if PySide.__version_info__ < (1,0,7):
127 if PySide.__version_info__ < (1,0,7):
128 self.log.warn("PySide %s < 1.0.7 detected, disabling calltips" % PySide.__version__)
128 self.log.warn("PySide %s < 1.0.7 detected, disabling calltips" % PySide.__version__)
129 self.enable_calltips = False
129 self.enable_calltips = False
130
130
131 # FrontendWidget protected variables.
131 # FrontendWidget protected variables.
132 self._bracket_matcher = BracketMatcher(self._control)
132 self._bracket_matcher = BracketMatcher(self._control)
133 self._call_tip_widget = CallTipWidget(self._control)
133 self._call_tip_widget = CallTipWidget(self._control)
134 self._completion_lexer = CompletionLexer(PythonLexer())
134 self._completion_lexer = CompletionLexer(PythonLexer())
135 self._copy_raw_action = QtGui.QAction('Copy (Raw Text)', None)
135 self._copy_raw_action = QtGui.QAction('Copy (Raw Text)', None)
136 self._hidden = False
136 self._hidden = False
137 self._highlighter = FrontendHighlighter(self)
137 self._highlighter = FrontendHighlighter(self)
138 self._input_splitter = self._input_splitter_class(input_mode='cell')
138 self._input_splitter = self._input_splitter_class(input_mode='cell')
139 self._kernel_manager = None
139 self._kernel_manager = None
140 self._request_info = {}
140 self._request_info = {}
141 self._callback_dict = {}
141 self._callback_dict = {}
142
142
143 # Configure the ConsoleWidget.
143 # Configure the ConsoleWidget.
144 self.tab_width = 4
144 self.tab_width = 4
145 self._set_continuation_prompt('... ')
145 self._set_continuation_prompt('... ')
146
146
147 # Configure the CallTipWidget.
147 # Configure the CallTipWidget.
148 self._call_tip_widget.setFont(self.font)
148 self._call_tip_widget.setFont(self.font)
149 self.font_changed.connect(self._call_tip_widget.setFont)
149 self.font_changed.connect(self._call_tip_widget.setFont)
150
150
151 # Configure actions.
151 # Configure actions.
152 action = self._copy_raw_action
152 action = self._copy_raw_action
153 key = QtCore.Qt.CTRL | QtCore.Qt.SHIFT | QtCore.Qt.Key_C
153 key = QtCore.Qt.CTRL | QtCore.Qt.SHIFT | QtCore.Qt.Key_C
154 action.setEnabled(False)
154 action.setEnabled(False)
155 action.setShortcut(QtGui.QKeySequence(key))
155 action.setShortcut(QtGui.QKeySequence(key))
156 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
156 action.setShortcutContext(QtCore.Qt.WidgetWithChildrenShortcut)
157 action.triggered.connect(self.copy_raw)
157 action.triggered.connect(self.copy_raw)
158 self.copy_available.connect(action.setEnabled)
158 self.copy_available.connect(action.setEnabled)
159 self.addAction(action)
159 self.addAction(action)
160
160
161 # Connect signal handlers.
161 # Connect signal handlers.
162 document = self._control.document()
162 document = self._control.document()
163 document.contentsChange.connect(self._document_contents_change)
163 document.contentsChange.connect(self._document_contents_change)
164
164
165 # Set flag for whether we are connected via localhost.
165 # Set flag for whether we are connected via localhost.
166 self._local_kernel = kw.get('local_kernel',
166 self._local_kernel = kw.get('local_kernel',
167 FrontendWidget._local_kernel)
167 FrontendWidget._local_kernel)
168
168
169 #---------------------------------------------------------------------------
169 #---------------------------------------------------------------------------
170 # 'ConsoleWidget' public interface
170 # 'ConsoleWidget' public interface
171 #---------------------------------------------------------------------------
171 #---------------------------------------------------------------------------
172
172
173 def copy(self):
173 def copy(self):
174 """ Copy the currently selected text to the clipboard, removing prompts.
174 """ Copy the currently selected text to the clipboard, removing prompts.
175 """
175 """
176 text = self._control.textCursor().selection().toPlainText()
176 text = self._control.textCursor().selection().toPlainText()
177 if text:
177 if text:
178 lines = map(transform_classic_prompt, text.splitlines())
178 lines = map(transform_classic_prompt, text.splitlines())
179 text = '\n'.join(lines)
179 text = '\n'.join(lines)
180 QtGui.QApplication.clipboard().setText(text)
180 QtGui.QApplication.clipboard().setText(text)
181
181
182 #---------------------------------------------------------------------------
182 #---------------------------------------------------------------------------
183 # 'ConsoleWidget' abstract interface
183 # 'ConsoleWidget' abstract interface
184 #---------------------------------------------------------------------------
184 #---------------------------------------------------------------------------
185
185
186 def _is_complete(self, source, interactive):
186 def _is_complete(self, source, interactive):
187 """ Returns whether 'source' can be completely processed and a new
187 """ Returns whether 'source' can be completely processed and a new
188 prompt created. When triggered by an Enter/Return key press,
188 prompt created. When triggered by an Enter/Return key press,
189 'interactive' is True; otherwise, it is False.
189 'interactive' is True; otherwise, it is False.
190 """
190 """
191 complete = self._input_splitter.push(source)
191 complete = self._input_splitter.push(source)
192 if interactive:
192 if interactive:
193 complete = not self._input_splitter.push_accepts_more()
193 complete = not self._input_splitter.push_accepts_more()
194 return complete
194 return complete
195
195
196 def _execute(self, source, hidden):
196 def _execute(self, source, hidden):
197 """ Execute 'source'. If 'hidden', do not show any output.
197 """ Execute 'source'. If 'hidden', do not show any output.
198
198
199 See parent class :meth:`execute` docstring for full details.
199 See parent class :meth:`execute` docstring for full details.
200 """
200 """
201 msg_id = self.kernel_manager.shell_channel.execute(source, hidden)
201 msg_id = self.kernel_manager.shell_channel.execute(source, hidden)
202 self._request_info['execute'] = self._ExecutionRequest(msg_id, 'user')
202 self._request_info['execute'] = self._ExecutionRequest(msg_id, 'user')
203 self._hidden = hidden
203 self._hidden = hidden
204 if not hidden:
204 if not hidden:
205 self.executing.emit(source)
205 self.executing.emit(source)
206
206
207 def _prompt_started_hook(self):
207 def _prompt_started_hook(self):
208 """ Called immediately after a new prompt is displayed.
208 """ Called immediately after a new prompt is displayed.
209 """
209 """
210 if not self._reading:
210 if not self._reading:
211 self._highlighter.highlighting_on = True
211 self._highlighter.highlighting_on = True
212
212
213 def _prompt_finished_hook(self):
213 def _prompt_finished_hook(self):
214 """ Called immediately after a prompt is finished, i.e. when some input
214 """ Called immediately after a prompt is finished, i.e. when some input
215 will be processed and a new prompt displayed.
215 will be processed and a new prompt displayed.
216 """
216 """
217 # Flush all state from the input splitter so the next round of
217 # Flush all state from the input splitter so the next round of
218 # reading input starts with a clean buffer.
218 # reading input starts with a clean buffer.
219 self._input_splitter.reset()
219 self._input_splitter.reset()
220
220
221 if not self._reading:
221 if not self._reading:
222 self._highlighter.highlighting_on = False
222 self._highlighter.highlighting_on = False
223
223
224 def _tab_pressed(self):
224 def _tab_pressed(self):
225 """ Called when the tab key is pressed. Returns whether to continue
225 """ Called when the tab key is pressed. Returns whether to continue
226 processing the event.
226 processing the event.
227 """
227 """
228 # Perform tab completion if:
228 # Perform tab completion if:
229 # 1) The cursor is in the input buffer.
229 # 1) The cursor is in the input buffer.
230 # 2) There is a non-whitespace character before the cursor.
230 # 2) There is a non-whitespace character before the cursor.
231 text = self._get_input_buffer_cursor_line()
231 text = self._get_input_buffer_cursor_line()
232 if text is None:
232 if text is None:
233 return False
233 return False
234 complete = bool(text[:self._get_input_buffer_cursor_column()].strip())
234 complete = bool(text[:self._get_input_buffer_cursor_column()].strip())
235 if complete:
235 if complete:
236 self._complete()
236 self._complete()
237 return not complete
237 return not complete
238
238
239 #---------------------------------------------------------------------------
239 #---------------------------------------------------------------------------
240 # 'ConsoleWidget' protected interface
240 # 'ConsoleWidget' protected interface
241 #---------------------------------------------------------------------------
241 #---------------------------------------------------------------------------
242
242
243 def _context_menu_make(self, pos):
243 def _context_menu_make(self, pos):
244 """ Reimplemented to add an action for raw copy.
244 """ Reimplemented to add an action for raw copy.
245 """
245 """
246 menu = super(FrontendWidget, self)._context_menu_make(pos)
246 menu = super(FrontendWidget, self)._context_menu_make(pos)
247 for before_action in menu.actions():
247 for before_action in menu.actions():
248 if before_action.shortcut().matches(QtGui.QKeySequence.Paste) == \
248 if before_action.shortcut().matches(QtGui.QKeySequence.Paste) == \
249 QtGui.QKeySequence.ExactMatch:
249 QtGui.QKeySequence.ExactMatch:
250 menu.insertAction(before_action, self._copy_raw_action)
250 menu.insertAction(before_action, self._copy_raw_action)
251 break
251 break
252 return menu
252 return menu
253
253
254 def request_interrupt_kernel(self):
254 def request_interrupt_kernel(self):
255 if self._executing:
255 if self._executing:
256 self.interrupt_kernel()
256 self.interrupt_kernel()
257
257
258 def request_restart_kernel(self):
258 def request_restart_kernel(self):
259 message = 'Are you sure you want to restart the kernel?'
259 message = 'Are you sure you want to restart the kernel?'
260 self.restart_kernel(message, now=False)
260 self.restart_kernel(message, now=False)
261
261
262 def _event_filter_console_keypress(self, event):
262 def _event_filter_console_keypress(self, event):
263 """ Reimplemented for execution interruption and smart backspace.
263 """ Reimplemented for execution interruption and smart backspace.
264 """
264 """
265 key = event.key()
265 key = event.key()
266 if self._control_key_down(event.modifiers(), include_command=False):
266 if self._control_key_down(event.modifiers(), include_command=False):
267
267
268 if key == QtCore.Qt.Key_C and self._executing:
268 if key == QtCore.Qt.Key_C and self._executing:
269 self.request_interrupt_kernel()
269 self.request_interrupt_kernel()
270 return True
270 return True
271
271
272 elif key == QtCore.Qt.Key_Period:
272 elif key == QtCore.Qt.Key_Period:
273 self.request_restart_kernel()
273 self.request_restart_kernel()
274 return True
274 return True
275
275
276 elif not event.modifiers() & QtCore.Qt.AltModifier:
276 elif not event.modifiers() & QtCore.Qt.AltModifier:
277
277
278 # Smart backspace: remove four characters in one backspace if:
278 # Smart backspace: remove four characters in one backspace if:
279 # 1) everything left of the cursor is whitespace
279 # 1) everything left of the cursor is whitespace
280 # 2) the four characters immediately left of the cursor are spaces
280 # 2) the four characters immediately left of the cursor are spaces
281 if key == QtCore.Qt.Key_Backspace:
281 if key == QtCore.Qt.Key_Backspace:
282 col = self._get_input_buffer_cursor_column()
282 col = self._get_input_buffer_cursor_column()
283 cursor = self._control.textCursor()
283 cursor = self._control.textCursor()
284 if col > 3 and not cursor.hasSelection():
284 if col > 3 and not cursor.hasSelection():
285 text = self._get_input_buffer_cursor_line()[:col]
285 text = self._get_input_buffer_cursor_line()[:col]
286 if text.endswith(' ') and not text.strip():
286 if text.endswith(' ') and not text.strip():
287 cursor.movePosition(QtGui.QTextCursor.Left,
287 cursor.movePosition(QtGui.QTextCursor.Left,
288 QtGui.QTextCursor.KeepAnchor, 4)
288 QtGui.QTextCursor.KeepAnchor, 4)
289 cursor.removeSelectedText()
289 cursor.removeSelectedText()
290 return True
290 return True
291
291
292 return super(FrontendWidget, self)._event_filter_console_keypress(event)
292 return super(FrontendWidget, self)._event_filter_console_keypress(event)
293
293
294 def _insert_continuation_prompt(self, cursor):
294 def _insert_continuation_prompt(self, cursor):
295 """ Reimplemented for auto-indentation.
295 """ Reimplemented for auto-indentation.
296 """
296 """
297 super(FrontendWidget, self)._insert_continuation_prompt(cursor)
297 super(FrontendWidget, self)._insert_continuation_prompt(cursor)
298 cursor.insertText(' ' * self._input_splitter.indent_spaces)
298 cursor.insertText(' ' * self._input_splitter.indent_spaces)
299
299
300 #---------------------------------------------------------------------------
300 #---------------------------------------------------------------------------
301 # 'BaseFrontendMixin' abstract interface
301 # 'BaseFrontendMixin' abstract interface
302 #---------------------------------------------------------------------------
302 #---------------------------------------------------------------------------
303
303
304 def _handle_complete_reply(self, rep):
304 def _handle_complete_reply(self, rep):
305 """ Handle replies for tab completion.
305 """ Handle replies for tab completion.
306 """
306 """
307 self.log.debug("complete: %s", rep.get('content', ''))
307 self.log.debug("complete: %s", rep.get('content', ''))
308 cursor = self._get_cursor()
308 cursor = self._get_cursor()
309 info = self._request_info.get('complete')
309 info = self._request_info.get('complete')
310 if info and info.id == rep['parent_header']['msg_id'] and \
310 if info and info.id == rep['parent_header']['msg_id'] and \
311 info.pos == cursor.position():
311 info.pos == cursor.position():
312 text = '.'.join(self._get_context())
312 text = '.'.join(self._get_context())
313 cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
313 cursor.movePosition(QtGui.QTextCursor.Left, n=len(text))
314 self._complete_with_items(cursor, rep['content']['matches'])
314 self._complete_with_items(cursor, rep['content']['matches'])
315
315
316 def _silent_exec_callback(self, expr, callback):
316 def _silent_exec_callback(self, expr, callback):
317 """Silently execute `expr` in the kernel and call `callback` with reply
317 """Silently execute `expr` in the kernel and call `callback` with reply
318
318
319 the `expr` is evaluated silently in the kernel (without) output in
319 the `expr` is evaluated silently in the kernel (without) output in
320 the frontend. Call `callback` with the
320 the frontend. Call `callback` with the
321 `repr <http://docs.python.org/library/functions.html#repr> `_ as first argument
321 `repr <http://docs.python.org/library/functions.html#repr> `_ as first argument
322
322
323 Parameters
323 Parameters
324 ----------
324 ----------
325 expr : string
325 expr : string
326 valid string to be executed by the kernel.
326 valid string to be executed by the kernel.
327 callback : function
327 callback : function
328 function accepting one arguement, as a string. The string will be
328 function accepting one arguement, as a string. The string will be
329 the `repr` of the result of evaluating `expr`
329 the `repr` of the result of evaluating `expr`
330
330
331 The `callback` is called with the 'repr()' of the result of `expr` as
331 The `callback` is called with the 'repr()' of the result of `expr` as
332 first argument. To get the object, do 'eval()' onthe passed value.
332 first argument. To get the object, do 'eval()' onthe passed value.
333
333
334 See Also
334 See Also
335 --------
335 --------
336 _handle_exec_callback : private method, deal with calling callback with reply
336 _handle_exec_callback : private method, deal with calling callback with reply
337
337
338 """
338 """
339
339
340 # generate uuid, which would be used as a indication of wether or not
340 # generate uuid, which would be used as a indication of wether or not
341 # the unique request originate from here (can use msg id ?)
341 # the unique request originate from here (can use msg id ?)
342 local_uuid = str(uuid.uuid1())
342 local_uuid = str(uuid.uuid1())
343 msg_id = self.kernel_manager.shell_channel.execute('',
343 msg_id = self.kernel_manager.shell_channel.execute('',
344 silent=True, user_expressions={ local_uuid:expr })
344 silent=True, user_expressions={ local_uuid:expr })
345 self._callback_dict[local_uuid] = callback
345 self._callback_dict[local_uuid] = callback
346 self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
346 self._request_info['execute'] = self._ExecutionRequest(msg_id, 'silent_exec_callback')
347
347
348 def _handle_exec_callback(self, msg):
348 def _handle_exec_callback(self, msg):
349 """Execute `callback` corresonding to `msg` reply, after ``_silent_exec_callback``
349 """Execute `callback` corresonding to `msg` reply, after ``_silent_exec_callback``
350
350
351 Parameters
351 Parameters
352 ----------
352 ----------
353 msg : raw message send by the kernel containing an `user_expressions`
353 msg : raw message send by the kernel containing an `user_expressions`
354 and having a 'silent_exec_callback' kind.
354 and having a 'silent_exec_callback' kind.
355
355
356 Notes
356 Notes
357 -----
357 -----
358 This fonction will look for a `callback` associated with the
358 This fonction will look for a `callback` associated with the
359 corresponding message id. Association has been made by
359 corresponding message id. Association has been made by
360 `_silent_exec_callback`. `callback` is then called with the `repr()`
360 `_silent_exec_callback`. `callback` is then called with the `repr()`
361 of the value of corresponding `user_expressions` as argument.
361 of the value of corresponding `user_expressions` as argument.
362 `callback` is then removed from the known list so that any message
362 `callback` is then removed from the known list so that any message
363 coming again with the same id won't trigger it.
363 coming again with the same id won't trigger it.
364
364
365 """
365 """
366
366
367 user_exp = msg['content']['user_expressions']
367 user_exp = msg['content']['user_expressions']
368 for expression in user_exp:
368 for expression in user_exp:
369 if expression in self._callback_dict:
369 if expression in self._callback_dict:
370 self._callback_dict.pop(expression)(user_exp[expression])
370 self._callback_dict.pop(expression)(user_exp[expression])
371
371
372 def _handle_execute_reply(self, msg):
372 def _handle_execute_reply(self, msg):
373 """ Handles replies for code execution.
373 """ Handles replies for code execution.
374 """
374 """
375 self.log.debug("execute: %s", msg.get('content', ''))
375 self.log.debug("execute: %s", msg.get('content', ''))
376 info = self._request_info.get('execute')
376 info = self._request_info.get('execute')
377 # unset reading flag, because if execute finished, raw_input can't
377 # unset reading flag, because if execute finished, raw_input can't
378 # still be pending.
378 # still be pending.
379 self._reading = False
379 self._reading = False
380 if info and info.id == msg['parent_header']['msg_id'] and \
380 if info and info.id == msg['parent_header']['msg_id'] and \
381 info.kind == 'user' and not self._hidden:
381 info.kind == 'user' and not self._hidden:
382 # Make sure that all output from the SUB channel has been processed
382 # Make sure that all output from the SUB channel has been processed
383 # before writing a new prompt.
383 # before writing a new prompt.
384 self.kernel_manager.sub_channel.flush()
384 self.kernel_manager.sub_channel.flush()
385
385
386 # Reset the ANSI style information to prevent bad text in stdout
386 # Reset the ANSI style information to prevent bad text in stdout
387 # from messing up our colors. We're not a true terminal so we're
387 # from messing up our colors. We're not a true terminal so we're
388 # allowed to do this.
388 # allowed to do this.
389 if self.ansi_codes:
389 if self.ansi_codes:
390 self._ansi_processor.reset_sgr()
390 self._ansi_processor.reset_sgr()
391
391
392 content = msg['content']
392 content = msg['content']
393 status = content['status']
393 status = content['status']
394 if status == 'ok':
394 if status == 'ok':
395 self._process_execute_ok(msg)
395 self._process_execute_ok(msg)
396 elif status == 'error':
396 elif status == 'error':
397 self._process_execute_error(msg)
397 self._process_execute_error(msg)
398 elif status == 'aborted':
398 elif status == 'aborted':
399 self._process_execute_abort(msg)
399 self._process_execute_abort(msg)
400
400
401 self._show_interpreter_prompt_for_reply(msg)
401 self._show_interpreter_prompt_for_reply(msg)
402 self.executed.emit(msg)
402 self.executed.emit(msg)
403 elif info and info.id == msg['parent_header']['msg_id'] and \
403 elif info and info.id == msg['parent_header']['msg_id'] and \
404 info.kind == 'silent_exec_callback' and not self._hidden:
404 info.kind == 'silent_exec_callback' and not self._hidden:
405 self._handle_exec_callback(msg)
405 self._handle_exec_callback(msg)
406 else:
406 else:
407 super(FrontendWidget, self)._handle_execute_reply(msg)
407 super(FrontendWidget, self)._handle_execute_reply(msg)
408
408
409 def _handle_input_request(self, msg):
409 def _handle_input_request(self, msg):
410 """ Handle requests for raw_input.
410 """ Handle requests for raw_input.
411 """
411 """
412 self.log.debug("input: %s", msg.get('content', ''))
412 self.log.debug("input: %s", msg.get('content', ''))
413 if self._hidden:
413 if self._hidden:
414 raise RuntimeError('Request for raw input during hidden execution.')
414 raise RuntimeError('Request for raw input during hidden execution.')
415
415
416 # Make sure that all output from the SUB channel has been processed
416 # Make sure that all output from the SUB channel has been processed
417 # before entering readline mode.
417 # before entering readline mode.
418 self.kernel_manager.sub_channel.flush()
418 self.kernel_manager.sub_channel.flush()
419
419
420 def callback(line):
420 def callback(line):
421 self.kernel_manager.stdin_channel.input(line)
421 self.kernel_manager.stdin_channel.input(line)
422 if self._reading:
422 if self._reading:
423 self.log.debug("Got second input request, assuming first was interrupted.")
423 self.log.debug("Got second input request, assuming first was interrupted.")
424 self._reading = False
424 self._reading = False
425 self._readline(msg['content']['prompt'], callback=callback)
425 self._readline(msg['content']['prompt'], callback=callback)
426
426
427 def _handle_kernel_died(self, since_last_heartbeat):
427 def _handle_kernel_died(self, since_last_heartbeat):
428 """ Handle the kernel's death by asking if the user wants to restart.
428 """ Handle the kernel's death by asking if the user wants to restart.
429 """
429 """
430 self.log.debug("kernel died: %s", since_last_heartbeat)
430 self.log.debug("kernel died: %s", since_last_heartbeat)
431 if self.custom_restart:
431 if self.custom_restart:
432 self.custom_restart_kernel_died.emit(since_last_heartbeat)
432 self.custom_restart_kernel_died.emit(since_last_heartbeat)
433 else:
433 else:
434 message = 'The kernel heartbeat has been inactive for %.2f ' \
434 message = 'The kernel heartbeat has been inactive for %.2f ' \
435 'seconds. Do you want to restart the kernel? You may ' \
435 'seconds. Do you want to restart the kernel? You may ' \
436 'first want to check the network connection.' % \
436 'first want to check the network connection.' % \
437 since_last_heartbeat
437 since_last_heartbeat
438 self.restart_kernel(message, now=True)
438 self.restart_kernel(message, now=True)
439
439
440 def _handle_object_info_reply(self, rep):
440 def _handle_object_info_reply(self, rep):
441 """ Handle replies for call tips.
441 """ Handle replies for call tips.
442 """
442 """
443 self.log.debug("oinfo: %s", rep.get('content', ''))
443 self.log.debug("oinfo: %s", rep.get('content', ''))
444 cursor = self._get_cursor()
444 cursor = self._get_cursor()
445 info = self._request_info.get('call_tip')
445 info = self._request_info.get('call_tip')
446 if info and info.id == rep['parent_header']['msg_id'] and \
446 if info and info.id == rep['parent_header']['msg_id'] and \
447 info.pos == cursor.position():
447 info.pos == cursor.position():
448 # Get the information for a call tip. For now we format the call
448 # Get the information for a call tip. For now we format the call
449 # line as string, later we can pass False to format_call and
449 # line as string, later we can pass False to format_call and
450 # syntax-highlight it ourselves for nicer formatting in the
450 # syntax-highlight it ourselves for nicer formatting in the
451 # calltip.
451 # calltip.
452 content = rep['content']
452 content = rep['content']
453 # if this is from pykernel, 'docstring' will be the only key
453 # if this is from pykernel, 'docstring' will be the only key
454 if content.get('ismagic', False):
454 if content.get('ismagic', False):
455 # Don't generate a call-tip for magics. Ideally, we should
455 # Don't generate a call-tip for magics. Ideally, we should
456 # generate a tooltip, but not on ( like we do for actual
456 # generate a tooltip, but not on ( like we do for actual
457 # callables.
457 # callables.
458 call_info, doc = None, None
458 call_info, doc = None, None
459 else:
459 else:
460 call_info, doc = call_tip(content, format_call=True)
460 call_info, doc = call_tip(content, format_call=True)
461 if call_info or doc:
461 if call_info or doc:
462 self._call_tip_widget.show_call_info(call_info, doc)
462 self._call_tip_widget.show_call_info(call_info, doc)
463
463
464 def _handle_pyout(self, msg):
464 def _handle_pyout(self, msg):
465 """ Handle display hook output.
465 """ Handle display hook output.
466 """
466 """
467 self.log.debug("pyout: %s", msg.get('content', ''))
467 self.log.debug("pyout: %s", msg.get('content', ''))
468 if not self._hidden and self._is_from_this_session(msg):
468 if not self._hidden and self._is_from_this_session(msg):
469 text = msg['content']['data']
469 text = msg['content']['data']
470 self._append_plain_text(text + '\n', before_prompt=True)
470 self._append_plain_text(text + '\n', before_prompt=True)
471
471
472 def _handle_stream(self, msg):
472 def _handle_stream(self, msg):
473 """ Handle stdout, stderr, and stdin.
473 """ Handle stdout, stderr, and stdin.
474 """
474 """
475 self.log.debug("stream: %s", msg.get('content', ''))
475 self.log.debug("stream: %s", msg.get('content', ''))
476 if not self._hidden and self._is_from_this_session(msg):
476 if not self._hidden and self._is_from_this_session(msg):
477 # Most consoles treat tabs as being 8 space characters. Convert tabs
477 # Most consoles treat tabs as being 8 space characters. Convert tabs
478 # to spaces so that output looks as expected regardless of this
478 # to spaces so that output looks as expected regardless of this
479 # widget's tab width.
479 # widget's tab width.
480 text = msg['content']['data'].expandtabs(8)
480 text = msg['content']['data'].expandtabs(8)
481
481
482 self._append_plain_text(text, before_prompt=True)
482 self._append_plain_text(text, before_prompt=True)
483 self._control.moveCursor(QtGui.QTextCursor.End)
483 self._control.moveCursor(QtGui.QTextCursor.End)
484
484
485 def _handle_shutdown_reply(self, msg):
485 def _handle_shutdown_reply(self, msg):
486 """ Handle shutdown signal, only if from other console.
486 """ Handle shutdown signal, only if from other console.
487 """
487 """
488 self.log.debug("shutdown: %s", msg.get('content', ''))
488 self.log.debug("shutdown: %s", msg.get('content', ''))
489 if not self._hidden and not self._is_from_this_session(msg):
489 if not self._hidden and not self._is_from_this_session(msg):
490 if self._local_kernel:
490 if self._local_kernel:
491 if not msg['content']['restart']:
491 if not msg['content']['restart']:
492 self.exit_requested.emit(self)
492 self.exit_requested.emit(self)
493 else:
493 else:
494 # we just got notified of a restart!
494 # we just got notified of a restart!
495 time.sleep(0.25) # wait 1/4 sec to reset
495 time.sleep(0.25) # wait 1/4 sec to reset
496 # lest the request for a new prompt
496 # lest the request for a new prompt
497 # goes to the old kernel
497 # goes to the old kernel
498 self.reset()
498 self.reset()
499 else: # remote kernel, prompt on Kernel shutdown/reset
499 else: # remote kernel, prompt on Kernel shutdown/reset
500 title = self.window().windowTitle()
500 title = self.window().windowTitle()
501 if not msg['content']['restart']:
501 if not msg['content']['restart']:
502 reply = QtGui.QMessageBox.question(self, title,
502 reply = QtGui.QMessageBox.question(self, title,
503 "Kernel has been shutdown permanently. "
503 "Kernel has been shutdown permanently. "
504 "Close the Console?",
504 "Close the Console?",
505 QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
505 QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
506 if reply == QtGui.QMessageBox.Yes:
506 if reply == QtGui.QMessageBox.Yes:
507 self.exit_requested.emit(self)
507 self.exit_requested.emit(self)
508 else:
508 else:
509 reply = QtGui.QMessageBox.question(self, title,
509 reply = QtGui.QMessageBox.question(self, title,
510 "Kernel has been reset. Clear the Console?",
510 "Kernel has been reset. Clear the Console?",
511 QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
511 QtGui.QMessageBox.Yes,QtGui.QMessageBox.No)
512 if reply == QtGui.QMessageBox.Yes:
512 if reply == QtGui.QMessageBox.Yes:
513 time.sleep(0.25) # wait 1/4 sec to reset
513 time.sleep(0.25) # wait 1/4 sec to reset
514 # lest the request for a new prompt
514 # lest the request for a new prompt
515 # goes to the old kernel
515 # goes to the old kernel
516 self.reset()
516 self.reset()
517
517
518 def _started_channels(self):
518 def _started_channels(self):
519 """ Called when the KernelManager channels have started listening or
519 """ Called when the KernelManager channels have started listening or
520 when the frontend is assigned an already listening KernelManager.
520 when the frontend is assigned an already listening KernelManager.
521 """
521 """
522 self.reset()
522 self.reset()
523
523
524 #---------------------------------------------------------------------------
524 #---------------------------------------------------------------------------
525 # 'FrontendWidget' public interface
525 # 'FrontendWidget' public interface
526 #---------------------------------------------------------------------------
526 #---------------------------------------------------------------------------
527
527
528 def copy_raw(self):
528 def copy_raw(self):
529 """ Copy the currently selected text to the clipboard without attempting
529 """ Copy the currently selected text to the clipboard without attempting
530 to remove prompts or otherwise alter the text.
530 to remove prompts or otherwise alter the text.
531 """
531 """
532 self._control.copy()
532 self._control.copy()
533
533
534 def execute_file(self, path, hidden=False):
534 def execute_file(self, path, hidden=False):
535 """ Attempts to execute file with 'path'. If 'hidden', no output is
535 """ Attempts to execute file with 'path'. If 'hidden', no output is
536 shown.
536 shown.
537 """
537 """
538 self.execute('execfile(%r)' % path, hidden=hidden)
538 self.execute('execfile(%r)' % path, hidden=hidden)
539
539
540 def interrupt_kernel(self):
540 def interrupt_kernel(self):
541 """ Attempts to interrupt the running kernel.
541 """ Attempts to interrupt the running kernel.
542
542
543 Also unsets _reading flag, to avoid runtime errors
543 Also unsets _reading flag, to avoid runtime errors
544 if raw_input is called again.
544 if raw_input is called again.
545 """
545 """
546 if self.custom_interrupt:
546 if self.custom_interrupt:
547 self._reading = False
547 self._reading = False
548 self.custom_interrupt_requested.emit()
548 self.custom_interrupt_requested.emit()
549 elif self.kernel_manager.has_kernel:
549 elif self.kernel_manager.has_kernel:
550 self._reading = False
550 self._reading = False
551 self.kernel_manager.interrupt_kernel()
551 self.kernel_manager.interrupt_kernel()
552 else:
552 else:
553 self._append_plain_text('Kernel process is either remote or '
553 self._append_plain_text('Kernel process is either remote or '
554 'unspecified. Cannot interrupt.\n')
554 'unspecified. Cannot interrupt.\n')
555
555
556 def reset(self):
556 def reset(self):
557 """ Resets the widget to its initial state. Similar to ``clear``, but
557 """ Resets the widget to its initial state. Similar to ``clear``, but
558 also re-writes the banner and aborts execution if necessary.
558 also re-writes the banner and aborts execution if necessary.
559 """
559 """
560 if self._executing:
560 if self._executing:
561 self._executing = False
561 self._executing = False
562 self._request_info['execute'] = None
562 self._request_info['execute'] = None
563 self._reading = False
563 self._reading = False
564 self._highlighter.highlighting_on = False
564 self._highlighter.highlighting_on = False
565
565
566 self._control.clear()
566 self._control.clear()
567 self._append_plain_text(self.banner)
567 self._append_plain_text(self.banner)
568 # update output marker for stdout/stderr, so that startup
568 # update output marker for stdout/stderr, so that startup
569 # messages appear after banner:
569 # messages appear after banner:
570 self._append_before_prompt_pos = self._get_cursor().position()
570 self._append_before_prompt_pos = self._get_cursor().position()
571 self._show_interpreter_prompt()
571 self._show_interpreter_prompt()
572
572
573 def restart_kernel(self, message, now=False):
573 def restart_kernel(self, message, now=False):
574 """ Attempts to restart the running kernel.
574 """ Attempts to restart the running kernel.
575 """
575 """
576 # FIXME: now should be configurable via a checkbox in the dialog. Right
576 # FIXME: now should be configurable via a checkbox in the dialog. Right
577 # now at least the heartbeat path sets it to True and the manual restart
577 # now at least the heartbeat path sets it to True and the manual restart
578 # to False. But those should just be the pre-selected states of a
578 # to False. But those should just be the pre-selected states of a
579 # checkbox that the user could override if so desired. But I don't know
579 # checkbox that the user could override if so desired. But I don't know
580 # enough Qt to go implementing the checkbox now.
580 # enough Qt to go implementing the checkbox now.
581
581
582 if self.custom_restart:
582 if self.custom_restart:
583 self.custom_restart_requested.emit()
583 self.custom_restart_requested.emit()
584
584
585 elif self.kernel_manager.has_kernel:
585 elif self.kernel_manager.has_kernel:
586 # Pause the heart beat channel to prevent further warnings.
586 # Pause the heart beat channel to prevent further warnings.
587 self.kernel_manager.hb_channel.pause()
587 self.kernel_manager.hb_channel.pause()
588
588
589 # Prompt the user to restart the kernel. Un-pause the heartbeat if
589 # Prompt the user to restart the kernel. Un-pause the heartbeat if
590 # they decline. (If they accept, the heartbeat will be un-paused
590 # they decline. (If they accept, the heartbeat will be un-paused
591 # automatically when the kernel is restarted.)
591 # automatically when the kernel is restarted.)
592 buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
592 buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No
593 result = QtGui.QMessageBox.question(self, 'Restart kernel?',
593 result = QtGui.QMessageBox.question(self, 'Restart kernel?',
594 message, buttons)
594 message, buttons)
595 if result == QtGui.QMessageBox.Yes:
595 if result == QtGui.QMessageBox.Yes:
596 try:
596 try:
597 self.kernel_manager.restart_kernel(now=now)
597 self.kernel_manager.restart_kernel(now=now)
598 except RuntimeError:
598 except RuntimeError:
599 self._append_plain_text('Kernel started externally. '
599 self._append_plain_text('Kernel started externally. '
600 'Cannot restart.\n')
600 'Cannot restart.\n',
601 before_prompt=True
602 )
601 else:
603 else:
602 self.reset()
604 self.reset()
603 else:
605 else:
604 self.kernel_manager.hb_channel.unpause()
606 self.kernel_manager.hb_channel.unpause()
605
607
606 else:
608 else:
607 self._append_plain_text('Kernel process is either remote or '
609 self._append_plain_text('Kernel process is either remote or '
608 'unspecified. Cannot restart.\n')
610 'unspecified. Cannot restart.\n',
611 before_prompt=True
612 )
609
613
610 #---------------------------------------------------------------------------
614 #---------------------------------------------------------------------------
611 # 'FrontendWidget' protected interface
615 # 'FrontendWidget' protected interface
612 #---------------------------------------------------------------------------
616 #---------------------------------------------------------------------------
613
617
614 def _call_tip(self):
618 def _call_tip(self):
615 """ Shows a call tip, if appropriate, at the current cursor location.
619 """ Shows a call tip, if appropriate, at the current cursor location.
616 """
620 """
617 # Decide if it makes sense to show a call tip
621 # Decide if it makes sense to show a call tip
618 if not self.enable_calltips:
622 if not self.enable_calltips:
619 return False
623 return False
620 cursor = self._get_cursor()
624 cursor = self._get_cursor()
621 cursor.movePosition(QtGui.QTextCursor.Left)
625 cursor.movePosition(QtGui.QTextCursor.Left)
622 if cursor.document().characterAt(cursor.position()) != '(':
626 if cursor.document().characterAt(cursor.position()) != '(':
623 return False
627 return False
624 context = self._get_context(cursor)
628 context = self._get_context(cursor)
625 if not context:
629 if not context:
626 return False
630 return False
627
631
628 # Send the metadata request to the kernel
632 # Send the metadata request to the kernel
629 name = '.'.join(context)
633 name = '.'.join(context)
630 msg_id = self.kernel_manager.shell_channel.object_info(name)
634 msg_id = self.kernel_manager.shell_channel.object_info(name)
631 pos = self._get_cursor().position()
635 pos = self._get_cursor().position()
632 self._request_info['call_tip'] = self._CallTipRequest(msg_id, pos)
636 self._request_info['call_tip'] = self._CallTipRequest(msg_id, pos)
633 return True
637 return True
634
638
635 def _complete(self):
639 def _complete(self):
636 """ Performs completion at the current cursor location.
640 """ Performs completion at the current cursor location.
637 """
641 """
638 context = self._get_context()
642 context = self._get_context()
639 if context:
643 if context:
640 # Send the completion request to the kernel
644 # Send the completion request to the kernel
641 msg_id = self.kernel_manager.shell_channel.complete(
645 msg_id = self.kernel_manager.shell_channel.complete(
642 '.'.join(context), # text
646 '.'.join(context), # text
643 self._get_input_buffer_cursor_line(), # line
647 self._get_input_buffer_cursor_line(), # line
644 self._get_input_buffer_cursor_column(), # cursor_pos
648 self._get_input_buffer_cursor_column(), # cursor_pos
645 self.input_buffer) # block
649 self.input_buffer) # block
646 pos = self._get_cursor().position()
650 pos = self._get_cursor().position()
647 info = self._CompletionRequest(msg_id, pos)
651 info = self._CompletionRequest(msg_id, pos)
648 self._request_info['complete'] = info
652 self._request_info['complete'] = info
649
653
650 def _get_context(self, cursor=None):
654 def _get_context(self, cursor=None):
651 """ Gets the context for the specified cursor (or the current cursor
655 """ Gets the context for the specified cursor (or the current cursor
652 if none is specified).
656 if none is specified).
653 """
657 """
654 if cursor is None:
658 if cursor is None:
655 cursor = self._get_cursor()
659 cursor = self._get_cursor()
656 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
660 cursor.movePosition(QtGui.QTextCursor.StartOfBlock,
657 QtGui.QTextCursor.KeepAnchor)
661 QtGui.QTextCursor.KeepAnchor)
658 text = cursor.selection().toPlainText()
662 text = cursor.selection().toPlainText()
659 return self._completion_lexer.get_context(text)
663 return self._completion_lexer.get_context(text)
660
664
661 def _process_execute_abort(self, msg):
665 def _process_execute_abort(self, msg):
662 """ Process a reply for an aborted execution request.
666 """ Process a reply for an aborted execution request.
663 """
667 """
664 self._append_plain_text("ERROR: execution aborted\n")
668 self._append_plain_text("ERROR: execution aborted\n")
665
669
666 def _process_execute_error(self, msg):
670 def _process_execute_error(self, msg):
667 """ Process a reply for an execution request that resulted in an error.
671 """ Process a reply for an execution request that resulted in an error.
668 """
672 """
669 content = msg['content']
673 content = msg['content']
670 # If a SystemExit is passed along, this means exit() was called - also
674 # If a SystemExit is passed along, this means exit() was called - also
671 # all the ipython %exit magic syntax of '-k' to be used to keep
675 # all the ipython %exit magic syntax of '-k' to be used to keep
672 # the kernel running
676 # the kernel running
673 if content['ename']=='SystemExit':
677 if content['ename']=='SystemExit':
674 keepkernel = content['evalue']=='-k' or content['evalue']=='True'
678 keepkernel = content['evalue']=='-k' or content['evalue']=='True'
675 self._keep_kernel_on_exit = keepkernel
679 self._keep_kernel_on_exit = keepkernel
676 self.exit_requested.emit(self)
680 self.exit_requested.emit(self)
677 else:
681 else:
678 traceback = ''.join(content['traceback'])
682 traceback = ''.join(content['traceback'])
679 self._append_plain_text(traceback)
683 self._append_plain_text(traceback)
680
684
681 def _process_execute_ok(self, msg):
685 def _process_execute_ok(self, msg):
682 """ Process a reply for a successful execution equest.
686 """ Process a reply for a successful execution equest.
683 """
687 """
684 payload = msg['content']['payload']
688 payload = msg['content']['payload']
685 for item in payload:
689 for item in payload:
686 if not self._process_execute_payload(item):
690 if not self._process_execute_payload(item):
687 warning = 'Warning: received unknown payload of type %s'
691 warning = 'Warning: received unknown payload of type %s'
688 print(warning % repr(item['source']))
692 print(warning % repr(item['source']))
689
693
690 def _process_execute_payload(self, item):
694 def _process_execute_payload(self, item):
691 """ Process a single payload item from the list of payload items in an
695 """ Process a single payload item from the list of payload items in an
692 execution reply. Returns whether the payload was handled.
696 execution reply. Returns whether the payload was handled.
693 """
697 """
694 # The basic FrontendWidget doesn't handle payloads, as they are a
698 # The basic FrontendWidget doesn't handle payloads, as they are a
695 # mechanism for going beyond the standard Python interpreter model.
699 # mechanism for going beyond the standard Python interpreter model.
696 return False
700 return False
697
701
698 def _show_interpreter_prompt(self):
702 def _show_interpreter_prompt(self):
699 """ Shows a prompt for the interpreter.
703 """ Shows a prompt for the interpreter.
700 """
704 """
701 self._show_prompt('>>> ')
705 self._show_prompt('>>> ')
702
706
703 def _show_interpreter_prompt_for_reply(self, msg):
707 def _show_interpreter_prompt_for_reply(self, msg):
704 """ Shows a prompt for the interpreter given an 'execute_reply' message.
708 """ Shows a prompt for the interpreter given an 'execute_reply' message.
705 """
709 """
706 self._show_interpreter_prompt()
710 self._show_interpreter_prompt()
707
711
708 #------ Signal handlers ----------------------------------------------------
712 #------ Signal handlers ----------------------------------------------------
709
713
710 def _document_contents_change(self, position, removed, added):
714 def _document_contents_change(self, position, removed, added):
711 """ Called whenever the document's content changes. Display a call tip
715 """ Called whenever the document's content changes. Display a call tip
712 if appropriate.
716 if appropriate.
713 """
717 """
714 # Calculate where the cursor should be *after* the change:
718 # Calculate where the cursor should be *after* the change:
715 position += added
719 position += added
716
720
717 document = self._control.document()
721 document = self._control.document()
718 if position == self._get_cursor().position():
722 if position == self._get_cursor().position():
719 self._call_tip()
723 self._call_tip()
720
724
721 #------ Trait default initializers -----------------------------------------
725 #------ Trait default initializers -----------------------------------------
722
726
723 def _banner_default(self):
727 def _banner_default(self):
724 """ Returns the standard Python banner.
728 """ Returns the standard Python banner.
725 """
729 """
726 banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \
730 banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \
727 '"license" for more information.'
731 '"license" for more information.'
728 return banner % (sys.version, sys.platform)
732 return banner % (sys.version, sys.platform)
General Comments 0
You need to be logged in to leave comments. Login now