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 # ifdef HAVE_STRINGS_H
38 // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
41 #include <sys/select.h>
48 #include "spellchecker.h"
52 #include "BufferView.h"
54 #include "lyx_gui_misc.h"
56 #include "support/lstrings.h"
62 // Spellchecker status
72 static bool RunSpellChecker(BufferView * bv);
74 static FILE * in, * out; /* streams to communicate with ispell */
75 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
78 // the true spell checker program being used
79 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
80 static ActualSpellChecker actual_spell_checker;
84 static FD_form_spell_options *fd_form_spell_options = 0;
85 FD_form_spell_check *fd_form_spell_check = 0;
87 //void sigchldhandler(int sig);
88 void sigchldhandler(pid_t pid, int *status);
90 //extern void sigchldchecker(int sig);
91 extern void sigchldchecker(pid_t pid, int *status);
101 misses = static_cast<char**>(0);
109 /***** Spellchecker options *****/
111 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
112 // now also string. Amazing, eh? (Asger)
114 // Set (sane) values in form to current spellchecker options
115 void SpellOptionsUpdate()
117 // Alternate language
118 if (lyxrc.isp_alt_lang.empty()) {
119 lyxrc.isp_use_alt_lang = false;
121 fl_set_input(fd_form_spell_options->altlang_input,
122 lyxrc.isp_alt_lang.c_str());
124 if (lyxrc.isp_use_alt_lang) {
125 fl_set_button(fd_form_spell_options->buflang, 0);
126 fl_set_button(fd_form_spell_options->altlang, 1);
128 fl_set_button(fd_form_spell_options->buflang, 1);
129 fl_set_button(fd_form_spell_options->altlang, 0);
132 // Personal dictionary
133 if (lyxrc.isp_pers_dict.empty()) {
134 lyxrc.isp_use_pers_dict = false;
136 fl_set_input(fd_form_spell_options->perdict_input,
137 lyxrc.isp_pers_dict.c_str());
139 fl_set_button(fd_form_spell_options->perdict,
140 lyxrc.isp_use_pers_dict ? 1:0);
143 if (lyxrc.isp_esc_chars.empty()) {
144 lyxrc.isp_use_esc_chars = false;
146 fl_set_input(fd_form_spell_options->esc_chars_input,
147 lyxrc.isp_esc_chars.c_str());
149 fl_set_button(fd_form_spell_options->esc_chars,
150 lyxrc.isp_use_esc_chars ? 1:0);
153 fl_set_button(fd_form_spell_options->compounds,
154 lyxrc.isp_accept_compound ? 1 : 0);
155 fl_set_button(fd_form_spell_options->inpenc,
156 lyxrc.isp_use_input_encoding ? 1 : 0);
159 // Update spellchecker options
160 void SpellOptionsApplyCB(FL_OBJECT *, long)
162 // Build new status from form data
163 lyxrc.isp_use_alt_lang =
164 fl_get_button(fd_form_spell_options->altlang);
165 lyxrc.isp_use_pers_dict =
166 fl_get_button(fd_form_spell_options->perdict);
167 lyxrc.isp_accept_compound =
168 fl_get_button(fd_form_spell_options->compounds);
169 lyxrc.isp_use_esc_chars =
170 fl_get_button(fd_form_spell_options->esc_chars);
171 lyxrc.isp_use_input_encoding =
172 fl_get_button(fd_form_spell_options->inpenc);
174 // Update strings with data from input fields
176 fl_get_input(fd_form_spell_options->altlang_input);
177 lyxrc.isp_pers_dict =
178 fl_get_input(fd_form_spell_options->perdict_input);
179 lyxrc.isp_esc_chars =
180 fl_get_input(fd_form_spell_options->esc_chars_input);
183 SpellOptionsUpdate();
187 void SpellOptionsCancelCB(FL_OBJECT *, long)
189 fl_hide_form(fd_form_spell_options->form_spell_options);
193 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
195 SpellOptionsApplyCB(ob, data);
196 SpellOptionsCancelCB(ob, data);
200 // Show spellchecker options form
201 void SpellCheckerOptions()
203 // Create form if nescessary
204 if (fd_form_spell_options == 0) {
205 fd_form_spell_options = create_form_form_spell_options();
206 // Make sure pressing the close box does not kill LyX. (RvdK)
207 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
208 CancelCloseBoxCB, 0);
211 // Update form to current options
212 SpellOptionsUpdate();
214 // Focus in alternate language field
215 fl_set_focus_object(fd_form_spell_options->form_spell_options,
216 fd_form_spell_options->altlang_input);
219 if (fd_form_spell_options->form_spell_options->visible) {
220 fl_raise_form(fd_form_spell_options->form_spell_options);
222 fl_show_form(fd_form_spell_options->form_spell_options,
223 FL_PLACE_MOUSE, FL_FULLBORDER,
224 _("Spellchecker Options"));
229 /***** Spellchecker *****/
231 // Could also use a clean up. (Asger Alstrup)
234 void create_ispell_pipe(BufferParams const & params, string const & lang)
236 static char o_buf[BUFSIZ]; // jc: it could be smaller
237 int pipein[2], pipeout[2];
243 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
244 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
248 if ((out = fdopen(pipein[1], "w")) == 0) {
249 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
254 if ((in = fdopen(pipeout[0], "r")) == 0) {
255 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
260 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
267 lyxerr << "LyX: Can't create child process for spellchecker!"
274 dup2(pipein[0], STDIN_FILENO);
275 dup2(pipeout[1], STDOUT_FILENO);
282 char * tmp = new char[lyxrc.isp_command.length() + 1];
283 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
284 tmp[lyxrc.isp_command.length()] = '\0';
287 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
290 if (lang != "default") {
292 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
294 tmp = new char[lang.length() + 1];
295 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
299 if (lyxrc.isp_accept_compound) {
300 // Consider run-together words as legal compounds
302 string("-C").copy(tmp, 2); tmp[2] = '\0';
305 // Report run-together words with
306 // missing blanks as errors
308 string("-B").copy(tmp, 2); tmp[2] = '\0';
311 if (lyxrc.isp_use_esc_chars) {
312 // Specify additional characters that
313 // can be part of a word
315 string("-w").copy(tmp, 2); tmp[2] = '\0';
317 // Put the escape chars in ""s
318 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
319 tmp = new char[tms.length() + 1];
320 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
323 if (lyxrc.isp_use_pers_dict) {
324 // Specify an alternate personal dictionary
326 string("-p").copy(tmp, 2);
329 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
330 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
331 tmp[lyxrc.isp_pers_dict.length()] = '\0';
334 if (lyxrc.isp_use_input_encoding &&
335 params.inputenc != "default") {
336 string enc = (params.inputenc == "auto")
337 ? params.language_info->encoding()->LatexName()
339 string::size_type n = enc.length();
341 string("-T").copy(tmp, 2); tmp[2] = '\0';
342 argv[argc++] = tmp; // Input encoding
343 tmp = new char[n + 1];
351 execvp(argv[0], const_cast<char * const *>(argv));
353 // free the memory used by string::copy in the
355 for (int i= 0; i < argc -1; ++i)
358 lyxerr << "LyX: Failed to start ispell!" << endl;
362 /* Parent process: Read ispells identification message */
363 // Hmm...what are we using this id msg for? Nothing? (Lgb)
364 // Actually I used it to tell if it's truly Ispell or if it's
365 // aspell -- (kevinatk@home.com)
371 FD_SET(pipeout[0], &infds);
372 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
373 // but it can't really hurt.
376 // Configure provides us with macros which are supposed to do
377 // the right typecast.
378 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
379 SELECT_TYPE_ARG234 (&infds),
382 SELECT_TYPE_ARG5 (&tv));
385 // Ok, do the reading. We don't have to FD_ISSET since
386 // there is only one fd in infds.
387 fgets(buf, 2048, in);
389 // determine if the spell checker is really Aspell
390 if (strstr(buf, "Aspell"))
391 actual_spell_checker = ASC_ASPELL;
393 actual_spell_checker = ASC_ISPELL;
395 } else if (retval == 0) {
396 // timeout. Give nice message to user.
397 lyxerr << "Ispell read timed out, what now?" << endl;
398 // This probably works but could need some thought
400 close(pipeout[0]); close(pipeout[1]);
401 close(pipein[0]); close(pipein[1]);
404 // Select returned error
405 lyxerr << "Select on ispell returned error, what now?" << endl;
410 // Send word to ispell and get reply
412 isp_result *ispell_check_word(char *word)
414 //Please rewrite to use string.
422 fgets(buf, 1024, in);
424 /* I think we have to check if ispell is still alive here because
425 the signal-handler could have disabled blocking on the fd */
426 if (isp_pid == -1) return 0;
428 result = new isp_result;
431 case '*': // Word found
432 result->flag = ISP_OK;
434 case '+': // Word found through affix removal
435 result->flag = ISP_ROOT;
437 case '-': // Word found through compound formation
438 result->flag = ISP_COMPOUNDWORD;
440 case '\n': // Number or when in terse mode: no problems
441 result->flag = ISP_IGNORE;
443 case '#': // Not found, no near misses and guesses
444 result->flag = ISP_UNKNOWN;
446 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
447 case '&': // Not found, but we have near misses
449 result->flag = ISP_MISSED;
451 // nb is leaked! where should it be freed? I have to
452 // admit I do not understand the intent of the code :(
454 char * nb = new char[result->str.length() + 1];
455 result->str.copy(nb, result->str.length());
456 nb[result->str.length()]= '\0';
457 p = strpbrk(nb+2, " ");
458 sscanf(p, "%d", &count); // Get near misses count
459 result->count = count;
460 if (count) result->misses = new char*[count];
461 p = strpbrk(nb, ":");
463 for (i = 0; i < count; ++i) {
464 result->misses[i] = p;
465 p = strpbrk(p, ",\n");
471 default: // This shouldn't happend, but you know Murphy
472 result->flag = ISP_UNKNOWN;
476 if (result->flag!= ISP_IGNORE) {
477 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
484 void ispell_terminate()
486 // Note: If you decide to optimize this out when it is not
487 // needed please note that when Aspell is used this command
488 // is also needed to save the replacement dictionary.
489 // -- Kevin Atkinson (kevinatk@home.com)
491 fputs("#\n", out); // Save personal dictionary
499 void ispell_terse_mode()
501 fputs("!\n", out); // Set terse mode (silently accept correct words)
506 void ispell_insert_word(char const *word)
508 fputc('*', out); // Insert word in personal dictionary
515 void ispell_accept_word(char const *word)
517 fputc('@', out); // Accept in this session
523 void ispell_store_replacement(char const *mis, string const & cor) {
524 if(actual_spell_checker == ASC_ASPELL) {
528 fputs(cor.c_str(), out);
534 void ShowSpellChecker(BufferView * bv)
539 // Exit if we don't have a document open
540 if (!bv->available())
543 if (fd_form_spell_check == 0) {
544 fd_form_spell_check = create_form_form_spell_check();
545 // Make sure pressing the close box does not kill LyX. (RvdK)
546 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
550 fl_set_slider_value(fd_form_spell_check->slider, 0);
551 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
552 fl_set_object_label(fd_form_spell_check->text, "");
553 fl_set_input(fd_form_spell_check->input, "");
554 fl_clear_browser(fd_form_spell_check->browser);
557 if (fd_form_spell_check->form_spell_check->visible) {
558 fl_raise_form(fd_form_spell_check->form_spell_check);
560 fl_show_form(fd_form_spell_check->form_spell_check,
561 FL_PLACE_MOUSE, FL_FULLBORDER,
564 fl_deactivate_object(fd_form_spell_check->slider);
566 // deactivate insert, accept, replace, and stop
567 fl_deactivate_object(fd_form_spell_check->insert);
568 fl_deactivate_object(fd_form_spell_check->accept);
569 fl_deactivate_object(fd_form_spell_check->ignore);
570 fl_deactivate_object(fd_form_spell_check->replace);
571 fl_deactivate_object(fd_form_spell_check->stop);
572 fl_deactivate_object(fd_form_spell_check->input);
573 fl_deactivate_object(fd_form_spell_check->browser);
574 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
575 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
576 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
577 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
578 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
579 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
580 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
584 if (obj == fd_form_spell_check->options){
585 SpellCheckerOptions();
587 if (obj == fd_form_spell_check->start){
588 // activate insert, accept, and stop
589 fl_activate_object(fd_form_spell_check->insert);
590 fl_activate_object(fd_form_spell_check->accept);
591 fl_activate_object(fd_form_spell_check->ignore);
592 fl_activate_object(fd_form_spell_check->stop);
593 fl_activate_object(fd_form_spell_check->input);
594 fl_activate_object(fd_form_spell_check->browser);
595 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
596 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
597 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
598 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
599 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
600 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
601 // activate replace only if the file is not read-only
602 if (!bv->buffer()->isReadonly()) {
603 fl_activate_object(fd_form_spell_check->replace);
604 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
607 // deactivate options and start
608 fl_deactivate_object(fd_form_spell_check->options);
609 fl_deactivate_object(fd_form_spell_check->start);
610 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
611 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
613 ret = RunSpellChecker(bv);
615 // deactivate insert, accept, replace, and stop
616 fl_deactivate_object(fd_form_spell_check->insert);
617 fl_deactivate_object(fd_form_spell_check->accept);
618 fl_deactivate_object(fd_form_spell_check->ignore);
619 fl_deactivate_object(fd_form_spell_check->replace);
620 fl_deactivate_object(fd_form_spell_check->stop);
621 fl_deactivate_object(fd_form_spell_check->input);
622 fl_deactivate_object(fd_form_spell_check->browser);
623 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
624 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
625 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
626 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
627 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
628 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
629 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
631 // activate options and start
632 fl_activate_object(fd_form_spell_check->options);
633 fl_activate_object(fd_form_spell_check->start);
634 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
635 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
637 // if RunSpellChecker returns false quit spellchecker
640 if (obj == fd_form_spell_check->done) break;
642 fl_hide_form(fd_form_spell_check->form_spell_check);
643 bv->endOfSpellCheck();
648 // Perform an ispell session
650 bool RunSpellChecker(BufferView * bv)
656 string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
657 bool rtl = tmp == "hebrew" || tmp == "arabic";
659 int oldval = 0; /* used for updating slider only when needed */
662 /* create ispell process */
663 create_ispell_pipe(bv->buffer()->params, tmp);
668 "The ispell-process has died for some reason. *One* possible reason\n"
669 "could be that you do not have a dictionary file\n"
670 "for the language of this document installed.\n"
671 "Check /usr/lib/ispell or set another\n"
672 "dictionary in the Spellchecker Options menu."), "", "");
677 // Put ispell in terse mode to improve speed
680 unsigned int word_count = 0;
682 char * word = bv->nextWord(newval);
683 if (word == 0) break;
686 // Update slider if and only if value has changed
687 newvalue = int(100.0*newval);
688 if(newvalue!= oldval) {
690 fl_set_slider_value(fd_form_spell_check->slider, oldval);
693 if (word_count%1000 == 0) {
694 obj = fl_check_forms();
695 if (obj == fd_form_spell_check->stop) {
700 if (obj == fd_form_spell_check->done) {
707 result = ispell_check_word(word);
714 switch (result->flag) {
718 bv->selectLastWord();
721 reverse(tmp.begin(),tmp.end());
722 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
724 fl_set_object_label(fd_form_spell_check->text, word);
725 fl_set_input(fd_form_spell_check->input, word);
726 fl_clear_browser(fd_form_spell_check->browser);
727 for (i = 0; i < result->count; ++i) {
729 string tmp = result->misses[i];
730 reverse(tmp.begin(),tmp.end());
731 fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
733 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
739 if (obj == fd_form_spell_check->insert) {
740 ispell_insert_word(word);
743 if (obj == fd_form_spell_check->accept) {
744 ispell_accept_word(word);
747 if (obj == fd_form_spell_check->ignore) {
750 if (obj == fd_form_spell_check->replace ||
751 obj == fd_form_spell_check->input) {
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 if (obj == fd_form_spell_check->browser) {
757 // implements double click in the browser window.
758 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
760 fl_get_browser(fd_form_spell_check->browser)) {
761 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
762 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
765 clickline = fl_get_browser(fd_form_spell_check->browser);
767 /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
769 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
771 reverse(tmp.begin(),tmp.end());
772 fl_set_input(fd_form_spell_check->input, tmp.c_str());
774 fl_set_input(fd_form_spell_check->input,
775 fl_get_browser_line(fd_form_spell_check->browser,
778 if (obj == fd_form_spell_check->stop) {
785 if (obj == fd_form_spell_check->done) {
801 string word_msg(tostr(word_count));
802 if (word_count != 1) {
803 word_msg += _(" words checked.");
805 word_msg += _(" word checked.");
807 fl_show_message("", _("Spellchecking completed!"),
811 fl_show_message(_("The ispell-process has died for some reason.\n"
812 "Maybe it has been killed."), "", "");
819 void sigchldhandler(pid_t pid, int * status)
822 if (pid == isp_pid) {
824 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
825 to nonblocking so we can
828 sigchldchecker(pid, status);