# HG changeset patch
# User Stefan Miereis <stefan.miereis@gmx.de>
# Date 1455236623 -3600
#      Fri Feb 12 01:23:43 2016 +0100
# Node ID e5a56f884b59aa51be489b73c5bcfa2e421b46ce
# Parent  a032c6708f5ce8373e37ef058cce96634c7ff89d
Make command line mouse compatible (bug #45003)

diff -r a032c6708f5c -r e5a56f884b59 libgui/qterminal/libqterminal/unix/TerminalView.cpp
--- a/libgui/qterminal/libqterminal/unix/TerminalView.cpp	Thu Feb 11 17:05:47 2016 -0500
+++ b/libgui/qterminal/libqterminal/unix/TerminalView.cpp	Fri Feb 12 01:23:43 2016 +0100
@@ -1556,6 +1556,40 @@
   getCharacterPosition(ev->pos(),charLine,charColumn);
   QPoint pos = QPoint(charColumn,charLine);
 
+  // reposition cursor if mouseclick happens in the currently editable line
+  QVector<LineProperty> lineprop = _screenWindow->getLineProperties();
+  int lineStart = _screenWindow->cursorPosition().y();
+  while ( lineprop[lineStart-1] & LINE_WRAPPED ) {
+    lineStart--;
+  }
+  if ( (charLine >= lineStart) )
+    {
+      int posInLineCursor = _screenWindow->cursorPosition().x()
+                            + qMax(0,(_screenWindow->cursorPosition().y()-lineStart) )
+                            * _screenWindow->windowColumns();
+      int posInLineClick = charColumn
+                           + qMax(0, (charLine - lineStart) ) * _screenWindow->windowColumns();
+
+      if ( posInLineCursor > posInLineClick )
+        {
+          QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,Qt::Key_Left,
+                                           Qt::NoModifier,QString(""));
+
+          for (int i=0; i< posInLineCursor - posInLineClick; i++) {
+            emit keyPressedSignal(event);
+          }
+        }
+      else if ( posInLineClick > posInLineCursor )
+        {
+          QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,Qt::Key_Right,
+                                           Qt::NoModifier,QString(""));
+
+          for (int i=0; i<posInLineClick - posInLineCursor; i++) {
+            emit keyPressedSignal(event);
+          }
+        }
+    }
+
   if ( ev->button() == Qt::LeftButton)
     {
       _lineSelectionMode = false;
@@ -2387,8 +2421,50 @@
     }
 
   if ( emitKeyPressSignal && !_readonly )
-    emit keyPressedSignal(event);
-
+    {
+      //Clear selection if the cursor is moved with arrow keys
+      bool emitKey = true;
+      if ( (event->modifiers() == Qt::NoModifier) && (event->key() == Qt::Key_Right || event->key() == Qt::Key_Left) )
+        {
+          _screenWindow->clearSelection();
+        }
+      //Delete selected text if printable key is pressed
+      if ( !event->text().isEmpty() )
+        {
+          int lineStart, colStart, lineEnd, colEnd;
+          _screenWindow->getSelectionStart(colStart, lineStart);
+          _screenWindow->getSelectionEnd(colEnd, lineEnd);
+          int cursorLoc = loc(_screenWindow->cursorPosition().x(), _screenWindow->cursorPosition().y() );
+
+          //Check if there is a selection in the current line
+          if ( (loc(colStart,lineStart) != loc(colEnd, lineEnd))
+               && (cursorLoc >=loc(colStart,lineStart))
+               && (cursorLoc-1 <= loc(colEnd,lineEnd)) )
+            {
+              QKeyEvent *ev = new QKeyEvent(QEvent::KeyPress,Qt::Key_Backspace,
+                                            Qt::NoModifier,QString(""));
+
+              if ( cursorLoc < loc(colEnd, lineEnd) ) {
+                QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,Qt::Key_Right,
+                                                 Qt::NoModifier,QString(""));
+                for (int i=0; i<loc(colEnd,lineEnd) - cursorLoc + 1; i++)
+                  emit keyPressedSignal(event);
+              }
+
+              for (int i = 0; i < loc(colEnd, lineEnd) - loc(colStart, lineStart) + 1; i++)
+                emit keyPressedSignal(ev);
+
+              // Backspace deleted the selected text and has done its duty, no need to call it again
+              if (event->key() == Qt::Key_Backspace) {
+                _screenWindow->clearSelection();
+                emitKey = false;
+              }
+            }
+        }
+      if (emitKey) {
+        emit keyPressedSignal(event);
+      }
+    }
   if (_readonly) {
       event->ignore();
     }
