Last active
September 12, 2016 14:31
-
-
Save codebrainz/78a23f54970b975e0898ae544c2f5bbd to your computer and use it in GitHub Desktop.
Attaching arbitrary data to documents in a Geany plugin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "documentmanager.h" | |
static GHashTable *geany_document_data = NULL; | |
static GData **geany_document_data_new(void) | |
{ | |
GData **doc_data = g_slice_new0(GData*); | |
g_datalist_init(doc_data); | |
return doc_data; | |
} | |
static void geany_document_data_free(gpointer pdata) | |
{ | |
if (G_LIKELY(pdata != NULL)) | |
{ | |
GData **doc_data = pdata; | |
g_datalist_clear(doc_data); | |
g_slice_free(GData*, doc_data); | |
} | |
} | |
static void geany_document_on_open(G_GNUC_UNUSED GObject *object, GeanyDocument *doc, G_GNUC_UNUSED gpointer user_data) | |
{ | |
g_hash_table_insert(geany_document_data, doc, geany_document_data_new()); | |
} | |
static void geany_document_on_close(G_GNUC_UNUSED GObject *object, GeanyDocument *doc, G_GNUC_UNUSED gpointer user_data) | |
{ | |
g_hash_table_remove(geany_document_data, doc); | |
} | |
void geany_document_manager_init(GeanyPlugin *plugin) | |
{ | |
g_return_if_fail(plugin != NULL); | |
g_return_if_fail(geany_document_data == NULL); | |
geany_document_data = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, geany_document_data_free); | |
plugin_signal_connect(plugin, NULL, "document-new", TRUE, G_CALLBACK(geany_document_on_open), NULL); | |
plugin_signal_connect(plugin, NULL, "document-open", TRUE, G_CALLBACK(geany_document_on_open), NULL); | |
plugin_signal_connect(plugin, NULL, "document-close", TRUE, G_CALLBACK(geany_document_on_close), NULL); | |
for (gsize i = 0; i < plugin->geany_data->documents_array->len; i++) | |
{ | |
GeanyDocument *doc = plugin->geany_data->documents_array->pdata[i]; | |
if (G_LIKELY(DOC_VALID(doc))) | |
g_hash_table_insert(geany_document_data, doc, geany_document_data_new()); | |
} | |
} | |
void geany_document_manager_cleanup(void) | |
{ | |
g_return_if_fail(geany_document_data != NULL); | |
g_hash_table_destroy(geany_document_data); | |
geany_document_data = NULL; | |
} | |
gpointer geany_document_get_data(const GeanyDocument *doc, const gchar *key) | |
{ | |
GData **doc_data; | |
g_return_val_if_fail(DOC_VALID(doc), NULL); | |
g_return_val_if_fail(key != NULL, NULL); | |
doc_data = g_hash_table_lookup(geany_document_data, doc); | |
g_return_val_if_fail(doc_data != NULL, NULL); | |
return g_datalist_get_data(doc_data, key); | |
} | |
void geany_document_set_data(GeanyDocument *doc, const gchar *key, gpointer data) | |
{ | |
geany_document_set_data_full(doc, key, data, NULL); | |
} | |
void geany_document_set_data_full(GeanyDocument *doc, const gchar *key, | |
gpointer data, GDestroyNotify free_func) | |
{ | |
GData **doc_data; | |
g_return_if_fail(DOC_VALID(doc)); | |
g_return_if_fail(key != NULL); | |
doc_data = g_hash_table_lookup(geany_document_data, doc); | |
g_return_if_fail(doc_data != NULL); | |
g_datalist_set_data_full(doc_data, key, data, free_func); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef GEANY_DOCUMENT_MANAGER_H | |
#define GEANY_DOCUMENT_MANAGER_H 1 | |
#include <geanyplugin.h> | |
G_BEGIN_DECLS | |
void geany_document_manager_init(GeanyPlugin *plugin); | |
void geany_document_manager_cleanup(void); | |
gpointer geany_document_get_data(const GeanyDocument *doc, const gchar *key); | |
void geany_document_set_data(GeanyDocument *doc, const gchar *key, gpointer data); | |
void geany_document_set_data_full(GeanyDocument *doc, const gchar *key, gpointer data, GDestroyNotify free_func); | |
G_END_DECLS | |
#endif /* GEANY_DOCUMENT_MANAGER_H */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "documentmanager.h" | |
static gboolean init(GeanyPlugin *plugin, gpointer pdata) | |
{ | |
geany_document_manager_init(plugin); | |
/* ... regular plugin initialization here ... */ | |
return TRUE; | |
} | |
static void cleanup(GeanyPlugin *plugin, gpointer pdata) | |
{ | |
/* ... regular plugin finalization here ... */ | |
geany_document_manager_cleanup(); | |
} | |
G_MODULE_EXPORT | |
void geany_load_module(GeanyPlugin *plugin) | |
{ | |
plugin->info->name = _("Document Data Plugin"); | |
plugin->info->description = _("A plugin which attaches data to documents"); | |
plugin->info->version = "0.1"; | |
plugin->info->author = "Matthew Brush"; | |
plugin->funcs->init = init; | |
plugin->funcs->cleanup = cleanup; | |
GEANY_PLUGIN_REGISTER(plugin, 228); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment