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"
53 extern BufferView *current_view;
55 // Spellchecker status
65 static bool RunSpellChecker(string const &);
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);
97 if (misses) // DEL LINE
103 /***** Spellchecker options *****/
105 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
106 // now also string. Amazing, eh? (Asger)
108 // Set (sane) values in form to current spellchecker options
109 void SpellOptionsUpdate()
111 // Alternate language
112 if (lyxrc->isp_alt_lang.empty()) {
113 lyxrc->isp_use_alt_lang = false;
115 fl_set_input(fd_form_spell_options->altlang_input,
116 lyxrc->isp_alt_lang.c_str());
118 if (lyxrc->isp_use_alt_lang) {
119 fl_set_button(fd_form_spell_options->buflang, 0);
120 fl_set_button(fd_form_spell_options->altlang, 1);
122 fl_set_button(fd_form_spell_options->buflang, 1);
123 fl_set_button(fd_form_spell_options->altlang, 0);
126 // Personal dictionary
127 if (lyxrc->isp_pers_dict.empty()) {
128 lyxrc->isp_use_pers_dict = false;
130 fl_set_input(fd_form_spell_options->perdict_input,
131 lyxrc->isp_pers_dict.c_str());
133 fl_set_button(fd_form_spell_options->perdict,
134 lyxrc->isp_use_pers_dict ? 1:0);
137 if (lyxrc->isp_esc_chars.empty()) {
138 lyxrc->isp_use_esc_chars = false;
140 fl_set_input(fd_form_spell_options->esc_chars_input,
141 lyxrc->isp_esc_chars.c_str());
143 fl_set_button(fd_form_spell_options->esc_chars,
144 lyxrc->isp_use_esc_chars ? 1:0);
147 fl_set_button(fd_form_spell_options->compounds,
148 lyxrc->isp_accept_compound ? 1 : 0);
149 fl_set_button(fd_form_spell_options->inpenc,
150 lyxrc->isp_use_input_encoding ? 1 : 0);
153 // Update spellchecker options
154 void SpellOptionsApplyCB(FL_OBJECT *, long)
156 // Build new status from form data
157 lyxrc->isp_use_alt_lang =
158 fl_get_button(fd_form_spell_options->altlang);
159 lyxrc->isp_use_pers_dict =
160 fl_get_button(fd_form_spell_options->perdict);
161 lyxrc->isp_accept_compound =
162 fl_get_button(fd_form_spell_options->compounds);
163 lyxrc->isp_use_esc_chars =
164 fl_get_button(fd_form_spell_options->esc_chars);
165 lyxrc->isp_use_input_encoding =
166 fl_get_button(fd_form_spell_options->inpenc);
168 // Update strings with data from input fields
169 lyxrc->isp_alt_lang =
170 fl_get_input(fd_form_spell_options->altlang_input);
171 lyxrc->isp_pers_dict =
172 fl_get_input(fd_form_spell_options->perdict_input);
173 lyxrc->isp_esc_chars =
174 fl_get_input(fd_form_spell_options->esc_chars_input);
177 SpellOptionsUpdate();
181 void SpellOptionsCancelCB(FL_OBJECT *, long)
183 fl_hide_form(fd_form_spell_options->form_spell_options);
187 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
189 SpellOptionsApplyCB(ob, data);
190 SpellOptionsCancelCB(ob, data);
194 // Show spellchecker options form
195 void SpellCheckerOptions()
197 // Create form if nescessary
198 if (fd_form_spell_options == 0) {
199 fd_form_spell_options = create_form_form_spell_options();
200 // Make sure pressing the close box does not kill LyX. (RvdK)
201 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
202 CancelCloseBoxCB, 0);
205 // Update form to current options
206 SpellOptionsUpdate();
208 // Focus in alternate language field
209 fl_set_focus_object(fd_form_spell_options->form_spell_options,
210 fd_form_spell_options->altlang_input);
213 if (fd_form_spell_options->form_spell_options->visible) {
214 fl_raise_form(fd_form_spell_options->form_spell_options);
216 fl_show_form(fd_form_spell_options->form_spell_options,
217 FL_PLACE_MOUSE, FL_FULLBORDER,
218 _("Spellchecker Options"));
223 /***** Spellchecker *****/
225 // Could also use a clean up. (Asger Alstrup)
228 void create_ispell_pipe(string const & lang)
230 static char o_buf[BUFSIZ]; // jc: it could be smaller
231 int pipein[2], pipeout[2];
237 if(pipe(pipein) == -1 || pipe(pipeout) == -1) {
238 lyxerr << "LyX: Can't create pipe for spellchecker!" << endl;
242 if ((out = fdopen(pipein[1], "w")) == 0) {
243 lyxerr << "LyX: Can't create stream for pipe for spellchecker!"
248 if ((in = fdopen(pipeout[0], "r")) == 0) {
249 lyxerr <<"LyX: Can't create stream for pipe for spellchecker!"
254 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
261 lyxerr << "LyX: Can't create child process for spellchecker!"
268 dup2(pipein[0], STDIN_FILENO);
269 dup2(pipeout[1], STDOUT_FILENO);
276 char * tmp = new char[lyxrc->isp_command.length() + 1];
277 lyxrc->isp_command.copy(tmp, lyxrc->isp_command.length());
278 tmp[lyxrc->isp_command.length()] = '\0';
281 string("-a").copy(tmp, 2); tmp[2] = '\0'; // pipe mode
284 if (lang != "default") {
286 string("-d").copy(tmp, 2); tmp[2] = '\0'; // Dictionary file
288 tmp = new char[lang.length() + 1];
289 lang.copy(tmp, lang.length()); tmp[lang.length()] = '\0';
293 if (lyxrc->isp_accept_compound) {
294 // Consider run-together words as legal compounds
296 string("-C").copy(tmp, 2); tmp[2] = '\0';
299 // Report run-together words with
300 // missing blanks as errors
302 string("-B").copy(tmp, 2); tmp[2] = '\0';
305 if (lyxrc->isp_use_esc_chars) {
306 // Specify additional characters that
307 // can be part of a word
309 string("-w").copy(tmp, 2); tmp[2] = '\0';
311 // Put the escape chars in ""s
312 string tms = "\"" + lyxrc->isp_esc_chars + "\"";
313 tmp = new char[tms.length() + 1];
314 tms.copy(tmp, tms.length()); tmp[tms.length()] = '\0';
317 if (lyxrc->isp_use_pers_dict) {
318 // Specify an alternate personal dictionary
320 string("-p").copy(tmp, 2);
323 tmp = new char[lyxrc->isp_pers_dict.length() + 1];
324 lyxrc->isp_pers_dict.copy(tmp, lyxrc->isp_pers_dict.length());
325 tmp[lyxrc->isp_pers_dict.length()] = '\0';
328 if (lyxrc->isp_use_input_encoding &&
329 current_view->buffer()->params.inputenc != "default") {
331 string("-T").copy(tmp, 2); tmp[2] = '\0';
332 argv[argc++] = tmp; // Input encoding
333 tmp = new char[current_view->buffer()->params.inputenc.length() + 1];
334 current_view->buffer()->params.inputenc.copy(tmp, current_view->buffer()->params.inputenc.length());
335 tmp[current_view->buffer()->params.inputenc.length()] = '\0';
341 execvp("ispell", const_cast<char * const *>(argv));
343 // free the memory used by string::copy in the
345 for (int i= 0; i < argc -1; ++i)
348 lyxerr << "LyX: Failed to start ispell!" << endl;
352 /* Parent process: Read ispells identification message */
353 // Hmm...what are we using this id msg for? Nothing? (Lgb)
354 // Actually I used it to tell if it's truly Ispell or if it's
355 // aspell -- (kevinatk@home.com)
361 FD_SET(pipeout[0], &infds);
362 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
363 // but it can't really hurt.
366 // Configure provides us with macros which are supposed to do
367 // the right typecast.
368 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
369 SELECT_TYPE_ARG234 (&infds),
372 SELECT_TYPE_ARG5 (&tv));
375 // Ok, do the reading. We don't have to FD_ISSET since
376 // there is only one fd in infds.
377 fgets(buf, 2048, in);
379 // determine if the spell checker is really Aspell
380 if (strstr(buf, "Aspell"))
381 actual_spell_checker = ASC_ASPELL;
383 actual_spell_checker = ASC_ISPELL;
385 } else if (retval == 0) {
386 // timeout. Give nice message to user.
387 lyxerr << "Ispell read timed out, what now?" << endl;
388 // This probably works but could need some thought
390 close(pipeout[0]); close(pipeout[1]);
391 close(pipein[0]); close(pipein[1]);
394 // Select returned error
395 lyxerr << "Select on ispell returned error, what now?" << endl;
400 // Send word to ispell and get reply
402 isp_result *ispell_check_word(char *word)
404 //Please rewrite to use string.
412 fgets(buf, 1024, in);
414 /* I think we have to check if ispell is still alive here because
415 the signal-handler could have disabled blocking on the fd */
416 if (isp_pid == -1) return 0;
418 result = new isp_result;
421 case '*': // Word found
422 result->flag = ISP_OK;
424 case '+': // Word found through affix removal
425 result->flag = ISP_ROOT;
427 case '-': // Word found through compound formation
428 result->flag = ISP_COMPOUNDWORD;
430 case '\n': // Number or when in terse mode: no problems
431 result->flag = ISP_IGNORE;
433 case '#': // Not found, no near misses and guesses
434 result->flag = ISP_UNKNOWN;
436 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
437 case '&': // Not found, but we have near misses
439 result->flag = ISP_MISSED;
441 // nb is leaked! where should it be freed? I have to
442 // admit I do not understand the intent of the code :(
444 char * nb = new char[result->str.length() + 1];
445 result->str.copy(nb, result->str.length());
446 nb[result->str.length()]= '\0';
447 p = strpbrk(nb+2, " ");
448 sscanf(p, "%d", &count); // Get near misses count
449 result->count = count;
450 if (count) result->misses = new char*[count];
451 p = strpbrk(nb, ":");
453 for (i = 0; i < count; ++i) {
454 result->misses[i] = p;
455 p = strpbrk(p, ",\n");
461 default: // This shouldn't happend, but you know Murphy
462 result->flag = ISP_UNKNOWN;
466 if (result->flag!= ISP_IGNORE) {
467 while (*buf!= '\n') fgets(buf, 255, in); /* wait for ispell to finish */
474 inline void ispell_terminate()
476 // Note: If you decide to optimize this out when it is not
477 // needed please note that when Aspell is used this command
478 // is also needed to save the replacement dictionary.
479 // -- Kevin Atkinson (kevinatk@home.com)
481 fputs("#\n", out); // Save personal dictionary
489 inline void ispell_terse_mode()
491 fputs("!\n", out); // Set terse mode (silently accept correct words)
496 inline void ispell_insert_word(char const *word)
498 fputc('*', out); // Insert word in personal dictionary
505 inline void ispell_accept_word(char const *word)
507 fputc('@', out); // Accept in this session
513 inline void ispell_store_replacement(char const *mis, string const & cor) {
514 if(actual_spell_checker == ASC_ASPELL) {
518 fputs(cor.c_str(), out);
524 void ShowSpellChecker()
529 // Exit if we don't have a document open
530 if (!current_view->getScreen())
533 if (fd_form_spell_check == 0) {
534 fd_form_spell_check = create_form_form_spell_check();
535 // Make sure pressing the close box does not kill LyX. (RvdK)
536 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
540 fl_set_slider_value(fd_form_spell_check->slider, 0);
541 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
542 fl_set_object_label(fd_form_spell_check->text, "");
543 fl_set_input(fd_form_spell_check->input, "");
544 fl_clear_browser(fd_form_spell_check->browser);
547 if (fd_form_spell_check->form_spell_check->visible) {
548 fl_raise_form(fd_form_spell_check->form_spell_check);
550 fl_show_form(fd_form_spell_check->form_spell_check,
551 FL_PLACE_MOUSE, FL_FULLBORDER,
554 fl_deactivate_object(fd_form_spell_check->slider);
556 // deactivate insert, accept, replace, and stop
557 fl_deactivate_object(fd_form_spell_check->insert);
558 fl_deactivate_object(fd_form_spell_check->accept);
559 fl_deactivate_object(fd_form_spell_check->ignore);
560 fl_deactivate_object(fd_form_spell_check->replace);
561 fl_deactivate_object(fd_form_spell_check->stop);
562 fl_deactivate_object(fd_form_spell_check->input);
563 fl_deactivate_object(fd_form_spell_check->browser);
564 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
565 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
566 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
567 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
568 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
569 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
570 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
574 if (obj == fd_form_spell_check->options){
575 SpellCheckerOptions();
577 if (obj == fd_form_spell_check->start){
578 // activate insert, accept, and stop
579 fl_activate_object(fd_form_spell_check->insert);
580 fl_activate_object(fd_form_spell_check->accept);
581 fl_activate_object(fd_form_spell_check->ignore);
582 fl_activate_object(fd_form_spell_check->stop);
583 fl_activate_object(fd_form_spell_check->input);
584 fl_activate_object(fd_form_spell_check->browser);
585 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
586 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
587 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
588 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
589 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
590 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
591 // activate replace only if the file is not read-only
592 if (!current_view->buffer()->isReadonly()) {
593 fl_activate_object(fd_form_spell_check->replace);
594 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
597 // deactivate options and start
598 fl_deactivate_object(fd_form_spell_check->options);
599 fl_deactivate_object(fd_form_spell_check->start);
600 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
601 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
603 ret = RunSpellChecker(current_view->buffer()->GetLanguage());
605 // deactivate insert, accept, replace, and stop
606 fl_deactivate_object(fd_form_spell_check->insert);
607 fl_deactivate_object(fd_form_spell_check->accept);
608 fl_deactivate_object(fd_form_spell_check->ignore);
609 fl_deactivate_object(fd_form_spell_check->replace);
610 fl_deactivate_object(fd_form_spell_check->stop);
611 fl_deactivate_object(fd_form_spell_check->input);
612 fl_deactivate_object(fd_form_spell_check->browser);
613 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
614 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
615 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
616 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
617 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
618 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
619 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
621 // activate options and start
622 fl_activate_object(fd_form_spell_check->options);
623 fl_activate_object(fd_form_spell_check->start);
624 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
625 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
627 // if RunSpellChecker returns false quit spellchecker
630 if (obj == fd_form_spell_check->done) break;
632 fl_hide_form(fd_form_spell_check->form_spell_check);
633 current_view->endOfSpellCheck();
638 // Perform an ispell session
640 bool RunSpellChecker(string const & lang)
644 int i, oldval, clickline, newvalue;
647 unsigned int word_count = 0;
649 string tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang:lang;
651 oldval = 0; /* used for updating slider only when needed */
654 /* create ispell process */
655 create_ispell_pipe(tmp);
660 "The ispell-process has died for some reason. *One* possible reason\n"
661 "could be that you do not have a dictionary file\n"
662 "for the language of this document installed.\n"
663 "Check /usr/lib/ispell or set another\n"
664 "dictionary in the Spellchecker Options menu."), "", "");
669 // Put ispell in terse mode to improve speed
673 word = current_view->nextWord(newval);
674 if (word == 0) break;
677 // Update slider if and only if value has changed
678 newvalue = int(100.0*newval);
679 if(newvalue!= oldval) {
681 fl_set_slider_value(fd_form_spell_check->slider, oldval);
684 result = ispell_check_word(word);
690 obj = fl_check_forms();
691 if (obj == fd_form_spell_check->stop) {
697 if (obj == fd_form_spell_check->done) {
704 switch (result->flag) {
707 current_view->selectLastWord();
708 fl_set_object_label(fd_form_spell_check->text, word);
709 fl_set_input(fd_form_spell_check->input, word);
710 fl_clear_browser(fd_form_spell_check->browser);
711 for (i = 0; i < result->count; ++i) {
712 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
718 if (obj == fd_form_spell_check->insert) {
719 ispell_insert_word(word);
722 if (obj == fd_form_spell_check->accept) {
723 ispell_accept_word(word);
726 if (obj == fd_form_spell_check->ignore) {
729 if (obj == fd_form_spell_check->replace ||
730 obj == fd_form_spell_check->input) {
731 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
732 current_view->replaceWord(fl_get_input(fd_form_spell_check->input));
735 if (obj == fd_form_spell_check->browser) {
736 // implements double click in the browser window.
737 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
739 fl_get_browser(fd_form_spell_check->browser)) {
740 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
741 current_view->replaceWord(fl_get_input(fd_form_spell_check->input));
744 clickline = fl_get_browser(fd_form_spell_check->browser);
745 fl_set_input(fd_form_spell_check->input,
746 fl_get_browser_line(fd_form_spell_check->browser,
747 fl_get_browser(fd_form_spell_check->browser)));
750 if (obj == fd_form_spell_check->stop) {
757 if (obj == fd_form_spell_check->done) {
773 word_msg += tostr(word_count);
774 if (word_count != 1) {
775 word_msg += _(" words checked.");
777 word_msg += _(" word checked.");
779 fl_show_message("", _("Spellchecking completed!"),
783 fl_show_message(_("The ispell-process has died for some reason.\n"
784 "Maybe it has been killed."), "", "");
791 //void sigchldhandler(int sig)
792 void sigchldhandler(pid_t pid, int *status)
797 //if (waitpid(isp_pid, &status, WNOHANG) == isp_pid) {
798 if (pid == isp_pid) {
800 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
801 to nonblocking so we can
804 //sigchldchecker(sig);
805 sigchldchecker(pid, status);