##// END OF EJS Templates
Removed use of hard tabs in FrontendWidget and implemented "smart" backspace.
epatters -
Show More
@@ -1163,8 +1163,8 class ConsoleWidget(Configurable, QtGui.QWidget):
1163 return cursor.columnNumber() - len(prompt)
1163 return cursor.columnNumber() - len(prompt)
1164
1164
1165 def _get_input_buffer_cursor_line(self):
1165 def _get_input_buffer_cursor_line(self):
1166 """ Returns line of the input buffer that contains the cursor, or None
1166 """ Returns the text of the line of the input buffer that contains the
1167 if there is no such line.
1167 cursor, or None if there is no such line.
1168 """
1168 """
1169 prompt = self._get_input_buffer_cursor_prompt()
1169 prompt = self._get_input_buffer_cursor_prompt()
1170 if prompt is None:
1170 if prompt is None:
@@ -147,11 +147,9 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
147 """
147 """
148 text = str(self._control.textCursor().selection().toPlainText())
148 text = str(self._control.textCursor().selection().toPlainText())
149 if text:
149 if text:
150 # Remove prompts.
151 lines = map(transform_classic_prompt, text.splitlines())
150 lines = map(transform_classic_prompt, text.splitlines())
152 text = '\n'.join(lines)
151 text = '\n'.join(lines)
153 # Expand tabs so that we respect PEP-8.
152 QtGui.QApplication.clipboard().setText(text)
154 QtGui.QApplication.clipboard().setText(text.expandtabs(4))
155
153
156 #---------------------------------------------------------------------------
154 #---------------------------------------------------------------------------
157 # 'ConsoleWidget' abstract interface
155 # 'ConsoleWidget' abstract interface
@@ -162,7 +160,7 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
162 prompt created. When triggered by an Enter/Return key press,
160 prompt created. When triggered by an Enter/Return key press,
163 'interactive' is True; otherwise, it is False.
161 'interactive' is True; otherwise, it is False.
164 """
162 """
165 complete = self._input_splitter.push(source.expandtabs(4))
163 complete = self._input_splitter.push(source)
166 if interactive:
164 if interactive:
167 complete = not self._input_splitter.push_accepts_more()
165 complete = not self._input_splitter.push_accepts_more()
168 return complete
166 return complete
@@ -220,26 +218,43 class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
220 return menu
218 return menu
221
219
222 def _event_filter_console_keypress(self, event):
220 def _event_filter_console_keypress(self, event):
223 """ Reimplemented to allow execution interruption.
221 """ Reimplemented for execution interruption and smart backspace.
224 """
222 """
225 key = event.key()
223 key = event.key()
226 if self._control_key_down(event.modifiers(), include_command=False):
224 if self._control_key_down(event.modifiers(), include_command=False):
225
227 if key == QtCore.Qt.Key_C and self._executing:
226 if key == QtCore.Qt.Key_C and self._executing:
228 self.interrupt_kernel()
227 self.interrupt_kernel()
229 return True
228 return True
229
230 elif key == QtCore.Qt.Key_Period:
230 elif key == QtCore.Qt.Key_Period:
231 message = 'Are you sure you want to restart the kernel?'
231 message = 'Are you sure you want to restart the kernel?'
232 self.restart_kernel(message, instant_death=False)
232 self.restart_kernel(message, instant_death=False)
233 return True
233 return True
234
235 elif not event.modifiers() & QtCore.Qt.AltModifier:
236
237 # Smart backspace: remove four characters in one backspace if:
238 # 1) everything left of the cursor is whitespace
239 # 2) the four characters immediately left of the cursor are spaces
240 if key == QtCore.Qt.Key_Backspace:
241 col = self._get_input_buffer_cursor_column()
242 cursor = self._control.textCursor()
243 if col > 3 and not cursor.hasSelection():
244 text = self._get_input_buffer_cursor_line()[:col]
245 if text.endswith(' ') and not text.strip():
246 cursor.movePosition(QtGui.QTextCursor.Left,
247 QtGui.QTextCursor.KeepAnchor, 4)
248 cursor.removeSelectedText()
249 return True
250
234 return super(FrontendWidget, self)._event_filter_console_keypress(event)
251 return super(FrontendWidget, self)._event_filter_console_keypress(event)
235
252
236 def _insert_continuation_prompt(self, cursor):
253 def _insert_continuation_prompt(self, cursor):
237 """ Reimplemented for auto-indentation.
254 """ Reimplemented for auto-indentation.
238 """
255 """
239 super(FrontendWidget, self)._insert_continuation_prompt(cursor)
256 super(FrontendWidget, self)._insert_continuation_prompt(cursor)
240 spaces = self._input_splitter.indent_spaces
257 cursor.insertText(' ' * self._input_splitter.indent_spaces)
241 cursor.insertText('\t' * (spaces / self.tab_width))
242 cursor.insertText(' ' * (spaces % self.tab_width))
243
258
244 #---------------------------------------------------------------------------
259 #---------------------------------------------------------------------------
245 # 'BaseFrontendMixin' abstract interface
260 # 'BaseFrontendMixin' abstract interface
@@ -208,11 +208,9 class IPythonWidget(FrontendWidget):
208 """
208 """
209 text = str(self._control.textCursor().selection().toPlainText())
209 text = str(self._control.textCursor().selection().toPlainText())
210 if text:
210 if text:
211 # Remove prompts.
212 lines = map(transform_ipy_prompt, text.splitlines())
211 lines = map(transform_ipy_prompt, text.splitlines())
213 text = '\n'.join(lines)
212 text = '\n'.join(lines)
214 # Expand tabs so that we respect PEP-8.
213 QtGui.QApplication.clipboard().setText(text)
215 QtGui.QApplication.clipboard().setText(text.expandtabs(4))
216
214
217 #---------------------------------------------------------------------------
215 #---------------------------------------------------------------------------
218 # 'FrontendWidget' public interface
216 # 'FrontendWidget' public interface
General Comments 0
You need to be logged in to leave comments. Login now