00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "intl.h"
00020 #include <gtk/gtk.h>
00021
00022 #include <sys/types.h>
00023 #ifndef __MINGW32__
00024 #include <sys/wait.h>
00025
00026 #if HAVE_POLL_H
00027 #include <sys/poll.h>
00028 #endif
00029 #endif
00030 #include <unistd.h>
00031 #include <stdio.h>
00032 #include <signal.h>
00033 #include <ctype.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036 #include <errno.h>
00037
00038
00039
00040
00041
00042
00043
00044 #define BUFSIZE 1024
00045
00046 #define MENUCOUNT 20
00047 #define BUGEMAIL "gtkspell-devel@lists.sourceforge.net"
00048
00049
00050
00051
00052 static pid_t spell_pid = -1;
00053 static int fd_write[2], fd_read[2];
00054 static int signal_set_up = 0;
00055
00056
00057 static GdkColor highlight = { 0, 255*256, 0, 0 };
00058
00059 static void entry_insert_cb(GtkText *gtktext,
00060 gchar *newtext, guint len, guint *ppos, gpointer d);
00061 static void set_up_signal();
00062
00063 int gtkspell_running() {
00064 return (spell_pid > 0);
00065 }
00066
00067 static void error_print(const char *fmt, ...) {
00068 va_list ap;
00069 va_start(ap, fmt);
00070 fprintf(stderr, "gtkspell: ");
00071 vfprintf(stderr, fmt, ap);
00072 va_end(ap);
00073 }
00074
00075
00076 static void writetext(char *text) {
00077 write(fd_write[1], text, strlen(text));
00078 }
00079 static int readpipe(char *buf, int bufsize) {
00080 int len;
00081 len = read(fd_read[0], buf, bufsize-1);
00082 if (len < 0) {
00083 error_print("read: %s\n", strerror(errno));
00084 return -1;
00085 } else if (len == 0) {
00086 error_print("pipe closed.\n");
00087 return -1;
00088 } else if (len == bufsize-1) {
00089 error_print("buffer overflowed?\n");
00090 }
00091
00092 buf[len] = 0;
00093 return len;
00094 }
00095 static int readline(char *buf) {
00096 return readpipe(buf, BUFSIZE);
00097 }
00098
00099 static int readresponse(char *buf) {
00100 int len;
00101 len = readpipe(buf, BUFSIZE);
00102
00103
00104
00105
00106 if (len >= 2 && (buf[len-1] != '\n' || buf[len-2] != '\n')) {
00107 len += readpipe(buf+len, BUFSIZE-len);
00108 }
00109
00110
00111 while (len > 0 && buf[len-1] == '\n')
00112 buf[--len] = 0;
00113
00114 return len;
00115 }
00116
00117
00118 void gtkspell_stop() {
00119 #ifndef __MINGW32__
00120 if (gtkspell_running()) {
00121 kill(spell_pid, SIGQUIT);
00122 spell_pid = -1;
00123 }
00124 #endif
00125 }
00126
00127 int gtkspell_start(char *path, char * args[]) {
00128 #ifndef __MINGW32__
00129 int fd_error[2];
00130 char buf[BUFSIZE];
00131
00132 if (gtkspell_running()) {
00133 error_print("gtkspell_start called while already running.\n");
00134 gtkspell_stop();
00135 }
00136
00137 if (!signal_set_up) {
00138 set_up_signal();
00139 signal_set_up = 1;
00140 }
00141
00142 pipe(fd_write);
00143 pipe(fd_read);
00144 pipe(fd_error);
00145
00146 spell_pid = fork();
00147 if (spell_pid < 0) {
00148 error_print("fork: %s\n", strerror(errno));
00149 return -1;
00150 } else if (spell_pid == 0) {
00151 dup2(fd_write[0], 0);
00152 dup2(fd_read[1], 1);
00153 dup2(fd_error[1], 2);
00154 close(fd_read[0]);
00155 close(fd_error[0]);
00156 close(fd_write[1]);
00157
00158 if (path == NULL) {
00159 if (execvp(args[0], args) < 0)
00160 error_print("execvp('%s'): %s\n", args[0], strerror(errno));
00161 } else {
00162 if (execv(path, args) < 0)
00163 error_print("execv('%s'): %s\n", path, strerror(errno));
00164 }
00165
00166
00167
00168 write(fd_read[1], "!", 1);
00169
00170 _exit(0);
00171 } else {
00172
00173
00174
00175
00176
00177
00178 #if HAVE_POLL_H
00179 struct pollfd fds[2];
00180
00181 fds[0].fd = fd_error[0];
00182 fds[0].events = POLLIN | POLLERR;
00183 fds[1].fd = fd_read[0];
00184 fds[1].events = POLLIN | POLLERR;
00185 if (poll(fds, 2, 2000) <= 0) {
00186
00187 error_print("Timed out waiting for spell command.\n");
00188 gtkspell_stop();
00189 return -1;
00190 }
00191
00192
00193 if (fds[0].revents) {
00194 error_print("Spell command printed on stderr -- probably failed.\n");
00195 gtkspell_stop();
00196 return -1;
00197 }
00198
00199 #endif
00200
00201 readline(buf);
00202
00203
00204
00205 if (buf[0] != '@') {
00206 gtkspell_stop();
00207 return -1;
00208 }
00209 }
00210
00211
00212
00213 sprintf(buf, "!\n");
00214 writetext(buf);
00215 #endif
00216 return 0;
00217 }
00218
00219 static GList* misspelled_suggest(char *word) {
00220 char buf[BUFSIZE];
00221 char *newword;
00222 GList *l = NULL;
00223 int count;
00224
00225 sprintf(buf, "^%s\n", word);
00226 writetext(buf);
00227 readresponse(buf);
00228
00229 switch (buf[0]) {
00230 case 0:
00231 return NULL;
00232 case '&':
00233
00234 strtok(buf, " ");
00235 newword = strtok(NULL, " ");
00236 l = g_list_append(l, g_strdup(newword));
00237 newword = strtok(NULL, " ");
00238 count = atoi(newword);
00239 strtok(NULL, " ");
00240
00241 while ((newword = strtok(NULL, ",")) != NULL) {
00242 int len = strlen(newword);
00243 if (newword[len-1] == ' ' || newword[len-1] == '\n')
00244 newword[len-1] = 0;
00245 if (count == 0) {
00246 g_list_append(l, NULL);
00247 }
00248
00249 l = g_list_append(l,
00250 g_strdup(newword[0] == ' ' ? newword+1 : newword));
00251
00252 count--;
00253 }
00254 return l;
00255
00256 case '#':
00257
00258 strtok(buf, " ");
00259 newword = strtok(NULL, " ");
00260 l = g_list_append(l, g_strdup(newword));
00261 return l;
00262 default:
00263 error_print("Unsupported spell command '%c'.\n"
00264 "This is a bug; mail " BUGEMAIL " about it.\n", buf[0]);
00265 }
00266 return NULL;
00267 }
00268
00269 static int misspelled_test(char *word) {
00270 char buf[BUFSIZE];
00271 sprintf(buf, "^%s\n", word);
00272 writetext(buf);
00273 readresponse(buf);
00274
00275 if (buf[0] == 0) {
00276 return 0;
00277 } else if (buf[0] == '&' || buf[0] == '#') {
00278 return 1;
00279 }
00280
00281 error_print("Unsupported spell command '%c'.\n"
00282 "This is a bug; mail " BUGEMAIL " about it.\n", buf[0]);
00283 return -1;
00284 }
00285
00286 static gboolean iswordsep(char c) {
00287 return !isalpha(c) && c != '\'';
00288 }
00289
00290 static gboolean get_word_from_pos(GtkText* gtktext, int pos, char* buf,
00291 int *pstart, int *pend) {
00292 gint start, end;
00293
00294 if (iswordsep(GTK_TEXT_INDEX(gtktext, pos))) return FALSE;
00295
00296 for (start = pos; start >= 0; --start) {
00297 if (iswordsep(GTK_TEXT_INDEX(gtktext, start))) break;
00298 }
00299 start++;
00300
00301 for (end = pos; end <= gtk_text_get_length(gtktext); end++) {
00302 if (iswordsep(GTK_TEXT_INDEX(gtktext, end))) break;
00303 }
00304
00305 if (buf) {
00306 for (pos = start; pos < end; pos++)
00307 buf[pos-start] = GTK_TEXT_INDEX(gtktext, pos);
00308 buf[pos-start] = 0;
00309 }
00310
00311 if (pstart) *pstart = start;
00312 if (pend) *pend = end;
00313
00314 return TRUE;
00315 }
00316
00317 static gboolean get_curword(GtkText* gtktext, char* buf,
00318 int *pstart, int *pend) {
00319 int pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
00320 return get_word_from_pos(gtktext, pos, buf, pstart, pend);
00321 }
00322
00323 static void change_color(GtkText *gtktext,
00324 int start, int end, GdkColor *color) {
00325 char *newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
00326 gtk_text_freeze(gtktext);
00327 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
00328 GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
00329
00330 gtk_text_set_point(gtktext, start);
00331
00332 if(newtext && end-start > 0)
00333 {
00334 gtk_text_forward_delete(gtktext, end-start);
00335 gtk_text_insert(gtktext, NULL, color, NULL, newtext, end-start);
00336 }
00337
00338 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
00339 GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
00340 gtk_text_thaw(gtktext);
00341 g_free(newtext);
00342 }
00343
00344 static gboolean check_at(GtkText *gtktext, int from_pos) {
00345 int start, end;
00346 char buf[BUFSIZE];
00347
00348 if (!get_word_from_pos(gtktext, from_pos, buf, &start, &end)) {
00349 return FALSE;
00350 }
00351
00352 if (misspelled_test(buf)) {
00353 if (highlight.pixel == 0) {
00354
00355 GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
00356 gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);;
00357 }
00358 change_color(gtktext, start, end, &highlight);
00359 return TRUE;
00360 } else {
00361 change_color(gtktext, start, end,
00362 &(GTK_WIDGET(gtktext)->style->fg[0]));
00363 return FALSE;
00364 }
00365 }
00366
00367 void gtkspell_check_all(GtkText *gtktext) {
00368 guint origpos;
00369 guint pos = 0;
00370 guint len;
00371 float adj_value;
00372
00373 if (!gtkspell_running()) return;
00374
00375 len = gtk_text_get_length(gtktext);
00376
00377 adj_value = gtktext->vadj->value;
00378 gtk_text_freeze(gtktext);
00379 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
00380 while (pos < len) {
00381 while (pos < len && iswordsep(GTK_TEXT_INDEX(gtktext, pos)))
00382 pos++;
00383 while (pos < len && !iswordsep(GTK_TEXT_INDEX(gtktext, pos)))
00384 pos++;
00385 if (pos > 0)
00386 check_at(gtktext, pos-1);
00387 }
00388 gtk_text_thaw(gtktext);
00389 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
00390 }
00391
00392 static void entry_insert_cb(GtkText *gtktext,
00393 gchar *newtext, guint len, guint *ppos, gpointer d) {
00394 int origpos;
00395
00396 if (!gtkspell_running()) return;
00397
00398 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
00399 GTK_SIGNAL_FUNC(entry_insert_cb),
00400 NULL);
00401 gtk_text_insert(GTK_TEXT(gtktext), NULL,
00402 &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
00403 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
00404 GTK_SIGNAL_FUNC(entry_insert_cb),
00405 NULL);
00406 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "insert-text");
00407 *ppos += len;
00408
00409 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
00410
00411 if (iswordsep(newtext[0])) {
00412
00413 if (*ppos >= 2) check_at(gtktext, *ppos-2);
00414
00415
00416 if (*ppos < gtk_text_get_length(gtktext))
00417 check_at(gtktext, *ppos+1);
00418 } else {
00419
00420
00421
00422 if (*ppos < gtk_text_get_length(gtktext) &&
00423 !iswordsep(GTK_TEXT_INDEX(gtktext, *ppos)))
00424 check_at(gtktext, *ppos-1);
00425 }
00426
00427 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
00428 }
00429
00430 static void entry_delete_cb(GtkText *gtktext,
00431 gint start, gint end, gpointer d) {
00432 int origpos;
00433
00434 if (!gtkspell_running()) return;
00435
00436 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
00437 check_at(gtktext, start-1);
00438 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
00439 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
00440
00441
00442 }
00443
00444 static void replace_word(GtkWidget *w, gpointer d) {
00445 int start, end, newword_len;
00446 char *newword;
00447 char buf[BUFSIZE];
00448
00449
00450
00451
00452 gtk_text_freeze(GTK_TEXT(d));
00453
00454 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), &newword);
00455 get_curword(GTK_TEXT(d), buf, &start, &end);
00456
00457 newword_len = strlen(newword);
00458
00459 gtk_text_set_point(GTK_TEXT(d), end);
00460 gtk_text_backward_delete(GTK_TEXT(d), end-start);
00461 gtk_text_insert(GTK_TEXT(d), NULL, NULL, NULL, newword, newword_len);
00462
00463 gtk_text_thaw(GTK_TEXT(d));
00464
00465 gtk_editable_set_position(GTK_EDITABLE(d), start+newword_len);
00466 }
00467
00468 static GtkMenu *make_menu(GList *l, GtkText *gtktext) {
00469 GtkWidget *menu, *item;
00470 char *caption;
00471 menu = gtk_menu_new(); {
00472 caption = g_strdup_printf("Not in dictionary: %s", (char*)l->data);
00473 item = gtk_menu_item_new_with_label(caption);
00474
00475
00476
00477 gtk_widget_show(item);
00478 gtk_menu_append(GTK_MENU(menu), item);
00479
00480 item = gtk_menu_item_new();
00481 gtk_widget_show(item);
00482 gtk_menu_append(GTK_MENU(menu), item);
00483
00484 l = l->next;
00485 if (l == NULL) {
00486 item = gtk_menu_item_new_with_label("(no suggestions)");
00487 gtk_widget_show(item);
00488 gtk_menu_append(GTK_MENU(menu), item);
00489 } else {
00490 GtkWidget *curmenu = menu;
00491 int count = 0;
00492 do {
00493 if (l->data == NULL && l->next != NULL) {
00494 count = 0;
00495 curmenu = gtk_menu_new();
00496 item = gtk_menu_item_new_with_label("Other Possibilities...");
00497 gtk_widget_show(item);
00498 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
00499 gtk_menu_append(GTK_MENU(curmenu), item);
00500 l = l->next;
00501 } else if (count > MENUCOUNT) {
00502 count -= MENUCOUNT;
00503 item = gtk_menu_item_new_with_label("More...");
00504 gtk_widget_show(item);
00505 gtk_menu_append(GTK_MENU(curmenu), item);
00506 curmenu = gtk_menu_new();
00507 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
00508 }
00509 item = gtk_menu_item_new_with_label((char*)l->data);
00510 gtk_signal_connect(GTK_OBJECT(item), "activate",
00511 GTK_SIGNAL_FUNC(replace_word), gtktext);
00512 gtk_widget_show(item);
00513 gtk_menu_append(GTK_MENU(curmenu), item);
00514 count++;
00515 } while ((l = l->next) != NULL);
00516 }
00517 }
00518 return GTK_MENU(menu);
00519 }
00520
00521 static void popup_menu(GtkText *gtktext, GdkEventButton *eb) {
00522 char buf[BUFSIZE];
00523 GList *list, *l;
00524
00525 get_curword(gtktext, buf, NULL, NULL);
00526
00527 list = misspelled_suggest(buf);
00528 if (list != NULL) {
00529 gtk_menu_popup(make_menu(list, gtktext), NULL, NULL, NULL, NULL,
00530 eb->button, eb->time);
00531 for (l = list; l != NULL; l = l->next)
00532 g_free(l->data);
00533 g_list_free(list);
00534 }
00535 }
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545 static gint button_press_intercept_cb(GtkText *gtktext, GdkEvent *e, gpointer d) {
00546 GdkEventButton *eb;
00547 gboolean retval;
00548
00549 if (!gtkspell_running()) return FALSE;
00550
00551 if (e->type != GDK_BUTTON_PRESS) return FALSE;
00552 eb = (GdkEventButton*) e;
00553
00554 if (eb->button != 3) return FALSE;
00555
00556
00557 eb->button = 1;
00558
00559 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
00560 GTK_SIGNAL_FUNC(button_press_intercept_cb), d);
00561 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
00562 e, &retval);
00563 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
00564 GTK_SIGNAL_FUNC(button_press_intercept_cb), d);
00565 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
00566
00567
00568 popup_menu(gtktext, eb);
00569 return TRUE;
00570 }
00571
00572 void gtkspell_uncheck_all(GtkText *gtktext) {
00573 int origpos;
00574 char *text;
00575 float adj_value;
00576
00577 adj_value = gtktext->vadj->value;
00578 gtk_text_freeze(gtktext);
00579 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
00580 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
00581 gtk_text_set_point(gtktext, 0);
00582 gtk_text_forward_delete(gtktext, gtk_text_get_length(gtktext));
00583 gtk_text_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
00584 gtk_text_thaw(gtktext);
00585
00586 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
00587 gtk_adjustment_set_value(gtktext->vadj, adj_value);
00588 }
00589
00590 void gtkspell_attach(GtkText *gtktext) {
00591 gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text",
00592 GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
00593 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
00594 GTK_SIGNAL_FUNC(entry_delete_cb), NULL);
00595 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
00596 GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL);
00597 }
00598
00599 void gtkspell_detach(GtkText *gtktext) {
00600 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
00601 GTK_SIGNAL_FUNC(entry_insert_cb), NULL);
00602 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
00603 GTK_SIGNAL_FUNC(entry_delete_cb), NULL);
00604 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
00605 GTK_SIGNAL_FUNC(button_press_intercept_cb), NULL);
00606
00607 gtkspell_uncheck_all(gtktext);
00608 }
00609
00610 static void sigchld(int param) {
00611 #ifndef __MINGW32__
00612 if (gtkspell_running() &&
00613 (waitpid(spell_pid, NULL, WNOHANG) == spell_pid)) {
00614 spell_pid = 0;
00615 } else {
00616
00617
00618 waitpid(-1, NULL, WNOHANG);
00619 }
00620 #endif
00621 }
00622
00623 static void set_up_signal() {
00624 #ifndef __MINGW32__
00625 struct sigaction sigact;
00626 memset(&sigact, 0, sizeof(struct sigaction));
00627
00628 sigact.sa_handler = sigchld;
00629 sigaction(SIGCHLD, &sigact, NULL);
00630 #endif
00631 }