3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-1998 The LyX Team
10 * ======================================================
16 #pragma implementation
24 #include <sys/types.h>
26 #include FORMS_H_LOCATION
28 #if TIME_WITH_SYS_TIME
29 # include <sys/time.h>
33 # include <sys/time.h>
39 #ifdef HAVE_SYS_SELECT_H
40 # ifdef HAVE_STRINGS_H
41 // <strings.h> is needed at least on AIX because FD_ZERO uses bzero().
42 // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
47 #include <sys/select.h>
54 #include "spellchecker.h"
58 #include "BufferView.h"
60 #include "frontends/Dialogs.h"
62 #include "lyx_gui_misc.h"
64 #include "support/lstrings.h"
67 #include "support/lstrings.h"
70 #define USE_ORIGINAL_MANAGER_FUNCS 1
71 # include <pspell/pspell.h>
79 // Spellchecker status
89 bool RunSpellChecker(BufferView * bv);
94 FILE * out; /* streams to communicate with ispell */
95 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
98 // the true spell checker program being used
99 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
100 ActualSpellChecker actual_spell_checker;
113 // Non-static so that it can be redrawn if the xforms colors are re-mapped
114 FD_form_spell_check *fd_form_spell_check = 0;
116 void sigchldhandler(pid_t pid, int *status);
118 extern void sigchldchecker(pid_t pid, int *status);
127 const char * next_miss();
137 const char * isp_result::next_miss() {
138 if (str == 0 || *(e+1) == '\0') return 0;
140 e = strpbrk(b, ",\n");
149 PspellStringEmulation * els;
151 const char * next_miss();
157 delete_pspell_string_emulation(els);
161 const char * isp_result::next_miss()
163 return pspell_string_emulation_next(els);
170 const char * spell_error;
174 /***** Spellchecker *****/
176 // Could also use a clean up. (Asger Alstrup)
178 void init_spell_checker(BufferParams const & params, string const & lang)
180 static char o_buf[BUFSIZ]; // jc: it could be smaller
181 int pipein[2], pipeout[2];
187 if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
188 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
192 if ((out = fdopen(pipein[1], "w")) == 0) {
193 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
198 if ((in = fdopen(pipeout[0], "r")) == 0) {
199 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
204 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
211 lyxerr << "LyX: Can't create child process for spellchecker!"
218 dup2(pipein[0], STDIN_FILENO);
219 dup2(pipeout[1], STDOUT_FILENO);
226 char * tmp = new char[lyxrc.isp_command.length() + 1];
227 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
228 tmp[lyxrc.isp_command.length()] = '\0';
231 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
234 if (lang != "default") {
236 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
238 tmp = new char[lang.length() + 1];
239 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
243 if (lyxrc.isp_accept_compound) {
244 // Consider run-together words as legal compounds
246 string("-C").copy(tmp, 2); tmp[2] = '\0';
249 // Report run-together words with
250 // missing blanks as errors
252 string("-B").copy(tmp, 2); tmp[2] = '\0';
255 if (lyxrc.isp_use_esc_chars) {
256 // Specify additional characters that
257 // can be part of a word
259 string("-w").copy(tmp, 2); tmp[2] = '\0';
261 // Put the escape chars in ""s
262 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
263 tmp = new char[tms.length() + 1];
264 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
267 if (lyxrc.isp_use_pers_dict) {
268 // Specify an alternate personal dictionary
270 string("-p").copy(tmp, 2);
273 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
274 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
275 tmp[lyxrc.isp_pers_dict.length()] = '\0';
278 if (lyxrc.isp_use_input_encoding &&
279 params.inputenc != "default") {
280 string enc = (params.inputenc == "auto")
281 ? params.language->encoding()->LatexName()
283 string::size_type n = enc.length();
285 string("-T").copy(tmp, 2); tmp[2] = '\0';
286 argv[argc++] = tmp; // Input encoding
287 tmp = new char[n + 1];
295 execvp(argv[0], const_cast<char * const *>(argv));
297 // free the memory used by string::copy in the
299 for (int i= 0; i < argc -1; ++i)
302 lyxerr << "LyX: Failed to start ispell!" << endl;
306 /* Parent process: Read ispells identification message */
307 // Hmm...what are we using this id msg for? Nothing? (Lgb)
308 // Actually I used it to tell if it's truly Ispell or if it's
309 // aspell -- (kevinatk@home.com)
315 FD_SET(pipeout[0], &infds);
316 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
317 // but it can't really hurt.
320 // Configure provides us with macros which are supposed to do
321 // the right typecast.
322 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
323 SELECT_TYPE_ARG234 (&infds),
326 SELECT_TYPE_ARG5 (&tv));
329 // Ok, do the reading. We don't have to FD_ISSET since
330 // there is only one fd in infds.
331 fgets(buf, 2048, in);
333 // determine if the spell checker is really Aspell
334 if (strstr(buf, "Aspell"))
335 actual_spell_checker = ASC_ASPELL;
337 actual_spell_checker = ASC_ISPELL;
339 fputs("!\n", out); // Set terse mode (silently accept correct words)
342 } else if (retval == 0) {
343 // timeout. Give nice message to user.
344 lyxerr << "Ispell read timed out, what now?" << endl;
345 // This probably works but could need some thought
347 close(pipeout[0]); close(pipeout[1]);
348 close(pipein[0]); close(pipein[1]);
351 // Select returned error
352 lyxerr << "Select on ispell returned error, what now?" << endl;
359 "The ispell-process has died for some reason. *One* possible reason\n"
360 "could be that you do not have a dictionary file\n"
361 "for the language of this document installed.\n"
362 "Check /usr/lib/ispell or set another\n"
363 "dictionary in the spellchecker preferences.";
370 bool sc_still_alive() {
371 return isp_pid != -1;
375 void sc_clean_up_after_error()
381 // Send word to ispell and get reply
382 isp_result * sc_check_word(string const & word)
384 //Please rewrite to use string.
386 ::fputs(word.c_str(), out);
390 ::fgets(buf, 1024, in);
392 /* I think we have to check if ispell is still alive here because
393 the signal-handler could have disabled blocking on the fd */
394 if (!sc_still_alive()) return 0;
396 isp_result * result = new isp_result;
399 case '*': // Word found
400 result->flag = ISP_OK;
402 case '+': // Word found through affix removal
403 result->flag = ISP_ROOT;
405 case '-': // Word found through compound formation
406 result->flag = ISP_COMPOUNDWORD;
408 case '\n': // Number or when in terse mode: no problems
409 result->flag = ISP_IGNORE;
411 case '#': // Not found, no near misses and guesses
412 result->flag = ISP_UNKNOWN;
414 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
415 case '&': // Not found, but we have near misses
417 result->flag = ISP_MISSED;
418 char * p = strpbrk(buf, ":");
419 result->str = new char[strlen(p) + 1];
420 result->e = result->str;
421 strcpy(result->str, p);
424 default: // This shouldn't happend, but you know Murphy
425 result->flag = ISP_UNKNOWN;
429 if (result->flag!= ISP_IGNORE) {
430 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
437 void close_spell_checker()
439 // Note: If you decide to optimize this out when it is not
440 // needed please note that when Aspell is used this command
441 // is also needed to save the replacement dictionary.
442 // -- Kevin Atkinson (kevinatk@home.com)
444 fputs("#\n", out); // Save personal dictionary
452 void sc_insert_word(string const & word)
454 ::fputc('*', out); // Insert word in personal dictionary
455 ::fputs(word.c_str(), out);
461 void sc_accept_word(string const & word)
463 ::fputc('@', out); // Accept in this session
464 ::fputs(word.c_str(), out);
470 void sc_store_replacement(string const & mis, string const & cor) {
471 if (actual_spell_checker == ASC_ASPELL) {
472 ::fputs("$$ra ", out);
473 ::fputs(mis.c_str(), out);
475 ::fputs(cor.c_str(), out);
482 PspellCanHaveError * spell_error_object;
484 void init_spell_checker(BufferParams const &, string const & lang)
486 PspellConfig * config = new_pspell_config();
488 split(lang, code, '_');
489 config->replace("language-tag", code.c_str());
490 spell_error_object = new_pspell_manager(config);
491 if (pspell_error_number(spell_error_object) != 0) {
492 spell_error = pspell_error_message(spell_error_object);
495 sc = to_pspell_manager(spell_error_object);
496 spell_error_object = 0;
501 bool sc_still_alive() {
506 void sc_clean_up_after_error()
508 delete_pspell_can_have_error(spell_error_object);
513 // Send word to pspell and get reply
514 isp_result * sc_check_word(string const & word)
516 isp_result * result = new isp_result;
518 #warning Why isnt word_ok a bool? (Lgb)
520 int word_ok = pspell_manager_check(sc, word.c_str());
521 lyx::Assert(word_ok != -1);
524 result->flag = ISP_OK;
526 PspellWordList const * sugs =
527 pspell_manager_suggest(sc, word.c_str());
528 lyx::Assert(sugs != 0);
529 result->els = pspell_word_list_elements(sugs);
530 if (pspell_word_list_empty(sugs))
531 result->flag = ISP_UNKNOWN;
533 result->flag = ISP_MISSED;
540 void close_spell_checker()
542 pspell_manager_save_all_word_lists(sc);
547 void sc_insert_word(string const & word)
549 pspell_manager_add_to_personal(sc, word.c_str());
554 void sc_accept_word(string const & word)
556 pspell_manager_add_to_session(sc, word.c_str());
561 void sc_store_replacement(string const & mis, string const & cor)
563 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
571 void ShowSpellChecker(BufferView * bv)
576 // Exit if we don't have a document open
577 if (!bv->available())
580 if (fd_form_spell_check == 0) {
581 fd_form_spell_check = create_form_form_spell_check();
582 // Make sure pressing the close box does not kill LyX. (RvdK)
583 fl_set_form_atclose(fd_form_spell_check->form_spell_check,
584 CancelCloseBoxCB, 0);
588 fl_set_slider_value(fd_form_spell_check->slider, 0);
589 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
590 fl_set_object_label(fd_form_spell_check->text, "");
591 fl_set_input(fd_form_spell_check->input, "");
592 fl_clear_browser(fd_form_spell_check->browser);
595 if (fd_form_spell_check->form_spell_check->visible) {
596 fl_raise_form(fd_form_spell_check->form_spell_check);
598 fl_show_form(fd_form_spell_check->form_spell_check,
599 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
602 fl_deactivate_object(fd_form_spell_check->slider);
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);
622 if (obj == fd_form_spell_check->options) {
623 bv->owner()->getDialogs()->showSpellcheckerPreferences();
625 if (obj == fd_form_spell_check->start) {
626 // activate insert, accept, and stop
627 fl_activate_object(fd_form_spell_check->insert);
628 fl_activate_object(fd_form_spell_check->accept);
629 fl_activate_object(fd_form_spell_check->ignore);
630 fl_activate_object(fd_form_spell_check->stop);
631 fl_activate_object(fd_form_spell_check->input);
632 fl_activate_object(fd_form_spell_check->browser);
633 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
634 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
635 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
636 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
637 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
638 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
639 // activate replace only if the file is not read-only
640 if (!bv->buffer()->isReadonly()) {
641 fl_activate_object(fd_form_spell_check->replace);
642 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
645 // deactivate options and start
646 fl_deactivate_object(fd_form_spell_check->options);
647 fl_deactivate_object(fd_form_spell_check->start);
648 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
649 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
651 ret = RunSpellChecker(bv);
653 // deactivate insert, accept, replace, and stop
654 fl_deactivate_object(fd_form_spell_check->insert);
655 fl_deactivate_object(fd_form_spell_check->accept);
656 fl_deactivate_object(fd_form_spell_check->ignore);
657 fl_deactivate_object(fd_form_spell_check->replace);
658 fl_deactivate_object(fd_form_spell_check->stop);
659 fl_deactivate_object(fd_form_spell_check->input);
660 fl_deactivate_object(fd_form_spell_check->browser);
661 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
662 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
663 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
664 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
665 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
666 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
667 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
669 // activate options and start
670 fl_activate_object(fd_form_spell_check->options);
671 fl_activate_object(fd_form_spell_check->start);
672 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
673 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
675 // if RunSpellChecker returns false quit spellchecker
678 if (obj == fd_form_spell_check->done) break;
680 fl_hide_form(fd_form_spell_check->form_spell_check);
681 bv->endOfSpellCheck();
686 // Perform a spell session
689 bool RunSpellChecker(BufferView * bv)
696 string tmp = (lyxrc.isp_use_alt_lang) ?
697 lyxrc.isp_alt_lang : bv->buffer()->params.language->code();
699 string tmp = (lyxrc.isp_use_alt_lang) ?
700 lyxrc.isp_alt_lang : bv->buffer()->params.language->lang();
703 if (lyxrc.isp_use_alt_lang) {
704 Language const * lang = languages.getLanguage(tmp);
706 rtl = lang->RightToLeft();
708 rtl = bv->buffer()->params.language->RightToLeft();
710 int oldval = 0; /* used for updating slider only when needed */
713 /* create ispell process */
714 init_spell_checker(bv->buffer()->params, tmp);
716 if (spell_error != 0) {
717 fl_show_message(_(spell_error), "", "");
718 sc_clean_up_after_error();
722 unsigned int word_count = 0;
725 string const word = bv->nextWord(newval);
726 if (word.empty()) break;
729 // Update slider if and only if value has changed
730 newvalue = int(100.0*newval);
731 if (newvalue!= oldval) {
733 fl_set_slider_value(fd_form_spell_check->slider, oldval);
736 if (word_count%1000 == 0) {
737 obj = fl_check_forms();
738 if (obj == fd_form_spell_check->stop) {
739 close_spell_checker();
742 if (obj == fd_form_spell_check->done) {
743 close_spell_checker();
748 result = sc_check_word(word);
749 if (!sc_still_alive()) {
754 switch (result->flag) {
758 bv->selectLastWord();
761 reverse(tmp.begin(),tmp.end());
762 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
764 fl_set_object_label(fd_form_spell_check->text, word.c_str());
765 fl_set_input(fd_form_spell_check->input, word.c_str());
766 fl_clear_browser(fd_form_spell_check->browser);
768 while ((w = result->next_miss()) != 0) {
771 reverse(tmp.begin(),tmp.end());
772 fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
774 fl_add_browser_line(fd_form_spell_check->browser, w);
780 if (obj == fd_form_spell_check->insert) {
781 sc_insert_word(word);
784 if (obj == fd_form_spell_check->accept) {
785 sc_accept_word(word);
788 if (obj == fd_form_spell_check->ignore) {
791 if (obj == fd_form_spell_check->replace ||
792 obj == fd_form_spell_check->input) {
793 sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
794 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
797 if (obj == fd_form_spell_check->browser) {
798 // implements double click in the browser window.
799 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
801 fl_get_browser(fd_form_spell_check->browser)) {
802 sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
803 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
806 clickline = fl_get_browser(fd_form_spell_check->browser);
808 /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
810 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
812 reverse(tmp.begin(),tmp.end());
813 fl_set_input(fd_form_spell_check->input, tmp.c_str());
815 fl_set_input(fd_form_spell_check->input,
816 fl_get_browser_line(fd_form_spell_check->browser,
819 if (obj == fd_form_spell_check->stop) {
821 close_spell_checker();
825 if (obj == fd_form_spell_check->done) {
827 close_spell_checker();
837 if (sc_still_alive()) {
838 close_spell_checker();
839 string word_msg(tostr(word_count));
840 if (word_count != 1) {
841 word_msg += _(" words checked.");
843 word_msg += _(" word checked.");
845 fl_show_message("", _("Spellchecking completed!"),
849 fl_show_message(_("The spell checker has died for some reason.\n"
850 "Maybe it has been killed."), "", "");
851 sc_clean_up_after_error();
859 #warning should go somewhere more sensible
861 void sigchldhandler(pid_t pid, int * status)
865 if (pid == isp_pid) {
867 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
868 to nonblocking so we can
872 sigchldchecker(pid, status);