gspell-inline-checker-text-buffer.c 35.6 KB
Newer Older
1
/*
2
 * This file is part of gspell, a spell-checking library.
3
 *
4
 * Copyright 2002 - Paolo Maggi
5
 * Copyright 2015, 2016, 2017 - Sébastien Wilmet
6
 *
7 8 9 10
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * This library is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
16
 *
17 18
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 20
 */

21 22
/* This is a modified version of GtkSpell 2.0.5 (gtkspell.sf.net)
 * Copyright 2002 - Evan Martin
23 24
 */

25
#ifdef HAVE_CONFIG_H
26 27 28
#include <config.h>
#endif

29
#include "gspell-inline-checker-text-buffer.h"
30
#include <string.h>
31
#include <glib/gi18n-lib.h>
32
#include "gspellregion.h"
33
#include "gspell-checker.h"
34
#include "gspell-context-menu.h"
35
#include "gspell-current-word-policy.h"
36
#include "gspell-text-buffer.h"
37
#include "gspell-text-iter.h"
38
#include "gspell-utils.h"
39

40
struct _GspellInlineCheckerTextBuffer
41 42 43 44
{
	GObject parent;

	GtkTextBuffer *buffer;
45
	GspellChecker *spell_checker;
46 47 48 49

	/* List of GtkTextView* */
	GSList *views;

50
	GtkTextTag *highlight_tag;
51
	GtkTextTag *no_spell_check_tag;
52 53 54

	GtkTextMark *mark_click;

55
	GspellRegion *scan_region;
56
	guint timeout_id;
57

58
	GspellCurrentWordPolicy *current_word_policy;
59

60 61 62 63 64 65 66
	/* If the unit test mode is enabled, there is no timeouts, and the whole
	 * buffer is scanned synchronously.
	 * The unit test mode tries to follow as most as possible the same code
	 * paths as the real code paths, otherwise the unit tests would be
	 * useless. As such, the function names reflect the real code paths.
	 */
	guint unit_test_mode : 1;
67 68 69 70 71 72 73 74
};

enum
{
	PROP_0,
	PROP_BUFFER,
};

75 76 77 78 79 80
typedef enum
{
	ADJUST_MODE_STRICTLY_INSIDE_WORD,
	ADJUST_MODE_INCLUDE_NEIGHBORS,
} AdjustMode;

81
#define INLINE_CHECKER_TEXT_BUFFER_KEY "GspellInlineCheckerTextBufferID"
82 83 84 85

/* Timeout durations in milliseconds. Writing and deleting text should be smooth
 * and responsive.
 */
86
#define TIMEOUT_DURATION_BUFFER_MODIFIED 16
87 88
#define TIMEOUT_DURATION_DRAWING 20

89 90
#define PERF_DEBUG FALSE

91
G_DEFINE_TYPE (GspellInlineCheckerTextBuffer, _gspell_inline_checker_text_buffer, G_TYPE_OBJECT)
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
/* Remove the highlight_tag only if present. If gtk_text_buffer_remove_tag() is
 * called when the tag is not present, GtkTextView anyway queues a redraw, which
 * we want to avoid (it can lead to an infinite loop).
 */
static void
remove_highlight_tag_if_present (GspellInlineCheckerTextBuffer *spell,
				 const GtkTextIter             *start,
				 const GtkTextIter             *end)
{
	gboolean remove = FALSE;

	if (gtk_text_iter_has_tag (start, spell->highlight_tag))
	{
		remove = TRUE;
	}
	else
	{
		GtkTextIter iter = *start;

		if (gtk_text_iter_forward_to_tag_toggle (&iter, spell->highlight_tag) &&
		    gtk_text_iter_compare (&iter, end) < 0)
		{
			remove = TRUE;
		}
	}

	if (remove)
	{
		gtk_text_buffer_remove_tag (spell->buffer,
					    spell->highlight_tag,
					    start,
					    end);
	}
}

128 129 130 131 132 133 134 135
static void
adjust_iters (GtkTextIter *start,
	      GtkTextIter *end,
	      AdjustMode   mode)
{
	switch (mode)
	{
		case ADJUST_MODE_STRICTLY_INSIDE_WORD:
136 137
			if (_gspell_text_iter_inside_word (start) &&
			    !_gspell_text_iter_starts_word (start))
138
			{
139
				_gspell_text_iter_backward_word_start (start);
140 141
			}

142 143
			if (_gspell_text_iter_inside_word (end) &&
			    !_gspell_text_iter_starts_word (end))
144
			{
145
				_gspell_text_iter_forward_word_end (end);
146 147 148 149
			}
			break;

		case ADJUST_MODE_INCLUDE_NEIGHBORS:
150 151 152
			if (_gspell_text_iter_ends_word (start) ||
			    (_gspell_text_iter_inside_word (start) &&
			     !_gspell_text_iter_starts_word (start)))
153
			{
154
				_gspell_text_iter_backward_word_start (start);
155 156
			}

157
			if (_gspell_text_iter_inside_word (end))
158
			{
159
				_gspell_text_iter_forward_word_end (end);
160 161 162 163 164 165 166 167
			}
			break;

		default:
			g_assert_not_reached ();
	}
}

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
/* Free *attrs with g_free() when no longer needed. */
static void
get_pango_log_attrs (const gchar   *text,
		     PangoLogAttr **attrs,
		     gint          *n_attrs)
{
	*n_attrs = g_utf8_strlen (text, -1) + 1;
	*attrs = g_new0 (PangoLogAttr, *n_attrs);

	pango_get_log_attrs (text,
			     strlen (text),
			     -1,
			     NULL,
			     *attrs,
			     *n_attrs);

	_gspell_utils_improve_word_boundaries (text, *attrs, *n_attrs);
}

static gboolean
should_apply_tag_to_misspelled_word (GspellInlineCheckerTextBuffer *spell,
				     const GtkTextIter             *word_start,
				     const GtkTextIter             *word_end)
{
	GtkTextIter iter;

	if (spell->no_spell_check_tag == NULL)
	{
		return TRUE;
	}

	if (gtk_text_iter_has_tag (word_start, spell->no_spell_check_tag))
	{
		return FALSE;
	}

	iter = *word_start;
	if (!gtk_text_iter_forward_to_tag_toggle (&iter, spell->no_spell_check_tag))
	{
		return TRUE;
	}

	return gtk_text_iter_compare (word_end, &iter) <= 0;
}

/* A first implementation of this function used the _gspell_text_iter_*()
 * functions in a loop to navigate through words between @start and @end.
 * But the _gspell_text_iter_*() functions are *slow*. So a new implementation
 * has been written to reduce the number of calls to GtkTextView functions, and
 * it's up to 20x faster! (200 ms -> 10 ms).
 * And there is most probably still room for performance improvements.
 */
220
static void
221 222 223
check_subregion (GspellInlineCheckerTextBuffer *spell,
		 GtkTextIter                   *start,
		 GtkTextIter                   *end)
224
{
225 226 227 228 229 230 231 232 233
	gchar *text;
	const gchar *cur_text_pos;
	const gchar *word_start;
	gint word_start_char_pos;
	PangoLogAttr *attrs;
	gint n_attrs;
	gint attr_num;
	gint start_offset;

234
	g_return_if_fail (gtk_text_iter_compare (start, end) <= 0);
235

236
	adjust_iters (start, end, ADJUST_MODE_STRICTLY_INSIDE_WORD);
237 238

	gtk_text_buffer_remove_tag (spell->buffer,
239
				    spell->highlight_tag,
240 241
				    start,
				    end);
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

	if (spell->spell_checker == NULL ||
	    gspell_checker_get_language (spell->spell_checker) == NULL)
	{
		return;
	}

	text = gtk_text_iter_get_slice (start, end);

	get_pango_log_attrs (text, &attrs, &n_attrs);

	attr_num = 0;
	cur_text_pos = text;
	word_start = NULL;
	word_start_char_pos = 0;

	start_offset = gtk_text_iter_get_offset (start);

	while (attr_num < n_attrs)
	{
		PangoLogAttr *cur_attr = &attrs[attr_num];

		if (word_start != NULL &&
		    cur_attr->is_word_end)
		{
			gint word_byte_length;
			gboolean misspelled;
			GError *error = NULL;

			if (cur_text_pos != NULL)
			{
				word_byte_length = cur_text_pos - word_start;
			}
			else
			{
				word_byte_length = -1;
			}

			misspelled = !gspell_checker_check_word (spell->spell_checker,
								 word_start,
								 word_byte_length,
								 &error);

			if (error != NULL)
			{
				g_warning ("Inline spell checker: %s", error->message);
				g_clear_error (&error);
			}

			if (misspelled)
			{
				gint word_start_offset;
				gint word_end_offset;
				GtkTextIter word_start_iter;
				GtkTextIter word_end_iter;

				word_start_offset = start_offset + word_start_char_pos;
				word_end_offset = start_offset + attr_num;

				gtk_text_buffer_get_iter_at_offset (spell->buffer,
								    &word_start_iter,
								    word_start_offset);

				gtk_text_buffer_get_iter_at_offset (spell->buffer,
								    &word_end_iter,
								    word_end_offset);

309 310 311 312 313
				/* FIXME: it's a bit stupid to spell-check words
				 * in the no-spell-check region. The relevant
				 * word boundaries in the PangoLogAttr array
				 * should be removed beforehand.
				 */
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
				if (should_apply_tag_to_misspelled_word (spell, &word_start_iter, &word_end_iter))
				{
					gtk_text_buffer_apply_tag (spell->buffer,
								   spell->highlight_tag,
								   &word_start_iter,
								   &word_end_iter);
				}
			}

			/* Find next word start. */
			word_start = NULL;
		}

		if (word_start == NULL &&
		    cur_attr->is_word_start)
		{
			word_start = cur_text_pos;
			word_start_char_pos = attr_num;
		}

		if (cur_text_pos == NULL &&
		    attr_num != n_attrs - 1)
		{
			g_warning ("%s(): problem in loop iteration, attr_num=%d but should be %d.",
				   G_STRFUNC,
				   attr_num,
				   n_attrs - 1);
			break;
		}

		attr_num++;
		cur_text_pos = g_utf8_find_next_char (cur_text_pos, NULL);
	}

	g_free (text);
	g_free (attrs);
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
}

static void
get_visible_region (GtkTextView *view,
		    GtkTextIter *start,
		    GtkTextIter *end)
{
	GdkRectangle visible_rect;

	gtk_text_view_get_visible_rect (view, &visible_rect);

	gtk_text_view_get_line_at_y (view,
				     start,
				     visible_rect.y,
				     NULL);

	gtk_text_view_get_line_at_y (view,
				     end,
				     visible_rect.y + visible_rect.height,
				     NULL);

	gtk_text_iter_backward_line (start);
	gtk_text_iter_forward_line (end);
}

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
/* Returns TRUE if there is a current word. */
static gboolean
get_current_word_boundaries (GtkTextBuffer *buffer,
			     GtkTextIter   *current_word_start,
			     GtkTextIter   *current_word_end)
{
	GtkTextIter insert_iter;

	g_assert (current_word_start != NULL);
	g_assert (current_word_end != NULL);

	if (gtk_text_buffer_get_has_selection (buffer))
	{
		return FALSE;
	}

	gtk_text_buffer_get_iter_at_mark (buffer,
					  &insert_iter,
					  gtk_text_buffer_get_insert (buffer));

	*current_word_start = insert_iter;
	*current_word_end = insert_iter;

398 399 400 401
	adjust_iters (current_word_start, current_word_end, ADJUST_MODE_INCLUDE_NEIGHBORS);

	return (!gtk_text_iter_equal (current_word_start, &insert_iter) ||
		!gtk_text_iter_equal (current_word_end, &insert_iter));
402 403
}

404
static void
405 406
check_visible_region_in_view (GspellInlineCheckerTextBuffer *spell,
			      GtkTextView                   *view)
407 408 409
{
	GtkTextIter visible_start;
	GtkTextIter visible_end;
410 411
	GspellRegion *intersect;
	GspellRegionIter intersect_iter;
412 413 414 415 416 417

	if (spell->scan_region == NULL)
	{
		return;
	}

418 419 420 421 422 423
	if (view != NULL)
	{
		get_visible_region (view, &visible_start, &visible_end);
	}
	else
	{
424
		g_assert (spell->unit_test_mode);
425 426
		gtk_text_buffer_get_bounds (spell->buffer, &visible_start, &visible_end);
	}
427

428 429 430
	intersect = _gspell_region_intersect_subregion (spell->scan_region,
							&visible_start,
							&visible_end);
431

432
	if (_gspell_region_is_empty (intersect))
433
	{
434
		g_clear_object (&intersect);
435 436 437
		return;
	}

438
	if (!_gspell_current_word_policy_get_check_current_word (spell->current_word_policy))
439 440 441 442 443 444 445 446
	{
		GtkTextIter current_word_start;
		GtkTextIter current_word_end;

		if (get_current_word_boundaries (spell->buffer,
						 &current_word_start,
						 &current_word_end))
		{
447 448 449
			remove_highlight_tag_if_present (spell,
							 &current_word_start,
							 &current_word_end);
450

451 452 453
			_gspell_region_subtract_subregion (intersect,
							   &current_word_start,
							   &current_word_end);
454 455 456 457

			/* Be sure that the current word will be re-checked
			 * later when it will no longer be the current word.
			 */
458 459 460
			_gspell_region_add_subregion (spell->scan_region,
						      &current_word_start,
						      &current_word_end);
461

462 463 464 465 466 467
			if (_gspell_region_is_empty (intersect))
			{
				g_clear_object (&intersect);
				return;
			}
		}
468 469
	}

470
	_gspell_region_get_start_region_iter (intersect, &intersect_iter);
471

472
	while (!_gspell_region_iter_is_end (&intersect_iter))
473 474 475
	{
		GtkTextIter start;
		GtkTextIter end;
476 477
		GtkTextIter orig_start;
		GtkTextIter orig_end;
478
		gboolean bug = FALSE;
479

480
		if (!_gspell_region_iter_get_subregion (&intersect_iter, &start, &end))
481
		{
482
			break;
483 484
		}

485 486 487
		orig_start = start;
		orig_end = end;

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
		{
#if PERF_DEBUG
			GTimer *timer;

			g_print ("check_subregion [%d, %d]\n",
				 gtk_text_iter_get_offset (&start),
				 gtk_text_iter_get_offset (&end));

			timer = g_timer_new ();
#endif

			check_subregion (spell, &start, &end);

#if PERF_DEBUG
			g_print ("check_subregion took %lf ms.\n\n",
				 1000 * g_timer_elapsed (timer, NULL));
			g_timer_destroy (timer);
#endif
		}
507

508
		/* Ensure that we don't have an infinite loop. We must subtract
509 510
		 * from scan_region at least [orig_start, orig_end], otherwise
		 * we will re-check the same subregion again and again.
511
		 */
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		if (gtk_text_iter_compare (&orig_start, &start) < 0)
		{
			g_warning ("Should not reach this code path.");
			bug = TRUE;
			start = orig_start;
		}
		if (gtk_text_iter_compare (&end, &orig_end) < 0)
		{
			g_warning ("Should not reach this code path.");
			bug = TRUE;
			end = orig_end;
		}

		if (bug)
		{
			gchar *text;

			text = gtk_text_iter_get_slice (&start, &end);
			g_warning ("Text that caused the bug: '%s'", text);
			g_warning ("Please report the bug to: " PACKAGE_BUGREPORT);
			g_free (text);
		}
534

535
		_gspell_region_subtract_subregion (spell->scan_region, &start, &end);
536

537
		_gspell_region_iter_next (&intersect_iter);
538 539
	}

540
	g_clear_object (&intersect);
541

542
	if (_gspell_region_is_empty (spell->scan_region))
543
	{
544
		g_clear_object (&spell->scan_region);
545 546 547 548
	}
}

static void
549
check_visible_region (GspellInlineCheckerTextBuffer *spell)
550 551 552 553 554 555 556 557
{
	GSList *l;

	if (spell->scan_region == NULL)
	{
		return;
	}

558 559 560 561 562 563
	if (spell->unit_test_mode)
	{
		check_visible_region_in_view (spell, NULL);
		return;
	}

564 565
	for (l = spell->views; l != NULL; l = l->next)
	{
566
		GtkTextView *view = GTK_TEXT_VIEW (l->data);
567 568 569 570 571
		check_visible_region_in_view (spell, view);
	}
}

static gboolean
572
timeout_cb (GspellInlineCheckerTextBuffer *spell)
573 574 575 576 577 578 579 580
{
	check_visible_region (spell);

	spell->timeout_id = 0;
	return G_SOURCE_REMOVE;
}

static void
581 582
install_timeout (GspellInlineCheckerTextBuffer *spell,
		 guint                          duration)
583 584 585 586
{
	if (spell->timeout_id != 0)
	{
		g_source_remove (spell->timeout_id);
587
		spell->timeout_id = 0;
588 589
	}

590 591 592 593 594 595 596 597 598 599
	if (spell->unit_test_mode)
	{
		timeout_cb (spell);
	}
	else
	{
		spell->timeout_id = g_timeout_add (duration,
						   (GSourceFunc) timeout_cb,
						   spell);
	}
600 601 602
}

static void
603 604 605
add_subregion_to_scan (GspellInlineCheckerTextBuffer *spell,
		       const GtkTextIter             *start,
		       const GtkTextIter             *end)
606 607 608
{
	if (spell->scan_region == NULL)
	{
609
		spell->scan_region = _gspell_region_new (spell->buffer);
610 611
	}

612
	_gspell_region_add_subregion (spell->scan_region, start, end);
613 614 615
}

static void
616
recheck_all (GspellInlineCheckerTextBuffer *spell)
617 618 619 620 621 622
{
	GtkTextIter start;
	GtkTextIter end;

	gtk_text_buffer_get_bounds (spell->buffer, &start, &end);
	add_subregion_to_scan (spell, &start, &end);
623

624 625 626
	check_visible_region (spell);
}

627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
/* The word boundaries are not necessarily the same before and after a text
 * insertion or deletion. We need the broader boundaries, so we connect to the
 * signal without and with the AFTER flag.
 */
static void
insert_text_before_cb (GtkTextBuffer                 *buffer,
		       GtkTextIter                   *location,
		       gchar                         *text,
		       gint                           length,
		       GspellInlineCheckerTextBuffer *spell)
{
	GtkTextIter start;
	GtkTextIter end;

	start = *location;
	end = *location;
643
	adjust_iters (&start, &end, ADJUST_MODE_INCLUDE_NEIGHBORS);
644 645 646 647 648 649 650 651 652 653 654
	add_subregion_to_scan (spell, &start, &end);

	/* Don't install_timeout(), it will anyway be called in
	 * insert_text_after_cb(). If install_timeout() is called here, it is a
	 * problem for the unit test mode because the subregion would be scanned
	 * directly, but we need to wait that the text is inserted, otherwise
	 * this can give different results (since the word boundaries are not
	 * necessarily the same).
	 */
}

655
static void
656 657 658 659 660
insert_text_after_cb (GtkTextBuffer                 *buffer,
		      GtkTextIter                   *location,
		      gchar                         *text,
		      gint                           length,
		      GspellInlineCheckerTextBuffer *spell)
661
{
662
	glong n_chars;
663 664 665
	GtkTextIter start;
	GtkTextIter end;

666 667
	n_chars = g_utf8_strlen (text, length);

668 669
	start = *location;
	end = *location;
670
	gtk_text_iter_backward_chars (&start, n_chars);
671

672 673
	adjust_iters (&start, &end, ADJUST_MODE_INCLUDE_NEIGHBORS);
	add_subregion_to_scan (spell, &start, &end);
674

675
	/* Check current word? */
676
	if (n_chars > 1)
677
	{
678
		_gspell_current_word_policy_several_chars_inserted (spell->current_word_policy);
679 680 681
	}
	else
	{
682 683
		gunichar ch;
		gboolean empty_selection;
684
		GtkTextIter cursor_pos;
685 686 687 688
		gboolean at_cursor_pos;

		ch = g_utf8_get_char (text);
		empty_selection = !gtk_text_buffer_get_has_selection (buffer);
689 690 691 692 693

		gtk_text_buffer_get_iter_at_mark (buffer,
						  &cursor_pos,
						  gtk_text_buffer_get_insert (buffer));

694 695 696 697 698 699
		at_cursor_pos = gtk_text_iter_equal (location, &cursor_pos);

		_gspell_current_word_policy_single_char_inserted (spell->current_word_policy,
								  ch,
								  empty_selection,
								  at_cursor_pos);
700 701
	}

702 703 704
	install_timeout (spell, TIMEOUT_DURATION_BUFFER_MODIFIED);
}

705 706 707 708 709 710 711
/* Same reasoning as for the ::insert-text signal. */
static void
delete_range_before_cb (GtkTextBuffer                 *buffer,
			GtkTextIter                   *start,
			GtkTextIter                   *end,
			GspellInlineCheckerTextBuffer *spell)
{
712 713 714
	{
		GtkTextIter start_adjusted;
		GtkTextIter end_adjusted;
715

716 717 718 719 720
		start_adjusted = *start;
		end_adjusted = *end;
		adjust_iters (&start_adjusted, &end_adjusted, ADJUST_MODE_INCLUDE_NEIGHBORS);
		add_subregion_to_scan (spell, &start_adjusted, &end_adjusted);
	}
721 722 723

	/* Check current word? */
	{
724 725 726
		gboolean empty_selection;
		gboolean spans_several_lines;
		gboolean several_chars;
727
		GtkTextIter cursor_pos;
728 729 730 731 732 733 734 735 736 737
		gboolean cursor_pos_at_start;
		gboolean cursor_pos_at_end;
		gboolean start_is_inside_word;
		gboolean start_ends_word;
		gboolean end_is_inside_word;
		gboolean end_ends_word;

		empty_selection = !gtk_text_buffer_get_has_selection (buffer);
		spans_several_lines = gtk_text_iter_get_line (start) != gtk_text_iter_get_line (end);
		several_chars = gtk_text_iter_get_offset (end) - gtk_text_iter_get_offset (start) > 1;
738 739 740 741 742

		gtk_text_buffer_get_iter_at_mark (buffer,
						  &cursor_pos,
						  gtk_text_buffer_get_insert (buffer));

743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		cursor_pos_at_start = gtk_text_iter_equal (&cursor_pos, start);
		cursor_pos_at_end = gtk_text_iter_equal (&cursor_pos, end);

		start_is_inside_word = _gspell_text_iter_inside_word (start);
		start_ends_word = _gspell_text_iter_ends_word (start);
		end_is_inside_word = _gspell_text_iter_inside_word (end);
		end_ends_word = _gspell_text_iter_ends_word (end);

		_gspell_current_word_policy_text_deleted (spell->current_word_policy,
							  empty_selection,
							  spans_several_lines,
							  several_chars,
							  cursor_pos_at_start,
							  cursor_pos_at_end,
							  start_is_inside_word,
							  start_ends_word,
							  end_is_inside_word,
							  end_ends_word);
761
	}
762 763
}

764
static void
765 766 767 768
delete_range_after_cb (GtkTextBuffer                 *buffer,
		       GtkTextIter                   *start,
		       GtkTextIter                   *end,
		       GspellInlineCheckerTextBuffer *spell)
769
{
770 771 772
	GtkTextIter start_adjusted;
	GtkTextIter end_adjusted;

773 774
	g_return_if_fail (gtk_text_iter_equal (start, end));

775 776
	start_adjusted = *start;
	end_adjusted = *end;
777 778
	adjust_iters (&start_adjusted, &end_adjusted, ADJUST_MODE_INCLUDE_NEIGHBORS);
	add_subregion_to_scan (spell, &start_adjusted, &end_adjusted);
779

780 781 782
	install_timeout (spell, TIMEOUT_DURATION_BUFFER_MODIFIED);
}

783 784 785 786 787 788 789 790
static void
mark_set_after_cb (GtkTextBuffer                 *buffer,
		   GtkTextIter                   *location,
		   GtkTextMark                   *mark,
		   GspellInlineCheckerTextBuffer *spell)
{
	if (mark == gtk_text_buffer_get_insert (buffer))
	{
791
		_gspell_current_word_policy_cursor_moved (spell->current_word_policy);
792 793 794 795
		install_timeout (spell, TIMEOUT_DURATION_BUFFER_MODIFIED);
	}
}

796
static gboolean
797 798 799
get_word_extents_at_click_position (GspellInlineCheckerTextBuffer *spell,
				    GtkTextIter                   *start,
				    GtkTextIter                   *end)
800 801 802 803 804
{
	GtkTextIter iter;

	gtk_text_buffer_get_iter_at_mark (spell->buffer, &iter, spell->mark_click);

805 806
	if (!_gspell_text_iter_inside_word (&iter) &&
	    !_gspell_text_iter_ends_word (&iter))
807 808 809 810 811
	{
		return FALSE;
	}

	*start = iter;
812
	if (!_gspell_text_iter_starts_word (start))
813
	{
814
		_gspell_text_iter_backward_word_start (start);
815 816 817
	}

	*end = iter;
818
	if (!_gspell_text_iter_ends_word (end))
819
	{
820
		_gspell_text_iter_forward_word_end (end);
821 822 823 824 825 826
	}

	return TRUE;
}

static void
827 828
suggestion_activated_cb (const gchar *suggested_word,
			 gpointer     user_data)
829
{
830
	GspellInlineCheckerTextBuffer *spell;
831 832
	GtkTextIter start;
	GtkTextIter end;
833
	gchar *old_word;
834

835
	g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (user_data));
836

837
	spell = GSPELL_INLINE_CHECKER_TEXT_BUFFER (user_data);
838 839 840 841 842 843 844 845 846 847 848

	if (!get_word_extents_at_click_position (spell, &start, &end))
	{
		return;
	}

	old_word = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);

	gtk_text_buffer_begin_user_action (spell->buffer);

	gtk_text_buffer_delete (spell->buffer, &start, &end);
849
	gtk_text_buffer_insert (spell->buffer, &start, suggested_word, -1);
850 851 852

	gtk_text_buffer_end_user_action (spell->buffer);

853 854 855 856
	if (spell->spell_checker != NULL)
	{
		gspell_checker_set_correction (spell->spell_checker,
					       old_word, -1,
857
					       suggested_word, -1);
858
	}
859 860 861 862

	g_free (old_word);
}

863 864 865
void
_gspell_inline_checker_text_buffer_populate_popup (GspellInlineCheckerTextBuffer *spell,
						   GtkMenu                       *menu)
866
{
867
	GtkMenuItem *menu_item;
868 869
	GtkTextIter start;
	GtkTextIter end;
870
	gchar *misspelled_word;
871 872 873 874 875 876

	if (!get_word_extents_at_click_position (spell, &start, &end))
	{
		return;
	}

877
	if (!gtk_text_iter_has_tag (&start, spell->highlight_tag))
878 879 880 881
	{
		return;
	}

882 883 884 885 886
	if (spell->spell_checker == NULL)
	{
		return;
	}

887 888
	/* Prepend suggestions */

889 890 891 892 893 894 895 896 897
	misspelled_word = gtk_text_buffer_get_text (spell->buffer, &start, &end, FALSE);

	menu_item = _gspell_context_menu_get_suggestions_menu_item (spell->spell_checker,
								    misspelled_word,
								    suggestion_activated_cb,
								    spell);

	gtk_menu_shell_prepend (GTK_MENU_SHELL (menu),
				GTK_WIDGET (menu_item));
898

899
	g_free (misspelled_word);
900 901 902
}

static gboolean
903 904 905
draw_cb (GtkWidget                     *text_view,
	 cairo_t                       *cr,
	 GspellInlineCheckerTextBuffer *spell)
906 907 908 909 910 911 912
{
	install_timeout (spell, TIMEOUT_DURATION_DRAWING);

	return GDK_EVENT_PROPAGATE;
}

static void
913 914
remove_tag_to_word (GspellInlineCheckerTextBuffer *spell,
		    const gchar                   *word)
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
{
	GtkTextIter iter;

	gtk_text_buffer_get_start_iter (spell->buffer, &iter);

	while (TRUE)
	{
		gboolean found;
		GtkTextIter match_start;
		GtkTextIter match_end;

		found = gtk_text_iter_forward_search (&iter,
						      word,
						      GTK_TEXT_SEARCH_VISIBLE_ONLY |
						      GTK_TEXT_SEARCH_TEXT_ONLY,
						      &match_start,
						      &match_end,
						      NULL);

		if (!found)
		{
			break;
		}

939 940
		if (_gspell_text_iter_starts_word (&match_start) &&
		    _gspell_text_iter_ends_word (&match_end))
941 942
		{
			gtk_text_buffer_remove_tag (spell->buffer,
943
						    spell->highlight_tag,
944 945 946 947 948 949 950 951 952
						    &match_start,
						    &match_end);
		}

		iter = match_end;
	}
}

static void
953 954 955
word_added_cb (GspellChecker                 *checker,
	       const gchar                   *word,
	       GspellInlineCheckerTextBuffer *spell)
956 957 958 959 960
{
	remove_tag_to_word (spell, word);
}

static void
961 962
session_cleared_cb (GspellChecker                 *checker,
		    GspellInlineCheckerTextBuffer *spell)
963
{
964
	_gspell_current_word_policy_session_cleared (spell->current_word_policy);
965 966 967 968
	recheck_all (spell);
}

static void
969 970 971
language_notify_cb (GspellChecker                 *checker,
		    GParamSpec                    *pspec,
		    GspellInlineCheckerTextBuffer *spell)
972
{
973
	_gspell_current_word_policy_language_changed (spell->current_word_policy);
974 975 976 977 978 979 980 981
	recheck_all (spell);
}

/* When the user right-clicks on a word, they want to check that word.
 * Here, we do NOT move the cursor to the location of the clicked-upon word
 * since that prevents the use of edit functions on the context menu.
 */
static gboolean
982 983 984
button_press_event_cb (GtkTextView                   *view,
		       GdkEventButton                *event,
		       GspellInlineCheckerTextBuffer *spell)
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
{
	if (event->button == GDK_BUTTON_SECONDARY)
	{
		GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);
		GtkTextIter iter;
		gint x;
		gint y;

		gtk_text_view_window_to_buffer_coords (view,
						       GTK_TEXT_WINDOW_TEXT,
						       event->x, event->y,
						       &x, &y);

		gtk_text_view_get_iter_at_location (view, &iter, x, y);

		gtk_text_buffer_move_mark (buffer, spell->mark_click, &iter);
	}

	return GDK_EVENT_PROPAGATE;
}

/* Move the insert mark before popping up the menu, otherwise it
 * will contain the wrong set of suggestions.
 */
static gboolean
1010 1011
popup_menu_cb (GtkTextView                   *view,
	       GspellInlineCheckerTextBuffer *spell)
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
{
	GtkTextIter iter;

	gtk_text_buffer_get_iter_at_mark (spell->buffer, &iter,
					  gtk_text_buffer_get_insert (spell->buffer));
	gtk_text_buffer_move_mark (spell->buffer, spell->mark_click, &iter);

	return FALSE;
}

1022
static void
1023 1024 1025 1026 1027
apply_or_remove_tag_cb (GtkTextBuffer                 *buffer,
			GtkTextTag                    *tag,
			GtkTextIter                   *start,
			GtkTextIter                   *end,
			GspellInlineCheckerTextBuffer *spell)
1028 1029 1030 1031 1032 1033 1034 1035 1036
{
	if (spell->no_spell_check_tag != NULL &&
	    spell->no_spell_check_tag == tag)
	{
		add_subregion_to_scan (spell, start, end);
		install_timeout (spell, TIMEOUT_DURATION_BUFFER_MODIFIED);
	}
}

1037
static void
1038 1039 1040
tag_added_cb (GtkTextTagTable               *table,
	      GtkTextTag                    *tag,
	      GspellInlineCheckerTextBuffer *spell)
1041
{
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	gchar *name;

	g_object_get (tag, "name", &name, NULL);

	if (g_strcmp0 (name, "gtksourceview:context-classes:no-spell-check") == 0)
	{
		g_return_if_fail (spell->no_spell_check_tag == NULL);

		spell->no_spell_check_tag = g_object_ref (tag);

1052
		_gspell_current_word_policy_set_check_current_word (spell->current_word_policy, TRUE);
1053 1054 1055 1056
		recheck_all (spell);
	}

	g_free (name);
1057 1058 1059
}

static void
1060 1061 1062
tag_removed_cb (GtkTextTagTable               *table,
		GtkTextTag                    *tag,
		GspellInlineCheckerTextBuffer *spell)
1063
{
1064 1065 1066 1067 1068
	if (spell->no_spell_check_tag != NULL &&
	    spell->no_spell_check_tag == tag)
	{
		g_clear_object (&spell->no_spell_check_tag);

1069
		_gspell_current_word_policy_set_check_current_word (spell->current_word_policy, TRUE);
1070 1071
		recheck_all (spell);
	}
1072 1073
}

1074
static void
1075 1076
set_spell_checker (GspellInlineCheckerTextBuffer *spell,
		   GspellChecker                 *checker)
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
{
	g_return_if_fail (checker == NULL || GSPELL_IS_CHECKER (checker));

	if (spell->spell_checker == checker)
	{
		return;
	}

	if (spell->spell_checker != NULL)
	{
		g_signal_handlers_disconnect_by_data (spell->spell_checker, spell);
		g_object_unref (spell->spell_checker);
	}

	spell->spell_checker = checker;

	if (spell->spell_checker != NULL)
	{
		g_object_ref (spell->spell_checker);

		g_signal_connect (spell->spell_checker,
				  "word-added-to-session",
				  G_CALLBACK (word_added_cb),
				  spell);

		g_signal_connect (spell->spell_checker,
				  "word-added-to-personal",
				  G_CALLBACK (word_added_cb),
				  spell);

		g_signal_connect (spell->spell_checker,
				  "session-cleared",
				  G_CALLBACK (session_cleared_cb),
				  spell);

		g_signal_connect (spell->spell_checker,
				  "notify::language",
				  G_CALLBACK (language_notify_cb),
				  spell);
	}
}

static void
1120 1121 1122
spell_checker_notify_cb (GspellTextBuffer              *gspell_buffer,
			 GParamSpec                    *pspec,
			 GspellInlineCheckerTextBuffer *spell)
1123
{
1124 1125 1126 1127 1128
	GspellChecker *new_checker;

	new_checker = gspell_text_buffer_get_spell_checker (gspell_buffer);
	set_spell_checker (spell, new_checker);

1129
	_gspell_current_word_policy_checker_changed (spell->current_word_policy);
1130
	recheck_all (spell);
1131 1132
}

1133
static void
1134 1135
set_buffer (GspellInlineCheckerTextBuffer *spell,
	    GtkTextBuffer                 *buffer)
1136 1137 1138
{
	GtkTextTagTable *tag_table;
	GtkTextIter start;
1139
	GspellTextBuffer *gspell_buffer;
1140
	GspellChecker *checker;
1141
	GdkRGBA underline_color;
1142

1143
	g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1144
	g_return_if_fail (spell->buffer == NULL);
1145
	g_return_if_fail (spell->highlight_tag == NULL);
1146
	g_return_if_fail (spell->no_spell_check_tag == NULL);
1147 1148 1149 1150 1151
	g_return_if_fail (spell->mark_click == NULL);

	spell->buffer = g_object_ref (buffer);

	g_object_set_data (G_OBJECT (buffer),
1152
			   INLINE_CHECKER_TEXT_BUFFER_KEY,
1153 1154
			   spell);

1155 1156 1157 1158 1159 1160
	g_signal_connect_object (buffer,
				 "insert-text",
				 G_CALLBACK (insert_text_before_cb),
				 spell,
				 0);

1161 1162 1163 1164 1165 1166
	g_signal_connect_object (buffer,
				 "insert-text",
				 G_CALLBACK (insert_text_after_cb),
				 spell,
				 G_CONNECT_AFTER);

1167 1168 1169 1170 1171 1172
	g_signal_connect_object (buffer,
				 "delete-range",
				 G_CALLBACK (delete_range_before_cb),
				 spell,
				 0);

1173 1174 1175 1176 1177 1178
	g_signal_connect_object (buffer,
				 "delete-range",
				 G_CALLBACK (delete_range_after_cb),
				 spell,
				 G_CONNECT_AFTER);

1179 1180 1181 1182 1183 1184
	g_signal_connect_object (buffer,
				 "mark-set",
				 G_CALLBACK (mark_set_after_cb),
				 spell,
				 G_CONNECT_AFTER);

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
	g_signal_connect_object (buffer,
				 "apply-tag",
				 G_CALLBACK (apply_or_remove_tag_cb),
				 spell,
				 G_CONNECT_AFTER);

	g_signal_connect_object (buffer,
				 "remove-tag",
				 G_CALLBACK (apply_or_remove_tag_cb),
				 spell,
				 G_CONNECT_AFTER);

1197 1198
	_gspell_utils_init_underline_rgba (&underline_color);

1199
	spell->highlight_tag = gtk_text_buffer_create_tag (spell->buffer, NULL,
1200 1201
							   "underline", PANGO_UNDERLINE_SINGLE,
							   "underline-rgba", &underline_color,
1202
							   NULL);
1203
	g_object_ref (spell->highlight_tag);
1204

1205 1206 1207 1208 1209 1210
	spell->no_spell_check_tag = _gspell_utils_get_no_spell_check_tag (spell->buffer);
	if (spell->no_spell_check_tag != NULL)
	{
		g_object_ref (spell->no_spell_check_tag);
	}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
	tag_table = gtk_text_buffer_get_tag_table (spell->buffer);

	g_signal_connect_object (tag_table,
				 "tag-added",
				 G_CALLBACK (tag_added_cb),
				 spell,
				 0);

	g_signal_connect_object (tag_table,
				 "tag-removed",
				 G_CALLBACK (tag_removed_cb),
				 spell,
				 0);

	/* For now we don't care where the mark points. The start looks like a
	 * good place to begin with.
	 */
	gtk_text_buffer_get_start_iter (spell->buffer, &start);
	spell->mark_click = gtk_text_buffer_create_mark (spell->buffer, NULL, &start, TRUE);

1231 1232
	gspell_buffer = gspell_text_buffer_get_from_gtk_text_buffer (spell->buffer);
	checker = gspell_text_buffer_get_spell_checker (gspell_buffer);
1233
	set_spell_checker (spell, checker);
1234

1235 1236 1237
	g_signal_connect_object (gspell_buffer,
				 "notify::spell-checker",
				 G_CALLBACK (spell_checker_notify_cb),
1238 1239 1240
				 spell,
				 0);

1241
	recheck_all (spell);
1242

1243
	g_object_notify (G_OBJECT (spell), "buffer");
1244 1245 1246
}

static void
1247 1248 1249 1250
_gspell_inline_checker_text_buffer_get_property (GObject    *object,
						 guint       prop_id,
						 GValue     *value,
						 GParamSpec *pspec)
1251
{
1252
	GspellInlineCheckerTextBuffer *spell = GSPELL_INLINE_CHECKER_TEXT_BUFFER (object);
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266

	switch (prop_id)
	{
		case PROP_BUFFER:
			g_value_set_object (value, spell->buffer);
			break;

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

static void
1267 1268 1269 1270
_gspell_inline_checker_text_buffer_set_property (GObject      *object,
						 guint         prop_id,
						 const GValue *value,
						 GParamSpec   *pspec)
1271
{
1272
	GspellInlineCheckerTextBuffer *spell = GSPELL_INLINE_CHECKER_TEXT_BUFFER (object);
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

	switch (prop_id)
	{
		case PROP_BUFFER:
			set_buffer (spell, g_value_get_object (value));
			break;

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

static void
1287
_gspell_inline_checker_text_buffer_dispose (GObject *object)
1288
{
1289
	GspellInlineCheckerTextBuffer *spell = GSPELL_INLINE_CHECKER_TEXT_BUFFER (object);
1290 1291 1292 1293 1294 1295 1296

	if (spell->buffer != NULL)
	{
		GtkTextTagTable *table;

		table = gtk_text_buffer_get_tag_table (spell->buffer);

1297
		if (table != NULL && spell->highlight_tag != NULL)
1298
		{
1299
			gtk_text_tag_table_remove (table, spell->highlight_tag);
1300 1301 1302 1303 1304 1305 1306 1307
		}

		if (spell->mark_click != NULL)
		{
			gtk_text_buffer_delete_mark (spell->buffer, spell->mark_click);
			spell->mark_click = NULL;
		}

1308
		g_object_set_data (G_OBJECT (spell->buffer), INLINE_CHECKER_TEXT_BUFFER_KEY, NULL);
1309 1310 1311 1312 1313

		g_object_unref (spell->buffer);
		spell->buffer = NULL;
	}

1314 1315
	set_spell_checker (spell, NULL);

1316
	g_clear_object (&spell->highlight_tag);
1317
	g_clear_object (&spell->no_spell_check_tag);
1318
	g_clear_object (&spell->scan_region);
1319
	g_clear_object (&spell->current_word_policy);
1320

1321
	g_slist_free (spell->views);
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
	spell->views = NULL;

	spell->mark_click = NULL;

	if (spell->timeout_id != 0)
	{
		g_source_remove (spell->timeout_id);
		spell->timeout_id = 0;
	}

1332
	G_OBJECT_CLASS (_gspell_inline_checker_text_buffer_parent_class)->dispose (object);
1333 1334 1335
}

static void
1336
_gspell_inline_checker_text_buffer_class_init (GspellInlineCheckerTextBufferClass *klass)
1337 1338 1339
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

1340 1341 1342
	object_class->get_property = _gspell_inline_checker_text_buffer_get_property;
	object_class->set_property = _gspell_inline_checker_text_buffer_set_property;
	object_class->dispose = _gspell_inline_checker_text_buffer_dispose;
1343 1344 1345 1346 1347 1348

	g_object_class_install_property (object_class,
					 PROP_BUFFER,
					 g_param_spec_object ("buffer",
							      "Buffer",
							      "",
1349
							      GTK_TYPE_TEXT_BUFFER,
1350 1351 1352 1353 1354 1355
							      G_PARAM_READWRITE |
							      G_PARAM_CONSTRUCT_ONLY |
							      G_PARAM_STATIC_STRINGS));
}

static void
1356
_gspell_inline_checker_text_buffer_init (GspellInlineCheckerTextBuffer *spell)
1357
{
1358
	spell->current_word_policy = _gspell_current_word_policy_new ();
1359 1360
}

1361
GspellInlineCheckerTextBuffer *
1362
_gspell_inline_checker_text_buffer_new (GtkTextBuffer *buffer)
1363
{
1364
	GspellInlineCheckerTextBuffer *spell;
1365

1366
	g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1367

1368
	spell = g_object_get_data (G_OBJECT (buffer), INLINE_CHECKER_TEXT_BUFFER_KEY);
1369 1370
	if (spell != NULL)
	{
1371
		return g_object_ref (spell);
1372 1373
	}

1374
	return g_object_new (GSPELL_TYPE_INLINE_CHECKER_TEXT_BUFFER,
1375 1376 1377 1378 1379
			     "buffer", buffer,
			     NULL);
}

/**
1380
 * _gspell_inline_checker_text_buffer_attach_view:
1381
 * @spell: a #GspellInlineCheckerTextBuffer.
1382 1383 1384
 * @view: a #GtkTextView.
 *
 * Enables the inline spell checker for @view. @view must have the same buffer as
1385
 * the #GspellInlineCheckerTextBuffer:buffer property.
1386 1387
 */
void
1388 1389
_gspell_inline_checker_text_buffer_attach_view (GspellInlineCheckerTextBuffer *spell,
						GtkTextView                   *view)
1390
{
1391
	g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
	g_return_if_fail (GTK_IS_TEXT_VIEW (view));
	g_return_if_fail (gtk_text_view_get_buffer (view) == spell->buffer);
	g_return_if_fail (g_slist_find (spell->views, view) == NULL);

	g_signal_connect_object (view,
				 "button-press-event",
				 G_CALLBACK (button_press_event_cb),
				 spell,
				 0);

	g_signal_connect_object (view,
				 "popup-menu",
				 G_CALLBACK (popup_menu_cb),
				 spell,
				 0);

	g_signal_connect_object (view,
				 "draw",
				 G_CALLBACK (draw_cb),
				 spell,
				 0);

	spell->views = g_slist_prepend (spell->views, view);

1416
	_gspell_current_word_policy_set_check_current_word (spell->current_word_policy, TRUE);
1417 1418 1419 1420
	check_visible_region_in_view (spell, view);
}

/**
1421
 * _gspell_inline_checker_text_buffer_detach_view:
1422
 * @spell: a #GspellInlineCheckerTextBuffer.
1423 1424 1425 1426 1427
 * @view: a #GtkTextView.
 *
 * Disables the inline spell checker for @view.
 */
void
1428 1429
_gspell_inline_checker_text_buffer_detach_view (GspellInlineCheckerTextBuffer *spell,
						GtkTextView                   *view)
1430
{
1431
	g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));
1432 1433 1434 1435 1436 1437 1438 1439
	g_return_if_fail (GTK_IS_TEXT_VIEW (view));
	g_return_if_fail (g_slist_find (spell->views, view) != NULL);

	g_signal_handlers_disconnect_by_data (view, spell);

	spell->views = g_slist_remove (spell->views, view);
}

1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
void
_gspell_inline_checker_text_buffer_set_unit_test_mode (GspellInlineCheckerTextBuffer *spell,
						       gboolean                       unit_test_mode)
{
	g_return_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell));

	spell->unit_test_mode = unit_test_mode != FALSE;

	if (spell->timeout_id != 0)
	{
		g_source_remove (spell->timeout_id);
		spell->timeout_id = 0;
		timeout_cb (spell);
	}

	check_visible_region (spell);
}

GtkTextTag *
_gspell_inline_checker_text_buffer_get_highlight_tag (GspellInlineCheckerTextBuffer *spell)
{
	g_return_val_if_fail (GSPELL_IS_INLINE_CHECKER_TEXT_BUFFER (spell), NULL);

	return spell->highlight_tag;
}

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