diff --git a/indra/llui/lltexteditor.cpp b/indra/llui/lltexteditor.cpp
index 81d9fd1ec9c0a33222b97f69df341a918ebd33e3..7896fcef9547cd34ccb87035db6c6c2de7a1370c 100755
--- a/indra/llui/lltexteditor.cpp
+++ b/indra/llui/lltexteditor.cpp
@@ -2338,7 +2338,8 @@ void LLTextEditor::autoIndent()
 	S32 i;
 
 	LLWString text = getWText();
-	while( ' ' == text[line_start] )
+	S32 offset = getLineOffsetFromDocIndex(mCursorPos);
+	while(( ' ' == text[line_start] ) && (space_count < offset))
 	{
 		space_count++;
 		line_start++;