3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright (C) 1995 Matthias Ettrich
8 * Copyright (C) 1995-1998 The LyX Team
10 *======================================================
22 #include <sys/types.h>
24 #include FORMS_H_LOCATION
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
31 # include <sys/time.h>
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
43 #include "spellchecker.h"
47 #include "BufferView.h"
49 #include "lyx_gui_misc.h"
52 extern BufferView *current_view;
54 // $Id: spellchecker.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $
56 #if !defined(lint) && !defined(WITH_WARNINGS)
57 static char vcid[] = "$Id: spellchecker.C,v 1.1 1999/09/27 18:44:38 larsbj Exp $";
60 // Spellchecker status
70 static bool RunSpellChecker(LString const &);
72 static FILE *in, *out; /* streams to communicate with ispell */
73 pid_t isp_pid = -1; // pid for the `ispell' process. Also used (RO) in
76 // the true spell checker program being used
77 enum ActualSpellChecker {ASC_ISPELL, ASC_ASPELL};
78 static ActualSpellChecker actual_spell_checker;
82 static FD_form_spell_options *fd_form_spell_options = NULL;
83 FD_form_spell_check *fd_form_spell_check = NULL;
85 //void sigchldhandler(int sig);
86 void sigchldhandler(pid_t pid, int *status);
88 //extern void sigchldchecker(int sig);
89 extern void sigchldchecker(pid_t pid, int *status);
99 misses = (char**)NULL;
102 if (misses) delete[] misses;
107 /***** Spellchecker options *****/
109 // Rewritten to use ordinary LyX xforms loop and OK, Apply and Cancel set,
110 // now also LString. Amazing, eh? (Asger)
112 // Set (sane) values in form to current spellchecker options
113 void SpellOptionsUpdate()
115 // Alternate language
116 if (lyxrc->isp_alt_lang.empty()) {
117 lyxrc->isp_use_alt_lang = false;
119 fl_set_input(fd_form_spell_options->altlang_input,
120 lyxrc->isp_alt_lang.c_str());
122 if (lyxrc->isp_use_alt_lang) {
123 fl_set_button(fd_form_spell_options->buflang,0);
124 fl_set_button(fd_form_spell_options->altlang,1);
126 fl_set_button(fd_form_spell_options->buflang,1);
127 fl_set_button(fd_form_spell_options->altlang,0);
130 // Personal dictionary
131 if (lyxrc->isp_pers_dict.empty()) {
132 lyxrc->isp_use_pers_dict = false;
134 fl_set_input(fd_form_spell_options->perdict_input,
135 lyxrc->isp_pers_dict.c_str());
137 fl_set_button(fd_form_spell_options->perdict,
138 lyxrc->isp_use_pers_dict ? 1:0);
141 if (lyxrc->isp_esc_chars.empty()) {
142 lyxrc->isp_use_esc_chars = false;
144 fl_set_input(fd_form_spell_options->esc_chars_input,
145 lyxrc->isp_esc_chars.c_str());
147 fl_set_button(fd_form_spell_options->esc_chars,
148 lyxrc->isp_use_esc_chars ? 1:0);
151 fl_set_button(fd_form_spell_options->compounds,
152 lyxrc->isp_accept_compound ? 1 : 0);
153 fl_set_button(fd_form_spell_options->inpenc,
154 lyxrc->isp_use_input_encoding ? 1 : 0);
157 // Update spellchecker options
158 void SpellOptionsApplyCB(FL_OBJECT *, long)
160 // Build new status from form data
161 lyxrc->isp_use_alt_lang =
162 fl_get_button(fd_form_spell_options->altlang);
163 lyxrc->isp_use_pers_dict =
164 fl_get_button(fd_form_spell_options->perdict);
165 lyxrc->isp_accept_compound =
166 fl_get_button(fd_form_spell_options->compounds);
167 lyxrc->isp_use_esc_chars =
168 fl_get_button(fd_form_spell_options->esc_chars);
169 lyxrc->isp_use_input_encoding =
170 fl_get_button(fd_form_spell_options->inpenc);
172 // Update strings with data from input fields
173 lyxrc->isp_alt_lang =
174 fl_get_input(fd_form_spell_options->altlang_input);
175 lyxrc->isp_pers_dict =
176 fl_get_input(fd_form_spell_options->perdict_input);
177 lyxrc->isp_esc_chars =
178 fl_get_input(fd_form_spell_options->esc_chars_input);
181 SpellOptionsUpdate();
185 void SpellOptionsCancelCB(FL_OBJECT *, long)
187 fl_hide_form(fd_form_spell_options->form_spell_options);
191 void SpellOptionsOKCB(FL_OBJECT * ob, long data)
193 SpellOptionsApplyCB(ob, data);
194 SpellOptionsCancelCB(ob, data);
198 // Show spellchecker options form
199 void SpellCheckerOptions()
201 // Create form if nescessary
202 if (fd_form_spell_options == NULL) {
203 fd_form_spell_options = create_form_form_spell_options();
204 // Make sure pressing the close box does not kill LyX. (RvdK)
205 fl_set_form_atclose(fd_form_spell_options->form_spell_options,
206 CancelCloseBoxCB, NULL);
209 // Update form to current options
210 SpellOptionsUpdate();
212 // Focus in alternate language field
213 fl_set_focus_object(fd_form_spell_options->form_spell_options,
214 fd_form_spell_options->altlang_input);
217 if (fd_form_spell_options->form_spell_options->visible) {
218 fl_raise_form(fd_form_spell_options->form_spell_options);
220 fl_show_form(fd_form_spell_options->form_spell_options,
221 FL_PLACE_MOUSE,FL_FULLBORDER,
222 _("Spellchecker Options"));
227 /***** Spellchecker *****/
229 // Could also use a clean up. (Asger Alstrup)
232 void create_ispell_pipe(LString const & lang)
234 static char o_buf[BUFSIZ]; // jc: it could be smaller
235 int pipein[2], pipeout[2];
241 if(pipe(pipein)==-1 || pipe(pipeout)==-1) {
242 fprintf(stderr,"LyX: Can't create pipe for spellchecker!");
246 if ((out = fdopen(pipein[1], "w"))==NULL) {
247 fprintf(stderr,"LyX: Can't create stream for pipe for spellchecker!");
251 if ((in = fdopen(pipeout[0], "r"))==NULL) {
252 fprintf(stderr,"LyX: Can't create stream for pipe for spellchecker!");
256 setvbuf(out, o_buf, _IOLBF, BUFSIZ);
263 fprintf(stderr,"LyX: Can't create child process for spellchecker!");
269 dup2(pipein[0], STDIN_FILENO);
270 dup2(pipeout[1], STDOUT_FILENO);
277 argv[argc++] = lyxrc->isp_command.copy();
278 argv[argc++] = LString("-a").copy(); // "Pipe" mode
280 if (lang != "default") {
281 argv[argc++] = LString("-d").copy(); // Dictionary file
282 argv[argc++] = lang.copy();
285 if (lyxrc->isp_accept_compound)
286 // Consider run-together words as legal compounds
287 argv[argc++] = LString("-C").copy();
289 // Report run-together words with
290 // missing blanks as errors
291 argv[argc++] = LString("-B").copy();
293 if (lyxrc->isp_use_esc_chars) {
294 // Specify additional characters that
295 // can be part of a word
296 argv[argc++] = LString("-w").copy();
297 // Put the escape chars in ""s
298 argv[argc++] = (LString("\"") + lyxrc->isp_esc_chars +
299 LString("\"")).copy();
301 if (lyxrc->isp_use_pers_dict) {
302 // Specify an alternate personal dictionary
303 argv[argc++] = LString("-p").copy();
304 argv[argc++] = lyxrc->isp_pers_dict.copy();
306 if (lyxrc->isp_use_input_encoding &&
307 current_view->currentBuffer()->params.inputenc != "default") {
308 argv[argc++] = LString("-T").copy(); // Input encoding
309 argv[argc++] = current_view->currentBuffer()->params.inputenc.copy();
314 execvp("ispell", (char * const *) argv);
316 // free the memory used by LString::copy in the
318 for (int i=0; i < argc -1; i++)
321 fprintf(stderr, "LyX: Failed to start ispell!\n");
325 /* Parent process: Read ispells identification message */
326 // Hmm...what are we using this id msg for? Nothing? (Lgb)
327 // Actually I used it to tell if it's truly Ispell or if it's
328 // aspell -- (kevinatk@home.com)
331 #warning verify that this works.
337 FD_SET(pipeout[0], &infds);
338 tv.tv_sec = 15; // fifteen second timeout. Probably too much,
339 // but it can't really hurt.
342 // Configure provides us with macros which are supposed to do
343 // the right typecast.
344 retval = select(SELECT_TYPE_ARG1 (pipeout[0]+1),
345 SELECT_TYPE_ARG234 (&infds),
348 SELECT_TYPE_ARG5 (&tv));
351 // Ok, do the reading. We don't have to FD_ISSET since
352 // there is only one fd in infds.
353 fgets(buf, 2048, in);
355 // determine if the spell checker is really Aspell
356 if (strstr(buf, "Aspell"))
357 actual_spell_checker = ASC_ASPELL;
359 actual_spell_checker = ASC_ISPELL;
361 } else if (retval == 0) {
362 // timeout. Give nice message to user.
363 fprintf(stderr, "Ispell read timed out, what now?\n");
365 #warning Is this the correct thing to do?
368 close(pipeout[0]); close(pipeout[1]);
369 close(pipein[0]); close(pipein[1]);
372 // Select returned error
373 fprintf(stderr, "Select on ispell returned error, what now?\n");
378 // Send word to ispell and get reply
380 isp_result *ispell_check_word(char *word)
382 //Please rewrite to use LString.
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 (isp_pid == -1) return (isp_result *) NULL;
396 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 result->string = buf;
419 char * nb = result->string.copy(); // This leaks.
420 p = strpbrk(nb+2, " ");
421 sscanf(p, "%d", &count); // Get near misses count
422 result->count = count;
423 if (count) result->misses = new char*[count];
424 p = strpbrk(nb, ":");
426 for (i = 0; i<count; i++) {
427 result->misses[i] = p;
428 p = strpbrk(p, ",\n");
434 default: // This shouldn't happend, but you know Murphy
435 result->flag = ISP_UNKNOWN;
439 if (result->flag!=ISP_IGNORE) {
440 while (*buf!='\n') fgets(buf, 255, in); /* wait for ispell to finish */
447 inline void ispell_terminate()
449 // Note: If you decide to optimize this out when it is not
450 // needed please note that when Aspell is used this command
451 // is also needed to save the replacement dictionary.
452 // -- Kevin Atkinson (kevinatk@home.com)
454 fputs("#\n", out); // Save personal dictionary
462 inline void ispell_terse_mode()
464 fputs("!\n", out); // Set terse mode (silently accept correct words)
469 inline void ispell_insert_word(char const *word)
471 fputc('*', out); // Insert word in personal dictionary
478 inline void ispell_accept_word(char const *word)
480 fputc('@', out); // Accept in this session
486 inline void ispell_store_replacement(const char *mis, LString const & cor) {
487 if(actual_spell_checker == ASC_ASPELL) {
491 fputs(cor.c_str(), out);
497 void ShowSpellChecker()
502 // Exit if we don't have a document open
503 if (!current_view->getScreen())
506 if (fd_form_spell_check == NULL) {
507 fd_form_spell_check = create_form_form_spell_check();
508 // Make sure pressing the close box does not kill LyX. (RvdK)
509 fl_set_form_atclose(fd_form_spell_check->form_spell_check, IgnoreCloseBoxCB, NULL);
513 fl_set_slider_value(fd_form_spell_check->slider, 0);
514 fl_set_slider_bounds(fd_form_spell_check->slider, 0, 100);
515 fl_set_object_label(fd_form_spell_check->text, "");
516 fl_set_input(fd_form_spell_check->input, "");
517 fl_clear_browser(fd_form_spell_check->browser);
520 if (fd_form_spell_check->form_spell_check->visible) {
521 fl_raise_form(fd_form_spell_check->form_spell_check);
523 fl_show_form(fd_form_spell_check->form_spell_check,
524 FL_PLACE_MOUSE,FL_FULLBORDER,
527 fl_deactivate_object(fd_form_spell_check->slider);
529 // deactivate insert, accept, replace, and stop
530 fl_deactivate_object(fd_form_spell_check->insert);
531 fl_deactivate_object(fd_form_spell_check->accept);
532 fl_deactivate_object(fd_form_spell_check->ignore);
533 fl_deactivate_object(fd_form_spell_check->replace);
534 fl_deactivate_object(fd_form_spell_check->stop);
535 fl_deactivate_object(fd_form_spell_check->input);
536 fl_deactivate_object(fd_form_spell_check->browser);
537 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
538 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
539 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
540 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
541 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
542 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
543 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
547 if (obj == fd_form_spell_check->options){
548 SpellCheckerOptions();
550 if (obj == fd_form_spell_check->start){
551 // activate insert, accept, and stop
552 fl_activate_object(fd_form_spell_check->insert);
553 fl_activate_object(fd_form_spell_check->accept);
554 fl_activate_object(fd_form_spell_check->ignore);
555 fl_activate_object(fd_form_spell_check->stop);
556 fl_activate_object(fd_form_spell_check->input);
557 fl_activate_object(fd_form_spell_check->browser);
558 fl_set_object_lcol(fd_form_spell_check->insert, FL_BLACK);
559 fl_set_object_lcol(fd_form_spell_check->accept, FL_BLACK);
560 fl_set_object_lcol(fd_form_spell_check->ignore, FL_BLACK);
561 fl_set_object_lcol(fd_form_spell_check->stop, FL_BLACK);
562 fl_set_object_lcol(fd_form_spell_check->input, FL_BLACK);
563 fl_set_object_lcol(fd_form_spell_check->browser, FL_BLACK);
564 // activate replace only if the file is not read-only
565 if (!current_view->currentBuffer()->isReadonly()) {
566 fl_activate_object(fd_form_spell_check->replace);
567 fl_set_object_lcol(fd_form_spell_check->replace, FL_BLACK);
570 // deactivate options and start
571 fl_deactivate_object(fd_form_spell_check->options);
572 fl_deactivate_object(fd_form_spell_check->start);
573 fl_set_object_lcol(fd_form_spell_check->options, FL_INACTIVE);
574 fl_set_object_lcol(fd_form_spell_check->start, FL_INACTIVE);
576 ret = RunSpellChecker(current_view->currentBuffer()->GetLanguage());
578 // deactivate insert, accept, replace, and stop
579 fl_deactivate_object(fd_form_spell_check->insert);
580 fl_deactivate_object(fd_form_spell_check->accept);
581 fl_deactivate_object(fd_form_spell_check->ignore);
582 fl_deactivate_object(fd_form_spell_check->replace);
583 fl_deactivate_object(fd_form_spell_check->stop);
584 fl_deactivate_object(fd_form_spell_check->input);
585 fl_deactivate_object(fd_form_spell_check->browser);
586 fl_set_object_lcol(fd_form_spell_check->insert, FL_INACTIVE);
587 fl_set_object_lcol(fd_form_spell_check->accept, FL_INACTIVE);
588 fl_set_object_lcol(fd_form_spell_check->ignore, FL_INACTIVE);
589 fl_set_object_lcol(fd_form_spell_check->replace, FL_INACTIVE);
590 fl_set_object_lcol(fd_form_spell_check->stop, FL_INACTIVE);
591 fl_set_object_lcol(fd_form_spell_check->input, FL_INACTIVE);
592 fl_set_object_lcol(fd_form_spell_check->browser, FL_INACTIVE);
594 // activate options and start
595 fl_activate_object(fd_form_spell_check->options);
596 fl_activate_object(fd_form_spell_check->start);
597 fl_set_object_lcol(fd_form_spell_check->options, FL_BLACK);
598 fl_set_object_lcol(fd_form_spell_check->start, FL_BLACK);
600 // if RunSpellChecker returns false quit spellchecker
603 if (obj == fd_form_spell_check->done) break;
605 fl_hide_form(fd_form_spell_check->form_spell_check);
611 // Perform an ispell session
613 bool RunSpellChecker(LString const & lang)
617 int i, oldval, clickline, newvalue;
620 unsigned int word_count = 0;
622 LString tmp = (lyxrc->isp_use_alt_lang) ? lyxrc->isp_alt_lang:lang;
624 oldval = 0; /* used for updating slider only when needed */
627 /* create ispell process */
628 create_ispell_pipe(tmp);
633 "The ispell-process has died for some reason. *One* possible reason\n"
634 "could be that you do not have a dictionary file\n"
635 "for the language of this document installed.\n"
636 "Check /usr/lib/ispell or set another\n"
637 "dictionary in the Spellchecker Options menu."), "", "");
642 // Put ispell in terse mode to improve speed
646 word = NextWord(newval);
647 if (word==NULL) break;
650 // Update slider if and only if value has changed
651 newvalue = int(100.0*newval);
652 if(newvalue!=oldval) {
654 fl_set_slider_value(fd_form_spell_check->slider, oldval);
657 result = ispell_check_word(word);
663 obj = fl_check_forms();
664 if (obj == fd_form_spell_check->stop) {
670 if (obj == fd_form_spell_check->done) {
677 switch (result->flag) {
681 fl_set_object_label(fd_form_spell_check->text, word);
682 fl_set_input(fd_form_spell_check->input, word);
683 fl_clear_browser(fd_form_spell_check->browser);
684 for (i=0; i<result->count; i++) {
685 fl_add_browser_line(fd_form_spell_check->browser, result->misses[i]);
691 if (obj==fd_form_spell_check->insert) {
692 ispell_insert_word(word);
695 if (obj==fd_form_spell_check->accept) {
696 ispell_accept_word(word);
699 if (obj==fd_form_spell_check->ignore) {
702 if (obj==fd_form_spell_check->replace ||
703 obj==fd_form_spell_check->input) {
704 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
705 ReplaceWord(fl_get_input(fd_form_spell_check->input));
708 if (obj==fd_form_spell_check->browser) {
709 // implements double click in the browser window.
710 // sent to lyx@via by Mark Burton <mark@cbl.leeds.ac.uk>
712 fl_get_browser(fd_form_spell_check->browser)) {
713 ispell_store_replacement(word, fl_get_input(fd_form_spell_check->input));
714 ReplaceWord(fl_get_input(fd_form_spell_check->input));
717 clickline = fl_get_browser(fd_form_spell_check->browser);
718 fl_set_input(fd_form_spell_check->input,
719 fl_get_browser_line(fd_form_spell_check->browser,
720 fl_get_browser(fd_form_spell_check->browser)));
723 if (obj==fd_form_spell_check->stop) {
730 if (obj==fd_form_spell_check->done) {
746 word_msg += int(word_count);
747 if (word_count != 1) {
748 word_msg += _(" words checked.");
750 word_msg += _(" word checked.");
752 fl_show_message("",_("Spellchecking completed!"),
756 fl_show_message(_("The ispell-process has died for some reason.\n"
757 "Maybe it has been killed."), "", "");
764 //void sigchldhandler(int sig)
765 void sigchldhandler(pid_t pid, int *status)
770 //if (waitpid(isp_pid,&status,WNOHANG)==isp_pid) {
771 if (pid == isp_pid) {
773 fcntl(isp_fd, F_SETFL, O_NONBLOCK); /* set the file descriptor
774 to nonblocking so we can
777 //sigchldchecker(sig);
778 sigchldchecker(pid, status);