dingstudio / rpms / konsole

Forked from rpms/konsole a year ago
Clone

Blame SOURCES/konsole-avoid-pasting-control-characters.patch

bc4002
diff --git a/src/Emulation.cpp b/src/Emulation.cpp
bc4002
index 02ed4be..80d93ba 100644
bc4002
--- a/src/Emulation.cpp
bc4002
+++ b/src/Emulation.cpp
bc4002
@@ -39,6 +39,7 @@ Emulation::Emulation() :
bc4002
     _decoder(0),
bc4002
     _keyTranslator(0),
bc4002
     _usesMouse(false),
bc4002
+    _bracketedPasteMode(false),
bc4002
     _imageSizeInitialized(false)
bc4002
 {
bc4002
     // create screens with a default size
bc4002
@@ -52,6 +53,8 @@ Emulation::Emulation() :
bc4002
     // listen for mouse status changes
bc4002
     connect(this , SIGNAL(programUsesMouseChanged(bool)) ,
bc4002
             SLOT(usesMouseChanged(bool)));
bc4002
+    connect(this , SIGNAL(programBracketedPasteModeChanged(bool)) ,
bc4002
+            SLOT(bracketedPasteModeChanged(bool)));
bc4002
 }
bc4002
 
bc4002
 bool Emulation::programUsesMouse() const
bc4002
@@ -64,6 +67,16 @@ void Emulation::usesMouseChanged(bool usesMouse)
bc4002
     _usesMouse = usesMouse;
bc4002
 }
bc4002
 
bc4002
+bool Emulation::programBracketedPasteMode() const
bc4002
+{
bc4002
+    return _bracketedPasteMode;
bc4002
+}
bc4002
+
bc4002
+void Emulation::bracketedPasteModeChanged(bool bracketedPasteMode)
bc4002
+{
bc4002
+    _bracketedPasteMode = bracketedPasteMode;
bc4002
+}
bc4002
+
bc4002
 ScreenWindow* Emulation::createWindow()
bc4002
 {
bc4002
     ScreenWindow* window = new ScreenWindow();
bc4002
diff --git a/src/Emulation.h b/src/Emulation.h
bc4002
index aa24d69..966b423 100644
bc4002
--- a/src/Emulation.h
bc4002
+++ b/src/Emulation.h
bc4002
@@ -215,6 +215,8 @@ public:
bc4002
      */
bc4002
     bool programUsesMouse() const;
bc4002
 
bc4002
+    bool programBracketedPasteMode() const;
bc4002
+
bc4002
 public slots:
bc4002
 
bc4002
     /** Change the size of the emulation's image */
bc4002
@@ -314,6 +316,8 @@ signals:
bc4002
      */
bc4002
     void programUsesMouseChanged(bool usesMouse);
bc4002
 
bc4002
+    void programBracketedPasteModeChanged(bool bracketedPasteMode);
bc4002
+
bc4002
     /**
bc4002
      * Emitted when the contents of the screen image change.
bc4002
      * The emulation buffers the updates from successive image changes,
bc4002
@@ -474,8 +478,11 @@ private slots:
bc4002
 
bc4002
     void usesMouseChanged(bool usesMouse);
bc4002
 
bc4002
+    void bracketedPasteModeChanged(bool bracketedPasteMode);
bc4002
+
bc4002
 private:
bc4002
     bool _usesMouse;
bc4002
+    bool _bracketedPasteMode;
bc4002
     QTimer _bulkTimer1;
bc4002
     QTimer _bulkTimer2;
bc4002
     bool _imageSizeInitialized;
bc4002
diff --git a/src/Session.cpp b/src/Session.cpp
bc4002
index 2d8ca89..68e8ef7 100644
bc4002
--- a/src/Session.cpp
bc4002
+++ b/src/Session.cpp
bc4002
@@ -326,6 +326,11 @@ void Session::addView(TerminalDisplay* widget)
bc4002
 
bc4002
     widget->setUsesMouse(_emulation->programUsesMouse());
bc4002
 
bc4002
+    connect(_emulation, SIGNAL(programBracketedPasteModeChanged(bool)),
bc4002
+            widget, SLOT(setBracketedPasteMode(bool)));
bc4002
+
bc4002
+    widget->setBracketedPasteMode(_emulation->programBracketedPasteMode());
bc4002
+
bc4002
     widget->setScreenWindow(_emulation->createWindow());
bc4002
 
bc4002
     //connect view signals and slots
bc4002
diff --git a/src/TerminalDisplay.cpp b/src/TerminalDisplay.cpp
bc4002
index 871dc9f..be80e7c 100644
bc4002
--- a/src/TerminalDisplay.cpp
bc4002
+++ b/src/TerminalDisplay.cpp
bc4002
@@ -51,6 +51,7 @@
bc4002
 #include <KIO/NetAccess>
bc4002
 #include <konq_operations.h>
bc4002
 #include <KFileItem>
bc4002
+#include <KMessageBox>
bc4002
 
bc4002
 // Konsole
bc4002
 #include "Filter.h"
bc4002
@@ -367,6 +368,7 @@ TerminalDisplay::TerminalDisplay(QWidget* parent)
bc4002
     setMouseTracking(true);
bc4002
 
bc4002
     setUsesMouse(true);
bc4002
+    setBracketedPasteMode(false);
bc4002
 
bc4002
     setColorTable(ColorScheme::defaultTable);
bc4002
 
bc4002
@@ -2554,6 +2556,15 @@ bool TerminalDisplay::usesMouse() const
bc4002
     return _mouseMarks;
bc4002
 }
bc4002
 
bc4002
+void TerminalDisplay::setBracketedPasteMode(bool on)
bc4002
+{
bc4002
+    _bracketedPasteMode = on;
bc4002
+}
bc4002
+bool TerminalDisplay::bracketedPasteMode() const
bc4002
+{
bc4002
+    return _bracketedPasteMode;
bc4002
+}
bc4002
+
bc4002
 /* ------------------------------------------------------------------------- */
bc4002
 /*                                                                           */
bc4002
 /*                               Clipboard                                   */
bc4002
@@ -2568,8 +2579,84 @@ void TerminalDisplay::doPaste(QString text, bool appendReturn)
bc4002
     if (appendReturn)
bc4002
         text.append("\r");
bc4002
 
bc4002
+    QStringList unsafeCharacters;
bc4002
+    Q_FOREACH (const QChar &c, text) {
bc4002
+        if (!c.isPrint() && c != QLatin1Char('\t') && c != QLatin1Char('\n')) {
bc4002
+            QString description;
bc4002
+            switch(c.unicode()) {
bc4002
+            case '\x03':
bc4002
+                description = i18n("^C Interrupt: May abort the current process");
bc4002
+                break;
bc4002
+            case '\x04':
bc4002
+                description = i18n("^D End of transmission: May exit the current process");
bc4002
+                break;
bc4002
+            case '\x07':
bc4002
+                description = i18n("^G Bell: Will try to emit an audible warning");
bc4002
+                break;
bc4002
+            case '\x08':
bc4002
+                description = i18n("^H Backspace");
bc4002
+                break;
bc4002
+            case '\x13':
bc4002
+                description = i18n("^S Scroll lock: Locks terminal output");
bc4002
+                break;
bc4002
+            case '\x1a':
bc4002
+                description = i18n("^Z Suspend: Stops current process");
bc4002
+                break;
bc4002
+            case '\x1b':
bc4002
+                description = i18n("ESC: Used for special commands to the current process");
bc4002
+                break;
bc4002
+            default:
bc4002
+                description = i18n("Other unprintable character (\\x%1)").arg(QString::number(c.unicode(), 16));
bc4002
+                break;
bc4002
+            }
bc4002
+            unsafeCharacters.append(description);
bc4002
+        }
bc4002
+    }
bc4002
+    unsafeCharacters.removeDuplicates();
bc4002
+
bc4002
+    if (!unsafeCharacters.isEmpty()) {
bc4002
+        int result = KMessageBox::warningYesNoCancelList(window(),
bc4002
+                i18n("The text you're trying to paste contains hidden unprintable characters, "
bc4002
+                    "do you want to filter them out?"),
bc4002
+                unsafeCharacters,
bc4002
+                i18nc("@title", "Filter"),
bc4002
+                KGuiItem(i18nc("@action:button",
bc4002
+                    "&Remove unprintable"),
bc4002
+                    QLatin1String("filter-symbolic")),
bc4002
+                KGuiItem(i18nc("@action:button",
bc4002
+                    "Confirm &paste"),
bc4002
+                    QLatin1String("edit-paste")),
bc4002
+                KGuiItem(i18nc("@action:button",
bc4002
+                    "&Cancel paste"),
bc4002
+                    QLatin1String("dialog-cancel")),
bc4002
+                QLatin1String("ShowPasteUnprintableWarning")
bc4002
+            );
bc4002
+        switch(result){
bc4002
+        case KMessageBox::Cancel:
bc4002
+            return;
bc4002
+        case KMessageBox::Yes: {
bc4002
+            QString sanitized;
bc4002
+            Q_FOREACH (const QChar &c, text) {
bc4002
+                if (c.isPrint() || c == QLatin1Char('\t') || c == QLatin1Char('\n')) {
bc4002
+                    sanitized.append(c);
bc4002
+                }
bc4002
+            }
bc4002
+            text = sanitized;
bc4002
+        }
bc4002
+        case KMessageBox::No:
bc4002
+            break;
bc4002
+        default:
bc4002
+            break;
bc4002
+        }
bc4002
+    }
bc4002
+
bc4002
     if (!text.isEmpty()) {
bc4002
         text.replace('\n', '\r');
bc4002
+        if (bracketedPasteMode()) {
bc4002
+            text.remove(QLatin1String("\033"));
bc4002
+            text.prepend("\033[200~");
bc4002
+            text.append("\033[201~");
bc4002
+        }
bc4002
         // perform paste by simulating keypress events
bc4002
         QKeyEvent e(QEvent::KeyPress, 0, Qt::NoModifier, text);
bc4002
         emit keyPressedSignal(&e);
bc4002
diff --git a/src/TerminalDisplay.h b/src/TerminalDisplay.h
bc4002
index 8e246b8..23913d1 100644
bc4002
--- a/src/TerminalDisplay.h
bc4002
+++ b/src/TerminalDisplay.h
bc4002
@@ -512,6 +512,9 @@ public slots:
bc4002
     /** See setUsesMouse() */
bc4002
     bool usesMouse() const;
bc4002
 
bc4002
+    void setBracketedPasteMode(bool bracketedPasteMode);
bc4002
+    bool bracketedPasteMode() const;
bc4002
+
bc4002
     /**
bc4002
      * Shows a notification that a bell event has occurred in the terminal.
bc4002
      * TODO: More documentation here
bc4002
@@ -770,6 +773,7 @@ private:
bc4002
     bool _showTerminalSizeHint;
bc4002
     bool _bidiEnabled;
bc4002
     bool _mouseMarks;
bc4002
+    bool _bracketedPasteMode;
bc4002
 
bc4002
     QPoint  _iPntSel; // initial selection point
bc4002
     QPoint  _pntSel; // current selection point
bc4002
diff --git a/src/Vt102Emulation.cpp b/src/Vt102Emulation.cpp
bc4002
index 7f663f2..76b3ac5 100644
bc4002
--- a/src/Vt102Emulation.cpp
bc4002
+++ b/src/Vt102Emulation.cpp
bc4002
@@ -787,6 +787,11 @@ void Vt102Emulation::processToken(int token, int p, int q)
bc4002
     case TY_CSI_PR('h', 1049) : saveCursor(); _screen[1]->clearEntireScreen(); setMode(MODE_AppScreen); break; //XTERM
bc4002
     case TY_CSI_PR('l', 1049) : resetMode(MODE_AppScreen); restoreCursor(); break; //XTERM
bc4002
 
bc4002
+    case TY_CSI_PR('h', 2004) :          setMode      (MODE_BracketedPaste); break; //XTERM
bc4002
+    case TY_CSI_PR('l', 2004) :        resetMode      (MODE_BracketedPaste); break; //XTERM
bc4002
+    case TY_CSI_PR('s', 2004) :         saveMode      (MODE_BracketedPaste); break; //XTERM
bc4002
+    case TY_CSI_PR('r', 2004) :      restoreMode      (MODE_BracketedPaste); break; //XTERM
bc4002
+
bc4002
     //FIXME: weird DEC reset sequence
bc4002
     case TY_CSI_PE('p'      ) : /* IGNORED: reset         (        ) */ break;
bc4002
 
bc4002
@@ -1194,6 +1199,7 @@ void Vt102Emulation::resetModes()
bc4002
     resetMode(MODE_Mouse1005);  saveMode(MODE_Mouse1005);
bc4002
     resetMode(MODE_Mouse1006);  saveMode(MODE_Mouse1006);
bc4002
     resetMode(MODE_Mouse1015);  saveMode(MODE_Mouse1015);
bc4002
+    resetMode(MODE_BracketedPaste);  saveMode(MODE_BracketedPaste);
bc4002
 
bc4002
     resetMode(MODE_AppScreen);  saveMode(MODE_AppScreen);
bc4002
     resetMode(MODE_AppCuKeys);  saveMode(MODE_AppCuKeys);
bc4002
@@ -1219,6 +1225,10 @@ void Vt102Emulation::setMode(int m)
bc4002
         emit programUsesMouseChanged(false);
bc4002
         break;
bc4002
 
bc4002
+    case MODE_BracketedPaste:
bc4002
+        emit programBracketedPasteModeChanged(true);
bc4002
+        break;
bc4002
+
bc4002
     case MODE_AppScreen :
bc4002
         _screen[1]->clearSelection();
bc4002
         setScreen(1);
bc4002
@@ -1245,6 +1255,10 @@ void Vt102Emulation::resetMode(int m)
bc4002
         emit programUsesMouseChanged(true);
bc4002
         break;
bc4002
 
bc4002
+    case MODE_BracketedPaste:
bc4002
+        emit programBracketedPasteModeChanged(false);
bc4002
+        break;
bc4002
+
bc4002
     case MODE_AppScreen :
bc4002
         _screen[0]->clearSelection();
bc4002
         setScreen(0);
bc4002
diff --git a/src/Vt102Emulation.h b/src/Vt102Emulation.h
bc4002
index c15f6ea..8d678e0 100644
bc4002
--- a/src/Vt102Emulation.h
bc4002
+++ b/src/Vt102Emulation.h
bc4002
@@ -46,7 +46,8 @@ class QKeyEvent;
bc4002
 #define MODE_Ansi            (MODES_SCREEN+10)   // Use US Ascii for character sets G0-G3 (DECANM)
bc4002
 #define MODE_132Columns      (MODES_SCREEN+11)  // 80 <-> 132 column mode switch (DECCOLM)
bc4002
 #define MODE_Allow132Columns (MODES_SCREEN+12)  // Allow DECCOLM mode
bc4002
-#define MODE_total           (MODES_SCREEN+13)
bc4002
+#define MODE_BracketedPaste  (MODES_SCREEN+13)  // Xterm-style bracketed paste mode
bc4002
+#define MODE_total           (MODES_SCREEN+14)
bc4002
 
bc4002
 namespace Konsole
bc4002
 {