summaryrefslogtreecommitdiff
path: root/pico/osdep/spell.c
blob: f05446b4adef0249daf90f126d1592aaac189695 (plain)
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
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: spell.c 854 2007-12-07 17:44:43Z hubert@u.washington.edu $";
#endif

/*
 * ========================================================================
 * Copyright 2006-2007 University of Washington
 * Copyright 2013-2017 Eduardo Chappa
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * ========================================================================
 *
 * Program:	spell.c
 */


#include <system.h>
#include <general.h>

#include "../headers.h"
/*
#include "../estruct.h"
#include "../mode.h"
#include "../pico.h"
#include "../edef.h"
#include "../efunc.h"
#include "../keydefs.h"
*/

#include "../../pith/charconv/filesys.h"

#include "popen.h"
#include "altedit.h"
#include "spell.h"


#ifdef	SPELLER

int  movetoword(UCS *);


static char *spellhelp[] = {
/* TRANSLATORS: Some help text. The ~ characters cause
   the characters they are in front of to be bold. */
  N_("Spell Check Help"),
  " ",
  N_("        The spell checker examines all words in the text.  It then"),
  N_("        offers each misspelled word for correction while simultaneously"),
  N_("        highlighting it in the text.  To leave a word unchanged simply"),
  N_("~        hit ~R~e~t~u~r~n at the edit prompt.  If a word has been corrected,"),
  N_("        each occurrence of the incorrect word is offered for replacement."),
  " ",
  N_("~        Spell checking can be cancelled at any time by typing ~^~C (~F~3)"),
  N_("        after exiting help."),
  " ",
  N_("End of Spell Check Help"),
  " ",
  NULL
};


static char *pinespellhelp[] = {
  N_("Spell Check Help"),
  " ",
  N_("\tThe spell checker examines all words in the text.  It then"),
  N_("\toffers each misspelled word for correction while simultaneously"),
  N_("\thighlighting it in the text.  To leave a word unchanged simply"),
  N_("\thit Return at the edit prompt.  If a word has been corrected,"),
  N_("\teach occurrence of the incorrect word is offered for replacement."),
  " ",
  N_("\tSpell checking can be cancelled at any time by typing ^C (F3)"),
  N_("\tafter exiting help."),
  " ",
  N_("End of Spell Check Help"),
  " ",
  NULL
};

#ifndef	_WINDOWS
/*
 * spell() - check for potentially missspelled words and offer them for
 *           correction
 */
int
spell(int f, int n)
{
    int    status, next, ret;
    char   ccb[NLINE], *sp, *fn, *lp, *wsp, c, spc[NLINE];
    UCS   *b;
    UCS    wb[NLINE], cb[NLINE];
    EML    eml;

    setimark(0, 1);
    emlwrite(_("Checking spelling..."), NULL); 	/* greetings */

    if(alt_speller)
      return(alt_editor(1, 0));			/* f == 1 means fork speller */

    if((fn = writetmp(0, NULL)) == NULL){
	emlwrite(_("Can't write temp file for spell checker"), NULL);
	return(-1);
    }

    if((sp = (char *)getenv("SPELL")) == NULL)
      sp = SPELLER;

    /* exists? */
    ret = (strlen(sp) + 1);
    snprintf(spc, sizeof(spc), "%s", sp);

    for(lp = spc, ret = FIOERR; *lp; lp++){
	if((wsp = strpbrk(lp, " \t")) != NULL){
	    c = *wsp;
	    *wsp = '\0';
	}

	if(strchr(lp, '/')){
	    ret = fexist(lp, "x", (off_t *)NULL);
	}
	else{
	    char *path, fname[MAXPATH+1];

	    if(!(path = getenv("PATH")))
	      path = ":/bin:/usr/bin";

	    ret = ~FIOSUC;
	    while(ret != FIOSUC && *path && pathcat(fname, &path, lp))
	      ret = fexist(fname, "x", (off_t *)NULL);
	}

	if(wsp)
	  *wsp = c;

	if(ret == FIOSUC)
	  break;
    }

    if(ret != FIOSUC){
	eml.s = sp;
        emlwrite(_("\007Spell-checking file \"%s\" not found"), &eml);
	return(-1);
    }

    snprintf(ccb, sizeof(ccb), "( %s ) < %s", sp, fn);
    if(P_open(ccb) != FIOSUC){ 		/* read output from command */
	our_unlink(fn);
	emlwrite(_("Can't fork spell checker"), NULL);
	return(-1);
    }

    ret = 1;
    while(ffgetline(wb, NLINE, NULL, 0) == FIOSUC && ret){
	if((b = ucs4_strchr(wb, (UCS) '\n')) != NULL)
	  *b = '\0';

	ucs4_strncpy(cb, wb, NLINE);
	cb[NLINE-1] = '\0';

	gotobob(0, 1);

	status = TRUE;
	next = 1;

	while(status){
	    if(next++)
	      if(movetoword(wb) != TRUE)
		break;

	    update();
	    (*term.t_rev)(1);
	    pputs(wb, 1);			/* highlight word */
	    (*term.t_rev)(0);

	    if(ucs4_strcmp(cb, wb)){
		char prompt[2*NLINE + 32];
		char *wbu, *cbu;

		wbu = ucs4_to_utf8_cpystr(wb);
		cbu = ucs4_to_utf8_cpystr(cb);

		snprintf(prompt, sizeof(prompt), _("Replace \"%s\" with \"%s\""), wbu, cbu);
		status=mlyesno_utf8(prompt, TRUE);
		if(wbu)
		  fs_give((void **) &wbu);
		if(cbu)
		  fs_give((void **) &cbu);
	    }
	    else{
		UCS *p;

		p = utf8_to_ucs4_cpystr(_("Edit a replacement: "));
		status=mlreplyd(p, cb, NLINE, QDEFLT, NULL);
		if(p)
		  fs_give((void **) &p);
	    }


	    curwp->w_flag |= WFMOVE;		/* put cursor back */
	    sgarbk = 0;				/* fake no-keymenu-change! */
	    update();
	    pputs(wb, 0);			/* un-highlight */

	    switch(status){
	      case TRUE:
		chword(wb, cb, 0);		/* correct word    */
	      case FALSE:
		update();			/* place cursor */
		break;
	      case ABORT:
		emlwrite(_("Spell Checking Cancelled"), NULL);
		ret = FALSE;
		status = FALSE;
		break;
	      case HELPCH:
		if(Pmaster){
		    VARS_TO_SAVE *saved_state;

		    saved_state = save_pico_state();
		    (*Pmaster->helper)(pinespellhelp, 
				       _("Help with Spelling Checker"), 1);
		    if(saved_state){
			restore_pico_state(saved_state);
			free_pico_state(saved_state);
		    }
		}
		else
		  pico_help(spellhelp, _("Help with Spelling Checker"), 1);

	      case (CTRL|'L'):
		next = 0;			/* don't get next word */
		sgarbf = TRUE;			/* repaint full screen */
		update();
		status = TRUE;
		continue;
	      default:
		emlwrite("Huh?", NULL);		/* shouldn't get here, but.. */
		status = TRUE;
		sleep(1);
		break;
	    }
	    
	    forwword(0, 1);			/* goto next word */
	}
    }

    P_close();					/* clean up */
    our_unlink(fn);
    swapimark(0, 1);
    curwp->w_flag |= WFHARD|WFMODE;
    sgarbk = TRUE;

    if(ret)
      emlwrite(_("Done checking spelling"), NULL);

    return(ret);
}


#endif /* UNIX */


/* 
 * movetoword() - move to the first occurance of the word w
 *
 *	returns:
 *		TRUE upon success
 *		FALSE otherwise
 */
int
movetoword(UCS *w)
{
    int      i;
    int      ret  = FALSE;
    int	     olddoto;
    LINE     *olddotp;
    register int   off;				/* curwp offset */
    register LINE *lp;				/* curwp line   */

    olddoto = curwp->w_doto;			/* save where we are */
    olddotp = curwp->w_dotp;

    curwp->w_bufp->b_mode |= MDEXACT;		/* case sensitive */
    while(forscan(&i, w, SR_FORWARD, NULL, 0, 1) == TRUE){
	if(i)
	  break;				/* wrap NOT allowed! */

	lp  = curwp->w_dotp;			/* for convenience */
	off = curwp->w_doto;

	/*
	 * We want to minimize the number of substrings that we report
	 * as matching a misspelled word...
	 */
	if(off == 0 || !ucs4_isalpha(lgetc(lp, off - 1).c)){
	    off += ucs4_strlen(w);
	    if((!ucs4_isalpha(lgetc(lp, off).c) || off == llength(lp))
	       && lgetc(lp, 0).c != '>'){
		ret = TRUE;
		break;
	    }
	}

	forwchar(0, 1);				/* move on... */

    }
    curwp->w_bufp->b_mode ^= MDEXACT;		/* case insensitive */

    if(ret == FALSE){
	curwp->w_dotp = olddotp;
	curwp->w_doto = olddoto;
    }
    else
      curwp->w_flag |= WFHARD;

    return(ret);
}

#endif	/* SPELLER */