]> git.lyx.org Git - lyx.git/blob - src/spellchecker.C
Patch from Dekel: display text RTL in spellchecker and TOC
[lyx.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 <algorithm>
41
42 #include "LString.h"
43 #include "sp_form.h"
44 #include "spellchecker.h"
45 #include "lyx_main.h"
46 #include "buffer.h"
47 #include "lyxrc.h"
48 #include "BufferView.h"
49 #include "gettext.h"
50 #include "lyx_gui_misc.h"
51 #include "debug.h"
52 #include "support/lstrings.h"
53
54
55 using std::endl;
56
57 // Spellchecker status
58 enum {
59         ISP_OK = 1,
60         ISP_ROOT,
61         ISP_COMPOUNDWORD,
62         ISP_UNKNOWN,
63         ISP_MISSED,
64         ISP_IGNORE
65 };
66
67 static bool RunSpellChecker(BufferView * bv);
68
69 static FILE * in, * out;  /* streams to communicate with ispell */
70 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
71                     // lyx_cb.C
72
73 // the true spell checker program being used
74 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
75 static ActualSpellChecker actual_spell_checker;
76
77 static int isp_fd;
78
79 static FD_form_spell_options *fd_form_spell_options = 0;
80 FD_form_spell_check *fd_form_spell_check = 0;
81
82 //void sigchldhandler(int sig);
83 void sigchldhandler(pid_t pid, int *status);
84
85 //extern void sigchldchecker(int sig);
86 extern void sigchldchecker(pid_t pid, int *status);
87
88 struct isp_result {
89         int flag;
90         int count;
91         string str;
92         char ** misses;
93         isp_result() {
94                 flag = ISP_UNKNOWN;
95                 count = 0;
96                 misses = static_cast<char**>(0);
97         }
98         ~isp_result() {
99                 delete[] misses;
100         }
101 };
102
103
104 /***** Spellchecker options *****/
105
106 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
107 // now also string. Amazing, eh? (Asger)
108
109 // Set (sane) values in form to current spellchecker options
110 void SpellOptionsUpdate() 
111 {
112         // Alternate language
113         if (lyxrc.isp_alt_lang.empty()) {
114                 lyxrc.isp_use_alt_lang = false;
115         } else {
116                 fl_set_input(fd_form_spell_options->altlang_input,
117                              lyxrc.isp_alt_lang.c_str());
118         }
119         if (lyxrc.isp_use_alt_lang) {
120                 fl_set_button(fd_form_spell_options->buflang, 0);
121                 fl_set_button(fd_form_spell_options->altlang, 1);
122         } else {
123                 fl_set_button(fd_form_spell_options->buflang, 1);
124                 fl_set_button(fd_form_spell_options->altlang, 0);
125         }  
126
127         // Personal dictionary
128         if (lyxrc.isp_pers_dict.empty()) {
129                 lyxrc.isp_use_pers_dict = false;
130         } else {
131                 fl_set_input(fd_form_spell_options->perdict_input,
132                              lyxrc.isp_pers_dict.c_str());
133         }
134         fl_set_button(fd_form_spell_options->perdict,
135                       lyxrc.isp_use_pers_dict ? 1:0);
136
137         // Escape chars
138         if (lyxrc.isp_esc_chars.empty()) {
139                 lyxrc.isp_use_esc_chars = false;
140         } else {
141                 fl_set_input(fd_form_spell_options->esc_chars_input,
142                              lyxrc.isp_esc_chars.c_str());
143         }
144         fl_set_button(fd_form_spell_options->esc_chars,
145                       lyxrc.isp_use_esc_chars ? 1:0);
146
147         // Options
148         fl_set_button(fd_form_spell_options->compounds,
149                       lyxrc.isp_accept_compound ? 1 : 0);
150         fl_set_button(fd_form_spell_options->inpenc,
151                       lyxrc.isp_use_input_encoding ? 1 : 0);
152 }
153
154 // Update spellchecker options
155 void SpellOptionsApplyCB(FL_OBJECT *, long)
156 {
157         // Build new status from form data
158         lyxrc.isp_use_alt_lang = 
159                 fl_get_button(fd_form_spell_options->altlang);
160         lyxrc.isp_use_pers_dict = 
161                 fl_get_button(fd_form_spell_options->perdict);
162         lyxrc.isp_accept_compound = 
163                 fl_get_button(fd_form_spell_options->compounds);
164         lyxrc.isp_use_esc_chars = 
165                 fl_get_button(fd_form_spell_options->esc_chars);
166         lyxrc.isp_use_input_encoding = 
167                 fl_get_button(fd_form_spell_options->inpenc);
168
169         // Update strings with data from input fields
170         lyxrc.isp_alt_lang = 
171                 fl_get_input(fd_form_spell_options->altlang_input);
172         lyxrc.isp_pers_dict = 
173                 fl_get_input(fd_form_spell_options->perdict_input);
174         lyxrc.isp_esc_chars = 
175                 fl_get_input(fd_form_spell_options->esc_chars_input);
176
177         // Update form
178         SpellOptionsUpdate();
179 }
180
181
182 void SpellOptionsCancelCB(FL_OBJECT *, long)
183 {
184         fl_hide_form(fd_form_spell_options->form_spell_options);
185 }
186
187
188 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
189 {
190         SpellOptionsApplyCB(ob, data);
191         SpellOptionsCancelCB(ob, data);
192 }
193
194
195 // Show spellchecker options form
196 void SpellCheckerOptions()
197 {
198         // Create form if nescessary
199         if (fd_form_spell_options ==  0) {
200                 fd_form_spell_options = create_form_form_spell_options();
201                 // Make sure pressing the close box does not kill LyX. (RvdK)
202                 fl_set_form_atclose(fd_form_spell_options->form_spell_options, 
203                                     CancelCloseBoxCB, 0);
204         }
205
206         // Update form to current options
207         SpellOptionsUpdate();
208
209         // Focus in alternate language field
210         fl_set_focus_object(fd_form_spell_options->form_spell_options,
211                             fd_form_spell_options->altlang_input);
212
213         // Show form
214         if (fd_form_spell_options->form_spell_options->visible) {
215                 fl_raise_form(fd_form_spell_options->form_spell_options);
216         } else {
217                 fl_show_form(fd_form_spell_options->form_spell_options,
218                              FL_PLACE_MOUSE, FL_FULLBORDER,
219                              _("Spellchecker Options"));
220         }
221 }
222
223
224 /***** Spellchecker *****/
225
226 // Could also use a clean up. (Asger Alstrup)
227
228 static
229 void create_ispell_pipe(BufferParams const & params, string const & lang)
230 {
231         static char o_buf[BUFSIZ];  // jc: it could be smaller
232         int pipein[2], pipeout[2];
233         char * argv[14];
234         int argc;
235
236         isp_pid = -1;
237
238         if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
239                 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
240                 return;
241         }
242
243         if ((out = fdopen(pipein[1], "w")) == 0) {
244                 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
245                        << endl;
246                 return;
247         }
248
249         if ((in = fdopen(pipeout[0], "r")) == 0) {
250                 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
251                        << endl;
252                 return;
253         }
254
255         setvbuf(out, o_buf, _IOLBF, BUFSIZ);
256
257         isp_fd = pipeout[0];
258
259         isp_pid = fork();
260
261         if(isp_pid == -1) {
262                 lyxerr << "LyX: Can't create child process for spellchecker!"
263                        << endl;
264                 return;
265         }
266
267         if(isp_pid == 0) {        
268                 /* child process */
269                 dup2(pipein[0], STDIN_FILENO);
270                 dup2(pipeout[1], STDOUT_FILENO);
271                 close(pipein[0]);
272                 close(pipein[1]);
273                 close(pipeout[0]);
274                 close(pipeout[1]);
275
276                 argc = 0;
277                 char * tmp = new char[lyxrc.isp_command.length() + 1];
278                 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
279                 tmp[lyxrc.isp_command.length()] = '\0';
280                 argv[argc++] = tmp;
281                 tmp = new char[3];
282                 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
283                 argv[argc++] = tmp;
284
285                 if (lang != "default") {
286                         tmp = new char[3];
287                         string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
288                         argv[argc++] = tmp;
289                         tmp = new char[lang.length() + 1];
290                         lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
291                         argv[argc++] = tmp;
292                 }
293
294                 if (lyxrc.isp_accept_compound) {
295                         // Consider run-together words as legal compounds
296                         tmp = new char[3];
297                         string("-C").copy(tmp, 2); tmp[2] = '\0';
298                         argv[argc++] = tmp;
299                 } else {
300                         // Report run-together words with
301                         // missing blanks as errors
302                         tmp = new char[3]; 
303                         string("-B").copy(tmp, 2); tmp[2] = '\0';
304                         argv[argc++] = tmp; 
305                 }
306                 if (lyxrc.isp_use_esc_chars) {
307                         // Specify additional characters that
308                         // can be part of a word
309                         tmp = new char[3];
310                         string("-w").copy(tmp, 2); tmp[2] = '\0';
311                         argv[argc++] = tmp; 
312                         // Put the escape chars in ""s
313                         string tms = "\"" + lyxrc.isp_esc_chars + "\"";
314                         tmp = new char[tms.length() + 1];
315                         tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
316                         argv[argc++] = tmp;
317                 }
318                 if (lyxrc.isp_use_pers_dict) {
319                         // Specify an alternate personal dictionary
320                         tmp = new char[3];
321                         string("-p").copy(tmp, 2);
322                         tmp[2]= '\0';
323                         argv[argc++] = tmp;
324                         tmp = new char[lyxrc.isp_pers_dict.length() + 1];
325                         lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
326                         tmp[lyxrc.isp_pers_dict.length()] = '\0';
327                         argv[argc++] = tmp;
328                 }
329                 if (lyxrc.isp_use_input_encoding &&
330                     params.inputenc != "default") {
331                         tmp = new char[3];
332                         string("-T").copy(tmp, 2); tmp[2] = '\0';
333                         argv[argc++] = tmp; // Input encoding
334                         tmp = new char[params.inputenc.length() + 1];
335                         params.inputenc.copy(tmp, params.inputenc.length());
336                         tmp[params.inputenc.length()] = '\0';
337                         argv[argc++] = tmp;
338                 }
339
340                 argv[argc++] = 0;
341
342                 execvp(argv[0], const_cast<char * const *>(argv));
343
344                 // free the memory used by string::copy in the
345                 // setup of argv
346                 for (int i= 0; i < argc -1; ++i)
347                         delete[] argv[i];
348                 
349                 lyxerr << "LyX: Failed to start ispell!" << endl;
350                 _exit(0);
351         }
352
353         /* Parent process: Read ispells identification message */
354         // Hmm...what are we using this id msg for? Nothing? (Lgb)
355         // Actually I used it to tell if it's truly Ispell or if it's
356         // aspell -- (kevinatk@home.com)
357         char buf[2048];
358         fd_set infds;
359         struct timeval tv;
360         int retval = 0;
361         FD_ZERO(&infds);
362         FD_SET(pipeout[0], &infds);
363         tv.tv_sec = 15; // fifteen second timeout. Probably too much,
364                         // but it can't really hurt.
365         tv.tv_usec = 0;
366
367         // Configure provides us with macros which are supposed to do
368         // the right typecast.
369         retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1), 
370                         SELECT_TYPE_ARG234 (&infds), 
371                         0, 
372                         0, 
373                         SELECT_TYPE_ARG5 (&tv));
374
375         if (retval > 0) {
376                 // Ok, do the reading. We don't have to FD_ISSET since
377                 // there is only one fd in infds.
378                 fgets(buf, 2048, in);
379                 
380                 // determine if the spell checker is really Aspell
381                 if (strstr(buf, "Aspell"))
382                   actual_spell_checker = ASC_ASPELL;
383                 else
384                   actual_spell_checker = ASC_ISPELL;
385                 
386         } else if (retval == 0) {
387                 // timeout. Give nice message to user.
388                 lyxerr << "Ispell read timed out, what now?" << endl;
389                 // This probably works but could need some thought
390                 isp_pid = -1;
391                 close(pipeout[0]); close(pipeout[1]);
392                 close(pipein[0]); close(pipein[1]);
393                 isp_fd = -1;
394         } else {
395                 // Select returned error
396                 lyxerr << "Select on ispell returned error, what now?" << endl;
397         }
398 }
399
400
401 // Send word to ispell and get reply
402 static
403 isp_result *ispell_check_word(char *word)
404 {
405         //Please rewrite to use string.
406         isp_result *result;
407         char buf[1024], *p;
408         int count, i;
409
410         fputs(word, out); 
411         fputc('\n', out);
412   
413         fgets(buf, 1024, in); 
414   
415         /* I think we have to check if ispell is still alive here because
416            the signal-handler could have disabled blocking on the fd */
417         if (isp_pid == -1) return 0;
418
419         result = new isp_result;
420   
421         switch (*buf) {
422         case '*': // Word found
423                 result->flag = ISP_OK;
424                 break;
425         case '+': // Word found through affix removal
426                 result->flag = ISP_ROOT;
427                 break;
428         case '-': // Word found through compound formation
429                 result->flag = ISP_COMPOUNDWORD;
430                 break;
431         case '\n': // Number or when in terse mode: no problems
432                 result->flag = ISP_IGNORE;
433                 break;
434         case '#': // Not found, no near misses and guesses
435                 result->flag = ISP_UNKNOWN;
436                 break;
437         case '?': // Not found, and no near misses, but guesses (guesses are ignored)
438         case '&': // Not found, but we have near misses
439         {
440                 result->flag = ISP_MISSED;
441                 result->str = buf;
442                 // nb is leaked! where should it be freed? I have to
443                 // admit I do not understand the intent of the code :(
444                 // (JMarc) 
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 inline
475 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 inline
490 void ispell_terse_mode()
491 {
492         fputs("!\n", out); // Set terse mode (silently accept correct words)
493 }
494
495
496 static inline
497 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 inline
506 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 inline
514 void ispell_store_replacement(char const *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(BufferView * bv)
526 {
527         FL_OBJECT * obj;
528         int ret;
529
530         // Exit if we don't have a document open
531         if (!bv->available())
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 (!bv->buffer()->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(bv);
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         bv->endOfSpellCheck();
635         return;
636 }
637
638
639 // Perform an ispell session
640 static
641 bool RunSpellChecker(BufferView * bv)
642 {
643         isp_result * result;
644         int i, newvalue;
645         FL_OBJECT * obj;
646
647         string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
648         bool rtl = tmp == "hebrew" || tmp == "arabic";
649
650         int oldval = 0;  /* used for updating slider only when needed */
651         float newval = 0.0;
652    
653         /* create ispell process */
654         create_ispell_pipe(bv->buffer()->params, tmp);
655
656         if (isp_pid == -1) {
657                 fl_show_message(
658                         _("\n\n"
659                           "The ispell-process has died for some reason. *One* possible reason\n"
660                           "could be that you do not have a dictionary file\n"
661                           "for the language of this document installed.\n"
662                           "Check /usr/lib/ispell or set another\n"
663                           "dictionary in the Spellchecker Options menu."), "", "");
664                 fclose(out);
665                 return true;
666         }
667
668         // Put ispell in terse mode to improve speed
669         ispell_terse_mode();
670
671         unsigned int word_count = 0;
672         while (true) {
673                 char * word = bv->nextWord(newval);
674                 if (word == 0) break;
675                 ++word_count;
676                 
677                 // Update slider if and only if value has changed
678                 newvalue = int(100.0*newval);
679                 if(newvalue!= oldval) {
680                         oldval = newvalue;
681                         fl_set_slider_value(fd_form_spell_check->slider, oldval);
682                 }
683
684                 if (word_count%1000 == 0) {
685                         obj =  fl_check_forms();
686                         if (obj == fd_form_spell_check->stop) {
687                                 delete[] word;
688                                 ispell_terminate();
689                                 return true;
690                         }
691                         if (obj == fd_form_spell_check->done) {
692                                 delete[] word;
693                                 ispell_terminate(); 
694                                 return false;
695                         }
696                 }
697
698                 result = ispell_check_word(word);
699                 if (isp_pid == -1) {
700                         delete result;
701                         delete[] word;
702                         break;
703                 }
704
705                 switch (result->flag) {
706                 case ISP_UNKNOWN:
707                 case ISP_MISSED:
708                 {
709                         bv->selectLastWord();
710                         if (rtl) {
711                                 string tmp = word;
712                                 reverse(tmp.begin(),tmp.end());
713                                 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
714                         } else
715                                 fl_set_object_label(fd_form_spell_check->text, word);
716                         fl_set_input(fd_form_spell_check->input, word);
717                         fl_clear_browser(fd_form_spell_check->browser);
718                         for (i = 0; i < result->count; ++i) {
719                                 if (rtl) {
720                                         string tmp = result->misses[i];
721                                         reverse(tmp.begin(),tmp.end());
722                                         fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
723                                 } else
724                                         fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
725                         }
726
727                         int clickline = -1;
728                         while (true) {
729                                 obj = fl_do_forms();
730                                 if (obj == fd_form_spell_check->insert) {
731                                         ispell_insert_word(word);
732                                         break;
733                                 }
734                                 if (obj == fd_form_spell_check->accept) {
735                                         ispell_accept_word(word);
736                                         break;
737                                 }
738                                 if (obj == fd_form_spell_check->ignore) {
739                                         break;
740                                 }
741                                 if (obj == fd_form_spell_check->replace || 
742                                     obj == fd_form_spell_check->input) {
743                                         ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
744                                         bv->replaceWord(fl_get_input(fd_form_spell_check->input));
745                                         break;
746                                 }
747                                 if (obj == fd_form_spell_check->browser) {
748                                         // implements double click in the browser window.
749                                         // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
750                                         if (clickline == 
751                                             fl_get_browser(fd_form_spell_check->browser)) {
752                                                 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
753                                                 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
754                                                 break;
755                                         }
756                                         clickline = fl_get_browser(fd_form_spell_check->browser);
757                                         /// Why not use
758                                         /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
759                                         if (rtl) {
760                                                 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
761                                                                                  clickline);
762                                                 reverse(tmp.begin(),tmp.end());
763                                                 fl_set_input(fd_form_spell_check->input, tmp.c_str());
764                                         } else
765                                                 fl_set_input(fd_form_spell_check->input,
766                                                              fl_get_browser_line(fd_form_spell_check->browser,
767                                                                                  clickline));
768                                 }
769                                 if (obj == fd_form_spell_check->stop) {
770                                         delete result;
771                                         delete[] word;
772                                         ispell_terminate();
773                                         return true;
774                                 }
775             
776                                 if (obj == fd_form_spell_check->done) {
777                                         delete result;
778                                         delete[] word;
779                                         ispell_terminate();
780                                         return false;
781                                 }
782                         }
783                 }
784                 default:
785                         delete result;
786                         delete[] word;
787                 }
788         }
789    
790         if(isp_pid!= -1) {
791                 ispell_terminate();
792                 string word_msg(tostr(word_count));
793                 if (word_count != 1) {
794                         word_msg += _(" words checked.");
795                 } else {
796                         word_msg += _(" word checked.");
797                 }
798                 fl_show_message("", _("Spellchecking completed!"),
799                                 word_msg.c_str());
800                 return false;
801         } else {
802                 fl_show_message(_("The ispell-process has died for some reason.\n"
803                                 "Maybe it has been killed."), "", "");
804                 fclose(out);
805                 return true;
806         }
807 }
808
809
810 void sigchldhandler(pid_t pid, int * status)
811
812         if (isp_pid > 0)
813                 if (pid == isp_pid) {
814                         isp_pid= -1;
815                         fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
816                                                                to nonblocking so we can 
817                                                                continue */
818                 }
819         sigchldchecker(pid, status);
820 }