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