Show More
@@ -0,0 +1,226 | |||
|
1 | # System library imports | |
|
2 | from IPython.external.qt import QtCore, QtGui | |
|
3 | import IPython.utils.html_utils as html_utils | |
|
4 | ||
|
5 | ||
|
6 | class CompletionHtml(QtGui.QWidget): | |
|
7 | """ A widget for tab completion, navigable by arrow keys """ | |
|
8 | ||
|
9 | #-------------------------------------------------------------------------- | |
|
10 | # 'QObject' interface | |
|
11 | #-------------------------------------------------------------------------- | |
|
12 | ||
|
13 | _items = () | |
|
14 | _index = (0, 0) | |
|
15 | _consecutive_tab = 0 | |
|
16 | _size = (1, 1) | |
|
17 | _old_cursor = None | |
|
18 | _start_position = 0 | |
|
19 | ||
|
20 | def __init__(self, console_widget): | |
|
21 | """ Create a completion widget that is attached to the specified Qt | |
|
22 | text edit widget. | |
|
23 | """ | |
|
24 | assert isinstance(console_widget._control, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) | |
|
25 | super(CompletionHtml, self).__init__() | |
|
26 | ||
|
27 | self._text_edit = console_widget._control | |
|
28 | self._console_widget = console_widget | |
|
29 | self._text_edit.installEventFilter(self) | |
|
30 | ||
|
31 | # Ensure that the text edit keeps focus when widget is displayed. | |
|
32 | self.setFocusProxy(self._text_edit) | |
|
33 | ||
|
34 | ||
|
35 | def eventFilter(self, obj, event): | |
|
36 | """ Reimplemented to handle keyboard input and to auto-hide when the | |
|
37 | text edit loses focus. | |
|
38 | """ | |
|
39 | if obj == self._text_edit: | |
|
40 | etype = event.type() | |
|
41 | if etype == QtCore.QEvent.KeyPress: | |
|
42 | key = event.key() | |
|
43 | if self._consecutive_tab == 0 and key in (QtCore.Qt.Key_Tab,): | |
|
44 | return False | |
|
45 | elif self._consecutive_tab == 1 and key in (QtCore.Qt.Key_Tab,): | |
|
46 | # ok , called twice, we grab focus, and show the cursor | |
|
47 | self._consecutive_tab = self._consecutive_tab+1 | |
|
48 | self._update_list() | |
|
49 | return True | |
|
50 | elif self._consecutive_tab == 2: | |
|
51 | if key in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter): | |
|
52 | self._complete_current() | |
|
53 | return True | |
|
54 | if key in (QtCore.Qt.Key_Tab,): | |
|
55 | self.select_right() | |
|
56 | self._update_list() | |
|
57 | return True | |
|
58 | elif key in ( QtCore.Qt.Key_Down,): | |
|
59 | self.select_down() | |
|
60 | self._update_list() | |
|
61 | return True | |
|
62 | elif key in (QtCore.Qt.Key_Right,): | |
|
63 | self.select_right() | |
|
64 | self._update_list() | |
|
65 | return True | |
|
66 | elif key in ( QtCore.Qt.Key_Up,): | |
|
67 | self.select_up() | |
|
68 | self._update_list() | |
|
69 | return True | |
|
70 | elif key in ( QtCore.Qt.Key_Left,): | |
|
71 | self.select_left() | |
|
72 | self._update_list() | |
|
73 | return True | |
|
74 | else : | |
|
75 | self._cancel_completion() | |
|
76 | else: | |
|
77 | self._cancel_completion() | |
|
78 | ||
|
79 | elif etype == QtCore.QEvent.FocusOut: | |
|
80 | self._cancel_completion() | |
|
81 | ||
|
82 | return super(CompletionHtml, self).eventFilter(obj, event) | |
|
83 | ||
|
84 | #-------------------------------------------------------------------------- | |
|
85 | # 'CompletionHtml' interface | |
|
86 | #-------------------------------------------------------------------------- | |
|
87 | def _cancel_completion(self): | |
|
88 | """Cancel the completion, reseting internal variable, clearing buffer """ | |
|
89 | self._consecutive_tab = 0 | |
|
90 | self._console_widget._clear_temporary_buffer() | |
|
91 | self._index = (0, 0) | |
|
92 | ||
|
93 | # | |
|
94 | # ... 2 4 4 4 4 4 4 4 4 4 4 4 4 | |
|
95 | # 2 2 4 4 4 4 4 4 4 4 4 4 4 4 | |
|
96 | # | |
|
97 | #2 2 x x x x x x x x x x x 5 5 | |
|
98 | #6 6 x x x x x x x x x x x 5 5 | |
|
99 | #6 6 x x x x x x x x x x ? 5 5 | |
|
100 | #6 6 x x x x x x x x x x ? 1 1 | |
|
101 | # | |
|
102 | #3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 ... | |
|
103 | #3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 ... | |
|
104 | def _select_index(self, row, col): | |
|
105 | """Change the selection index, and make sure it stays in the right range | |
|
106 | ||
|
107 | A little more complicated than just dooing modulo the number of row columns | |
|
108 | to be sure to cycle through all element. | |
|
109 | ||
|
110 | horizontaly, the element are maped like this : | |
|
111 | to r <-- a b c d e f --> to g | |
|
112 | to f <-- g h i j k l --> to m | |
|
113 | to l <-- m n o p q r --> to a | |
|
114 | ||
|
115 | and vertically | |
|
116 | a d g j m p | |
|
117 | b e h k n q | |
|
118 | c f i l o r | |
|
119 | """ | |
|
120 | ||
|
121 | nr, nc = self._size | |
|
122 | nr = nr-1 | |
|
123 | nc = nc-1 | |
|
124 | ||
|
125 | # case 1 | |
|
126 | if (row > nr and col >= nc) or (row >= nr and col > nc): | |
|
127 | self._select_index(0, 0) | |
|
128 | # case 2 | |
|
129 | elif (row <= 0 and col < 0) or (row < 0 and col <= 0): | |
|
130 | self._select_index(nr, nc) | |
|
131 | # case 3 | |
|
132 | elif row > nr : | |
|
133 | self._select_index(0, col+1) | |
|
134 | # case 4 | |
|
135 | elif row < 0 : | |
|
136 | self._select_index(nr, col-1) | |
|
137 | # case 5 | |
|
138 | elif col > nc : | |
|
139 | self._select_index(row+1, 0) | |
|
140 | # case 6 | |
|
141 | elif col < 0 : | |
|
142 | self._select_index(row-1, nc) | |
|
143 | elif 0 <= row and row <= nr and 0 <= col and col <= nc : | |
|
144 | self._index = (row, col) | |
|
145 | else : | |
|
146 | raise NotImplementedError("you'r trying to go where no completion\ | |
|
147 | have gone before : %d:%d (%d:%d)"%(row, col, nr, nc) ) | |
|
148 | ||
|
149 | ||
|
150 | ||
|
151 | def select_up(self): | |
|
152 | """move cursor up""" | |
|
153 | r, c = self._index | |
|
154 | self._select_index(r-1, c) | |
|
155 | ||
|
156 | def select_down(self): | |
|
157 | """move cursor down""" | |
|
158 | r, c = self._index | |
|
159 | self._select_index(r+1, c) | |
|
160 | ||
|
161 | def select_left(self): | |
|
162 | """move cursor left""" | |
|
163 | r, c = self._index | |
|
164 | self._select_index(r, c-1) | |
|
165 | ||
|
166 | def select_right(self): | |
|
167 | """move cursor right""" | |
|
168 | r, c = self._index | |
|
169 | self._select_index(r, c+1) | |
|
170 | ||
|
171 | def show_items(self, cursor, items): | |
|
172 | """ Shows the completion widget with 'items' at the position specified | |
|
173 | by 'cursor'. | |
|
174 | """ | |
|
175 | if not items : | |
|
176 | return | |
|
177 | self._start_position = cursor.position() | |
|
178 | self._consecutive_tab = 1 | |
|
179 | ci = html_utils.columnize_info(items, empty=' ') | |
|
180 | self._items = ci['item_matrix'] | |
|
181 | self._size = (ci['rows_number'], ci['columns_number']) | |
|
182 | self._old_cursor = cursor | |
|
183 | self._index = (0, 0) | |
|
184 | self._update_list(hilight=False) | |
|
185 | ||
|
186 | ||
|
187 | def _update_list(self, hilight=True): | |
|
188 | """ update the list of completion and hilight the currently selected completion """ | |
|
189 | if len(self._items) > 100: | |
|
190 | items = self._items[:100] | |
|
191 | else : | |
|
192 | items = self._items | |
|
193 | items_m = items | |
|
194 | ||
|
195 | self._console_widget._clear_temporary_buffer() | |
|
196 | if(hilight): | |
|
197 | strng = html_utils.html_tableify(items_m, select=self._index) | |
|
198 | else: | |
|
199 | strng = html_utils.html_tableify(items_m, select=None) | |
|
200 | self._console_widget._fill_temporary_buffer(self._old_cursor, strng, html=True) | |
|
201 | ||
|
202 | #-------------------------------------------------------------------------- | |
|
203 | # Protected interface | |
|
204 | #-------------------------------------------------------------------------- | |
|
205 | ||
|
206 | def _complete_current(self): | |
|
207 | """ Perform the completion with the currently selected item. | |
|
208 | """ | |
|
209 | i = self._index | |
|
210 | item = self._items[i[0]][i[1]] | |
|
211 | item = item.strip() | |
|
212 | if item : | |
|
213 | self._current_text_cursor().insertText(item) | |
|
214 | self._cancel_completion() | |
|
215 | ||
|
216 | def _current_text_cursor(self): | |
|
217 | """ Returns a cursor with text between the start position and the | |
|
218 | current position selected. | |
|
219 | """ | |
|
220 | cursor = self._text_edit.textCursor() | |
|
221 | if cursor.position() >= self._start_position: | |
|
222 | cursor.setPosition(self._start_position, | |
|
223 | QtGui.QTextCursor.KeepAnchor) | |
|
224 | return cursor | |
|
225 | ||
|
226 |
@@ -0,0 +1,73 | |||
|
1 | # System library imports | |
|
2 | from IPython.external.qt import QtCore, QtGui | |
|
3 | import IPython.utils.html_utils as html_utils | |
|
4 | ||
|
5 | ||
|
6 | class CompletionPlain(QtGui.QWidget): | |
|
7 | """ A widget for tab completion, navigable by arrow keys """ | |
|
8 | ||
|
9 | #-------------------------------------------------------------------------- | |
|
10 | # 'QObject' interface | |
|
11 | #-------------------------------------------------------------------------- | |
|
12 | ||
|
13 | _items = () | |
|
14 | _index = (0, 0) | |
|
15 | _old_cursor = None | |
|
16 | ||
|
17 | def __init__(self, console_widget): | |
|
18 | """ Create a completion widget that is attached to the specified Qt | |
|
19 | text edit widget. | |
|
20 | """ | |
|
21 | assert isinstance(console_widget._control, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) | |
|
22 | super(CompletionPlain, self).__init__() | |
|
23 | ||
|
24 | self._text_edit = console_widget._control | |
|
25 | self._console_widget = console_widget | |
|
26 | ||
|
27 | self._text_edit.installEventFilter(self) | |
|
28 | ||
|
29 | def eventFilter(self, obj, event): | |
|
30 | """ Reimplemented to handle keyboard input and to auto-hide when the | |
|
31 | text edit loses focus. | |
|
32 | """ | |
|
33 | if obj == self._text_edit: | |
|
34 | etype = event.type() | |
|
35 | ||
|
36 | if etype == QtCore.QEvent.KeyPress: | |
|
37 | self._cancel_completion() | |
|
38 | ||
|
39 | return super(CompletionPlain, self).eventFilter(obj, event) | |
|
40 | ||
|
41 | #-------------------------------------------------------------------------- | |
|
42 | # 'CompletionPlain' interface | |
|
43 | #-------------------------------------------------------------------------- | |
|
44 | def _cancel_completion(self): | |
|
45 | """Cancel the completion, reseting internal variable, clearing buffer """ | |
|
46 | self._console_widget._clear_temporary_buffer() | |
|
47 | self._index = (0, 0) | |
|
48 | ||
|
49 | ||
|
50 | def show_items(self, cursor, items): | |
|
51 | """ Shows the completion widget with 'items' at the position specified | |
|
52 | by 'cursor'. | |
|
53 | """ | |
|
54 | if not items : | |
|
55 | return | |
|
56 | ||
|
57 | ci = html_utils.columnize_info(items, empty=' ') | |
|
58 | self._items = ci['item_matrix'] | |
|
59 | self._old_cursor = cursor | |
|
60 | self._update_list() | |
|
61 | ||
|
62 | ||
|
63 | def _update_list(self): | |
|
64 | """ update the list of completion and hilight the currently selected completion """ | |
|
65 | if len(self._items) > 100: | |
|
66 | items = self._items[:100] | |
|
67 | else : | |
|
68 | items = self._items | |
|
69 | items_m = items | |
|
70 | ||
|
71 | self._console_widget._clear_temporary_buffer() | |
|
72 | strng = html_utils.html_tableify(items_m, select=None) | |
|
73 | self._console_widget._fill_temporary_buffer(self._old_cursor, strng, html=True) |
@@ -0,0 +1,124 | |||
|
1 | """some html utilis""" | |
|
2 | from IPython.core.display import HTML | |
|
3 | ||
|
4 | ||
|
5 | def columnize_info(items, separator_width=1, displaywidth=80, empty=None): | |
|
6 | """ Get info on a list of string to display it as a multicolumns list | |
|
7 | ||
|
8 | returns : | |
|
9 | --------- | |
|
10 | ||
|
11 | a dict containing several parameters: | |
|
12 | ||
|
13 | 'item_matrix' : list of list with the innermost list representing a row | |
|
14 | 'columns_number': number of columns | |
|
15 | 'rows_number' : number of rown | |
|
16 | 'columns_width' : a list indicating the maximum length of the element in each columns | |
|
17 | ||
|
18 | Parameters : | |
|
19 | ------------ | |
|
20 | separator_width : when trying to ajust the number of column, consider a separator size of this much caracters | |
|
21 | displaywidth : try to fit the columns in this width | |
|
22 | empty : if the number of items is different from nrows * ncols, fill with empty | |
|
23 | ||
|
24 | """ | |
|
25 | # Note: this code is adapted from columnize 0.3.2. | |
|
26 | # See http://code.google.com/p/pycolumnize/ | |
|
27 | ||
|
28 | # Some degenerate cases. | |
|
29 | size = len(items) | |
|
30 | if size == 0: | |
|
31 | return {'item_matrix' :[[empty]], | |
|
32 | 'columns_number':1, | |
|
33 | 'rows_number':1, | |
|
34 | 'columns_width':[0]} | |
|
35 | elif size == 1: | |
|
36 | return {'item_matrix' :[[items[0]]], | |
|
37 | 'columns_number':1, | |
|
38 | 'rows_number':1, | |
|
39 | 'columns_width':[len(items[0])]} | |
|
40 | ||
|
41 | # Special case: if any item is longer than the maximum width, there's no | |
|
42 | # point in triggering the logic below... | |
|
43 | item_len = map(len, items) # save these, we can reuse them below | |
|
44 | #longest = max(item_len) | |
|
45 | #if longest >= displaywidth: | |
|
46 | # return (items, [longest]) | |
|
47 | ||
|
48 | # Try every row count from 1 upwards | |
|
49 | array_index = lambda nrows, row, col: nrows*col + row | |
|
50 | nrows = 1 | |
|
51 | for nrows in range(1, size): | |
|
52 | ncols = (size + nrows - 1) // nrows | |
|
53 | colwidths = [] | |
|
54 | totwidth = -separator_width | |
|
55 | for col in range(ncols): | |
|
56 | # Get max column width for this column | |
|
57 | colwidth = 0 | |
|
58 | for row in range(nrows): | |
|
59 | i = array_index(nrows, row, col) | |
|
60 | if i >= size: | |
|
61 | break | |
|
62 | len_x = item_len[i] | |
|
63 | colwidth = max(colwidth, len_x) | |
|
64 | colwidths.append(colwidth) | |
|
65 | totwidth += colwidth + separator_width | |
|
66 | if totwidth > displaywidth: | |
|
67 | break | |
|
68 | if totwidth <= displaywidth: | |
|
69 | break | |
|
70 | ||
|
71 | # The smallest number of rows computed and the max widths for each | |
|
72 | # column has been obtained. Now we just have to format each of the rows. | |
|
73 | reorderd_items = [] | |
|
74 | for row in range(nrows): | |
|
75 | texts = [] | |
|
76 | for col in range(ncols): | |
|
77 | i = row + nrows*col | |
|
78 | if i >= size: | |
|
79 | texts.append(empty) | |
|
80 | else: | |
|
81 | texts.append(items[i]) | |
|
82 | #while texts and not texts[-1]: | |
|
83 | # del texts[-1] | |
|
84 | #for col in range(len(texts)): | |
|
85 | # texts[col] = texts[col].ljust(colwidths[col]) | |
|
86 | reorderd_items.append(texts) | |
|
87 | ||
|
88 | return {'item_matrix' :reorderd_items, | |
|
89 | 'columns_number':ncols, | |
|
90 | 'rows_number':nrows, | |
|
91 | 'columns_width':colwidths} | |
|
92 | ||
|
93 | ||
|
94 | def column_table(items, select=None) : | |
|
95 | """ return a html table of the item with a select class on one""" | |
|
96 | items_m = columnize_info(items)['item_matrix'] | |
|
97 | return HTML(html_tableify(items_m, select=select)) | |
|
98 | ||
|
99 | def html_tableify(item_matrix, select=None) : | |
|
100 | """ returnr a string for an html table""" | |
|
101 | if not item_matrix : | |
|
102 | return '' | |
|
103 | html_cols = [] | |
|
104 | tds = lambda text : u'<td>'+text+u'</td>' | |
|
105 | trs = lambda text : u'<tr>'+text+u'</tr>' | |
|
106 | tds_items = [map(tds, row) for row in item_matrix ] | |
|
107 | if select : | |
|
108 | row, col = select | |
|
109 | try : | |
|
110 | tds_items[row][col] = u'<td class="inverted">'\ | |
|
111 | +item_matrix[row][col]\ | |
|
112 | +u'</td>' | |
|
113 | except IndexError : | |
|
114 | pass | |
|
115 | #select the right item | |
|
116 | html_cols = map(trs, (u''.join(row) for row in tds_items)) | |
|
117 | html = (u'<table class="completion">'+(u''.join(html_cols))+u'</table>') | |
|
118 | css = u""" | |
|
119 | <style> | |
|
120 | table.completion tr td | |
|
121 | { padding-right : 4px; } | |
|
122 | </style> | |
|
123 | """ | |
|
124 | return css+html |
@@ -10,10 +10,11 class CompletionWidget(QtGui.QListWidget): | |||
|
10 | 10 | # 'QObject' interface |
|
11 | 11 | #-------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 |
def __init__(self, |
|
|
13 | def __init__(self, console_widget): | |
|
14 | 14 | """ Create a completion widget that is attached to the specified Qt |
|
15 | 15 | text edit widget. |
|
16 | 16 | """ |
|
17 | text_edit = console_widget._text_edit | |
|
17 | 18 | assert isinstance(text_edit, (QtGui.QTextEdit, QtGui.QPlainTextEdit)) |
|
18 | 19 | super(CompletionWidget, self).__init__() |
|
19 | 20 |
@@ -5,7 +5,6 | |||
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | |
|
7 | 7 | # Standard library imports |
|
8 | import os | |
|
9 | 8 | from os.path import commonprefix |
|
10 | 9 | import re |
|
11 | 10 | import sys |
@@ -23,6 +22,8 from IPython.utils.text import columnize | |||
|
23 | 22 | from IPython.utils.traitlets import Bool, Enum, Integer, Unicode |
|
24 | 23 | from ansi_code_processor import QtAnsiCodeProcessor |
|
25 | 24 | from completion_widget import CompletionWidget |
|
25 | from completion_html import CompletionHtml | |
|
26 | from completion_plain import CompletionPlain | |
|
26 | 27 | from kill_ring import QtKillRing |
|
27 | 28 | |
|
28 | 29 | #----------------------------------------------------------------------------- |
@@ -65,10 +66,19 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
65 | 66 | non-positive number disables text truncation (not recommended). |
|
66 | 67 | """ |
|
67 | 68 | ) |
|
68 |
gui_completion = |
|
|
69 | help=""" | |
|
70 | Use a list widget instead of plain text output for tab completion. | |
|
71 | """ | |
|
69 | gui_completion = Enum(['plain', 'droplist', 'ncurses'], config=True, | |
|
70 | default_value = 'ncurses', | |
|
71 | help=""" | |
|
72 | The type of completer to use. Valid values are: | |
|
73 | ||
|
74 | 'plain' : Show the availlable completion as a text list | |
|
75 | Below the editting area. | |
|
76 | 'droplist': Show the completion in a drop down list navigable | |
|
77 | by the arrow keys, and from which you can select | |
|
78 | completion by pressing Return. | |
|
79 | 'ncurses' : Show the completion as a text list which is navigable by | |
|
80 | `tab` and arrow keys. | |
|
81 | """ | |
|
72 | 82 | ) |
|
73 | 83 | # NOTE: this value can only be specified during initialization. |
|
74 | 84 | kind = Enum(['plain', 'rich'], default_value='plain', config=True, |
@@ -137,12 +147,12 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
137 | 147 | font_changed = QtCore.Signal(QtGui.QFont) |
|
138 | 148 | |
|
139 | 149 | #------ Protected class variables ------------------------------------------ |
|
140 | ||
|
150 | ||
|
141 | 151 | # control handles |
|
142 | 152 | _control = None |
|
143 | 153 | _page_control = None |
|
144 | 154 | _splitter = None |
|
145 | ||
|
155 | ||
|
146 | 156 | # When the control key is down, these keys are mapped. |
|
147 | 157 | _ctrl_down_remap = { QtCore.Qt.Key_B : QtCore.Qt.Key_Left, |
|
148 | 158 | QtCore.Qt.Key_F : QtCore.Qt.Key_Right, |
@@ -161,6 +171,8 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
161 | 171 | [ QtCore.Qt.Key_C, QtCore.Qt.Key_G, QtCore.Qt.Key_O, |
|
162 | 172 | QtCore.Qt.Key_V ]) |
|
163 | 173 | |
|
174 | _is_completing = False | |
|
175 | ||
|
164 | 176 | #--------------------------------------------------------------------------- |
|
165 | 177 | # 'QObject' interface |
|
166 | 178 | #--------------------------------------------------------------------------- |
@@ -211,7 +223,13 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
211 | 223 | # information for subclasses; they should be considered read-only. |
|
212 | 224 | self._append_before_prompt_pos = 0 |
|
213 | 225 | self._ansi_processor = QtAnsiCodeProcessor() |
|
214 | self._completion_widget = CompletionWidget(self._control) | |
|
226 | if self.gui_completion == 'ncurses': | |
|
227 | self._completion_widget = CompletionHtml(self) | |
|
228 | elif self.gui_completion == 'droplist': | |
|
229 | self._completion_widget = CompletionWidget(self) | |
|
230 | elif self.gui_completion == 'plain': | |
|
231 | self._completion_widget = CompletionPlain(self) | |
|
232 | ||
|
215 | 233 | self._continuation_prompt = '> ' |
|
216 | 234 | self._continuation_prompt_html = None |
|
217 | 235 | self._executing = False |
@@ -228,7 +246,6 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
228 | 246 | self._reading = False |
|
229 | 247 | self._reading_callback = None |
|
230 | 248 | self._tab_width = 8 |
|
231 | self._text_completing_pos = 0 | |
|
232 | 249 | |
|
233 | 250 | # Set a monospaced font. |
|
234 | 251 | self.reset_font() |
@@ -823,12 +840,10 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
823 | 840 | """ |
|
824 | 841 | self._append_custom(self._insert_plain_text, text, before_prompt) |
|
825 | 842 | |
|
826 |
def _cancel_ |
|
|
843 | def _cancel_completion(self): | |
|
827 | 844 | """ If text completion is progress, cancel it. |
|
828 | 845 | """ |
|
829 |
|
|
|
830 | self._clear_temporary_buffer() | |
|
831 | self._text_completing_pos = 0 | |
|
846 | self._completion_widget._cancel_completion() | |
|
832 | 847 | |
|
833 | 848 | def _clear_temporary_buffer(self): |
|
834 | 849 | """ Clears the "temporary text" buffer, i.e. all the text following |
@@ -862,7 +877,7 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
862 | 877 | def _complete_with_items(self, cursor, items): |
|
863 | 878 | """ Performs completion with 'items' at the specified cursor location. |
|
864 | 879 | """ |
|
865 |
self._cancel_ |
|
|
880 | self._cancel_completion() | |
|
866 | 881 | |
|
867 | 882 | if len(items) == 1: |
|
868 | 883 | cursor.setPosition(self._control.textCursor().position(), |
@@ -877,19 +892,25 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
877 | 892 | cursor.insertText(prefix) |
|
878 | 893 | current_pos = cursor.position() |
|
879 | 894 | |
|
880 | if self.gui_completion: | |
|
881 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(prefix)) | |
|
882 | self._completion_widget.show_items(cursor, items) | |
|
883 | else: | |
|
884 | cursor.beginEditBlock() | |
|
885 | self._append_plain_text('\n') | |
|
886 | self._page(self._format_as_columns(items)) | |
|
887 | cursor.endEditBlock() | |
|
895 | cursor.movePosition(QtGui.QTextCursor.Left, n=len(prefix)) | |
|
896 | self._completion_widget.show_items(cursor, items) | |
|
897 | self._is_completing = True | |
|
898 | ||
|
899 | ||
|
900 | def _fill_temporary_buffer(self, cursor, text, html=False): | |
|
901 | """fill the area below the active editting zone with text""" | |
|
902 | ||
|
903 | current_pos = self._control.textCursor().position() | |
|
904 | ||
|
905 | cursor.beginEditBlock() | |
|
906 | self._append_plain_text('\n') | |
|
907 | self._page(text, html=html) | |
|
908 | cursor.endEditBlock() | |
|
909 | ||
|
910 | cursor.setPosition(current_pos) | |
|
911 | self._control.moveCursor(QtGui.QTextCursor.End) | |
|
912 | self._control.setTextCursor(cursor) | |
|
888 | 913 | |
|
889 | cursor.setPosition(current_pos) | |
|
890 | self._control.moveCursor(QtGui.QTextCursor.End) | |
|
891 | self._control.setTextCursor(cursor) | |
|
892 | self._text_completing_pos = current_pos | |
|
893 | 914 | |
|
894 | 915 | def _context_menu_make(self, pos): |
|
895 | 916 | """ Creates a context menu for the given QPoint (in widget coordinates). |
@@ -951,7 +972,6 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
951 | 972 | control.viewport().installEventFilter(self) |
|
952 | 973 | |
|
953 | 974 | # Connect signals. |
|
954 | control.cursorPositionChanged.connect(self._cursor_position_changed) | |
|
955 | 975 | control.customContextMenuRequested.connect( |
|
956 | 976 | self._custom_context_menu_requested) |
|
957 | 977 | control.copyAvailable.connect(self.copy_available) |
@@ -1021,7 +1041,7 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
1021 | 1041 | intercepted = True |
|
1022 | 1042 | |
|
1023 | 1043 | # Special handling when tab completing in text mode. |
|
1024 |
self._cancel_ |
|
|
1044 | self._cancel_completion() | |
|
1025 | 1045 | |
|
1026 | 1046 | if self._in_buffer(position): |
|
1027 | 1047 | # Special handling when a reading a line of raw input. |
@@ -1634,8 +1654,8 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
1634 | 1654 | def _keyboard_quit(self): |
|
1635 | 1655 | """ Cancels the current editing task ala Ctrl-G in Emacs. |
|
1636 | 1656 | """ |
|
1637 |
if self._ |
|
|
1638 |
self._cancel_ |
|
|
1657 | if self._is_completing: | |
|
1658 | self._cancel_completion() | |
|
1639 | 1659 | else: |
|
1640 | 1660 | self.input_buffer = '' |
|
1641 | 1661 | |
@@ -1853,24 +1873,6 class ConsoleWidget(LoggingConfigurable, QtGui.QWidget): | |||
|
1853 | 1873 | if diff < 0 and document.blockCount() == document.maximumBlockCount(): |
|
1854 | 1874 | scrollbar.setValue(scrollbar.value() + diff) |
|
1855 | 1875 | |
|
1856 | def _cursor_position_changed(self): | |
|
1857 | """ Clears the temporary buffer based on the cursor position. | |
|
1858 | """ | |
|
1859 | if self._text_completing_pos: | |
|
1860 | document = self._control.document() | |
|
1861 | if self._text_completing_pos < document.characterCount(): | |
|
1862 | cursor = self._control.textCursor() | |
|
1863 | pos = cursor.position() | |
|
1864 | text_cursor = self._control.textCursor() | |
|
1865 | text_cursor.setPosition(self._text_completing_pos) | |
|
1866 | if pos < self._text_completing_pos or \ | |
|
1867 | cursor.blockNumber() > text_cursor.blockNumber(): | |
|
1868 | self._clear_temporary_buffer() | |
|
1869 | self._text_completing_pos = 0 | |
|
1870 | else: | |
|
1871 | self._clear_temporary_buffer() | |
|
1872 | self._text_completing_pos = 0 | |
|
1873 | ||
|
1874 | 1876 | def _custom_context_menu_requested(self, pos): |
|
1875 | 1877 | """ Shows a context menu at the given QPoint (in widget coordinates). |
|
1876 | 1878 | """ |
@@ -104,11 +104,14 qt_flags = { | |||
|
104 | 104 | 'plain' : ({'IPythonQtConsoleApp' : {'plain' : True}}, |
|
105 | 105 | "Disable rich text support."), |
|
106 | 106 | } |
|
107 | qt_flags.update(boolean_flag( | |
|
108 | 'gui-completion', 'ConsoleWidget.gui_completion', | |
|
109 | "use a GUI widget for tab completion", | |
|
110 | "use plaintext output for completion" | |
|
111 | )) | |
|
107 | ||
|
108 | # not quite sure on how this works | |
|
109 | #qt_flags.update(boolean_flag( | |
|
110 | # 'gui-completion', 'ConsoleWidget.gui_completion', | |
|
111 | # "use a GUI widget for tab completion", | |
|
112 | # "use plaintext output for completion" | |
|
113 | #)) | |
|
114 | ||
|
112 | 115 | # and app_flags from the Console Mixin |
|
113 | 116 | qt_flags.update(app_flags) |
|
114 | 117 | # add frontend flags to the full set |
@@ -117,7 +120,6 flags.update(qt_flags) | |||
|
117 | 120 | # start with copy of front&backend aliases list |
|
118 | 121 | aliases = dict(aliases) |
|
119 | 122 | qt_aliases = dict( |
|
120 | ||
|
121 | 123 | style = 'IPythonWidget.syntax_style', |
|
122 | 124 | stylesheet = 'IPythonQtConsoleApp.stylesheet', |
|
123 | 125 | colors = 'ZMQInteractiveShell.colors', |
@@ -127,6 +129,7 qt_aliases = dict( | |||
|
127 | 129 | ) |
|
128 | 130 | # and app_aliases from the Console Mixin |
|
129 | 131 | qt_aliases.update(app_aliases) |
|
132 | qt_aliases.update({'gui-completion':'ConsoleWidget.gui_completion'}) | |
|
130 | 133 | # add frontend aliases to the full set |
|
131 | 134 | aliases.update(qt_aliases) |
|
132 | 135 |
@@ -22,6 +22,7 default_light_style_template = ''' | |||
|
22 | 22 | .in-prompt-number { font-weight: bold; } |
|
23 | 23 | .out-prompt { color: darkred; } |
|
24 | 24 | .out-prompt-number { font-weight: bold; } |
|
25 | .inverted { background-color: %(fgcolor)s ; color:%(bgcolor)s;} | |
|
25 | 26 | ''' |
|
26 | 27 | default_light_style_sheet = default_light_style_template%dict( |
|
27 | 28 | bgcolor='white', fgcolor='black', select="#ccc") |
@@ -38,6 +39,7 default_dark_style_template = ''' | |||
|
38 | 39 | .in-prompt-number { color: lime; font-weight: bold; } |
|
39 | 40 | .out-prompt { color: red; } |
|
40 | 41 | .out-prompt-number { color: red; font-weight: bold; } |
|
42 | .inverted { background-color: %(fgcolor)s ; color:%(bgcolor)s;} | |
|
41 | 43 | ''' |
|
42 | 44 | default_dark_style_sheet = default_dark_style_template%dict( |
|
43 | 45 | bgcolor='black', fgcolor='white', select="#555") |
@@ -50,6 +52,7 default_bw_style_sheet = ''' | |||
|
50 | 52 | selection-background-color: #cccccc} |
|
51 | 53 | .in-prompt-number { font-weight: bold; } |
|
52 | 54 | .out-prompt-number { font-weight: bold; } |
|
55 | .inverted { background-color: black ; color: white;} | |
|
53 | 56 | ''' |
|
54 | 57 | default_bw_syntax_style = 'bw' |
|
55 | 58 |
General Comments 0
You need to be logged in to leave comments.
Login now