~ubuntu-branches/ubuntu/natty/bluefish/natty-proposed

« back to all changes in this revision

Viewing changes to src/spell.c

  • Committer: Bazaar Package Importer
  • Author(s): Davide Puricelli (evo)
  • Date: 2005-04-23 17:05:18 UTC
  • mto: (1.1.5 upstream) (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050423170518-izh2k25xve7ui1jx
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Bluefish HTML Editor
2
 
 * spell.c - Check Spelling
3
 
 *
4
 
 * Copyright (C)2000 Pablo De Napoli (for this module)
5
 
 * Minor updates Copyright (C)2001 Olivier Sessink
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 
 *
21
 
 * Note: some code has been taken from Lyx (wich is also covered by the
22
 
 * GNU Public Licence).
23
 
*/
24
 
 
25
 
/* For the select function */
26
 
#ifdef HAVE_CONFIG_H
27
 
#  include "config.h"
28
 
#endif
29
 
#ifdef WITH_SPC
30
 
#define _GNU_SOURCE
31
 
 
32
 
 
33
 
/*********************************************************************
34
 
 * The spellchecker and it's GUI run like this:
35
 
 *
36
 
 * run_spell_checker
37
 
 *      create_spc_window
38
 
 *              spc_start_button_clicked_lcb
39
 
 *                      run_spell_checker
40
 
 *                              create_ispell_pipe
41
 
 *                              --
42
 
 *                              get_next_word
43
 
 *                              check_word
44
 
 *                                      ispell_check_word
45
 
 *                                      correct_word
46
 
 *                              --
47
 
 *                              ispell_terminate
48
 
 *                              destroy_spc_window
49
 
 *      
50
 
 **********************************************************************/
51
 
 
52
 
 
53
 
/* Print debug messages using standard error */
54
 
/* This is needed when the stdin is redirected */
55
 
#define MY_DEBUG_MSG g_printerr
56
 
 
57
 
#include "intl.h"
58
 
#include <glib.h>
59
 
#include <gtk/gtk.h>
60
 
 
61
 
#include "spell.h"
62
 
#include "bluefish.h"
63
 
#include "debug.h"
64
 
#include "gtk_easy.h"
65
 
#include "char_table.h"
66
 
#include "interface.h"
67
 
#include "document.h"
68
 
 
69
 
#include <unistd.h>
70
 
#include <fcntl.h>
71
 
#include <stdio.h>
72
 
#include <stdlib.h>
73
 
#include <string.h>
74
 
#include <signal.h>
75
 
#include <sys/wait.h>
76
 
#include <sys/types.h>
77
 
#include <ctype.h>
78
 
 
79
 
#if TIME_WITH_SYS_TIME
80
 
# include <sys/time.h>
81
 
# include <time.h>
82
 
#else
83
 
# if HAVE_SYS_TIME_H
84
 
#  include <sys/time.h>
85
 
# else
86
 
#  include <time.h>
87
 
# endif
88
 
#endif
89
 
 
90
 
#ifdef HAVE_SYS_SELECT_H
91
 
#include <sys/select.h>
92
 
#endif
93
 
 
94
 
/* Spellchecker status */
95
 
enum {
96
 
        ISP_OK = 1,
97
 
        ISP_ROOT,
98
 
        ISP_COMPOUNDWORD,
99
 
        ISP_UNKNOWN,
100
 
        ISP_MISSED,
101
 
        ISP_IGNORE
102
 
};
103
 
 
104
 
static FILE *in, *out;  /* streams to communicate with ispell */
105
 
pid_t isp_pid = -1; /* pid for the `ispell' process */
106
 
GdkColor misspelled_color; /* tb */
107
 
static int isp_fd;
108
 
 
109
 
/* void sigchldhandler(int sig); */
110
 
void sigchldhandler(pid_t pid, int *status);
111
 
 
112
 
/* extern void sigchldchecker(int sig); */
113
 
extern void sigchldchecker(pid_t pid, int *status);
114
 
 
115
 
typedef  struct  {
116
 
        gint flag;
117
 
        gint count;
118
 
        gchar **misses;
119
 
        } isp_result;
120
 
 
121
 
/* message from the spell checker GUI */
122
 
gint spc_message;
123
 
 
124
 
/*  flag to know if we reach the end of text */
125
 
static gboolean end_of_text;
126
 
 
127
 
#define SPC_CHAR_LENGTH 20
128
 
 
129
 
/* get_next_char:
130
 
  returns the next char in the text.
131
 
  set flags end_of_text
132
 
  converts special characters like &aacute; to iso characters
133
 
*/
134
 
 
135
 
static gchar get_next_char()
136
 
/* FIXME: a more eficient implementation? */
137
 
/* use an HTML parser like gnome-xml ? */
138
 
{
139
 
  gint current_position, old_position,i;
140
 
  gchar c,old_c ;
141
 
  gchar token[SPC_CHAR_LENGTH];
142
 
  DEBUG_MSG("getting next char\n");
143
 
  current_position = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
144
 
  DEBUG_MSG("current_position = %i\n",current_position);
145
 
  c = GTK_TEXT_INDEX(GTK_TEXT(CURRENT_DOCUMENT_TEXT) ,current_position);
146
 
  /* macro to get a single character */
147
 
  if (current_position < gtk_text_get_length(GTK_TEXT(CURRENT_DOCUMENT_TEXT)))
148
 
  {
149
 
   current_position++;
150
 
   gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT),current_position);
151
 
   end_of_text = 0;
152
 
   if (c=='&')
153
 
    {
154
 
     DEBUG_MSG("Character & detected\n");
155
 
     old_position = current_position;
156
 
     token[0] = c;
157
 
     old_c = c;
158
 
     for (i=1;i<SPC_CHAR_LENGTH-2;i++)
159
 
       {
160
 
        c= get_next_char();
161
 
        token[i] = c;
162
 
        if (c==';')
163
 
          break;
164
 
        else if (end_of_text)
165
 
          break;
166
 
       };
167
 
       i++;
168
 
       token[i] = '\0';
169
 
       DEBUG_MSG("Converting special character = \"%s\" \n",token);
170
 
       c = convert_from_html_chars (token,ANY_CHAR_SET);
171
 
       if (c=='\0')
172
 
         {
173
 
           current_position = old_position;
174
 
           c = old_c;
175
 
           /* FIXME: mark for gettext / beter dialog ? */
176
 
           g_printerr(_("Invalid especial character! %s\n"),token);
177
 
         }
178
 
    }
179
 
  }
180
 
  else
181
 
   end_of_text = 1;
182
 
  DEBUG_MSG("next_char ='%c'\n",c);
183
 
  return (c);
184
 
 } /* end get_next_char */
185
 
 
186
 
typedef struct 
187
 
{
188
 
 gchar* text;
189
 
 gint begin;
190
 
 gint end;
191
 
 gint space_length;
192
 
 gint index;
193
 
} Tword;
194
 
 
195
 
#define WORD_BUFFER_SIZE 20
196
 
 
197
 
inline static Tword* new_word (Tword* word)
198
 
{
199
 
  word=(Tword*) g_malloc(sizeof(Tword));
200
 
  word->index = 0;
201
 
  word->space_length = WORD_BUFFER_SIZE;
202
 
  word->text = g_malloc((word->space_length)*sizeof(gchar));
203
 
  return (word);
204
 
}
205
 
 
206
 
inline void free_word (Tword* word)
207
 
{
208
 
 DEBUG_MSG("Free word \"%s\" \n",word->text);
209
 
 g_free(word->text);
210
 
 g_free(word);
211
 
}
212
 
 
213
 
#define SPECIAL_CHAR_LENGTH 10
214
 
 
215
 
static void store_char_in_word (Tword* word,gchar c)
216
 
{
217
 
   DEBUG_MSG("indice =\n %i\n",word->index);
218
 
   /* enlarge the buffer if needed */
219
 
   if (word->index>=word->space_length-1)
220
 
     {
221
 
       DEBUG_MSG("enlarging word buffer\n");
222
 
       word->space_length = word->space_length + WORD_BUFFER_SIZE;
223
 
       DEBUG_MSG("space length=%i\n", word->space_length);
224
 
       word->text = g_realloc (word->text,word->space_length*sizeof(gchar));
225
 
     }
226
 
   (word->text)[word->index] =c;
227
 
   (word->index)++;
228
 
}
229
 
 
230
 
inline gboolean is_empty_word(Tword* word)
231
 
{
232
 
  return((word->index)==0);
233
 
}
234
 
 
235
 
/* Non alphabetical characters are ignored */
236
 
/* and also the text between < > (asumed to be html tags) */
237
 
/*  in PHP code you can have different numbers of > and <, so this should be extended!
238
 
 like in if (a < b) { */
239
 
/* we want to be sure that only valid characters are pased to ispell */
240
 
 
241
 
static Tword* get_next_word(void)
242
 
{
243
 
 Tword* next_word;
244
 
 gchar c ='\0';
245
 
 DEBUG_MSG("getting next word\n");
246
 
 next_word = new_word(next_word);
247
 
 next_word->begin = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
248
 
 
249
 
 while (!end_of_text)
250
 
  {
251
 
    DEBUG_MSG("Not end of text\n");
252
 
    c=get_next_char();
253
 
    if (isalpha((char)c)||isalpha_iso((char)c))
254
 
     store_char_in_word(next_word,c);
255
 
     /* FIXME: accept iso special characters! */
256
 
    else
257
 
     {
258
 
      DEBUG_MSG("Ignoring non alphabethical character\n");
259
 
      break; /* if it is a non alphabetical character exit the loop */
260
 
     };
261
 
  };
262
 
 store_char_in_word(next_word,'\0');
263
 
 if (is_empty_word(next_word))
264
 
   return  next_word;
265
 
 /* at the end of text , do not substract 1 */
266
 
 next_word->end = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT))-(!end_of_text);
267
 
 DEBUG_MSG("next word= \"%s\" \n",next_word-> text);
268
 
 /* ignore html tags */
269
 
 if (c=='<')
270
 
 {
271
 
   DEBUG_MSG("Ignoring html tag\n");
272
 
   while (((c=get_next_char())!='>') && (!end_of_text));
273
 
 }
274
 
 return next_word;
275
 
}
276
 
 
277
 
 
278
 
static
279
 
void create_ispell_pipe()
280
 
{
281
 
        static char o_buf[BUFSIZ];  
282
 
        int pipein[2], pipeout[2];
283
 
        char * argv[14];
284
 
        int argc;
285
 
        int i;
286
 
        
287
 
        gchar buf[2048];
288
 
        fd_set infds;
289
 
        struct timeval tv; 
290
 
        int retval = 0;
291
 
 
292
 
   MY_DEBUG_MSG("Creating ispell pipe ...\n");
293
 
        
294
 
        isp_pid = -1;
295
 
 
296
 
        if(pipe(pipein)==-1 || pipe(pipeout)==-1) {
297
 
                g_printerr(_("Bluefish: Can't create pipe for spellchecker!"));
298
 
                return;
299
 
        }
300
 
 
301
 
        if ((out = fdopen(pipein[1], "w"))==NULL) {
302
 
                g_printerr(_("Bluefish: Can't create stream for pipe for spellchecker!"));
303
 
                return;
304
 
        }
305
 
 
306
 
        if ((in = fdopen(pipeout[0], "r"))==NULL) {
307
 
                g_printerr(_("Bluefish: Can't create stream for pipe for spellchecker!"));
308
 
                return;
309
 
        }
310
 
 
311
 
        setvbuf(out, o_buf, _IOLBF, BUFSIZ);
312
 
 
313
 
        isp_fd = pipeout[0];
314
 
 
315
 
        isp_pid = fork();
316
 
 
317
 
        if(isp_pid==-1) {
318
 
                g_printerr(_("Bluefish: Can't create child process for spellchecker!"));
319
 
                return;
320
 
        }
321
 
 
322
 
        if(isp_pid==0) {        
323
 
                /* child process */
324
 
 
325
 
      /* NOTE: Don't use DEBUG_MSG in this part since the output is redirected! */
326
 
 
327
 
                
328
 
                MY_DEBUG_MSG ("Spell-checker child process \n");
329
 
                
330
 
                dup2(pipein[0], STDIN_FILENO);
331
 
                dup2(pipeout[1], STDOUT_FILENO);
332
 
                close(pipein[0]);
333
 
                close(pipein[1]);
334
 
                close(pipeout[0]);
335
 
                close(pipeout[1]);
336
 
 
337
 
                argc = 0;
338
 
                argv[argc++] = main_v->props.cfg_spc_cline ;
339
 
                argv[argc++] = g_strdup("-a"); /* "Pipe" mode */
340
 
 
341
 
                if ((g_strcasecmp(main_v -> props.cfg_spc_lang, "default")!=0) &&
342
 
          (*(main_v -> props.cfg_spc_lang)!='\0'))
343
 
      {
344
 
         if (main_v ->props.cfg_spc_lang != NULL)
345
 
         {
346
 
          argv[argc++] = g_strdup("-d"); /* Dictionary file */
347
 
                         argv[argc++] = main_v -> props.cfg_spc_lang;
348
 
         };
349
 
                }
350
 
 
351
 
                if (main_v-> props.spc_accept_compound)
352
 
                        /* Consider run-together words as legal compounds */
353
 
                        argv[argc++] = g_strdup("-C"); 
354
 
                else
355
 
                        /* Report run-together words with
356
 
                          missing blanks as errors */
357
 
                        argv[argc++] = g_strdup("-B"); 
358
 
 
359
 
                if (main_v->props.spc_use_esc_chars) {
360
 
                        /* Specify additional characters that
361
 
                           can be part of a word */
362
 
         if (main_v->props.spc_esc_chars !=NULL) {
363
 
                        argv[argc++] = g_strdup("-w"); 
364
 
                        /* Put the escape chars in ""s */
365
 
                        argv[argc++] = g_strconcat("\"",main_v->props.spc_esc_chars
366
 
                                        ,"\"");
367
 
         };
368
 
                }
369
 
                if (main_v->props.spc_use_pers_dict) {
370
 
                        /* Specify an alternate personal dictionary */
371
 
          if (main_v->props.spc_pers_dict != NULL)
372
 
          {
373
 
           argv[argc++] = g_strdup("-p");
374
 
                          argv[argc++] = main_v->props.spc_pers_dict;
375
 
          }
376
 
         }
377
 
 
378
 
                if (main_v->props.spc_use_input_encoding) {
379
 
         if (main_v->props.spc_input_encoding != NULL) {
380
 
                        argv[argc++] = g_strdup("-T"); /* Input encoding */
381
 
                        argv[argc++] = main_v->props.spc_input_encoding;
382
 
         }
383
 
                }
384
 
 
385
 
                argv[argc++] = NULL;
386
 
      #ifdef DEBUG
387
 
      MY_DEBUG_MSG("Executing:\n");
388
 
      for (i=0;i<argc-1;i++)
389
 
       MY_DEBUG_MSG("%s ",argv[i]);
390
 
      #endif
391
 
 
392
 
                execvp(main_v->props.cfg_spc_cline , (char * const *) argv);
393
 
                                
394
 
                /* free the memory used  */
395
 
                for (i=0; i < argc -1; i++)
396
 
                        g_free(argv[i]);
397
 
                
398
 
                g_printerr(_("Bluefish: Failed to start ispell!\n"));
399
 
                _exit(0);
400
 
        }
401
 
 
402
 
        /* Parent process: Read ispells identification message 
403
 
           Hmm...what are we using this id msg for? Nothing? (Lgb)
404
 
           Actually I used it to tell if it's truly Ispell or if it's
405
 
           aspell -- (kevinatk@home.com) */
406
 
        
407
 
#ifdef WITH_WARNINGS
408
 
#warning verify that this works.
409
 
#endif
410
 
        
411
 
        FD_ZERO(&infds);
412
 
        FD_SET(pipeout[0], &infds);
413
 
        tv.tv_sec = 15; /*  fifteen second timeout. Probably too much,
414
 
                         but it can't really hurt. */
415
 
        tv.tv_usec = 0;
416
 
 
417
 
        /* Configure provides us with macros which are supposed to do
418
 
           the right typecast. */
419
 
           
420
 
        retval = select(( SELECT_TYPE_ARG1 ) (pipeout[0]+1), 
421
 
                        SELECT_TYPE_ARG234 (&infds), 
422
 
                        0, 
423
 
                        0, 
424
 
                        SELECT_TYPE_ARG5 (&tv));
425
 
 
426
 
        if (retval > 0) {
427
 
                /* Ok, do the reading. We don't have to FD_ISSET since
428
 
                   there is only one fd in infds. */
429
 
                fgets(buf, 2048, in);
430
 
      MY_DEBUG_MSG ("\nIspell pipe created\n");
431
 
      g_print ("%s\n",buf);
432
 
                
433
 
        } else if (retval == 0) {
434
 
                /* timeout. Give nice message to user. */
435
 
                g_printerr("Ispell read timed out, what now?\n");
436
 
                isp_pid = -1;
437
 
                close(pipeout[0]); close(pipeout[1]);
438
 
                close(pipein[0]); close(pipein[1]);
439
 
                isp_fd = -1;
440
 
        } else {
441
 
                /* Select returned error */
442
 
                g_printerr(_("Select on ispell returned error, what now?\n"));
443
 
        }
444
 
}
445
 
 
446
 
 
447
 
static inline void ispell_terse_mode(void)
448
 
{
449
 
        fputs("!\n", out); /* Set terse mode (silently accept correct words) */
450
 
}
451
 
 
452
 
 
453
 
static inline void ispell_insert_word(char const *word)
454
 
{
455
 
        fputc('*', out); /* Insert word in personal dictionary */
456
 
        fputs(word, out);
457
 
        fputc('\n', out);
458
 
}
459
 
 
460
 
 
461
 
static
462
 
inline void ispell_accept_word(char const *word)
463
 
{
464
 
        fputc('@', out); /* Accept in this session */
465
 
        fputs(word, out);
466
 
        fputc('\n', out);
467
 
}
468
 
 
469
 
/* Send word to ispell and get reply */
470
 
static isp_result * ispell_check_word (gchar *word)
471
 
{
472
 
        isp_result *result;
473
 
        gchar buf[1024], *p, *np;
474
 
   gchar *nb;
475
 
        gint count;
476
 
 
477
 
   #ifdef DEBUG
478
 
   gint i;
479
 
   #endif
480
 
 
481
 
   /* Coment from the original author:
482
 
   I think we have to check if ispell is still alive here */
483
 
        
484
 
   if (isp_pid == -1) return (isp_result *) NULL;
485
 
 
486
 
   DEBUG_MSG("Sending word \"%s\" to ispell\n",word);
487
 
        fputs(word, out);
488
 
        fputc('\n', out);
489
 
 
490
 
   fgets(buf,1024,in);
491
 
 
492
 
   DEBUG_MSG("Message from ispell=\"%s\"\n",buf);
493
 
 
494
 
        result = g_malloc(sizeof(isp_result));
495
 
 
496
 
        switch (*buf) {
497
 
        case '*': /* Word found */
498
 
                result->flag = ISP_OK;
499
 
                DEBUG_MSG("Ispell: Word found\n");
500
 
                break;
501
 
        case '+': /* Word found through affix removal */
502
 
                result->flag = ISP_ROOT;
503
 
                DEBUG_MSG("Ispell: Word found through affix removal\n");
504
 
                break;
505
 
        case '-': /* Word found through compound formation */
506
 
                result->flag = ISP_COMPOUNDWORD;
507
 
                DEBUG_MSG("Ispell: Word found through compound formation\n");
508
 
                break;
509
 
        case '\n': /* Number or when in terse mode: no problems */
510
 
                result->flag = ISP_IGNORE;
511
 
                DEBUG_MSG("Ispell: Ignore Word\n");
512
 
                break;
513
 
        case '#': /* Not found, no near misses and guesses */
514
 
                result->flag = ISP_UNKNOWN;
515
 
                DEBUG_MSG("Ispell: Unknown Word\n");
516
 
                break;
517
 
        case '?': /* Not found, and no near misses, but guesses (guesses are ignored) */
518
 
        case '&': /* Not found, but we have near misses */
519
 
        {
520
 
                result->flag = ISP_MISSED;
521
 
      DEBUG_MSG("Ispell: Near Misses\n");
522
 
      /* FIXME: why duplicate buffer ?*/
523
 
      nb = g_strdup(buf);
524
 
                p = strpbrk(nb+2, " ");
525
 
                sscanf(p, "%d", &count); /* Get near misses count */
526
 
                result->count = count;
527
 
                if (count)
528
 
                {
529
 
                  p = strpbrk(nb, ":");
530
 
                  p +=2;
531
 
 
532
 
        /* remove the last '\n' from ispell output */
533
 
        DEBUG_MSG("Removing end-of-line \n");
534
 
        np = p;
535
 
        while (*np!='\0')np++;
536
 
        np--;
537
 
        *np = '\0';
538
 
 
539
 
        DEBUG_MSG  ("misses list \"%s\" \n",p);
540
 
 
541
 
                  result->misses = g_strsplit(p,", ",count);
542
 
        g_free(nb);
543
 
        #ifdef DEBUG
544
 
          for(i=0;i<count;i++)
545
 
           DEBUG_MSG("near_miss = \"%s\" \n", result-> misses[i]);
546
 
        #endif
547
 
                }
548
 
 
549
 
 
550
 
                break;
551
 
        }
552
 
        default: /* This shouldn't happend, but you know Murphy */
553
 
                result->flag = ISP_UNKNOWN;
554
 
                DEBUG_MSG("Ispell: Unknown\n");
555
 
        }
556
 
 
557
 
        *buf = 0;
558
 
        if (result->flag!=ISP_IGNORE) {
559
 
                while (*buf!='\n') fgets(buf, 255, in); /* wait for ispell to finish */
560
 
        }
561
 
        return result;
562
 
}
563
 
 
564
 
 
565
 
static inline void select_word (Tword* word)
566
 
{
567
 
        doc_unbind_signals(main_v->current_document);
568
 
        gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin); 
569
 
        gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);/* tb */
570
 
        gtk_text_insert(GTK_TEXT(CURRENT_DOCUMENT_TEXT), 0, &misspelled_color, 0,word->text, -1);/* tb */
571
 
        gtk_text_thaw(GTK_TEXT(CURRENT_DOCUMENT_TEXT));/* tb */                 
572
 
        /* gtk_editable_select_region (GTK_EDITABLE(CURRENT_DOCUMENT_TEXT),word->begin,word->end); *//* tb */
573
 
        doc_bind_signals(main_v->current_document);
574
 
}
575
 
 
576
 
/*for gtk_text_insert*/
577
 
#define ALL_THE_STRING -1
578
 
static void replace_word_with (Tword *word,gchar* replace_with)
579
 
{
580
 
        replace_text(replace_with, word->begin, word->end);
581
 
/*
582
 
  DEBUG_MSG("Deleting word \"%s\" \n",word->text);  
583
 
  gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin);
584
 
  gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);
585
 
  DEBUG_MSG("inserting replace text =\"%s\" \n",replace_with);
586
 
  gtk_editable_insert_text(GTK_EDITABLE(CURRENT_DOCUMENT_TEXT), replace_with,strlen(replace_with),&(word->begin));
587
 
*/
588
 
  /* FIXME: convert iso characters to html ? */
589
 
}
590
 
 
591
 
/*
592
 
 *change slected word color back to original color
593
 
 */
594
 
static void change_word_color(Tword * word) {
595
 
        doc_unbind_signals(main_v->current_document);
596
 
        gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin);
597
 
        gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);
598
 
        DEBUG_MSG("changing text color");
599
 
        gtk_editable_insert_text(GTK_EDITABLE(CURRENT_DOCUMENT_TEXT), word->text, strlen(word->text),&(word->begin));
600
 
        doc_bind_signals(main_v->current_document);
601
 
}
602
 
 
603
 
static void correct_word(Tword* word)
604
 
/* ask the user how to correct a word, and do it */
605
 
{
606
 
 gboolean word_corrected = 0;
607
 
 select_word(word);
608
 
 do {
609
 
     spc_message = SPC_NONE;
610
 
 
611
 
     while (gtk_events_pending())
612
 
           gtk_main_iteration();
613
 
     switch (spc_message)
614
 
     {
615
 
     case SPC_INSERT:      ispell_insert_word(word->text);
616
 
                                change_word_color(word);
617
 
                           word_corrected = 1;
618
 
                           break;
619
 
     case SPC_ACCEPT:      ispell_accept_word(word->text);
620
 
                           change_word_color(word);
621
 
                           word_corrected = 1;
622
 
                                          break;
623
 
     case SPC_IGNORE:      change_word_color(word);
624
 
                                word_corrected = 1;
625
 
                           break;
626
 
     case SPC_REPLACE:     replace_word_with (word,gtk_entry_get_text(GTK_ENTRY(spc_gui.replace_entry)));
627
 
                           word_corrected = 1;
628
 
                           break;
629
 
     case SPC_CLOSE:       DEBUG_MSG("close message\n");
630
 
                                change_word_color(word);
631
 
                           word_corrected = 1; /* to exit the while loop */
632
 
                           break;
633
 
 
634
 
     }
635
 
 } while (!word_corrected);
636
 
 /* gtk_editable_delete_selection (GTK_EDITABLE(CURRENT_DOCUMENT_TEXT));*/
637
 
 DEBUG_MSG ("Word corrected\n");
638
 
};
639
 
 
640
 
static void check_word (Tword* word)
641
 
{
642
 
 isp_result *result;
643
 
 gchar* converted_string;
644
 
 gint i;
645
 
 
646
 
/* FIXME: Is this really needed ? */
647
 
 while (gtk_events_pending())
648
 
        gtk_main_iteration();
649
 
 
650
 
 result = ispell_check_word (word->text);
651
 
 switch (result->flag) {
652
 
   case ISP_MISSED:  /* creates near misses list */
653
 
                     DEBUG_MSG("Creating misses list\n");
654
 
                     for (i=0; i<result->count; i++)
655
 
                     {
656
 
                         if (main_v->props.spc_output_html_chars)
657
 
                           {
658
 
                             converted_string = convert_string_iso_to_html(result->misses[i]);
659
 
                             g_free(result->misses[i]);
660
 
                             result->misses[i] = converted_string;
661
 
                            };
662
 
                         DEBUG_MSG("Adding \"%s\" to the near_misses_list\n",result->misses[i]);
663
 
                         gtk_clist_append(GTK_CLIST(spc_gui.near_misses_clist),&(result->misses[i]));
664
 
                     };
665
 
   case ISP_UNKNOWN: gtk_entry_set_text (GTK_ENTRY(spc_gui.word_entry),word->text);
666
 
                     gtk_entry_set_text (GTK_ENTRY(spc_gui.replace_entry),word->text);
667
 
                     correct_word(word);
668
 
                     DEBUG_MSG("spc_message = %i\n",spc_message);
669
 
   }; /* end switch */
670
 
 
671
 
 /* do a clean up */
672
 
  switch (result->flag) {
673
 
  case ISP_MISSED:   DEBUG_MSG("free & clean near_misses_lists ...\n");
674
 
                     gtk_clist_clear(GTK_CLIST(spc_gui.near_misses_clist));
675
 
                     for(i=0;i<result->count;i++)
676
 
                      g_free(result->misses[i]);
677
 
                     DEBUG_MSG("free & cleaning near_misses_list done\n");
678
 
 
679
 
  case ISP_UNKNOWN:  gtk_entry_set_text(GTK_ENTRY(spc_gui.word_entry),"");
680
 
                     gtk_entry_set_text (GTK_ENTRY(spc_gui.replace_entry),"");
681
 
                     DEBUG_MSG("word & replace entries cleaned\n");
682
 
                     }; /*end switch */
683
 
  g_free(result);
684
 
#ifdef DEBUG
685
 
  if (spc_message==SPC_CLOSE)
686
 
    DEBUG_MSG("Passing SPC_CLOSE message\n");
687
 
#endif
688
 
  DEBUG_MSG("word checked\n");
689
 
} /* end check_word  */
690
 
 
691
 
 
692
 
inline static void update_progress_bar(void) {
693
 
        gtk_progress_bar_update(GTK_PROGRESS_BAR(spc_gui.progress_bar),(gfloat) gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT))/ (gfloat) gtk_text_get_length(GTK_TEXT(CURRENT_DOCUMENT_TEXT)));
694
 
};
695
 
 
696
 
void ispell_terminate()
697
 
{
698
 
 if (isp_pid != -1) /* checks if ispell is running */
699
 
 {
700
 
   fputs("#\n", out); /* Save personal dictionary */
701
 
        fflush(out);
702
 
        fclose(out);
703
 
   kill(isp_pid,SIGTERM);
704
 
        if (waitpid(isp_pid, (int *)0, WNOHANG) != 0) {
705
 
           isp_pid = -1; /* ispell is not running.FIXME: Can we check this ? */
706
 
        }
707
 
 }
708
 
}
709
 
 
710
 
/* Perform an ispell session */
711
 
void run_spell_checker (void)
712
 
{
713
 
        Tword *word;
714
 
 
715
 
   misspelled_color.red = 0xffff; /* tb */
716
 
   misspelled_color.green = 0; /* tb */
717
 
   misspelled_color.blue = 0; /* tb */
718
 
   if (!gdk_color_alloc(main_v->colormap, &misspelled_color)) { /* tb */
719
 
        g_error("couldn't allocate required colors"); /* tb */
720
 
   } /* tb */
721
 
   gtk_text_freeze(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
722
 
     /* create ispell process */        
723
 
        ispell_terminate();
724
 
        create_ispell_pipe();
725
 
        gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT),0);
726
 
        end_of_text = 0; /* needed for get_next_word to work properly */
727
 
 
728
 
        if (isp_pid != -1) {
729
 
       /* Put ispell in terse mode to improve speed */
730
 
            ispell_terse_mode();
731
 
            do
732
 
       {
733
 
        word = get_next_word();
734
 
        if (!is_empty_word(word))
735
 
        {
736
 
         spc_message = SPC_NONE;
737
 
         check_word(word);
738
 
         free_word (word);
739
 
         update_progress_bar();
740
 
         if (spc_message==SPC_CLOSE)
741
 
                      {
742
 
                      DEBUG_MSG("Close message received\n");
743
 
                      break;
744
 
                      };
745
 
         };
746
 
       } while (!end_of_text);
747
 
      }
748
 
      else
749
 
            {
750
 
            error_dialog(_("Bluefish Spell Checker Error"),
751
 
                                      _("\n\n"
752
 
                                      "The ispell-process has died for some reason. *One* possible reason\n"
753
 
                                      "could be that you do not have a dictionary file\n"
754
 
                                      "for the language of this document installed.\n"
755
 
                                      "Check /usr/lib/ispell or set another\n"
756
 
                                      "dictionary in the Spellchecker Options menu."));
757
 
                                 fclose(out);
758
 
            }; /*end if*/
759
 
      DEBUG_MSG("Removing grab\n");
760
 
      gtk_grab_remove(spc_gui.window);
761
 
      DEBUG_MSG("Removing status \n");
762
 
      statusbar_remove(GINT_TO_POINTER(spc_gui.status_bar_count));
763
 
      DEBUG_MSG("Destroying window\n");
764
 
      gtk_widget_destroy(spc_gui.window);
765
 
                ispell_terminate();
766
 
  } /* end run_spell_checker */
767
 
 
768
 
 
769
 
 
770
 
#endif /* WITH_SPC */