Created
October 7, 2015 16:37
-
-
Save bergtholdt/9ca387772d13da0b2c1a to your computer and use it in GitHub Desktop.
qtconsole Copy/Paste Regressions #48
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From a4453ad198cbfbbf391d5dd134d1396700c7d1ea Mon Sep 17 00:00:00 2001 | |
From: Martin Bergtholdt <martin.bergtholdt@philips.com> | |
Date: Wed, 7 Oct 2015 18:33:35 +0200 | |
Subject: [PATCH] fix Copy/Paste Regressions #48 | |
--- | |
qtconsole/frontend_widget.py | 40 ++++++++++++++++++++++++++++++++++++++-- | |
1 file changed, 38 insertions(+), 2 deletions(-) | |
diff --git a/qtconsole/frontend_widget.py b/qtconsole/frontend_widget.py | |
index 6d7d0ef..7cca7ea 100644 | |
--- a/qtconsole/frontend_widget.py | |
+++ b/qtconsole/frontend_widget.py | |
@@ -12,6 +12,7 @@ try: | |
from queue import Empty | |
except ImportError: | |
from Queue import Empty | |
+import re | |
from qtconsole import qt | |
from qtconsole.qt import QtCore, QtGui | |
@@ -25,6 +26,35 @@ from .call_tip_widget import CallTipWidget | |
from .history_console_widget import HistoryConsoleWidget | |
from .pygments_highlighter import PygmentsHighlighter | |
+_classic_prompt_re = re.compile(r'^([ \t]*>>> |^[ \t]*\.\.\. )') | |
+ | |
+def transform_classic_prompt(line): | |
+ """Handle inputs that start with '>>> ' syntax.""" | |
+ | |
+ if not line or line.isspace(): | |
+ return line | |
+ m = _classic_prompt_re.match(line) | |
+ if m: | |
+ return line[len(m.group(0)):] | |
+ else: | |
+ return line | |
+ | |
+ | |
+_ipy_prompt_re = re.compile(r'^([ \t]*In \[\d+\]: |^[ \t]*\ \ \ \.\.\.+: )') | |
+ | |
+def transform_ipy_prompt(line): | |
+ """Handle inputs that start classic IPython prompt syntax.""" | |
+ | |
+ if not line or line.isspace(): | |
+ return line | |
+ #print 'LINE: %r' % line # dbg | |
+ m = _ipy_prompt_re.match(line) | |
+ if m: | |
+ #print 'MATCH! %r -> %r' % (line, line[len(m.group(0)):]) # dbg | |
+ return line[len(m.group(0)):] | |
+ else: | |
+ return line | |
+ | |
class FrontendHighlighter(PygmentsHighlighter): | |
""" A PygmentsHighlighter that understands and ignores prompts. | |
@@ -213,10 +243,16 @@ class FrontendWidget(HistoryConsoleWidget, BaseFrontendMixin): | |
elif self._control.hasFocus(): | |
text = self._control.textCursor().selection().toPlainText() | |
if text: | |
+ # Remove prompts. | |
+ lines = text.splitlines() | |
+ lines = map(transform_classic_prompt, lines) | |
+ lines = map(transform_ipy_prompt, lines) | |
+ text = '\n'.join(lines) | |
was_newline = text[-1] == '\n' | |
- if not was_newline: # user doesn't need newline | |
+ if was_newline: # user doesn't need newline | |
text = text[:-1] | |
- QtGui.QApplication.clipboard().setText(text) | |
+ # Expand tabs so that we respect PEP-8. | |
+ QtGui.QApplication.clipboard().setText(text.expandtabs(4)) | |
else: | |
self.log.debug("frontend widget : unknown copy target") | |
-- | |
2.1.0.9738.g8768113 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment