FLTK logo

[master] 7161cad - #329 #318: Fixed Fluid indenting and trailing whitespace

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.commit  ]
 
Previous Message ]Next Message ]

[master] 7161cad - #329 #318: Fixed Fluid indenting and trailing whitespace "Matthias Melcher" Dec 15, 2021  
 
commit 7161cad2c75c323fe8c9316ab7ac14e84411b37f
Author:     Matthias Melcher <git@matthiasm.com>
AuthorDate: Wed Dec 15 19:29:12 2021 +0100
Commit:     Matthias Melcher <github@matthiasm.com>
CommitDate: Wed Dec 15 19:38:09 2021 +0100

    #329 #318: Fixed Fluid indenting and trailing whitespace

 fluid/Fl_Function_Type.cxx | 19 +------------------
 fluid/Fl_Menu_Type.cxx     |  2 +-
 fluid/Fl_Widget_Type.cxx   |  2 +-
 fluid/code.cxx             | 31 ++++++++++++++++++++++++-------
 fluid/code.h               |  2 +-
 5 files changed, 28 insertions(+), 28 deletions(-)

diff --git fluid/Fl_Function_Type.cxx fluid/Fl_Function_Type.cxx
index ac367c7..ab3672e 100644
--- fluid/Fl_Function_Type.cxx
+++ fluid/Fl_Function_Type.cxx
@@ -634,24 +634,7 @@ void Fl_Code_Type::write_code1() {
     main_window->redraw();    // tell fluid to redraw; edits may affect tree's contents
   }
 
-  const char* c = name();
-  if (!c) return;
-
-  const char *pch;
-  const char *ind = indent();
-  while( (pch=strchr(c,'\n')) )
-  {
-    int line_len = int(pch - c);
-    if (line_len < 1)
-      write_c("\n");
-    else
-      write_c("%s%.*s\n", ind, line_len, c);
-    c = pch+1;
-  }
-  if (*c)
-    write_c("%s%s\n", ind, c);
-  else
-    write_c("\n");
+  write_c_indented(name(), 0, '\n');
 }
 
 /**
diff --git fluid/Fl_Menu_Type.cxx fluid/Fl_Menu_Type.cxx
index 7bd55ce..d05f7ea 100644
--- fluid/Fl_Menu_Type.cxx
+++ fluid/Fl_Menu_Type.cxx
@@ -228,7 +228,7 @@ void Fl_Menu_Item_Type::write_static() {
     write_c(", %s", ut);
     if (use_v) write_c(" v");
     write_c(") {\n");
-    write_c_indented(callback());
+    write_c_indented(callback(), 1, 0);
     if (*(d-1) != ';' && *(d-1) != '}') {
       const char *p = strrchr(callback(), '\n');
       if (p) p ++;
diff --git fluid/Fl_Widget_Type.cxx fluid/Fl_Widget_Type.cxx
index faee215..6ad732f 100644
--- fluid/Fl_Widget_Type.cxx
+++ fluid/Fl_Widget_Type.cxx
@@ -2067,7 +2067,7 @@ void Fl_Widget_Type::write_static() {
     write_c(", %s", ut);
     if (use_v) write_c(" v");
     write_c(") {\n");
-    write_c_indented(callback());
+    write_c_indented(callback(), 1, 0);
     if (*(d-1) != ';' && *(d-1) != '}') {
       const char *p = strrchr(callback(), '\n');
       if (p) p ++;
diff --git fluid/code.cxx fluid/code.cxx
index 11d8d2d..ec61902 100644
--- fluid/code.cxx
+++ fluid/code.cxx
@@ -410,18 +410,35 @@ void write_hc(const char *indent, int n, const char* c, const char *com) {
  Write one or more lines of code, indenting each one of them.
  \param[in] textlines one or more lines of text, seperated by \\n
  */
-void write_c_indented(const char *textlines) {
+void write_c_indented(const char *textlines, int inIndent, char inTrailwWith) {
   if (textlines) {
-    indentation++;
+    indentation += inIndent;
     for (;;) {
+      int line_len;
       const char *newline = strchr(textlines, '\n');
-      if (!newline) break;
-      write_c("%s%.*s\n", indent(), (int)(newline-textlines), textlines);
+      if (newline)
+        line_len = (int)(newline-textlines);
+      else
+        line_len = strlen(textlines);
+      if (textlines[0]=='\n') {
+        // avoid trailing spaces
+      } else if (textlines[0]=='#') {
+        // don't indent preprocessor statments starting with '#'
+        write_c("%.*s", (int)(newline-textlines), textlines);
+      } else {
+        // indent all other text lines
+        write_c("%s%.*s", indent(), (int)(newline-textlines), textlines);
+      }
+      if (newline) {
+        write_c("\n");
+      } else {
+        if (inTrailwWith)
+          write_c("%c", inTrailwWith);
+        break;
+      }
       textlines = newline+1;
     }
-    if (*textlines)
-      write_c("%s%s", indent(), textlines);
-    indentation--;
+    indentation -= inIndent;
   }
 }
 
diff --git fluid/code.h fluid/code.h
index 474b87b..5e9a702 100644
--- fluid/code.h
+++ fluid/code.h
@@ -39,7 +39,7 @@ void write_c(const char*, ...) __fl_attr((__format__ (__printf__, 1, 2)));
 void write_cc(const char *, int, const char*, const char*);
 void write_h(const char*, ...) __fl_attr((__format__ (__printf__, 1, 2)));
 void write_hc(const char *, int, const char*, const char*);
-void write_c_indented(const char *textlines);
+void write_c_indented(const char *textlines, int inIndent, char inTrailwWith);
 int write_code(const char *cfile, const char *hfile);
 int write_strings(const char *sfile);
 void write_public(int state); // writes pubic:/private: as needed
Direct Link to Message ]
 
     
Previous Message ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.