summaryrefslogtreecommitdiffstats
path: root/plugins/quickhighlight/gedit-quick-highlight-plugin.c
blob: 7b4289dc77cbafadaa01ff1db3a681ea49ab5014 (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * gedit-quick-highlight-plugin.c
 *
 * Copyright (C) 2018 Martin Blanchard
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"

#include <glib/gi18n.h>

#include <gedit/gedit-debug.h>
#include <gedit/gedit-document.h>
#include <gedit/gedit-view-activatable.h>
#include <gedit/gedit-view.h>

#include "gedit-quick-highlight-plugin.h"

struct _GeditQuickHighlightPluginPrivate
{
	GeditView              *view;

	GeditDocument          *buffer;
	GtkTextMark            *insert_mark;

	GtkSourceSearchContext *search_context;
	GtkSourceStyle         *style;

	gulong                  buffer_handler_id;
	gulong                  mark_set_handler_id;
	gulong                  delete_range_handler_id;
	gulong                  style_scheme_handler_id;

	guint                   queued_highlight;
};

enum
{
	PROP_0,
	PROP_VIEW
};

static void gedit_view_activatable_iface_init (GeditViewActivatableInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED (GeditQuickHighlightPlugin,
                                gedit_quick_highlight_plugin,
                                PEAS_TYPE_EXTENSION_BASE,
                                0,
                                G_IMPLEMENT_INTERFACE_DYNAMIC (GEDIT_TYPE_VIEW_ACTIVATABLE,
                                                               gedit_view_activatable_iface_init)
                                G_ADD_PRIVATE_DYNAMIC (GeditQuickHighlightPlugin))

static void gedit_quick_highlight_plugin_notify_buffer_cb (GObject *object, GParamSpec *pspec, gpointer user_data);
static void gedit_quick_highlight_plugin_mark_set_cb (GtkTextBuffer *textbuffer, GtkTextIter *location, GtkTextMark *mark, gpointer user_data);
static void gedit_quick_highlight_plugin_delete_range_cb (GtkTextBuffer *textbuffer, GtkTextIter *start, GtkTextIter *end, gpointer user_data);
static void gedit_quick_highlight_plugin_notify_style_scheme_cb (GObject *object, GParamSpec *pspec, gpointer user_data);

static void
gedit_quick_highlight_plugin_load_style (GeditQuickHighlightPlugin *plugin)
{
	GtkSourceStyleScheme *style_scheme;
	GtkSourceStyle *style = NULL;

	g_return_if_fail (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	if (plugin->priv->buffer == NULL)
	{
		return;
	}

	gedit_debug (DEBUG_PLUGINS);

	g_clear_object (&plugin->priv->style);

	style_scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (plugin->priv->buffer));

	if (style_scheme != NULL)
	{
		style = gtk_source_style_scheme_get_style (style_scheme, "quick-highlight-match");

		if (style != NULL)
		{
			plugin->priv->style = gtk_source_style_copy (style);
		}
	}
}

static gboolean
gedit_quick_highlight_plugin_highlight_worker (gpointer user_data)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (user_data);
	GtkSourceSearchSettings *search_settings;
	GtkTextIter start, end;
	g_autofree gchar *text = NULL;

	g_assert (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	plugin->priv->queued_highlight = 0;

	if (!gtk_text_buffer_get_selection_bounds (GTK_TEXT_BUFFER (plugin->priv->buffer), &start, &end))
	{
		g_clear_object (&plugin->priv->search_context);
		return G_SOURCE_REMOVE;
	}

	if (gtk_text_iter_get_line (&start) != gtk_text_iter_get_line (&end))
	{
		g_clear_object (&plugin->priv->search_context);
		return G_SOURCE_REMOVE;
	}

	if (plugin->priv->search_context == NULL)
	{
		search_settings =
			g_object_new (GTK_SOURCE_TYPE_SEARCH_SETTINGS,
			              "at-word-boundaries", FALSE,
			              "case-sensitive", TRUE,
			              "regex-enabled", FALSE,
			              NULL);

		plugin->priv->search_context =
			g_object_new (GTK_SOURCE_TYPE_SEARCH_CONTEXT,
			              "buffer", plugin->priv->buffer,
			              "highlight", FALSE,
			              "match-style", plugin->priv->style,
			              "settings", search_settings,
			              NULL);

		g_object_unref (search_settings);
	}
	else
	{
		search_settings =
			gtk_source_search_context_get_settings (plugin->priv->search_context);
	}

	text = gtk_text_iter_get_slice (&start, &end);

	gtk_source_search_settings_set_search_text (search_settings, text);

	gtk_source_search_context_set_highlight (plugin->priv->search_context, TRUE);

	return G_SOURCE_REMOVE;
}

static void
gedit_quick_highlight_plugin_queue_update (GeditQuickHighlightPlugin *plugin)
{
	g_return_if_fail (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	if (plugin->priv->queued_highlight != 0)
	{
		return;
	}

	plugin->priv->queued_highlight =
		gdk_threads_add_idle_full (G_PRIORITY_LOW,
		                           gedit_quick_highlight_plugin_highlight_worker,
		                           g_object_ref (plugin),
		                           g_object_unref);
}

static void
gedit_quick_highlight_plugin_notify_weak_buffer_cb (gpointer data,
                                                    GObject *where_the_object_was)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (data);

	g_assert (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	plugin->priv->style_scheme_handler_id = 0;
	plugin->priv->buffer = NULL;
}

static void
gedit_quick_highlight_plugin_unref_weak_buffer (GeditQuickHighlightPlugin *plugin)
{
	g_return_if_fail (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	if (plugin->priv->buffer == NULL)
	{
		return;
	}

	if (plugin->priv->delete_range_handler_id > 0)
	{
		g_signal_handler_disconnect (plugin->priv->buffer,
		                             plugin->priv->delete_range_handler_id);
		plugin->priv->delete_range_handler_id = 0;
	}

	if (plugin->priv->mark_set_handler_id > 0)
	{
		g_signal_handler_disconnect (plugin->priv->buffer,
		                             plugin->priv->mark_set_handler_id);
		plugin->priv->mark_set_handler_id = 0;
	}

	if (plugin->priv->style_scheme_handler_id > 0)
	{
		g_signal_handler_disconnect (plugin->priv->buffer,
		                             plugin->priv->style_scheme_handler_id);
		plugin->priv->style_scheme_handler_id = 0;
	}

	g_object_weak_unref (G_OBJECT (plugin->priv->buffer),
	                     gedit_quick_highlight_plugin_notify_weak_buffer_cb,
	                     plugin);

	plugin->priv->buffer = NULL;
}

static void
gedit_quick_highlight_plugin_set_buffer (GeditQuickHighlightPlugin *plugin,
                                         GeditDocument             *buffer)
{
	g_return_if_fail (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));
	g_return_if_fail (GEDIT_IS_DOCUMENT (buffer));

	if (plugin->priv->buffer == buffer)
	{
		return;
	}

	gedit_debug (DEBUG_PLUGINS);

	gedit_quick_highlight_plugin_unref_weak_buffer (plugin);

	plugin->priv->buffer = buffer;

	if (plugin->priv->buffer != NULL)
	{
		g_object_weak_ref (G_OBJECT (plugin->priv->buffer),
		                   gedit_quick_highlight_plugin_notify_weak_buffer_cb,
		                   plugin);

		plugin->priv->style_scheme_handler_id =
			g_signal_connect (plugin->priv->buffer,
			                  "notify::style-scheme",
			                  G_CALLBACK (gedit_quick_highlight_plugin_notify_style_scheme_cb),
			                  plugin);

		plugin->priv->mark_set_handler_id =
			g_signal_connect (plugin->priv->buffer,
			                  "mark-set",
			                  G_CALLBACK (gedit_quick_highlight_plugin_mark_set_cb),
			                  plugin);

		plugin->priv->delete_range_handler_id =
			g_signal_connect (plugin->priv->buffer,
			                  "delete-range",
			                  G_CALLBACK (gedit_quick_highlight_plugin_delete_range_cb),
			                  plugin);

		plugin->priv->insert_mark =
			gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (plugin->priv->buffer));

		gedit_quick_highlight_plugin_load_style (plugin);

		gedit_quick_highlight_plugin_queue_update (plugin);
	}
}

static void
gedit_quick_highlight_plugin_mark_set_cb (GtkTextBuffer *textbuffer,
                                          GtkTextIter   *location,
                                          GtkTextMark   *mark,
                                          gpointer       user_data)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (user_data);

	g_assert (GEDIT_QUICK_HIGHLIGHT_PLUGIN (plugin));

	if G_LIKELY (mark != plugin->priv->insert_mark)
	{
		return;
	}

	gedit_quick_highlight_plugin_queue_update (plugin);
}

static void
gedit_quick_highlight_plugin_delete_range_cb (GtkTextBuffer *textbuffer,
                                              GtkTextIter   *start,
                                              GtkTextIter   *end,
                                              gpointer       user_data)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (user_data);

	g_assert (GEDIT_QUICK_HIGHLIGHT_PLUGIN (plugin));

	gedit_quick_highlight_plugin_queue_update (plugin);
}

static void
gedit_quick_highlight_plugin_notify_style_scheme_cb (GObject    *object,
                                                     GParamSpec *pspec,
                                                     gpointer    user_data)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (user_data);

	g_assert (GEDIT_QUICK_HIGHLIGHT_PLUGIN (plugin));

	gedit_quick_highlight_plugin_load_style (plugin);
}

static void
gedit_quick_highlight_plugin_dispose (GObject *object)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (object);

	g_clear_object (&plugin->priv->search_context);

	gedit_quick_highlight_plugin_unref_weak_buffer (plugin);

	g_clear_object (&plugin->priv->view);

	G_OBJECT_CLASS (gedit_quick_highlight_plugin_parent_class)->dispose (object);
}

static void
gedit_quick_highlight_plugin_finalize (GObject *object)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (object);

	g_clear_object (&plugin->priv->style);

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

static void
gedit_quick_highlight_plugin_get_property (GObject    *object,
                                           guint       prop_id,
                                           GValue     *value,
                                           GParamSpec *pspec)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (object);

	switch (prop_id)
	{
		case PROP_VIEW:
			g_value_set_object (value, plugin->priv->view);
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gedit_quick_highlight_plugin_set_property (GObject      *object,
                                           guint         prop_id,
                                           const GValue *value,
                                           GParamSpec   *pspec)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (object);

	switch (prop_id)
	{
		case PROP_VIEW:
			plugin->priv->view = GEDIT_VIEW (g_value_dup_object (value));
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
			break;
	}
}

static void
gedit_quick_highlight_plugin_class_init (GeditQuickHighlightPluginClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->dispose = gedit_quick_highlight_plugin_dispose;
	object_class->finalize = gedit_quick_highlight_plugin_finalize;
	object_class->set_property = gedit_quick_highlight_plugin_set_property;
	object_class->get_property = gedit_quick_highlight_plugin_get_property;

	g_object_class_override_property (object_class, PROP_VIEW, "view");
}

static void
gedit_quick_highlight_plugin_class_finalize (GeditQuickHighlightPluginClass *klass)
{
}

static void
gedit_quick_highlight_plugin_init (GeditQuickHighlightPlugin *plugin)
{
	plugin->priv = gedit_quick_highlight_plugin_get_instance_private (plugin);
}

static void
gedit_quick_highlight_plugin_activate (GeditViewActivatable *activatable)
{
	GeditQuickHighlightPlugin *plugin;
	GtkTextBuffer *buffer;

	gedit_debug (DEBUG_PLUGINS);

	plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (activatable);

	plugin->priv->buffer_handler_id =
		g_signal_connect (plugin->priv->view,
		                  "notify::buffer",
		                  G_CALLBACK (gedit_quick_highlight_plugin_notify_buffer_cb),
		                  plugin);

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plugin->priv->view));

	gedit_quick_highlight_plugin_set_buffer (plugin, GEDIT_DOCUMENT (buffer));
}

static void
gedit_quick_highlight_plugin_deactivate (GeditViewActivatable *activatable)
{
	GeditQuickHighlightPlugin *plugin;

	gedit_debug (DEBUG_PLUGINS);

	plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (activatable);

	g_clear_object (&plugin->priv->style);
	g_clear_object (&plugin->priv->search_context);

	gedit_quick_highlight_plugin_unref_weak_buffer (plugin);

	if (plugin->priv->view != NULL && plugin->priv->buffer_handler_id > 0)
	{
		g_signal_handler_disconnect (plugin->priv->view,
		                             plugin->priv->buffer_handler_id);
		plugin->priv->buffer_handler_id = 0;
	}
}

static void
gedit_quick_highlight_plugin_notify_buffer_cb (GObject    *object,
                                               GParamSpec *pspec,
                                               gpointer    user_data)
{
	GeditQuickHighlightPlugin *plugin = GEDIT_QUICK_HIGHLIGHT_PLUGIN (user_data);
	GtkTextBuffer *buffer;

	g_assert (GEDIT_IS_QUICK_HIGHLIGHT_PLUGIN (plugin));

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plugin->priv->view));

	gedit_quick_highlight_plugin_set_buffer (plugin, GEDIT_DOCUMENT (buffer));
}

static void
gedit_view_activatable_iface_init (GeditViewActivatableInterface *iface)
{
	iface->activate = gedit_quick_highlight_plugin_activate;
	iface->deactivate = gedit_quick_highlight_plugin_deactivate;
}

G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
	gedit_quick_highlight_plugin_register_type (G_TYPE_MODULE (module));

	peas_object_module_register_extension_type (module,
	                                            GEDIT_TYPE_VIEW_ACTIVATABLE,
	                                            GEDIT_TYPE_QUICK_HIGHLIGHT_PLUGIN);
}

/* ex:set ts=8 noet: */