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().
43 // BUT we cannot include both string.h and strings.h on Irix 6.5 :(
48 #include <sys/select.h>
55 #include "spellchecker.h"
59 #include "BufferView.h"
61 #include "lyx_gui_misc.h"
63 #include "support/lstrings.h"
66 #include "support/lstrings.h"
69 # include <pspell/pspell.h>
77 // Spellchecker status
87 bool RunSpellChecker(BufferView * bv);
92 FILE * out; /* streams to communicate with ispell */
93 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
96 // the true spell checker program being used
97 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
98 ActualSpellChecker actual_spell_checker;
111 // Non-static so that it can be redrawn if the xforms colors are re-mapped
112 FD_form_spell_options *fd_form_spell_options = 0;
113 FD_form_spell_check *fd_form_spell_check = 0;
115 void sigchldhandler(pid_t pid, int *status);
117 extern void sigchldchecker(pid_t pid, int *status);
126 const char * next_miss();
136 const char * isp_result::next_miss() {
137 if (str == 0 || *(e+1) == '\0') return 0;
139 e = strpbrk(b, ",\n");
148 PspellStringEmulation * els;
150 const char * next_miss();
156 delete_pspell_string_emulation(els);
160 const char * isp_result::next_miss()
162 return pspell_string_emulation_next(els);
167 const char * spell_error;
170 /***** Spellchecker options *****/
172 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
173 // now also string. Amazing, eh? (Asger)
175 // Set (sane) values in form to current spellchecker options
176 void SpellOptionsUpdate()
178 // Alternate language
179 if (lyxrc.isp_alt_lang.empty()) {
180 lyxrc.isp_use_alt_lang = false;
182 fl_set_input(fd_form_spell_options->altlang_input,
183 lyxrc.isp_alt_lang.c_str());
185 if (lyxrc.isp_use_alt_lang) {
186 fl_set_button(fd_form_spell_options->buflang, 0);
187 fl_set_button(fd_form_spell_options->altlang, 1);
189 fl_set_button(fd_form_spell_options->buflang, 1);
190 fl_set_button(fd_form_spell_options->altlang, 0);
193 // Personal dictionary
194 if (lyxrc.isp_pers_dict.empty()) {
195 lyxrc.isp_use_pers_dict = false;
197 fl_set_input(fd_form_spell_options->perdict_input,
198 lyxrc.isp_pers_dict.c_str());
200 fl_set_button(fd_form_spell_options->perdict,
201 lyxrc.isp_use_pers_dict ? 1:0);
204 if (lyxrc.isp_esc_chars.empty()) {
205 lyxrc.isp_use_esc_chars = false;
207 fl_set_input(fd_form_spell_options->esc_chars_input,
208 lyxrc.isp_esc_chars.c_str());
210 fl_set_button(fd_form_spell_options->esc_chars,
211 lyxrc.isp_use_esc_chars ? 1:0);
214 fl_set_button(fd_form_spell_options->compounds,
215 lyxrc.isp_accept_compound ? 1 : 0);
216 fl_set_button(fd_form_spell_options->inpenc,
217 lyxrc.isp_use_input_encoding ? 1 : 0);
220 // Update spellchecker options
221 void SpellOptionsApplyCB(FL_OBJECT *, long)
223 // Build new status from form data
224 lyxrc.isp_use_alt_lang =
225 fl_get_button(fd_form_spell_options->altlang);
226 lyxrc.isp_use_pers_dict =
227 fl_get_button(fd_form_spell_options->perdict);
228 lyxrc.isp_accept_compound =
229 fl_get_button(fd_form_spell_options->compounds);
230 lyxrc.isp_use_esc_chars =
231 fl_get_button(fd_form_spell_options->esc_chars);
232 lyxrc.isp_use_input_encoding =
233 fl_get_button(fd_form_spell_options->inpenc);
235 // Update strings with data from input fields
237 fl_get_input(fd_form_spell_options->altlang_input);
238 lyxrc.isp_pers_dict =
239 fl_get_input(fd_form_spell_options->perdict_input);
240 lyxrc.isp_esc_chars =
241 fl_get_input(fd_form_spell_options->esc_chars_input);
244 SpellOptionsUpdate();
248 void SpellOptionsCancelCB(FL_OBJECT *, long)
250 fl_hide_form(fd_form_spell_options->form_spell_options);
254 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
256 SpellOptionsApplyCB(ob, data);
257 SpellOptionsCancelCB(ob, data);
261 // Show spellchecker options form
262 void SpellCheckerOptions()
264 // Create form if nescessary
265 if (fd_form_spell_options == 0) {
266 fd_form_spell_options = create_form_form_spell_options();
267 // Make sure pressing the close box does not kill LyX. (RvdK)
268 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
269 CancelCloseBoxCB, 0);
272 // Update form to current options
273 SpellOptionsUpdate();
275 // Focus in alternate language field
276 fl_set_focus_object(fd_form_spell_options->form_spell_options,
277 fd_form_spell_options->altlang_input);
280 if (fd_form_spell_options->form_spell_options->visible) {
281 fl_raise_form(fd_form_spell_options->form_spell_options);
283 fl_show_form(fd_form_spell_options->form_spell_options,
284 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
285 _("Spellchecker Options"));
291 /***** Spellchecker *****/
293 // Could also use a clean up. (Asger Alstrup)
297 void init_spell_checker(BufferParams const & params, string const & lang)
299 static char o_buf[BUFSIZ]; // jc: it could be smaller
300 int pipein[2], pipeout[2];
306 if (pipe(pipein) == -1 || pipe(pipeout) == -1) {
307 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
311 if ((out = fdopen(pipein[1], "w")) == 0) {
312 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
317 if ((in = fdopen(pipeout[0], "r")) == 0) {
318 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
323 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
330 lyxerr << "LyX: Can't create child process for spellchecker!"
337 dup2(pipein[0], STDIN_FILENO);
338 dup2(pipeout[1], STDOUT_FILENO);
345 char * tmp = new char[lyxrc.isp_command.length() + 1];
346 lyxrc.isp_command.copy(tmp, lyxrc.isp_command.length());
347 tmp[lyxrc.isp_command.length()] = '\0';
350 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
353 if (lang != "default") {
355 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
357 tmp = new char[lang.length() + 1];
358 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
362 if (lyxrc.isp_accept_compound) {
363 // Consider run-together words as legal compounds
365 string("-C").copy(tmp, 2); tmp[2] = '\0';
368 // Report run-together words with
369 // missing blanks as errors
371 string("-B").copy(tmp, 2); tmp[2] = '\0';
374 if (lyxrc.isp_use_esc_chars) {
375 // Specify additional characters that
376 // can be part of a word
378 string("-w").copy(tmp, 2); tmp[2] = '\0';
380 // Put the escape chars in ""s
381 string tms = "\"" + lyxrc.isp_esc_chars + "\"";
382 tmp = new char[tms.length() + 1];
383 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
386 if (lyxrc.isp_use_pers_dict) {
387 // Specify an alternate personal dictionary
389 string("-p").copy(tmp, 2);
392 tmp = new char[lyxrc.isp_pers_dict.length() + 1];
393 lyxrc.isp_pers_dict.copy(tmp, lyxrc.isp_pers_dict.length());
394 tmp[lyxrc.isp_pers_dict.length()] = '\0';
397 if (lyxrc.isp_use_input_encoding &&
398 params.inputenc != "default") {
399 string enc = (params.inputenc == "auto")
400 ? params.language->encoding()->LatexName()
402 string::size_type n = enc.length();
404 string("-T").copy(tmp, 2); tmp[2] = '\0';
405 argv[argc++] = tmp; // Input encoding
406 tmp = new char[n + 1];
414 execvp(argv[0], const_cast<char * const *>(argv));
416 // free the memory used by string::copy in the
418 for (int i= 0; i < argc -1; ++i)
421 lyxerr << "LyX: Failed to start ispell!" << endl;
425 /* Parent process: Read ispells identification message */
426 // Hmm...what are we using this id msg for? Nothing? (Lgb)
427 // Actually I used it to tell if it's truly Ispell or if it's
428 // aspell -- (kevinatk@home.com)
434 FD_SET(pipeout[0], &infds);
435 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
436 // but it can't really hurt.
439 // Configure provides us with macros which are supposed to do
440 // the right typecast.
441 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
442 SELECT_TYPE_ARG234 (&infds),
445 SELECT_TYPE_ARG5 (&tv));
448 // Ok, do the reading. We don't have to FD_ISSET since
449 // there is only one fd in infds.
450 fgets(buf, 2048, in);
452 // determine if the spell checker is really Aspell
453 if (strstr(buf, "Aspell"))
454 actual_spell_checker = ASC_ASPELL;
456 actual_spell_checker = ASC_ISPELL;
458 fputs("!\n", out); // Set terse mode (silently accept correct words)
461 } else if (retval == 0) {
462 // timeout. Give nice message to user.
463 lyxerr << "Ispell read timed out, what now?" << endl;
464 // This probably works but could need some thought
466 close(pipeout[0]); close(pipeout[1]);
467 close(pipein[0]); close(pipein[1]);
470 // Select returned error
471 lyxerr << "Select on ispell returned error, what now?" << endl;
478 "The ispell-process has died for some reason. *One* possible reason\n"
479 "could be that you do not have a dictionary file\n"
480 "for the language of this document installed.\n"
481 "Check /usr/lib/ispell or set another\n"
482 "dictionary in the Spellchecker Options menu.";
489 bool sc_still_alive() {
490 return isp_pid != -1;
494 void sc_clean_up_after_error()
500 // Send word to ispell and get reply
501 isp_result * sc_check_word(string const & word)
503 //Please rewrite to use string.
505 ::fputs(word.c_str(), out);
509 ::fgets(buf, 1024, in);
511 /* I think we have to check if ispell is still alive here because
512 the signal-handler could have disabled blocking on the fd */
513 if (!sc_still_alive()) return 0;
515 isp_result * result = new isp_result;
518 case '*': // Word found
519 result->flag = ISP_OK;
521 case '+': // Word found through affix removal
522 result->flag = ISP_ROOT;
524 case '-': // Word found through compound formation
525 result->flag = ISP_COMPOUNDWORD;
527 case '\n': // Number or when in terse mode: no problems
528 result->flag = ISP_IGNORE;
530 case '#': // Not found, no near misses and guesses
531 result->flag = ISP_UNKNOWN;
533 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
534 case '&': // Not found, but we have near misses
536 result->flag = ISP_MISSED;
537 char * p = strpbrk(buf, ":");
538 result->str = new char[strlen(p) + 1];
539 result->e = result->str;
540 strcpy(result->str, p);
543 default: // This shouldn't happend, but you know Murphy
544 result->flag = ISP_UNKNOWN;
548 if (result->flag!= ISP_IGNORE) {
549 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
556 void close_spell_checker()
558 // Note: If you decide to optimize this out when it is not
559 // needed please note that when Aspell is used this command
560 // is also needed to save the replacement dictionary.
561 // -- Kevin Atkinson (kevinatk@home.com)
563 fputs("#\n", out); // Save personal dictionary
571 void sc_insert_word(string const & word)
573 ::fputc('*', out); // Insert word in personal dictionary
574 ::fputs(word.c_str(), out);
580 void sc_accept_word(string const & word)
582 ::fputc('@', out); // Accept in this session
583 ::fputs(word.c_str(), out);
589 void sc_store_replacement(string const & mis, string const & cor) {
590 if (actual_spell_checker == ASC_ASPELL) {
591 ::fputs("$$ra ", out);
592 ::fputs(mis.c_str(), out);
594 ::fputs(cor.c_str(), out);
601 PspellCanHaveError * spell_error_object;
603 void init_spell_checker(BufferParams const &, string const & lang)
605 PspellConfig * config = new_pspell_config();
607 split(lang, code, '_');
608 config->replace("language-tag", code.c_str());
609 spell_error_object = new_pspell_manager(config);
610 if (pspell_error_number(spell_error_object) != 0) {
611 spell_error = pspell_error_message(spell_error_object);
614 sc = to_pspell_manager(spell_error_object);
615 spell_error_object = 0;
620 bool sc_still_alive() {
625 void sc_clean_up_after_error()
627 delete_pspell_can_have_error(spell_error_object);
632 // Send word to pspell and get reply
633 isp_result * sc_check_word(string const & word)
635 isp_result * result = new isp_result;
636 #warning Why isnt word_ok a bool? (Lgb)
637 int word_ok = pspell_manager_check(sc, word.c_str());
638 Assert(word_ok != -1);
641 result->flag = ISP_OK;
643 PspellWordList const * sugs =
644 pspell_manager_suggest(sc, word.c_str());
646 result->els = pspell_word_list_elements(sugs);
647 if (pspell_word_list_empty(sugs))
648 result->flag = ISP_UNKNOWN;
650 result->flag = ISP_MISSED;
657 void close_spell_checker()
659 pspell_manager_save_all_word_lists(sc);
664 void sc_insert_word(string const & word)
666 pspell_manager_add_to_personal(sc, word.c_str());
671 void sc_accept_word(string const & word)
673 pspell_manager_add_to_session(sc, word.c_str());
678 void sc_store_replacement(string const & mis, string const & cor)
680 pspell_manager_store_replacement(sc, mis.c_str(), cor.c_str());
688 void ShowSpellChecker(BufferView * bv)
693 // Exit if we don't have a document open
694 if (!bv->available())
697 if (fd_form_spell_check == 0) {
698 fd_form_spell_check = create_form_form_spell_check();
699 // Make sure pressing the close box does not kill LyX. (RvdK)
700 fl_set_form_atclose(fd_form_spell_check->form_spell_check,
701 CancelCloseBoxCB, 0);
705 fl_set_slider_value(fd_form_spell_check->slider, 0);
706 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
707 fl_set_object_label(fd_form_spell_check->text, "");
708 fl_set_input(fd_form_spell_check->input, "");
709 fl_clear_browser(fd_form_spell_check->browser);
712 if (fd_form_spell_check->form_spell_check->visible) {
713 fl_raise_form(fd_form_spell_check->form_spell_check);
715 fl_show_form(fd_form_spell_check->form_spell_check,
716 FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
719 fl_deactivate_object(fd_form_spell_check->slider);
721 // deactivate insert, accept, replace, and stop
722 fl_deactivate_object(fd_form_spell_check->insert);
723 fl_deactivate_object(fd_form_spell_check->accept);
724 fl_deactivate_object(fd_form_spell_check->ignore);
725 fl_deactivate_object(fd_form_spell_check->replace);
726 fl_deactivate_object(fd_form_spell_check->stop);
727 fl_deactivate_object(fd_form_spell_check->input);
728 fl_deactivate_object(fd_form_spell_check->browser);
729 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
730 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
731 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
732 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
733 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
734 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
735 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
739 if (obj == fd_form_spell_check->options) {
740 SpellCheckerOptions();
742 if (obj == fd_form_spell_check->start) {
743 // activate insert, accept, and stop
744 fl_activate_object(fd_form_spell_check->insert);
745 fl_activate_object(fd_form_spell_check->accept);
746 fl_activate_object(fd_form_spell_check->ignore);
747 fl_activate_object(fd_form_spell_check->stop);
748 fl_activate_object(fd_form_spell_check->input);
749 fl_activate_object(fd_form_spell_check->browser);
750 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
751 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
752 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
753 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
754 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
755 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
756 // activate replace only if the file is not read-only
757 if (!bv->buffer()->isReadonly()) {
758 fl_activate_object(fd_form_spell_check->replace);
759 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
762 // deactivate options and start
763 fl_deactivate_object(fd_form_spell_check->options);
764 fl_deactivate_object(fd_form_spell_check->start);
765 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
766 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
768 ret = RunSpellChecker(bv);
770 // deactivate insert, accept, replace, and stop
771 fl_deactivate_object(fd_form_spell_check->insert);
772 fl_deactivate_object(fd_form_spell_check->accept);
773 fl_deactivate_object(fd_form_spell_check->ignore);
774 fl_deactivate_object(fd_form_spell_check->replace);
775 fl_deactivate_object(fd_form_spell_check->stop);
776 fl_deactivate_object(fd_form_spell_check->input);
777 fl_deactivate_object(fd_form_spell_check->browser);
778 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
779 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
780 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
781 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
782 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
783 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
784 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
786 // activate options and start
787 fl_activate_object(fd_form_spell_check->options);
788 fl_activate_object(fd_form_spell_check->start);
789 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
790 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
792 // if RunSpellChecker returns false quit spellchecker
795 if (obj == fd_form_spell_check->done) break;
797 fl_hide_form(fd_form_spell_check->form_spell_check);
798 bv->endOfSpellCheck();
803 // Perform a spell session
806 bool RunSpellChecker(BufferView * bv)
819 string tmp = (lyxrc.isp_use_alt_lang) ?
820 lyxrc.isp_alt_lang : bv->buffer()->params.language->code();
822 string tmp = (lyxrc.isp_use_alt_lang) ?
823 lyxrc.isp_alt_lang : bv->buffer()->params.language->lang();
826 if (lyxrc.isp_use_alt_lang) {
827 Language const * lang = languages.getLanguage(tmp);
829 rtl = lang->RightToLeft();
831 rtl = bv->buffer()->params.language->RightToLeft();
833 int oldval = 0; /* used for updating slider only when needed */
836 /* create ispell process */
837 init_spell_checker(bv->buffer()->params, tmp);
839 if (spell_error != 0) {
840 fl_show_message(_(spell_error), "", "");
841 sc_clean_up_after_error();
845 unsigned int word_count = 0;
848 string const word = bv->nextWord(newval);
849 if (word.empty()) break;
852 // Update slider if and only if value has changed
853 newvalue = int(100.0*newval);
854 if (newvalue!= oldval) {
856 fl_set_slider_value(fd_form_spell_check->slider, oldval);
859 if (word_count%1000 == 0) {
860 obj = fl_check_forms();
861 if (obj == fd_form_spell_check->stop) {
862 close_spell_checker();
865 if (obj == fd_form_spell_check->done) {
866 close_spell_checker();
871 result = sc_check_word(word);
872 if (!sc_still_alive()) {
877 switch (result->flag) {
881 bv->selectLastWord();
884 reverse(tmp.begin(),tmp.end());
885 fl_set_object_label(fd_form_spell_check->text, tmp.c_str());
887 fl_set_object_label(fd_form_spell_check->text, word.c_str());
888 fl_set_input(fd_form_spell_check->input, word.c_str());
889 fl_clear_browser(fd_form_spell_check->browser);
891 while ((w = result->next_miss()) != 0) {
894 reverse(tmp.begin(),tmp.end());
895 fl_add_browser_line(fd_form_spell_check->browser, tmp.c_str());
897 fl_add_browser_line(fd_form_spell_check->browser, w);
903 if (obj == fd_form_spell_check->insert) {
904 sc_insert_word(word);
907 if (obj == fd_form_spell_check->accept) {
908 sc_accept_word(word);
911 if (obj == fd_form_spell_check->ignore) {
914 if (obj == fd_form_spell_check->replace ||
915 obj == fd_form_spell_check->input) {
916 sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
917 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
920 if (obj == fd_form_spell_check->browser) {
921 // implements double click in the browser window.
922 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
924 fl_get_browser(fd_form_spell_check->browser)) {
925 sc_store_replacement(word, fl_get_input(fd_form_spell_check->input));
926 bv->replaceWord(fl_get_input(fd_form_spell_check->input));
929 clickline = fl_get_browser(fd_form_spell_check->browser);
931 /// fl_set_input(fd_form_spell_check->input, result->misses[clickline-1]); ?
933 string tmp = fl_get_browser_line(fd_form_spell_check->browser,
935 reverse(tmp.begin(),tmp.end());
936 fl_set_input(fd_form_spell_check->input, tmp.c_str());
938 fl_set_input(fd_form_spell_check->input,
939 fl_get_browser_line(fd_form_spell_check->browser,
942 if (obj == fd_form_spell_check->stop) {
944 close_spell_checker();
948 if (obj == fd_form_spell_check->done) {
950 close_spell_checker();
960 if (sc_still_alive()) {
961 close_spell_checker();
962 string word_msg(tostr(word_count));
963 if (word_count != 1) {
964 word_msg += _(" words checked.");
966 word_msg += _(" word checked.");
968 fl_show_message("", _("Spellchecking completed!"),
972 fl_show_message(_("The spell checker has died for some reason.\n"
973 "Maybe it has been killed."), "", "");
974 sc_clean_up_after_error();
982 #warning should go somewhere more sensible
984 void sigchldhandler(pid_t pid, int * status)
988 if (pid == isp_pid) {
990 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
991 to nonblocking so we can
995 sigchldchecker(pid, status);