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"
55 // Spellchecker status
65 static bool RunSpellChecker(BufferView * bv);
67 static FILE * in, * out; /* streams to communicate with ispell */
68 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
71 // the true spell checker program being used
72 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
73 static ActualSpellChecker actual_spell_checker;
77 static FD_form_spell_options *fd_form_spell_options = 0;
78 FD_form_spell_check *fd_form_spell_check = 0;
80 //void sigchldhandler(int sig);
81 void sigchldhandler(pid_t pid, int *status);
83 //extern void sigchldchecker(int sig);
84 extern void sigchldchecker(pid_t pid, int *status);
94 misses = static_cast<char**>(0);
102 /***** Spellchecker options *****/
104 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
105 // now also string. Amazing, eh? (Asger)
107 // Set (sane) values in form to current spellchecker options
108 void SpellOptionsUpdate()
110 // Alternate language
111 if (lyxrc.isp_alt_lang.empty()) {
112 lyxrc.isp_use_alt_lang = false;
114 fl_set_input(fd_form_spell_options->altlang_input,
115 lyxrc.isp_alt_lang.c_str());
117 if (lyxrc.isp_use_alt_lang) {
118 fl_set_button(fd_form_spell_options->buflang, 0);
119 fl_set_button(fd_form_spell_options->altlang, 1);
121 fl_set_button(fd_form_spell_options->buflang, 1);
122 fl_set_button(fd_form_spell_options->altlang, 0);
125 // Personal dictionary
126 if (lyxrc.isp_pers_dict.empty()) {
127 lyxrc.isp_use_pers_dict = false;
129 fl_set_input(fd_form_spell_options->perdict_input,
130 lyxrc.isp_pers_dict.c_str());
132 fl_set_button(fd_form_spell_options->perdict,
133 lyxrc.isp_use_pers_dict ? 1:0);
136 if (lyxrc.isp_esc_chars.empty()) {
137 lyxrc.isp_use_esc_chars = false;
139 fl_set_input(fd_form_spell_options->esc_chars_input,
140 lyxrc.isp_esc_chars.c_str());
142 fl_set_button(fd_form_spell_options->esc_chars,
143 lyxrc.isp_use_esc_chars ? 1:0);
146 fl_set_button(fd_form_spell_options->compounds,
147 lyxrc.isp_accept_compound ? 1 : 0);
148 fl_set_button(fd_form_spell_options->inpenc,
149 lyxrc.isp_use_input_encoding ? 1 : 0);
152 // Update spellchecker options
153 void SpellOptionsApplyCB(FL_OBJECT *, long)
155 // Build new status from form data
156 lyxrc.isp_use_alt_lang =
157 fl_get_button(fd_form_spell_options->altlang);
158 lyxrc.isp_use_pers_dict =
159 fl_get_button(fd_form_spell_options->perdict);
160 lyxrc.isp_accept_compound =
161 fl_get_button(fd_form_spell_options->compounds);
162 lyxrc.isp_use_esc_chars =
163 fl_get_button(fd_form_spell_options->esc_chars);
164 lyxrc.isp_use_input_encoding =
165 fl_get_button(fd_form_spell_options->inpenc);
167 // Update strings with data from input fields
169 fl_get_input(fd_form_spell_options->altlang_input);
170 lyxrc.isp_pers_dict =
171 fl_get_input(fd_form_spell_options->perdict_input);
172 lyxrc.isp_esc_chars =
173 fl_get_input(fd_form_spell_options->esc_chars_input);
176 SpellOptionsUpdate();
180 void SpellOptionsCancelCB(FL_OBJECT *, long)
182 fl_hide_form(fd_form_spell_options->form_spell_options);
186 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
188 SpellOptionsApplyCB(ob, data);
189 SpellOptionsCancelCB(ob, data);
193 // Show spellchecker options form
194 void SpellCheckerOptions()
196 // Create form if nescessary
197 if (fd_form_spell_options == 0) {
198 fd_form_spell_options = create_form_form_spell_options();
199 // Make sure pressing the close box does not kill LyX. (RvdK)
200 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
201 CancelCloseBoxCB, 0);
204 // Update form to current options
205 SpellOptionsUpdate();
207 // Focus in alternate language field
208 fl_set_focus_object(fd_form_spell_options->form_spell_options,
209 fd_form_spell_options->altlang_input);
212 if (fd_form_spell_options->form_spell_options->visible) {
213 fl_raise_form(fd_form_spell_options->form_spell_options);
215 fl_show_form(fd_form_spell_options->form_spell_options,
216 FL_PLACE_MOUSE, FL_FULLBORDER,
217 _("Spellchecker Options"));
222 /***** Spellchecker *****/
224 // Could also use a clean up. (Asger Alstrup)
227 void create_ispell_pipe(BufferParams const & params, string const & lang)
229 static char o_buf[BUFSIZ]; // jc: it could be smaller
230 int pipein[2], pipeout[2];
236 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
237 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
241 if ((out = fdopen(pipein[1], "w")) == 0) {
242 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
247 if ((in = fdopen(pipeout[0], "r")) == 0) {
248 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
253 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
260 lyxerr << "LyX: Can't create child process for spellchecker!"
267 dup2(pipein[0], STDIN_FILENO);
268 dup2(pipeout[1], STDOUT_FILENO);
275 char * tmp = new char[lyxrc.isp_command.length() + 1];
276 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
277 tmp[lyxrc.isp_command.length()] = '\0';
280 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
283 if (lang != "default") {
285 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
287 tmp = new char[lang.length() + 1];
288 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
292 if (lyxrc.isp_accept_compound) {
293 // Consider run-together words as legal compounds
295 string("-C").copy(tmp, 2); tmp[2] = '\0';
298 // Report run-together words with
299 // missing blanks as errors
301 string("-B").copy(tmp, 2); tmp[2] = '\0';
304 if (lyxrc.isp_use_esc_chars) {
305 // Specify additional characters that
306 // can be part of a word
308 string("-w").copy(tmp, 2); tmp[2] = '\0';
310 // Put the escape chars in ""s
311 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
312 tmp = new char[tms.length() + 1];
313 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
316 if (lyxrc.isp_use_pers_dict) {
317 // Specify an alternate personal dictionary
319 string("-p").copy(tmp, 2);
322 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
323 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
324 tmp[lyxrc.isp_pers_dict.length()] = '\0';
327 if (lyxrc.isp_use_input_encoding &&
328 params.inputenc != "default") {
330 string("-T").copy(tmp, 2); tmp[2] = '\0';
331 argv[argc++] = tmp; // Input encoding
332 tmp = new char[params.inputenc.length() + 1];
333 params.inputenc.copy(tmp, params.inputenc.length());
334 tmp[params.inputenc.length()] = '\0';
340 execvp(argv[0], const_cast<char * const *>(argv));
342 // free the memory used by string::copy in the
344 for (int i= 0; i < argc -1; ++i)
347 lyxerr << "LyX: Failed to start ispell!" << endl;
351 /* Parent process: Read ispells identification message */
352 // Hmm...what are we using this id msg for? Nothing? (Lgb)
353 // Actually I used it to tell if it's truly Ispell or if it's
354 // aspell -- (kevinatk@home.com)
360 FD_SET(pipeout[0], &infds);
361 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
362 // but it can't really hurt.
365 // Configure provides us with macros which are supposed to do
366 // the right typecast.
367 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
368 SELECT_TYPE_ARG234 (&infds),
371 SELECT_TYPE_ARG5 (&tv));
374 // Ok, do the reading. We don't have to FD_ISSET since
375 // there is only one fd in infds.
376 fgets(buf, 2048, in);
378 // determine if the spell checker is really Aspell
379 if (strstr(buf, "Aspell"))
380 actual_spell_checker = ASC_ASPELL;
382 actual_spell_checker = ASC_ISPELL;
384 } else if (retval == 0) {
385 // timeout. Give nice message to user.
386 lyxerr << "Ispell read timed out, what now?" << endl;
387 // This probably works but could need some thought
389 close(pipeout[0]); close(pipeout[1]);
390 close(pipein[0]); close(pipein[1]);
393 // Select returned error
394 lyxerr << "Select on ispell returned error, what now?" << endl;
399 // Send word to ispell and get reply
401 isp_result *ispell_check_word(char *word)
403 //Please rewrite to use string.
411 fgets(buf, 1024, in);
413 /* I think we have to check if ispell is still alive here because
414 the signal-handler could have disabled blocking on the fd */
415 if (isp_pid == -1) return 0;
417 result = new isp_result;
420 case '*': // Word found
421 result->flag = ISP_OK;
423 case '+': // Word found through affix removal
424 result->flag = ISP_ROOT;
426 case '-': // Word found through compound formation
427 result->flag = ISP_COMPOUNDWORD;
429 case '\n': // Number or when in terse mode: no problems
430 result->flag = ISP_IGNORE;
432 case '#': // Not found, no near misses and guesses
433 result->flag = ISP_UNKNOWN;
435 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
436 case '&': // Not found, but we have near misses
438 result->flag = ISP_MISSED;
440 // nb is leaked! where should it be freed? I have to
441 // admit I do not understand the intent of the code :(
443 char * nb = new char[result->str.length() + 1];
444 result->str.copy(nb, result->str.length());
445 nb[result->str.length()]= '\0';
446 p = strpbrk(nb+2, " ");
447 sscanf(p, "%d", &count); // Get near misses count
448 result->count = count;
449 if (count) result->misses = new char*[count];
450 p = strpbrk(nb, ":");
452 for (i = 0; i < count; ++i) {
453 result->misses[i] = p;
454 p = strpbrk(p, ",\n");
460 default: // This shouldn't happend, but you know Murphy
461 result->flag = ISP_UNKNOWN;
465 if (result->flag!= ISP_IGNORE) {
466 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
473 void ispell_terminate()
475 // Note: If you decide to optimize this out when it is not
476 // needed please note that when Aspell is used this command
477 // is also needed to save the replacement dictionary.
478 // -- Kevin Atkinson (kevinatk@home.com)
480 fputs("#\n", out); // Save personal dictionary
488 void ispell_terse_mode()
490 fputs("!\n", out); // Set terse mode (silently accept correct words)
495 void ispell_insert_word(char const *word)
497 fputc('*', out); // Insert word in personal dictionary
504 void ispell_accept_word(char const *word)
506 fputc('@', out); // Accept in this session
512 void ispell_store_replacement(char const *mis, string const & cor) {
513 if(actual_spell_checker == ASC_ASPELL) {
517 fputs(cor.c_str(), out);
523 void ShowSpellChecker(BufferView * bv)
528 // Exit if we don't have a document open
529 if (!bv->available())
532 if (fd_form_spell_check == 0) {
533 fd_form_spell_check = create_form_form_spell_check();
534 // Make sure pressing the close box does not kill LyX. (RvdK)
535 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
539 fl_set_slider_value(fd_form_spell_check->slider, 0);
540 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
541 fl_set_object_label(fd_form_spell_check->text, "");
542 fl_set_input(fd_form_spell_check->input, "");
543 fl_clear_browser(fd_form_spell_check->browser);
546 if (fd_form_spell_check->form_spell_check->visible) {
547 fl_raise_form(fd_form_spell_check->form_spell_check);
549 fl_show_form(fd_form_spell_check->form_spell_check,
550 FL_PLACE_MOUSE, FL_FULLBORDER,
553 fl_deactivate_object(fd_form_spell_check->slider);
555 // deactivate insert, accept, replace, and stop
556 fl_deactivate_object(fd_form_spell_check->insert);
557 fl_deactivate_object(fd_form_spell_check->accept);
558 fl_deactivate_object(fd_form_spell_check->ignore);
559 fl_deactivate_object(fd_form_spell_check->replace);
560 fl_deactivate_object(fd_form_spell_check->stop);
561 fl_deactivate_object(fd_form_spell_check->input);
562 fl_deactivate_object(fd_form_spell_check->browser);
563 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
564 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
565 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
566 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
567 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
568 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
569 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
573 if (obj == fd_form_spell_check->options){
574 SpellCheckerOptions();
576 if (obj == fd_form_spell_check->start){
577 // activate insert, accept, and stop
578 fl_activate_object(fd_form_spell_check->insert);
579 fl_activate_object(fd_form_spell_check->accept);
580 fl_activate_object(fd_form_spell_check->ignore);
581 fl_activate_object(fd_form_spell_check->stop);
582 fl_activate_object(fd_form_spell_check->input);
583 fl_activate_object(fd_form_spell_check->browser);
584 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
585 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
586 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
587 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
588 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
589 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
590 // activate replace only if the file is not read-only
591 if (!bv->buffer()->isReadonly()) {
592 fl_activate_object(fd_form_spell_check->replace);
593 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
596 // deactivate options and start
597 fl_deactivate_object(fd_form_spell_check->options);
598 fl_deactivate_object(fd_form_spell_check->start);
599 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
600 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
602 ret = RunSpellChecker(bv);
604 // deactivate insert, accept, replace, and stop
605 fl_deactivate_object(fd_form_spell_check->insert);
606 fl_deactivate_object(fd_form_spell_check->accept);
607 fl_deactivate_object(fd_form_spell_check->ignore);
608 fl_deactivate_object(fd_form_spell_check->replace);
609 fl_deactivate_object(fd_form_spell_check->stop);
610 fl_deactivate_object(fd_form_spell_check->input);
611 fl_deactivate_object(fd_form_spell_check->browser);
612 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
613 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
614 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
615 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
616 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
617 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
618 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
620 // activate options and start
621 fl_activate_object(fd_form_spell_check->options);
622 fl_activate_object(fd_form_spell_check->start);
623 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
624 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
626 // if RunSpellChecker returns false quit spellchecker
629 if (obj == fd_form_spell_check->done) break;
631 fl_hide_form(fd_form_spell_check->form_spell_check);
632 bv->endOfSpellCheck();
637 // Perform an ispell session
639 bool RunSpellChecker(BufferView * bv)
645 string tmp = (lyxrc.isp_use_alt_lang) ? lyxrc.isp_alt_lang : bv->buffer()->GetLanguage();
647 int oldval = 0; /* used for updating slider only when needed */
650 /* create ispell process */
651 create_ispell_pipe(bv->buffer()->params, tmp);
656 "The ispell-process has died for some reason. *One* possible reason\n"
657 "could be that you do not have a dictionary file\n"
658 "for the language of this document installed.\n"
659 "Check /usr/lib/ispell or set another\n"
660 "dictionary in the Spellchecker Options menu."), "", "");
665 // Put ispell in terse mode to improve speed
668 unsigned int word_count = 0;
670 char * word = bv->nextWord(newval);
671 if (word == 0) break;
674 // Update slider if and only if value has changed
675 newvalue = int(100.0*newval);
676 if(newvalue!= oldval) {
678 fl_set_slider_value(fd_form_spell_check->slider, oldval);
681 result = ispell_check_word(word);
687 obj = fl_check_forms();
688 if (obj == fd_form_spell_check->stop) {
694 if (obj == fd_form_spell_check->done) {
701 switch (result->flag) {
705 bv->selectLastWord();
706 fl_set_object_label(fd_form_spell_check->text, word);
707 fl_set_input(fd_form_spell_check->input, word);
708 fl_clear_browser(fd_form_spell_check->browser);
709 for (i = 0; i < result->count; ++i) {
710 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
716 if (obj == fd_form_spell_check->insert) {
717 ispell_insert_word(word);
720 if (obj == fd_form_spell_check->accept) {
721 ispell_accept_word(word);
724 if (obj == fd_form_spell_check->ignore) {
727 if (obj == fd_form_spell_check->replace ||
728 obj == fd_form_spell_check->input) {
729 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
730 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
733 if (obj == fd_form_spell_check->browser) {
734 // implements double click in the browser window.
735 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
737 fl_get_browser(fd_form_spell_check->browser)) {
738 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
739 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
742 clickline = fl_get_browser(fd_form_spell_check->browser);
743 fl_set_input(fd_form_spell_check->input,
744 fl_get_browser_line(fd_form_spell_check->browser,
745 fl_get_browser(fd_form_spell_check->browser)));
748 if (obj == fd_form_spell_check->stop) {
755 if (obj == fd_form_spell_check->done) {
771 string 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);