fix for any position
@@ -215,24 +215,37 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin):
elif self._control.hasFocus():
text = self._control.textCursor().selection().toPlainText()
if text:
- # Adjust the cursor to not take partial prompts
+ first_line_selection, *remaining_lines = text.splitlines()
+
+ # Get preceding text
cursor = self._control.textCursor()
- start_pos = cursor.selectionStart()
- cursor.setPosition(start_pos)
- if (cursor.blockNumber() >=
- self._get_prompt_cursor().blockNumber()):
- # Only remove partial prompt if in edit area
- line_prompt_pos = (
- cursor.block().position()
- + len(self._continuation_prompt))
- if start_pos < line_prompt_pos:
- text = text[(line_prompt_pos - start_pos):]
-
- # Remove prompts.
- lines = text.splitlines()
- lines = map(self._highlighter.transform_classic_prompt, lines)
- lines = map(self._highlighter.transform_ipy_prompt, lines)
- text = '\n'.join(lines)
+ cursor.setPosition(cursor.selectionStart())
+ cursor.setPosition(cursor.block().position(),
+ QtGui.QTextCursor.KeepAnchor)
+ preceding_text = cursor.selectedText()
+
+ # Get first line promp len
+ first_line = preceding_text + first_line_selection
+ len_with_prompt = len(first_line)
+ first_line = self._highlighter.transform_classic_prompt(
+ first_line)
+ first_line = self._highlighter.transform_ipy_prompt(
+ first_line)
+ prompt_len = len_with_prompt - len(first_line)
+
+ # Remove not selected part
+ if prompt_len < len(preceding_text):
+ first_line = first_line[len(preceding_text)-prompt_len:]
+
+ # Remove prompts for other lines.
+ remaining_lines = map(
+ self._highlighter.transform_classic_prompt,
+ remaining_lines)
+ remaining_lines = map(
+ self._highlighter.transform_ipy_prompt,
+ remaining_lines)
+ text = '\n'.join([first_line, *remaining_lines])
+
# Needed to prevent errors when copying the prompt.
# See issue 264
try:
