summaryrefslogtreecommitdiffstats
path: root/input-methods/sulekha/src/sulekhaspell.c
blob: 4ced30215110059edeaba297cda44b6096a1bac0 (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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
/* sulekhaspell - a transliteratio - spell-checking addon for GTK's TextView widget
 * Based on gtkspell by Evan Martin.
 *
 * Copyright (C) 2007-2008
 *  Santhosh Thottingal<santhosh00@gmail.com>,
 *  Praveen Arimprathodiyil <pravi.a@gmail.com>
 *  Swathanthra Malayalam Computing.
 *
 * 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 3 of the License, 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <gtk/gtk.h>
#include <libintl.h>
#include <locale.h>
#include "../config.h"
#include "sulekhaspell.h"
#include "transliteration.h"

#define _(String) dgettext (PACKAGE, String)

#define SULEKHASPELL_MISSPELLED_TAG "sulekhaspell-misspelled"

#ifdef HAVE_ASPELL_H
#define USING_ASPELL
#include <aspell.h>
#elif defined HAVE_PSPELL_H
#define USING_PSPELL
#include <pspell/pspell.h>
#define AspellSpeller PspellManager
#define speller manager
#define aspell_speller_check pspell_manager_check
#define aspell_speller_add_to_session pspell_manager_add_to_session
#define aspell_speller_add_to_personal pspell_manager_add_to_personal
#define aspell_speller_save_all_word_lists pspell_manager_save_all_word_lists
#define aspell_speller_store_replacement pspell_manager_store_replacement
#define AspellWordList PspellWordList
#define AspellStringEnumeration PspellStringEmulation
#define aspell_speller_suggest pspell_manager_suggest
#define aspell_word_list_elements pspell_word_list_elements
#define aspell_string_enumeration_next pspell_string_emulation_next
#define delete_aspell_string_enumeration delete_pspell_string_emulation
#define AspellConfig PspellConfig
#define AspellCanHaveError PspellCanHaveError
#define new_aspell_config new_pspell_config
#define aspell_config_replace pspell_config_replace
#define new_aspell_speller new_pspell_manager
#define delete_aspell_config delete_pspell_config
#define aspell_error_message pspell_error_message
#define delete_aspell_speller delete_pspell_manager
#define to_aspell_speller to_pspell_manager
#define aspell_error_number pspell_error_number
#define aspell pspell
#endif

const int debug = 1;
const int quiet = 0;
int check = 1;
int replace = 0;

struct _SulekhaSpell
{
  GtkTextView *view;
  GtkTextTag *tag_highlight;
  GtkTextMark *mark_insert_start;
  GtkTextMark *mark_insert_end;
  gboolean deferred_check;
  AspellSpeller *speller;
  GtkTextMark *mark_click;
};

static void sulekhaspell_free (SulekhaSpell * spell);
static void replace_by_transliterated_word (SulekhaSpell * spell);

#define SULEKHASPELL_OBJECT_KEY "sulekhaspell"

GQuark
sulekhaspell_error_quark (void)
{
  static GQuark q = 0;
  if (q == 0)
    q = g_quark_from_static_string ("sulekhaspell-error-quark");
  return q;
}

static gboolean
sulekhaspell_text_iter_forward_word_end (GtkTextIter * i)
{
  GtkTextIter iter;

/* heuristic: 
 * if we're on an singlequote/apostrophe and
 * if the next letter is alphanumeric,
 * this is an apostrophe. */

  if (!gtk_text_iter_forward_word_end (i))
    return FALSE;

  if (gtk_text_iter_get_char (i) != '\'')
    return TRUE;

  iter = *i;
  if (gtk_text_iter_forward_char (&iter))
    {
      if (g_unichar_isalpha (gtk_text_iter_get_char (&iter)))
	{
	  return (gtk_text_iter_forward_word_end (i));
	}
    }

  return TRUE;
}

static gboolean
sulekhaspell_text_iter_backward_word_start (GtkTextIter * i)
{
  GtkTextIter iter;

  if (!gtk_text_iter_backward_word_start (i))
    return FALSE;

  iter = *i;
  if (gtk_text_iter_backward_char (&iter))
    {
      if (gtk_text_iter_get_char (&iter) == '\'')
	{
	  if (gtk_text_iter_backward_char (&iter))
	    {
	      if (g_unichar_isalpha (gtk_text_iter_get_char (&iter)))
		{
		  return (gtk_text_iter_backward_word_start (i));
		}
	    }
	}
    }

  return TRUE;
}

#define gtk_text_iter_backward_word_start sulekhaspell_text_iter_backward_word_start
#define gtk_text_iter_forward_word_end sulekhaspell_text_iter_forward_word_end

static void
check_word (SulekhaSpell * spell, GtkTextBuffer * buffer,
	    GtkTextIter * start, GtkTextIter * end)
{
  char *text;
  text = gtk_text_buffer_get_text (buffer, start, end, FALSE);
  if (debug)
    g_print ("checking: %s\n", text);
  text = transliterate_ml (text, 0, strlen (text));
  if (g_unichar_isdigit (*text) == FALSE)	/* don't check numbers */
    if (aspell_speller_check (spell->speller, text, -1) == FALSE)
      gtk_text_buffer_apply_tag (buffer, spell->tag_highlight, start, end);

  g_free (text);
}

static void
print_iter (char *name, GtkTextIter * iter)
{
  g_print ("%1s[%d%c%c%c] ", name, gtk_text_iter_get_offset (iter),
	   gtk_text_iter_starts_word (iter) ? 's' : ' ',
	   gtk_text_iter_inside_word (iter) ? 'i' : ' ',
	   gtk_text_iter_ends_word (iter) ? 'e' : ' ');
}

static void
check_range (SulekhaSpell * spell, GtkTextBuffer * buffer,
	     GtkTextIter start, GtkTextIter end, gboolean force_all)
{
  /* we need to "split" on word boundaries.
   * luckily, pango knows what "words" are 
   * so we don't have to figure it out. */

  GtkTextIter wstart, wend, cursor, precursor;
  gboolean inword, highlight;

  if (debug)
    {
      g_print ("check_range: ");
      print_iter ("s", &start);
      print_iter ("e", &end);
      g_print (" -> ");
    }


//inside word
  if (gtk_text_iter_inside_word (&end))
    gtk_text_iter_forward_word_end (&end);	//move the end to the end of the word
//start of the word
  if (!gtk_text_iter_starts_word (&start))	//not at the beginning of the word
    {
      if (gtk_text_iter_inside_word (&start)
	  || gtk_text_iter_ends_word (&start))
	{
	  gtk_text_iter_backward_word_start (&start);	//move the start to the start of the  word
	}
      else
	{
	  /* if we're neither at the beginning nor inside a word,
	   * me must be in some spaces.
	   * skip forward to the beginning of the next word. 
	   */
	  if (gtk_text_iter_forward_word_end (&start))
	    gtk_text_iter_backward_word_start (&start);
	}
    }
  /*
   * Now start is at the start of the word and end is at the end of the word
   * Initializes iter with the current position of mark.
   */
  gtk_text_buffer_get_iter_at_mark (buffer, &cursor,
				    gtk_text_buffer_get_insert (buffer));
/*gtk_text_buffer_get_insert: Returns the mark that represents the cursor (insertion point). Equivalent to calling 
 *  gtk_text_buffer_get_mark() to get the mark named "insert",
 * but very slightly more efficient, and involves less typing.
 */



  precursor = cursor;
  gtk_text_iter_backward_char (&precursor);
  highlight = gtk_text_iter_has_tag (&cursor, spell->tag_highlight) ||
    gtk_text_iter_has_tag (&precursor, spell->tag_highlight);

  gtk_text_buffer_remove_tag (buffer, spell->tag_highlight, &start, &end);

  /* Fix a corner case when replacement occurs at beginning of buffer:
   * An iter at offset 0 seems to always be inside a word,
   * even if it's not.  Possibly a pango bug.
   */
  if (gtk_text_iter_get_offset (&start) == 0)
    {
      gtk_text_iter_forward_word_end (&start);
      gtk_text_iter_backward_word_start (&start);
    }

  if (debug)
    {
      print_iter ("s", &start);
      print_iter ("e", &end);
      g_print ("\n");
    }

  wstart = start;
  while (gtk_text_iter_compare (&wstart, &end) < 0)
    {
      /* move wend to the end of the current word. */
      wend = wstart;
      gtk_text_iter_forward_word_end (&wend);

      inword = (gtk_text_iter_compare (&wstart, &cursor) < 0) &&
	(gtk_text_iter_compare (&cursor, &wend) <= 0);
      //i.e start is before cursor and cursor is less than end--> cursor in between the word

      if (inword && !force_all)
	{
	  /* this word is being actively edited, 
	   * only check if it's already highligted,
	   * otherwise defer this check until later. */
	  if (highlight)
	    check_word (spell, buffer, &wstart, &wend);
	  else
	    spell->deferred_check = TRUE;

	}
      else			//cursor at the end of the word. now check the spelling
	{
	  g_print ("checking\n");
	  check_word (spell, buffer, &wstart, &wend);
	  spell->deferred_check = FALSE;

	}

      /* now move wend to the beginning of the next word, */
      gtk_text_iter_forward_word_end (&wend);
      gtk_text_iter_backward_word_start (&wend);
      /* make sure we've actually advanced
       * (we don't advance in some corner cases), */
      if (gtk_text_iter_equal (&wstart, &wend))
	break;			/* we're done in these cases.. */
      /* and then pick this as the new next word beginning. */
      wstart = wend;
    }

}

static void
check_deferred_range (SulekhaSpell * spell, GtkTextBuffer * buffer,
		      gboolean force_all)
{
  GtkTextIter start, end;
  gtk_text_buffer_get_iter_at_mark (buffer, &start, spell->mark_insert_start);
  gtk_text_buffer_get_iter_at_mark (buffer, &end, spell->mark_insert_end);
  check_range (spell, buffer, start, end, force_all);
}

/* insertion works like this:
 *  - before the text is inserted, we mark the position in the buffer.
 *  - after the text is inserted, we see where our mark is and use that and
 *    the current position to check the entire range of inserted text.
 *
 * this may be overkill for the common case (inserting one character). */

static void
insert_text_before (GtkTextBuffer * buffer, GtkTextIter * iter,
		    gchar * text, gint len, SulekhaSpell * spell)
{
  gtk_text_buffer_move_mark (buffer, spell->mark_insert_start, iter);
}

static void
insert_text_after (GtkTextBuffer * buffer, GtkTextIter * iter,
		   gchar * text, gint len, SulekhaSpell * spell)
{
  GtkTextIter start, end;

  if (debug)
    g_print ("insert %s\n", text );
  /*-----------------------------------*/

  if (!gtk_text_iter_ends_word (iter))
    {
      g_print ("ivideyaanu mone kali\n");

      replace_by_transliterated_word (spell);
      gtk_text_buffer_get_iter_at_mark (buffer, &end, spell->mark_insert_end);
      gtk_text_buffer_get_iter_at_mark (buffer, &start,
					spell->mark_insert_start);


      check_range (spell, buffer, start, end, FALSE);

    //  gtk_text_buffer_move_mark (buffer, spell->mark_insert_end, &end);
    }
/*-----------------------------------*/
  else
    {
      /* we need to check a range of text. */
      gtk_text_buffer_get_iter_at_mark (buffer, &start,
					spell->mark_insert_start);


      check_range (spell, buffer, start, *iter, FALSE);

      gtk_text_buffer_move_mark (buffer, spell->mark_insert_end, iter);
    }
}

/* deleting is more simple:  we're given the range of deleted text.
 * after deletion, the start and end iters should be at the same position
 * (because all of the text between them was deleted!).
 * this means we only really check the words immediately bounding the
 * deletion.
 */

static void
delete_range_after (GtkTextBuffer * buffer,
		    GtkTextIter * start, GtkTextIter * end,
		    SulekhaSpell * spell)
{
  if (debug)
    g_print ("delete\n");
  check_range (spell, buffer, *start, *end, FALSE);
}

static void
mark_set (GtkTextBuffer * buffer, GtkTextIter * iter,
	  GtkTextMark * mark, SulekhaSpell * spell)
{
  /* if the cursor has moved and there is a deferred check so handle it now */
  if ((mark == gtk_text_buffer_get_insert (buffer)) && spell->deferred_check)
    check_deferred_range (spell, buffer, FALSE);
}

static void
get_word_extents_from_mark (GtkTextBuffer * buffer,
			    GtkTextIter * start, GtkTextIter * end,
			    GtkTextMark * mark)
{
  gtk_text_buffer_get_iter_at_mark (buffer, start, mark);
  if (!gtk_text_iter_starts_word (start))
    gtk_text_iter_backward_word_start (start);
  *end = *start;
  if (gtk_text_iter_inside_word (end))
    gtk_text_iter_forward_word_end (end);
}

static void
add_to_dictionary (GtkWidget * menuitem, SulekhaSpell * spell)
{
  char *word;
  GtkTextIter start, end;
  GtkTextBuffer *buffer;

  buffer = gtk_text_view_get_buffer (spell->view);

  get_word_extents_from_mark (buffer, &start, &end, spell->mark_click);
  word = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  word = transliterate_ml (word, 0, strlen (word));
  aspell_speller_add_to_personal (spell->speller, word, strlen (word));
  aspell_speller_save_all_word_lists (spell->speller);

  sulekhaspell_recheck_all (spell);

  g_free (word);
}

static void
ignore_all (GtkWidget * menuitem, SulekhaSpell * spell)
{
  char *word;
  GtkTextIter start, end;
  GtkTextBuffer *buffer;

  buffer = gtk_text_view_get_buffer (spell->view);

  get_word_extents_from_mark (buffer, &start, &end, spell->mark_click);
  word = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

  aspell_speller_add_to_session (spell->speller, word, strlen (word));

  sulekhaspell_recheck_all (spell);

  g_free (word);
}

static void
replace_word (GtkWidget * menuitem, SulekhaSpell * spell)
{
  char *oldword;
  const char *newword;
  GtkTextIter start, end;
  GtkTextBuffer *buffer;

  buffer = gtk_text_view_get_buffer (spell->view);

  get_word_extents_from_mark (buffer, &start, &end, spell->mark_click);
  oldword = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  newword = gtk_label_get_text (GTK_LABEL (GTK_BIN (menuitem)->child));

  if (debug)
    {
      g_print ("old word: '%s'\n", oldword);
      print_iter ("s", &start);
      print_iter ("e", &end);
      g_print ("\nnew word: '%s'\n", newword);
    }

  gtk_text_buffer_delete (buffer, &start, &end);
  gtk_text_buffer_insert (buffer, &start, newword, -1);

  aspell_speller_store_replacement (spell->speller,
				    oldword, strlen (oldword),
				    newword, strlen (newword));

  g_free (oldword);
}

static void
replace_by_transliterated_word (SulekhaSpell * spell)
{
  char *oldword;
  const char *newword;
  GtkTextIter start, end;
  GtkTextBuffer *buffer;

  buffer = gtk_text_view_get_buffer (spell->view);

  get_word_extents_from_mark (buffer, &start, &end,spell->mark_insert_end);
  oldword = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
  // newword = gtk_label_get_text (GTK_LABEL (GTK_BIN (menuitem)->child));
  newword = transliterate_ml (oldword, 0, strlen (oldword));
  // if (debug)
  if (1)
    {
      g_print ("old word: '%s'\n", oldword);
      print_iter ("s", &start);
      print_iter ("e", &end);
      g_print ("\nnew word: '%s'\n", newword);
    }

  gtk_text_buffer_delete (buffer, &start, &end);
  gtk_text_buffer_insert (buffer, &start, newword, -1);


  g_free (oldword);
  g_print ("\nReplaced\n");
}


static void
populate_popup (GtkTextView * textview, GtkMenu * menu, SulekhaSpell * spell)
{
  GtkWidget *img, *mi;
  const AspellWordList *suggestions;
  GtkWidget *topmenu;
  AspellStringEnumeration *elements;
  int count = 0;
  char *label;
  GtkTextBuffer *buffer = gtk_text_view_get_buffer (textview);
  GtkTextIter start, end;
  char *word;
  const char *suggestion;
  topmenu = menu;
  /* we need to figure out if they picked a misspelled word. */
  get_word_extents_from_mark (buffer, &start, &end, spell->mark_click);

  /* if our highlight algorithm ever messes up, 
   * this isn't correct, either. */
  if (!gtk_text_iter_has_tag (&start, spell->tag_highlight))
    return;			/* word wasn't misspelled. */

  /* menu separator comes first. */
  mi = gtk_menu_item_new ();
  gtk_widget_show (mi);
  gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mi);

  word = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);

  /* - Ignore All */
  mi = gtk_image_menu_item_new_with_label (_("Ignore All"));
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi),
				 gtk_image_new_from_stock (GTK_STOCK_REMOVE,
							   GTK_ICON_SIZE_MENU));
  g_signal_connect (G_OBJECT (mi), "activate", G_CALLBACK (ignore_all),
		    spell);
  gtk_widget_show_all (mi);
  gtk_menu_shell_prepend (GTK_MENU_SHELL (topmenu), mi);

  word = transliterate_ml (word, 0, strlen (word));
/* + Add to Dictionary */
  label = g_strdup_printf (_("Add \"%s\" to Dictionary"), word);
  mi = gtk_image_menu_item_new_with_label (label);
  g_free (label);
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi),
				 gtk_image_new_from_stock (GTK_STOCK_ADD,
							   GTK_ICON_SIZE_MENU));
  g_signal_connect (G_OBJECT (mi), "activate", G_CALLBACK (add_to_dictionary),
		    spell);
  gtk_widget_show_all (mi);
  gtk_menu_shell_prepend (GTK_MENU_SHELL (topmenu), mi);
  /* menu separator comes first. */
  mi = gtk_menu_item_new ();
  gtk_widget_show (mi);
  gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mi);
  /* then, on top of it, the suggestions menu. */
  img = gtk_image_new_from_stock (GTK_STOCK_SPELL_CHECK, GTK_ICON_SIZE_MENU);
  mi = gtk_image_menu_item_new_with_label (_("Spelling Suggestions"));
  gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mi), img);



  suggestions = aspell_speller_suggest (spell->speller, word, -1);
  elements = aspell_word_list_elements (suggestions);

  suggestion = aspell_string_enumeration_next (elements);
  if (suggestion == NULL)
    {
      /* no suggestions.  put something in the menu anyway... */
      GtkWidget *label;
      label = gtk_label_new ("");
      gtk_label_set_markup (GTK_LABEL (label), _("<i>(no suggestions)</i>"));

      mi = gtk_menu_item_new ();
      gtk_container_add (GTK_CONTAINER (mi), label);
      gtk_widget_show_all (mi);
      gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mi);
    }
  else
    {
      /* build a set of menus with suggestions. */
      while (suggestion != NULL)
	{
	  if (count == 5)
	    {
//            mi = gtk_menu_item_new ();
//            gtk_widget_show (mi);
//            gtk_menu_shell_insert (GTK_MENU_SHELL (menu), mi,count+1);

	      mi = gtk_menu_item_new_with_label (_("More..."));
	      gtk_widget_show (mi);
	      gtk_menu_shell_insert (GTK_MENU_SHELL (menu), mi, count + 1);

	      menu = gtk_menu_new ();
	      gtk_menu_item_set_submenu (GTK_MENU_ITEM (mi), menu);
	      count = 0;
	    }
	  mi = gtk_menu_item_new_with_label (suggestion);
	  g_signal_connect (G_OBJECT (mi), "activate",
			    G_CALLBACK (replace_word), spell);
	  gtk_widget_show (mi);
	  gtk_menu_shell_insert (GTK_MENU_SHELL (menu), mi, count);
	  count++;
	  suggestion = aspell_string_enumeration_next (elements);
	}
    }

  delete_aspell_string_enumeration (elements);


  g_free (word);


  gtk_widget_show_all (mi);
  gtk_menu_shell_prepend (GTK_MENU_SHELL (menu), mi);
}



/* 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
button_press_event (GtkTextView * view, GdkEventButton * event,
		    SulekhaSpell * spell)
{
  if (event->button == 3)
    {
      gint x, y;
      GtkTextIter iter;
      GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);

      /* handle deferred check if it exists */
      if (spell->deferred_check)
	check_deferred_range (spell, buffer, TRUE);

      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 FALSE;			/* false: let gtk process this event, too.
				   we don't want to eat any events. */
}

/* This event occurs when the popup menu is requested through a key-binding
 * (Menu Key or <shift>+F10 by default).  In this case we want to set
 * spell->mark_click to the cursor position. */
static gboolean
popup_menu_event (GtkTextView * view, SulekhaSpell * spell)
{
  GtkTextIter iter;
  GtkTextBuffer *buffer = gtk_text_view_get_buffer (view);

  gtk_text_buffer_get_iter_at_mark (buffer, &iter,
				    gtk_text_buffer_get_insert (buffer));
  gtk_text_buffer_move_mark (buffer, spell->mark_click, &iter);
  return FALSE;			/* false: let gtk process this event, too. */
}

static gboolean
sulekhaspell_set_language_internal (SulekhaSpell * spell, const gchar * lang,
				    GError ** error)
{
  AspellConfig *config;
  AspellCanHaveError *err;
  AspellSpeller *speller;

  if (lang == NULL)
    {
      lang = g_getenv ("LANG");
      if (lang)
	{
	  if (g_strncasecmp (lang, "C", 1) == 0)
	    lang = NULL;
	  else if (lang[0] == 0)
	    lang = NULL;
	}
    }

  config = new_aspell_config ();
  if (lang)
    aspell_config_replace (config, "language-tag", lang);
  aspell_config_replace (config, "encoding", "utf-8");
  //aspell_config_replace (config, "sug-edit-dist", "2");
  aspell_config_replace (config, "sug-mode", "ultra");

  err = new_aspell_speller (config);
  delete_aspell_config (config);

  if (aspell_error_number (err) != 0)
    {
#ifdef USING_ASPELL
      g_set_error (error, SULEKHASPELL_ERROR, SULEKHASPELL_ERROR_BACKEND,
		   "aspell: %s", aspell_error_message (err));
#elif defined USING_PSPELL
      g_set_error (error, SULEKHASPELL_ERROR, SULEKHASPELL_ERROR_BACKEND,
		   "pspell: %s", aspell_error_message (err));
#endif
      return FALSE;
    }
  if (spell->speller)
    delete_aspell_speller (spell->speller);
  spell->speller = to_aspell_speller (err);

  return TRUE;
}

/**
 * sulekhaspell_set_language:
 * @spell:  The #SulekhaSpell object.
 * @lang: The language to use, in a form pspell understands (it appears to
 * be a locale specifier?).
 * @error: Return location for error.
 *
 * Set the language on @spell to @lang, possibily returning an error in
 * @error.
 *
 * Returns: FALSE if there was an error.
 */
gboolean
sulekhaspell_set_language (SulekhaSpell * spell, const gchar * lang,
			   GError ** error)
{
  gboolean ret;

  if (error)
    g_return_val_if_fail (*error == NULL, FALSE);

  ret = sulekhaspell_set_language_internal (spell, lang, error);
  if (ret)
    sulekhaspell_recheck_all (spell);

  return ret;
}

/**
 * sulekhaspell_recheck_all:
 * @spell:  The #SulekhaSpell object.
 *
 * Recheck the spelling in the entire buffer.
 */
void
sulekhaspell_recheck_all (SulekhaSpell * spell)
{
  GtkTextBuffer *buffer;
  GtkTextIter start, end;

  buffer = gtk_text_view_get_buffer (spell->view);

  gtk_text_buffer_get_bounds (buffer, &start, &end);

  check_range (spell, buffer, start, end, TRUE);
}

/**
 * sulekhaspell_new_attach:
 * @view: The #GtkTextView to attach to.
 * @lang: The language to use, in a form pspell understands (it appears to
 * be a locale specifier?).
 * @error: Return location for error.
 *
 * Create a new #SulekhaSpell object attached to @view with language @lang.
 *
 * Returns: a new #SulekhaSpell object, or %NULL on error.
 */
SulekhaSpell *
sulekhaspell_new_attach (GtkTextView * view, const gchar * lang,
			 GError ** error)
{
  GtkTextBuffer *buffer;
  GtkTextTagTable *tagtable;
  GtkTextIter start, end;

  SulekhaSpell *spell;

#ifdef ENABLE_NLS
  bindtextdomain (PACKAGE, LOCALEDIR);
  bind_textdomain_codeset (PACKAGE, "UTF-8");
#endif

  if (error)
    g_return_val_if_fail (*error == NULL, NULL);

  spell = g_object_get_data (G_OBJECT (view), SULEKHASPELL_OBJECT_KEY);
  g_assert (spell == NULL);

  /* attach to the widget */
  spell = g_new0 (SulekhaSpell, 1);
  spell->view = view;
  if (!sulekhaspell_set_language_internal (spell, lang, error))
    {
      g_free (spell);
      return NULL;
    }
  g_object_set_data (G_OBJECT (view), SULEKHASPELL_OBJECT_KEY, spell);

  g_signal_connect_swapped (G_OBJECT (view), "destroy",
			    G_CALLBACK (sulekhaspell_free), spell);
  g_signal_connect (G_OBJECT (view), "button-press-event",
		    G_CALLBACK (button_press_event), spell);
  g_signal_connect (G_OBJECT (view), "populate-popup",
		    G_CALLBACK (populate_popup), spell);
  g_signal_connect (G_OBJECT (view), "popup-menu",
		    G_CALLBACK (popup_menu_event), spell);

  buffer = gtk_text_view_get_buffer (view);

  g_signal_connect (G_OBJECT (buffer),
		    "insert-text", G_CALLBACK (insert_text_before), spell);
  g_signal_connect_after (G_OBJECT (buffer),
			  "insert-text",
			  G_CALLBACK (insert_text_after), spell);
  g_signal_connect_after (G_OBJECT (buffer),
			  "delete-range",
			  G_CALLBACK (delete_range_after), spell);
  g_signal_connect (G_OBJECT (buffer),
		    "mark-set", G_CALLBACK (mark_set), spell);

  tagtable = gtk_text_buffer_get_tag_table (buffer);
  spell->tag_highlight =
    gtk_text_tag_table_lookup (tagtable, SULEKHASPELL_MISSPELLED_TAG);

  if (spell->tag_highlight == NULL)
    {
      spell->tag_highlight = gtk_text_buffer_create_tag (buffer,
							 SULEKHASPELL_MISSPELLED_TAG,
#ifdef HAVE_PANGO_UNDERLINE_ERROR
							 "underline",
							 PANGO_UNDERLINE_ERROR,
#else
							 "foreground", "red",
							 "underline",
							 PANGO_UNDERLINE_SINGLE,
#endif
							 NULL);
    }

  /* we create the mark here, but we don't use it until text is
   * inserted, so we don't really care where iter points.  */
  gtk_text_buffer_get_bounds (buffer, &start, &end);
  spell->mark_insert_start = gtk_text_buffer_create_mark (buffer,
							  "sulekhaspell-insert-start",
							  &start, TRUE);
  spell->mark_insert_end = gtk_text_buffer_create_mark (buffer,
							"sulekhaspell-insert-end",
							&start, TRUE);
  spell->mark_click = gtk_text_buffer_create_mark (buffer,
						   "sulekhaspell-click",
						   &start, TRUE);

  spell->deferred_check = FALSE;

  /* now check the entire text buffer. */
  sulekhaspell_recheck_all (spell);
  return spell;
}

static void
sulekhaspell_free (SulekhaSpell * spell)
{
  GtkTextBuffer *buffer;
  GtkTextTagTable *table;
  GtkTextIter start, end;

  buffer = gtk_text_view_get_buffer (spell->view);
  table = gtk_text_buffer_get_tag_table (buffer);

  gtk_text_buffer_get_bounds (buffer, &start, &end);
  gtk_text_buffer_remove_tag (buffer, spell->tag_highlight, &start, &end);

  gtk_text_buffer_delete_mark (buffer, spell->mark_insert_start);
  gtk_text_buffer_delete_mark (buffer, spell->mark_insert_end);
  gtk_text_buffer_delete_mark (buffer, spell->mark_click);

  delete_aspell_speller (spell->speller);

  g_signal_handlers_disconnect_matched (spell->view,
					G_SIGNAL_MATCH_DATA,
					0, 0, NULL, NULL, spell);
  g_signal_handlers_disconnect_matched (buffer,
					G_SIGNAL_MATCH_DATA,
					0, 0, NULL, NULL, spell);
  g_free (spell);
}

/**
 * sulekhaspell_get_from_text_view:
 * @view: A #GtkTextView.
 *
 * Retrieves the #SulekhaSpell object attached to a text view.
 *
 * Returns: the #SulekhaSpell object, or %NULL if there is no #SulekhaSpell
 * attached to @view.
 */
SulekhaSpell *
sulekhaspell_get_from_text_view (GtkTextView * view)
{
  return g_object_get_data (G_OBJECT (view), SULEKHASPELL_OBJECT_KEY);
}

/**
 * sulekhaspell_detach:
 * @spell: A #SulekhaSpell.
 *
 * Detaches this #SulekhaSpell from its text view.  Use
 * sulekhaspell_get_from_text_view() to retrieve a SulekhaSpell from a
 * #GtkTextView.
 */
void
sulekhaspell_detach (SulekhaSpell * spell)
{
  g_return_if_fail (spell != NULL);

  g_object_set_data (G_OBJECT (spell->view), SULEKHASPELL_OBJECT_KEY, NULL);
  sulekhaspell_free (spell);
}