Browse Source

Implement "Remove trailing spaces" (the same way as Kate implements it: remove them on load, save and on the edits Kate removes them on (pressing Enter, basically)).

git-svn-id: file:///var/svn/tigccpp/trunk@553 9552661e-59e3-4036-b4f2-dbe53926924f
kevinkofler 18 years ago
parent
commit
98e34dd422
4 changed files with 56 additions and 6 deletions
  1. 1 0
      ktigcc/mainform.ui
  2. 38 3
      ktigcc/mainform.ui.h
  3. 2 1
      ktigcc/preferences.cpp
  4. 15 2
      ktigcc/tpr.cpp

+ 1 - 0
ktigcc/mainform.ui

@@ -1639,6 +1639,7 @@
     <function access="private" specifier="non virtual">updateLeftStatusLabel()</function>
     <function access="private" specifier="non virtual">updateRightStatusLabel()</function>
     <function access="protected">closeEvent( QCloseEvent * e )</function>
+    <function access="private" specifier="non virtual">removeTrailingSpacesFromView(void *view)</function>
 </functions>
 <pixmapinproject/>
 <layoutdefaults spacing="0" margin="0"/>

+ 38 - 3
ktigcc/mainform.ui.h

@@ -27,6 +27,8 @@
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 */
 
+#include <qstring.h>
+#include <qregexp.h>
 #include <qapplication.h>
 #include <qlabel.h>
 #include <qstatusbar.h>
@@ -113,6 +115,9 @@ enum {TIGCCOpenProjectFileFilter,TIGCCAddFilesFilter};
                                         othFileCount)
 #define CURRENT_VIEW (static_cast<Kate::View *>(widgetStack->visibleWidget()))
 
+// For some reason, this flag is not in the public ConfigFlags enum.
+#define CF_REMOVE_SPACES_ONLINE 0x4000000
+
 static QListViewItem *currentListItem;
 static QListViewItem *replaceCurrentDocument;
 static unsigned replaceCurrentLine;
@@ -1091,6 +1096,10 @@ void *MainForm::createView(const QString &fileName, const QString &fileText, QLi
   newView->getDoc()->setHlMode(i);
   // Set options.
   dynWordWrapInterface(newView)->setDynWordWrap(FALSE);
+  if (preferences.removeTrailingSpaces)
+    newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_SPACES_ONLINE));
+  else
+    newView->getDoc()->setConfigFlags(newView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_SPACES_ONLINE));
   newView->setTabWidth(
     (category==sFilesListItem||category==asmFilesListItem||((category==hFilesListItem&&!fileText.isNull()&&!fileText.isEmpty()&&(fileText[0]=='|'||fileText[0]==';'))))?preferences.tabWidthAsm:
     (category==cFilesListItem||category==qllFilesListItem||category==hFilesListItem)?preferences.tabWidthC:
@@ -1318,6 +1327,22 @@ int MainForm::savePrompt(void)
   return 0;
 }
 
+void MainForm::removeTrailingSpacesFromView(void *view)
+{
+  if (!preferences.removeTrailingSpaces) return;
+  Kate::View *kateView=reinterpret_cast<Kate::View *>(view);
+  Kate::Document *doc=kateView->getDoc();
+  KTextEditor::EditInterfaceExt *editExt=KTextEditor::editInterfaceExt(doc);
+  editExt->editBegin();
+  unsigned numLines=doc->numLines();
+  for (unsigned i=0; i<numLines; i++) {
+    QString line=doc->textLine(i);
+    int whitespace=line.find(QRegExp("\\s+$"));
+    if (whitespace>=0) doc->removeText(i,whitespace,i,line.length());
+  }
+  editExt->editEnd();
+}
+
 void MainForm::fileSave_save(QListViewItem *theItem)
 {
   if (!IS_FILE(theItem))
@@ -1338,8 +1363,10 @@ void MainForm::fileSave_save(QListViewItem *theItem)
     else {
       KDirWatch::self()->addFile(theFile->fileName);
       theFile->isNew=FALSE;
-      if (theFile->kateView)
+      if (theFile->kateView) {
+        removeTrailingSpacesFromView(theFile->kateView);
         theFile->kateView->getDoc()->setModified(FALSE);
+      }
       projectIsDirty=TRUE;
     }
   }
@@ -1399,8 +1426,10 @@ void MainForm::fileSave_saveAs(QListViewItem *theItem)
     theFile->fileName=saveFileName;
     if (IS_EDITABLE_CATEGORY(category)) {
       KDirWatch::self()->addFile(saveFileName);
-      if (theFile->kateView)
+      if (theFile->kateView) {
+        removeTrailingSpacesFromView(theFile->kateView);
         theFile->kateView->getDoc()->setModified(FALSE);
+      }
     }
     theFile->isNew=FALSE;
     updateRightStatusLabel();
@@ -1470,8 +1499,10 @@ void MainForm::fileSave_loadList(QListViewItem *category,void *fileListV,const Q
           theFile->fileName=saveFileName;
           if (IS_EDITABLE_CATEGORY(category)) {
             KDirWatch::self()->addFile(theFile->fileName);
-            if (theFile->kateView)
+            if (theFile->kateView) {
+              removeTrailingSpacesFromView(theFile->kateView);
               theFile->kateView->getDoc()->setModified(FALSE);
+            }
           }
           theFile->isNew=FALSE;
           projectIsDirty=TRUE; // in case saving the project fails
@@ -1605,6 +1636,10 @@ void MainForm::filePreferences()
         if (kateView) {
           QString fileText=kateView->getDoc()->text();
           CATEGORY_OF(category,item);
+          if (preferences.removeTrailingSpaces)
+            kateView->getDoc()->setConfigFlags(kateView->getDoc()->configFlags()|(Kate::Document::cfRemoveSpaces|CF_REMOVE_SPACES_ONLINE));
+          else
+            kateView->getDoc()->setConfigFlags(kateView->getDoc()->configFlags()&~(Kate::Document::cfRemoveSpaces|CF_REMOVE_SPACES_ONLINE));
           kateView->setTabWidth(
             (category==sFilesListItem||category==asmFilesListItem||((category==hFilesListItem&&!fileText.isNull()&&!fileText.isEmpty()&&(fileText[0]=='|'||fileText[0]==';'))))?preferences.tabWidthAsm:
             (category==cFilesListItem||category==qllFilesListItem||category==hFilesListItem)?preferences.tabWidthC:

+ 2 - 1
ktigcc/preferences.cpp

@@ -1265,7 +1265,7 @@ void loadPreferences(void)
   preferences.useCalcCharset=pconfig->readBoolEntry("Use Calc Charset",true);
   preferences.lazyLoading=pconfig->readBoolEntry("Lazy Loading",true);
   preferences.autoBlocks=pconfig->readBoolEntry("Auto Blocks",true);
-  preferences.removeTrailingSpaces=pconfig->readBoolEntry("Remove Trailing Spaces",true);
+  preferences.removeTrailingSpaces=pconfig->readBoolEntry("Remove Trailing Spaces",false);
 
   updateEditorPreferences();
 }
@@ -1273,6 +1273,7 @@ void loadPreferences(void)
 void savePreferences(void)
 {
   pconfig->setGroup("Preferences");
+
   // General
   pconfig->writeEntry("Stop at First Error",(bool)preferences.stopAtFirstError);
   pconfig->writeEntry("Jump to Error",(bool)preferences.jumpToError);

+ 15 - 2
ktigcc/tpr.cpp

@@ -31,6 +31,8 @@
 #include <kapplication.h>
 #include <kcmdlineargs.h>
 #include <kaboutdata.h>
+#include <qstring.h>
+#include <qregexp.h>
 #include <qtextcodec.h>
 #include <glib.h>
 #include <ticonv.h>
@@ -516,6 +518,11 @@ QString loadFileText(const char *fileName)
     ret=ret.replace("\r\n","\n");
     // interpret remaining \r characters as Mac line endings
     ret=ret.replace('\r','\n');
+    // remove trailing spaces if requested
+    if (preferences.removeTrailingSpaces) {
+      ret=ret.replace(QRegExp("\\s*\n"),"\n");
+      ret=ret.remove(QRegExp("\\s*$"));
+    }
   }
   return ret;
 }
@@ -744,6 +751,12 @@ int saveFileText(const char *fileName,const QString &fileText)
 {
   FILE *f;
   size_t l;
+  // remove trailing spaces if requested
+  QString text=fileText;
+  if (preferences.removeTrailingSpaces && !text.isNull()) {
+    text=text.replace(QRegExp("\\s*\n"),"\n");
+    text=text.remove(QRegExp("\\s*$"));
+  }
   
   f=fopen(fileName,"wb");
   if (!f)
@@ -754,7 +767,7 @@ int saveFileText(const char *fileName,const QString &fileText)
         return -1;
   }
   if (preferences.useCalcCharset) {
-    const unsigned short *utf16=fileText.ucs2();
+    const unsigned short *utf16=text.ucs2();
     if (utf16) {
       char *s=ticonv_charset_utf16_to_ti(CALC_TI89,utf16);
       l=std::strlen(s);
@@ -766,7 +779,7 @@ int saveFileText(const char *fileName,const QString &fileText)
       g_free(s);
     }
   } else {
-    const char *s=smartAscii(fileText);
+    const char *s=smartAscii(text);
     l=fileText.length();
     if (fwrite(s,1,l,f)<l) {
       fclose(f);