]> git.lyx.org Git - lyx.git/blob - src/spellchecker.C
get rid of dead code, some new functions constify variables.
[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 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include <unistd.h>
20 #include <fcntl.h>
21 #include <cstdlib>
22 #include <cstring>
23 #include <csignal>
24 #include <sys/wait.h>
25 #include <sys/types.h>
26 #include <cctype>
27 #include FORMS_H_LOCATION
28
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
31 # include <ctime>
32 #else
33 # if HAVE_SYS_TIME_H
34 #  include <sys/time.h>
35 # else
36 #  include <ctime>
37 # endif
38 #endif
39
40 #ifdef HAVE_SYS_SELECT_H
41 # ifdef HAVE_STRINGS_H
42    // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
43 # include <strings.h> 
44 # endif
45 #include <sys/select.h>
46 #endif
47
48 #include <algorithm>
49
50 #include "LString.h"
51 #include "sp_form.h"
52 #include "spellchecker.h"
53 #include "lyx_main.h"
54 #include "buffer.h"
55 #include "lyxrc.h"
56 #include "BufferView.h"
57 #include "gettext.h"
58 #include "lyx_gui_misc.h"
59 #include "debug.h"
60 #include "support/lstrings.h"
61 #include "language.h"
62 #include "encoding.h"
63 #include "support/lstrings.h"
64
65 //#define USE_PSPELL 1
66
67 #ifdef USE_PSPELL
68
69 #include <pspell/pspell.h>
70
71 #endif
72
73 using std::reverse;
74 using std::endl;
75
76 // Spellchecker status
77 enum {
78         ISP_OK = 1,
79         ISP_ROOT,
80         ISP_COMPOUNDWORD,
81         ISP_UNKNOWN,
82         ISP_MISSED,
83         ISP_IGNORE
84 };
85
86 static bool RunSpellChecker(BufferView * bv);
87
88 #ifndef USE_PSPELL
89
90 static FILE * in, * out;  /* streams to communicate with ispell */
91 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
92                     // lyx_cb.C
93
94 // the true spell checker program being used
95 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
96 static ActualSpellChecker actual_spell_checker;
97
98 static int isp_fd;
99
100 #else
101
102 PspellManager * sc;
103
104 #endif
105
106 static FD_form_spell_options *fd_form_spell_options = 0;
107 FD_form_spell_check *fd_form_spell_check = 0;
108
109 //void sigchldhandler(int sig);
110 void sigchldhandler(pid_t pid, int *status);
111
112 //extern void sigchldchecker(int sig);
113 extern void sigchldchecker(pid_t pid, int *status);
114
115 #ifndef USE_PSPELL
116
117 struct isp_result {
118         int flag;
119         char * str;
120         char * b;
121         char * e;
122         const char * next_miss();
123         isp_result() {
124                 flag = ISP_UNKNOWN;
125                 str = 0;
126         }
127         ~isp_result() {
128                 delete[] str;
129         }
130 };
131
132 const char * isp_result::next_miss() {
133         if (str == 0 || *(e+1) == '\0') return 0;
134         b = e + 2;
135         e = strpbrk(b, ",\n");
136         *e = '\0';
137         return b;
138 }
139
140 #else
141
142 struct isp_result {
143         int flag;
144         PspellStringEmulation * els;
145         
146         const char * next_miss();
147         isp_result() {
148                 flag = ISP_UNKNOWN;
149                 els = 0;
150         }
151         ~isp_result() {
152                 delete_pspell_string_emulation(els);
153         }
154 };
155
156 const char * isp_result::next_miss() 
157 {
158         return pspell_string_emulation_next(els);
159 }
160
161 #endif
162
163 const char * spell_error;
164
165
166 /***** Spellchecker options *****/
167
168 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
169 // now also string. Amazing, eh? (Asger)
170
171 // Set (sane) values in form to current spellchecker options
172 void SpellOptionsUpdate() 
173 {
174         // Alternate language
175         if (lyxrc.isp_alt_lang.empty()) {
176                 lyxrc.isp_use_alt_lang = false;
177         } else {
178                 fl_set_input(fd_form_spell_options->altlang_input,
179                              lyxrc.isp_alt_lang.c_str());
180         }
181         if (lyxrc.isp_use_alt_lang) {
182                 fl_set_button(fd_form_spell_options->buflang, 0);
183                 fl_set_button(fd_form_spell_options->altlang, 1);
184         } else {
185                 fl_set_button(fd_form_spell_options->buflang, 1);
186                 fl_set_button(fd_form_spell_options->altlang, 0);
187         }  
188
189         // Personal dictionary
190         if (lyxrc.isp_pers_dict.empty()) {
191                 lyxrc.isp_use_pers_dict = false;
192         } else {
193                 fl_set_input(fd_form_spell_options->perdict_input,
194                              lyxrc.isp_pers_dict.c_str());
195         }
196         fl_set_button(fd_form_spell_options->perdict,
197                       lyxrc.isp_use_pers_dict ? 1:0);
198
199         // Escape chars
200         if (lyxrc.isp_esc_chars.empty()) {
201                 lyxrc.isp_use_esc_chars = false;
202         } else {
203                 fl_set_input(fd_form_spell_options->esc_chars_input,
204                              lyxrc.isp_esc_chars.c_str());
205         }
206         fl_set_button(fd_form_spell_options->esc_chars,
207                       lyxrc.isp_use_esc_chars ? 1:0);
208
209         // Options
210         fl_set_button(fd_form_spell_options->compounds,
211                       lyxrc.isp_accept_compound ? 1 : 0);
212         fl_set_button(fd_form_spell_options->inpenc,
213                       lyxrc.isp_use_input_encoding ? 1 : 0);
214 }
215
216 // Update spellchecker options
217 void SpellOptionsApplyCB(FL_OBJECT *, long)
218 {
219         // Build new status from form data
220         lyxrc.isp_use_alt_lang = 
221                 fl_get_button(fd_form_spell_options->altlang);
222         lyxrc.isp_use_pers_dict = 
223                 fl_get_button(fd_form_spell_options->perdict);
224         lyxrc.isp_accept_compound = 
225                 fl_get_button(fd_form_spell_options->compounds);
226         lyxrc.isp_use_esc_chars = 
227                 fl_get_button(fd_form_spell_options->esc_chars);
228         lyxrc.isp_use_input_encoding = 
229                 fl_get_button(fd_form_spell_options->inpenc);
230
231         // Update strings with data from input fields
232         lyxrc.isp_alt_lang = 
233                 fl_get_input(fd_form_spell_options->altlang_input);
234         lyxrc.isp_pers_dict = 
235                 fl_get_input(fd_form_spell_options->perdict_input);
236         lyxrc.isp_esc_chars = 
237                 fl_get_input(fd_form_spell_options->esc_chars_input);
238
239         // Update form
240         SpellOptionsUpdate();
241 }
242
243
244 void SpellOptionsCancelCB(FL_OBJECT *, long)
245 {
246         fl_hide_form(fd_form_spell_options->form_spell_options);
247 }
248
249
250 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
251 {
252         SpellOptionsApplyCB(ob, data);
253         SpellOptionsCancelCB(ob, data);
254 }
255
256
257 // Show spellchecker options form
258 void SpellCheckerOptions()
259 {
260         // Create form if nescessary
261         if (fd_form_spell_options ==  0) {
262                 fd_form_spell_options = create_form_form_spell_options();
263                 // Make sure pressing the close box does not kill LyX. (RvdK)
264                 fl_set_form_atclose(fd_form_spell_options->form_spell_options, 
265                                     CancelCloseBoxCB, 0);
266         }
267
268         // Update form to current options
269         SpellOptionsUpdate();
270
271         // Focus in alternate language field
272         fl_set_focus_object(fd_form_spell_options->form_spell_options,
273                             fd_form_spell_options->altlang_input);
274
275         // Show form
276         if (fd_form_spell_options->form_spell_options->visible) {
277                 fl_raise_form(fd_form_spell_options->form_spell_options);
278         } else {
279                 fl_show_form(fd_form_spell_options->form_spell_options,
280                              FL_PLACE_MOUSE, FL_FULLBORDER,
281                              _("Spellchecker Options"));
282         }
283 }
284
285 #ifndef USE_PSPELL
286
287 /***** Spellchecker *****/
288
289 // Could also use a clean up. (Asger Alstrup)
290
291 static
292 void init_spell_checker(BufferParams const & params, string const & lang)
293 {
294         static char o_buf[BUFSIZ];  // jc: it could be smaller
295         int pipein[2], pipeout[2];
296         char * argv[14];
297         int argc;
298
299         isp_pid = -1;
300
301         if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
302                 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
303                 goto END;
304         }
305
306         if ((out = fdopen(pipein[1], "w")) == 0) {
307                 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
308                        << endl;
309                 goto END;
310         }
311
312         if ((in = fdopen(pipeout[0], "r")) == 0) {
313                 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
314                        << endl;
315                 goto END;
316         }
317
318         setvbuf(out, o_buf, _IOLBF, BUFSIZ);
319
320         isp_fd = pipeout[0];
321
322         isp_pid = fork();
323
324         if(isp_pid == -1) {
325                 lyxerr << "LyX: Can't create child process for spellchecker!"
326                        << endl;
327                 goto END;
328         }
329         
330         if(isp_pid == 0) {        
331                 /* child process */
332                 dup2(pipein[0], STDIN_FILENO);
333                 dup2(pipeout[1], STDOUT_FILENO);
334                 close(pipein[0]);
335                 close(pipein[1]);
336                 close(pipeout[0]);
337                 close(pipeout[1]);
338
339                 argc = 0;
340                 char * tmp = new char[lyxrc.isp_command.length() + 1];
341                 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
342                 tmp[lyxrc.isp_command.length()] = '\0';
343                 argv[argc++] = tmp;
344                 tmp = new char[3];
345                 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
346                 argv[argc++] = tmp;
347
348                 if (lang != "default") {
349                         tmp = new char[3];
350                         string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
351                         argv[argc++] = tmp;
352                         tmp = new char[lang.length() + 1];
353                         lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
354                         argv[argc++] = tmp;
355                 }
356
357                 if (lyxrc.isp_accept_compound) {
358                         // Consider run-together words as legal compounds
359                         tmp = new char[3];
360                         string("-C").copy(tmp, 2); tmp[2] = '\0';
361                         argv[argc++] = tmp;
362                 } else {
363                         // Report run-together words with
364                         // missing blanks as errors
365                         tmp = new char[3]; 
366                         string("-B").copy(tmp, 2); tmp[2] = '\0';
367                         argv[argc++] = tmp; 
368                 }
369                 if (lyxrc.isp_use_esc_chars) {
370                         // Specify additional characters that
371                         // can be part of a word
372                         tmp = new char[3];
373                         string("-w").copy(tmp, 2); tmp[2] = '\0';
374                         argv[argc++] = tmp; 
375                         // Put the escape chars in ""s
376                         string tms = "\"" + lyxrc.isp_esc_chars + "\"";
377                         tmp = new char[tms.length() + 1];
378                         tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
379                         argv[argc++] = tmp;
380                 }
381                 if (lyxrc.isp_use_pers_dict) {
382                         // Specify an alternate personal dictionary
383                         tmp = new char[3];
384                         string("-p").copy(tmp, 2);
385                         tmp[2]= '\0';
386                         argv[argc++] = tmp;
387                         tmp = new char[lyxrc.isp_pers_dict.length() + 1];
388                         lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
389                         tmp[lyxrc.isp_pers_dict.length()] = '\0';
390                         argv[argc++] = tmp;
391                 }
392                 if (lyxrc.isp_use_input_encoding &&
393                     params.inputenc != "default") {
394                         string enc = (params.inputenc == "auto")
395                                 ? params.language_info->encoding()->LatexName()
396                                 : params.inputenc;
397                         string::size_type n = enc.length();
398                         tmp = new char[3];
399                         string("-T").copy(tmp, 2); tmp[2] = '\0';
400                         argv[argc++] = tmp; // Input encoding
401                         tmp = new char[n + 1];
402                         enc.copy(tmp, n);
403                         tmp[n] = '\0';
404                         argv[argc++] = tmp;
405                 }
406
407                 argv[argc++] = 0;
408
409                 execvp(argv[0], const_cast<char * const *>(argv));
410
411                 // free the memory used by string::copy in the
412                 // setup of argv
413                 for (int i= 0; i < argc -1; ++i)
414                         delete[] argv[i];
415                 
416                 lyxerr << "LyX: Failed to start ispell!" << endl;
417                 _exit(0);
418         }
419         {
420         /* Parent process: Read ispells identification message */
421         // Hmm...what are we using this id msg for? Nothing? (Lgb)
422         // Actually I used it to tell if it's truly Ispell or if it's
423         // aspell -- (kevinatk@home.com)
424         char buf[2048];
425         fd_set infds;
426         struct timeval tv;
427         int retval = 0;
428         FD_ZERO(&infds);
429         FD_SET(pipeout[0], &infds);
430         tv.tv_sec = 15; // fifteen second timeout. Probably too much,
431                         // but it can't really hurt.
432         tv.tv_usec = 0;
433
434         // Configure provides us with macros which are supposed to do
435         // the right typecast.
436         retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1), 
437                         SELECT_TYPE_ARG234 (&infds), 
438                         0, 
439                         0, 
440                         SELECT_TYPE_ARG5 (&tv));
441
442         if (retval > 0) {
443                 // Ok, do the reading. We don't have to FD_ISSET since
444                 // there is only one fd in infds.
445                 fgets(buf, 2048, in);
446                 
447                 // determine if the spell checker is really Aspell
448                 if (strstr(buf, "Aspell"))
449                   actual_spell_checker = ASC_ASPELL;
450                 else
451                   actual_spell_checker = ASC_ISPELL;
452
453                 fputs("!\n", out); // Set terse mode (silently accept correct words)
454
455                 
456         } else if (retval == 0) {
457                 // timeout. Give nice message to user.
458                 lyxerr << "Ispell read timed out, what now?" << endl;
459                 // This probably works but could need some thought
460                 isp_pid = -1;
461                 close(pipeout[0]); close(pipeout[1]);
462                 close(pipein[0]); close(pipein[1]);
463                 isp_fd = -1;
464         } else {
465                 // Select returned error
466                 lyxerr << "Select on ispell returned error, what now?" << endl;
467         }
468         }
469  END:
470         if (isp_pid == -1) {
471                 spell_error = 
472                         "\n\n"
473                         "The ispell-process has died for some reason. *One* possible reason\n"
474                         "could be that you do not have a dictionary file\n"
475                         "for the language of this document installed.\n"
476                         "Check /usr/lib/ispell or set another\n"
477                         "dictionary in the Spellchecker Options menu.";
478         } else {
479                 spell_error = 0;
480         }
481 }
482
483 static
484 bool sc_still_alive() {
485         return isp_pid != -1;
486 }
487
488 static
489 void sc_clean_up_after_error() 
490 {
491         ::fclose(out);
492 }
493
494 // Send word to ispell and get reply
495 static
496 isp_result * sc_check_word(string const & word)
497 {
498         //Please rewrite to use string.
499
500         ::fputs(word.c_str(), out);
501         ::fputc('\n', out);
502   
503         char buf[1024];
504         ::fgets(buf, 1024, in); 
505   
506         /* I think we have to check if ispell is still alive here because
507            the signal-handler could have disabled blocking on the fd */
508         if (!sc_still_alive()) return 0;
509
510         isp_result * result = new isp_result;
511   
512         switch (*buf) {
513         case '*': // Word found
514                 result->flag = ISP_OK;
515                 break;
516         case '+': // Word found through affix removal
517                 result->flag = ISP_ROOT;
518                 break;
519         case '-': // Word found through compound formation
520                 result->flag = ISP_COMPOUNDWORD;
521                 break;
522         case '\n': // Number or when in terse mode: no problems
523                 result->flag = ISP_IGNORE;
524                 break;
525         case '#': // Not found, no near misses and guesses
526                 result->flag = ISP_UNKNOWN;
527                 break;
528         case '?': // Not found, and no near misses, but guesses (guesses are ignored)
529         case '&': // Not found, but we have near misses
530         {
531                 result->flag = ISP_MISSED;
532                 char * p = strpbrk(buf, ":");
533                 result->str = new char[strlen(p) + 1];
534                 result->e   = result->str;
535                 strcpy(result->str, p);
536                 break;
537         }
538         default: // This shouldn't happend, but you know Murphy
539                 result->flag = ISP_UNKNOWN;
540         }
541
542         *buf = 0;
543         if (result->flag!= ISP_IGNORE) {
544                 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
545         }
546         return result;
547 }
548
549
550 static inline 
551 void close_spell_checker()
552 {
553         // Note: If you decide to optimize this out when it is not 
554         // needed please note that when Aspell is used this command 
555         // is also needed to save the replacement dictionary.
556         // -- Kevin Atkinson (kevinatk@home.com)
557
558         fputs("#\n", out); // Save personal dictionary
559
560         fflush(out);
561         fclose(out);
562 }
563
564
565 static inline 
566 void sc_insert_word(string const & word)
567 {
568         ::fputc('*', out); // Insert word in personal dictionary
569         ::fputs(word.c_str(), out);
570         ::fputc('\n', out);
571 }
572
573
574 static inline 
575 void sc_accept_word(string const & word) 
576 {
577         ::fputc('@', out); // Accept in this session
578         ::fputs(word.c_str(), out);
579         ::fputc('\n', out);
580 }
581
582 static inline
583 void sc_store_replacement(string const & mis, string const & cor) {
584         if(actual_spell_checker == ASC_ASPELL) {
585                 ::fputs("$$ra ", out);
586                 ::fputs(mis.c_str(), out);
587                 ::fputc(',', out);
588                 ::fputs(cor.c_str(), out);
589                 ::fputc('\n', out);
590         }
591 }
592
593 #else
594
595 PspellCanHaveError * spell_error_object;
596
597 static
598 void init_spell_checker(BufferParams const &, string const & lang)
599 {
600         PspellConfig * config = new_pspell_config();
601         string code;
602         split(lang, code, '_');
603         config->replace("language-tag", code.c_str());
604         spell_error_object = new_pspell_manager(config);
605         if (pspell_error_number(spell_error_object) != 0) {
606                 spell_error = pspell_error_message(spell_error_object);
607         } else {
608                 spell_error = 0;
609                 sc = to_pspell_manager(spell_error_object);
610                 spell_error_object = 0;
611         }
612 }
613
614 static 
615 bool sc_still_alive() {
616         return true;
617 }
618
619 static
620 void sc_clean_up_after_error() 
621 {
622         delete_pspell_can_have_error(spell_error_object);
623 }
624
625
626
627 // Send word to ispell and get reply
628 static
629 isp_result * sc_check_word(string const & word)
630 {
631         isp_result * result = new isp_result;
632 #warning Why isnt word_ok a bool? (Lgb)
633         int word_ok = pspell_manager_check(sc, word.c_str());
634         Assert(word_ok != -1);
635
636         if (word_ok) {
637                 result->flag = ISP_OK;
638         } else {
639                 PspellWordList const * sugs =
640                         pspell_manager_suggest(sc, word.c_str());
641                 Assert(sugs != 0);
642                 result->els = pspell_word_list_elements(sugs);
643                 if (pspell_word_list_empty(sugs)) 
644                         result->flag = ISP_UNKNOWN;
645                 else 
646                         result->flag = ISP_MISSED;
647         }
648         return result;
649 }
650
651
652 static inline 
653 void close_spell_checker()
654 {
655         pspell_manager_save_all_word_lists(sc);
656 }
657
658
659 static inline 
660 void sc_insert_word(string const & word)
661 {
662         pspell_manager_add_to_personal(sc, word.c_str());
663 }
664
665
666 static inline 
667 void sc_accept_word(string const & word) 
668 {
669         pspell_manager_add_to_session(sc, word.c_str());
670 }
671
672
673 static inline 
674 void sc_store_replacement(string const & mis, string const & cor)
675 {
676         pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
677 }
678
679 #endif
680
681 void ShowSpellChecker(BufferView * bv)
682 {
683         FL_OBJECT * obj;
684         int ret;
685
686         // Exit if we don't have a document open
687         if (!bv->available())
688                 return;
689
690         if (fd_form_spell_check == 0) {
691                 fd_form_spell_check = create_form_form_spell_check();
692                 // Make sure pressing the close box does not kill LyX. (RvdK)
693                 fl_set_form_atclose(fd_form_spell_check->form_spell_check, 
694                                     CancelCloseBoxCB, 0);
695         }
696
697         // Clear form
698         fl_set_slider_value(fd_form_spell_check->slider, 0);
699         fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
700         fl_set_object_label(fd_form_spell_check->text, "");
701         fl_set_input(fd_form_spell_check->input, "");
702         fl_clear_browser(fd_form_spell_check->browser);
703
704         // Show form
705         if (fd_form_spell_check->form_spell_check->visible) {
706                 fl_raise_form(fd_form_spell_check->form_spell_check);
707         } else {
708                 fl_show_form(fd_form_spell_check->form_spell_check,
709                              FL_PLACE_MOUSE, FL_FULLBORDER,
710                              _("Spellchecker"));
711         }
712         fl_deactivate_object(fd_form_spell_check->slider); 
713
714         // deactivate insert, accept, replace, and stop
715         fl_deactivate_object(fd_form_spell_check->insert);
716         fl_deactivate_object(fd_form_spell_check->accept);
717         fl_deactivate_object(fd_form_spell_check->ignore);
718         fl_deactivate_object(fd_form_spell_check->replace);
719         fl_deactivate_object(fd_form_spell_check->stop);
720         fl_deactivate_object(fd_form_spell_check->input);
721         fl_deactivate_object(fd_form_spell_check->browser);
722         fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
723         fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
724         fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
725         fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
726         fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
727         fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
728         fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
729
730         while (true) {
731                 obj = fl_do_forms();
732                 if (obj == fd_form_spell_check->options) {
733                         SpellCheckerOptions();
734                 }
735                 if (obj == fd_form_spell_check->start) {
736                         // activate insert, accept, and stop
737                         fl_activate_object(fd_form_spell_check->insert);
738                         fl_activate_object(fd_form_spell_check->accept);
739                         fl_activate_object(fd_form_spell_check->ignore);
740                         fl_activate_object(fd_form_spell_check->stop);
741                         fl_activate_object(fd_form_spell_check->input);
742                         fl_activate_object(fd_form_spell_check->browser);
743                         fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
744                         fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
745                         fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
746                         fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
747                         fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
748                         fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
749                         // activate replace only if the file is not read-only
750                         if (!bv->buffer()->isReadonly()) { 
751                           fl_activate_object(fd_form_spell_check->replace);
752                           fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
753                         }
754
755                         // deactivate options and start
756                         fl_deactivate_object(fd_form_spell_check->options);
757                         fl_deactivate_object(fd_form_spell_check->start);
758                         fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
759                         fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
760
761                         ret = RunSpellChecker(bv);
762
763                         // deactivate insert, accept, replace, and stop
764                         fl_deactivate_object(fd_form_spell_check->insert);
765                         fl_deactivate_object(fd_form_spell_check->accept);
766                         fl_deactivate_object(fd_form_spell_check->ignore);
767                         fl_deactivate_object(fd_form_spell_check->replace);
768                         fl_deactivate_object(fd_form_spell_check->stop);
769                         fl_deactivate_object(fd_form_spell_check->input);
770                         fl_deactivate_object(fd_form_spell_check->browser);
771                         fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
772                         fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
773                         fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
774                         fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
775                         fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
776                         fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
777                         fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
778
779                         // activate options and start
780                         fl_activate_object(fd_form_spell_check->options);
781                         fl_activate_object(fd_form_spell_check->start);
782                         fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
783                         fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
784
785                         // if RunSpellChecker returns false quit spellchecker
786                         if (!ret) break;
787                 }
788                 if (obj == fd_form_spell_check->done) break;
789         }
790         fl_hide_form(fd_form_spell_check->form_spell_check);
791         bv->endOfSpellCheck();
792         return;
793 }
794
795
796 // Perform a spell session
797 static
798 bool RunSpellChecker(BufferView * bv)
799 {
800         isp_result * result;
801         int newvalue;
802         FL_OBJECT * obj;
803
804 #ifdef USE_PSPELL
805         string tmp = (lyxrc.isp_use_alt_lang) ?
806             lyxrc.isp_alt_lang : bv->buffer()->params.language_info->code();
807 #else
808         string tmp = (lyxrc.isp_use_alt_lang) ?
809             lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
810 #endif
811 #warning This is not good we should find a way to identify a rtl-language in a more general way. Please have a look Dekel! (Jug)
812 // For now I'll change this to a bit more general solution but
813 // Please comment on this if you don't like it. We probaly need
814 // anoter flag something like lyxrc.isp_use_alt_lang_rtl (true/false)!
815         bool rtl;
816         if (lyxrc.isp_use_alt_lang)
817             rtl = (tmp == "hebrew" || tmp == "arabic");
818         else
819             rtl = bv->buffer()->params.language_info->RightToLeft();
820
821         int oldval = 0;  /* used for updating slider only when needed */
822         float newval = 0.0;
823    
824         /* create ispell process */
825         init_spell_checker(bv->buffer()->params, tmp);
826
827         if (spell_error != 0) {
828                 fl_show_message(_(spell_error), "", "");
829                 sc_clean_up_after_error();
830                 return false;
831         }
832
833         unsigned int word_count = 0;
834
835         while (true) {
836                 string const word = bv->nextWord(newval);
837                 if (word.empty()) break;
838                 ++word_count;
839                 
840                 // Update slider if and only if value has changed
841                 newvalue = int(100.0*newval);
842                 if(newvalue!= oldval) {
843                         oldval = newvalue;
844                         fl_set_slider_value(fd_form_spell_check->slider, oldval);
845                 }
846
847                 if (word_count%1000 == 0) {
848                         obj =  fl_check_forms();
849                         if (obj == fd_form_spell_check->stop) {
850                                 close_spell_checker();
851                                 return true;
852                         }
853                         if (obj == fd_form_spell_check->done) {
854                                 close_spell_checker();
855                                 return false;
856                         }
857                 }
858
859                 result = sc_check_word(word);
860                 if (!sc_still_alive()) {
861                         delete result;
862                         break;
863                 }
864
865                 switch (result->flag) {
866                 case ISP_UNKNOWN:
867                 case ISP_MISSED:
868                 {
869                         bv->selectLastWord();
870                         if (rtl) {
871                                 string tmp = word;
872                                 reverse(tmp.begin(),tmp.end());
873                                 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
874                         } else
875                                 fl_set_object_label(fd_form_spell_check->text, word.c_str());
876                         fl_set_input(fd_form_spell_check->input, word.c_str());
877                         fl_clear_browser(fd_form_spell_check->browser);
878                         const char * w;
879                         while ((w = result->next_miss()) != 0) {
880                                 if (rtl) {
881                                         string tmp = w;
882                                         reverse(tmp.begin(),tmp.end());
883                                         fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
884                                 } else
885                                         fl_add_browser_line(fd_form_spell_check->browser, w);
886                         }
887
888                         int clickline = -1;
889                         while (true) {
890                                 obj = fl_do_forms();
891                                 if (obj == fd_form_spell_check->insert) {
892                                         sc_insert_word(word);
893                                         break;
894                                 }
895                                 if (obj == fd_form_spell_check->accept) {
896                                         sc_accept_word(word);
897                                         break;
898                                 }
899                                 if (obj == fd_form_spell_check->ignore) {
900                                         break;
901                                 }
902                                 if (obj == fd_form_spell_check->replace || 
903                                     obj == fd_form_spell_check->input) {
904                                         sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
905                                         bv->replaceWord(fl_get_input(fd_form_spell_check->input));
906                                         break;
907                                 }
908                                 if (obj == fd_form_spell_check->browser) {
909                                         // implements double click in the browser window.
910                                         // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
911                                         if (clickline == 
912                                             fl_get_browser(fd_form_spell_check->browser)) {
913                                                 sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
914                                                 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
915                                                 break;
916                                         }
917                                         clickline = fl_get_browser(fd_form_spell_check->browser);
918                                         /// Why not use
919                                         /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
920                                         if (rtl) {
921                                                 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
922                                                                                  clickline);
923                                                 reverse(tmp.begin(),tmp.end());
924                                                 fl_set_input(fd_form_spell_check->input, tmp.c_str());
925                                         } else
926                                                 fl_set_input(fd_form_spell_check->input,
927                                                              fl_get_browser_line(fd_form_spell_check->browser,
928                                                                                  clickline));
929                                 }
930                                 if (obj == fd_form_spell_check->stop) {
931                                         delete result;
932                                         close_spell_checker();
933                                         return true;
934                                 }
935             
936                                 if (obj == fd_form_spell_check->done) {
937                                         delete result;
938                                         close_spell_checker();
939                                         return false;
940                                 }
941                         }
942                 }
943                 default:
944                         delete result;
945                 }
946         }
947    
948         if(sc_still_alive()) {
949                 close_spell_checker();
950                 string word_msg(tostr(word_count));
951                 if (word_count != 1) {
952                         word_msg += _(" words checked.");
953                 } else {
954                         word_msg += _(" word checked.");
955                 }
956                 fl_show_message("", _("Spellchecking completed!"),
957                                 word_msg.c_str());
958                 return false;
959         } else {
960                 fl_show_message(_("The spell checker has died for some reason.\n"
961                                 "Maybe it has been killed."), "", "");
962                 sc_clean_up_after_error();
963                 return true;
964         }
965 }
966
967
968 #ifndef USE_PSPELL
969
970 void sigchldhandler(pid_t pid, int * status)
971
972         if (isp_pid > 0)
973                 if (pid == isp_pid) {
974                         isp_pid= -1;
975                         fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
976                                                                to nonblocking so we can 
977                                                                continue */
978                 }
979         sigchldchecker(pid, status);
980 }
981
982 #else
983
984 void sigchldhandler(pid_t, int *)
985
986         // do nothing
987 }
988
989 #endif