[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE_3_2_BRANCH: kdelibs/kate/part
From:       Anders Lund <anders () alweb ! dk>
Date:       2004-03-29 5:54:20
Message-ID: 20040329055420.626A3997E () office ! kde ! org
[Download RAW message or body]

CVS commit by alund: 

Backport: Enhance the on save cleanups
* Calculate correct number of spaces for replacing tabs.
* Replace tabs in document content rather than just in the data stream
  when saving, so that the change is real and visible to the user.
* Remove trailing spaces in the document content rather than just in the
  data stream when saving, so that the change becomes real and visible
  to the user.

This is using document methods, and the entire editing will be undoable.

CCMAIL:74700@bugs.kde.org


  M +81 -39    katebuffer.cpp   1.143.2.3


--- kdelibs/kate/part/katebuffer.cpp  #1.143.2.2:1.143.2.3
@@ -680,19 +680,61 @@ bool KateBuffer::saveFile (const QString
   QString eol = m_doc->config()->eolString ();
 
-  QString tabs;
-  if (m_doc->configFlags() & KateDocument::cfReplaceTabs)
-    tabs.fill (QChar(' '), m_doc->config()->tabWidth());
+//   QString tabs;
+//   if (m_doc->configFlags() & KateDocument::cfReplaceTabs)
+//     tabs.fill (QChar(' '), m_doc->config()->tabWidth());
+
+  // for tab replacement, initialize only once
+  uint pos, found, ml, l;
+  QChar onespace(' ');
+  QString onetab("\t");
+  uint tw = m_doc->config()->tabWidth();
+
+  // Use the document methods
+  if ( m_doc->configFlags() & KateDocument::cfReplaceTabs ||
+       m_doc->configFlags() & KateDocument::cfRemoveSpaces )
+    m_doc->editStart();
 
   for (uint i=0; i < m_lines; i++)
   {
-    // if enabled strip the trailing spaces !
-    if (m_doc->configFlags() & KateDocument::cfReplaceTabs)
-      stream << textLine (i, m_doc->configFlags() & \
                KateDocument::cfRemoveSpaces).replace (QChar('\t'), tabs);
-    else
-      stream << textLine (i, m_doc->configFlags() & KateDocument::cfRemoveSpaces);
+    TextLine::Ptr textLine = plainLine(i);
 
-    if (i < (m_lines-1))
+    if (textLine)
+    {
+      // replace tabs if required
+      if ( m_doc->configFlags() & KateDocument::cfReplaceTabs )
+      {
+        pos = 0;
+        while ( textLine->searchText( pos, onetab, &found, &ml ) )
+        {
+          l = tw - ( found%tw );
+          if ( l )
+          {
+            QString t;
+            m_doc->editRemoveText( i, found, 1 );
+            m_doc->editInsertText( i, found, t.fill(onespace, l) ); // ### anything \
more eficient? +            pos += l-1;
+          }
+        }
+      }
+
+      // remove trailing spaces if required
+      if ( (m_doc->configFlags() & KateDocument::cfRemoveSpaces) && \
textLine->length() ) +      {
+        pos = textLine->length() - 1;
+        uint lns = textLine->lastChar();
+        if ( lns != pos )
+          m_doc->editRemoveText( i, lns + 1, pos - lns );
+      }
+
+      stream << textLine->string();
+
+      if ((i+1) < m_lines)
       stream << eol;
   }
+  }
+
+  if ( m_doc->configFlags() & KateDocument::cfReplaceTabs ||
+       m_doc->configFlags() & KateDocument::cfRemoveSpaces )
+    m_doc->editEnd();
 
   file.close ();


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic