diff --git a/src/libs/cplusplus/TypeOfExpression.cpp b/src/libs/cplusplus/TypeOfExpression.cpp
index 53fa7e78396551314ddedc84607676946cf73e9d..68732f78ea09d31103c2329a2157c973e8bdb435 100644
--- a/src/libs/cplusplus/TypeOfExpression.cpp
+++ b/src/libs/cplusplus/TypeOfExpression.cpp
@@ -107,6 +107,8 @@ void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents
                                           Document::Ptr doc, Environment *env,
                                           QSet<QString> *processed) const
 {
+    if (! doc)
+        return;
     if (processed->contains(doc->fileName()))
         return;
     processed->insert(doc->fileName());
diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp
index e3bf12757bd3e479c7ad9a198f1efe7e746358e2..acf55f6d93fb78d36be21f9c3f9ac2372b43b8a0 100644
--- a/src/plugins/cppeditor/cppeditor.cpp
+++ b/src/plugins/cppeditor/cppeditor.cpp
@@ -744,7 +744,8 @@ void CPPEditor::unCommentSelection()
 
         QString endText = endBlock.text();
         int endPos = end - endBlock.position();
-        bool hasTrailingCharacters = !endText.mid(endPos).trimmed().isEmpty();
+        bool hasTrailingCharacters = !endText.left(endPos).remove(QLatin1String("//")).trimmed().isEmpty()
+                                     && !endText.mid(endPos).trimmed().isEmpty();
         if ((endPos <= endText.length() - 2
             && endText.at(endPos) == QLatin1Char('*')
              && endText.at(endPos+1) == QLatin1Char('/'))) {
diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp
index 59ee2c005a4c4766d752b0be5e34b268bd25ef4d..0772e4f154a99ffe974679cfbcef818387d62ea5 100644
--- a/src/plugins/texteditor/basetexteditor.cpp
+++ b/src/plugins/texteditor/basetexteditor.cpp
@@ -735,12 +735,15 @@ void BaseTextEditor::moveLineUpDown(bool up)
     move.clearSelection();
     move.insertText(text);
     int end = move.position();
-    move.endEditBlock();
+
     if (hasSelection) {
         move.setPosition(start);
         move.setPosition(end, QTextCursor::KeepAnchor);
     }
 
+    indent(document(), move, QChar::Null);
+    move.endEditBlock();
+
     setTextCursor(move);
 }
 
@@ -2765,6 +2768,8 @@ void BaseTextEditor::handleHomeKey(bool anchor)
 
     while (character == tab || character.category() == QChar::Separator_Space) {
         ++pos;
+        if (pos == initpos)
+            break;
         character = characterAt(pos);
     }
 
@@ -2952,12 +2957,13 @@ void BaseTextEditor::markBlocksAsChanged(QList<int> blockNumbers) {
 
 TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor *cursor, QChar c)
 {
-    if (!TextEditDocumentLayout::hasParentheses(cursor->block()))
+    QTextBlock block = cursor->block();
+    if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
         return NoMatch;
 
-    Parentheses parenList = TextEditDocumentLayout::parentheses(cursor->block());
+    Parentheses parenList = TextEditDocumentLayout::parentheses(block);
     Parenthesis openParen, closedParen;
-    QTextBlock closedParenParag = cursor->block();
+    QTextBlock closedParenParag = block;
 
     const int cursorPos = cursor->position() - closedParenParag.position();
     int i = 0;
@@ -2982,7 +2988,8 @@ TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor
                 closedParenParag = closedParenParag.next();
                 if (!closedParenParag.isValid())
                     return NoMatch;
-                if (TextEditDocumentLayout::hasParentheses(closedParenParag)) {
+                if (TextEditDocumentLayout::hasParentheses(closedParenParag)
+                    && !TextEditDocumentLayout::ifdefedOut(closedParenParag)) {
                     parenList = TextEditDocumentLayout::parentheses(closedParenParag);
                     break;
                 }
@@ -3019,12 +3026,13 @@ TextBlockUserData::MatchType TextBlockUserData::checkOpenParenthesis(QTextCursor
 
 TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCursor *cursor, QChar c)
 {
-    if (!TextEditDocumentLayout::hasParentheses(cursor->block()))
+    QTextBlock block = cursor->block();
+    if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
         return NoMatch;
 
-    Parentheses parenList = TextEditDocumentLayout::parentheses(cursor->block());
+    Parentheses parenList = TextEditDocumentLayout::parentheses(block);
     Parenthesis openParen, closedParen;
-    QTextBlock openParenParag = cursor->block();
+    QTextBlock openParenParag = block;
 
     const int cursorPos = cursor->position() - openParenParag.position();
     int i = parenList.count() - 1;
@@ -3050,7 +3058,8 @@ TextBlockUserData::MatchType TextBlockUserData::checkClosedParenthesis(QTextCurs
                 if (!openParenParag.isValid())
                     return NoMatch;
 
-                if (TextEditDocumentLayout::hasParentheses(openParenParag)) {
+                if (TextEditDocumentLayout::hasParentheses(openParenParag)
+                    && !TextEditDocumentLayout::ifdefedOut(openParenParag)) {
                     parenList = TextEditDocumentLayout::parentheses(openParenParag);
                     break;
                 }
@@ -3092,7 +3101,7 @@ bool TextBlockUserData::findPreviousOpenParenthesis(QTextCursor *cursor, bool se
     int ignore = 0;
     while (block.isValid()) {
         Parentheses parenList = TextEditDocumentLayout::parentheses(block);
-        if (!parenList.isEmpty()) {
+        if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
             for (int i = parenList.count()-1; i >= 0; --i) {
                 Parenthesis paren = parenList.at(i);
                 if (block == cursor->block() && position - block.position() <= paren.pos + 1)
@@ -3119,7 +3128,7 @@ bool TextBlockUserData::findNextClosingParenthesis(QTextCursor *cursor, bool sel
     int ignore = 0;
     while (block.isValid()) {
         Parentheses parenList = TextEditDocumentLayout::parentheses(block);
-        if (!parenList.isEmpty()) {
+        if (!parenList.isEmpty() && !TextEditDocumentLayout::ifdefedOut(block)) {
             for (int i = 0; i < parenList.count(); ++i) {
                 Parenthesis paren = parenList.at(i);
                 if (block == cursor->block() && position - block.position() >= paren.pos)
@@ -3144,7 +3153,7 @@ TextBlockUserData::MatchType TextBlockUserData::matchCursorBackward(QTextCursor
     cursor->clearSelection();
     const QTextBlock block = cursor->block();
 
-    if (!TextEditDocumentLayout::hasParentheses(block))
+    if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
         return NoMatch;
 
     const int relPos = cursor->position() - block.position();
@@ -3166,7 +3175,7 @@ TextBlockUserData::MatchType TextBlockUserData::matchCursorForward(QTextCursor *
     cursor->clearSelection();
     const QTextBlock block = cursor->block();
 
-    if (!TextEditDocumentLayout::hasParentheses(block))
+    if (!TextEditDocumentLayout::hasParentheses(block) || TextEditDocumentLayout::ifdefedOut(block))
         return NoMatch;
 
     const int relPos = cursor->position() - block.position();