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