teknoraver icon

konsole copy

teknoraver | PRO | 11/10/21 12:18:08 AM UTC (Edited) | 0 ⭐ | 1573 👁️ | Never ⏰ | []
Diff |

7.6 KB

|

None

|

0 👍

/

0 👎

From c5cdd0f2573dc84d24c0f1ee37f2badbf468bc6a Mon Sep 17 00:00:00 2001
From: Matteo Croce <[email protected]>
Date: Wed, 10 Nov 2021 01:22:04 +0100
Subject: [PATCH] copy selected text to clipboard with ^C
 
Pressing ^C usually sends a SIGINT to the shell.
When the selection is not empty, trap ^C and copy the selection
to the clipboard instead.
 
Make this user configurable and disabled by default.
---
 src/profile/Profile.cpp                 |  2 ++
 src/profile/Profile.h                   | 11 +++++++++++
 src/terminalDisplay/TerminalDisplay.cpp |  7 +++++++
 src/widgets/EditProfileAdvancedPage.ui  | 24 ++++++++++++++++++++----
 src/widgets/EditProfileDialog.cpp       |  7 +++++++
 src/widgets/EditProfileDialog.h         |  1 +
 6 files changed, 48 insertions(+), 4 deletions(-)
 
diff --git a/src/profile/Profile.cpp b/src/profile/Profile.cpp
index 09eaf50e..3e30700b 100644
--- a/src/profile/Profile.cpp
+++ b/src/profile/Profile.cpp
@@ -97,6 +97,7 @@ const Profile::PropertyInfo Profile::DefaultPropertyNames[] = {
     {FlowControlEnabled, "FlowControlEnabled", TERMINAL_GROUP, QVariant::Bool},
     {BidiRenderingEnabled, "BidiRenderingEnabled", TERMINAL_GROUP, QVariant::Bool},
     {BlinkingCursorEnabled, "BlinkingCursorEnabled", TERMINAL_GROUP, QVariant::Bool},
+    {CtrlCCopy, "CtrlCCopy", TERMINAL_GROUP, QVariant::Bool},
     {BellMode, "BellMode", TERMINAL_GROUP, QVariant::Int},
     {VerticalLine, "VerticalLine", TERMINAL_GROUP, QVariant::Bool},
     {VerticalLineAtChar, "VerticalLineAtChar", TERMINAL_GROUP, QVariant::Int},
@@ -210,6 +211,7 @@ void Profile::useFallback()
     setProperty(UnderlineLinksEnabled, true);
     setProperty(UnderlineFilesEnabled, false);
     setProperty(OpenLinksByDirectClickEnabled, false);
+    setProperty(CtrlCCopy, false);
 
     setProperty(TextEditorCmd, Enum::Kate);
     setProperty(TextEditorCmdCustom, QStringLiteral("kate PATH:LINE:COLUMN"));
diff --git a/src/profile/Profile.h b/src/profile/Profile.h
index 6dc3c30f..e107052c 100644
--- a/src/profile/Profile.h
+++ b/src/profile/Profile.h
@@ -345,6 +345,11 @@ public:
          *  when hovered by the mouse pointer.
          */
         ColorFilterEnabled,
+
+        /** (bool) If true, if the selection is not empty, Ctrl+C will copy the selection
+         * instead of sending a SIGINT signal to the shell.
+         */
+        CtrlCCopy,
     };
 
     Q_ENUM(Property)
@@ -564,6 +569,12 @@ public:
         return property<bool>(Profile::BidiRenderingEnabled);
     }
 
+    /** Convenience method for property<bool>(Profile::CtrlCCopy) */
+    bool ctrlCCopyEnabled() const
+    {
+        return property<bool>(Profile::CtrlCCopy);
+    }
+
     /** Convenience method for property<bool>(Profile::LineSpacing) */
     int lineSpacing() const
     {
diff --git a/src/terminalDisplay/TerminalDisplay.cpp b/src/terminalDisplay/TerminalDisplay.cpp
index 6270d2d5..fda37417 100644
--- a/src/terminalDisplay/TerminalDisplay.cpp
+++ b/src/terminalDisplay/TerminalDisplay.cpp
@@ -2452,6 +2452,13 @@ void TerminalDisplay::keyPressEvent(QKeyEvent *event)
         }
     }
 
+    auto profile = SessionManager::instance()->sessionProfile(_sessionController->session());
+    if (profile->ctrlCCopyEnabled() && event->key() == 'C' && event->modifiers() == Qt::ControlModifier
+        && !_screenWindow->selectedText(currentDecodingOptions()).isEmpty()) {
+        copyToClipboard();
+        _screenWindow->clearSelection();
+        return;
+    }
     Q_EMIT keyPressedSignal(event);
 
 #ifndef QT_NO_ACCESSIBILITY
diff --git a/src/widgets/EditProfileAdvancedPage.ui b/src/widgets/EditProfileAdvancedPage.ui
index 12c52d01..f8962c14 100644
--- a/src/widgets/EditProfileAdvancedPage.ui
+++ b/src/widgets/EditProfileAdvancedPage.ui
@@ -235,6 +235,22 @@
       </widget>
      </item>
      <item row="6" column="1">
+      <widget class="QCheckBox" name="enableCtrlCCopyButton">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="toolTip">
+        <string>When selection is not empty, Ctrl+C will copy selected text instead of sending a SIGINT signal to the shell</string>
+       </property>
+       <property name="text">
+        <string>Copy selection with Ctrl+C</string>
+       </property>
+      </widget>
+     </item>
+     <item row="7" column="1">
       <spacer>
        <property name="orientation">
         <enum>Qt::Vertical</enum>
@@ -250,7 +266,7 @@
        </property>
       </spacer>
      </item>
-     <item row="7" column="0" alignment="Qt::AlignRight">
+     <item row="8" column="0" alignment="Qt::AlignRight">
       <widget class="QLabel" name="label_14">
        <property name="text">
         <string>Default character encoding:</string>
@@ -263,14 +279,14 @@
        </property>
       </widget>
      </item>
-     <item row="7" column="1">
+     <item row="8" column="1">
       <widget class="QPushButton" name="selectEncodingButton">
        <property name="text">
         <string comment="KDE::DoNotExtract">DEFAULTENCODING</string>
        </property>
       </widget>
      </item>
-     <item row="8" column="0" alignment="Qt::AlignRight">
+     <item row="9" column="0" alignment="Qt::AlignRight">
       <widget class="QLabel" name="peekPrimaryLabel">
        <property name="text">
         <string>Shortcut for peeking the primary screen:</string>
@@ -280,7 +296,7 @@
        </property>
       </widget>
      </item>
-     <item row="8" column="1" alignment="Qt::AlignRight">
+     <item row="9" column="1" alignment="Qt::AlignRight">
       <widget class="QKeySequenceEdit" name="peekPrimaryWidget">
       </widget>
      </item>
diff --git a/src/widgets/EditProfileDialog.cpp b/src/widgets/EditProfileDialog.cpp
index 9c856ee6..d5b29653 100644
--- a/src/widgets/EditProfileDialog.cpp
+++ b/src/widgets/EditProfileDialog.cpp
@@ -1771,6 +1771,8 @@ void EditProfileDialog::setupAdvancedPage(const Profile::Ptr &profile)
     connect(_advancedUi->enableBidiRenderingButton, &QPushButton::toggled, this, &EditProfileDialog::togglebidiRendering);
     _advancedUi->enableReverseUrlHints->setChecked(profile->property<bool>(Profile::ReverseUrlHints));
     connect(_advancedUi->enableReverseUrlHints, &QPushButton::toggled, this, &EditProfileDialog::toggleReverseUrlHints);
+    _advancedUi->enableCtrlCCopyButton->setChecked(profile->property<bool>(Profile::CtrlCCopy));
+    connect(_advancedUi->enableCtrlCCopyButton, &QPushButton::toggled, this, &EditProfileDialog::toggleCtrlCCopy);
 
     // Setup the URL hints modifier checkboxes
     {
@@ -1957,6 +1959,11 @@ void EditProfileDialog::toggleFlowControl(bool enable)
     updateTempProfileProperty(Profile::FlowControlEnabled, enable);
 }
 
+void EditProfileDialog::toggleCtrlCCopy(bool enable)
+{
+    updateTempProfileProperty(Profile::CtrlCCopy, enable);
+}
+
 void EditProfileDialog::peekPrimaryKeySequenceChanged()
 {
     updateTempProfileProperty(Profile::PeekPrimaryKeySequence, _advancedUi->peekPrimaryWidget->keySequence().toString());
diff --git a/src/widgets/EditProfileDialog.h b/src/widgets/EditProfileDialog.h
index 8ed10549..5a3bfa9e 100644
--- a/src/widgets/EditProfileDialog.h
+++ b/src/widgets/EditProfileDialog.h
@@ -205,6 +205,7 @@ private Q_SLOTS:
     void togglebidiRendering(bool);
     void updateUrlHintsModifier(bool);
     void toggleReverseUrlHints(bool);
+    void toggleCtrlCCopy(bool);
 
     void setDefaultCodec(QTextCodec *);
 
-- 
2.33.1
 
 

Comments