common.c revision 1.14
1/*	$OpenBSD: common.c,v 1.14 2016/03/20 23:48:27 schwarze Exp $	*/
2/*	$NetBSD: common.c,v 1.24 2009/12/30 22:37:40 christos Exp $	*/
3
4/*-
5 * Copyright (c) 1992, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Christos Zoulas of Cornell University.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "config.h"
37
38/*
39 * common.c: Common Editor functions
40 */
41#include <ctype.h>
42#include <string.h>
43
44#include "el.h"
45#include "common.h"
46#include "parse.h"
47#include "vi.h"
48
49/* ed_end_of_file():
50 *	Indicate end of file
51 *	[^D]
52 */
53protected el_action_t
54/*ARGSUSED*/
55ed_end_of_file(EditLine *el, wint_t c __attribute__((__unused__)))
56{
57
58	re_goto_bottom(el);
59	*el->el_line.lastchar = '\0';
60	return CC_EOF;
61}
62
63
64/* ed_insert():
65 *	Add character to the line
66 *	Insert a character [bound to all insert keys]
67 */
68protected el_action_t
69ed_insert(EditLine *el, wint_t c)
70{
71	int count = el->el_state.argument;
72
73	if (c == '\0')
74		return CC_ERROR;
75
76	if (el->el_line.lastchar + el->el_state.argument >=
77	    el->el_line.limit) {
78		/* end of buffer space, try to allocate more */
79		if (!ch_enlargebufs(el, (size_t) count))
80			return CC_ERROR;	/* error allocating more */
81	}
82
83	if (count == 1) {
84		if (el->el_state.inputmode == MODE_INSERT
85		    || el->el_line.cursor >= el->el_line.lastchar)
86			c_insert(el, 1);
87
88		*el->el_line.cursor++ = c;
89		re_fastaddc(el);		/* fast refresh for one char. */
90	} else {
91		if (el->el_state.inputmode != MODE_REPLACE_1)
92			c_insert(el, el->el_state.argument);
93
94		while (count-- && el->el_line.cursor < el->el_line.lastchar)
95			*el->el_line.cursor++ = c;
96		re_refresh(el);
97	}
98
99	if (el->el_state.inputmode == MODE_REPLACE_1)
100		return vi_command_mode(el, 0);
101
102	return CC_NORM;
103}
104
105
106/* ed_delete_prev_word():
107 *	Delete from beginning of current word to cursor
108 *	[M-^?] [^W]
109 */
110protected el_action_t
111/*ARGSUSED*/
112ed_delete_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
113{
114	Char *cp, *p, *kp;
115
116	if (el->el_line.cursor == el->el_line.buffer)
117		return CC_ERROR;
118
119	cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
120	    el->el_state.argument, ce__isword);
121
122	for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
123		*kp++ = *p;
124	el->el_chared.c_kill.last = kp;
125
126	c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
127	el->el_line.cursor = cp;
128	if (el->el_line.cursor < el->el_line.buffer)
129		el->el_line.cursor = el->el_line.buffer; /* bounds check */
130	return CC_REFRESH;
131}
132
133
134/* ed_delete_next_char():
135 *	Delete character under cursor
136 *	[^D] [x]
137 */
138protected el_action_t
139/*ARGSUSED*/
140ed_delete_next_char(EditLine *el, wint_t c)
141{
142#ifdef notdef			/* XXX */
143#define	EL	el->el_line
144	(void) fprintf(el->el_errlfile,
145	    "\nD(b: %x(%s)  c: %x(%s) last: %x(%s) limit: %x(%s)\n",
146	    EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
147	    EL.lastchar, EL.limit, EL.limit);
148#endif
149	if (el->el_line.cursor == el->el_line.lastchar) {
150			/* if I'm at the end */
151		if (el->el_map.type == MAP_VI) {
152			if (el->el_line.cursor == el->el_line.buffer) {
153				/* if I'm also at the beginning */
154#ifdef KSHVI
155				return CC_ERROR;
156#else
157				/* then do an EOF */
158				terminal_writec(el, c);
159				return CC_EOF;
160#endif
161			} else {
162#ifdef KSHVI
163				el->el_line.cursor--;
164#else
165				return CC_ERROR;
166#endif
167			}
168		} else {
169			if (el->el_line.cursor != el->el_line.buffer)
170				el->el_line.cursor--;
171			else
172				return CC_ERROR;
173		}
174	}
175	c_delafter(el, el->el_state.argument);	/* delete after dot */
176	if (el->el_line.cursor >= el->el_line.lastchar &&
177	    el->el_line.cursor > el->el_line.buffer)
178			/* bounds check */
179		el->el_line.cursor = el->el_line.lastchar - 1;
180	return CC_REFRESH;
181}
182
183
184/* ed_kill_line():
185 *	Cut to the end of line
186 *	[^K] [^K]
187 */
188protected el_action_t
189/*ARGSUSED*/
190ed_kill_line(EditLine *el, wint_t c __attribute__((__unused__)))
191{
192	Char *kp, *cp;
193
194	cp = el->el_line.cursor;
195	kp = el->el_chared.c_kill.buf;
196	while (cp < el->el_line.lastchar)
197		*kp++ = *cp++;	/* copy it */
198	el->el_chared.c_kill.last = kp;
199			/* zap! -- delete to end */
200	el->el_line.lastchar = el->el_line.cursor;
201	return CC_REFRESH;
202}
203
204
205/* ed_move_to_end():
206 *	Move cursor to the end of line
207 *	[^E] [^E]
208 */
209protected el_action_t
210/*ARGSUSED*/
211ed_move_to_end(EditLine *el, wint_t c __attribute__((__unused__)))
212{
213
214	el->el_line.cursor = el->el_line.lastchar;
215	if (el->el_map.type == MAP_VI) {
216		if (el->el_chared.c_vcmd.action != NOP) {
217			cv_delfini(el);
218			return CC_REFRESH;
219		}
220#ifdef VI_MOVE
221		el->el_line.cursor--;
222#endif
223	}
224	return CC_CURSOR;
225}
226
227
228/* ed_move_to_beg():
229 *	Move cursor to the beginning of line
230 *	[^A] [^A]
231 */
232protected el_action_t
233/*ARGSUSED*/
234ed_move_to_beg(EditLine *el, wint_t c __attribute__((__unused__)))
235{
236
237	el->el_line.cursor = el->el_line.buffer;
238
239	if (el->el_map.type == MAP_VI) {
240			/* We want FIRST non space character */
241		while (Isspace(*el->el_line.cursor))
242			el->el_line.cursor++;
243		if (el->el_chared.c_vcmd.action != NOP) {
244			cv_delfini(el);
245			return CC_REFRESH;
246		}
247	}
248	return CC_CURSOR;
249}
250
251
252/* ed_transpose_chars():
253 *	Exchange the character to the left of the cursor with the one under it
254 *	[^T] [^T]
255 */
256protected el_action_t
257ed_transpose_chars(EditLine *el, wint_t c)
258{
259
260	if (el->el_line.cursor < el->el_line.lastchar) {
261		if (el->el_line.lastchar <= &el->el_line.buffer[1])
262			return CC_ERROR;
263		else
264			el->el_line.cursor++;
265	}
266	if (el->el_line.cursor > &el->el_line.buffer[1]) {
267		/* must have at least two chars entered */
268		c = el->el_line.cursor[-2];
269		el->el_line.cursor[-2] = el->el_line.cursor[-1];
270		el->el_line.cursor[-1] = c;
271		return CC_REFRESH;
272	} else
273		return CC_ERROR;
274}
275
276
277/* ed_next_char():
278 *	Move to the right one character
279 *	[^F] [^F]
280 */
281protected el_action_t
282/*ARGSUSED*/
283ed_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
284{
285	Char *lim = el->el_line.lastchar;
286
287	if (el->el_line.cursor >= lim ||
288	    (el->el_line.cursor == lim - 1 &&
289	    el->el_map.type == MAP_VI &&
290	    el->el_chared.c_vcmd.action == NOP))
291		return CC_ERROR;
292
293	el->el_line.cursor += el->el_state.argument;
294	if (el->el_line.cursor > lim)
295		el->el_line.cursor = lim;
296
297	if (el->el_map.type == MAP_VI)
298		if (el->el_chared.c_vcmd.action != NOP) {
299			cv_delfini(el);
300			return CC_REFRESH;
301		}
302	return CC_CURSOR;
303}
304
305
306/* ed_prev_word():
307 *	Move to the beginning of the current word
308 *	[M-b] [b]
309 */
310protected el_action_t
311/*ARGSUSED*/
312ed_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
313{
314
315	if (el->el_line.cursor == el->el_line.buffer)
316		return CC_ERROR;
317
318	el->el_line.cursor = c__prev_word(el->el_line.cursor,
319	    el->el_line.buffer,
320	    el->el_state.argument,
321	    ce__isword);
322
323	if (el->el_map.type == MAP_VI)
324		if (el->el_chared.c_vcmd.action != NOP) {
325			cv_delfini(el);
326			return CC_REFRESH;
327		}
328	return CC_CURSOR;
329}
330
331
332/* ed_prev_char():
333 *	Move to the left one character
334 *	[^B] [^B]
335 */
336protected el_action_t
337/*ARGSUSED*/
338ed_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
339{
340
341	if (el->el_line.cursor > el->el_line.buffer) {
342		el->el_line.cursor -= el->el_state.argument;
343		if (el->el_line.cursor < el->el_line.buffer)
344			el->el_line.cursor = el->el_line.buffer;
345
346		if (el->el_map.type == MAP_VI)
347			if (el->el_chared.c_vcmd.action != NOP) {
348				cv_delfini(el);
349				return CC_REFRESH;
350			}
351		return CC_CURSOR;
352	} else
353		return CC_ERROR;
354}
355
356
357/* ed_quoted_insert():
358 *	Add the next character typed verbatim
359 *	[^V] [^V]
360 */
361protected el_action_t
362ed_quoted_insert(EditLine *el, wint_t c)
363{
364	int num;
365	Char tc;
366
367	tty_quotemode(el);
368	num = FUN(el,getc)(el, &tc);
369	c = tc;
370	tty_noquotemode(el);
371	if (num == 1)
372		return ed_insert(el, c);
373	else
374		return ed_end_of_file(el, 0);
375}
376
377
378/* ed_digit():
379 *	Adds to argument or enters a digit
380 */
381protected el_action_t
382ed_digit(EditLine *el, wint_t c)
383{
384
385	if (!Isdigit(c))
386		return CC_ERROR;
387
388	if (el->el_state.doingarg) {
389			/* if doing an arg, add this in... */
390		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
391			el->el_state.argument = c - '0';
392		else {
393			if (el->el_state.argument > 1000000)
394				return CC_ERROR;
395			el->el_state.argument =
396			    (el->el_state.argument * 10) + (c - '0');
397		}
398		return CC_ARGHACK;
399	}
400
401	return ed_insert(el, c);
402}
403
404
405/* ed_argument_digit():
406 *	Digit that starts argument
407 *	For ESC-n
408 */
409protected el_action_t
410ed_argument_digit(EditLine *el, wint_t c)
411{
412
413	if (!Isdigit(c))
414		return CC_ERROR;
415
416	if (el->el_state.doingarg) {
417		if (el->el_state.argument > 1000000)
418			return CC_ERROR;
419		el->el_state.argument = (el->el_state.argument * 10) +
420		    (c - '0');
421	} else {		/* else starting an argument */
422		el->el_state.argument = c - '0';
423		el->el_state.doingarg = 1;
424	}
425	return CC_ARGHACK;
426}
427
428
429/* ed_unassigned():
430 *	Indicates unbound character
431 *	Bound to keys that are not assigned
432 */
433protected el_action_t
434/*ARGSUSED*/
435ed_unassigned(EditLine *el, wint_t c __attribute__((__unused__)))
436{
437
438	return CC_ERROR;
439}
440
441
442/**
443 ** TTY key handling.
444 **/
445
446/* ed_tty_sigint():
447 *	Tty interrupt character
448 *	[^C]
449 */
450protected el_action_t
451/*ARGSUSED*/
452ed_tty_sigint(EditLine *el __attribute__((__unused__)),
453	      wint_t c __attribute__((__unused__)))
454{
455
456	return CC_NORM;
457}
458
459
460/* ed_tty_dsusp():
461 *	Tty delayed suspend character
462 *	[^Y]
463 */
464protected el_action_t
465/*ARGSUSED*/
466ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
467	     wint_t c __attribute__((__unused__)))
468{
469
470	return CC_NORM;
471}
472
473
474/* ed_tty_flush_output():
475 *	Tty flush output characters
476 *	[^O]
477 */
478protected el_action_t
479/*ARGSUSED*/
480ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
481		    wint_t c __attribute__((__unused__)))
482{
483
484	return CC_NORM;
485}
486
487
488/* ed_tty_sigquit():
489 *	Tty quit character
490 *	[^\]
491 */
492protected el_action_t
493/*ARGSUSED*/
494ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
495	       wint_t c __attribute__((__unused__)))
496{
497
498	return CC_NORM;
499}
500
501
502/* ed_tty_sigtstp():
503 *	Tty suspend character
504 *	[^Z]
505 */
506protected el_action_t
507/*ARGSUSED*/
508ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
509	       wint_t c __attribute__((__unused__)))
510{
511
512	return CC_NORM;
513}
514
515
516/* ed_tty_stop_output():
517 *	Tty disallow output characters
518 *	[^S]
519 */
520protected el_action_t
521/*ARGSUSED*/
522ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
523		   wint_t c __attribute__((__unused__)))
524{
525
526	return CC_NORM;
527}
528
529
530/* ed_tty_start_output():
531 *	Tty allow output characters
532 *	[^Q]
533 */
534protected el_action_t
535/*ARGSUSED*/
536ed_tty_start_output(EditLine *el __attribute__((__unused__)),
537		    wint_t c __attribute__((__unused__)))
538{
539
540	return CC_NORM;
541}
542
543
544/* ed_newline():
545 *	Execute command
546 *	[^J]
547 */
548protected el_action_t
549/*ARGSUSED*/
550ed_newline(EditLine *el, wint_t c __attribute__((__unused__)))
551{
552
553	re_goto_bottom(el);
554	*el->el_line.lastchar++ = '\n';
555	*el->el_line.lastchar = '\0';
556	return CC_NEWLINE;
557}
558
559
560/* ed_delete_prev_char():
561 *	Delete the character to the left of the cursor
562 *	[^?]
563 */
564protected el_action_t
565/*ARGSUSED*/
566ed_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
567{
568
569	if (el->el_line.cursor <= el->el_line.buffer)
570		return CC_ERROR;
571
572	c_delbefore(el, el->el_state.argument);
573	el->el_line.cursor -= el->el_state.argument;
574	if (el->el_line.cursor < el->el_line.buffer)
575		el->el_line.cursor = el->el_line.buffer;
576	return CC_REFRESH;
577}
578
579
580/* ed_clear_screen():
581 *	Clear screen leaving current line at the top
582 *	[^L]
583 */
584protected el_action_t
585/*ARGSUSED*/
586ed_clear_screen(EditLine *el, wint_t c __attribute__((__unused__)))
587{
588
589	terminal_clear_screen(el);	/* clear the whole real screen */
590	re_clear_display(el);	/* reset everything */
591	return CC_REFRESH;
592}
593
594
595/* ed_redisplay():
596 *	Redisplay everything
597 *	^R
598 */
599protected el_action_t
600/*ARGSUSED*/
601ed_redisplay(EditLine *el __attribute__((__unused__)),
602	     wint_t c __attribute__((__unused__)))
603{
604
605	return CC_REDISPLAY;
606}
607
608
609/* ed_start_over():
610 *	Erase current line and start from scratch
611 *	[^G]
612 */
613protected el_action_t
614/*ARGSUSED*/
615ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
616{
617
618	ch_reset(el, 0);
619	return CC_REFRESH;
620}
621
622
623/* ed_sequence_lead_in():
624 *	First character in a bound sequence
625 *	Placeholder for external keys
626 */
627protected el_action_t
628/*ARGSUSED*/
629ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
630		    wint_t c __attribute__((__unused__)))
631{
632
633	return CC_NORM;
634}
635
636
637/* ed_prev_history():
638 *	Move to the previous history line
639 *	[^P] [k]
640 */
641protected el_action_t
642/*ARGSUSED*/
643ed_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
644{
645	char beep = 0;
646	int sv_event = el->el_history.eventno;
647
648	el->el_chared.c_undo.len = -1;
649	*el->el_line.lastchar = '\0';		/* just in case */
650
651	if (el->el_history.eventno == 0) {	/* save the current buffer
652						 * away */
653		(void) Strncpy(el->el_history.buf, el->el_line.buffer,
654		    EL_BUFSIZ);
655		el->el_history.last = el->el_history.buf +
656		    (el->el_line.lastchar - el->el_line.buffer);
657	}
658	el->el_history.eventno += el->el_state.argument;
659
660	if (hist_get(el) == CC_ERROR) {
661		if (el->el_map.type == MAP_VI) {
662			el->el_history.eventno = sv_event;
663		}
664		beep = 1;
665		/* el->el_history.eventno was fixed by first call */
666		(void) hist_get(el);
667	}
668	if (beep)
669		return CC_REFRESH_BEEP;
670	return CC_REFRESH;
671}
672
673
674/* ed_next_history():
675 *	Move to the next history line
676 *	[^N] [j]
677 */
678protected el_action_t
679/*ARGSUSED*/
680ed_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
681{
682	el_action_t beep = CC_REFRESH, rval;
683
684	el->el_chared.c_undo.len = -1;
685	*el->el_line.lastchar = '\0';	/* just in case */
686
687	el->el_history.eventno -= el->el_state.argument;
688
689	if (el->el_history.eventno < 0) {
690		el->el_history.eventno = 0;
691		beep = CC_REFRESH_BEEP;
692	}
693	rval = hist_get(el);
694	if (rval == CC_REFRESH)
695		return beep;
696	return rval;
697
698}
699
700
701/* ed_search_prev_history():
702 *	Search previous in history for a line matching the current
703 *	next search history [M-P] [K]
704 */
705protected el_action_t
706/*ARGSUSED*/
707ed_search_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
708{
709	const Char *hp;
710	int h;
711	int found = 0;
712
713	el->el_chared.c_vcmd.action = NOP;
714	el->el_chared.c_undo.len = -1;
715	*el->el_line.lastchar = '\0';	/* just in case */
716	if (el->el_history.eventno < 0) {
717#ifdef DEBUG_EDIT
718		(void) fprintf(el->el_errfile,
719		    "e_prev_search_hist(): eventno < 0;\n");
720#endif
721		el->el_history.eventno = 0;
722		return CC_ERROR;
723	}
724	if (el->el_history.eventno == 0) {
725		(void) Strncpy(el->el_history.buf, el->el_line.buffer,
726		    EL_BUFSIZ);
727		el->el_history.last = el->el_history.buf +
728		    (el->el_line.lastchar - el->el_line.buffer);
729	}
730	if (el->el_history.ref == NULL)
731		return CC_ERROR;
732
733	hp = HIST_FIRST(el);
734	if (hp == NULL)
735		return CC_ERROR;
736
737	c_setpat(el);		/* Set search pattern !! */
738
739	for (h = 1; h <= el->el_history.eventno; h++)
740		hp = HIST_NEXT(el);
741
742	while (hp != NULL) {
743#ifdef SDEBUG
744		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
745#endif
746		if ((Strncmp(hp, el->el_line.buffer, (size_t)
747			    (el->el_line.lastchar - el->el_line.buffer)) ||
748			hp[el->el_line.lastchar - el->el_line.buffer]) &&
749		    c_hmatch(el, hp)) {
750			found = 1;
751			break;
752		}
753		h++;
754		hp = HIST_NEXT(el);
755	}
756
757	if (!found) {
758#ifdef SDEBUG
759		(void) fprintf(el->el_errfile, "not found\n");
760#endif
761		return CC_ERROR;
762	}
763	el->el_history.eventno = h;
764
765	return hist_get(el);
766}
767
768
769/* ed_search_next_history():
770 *	Search next in history for a line matching the current
771 *	[M-N] [J]
772 */
773protected el_action_t
774/*ARGSUSED*/
775ed_search_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
776{
777	const Char *hp;
778	int h;
779	int found = 0;
780
781	el->el_chared.c_vcmd.action = NOP;
782	el->el_chared.c_undo.len = -1;
783	*el->el_line.lastchar = '\0';	/* just in case */
784
785	if (el->el_history.eventno == 0)
786		return CC_ERROR;
787
788	if (el->el_history.ref == NULL)
789		return CC_ERROR;
790
791	hp = HIST_FIRST(el);
792	if (hp == NULL)
793		return CC_ERROR;
794
795	c_setpat(el);		/* Set search pattern !! */
796
797	for (h = 1; h < el->el_history.eventno && hp; h++) {
798#ifdef SDEBUG
799		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
800#endif
801		if ((Strncmp(hp, el->el_line.buffer, (size_t)
802			    (el->el_line.lastchar - el->el_line.buffer)) ||
803			hp[el->el_line.lastchar - el->el_line.buffer]) &&
804		    c_hmatch(el, hp))
805			found = h;
806		hp = HIST_NEXT(el);
807	}
808
809	if (!found) {		/* is it the current history number? */
810		if (!c_hmatch(el, el->el_history.buf)) {
811#ifdef SDEBUG
812			(void) fprintf(el->el_errfile, "not found\n");
813#endif
814			return CC_ERROR;
815		}
816	}
817	el->el_history.eventno = found;
818
819	return hist_get(el);
820}
821
822
823/* ed_prev_line():
824 *	Move up one line
825 *	Could be [k] [^p]
826 */
827protected el_action_t
828/*ARGSUSED*/
829ed_prev_line(EditLine *el, wint_t c __attribute__((__unused__)))
830{
831	Char *ptr;
832	int nchars = c_hpos(el);
833
834	/*
835         * Move to the line requested
836         */
837	if (*(ptr = el->el_line.cursor) == '\n')
838		ptr--;
839
840	for (; ptr >= el->el_line.buffer; ptr--)
841		if (*ptr == '\n' && --el->el_state.argument <= 0)
842			break;
843
844	if (el->el_state.argument > 0)
845		return CC_ERROR;
846
847	/*
848         * Move to the beginning of the line
849         */
850	for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
851		continue;
852
853	/*
854         * Move to the character requested
855         */
856	for (ptr++;
857	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
858	    ptr++)
859		continue;
860
861	el->el_line.cursor = ptr;
862	return CC_CURSOR;
863}
864
865
866/* ed_next_line():
867 *	Move down one line
868 *	Could be [j] [^n]
869 */
870protected el_action_t
871/*ARGSUSED*/
872ed_next_line(EditLine *el, wint_t c __attribute__((__unused__)))
873{
874	Char *ptr;
875	int nchars = c_hpos(el);
876
877	/*
878         * Move to the line requested
879         */
880	for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
881		if (*ptr == '\n' && --el->el_state.argument <= 0)
882			break;
883
884	if (el->el_state.argument > 0)
885		return CC_ERROR;
886
887	/*
888         * Move to the character requested
889         */
890	for (ptr++;
891	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
892	    ptr++)
893		continue;
894
895	el->el_line.cursor = ptr;
896	return CC_CURSOR;
897}
898
899
900/* ed_command():
901 *	Editline extended command
902 *	[M-X] [:]
903 */
904protected el_action_t
905/*ARGSUSED*/
906ed_command(EditLine *el, wint_t c __attribute__((__unused__)))
907{
908	Char tmpbuf[EL_BUFSIZ];
909	int tmplen;
910
911	tmplen = c_gets(el, tmpbuf, STR("\n: "));
912	terminal__putc(el, '\n');
913
914	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
915		terminal_beep(el);
916
917	el->el_map.current = el->el_map.key;
918	re_clear_display(el);
919	return CC_REFRESH;
920}
921