xed/plugins/modelines/xedit-modeline-plugin.c

249 lines
6.2 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2016-01-25 08:13:49 -06:00
* xedit-modeline-plugin.c
* Emacs, Kate and Vim-style modelines support for xedit.
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2005-2007 - Steve Frécinaux <code@istique.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib/gi18n-lib.h>
#include <gmodule.h>
2016-01-25 08:13:49 -06:00
#include "xedit-modeline-plugin.h"
2011-11-07 13:46:58 -06:00
#include "modeline-parser.h"
2016-01-25 08:13:49 -06:00
#include <xedit/xedit-debug.h>
#include <xedit/xedit-utils.h>
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
#define WINDOW_DATA_KEY "XeditModelinePluginWindowData"
#define DOCUMENT_DATA_KEY "XeditModelinePluginDocumentData"
2011-11-07 13:46:58 -06:00
typedef struct
{
gulong tab_added_handler_id;
gulong tab_removed_handler_id;
} WindowData;
typedef struct
{
gulong document_loaded_handler_id;
gulong document_saved_handler_id;
} DocumentData;
2016-01-25 08:13:49 -06:00
static void xedit_modeline_plugin_activate (XeditPlugin *plugin, XeditWindow *window);
static void xedit_modeline_plugin_deactivate (XeditPlugin *plugin, XeditWindow *window);
static GObject *xedit_modeline_plugin_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_param);
static void xedit_modeline_plugin_finalize (GObject *object);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
XEDIT_PLUGIN_REGISTER_TYPE(XeditModelinePlugin, xedit_modeline_plugin)
2011-11-07 13:46:58 -06:00
static void
window_data_free (WindowData *wdata)
{
g_slice_free (WindowData, wdata);
}
static void
document_data_free (DocumentData *ddata)
{
g_slice_free (DocumentData, ddata);
}
static void
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_class_init (XeditModelinePluginClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2016-01-25 08:13:49 -06:00
XeditPluginClass *plugin_class = XEDIT_PLUGIN_CLASS (klass);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
object_class->constructor = xedit_modeline_plugin_constructor;
object_class->finalize = xedit_modeline_plugin_finalize;
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
plugin_class->activate = xedit_modeline_plugin_activate;
plugin_class->deactivate = xedit_modeline_plugin_deactivate;
2011-11-07 13:46:58 -06:00
}
static GObject *
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_constructor (GType type,
2011-11-07 13:46:58 -06:00
guint n_construct_properties,
GObjectConstructParam *construct_param)
{
GObject *object;
gchar *data_dir;
2016-01-25 08:13:49 -06:00
object = G_OBJECT_CLASS (xedit_modeline_plugin_parent_class)->constructor (type,
2011-11-07 13:46:58 -06:00
n_construct_properties,
construct_param);
2016-01-25 08:13:49 -06:00
data_dir = xedit_plugin_get_data_dir (XEDIT_PLUGIN (object));
2011-11-07 13:46:58 -06:00
modeline_parser_init (data_dir);
g_free (data_dir);
return object;
}
static void
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_init (XeditModelinePlugin *plugin)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "XeditModelinePlugin initializing");
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2016-01-25 08:13:49 -06:00
xedit_debug_message (DEBUG_PLUGINS, "XeditModelinePlugin finalizing");
2011-11-07 13:46:58 -06:00
modeline_parser_shutdown ();
2016-01-25 08:13:49 -06:00
G_OBJECT_CLASS (xedit_modeline_plugin_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
on_document_loaded_or_saved (XeditDocument *document,
2011-11-07 13:46:58 -06:00
const GError *error,
GtkSourceView *view)
{
modeline_parser_apply_modeline (view);
}
static void
2016-01-25 08:13:49 -06:00
connect_handlers (XeditView *view)
2011-11-07 13:46:58 -06:00
{
DocumentData *data;
GtkTextBuffer *doc;
doc = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
data = g_slice_new (DocumentData);
data->document_loaded_handler_id =
g_signal_connect (doc, "loaded",
G_CALLBACK (on_document_loaded_or_saved),
view);
data->document_saved_handler_id =
g_signal_connect (doc, "saved",
G_CALLBACK (on_document_loaded_or_saved),
view);
g_object_set_data_full (G_OBJECT (doc), DOCUMENT_DATA_KEY,
data, (GDestroyNotify) document_data_free);
}
static void
2016-01-25 08:13:49 -06:00
disconnect_handlers (XeditView *view)
2011-11-07 13:46:58 -06:00
{
DocumentData *data;
GtkTextBuffer *doc;
doc = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
data = g_object_steal_data (G_OBJECT (doc), DOCUMENT_DATA_KEY);
if (data)
{
g_signal_handler_disconnect (doc, data->document_loaded_handler_id);
g_signal_handler_disconnect (doc, data->document_saved_handler_id);
document_data_free (data);
}
else
{
g_warning ("Modeline handlers not found");
}
}
static void
2016-01-25 08:13:49 -06:00
on_window_tab_added (XeditWindow *window,
XeditTab *tab,
2011-11-07 13:46:58 -06:00
gpointer user_data)
{
2016-01-25 08:13:49 -06:00
connect_handlers (xedit_tab_get_view (tab));
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
on_window_tab_removed (XeditWindow *window,
XeditTab *tab,
2011-11-07 13:46:58 -06:00
gpointer user_data)
{
2016-01-25 08:13:49 -06:00
disconnect_handlers (xedit_tab_get_view (tab));
2011-11-07 13:46:58 -06:00
}
static void
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_activate (XeditPlugin *plugin,
XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
WindowData *wdata;
GList *views;
GList *l;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_PLUGINS);
2011-11-07 13:46:58 -06:00
2016-01-25 08:13:49 -06:00
views = xedit_window_get_views (window);
2011-11-07 13:46:58 -06:00
for (l = views; l != NULL; l = l->next)
{
2016-01-25 08:13:49 -06:00
connect_handlers (XEDIT_VIEW (l->data));
2011-11-07 13:46:58 -06:00
modeline_parser_apply_modeline (GTK_SOURCE_VIEW (l->data));
}
g_list_free (views);
wdata = g_slice_new (WindowData);
wdata->tab_added_handler_id =
g_signal_connect (window, "tab-added",
G_CALLBACK (on_window_tab_added), NULL);
wdata->tab_removed_handler_id =
g_signal_connect (window, "tab-removed",
G_CALLBACK (on_window_tab_removed), NULL);
g_object_set_data_full (G_OBJECT (window), WINDOW_DATA_KEY,
wdata, (GDestroyNotify) window_data_free);
}
static void
2016-01-25 08:13:49 -06:00
xedit_modeline_plugin_deactivate (XeditPlugin *plugin,
XeditWindow *window)
2011-11-07 13:46:58 -06:00
{
WindowData *wdata;
GList *views;
GList *l;
2016-01-25 08:13:49 -06:00
xedit_debug (DEBUG_PLUGINS);
2011-11-07 13:46:58 -06:00
wdata = g_object_steal_data (G_OBJECT (window), WINDOW_DATA_KEY);
g_signal_handler_disconnect (window, wdata->tab_added_handler_id);
g_signal_handler_disconnect (window, wdata->tab_removed_handler_id);
window_data_free (wdata);
2016-01-25 08:13:49 -06:00
views = xedit_window_get_views (window);
2011-11-07 13:46:58 -06:00
for (l = views; l != NULL; l = l->next)
{
2016-01-25 08:13:49 -06:00
disconnect_handlers (XEDIT_VIEW (l->data));
2011-11-07 13:46:58 -06:00
modeline_parser_deactivate (GTK_SOURCE_VIEW (l->data));
}
g_list_free (views);
}