[gtranslator] Use custom language for gtksourceview



commit 11b7fb5a7ac361133f7c895b74a083f4925131a6
Author: Daniel GarcĂ­a Moreno <dani danigm net>
Date:   Tue Mar 5 21:27:10 2019 +0100

    Use custom language for gtksourceview
    
    close #50

 data/meson.build                 |   5 +
 data/sourceview/gtranslator.lang | 237 +++++++++++++++++++++++++++++++++++++++
 src/gtr-dirs.c                   |   9 ++
 src/gtr-dirs.h                   |   2 +
 src/gtr-view.c                   |  16 ++-
 5 files changed, 268 insertions(+), 1 deletion(-)
---
diff --git a/data/meson.build b/data/meson.build
index 944ffbe3..066c5045 100644
--- a/data/meson.build
+++ b/data/meson.build
@@ -63,3 +63,8 @@ install_data(
   'gtr-languages.ini',
   install_dir: join_paths(gtr_pkgdatadir, 'ui'),
 )
+
+install_data(
+  'sourceview/gtranslator.lang',
+  install_dir: join_paths(gtr_pkgdatadir, 'sourceview'),
+)
diff --git a/data/sourceview/gtranslator.lang b/data/sourceview/gtranslator.lang
new file mode 100644
index 00000000..a40d2a76
--- /dev/null
+++ b/data/sourceview/gtranslator.lang
@@ -0,0 +1,237 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+ This file is part of GtkSourceView
+
+ Authors: Marco Barisione, Emanuele Aina, Paolo Borelli
+ Copyright (C) 2005-2007 Marco Barisione <barisione gmail com>
+ Copyright (C) 2005-2007 Emanuele Aina
+ Copyright (C) 2007 Paolo Borelli
+
+ GtkSourceView is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ GtkSourceView is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, see <http://www.gnu.org/licenses/>.
+
+-->
+<language id="gtranslator" name="GTranslator" version="2.0" _section="Markup">
+    <metadata>
+        <property name="mimetypes">application/xml;text/xml;text/sgml</property>
+        <property 
name="globs">*.xml;*.xspf;*.siv;*.smil;*.smi;*.sml;*.kino;*.xul;*.xbel;*.abw;*.zabw;*.glade;*.jnlp;*.xhtml;*.svg;*.mml;*.rdf;*.rss;*.wml;*.xmi;*.fo;*.xslfo;*.sgml</property>
+        <property name="block-comment-start">&lt;!--</property>
+        <property name="block-comment-end">--&gt;</property>
+    </metadata>
+
+    <styles>
+        <style id="comment"                 name="Comment"                 map-to="def:comment"/>
+        <style id="doctype"                 name="DOCTYPE"                 map-to="def:preprocessor"/>
+        <style id="cdata-delim"             name="CDATA delimiter"         map-to="def:preprocessor"/>
+        <style id="processing-instruction"  name="Processing instruction"  map-to="def:preprocessor"/>
+        <style id="element-name"            name="Element name"            map-to="def:identifier"/>
+        <style id="attribute-name"          name="Attribute name"          map-to="def:type"/>
+        <style id="attribute-value"         name="Attribute value"         map-to="def:string"/>
+        <style id="entity"                  name="Entity"                  map-to="def:preprocessor"/>
+        <style id="tag"                     name="Tag"/>
+        <style id="namespace"               name="Namespace"               map-to="xml:element-name"/>
+        <style id="error"                   name="Error"                   map-to="def:error"/>
+    </styles>
+
+    <definitions>
+        <define-regex id="prefix">[[:alpha:]_][[:alnum:]._-]*</define-regex>
+
+        <define-regex id="name">[[:alpha:]_][[:alnum:].:_-]*</define-regex>
+
+        <context id="entity" style-ref="entity" class="no-spell-check">
+            <match>&amp;\%{name};</match>
+        </context>
+
+        <context id="character-reference" style-ref="entity">
+            <match>&amp;#([0-9]+|x[a-fA-F0-9]+);</match>
+        </context>
+
+        <context id="unallowed-chars" style-ref="error" extend-parent="false">
+            <match>[&amp;&lt;]</match>
+        </context>
+
+        <context id="comment" style-ref="comment" class="comment">
+            <start>&lt;!--</start>
+            <end>--&gt;</end>
+            <include>
+                <context style-ref="error" extend-parent="false">
+                    <match>--+</match>
+                </context>
+                <context ref="def:in-comment"/>
+            </include>
+        </context>
+
+        <context id="doctype" class="no-spell-check">
+            <start>(&lt;!DOCTYPE)\s+(\%{name})</start>
+            <end>&gt;</end>
+            <include>
+              <context sub-pattern="1" where="start" style-ref="doctype"/>
+              <context sub-pattern="2" where="start" style-ref="doctype"/>
+              <context sub-pattern="0" where="end" style-ref="doctype"/>
+              <context>
+                <match>(SYSTEM)\s+(\"[^\"]*\")</match>
+                <include>
+                  <context sub-pattern="1" style-ref="doctype"/>
+                  <context sub-pattern="2" style-ref="attribute-value"/>
+                </include>
+              </context>
+              <context>
+                <match>(PUBLIC)\s+(\"[^\"]*\")\s+(\"[^\"]*\")</match>
+                <include>
+                  <context sub-pattern="1" style-ref="doctype"/>
+                  <context sub-pattern="2" style-ref="attribute-value"/>
+                  <context sub-pattern="3" style-ref="attribute-value"/>
+                </include>
+              </context>
+              <context>
+                <start>\[</start>
+                <end>\]</end>
+                <include>
+                  <context sub-pattern="0" where="start" style-ref="doctype"/>
+                  <context sub-pattern="0" where="end" style-ref="doctype"/>
+                  <context ref="dtd:dtd"/>
+                </include>
+              </context>
+            </include>
+        </context>
+
+        <context id="cdata" class="no-spell-check">
+            <start>&lt;!\[CDATA\[</start>
+            <end>\]\]&gt;</end>
+            <include>
+                <context sub-pattern="0" where="start" style-ref="cdata-delim" class="no-spell-check"/>
+                <context sub-pattern="0" where="end" style-ref="cdata-delim"/>
+            </include>
+        </context>
+
+        <context id="processing-instruction" style-ref="processing-instruction">
+            <start>&lt;\?</start>
+            <end>\?&gt;</end>
+        </context>
+
+        <!-- The following three contexts can be <replace>d in xml
+             dialect lang files, to custommize highlighting, e.g.
+             highlighting docbook tags as keywords -->
+
+        <context id="namespace" style-ref="namespace" class="no-spell-check">
+            <match>(?&lt;!:)\%{prefix}:</match>
+        </context>
+
+        <!-- Match attribute-name before element-name otherwise
+             "text" in <fo:block text-align="left"> is detected as
+             element -->
+        <context id="attribute-name" style-ref="attribute-name" class="no-spell-check">
+            <match>\b\%{name}\s*=</match>
+        </context>
+
+        <context id="attribute-value" style-ref="attribute-value" class="string" 
class-disabled="no-spell-check">
+            <start>["']</start>
+            <end>\%{0@start}</end>
+            <include>
+                <context ref="entity"/>
+                <context ref="character-reference"/>
+                <context ref="unallowed-chars"/>
+            </include>
+        </context>
+
+        <context id="c-format" end-at-line-end="true" style-ref="attribute-value" class="string" 
class-disabled="no-spell-check">
+          <start>[%]</start>
+          <end>\s</end>
+        </context>
+
+        <context id="python-format" end-at-line-end="true" style-ref="attribute-value" class="string" 
class-disabled="no-spell-check">
+          <start>{</start>
+          <end>}</end>
+        </context>
+
+        <context id="n-format" end-at-line-end="true" style-ref="attribute-value" class="string" 
class-disabled="no-spell-check">
+          <start>~</start>
+          <end>\s</end>
+        </context>
+
+        <context id="element-name" once-only="true" style-ref="element-name" class="no-spell-check">
+            <match>\b\%{name}\b(?!\s*=)</match>
+        </context>
+
+        <context id="prolog" class="no-spell-check">
+            <start>&lt;\?xml</start>
+            <end>\?&gt;</end>
+            <include>
+                <context sub-pattern="0" where="start" style-ref="processing-instruction"/>
+                <context sub-pattern="0" where="end" style-ref="processing-instruction"/>
+                <context ref="attribute-value"/>
+                <context ref="attribute-name"/>
+            </include>
+        </context>
+
+        <context id="start-tag" style-ref="tag" class="no-spell-check">
+            <start>&lt;(?!/)</start>
+            <end>/?&gt;</end>
+            <include>
+                <context sub-pattern="0" where="start" style-ref="element-name"/>
+                <context sub-pattern="0" where="end" style-ref="element-name"/>
+                <context ref="entity"/>
+                <context ref="character-reference"/>
+                <context ref="unallowed-chars"/>
+                <context ref="namespace"/>
+                <context ref="attribute-name"/>
+                <context ref="attribute-value"/>
+                <context ref="element-name"/>
+                <context style-ref="error" extend-parent="false">
+                  <match>\S</match>
+                </context>
+            </include>
+        </context>
+
+        <context id="end-tag" style-ref="tag" class="no-spell-check">
+            <start>&lt;/</start>
+            <end>&gt;</end>
+            <include>
+                <context sub-pattern="0" where="start" style-ref="element-name"/>
+                <context sub-pattern="0" where="end" style-ref="element-name"/>
+                <context ref="entity"/>
+                <context ref="character-reference"/>
+                <context ref="unallowed-chars"/>
+                <context ref="namespace"/>
+                <context ref="element-name"/>
+                <context style-ref="error" extend-parent="false">
+                  <match>\S</match>
+                </context>
+            </include>
+        </context>
+
+        <context id="close-tag-outside-tag" style-ref="error">
+          <match>/&gt;</match>
+        </context>
+
+        <context id="gtranslator">
+            <include>
+                <context ref="c-format"/>
+                <context ref="python-format"/>
+                <context ref="n-format"/>
+                <context ref="comment"/>
+                <context ref="doctype"/>
+                <context ref="cdata"/>
+                <context ref="prolog"/>
+                <context ref="processing-instruction"/>
+                <context ref="start-tag"/>
+                <context ref="end-tag"/>
+                <context ref="entity"/>
+                <context ref="character-reference"/>
+                <context ref="unallowed-chars"/>
+                <context ref="close-tag-outside-tag"/>
+            </include>
+        </context>
+    </definitions>
+</language>
diff --git a/src/gtr-dirs.c b/src/gtr-dirs.c
index abb96b3e..f294899e 100644
--- a/src/gtr-dirs.c
+++ b/src/gtr-dirs.c
@@ -39,6 +39,7 @@ static gchar *gtr_lib_dir = NULL;
 static gchar *gtr_plugins_dir = NULL;
 static gchar *gtr_plugins_data_dir = NULL;
 static gchar *gtr_pixmaps_dir = NULL;
+static gchar *gtr_sourceview_dir = NULL;
 
 void
 gtr_dirs_init ()
@@ -91,6 +92,7 @@ gtr_dirs_init ()
   gtr_plugins_data_dir = g_build_filename (gtr_data_dir, "plugins", NULL);
 
   gtr_pixmaps_dir = g_build_filename (gtr_data_dir, "pixmaps", NULL);
+  gtr_sourceview_dir = g_build_filename (gtr_data_dir, "sourceview", NULL);
 }
 
 void
@@ -106,6 +108,7 @@ gtr_dirs_shutdown ()
   g_free (gtr_plugins_dir);
   g_free (gtr_plugins_data_dir);
   g_free (gtr_pixmaps_dir);
+  g_free (gtr_sourceview_dir);
 }
 
 const gchar *
@@ -168,6 +171,12 @@ gtr_dirs_get_gtr_pixmaps_dir (void)
   return gtr_pixmaps_dir;
 }
 
+const gchar *
+gtr_dirs_get_gtr_sourceview_dir (void)
+{
+  return gtr_sourceview_dir;
+}
+
 gchar *
 gtr_dirs_get_ui_file (const gchar * file)
 {
diff --git a/src/gtr-dirs.h b/src/gtr-dirs.h
index 34a0be51..a49079b3 100644
--- a/src/gtr-dirs.h
+++ b/src/gtr-dirs.h
@@ -52,6 +52,8 @@ const gchar *gtr_dirs_get_gtr_plugins_data_dir (void);
 
 const gchar *gtr_dirs_get_gtr_pixmaps_dir (void);
 
+const gchar *gtr_dirs_get_gtr_sourceview_dir (void);
+
 gchar *gtr_dirs_get_ui_file (const gchar * file);
 
 G_END_DECLS
diff --git a/src/gtr-view.c b/src/gtr-view.c
index 1db3e113..2c53cfb5 100644
--- a/src/gtr-view.c
+++ b/src/gtr-view.c
@@ -338,11 +338,25 @@ gtr_view_enable_visible_whitespace (GtrView * view, gboolean enable)
   GtkSourceBuffer *buffer;
   GtkSourceLanguageManager *manager;
   GtkSourceLanguage *lang;
+  const gchar * const * deflangs = NULL;
+  const gchar *langs[20] = {NULL};
+  gint i = 0;
 
   g_return_if_fail (GTR_IS_VIEW (view));
 
   manager = gtk_source_language_manager_get_default ();
-  lang = gtk_source_language_manager_guess_language (manager, "file.xml", NULL);
+  deflangs = gtk_source_language_manager_get_search_path (manager);
+  langs[1] = deflangs[0];
+
+  while (deflangs[i] && i < 18) {
+    langs[i] = deflangs[i];
+    i++;
+  }
+  langs[i] = gtr_dirs_get_gtr_sourceview_dir ();
+
+  manager = gtk_source_language_manager_new ();
+  gtk_source_language_manager_set_search_path (manager, (gchar **)langs);
+  lang = gtk_source_language_manager_get_language (manager, "gtranslator");
 
   source = GTK_SOURCE_VIEW (view);
   drawer = gtk_source_view_get_space_drawer (source);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]