3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1998 The LyX Team
10 * ======================================================
21 #include <sys/types.h>
23 #include FORMS_H_LOCATION
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
36 #ifdef HAVE_SYS_SELECT_H
37 #include <sys/select.h>
44 #include "spellchecker.h"
48 #include "BufferView.h"
50 #include "lyx_gui_misc.h"
52 #include "support/lstrings.h"
57 // Spellchecker status
67 static bool RunSpellChecker(BufferView * bv);
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
73 // the true spell checker program being used
74 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
75 static ActualSpellChecker actual_spell_checker;
79 static FD_form_spell_options *fd_form_spell_options = 0;
80 FD_form_spell_check *fd_form_spell_check = 0;
82 //void sigchldhandler(int sig);
83 void sigchldhandler(pid_t pid, int *status);
85 //extern void sigchldchecker(int sig);
86 extern void sigchldchecker(pid_t pid, int *status);
96 misses = static_cast<char**>(0);
104 /***** Spellchecker options *****/
106 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
107 // now also string. Amazing, eh? (Asger)
109 // Set (sane) values in form to current spellchecker options
110 void SpellOptionsUpdate()
112 // Alternate language
113 if (lyxrc.isp_alt_lang.empty()) {
114 lyxrc.isp_use_alt_lang = false;
116 fl_set_input(fd_form_spell_options->altlang_input,
117 lyxrc.isp_alt_lang.c_str());
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);
123 fl_set_button(fd_form_spell_options->buflang, 1);
124 fl_set_button(fd_form_spell_options->altlang, 0);
127 // Personal dictionary
128 if (lyxrc.isp_pers_dict.empty()) {
129 lyxrc.isp_use_pers_dict = false;
131 fl_set_input(fd_form_spell_options->perdict_input,
132 lyxrc.isp_pers_dict.c_str());
134 fl_set_button(fd_form_spell_options->perdict,
135 lyxrc.isp_use_pers_dict ? 1:0);
138 if (lyxrc.isp_esc_chars.empty()) {
139 lyxrc.isp_use_esc_chars = false;
141 fl_set_input(fd_form_spell_options->esc_chars_input,
142 lyxrc.isp_esc_chars.c_str());
144 fl_set_button(fd_form_spell_options->esc_chars,
145 lyxrc.isp_use_esc_chars ? 1:0);
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);
154 // Update spellchecker options
155 void SpellOptionsApplyCB(FL_OBJECT *, long)
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);
169 // Update strings with data from input fields
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);
178 SpellOptionsUpdate();
182 void SpellOptionsCancelCB(FL_OBJECT *, long)
184 fl_hide_form(fd_form_spell_options->form_spell_options);
188 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
190 SpellOptionsApplyCB(ob, data);
191 SpellOptionsCancelCB(ob, data);
195 // Show spellchecker options form
196 void SpellCheckerOptions()
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);
206 // Update form to current options
207 SpellOptionsUpdate();
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);
214 if (fd_form_spell_options->form_spell_options->visible) {
215 fl_raise_form(fd_form_spell_options->form_spell_options);
217 fl_show_form(fd_form_spell_options->form_spell_options,
218 FL_PLACE_MOUSE, FL_FULLBORDER,
219 _("Spellchecker Options"));
224 /***** Spellchecker *****/
226 // Could also use a clean up. (Asger Alstrup)
229 void create_ispell_pipe(BufferParams const & params, string const & lang)
231 static char o_buf[BUFSIZ]; // jc: it could be smaller
232 int pipein[2], pipeout[2];
238 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
239 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
243 if ((out = fdopen(pipein[1], "w")) == 0) {
244 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
249 if ((in = fdopen(pipeout[0], "r")) == 0) {
250 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
255 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
262 lyxerr << "LyX: Can't create child process for spellchecker!"
269 dup2(pipein[0], STDIN_FILENO);
270 dup2(pipeout[1], STDOUT_FILENO);
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';
282 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
285 if (lang != "default") {
287 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
289 tmp = new char[lang.length() + 1];
290 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
294 if (lyxrc.isp_accept_compound) {
295 // Consider run-together words as legal compounds
297 string("-C").copy(tmp, 2); tmp[2] = '\0';
300 // Report run-together words with
301 // missing blanks as errors
303 string("-B").copy(tmp, 2); tmp[2] = '\0';
306 if (lyxrc.isp_use_esc_chars) {
307 // Specify additional characters that
308 // can be part of a word
310 string("-w").copy(tmp, 2); tmp[2] = '\0';
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';
318 if (lyxrc.isp_use_pers_dict) {
319 // Specify an alternate personal dictionary
321 string("-p").copy(tmp, 2);
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';
329 if (lyxrc.isp_use_input_encoding &&
330 params.inputenc != "default") {
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';
342 execvp(argv[0], const_cast<char * const *>(argv));
344 // free the memory used by string::copy in the
346 for (int i= 0; i < argc -1; ++i)
349 lyxerr << "LyX: Failed to start ispell!" << endl;
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)
362 FD_SET(pipeout[0], &infds);
363 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
364 // but it can't really hurt.
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),
373 SELECT_TYPE_ARG5 (&tv));
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);
380 // determine if the spell checker is really Aspell
381 if (strstr(buf, "Aspell"))
382 actual_spell_checker = ASC_ASPELL;
384 actual_spell_checker = ASC_ISPELL;
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
391 close(pipeout[0]); close(pipeout[1]);
392 close(pipein[0]); close(pipein[1]);
395 // Select returned error
396 lyxerr << "Select on ispell returned error, what now?" << endl;
401 // Send word to ispell and get reply
403 isp_result *ispell_check_word(char *word)
405 //Please rewrite to use string.
413 fgets(buf, 1024, in);
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;
419 result = new isp_result;
422 case '*': // Word found
423 result->flag = ISP_OK;
425 case '+': // Word found through affix removal
426 result->flag = ISP_ROOT;
428 case '-': // Word found through compound formation
429 result->flag = ISP_COMPOUNDWORD;
431 case '\n': // Number or when in terse mode: no problems
432 result->flag = ISP_IGNORE;
434 case '#': // Not found, no near misses and guesses
435 result->flag = ISP_UNKNOWN;
437 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
438 case '&': // Not found, but we have near misses
440 result->flag = ISP_MISSED;
442 // nb is leaked! where should it be freed? I have to
443 // admit I do not understand the intent of the code :(
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, ":");
454 for (i = 0; i < count; ++i) {
455 result->misses[i] = p;
456 p = strpbrk(p, ",\n");
462 default: // This shouldn't happend, but you know Murphy
463 result->flag = ISP_UNKNOWN;
467 if (result->flag!= ISP_IGNORE) {
468 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
475 void ispell_terminate()
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)
482 fputs("#\n", out); // Save personal dictionary
490 void ispell_terse_mode()
492 fputs("!\n", out); // Set terse mode (silently accept correct words)
497 void ispell_insert_word(char const *word)
499 fputc('*', out); // Insert word in personal dictionary
506 void ispell_accept_word(char const *word)
508 fputc('@', out); // Accept in this session
514 void ispell_store_replacement(char const *mis, string const & cor) {
515 if(actual_spell_checker == ASC_ASPELL) {
519 fputs(cor.c_str(), out);
525 void ShowSpellChecker(BufferView * bv)
530 // Exit if we don't have a document open
531 if (!bv->available())
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);
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);
548 if (fd_form_spell_check->form_spell_check->visible) {
549 fl_raise_form(fd_form_spell_check->form_spell_check);
551 fl_show_form(fd_form_spell_check->form_spell_check,
552 FL_PLACE_MOUSE, FL_FULLBORDER,
555 fl_deactivate_object(fd_form_spell_check->slider);
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);
575 if (obj == fd_form_spell_check->options){
576 SpellCheckerOptions();
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);
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);
604 ret = RunSpellChecker(bv);
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);
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);
628 // if RunSpellChecker returns false quit spellchecker
631 if (obj == fd_form_spell_check->done) break;
633 fl_hide_form(fd_form_spell_check->form_spell_check);
634 bv->endOfSpellCheck();
639 // Perform an ispell session
641 bool RunSpellChecker(BufferView * bv)
647 string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
648 bool rtl = tmp == "hebrew" || tmp == "arabic";
650 int oldval = 0; /* used for updating slider only when needed */
653 /* create ispell process */
654 create_ispell_pipe(bv->buffer()->params, tmp);
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."), "", "");
668 // Put ispell in terse mode to improve speed
671 unsigned int word_count = 0;
673 char * word = bv->nextWord(newval);
674 if (word == 0) break;
677 // Update slider if and only if value has changed
678 newvalue = int(100.0*newval);
679 if(newvalue!= oldval) {
681 fl_set_slider_value(fd_form_spell_check->slider, oldval);
684 if (word_count%1000 == 0) {
685 obj = fl_check_forms();
686 if (obj == fd_form_spell_check->stop) {
691 if (obj == fd_form_spell_check->done) {
698 result = ispell_check_word(word);
705 switch (result->flag) {
709 bv->selectLastWord();
712 reverse(tmp.begin(),tmp.end());
713 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
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) {
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());
724 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
730 if (obj == fd_form_spell_check->insert) {
731 ispell_insert_word(word);
734 if (obj == fd_form_spell_check->accept) {
735 ispell_accept_word(word);
738 if (obj == fd_form_spell_check->ignore) {
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));
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>
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));
756 clickline = fl_get_browser(fd_form_spell_check->browser);
758 /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
760 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
762 reverse(tmp.begin(),tmp.end());
763 fl_set_input(fd_form_spell_check->input, tmp.c_str());
765 fl_set_input(fd_form_spell_check->input,
766 fl_get_browser_line(fd_form_spell_check->browser,
769 if (obj == fd_form_spell_check->stop) {
776 if (obj == fd_form_spell_check->done) {
792 string word_msg(tostr(word_count));
793 if (word_count != 1) {
794 word_msg += _(" words checked.");
796 word_msg += _(" word checked.");
798 fl_show_message("", _("Spellchecking completed!"),
802 fl_show_message(_("The ispell-process has died for some reason.\n"
803 "Maybe it has been killed."), "", "");
810 void sigchldhandler(pid_t pid, int * status)
813 if (pid == isp_pid) {
815 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
816 to nonblocking so we can
819 sigchldchecker(pid, status);