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>
42 #include "spellchecker.h"
46 #include "BufferView.h"
48 #include "lyx_gui_misc.h"
50 #include "support/lstrings.h"
54 // Spellchecker status
64 static bool RunSpellChecker(BufferView * bv);
66 static FILE *in, *out; /* streams to communicate with ispell */
67 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
70 // the true spell checker program being used
71 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
72 static ActualSpellChecker actual_spell_checker;
76 static FD_form_spell_options *fd_form_spell_options = 0;
77 FD_form_spell_check *fd_form_spell_check = 0;
79 //void sigchldhandler(int sig);
80 void sigchldhandler(pid_t pid, int *status);
82 //extern void sigchldchecker(int sig);
83 extern void sigchldchecker(pid_t pid, int *status);
93 misses = static_cast<char**>(0);
101 /***** Spellchecker options *****/
103 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
104 // now also string. Amazing, eh? (Asger)
106 // Set (sane) values in form to current spellchecker options
107 void SpellOptionsUpdate()
109 // Alternate language
110 if (lyxrc->isp_alt_lang.empty()) {
111 lyxrc->isp_use_alt_lang = false;
113 fl_set_input(fd_form_spell_options->altlang_input,
114 lyxrc->isp_alt_lang.c_str());
116 if (lyxrc->isp_use_alt_lang) {
117 fl_set_button(fd_form_spell_options->buflang, 0);
118 fl_set_button(fd_form_spell_options->altlang, 1);
120 fl_set_button(fd_form_spell_options->buflang, 1);
121 fl_set_button(fd_form_spell_options->altlang, 0);
124 // Personal dictionary
125 if (lyxrc->isp_pers_dict.empty()) {
126 lyxrc->isp_use_pers_dict = false;
128 fl_set_input(fd_form_spell_options->perdict_input,
129 lyxrc->isp_pers_dict.c_str());
131 fl_set_button(fd_form_spell_options->perdict,
132 lyxrc->isp_use_pers_dict ? 1:0);
135 if (lyxrc->isp_esc_chars.empty()) {
136 lyxrc->isp_use_esc_chars = false;
138 fl_set_input(fd_form_spell_options->esc_chars_input,
139 lyxrc->isp_esc_chars.c_str());
141 fl_set_button(fd_form_spell_options->esc_chars,
142 lyxrc->isp_use_esc_chars ? 1:0);
145 fl_set_button(fd_form_spell_options->compounds,
146 lyxrc->isp_accept_compound ? 1 : 0);
147 fl_set_button(fd_form_spell_options->inpenc,
148 lyxrc->isp_use_input_encoding ? 1 : 0);
151 // Update spellchecker options
152 void SpellOptionsApplyCB(FL_OBJECT *, long)
154 // Build new status from form data
155 lyxrc->isp_use_alt_lang =
156 fl_get_button(fd_form_spell_options->altlang);
157 lyxrc->isp_use_pers_dict =
158 fl_get_button(fd_form_spell_options->perdict);
159 lyxrc->isp_accept_compound =
160 fl_get_button(fd_form_spell_options->compounds);
161 lyxrc->isp_use_esc_chars =
162 fl_get_button(fd_form_spell_options->esc_chars);
163 lyxrc->isp_use_input_encoding =
164 fl_get_button(fd_form_spell_options->inpenc);
166 // Update strings with data from input fields
167 lyxrc->isp_alt_lang =
168 fl_get_input(fd_form_spell_options->altlang_input);
169 lyxrc->isp_pers_dict =
170 fl_get_input(fd_form_spell_options->perdict_input);
171 lyxrc->isp_esc_chars =
172 fl_get_input(fd_form_spell_options->esc_chars_input);
175 SpellOptionsUpdate();
179 void SpellOptionsCancelCB(FL_OBJECT *, long)
181 fl_hide_form(fd_form_spell_options->form_spell_options);
185 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
187 SpellOptionsApplyCB(ob, data);
188 SpellOptionsCancelCB(ob, data);
192 // Show spellchecker options form
193 void SpellCheckerOptions()
195 // Create form if nescessary
196 if (fd_form_spell_options == 0) {
197 fd_form_spell_options = create_form_form_spell_options();
198 // Make sure pressing the close box does not kill LyX. (RvdK)
199 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
200 CancelCloseBoxCB, 0);
203 // Update form to current options
204 SpellOptionsUpdate();
206 // Focus in alternate language field
207 fl_set_focus_object(fd_form_spell_options->form_spell_options,
208 fd_form_spell_options->altlang_input);
211 if (fd_form_spell_options->form_spell_options->visible) {
212 fl_raise_form(fd_form_spell_options->form_spell_options);
214 fl_show_form(fd_form_spell_options->form_spell_options,
215 FL_PLACE_MOUSE, FL_FULLBORDER,
216 _("Spellchecker Options"));
221 /***** Spellchecker *****/
223 // Could also use a clean up. (Asger Alstrup)
226 void create_ispell_pipe(BufferParams const & params, string const & lang)
228 static char o_buf[BUFSIZ]; // jc: it could be smaller
229 int pipein[2], pipeout[2];
235 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
236 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
240 if ((out = fdopen(pipein[1], "w")) == 0) {
241 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
246 if ((in = fdopen(pipeout[0], "r")) == 0) {
247 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
252 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
259 lyxerr << "LyX: Can't create child process for spellchecker!"
266 dup2(pipein[0], STDIN_FILENO);
267 dup2(pipeout[1], STDOUT_FILENO);
274 char * tmp = new char[lyxrc->isp_command.length() + 1];
275 lyxrc->isp_command.copy(tmp, lyxrc->isp_command.length());
276 tmp[lyxrc->isp_command.length()] = '\0';
279 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
282 if (lang != "default") {
284 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
286 tmp = new char[lang.length() + 1];
287 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
291 if (lyxrc->isp_accept_compound) {
292 // Consider run-together words as legal compounds
294 string("-C").copy(tmp, 2); tmp[2] = '\0';
297 // Report run-together words with
298 // missing blanks as errors
300 string("-B").copy(tmp, 2); tmp[2] = '\0';
303 if (lyxrc->isp_use_esc_chars) {
304 // Specify additional characters that
305 // can be part of a word
307 string("-w").copy(tmp, 2); tmp[2] = '\0';
309 // Put the escape chars in ""s
310 string tms = "\"" + lyxrc->isp_esc_chars + "\"";
311 tmp = new char[tms.length() + 1];
312 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
315 if (lyxrc->isp_use_pers_dict) {
316 // Specify an alternate personal dictionary
318 string("-p").copy(tmp, 2);
321 tmp = new char[lyxrc->isp_pers_dict.length() + 1];
322 lyxrc->isp_pers_dict.copy(tmp, lyxrc->isp_pers_dict.length());
323 tmp[lyxrc->isp_pers_dict.length()] = '\0';
326 if (lyxrc->isp_use_input_encoding &&
327 params.inputenc != "default") {
329 string("-T").copy(tmp, 2); tmp[2] = '\0';
330 argv[argc++] = tmp; // Input encoding
331 tmp = new char[params.inputenc.length() + 1];
332 params.inputenc.copy(tmp, params.inputenc.length());
333 tmp[params.inputenc.length()] = '\0';
339 execvp("ispell", const_cast<char * const *>(argv));
341 // free the memory used by string::copy in the
343 for (int i= 0; i < argc -1; ++i)
346 lyxerr << "LyX: Failed to start ispell!" << endl;
350 /* Parent process: Read ispells identification message */
351 // Hmm...what are we using this id msg for? Nothing? (Lgb)
352 // Actually I used it to tell if it's truly Ispell or if it's
353 // aspell -- (kevinatk@home.com)
359 FD_SET(pipeout[0], &infds);
360 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
361 // but it can't really hurt.
364 // Configure provides us with macros which are supposed to do
365 // the right typecast.
366 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
367 SELECT_TYPE_ARG234 (&infds),
370 SELECT_TYPE_ARG5 (&tv));
373 // Ok, do the reading. We don't have to FD_ISSET since
374 // there is only one fd in infds.
375 fgets(buf, 2048, in);
377 // determine if the spell checker is really Aspell
378 if (strstr(buf, "Aspell"))
379 actual_spell_checker = ASC_ASPELL;
381 actual_spell_checker = ASC_ISPELL;
383 } else if (retval == 0) {
384 // timeout. Give nice message to user.
385 lyxerr << "Ispell read timed out, what now?" << endl;
386 // This probably works but could need some thought
388 close(pipeout[0]); close(pipeout[1]);
389 close(pipein[0]); close(pipein[1]);
392 // Select returned error
393 lyxerr << "Select on ispell returned error, what now?" << endl;
398 // Send word to ispell and get reply
400 isp_result *ispell_check_word(char *word)
402 //Please rewrite to use string.
410 fgets(buf, 1024, in);
412 /* I think we have to check if ispell is still alive here because
413 the signal-handler could have disabled blocking on the fd */
414 if (isp_pid == -1) return 0;
416 result = new isp_result;
419 case '*': // Word found
420 result->flag = ISP_OK;
422 case '+': // Word found through affix removal
423 result->flag = ISP_ROOT;
425 case '-': // Word found through compound formation
426 result->flag = ISP_COMPOUNDWORD;
428 case '\n': // Number or when in terse mode: no problems
429 result->flag = ISP_IGNORE;
431 case '#': // Not found, no near misses and guesses
432 result->flag = ISP_UNKNOWN;
434 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
435 case '&': // Not found, but we have near misses
437 result->flag = ISP_MISSED;
439 // nb is leaked! where should it be freed? I have to
440 // admit I do not understand the intent of the code :(
442 char * nb = new char[result->str.length() + 1];
443 result->str.copy(nb, result->str.length());
444 nb[result->str.length()]= '\0';
445 p = strpbrk(nb+2, " ");
446 sscanf(p, "%d", &count); // Get near misses count
447 result->count = count;
448 if (count) result->misses = new char*[count];
449 p = strpbrk(nb, ":");
451 for (i = 0; i < count; ++i) {
452 result->misses[i] = p;
453 p = strpbrk(p, ",\n");
459 default: // This shouldn't happend, but you know Murphy
460 result->flag = ISP_UNKNOWN;
464 if (result->flag!= ISP_IGNORE) {
465 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
472 inline void ispell_terminate()
474 // Note: If you decide to optimize this out when it is not
475 // needed please note that when Aspell is used this command
476 // is also needed to save the replacement dictionary.
477 // -- Kevin Atkinson (kevinatk@home.com)
479 fputs("#\n", out); // Save personal dictionary
487 inline void ispell_terse_mode()
489 fputs("!\n", out); // Set terse mode (silently accept correct words)
494 inline void ispell_insert_word(char const *word)
496 fputc('*', out); // Insert word in personal dictionary
503 inline void ispell_accept_word(char const *word)
505 fputc('@', out); // Accept in this session
511 inline void ispell_store_replacement(char const *mis, string const & cor) {
512 if(actual_spell_checker == ASC_ASPELL) {
516 fputs(cor.c_str(), out);
522 void ShowSpellChecker(BufferView * bv)
527 // Exit if we don't have a document open
528 if (!bv->available())
531 if (fd_form_spell_check == 0) {
532 fd_form_spell_check = create_form_form_spell_check();
533 // Make sure pressing the close box does not kill LyX. (RvdK)
534 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
538 fl_set_slider_value(fd_form_spell_check->slider, 0);
539 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
540 fl_set_object_label(fd_form_spell_check->text, "");
541 fl_set_input(fd_form_spell_check->input, "");
542 fl_clear_browser(fd_form_spell_check->browser);
545 if (fd_form_spell_check->form_spell_check->visible) {
546 fl_raise_form(fd_form_spell_check->form_spell_check);
548 fl_show_form(fd_form_spell_check->form_spell_check,
549 FL_PLACE_MOUSE, FL_FULLBORDER,
552 fl_deactivate_object(fd_form_spell_check->slider);
554 // deactivate insert, accept, replace, and stop
555 fl_deactivate_object(fd_form_spell_check->insert);
556 fl_deactivate_object(fd_form_spell_check->accept);
557 fl_deactivate_object(fd_form_spell_check->ignore);
558 fl_deactivate_object(fd_form_spell_check->replace);
559 fl_deactivate_object(fd_form_spell_check->stop);
560 fl_deactivate_object(fd_form_spell_check->input);
561 fl_deactivate_object(fd_form_spell_check->browser);
562 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
563 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
564 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
565 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
566 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
567 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
568 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
572 if (obj == fd_form_spell_check->options){
573 SpellCheckerOptions();
575 if (obj == fd_form_spell_check->start){
576 // activate insert, accept, and stop
577 fl_activate_object(fd_form_spell_check->insert);
578 fl_activate_object(fd_form_spell_check->accept);
579 fl_activate_object(fd_form_spell_check->ignore);
580 fl_activate_object(fd_form_spell_check->stop);
581 fl_activate_object(fd_form_spell_check->input);
582 fl_activate_object(fd_form_spell_check->browser);
583 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
584 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
585 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
586 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
587 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
588 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
589 // activate replace only if the file is not read-only
590 if (!bv->buffer()->isReadonly()) {
591 fl_activate_object(fd_form_spell_check->replace);
592 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
595 // deactivate options and start
596 fl_deactivate_object(fd_form_spell_check->options);
597 fl_deactivate_object(fd_form_spell_check->start);
598 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
599 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
601 ret = RunSpellChecker(bv);
603 // deactivate insert, accept, replace, and stop
604 fl_deactivate_object(fd_form_spell_check->insert);
605 fl_deactivate_object(fd_form_spell_check->accept);
606 fl_deactivate_object(fd_form_spell_check->ignore);
607 fl_deactivate_object(fd_form_spell_check->replace);
608 fl_deactivate_object(fd_form_spell_check->stop);
609 fl_deactivate_object(fd_form_spell_check->input);
610 fl_deactivate_object(fd_form_spell_check->browser);
611 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
612 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
613 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
614 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
615 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
616 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
617 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
619 // activate options and start
620 fl_activate_object(fd_form_spell_check->options);
621 fl_activate_object(fd_form_spell_check->start);
622 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
623 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
625 // if RunSpellChecker returns false quit spellchecker
628 if (obj == fd_form_spell_check->done) break;
630 fl_hide_form(fd_form_spell_check->form_spell_check);
631 bv->endOfSpellCheck();
636 // Perform an ispell session
638 bool RunSpellChecker(BufferView * bv)
644 string tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang : bv->buffer()->GetLanguage();
646 int oldval = 0; /* used for updating slider only when needed */
649 /* create ispell process */
650 create_ispell_pipe(bv->buffer()->params, tmp);
655 "The ispell-process has died for some reason. *One* possible reason\n"
656 "could be that you do not have a dictionary file\n"
657 "for the language of this document installed.\n"
658 "Check /usr/lib/ispell or set another\n"
659 "dictionary in the Spellchecker Options menu."), "", "");
664 // Put ispell in terse mode to improve speed
667 unsigned int word_count = 0;
669 char * word = bv->nextWord(newval);
670 if (word == 0) break;
673 // Update slider if and only if value has changed
674 newvalue = int(100.0*newval);
675 if(newvalue!= oldval) {
677 fl_set_slider_value(fd_form_spell_check->slider, oldval);
680 result = ispell_check_word(word);
686 obj = fl_check_forms();
687 if (obj == fd_form_spell_check->stop) {
693 if (obj == fd_form_spell_check->done) {
700 switch (result->flag) {
704 bv->selectLastWord();
705 fl_set_object_label(fd_form_spell_check->text, word);
706 fl_set_input(fd_form_spell_check->input, word);
707 fl_clear_browser(fd_form_spell_check->browser);
708 for (i = 0; i < result->count; ++i) {
709 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
715 if (obj == fd_form_spell_check->insert) {
716 ispell_insert_word(word);
719 if (obj == fd_form_spell_check->accept) {
720 ispell_accept_word(word);
723 if (obj == fd_form_spell_check->ignore) {
726 if (obj == fd_form_spell_check->replace ||
727 obj == fd_form_spell_check->input) {
728 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
729 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
732 if (obj == fd_form_spell_check->browser) {
733 // implements double click in the browser window.
734 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
736 fl_get_browser(fd_form_spell_check->browser)) {
737 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
738 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
741 clickline = fl_get_browser(fd_form_spell_check->browser);
742 fl_set_input(fd_form_spell_check->input,
743 fl_get_browser_line(fd_form_spell_check->browser,
744 fl_get_browser(fd_form_spell_check->browser)));
747 if (obj == fd_form_spell_check->stop) {
754 if (obj == fd_form_spell_check->done) {
771 word_msg += tostr(word_count);
772 if (word_count != 1) {
773 word_msg += _(" words checked.");
775 word_msg += _(" word checked.");
777 fl_show_message("", _("Spellchecking completed!"),
781 fl_show_message(_("The ispell-process has died for some reason.\n"
782 "Maybe it has been killed."), "", "");
789 void sigchldhandler(pid_t pid, int * status)
792 if (pid == isp_pid) {
794 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
795 to nonblocking so we can
798 sigchldchecker(pid, status);