1
/* Bluefish HTML Editor
2
* spell.c - Check Spelling
4
* Copyright (C)2000 Pablo De Napoli (for this module)
5
* Minor updates Copyright (C)2001 Olivier Sessink
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
* Note: some code has been taken from Lyx (wich is also covered by the
22
* GNU Public Licence).
25
/* For the select function */
33
/*********************************************************************
34
* The spellchecker and it's GUI run like this:
38
* spc_start_button_clicked_lcb
50
**********************************************************************/
53
/* Print debug messages using standard error */
54
/* This is needed when the stdin is redirected */
55
#define MY_DEBUG_MSG g_printerr
65
#include "char_table.h"
66
#include "interface.h"
76
#include <sys/types.h>
79
#if TIME_WITH_SYS_TIME
80
# include <sys/time.h>
84
# include <sys/time.h>
90
#ifdef HAVE_SYS_SELECT_H
91
#include <sys/select.h>
94
/* Spellchecker status */
104
static FILE *in, *out; /* streams to communicate with ispell */
105
pid_t isp_pid = -1; /* pid for the `ispell' process */
106
GdkColor misspelled_color; /* tb */
109
/* void sigchldhandler(int sig); */
110
void sigchldhandler(pid_t pid, int *status);
112
/* extern void sigchldchecker(int sig); */
113
extern void sigchldchecker(pid_t pid, int *status);
121
/* message from the spell checker GUI */
124
/* flag to know if we reach the end of text */
125
static gboolean end_of_text;
127
#define SPC_CHAR_LENGTH 20
130
returns the next char in the text.
131
set flags end_of_text
132
converts special characters like á to iso characters
135
static gchar get_next_char()
136
/* FIXME: a more eficient implementation? */
137
/* use an HTML parser like gnome-xml ? */
139
gint current_position, old_position,i;
141
gchar token[SPC_CHAR_LENGTH];
142
DEBUG_MSG("getting next char\n");
143
current_position = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
144
DEBUG_MSG("current_position = %i\n",current_position);
145
c = GTK_TEXT_INDEX(GTK_TEXT(CURRENT_DOCUMENT_TEXT) ,current_position);
146
/* macro to get a single character */
147
if (current_position < gtk_text_get_length(GTK_TEXT(CURRENT_DOCUMENT_TEXT)))
150
gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT),current_position);
154
DEBUG_MSG("Character & detected\n");
155
old_position = current_position;
158
for (i=1;i<SPC_CHAR_LENGTH-2;i++)
164
else if (end_of_text)
169
DEBUG_MSG("Converting special character = \"%s\" \n",token);
170
c = convert_from_html_chars (token,ANY_CHAR_SET);
173
current_position = old_position;
175
/* FIXME: mark for gettext / beter dialog ? */
176
g_printerr(_("Invalid especial character! %s\n"),token);
182
DEBUG_MSG("next_char ='%c'\n",c);
184
} /* end get_next_char */
195
#define WORD_BUFFER_SIZE 20
197
inline static Tword* new_word (Tword* word)
199
word=(Tword*) g_malloc(sizeof(Tword));
201
word->space_length = WORD_BUFFER_SIZE;
202
word->text = g_malloc((word->space_length)*sizeof(gchar));
206
inline void free_word (Tword* word)
208
DEBUG_MSG("Free word \"%s\" \n",word->text);
213
#define SPECIAL_CHAR_LENGTH 10
215
static void store_char_in_word (Tword* word,gchar c)
217
DEBUG_MSG("indice =\n %i\n",word->index);
218
/* enlarge the buffer if needed */
219
if (word->index>=word->space_length-1)
221
DEBUG_MSG("enlarging word buffer\n");
222
word->space_length = word->space_length + WORD_BUFFER_SIZE;
223
DEBUG_MSG("space length=%i\n", word->space_length);
224
word->text = g_realloc (word->text,word->space_length*sizeof(gchar));
226
(word->text)[word->index] =c;
230
inline gboolean is_empty_word(Tword* word)
232
return((word->index)==0);
235
/* Non alphabetical characters are ignored */
236
/* and also the text between < > (asumed to be html tags) */
237
/* in PHP code you can have different numbers of > and <, so this should be extended!
238
like in if (a < b) { */
239
/* we want to be sure that only valid characters are pased to ispell */
241
static Tword* get_next_word(void)
245
DEBUG_MSG("getting next word\n");
246
next_word = new_word(next_word);
247
next_word->begin = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
251
DEBUG_MSG("Not end of text\n");
253
if (isalpha((char)c)||isalpha_iso((char)c))
254
store_char_in_word(next_word,c);
255
/* FIXME: accept iso special characters! */
258
DEBUG_MSG("Ignoring non alphabethical character\n");
259
break; /* if it is a non alphabetical character exit the loop */
262
store_char_in_word(next_word,'\0');
263
if (is_empty_word(next_word))
265
/* at the end of text , do not substract 1 */
266
next_word->end = gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT))-(!end_of_text);
267
DEBUG_MSG("next word= \"%s\" \n",next_word-> text);
268
/* ignore html tags */
271
DEBUG_MSG("Ignoring html tag\n");
272
while (((c=get_next_char())!='>') && (!end_of_text));
279
void create_ispell_pipe()
281
static char o_buf[BUFSIZ];
282
int pipein[2], pipeout[2];
292
MY_DEBUG_MSG("Creating ispell pipe ...\n");
296
if(pipe(pipein)==-1 || pipe(pipeout)==-1) {
297
g_printerr(_("Bluefish: Can't create pipe for spellchecker!"));
301
if ((out = fdopen(pipein[1], "w"))==NULL) {
302
g_printerr(_("Bluefish: Can't create stream for pipe for spellchecker!"));
306
if ((in = fdopen(pipeout[0], "r"))==NULL) {
307
g_printerr(_("Bluefish: Can't create stream for pipe for spellchecker!"));
311
setvbuf(out, o_buf, _IOLBF, BUFSIZ);
318
g_printerr(_("Bluefish: Can't create child process for spellchecker!"));
325
/* NOTE: Don't use DEBUG_MSG in this part since the output is redirected! */
328
MY_DEBUG_MSG ("Spell-checker child process \n");
330
dup2(pipein[0], STDIN_FILENO);
331
dup2(pipeout[1], STDOUT_FILENO);
338
argv[argc++] = main_v->props.cfg_spc_cline ;
339
argv[argc++] = g_strdup("-a"); /* "Pipe" mode */
341
if ((g_strcasecmp(main_v -> props.cfg_spc_lang, "default")!=0) &&
342
(*(main_v -> props.cfg_spc_lang)!='\0'))
344
if (main_v ->props.cfg_spc_lang != NULL)
346
argv[argc++] = g_strdup("-d"); /* Dictionary file */
347
argv[argc++] = main_v -> props.cfg_spc_lang;
351
if (main_v-> props.spc_accept_compound)
352
/* Consider run-together words as legal compounds */
353
argv[argc++] = g_strdup("-C");
355
/* Report run-together words with
356
missing blanks as errors */
357
argv[argc++] = g_strdup("-B");
359
if (main_v->props.spc_use_esc_chars) {
360
/* Specify additional characters that
361
can be part of a word */
362
if (main_v->props.spc_esc_chars !=NULL) {
363
argv[argc++] = g_strdup("-w");
364
/* Put the escape chars in ""s */
365
argv[argc++] = g_strconcat("\"",main_v->props.spc_esc_chars
369
if (main_v->props.spc_use_pers_dict) {
370
/* Specify an alternate personal dictionary */
371
if (main_v->props.spc_pers_dict != NULL)
373
argv[argc++] = g_strdup("-p");
374
argv[argc++] = main_v->props.spc_pers_dict;
378
if (main_v->props.spc_use_input_encoding) {
379
if (main_v->props.spc_input_encoding != NULL) {
380
argv[argc++] = g_strdup("-T"); /* Input encoding */
381
argv[argc++] = main_v->props.spc_input_encoding;
387
MY_DEBUG_MSG("Executing:\n");
388
for (i=0;i<argc-1;i++)
389
MY_DEBUG_MSG("%s ",argv[i]);
392
execvp(main_v->props.cfg_spc_cline , (char * const *) argv);
394
/* free the memory used */
395
for (i=0; i < argc -1; i++)
398
g_printerr(_("Bluefish: Failed to start ispell!\n"));
402
/* Parent process: Read ispells identification message
403
Hmm...what are we using this id msg for? Nothing? (Lgb)
404
Actually I used it to tell if it's truly Ispell or if it's
405
aspell -- (kevinatk@home.com) */
408
#warning verify that this works.
412
FD_SET(pipeout[0], &infds);
413
tv.tv_sec = 15; /* fifteen second timeout. Probably too much,
414
but it can't really hurt. */
417
/* Configure provides us with macros which are supposed to do
418
the right typecast. */
420
retval = select(( SELECT_TYPE_ARG1 ) (pipeout[0]+1),
421
SELECT_TYPE_ARG234 (&infds),
424
SELECT_TYPE_ARG5 (&tv));
427
/* Ok, do the reading. We don't have to FD_ISSET since
428
there is only one fd in infds. */
429
fgets(buf, 2048, in);
430
MY_DEBUG_MSG ("\nIspell pipe created\n");
431
g_print ("%s\n",buf);
433
} else if (retval == 0) {
434
/* timeout. Give nice message to user. */
435
g_printerr("Ispell read timed out, what now?\n");
437
close(pipeout[0]); close(pipeout[1]);
438
close(pipein[0]); close(pipein[1]);
441
/* Select returned error */
442
g_printerr(_("Select on ispell returned error, what now?\n"));
447
static inline void ispell_terse_mode(void)
449
fputs("!\n", out); /* Set terse mode (silently accept correct words) */
453
static inline void ispell_insert_word(char const *word)
455
fputc('*', out); /* Insert word in personal dictionary */
462
inline void ispell_accept_word(char const *word)
464
fputc('@', out); /* Accept in this session */
469
/* Send word to ispell and get reply */
470
static isp_result * ispell_check_word (gchar *word)
473
gchar buf[1024], *p, *np;
481
/* Coment from the original author:
482
I think we have to check if ispell is still alive here */
484
if (isp_pid == -1) return (isp_result *) NULL;
486
DEBUG_MSG("Sending word \"%s\" to ispell\n",word);
492
DEBUG_MSG("Message from ispell=\"%s\"\n",buf);
494
result = g_malloc(sizeof(isp_result));
497
case '*': /* Word found */
498
result->flag = ISP_OK;
499
DEBUG_MSG("Ispell: Word found\n");
501
case '+': /* Word found through affix removal */
502
result->flag = ISP_ROOT;
503
DEBUG_MSG("Ispell: Word found through affix removal\n");
505
case '-': /* Word found through compound formation */
506
result->flag = ISP_COMPOUNDWORD;
507
DEBUG_MSG("Ispell: Word found through compound formation\n");
509
case '\n': /* Number or when in terse mode: no problems */
510
result->flag = ISP_IGNORE;
511
DEBUG_MSG("Ispell: Ignore Word\n");
513
case '#': /* Not found, no near misses and guesses */
514
result->flag = ISP_UNKNOWN;
515
DEBUG_MSG("Ispell: Unknown Word\n");
517
case '?': /* Not found, and no near misses, but guesses (guesses are ignored) */
518
case '&': /* Not found, but we have near misses */
520
result->flag = ISP_MISSED;
521
DEBUG_MSG("Ispell: Near Misses\n");
522
/* FIXME: why duplicate buffer ?*/
524
p = strpbrk(nb+2, " ");
525
sscanf(p, "%d", &count); /* Get near misses count */
526
result->count = count;
529
p = strpbrk(nb, ":");
532
/* remove the last '\n' from ispell output */
533
DEBUG_MSG("Removing end-of-line \n");
535
while (*np!='\0')np++;
539
DEBUG_MSG ("misses list \"%s\" \n",p);
541
result->misses = g_strsplit(p,", ",count);
545
DEBUG_MSG("near_miss = \"%s\" \n", result-> misses[i]);
552
default: /* This shouldn't happend, but you know Murphy */
553
result->flag = ISP_UNKNOWN;
554
DEBUG_MSG("Ispell: Unknown\n");
558
if (result->flag!=ISP_IGNORE) {
559
while (*buf!='\n') fgets(buf, 255, in); /* wait for ispell to finish */
565
static inline void select_word (Tword* word)
567
doc_unbind_signals(main_v->current_document);
568
gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin);
569
gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);/* tb */
570
gtk_text_insert(GTK_TEXT(CURRENT_DOCUMENT_TEXT), 0, &misspelled_color, 0,word->text, -1);/* tb */
571
gtk_text_thaw(GTK_TEXT(CURRENT_DOCUMENT_TEXT));/* tb */
572
/* gtk_editable_select_region (GTK_EDITABLE(CURRENT_DOCUMENT_TEXT),word->begin,word->end); *//* tb */
573
doc_bind_signals(main_v->current_document);
576
/*for gtk_text_insert*/
577
#define ALL_THE_STRING -1
578
static void replace_word_with (Tword *word,gchar* replace_with)
580
replace_text(replace_with, word->begin, word->end);
582
DEBUG_MSG("Deleting word \"%s\" \n",word->text);
583
gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin);
584
gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);
585
DEBUG_MSG("inserting replace text =\"%s\" \n",replace_with);
586
gtk_editable_insert_text(GTK_EDITABLE(CURRENT_DOCUMENT_TEXT), replace_with,strlen(replace_with),&(word->begin));
588
/* FIXME: convert iso characters to html ? */
592
*change slected word color back to original color
594
static void change_word_color(Tword * word) {
595
doc_unbind_signals(main_v->current_document);
596
gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->begin);
597
gtk_text_forward_delete(GTK_TEXT(CURRENT_DOCUMENT_TEXT), word->end - word->begin);
598
DEBUG_MSG("changing text color");
599
gtk_editable_insert_text(GTK_EDITABLE(CURRENT_DOCUMENT_TEXT), word->text, strlen(word->text),&(word->begin));
600
doc_bind_signals(main_v->current_document);
603
static void correct_word(Tword* word)
604
/* ask the user how to correct a word, and do it */
606
gboolean word_corrected = 0;
609
spc_message = SPC_NONE;
611
while (gtk_events_pending())
612
gtk_main_iteration();
615
case SPC_INSERT: ispell_insert_word(word->text);
616
change_word_color(word);
619
case SPC_ACCEPT: ispell_accept_word(word->text);
620
change_word_color(word);
623
case SPC_IGNORE: change_word_color(word);
626
case SPC_REPLACE: replace_word_with (word,gtk_entry_get_text(GTK_ENTRY(spc_gui.replace_entry)));
629
case SPC_CLOSE: DEBUG_MSG("close message\n");
630
change_word_color(word);
631
word_corrected = 1; /* to exit the while loop */
635
} while (!word_corrected);
636
/* gtk_editable_delete_selection (GTK_EDITABLE(CURRENT_DOCUMENT_TEXT));*/
637
DEBUG_MSG ("Word corrected\n");
640
static void check_word (Tword* word)
643
gchar* converted_string;
646
/* FIXME: Is this really needed ? */
647
while (gtk_events_pending())
648
gtk_main_iteration();
650
result = ispell_check_word (word->text);
651
switch (result->flag) {
652
case ISP_MISSED: /* creates near misses list */
653
DEBUG_MSG("Creating misses list\n");
654
for (i=0; i<result->count; i++)
656
if (main_v->props.spc_output_html_chars)
658
converted_string = convert_string_iso_to_html(result->misses[i]);
659
g_free(result->misses[i]);
660
result->misses[i] = converted_string;
662
DEBUG_MSG("Adding \"%s\" to the near_misses_list\n",result->misses[i]);
663
gtk_clist_append(GTK_CLIST(spc_gui.near_misses_clist),&(result->misses[i]));
665
case ISP_UNKNOWN: gtk_entry_set_text (GTK_ENTRY(spc_gui.word_entry),word->text);
666
gtk_entry_set_text (GTK_ENTRY(spc_gui.replace_entry),word->text);
668
DEBUG_MSG("spc_message = %i\n",spc_message);
672
switch (result->flag) {
673
case ISP_MISSED: DEBUG_MSG("free & clean near_misses_lists ...\n");
674
gtk_clist_clear(GTK_CLIST(spc_gui.near_misses_clist));
675
for(i=0;i<result->count;i++)
676
g_free(result->misses[i]);
677
DEBUG_MSG("free & cleaning near_misses_list done\n");
679
case ISP_UNKNOWN: gtk_entry_set_text(GTK_ENTRY(spc_gui.word_entry),"");
680
gtk_entry_set_text (GTK_ENTRY(spc_gui.replace_entry),"");
681
DEBUG_MSG("word & replace entries cleaned\n");
685
if (spc_message==SPC_CLOSE)
686
DEBUG_MSG("Passing SPC_CLOSE message\n");
688
DEBUG_MSG("word checked\n");
689
} /* end check_word */
692
inline static void update_progress_bar(void) {
693
gtk_progress_bar_update(GTK_PROGRESS_BAR(spc_gui.progress_bar),(gfloat) gtk_text_get_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT))/ (gfloat) gtk_text_get_length(GTK_TEXT(CURRENT_DOCUMENT_TEXT)));
696
void ispell_terminate()
698
if (isp_pid != -1) /* checks if ispell is running */
700
fputs("#\n", out); /* Save personal dictionary */
703
kill(isp_pid,SIGTERM);
704
if (waitpid(isp_pid, (int *)0, WNOHANG) != 0) {
705
isp_pid = -1; /* ispell is not running.FIXME: Can we check this ? */
710
/* Perform an ispell session */
711
void run_spell_checker (void)
715
misspelled_color.red = 0xffff; /* tb */
716
misspelled_color.green = 0; /* tb */
717
misspelled_color.blue = 0; /* tb */
718
if (!gdk_color_alloc(main_v->colormap, &misspelled_color)) { /* tb */
719
g_error("couldn't allocate required colors"); /* tb */
721
gtk_text_freeze(GTK_TEXT(CURRENT_DOCUMENT_TEXT));
722
/* create ispell process */
724
create_ispell_pipe();
725
gtk_text_set_point(GTK_TEXT(CURRENT_DOCUMENT_TEXT),0);
726
end_of_text = 0; /* needed for get_next_word to work properly */
729
/* Put ispell in terse mode to improve speed */
733
word = get_next_word();
734
if (!is_empty_word(word))
736
spc_message = SPC_NONE;
739
update_progress_bar();
740
if (spc_message==SPC_CLOSE)
742
DEBUG_MSG("Close message received\n");
746
} while (!end_of_text);
750
error_dialog(_("Bluefish Spell Checker Error"),
752
"The ispell-process has died for some reason. *One* possible reason\n"
753
"could be that you do not have a dictionary file\n"
754
"for the language of this document installed.\n"
755
"Check /usr/lib/ispell or set another\n"
756
"dictionary in the Spellchecker Options menu."));
759
DEBUG_MSG("Removing grab\n");
760
gtk_grab_remove(spc_gui.window);
761
DEBUG_MSG("Removing status \n");
762
statusbar_remove(GINT_TO_POINTER(spc_gui.status_bar_count));
763
DEBUG_MSG("Destroying window\n");
764
gtk_widget_destroy(spc_gui.window);
766
} /* end run_spell_checker */
770
#endif /* WITH_SPC */