From 4abe4d186c225e85b8cfa97983b55bd727766b7a Mon Sep 17 00:00:00 2001
From: mae <qt-info@nokia.com>
Date: Thu, 24 Sep 2009 13:39:45 +0200
Subject: [PATCH] In auto-indent mode, also indent pasted text.

---
 src/plugins/texteditor/basetexteditor.cpp | 33 ++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index b5e47fa6be3..0ec24aef20f 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -4475,7 +4475,38 @@ void BaseTextEditor::insertFromMimeData(const QMimeData *source)
         ensureCursorVisible();
         return;
     }
-    QPlainTextEdit::insertFromMimeData(source);
+
+    QString text = source->text();
+    if (text.isEmpty())
+        return;
+
+    const TabSettings &ts = d->m_document->tabSettings();
+    QTextCursor cursor = textCursor();
+    if (cursor.hasSelection() || !ts.m_autoIndent) {
+        cursor.beginEditBlock();
+        cursor.insertText(text);
+        cursor.endEditBlock();
+        setTextCursor(cursor);
+        return;
+    }
+
+    cursor.beginEditBlock();
+    int bpos = cursor.block().position();
+    int pos = cursor.position() - bpos;
+    int fns = ts.firstNonSpace(cursor.block().text());
+
+    cursor.insertText(text);
+
+    if (pos <= fns) { // cursor was at beginning of line, do reindent
+        pos = cursor.position();
+        cursor.setPosition(bpos);
+        cursor.setPosition(pos, QTextCursor::KeepAnchor);
+        indent(document(), cursor, QChar::Null);
+        cursor.clearSelection();
+    }
+
+    cursor.endEditBlock();
+    setTextCursor(cursor);
 }
 
 BaseTextEditorEditable::BaseTextEditorEditable(BaseTextEditor *editor)
-- 
GitLab