forked from bloovis/microemacs.mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspell.c
More file actions
457 lines (415 loc) · 9.78 KB
/
spell.c
File metadata and controls
457 lines (415 loc) · 9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
Copyright (C) 2018 Mark Alexander
This file is part of MicroEMACS, a small text editor.
MicroEMACS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "def.h"
#define CCHR(x) ((x)-'@')
/*
* Local variables.
*/
static FILE *ispell_input;
static FILE *ispell_output;
static char word[NPAT]; /* word to check for spelling */
static char repl[NPAT]; /* string to replace word */
static char buf[256]; /* line buffer for input from ispell */
static char *guesses[10]; /* guesses returned by ispell */
static int nguesses; /* number of guesses */
static LINE *clp; /* saved line pointer */
static int cbo; /* offset into the saved line */
static int nrepl; /* number of replacements performed */
/*
* Return TRUE if the character at dot is a letter or an
* apostrophe (as in "doesn't", for example).
*/
static int
inalpha (void)
{
return inwordpos (curwp->w_dot.p, curwp->w_dot.o, TRUE);
}
/*
* Open a two-way pipe to the ispell program.
*/
static int
open_ispell (void)
{
const char *args[3];
if (ispell_input != NULL)
return TRUE;
args[0] = "ispell";
args[1] = "-a";
args[2] = NULL;
if (openpipe ("ispell", args, &ispell_input, &ispell_output) == FALSE)
return FALSE;
/* Read the identification message from ispell.
*/
if (fgets (buf, sizeof (buf), ispell_input) == NULL)
return FALSE;
else
return TRUE;
}
/*
* Replace the string 'word' with 'repl' at dot. The dot
* must be past the end of the old word, because lreplace expects
* dot to be after, not before, the string being replaced.
*/
static int
replace (void)
{
int oldlen = uslen ((const uchar *) word);
int status;
curwp->w_savep = clp;
status = lreplace (oldlen, repl, FALSE);
++nrepl;
clp = curwp->w_savep;
return status;
}
/*
* Remove plus signs from a string, and remove suffixes preceded
* with minus signs. This is useful for cleaning up ispell
* suggestions about suffixes. For example, it will suggest
* "instinctual+ly" for "instinctually", and will suggest
* "instinctual-ism" for "instinctualism".
*/
static void
cleanupguess (char *s)
{
char *d = s;
char c;
while ((c = *s) != '\0')
{
if (c == '-')
{
++s;
while ((c = *s) != '\0' && c != '+')
++s;
}
else if (c == '+')
++s;
else
{
*d = *s;
++d;
++s;
}
}
*d = '\0';
}
/*
* Prompt for a replacement word. If the user specifies a replacement
* successfully, copy the replacement to repl and return TRUE.
* Otherwise return FALSE, or ABORT if the user enters Q or Control-G.
*/
static int
getrepl (const char *prompt)
{
int c;
int status;
int done;
eprintf (prompt);
done = FALSE;
while (!done)
{
if (!inprof)
update (); /* show current position */
c = getinp ();
switch (c)
{
case CCHR ('G'):
/* Abort the replacement.
*/
status = ctrlg (FALSE, 0, KRANDOM);
done = TRUE;
break;
case 'q':
case 'Q':
status = ABORT;
done = TRUE;
break;
case ' ':
/* Ignore the word and don't replace it.
*/
status = FALSE;
done = TRUE;
break;
case 'r':
case 'R':
/* User doesn't like any of the suggestions. Prompt
* for a replacement string.
*/
status = ereply ("Replace with: ", repl, sizeof (repl));
done = TRUE;
break;
case 'a':
case 'A':
/* Tell ispell to accept the word in the future, and
* leave it unchanged.
*/
fputc ('@', ispell_output);
fputs (word, ispell_output);
fputc ('\n', ispell_output);
fflush (ispell_output);
status = FALSE;
done = TRUE;
break;
default:
/* A digit from 0 to 9 means use ispell's nth
* suggested replacement.
*/
if (c >= '0' && c <= '9')
{
int n = c - '0';
if (n < nguesses)
{
cleanupguess (guesses[n]);
strncpy (repl, guesses[n], sizeof (repl));
status = TRUE;
done = TRUE;
}
}
break;
}
}
eerase ();
return status;
}
/*
* Ask ispell to check a word, and if it is not correct,
* prompt the user with the suggestions from ispell.
* If info is TRUE, print status messages on the echo line
* about the replacement that was performed, if any.
*/
static int
ask_ispell (int info)
{
char *s;
char prompt[256];
int status;
int chars;
int i;
const char *fmt;
fputs (word, ispell_output);
fputc ('\n', ispell_output);
fflush (ispell_output);
status = FALSE;
while (TRUE)
{
if (fgets (buf, sizeof (buf), ispell_input) == NULL)
return FALSE;
/* ispell outputs a blank line after the line containing
* spelling suggestions, so we need to read and discard it.
*/
if (buf[0] == '\n' || buf[0] == '\0')
break;
/* Zap the terminating line feed.
*/
s = strchr (buf, '\n');
if (s != NULL)
*s = '\0';
/* Parse the ispell response.
*/
switch (buf[0])
{
case '*':
if (info)
eprintf ("%s is spelled correctly", word);
status = TRUE;
break;
case '+':
if (info)
eprintf ("%s found via root %s", word, &buf[2]);
status = TRUE;
break;
case '#':
case '&':
case '?':
/* Ignore the first part of the response, which gives
* the original word, an offset, and possibly the
* number of guesses.
*/
if (buf[0] == '#')
fmt = " %*s %*d%n";
else
fmt = " %*s %*d %*d: %n";
s = &buf[1];
if (sscanf (s, fmt, &chars) != 0)
break;
s += chars;
/* The rest of the response contains guesses separated by commas.
* Break up the guesses into individual words.
*/
nguesses = 0;
for (i = 0; i < 10 && *s != '\0'; i++)
{
guesses[i] = s;
nguesses = i + 1;
while (*s != ',' && *s != '\0')
++s;
if (*s == '\0')
break;
*s++ = '\0';
while (*s != '\0' && *s != ' ')
++s;
if (*s == '\0')
break;
++s;
}
/* Construct a prompt string that includes as many guesses
* as will fit on one line.
*/
strcpy (prompt, word);
strcat (prompt, ": SPC=ignore,A=accept,R=replace,Q=quit");
for (i = 0; i < nguesses; i++)
{
char n[2];
/* If the prompt exceeds the window width, truncate
* the number of guesses. This is a horrible hack
* but I can't see a good way to display a long list
* of guesses on a single line.
*/
if (strlen (prompt) + strlen (guesses[i]) + 3 > ncol)
{
nguesses = i;
break;
}
n[0] = i + '0';
n[1] = '\0';
strcat (prompt, ",");
strcat (prompt, n);
strcat (prompt, "=");
strcat (prompt, guesses[i]);
}
/* Prompt for a replacement word, and do the replacement
* if the user specifies one.
*/
if ((status = getrepl (prompt)) == TRUE)
status = replace ();
if (info)
{
if (status == TRUE)
eprintf ("%s replaced with %s", word, repl);
else
eprintf ("No replacement done");
}
break;
default:
eprintf ("Unrecognized ispell response: %s", buf);
break;
}
}
return status;
}
/*
* Check the word under the cursor for spelling,
* and prompt the user for a replacement.
*/
int
checkword (REGION *r)
{
int status;
if (open_ispell () == FALSE)
{
eprintf ("Unable to open a pipe to ispell");
return FALSE;
}
/* Get the word under the cursor (if any). Then prompt
* for a word to spell-check, using the cursor word as the default.
*/
word[0] = '\0';
getcursorword (word, sizeof (word), TRUE);
if (word[0] == '\0')
{
eprintf("No word under cursor");
return FALSE;
}
/* Move past the word, because lreplace expects that.
*/
while (inalpha ())
{
if ((status = forwchar (TRUE, 1, KRANDOM)) != TRUE)
return status;
/* If we're keeping track of a region size, decrement
* the size for each character we move past.
*/
if (r != NULL)
r->r_size--;
}
/* Ask ispell for the correct spelling, and prompt user
* for a replacement.
*/
return ask_ispell (r == NULL);
}
/*
* Run ispell on the word under the cursor, or
* if there is no word there, prompt for the word.
*/
int
spellword (int f, int n, int k)
{
return checkword (NULL);
}
/*
* Run ispell on the current marked region.
*/
int
spellregion (int f, int n, int k)
{
REGION r;
int status;
int len;
if ((status = getregion (&r)) != TRUE)
return status;
/* Save the current location.
*/
clp = curwp->w_dot.p;
cbo = curwp->w_dot.o;
/* Scan through the region looking for words, and spell-check
* each found word.
*/
curwp->w_dot = r.r_pos;
nrepl = 0;
while (r.r_size > 0)
{
/* Find start of word.
*/
while (r.r_size > 0)
{
if (inalpha ())
break;
if (forwchar (TRUE, 1, KRANDOM) != TRUE)
break;
r.r_size--;
}
/* If we're not in a word, we must have reached the end
* of the region or buffer, so terminate the outer loop.
*/
if (!inalpha ())
break;
/* Spell-check the current word.
*/
if ((status = checkword (&r)) == ABORT)
break;
}
eprintf ("%d replacement%s done.", nrepl, nrepl == 1 ? "" : "s");
/* Restore the current location.
*/
curwp->w_dot.p = clp;
len = wllength (clp);
if (cbo > len)
curwp->w_dot.o = len;
else
curwp->w_dot.o = cbo;
curwp->w_flag |= WFHARD;
if (!inprof)
update ();
return TRUE;
}