[gnome-text-editor] page: add support for drawing spaces



commit 416a65af17f6b759721ef4606f7b7805fe7af67a
Author: Christian Hergert <chergert redhat com>
Date:   Wed Dec 1 20:11:50 2021 -0800

    page: add support for drawing spaces
    
    This does not currently have UI exposed, but the GSetting is there for
    those that want to enable it that way.
    
    You can do something like:
    
      gsettings set org.gnome.TextEditor draw-spaces "['tab','space']"
    
    The flags you can enable:
    
      * space
      * tab (\t)
      * newline (\n)
      * nbsp (non-breaking space)
      * leading (leading text on line)
      * text (only within text)
      * trailing (trailing text in line)
    
    Fixes #150

 data/org.gnome.TextEditor.gschema.xml | 14 +++++++
 src/editor-page.c                     | 69 +++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+)
---
diff --git a/data/org.gnome.TextEditor.gschema.xml b/data/org.gnome.TextEditor.gschema.xml
index 2c7f2cd..78e394a 100644
--- a/data/org.gnome.TextEditor.gschema.xml
+++ b/data/org.gnome.TextEditor.gschema.xml
@@ -1,5 +1,14 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <schemalist gettext-domain="gnome-text-editor">
+  <flags id="org.gnome.TextEditor.DrawSpaces">
+    <value nick="space" value="1"/>
+    <value nick="tab" value="2"/>
+    <value nick="newline" value="4"/>
+    <value nick="nbsp" value="8"/>
+    <value nick="leading" value="16"/>
+    <value nick="text" value="32"/>
+    <value nick="trailing" value="64"/>
+  </flags>
   <schema id="org.gnome.TextEditor" path="/org/gnome/TextEditor/">
     <key name="auto-save-delay" type="u">
       <range min="1" max="300"/>
@@ -128,5 +137,10 @@
       <summary>Last Save Directory</summary>
       <description>The directory last used in a save or save-as dialog.</description>
     </key>
+    <key name="draw-spaces" flags="org.gnome.TextEditor.DrawSpaces">
+      <default>[]</default>
+      <summary>Draw Spaces</summary>
+      <description>The various types of spaces to draw in the editor.</description>
+    </key>
   </schema>
 </schemalist>
diff --git a/src/editor-page.c b/src/editor-page.c
index 9a0f1bc..a339aac 100644
--- a/src/editor-page.c
+++ b/src/editor-page.c
@@ -52,6 +52,65 @@ G_DEFINE_TYPE (EditorPage, editor_page, GTK_TYPE_WIDGET)
 
 static GParamSpec *properties [N_PROPS];
 
+static void
+on_draw_spaces_changed (EditorPage  *self,
+                        const gchar *key,
+                        GSettings   *settings)
+{
+  GtkSourceSpaceLocationFlags location_flags = GTK_SOURCE_SPACE_LOCATION_NONE;
+  GtkSourceSpaceTypeFlags type_flags = GTK_SOURCE_SPACE_TYPE_NONE;
+  GtkSourceView *source_view;
+  GtkSourceSpaceDrawer *drawer;
+  guint flags;
+
+  g_assert (EDITOR_IS_PAGE (self));
+  g_assert (g_strcmp0 (key, "draw-spaces") == 0);
+  g_assert (G_IS_SETTINGS (settings));
+
+  source_view = GTK_SOURCE_VIEW (self->view);
+  drawer = gtk_source_view_get_space_drawer (source_view);
+  flags = g_settings_get_flags (settings, "draw-spaces");
+
+  if (flags == 0)
+    {
+      gtk_source_space_drawer_set_enable_matrix (drawer, FALSE);
+      return;
+    }
+
+  /* Reset the matrix before setting it */
+  gtk_source_space_drawer_set_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_ALL, 
GTK_SOURCE_SPACE_TYPE_NONE);
+
+  if (flags & 1)
+    type_flags |= GTK_SOURCE_SPACE_TYPE_SPACE;
+
+  if (flags & 2)
+    type_flags |= GTK_SOURCE_SPACE_TYPE_TAB;
+
+  if (flags & 4)
+    {
+      gtk_source_space_drawer_set_types_for_locations (drawer, GTK_SOURCE_SPACE_LOCATION_ALL, 
GTK_SOURCE_SPACE_TYPE_NEWLINE);
+      type_flags |= GTK_SOURCE_SPACE_TYPE_NEWLINE;
+    }
+
+  if (flags & 8)
+    type_flags |= GTK_SOURCE_SPACE_TYPE_NBSP;
+
+  if (flags & 16)
+    location_flags |= GTK_SOURCE_SPACE_LOCATION_LEADING;
+
+  if (flags & 32)
+    location_flags |= GTK_SOURCE_SPACE_LOCATION_INSIDE_TEXT;
+
+  if (flags & 64)
+    location_flags |= GTK_SOURCE_SPACE_LOCATION_TRAILING;
+
+  if (type_flags > 0 && location_flags == 0)
+    location_flags |= GTK_SOURCE_SPACE_LOCATION_ALL;
+
+  gtk_source_space_drawer_set_enable_matrix (drawer, TRUE);
+  gtk_source_space_drawer_set_types_for_locations (drawer, location_flags, type_flags);
+}
+
 static void
 editor_page_set_settings (EditorPage         *self,
                           EditorPageSettings *settings)
@@ -278,6 +337,7 @@ static void
 editor_page_constructed (GObject *object)
 {
   EditorPage *self = (EditorPage *)object;
+  EditorApplication *app;
 
   g_assert (EDITOR_IS_PAGE (self));
 
@@ -347,6 +407,15 @@ editor_page_constructed (GObject *object)
   editor_page_document_notify_busy_cb (self, NULL, self->document);
   editor_page_document_notify_language_cb (self, NULL, self->document);
 
+  /* Setup spaces drawing */
+  app = EDITOR_APPLICATION (g_application_get_default ());
+  g_signal_connect_object (app->settings,
+                           "changed::draw-spaces",
+                           G_CALLBACK (on_draw_spaces_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+  on_draw_spaces_changed (self, "draw-spaces", app->settings);
+
   _editor_page_vim_init (self);
 }
 


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