Show More
@@ -1,1277 +1,1271 b'' | |||||
1 | # Standard library imports |
|
1 | # Standard library imports | |
2 | import sys |
|
2 | import sys | |
3 | from textwrap import dedent |
|
3 | from textwrap import dedent | |
4 |
|
4 | |||
5 | # System library imports |
|
5 | # System library imports | |
6 | from PyQt4 import QtCore, QtGui |
|
6 | from PyQt4 import QtCore, QtGui | |
7 |
|
7 | |||
8 | # Local imports |
|
8 | # Local imports | |
9 | from ansi_code_processor import QtAnsiCodeProcessor |
|
9 | from ansi_code_processor import QtAnsiCodeProcessor | |
10 | from completion_widget import CompletionWidget |
|
10 | from completion_widget import CompletionWidget | |
11 |
|
11 | |||
12 |
|
12 | |||
13 | class ConsoleWidget(QtGui.QWidget): |
|
13 | class ConsoleWidget(QtGui.QWidget): | |
14 | """ An abstract base class for console-type widgets. This class has |
|
14 | """ An abstract base class for console-type widgets. This class has | |
15 | functionality for: |
|
15 | functionality for: | |
16 |
|
16 | |||
17 | * Maintaining a prompt and editing region |
|
17 | * Maintaining a prompt and editing region | |
18 | * Providing the traditional Unix-style console keyboard shortcuts |
|
18 | * Providing the traditional Unix-style console keyboard shortcuts | |
19 | * Performing tab completion |
|
19 | * Performing tab completion | |
20 | * Paging text |
|
20 | * Paging text | |
21 | * Handling ANSI escape codes |
|
21 | * Handling ANSI escape codes | |
22 |
|
22 | |||
23 | ConsoleWidget also provides a number of utility methods that will be |
|
23 | ConsoleWidget also provides a number of utility methods that will be | |
24 | convenient to implementors of a console-style widget. |
|
24 | convenient to implementors of a console-style widget. | |
25 | """ |
|
25 | """ | |
26 |
|
26 | |||
27 | # Whether to process ANSI escape codes. |
|
27 | # Whether to process ANSI escape codes. | |
28 | ansi_codes = True |
|
28 | ansi_codes = True | |
29 |
|
29 | |||
30 | # The maximum number of lines of text before truncation. |
|
30 | # The maximum number of lines of text before truncation. | |
31 | buffer_size = 500 |
|
31 | buffer_size = 500 | |
32 |
|
32 | |||
33 | # Whether to use a list widget or plain text output for tab completion. |
|
33 | # Whether to use a list widget or plain text output for tab completion. | |
34 | gui_completion = True |
|
34 | gui_completion = True | |
35 |
|
35 | |||
36 | # Whether to override ShortcutEvents for the keybindings defined by this |
|
36 | # Whether to override ShortcutEvents for the keybindings defined by this | |
37 | # widget (Ctrl+n, Ctrl+a, etc). Enable this if you want this widget to take |
|
37 | # widget (Ctrl+n, Ctrl+a, etc). Enable this if you want this widget to take | |
38 | # priority (when it has focus) over, e.g., window-level menu shortcuts. |
|
38 | # priority (when it has focus) over, e.g., window-level menu shortcuts. | |
39 | override_shortcuts = False |
|
39 | override_shortcuts = False | |
40 |
|
40 | |||
41 | # Signals that indicate ConsoleWidget state. |
|
41 | # Signals that indicate ConsoleWidget state. | |
42 | copy_available = QtCore.pyqtSignal(bool) |
|
42 | copy_available = QtCore.pyqtSignal(bool) | |
43 | redo_available = QtCore.pyqtSignal(bool) |
|
43 | redo_available = QtCore.pyqtSignal(bool) | |
44 | undo_available = QtCore.pyqtSignal(bool) |
|
44 | undo_available = QtCore.pyqtSignal(bool) | |
45 |
|
45 | |||
46 | # Signal emitted when paging is needed and the paging style has been |
|
46 | # Signal emitted when paging is needed and the paging style has been | |
47 | # specified as 'custom'. |
|
47 | # specified as 'custom'. | |
48 | custom_page_requested = QtCore.pyqtSignal(object) |
|
48 | custom_page_requested = QtCore.pyqtSignal(object) | |
49 |
|
49 | |||
50 | # Protected class variables. |
|
50 | # Protected class variables. | |
51 | _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left, |
|
51 | _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left, | |
52 | QtCore.Qt.Key_F : QtCore.Qt.Key_Right, |
|
52 | QtCore.Qt.Key_F : QtCore.Qt.Key_Right, | |
53 | QtCore.Qt.Key_A : QtCore.Qt.Key_Home, |
|
53 | QtCore.Qt.Key_A : QtCore.Qt.Key_Home, | |
54 | QtCore.Qt.Key_E : QtCore.Qt.Key_End, |
|
54 | QtCore.Qt.Key_E : QtCore.Qt.Key_End, | |
55 | QtCore.Qt.Key_P : QtCore.Qt.Key_Up, |
|
55 | QtCore.Qt.Key_P : QtCore.Qt.Key_Up, | |
56 | QtCore.Qt.Key_N : QtCore.Qt.Key_Down, |
|
56 | QtCore.Qt.Key_N : QtCore.Qt.Key_Down, | |
57 | QtCore.Qt.Key_D : QtCore.Qt.Key_Delete, } |
|
57 | QtCore.Qt.Key_D : QtCore.Qt.Key_Delete, } | |
58 | _shortcuts = set(_ctrl_down_remap.keys() + |
|
58 | _shortcuts = set(_ctrl_down_remap.keys() + | |
59 | [ QtCore.Qt.Key_C, QtCore.Qt.Key_V ]) |
|
59 | [ QtCore.Qt.Key_C, QtCore.Qt.Key_V ]) | |
60 |
|
60 | |||
61 | #--------------------------------------------------------------------------- |
|
61 | #--------------------------------------------------------------------------- | |
62 | # 'QObject' interface |
|
62 | # 'QObject' interface | |
63 | #--------------------------------------------------------------------------- |
|
63 | #--------------------------------------------------------------------------- | |
64 |
|
64 | |||
65 | def __init__(self, kind='plain', paging='inside', parent=None): |
|
65 | def __init__(self, kind='plain', paging='inside', parent=None): | |
66 | """ Create a ConsoleWidget. |
|
66 | """ Create a ConsoleWidget. | |
67 |
|
67 | |||
68 | Parameters |
|
68 | Parameters | |
69 | ---------- |
|
69 | ---------- | |
70 | kind : str, optional [default 'plain'] |
|
70 | kind : str, optional [default 'plain'] | |
71 | The type of underlying text widget to use. Valid values are 'plain', |
|
71 | The type of underlying text widget to use. Valid values are 'plain', | |
72 | which specifies a QPlainTextEdit, and 'rich', which specifies a |
|
72 | which specifies a QPlainTextEdit, and 'rich', which specifies a | |
73 | QTextEdit. |
|
73 | QTextEdit. | |
74 |
|
74 | |||
75 | paging : str, optional [default 'inside'] |
|
75 | paging : str, optional [default 'inside'] | |
76 | The type of paging to use. Valid values are: |
|
76 | The type of paging to use. Valid values are: | |
77 | 'inside' : The widget pages like a traditional terminal pager. |
|
77 | 'inside' : The widget pages like a traditional terminal pager. | |
78 | 'hsplit' : When paging is requested, the widget is split |
|
78 | 'hsplit' : When paging is requested, the widget is split | |
79 | horizontally. The top pane contains the console, |
|
79 | horizontally. The top pane contains the console, | |
80 | and the bottom pane contains the paged text. |
|
80 | and the bottom pane contains the paged text. | |
81 | 'vsplit' : Similar to 'hsplit', except that a vertical splitter |
|
81 | 'vsplit' : Similar to 'hsplit', except that a vertical splitter | |
82 | used. |
|
82 | used. | |
83 | 'custom' : No action is taken by the widget beyond emitting a |
|
83 | 'custom' : No action is taken by the widget beyond emitting a | |
84 | 'custom_page_requested(str)' signal. |
|
84 | 'custom_page_requested(str)' signal. | |
85 | 'none' : The text is written directly to the console. |
|
85 | 'none' : The text is written directly to the console. | |
86 |
|
86 | |||
87 | parent : QWidget, optional [default None] |
|
87 | parent : QWidget, optional [default None] | |
88 | The parent for this widget. |
|
88 | The parent for this widget. | |
89 | """ |
|
89 | """ | |
90 | super(ConsoleWidget, self).__init__(parent) |
|
90 | super(ConsoleWidget, self).__init__(parent) | |
91 |
|
91 | |||
92 | # Create the layout and underlying text widget. |
|
92 | # Create the layout and underlying text widget. | |
93 | layout = QtGui.QStackedLayout(self) |
|
93 | layout = QtGui.QStackedLayout(self) | |
94 | layout.setMargin(0) |
|
94 | layout.setMargin(0) | |
95 | self._control = self._create_control(kind) |
|
95 | self._control = self._create_control(kind) | |
96 | self._page_control = None |
|
96 | self._page_control = None | |
97 | self._splitter = None |
|
97 | self._splitter = None | |
98 | if paging in ('hsplit', 'vsplit'): |
|
98 | if paging in ('hsplit', 'vsplit'): | |
99 | self._splitter = QtGui.QSplitter() |
|
99 | self._splitter = QtGui.QSplitter() | |
100 | if paging == 'hsplit': |
|
100 | if paging == 'hsplit': | |
101 | self._splitter.setOrientation(QtCore.Qt.Horizontal) |
|
101 | self._splitter.setOrientation(QtCore.Qt.Horizontal) | |
102 | else: |
|
102 | else: | |
103 | self._splitter.setOrientation(QtCore.Qt.Vertical) |
|
103 | self._splitter.setOrientation(QtCore.Qt.Vertical) | |
104 | self._splitter.addWidget(self._control) |
|
104 | self._splitter.addWidget(self._control) | |
105 | layout.addWidget(self._splitter) |
|
105 | layout.addWidget(self._splitter) | |
106 | else: |
|
106 | else: | |
107 | layout.addWidget(self._control) |
|
107 | layout.addWidget(self._control) | |
108 |
|
108 | |||
109 | # Create the paging widget, if necessary. |
|
109 | # Create the paging widget, if necessary. | |
110 | self._page_style = paging |
|
110 | self._page_style = paging | |
111 | if paging in ('inside', 'hsplit', 'vsplit'): |
|
111 | if paging in ('inside', 'hsplit', 'vsplit'): | |
112 | self._page_control = self._create_page_control() |
|
112 | self._page_control = self._create_page_control() | |
113 | if self._splitter: |
|
113 | if self._splitter: | |
114 | self._page_control.hide() |
|
114 | self._page_control.hide() | |
115 | self._splitter.addWidget(self._page_control) |
|
115 | self._splitter.addWidget(self._page_control) | |
116 | else: |
|
116 | else: | |
117 | layout.addWidget(self._page_control) |
|
117 | layout.addWidget(self._page_control) | |
118 | elif paging not in ('custom', 'none'): |
|
118 | elif paging not in ('custom', 'none'): | |
119 | raise ValueError('Paging style %s unknown.' % repr(paging)) |
|
119 | raise ValueError('Paging style %s unknown.' % repr(paging)) | |
120 |
|
120 | |||
121 | # Initialize protected variables. Some variables contain useful state |
|
121 | # Initialize protected variables. Some variables contain useful state | |
122 | # information for subclasses; they should be considered read-only. |
|
122 | # information for subclasses; they should be considered read-only. | |
123 | self._ansi_processor = QtAnsiCodeProcessor() |
|
123 | self._ansi_processor = QtAnsiCodeProcessor() | |
124 | self._completion_widget = CompletionWidget(self._control) |
|
124 | self._completion_widget = CompletionWidget(self._control) | |
125 | self._continuation_prompt = '> ' |
|
125 | self._continuation_prompt = '> ' | |
126 | self._continuation_prompt_html = None |
|
126 | self._continuation_prompt_html = None | |
127 | self._executing = False |
|
127 | self._executing = False | |
128 | self._prompt = '' |
|
128 | self._prompt = '' | |
129 | self._prompt_html = None |
|
129 | self._prompt_html = None | |
130 | self._prompt_pos = 0 |
|
130 | self._prompt_pos = 0 | |
131 | self._reading = False |
|
131 | self._reading = False | |
132 | self._reading_callback = None |
|
132 | self._reading_callback = None | |
133 | self._tab_width = 8 |
|
133 | self._tab_width = 8 | |
134 |
|
134 | |||
135 | # Set a monospaced font. |
|
135 | # Set a monospaced font. | |
136 | self.reset_font() |
|
136 | self.reset_font() | |
137 |
|
137 | |||
138 | def eventFilter(self, obj, event): |
|
138 | def eventFilter(self, obj, event): | |
139 | """ Reimplemented to ensure a console-like behavior in the underlying |
|
139 | """ Reimplemented to ensure a console-like behavior in the underlying | |
140 | text widget. |
|
140 | text widget. | |
141 | """ |
|
141 | """ | |
142 | # Re-map keys for all filtered widgets. |
|
142 | # Re-map keys for all filtered widgets. | |
143 | etype = event.type() |
|
143 | etype = event.type() | |
144 | if etype == QtCore.QEvent.KeyPress and \ |
|
144 | if etype == QtCore.QEvent.KeyPress and \ | |
145 | self._control_key_down(event.modifiers()) and \ |
|
145 | self._control_key_down(event.modifiers()) and \ | |
146 | event.key() in self._ctrl_down_remap: |
|
146 | event.key() in self._ctrl_down_remap: | |
147 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, |
|
147 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, | |
148 | self._ctrl_down_remap[event.key()], |
|
148 | self._ctrl_down_remap[event.key()], | |
149 | QtCore.Qt.NoModifier) |
|
149 | QtCore.Qt.NoModifier) | |
150 | QtGui.qApp.sendEvent(obj, new_event) |
|
150 | QtGui.qApp.sendEvent(obj, new_event) | |
151 | return True |
|
151 | return True | |
152 |
|
152 | |||
153 | # Override shortucts for all filtered widgets. Note that on Mac OS it is |
|
153 | # Override shortucts for all filtered widgets. Note that on Mac OS it is | |
154 | # always unnecessary to override shortcuts, hence the check below (users |
|
154 | # always unnecessary to override shortcuts, hence the check below (users | |
155 | # should just use the Control key instead of the Command key). |
|
155 | # should just use the Control key instead of the Command key). | |
156 | elif etype == QtCore.QEvent.ShortcutOverride and \ |
|
156 | elif etype == QtCore.QEvent.ShortcutOverride and \ | |
157 | sys.platform != 'darwin' and \ |
|
157 | sys.platform != 'darwin' and \ | |
158 | self._control_key_down(event.modifiers()) and \ |
|
158 | self._control_key_down(event.modifiers()) and \ | |
159 | event.key() in self._shortcuts: |
|
159 | event.key() in self._shortcuts: | |
160 | event.accept() |
|
160 | event.accept() | |
161 | return False |
|
161 | return False | |
162 |
|
162 | |||
163 | elif obj == self._control: |
|
163 | elif obj == self._control: | |
164 | # Disable moving text by drag and drop. |
|
164 | # Disable moving text by drag and drop. | |
165 | if etype == QtCore.QEvent.DragMove: |
|
165 | if etype == QtCore.QEvent.DragMove: | |
166 | return True |
|
166 | return True | |
167 |
|
167 | |||
168 | elif etype == QtCore.QEvent.KeyPress: |
|
168 | elif etype == QtCore.QEvent.KeyPress: | |
169 | return self._event_filter_console_keypress(event) |
|
169 | return self._event_filter_console_keypress(event) | |
170 |
|
170 | |||
171 | elif obj == self._page_control: |
|
171 | elif obj == self._page_control: | |
172 | if etype == QtCore.QEvent.KeyPress: |
|
172 | if etype == QtCore.QEvent.KeyPress: | |
173 | return self._event_filter_page_keypress(event) |
|
173 | return self._event_filter_page_keypress(event) | |
174 |
|
174 | |||
175 | return super(ConsoleWidget, self).eventFilter(obj, event) |
|
175 | return super(ConsoleWidget, self).eventFilter(obj, event) | |
176 |
|
176 | |||
177 | #--------------------------------------------------------------------------- |
|
177 | #--------------------------------------------------------------------------- | |
178 | # 'QWidget' interface |
|
178 | # 'QWidget' interface | |
179 | #--------------------------------------------------------------------------- |
|
179 | #--------------------------------------------------------------------------- | |
180 |
|
180 | |||
181 | def sizeHint(self): |
|
181 | def sizeHint(self): | |
182 | """ Reimplemented to suggest a size that is 80 characters wide and |
|
182 | """ Reimplemented to suggest a size that is 80 characters wide and | |
183 | 25 lines high. |
|
183 | 25 lines high. | |
184 | """ |
|
184 | """ | |
185 | style = self.style() |
|
185 | style = self.style() | |
186 | opt = QtGui.QStyleOptionHeader() |
|
186 | opt = QtGui.QStyleOptionHeader() | |
187 | font_metrics = QtGui.QFontMetrics(self.font) |
|
187 | font_metrics = QtGui.QFontMetrics(self.font) | |
188 | splitwidth = style.pixelMetric(QtGui.QStyle.PM_SplitterWidth, opt, self) |
|
188 | splitwidth = style.pixelMetric(QtGui.QStyle.PM_SplitterWidth, opt, self) | |
189 |
|
189 | |||
190 | width = font_metrics.width(' ') * 80 |
|
190 | width = font_metrics.width(' ') * 80 | |
191 | width += style.pixelMetric(QtGui.QStyle.PM_ScrollBarExtent, opt, self) |
|
191 | width += style.pixelMetric(QtGui.QStyle.PM_ScrollBarExtent, opt, self) | |
192 | if self._page_style == 'hsplit': |
|
192 | if self._page_style == 'hsplit': | |
193 | width = width * 2 + splitwidth |
|
193 | width = width * 2 + splitwidth | |
194 |
|
194 | |||
195 | height = font_metrics.height() * 25 |
|
195 | height = font_metrics.height() * 25 | |
196 | if self._page_style == 'vsplit': |
|
196 | if self._page_style == 'vsplit': | |
197 | height = height * 2 + splitwidth |
|
197 | height = height * 2 + splitwidth | |
198 |
|
198 | |||
199 | return QtCore.QSize(width, height) |
|
199 | return QtCore.QSize(width, height) | |
200 |
|
200 | |||
201 | #--------------------------------------------------------------------------- |
|
201 | #--------------------------------------------------------------------------- | |
202 | # 'ConsoleWidget' public interface |
|
202 | # 'ConsoleWidget' public interface | |
203 | #--------------------------------------------------------------------------- |
|
203 | #--------------------------------------------------------------------------- | |
204 |
|
204 | |||
205 | def can_paste(self): |
|
205 | def can_paste(self): | |
206 | """ Returns whether text can be pasted from the clipboard. |
|
206 | """ Returns whether text can be pasted from the clipboard. | |
207 | """ |
|
207 | """ | |
208 | # Accept only text that can be ASCII encoded. |
|
208 | # Accept only text that can be ASCII encoded. | |
209 | if self._control.textInteractionFlags() & QtCore.Qt.TextEditable: |
|
209 | if self._control.textInteractionFlags() & QtCore.Qt.TextEditable: | |
210 | text = QtGui.QApplication.clipboard().text() |
|
210 | text = QtGui.QApplication.clipboard().text() | |
211 | if not text.isEmpty(): |
|
211 | if not text.isEmpty(): | |
212 | try: |
|
212 | try: | |
213 | str(text) |
|
213 | str(text) | |
214 | return True |
|
214 | return True | |
215 | except UnicodeEncodeError: |
|
215 | except UnicodeEncodeError: | |
216 | pass |
|
216 | pass | |
217 | return False |
|
217 | return False | |
218 |
|
218 | |||
219 |
def clear(self, keep_input= |
|
219 | def clear(self, keep_input=True): | |
220 | """ Clear the console, then write a new prompt. If 'keep_input' is set, |
|
220 | """ Clear the console, then write a new prompt. If 'keep_input' is set, | |
221 | restores the old input buffer when the new prompt is written. |
|
221 | restores the old input buffer when the new prompt is written. | |
222 | """ |
|
222 | """ | |
223 | self._control.clear() |
|
|||
224 | if keep_input: |
|
223 | if keep_input: | |
225 | input_buffer = self.input_buffer |
|
224 | input_buffer = self.input_buffer | |
|
225 | self._control.clear() | |||
226 | self._show_prompt() |
|
226 | self._show_prompt() | |
227 | if keep_input: |
|
227 | if keep_input: | |
228 | self.input_buffer = input_buffer |
|
228 | self.input_buffer = input_buffer | |
229 |
|
229 | |||
230 | def copy(self): |
|
230 | def copy(self): | |
231 | """ Copy the current selected text to the clipboard. |
|
231 | """ Copy the current selected text to the clipboard. | |
232 | """ |
|
232 | """ | |
233 | self._control.copy() |
|
233 | self._control.copy() | |
234 |
|
234 | |||
235 | def execute(self, source=None, hidden=False, interactive=False): |
|
235 | def execute(self, source=None, hidden=False, interactive=False): | |
236 | """ Executes source or the input buffer, possibly prompting for more |
|
236 | """ Executes source or the input buffer, possibly prompting for more | |
237 | input. |
|
237 | input. | |
238 |
|
238 | |||
239 | Parameters: |
|
239 | Parameters: | |
240 | ----------- |
|
240 | ----------- | |
241 | source : str, optional |
|
241 | source : str, optional | |
242 |
|
242 | |||
243 | The source to execute. If not specified, the input buffer will be |
|
243 | The source to execute. If not specified, the input buffer will be | |
244 | used. If specified and 'hidden' is False, the input buffer will be |
|
244 | used. If specified and 'hidden' is False, the input buffer will be | |
245 | replaced with the source before execution. |
|
245 | replaced with the source before execution. | |
246 |
|
246 | |||
247 | hidden : bool, optional (default False) |
|
247 | hidden : bool, optional (default False) | |
248 |
|
248 | |||
249 | If set, no output will be shown and the prompt will not be modified. |
|
249 | If set, no output will be shown and the prompt will not be modified. | |
250 | In other words, it will be completely invisible to the user that |
|
250 | In other words, it will be completely invisible to the user that | |
251 | an execution has occurred. |
|
251 | an execution has occurred. | |
252 |
|
252 | |||
253 | interactive : bool, optional (default False) |
|
253 | interactive : bool, optional (default False) | |
254 |
|
254 | |||
255 | Whether the console is to treat the source as having been manually |
|
255 | Whether the console is to treat the source as having been manually | |
256 | entered by the user. The effect of this parameter depends on the |
|
256 | entered by the user. The effect of this parameter depends on the | |
257 | subclass implementation. |
|
257 | subclass implementation. | |
258 |
|
258 | |||
259 | Raises: |
|
259 | Raises: | |
260 | ------- |
|
260 | ------- | |
261 | RuntimeError |
|
261 | RuntimeError | |
262 | If incomplete input is given and 'hidden' is True. In this case, |
|
262 | If incomplete input is given and 'hidden' is True. In this case, | |
263 | it is not possible to prompt for more input. |
|
263 | it is not possible to prompt for more input. | |
264 |
|
264 | |||
265 | Returns: |
|
265 | Returns: | |
266 | -------- |
|
266 | -------- | |
267 | A boolean indicating whether the source was executed. |
|
267 | A boolean indicating whether the source was executed. | |
268 | """ |
|
268 | """ | |
269 | if not hidden: |
|
269 | if not hidden: | |
270 | if source is not None: |
|
270 | if source is not None: | |
271 | self.input_buffer = source |
|
271 | self.input_buffer = source | |
272 |
|
272 | |||
273 | self._append_plain_text('\n') |
|
273 | self._append_plain_text('\n') | |
274 | self._executing_input_buffer = self.input_buffer |
|
274 | self._executing_input_buffer = self.input_buffer | |
275 | self._executing = True |
|
275 | self._executing = True | |
276 | self._prompt_finished() |
|
276 | self._prompt_finished() | |
277 |
|
277 | |||
278 | real_source = self.input_buffer if source is None else source |
|
278 | real_source = self.input_buffer if source is None else source | |
279 | complete = self._is_complete(real_source, interactive) |
|
279 | complete = self._is_complete(real_source, interactive) | |
280 | if complete: |
|
280 | if complete: | |
281 | if not hidden: |
|
281 | if not hidden: | |
282 | # The maximum block count is only in effect during execution. |
|
282 | # The maximum block count is only in effect during execution. | |
283 | # This ensures that _prompt_pos does not become invalid due to |
|
283 | # This ensures that _prompt_pos does not become invalid due to | |
284 | # text truncation. |
|
284 | # text truncation. | |
285 | self._control.document().setMaximumBlockCount(self.buffer_size) |
|
285 | self._control.document().setMaximumBlockCount(self.buffer_size) | |
286 | self._execute(real_source, hidden) |
|
286 | self._execute(real_source, hidden) | |
287 | elif hidden: |
|
287 | elif hidden: | |
288 | raise RuntimeError('Incomplete noninteractive input: "%s"' % source) |
|
288 | raise RuntimeError('Incomplete noninteractive input: "%s"' % source) | |
289 | else: |
|
289 | else: | |
290 | self._show_continuation_prompt() |
|
290 | self._show_continuation_prompt() | |
291 |
|
291 | |||
292 | return complete |
|
292 | return complete | |
293 |
|
293 | |||
294 | def _get_input_buffer(self): |
|
294 | def _get_input_buffer(self): | |
295 | """ The text that the user has entered entered at the current prompt. |
|
295 | """ The text that the user has entered entered at the current prompt. | |
296 | """ |
|
296 | """ | |
297 | # If we're executing, the input buffer may not even exist anymore due to |
|
297 | # If we're executing, the input buffer may not even exist anymore due to | |
298 | # the limit imposed by 'buffer_size'. Therefore, we store it. |
|
298 | # the limit imposed by 'buffer_size'. Therefore, we store it. | |
299 | if self._executing: |
|
299 | if self._executing: | |
300 | return self._executing_input_buffer |
|
300 | return self._executing_input_buffer | |
301 |
|
301 | |||
302 | cursor = self._get_end_cursor() |
|
302 | cursor = self._get_end_cursor() | |
303 | cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor) |
|
303 | cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor) | |
304 | input_buffer = str(cursor.selection().toPlainText()) |
|
304 | input_buffer = str(cursor.selection().toPlainText()) | |
305 |
|
305 | |||
306 | # Strip out continuation prompts. |
|
306 | # Strip out continuation prompts. | |
307 | return input_buffer.replace('\n' + self._continuation_prompt, '\n') |
|
307 | return input_buffer.replace('\n' + self._continuation_prompt, '\n') | |
308 |
|
308 | |||
309 | def _set_input_buffer(self, string): |
|
309 | def _set_input_buffer(self, string): | |
310 | """ Replaces the text in the input buffer with 'string'. |
|
310 | """ Replaces the text in the input buffer with 'string'. | |
311 | """ |
|
311 | """ | |
312 | # For now, it is an error to modify the input buffer during execution. |
|
312 | # For now, it is an error to modify the input buffer during execution. | |
313 | if self._executing: |
|
313 | if self._executing: | |
314 | raise RuntimeError("Cannot change input buffer during execution.") |
|
314 | raise RuntimeError("Cannot change input buffer during execution.") | |
315 |
|
315 | |||
316 | # Remove old text. |
|
316 | # Remove old text. | |
317 | cursor = self._get_end_cursor() |
|
317 | cursor = self._get_end_cursor() | |
318 | cursor.beginEditBlock() |
|
318 | cursor.beginEditBlock() | |
319 | cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor) |
|
319 | cursor.setPosition(self._prompt_pos, QtGui.QTextCursor.KeepAnchor) | |
320 | cursor.removeSelectedText() |
|
320 | cursor.removeSelectedText() | |
321 |
|
321 | |||
322 | # Insert new text with continuation prompts. |
|
322 | # Insert new text with continuation prompts. | |
323 | lines = string.splitlines(True) |
|
323 | lines = string.splitlines(True) | |
324 | if lines: |
|
324 | if lines: | |
325 | self._append_plain_text(lines[0]) |
|
325 | self._append_plain_text(lines[0]) | |
326 | for i in xrange(1, len(lines)): |
|
326 | for i in xrange(1, len(lines)): | |
327 | if self._continuation_prompt_html is None: |
|
327 | if self._continuation_prompt_html is None: | |
328 | self._append_plain_text(self._continuation_prompt) |
|
328 | self._append_plain_text(self._continuation_prompt) | |
329 | else: |
|
329 | else: | |
330 | self._append_html(self._continuation_prompt_html) |
|
330 | self._append_html(self._continuation_prompt_html) | |
331 | self._append_plain_text(lines[i]) |
|
331 | self._append_plain_text(lines[i]) | |
332 | cursor.endEditBlock() |
|
332 | cursor.endEditBlock() | |
333 | self._control.moveCursor(QtGui.QTextCursor.End) |
|
333 | self._control.moveCursor(QtGui.QTextCursor.End) | |
334 |
|
334 | |||
335 | input_buffer = property(_get_input_buffer, _set_input_buffer) |
|
335 | input_buffer = property(_get_input_buffer, _set_input_buffer) | |
336 |
|
336 | |||
337 | def _get_font(self): |
|
337 | def _get_font(self): | |
338 | """ The base font being used by the ConsoleWidget. |
|
338 | """ The base font being used by the ConsoleWidget. | |
339 | """ |
|
339 | """ | |
340 | return self._control.document().defaultFont() |
|
340 | return self._control.document().defaultFont() | |
341 |
|
341 | |||
342 | def _set_font(self, font): |
|
342 | def _set_font(self, font): | |
343 | """ Sets the base font for the ConsoleWidget to the specified QFont. |
|
343 | """ Sets the base font for the ConsoleWidget to the specified QFont. | |
344 | """ |
|
344 | """ | |
345 | font_metrics = QtGui.QFontMetrics(font) |
|
345 | font_metrics = QtGui.QFontMetrics(font) | |
346 | self._control.setTabStopWidth(self.tab_width * font_metrics.width(' ')) |
|
346 | self._control.setTabStopWidth(self.tab_width * font_metrics.width(' ')) | |
347 |
|
347 | |||
348 | self._completion_widget.setFont(font) |
|
348 | self._completion_widget.setFont(font) | |
349 | self._control.document().setDefaultFont(font) |
|
349 | self._control.document().setDefaultFont(font) | |
350 | if self._page_control: |
|
350 | if self._page_control: | |
351 | self._page_control.document().setDefaultFont(font) |
|
351 | self._page_control.document().setDefaultFont(font) | |
352 |
|
352 | |||
353 | font = property(_get_font, _set_font) |
|
353 | font = property(_get_font, _set_font) | |
354 |
|
354 | |||
355 | def paste(self): |
|
355 | def paste(self): | |
356 | """ Paste the contents of the clipboard into the input region. |
|
356 | """ Paste the contents of the clipboard into the input region. | |
357 | """ |
|
357 | """ | |
358 | if self._control.textInteractionFlags() & QtCore.Qt.TextEditable: |
|
358 | if self._control.textInteractionFlags() & QtCore.Qt.TextEditable: | |
359 | try: |
|
359 | try: | |
360 | text = str(QtGui.QApplication.clipboard().text()) |
|
360 | text = str(QtGui.QApplication.clipboard().text()) | |
361 | except UnicodeEncodeError: |
|
361 | except UnicodeEncodeError: | |
362 | pass |
|
362 | pass | |
363 | else: |
|
363 | else: | |
364 | self._insert_plain_text_into_buffer(dedent(text)) |
|
364 | self._insert_plain_text_into_buffer(dedent(text)) | |
365 |
|
365 | |||
366 | def print_(self, printer): |
|
366 | def print_(self, printer): | |
367 | """ Print the contents of the ConsoleWidget to the specified QPrinter. |
|
367 | """ Print the contents of the ConsoleWidget to the specified QPrinter. | |
368 | """ |
|
368 | """ | |
369 | self._control.print_(printer) |
|
369 | self._control.print_(printer) | |
370 |
|
370 | |||
371 | def redo(self): |
|
371 | def redo(self): | |
372 | """ Redo the last operation. If there is no operation to redo, nothing |
|
372 | """ Redo the last operation. If there is no operation to redo, nothing | |
373 | happens. |
|
373 | happens. | |
374 | """ |
|
374 | """ | |
375 | self._control.redo() |
|
375 | self._control.redo() | |
376 |
|
376 | |||
377 | def reset_font(self): |
|
377 | def reset_font(self): | |
378 | """ Sets the font to the default fixed-width font for this platform. |
|
378 | """ Sets the font to the default fixed-width font for this platform. | |
379 | """ |
|
379 | """ | |
380 | if sys.platform == 'win32': |
|
380 | if sys.platform == 'win32': | |
381 | name = 'Courier' |
|
381 | name = 'Courier' | |
382 | elif sys.platform == 'darwin': |
|
382 | elif sys.platform == 'darwin': | |
383 | name = 'Monaco' |
|
383 | name = 'Monaco' | |
384 | else: |
|
384 | else: | |
385 | name = 'Monospace' |
|
385 | name = 'Monospace' | |
386 | font = QtGui.QFont(name, QtGui.qApp.font().pointSize()) |
|
386 | font = QtGui.QFont(name, QtGui.qApp.font().pointSize()) | |
387 | font.setStyleHint(QtGui.QFont.TypeWriter) |
|
387 | font.setStyleHint(QtGui.QFont.TypeWriter) | |
388 | self._set_font(font) |
|
388 | self._set_font(font) | |
389 |
|
389 | |||
390 | def select_all(self): |
|
390 | def select_all(self): | |
391 | """ Selects all the text in the buffer. |
|
391 | """ Selects all the text in the buffer. | |
392 | """ |
|
392 | """ | |
393 | self._control.selectAll() |
|
393 | self._control.selectAll() | |
394 |
|
394 | |||
395 | def _get_tab_width(self): |
|
395 | def _get_tab_width(self): | |
396 | """ The width (in terms of space characters) for tab characters. |
|
396 | """ The width (in terms of space characters) for tab characters. | |
397 | """ |
|
397 | """ | |
398 | return self._tab_width |
|
398 | return self._tab_width | |
399 |
|
399 | |||
400 | def _set_tab_width(self, tab_width): |
|
400 | def _set_tab_width(self, tab_width): | |
401 | """ Sets the width (in terms of space characters) for tab characters. |
|
401 | """ Sets the width (in terms of space characters) for tab characters. | |
402 | """ |
|
402 | """ | |
403 | font_metrics = QtGui.QFontMetrics(self.font) |
|
403 | font_metrics = QtGui.QFontMetrics(self.font) | |
404 | self._control.setTabStopWidth(tab_width * font_metrics.width(' ')) |
|
404 | self._control.setTabStopWidth(tab_width * font_metrics.width(' ')) | |
405 |
|
405 | |||
406 | self._tab_width = tab_width |
|
406 | self._tab_width = tab_width | |
407 |
|
407 | |||
408 | tab_width = property(_get_tab_width, _set_tab_width) |
|
408 | tab_width = property(_get_tab_width, _set_tab_width) | |
409 |
|
409 | |||
410 | def undo(self): |
|
410 | def undo(self): | |
411 | """ Undo the last operation. If there is no operation to undo, nothing |
|
411 | """ Undo the last operation. If there is no operation to undo, nothing | |
412 | happens. |
|
412 | happens. | |
413 | """ |
|
413 | """ | |
414 | self._control.undo() |
|
414 | self._control.undo() | |
415 |
|
415 | |||
416 | #--------------------------------------------------------------------------- |
|
416 | #--------------------------------------------------------------------------- | |
417 | # 'ConsoleWidget' abstract interface |
|
417 | # 'ConsoleWidget' abstract interface | |
418 | #--------------------------------------------------------------------------- |
|
418 | #--------------------------------------------------------------------------- | |
419 |
|
419 | |||
420 | def _is_complete(self, source, interactive): |
|
420 | def _is_complete(self, source, interactive): | |
421 | """ Returns whether 'source' can be executed. When triggered by an |
|
421 | """ Returns whether 'source' can be executed. When triggered by an | |
422 | Enter/Return key press, 'interactive' is True; otherwise, it is |
|
422 | Enter/Return key press, 'interactive' is True; otherwise, it is | |
423 | False. |
|
423 | False. | |
424 | """ |
|
424 | """ | |
425 | raise NotImplementedError |
|
425 | raise NotImplementedError | |
426 |
|
426 | |||
427 | def _execute(self, source, hidden): |
|
427 | def _execute(self, source, hidden): | |
428 | """ Execute 'source'. If 'hidden', do not show any output. |
|
428 | """ Execute 'source'. If 'hidden', do not show any output. | |
429 | """ |
|
429 | """ | |
430 | raise NotImplementedError |
|
430 | raise NotImplementedError | |
431 |
|
431 | |||
432 | def _execute_interrupt(self): |
|
432 | def _execute_interrupt(self): | |
433 | """ Attempts to stop execution. Returns whether this method has an |
|
433 | """ Attempts to stop execution. Returns whether this method has an | |
434 | implementation. |
|
434 | implementation. | |
435 | """ |
|
435 | """ | |
436 | return False |
|
436 | return False | |
437 |
|
437 | |||
438 | def _prompt_started_hook(self): |
|
438 | def _prompt_started_hook(self): | |
439 | """ Called immediately after a new prompt is displayed. |
|
439 | """ Called immediately after a new prompt is displayed. | |
440 | """ |
|
440 | """ | |
441 | pass |
|
441 | pass | |
442 |
|
442 | |||
443 | def _prompt_finished_hook(self): |
|
443 | def _prompt_finished_hook(self): | |
444 | """ Called immediately after a prompt is finished, i.e. when some input |
|
444 | """ Called immediately after a prompt is finished, i.e. when some input | |
445 | will be processed and a new prompt displayed. |
|
445 | will be processed and a new prompt displayed. | |
446 | """ |
|
446 | """ | |
447 | pass |
|
447 | pass | |
448 |
|
448 | |||
449 | def _up_pressed(self): |
|
449 | def _up_pressed(self): | |
450 | """ Called when the up key is pressed. Returns whether to continue |
|
450 | """ Called when the up key is pressed. Returns whether to continue | |
451 | processing the event. |
|
451 | processing the event. | |
452 | """ |
|
452 | """ | |
453 | return True |
|
453 | return True | |
454 |
|
454 | |||
455 | def _down_pressed(self): |
|
455 | def _down_pressed(self): | |
456 | """ Called when the down key is pressed. Returns whether to continue |
|
456 | """ Called when the down key is pressed. Returns whether to continue | |
457 | processing the event. |
|
457 | processing the event. | |
458 | """ |
|
458 | """ | |
459 | return True |
|
459 | return True | |
460 |
|
460 | |||
461 | def _tab_pressed(self): |
|
461 | def _tab_pressed(self): | |
462 | """ Called when the tab key is pressed. Returns whether to continue |
|
462 | """ Called when the tab key is pressed. Returns whether to continue | |
463 | processing the event. |
|
463 | processing the event. | |
464 | """ |
|
464 | """ | |
465 | return False |
|
465 | return False | |
466 |
|
466 | |||
467 | #-------------------------------------------------------------------------- |
|
467 | #-------------------------------------------------------------------------- | |
468 | # 'ConsoleWidget' protected interface |
|
468 | # 'ConsoleWidget' protected interface | |
469 | #-------------------------------------------------------------------------- |
|
469 | #-------------------------------------------------------------------------- | |
470 |
|
470 | |||
471 | def _append_html(self, html): |
|
471 | def _append_html(self, html): | |
472 | """ Appends html at the end of the console buffer. |
|
472 | """ Appends html at the end of the console buffer. | |
473 | """ |
|
473 | """ | |
474 | cursor = self._get_end_cursor() |
|
474 | cursor = self._get_end_cursor() | |
475 | self._insert_html(cursor, html) |
|
475 | self._insert_html(cursor, html) | |
476 |
|
476 | |||
477 | def _append_html_fetching_plain_text(self, html): |
|
477 | def _append_html_fetching_plain_text(self, html): | |
478 | """ Appends 'html', then returns the plain text version of it. |
|
478 | """ Appends 'html', then returns the plain text version of it. | |
479 | """ |
|
479 | """ | |
480 | cursor = self._get_end_cursor() |
|
480 | cursor = self._get_end_cursor() | |
481 | return self._insert_html_fetching_plain_text(cursor, html) |
|
481 | return self._insert_html_fetching_plain_text(cursor, html) | |
482 |
|
482 | |||
483 | def _append_plain_text(self, text): |
|
483 | def _append_plain_text(self, text): | |
484 | """ Appends plain text at the end of the console buffer, processing |
|
484 | """ Appends plain text at the end of the console buffer, processing | |
485 | ANSI codes if enabled. |
|
485 | ANSI codes if enabled. | |
486 | """ |
|
486 | """ | |
487 | cursor = self._get_end_cursor() |
|
487 | cursor = self._get_end_cursor() | |
488 | self._insert_plain_text(cursor, text) |
|
488 | self._insert_plain_text(cursor, text) | |
489 |
|
489 | |||
490 | def _append_plain_text_keeping_prompt(self, text): |
|
490 | def _append_plain_text_keeping_prompt(self, text): | |
491 | """ Writes 'text' after the current prompt, then restores the old prompt |
|
491 | """ Writes 'text' after the current prompt, then restores the old prompt | |
492 | with its old input buffer. |
|
492 | with its old input buffer. | |
493 | """ |
|
493 | """ | |
494 | input_buffer = self.input_buffer |
|
494 | input_buffer = self.input_buffer | |
495 | self._append_plain_text('\n') |
|
495 | self._append_plain_text('\n') | |
496 | self._prompt_finished() |
|
496 | self._prompt_finished() | |
497 |
|
497 | |||
498 | self._append_plain_text(text) |
|
498 | self._append_plain_text(text) | |
499 | self._show_prompt() |
|
499 | self._show_prompt() | |
500 | self.input_buffer = input_buffer |
|
500 | self.input_buffer = input_buffer | |
501 |
|
501 | |||
502 | def _complete_with_items(self, cursor, items): |
|
502 | def _complete_with_items(self, cursor, items): | |
503 | """ Performs completion with 'items' at the specified cursor location. |
|
503 | """ Performs completion with 'items' at the specified cursor location. | |
504 | """ |
|
504 | """ | |
505 | if len(items) == 1: |
|
505 | if len(items) == 1: | |
506 | cursor.setPosition(self._control.textCursor().position(), |
|
506 | cursor.setPosition(self._control.textCursor().position(), | |
507 | QtGui.QTextCursor.KeepAnchor) |
|
507 | QtGui.QTextCursor.KeepAnchor) | |
508 | cursor.insertText(items[0]) |
|
508 | cursor.insertText(items[0]) | |
509 | elif len(items) > 1: |
|
509 | elif len(items) > 1: | |
510 | if self.gui_completion: |
|
510 | if self.gui_completion: | |
511 | self._completion_widget.show_items(cursor, items) |
|
511 | self._completion_widget.show_items(cursor, items) | |
512 | else: |
|
512 | else: | |
513 | text = self._format_as_columns(items) |
|
513 | text = self._format_as_columns(items) | |
514 | self._append_plain_text_keeping_prompt(text) |
|
514 | self._append_plain_text_keeping_prompt(text) | |
515 |
|
515 | |||
516 | def _control_key_down(self, modifiers): |
|
516 | def _control_key_down(self, modifiers): | |
517 | """ Given a KeyboardModifiers flags object, return whether the Control |
|
517 | """ Given a KeyboardModifiers flags object, return whether the Control | |
518 | key is down (on Mac OS, treat the Command key as a synonym for |
|
518 | key is down (on Mac OS, treat the Command key as a synonym for | |
519 | Control). |
|
519 | Control). | |
520 | """ |
|
520 | """ | |
521 | down = bool(modifiers & QtCore.Qt.ControlModifier) |
|
521 | down = bool(modifiers & QtCore.Qt.ControlModifier) | |
522 |
|
522 | |||
523 | # Note: on Mac OS, ControlModifier corresponds to the Command key while |
|
523 | # Note: on Mac OS, ControlModifier corresponds to the Command key while | |
524 | # MetaModifier corresponds to the Control key. |
|
524 | # MetaModifier corresponds to the Control key. | |
525 | if sys.platform == 'darwin': |
|
525 | if sys.platform == 'darwin': | |
526 | down = down ^ bool(modifiers & QtCore.Qt.MetaModifier) |
|
526 | down = down ^ bool(modifiers & QtCore.Qt.MetaModifier) | |
527 |
|
527 | |||
528 | return down |
|
528 | return down | |
529 |
|
529 | |||
530 | def _create_control(self, kind): |
|
530 | def _create_control(self, kind): | |
531 | """ Creates and connects the underlying text widget. |
|
531 | """ Creates and connects the underlying text widget. | |
532 | """ |
|
532 | """ | |
533 | if kind == 'plain': |
|
533 | if kind == 'plain': | |
534 | control = QtGui.QPlainTextEdit() |
|
534 | control = QtGui.QPlainTextEdit() | |
535 | elif kind == 'rich': |
|
535 | elif kind == 'rich': | |
536 | control = QtGui.QTextEdit() |
|
536 | control = QtGui.QTextEdit() | |
537 | control.setAcceptRichText(False) |
|
537 | control.setAcceptRichText(False) | |
538 | else: |
|
538 | else: | |
539 | raise ValueError("Kind %s unknown." % repr(kind)) |
|
539 | raise ValueError("Kind %s unknown." % repr(kind)) | |
540 | control.installEventFilter(self) |
|
540 | control.installEventFilter(self) | |
541 | control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) |
|
541 | control.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) | |
542 | control.customContextMenuRequested.connect(self._show_context_menu) |
|
542 | control.customContextMenuRequested.connect(self._show_context_menu) | |
543 | control.copyAvailable.connect(self.copy_available) |
|
543 | control.copyAvailable.connect(self.copy_available) | |
544 | control.redoAvailable.connect(self.redo_available) |
|
544 | control.redoAvailable.connect(self.redo_available) | |
545 | control.undoAvailable.connect(self.undo_available) |
|
545 | control.undoAvailable.connect(self.undo_available) | |
546 | control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) |
|
546 | control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
547 | return control |
|
547 | return control | |
548 |
|
548 | |||
549 | def _create_page_control(self): |
|
549 | def _create_page_control(self): | |
550 | """ Creates and connects the underlying paging widget. |
|
550 | """ Creates and connects the underlying paging widget. | |
551 | """ |
|
551 | """ | |
552 | control = QtGui.QPlainTextEdit() |
|
552 | control = QtGui.QPlainTextEdit() | |
553 | control.installEventFilter(self) |
|
553 | control.installEventFilter(self) | |
554 | control.setReadOnly(True) |
|
554 | control.setReadOnly(True) | |
555 | control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) |
|
555 | control.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn) | |
556 | return control |
|
556 | return control | |
557 |
|
557 | |||
558 | def _event_filter_console_keypress(self, event): |
|
558 | def _event_filter_console_keypress(self, event): | |
559 | """ Filter key events for the underlying text widget to create a |
|
559 | """ Filter key events for the underlying text widget to create a | |
560 | console-like interface. |
|
560 | console-like interface. | |
561 | """ |
|
561 | """ | |
562 | intercepted = False |
|
562 | intercepted = False | |
563 | cursor = self._control.textCursor() |
|
563 | cursor = self._control.textCursor() | |
564 | position = cursor.position() |
|
564 | position = cursor.position() | |
565 | key = event.key() |
|
565 | key = event.key() | |
566 | ctrl_down = self._control_key_down(event.modifiers()) |
|
566 | ctrl_down = self._control_key_down(event.modifiers()) | |
567 | alt_down = event.modifiers() & QtCore.Qt.AltModifier |
|
567 | alt_down = event.modifiers() & QtCore.Qt.AltModifier | |
568 | shift_down = event.modifiers() & QtCore.Qt.ShiftModifier |
|
568 | shift_down = event.modifiers() & QtCore.Qt.ShiftModifier | |
569 |
|
569 | |||
570 | if event.matches(QtGui.QKeySequence.Paste): |
|
570 | if event.matches(QtGui.QKeySequence.Paste): | |
571 | # Call our paste instead of the underlying text widget's. |
|
571 | # Call our paste instead of the underlying text widget's. | |
572 | self.paste() |
|
572 | self.paste() | |
573 | intercepted = True |
|
573 | intercepted = True | |
574 |
|
574 | |||
575 | elif ctrl_down: |
|
575 | elif ctrl_down: | |
576 | if key == QtCore.Qt.Key_C: |
|
576 | if key == QtCore.Qt.Key_C: | |
577 | intercepted = self._executing and self._execute_interrupt() |
|
577 | intercepted = self._executing and self._execute_interrupt() | |
578 |
|
578 | |||
579 | elif key == QtCore.Qt.Key_K: |
|
579 | elif key == QtCore.Qt.Key_K: | |
580 | if self._in_buffer(position): |
|
580 | if self._in_buffer(position): | |
581 | cursor.movePosition(QtGui.QTextCursor.EndOfLine, |
|
581 | cursor.movePosition(QtGui.QTextCursor.EndOfLine, | |
582 | QtGui.QTextCursor.KeepAnchor) |
|
582 | QtGui.QTextCursor.KeepAnchor) | |
583 | cursor.removeSelectedText() |
|
583 | cursor.removeSelectedText() | |
584 | intercepted = True |
|
584 | intercepted = True | |
585 |
|
585 | |||
586 | elif key == QtCore.Qt.Key_X: |
|
586 | elif key == QtCore.Qt.Key_X: | |
587 | intercepted = True |
|
587 | intercepted = True | |
588 |
|
588 | |||
589 | elif key == QtCore.Qt.Key_Y: |
|
589 | elif key == QtCore.Qt.Key_Y: | |
590 | self.paste() |
|
590 | self.paste() | |
591 | intercepted = True |
|
591 | intercepted = True | |
592 |
|
592 | |||
593 | elif alt_down: |
|
593 | elif alt_down: | |
594 | if key == QtCore.Qt.Key_B: |
|
594 | if key == QtCore.Qt.Key_B: | |
595 | self._set_cursor(self._get_word_start_cursor(position)) |
|
595 | self._set_cursor(self._get_word_start_cursor(position)) | |
596 | intercepted = True |
|
596 | intercepted = True | |
597 |
|
597 | |||
598 | elif key == QtCore.Qt.Key_F: |
|
598 | elif key == QtCore.Qt.Key_F: | |
599 | self._set_cursor(self._get_word_end_cursor(position)) |
|
599 | self._set_cursor(self._get_word_end_cursor(position)) | |
600 | intercepted = True |
|
600 | intercepted = True | |
601 |
|
601 | |||
602 | elif key == QtCore.Qt.Key_Backspace: |
|
602 | elif key == QtCore.Qt.Key_Backspace: | |
603 | cursor = self._get_word_start_cursor(position) |
|
603 | cursor = self._get_word_start_cursor(position) | |
604 | cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor) |
|
604 | cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor) | |
605 | cursor.removeSelectedText() |
|
605 | cursor.removeSelectedText() | |
606 | intercepted = True |
|
606 | intercepted = True | |
607 |
|
607 | |||
608 | elif key == QtCore.Qt.Key_D: |
|
608 | elif key == QtCore.Qt.Key_D: | |
609 | cursor = self._get_word_end_cursor(position) |
|
609 | cursor = self._get_word_end_cursor(position) | |
610 | cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor) |
|
610 | cursor.setPosition(position, QtGui.QTextCursor.KeepAnchor) | |
611 | cursor.removeSelectedText() |
|
611 | cursor.removeSelectedText() | |
612 | intercepted = True |
|
612 | intercepted = True | |
613 |
|
613 | |||
614 | else: |
|
614 | else: | |
615 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter): |
|
615 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter): | |
616 | if self._reading: |
|
616 | if self._reading: | |
617 | self._append_plain_text('\n') |
|
617 | self._append_plain_text('\n') | |
618 | self._reading = False |
|
618 | self._reading = False | |
619 | if self._reading_callback: |
|
619 | if self._reading_callback: | |
620 | self._reading_callback() |
|
620 | self._reading_callback() | |
621 | elif not self._executing: |
|
621 | elif not self._executing: | |
622 | self.execute(interactive=True) |
|
622 | self.execute(interactive=True) | |
623 | intercepted = True |
|
623 | intercepted = True | |
624 |
|
624 | |||
625 | elif key == QtCore.Qt.Key_Up: |
|
625 | elif key == QtCore.Qt.Key_Up: | |
626 | if self._reading or not self._up_pressed(): |
|
626 | if self._reading or not self._up_pressed(): | |
627 | intercepted = True |
|
627 | intercepted = True | |
628 | else: |
|
628 | else: | |
629 | prompt_line = self._get_prompt_cursor().blockNumber() |
|
629 | prompt_line = self._get_prompt_cursor().blockNumber() | |
630 | intercepted = cursor.blockNumber() <= prompt_line |
|
630 | intercepted = cursor.blockNumber() <= prompt_line | |
631 |
|
631 | |||
632 | elif key == QtCore.Qt.Key_Down: |
|
632 | elif key == QtCore.Qt.Key_Down: | |
633 | if self._reading or not self._down_pressed(): |
|
633 | if self._reading or not self._down_pressed(): | |
634 | intercepted = True |
|
634 | intercepted = True | |
635 | else: |
|
635 | else: | |
636 | end_line = self._get_end_cursor().blockNumber() |
|
636 | end_line = self._get_end_cursor().blockNumber() | |
637 | intercepted = cursor.blockNumber() == end_line |
|
637 | intercepted = cursor.blockNumber() == end_line | |
638 |
|
638 | |||
639 | elif key == QtCore.Qt.Key_Tab: |
|
639 | elif key == QtCore.Qt.Key_Tab: | |
640 | if self._reading: |
|
640 | if self._reading: | |
641 | intercepted = False |
|
641 | intercepted = False | |
642 | else: |
|
642 | else: | |
643 | intercepted = not self._tab_pressed() |
|
643 | intercepted = not self._tab_pressed() | |
644 |
|
644 | |||
645 | elif key == QtCore.Qt.Key_Left: |
|
645 | elif key == QtCore.Qt.Key_Left: | |
646 | intercepted = not self._in_buffer(position - 1) |
|
646 | intercepted = not self._in_buffer(position - 1) | |
647 |
|
647 | |||
648 | elif key == QtCore.Qt.Key_Home: |
|
648 | elif key == QtCore.Qt.Key_Home: | |
649 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) |
|
649 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) | |
650 | start_line = cursor.blockNumber() |
|
650 | start_line = cursor.blockNumber() | |
651 | if start_line == self._get_prompt_cursor().blockNumber(): |
|
651 | if start_line == self._get_prompt_cursor().blockNumber(): | |
652 | start_pos = self._prompt_pos |
|
652 | start_pos = self._prompt_pos | |
653 | else: |
|
653 | else: | |
654 | start_pos = cursor.position() |
|
654 | start_pos = cursor.position() | |
655 | start_pos += len(self._continuation_prompt) |
|
655 | start_pos += len(self._continuation_prompt) | |
656 | if shift_down and self._in_buffer(position): |
|
656 | if shift_down and self._in_buffer(position): | |
657 | self._set_selection(position, start_pos) |
|
657 | self._set_selection(position, start_pos) | |
658 | else: |
|
658 | else: | |
659 | self._set_position(start_pos) |
|
659 | self._set_position(start_pos) | |
660 | intercepted = True |
|
660 | intercepted = True | |
661 |
|
661 | |||
662 | elif key == QtCore.Qt.Key_Backspace: |
|
662 | elif key == QtCore.Qt.Key_Backspace: | |
663 |
|
663 | |||
664 | # Line deletion (remove continuation prompt) |
|
664 | # Line deletion (remove continuation prompt) | |
665 | len_prompt = len(self._continuation_prompt) |
|
665 | len_prompt = len(self._continuation_prompt) | |
666 | if not self._reading and \ |
|
666 | if not self._reading and \ | |
667 | cursor.columnNumber() == len_prompt and \ |
|
667 | cursor.columnNumber() == len_prompt and \ | |
668 | position != self._prompt_pos: |
|
668 | position != self._prompt_pos: | |
669 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, |
|
669 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, | |
670 | QtGui.QTextCursor.KeepAnchor) |
|
670 | QtGui.QTextCursor.KeepAnchor) | |
671 | cursor.removeSelectedText() |
|
671 | cursor.removeSelectedText() | |
672 | cursor.deletePreviousChar() |
|
672 | cursor.deletePreviousChar() | |
673 | intercepted = True |
|
673 | intercepted = True | |
674 |
|
674 | |||
675 | # Regular backwards deletion |
|
675 | # Regular backwards deletion | |
676 | else: |
|
676 | else: | |
677 | anchor = cursor.anchor() |
|
677 | anchor = cursor.anchor() | |
678 | if anchor == position: |
|
678 | if anchor == position: | |
679 | intercepted = not self._in_buffer(position - 1) |
|
679 | intercepted = not self._in_buffer(position - 1) | |
680 | else: |
|
680 | else: | |
681 | intercepted = not self._in_buffer(min(anchor, position)) |
|
681 | intercepted = not self._in_buffer(min(anchor, position)) | |
682 |
|
682 | |||
683 | elif key == QtCore.Qt.Key_Delete: |
|
683 | elif key == QtCore.Qt.Key_Delete: | |
684 | anchor = cursor.anchor() |
|
684 | anchor = cursor.anchor() | |
685 | intercepted = not self._in_buffer(min(anchor, position)) |
|
685 | intercepted = not self._in_buffer(min(anchor, position)) | |
686 |
|
686 | |||
687 | # Don't move the cursor if control is down to allow copy-paste using |
|
687 | # Don't move the cursor if control is down to allow copy-paste using | |
688 | # the keyboard in any part of the buffer. |
|
688 | # the keyboard in any part of the buffer. | |
689 | if not ctrl_down: |
|
689 | if not ctrl_down: | |
690 | self._keep_cursor_in_buffer() |
|
690 | self._keep_cursor_in_buffer() | |
691 |
|
691 | |||
692 | return intercepted |
|
692 | return intercepted | |
693 |
|
693 | |||
694 | def _event_filter_page_keypress(self, event): |
|
694 | def _event_filter_page_keypress(self, event): | |
695 | """ Filter key events for the paging widget to create console-like |
|
695 | """ Filter key events for the paging widget to create console-like | |
696 | interface. |
|
696 | interface. | |
697 | """ |
|
697 | """ | |
698 | key = event.key() |
|
698 | key = event.key() | |
699 |
|
699 | |||
700 | if key in (QtCore.Qt.Key_Q, QtCore.Qt.Key_Escape): |
|
700 | if key in (QtCore.Qt.Key_Q, QtCore.Qt.Key_Escape): | |
701 | if self._splitter: |
|
701 | if self._splitter: | |
702 | self._page_control.hide() |
|
702 | self._page_control.hide() | |
703 | else: |
|
703 | else: | |
704 | self.layout().setCurrentWidget(self._control) |
|
704 | self.layout().setCurrentWidget(self._control) | |
705 | return True |
|
705 | return True | |
706 |
|
706 | |||
707 | elif key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): |
|
707 | elif key in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return): | |
708 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, |
|
708 | new_event = QtGui.QKeyEvent(QtCore.QEvent.KeyPress, | |
709 | QtCore.Qt.Key_Down, |
|
709 | QtCore.Qt.Key_Down, | |
710 | QtCore.Qt.NoModifier) |
|
710 | QtCore.Qt.NoModifier) | |
711 | QtGui.qApp.sendEvent(self._page_control, new_event) |
|
711 | QtGui.qApp.sendEvent(self._page_control, new_event) | |
712 | return True |
|
712 | return True | |
713 |
|
713 | |||
714 | return False |
|
714 | return False | |
715 |
|
715 | |||
716 | def _format_as_columns(self, items, separator=' '): |
|
716 | def _format_as_columns(self, items, separator=' '): | |
717 | """ Transform a list of strings into a single string with columns. |
|
717 | """ Transform a list of strings into a single string with columns. | |
718 |
|
718 | |||
719 | Parameters |
|
719 | Parameters | |
720 | ---------- |
|
720 | ---------- | |
721 | items : sequence of strings |
|
721 | items : sequence of strings | |
722 | The strings to process. |
|
722 | The strings to process. | |
723 |
|
723 | |||
724 | separator : str, optional [default is two spaces] |
|
724 | separator : str, optional [default is two spaces] | |
725 | The string that separates columns. |
|
725 | The string that separates columns. | |
726 |
|
726 | |||
727 | Returns |
|
727 | Returns | |
728 | ------- |
|
728 | ------- | |
729 | The formatted string. |
|
729 | The formatted string. | |
730 | """ |
|
730 | """ | |
731 | # Note: this code is adapted from columnize 0.3.2. |
|
731 | # Note: this code is adapted from columnize 0.3.2. | |
732 | # See http://code.google.com/p/pycolumnize/ |
|
732 | # See http://code.google.com/p/pycolumnize/ | |
733 |
|
733 | |||
734 | width = self._control.viewport().width() |
|
734 | width = self._control.viewport().width() | |
735 | char_width = QtGui.QFontMetrics(self.font).width(' ') |
|
735 | char_width = QtGui.QFontMetrics(self.font).width(' ') | |
736 | displaywidth = max(5, width / char_width) |
|
736 | displaywidth = max(5, width / char_width) | |
737 |
|
737 | |||
738 | # Some degenerate cases. |
|
738 | # Some degenerate cases. | |
739 | size = len(items) |
|
739 | size = len(items) | |
740 | if size == 0: |
|
740 | if size == 0: | |
741 | return '\n' |
|
741 | return '\n' | |
742 | elif size == 1: |
|
742 | elif size == 1: | |
743 | return '%s\n' % str(items[0]) |
|
743 | return '%s\n' % str(items[0]) | |
744 |
|
744 | |||
745 | # Try every row count from 1 upwards |
|
745 | # Try every row count from 1 upwards | |
746 | array_index = lambda nrows, row, col: nrows*col + row |
|
746 | array_index = lambda nrows, row, col: nrows*col + row | |
747 | for nrows in range(1, size): |
|
747 | for nrows in range(1, size): | |
748 | ncols = (size + nrows - 1) // nrows |
|
748 | ncols = (size + nrows - 1) // nrows | |
749 | colwidths = [] |
|
749 | colwidths = [] | |
750 | totwidth = -len(separator) |
|
750 | totwidth = -len(separator) | |
751 | for col in range(ncols): |
|
751 | for col in range(ncols): | |
752 | # Get max column width for this column |
|
752 | # Get max column width for this column | |
753 | colwidth = 0 |
|
753 | colwidth = 0 | |
754 | for row in range(nrows): |
|
754 | for row in range(nrows): | |
755 | i = array_index(nrows, row, col) |
|
755 | i = array_index(nrows, row, col) | |
756 | if i >= size: break |
|
756 | if i >= size: break | |
757 | x = items[i] |
|
757 | x = items[i] | |
758 | colwidth = max(colwidth, len(x)) |
|
758 | colwidth = max(colwidth, len(x)) | |
759 | colwidths.append(colwidth) |
|
759 | colwidths.append(colwidth) | |
760 | totwidth += colwidth + len(separator) |
|
760 | totwidth += colwidth + len(separator) | |
761 | if totwidth > displaywidth: |
|
761 | if totwidth > displaywidth: | |
762 | break |
|
762 | break | |
763 | if totwidth <= displaywidth: |
|
763 | if totwidth <= displaywidth: | |
764 | break |
|
764 | break | |
765 |
|
765 | |||
766 | # The smallest number of rows computed and the max widths for each |
|
766 | # The smallest number of rows computed and the max widths for each | |
767 | # column has been obtained. Now we just have to format each of the rows. |
|
767 | # column has been obtained. Now we just have to format each of the rows. | |
768 | string = '' |
|
768 | string = '' | |
769 | for row in range(nrows): |
|
769 | for row in range(nrows): | |
770 | texts = [] |
|
770 | texts = [] | |
771 | for col in range(ncols): |
|
771 | for col in range(ncols): | |
772 | i = row + nrows*col |
|
772 | i = row + nrows*col | |
773 | if i >= size: |
|
773 | if i >= size: | |
774 | texts.append('') |
|
774 | texts.append('') | |
775 | else: |
|
775 | else: | |
776 | texts.append(items[i]) |
|
776 | texts.append(items[i]) | |
777 | while texts and not texts[-1]: |
|
777 | while texts and not texts[-1]: | |
778 | del texts[-1] |
|
778 | del texts[-1] | |
779 | for col in range(len(texts)): |
|
779 | for col in range(len(texts)): | |
780 | texts[col] = texts[col].ljust(colwidths[col]) |
|
780 | texts[col] = texts[col].ljust(colwidths[col]) | |
781 | string += '%s\n' % str(separator.join(texts)) |
|
781 | string += '%s\n' % str(separator.join(texts)) | |
782 | return string |
|
782 | return string | |
783 |
|
783 | |||
784 | def _get_block_plain_text(self, block): |
|
784 | def _get_block_plain_text(self, block): | |
785 | """ Given a QTextBlock, return its unformatted text. |
|
785 | """ Given a QTextBlock, return its unformatted text. | |
786 | """ |
|
786 | """ | |
787 | cursor = QtGui.QTextCursor(block) |
|
787 | cursor = QtGui.QTextCursor(block) | |
788 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) |
|
788 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) | |
789 | cursor.movePosition(QtGui.QTextCursor.EndOfBlock, |
|
789 | cursor.movePosition(QtGui.QTextCursor.EndOfBlock, | |
790 | QtGui.QTextCursor.KeepAnchor) |
|
790 | QtGui.QTextCursor.KeepAnchor) | |
791 | return str(cursor.selection().toPlainText()) |
|
791 | return str(cursor.selection().toPlainText()) | |
792 |
|
792 | |||
793 | def _get_cursor(self): |
|
793 | def _get_cursor(self): | |
794 | """ Convenience method that returns a cursor for the current position. |
|
794 | """ Convenience method that returns a cursor for the current position. | |
795 | """ |
|
795 | """ | |
796 | return self._control.textCursor() |
|
796 | return self._control.textCursor() | |
797 |
|
797 | |||
798 | def _get_end_cursor(self): |
|
798 | def _get_end_cursor(self): | |
799 | """ Convenience method that returns a cursor for the last character. |
|
799 | """ Convenience method that returns a cursor for the last character. | |
800 | """ |
|
800 | """ | |
801 | cursor = self._control.textCursor() |
|
801 | cursor = self._control.textCursor() | |
802 | cursor.movePosition(QtGui.QTextCursor.End) |
|
802 | cursor.movePosition(QtGui.QTextCursor.End) | |
803 | return cursor |
|
803 | return cursor | |
804 |
|
804 | |||
805 | def _get_input_buffer_cursor_column(self): |
|
805 | def _get_input_buffer_cursor_column(self): | |
806 | """ Returns the column of the cursor in the input buffer, excluding the |
|
806 | """ Returns the column of the cursor in the input buffer, excluding the | |
807 | contribution by the prompt, or -1 if there is no such column. |
|
807 | contribution by the prompt, or -1 if there is no such column. | |
808 | """ |
|
808 | """ | |
809 | prompt = self._get_input_buffer_cursor_prompt() |
|
809 | prompt = self._get_input_buffer_cursor_prompt() | |
810 | if prompt is None: |
|
810 | if prompt is None: | |
811 | return -1 |
|
811 | return -1 | |
812 | else: |
|
812 | else: | |
813 | cursor = self._control.textCursor() |
|
813 | cursor = self._control.textCursor() | |
814 | return cursor.columnNumber() - len(prompt) |
|
814 | return cursor.columnNumber() - len(prompt) | |
815 |
|
815 | |||
816 | def _get_input_buffer_cursor_line(self): |
|
816 | def _get_input_buffer_cursor_line(self): | |
817 | """ Returns line of the input buffer that contains the cursor, or None |
|
817 | """ Returns line of the input buffer that contains the cursor, or None | |
818 | if there is no such line. |
|
818 | if there is no such line. | |
819 | """ |
|
819 | """ | |
820 | prompt = self._get_input_buffer_cursor_prompt() |
|
820 | prompt = self._get_input_buffer_cursor_prompt() | |
821 | if prompt is None: |
|
821 | if prompt is None: | |
822 | return None |
|
822 | return None | |
823 | else: |
|
823 | else: | |
824 | cursor = self._control.textCursor() |
|
824 | cursor = self._control.textCursor() | |
825 | text = self._get_block_plain_text(cursor.block()) |
|
825 | text = self._get_block_plain_text(cursor.block()) | |
826 | return text[len(prompt):] |
|
826 | return text[len(prompt):] | |
827 |
|
827 | |||
828 | def _get_input_buffer_cursor_prompt(self): |
|
828 | def _get_input_buffer_cursor_prompt(self): | |
829 | """ Returns the (plain text) prompt for line of the input buffer that |
|
829 | """ Returns the (plain text) prompt for line of the input buffer that | |
830 | contains the cursor, or None if there is no such line. |
|
830 | contains the cursor, or None if there is no such line. | |
831 | """ |
|
831 | """ | |
832 | if self._executing: |
|
832 | if self._executing: | |
833 | return None |
|
833 | return None | |
834 | cursor = self._control.textCursor() |
|
834 | cursor = self._control.textCursor() | |
835 | if cursor.position() >= self._prompt_pos: |
|
835 | if cursor.position() >= self._prompt_pos: | |
836 | if cursor.blockNumber() == self._get_prompt_cursor().blockNumber(): |
|
836 | if cursor.blockNumber() == self._get_prompt_cursor().blockNumber(): | |
837 | return self._prompt |
|
837 | return self._prompt | |
838 | else: |
|
838 | else: | |
839 | return self._continuation_prompt |
|
839 | return self._continuation_prompt | |
840 | else: |
|
840 | else: | |
841 | return None |
|
841 | return None | |
842 |
|
842 | |||
843 | def _get_prompt_cursor(self): |
|
843 | def _get_prompt_cursor(self): | |
844 | """ Convenience method that returns a cursor for the prompt position. |
|
844 | """ Convenience method that returns a cursor for the prompt position. | |
845 | """ |
|
845 | """ | |
846 | cursor = self._control.textCursor() |
|
846 | cursor = self._control.textCursor() | |
847 | cursor.setPosition(self._prompt_pos) |
|
847 | cursor.setPosition(self._prompt_pos) | |
848 | return cursor |
|
848 | return cursor | |
849 |
|
849 | |||
850 | def _get_selection_cursor(self, start, end): |
|
850 | def _get_selection_cursor(self, start, end): | |
851 | """ Convenience method that returns a cursor with text selected between |
|
851 | """ Convenience method that returns a cursor with text selected between | |
852 | the positions 'start' and 'end'. |
|
852 | the positions 'start' and 'end'. | |
853 | """ |
|
853 | """ | |
854 | cursor = self._control.textCursor() |
|
854 | cursor = self._control.textCursor() | |
855 | cursor.setPosition(start) |
|
855 | cursor.setPosition(start) | |
856 | cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor) |
|
856 | cursor.setPosition(end, QtGui.QTextCursor.KeepAnchor) | |
857 | return cursor |
|
857 | return cursor | |
858 |
|
858 | |||
859 | def _get_word_start_cursor(self, position): |
|
859 | def _get_word_start_cursor(self, position): | |
860 | """ Find the start of the word to the left the given position. If a |
|
860 | """ Find the start of the word to the left the given position. If a | |
861 | sequence of non-word characters precedes the first word, skip over |
|
861 | sequence of non-word characters precedes the first word, skip over | |
862 | them. (This emulates the behavior of bash, emacs, etc.) |
|
862 | them. (This emulates the behavior of bash, emacs, etc.) | |
863 | """ |
|
863 | """ | |
864 | document = self._control.document() |
|
864 | document = self._control.document() | |
865 | position -= 1 |
|
865 | position -= 1 | |
866 | while position >= self._prompt_pos and \ |
|
866 | while position >= self._prompt_pos and \ | |
867 | not document.characterAt(position).isLetterOrNumber(): |
|
867 | not document.characterAt(position).isLetterOrNumber(): | |
868 | position -= 1 |
|
868 | position -= 1 | |
869 | while position >= self._prompt_pos and \ |
|
869 | while position >= self._prompt_pos and \ | |
870 | document.characterAt(position).isLetterOrNumber(): |
|
870 | document.characterAt(position).isLetterOrNumber(): | |
871 | position -= 1 |
|
871 | position -= 1 | |
872 | cursor = self._control.textCursor() |
|
872 | cursor = self._control.textCursor() | |
873 | cursor.setPosition(position + 1) |
|
873 | cursor.setPosition(position + 1) | |
874 | return cursor |
|
874 | return cursor | |
875 |
|
875 | |||
876 | def _get_word_end_cursor(self, position): |
|
876 | def _get_word_end_cursor(self, position): | |
877 | """ Find the end of the word to the right the given position. If a |
|
877 | """ Find the end of the word to the right the given position. If a | |
878 | sequence of non-word characters precedes the first word, skip over |
|
878 | sequence of non-word characters precedes the first word, skip over | |
879 | them. (This emulates the behavior of bash, emacs, etc.) |
|
879 | them. (This emulates the behavior of bash, emacs, etc.) | |
880 | """ |
|
880 | """ | |
881 | document = self._control.document() |
|
881 | document = self._control.document() | |
882 | end = self._get_end_cursor().position() |
|
882 | end = self._get_end_cursor().position() | |
883 | while position < end and \ |
|
883 | while position < end and \ | |
884 | not document.characterAt(position).isLetterOrNumber(): |
|
884 | not document.characterAt(position).isLetterOrNumber(): | |
885 | position += 1 |
|
885 | position += 1 | |
886 | while position < end and \ |
|
886 | while position < end and \ | |
887 | document.characterAt(position).isLetterOrNumber(): |
|
887 | document.characterAt(position).isLetterOrNumber(): | |
888 | position += 1 |
|
888 | position += 1 | |
889 | cursor = self._control.textCursor() |
|
889 | cursor = self._control.textCursor() | |
890 | cursor.setPosition(position) |
|
890 | cursor.setPosition(position) | |
891 | return cursor |
|
891 | return cursor | |
892 |
|
892 | |||
893 | def _insert_html(self, cursor, html): |
|
893 | def _insert_html(self, cursor, html): | |
894 | """ Inserts HTML using the specified cursor in such a way that future |
|
894 | """ Inserts HTML using the specified cursor in such a way that future | |
895 | formatting is unaffected. |
|
895 | formatting is unaffected. | |
896 | """ |
|
896 | """ | |
897 | cursor.beginEditBlock() |
|
897 | cursor.beginEditBlock() | |
898 | cursor.insertHtml(html) |
|
898 | cursor.insertHtml(html) | |
899 |
|
899 | |||
900 | # After inserting HTML, the text document "remembers" it's in "html |
|
900 | # After inserting HTML, the text document "remembers" it's in "html | |
901 | # mode", which means that subsequent calls adding plain text will result |
|
901 | # mode", which means that subsequent calls adding plain text will result | |
902 | # in unwanted formatting, lost tab characters, etc. The following code |
|
902 | # in unwanted formatting, lost tab characters, etc. The following code | |
903 | # hacks around this behavior, which I consider to be a bug in Qt, by |
|
903 | # hacks around this behavior, which I consider to be a bug in Qt, by | |
904 | # (crudely) resetting the document's style state. |
|
904 | # (crudely) resetting the document's style state. | |
905 | cursor.movePosition(QtGui.QTextCursor.Left, |
|
905 | cursor.movePosition(QtGui.QTextCursor.Left, | |
906 | QtGui.QTextCursor.KeepAnchor) |
|
906 | QtGui.QTextCursor.KeepAnchor) | |
907 | if cursor.selection().toPlainText() == ' ': |
|
907 | if cursor.selection().toPlainText() == ' ': | |
908 | cursor.removeSelectedText() |
|
908 | cursor.removeSelectedText() | |
909 | else: |
|
909 | else: | |
910 | cursor.movePosition(QtGui.QTextCursor.Right) |
|
910 | cursor.movePosition(QtGui.QTextCursor.Right) | |
911 | cursor.insertText(' ', QtGui.QTextCharFormat()) |
|
911 | cursor.insertText(' ', QtGui.QTextCharFormat()) | |
912 | cursor.endEditBlock() |
|
912 | cursor.endEditBlock() | |
913 |
|
913 | |||
914 | def _insert_html_fetching_plain_text(self, cursor, html): |
|
914 | def _insert_html_fetching_plain_text(self, cursor, html): | |
915 | """ Inserts HTML using the specified cursor, then returns its plain text |
|
915 | """ Inserts HTML using the specified cursor, then returns its plain text | |
916 | version. |
|
916 | version. | |
917 | """ |
|
917 | """ | |
918 | cursor.beginEditBlock() |
|
918 | cursor.beginEditBlock() | |
919 | cursor.removeSelectedText() |
|
919 | cursor.removeSelectedText() | |
920 |
|
920 | |||
921 | start = cursor.position() |
|
921 | start = cursor.position() | |
922 | self._insert_html(cursor, html) |
|
922 | self._insert_html(cursor, html) | |
923 | end = cursor.position() |
|
923 | end = cursor.position() | |
924 | cursor.setPosition(start, QtGui.QTextCursor.KeepAnchor) |
|
924 | cursor.setPosition(start, QtGui.QTextCursor.KeepAnchor) | |
925 | text = str(cursor.selection().toPlainText()) |
|
925 | text = str(cursor.selection().toPlainText()) | |
926 |
|
926 | |||
927 | cursor.setPosition(end) |
|
927 | cursor.setPosition(end) | |
928 | cursor.endEditBlock() |
|
928 | cursor.endEditBlock() | |
929 | return text |
|
929 | return text | |
930 |
|
930 | |||
931 | def _insert_plain_text(self, cursor, text): |
|
931 | def _insert_plain_text(self, cursor, text): | |
932 | """ Inserts plain text using the specified cursor, processing ANSI codes |
|
932 | """ Inserts plain text using the specified cursor, processing ANSI codes | |
933 | if enabled. |
|
933 | if enabled. | |
934 | """ |
|
934 | """ | |
935 | cursor.beginEditBlock() |
|
935 | cursor.beginEditBlock() | |
936 | if self.ansi_codes: |
|
936 | if self.ansi_codes: | |
937 | for substring in self._ansi_processor.split_string(text): |
|
937 | for substring in self._ansi_processor.split_string(text): | |
938 | for action in self._ansi_processor.actions: |
|
938 | for action in self._ansi_processor.actions: | |
939 | if action.kind == 'erase' and action.area == 'screen': |
|
939 | if action.kind == 'erase' and action.area == 'screen': | |
940 | cursor.select(QtGui.QTextCursor.Document) |
|
940 | cursor.select(QtGui.QTextCursor.Document) | |
941 | cursor.removeSelectedText() |
|
941 | cursor.removeSelectedText() | |
942 | format = self._ansi_processor.get_format() |
|
942 | format = self._ansi_processor.get_format() | |
943 | cursor.insertText(substring, format) |
|
943 | cursor.insertText(substring, format) | |
944 | else: |
|
944 | else: | |
945 | cursor.insertText(text) |
|
945 | cursor.insertText(text) | |
946 | cursor.endEditBlock() |
|
946 | cursor.endEditBlock() | |
947 |
|
947 | |||
948 | def _insert_plain_text_into_buffer(self, text): |
|
948 | def _insert_plain_text_into_buffer(self, text): | |
949 | """ Inserts text into the input buffer at the current cursor position, |
|
949 | """ Inserts text into the input buffer at the current cursor position, | |
950 | ensuring that continuation prompts are inserted as necessary. |
|
950 | ensuring that continuation prompts are inserted as necessary. | |
951 | """ |
|
951 | """ | |
952 | lines = str(text).splitlines(True) |
|
952 | lines = str(text).splitlines(True) | |
953 | if lines: |
|
953 | if lines: | |
954 | self._keep_cursor_in_buffer() |
|
954 | self._keep_cursor_in_buffer() | |
955 | cursor = self._control.textCursor() |
|
955 | cursor = self._control.textCursor() | |
956 | cursor.beginEditBlock() |
|
956 | cursor.beginEditBlock() | |
957 | cursor.insertText(lines[0]) |
|
957 | cursor.insertText(lines[0]) | |
958 | for line in lines[1:]: |
|
958 | for line in lines[1:]: | |
959 | if self._continuation_prompt_html is None: |
|
959 | if self._continuation_prompt_html is None: | |
960 | cursor.insertText(self._continuation_prompt) |
|
960 | cursor.insertText(self._continuation_prompt) | |
961 | else: |
|
961 | else: | |
962 | self._continuation_prompt = \ |
|
962 | self._continuation_prompt = \ | |
963 | self._insert_html_fetching_plain_text( |
|
963 | self._insert_html_fetching_plain_text( | |
964 | cursor, self._continuation_prompt_html) |
|
964 | cursor, self._continuation_prompt_html) | |
965 | cursor.insertText(line) |
|
965 | cursor.insertText(line) | |
966 | cursor.endEditBlock() |
|
966 | cursor.endEditBlock() | |
967 | self._control.setTextCursor(cursor) |
|
967 | self._control.setTextCursor(cursor) | |
968 |
|
968 | |||
969 | def _in_buffer(self, position): |
|
969 | def _in_buffer(self, position): | |
970 | """ Returns whether the given position is inside the editing region. |
|
970 | """ Returns whether the given position is inside the editing region. | |
971 | """ |
|
971 | """ | |
972 | cursor = self._control.textCursor() |
|
972 | cursor = self._control.textCursor() | |
973 | cursor.setPosition(position) |
|
973 | cursor.setPosition(position) | |
974 | line = cursor.blockNumber() |
|
974 | line = cursor.blockNumber() | |
975 | prompt_line = self._get_prompt_cursor().blockNumber() |
|
975 | prompt_line = self._get_prompt_cursor().blockNumber() | |
976 | if line == prompt_line: |
|
976 | if line == prompt_line: | |
977 | return position >= self._prompt_pos |
|
977 | return position >= self._prompt_pos | |
978 | elif line > prompt_line: |
|
978 | elif line > prompt_line: | |
979 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) |
|
979 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock) | |
980 | prompt_pos = cursor.position() + len(self._continuation_prompt) |
|
980 | prompt_pos = cursor.position() + len(self._continuation_prompt) | |
981 | return position >= prompt_pos |
|
981 | return position >= prompt_pos | |
982 | return False |
|
982 | return False | |
983 |
|
983 | |||
984 | def _keep_cursor_in_buffer(self): |
|
984 | def _keep_cursor_in_buffer(self): | |
985 | """ Ensures that the cursor is inside the editing region. Returns |
|
985 | """ Ensures that the cursor is inside the editing region. Returns | |
986 | whether the cursor was moved. |
|
986 | whether the cursor was moved. | |
987 | """ |
|
987 | """ | |
988 | cursor = self._control.textCursor() |
|
988 | cursor = self._control.textCursor() | |
989 | if self._in_buffer(cursor.position()): |
|
989 | if self._in_buffer(cursor.position()): | |
990 | return False |
|
990 | return False | |
991 | else: |
|
991 | else: | |
992 | cursor.movePosition(QtGui.QTextCursor.End) |
|
992 | cursor.movePosition(QtGui.QTextCursor.End) | |
993 | self._control.setTextCursor(cursor) |
|
993 | self._control.setTextCursor(cursor) | |
994 | return True |
|
994 | return True | |
995 |
|
995 | |||
996 | def _page(self, text): |
|
996 | def _page(self, text): | |
997 | """ Displays text using the pager. |
|
997 | """ Displays text using the pager. | |
998 | """ |
|
998 | """ | |
999 | if self._page_style == 'custom': |
|
999 | if self._page_style == 'custom': | |
1000 | self.custom_page_requested.emit(text) |
|
1000 | self.custom_page_requested.emit(text) | |
1001 | elif self._page_style == 'none': |
|
1001 | elif self._page_style == 'none': | |
1002 | self._append_plain_text(text) |
|
1002 | self._append_plain_text(text) | |
1003 | else: |
|
1003 | else: | |
1004 | self._page_control.clear() |
|
1004 | self._page_control.clear() | |
1005 | cursor = self._page_control.textCursor() |
|
1005 | cursor = self._page_control.textCursor() | |
1006 | self._insert_plain_text(cursor, text) |
|
1006 | self._insert_plain_text(cursor, text) | |
1007 | self._page_control.moveCursor(QtGui.QTextCursor.Start) |
|
1007 | self._page_control.moveCursor(QtGui.QTextCursor.Start) | |
1008 |
|
1008 | |||
1009 | self._page_control.viewport().resize(self._control.size()) |
|
1009 | self._page_control.viewport().resize(self._control.size()) | |
1010 | if self._splitter: |
|
1010 | if self._splitter: | |
1011 | self._page_control.show() |
|
1011 | self._page_control.show() | |
1012 | self._page_control.setFocus() |
|
1012 | self._page_control.setFocus() | |
1013 | else: |
|
1013 | else: | |
1014 | self.layout().setCurrentWidget(self._page_control) |
|
1014 | self.layout().setCurrentWidget(self._page_control) | |
1015 |
|
1015 | |||
1016 | def _prompt_started(self): |
|
1016 | def _prompt_started(self): | |
1017 | """ Called immediately after a new prompt is displayed. |
|
1017 | """ Called immediately after a new prompt is displayed. | |
1018 | """ |
|
1018 | """ | |
1019 | # Temporarily disable the maximum block count to permit undo/redo and |
|
1019 | # Temporarily disable the maximum block count to permit undo/redo and | |
1020 | # to ensure that the prompt position does not change due to truncation. |
|
1020 | # to ensure that the prompt position does not change due to truncation. | |
1021 | self._control.document().setMaximumBlockCount(0) |
|
1021 | self._control.document().setMaximumBlockCount(0) | |
1022 | self._control.setUndoRedoEnabled(True) |
|
1022 | self._control.setUndoRedoEnabled(True) | |
1023 |
|
1023 | |||
1024 | self._control.setReadOnly(False) |
|
1024 | self._control.setReadOnly(False) | |
1025 | self._control.moveCursor(QtGui.QTextCursor.End) |
|
1025 | self._control.moveCursor(QtGui.QTextCursor.End) | |
1026 |
|
1026 | |||
1027 | self._executing = False |
|
1027 | self._executing = False | |
1028 | self._prompt_started_hook() |
|
1028 | self._prompt_started_hook() | |
1029 |
|
1029 | |||
1030 | def _prompt_finished(self): |
|
1030 | def _prompt_finished(self): | |
1031 | """ Called immediately after a prompt is finished, i.e. when some input |
|
1031 | """ Called immediately after a prompt is finished, i.e. when some input | |
1032 | will be processed and a new prompt displayed. |
|
1032 | will be processed and a new prompt displayed. | |
1033 | """ |
|
1033 | """ | |
1034 | self._control.setUndoRedoEnabled(False) |
|
1034 | self._control.setUndoRedoEnabled(False) | |
1035 | self._control.setReadOnly(True) |
|
1035 | self._control.setReadOnly(True) | |
1036 | self._prompt_finished_hook() |
|
1036 | self._prompt_finished_hook() | |
1037 |
|
1037 | |||
1038 | def _readline(self, prompt='', callback=None): |
|
1038 | def _readline(self, prompt='', callback=None): | |
1039 | """ Reads one line of input from the user. |
|
1039 | """ Reads one line of input from the user. | |
1040 |
|
1040 | |||
1041 | Parameters |
|
1041 | Parameters | |
1042 | ---------- |
|
1042 | ---------- | |
1043 | prompt : str, optional |
|
1043 | prompt : str, optional | |
1044 | The prompt to print before reading the line. |
|
1044 | The prompt to print before reading the line. | |
1045 |
|
1045 | |||
1046 | callback : callable, optional |
|
1046 | callback : callable, optional | |
1047 | A callback to execute with the read line. If not specified, input is |
|
1047 | A callback to execute with the read line. If not specified, input is | |
1048 | read *synchronously* and this method does not return until it has |
|
1048 | read *synchronously* and this method does not return until it has | |
1049 | been read. |
|
1049 | been read. | |
1050 |
|
1050 | |||
1051 | Returns |
|
1051 | Returns | |
1052 | ------- |
|
1052 | ------- | |
1053 | If a callback is specified, returns nothing. Otherwise, returns the |
|
1053 | If a callback is specified, returns nothing. Otherwise, returns the | |
1054 | input string with the trailing newline stripped. |
|
1054 | input string with the trailing newline stripped. | |
1055 | """ |
|
1055 | """ | |
1056 | if self._reading: |
|
1056 | if self._reading: | |
1057 | raise RuntimeError('Cannot read a line. Widget is already reading.') |
|
1057 | raise RuntimeError('Cannot read a line. Widget is already reading.') | |
1058 |
|
1058 | |||
1059 | if not callback and not self.isVisible(): |
|
1059 | if not callback and not self.isVisible(): | |
1060 | # If the user cannot see the widget, this function cannot return. |
|
1060 | # If the user cannot see the widget, this function cannot return. | |
1061 | raise RuntimeError('Cannot synchronously read a line if the widget' |
|
1061 | raise RuntimeError('Cannot synchronously read a line if the widget' | |
1062 | 'is not visible!') |
|
1062 | 'is not visible!') | |
1063 |
|
1063 | |||
1064 | self._reading = True |
|
1064 | self._reading = True | |
1065 | self._show_prompt(prompt, newline=False) |
|
1065 | self._show_prompt(prompt, newline=False) | |
1066 |
|
1066 | |||
1067 | if callback is None: |
|
1067 | if callback is None: | |
1068 | self._reading_callback = None |
|
1068 | self._reading_callback = None | |
1069 | while self._reading: |
|
1069 | while self._reading: | |
1070 | QtCore.QCoreApplication.processEvents() |
|
1070 | QtCore.QCoreApplication.processEvents() | |
1071 | return self.input_buffer.rstrip('\n') |
|
1071 | return self.input_buffer.rstrip('\n') | |
1072 |
|
1072 | |||
1073 | else: |
|
1073 | else: | |
1074 | self._reading_callback = lambda: \ |
|
1074 | self._reading_callback = lambda: \ | |
1075 | callback(self.input_buffer.rstrip('\n')) |
|
1075 | callback(self.input_buffer.rstrip('\n')) | |
1076 |
|
1076 | |||
1077 | def _reset(self): |
|
|||
1078 | """ Clears the console and resets internal state variables. |
|
|||
1079 | """ |
|
|||
1080 | self._control.clear() |
|
|||
1081 | self._executing = self._reading = False |
|
|||
1082 |
|
||||
1083 | def _set_continuation_prompt(self, prompt, html=False): |
|
1077 | def _set_continuation_prompt(self, prompt, html=False): | |
1084 | """ Sets the continuation prompt. |
|
1078 | """ Sets the continuation prompt. | |
1085 |
|
1079 | |||
1086 | Parameters |
|
1080 | Parameters | |
1087 | ---------- |
|
1081 | ---------- | |
1088 | prompt : str |
|
1082 | prompt : str | |
1089 | The prompt to show when more input is needed. |
|
1083 | The prompt to show when more input is needed. | |
1090 |
|
1084 | |||
1091 | html : bool, optional (default False) |
|
1085 | html : bool, optional (default False) | |
1092 | If set, the prompt will be inserted as formatted HTML. Otherwise, |
|
1086 | If set, the prompt will be inserted as formatted HTML. Otherwise, | |
1093 | the prompt will be treated as plain text, though ANSI color codes |
|
1087 | the prompt will be treated as plain text, though ANSI color codes | |
1094 | will be handled. |
|
1088 | will be handled. | |
1095 | """ |
|
1089 | """ | |
1096 | if html: |
|
1090 | if html: | |
1097 | self._continuation_prompt_html = prompt |
|
1091 | self._continuation_prompt_html = prompt | |
1098 | else: |
|
1092 | else: | |
1099 | self._continuation_prompt = prompt |
|
1093 | self._continuation_prompt = prompt | |
1100 | self._continuation_prompt_html = None |
|
1094 | self._continuation_prompt_html = None | |
1101 |
|
1095 | |||
1102 | def _set_cursor(self, cursor): |
|
1096 | def _set_cursor(self, cursor): | |
1103 | """ Convenience method to set the current cursor. |
|
1097 | """ Convenience method to set the current cursor. | |
1104 | """ |
|
1098 | """ | |
1105 | self._control.setTextCursor(cursor) |
|
1099 | self._control.setTextCursor(cursor) | |
1106 |
|
1100 | |||
1107 | def _set_position(self, position): |
|
1101 | def _set_position(self, position): | |
1108 | """ Convenience method to set the position of the cursor. |
|
1102 | """ Convenience method to set the position of the cursor. | |
1109 | """ |
|
1103 | """ | |
1110 | cursor = self._control.textCursor() |
|
1104 | cursor = self._control.textCursor() | |
1111 | cursor.setPosition(position) |
|
1105 | cursor.setPosition(position) | |
1112 | self._control.setTextCursor(cursor) |
|
1106 | self._control.setTextCursor(cursor) | |
1113 |
|
1107 | |||
1114 | def _set_selection(self, start, end): |
|
1108 | def _set_selection(self, start, end): | |
1115 | """ Convenience method to set the current selected text. |
|
1109 | """ Convenience method to set the current selected text. | |
1116 | """ |
|
1110 | """ | |
1117 | self._control.setTextCursor(self._get_selection_cursor(start, end)) |
|
1111 | self._control.setTextCursor(self._get_selection_cursor(start, end)) | |
1118 |
|
1112 | |||
1119 | def _show_context_menu(self, pos): |
|
1113 | def _show_context_menu(self, pos): | |
1120 | """ Shows a context menu at the given QPoint (in widget coordinates). |
|
1114 | """ Shows a context menu at the given QPoint (in widget coordinates). | |
1121 | """ |
|
1115 | """ | |
1122 | menu = QtGui.QMenu() |
|
1116 | menu = QtGui.QMenu() | |
1123 |
|
1117 | |||
1124 | copy_action = menu.addAction('Copy', self.copy) |
|
1118 | copy_action = menu.addAction('Copy', self.copy) | |
1125 | copy_action.setEnabled(self._get_cursor().hasSelection()) |
|
1119 | copy_action.setEnabled(self._get_cursor().hasSelection()) | |
1126 | copy_action.setShortcut(QtGui.QKeySequence.Copy) |
|
1120 | copy_action.setShortcut(QtGui.QKeySequence.Copy) | |
1127 |
|
1121 | |||
1128 | paste_action = menu.addAction('Paste', self.paste) |
|
1122 | paste_action = menu.addAction('Paste', self.paste) | |
1129 | paste_action.setEnabled(self.can_paste()) |
|
1123 | paste_action.setEnabled(self.can_paste()) | |
1130 | paste_action.setShortcut(QtGui.QKeySequence.Paste) |
|
1124 | paste_action.setShortcut(QtGui.QKeySequence.Paste) | |
1131 |
|
1125 | |||
1132 | menu.addSeparator() |
|
1126 | menu.addSeparator() | |
1133 | menu.addAction('Select All', self.select_all) |
|
1127 | menu.addAction('Select All', self.select_all) | |
1134 |
|
1128 | |||
1135 | menu.exec_(self._control.mapToGlobal(pos)) |
|
1129 | menu.exec_(self._control.mapToGlobal(pos)) | |
1136 |
|
1130 | |||
1137 | def _show_prompt(self, prompt=None, html=False, newline=True): |
|
1131 | def _show_prompt(self, prompt=None, html=False, newline=True): | |
1138 | """ Writes a new prompt at the end of the buffer. |
|
1132 | """ Writes a new prompt at the end of the buffer. | |
1139 |
|
1133 | |||
1140 | Parameters |
|
1134 | Parameters | |
1141 | ---------- |
|
1135 | ---------- | |
1142 | prompt : str, optional |
|
1136 | prompt : str, optional | |
1143 | The prompt to show. If not specified, the previous prompt is used. |
|
1137 | The prompt to show. If not specified, the previous prompt is used. | |
1144 |
|
1138 | |||
1145 | html : bool, optional (default False) |
|
1139 | html : bool, optional (default False) | |
1146 | Only relevant when a prompt is specified. If set, the prompt will |
|
1140 | Only relevant when a prompt is specified. If set, the prompt will | |
1147 | be inserted as formatted HTML. Otherwise, the prompt will be treated |
|
1141 | be inserted as formatted HTML. Otherwise, the prompt will be treated | |
1148 | as plain text, though ANSI color codes will be handled. |
|
1142 | as plain text, though ANSI color codes will be handled. | |
1149 |
|
1143 | |||
1150 | newline : bool, optional (default True) |
|
1144 | newline : bool, optional (default True) | |
1151 | If set, a new line will be written before showing the prompt if |
|
1145 | If set, a new line will be written before showing the prompt if | |
1152 | there is not already a newline at the end of the buffer. |
|
1146 | there is not already a newline at the end of the buffer. | |
1153 | """ |
|
1147 | """ | |
1154 | # Insert a preliminary newline, if necessary. |
|
1148 | # Insert a preliminary newline, if necessary. | |
1155 | if newline: |
|
1149 | if newline: | |
1156 | cursor = self._get_end_cursor() |
|
1150 | cursor = self._get_end_cursor() | |
1157 | if cursor.position() > 0: |
|
1151 | if cursor.position() > 0: | |
1158 | cursor.movePosition(QtGui.QTextCursor.Left, |
|
1152 | cursor.movePosition(QtGui.QTextCursor.Left, | |
1159 | QtGui.QTextCursor.KeepAnchor) |
|
1153 | QtGui.QTextCursor.KeepAnchor) | |
1160 | if str(cursor.selection().toPlainText()) != '\n': |
|
1154 | if str(cursor.selection().toPlainText()) != '\n': | |
1161 | self._append_plain_text('\n') |
|
1155 | self._append_plain_text('\n') | |
1162 |
|
1156 | |||
1163 | # Write the prompt. |
|
1157 | # Write the prompt. | |
1164 | if prompt is None: |
|
1158 | if prompt is None: | |
1165 | if self._prompt_html is None: |
|
1159 | if self._prompt_html is None: | |
1166 | self._append_plain_text(self._prompt) |
|
1160 | self._append_plain_text(self._prompt) | |
1167 | else: |
|
1161 | else: | |
1168 | self._append_html(self._prompt_html) |
|
1162 | self._append_html(self._prompt_html) | |
1169 | else: |
|
1163 | else: | |
1170 | if html: |
|
1164 | if html: | |
1171 | self._prompt = self._append_html_fetching_plain_text(prompt) |
|
1165 | self._prompt = self._append_html_fetching_plain_text(prompt) | |
1172 | self._prompt_html = prompt |
|
1166 | self._prompt_html = prompt | |
1173 | else: |
|
1167 | else: | |
1174 | self._append_plain_text(prompt) |
|
1168 | self._append_plain_text(prompt) | |
1175 | self._prompt = prompt |
|
1169 | self._prompt = prompt | |
1176 | self._prompt_html = None |
|
1170 | self._prompt_html = None | |
1177 |
|
1171 | |||
1178 | self._prompt_pos = self._get_end_cursor().position() |
|
1172 | self._prompt_pos = self._get_end_cursor().position() | |
1179 | self._prompt_started() |
|
1173 | self._prompt_started() | |
1180 |
|
1174 | |||
1181 | def _show_continuation_prompt(self): |
|
1175 | def _show_continuation_prompt(self): | |
1182 | """ Writes a new continuation prompt at the end of the buffer. |
|
1176 | """ Writes a new continuation prompt at the end of the buffer. | |
1183 | """ |
|
1177 | """ | |
1184 | if self._continuation_prompt_html is None: |
|
1178 | if self._continuation_prompt_html is None: | |
1185 | self._append_plain_text(self._continuation_prompt) |
|
1179 | self._append_plain_text(self._continuation_prompt) | |
1186 | else: |
|
1180 | else: | |
1187 | self._continuation_prompt = self._append_html_fetching_plain_text( |
|
1181 | self._continuation_prompt = self._append_html_fetching_plain_text( | |
1188 | self._continuation_prompt_html) |
|
1182 | self._continuation_prompt_html) | |
1189 |
|
1183 | |||
1190 | self._prompt_started() |
|
1184 | self._prompt_started() | |
1191 |
|
1185 | |||
1192 |
|
1186 | |||
1193 | class HistoryConsoleWidget(ConsoleWidget): |
|
1187 | class HistoryConsoleWidget(ConsoleWidget): | |
1194 | """ A ConsoleWidget that keeps a history of the commands that have been |
|
1188 | """ A ConsoleWidget that keeps a history of the commands that have been | |
1195 | executed. |
|
1189 | executed. | |
1196 | """ |
|
1190 | """ | |
1197 |
|
1191 | |||
1198 | #--------------------------------------------------------------------------- |
|
1192 | #--------------------------------------------------------------------------- | |
1199 | # 'object' interface |
|
1193 | # 'object' interface | |
1200 | #--------------------------------------------------------------------------- |
|
1194 | #--------------------------------------------------------------------------- | |
1201 |
|
1195 | |||
1202 | def __init__(self, *args, **kw): |
|
1196 | def __init__(self, *args, **kw): | |
1203 | super(HistoryConsoleWidget, self).__init__(*args, **kw) |
|
1197 | super(HistoryConsoleWidget, self).__init__(*args, **kw) | |
1204 | self._history = [] |
|
1198 | self._history = [] | |
1205 | self._history_index = 0 |
|
1199 | self._history_index = 0 | |
1206 |
|
1200 | |||
1207 | #--------------------------------------------------------------------------- |
|
1201 | #--------------------------------------------------------------------------- | |
1208 | # 'ConsoleWidget' public interface |
|
1202 | # 'ConsoleWidget' public interface | |
1209 | #--------------------------------------------------------------------------- |
|
1203 | #--------------------------------------------------------------------------- | |
1210 |
|
1204 | |||
1211 | def execute(self, source=None, hidden=False, interactive=False): |
|
1205 | def execute(self, source=None, hidden=False, interactive=False): | |
1212 | """ Reimplemented to the store history. |
|
1206 | """ Reimplemented to the store history. | |
1213 | """ |
|
1207 | """ | |
1214 | if not hidden: |
|
1208 | if not hidden: | |
1215 | history = self.input_buffer if source is None else source |
|
1209 | history = self.input_buffer if source is None else source | |
1216 |
|
1210 | |||
1217 | executed = super(HistoryConsoleWidget, self).execute( |
|
1211 | executed = super(HistoryConsoleWidget, self).execute( | |
1218 | source, hidden, interactive) |
|
1212 | source, hidden, interactive) | |
1219 |
|
1213 | |||
1220 | if executed and not hidden: |
|
1214 | if executed and not hidden: | |
1221 | self._history.append(history.rstrip()) |
|
1215 | self._history.append(history.rstrip()) | |
1222 | self._history_index = len(self._history) |
|
1216 | self._history_index = len(self._history) | |
1223 |
|
1217 | |||
1224 | return executed |
|
1218 | return executed | |
1225 |
|
1219 | |||
1226 | #--------------------------------------------------------------------------- |
|
1220 | #--------------------------------------------------------------------------- | |
1227 | # 'ConsoleWidget' abstract interface |
|
1221 | # 'ConsoleWidget' abstract interface | |
1228 | #--------------------------------------------------------------------------- |
|
1222 | #--------------------------------------------------------------------------- | |
1229 |
|
1223 | |||
1230 | def _up_pressed(self): |
|
1224 | def _up_pressed(self): | |
1231 | """ Called when the up key is pressed. Returns whether to continue |
|
1225 | """ Called when the up key is pressed. Returns whether to continue | |
1232 | processing the event. |
|
1226 | processing the event. | |
1233 | """ |
|
1227 | """ | |
1234 | prompt_cursor = self._get_prompt_cursor() |
|
1228 | prompt_cursor = self._get_prompt_cursor() | |
1235 | if self._get_cursor().blockNumber() == prompt_cursor.blockNumber(): |
|
1229 | if self._get_cursor().blockNumber() == prompt_cursor.blockNumber(): | |
1236 | self.history_previous() |
|
1230 | self.history_previous() | |
1237 |
|
1231 | |||
1238 | # Go to the first line of prompt for seemless history scrolling. |
|
1232 | # Go to the first line of prompt for seemless history scrolling. | |
1239 | cursor = self._get_prompt_cursor() |
|
1233 | cursor = self._get_prompt_cursor() | |
1240 | cursor.movePosition(QtGui.QTextCursor.EndOfLine) |
|
1234 | cursor.movePosition(QtGui.QTextCursor.EndOfLine) | |
1241 | self._set_cursor(cursor) |
|
1235 | self._set_cursor(cursor) | |
1242 |
|
1236 | |||
1243 | return False |
|
1237 | return False | |
1244 | return True |
|
1238 | return True | |
1245 |
|
1239 | |||
1246 | def _down_pressed(self): |
|
1240 | def _down_pressed(self): | |
1247 | """ Called when the down key is pressed. Returns whether to continue |
|
1241 | """ Called when the down key is pressed. Returns whether to continue | |
1248 | processing the event. |
|
1242 | processing the event. | |
1249 | """ |
|
1243 | """ | |
1250 | end_cursor = self._get_end_cursor() |
|
1244 | end_cursor = self._get_end_cursor() | |
1251 | if self._get_cursor().blockNumber() == end_cursor.blockNumber(): |
|
1245 | if self._get_cursor().blockNumber() == end_cursor.blockNumber(): | |
1252 | self.history_next() |
|
1246 | self.history_next() | |
1253 | return False |
|
1247 | return False | |
1254 | return True |
|
1248 | return True | |
1255 |
|
1249 | |||
1256 | #--------------------------------------------------------------------------- |
|
1250 | #--------------------------------------------------------------------------- | |
1257 | # 'HistoryConsoleWidget' interface |
|
1251 | # 'HistoryConsoleWidget' interface | |
1258 | #--------------------------------------------------------------------------- |
|
1252 | #--------------------------------------------------------------------------- | |
1259 |
|
1253 | |||
1260 | def history_previous(self): |
|
1254 | def history_previous(self): | |
1261 | """ If possible, set the input buffer to the previous item in the |
|
1255 | """ If possible, set the input buffer to the previous item in the | |
1262 | history. |
|
1256 | history. | |
1263 | """ |
|
1257 | """ | |
1264 | if self._history_index > 0: |
|
1258 | if self._history_index > 0: | |
1265 | self._history_index -= 1 |
|
1259 | self._history_index -= 1 | |
1266 | self.input_buffer = self._history[self._history_index] |
|
1260 | self.input_buffer = self._history[self._history_index] | |
1267 |
|
1261 | |||
1268 | def history_next(self): |
|
1262 | def history_next(self): | |
1269 | """ Set the input buffer to the next item in the history, or a blank |
|
1263 | """ Set the input buffer to the next item in the history, or a blank | |
1270 | line if there is no subsequent item. |
|
1264 | line if there is no subsequent item. | |
1271 | """ |
|
1265 | """ | |
1272 | if self._history_index < len(self._history): |
|
1266 | if self._history_index < len(self._history): | |
1273 | self._history_index += 1 |
|
1267 | self._history_index += 1 | |
1274 | if self._history_index < len(self._history): |
|
1268 | if self._history_index < len(self._history): | |
1275 | self.input_buffer = self._history[self._history_index] |
|
1269 | self.input_buffer = self._history[self._history_index] | |
1276 | else: |
|
1270 | else: | |
1277 | self.input_buffer = '' |
|
1271 | self.input_buffer = '' |
@@ -1,379 +1,379 b'' | |||||
1 | # Standard library imports |
|
1 | # Standard library imports | |
2 | import signal |
|
2 | import signal | |
3 | import sys |
|
3 | import sys | |
4 |
|
4 | |||
5 | # System library imports |
|
5 | # System library imports | |
6 | from pygments.lexers import PythonLexer |
|
6 | from pygments.lexers import PythonLexer | |
7 | from PyQt4 import QtCore, QtGui |
|
7 | from PyQt4 import QtCore, QtGui | |
8 | import zmq |
|
8 | import zmq | |
9 |
|
9 | |||
10 | # Local imports |
|
10 | # Local imports | |
11 | from IPython.core.inputsplitter import InputSplitter |
|
11 | from IPython.core.inputsplitter import InputSplitter | |
12 | from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin |
|
12 | from IPython.frontend.qt.base_frontend_mixin import BaseFrontendMixin | |
13 | from call_tip_widget import CallTipWidget |
|
13 | from call_tip_widget import CallTipWidget | |
14 | from completion_lexer import CompletionLexer |
|
14 | from completion_lexer import CompletionLexer | |
15 | from console_widget import HistoryConsoleWidget |
|
15 | from console_widget import HistoryConsoleWidget | |
16 | from pygments_highlighter import PygmentsHighlighter |
|
16 | from pygments_highlighter import PygmentsHighlighter | |
17 |
|
17 | |||
18 |
|
18 | |||
19 | class FrontendHighlighter(PygmentsHighlighter): |
|
19 | class FrontendHighlighter(PygmentsHighlighter): | |
20 | """ A PygmentsHighlighter that can be turned on and off and that ignores |
|
20 | """ A PygmentsHighlighter that can be turned on and off and that ignores | |
21 | prompts. |
|
21 | prompts. | |
22 | """ |
|
22 | """ | |
23 |
|
23 | |||
24 | def __init__(self, frontend): |
|
24 | def __init__(self, frontend): | |
25 | super(FrontendHighlighter, self).__init__(frontend._control.document()) |
|
25 | super(FrontendHighlighter, self).__init__(frontend._control.document()) | |
26 | self._current_offset = 0 |
|
26 | self._current_offset = 0 | |
27 | self._frontend = frontend |
|
27 | self._frontend = frontend | |
28 | self.highlighting_on = False |
|
28 | self.highlighting_on = False | |
29 |
|
29 | |||
30 | def highlightBlock(self, qstring): |
|
30 | def highlightBlock(self, qstring): | |
31 | """ Highlight a block of text. Reimplemented to highlight selectively. |
|
31 | """ Highlight a block of text. Reimplemented to highlight selectively. | |
32 | """ |
|
32 | """ | |
33 | if not self.highlighting_on: |
|
33 | if not self.highlighting_on: | |
34 | return |
|
34 | return | |
35 |
|
35 | |||
36 | # The input to this function is unicode string that may contain |
|
36 | # The input to this function is unicode string that may contain | |
37 | # paragraph break characters, non-breaking spaces, etc. Here we acquire |
|
37 | # paragraph break characters, non-breaking spaces, etc. Here we acquire | |
38 | # the string as plain text so we can compare it. |
|
38 | # the string as plain text so we can compare it. | |
39 | current_block = self.currentBlock() |
|
39 | current_block = self.currentBlock() | |
40 | string = self._frontend._get_block_plain_text(current_block) |
|
40 | string = self._frontend._get_block_plain_text(current_block) | |
41 |
|
41 | |||
42 | # Decide whether to check for the regular or continuation prompt. |
|
42 | # Decide whether to check for the regular or continuation prompt. | |
43 | if current_block.contains(self._frontend._prompt_pos): |
|
43 | if current_block.contains(self._frontend._prompt_pos): | |
44 | prompt = self._frontend._prompt |
|
44 | prompt = self._frontend._prompt | |
45 | else: |
|
45 | else: | |
46 | prompt = self._frontend._continuation_prompt |
|
46 | prompt = self._frontend._continuation_prompt | |
47 |
|
47 | |||
48 | # Don't highlight the part of the string that contains the prompt. |
|
48 | # Don't highlight the part of the string that contains the prompt. | |
49 | if string.startswith(prompt): |
|
49 | if string.startswith(prompt): | |
50 | self._current_offset = len(prompt) |
|
50 | self._current_offset = len(prompt) | |
51 | qstring.remove(0, len(prompt)) |
|
51 | qstring.remove(0, len(prompt)) | |
52 | else: |
|
52 | else: | |
53 | self._current_offset = 0 |
|
53 | self._current_offset = 0 | |
54 |
|
54 | |||
55 | PygmentsHighlighter.highlightBlock(self, qstring) |
|
55 | PygmentsHighlighter.highlightBlock(self, qstring) | |
56 |
|
56 | |||
57 | def rehighlightBlock(self, block): |
|
57 | def rehighlightBlock(self, block): | |
58 | """ Reimplemented to temporarily enable highlighting if disabled. |
|
58 | """ Reimplemented to temporarily enable highlighting if disabled. | |
59 | """ |
|
59 | """ | |
60 | old = self.highlighting_on |
|
60 | old = self.highlighting_on | |
61 | self.highlighting_on = True |
|
61 | self.highlighting_on = True | |
62 | super(FrontendHighlighter, self).rehighlightBlock(block) |
|
62 | super(FrontendHighlighter, self).rehighlightBlock(block) | |
63 | self.highlighting_on = old |
|
63 | self.highlighting_on = old | |
64 |
|
64 | |||
65 | def setFormat(self, start, count, format): |
|
65 | def setFormat(self, start, count, format): | |
66 | """ Reimplemented to highlight selectively. |
|
66 | """ Reimplemented to highlight selectively. | |
67 | """ |
|
67 | """ | |
68 | start += self._current_offset |
|
68 | start += self._current_offset | |
69 | PygmentsHighlighter.setFormat(self, start, count, format) |
|
69 | PygmentsHighlighter.setFormat(self, start, count, format) | |
70 |
|
70 | |||
71 |
|
71 | |||
72 | class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): |
|
72 | class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |
73 | """ A Qt frontend for a generic Python kernel. |
|
73 | """ A Qt frontend for a generic Python kernel. | |
74 | """ |
|
74 | """ | |
75 |
|
75 | |||
76 | # Emitted when an 'execute_reply' has been received from the kernel and |
|
76 | # Emitted when an 'execute_reply' has been received from the kernel and | |
77 | # processed by the FrontendWidget. |
|
77 | # processed by the FrontendWidget. | |
78 | executed = QtCore.pyqtSignal(object) |
|
78 | executed = QtCore.pyqtSignal(object) | |
79 |
|
79 | |||
80 | # Protected class attributes. |
|
80 | # Protected class attributes. | |
81 | _highlighter_class = FrontendHighlighter |
|
81 | _highlighter_class = FrontendHighlighter | |
82 | _input_splitter_class = InputSplitter |
|
82 | _input_splitter_class = InputSplitter | |
83 |
|
83 | |||
84 | #--------------------------------------------------------------------------- |
|
84 | #--------------------------------------------------------------------------- | |
85 | # 'object' interface |
|
85 | # 'object' interface | |
86 | #--------------------------------------------------------------------------- |
|
86 | #--------------------------------------------------------------------------- | |
87 |
|
87 | |||
88 | def __init__(self, *args, **kw): |
|
88 | def __init__(self, *args, **kw): | |
89 | super(FrontendWidget, self).__init__(*args, **kw) |
|
89 | super(FrontendWidget, self).__init__(*args, **kw) | |
90 |
|
90 | |||
91 | # FrontendWidget protected variables. |
|
91 | # FrontendWidget protected variables. | |
92 | self._call_tip_widget = CallTipWidget(self._control) |
|
92 | self._call_tip_widget = CallTipWidget(self._control) | |
93 | self._completion_lexer = CompletionLexer(PythonLexer()) |
|
93 | self._completion_lexer = CompletionLexer(PythonLexer()) | |
94 | self._hidden = False |
|
94 | self._hidden = False | |
95 | self._highlighter = self._highlighter_class(self) |
|
95 | self._highlighter = self._highlighter_class(self) | |
96 | self._input_splitter = self._input_splitter_class(input_mode='replace') |
|
96 | self._input_splitter = self._input_splitter_class(input_mode='replace') | |
97 | self._kernel_manager = None |
|
97 | self._kernel_manager = None | |
98 |
|
98 | |||
99 | # Configure the ConsoleWidget. |
|
99 | # Configure the ConsoleWidget. | |
100 | self.tab_width = 4 |
|
100 | self.tab_width = 4 | |
101 | self._set_continuation_prompt('... ') |
|
101 | self._set_continuation_prompt('... ') | |
102 |
|
102 | |||
103 | # Connect signal handlers. |
|
103 | # Connect signal handlers. | |
104 | document = self._control.document() |
|
104 | document = self._control.document() | |
105 | document.contentsChange.connect(self._document_contents_change) |
|
105 | document.contentsChange.connect(self._document_contents_change) | |
106 |
|
106 | |||
107 | #--------------------------------------------------------------------------- |
|
107 | #--------------------------------------------------------------------------- | |
108 | # 'ConsoleWidget' abstract interface |
|
108 | # 'ConsoleWidget' abstract interface | |
109 | #--------------------------------------------------------------------------- |
|
109 | #--------------------------------------------------------------------------- | |
110 |
|
110 | |||
111 | def _is_complete(self, source, interactive): |
|
111 | def _is_complete(self, source, interactive): | |
112 | """ Returns whether 'source' can be completely processed and a new |
|
112 | """ Returns whether 'source' can be completely processed and a new | |
113 | prompt created. When triggered by an Enter/Return key press, |
|
113 | prompt created. When triggered by an Enter/Return key press, | |
114 | 'interactive' is True; otherwise, it is False. |
|
114 | 'interactive' is True; otherwise, it is False. | |
115 | """ |
|
115 | """ | |
116 | complete = self._input_splitter.push(source.expandtabs(4)) |
|
116 | complete = self._input_splitter.push(source.expandtabs(4)) | |
117 | if interactive: |
|
117 | if interactive: | |
118 | complete = not self._input_splitter.push_accepts_more() |
|
118 | complete = not self._input_splitter.push_accepts_more() | |
119 | return complete |
|
119 | return complete | |
120 |
|
120 | |||
121 | def _execute(self, source, hidden): |
|
121 | def _execute(self, source, hidden): | |
122 | """ Execute 'source'. If 'hidden', do not show any output. |
|
122 | """ Execute 'source'. If 'hidden', do not show any output. | |
123 | """ |
|
123 | """ | |
124 | self.kernel_manager.xreq_channel.execute(source) |
|
124 | self.kernel_manager.xreq_channel.execute(source) | |
125 | self._hidden = hidden |
|
125 | self._hidden = hidden | |
126 |
|
126 | |||
127 | def _execute_interrupt(self): |
|
127 | def _execute_interrupt(self): | |
128 | """ Attempts to stop execution. Returns whether this method has an |
|
128 | """ Attempts to stop execution. Returns whether this method has an | |
129 | implementation. |
|
129 | implementation. | |
130 | """ |
|
130 | """ | |
131 | self._interrupt_kernel() |
|
131 | self._interrupt_kernel() | |
132 | return True |
|
132 | return True | |
133 |
|
133 | |||
134 | def _prompt_started_hook(self): |
|
134 | def _prompt_started_hook(self): | |
135 | """ Called immediately after a new prompt is displayed. |
|
135 | """ Called immediately after a new prompt is displayed. | |
136 | """ |
|
136 | """ | |
137 | if not self._reading: |
|
137 | if not self._reading: | |
138 | self._highlighter.highlighting_on = True |
|
138 | self._highlighter.highlighting_on = True | |
139 |
|
139 | |||
140 | def _prompt_finished_hook(self): |
|
140 | def _prompt_finished_hook(self): | |
141 | """ Called immediately after a prompt is finished, i.e. when some input |
|
141 | """ Called immediately after a prompt is finished, i.e. when some input | |
142 | will be processed and a new prompt displayed. |
|
142 | will be processed and a new prompt displayed. | |
143 | """ |
|
143 | """ | |
144 | if not self._reading: |
|
144 | if not self._reading: | |
145 | self._highlighter.highlighting_on = False |
|
145 | self._highlighter.highlighting_on = False | |
146 |
|
146 | |||
147 | def _tab_pressed(self): |
|
147 | def _tab_pressed(self): | |
148 | """ Called when the tab key is pressed. Returns whether to continue |
|
148 | """ Called when the tab key is pressed. Returns whether to continue | |
149 | processing the event. |
|
149 | processing the event. | |
150 | """ |
|
150 | """ | |
151 | self._keep_cursor_in_buffer() |
|
151 | self._keep_cursor_in_buffer() | |
152 | cursor = self._get_cursor() |
|
152 | cursor = self._get_cursor() | |
153 | return not self._complete() |
|
153 | return not self._complete() | |
154 |
|
154 | |||
155 | #--------------------------------------------------------------------------- |
|
155 | #--------------------------------------------------------------------------- | |
156 | # 'ConsoleWidget' protected interface |
|
156 | # 'ConsoleWidget' protected interface | |
157 | #--------------------------------------------------------------------------- |
|
157 | #--------------------------------------------------------------------------- | |
158 |
|
158 | |||
159 | def _show_continuation_prompt(self): |
|
159 | def _show_continuation_prompt(self): | |
160 | """ Reimplemented for auto-indentation. |
|
160 | """ Reimplemented for auto-indentation. | |
161 | """ |
|
161 | """ | |
162 | super(FrontendWidget, self)._show_continuation_prompt() |
|
162 | super(FrontendWidget, self)._show_continuation_prompt() | |
163 | spaces = self._input_splitter.indent_spaces |
|
163 | spaces = self._input_splitter.indent_spaces | |
164 | self._append_plain_text('\t' * (spaces / self.tab_width)) |
|
164 | self._append_plain_text('\t' * (spaces / self.tab_width)) | |
165 | self._append_plain_text(' ' * (spaces % self.tab_width)) |
|
165 | self._append_plain_text(' ' * (spaces % self.tab_width)) | |
166 |
|
166 | |||
167 | #--------------------------------------------------------------------------- |
|
167 | #--------------------------------------------------------------------------- | |
168 | # 'BaseFrontendMixin' abstract interface |
|
168 | # 'BaseFrontendMixin' abstract interface | |
169 | #--------------------------------------------------------------------------- |
|
169 | #--------------------------------------------------------------------------- | |
170 |
|
170 | |||
171 | def _handle_complete_reply(self, rep): |
|
171 | def _handle_complete_reply(self, rep): | |
172 | """ Handle replies for tab completion. |
|
172 | """ Handle replies for tab completion. | |
173 | """ |
|
173 | """ | |
174 | cursor = self._get_cursor() |
|
174 | cursor = self._get_cursor() | |
175 | if rep['parent_header']['msg_id'] == self._complete_id and \ |
|
175 | if rep['parent_header']['msg_id'] == self._complete_id and \ | |
176 | cursor.position() == self._complete_pos: |
|
176 | cursor.position() == self._complete_pos: | |
177 | text = '.'.join(self._get_context()) |
|
177 | text = '.'.join(self._get_context()) | |
178 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(text)) |
|
178 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(text)) | |
179 | self._complete_with_items(cursor, rep['content']['matches']) |
|
179 | self._complete_with_items(cursor, rep['content']['matches']) | |
180 |
|
180 | |||
181 | def _handle_execute_reply(self, msg): |
|
181 | def _handle_execute_reply(self, msg): | |
182 | """ Handles replies for code execution. |
|
182 | """ Handles replies for code execution. | |
183 | """ |
|
183 | """ | |
184 | if not self._hidden: |
|
184 | if not self._hidden: | |
185 | # Make sure that all output from the SUB channel has been processed |
|
185 | # Make sure that all output from the SUB channel has been processed | |
186 | # before writing a new prompt. |
|
186 | # before writing a new prompt. | |
187 | self.kernel_manager.sub_channel.flush() |
|
187 | self.kernel_manager.sub_channel.flush() | |
188 |
|
188 | |||
189 | content = msg['content'] |
|
189 | content = msg['content'] | |
190 | status = content['status'] |
|
190 | status = content['status'] | |
191 | if status == 'ok': |
|
191 | if status == 'ok': | |
192 | self._process_execute_ok(msg) |
|
192 | self._process_execute_ok(msg) | |
193 | elif status == 'error': |
|
193 | elif status == 'error': | |
194 | self._process_execute_error(msg) |
|
194 | self._process_execute_error(msg) | |
195 | elif status == 'abort': |
|
195 | elif status == 'abort': | |
196 | self._process_execute_abort(msg) |
|
196 | self._process_execute_abort(msg) | |
197 |
|
197 | |||
198 | self._show_interpreter_prompt_for_reply(msg) |
|
198 | self._show_interpreter_prompt_for_reply(msg) | |
199 | self.executed.emit(msg) |
|
199 | self.executed.emit(msg) | |
200 |
|
200 | |||
201 | def _handle_input_request(self, msg): |
|
201 | def _handle_input_request(self, msg): | |
202 | """ Handle requests for raw_input. |
|
202 | """ Handle requests for raw_input. | |
203 | """ |
|
203 | """ | |
204 | if self._hidden: |
|
204 | if self._hidden: | |
205 | raise RuntimeError('Request for raw input during hidden execution.') |
|
205 | raise RuntimeError('Request for raw input during hidden execution.') | |
206 |
|
206 | |||
207 | # Make sure that all output from the SUB channel has been processed |
|
207 | # Make sure that all output from the SUB channel has been processed | |
208 | # before entering readline mode. |
|
208 | # before entering readline mode. | |
209 | self.kernel_manager.sub_channel.flush() |
|
209 | self.kernel_manager.sub_channel.flush() | |
210 |
|
210 | |||
211 | def callback(line): |
|
211 | def callback(line): | |
212 | self.kernel_manager.rep_channel.input(line) |
|
212 | self.kernel_manager.rep_channel.input(line) | |
213 | self._readline(msg['content']['prompt'], callback=callback) |
|
213 | self._readline(msg['content']['prompt'], callback=callback) | |
214 |
|
214 | |||
215 | def _handle_object_info_reply(self, rep): |
|
215 | def _handle_object_info_reply(self, rep): | |
216 | """ Handle replies for call tips. |
|
216 | """ Handle replies for call tips. | |
217 | """ |
|
217 | """ | |
218 | cursor = self._get_cursor() |
|
218 | cursor = self._get_cursor() | |
219 | if rep['parent_header']['msg_id'] == self._call_tip_id and \ |
|
219 | if rep['parent_header']['msg_id'] == self._call_tip_id and \ | |
220 | cursor.position() == self._call_tip_pos: |
|
220 | cursor.position() == self._call_tip_pos: | |
221 | doc = rep['content']['docstring'] |
|
221 | doc = rep['content']['docstring'] | |
222 | if doc: |
|
222 | if doc: | |
223 | self._call_tip_widget.show_docstring(doc) |
|
223 | self._call_tip_widget.show_docstring(doc) | |
224 |
|
224 | |||
225 | def _handle_pyout(self, msg): |
|
225 | def _handle_pyout(self, msg): | |
226 | """ Handle display hook output. |
|
226 | """ Handle display hook output. | |
227 | """ |
|
227 | """ | |
228 | if not self._hidden and self._is_from_this_session(msg): |
|
228 | if not self._hidden and self._is_from_this_session(msg): | |
229 | self._append_plain_text(msg['content']['data'] + '\n') |
|
229 | self._append_plain_text(msg['content']['data'] + '\n') | |
230 |
|
230 | |||
231 | def _handle_stream(self, msg): |
|
231 | def _handle_stream(self, msg): | |
232 | """ Handle stdout, stderr, and stdin. |
|
232 | """ Handle stdout, stderr, and stdin. | |
233 | """ |
|
233 | """ | |
234 | if not self._hidden and self._is_from_this_session(msg): |
|
234 | if not self._hidden and self._is_from_this_session(msg): | |
235 | self._append_plain_text(msg['content']['data']) |
|
235 | self._append_plain_text(msg['content']['data']) | |
236 | self._control.moveCursor(QtGui.QTextCursor.End) |
|
236 | self._control.moveCursor(QtGui.QTextCursor.End) | |
237 |
|
237 | |||
238 | def _started_channels(self): |
|
238 | def _started_channels(self): | |
239 | """ Called when the KernelManager channels have started listening or |
|
239 | """ Called when the KernelManager channels have started listening or | |
240 | when the frontend is assigned an already listening KernelManager. |
|
240 | when the frontend is assigned an already listening KernelManager. | |
241 | """ |
|
241 | """ | |
242 |
self._ |
|
242 | self._control.clear() | |
243 | self._append_plain_text(self._get_banner()) |
|
243 | self._append_plain_text(self._get_banner()) | |
244 | self._show_interpreter_prompt() |
|
244 | self._show_interpreter_prompt() | |
245 |
|
245 | |||
246 | def _stopped_channels(self): |
|
246 | def _stopped_channels(self): | |
247 | """ Called when the KernelManager channels have stopped listening or |
|
247 | """ Called when the KernelManager channels have stopped listening or | |
248 | when a listening KernelManager is removed from the frontend. |
|
248 | when a listening KernelManager is removed from the frontend. | |
249 | """ |
|
249 | """ | |
250 | # FIXME: Print a message here? |
|
250 | self._executing = self._reading = False | |
251 | pass |
|
251 | self._highlighter.highlighting_on = False | |
252 |
|
252 | |||
253 | #--------------------------------------------------------------------------- |
|
253 | #--------------------------------------------------------------------------- | |
254 | # 'FrontendWidget' interface |
|
254 | # 'FrontendWidget' interface | |
255 | #--------------------------------------------------------------------------- |
|
255 | #--------------------------------------------------------------------------- | |
256 |
|
256 | |||
257 | def execute_file(self, path, hidden=False): |
|
257 | def execute_file(self, path, hidden=False): | |
258 | """ Attempts to execute file with 'path'. If 'hidden', no output is |
|
258 | """ Attempts to execute file with 'path'. If 'hidden', no output is | |
259 | shown. |
|
259 | shown. | |
260 | """ |
|
260 | """ | |
261 | self.execute('execfile("%s")' % path, hidden=hidden) |
|
261 | self.execute('execfile("%s")' % path, hidden=hidden) | |
262 |
|
262 | |||
263 | #--------------------------------------------------------------------------- |
|
263 | #--------------------------------------------------------------------------- | |
264 | # 'FrontendWidget' protected interface |
|
264 | # 'FrontendWidget' protected interface | |
265 | #--------------------------------------------------------------------------- |
|
265 | #--------------------------------------------------------------------------- | |
266 |
|
266 | |||
267 | def _call_tip(self): |
|
267 | def _call_tip(self): | |
268 | """ Shows a call tip, if appropriate, at the current cursor location. |
|
268 | """ Shows a call tip, if appropriate, at the current cursor location. | |
269 | """ |
|
269 | """ | |
270 | # Decide if it makes sense to show a call tip |
|
270 | # Decide if it makes sense to show a call tip | |
271 | cursor = self._get_cursor() |
|
271 | cursor = self._get_cursor() | |
272 | cursor.movePosition(QtGui.QTextCursor.Left) |
|
272 | cursor.movePosition(QtGui.QTextCursor.Left) | |
273 | document = self._control.document() |
|
273 | document = self._control.document() | |
274 | if document.characterAt(cursor.position()).toAscii() != '(': |
|
274 | if document.characterAt(cursor.position()).toAscii() != '(': | |
275 | return False |
|
275 | return False | |
276 | context = self._get_context(cursor) |
|
276 | context = self._get_context(cursor) | |
277 | if not context: |
|
277 | if not context: | |
278 | return False |
|
278 | return False | |
279 |
|
279 | |||
280 | # Send the metadata request to the kernel |
|
280 | # Send the metadata request to the kernel | |
281 | name = '.'.join(context) |
|
281 | name = '.'.join(context) | |
282 | self._call_tip_id = self.kernel_manager.xreq_channel.object_info(name) |
|
282 | self._call_tip_id = self.kernel_manager.xreq_channel.object_info(name) | |
283 | self._call_tip_pos = self._get_cursor().position() |
|
283 | self._call_tip_pos = self._get_cursor().position() | |
284 | return True |
|
284 | return True | |
285 |
|
285 | |||
286 | def _complete(self): |
|
286 | def _complete(self): | |
287 | """ Performs completion at the current cursor location. |
|
287 | """ Performs completion at the current cursor location. | |
288 | """ |
|
288 | """ | |
289 | # Decide if it makes sense to do completion |
|
289 | # Decide if it makes sense to do completion | |
290 | context = self._get_context() |
|
290 | context = self._get_context() | |
291 | if not context: |
|
291 | if not context: | |
292 | return False |
|
292 | return False | |
293 |
|
293 | |||
294 | # Send the completion request to the kernel |
|
294 | # Send the completion request to the kernel | |
295 | self._complete_id = self.kernel_manager.xreq_channel.complete( |
|
295 | self._complete_id = self.kernel_manager.xreq_channel.complete( | |
296 | '.'.join(context), # text |
|
296 | '.'.join(context), # text | |
297 | self._get_input_buffer_cursor_line(), # line |
|
297 | self._get_input_buffer_cursor_line(), # line | |
298 | self._get_input_buffer_cursor_column(), # cursor_pos |
|
298 | self._get_input_buffer_cursor_column(), # cursor_pos | |
299 | self.input_buffer) # block |
|
299 | self.input_buffer) # block | |
300 | self._complete_pos = self._get_cursor().position() |
|
300 | self._complete_pos = self._get_cursor().position() | |
301 | return True |
|
301 | return True | |
302 |
|
302 | |||
303 | def _get_banner(self): |
|
303 | def _get_banner(self): | |
304 | """ Gets a banner to display at the beginning of a session. |
|
304 | """ Gets a banner to display at the beginning of a session. | |
305 | """ |
|
305 | """ | |
306 | banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \ |
|
306 | banner = 'Python %s on %s\nType "help", "copyright", "credits" or ' \ | |
307 | '"license" for more information.' |
|
307 | '"license" for more information.' | |
308 | return banner % (sys.version, sys.platform) |
|
308 | return banner % (sys.version, sys.platform) | |
309 |
|
309 | |||
310 | def _get_context(self, cursor=None): |
|
310 | def _get_context(self, cursor=None): | |
311 | """ Gets the context at the current cursor location. |
|
311 | """ Gets the context at the current cursor location. | |
312 | """ |
|
312 | """ | |
313 | if cursor is None: |
|
313 | if cursor is None: | |
314 | cursor = self._get_cursor() |
|
314 | cursor = self._get_cursor() | |
315 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, |
|
315 | cursor.movePosition(QtGui.QTextCursor.StartOfBlock, | |
316 | QtGui.QTextCursor.KeepAnchor) |
|
316 | QtGui.QTextCursor.KeepAnchor) | |
317 | text = str(cursor.selection().toPlainText()) |
|
317 | text = str(cursor.selection().toPlainText()) | |
318 | return self._completion_lexer.get_context(text) |
|
318 | return self._completion_lexer.get_context(text) | |
319 |
|
319 | |||
320 | def _interrupt_kernel(self): |
|
320 | def _interrupt_kernel(self): | |
321 | """ Attempts to the interrupt the kernel. |
|
321 | """ Attempts to the interrupt the kernel. | |
322 | """ |
|
322 | """ | |
323 | if self.kernel_manager.has_kernel: |
|
323 | if self.kernel_manager.has_kernel: | |
324 | self.kernel_manager.signal_kernel(signal.SIGINT) |
|
324 | self.kernel_manager.signal_kernel(signal.SIGINT) | |
325 | else: |
|
325 | else: | |
326 | self._append_plain_text('Kernel process is either remote or ' |
|
326 | self._append_plain_text('Kernel process is either remote or ' | |
327 | 'unspecified. Cannot interrupt.\n') |
|
327 | 'unspecified. Cannot interrupt.\n') | |
328 |
|
328 | |||
329 | def _process_execute_abort(self, msg): |
|
329 | def _process_execute_abort(self, msg): | |
330 | """ Process a reply for an aborted execution request. |
|
330 | """ Process a reply for an aborted execution request. | |
331 | """ |
|
331 | """ | |
332 | self._append_plain_text("ERROR: execution aborted\n") |
|
332 | self._append_plain_text("ERROR: execution aborted\n") | |
333 |
|
333 | |||
334 | def _process_execute_error(self, msg): |
|
334 | def _process_execute_error(self, msg): | |
335 | """ Process a reply for an execution request that resulted in an error. |
|
335 | """ Process a reply for an execution request that resulted in an error. | |
336 | """ |
|
336 | """ | |
337 | content = msg['content'] |
|
337 | content = msg['content'] | |
338 | traceback = ''.join(content['traceback']) |
|
338 | traceback = ''.join(content['traceback']) | |
339 | self._append_plain_text(traceback) |
|
339 | self._append_plain_text(traceback) | |
340 |
|
340 | |||
341 | def _process_execute_ok(self, msg): |
|
341 | def _process_execute_ok(self, msg): | |
342 | """ Process a reply for a successful execution equest. |
|
342 | """ Process a reply for a successful execution equest. | |
343 | """ |
|
343 | """ | |
344 | payload = msg['content']['payload'] |
|
344 | payload = msg['content']['payload'] | |
345 | for item in payload: |
|
345 | for item in payload: | |
346 | if not self._process_execute_payload(item): |
|
346 | if not self._process_execute_payload(item): | |
347 | warning = 'Received unknown payload of type %s\n' |
|
347 | warning = 'Received unknown payload of type %s\n' | |
348 | self._append_plain_text(warning % repr(item['source'])) |
|
348 | self._append_plain_text(warning % repr(item['source'])) | |
349 |
|
349 | |||
350 | def _process_execute_payload(self, item): |
|
350 | def _process_execute_payload(self, item): | |
351 | """ Process a single payload item from the list of payload items in an |
|
351 | """ Process a single payload item from the list of payload items in an | |
352 | execution reply. Returns whether the payload was handled. |
|
352 | execution reply. Returns whether the payload was handled. | |
353 | """ |
|
353 | """ | |
354 | # The basic FrontendWidget doesn't handle payloads, as they are a |
|
354 | # The basic FrontendWidget doesn't handle payloads, as they are a | |
355 | # mechanism for going beyond the standard Python interpreter model. |
|
355 | # mechanism for going beyond the standard Python interpreter model. | |
356 | return False |
|
356 | return False | |
357 |
|
357 | |||
358 | def _show_interpreter_prompt(self): |
|
358 | def _show_interpreter_prompt(self): | |
359 | """ Shows a prompt for the interpreter. |
|
359 | """ Shows a prompt for the interpreter. | |
360 | """ |
|
360 | """ | |
361 | self._show_prompt('>>> ') |
|
361 | self._show_prompt('>>> ') | |
362 |
|
362 | |||
363 | def _show_interpreter_prompt_for_reply(self, msg): |
|
363 | def _show_interpreter_prompt_for_reply(self, msg): | |
364 | """ Shows a prompt for the interpreter given an 'execute_reply' message. |
|
364 | """ Shows a prompt for the interpreter given an 'execute_reply' message. | |
365 | """ |
|
365 | """ | |
366 | self._show_interpreter_prompt() |
|
366 | self._show_interpreter_prompt() | |
367 |
|
367 | |||
368 | #------ Signal handlers ---------------------------------------------------- |
|
368 | #------ Signal handlers ---------------------------------------------------- | |
369 |
|
369 | |||
370 | def _document_contents_change(self, position, removed, added): |
|
370 | def _document_contents_change(self, position, removed, added): | |
371 | """ Called whenever the document's content changes. Display a call tip |
|
371 | """ Called whenever the document's content changes. Display a call tip | |
372 | if appropriate. |
|
372 | if appropriate. | |
373 | """ |
|
373 | """ | |
374 | # Calculate where the cursor should be *after* the change: |
|
374 | # Calculate where the cursor should be *after* the change: | |
375 | position += added |
|
375 | position += added | |
376 |
|
376 | |||
377 | document = self._control.document() |
|
377 | document = self._control.document() | |
378 | if position == self._get_cursor().position(): |
|
378 | if position == self._get_cursor().position(): | |
379 | self._call_tip() |
|
379 | self._call_tip() |
General Comments 0
You need to be logged in to leave comments.
Login now