Skip to content

Instantly share code, notes, and snippets.

@MatrixMike
Forked from codebrainz/tabsort.c
Created February 21, 2020 01:48
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 MatrixMike/75782cb4ff68903415afc79cf9a6d1e3 to your computer and use it in GitHub Desktop.
Save MatrixMike/75782cb4ff68903415afc79cf9a6d1e3 to your computer and use it in GitHub Desktop.
Automatically Sort Geany's Document Tabs
#include <geanyplugin.h>
GeanyData *geany_data;
GeanyPlugin *geany_plugin;
GeanyFunctions *geany_functions;
PLUGIN_VERSION_CHECK(211);
PLUGIN_SET_INFO("Tab Sort",
"Automatically keeps document tabs sorted",
"v0.01",
"Matthew Brush <matt@geany.org>");
struct tab_info
{
gchar *label;
GtkWidget *page;
};
static gint compare_tab_info(const struct tab_info *t1, const struct tab_info *t2)
{
return g_utf8_collate(t2->label, t1->label);
}
static void sort_tabs_callback(GObject *geany_object, GeanyDocument *doc, gpointer user_data)
{
GtkNotebook *nb = GTK_NOTEBOOK(geany_data->main_widgets->notebook);
gint index, n_pages = gtk_notebook_get_n_pages(nb);
GSList *tab, *tabs = NULL;
gtk_notebook_set_tab_reorderable(nb, GTK_WIDGET(doc->editor->sci), FALSE);
for (index = 0; index < n_pages; index++)
{
struct tab_info *inf = g_slice_alloc(sizeof(struct tab_info));
inf->page = gtk_notebook_get_nth_page(nb, index);
inf->label = document_get_basename_for_display(document_get_from_page(index), index);
tabs = g_slist_prepend(tabs, inf);
}
tabs = g_slist_sort(tabs, (GCompareFunc) compare_tab_info);
for (tab = tabs, index = 0; tab != NULL && index < n_pages; tab = tab->next, n_pages++)
gtk_notebook_reorder_child(nb, ((struct tab_info *)tab->data)->page, index);
for (tab = tabs; tab != NULL; tab = tab->next)
{
struct tab_info *inf = tab->data;
g_free(inf->label);
g_slice_free1(sizeof(struct tab_info), inf);
}
g_slist_free(tabs);
}
static void document_notebook_set_tabs_reorderable(GtkNotebook *nb, gboolean reorderable)
{
gint index, n_pages = gtk_notebook_get_n_pages(nb);
for (index = 0; index < n_pages; index++)
gtk_notebook_set_tab_reorderable(nb, gtk_notebook_get_nth_page(nb, index), reorderable);
}
void plugin_init(GeanyData *data)
{
document_notebook_set_tabs_reorderable(GTK_NOTEBOOK(data->main_widgets->notebook), FALSE);
plugin_signal_connect(geany_plugin, NULL, "document-open", TRUE, G_CALLBACK(sort_tabs_callback), NULL);
}
void plugin_cleanup(void)
{
document_notebook_set_tabs_reorderable(GTK_NOTEBOOK(geany_data->main_widgets->notebook), TRUE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment