Skip to content

Instantly share code, notes, and snippets.

@sharethewisdom
Created March 14, 2023 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharethewisdom/c0c5814fd596d8e891a64b952a00d535 to your computer and use it in GitHub Desktop.
Save sharethewisdom/c0c5814fd596d8e891a64b952a00d535 to your computer and use it in GitHub Desktop.
GTK4 snippet test

snippet test

I struggle to get snippets working... I'm probably missing a lot of pieces in the code.

I tried to put latex.rng in $XDG_DATA_HOME/gtksourceview-5/snippets/.

requirements

$ sudo apt install gcc pkg-config libgtksourceview-5-dev

compile

gcc $( pkg-config --cflags gtk4 gtksourceview-5) -o snippet-test snippet-test.c $( pkg-config --libs gtk4 gtksourceview-5)

useful links

(better latex language definitions)[https://wiki.gnome.org/Projects/GtkSourceView/LanguageDefinitions?action=AttachFile&do=view&target=latex.lang]

<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="snippets">
<choice>
<attribute name="group"/>
<attribute name="_group"/>
</choice>
<attribute name="version">
<value>1.0</value>
</attribute>
<optional>
<oneOrMore>
<element name="snippet">
<choice>
<attribute name="name"/>
<attribute name="_name"/>
</choice>
<choice>
<attribute name="description"/>
<attribute name="_description"/>
</choice>
<attribute name="trigger">begin</attribute>
<oneOrMore>
<element name="text">
<optional>
<attribute name="languages">latex</attribute>
</optional>
<text>
<![CDATA[
\begin{$1}$0\end{$1}
]]>
</text>
</element>
</oneOrMore>
<oneOrMore>
<element name="tooltip">
<attribute name="position"/>
<attribute name="text"/>
</element>
</oneOrMore>
</element>
<element name="snippet">
<choice>
<attribute name="name"/>
<attribute name="_name"/>
</choice>
<choice>
<attribute name="description"/>
<attribute name="_description"/>
</choice>
<attribute name="trigger">frac</attribute>
<oneOrMore>
<element name="text">
<optional>
<attribute name="languages">latex</attribute>
</optional>
<text>
<![CDATA[
\frac{$1}{$2}$0
]]>
</text>
</element>
</oneOrMore>
<oneOrMore>
<element name="tooltip">
<attribute name="position"/>
<attribute name="text"/>
</element>
</oneOrMore>
</element>
</oneOrMore>
</optional>
</element>
</start>
</grammar>
#include <stdio.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gtksourceview/gtksource.h>
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static void
activate (GtkApplication* app, gpointer user_data) {
GtkWidget *window, *scrolled_win, *textview;
GtkWidget *vbox, *label;
GtkSourceLanguageManager* lm = gtk_source_language_manager_get_default();
GtkSourceSnippetManager* sm = gtk_source_snippet_manager_get_default();
const gchar* cwd = ".";
const gchar* const* dirs[] = {gtk_source_snippet_manager_get_search_path(sm), &cwd, NULL};
printf("snippet manager search path ");
for (size_t i = 0; dirs[i]; i++)
printf("%s\n", *dirs[i]);
gtk_source_snippet_manager_set_search_path(sm, *dirs);
// const gchar* dirs[1] = {"."};
// gtk_source_snippet_manager_set_search_path(sm, dirs);
GtkSourceLanguage* lang = gtk_source_language_manager_guess_language(lm, "file.tex", NULL);
const gchar* langId = gtk_source_language_get_id(lang);
// printf("langId = %s\n", langId );
// GtkSourceSnippet* snippets = gtk_source_snippet_new("foo", langId);
// GtkSourceSnippet* snippets = gtk_source_snippet_manager_get_snippet(sm, NULL, langId, " ");
GtkSourceBuffer* sBuf = GTK_SOURCE_BUFFER(gtk_source_buffer_new_with_language(lang));
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Completion and Snippets");
gtk_window_set_default_size (GTK_WINDOW (window), 900, 600);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_widget_set_margin_start (vbox, 18);
gtk_widget_set_margin_end (vbox, 18);
gtk_widget_set_margin_top (vbox, 18);
gtk_widget_set_margin_bottom (vbox, 18);
gtk_window_set_child (GTK_WINDOW (window), vbox);
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), "Try typing <b>beg&lt;Tab&gt;</b> or <b>frac&lt;Tab&gt;</b> for example.");
gtk_box_append (GTK_BOX (vbox), label);
scrolled_win = gtk_scrolled_window_new();
textview = gtk_source_view_new_with_buffer(sBuf);
gtk_widget_set_hexpand(textview, TRUE);
gtk_widget_set_vexpand(textview, TRUE);
gtk_source_view_set_show_line_numbers (GTK_SOURCE_VIEW(textview), FALSE);
gtk_source_view_set_highlight_current_line (GTK_SOURCE_VIEW(textview), TRUE);
gtk_source_view_set_auto_indent (GTK_SOURCE_VIEW(textview), TRUE);
gtk_source_view_set_indent_on_tab (GTK_SOURCE_VIEW(textview), FALSE);
gtk_source_view_set_enable_snippets (GTK_SOURCE_VIEW(textview), TRUE);
printf("snippets are enabled? %i",gtk_source_view_get_enable_snippets(GTK_SOURCE_VIEW(textview)));
GtkSourceCompletion* cmp = gtk_source_view_get_completion (GTK_SOURCE_VIEW(textview));
GtkSourceCompletionSnippets* cmpSnips = gtk_source_completion_snippets_new();
gtk_source_completion_add_provider(cmp, GTK_SOURCE_COMPLETION_PROVIDER(cmpSnips));
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scrolled_win),textview);
gtk_box_append (GTK_BOX (vbox), scrolled_win);
gtk_widget_show (window);
}
int
main (int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment