]> git.lyx.org Git - features.git/blob - src/spellchecker.C
apply the ostream changes to mathed, some other small related things
[features.git] / src / spellchecker.C
1 /*
2  *This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *       
7  *          Copyright 1995 Matthias Ettrich
8  *          Copyright 1995-1998 The LyX Team
9  *
10  * ====================================================== 
11  */
12
13 #include <config.h>
14
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <cstdlib>
18 #include <cstring>
19 #include <csignal>
20 #include <sys/wait.h>
21 #include <sys/types.h>
22 #include <cctype>
23 #include FORMS_H_LOCATION
24
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
27 # include <ctime>
28 #else
29 # if HAVE_SYS_TIME_H
30 #  include <sys/time.h>
31 # else
32 #  include <ctime>
33 # endif
34 #endif
35
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
38 #endif
39
40 #include "LString.h"
41 #include "sp_form.h"
42 #include "spellchecker.h"
43 #include "lyx_main.h"
44 #include "buffer.h"
45 #include "lyxrc.h"
46 #include "BufferView.h"
47 #include "gettext.h"
48 #include "lyx_gui_misc.h"
49 #include "debug.h"
50 #include "support/lstrings.h"
51
52 extern LyXRC * lyxrc;
53
54 // Spellchecker status
55 enum {
56         ISP_OK = 1,
57         ISP_ROOT,
58         ISP_COMPOUNDWORD,
59         ISP_UNKNOWN,
60         ISP_MISSED,
61         ISP_IGNORE
62 };
63
64 static bool RunSpellChecker(BufferView * bv);
65
66 static FILE * in, * out;  /* streams to communicate with ispell */
67 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
68                     // lyx_cb.C
69
70 // the true spell checker program being used
71 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
72 static ActualSpellChecker actual_spell_checker;
73
74 static int isp_fd;
75
76 static FD_form_spell_options *fd_form_spell_options = 0;
77 FD_form_spell_check *fd_form_spell_check = 0;
78
79 //void sigchldhandler(int sig);
80 void sigchldhandler(pid_t pid, int *status);
81
82 //extern void sigchldchecker(int sig);
83 extern void sigchldchecker(pid_t pid, int *status);
84
85 struct isp_result {
86         int flag;
87         int count;
88         string str;
89         char ** misses;
90         isp_result() {
91                 flag = ISP_UNKNOWN;
92                 count = 0;
93                 misses = static_cast<char**>(0);
94         }
95         ~isp_result() {
96                 delete[] misses;
97         }
98 };
99
100
101 /***** Spellchecker options *****/
102
103 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
104 // now also string. Amazing, eh? (Asger)
105
106 // Set (sane) values in form to current spellchecker options
107 void SpellOptionsUpdate() 
108 {
109         // Alternate language
110         if (lyxrc->isp_alt_lang.empty()) {
111                 lyxrc->isp_use_alt_lang = false;
112         } else {
113                 fl_set_input(fd_form_spell_options->altlang_input,
114                              lyxrc->isp_alt_lang.c_str());
115         }
116         if (lyxrc->isp_use_alt_lang) {
117                 fl_set_button(fd_form_spell_options->buflang, 0);
118                 fl_set_button(fd_form_spell_options->altlang, 1);
119         } else {
120                 fl_set_button(fd_form_spell_options->buflang, 1);
121                 fl_set_button(fd_form_spell_options->altlang, 0);
122         }  
123
124         // Personal dictionary
125         if (lyxrc->isp_pers_dict.empty()) {
126                 lyxrc->isp_use_pers_dict = false;
127         } else {
128                 fl_set_input(fd_form_spell_options->perdict_input,
129                              lyxrc->isp_pers_dict.c_str());
130         }
131         fl_set_button(fd_form_spell_options->perdict,
132                       lyxrc->isp_use_pers_dict ? 1:0);
133
134         // Escape chars
135         if (lyxrc->isp_esc_chars.empty()) {
136                 lyxrc->isp_use_esc_chars = false;
137         } else {
138                 fl_set_input(fd_form_spell_options->esc_chars_input,
139                              lyxrc->isp_esc_chars.c_str());
140         }
141         fl_set_button(fd_form_spell_options->esc_chars,
142                       lyxrc->isp_use_esc_chars ? 1:0);
143
144         // Options
145         fl_set_button(fd_form_spell_options->compounds,
146                       lyxrc->isp_accept_compound ? 1 : 0);
147         fl_set_button(fd_form_spell_options->inpenc,
148                       lyxrc->isp_use_input_encoding ? 1 : 0);
149 }
150
151 // Update spellchecker options
152 void SpellOptionsApplyCB(FL_OBJECT *, long)
153 {
154         // Build new status from form data
155         lyxrc->isp_use_alt_lang = 
156                 fl_get_button(fd_form_spell_options->altlang);
157         lyxrc->isp_use_pers_dict = 
158                 fl_get_button(fd_form_spell_options->perdict);
159         lyxrc->isp_accept_compound = 
160                 fl_get_button(fd_form_spell_options->compounds);
161         lyxrc->isp_use_esc_chars = 
162                 fl_get_button(fd_form_spell_options->esc_chars);
163         lyxrc->isp_use_input_encoding = 
164                 fl_get_button(fd_form_spell_options->inpenc);
165
166         // Update strings with data from input fields
167         lyxrc->isp_alt_lang = 
168                 fl_get_input(fd_form_spell_options->altlang_input);
169         lyxrc->isp_pers_dict = 
170                 fl_get_input(fd_form_spell_options->perdict_input);
171         lyxrc->isp_esc_chars = 
172                 fl_get_input(fd_form_spell_options->esc_chars_input);
173
174         // Update form
175         SpellOptionsUpdate();
176 }
177
178
179 void SpellOptionsCancelCB(FL_OBJECT *, long)
180 {
181         fl_hide_form(fd_form_spell_options->form_spell_options);
182 }
183
184
185 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
186 {
187         SpellOptionsApplyCB(ob, data);
188         SpellOptionsCancelCB(ob, data);
189 }
190
191
192 // Show spellchecker options form
193 void SpellCheckerOptions()
194 {
195         // Create form if nescessary
196         if (fd_form_spell_options ==  0) {
197                 fd_form_spell_options = create_form_form_spell_options();
198                 // Make sure pressing the close box does not kill LyX. (RvdK)
199                 fl_set_form_atclose(fd_form_spell_options->form_spell_options, 
200                                     CancelCloseBoxCB, 0);
201         }
202
203         // Update form to current options
204         SpellOptionsUpdate();
205
206         // Focus in alternate language field
207         fl_set_focus_object(fd_form_spell_options->form_spell_options,
208                             fd_form_spell_options->altlang_input);
209
210         // Show form
211         if (fd_form_spell_options->form_spell_options->visible) {
212                 fl_raise_form(fd_form_spell_options->form_spell_options);
213         } else {
214                 fl_show_form(fd_form_spell_options->form_spell_options,
215                              FL_PLACE_MOUSE, FL_FULLBORDER,
216                              _("Spellchecker Options"));
217         }
218 }
219
220
221 /***** Spellchecker *****/
222
223 // Could also use a clean up. (Asger Alstrup)
224
225 static
226 void create_ispell_pipe(BufferParams const & params, string const & lang)
227 {
228         static char o_buf[BUFSIZ];  // jc: it could be smaller
229         int pipein[2], pipeout[2];
230         char * argv[14];
231         int argc;
232
233         isp_pid = -1;
234
235         if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
236                 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
237                 return;
238         }
239
240         if ((out = fdopen(pipein[1], "w")) == 0) {
241                 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
242                        << endl;
243                 return;
244         }
245
246         if ((in = fdopen(pipeout[0], "r")) == 0) {
247                 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
248                        << endl;
249                 return;
250         }
251
252         setvbuf(out, o_buf, _IOLBF, BUFSIZ);
253
254         isp_fd = pipeout[0];
255
256         isp_pid = fork();
257
258         if(isp_pid == -1) {
259                 lyxerr << "LyX: Can't create child process for spellchecker!"
260                        << endl;
261                 return;
262         }
263
264         if(isp_pid == 0) {        
265                 /* child process */
266                 dup2(pipein[0], STDIN_FILENO);
267                 dup2(pipeout[1], STDOUT_FILENO);
268                 close(pipein[0]);
269                 close(pipein[1]);
270                 close(pipeout[0]);
271                 close(pipeout[1]);
272
273                 argc = 0;
274                 char * tmp = new char[lyxrc->isp_command.length() + 1];
275                 lyxrc->isp_command.copy(tmp, lyxrc->isp_command.length());
276                 tmp[lyxrc->isp_command.length()] = '\0';
277                 argv[argc++] = tmp;
278                 tmp = new char[3];
279                 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
280                 argv[argc++] = tmp;
281
282                 if (lang != "default") {
283                         tmp = new char[3];
284                         string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
285                         argv[argc++] = tmp;
286                         tmp = new char[lang.length() + 1];
287                         lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
288                         argv[argc++] = tmp;
289                 }
290
291                 if (lyxrc->isp_accept_compound) {
292                         // Consider run-together words as legal compounds
293                         tmp = new char[3];
294                         string("-C").copy(tmp, 2); tmp[2] = '\0';
295                         argv[argc++] = tmp;
296                 } else {
297                         // Report run-together words with
298                         // missing blanks as errors
299                         tmp = new char[3]; 
300                         string("-B").copy(tmp, 2); tmp[2] = '\0';
301                         argv[argc++] = tmp; 
302                 }
303                 if (lyxrc->isp_use_esc_chars) {
304                         // Specify additional characters that
305                         // can be part of a word
306                         tmp = new char[3];
307                         string("-w").copy(tmp, 2); tmp[2] = '\0';
308                         argv[argc++] = tmp; 
309                         // Put the escape chars in ""s
310                         string tms = "\"" + lyxrc->isp_esc_chars + "\"";
311                         tmp = new char[tms.length() + 1];
312                         tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
313                         argv[argc++] = tmp;
314                 }
315                 if (lyxrc->isp_use_pers_dict) {
316                         // Specify an alternate personal dictionary
317                         tmp = new char[3];
318                         string("-p").copy(tmp, 2);
319                         tmp[2]= '\0';
320                         argv[argc++] = tmp;
321                         tmp = new char[lyxrc->isp_pers_dict.length() + 1];
322                         lyxrc->isp_pers_dict.copy(tmp, lyxrc->isp_pers_dict.length());
323                         tmp[lyxrc->isp_pers_dict.length()] = '\0';
324                         argv[argc++] = tmp;
325                 }
326                 if (lyxrc->isp_use_input_encoding &&
327                     params.inputenc != "default") {
328                         tmp = new char[3];
329                         string("-T").copy(tmp, 2); tmp[2] = '\0';
330                         argv[argc++] = tmp; // Input encoding
331                         tmp = new char[params.inputenc.length() + 1];
332                         params.inputenc.copy(tmp, params.inputenc.length());
333                         tmp[params.inputenc.length()] = '\0';
334                         argv[argc++] = tmp;
335                 }
336
337                 argv[argc++] = 0;
338
339                 execvp("ispell", const_cast<char * const *>(argv));
340
341                 // free the memory used by string::copy in the
342                 // setup of argv
343                 for (int i= 0; i < argc -1; ++i)
344                         delete[] argv[i];
345                 
346                 lyxerr << "LyX: Failed to start ispell!" << endl;
347                 _exit(0);
348         }
349
350         /* Parent process: Read ispells identification message */
351         // Hmm...what are we using this id msg for? Nothing? (Lgb)
352         // Actually I used it to tell if it's truly Ispell or if it's
353         // aspell -- (kevinatk@home.com)
354         char buf[2048];
355         fd_set infds;
356         struct timeval tv;
357         int retval = 0;
358         FD_ZERO(&infds);
359         FD_SET(pipeout[0], &infds);
360         tv.tv_sec = 15; // fifteen second timeout. Probably too much,
361                         // but it can't really hurt.
362         tv.tv_usec = 0;
363
364         // Configure provides us with macros which are supposed to do
365         // the right typecast.
366         retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1), 
367                         SELECT_TYPE_ARG234 (&infds), 
368                         0, 
369                         0, 
370                         SELECT_TYPE_ARG5 (&tv));
371
372         if (retval > 0) {
373                 // Ok, do the reading. We don't have to FD_ISSET since
374                 // there is only one fd in infds.
375                 fgets(buf, 2048, in);
376                 
377                 // determine if the spell checker is really Aspell
378                 if (strstr(buf, "Aspell"))
379                   actual_spell_checker = ASC_ASPELL;
380                 else
381                   actual_spell_checker = ASC_ISPELL;
382                 
383         } else if (retval == 0) {
384                 // timeout. Give nice message to user.
385                 lyxerr << "Ispell read timed out, what now?" << endl;
386                 // This probably works but could need some thought
387                 isp_pid = -1;
388                 close(pipeout[0]); close(pipeout[1]);
389                 close(pipein[0]); close(pipein[1]);
390                 isp_fd = -1;
391         } else {
392                 // Select returned error
393                 lyxerr << "Select on ispell returned error, what now?" << endl;
394         }
395 }
396
397
398 // Send word to ispell and get reply
399 static
400 isp_result *ispell_check_word(char *word)
401 {
402         //Please rewrite to use string.
403         isp_result *result;
404         char buf[1024], *p;
405         int count, i;
406
407         fputs(word, out); 
408         fputc('\n', out);
409   
410         fgets(buf, 1024, in); 
411   
412         /* I think we have to check if ispell is still alive here because
413            the signal-handler could have disabled blocking on the fd */
414         if (isp_pid == -1) return 0;
415
416         result = new isp_result;
417   
418         switch (*buf) {
419         case '*': // Word found
420                 result->flag = ISP_OK;
421                 break;
422         case '+': // Word found through affix removal
423                 result->flag = ISP_ROOT;
424                 break;
425         case '-': // Word found through compound formation
426                 result->flag = ISP_COMPOUNDWORD;
427                 break;
428         case '\n': // Number or when in terse mode: no problems
429                 result->flag = ISP_IGNORE;
430                 break;
431         case '#': // Not found, no near misses and guesses
432                 result->flag = ISP_UNKNOWN;
433                 break;
434         case '?': // Not found, and no near misses, but guesses (guesses are ignored)
435         case '&': // Not found, but we have near misses
436         {
437                 result->flag = ISP_MISSED;
438                 result->str = buf;
439                 // nb is leaked! where should it be freed? I have to
440                 // admit I do not understand the intent of the code :(
441                 // (JMarc) 
442                 char * nb = new char[result->str.length() + 1];
443                 result->str.copy(nb, result->str.length());
444                 nb[result->str.length()]= '\0';
445                 p = strpbrk(nb+2, " ");
446                 sscanf(p, "%d", &count); // Get near misses count
447                 result->count = count;
448                 if (count) result->misses = new char*[count];
449                 p = strpbrk(nb, ":");
450                 p += 2;
451                 for (i = 0; i < count; ++i) {
452                         result->misses[i] = p;
453                         p = strpbrk(p, ",\n");
454                         *p = 0;
455                         p += 2;
456                 }
457                 break;
458         }
459         default: // This shouldn't happend, but you know Murphy
460                 result->flag = ISP_UNKNOWN;
461         }
462
463         *buf = 0;
464         if (result->flag!= ISP_IGNORE) {
465                 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
466         }
467         return result;
468 }
469
470
471 static inline
472 void ispell_terminate()
473 {
474         // Note: If you decide to optimize this out when it is not 
475         // needed please note that when Aspell is used this command 
476         // is also needed to save the replacement dictionary.
477         // -- Kevin Atkinson (kevinatk@home.com)
478
479         fputs("#\n", out); // Save personal dictionary
480
481         fflush(out);
482         fclose(out);
483 }
484
485
486 static inline
487 void ispell_terse_mode()
488 {
489         fputs("!\n", out); // Set terse mode (silently accept correct words)
490 }
491
492
493 static inline
494 void ispell_insert_word(char const *word)
495 {
496         fputc('*', out); // Insert word in personal dictionary
497         fputs(word, out);
498         fputc('\n', out);
499 }
500
501
502 static inline
503 void ispell_accept_word(char const *word) 
504 {
505         fputc('@', out); // Accept in this session
506         fputs(word, out);
507         fputc('\n', out);
508 }
509
510 static inline
511 void ispell_store_replacement(char const *mis, string const & cor) {
512         if(actual_spell_checker == ASC_ASPELL) {
513                 fputs("$$ra ", out);
514                 fputs(mis, out);
515                 fputc(',', out);
516                 fputs(cor.c_str(), out);
517                 fputc('\n', out);
518         }
519 }
520
521
522 void ShowSpellChecker(BufferView * bv)
523 {
524         FL_OBJECT * obj;
525         int ret;
526
527         // Exit if we don't have a document open
528         if (!bv->available())
529                 return;
530
531         if (fd_form_spell_check == 0) {
532                 fd_form_spell_check = create_form_form_spell_check();
533                 // Make sure pressing the close box does not kill LyX. (RvdK)
534                 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
535         }
536
537         // Clear form
538         fl_set_slider_value(fd_form_spell_check->slider, 0);
539         fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
540         fl_set_object_label(fd_form_spell_check->text, "");
541         fl_set_input(fd_form_spell_check->input, "");
542         fl_clear_browser(fd_form_spell_check->browser);
543
544         // Show form
545         if (fd_form_spell_check->form_spell_check->visible) {
546                 fl_raise_form(fd_form_spell_check->form_spell_check);
547         } else {
548                 fl_show_form(fd_form_spell_check->form_spell_check,
549                              FL_PLACE_MOUSE, FL_FULLBORDER,
550                              _("Spellchecker"));
551         }
552         fl_deactivate_object(fd_form_spell_check->slider); 
553
554         // deactivate insert, accept, replace, and stop
555         fl_deactivate_object(fd_form_spell_check->insert);
556         fl_deactivate_object(fd_form_spell_check->accept);
557         fl_deactivate_object(fd_form_spell_check->ignore);
558         fl_deactivate_object(fd_form_spell_check->replace);
559         fl_deactivate_object(fd_form_spell_check->stop);
560         fl_deactivate_object(fd_form_spell_check->input);
561         fl_deactivate_object(fd_form_spell_check->browser);
562         fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
563         fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
564         fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
565         fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
566         fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
567         fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
568         fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
569
570         while (true){
571                 obj = fl_do_forms();
572                 if (obj == fd_form_spell_check->options){
573                         SpellCheckerOptions();
574                 }
575                 if (obj == fd_form_spell_check->start){
576                         // activate insert, accept, and stop
577                         fl_activate_object(fd_form_spell_check->insert);
578                         fl_activate_object(fd_form_spell_check->accept);
579                         fl_activate_object(fd_form_spell_check->ignore);
580                         fl_activate_object(fd_form_spell_check->stop);
581                         fl_activate_object(fd_form_spell_check->input);
582                         fl_activate_object(fd_form_spell_check->browser);
583                         fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
584                         fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
585                         fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
586                         fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
587                         fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
588                         fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
589                         // activate replace only if the file is not read-only
590                         if (!bv->buffer()->isReadonly()) { 
591                           fl_activate_object(fd_form_spell_check->replace);
592                           fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
593                         }
594
595                         // deactivate options and start
596                         fl_deactivate_object(fd_form_spell_check->options);
597                         fl_deactivate_object(fd_form_spell_check->start);
598                         fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
599                         fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
600
601                         ret = RunSpellChecker(bv);
602
603                         // deactivate insert, accept, replace, and stop
604                         fl_deactivate_object(fd_form_spell_check->insert);
605                         fl_deactivate_object(fd_form_spell_check->accept);
606                         fl_deactivate_object(fd_form_spell_check->ignore);
607                         fl_deactivate_object(fd_form_spell_check->replace);
608                         fl_deactivate_object(fd_form_spell_check->stop);
609                         fl_deactivate_object(fd_form_spell_check->input);
610                         fl_deactivate_object(fd_form_spell_check->browser);
611                         fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
612                         fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
613                         fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
614                         fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
615                         fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
616                         fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
617                         fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
618
619                         // activate options and start
620                         fl_activate_object(fd_form_spell_check->options);
621                         fl_activate_object(fd_form_spell_check->start);
622                         fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
623                         fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
624
625                         // if RunSpellChecker returns false quit spellchecker
626                         if (!ret) break;
627                 }
628                 if (obj == fd_form_spell_check->done) break;
629         }
630         fl_hide_form(fd_form_spell_check->form_spell_check);
631         bv->endOfSpellCheck();
632         return;
633 }
634
635
636 // Perform an ispell session
637 static
638 bool RunSpellChecker(BufferView * bv)
639 {
640         isp_result * result;
641         int i, newvalue;
642         FL_OBJECT * obj;
643
644         string tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang : bv->buffer()->GetLanguage();
645
646         int oldval = 0;  /* used for updating slider only when needed */
647         float newval = 0.0;
648    
649         /* create ispell process */
650         create_ispell_pipe(bv->buffer()->params, tmp);
651
652         if (isp_pid == -1) {
653                 fl_show_message(
654                         _("\n\n"
655                           "The ispell-process has died for some reason. *One* possible reason\n"
656                           "could be that you do not have a dictionary file\n"
657                           "for the language of this document installed.\n"
658                           "Check /usr/lib/ispell or set another\n"
659                           "dictionary in the Spellchecker Options menu."), "", "");
660                 fclose(out);
661                 return true;
662         }
663
664         // Put ispell in terse mode to improve speed
665         ispell_terse_mode();
666
667         unsigned int word_count = 0;
668         while (true) {
669                 char * word = bv->nextWord(newval);
670                 if (word == 0) break;
671                 ++word_count;
672                 
673                 // Update slider if and only if value has changed
674                 newvalue = int(100.0*newval);
675                 if(newvalue!= oldval) {
676                         oldval = newvalue;
677                         fl_set_slider_value(fd_form_spell_check->slider, oldval);
678                 }
679
680                 result = ispell_check_word(word);
681                 if (isp_pid == -1) {
682                         delete[] word;
683                         break;
684                 }
685
686                 obj =  fl_check_forms();
687                 if (obj == fd_form_spell_check->stop) {
688                         delete result;
689                         delete[] word;
690                         ispell_terminate();
691                         return true;
692                 }
693                 if (obj == fd_form_spell_check->done) {
694                         delete result;
695                         delete[] word;
696                         ispell_terminate(); 
697                         return false;
698                 }
699     
700                 switch (result->flag) {
701                 case ISP_UNKNOWN:
702                 case ISP_MISSED:
703                 {
704                         bv->selectLastWord();
705                         fl_set_object_label(fd_form_spell_check->text, word);
706                         fl_set_input(fd_form_spell_check->input, word);
707                         fl_clear_browser(fd_form_spell_check->browser);
708                         for (i = 0; i < result->count; ++i) {
709                                 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
710                         }
711
712                         int clickline = -1;
713                         while (true) {
714                                 obj = fl_do_forms();
715                                 if (obj == fd_form_spell_check->insert) {
716                                         ispell_insert_word(word);
717                                         break;
718                                 }
719                                 if (obj == fd_form_spell_check->accept) {
720                                         ispell_accept_word(word);
721                                         break;
722                                 }
723                                 if (obj == fd_form_spell_check->ignore) {
724                                         break;
725                                 }
726                                 if (obj == fd_form_spell_check->replace || 
727                                     obj == fd_form_spell_check->input) {
728                                         ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
729                                         bv->replaceWord(fl_get_input(fd_form_spell_check->input));
730                                         break;
731                                 }
732                                 if (obj == fd_form_spell_check->browser) {
733                                         // implements double click in the browser window.
734                                         // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
735                                         if (clickline == 
736                                             fl_get_browser(fd_form_spell_check->browser)) {
737                                                 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
738                                                 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
739                                                 break;
740                                         }
741                                         clickline = fl_get_browser(fd_form_spell_check->browser);
742                                         fl_set_input(fd_form_spell_check->input, 
743                                                      fl_get_browser_line(fd_form_spell_check->browser,
744                                                                          fl_get_browser(fd_form_spell_check->browser)));
745                                                      
746                                 }
747                                 if (obj == fd_form_spell_check->stop) {
748                                         delete result;
749                                         delete[] word;
750                                         ispell_terminate();
751                                         return true;
752                                 }
753             
754                                 if (obj == fd_form_spell_check->done) {
755                                         delete result;
756                                         delete[] word;
757                                         ispell_terminate();
758                                         return false;
759                                 }
760                         }
761                 }
762                 default:
763                         delete result;
764                         delete[] word;
765                 }
766         }
767    
768         if(isp_pid!= -1) {
769                 ispell_terminate();
770                 string word_msg(tostr(word_count));
771                 if (word_count != 1) {
772                         word_msg += _(" words checked.");
773                 } else {
774                         word_msg += _(" word checked.");
775                 }
776                 fl_show_message("", _("Spellchecking completed!"),
777                                 word_msg.c_str());
778                 return false;
779         } else {
780                 fl_show_message(_("The ispell-process has died for some reason.\n"
781                                 "Maybe it has been killed."), "", "");
782                 fclose(out);
783                 return true;
784         }
785 }
786
787
788 void sigchldhandler(pid_t pid, int * status)
789
790         if (isp_pid > 0)
791                 if (pid == isp_pid) {
792                         isp_pid= -1;
793                         fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
794                                                                to nonblocking so we can 
795                                                                continue */
796                 }
797         sigchldchecker(pid, status);
798 }