[Patch] Syntax highlighting in MlViewTreeEditor2

Nicolas Centa <[email protected]> Tue, 21 Oct 2003 21:52:41 +0200
Newsgroups gmane.editors.mlview.devel
Message-ID <[email protected]>
Hello !

Another patch, which does syntax highlighting in MlViewTreeEditor2.
Some code was taken from Conglomerate (thanks to Conge's developers :-)

Nicolas Centa
"HappyPeng"
colours.diff (application/octet-stream, 13.1 KB)
diff -ru orig/mlview/src/mlview-tree-editor2.c mlview/src/mlview-tree-editor2.c
--- orig/mlview/src/mlview-tree-editor2.c	2003-10-21 21:40:06.000000000 +0200
+++ mlview/src/mlview-tree-editor2.c	2003-10-21 21:41:45.000000000 +0200
@@ -170,6 +170,8 @@
         {(gchar *)"GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0}
 } ;
 
+/* Coloration */
+const gchar *get_colour_string (int type);
 
 static void xml_doc_prev_sibling_node_inserted_cb (MlViewXMLDocument *a_this,
                                                    xmlNode *a_sibling_node,
@@ -684,14 +686,74 @@
 static guchar *
 build_attrs_list_str (xmlNode * a_node)
 {
-        guchar *result = NULL;
-
         g_return_val_if_fail (a_node, NULL);
 
-        xml_attr_to_string (a_node->properties, &result);
+        guchar *result = g_strdup ("");
+        xmlAttrPtr attr_iter;
+        const gchar *attval_col = "#00FF00",
+                *attname_col = get_colour_string (XML_ATTRIBUTE_NODE);
+        gchar *attribute;
+
+        for (attr_iter = a_node->properties; attr_iter; attr_iter=attr_iter->next) { 
+                attribute = g_strdup_printf
+                        (" <span foreground=\"%s\">%s=<span foreground=\"%s\">\"%s\"</span></span>",
+                         attname_col, attr_iter->name, attval_col, attr_iter->children->content);
+                result = g_strconcat (attribute, result, NULL);
+                g_free(attribute); }
+
         return result;
 }
 
+/* Coloration (from Conglomerate) */
+const gchar*
+get_colour_string (int type)
+{
+	switch (type) {
+	default: 
+                g_assert_not_reached ();
+	case XML_ELEMENT_NODE:
+		return "#0080ff";
+	case XML_ATTRIBUTE_NODE:
+		return "#000000";
+	case XML_TEXT_NODE:
+		return "#ff0000";
+	case XML_CDATA_SECTION_NODE:
+		return "#000000";
+	case XML_ENTITY_REF_NODE:
+		return "#000000";
+	case XML_ENTITY_NODE:
+		return "#000000";
+	case XML_PI_NODE:
+		return "#000000";
+	case XML_COMMENT_NODE:
+		return "#0000FF";
+	case XML_DOCUMENT_NODE:
+		return "#0080ff";
+	case XML_DOCUMENT_TYPE_NODE:
+		return "#000000";
+	case XML_DOCUMENT_FRAG_NODE:
+		return "#000000";
+	case XML_NOTATION_NODE:
+		return "#000000";
+	case XML_HTML_DOCUMENT_NODE:
+		return "#000000";
+	case XML_DTD_NODE:
+		return "#0000FF";
+	case XML_ELEMENT_DECL:
+		return "#000000";
+	case XML_ATTRIBUTE_DECL:
+		return "#000000";
+	case XML_ENTITY_DECL:
+		return "#000000";
+	case XML_NAMESPACE_DECL:
+		return "#000000";
+	case XML_XINCLUDE_START:
+		return "#000000";
+	case XML_XINCLUDE_END:
+		return "#000000";
+	}
+}
+
 /**
  *Builds a start tag string 
  *(e.g: <node-name attr0="val0" attr1="val1">)out of
@@ -707,77 +769,56 @@
 
         g_return_val_if_fail (a_node != NULL, NULL);
 
+        const gchar *colour_str = get_colour_string (a_node->type);
+
         if (a_node->type == XML_ELEMENT_NODE) {
-                guchar *utf8_ns_prefix = NULL,
-                        *ns_prefix = NULL,
+                guchar *ns_prefix = NULL,
                         *attr_str = NULL,
-                        *name = NULL,
-                        *tmp_str = NULL;
+                        *name = NULL;
+
+                const gchar *attr_colour_str = get_colour_string (XML_ATTRIBUTE_NODE);
 
                 attr_str = build_attrs_list_str (a_node);
                 if (a_node->ns != NULL && a_node->ns->prefix) {
-                        enum MlViewStatus status = MLVIEW_OK;
-
-                        utf8_ns_prefix =
-                                g_strconcat (a_node->ns->prefix,
-                                             ":", NULL);
-                        status = mlview_utils_utf8_str_to_isolat1
-                                (utf8_ns_prefix, &ns_prefix);
-                        g_return_val_if_fail (status ==
-                                              MLVIEW_OK, NULL);
+                        ns_prefix = g_strconcat (a_node->ns->prefix, 
+                                                 ":", NULL);
                 } else {
                         ns_prefix = NULL;
                 }
 
                 if (ns_prefix) {
-                        enum MlViewStatus status = MLVIEW_OK;
-
-                        status = mlview_utils_utf8_str_to_isolat1
-                                ((guchar *) a_node->name,
-                                 &tmp_str);
-                        g_return_val_if_fail (status ==
-                                              MLVIEW_OK, NULL);
-                        g_return_val_if_fail (tmp_str != NULL,
-                                              NULL);
-                        name = g_strconcat (ns_prefix, tmp_str,
-                                            NULL);
+                        name = g_strconcat (ns_prefix, a_node->name, NULL);
                 } else {
-                        enum MlViewStatus status = MLVIEW_OK;
-
-                        status = mlview_utils_utf8_str_to_isolat1
-                                ((guchar *) a_node->name, &name);
-                        g_return_val_if_fail
-                                (status == MLVIEW_OK, NULL);
+                        name = g_strdup (a_node->name);
                 }
                 if (ns_prefix) {
                         g_free (ns_prefix);
                         ns_prefix = NULL;
                 }
 
-                if (utf8_ns_prefix) {
-                        g_free (utf8_ns_prefix);
-                        utf8_ns_prefix = NULL;
-                }
-                if (tmp_str) {
-                        g_free (tmp_str);
-                        tmp_str = NULL;
-                }
                 if (a_node->children != NULL) {
                         if (attr_str)
                                 result = g_strconcat
-                                        ("<", name, " ",
-                                         attr_str, ">", NULL);
+                                        ("<span foreground=\"", 
+                                         colour_str, "\">&lt;", name, "</span> <span foreground=\"",
+                                         attr_colour_str, "\">",
+                                         attr_str, "</span><span foreground=\"",
+                                         colour_str, "\">&gt;</span>", NULL);
                         else
                                 result = g_strconcat
-                                        ("<", name, ">", NULL);
+                                        ("<span foreground=\"", 
+                                         colour_str, "\">&lt;", name, "&gt;</span>", NULL);
                 } else {        /*empty tag */
                         if (attr_str)
                                 result = g_strconcat
-                                        ("<", name, " ",
-                                         attr_str, "/>", NULL);
+                                        ("<span foreground=\"", colour_str, "\">&lt;", 
+                                         name, "</span> <span foreground=\"", attr_colour_str,
+                                         "\">", attr_str, "</span><span foreground=\"", colour_str, 
+                                         "\">/&gt;</span>", NULL);
                         else
                                 result = g_strconcat
-                                        ("<", name, "/>", NULL);
+                                        ("<span foreground=\"", colour_str, "\">&lt;", name, 
+                                         "/&gt;</span>", NULL);
                 }
 
                 if (name) {
@@ -786,99 +827,41 @@
                 }
 
         } else if (xmlNodeIsText (a_node)) {
-                enum MlViewStatus status = MLVIEW_OK;
-                guchar *utf8_content = NULL;
-
-                utf8_content = xmlNodeGetContent (a_node);
-
-                if (utf8_content) {
-                        status = mlview_utils_utf8_str_to_isolat1
-                                (utf8_content, &content);
-                        g_return_val_if_fail
-                                (status == MLVIEW_OK, NULL);
-                        xmlFree (utf8_content);
-                        utf8_content = NULL;
-                }
+                content = xmlNodeGetContent (a_node);
                 if (content == NULL) {
                         xmlNodeSetContent (a_node, "text");
-                        content = xmlNodeGetContent (a_node);
-                }
-                if (content && strlen (content)
-                    > MLVIEW_CONTENT_MAX_LEN) {
-                        content[MLVIEW_CONTENT_MAX_LEN] = '\0';
-                }
-                if (content != NULL) {
-                        result = g_strdup (content);
-                        g_free (content);
-                        content = NULL ;
-                }
-        } else if (a_node->type == XML_COMMENT_NODE) {
-                enum MlViewStatus status = MLVIEW_OK;
-                guchar *utf8_content = NULL;
-
-                utf8_content = xmlNodeGetContent (a_node);
-                if (utf8_content) {
-                        status = mlview_utils_utf8_str_to_isolat1
-                                (utf8_content, &content);
-                        g_return_val_if_fail (status ==
-                                              MLVIEW_OK, NULL);
+                        content = xmlNodeGetContent (a_node); }
 
-                        xmlFree (utf8_content);
-                        utf8_content = NULL;
-                }
+                result = g_strconcat ("<span foreground=\"", colour_str,
+                                      "\">", content, "</span>", NULL);
+                
+                xmlFree (content);
+        } else if (a_node->type == XML_COMMENT_NODE) {
+                content = xmlNodeGetContent (a_node);
                 if (content == NULL) {
-                        xmlNodeSetContent (a_node,
-                                           "<!--comment-->");
-                        content = xmlNodeGetContent (a_node);
-                }
-                if (strlen (content) > MLVIEW_CONTENT_MAX_LEN)
-                        content[MLVIEW_CONTENT_MAX_LEN] = '\0';
-
-                result = g_strconcat ("<!--", content, "-->",
-                                      NULL);
-                if (content != NULL) {
-                        g_free (content);
-                        content = NULL;
-                }
-        } else if (a_node->type == XML_PI_NODE) {
-                enum MlViewStatus status = MLVIEW_OK;
-                guchar *utf8_content = NULL,
-                        *name = NULL;
+                        xmlNodeSetContent (a_node, "<!--comment-->");
+                        content = xmlNodeGetContent (a_node); }
 
-                utf8_content = xmlNodeGetContent (a_node);
+                result = g_strconcat ("<span foreground=\"", 
+                                      colour_str, "\">&lt;!--", content, 
+                                      "--&gt;</span>", NULL);
 
-                if (utf8_content) {
-                        status = mlview_utils_utf8_str_to_isolat1
-                                (utf8_content, &content);
-                        g_return_val_if_fail
-                                (status == MLVIEW_OK, NULL);
-
-                        xmlFree (utf8_content);
-                        utf8_content = NULL;
-                }
+                xmlFree (content);
+        } else if (a_node->type == XML_PI_NODE) {
+                content = xmlNodeGetContent (a_node);
                 if (content == NULL) {
                         xmlNodeSetContent
-                                (a_node,
-                                 "<?processing instruction node>");
-                }
-                status = mlview_utils_utf8_str_to_isolat1
-                        ((guchar *) a_node->name, &name);
-                g_return_val_if_fail (status == MLVIEW_OK, NULL);
-
-                if (strlen (content) > MLVIEW_CONTENT_MAX_LEN) {
-                        content[MLVIEW_CONTENT_MAX_LEN] = '\0';
-                }
+                                (a_node, "&lt;?processing instruction node&gt;");
+                        content = xmlNodeGetContent (a_node); }
+                
                 result = g_strconcat
-                        ("<?", name, " ", content, ">", NULL);
-                if (content != NULL) {
-                        g_free (content);
-                        content = NULL;
-                }
-                if (name != NULL) {
-                        g_free (name);
-                        name = NULL;
-                }
+                        ("<span foreground=\"",
+                         colour_str, "\">&lt;?", a_node->name, " ", 
+                         content, "&gt;</span>", NULL);
+                
+                xmlFree (content);
         }
+
         return result;
 }
 
@@ -1176,7 +1159,7 @@
         renderer = gtk_cell_renderer_text_new ();
         gtk_tree_view_insert_column_with_attributes
                 (tree_view, START_TAG_COLUMN, _("Element start tag"),
-                 renderer, "text", START_TAG_COLUMN, 
+                 renderer, "markup", START_TAG_COLUMN, 
                  "editable", IS_EDITABLE_COLUMN, NULL);
          g_signal_connect (G_OBJECT (renderer),
                           "edited", G_CALLBACK (node_cell_edited_cb),