xref: /csrg-svn/contrib/sc/vi.c (revision 45336)
1*45336Sbostic /*	SC	A Spreadsheet Calculator
2*45336Sbostic  *
3*45336Sbostic  *	One line vi emulation
4*45336Sbostic  *	$Revision: 6.8 $
5*45336Sbostic  */
6*45336Sbostic 
7*45336Sbostic 
8*45336Sbostic #include <signal.h>
9*45336Sbostic #include <curses.h>
10*45336Sbostic 
11*45336Sbostic #ifdef BSD42
12*45336Sbostic #include <strings.h>
13*45336Sbostic #else
14*45336Sbostic #ifndef SYSIII
15*45336Sbostic #include <string.h>
16*45336Sbostic #endif
17*45336Sbostic #endif
18*45336Sbostic 
19*45336Sbostic #if !defined(strchr) && !defined(UPORT)
20*45336Sbostic #define strchr index
21*45336Sbostic #endif
22*45336Sbostic extern	char	*strchr();
23*45336Sbostic 
24*45336Sbostic #include <stdio.h>
25*45336Sbostic #include <ctype.h>
26*45336Sbostic #include "sc.h"
27*45336Sbostic 
28*45336Sbostic #define istext(a) (isalnum(a) || ((a) == '_'))
29*45336Sbostic 
30*45336Sbostic extern int showrange;
31*45336Sbostic extern char mode_ind;		/* Mode indicator */
32*45336Sbostic 
33*45336Sbostic /* values for mode below */
34*45336Sbostic 
35*45336Sbostic #define INSERT_MODE	0	/* Insert mode */
36*45336Sbostic #define EDIT_MODE       1	/* Edit mode */
37*45336Sbostic #define REP_MODE        2	/* Replace mode */
38*45336Sbostic #define SEARCH_MODE	3	/* Get arguments for '/' command */
39*45336Sbostic 
40*45336Sbostic static int mode = INSERT_MODE;
41*45336Sbostic static char *history[HISTLEN];
42*45336Sbostic static int histp = -1;
43*45336Sbostic static char *last_search;
44*45336Sbostic static char *undo_line;
45*45336Sbostic static int undo_lim;
46*45336Sbostic static char dotb[100];
47*45336Sbostic static int doti = 0;
48*45336Sbostic static int do_dot = 0;
49*45336Sbostic 
50*45336Sbostic void
write_line(c)51*45336Sbostic write_line(c)
52*45336Sbostic int c;
53*45336Sbostic {
54*45336Sbostic     if (mode == EDIT_MODE) {
55*45336Sbostic 	switch(c) {
56*45336Sbostic 	case (ctl('h')):	linelim = back_line();		break;
57*45336Sbostic 	case (ctl('m')):  cr_line();			break;
58*45336Sbostic 	case ESC:	stop_edit();			break;
59*45336Sbostic 	case '+':	for_hist();			break;
60*45336Sbostic 	case '-':	back_hist();			break;
61*45336Sbostic 	case '$':	last_col();			break;
62*45336Sbostic 	case '.':	dotcmd();			break;
63*45336Sbostic 	case '/':	search_mode();			break;
64*45336Sbostic 	case '0':	col_0();			break;
65*45336Sbostic 	case 'D':	u_save(c);del_to_end();		break;
66*45336Sbostic 	case 'I':	u_save(c);col_0();insert_mode();break;
67*45336Sbostic 	case 'R':	replace_mode();			break;
68*45336Sbostic 	case 'X':	u_save(c); back_space();	break;
69*45336Sbostic 	case 'a':	u_save(c); append_line();	break;
70*45336Sbostic 	case 'b':	linelim = back_word();		break;
71*45336Sbostic 	case 'c':	u_save(c); change_cmd();	break;
72*45336Sbostic 	case 'd':	u_save(c); delete_cmd();	break;
73*45336Sbostic 	case 'f':	linelim = find_char();		break;
74*45336Sbostic 	case 'h':	linelim = back_line();		break;
75*45336Sbostic 	case 'i':	u_save(c); insert_mode();	break;
76*45336Sbostic 	case 'j':	for_hist();			break;
77*45336Sbostic 	case 'k':	back_hist();			break;
78*45336Sbostic 	case 'l':	linelim = for_line(0);		break;
79*45336Sbostic 	case 'n':	search_again();			break;
80*45336Sbostic 	case 'q':	stop_edit();			break;
81*45336Sbostic 	case 'r':	u_save(c); rep_char();		break;
82*45336Sbostic 	case 't':	linelim = to_char();		break;
83*45336Sbostic 	case 'u':	restore_it();			break;
84*45336Sbostic 	case 'w':	linelim = for_word(0);		break;
85*45336Sbostic 	case 'x':	u_save(c); del_in_line();	break;
86*45336Sbostic 	default:	break;
87*45336Sbostic 	}
88*45336Sbostic     } else if (mode == INSERT_MODE) {
89*45336Sbostic 	savedot(c);
90*45336Sbostic 	switch(c) {
91*45336Sbostic 	case (ctl('h')):	back_space();			break;
92*45336Sbostic 	case (ctl('m')):  cr_line();			break;
93*45336Sbostic 	case ESC:	edit_mode();			break;
94*45336Sbostic 	default:	ins_in_line(c);			break;
95*45336Sbostic 	}
96*45336Sbostic     } else if (mode == SEARCH_MODE) {
97*45336Sbostic 	switch(c) {
98*45336Sbostic 	case (ctl('h')):	back_space();			break;
99*45336Sbostic 	case (ctl('m')):  search_hist();			break;
100*45336Sbostic 	case ESC:	edit_mode();			break;
101*45336Sbostic 	default:	ins_in_line(c);			break;
102*45336Sbostic 	}
103*45336Sbostic    } else if (mode == REP_MODE) {
104*45336Sbostic 	savedot(c);
105*45336Sbostic 	switch(c) {
106*45336Sbostic 	case (ctl('h')):	back_space();			break;
107*45336Sbostic 	case (ctl('m')):  cr_line();			break;
108*45336Sbostic 	case ESC:	edit_mode();			break;
109*45336Sbostic 	default:	replace_in_line(c);		break;
110*45336Sbostic 	}
111*45336Sbostic     }
112*45336Sbostic }
113*45336Sbostic 
edit_mode()114*45336Sbostic edit_mode()
115*45336Sbostic {
116*45336Sbostic     mode = EDIT_MODE;
117*45336Sbostic     mode_ind = 'e';
118*45336Sbostic     histp = -1;
119*45336Sbostic     if (line[linelim] == '\0')
120*45336Sbostic 	linelim = back_line();
121*45336Sbostic }
122*45336Sbostic 
123*45336Sbostic void
insert_mode()124*45336Sbostic insert_mode()
125*45336Sbostic {
126*45336Sbostic     mode_ind = 'i';
127*45336Sbostic     mode = INSERT_MODE;
128*45336Sbostic }
129*45336Sbostic 
search_mode()130*45336Sbostic search_mode()
131*45336Sbostic {
132*45336Sbostic     line[0] = '/';
133*45336Sbostic     line[1] = 0;
134*45336Sbostic     linelim = 1;
135*45336Sbostic     histp = -1;
136*45336Sbostic     mode_ind = '/';
137*45336Sbostic     mode = SEARCH_MODE;
138*45336Sbostic }
139*45336Sbostic 
replace_mode()140*45336Sbostic replace_mode()
141*45336Sbostic {
142*45336Sbostic     mode_ind = 'R';
143*45336Sbostic     mode = REP_MODE;
144*45336Sbostic }
145*45336Sbostic 
146*45336Sbostic /* dot command functions.  Saves info so we can redo on a '.' command */
147*45336Sbostic 
savedot(c)148*45336Sbostic savedot(c)
149*45336Sbostic int c;
150*45336Sbostic {
151*45336Sbostic     if (do_dot)
152*45336Sbostic 	return;
153*45336Sbostic 
154*45336Sbostic     dotb[doti++] = c;
155*45336Sbostic     dotb[doti] = 0;
156*45336Sbostic }
157*45336Sbostic 
dotcmd()158*45336Sbostic dotcmd()
159*45336Sbostic {
160*45336Sbostic     int c;
161*45336Sbostic 
162*45336Sbostic     do_dot = 1;
163*45336Sbostic     doti = 0;
164*45336Sbostic     while(dotb[doti] != 0) {
165*45336Sbostic 	c = dotb[doti++];
166*45336Sbostic 	write_line(c);
167*45336Sbostic     }
168*45336Sbostic     do_dot = 0;
169*45336Sbostic     doti = 0;
170*45336Sbostic }
171*45336Sbostic 
vigetch()172*45336Sbostic vigetch()
173*45336Sbostic {
174*45336Sbostic     int c;
175*45336Sbostic 
176*45336Sbostic     if(do_dot) {
177*45336Sbostic 	if (dotb[doti] != 0) {
178*45336Sbostic 	    return(dotb[doti++]);
179*45336Sbostic 	} else {
180*45336Sbostic 	    do_dot = 0;
181*45336Sbostic 	    doti = 0;
182*45336Sbostic 	    return(nmgetch());
183*45336Sbostic 	}
184*45336Sbostic     }
185*45336Sbostic     c = nmgetch();
186*45336Sbostic     savedot(c);
187*45336Sbostic     return(c);
188*45336Sbostic }
189*45336Sbostic 
190*45336Sbostic /* saves the current line for possible use by an undo cmd */
191*45336Sbostic 
u_save(c)192*45336Sbostic u_save(c)
193*45336Sbostic int c;
194*45336Sbostic {
195*45336Sbostic     if (undo_line) {
196*45336Sbostic 	xfree(undo_line);
197*45336Sbostic 	undo_line = 0;
198*45336Sbostic     }
199*45336Sbostic     undo_line = strcpy(xmalloc((unsigned)(strlen(line)+1)), line);
200*45336Sbostic     undo_lim = linelim;
201*45336Sbostic 
202*45336Sbostic     /* reset dot command if not processing it. */
203*45336Sbostic 
204*45336Sbostic     if (!do_dot) {
205*45336Sbostic         doti = 0;
206*45336Sbostic 	savedot(c);
207*45336Sbostic     }
208*45336Sbostic }
209*45336Sbostic 
210*45336Sbostic /* Restores the current line saved by u_save() */
211*45336Sbostic 
restore_it()212*45336Sbostic restore_it()
213*45336Sbostic {
214*45336Sbostic     register char *tempc;
215*45336Sbostic     register int tempi;
216*45336Sbostic 
217*45336Sbostic     if (!undo_line)
218*45336Sbostic 	return;
219*45336Sbostic     tempc = strcpy(xmalloc((unsigned)(strlen(line)+1)), line);
220*45336Sbostic     tempi = linelim;
221*45336Sbostic     strcpy(line, undo_line);
222*45336Sbostic     linelim = undo_lim;
223*45336Sbostic     xfree(undo_line);
224*45336Sbostic     undo_line = tempc;
225*45336Sbostic     undo_lim = tempi;
226*45336Sbostic }
227*45336Sbostic 
228*45336Sbostic /* This command stops the editing process. */
229*45336Sbostic 
stop_edit()230*45336Sbostic stop_edit()
231*45336Sbostic {
232*45336Sbostic     showrange = 0;
233*45336Sbostic     linelim = -1;
234*45336Sbostic     (void) move(1, 0);
235*45336Sbostic     (void) clrtoeol();
236*45336Sbostic }
237*45336Sbostic 
238*45336Sbostic /*
239*45336Sbostic  * Motion commands.  Forward motion commands take an argument
240*45336Sbostic  * which, when set, cause the forward motion to continue onto
241*45336Sbostic  * the null at the end of the line instead of stopping at the
242*45336Sbostic  * the last character of the line.
243*45336Sbostic  */
244*45336Sbostic 
for_line(stop_null)245*45336Sbostic for_line(stop_null)
246*45336Sbostic int stop_null;
247*45336Sbostic {
248*45336Sbostic     if (linelim >= 0 && line[linelim] != 0 &&
249*45336Sbostic     		        (line[linelim+1] != 0 || stop_null))
250*45336Sbostic 	return(linelim+1);
251*45336Sbostic     else
252*45336Sbostic 	return(linelim);
253*45336Sbostic }
254*45336Sbostic 
for_word(stop_null)255*45336Sbostic for_word(stop_null)
256*45336Sbostic int stop_null;
257*45336Sbostic {
258*45336Sbostic     register int c;
259*45336Sbostic     register int cpos;
260*45336Sbostic 
261*45336Sbostic     cpos = linelim;
262*45336Sbostic 
263*45336Sbostic     if (line[cpos] == ' ') {
264*45336Sbostic 	while (line[cpos] == ' ')
265*45336Sbostic 	    cpos++;
266*45336Sbostic 	if (cpos > 0 && line[cpos] == 0)
267*45336Sbostic 	    --cpos;
268*45336Sbostic 	return(cpos);
269*45336Sbostic     }
270*45336Sbostic 
271*45336Sbostic     if (istext(line[cpos])) {
272*45336Sbostic     	while ((c = line[cpos]) && istext(c))
273*45336Sbostic 		cpos++;
274*45336Sbostic     } else {
275*45336Sbostic 	while ((c = line[cpos]) && !istext(c) && c != ' ')
276*45336Sbostic 		cpos++;
277*45336Sbostic     }
278*45336Sbostic 
279*45336Sbostic     while (line[cpos] == ' ')
280*45336Sbostic         cpos++;
281*45336Sbostic 
282*45336Sbostic     if (cpos > 0 && line[cpos] == 0 && !stop_null)
283*45336Sbostic         --cpos;
284*45336Sbostic 
285*45336Sbostic     return(cpos);
286*45336Sbostic }
287*45336Sbostic 
back_line()288*45336Sbostic back_line()
289*45336Sbostic {
290*45336Sbostic     if (linelim)
291*45336Sbostic         return(linelim-1);
292*45336Sbostic     else
293*45336Sbostic 	return(0);
294*45336Sbostic }
295*45336Sbostic 
back_word()296*45336Sbostic back_word()
297*45336Sbostic {
298*45336Sbostic     register int c;
299*45336Sbostic     register int cpos;
300*45336Sbostic 
301*45336Sbostic     cpos = linelim;
302*45336Sbostic 
303*45336Sbostic     if (line[cpos] == ' ') {
304*45336Sbostic 	/* Skip white space */
305*45336Sbostic         while (cpos > 0 && line[cpos] == ' ')
306*45336Sbostic 	    --cpos;
307*45336Sbostic     } else if (cpos > 0 && (line[cpos-1] == ' '
308*45336Sbostic 		     ||  istext(line[cpos]) && !istext(line[cpos-1])
309*45336Sbostic 		     || !istext(line[cpos]) &&  istext(line[cpos-1]))) {
310*45336Sbostic 	/* Started on the first char of a word - back up to prev. word */
311*45336Sbostic 	--cpos;
312*45336Sbostic         while (cpos > 0 && line[cpos] == ' ')
313*45336Sbostic 	    --cpos;
314*45336Sbostic     }
315*45336Sbostic 
316*45336Sbostic     /* Skip across the word - goes 1 too far */
317*45336Sbostic     if (istext(line[cpos])) {
318*45336Sbostic     	while (cpos > 0 && (c = line[cpos]) && istext(c))
319*45336Sbostic 		--cpos;
320*45336Sbostic     } else {
321*45336Sbostic 	while (cpos > 0 && (c = line[cpos]) && !istext(c) && c != ' ')
322*45336Sbostic 		--cpos;
323*45336Sbostic     }
324*45336Sbostic 
325*45336Sbostic     /* We are done - fix up the one too far */
326*45336Sbostic     if (cpos > 0 && line[cpos] && line[cpos+1])
327*45336Sbostic 	cpos++;
328*45336Sbostic 
329*45336Sbostic     return(cpos);
330*45336Sbostic }
331*45336Sbostic 
332*45336Sbostic /* Text manipulation commands */
333*45336Sbostic 
del_in_line()334*45336Sbostic del_in_line()
335*45336Sbostic {
336*45336Sbostic     register int len, i;
337*45336Sbostic 
338*45336Sbostic     if (linelim >= 0) {
339*45336Sbostic 	len = strlen(line);
340*45336Sbostic 	if (linelim == len && linelim > 0)
341*45336Sbostic 	    linelim--;
342*45336Sbostic 	for (i = linelim; i < len; i++)
343*45336Sbostic 	    line[i] = line[i+1];
344*45336Sbostic     }
345*45336Sbostic     if (linelim > 0 && line[linelim] == 0)
346*45336Sbostic 	--linelim;
347*45336Sbostic }
348*45336Sbostic 
ins_in_line(c)349*45336Sbostic ins_in_line(c)
350*45336Sbostic int c;
351*45336Sbostic {
352*45336Sbostic     register int i, len;
353*45336Sbostic 
354*45336Sbostic     len = strlen(line);
355*45336Sbostic     for (i = len; i >= linelim; --i)
356*45336Sbostic 	line[i+1] = line[i];
357*45336Sbostic     line[linelim++] = c;
358*45336Sbostic     line[len+1] = 0;
359*45336Sbostic }
360*45336Sbostic 
361*45336Sbostic void
ins_string(s)362*45336Sbostic ins_string(s)
363*45336Sbostic char *s;
364*45336Sbostic {
365*45336Sbostic     while (*s)
366*45336Sbostic 	ins_in_line(*s++);
367*45336Sbostic }
368*45336Sbostic 
append_line()369*45336Sbostic append_line()
370*45336Sbostic {
371*45336Sbostic     register int i;
372*45336Sbostic 
373*45336Sbostic     i = linelim;
374*45336Sbostic     if (i >= 0 && line[i])
375*45336Sbostic 	linelim++;
376*45336Sbostic     insert_mode();
377*45336Sbostic }
378*45336Sbostic 
rep_char()379*45336Sbostic rep_char()
380*45336Sbostic {
381*45336Sbostic     int c;
382*45336Sbostic 
383*45336Sbostic     c = vigetch();
384*45336Sbostic     if (line[linelim] != 0) {
385*45336Sbostic     	line[linelim] = c;
386*45336Sbostic     } else {
387*45336Sbostic 	line[linelim] = c;
388*45336Sbostic 	line[linelim+1] = 0;
389*45336Sbostic     }
390*45336Sbostic }
391*45336Sbostic 
replace_in_line(c)392*45336Sbostic replace_in_line(c)
393*45336Sbostic {
394*45336Sbostic     register int len;
395*45336Sbostic 
396*45336Sbostic     len = strlen(line);
397*45336Sbostic     line[linelim++] = c;
398*45336Sbostic     if (linelim > len)
399*45336Sbostic 	line[linelim] = 0;
400*45336Sbostic }
401*45336Sbostic 
back_space()402*45336Sbostic back_space()
403*45336Sbostic {
404*45336Sbostic     if (linelim == 0)
405*45336Sbostic 	return;
406*45336Sbostic 
407*45336Sbostic     if (line[linelim] == 0) {
408*45336Sbostic 	linelim = back_line();
409*45336Sbostic 	del_in_line();
410*45336Sbostic 	linelim = strlen(line);
411*45336Sbostic     } else {
412*45336Sbostic 	linelim = back_line();
413*45336Sbostic 	del_in_line();
414*45336Sbostic     }
415*45336Sbostic }
416*45336Sbostic 
get_motion()417*45336Sbostic get_motion()
418*45336Sbostic {
419*45336Sbostic     int c;
420*45336Sbostic 
421*45336Sbostic     c = vigetch();
422*45336Sbostic     switch (c) {
423*45336Sbostic     case 'b':	return(back_word());
424*45336Sbostic     case 'f':	return(find_char()+1);
425*45336Sbostic     case 'h':	return(back_line());
426*45336Sbostic     case 'l':	return(for_line(1));
427*45336Sbostic     case 't':	return(to_char()+1);
428*45336Sbostic     case 'w':	return(for_word(1));
429*45336Sbostic     default:	return(linelim);
430*45336Sbostic     }
431*45336Sbostic }
432*45336Sbostic 
delete_cmd()433*45336Sbostic delete_cmd()
434*45336Sbostic {
435*45336Sbostic     int cpos;
436*45336Sbostic 
437*45336Sbostic     cpos = get_motion();
438*45336Sbostic     del_chars(cpos, linelim);
439*45336Sbostic }
440*45336Sbostic 
change_cmd()441*45336Sbostic change_cmd()
442*45336Sbostic {
443*45336Sbostic     delete_cmd();
444*45336Sbostic     insert_mode();
445*45336Sbostic }
446*45336Sbostic 
del_chars(first,last)447*45336Sbostic del_chars(first, last)
448*45336Sbostic register int first, last;
449*45336Sbostic {
450*45336Sbostic     int temp;
451*45336Sbostic 
452*45336Sbostic     if (first == last)
453*45336Sbostic 	return;
454*45336Sbostic 
455*45336Sbostic     if (last < first) {
456*45336Sbostic 	temp = last; last = first; first = temp;
457*45336Sbostic     }
458*45336Sbostic 
459*45336Sbostic     linelim = first;
460*45336Sbostic     while(first < last) {
461*45336Sbostic 	del_in_line();
462*45336Sbostic 	--last;
463*45336Sbostic     }
464*45336Sbostic }
465*45336Sbostic 
del_to_end()466*45336Sbostic del_to_end()
467*45336Sbostic {
468*45336Sbostic     if (linelim < 0)
469*45336Sbostic 	return;
470*45336Sbostic     line[linelim] = 0;
471*45336Sbostic     linelim = back_line();
472*45336Sbostic }
473*45336Sbostic 
cr_line()474*45336Sbostic cr_line()
475*45336Sbostic {
476*45336Sbostic     showrange = 0;
477*45336Sbostic     insert_mode();
478*45336Sbostic     save_hist();
479*45336Sbostic     linelim = 0;
480*45336Sbostic     (void) yyparse ();
481*45336Sbostic     linelim = -1;
482*45336Sbostic }
483*45336Sbostic 
484*45336Sbostic /* History functions */
485*45336Sbostic 
save_hist()486*45336Sbostic save_hist()
487*45336Sbostic {
488*45336Sbostic     register int i;
489*45336Sbostic 
490*45336Sbostic     /* free the oldest one */
491*45336Sbostic     if (history[HISTLEN-1]) {
492*45336Sbostic 	xfree(history[HISTLEN-1]);
493*45336Sbostic 	history[HISTLEN-1] = 0;
494*45336Sbostic     }
495*45336Sbostic 
496*45336Sbostic     /* Move the others back */
497*45336Sbostic     for (i = HISTLEN-1; i > 0; --i)
498*45336Sbostic 	history[i] = history[i-1];
499*45336Sbostic 
500*45336Sbostic     history[0] = xmalloc((unsigned) strlen(line)+1);
501*45336Sbostic     strcpy(history[0], line);
502*45336Sbostic }
503*45336Sbostic 
back_hist()504*45336Sbostic back_hist()
505*45336Sbostic {
506*45336Sbostic     if (histp == -1 || histp < HISTLEN-1 && history[histp + 1])
507*45336Sbostic 	histp++;
508*45336Sbostic 
509*45336Sbostic     if (history[histp]) {
510*45336Sbostic     	strcpy(line, history[histp]);
511*45336Sbostic 	linelim = 0;
512*45336Sbostic     } else
513*45336Sbostic 	line[linelim = 0] = 0;
514*45336Sbostic 
515*45336Sbostic }
516*45336Sbostic 
search_hist()517*45336Sbostic search_hist()
518*45336Sbostic {
519*45336Sbostic     if (last_search) {
520*45336Sbostic 	xfree(last_search);
521*45336Sbostic 	last_search = 0;
522*45336Sbostic     }
523*45336Sbostic 
524*45336Sbostic     if(linelim < 1) {
525*45336Sbostic 	linelim = 0;
526*45336Sbostic 	edit_mode();
527*45336Sbostic 	return;
528*45336Sbostic     }
529*45336Sbostic 
530*45336Sbostic     last_search = strcpy(xmalloc((unsigned)(strlen(line+1)+1)), line+1);
531*45336Sbostic     search_again();
532*45336Sbostic     mode = EDIT_MODE;
533*45336Sbostic }
534*45336Sbostic 
search_again()535*45336Sbostic search_again()
536*45336Sbostic {
537*45336Sbostic     int found_it;
538*45336Sbostic     int do_next;
539*45336Sbostic     int prev_histp;
540*45336Sbostic     char *look_here;
541*45336Sbostic 
542*45336Sbostic     prev_histp = histp;
543*45336Sbostic     if (!last_search)
544*45336Sbostic 	return;
545*45336Sbostic 
546*45336Sbostic     do {
547*45336Sbostic 	back_hist();
548*45336Sbostic 	if (prev_histp == histp)
549*45336Sbostic 	    break;
550*45336Sbostic 	prev_histp = histp;
551*45336Sbostic 	look_here = line;
552*45336Sbostic 	found_it = do_next = 0;
553*45336Sbostic 	while ((look_here = strchr(look_here, last_search[0])) &&
554*45336Sbostic 						!found_it && !do_next) {
555*45336Sbostic 
556*45336Sbostic 	    if (strncmp(look_here, last_search, strlen(last_search)) == 0)
557*45336Sbostic 		found_it++;
558*45336Sbostic 	    else if (look_here < line + strlen(line) - 1)
559*45336Sbostic 	        look_here++;
560*45336Sbostic 	    else
561*45336Sbostic 		do_next++;
562*45336Sbostic 	}
563*45336Sbostic     } while (!found_it);
564*45336Sbostic }
565*45336Sbostic 
for_hist()566*45336Sbostic for_hist()
567*45336Sbostic {
568*45336Sbostic     if (histp > 0)
569*45336Sbostic         histp--;
570*45336Sbostic 
571*45336Sbostic     if (histp >= 0 && history[histp]) {
572*45336Sbostic     	strcpy(line, history[histp]);
573*45336Sbostic 	linelim = 0;
574*45336Sbostic     } else
575*45336Sbostic 	line[linelim = 0] = 0;
576*45336Sbostic }
577*45336Sbostic 
col_0()578*45336Sbostic col_0()
579*45336Sbostic {
580*45336Sbostic     linelim = 0;
581*45336Sbostic }
582*45336Sbostic 
last_col()583*45336Sbostic last_col()
584*45336Sbostic {
585*45336Sbostic     linelim = strlen(line);
586*45336Sbostic     if (linelim > 0)
587*45336Sbostic 	--linelim;
588*45336Sbostic }
589*45336Sbostic 
find_char()590*45336Sbostic find_char()
591*45336Sbostic {
592*45336Sbostic     register int c;
593*45336Sbostic     register int i;
594*45336Sbostic 
595*45336Sbostic 
596*45336Sbostic     c = vigetch();
597*45336Sbostic     i = linelim;
598*45336Sbostic     while(line[i] && line[i] != c)
599*45336Sbostic 	i++;
600*45336Sbostic     if (!line[i])
601*45336Sbostic 	i = linelim;
602*45336Sbostic     return(i);
603*45336Sbostic }
604*45336Sbostic 
to_char()605*45336Sbostic to_char()
606*45336Sbostic {
607*45336Sbostic     register int i;
608*45336Sbostic 
609*45336Sbostic     i = find_char();
610*45336Sbostic     if (i > 0 && i != linelim)
611*45336Sbostic 	--i;
612*45336Sbostic 
613*45336Sbostic     return(i);
614*45336Sbostic }
615