summaryrefslogtreecommitdiff
path: root/plugins/modelines/pluma-modeline-plugin.c
blob: 346393241c00207105233cb495e83faa3b8350c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * pluma-modeline-plugin.c
 * Emacs, Kate and Vim-style modelines support for pluma.
 * 
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
 
#ifdef HAVE_CONFIG_H
#	include <config.h>
#endif

#include <glib/gi18n-lib.h>
#include <gmodule.h>
#include "pluma-modeline-plugin.h"
#include "modeline-parser.h"

#include <pluma/pluma-debug.h>
#include <pluma/pluma-utils.h>

#define WINDOW_DATA_KEY "PlumaModelinePluginWindowData"
#define DOCUMENT_DATA_KEY "PlumaModelinePluginDocumentData"

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;

static void	pluma_modeline_plugin_activate (PlumaPlugin *plugin, PlumaWindow *window);
static void	pluma_modeline_plugin_deactivate (PlumaPlugin *plugin, PlumaWindow *window);
static GObject	*pluma_modeline_plugin_constructor (GType type, guint n_construct_properties, GObjectConstructParam *construct_param);
static void	pluma_modeline_plugin_finalize (GObject *object);

PLUMA_PLUGIN_REGISTER_TYPE(PlumaModelinePlugin, pluma_modeline_plugin)

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
pluma_modeline_plugin_class_init (PlumaModelinePluginClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	PlumaPluginClass *plugin_class = PLUMA_PLUGIN_CLASS (klass);

	object_class->constructor = pluma_modeline_plugin_constructor;
	object_class->finalize = pluma_modeline_plugin_finalize;

	plugin_class->activate = pluma_modeline_plugin_activate;
	plugin_class->deactivate = pluma_modeline_plugin_deactivate;
}

static GObject *
pluma_modeline_plugin_constructor (GType                  type,
				   guint                  n_construct_properties,
				   GObjectConstructParam *construct_param)
{
	GObject *object;
	gchar *data_dir;

	object = G_OBJECT_CLASS (pluma_modeline_plugin_parent_class)->constructor (type,
										   n_construct_properties,
										   construct_param);

	data_dir = pluma_plugin_get_data_dir (PLUMA_PLUGIN (object));

	modeline_parser_init (data_dir);

	g_free (data_dir);

	return object;
}

static void
pluma_modeline_plugin_init (PlumaModelinePlugin *plugin)
{
	pluma_debug_message (DEBUG_PLUGINS, "PlumaModelinePlugin initializing");
}

static void
pluma_modeline_plugin_finalize (GObject *object)
{
	pluma_debug_message (DEBUG_PLUGINS, "PlumaModelinePlugin finalizing");

	modeline_parser_shutdown ();

	G_OBJECT_CLASS (pluma_modeline_plugin_parent_class)->finalize (object);
}

static void
on_document_loaded_or_saved (PlumaDocument *document,
			     const GError  *error,
			     GtkSourceView *view)
{
	modeline_parser_apply_modeline (view);
}

static void
connect_handlers (PlumaView *view)
{
	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
disconnect_handlers (PlumaView *view)
{
	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
on_window_tab_added (PlumaWindow *window,
		     PlumaTab *tab,
		     gpointer user_data)
{
	connect_handlers (pluma_tab_get_view (tab));
}

static void
on_window_tab_removed (PlumaWindow *window,
		       PlumaTab *tab,
		       gpointer user_data)
{
	disconnect_handlers (pluma_tab_get_view (tab));
}

static void
pluma_modeline_plugin_activate (PlumaPlugin *plugin,
				PlumaWindow *window)
{
	WindowData *wdata;
	GList *views;
	GList *l;

	pluma_debug (DEBUG_PLUGINS);

	views = pluma_window_get_views (window);
	for (l = views; l != NULL; l = l->next)
	{
		connect_handlers (PLUMA_VIEW (l->data));
		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
pluma_modeline_plugin_deactivate (PlumaPlugin *plugin,
				  PlumaWindow *window)
{
	WindowData *wdata;
	GList *views;
	GList *l;

	pluma_debug (DEBUG_PLUGINS);

	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);

	views = pluma_window_get_views (window);

	for (l = views; l != NULL; l = l->next)
	{
		disconnect_handlers (PLUMA_VIEW (l->data));
		
		modeline_parser_deactivate (GTK_SOURCE_VIEW (l->data));
	}
	
	g_list_free (views);
}