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);
97 if (misses) delete[] misses;
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
168 lyxrc->isp_alt_lang =
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(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 current_view->currentBuffer()->params.inputenc != "default") {
330 string("-T").copy(tmp, 2); tmp[2] = '\0';
331 argv[argc++] = tmp; // Input encoding
332 tmp = new char[current_view->currentBuffer()->params.inputenc.length() + 1];
333 current_view->currentBuffer()->params.inputenc.copy(tmp, current_view->currentBuffer()->params.inputenc.length());
334 tmp[current_view->currentBuffer()->params.inputenc.length()] = '\0';
340 execvp("ispell", (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)
357 #warning verify that this works.
363 FD_SET(pipeout[0], &infds);
364 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
365 // but it can't really hurt.
368 // Configure provides us with macros which are supposed to do
369 // the right typecast.
370 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
371 SELECT_TYPE_ARG234 (&infds),
374 SELECT_TYPE_ARG5 (&tv));
377 // Ok, do the reading. We don't have to FD_ISSET since
378 // there is only one fd in infds.
379 fgets(buf, 2048, in);
381 // determine if the spell checker is really Aspell
382 if (strstr(buf, "Aspell"))
383 actual_spell_checker = ASC_ASPELL;
385 actual_spell_checker = ASC_ISPELL;
387 } else if (retval == 0) {
388 // timeout. Give nice message to user.
389 lyxerr << "Ispell read timed out, what now?" << endl;
391 #warning Is this the correct thing to do?
394 close(pipeout[0]); close(pipeout[1]);
395 close(pipein[0]); close(pipein[1]);
398 // Select returned error
399 lyxerr << "Select on ispell returned error, what now?" << endl;
404 // Send word to ispell and get reply
406 isp_result *ispell_check_word(char *word)
408 //Please rewrite to use string.
416 fgets(buf, 1024, in);
418 /* I think we have to check if ispell is still alive here because
419 the signal-handler could have disabled blocking on the fd */
420 if (isp_pid == -1) return (isp_result *) 0;
422 result = new isp_result;
425 case '*': // Word found
426 result->flag = ISP_OK;
428 case '+': // Word found through affix removal
429 result->flag = ISP_ROOT;
431 case '-': // Word found through compound formation
432 result->flag = ISP_COMPOUNDWORD;
434 case '\n': // Number or when in terse mode: no problems
435 result->flag = ISP_IGNORE;
437 case '#': // Not found, no near misses and guesses
438 result->flag = ISP_UNKNOWN;
440 case '?': // Not found, and no near misses, but guesses (guesses are ignored)
441 case '&': // Not found, but we have near misses
443 result->flag = ISP_MISSED;
445 char * nb = new char[result->str.length() + 1];
446 result->str.copy(nb, result->str.length());
447 nb[result->str.length()]='\0';
448 p = strpbrk(nb+2, " ");
449 sscanf(p, "%d", &count); // Get near misses count
450 result->count = count;
451 if (count) result->misses = new char*[count];
452 p = strpbrk(nb, ":");
454 for (i = 0; i<count; i++) {
455 result->misses[i] = p;
456 p = strpbrk(p, ",\n");
462 default: // This shouldn't happend, but you know Murphy
463 result->flag = ISP_UNKNOWN;
467 if (result->flag!=ISP_IGNORE) {
468 while (*buf!='\n') fgets(buf, 255, in); /* wait for ispell to finish */
475 inline void ispell_terminate()
477 // Note: If you decide to optimize this out when it is not
478 // needed please note that when Aspell is used this command
479 // is also needed to save the replacement dictionary.
480 // -- Kevin Atkinson (kevinatk@home.com)
482 fputs("#\n", out); // Save personal dictionary
490 inline void ispell_terse_mode()
492 fputs("!\n", out); // Set terse mode (silently accept correct words)
497 inline void ispell_insert_word(char const *word)
499 fputc('*', out); // Insert word in personal dictionary
506 inline void ispell_accept_word(char const *word)
508 fputc('@', out); // Accept in this session
514 inline void ispell_store_replacement(const char *mis, string const & cor) {
515 if(actual_spell_checker == ASC_ASPELL) {
519 fputs(cor.c_str(), out);
525 void ShowSpellChecker()
530 // Exit if we don't have a document open
531 if (!current_view->getScreen())
534 if (fd_form_spell_check == 0) {
535 fd_form_spell_check = create_form_form_spell_check();
536 // Make sure pressing the close box does not kill LyX. (RvdK)
537 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, 0);
541 fl_set_slider_value(fd_form_spell_check->slider, 0);
542 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
543 fl_set_object_label(fd_form_spell_check->text, "");
544 fl_set_input(fd_form_spell_check->input, "");
545 fl_clear_browser(fd_form_spell_check->browser);
548 if (fd_form_spell_check->form_spell_check->visible) {
549 fl_raise_form(fd_form_spell_check->form_spell_check);
551 fl_show_form(fd_form_spell_check->form_spell_check,
552 FL_PLACE_MOUSE,FL_FULLBORDER,
555 fl_deactivate_object(fd_form_spell_check->slider);
557 // deactivate insert, accept, replace, and stop
558 fl_deactivate_object(fd_form_spell_check->insert);
559 fl_deactivate_object(fd_form_spell_check->accept);
560 fl_deactivate_object(fd_form_spell_check->ignore);
561 fl_deactivate_object(fd_form_spell_check->replace);
562 fl_deactivate_object(fd_form_spell_check->stop);
563 fl_deactivate_object(fd_form_spell_check->input);
564 fl_deactivate_object(fd_form_spell_check->browser);
565 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
566 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
567 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
568 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
569 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
570 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
571 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
575 if (obj == fd_form_spell_check->options){
576 SpellCheckerOptions();
578 if (obj == fd_form_spell_check->start){
579 // activate insert, accept, and stop
580 fl_activate_object(fd_form_spell_check->insert);
581 fl_activate_object(fd_form_spell_check->accept);
582 fl_activate_object(fd_form_spell_check->ignore);
583 fl_activate_object(fd_form_spell_check->stop);
584 fl_activate_object(fd_form_spell_check->input);
585 fl_activate_object(fd_form_spell_check->browser);
586 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
587 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
588 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
589 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
590 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
591 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
592 // activate replace only if the file is not read-only
593 if (!current_view->currentBuffer()->isReadonly()) {
594 fl_activate_object(fd_form_spell_check->replace);
595 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
598 // deactivate options and start
599 fl_deactivate_object(fd_form_spell_check->options);
600 fl_deactivate_object(fd_form_spell_check->start);
601 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
602 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
604 ret = RunSpellChecker(current_view->currentBuffer()->GetLanguage());
606 // deactivate insert, accept, replace, and stop
607 fl_deactivate_object(fd_form_spell_check->insert);
608 fl_deactivate_object(fd_form_spell_check->accept);
609 fl_deactivate_object(fd_form_spell_check->ignore);
610 fl_deactivate_object(fd_form_spell_check->replace);
611 fl_deactivate_object(fd_form_spell_check->stop);
612 fl_deactivate_object(fd_form_spell_check->input);
613 fl_deactivate_object(fd_form_spell_check->browser);
614 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
615 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
616 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
617 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
618 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
619 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
620 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
622 // activate options and start
623 fl_activate_object(fd_form_spell_check->options);
624 fl_activate_object(fd_form_spell_check->start);
625 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
626 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
628 // if RunSpellChecker returns false quit spellchecker
631 if (obj == fd_form_spell_check->done) break;
633 fl_hide_form(fd_form_spell_check->form_spell_check);
639 // Perform an ispell session
641 bool RunSpellChecker(string const & lang)
645 int i, oldval, clickline, newvalue;
648 unsigned int word_count = 0;
650 string tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang:lang;
652 oldval = 0; /* used for updating slider only when needed */
655 /* create ispell process */
656 create_ispell_pipe(tmp);
661 "The ispell-process has died for some reason. *One* possible reason\n"
662 "could be that you do not have a dictionary file\n"
663 "for the language of this document installed.\n"
664 "Check /usr/lib/ispell or set another\n"
665 "dictionary in the Spellchecker Options menu."), "", "");
670 // Put ispell in terse mode to improve speed
674 word = NextWord(newval);
678 // Update slider if and only if value has changed
679 newvalue = int(100.0*newval);
680 if(newvalue!=oldval) {
682 fl_set_slider_value(fd_form_spell_check->slider, oldval);
685 result = ispell_check_word(word);
691 obj = fl_check_forms();
692 if (obj == fd_form_spell_check->stop) {
698 if (obj == fd_form_spell_check->done) {
705 switch (result->flag) {
709 fl_set_object_label(fd_form_spell_check->text, word);
710 fl_set_input(fd_form_spell_check->input, word);
711 fl_clear_browser(fd_form_spell_check->browser);
712 for (i=0; i<result->count; i++) {
713 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
719 if (obj==fd_form_spell_check->insert) {
720 ispell_insert_word(word);
723 if (obj==fd_form_spell_check->accept) {
724 ispell_accept_word(word);
727 if (obj==fd_form_spell_check->ignore) {
730 if (obj==fd_form_spell_check->replace ||
731 obj==fd_form_spell_check->input) {
732 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
733 ReplaceWord(fl_get_input(fd_form_spell_check->input));
736 if (obj==fd_form_spell_check->browser) {
737 // implements double click in the browser window.
738 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
740 fl_get_browser(fd_form_spell_check->browser)) {
741 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
742 ReplaceWord(fl_get_input(fd_form_spell_check->input));
745 clickline = fl_get_browser(fd_form_spell_check->browser);
746 fl_set_input(fd_form_spell_check->input,
747 fl_get_browser_line(fd_form_spell_check->browser,
748 fl_get_browser(fd_form_spell_check->browser)));
751 if (obj==fd_form_spell_check->stop) {
758 if (obj==fd_form_spell_check->done) {
774 word_msg += tostr(word_count);
775 if (word_count != 1) {
776 word_msg += _(" words checked.");
778 word_msg += _(" word checked.");
780 fl_show_message("",_("Spellchecking completed!"),
784 fl_show_message(_("The ispell-process has died for some reason.\n"
785 "Maybe it has been killed."), "", "");
792 //void sigchldhandler(int sig)
793 void sigchldhandler(pid_t pid, int *status)
798 //if (waitpid(isp_pid,&status,WNOHANG)==isp_pid) {
799 if (pid == isp_pid) {
801 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
802 to nonblocking so we can
805 //sigchldchecker(sig);
806 sigchldchecker(pid, status);