3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1998 The LyX Team
10 * ======================================================
16 #pragma implementation
25 #include <sys/types.h>
27 #include FORMS_H_LOCATION
29 #if TIME_WITH_SYS_TIME
30 # include <sys/time.h>
34 # include <sys/time.h>
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().
45 #include <sys/select.h>
52 #include "spellchecker.h"
56 #include "BufferView.h"
58 #include "lyx_gui_misc.h"
60 #include "support/lstrings.h"
66 // Spellchecker status
76 static bool RunSpellChecker(BufferView * bv);
78 static FILE * in, * out; /* streams to communicate with ispell */
79 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
82 // the true spell checker program being used
83 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
84 static ActualSpellChecker actual_spell_checker;
88 static FD_form_spell_options *fd_form_spell_options = 0;
89 FD_form_spell_check *fd_form_spell_check = 0;
91 //void sigchldhandler(int sig);
92 void sigchldhandler(pid_t pid, int *status);
94 //extern void sigchldchecker(int sig);
95 extern void sigchldchecker(pid_t pid, int *status);
105 misses = static_cast<char**>(0);
113 /***** Spellchecker options *****/
115 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
116 // now also string. Amazing, eh? (Asger)
118 // Set (sane) values in form to current spellchecker options
119 void SpellOptionsUpdate()
121 // Alternate language
122 if (lyxrc.isp_alt_lang.empty()) {
123 lyxrc.isp_use_alt_lang = false;
125 fl_set_input(fd_form_spell_options->altlang_input,
126 lyxrc.isp_alt_lang.c_str());
128 if (lyxrc.isp_use_alt_lang) {
129 fl_set_button(fd_form_spell_options->buflang, 0);
130 fl_set_button(fd_form_spell_options->altlang, 1);
132 fl_set_button(fd_form_spell_options->buflang, 1);
133 fl_set_button(fd_form_spell_options->altlang, 0);
136 // Personal dictionary
137 if (lyxrc.isp_pers_dict.empty()) {
138 lyxrc.isp_use_pers_dict = false;
140 fl_set_input(fd_form_spell_options->perdict_input,
141 lyxrc.isp_pers_dict.c_str());
143 fl_set_button(fd_form_spell_options->perdict,
144 lyxrc.isp_use_pers_dict ? 1:0);
147 if (lyxrc.isp_esc_chars.empty()) {
148 lyxrc.isp_use_esc_chars = false;
150 fl_set_input(fd_form_spell_options->esc_chars_input,
151 lyxrc.isp_esc_chars.c_str());
153 fl_set_button(fd_form_spell_options->esc_chars,
154 lyxrc.isp_use_esc_chars ? 1:0);
157 fl_set_button(fd_form_spell_options->compounds,
158 lyxrc.isp_accept_compound ? 1 : 0);
159 fl_set_button(fd_form_spell_options->inpenc,
160 lyxrc.isp_use_input_encoding ? 1 : 0);
163 // Update spellchecker options
164 void SpellOptionsApplyCB(FL_OBJECT *, long)
166 // Build new status from form data
167 lyxrc.isp_use_alt_lang =
168 fl_get_button(fd_form_spell_options->altlang);
169 lyxrc.isp_use_pers_dict =
170 fl_get_button(fd_form_spell_options->perdict);
171 lyxrc.isp_accept_compound =
172 fl_get_button(fd_form_spell_options->compounds);
173 lyxrc.isp_use_esc_chars =
174 fl_get_button(fd_form_spell_options->esc_chars);
175 lyxrc.isp_use_input_encoding =
176 fl_get_button(fd_form_spell_options->inpenc);
178 // Update strings with data from input fields
180 fl_get_input(fd_form_spell_options->altlang_input);
181 lyxrc.isp_pers_dict =
182 fl_get_input(fd_form_spell_options->perdict_input);
183 lyxrc.isp_esc_chars =
184 fl_get_input(fd_form_spell_options->esc_chars_input);
187 SpellOptionsUpdate();
191 void SpellOptionsCancelCB(FL_OBJECT *, long)
193 fl_hide_form(fd_form_spell_options->form_spell_options);
197 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
199 SpellOptionsApplyCB(ob, data);
200 SpellOptionsCancelCB(ob, data);
204 // Show spellchecker options form
205 void SpellCheckerOptions()
207 // Create form if nescessary
208 if (fd_form_spell_options == 0) {
209 fd_form_spell_options = create_form_form_spell_options();
210 // Make sure pressing the close box does not kill LyX. (RvdK)
211 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
212 CancelCloseBoxCB, 0);
215 // Update form to current options
216 SpellOptionsUpdate();
218 // Focus in alternate language field
219 fl_set_focus_object(fd_form_spell_options->form_spell_options,
220 fd_form_spell_options->altlang_input);
223 if (fd_form_spell_options->form_spell_options->visible) {
224 fl_raise_form(fd_form_spell_options->form_spell_options);
226 fl_show_form(fd_form_spell_options->form_spell_options,
227 FL_PLACE_MOUSE, FL_FULLBORDER,
228 _("Spellchecker Options"));
233 /***** Spellchecker *****/
235 // Could also use a clean up. (Asger Alstrup)
238 void create_ispell_pipe(BufferParams const & params, string const & lang)
240 static char o_buf[BUFSIZ]; // jc: it could be smaller
241 int pipein[2], pipeout[2];
247 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
248 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
252 if ((out = fdopen(pipein[1], "w")) == 0) {
253 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
258 if ((in = fdopen(pipeout[0], "r")) == 0) {
259 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
264 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
271 lyxerr << "LyX: Can't create child process for spellchecker!"
278 dup2(pipein[0], STDIN_FILENO);
279 dup2(pipeout[1], STDOUT_FILENO);
286 char * tmp = new char[lyxrc.isp_command.length() + 1];
287 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
288 tmp[lyxrc.isp_command.length()] = '\0';
291 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
294 if (lang != "default") {
296 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
298 tmp = new char[lang.length() + 1];
299 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
303 if (lyxrc.isp_accept_compound) {
304 // Consider run-together words as legal compounds
306 string("-C").copy(tmp, 2); tmp[2] = '\0';
309 // Report run-together words with
310 // missing blanks as errors
312 string("-B").copy(tmp, 2); tmp[2] = '\0';
315 if (lyxrc.isp_use_esc_chars) {
316 // Specify additional characters that
317 // can be part of a word
319 string("-w").copy(tmp, 2); tmp[2] = '\0';
321 // Put the escape chars in ""s
322 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
323 tmp = new char[tms.length() + 1];
324 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
327 if (lyxrc.isp_use_pers_dict) {
328 // Specify an alternate personal dictionary
330 string("-p").copy(tmp, 2);
333 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
334 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
335 tmp[lyxrc.isp_pers_dict.length()] = '\0';
338 if (lyxrc.isp_use_input_encoding &&
339 params.inputenc != "default") {
340 string enc = (params.inputenc == "auto")
341 ? params.language_info->encoding()->LatexName()
343 string::size_type n = enc.length();
345 string("-T").copy(tmp, 2); tmp[2] = '\0';
346 argv[argc++] = tmp; // Input encoding
347 tmp = new char[n + 1];
355 execvp(argv[0], const_cast<char * const *>(argv));
357 // free the memory used by string::copy in the
359 for (int i= 0; i < argc -1; ++i)
362 lyxerr << "LyX: Failed to start ispell!" << endl;
366 /* Parent process: Read ispells identification message */
367 // Hmm...what are we using this id msg for? Nothing? (Lgb)
368 // Actually I used it to tell if it's truly Ispell or if it's
369 // aspell -- (kevinatk@home.com)
375 FD_SET(pipeout[0], &infds);
376 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
377 // but it can't really hurt.
380 // Configure provides us with macros which are supposed to do
381 // the right typecast.
382 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
383 SELECT_TYPE_ARG234 (&infds),
386 SELECT_TYPE_ARG5 (&tv));
389 // Ok, do the reading. We don't have to FD_ISSET since
390 // there is only one fd in infds.
391 fgets(buf, 2048, in);
393 // determine if the spell checker is really Aspell
394 if (strstr(buf, "Aspell"))
395 actual_spell_checker = ASC_ASPELL;
397 actual_spell_checker = ASC_ISPELL;
399 } else if (retval == 0) {
400 // timeout. Give nice message to user.
401 lyxerr << "Ispell read timed out, what now?" << endl;
402 // This probably works but could need some thought
404 close(pipeout[0]); close(pipeout[1]);
405 close(pipein[0]); close(pipein[1]);
408 // Select returned error
409 lyxerr << "Select on ispell returned error, what now?" << endl;
414 // Send word to ispell and get reply
416 isp_result *ispell_check_word(char *word)
418 //Please rewrite to use string.
426 fgets(buf, 1024, in);
428 /* I think we have to check if ispell is still alive here because
429 the signal-handler could have disabled blocking on the fd */
430 if (isp_pid == -1) return 0;
432 result = new isp_result;
435 case '*': // Word found
436 result->flag = ISP_OK;
438 case '+': // Word found through affix removal
439 result->flag = ISP_ROOT;
441 case '-': // Word found through compound formation
442 result->flag = ISP_COMPOUNDWORD;
444 case '\n': // Number or when in terse mode: no problems
445 result->flag = ISP_IGNORE;
447 case '#': // Not found, no near misses and guesses
448 result->flag = ISP_UNKNOWN;
450 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
451 case '&': // Not found, but we have near misses
453 result->flag = ISP_MISSED;
455 // nb is leaked! where should it be freed? I have to
456 // admit I do not understand the intent of the code :(
458 char * nb = new char[result->str.length() + 1];
459 result->str.copy(nb, result->str.length());
460 nb[result->str.length()]= '\0';
461 p = strpbrk(nb+2, " ");
462 sscanf(p, "%d", &count); // Get near misses count
463 result->count = count;
464 if (count) result->misses = new char*[count];
465 p = strpbrk(nb, ":");
467 for (i = 0; i < count; ++i) {
468 result->misses[i] = p;
469 p = strpbrk(p, ",\n");
475 default: // This shouldn't happend, but you know Murphy
476 result->flag = ISP_UNKNOWN;
480 if (result->flag!= ISP_IGNORE) {
481 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
488 void ispell_terminate()
490 // Note: If you decide to optimize this out when it is not
491 // needed please note that when Aspell is used this command
492 // is also needed to save the replacement dictionary.
493 // -- Kevin Atkinson (kevinatk@home.com)
495 fputs("#\n", out); // Save personal dictionary
503 void ispell_terse_mode()
505 fputs("!\n", out); // Set terse mode (silently accept correct words)
510 void ispell_insert_word(char const *word)
512 fputc('*', out); // Insert word in personal dictionary
519 void ispell_accept_word(char const *word)
521 fputc('@', out); // Accept in this session
527 void ispell_store_replacement(char const *mis, string const & cor) {
528 if(actual_spell_checker == ASC_ASPELL) {
532 fputs(cor.c_str(), out);
538 void ShowSpellChecker(BufferView * bv)
543 // Exit if we don't have a document open
544 if (!bv->available())
547 if (fd_form_spell_check == 0) {
548 fd_form_spell_check = create_form_form_spell_check();
549 // Make sure pressing the close box does not kill LyX. (RvdK)
550 fl_set_form_atclose(fd_form_spell_check->form_spell_check,
551 CancelCloseBoxCB, 0);
555 fl_set_slider_value(fd_form_spell_check->slider, 0);
556 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
557 fl_set_object_label(fd_form_spell_check->text, "");
558 fl_set_input(fd_form_spell_check->input, "");
559 fl_clear_browser(fd_form_spell_check->browser);
562 if (fd_form_spell_check->form_spell_check->visible) {
563 fl_raise_form(fd_form_spell_check->form_spell_check);
565 fl_show_form(fd_form_spell_check->form_spell_check,
566 FL_PLACE_MOUSE, FL_FULLBORDER,
569 fl_deactivate_object(fd_form_spell_check->slider);
571 // deactivate insert, accept, replace, and stop
572 fl_deactivate_object(fd_form_spell_check->insert);
573 fl_deactivate_object(fd_form_spell_check->accept);
574 fl_deactivate_object(fd_form_spell_check->ignore);
575 fl_deactivate_object(fd_form_spell_check->replace);
576 fl_deactivate_object(fd_form_spell_check->stop);
577 fl_deactivate_object(fd_form_spell_check->input);
578 fl_deactivate_object(fd_form_spell_check->browser);
579 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
580 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
581 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
582 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
583 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
584 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
585 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
589 if (obj == fd_form_spell_check->options){
590 SpellCheckerOptions();
592 if (obj == fd_form_spell_check->start){
593 // activate insert, accept, and stop
594 fl_activate_object(fd_form_spell_check->insert);
595 fl_activate_object(fd_form_spell_check->accept);
596 fl_activate_object(fd_form_spell_check->ignore);
597 fl_activate_object(fd_form_spell_check->stop);
598 fl_activate_object(fd_form_spell_check->input);
599 fl_activate_object(fd_form_spell_check->browser);
600 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
601 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
602 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
603 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
604 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
605 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
606 // activate replace only if the file is not read-only
607 if (!bv->buffer()->isReadonly()) {
608 fl_activate_object(fd_form_spell_check->replace);
609 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
612 // deactivate options and start
613 fl_deactivate_object(fd_form_spell_check->options);
614 fl_deactivate_object(fd_form_spell_check->start);
615 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
616 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
618 ret = RunSpellChecker(bv);
620 // deactivate insert, accept, replace, and stop
621 fl_deactivate_object(fd_form_spell_check->insert);
622 fl_deactivate_object(fd_form_spell_check->accept);
623 fl_deactivate_object(fd_form_spell_check->ignore);
624 fl_deactivate_object(fd_form_spell_check->replace);
625 fl_deactivate_object(fd_form_spell_check->stop);
626 fl_deactivate_object(fd_form_spell_check->input);
627 fl_deactivate_object(fd_form_spell_check->browser);
628 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
629 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
630 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
631 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
632 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
633 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
634 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
636 // activate options and start
637 fl_activate_object(fd_form_spell_check->options);
638 fl_activate_object(fd_form_spell_check->start);
639 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
640 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
642 // if RunSpellChecker returns false quit spellchecker
645 if (obj == fd_form_spell_check->done) break;
647 fl_hide_form(fd_form_spell_check->form_spell_check);
648 bv->endOfSpellCheck();
653 // Perform an ispell session
655 bool RunSpellChecker(BufferView * bv)
661 string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
662 bool rtl = tmp == "hebrew" || tmp == "arabic";
664 int oldval = 0; /* used for updating slider only when needed */
667 /* create ispell process */
668 create_ispell_pipe(bv->buffer()->params, tmp);
673 "The ispell-process has died for some reason. *One* possible reason\n"
674 "could be that you do not have a dictionary file\n"
675 "for the language of this document installed.\n"
676 "Check /usr/lib/ispell or set another\n"
677 "dictionary in the Spellchecker Options menu."), "", "");
682 // Put ispell in terse mode to improve speed
685 unsigned int word_count = 0;
687 char * word = bv->nextWord(newval);
688 if (word == 0) break;
691 // Update slider if and only if value has changed
692 newvalue = int(100.0*newval);
693 if(newvalue!= oldval) {
695 fl_set_slider_value(fd_form_spell_check->slider, oldval);
698 if (word_count%1000 == 0) {
699 obj = fl_check_forms();
700 if (obj == fd_form_spell_check->stop) {
705 if (obj == fd_form_spell_check->done) {
712 result = ispell_check_word(word);
719 switch (result->flag) {
723 bv->selectLastWord();
726 reverse(tmp.begin(),tmp.end());
727 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
729 fl_set_object_label(fd_form_spell_check->text, word);
730 fl_set_input(fd_form_spell_check->input, word);
731 fl_clear_browser(fd_form_spell_check->browser);
732 for (i = 0; i < result->count; ++i) {
734 string tmp = result->misses[i];
735 reverse(tmp.begin(),tmp.end());
736 fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
738 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
744 if (obj == fd_form_spell_check->insert) {
745 ispell_insert_word(word);
748 if (obj == fd_form_spell_check->accept) {
749 ispell_accept_word(word);
752 if (obj == fd_form_spell_check->ignore) {
755 if (obj == fd_form_spell_check->replace ||
756 obj == fd_form_spell_check->input) {
757 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
758 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
761 if (obj == fd_form_spell_check->browser) {
762 // implements double click in the browser window.
763 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
765 fl_get_browser(fd_form_spell_check->browser)) {
766 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
767 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
770 clickline = fl_get_browser(fd_form_spell_check->browser);
772 /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
774 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
776 reverse(tmp.begin(),tmp.end());
777 fl_set_input(fd_form_spell_check->input, tmp.c_str());
779 fl_set_input(fd_form_spell_check->input,
780 fl_get_browser_line(fd_form_spell_check->browser,
783 if (obj == fd_form_spell_check->stop) {
790 if (obj == fd_form_spell_check->done) {
806 string word_msg(tostr(word_count));
807 if (word_count != 1) {
808 word_msg += _(" words checked.");
810 word_msg += _(" word checked.");
812 fl_show_message("", _("Spellchecking completed!"),
816 fl_show_message(_("The ispell-process has died for some reason.\n"
817 "Maybe it has been killed."), "", "");
824 void sigchldhandler(pid_t pid, int * status)
827 if (pid == isp_pid) {
829 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
830 to nonblocking so we can
833 sigchldchecker(pid, status);