Home | History | Annotate | Line # | Download | only in libedit
common.c revision 1.44
      1 /*	$NetBSD: common.c,v 1.44 2016/04/17 18:39: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.44 2016/04/17 18:39: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++ = 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++ = 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 	wchar_t *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_errfile,
    151 	    "\nD(b: %p(%ls)  c: %p(%ls) last: %p(%ls) limit: %p(%ls)\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 	wchar_t *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 (iswspace(*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] = 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 	wchar_t *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 
    369 	tty_quotemode(el);
    370 	num = el_wgetc(el, &c);
    371 	tty_noquotemode(el);
    372 	if (num == 1)
    373 		return ed_insert(el, c);
    374 	else
    375 		return ed_end_of_file(el, 0);
    376 }
    377 
    378 
    379 /* ed_digit():
    380  *	Adds to argument or enters a digit
    381  */
    382 protected el_action_t
    383 ed_digit(EditLine *el, wint_t c)
    384 {
    385 
    386 	if (!iswdigit(c))
    387 		return CC_ERROR;
    388 
    389 	if (el->el_state.doingarg) {
    390 			/* if doing an arg, add this in... */
    391 		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
    392 			el->el_state.argument = c - '0';
    393 		else {
    394 			if (el->el_state.argument > 1000000)
    395 				return CC_ERROR;
    396 			el->el_state.argument =
    397 			    (el->el_state.argument * 10) + (c - '0');
    398 		}
    399 		return CC_ARGHACK;
    400 	}
    401 
    402 	return ed_insert(el, c);
    403 }
    404 
    405 
    406 /* ed_argument_digit():
    407  *	Digit that starts argument
    408  *	For ESC-n
    409  */
    410 protected el_action_t
    411 ed_argument_digit(EditLine *el, wint_t c)
    412 {
    413 
    414 	if (!iswdigit(c))
    415 		return CC_ERROR;
    416 
    417 	if (el->el_state.doingarg) {
    418 		if (el->el_state.argument > 1000000)
    419 			return CC_ERROR;
    420 		el->el_state.argument = (el->el_state.argument * 10) +
    421 		    (c - '0');
    422 	} else {		/* else starting an argument */
    423 		el->el_state.argument = c - '0';
    424 		el->el_state.doingarg = 1;
    425 	}
    426 	return CC_ARGHACK;
    427 }
    428 
    429 
    430 /* ed_unassigned():
    431  *	Indicates unbound character
    432  *	Bound to keys that are not assigned
    433  */
    434 protected el_action_t
    435 /*ARGSUSED*/
    436 ed_unassigned(EditLine *el __attribute__((__unused__)),
    437     wint_t c __attribute__((__unused__)))
    438 {
    439 
    440 	return CC_ERROR;
    441 }
    442 
    443 
    444 /* ed_ignore():
    445  *	Input characters that have no effect
    446  *	[^C ^O ^Q ^S ^Z ^\ ^]] [^C ^O ^Q ^S ^\]
    447  */
    448 protected el_action_t
    449 /*ARGSUSED*/
    450 ed_ignore(EditLine *el __attribute__((__unused__)),
    451 	      wint_t c __attribute__((__unused__)))
    452 {
    453 
    454 	return CC_NORM;
    455 }
    456 
    457 
    458 /* ed_newline():
    459  *	Execute command
    460  *	[^J]
    461  */
    462 protected el_action_t
    463 /*ARGSUSED*/
    464 ed_newline(EditLine *el, wint_t c __attribute__((__unused__)))
    465 {
    466 
    467 	re_goto_bottom(el);
    468 	*el->el_line.lastchar++ = '\n';
    469 	*el->el_line.lastchar = '\0';
    470 	return CC_NEWLINE;
    471 }
    472 
    473 
    474 /* ed_delete_prev_char():
    475  *	Delete the character to the left of the cursor
    476  *	[^?]
    477  */
    478 protected el_action_t
    479 /*ARGSUSED*/
    480 ed_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    481 {
    482 
    483 	if (el->el_line.cursor <= el->el_line.buffer)
    484 		return CC_ERROR;
    485 
    486 	c_delbefore(el, el->el_state.argument);
    487 	el->el_line.cursor -= el->el_state.argument;
    488 	if (el->el_line.cursor < el->el_line.buffer)
    489 		el->el_line.cursor = el->el_line.buffer;
    490 	return CC_REFRESH;
    491 }
    492 
    493 
    494 /* ed_clear_screen():
    495  *	Clear screen leaving current line at the top
    496  *	[^L]
    497  */
    498 protected el_action_t
    499 /*ARGSUSED*/
    500 ed_clear_screen(EditLine *el, wint_t c __attribute__((__unused__)))
    501 {
    502 
    503 	terminal_clear_screen(el);	/* clear the whole real screen */
    504 	re_clear_display(el);	/* reset everything */
    505 	return CC_REFRESH;
    506 }
    507 
    508 
    509 /* ed_redisplay():
    510  *	Redisplay everything
    511  *	^R
    512  */
    513 protected el_action_t
    514 /*ARGSUSED*/
    515 ed_redisplay(EditLine *el __attribute__((__unused__)),
    516 	     wint_t c __attribute__((__unused__)))
    517 {
    518 
    519 	return CC_REDISPLAY;
    520 }
    521 
    522 
    523 /* ed_start_over():
    524  *	Erase current line and start from scratch
    525  *	[^G]
    526  */
    527 protected el_action_t
    528 /*ARGSUSED*/
    529 ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
    530 {
    531 
    532 	ch_reset(el, 0);
    533 	return CC_REFRESH;
    534 }
    535 
    536 
    537 /* ed_sequence_lead_in():
    538  *	First character in a bound sequence
    539  *	Placeholder for external keys
    540  */
    541 protected el_action_t
    542 /*ARGSUSED*/
    543 ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
    544 		    wint_t c __attribute__((__unused__)))
    545 {
    546 
    547 	return CC_NORM;
    548 }
    549 
    550 
    551 /* ed_prev_history():
    552  *	Move to the previous history line
    553  *	[^P] [k]
    554  */
    555 protected el_action_t
    556 /*ARGSUSED*/
    557 ed_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
    558 {
    559 	char beep = 0;
    560 	int sv_event = el->el_history.eventno;
    561 
    562 	el->el_chared.c_undo.len = -1;
    563 	*el->el_line.lastchar = '\0';		/* just in case */
    564 
    565 	if (el->el_history.eventno == 0) {	/* save the current buffer
    566 						 * away */
    567 		(void) wcsncpy(el->el_history.buf, el->el_line.buffer,
    568 		    EL_BUFSIZ);
    569 		el->el_history.last = el->el_history.buf +
    570 		    (el->el_line.lastchar - el->el_line.buffer);
    571 	}
    572 	el->el_history.eventno += el->el_state.argument;
    573 
    574 	if (hist_get(el) == CC_ERROR) {
    575 		if (el->el_map.type == MAP_VI) {
    576 			el->el_history.eventno = sv_event;
    577 		}
    578 		beep = 1;
    579 		/* el->el_history.eventno was fixed by first call */
    580 		(void) hist_get(el);
    581 	}
    582 	if (beep)
    583 		return CC_REFRESH_BEEP;
    584 	return CC_REFRESH;
    585 }
    586 
    587 
    588 /* ed_next_history():
    589  *	Move to the next history line
    590  *	[^N] [j]
    591  */
    592 protected el_action_t
    593 /*ARGSUSED*/
    594 ed_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
    595 {
    596 	el_action_t beep = CC_REFRESH, rval;
    597 
    598 	el->el_chared.c_undo.len = -1;
    599 	*el->el_line.lastchar = '\0';	/* just in case */
    600 
    601 	el->el_history.eventno -= el->el_state.argument;
    602 
    603 	if (el->el_history.eventno < 0) {
    604 		el->el_history.eventno = 0;
    605 		beep = CC_REFRESH_BEEP;
    606 	}
    607 	rval = hist_get(el);
    608 	if (rval == CC_REFRESH)
    609 		return beep;
    610 	return rval;
    611 
    612 }
    613 
    614 
    615 /* ed_search_prev_history():
    616  *	Search previous in history for a line matching the current
    617  *	next search history [M-P] [K]
    618  */
    619 protected el_action_t
    620 /*ARGSUSED*/
    621 ed_search_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
    622 {
    623 	const wchar_t *hp;
    624 	int h;
    625 	int found = 0;
    626 
    627 	el->el_chared.c_vcmd.action = NOP;
    628 	el->el_chared.c_undo.len = -1;
    629 	*el->el_line.lastchar = '\0';	/* just in case */
    630 	if (el->el_history.eventno < 0) {
    631 #ifdef DEBUG_EDIT
    632 		(void) fprintf(el->el_errfile,
    633 		    "e_prev_search_hist(): eventno < 0;\n");
    634 #endif
    635 		el->el_history.eventno = 0;
    636 		return CC_ERROR;
    637 	}
    638 	if (el->el_history.eventno == 0) {
    639 		(void) wcsncpy(el->el_history.buf, el->el_line.buffer,
    640 		    EL_BUFSIZ);
    641 		el->el_history.last = el->el_history.buf +
    642 		    (el->el_line.lastchar - el->el_line.buffer);
    643 	}
    644 	if (el->el_history.ref == NULL)
    645 		return CC_ERROR;
    646 
    647 	hp = HIST_FIRST(el);
    648 	if (hp == NULL)
    649 		return CC_ERROR;
    650 
    651 	c_setpat(el);		/* Set search pattern !! */
    652 
    653 	for (h = 1; h <= el->el_history.eventno; h++)
    654 		hp = HIST_NEXT(el);
    655 
    656 	while (hp != NULL) {
    657 #ifdef SDEBUG
    658 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    659 #endif
    660 		if ((wcsncmp(hp, el->el_line.buffer, (size_t)
    661 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    662 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    663 		    c_hmatch(el, hp)) {
    664 			found = 1;
    665 			break;
    666 		}
    667 		h++;
    668 		hp = HIST_NEXT(el);
    669 	}
    670 
    671 	if (!found) {
    672 #ifdef SDEBUG
    673 		(void) fprintf(el->el_errfile, "not found\n");
    674 #endif
    675 		return CC_ERROR;
    676 	}
    677 	el->el_history.eventno = h;
    678 
    679 	return hist_get(el);
    680 }
    681 
    682 
    683 /* ed_search_next_history():
    684  *	Search next in history for a line matching the current
    685  *	[M-N] [J]
    686  */
    687 protected el_action_t
    688 /*ARGSUSED*/
    689 ed_search_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
    690 {
    691 	const wchar_t *hp;
    692 	int h;
    693 	int found = 0;
    694 
    695 	el->el_chared.c_vcmd.action = NOP;
    696 	el->el_chared.c_undo.len = -1;
    697 	*el->el_line.lastchar = '\0';	/* just in case */
    698 
    699 	if (el->el_history.eventno == 0)
    700 		return CC_ERROR;
    701 
    702 	if (el->el_history.ref == NULL)
    703 		return CC_ERROR;
    704 
    705 	hp = HIST_FIRST(el);
    706 	if (hp == NULL)
    707 		return CC_ERROR;
    708 
    709 	c_setpat(el);		/* Set search pattern !! */
    710 
    711 	for (h = 1; h < el->el_history.eventno && hp; h++) {
    712 #ifdef SDEBUG
    713 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    714 #endif
    715 		if ((wcsncmp(hp, el->el_line.buffer, (size_t)
    716 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    717 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    718 		    c_hmatch(el, hp))
    719 			found = h;
    720 		hp = HIST_NEXT(el);
    721 	}
    722 
    723 	if (!found) {		/* is it the current history number? */
    724 		if (!c_hmatch(el, el->el_history.buf)) {
    725 #ifdef SDEBUG
    726 			(void) fprintf(el->el_errfile, "not found\n");
    727 #endif
    728 			return CC_ERROR;
    729 		}
    730 	}
    731 	el->el_history.eventno = found;
    732 
    733 	return hist_get(el);
    734 }
    735 
    736 
    737 /* ed_prev_line():
    738  *	Move up one line
    739  *	Could be [k] [^p]
    740  */
    741 protected el_action_t
    742 /*ARGSUSED*/
    743 ed_prev_line(EditLine *el, wint_t c __attribute__((__unused__)))
    744 {
    745 	wchar_t *ptr;
    746 	int nchars = c_hpos(el);
    747 
    748 	/*
    749          * Move to the line requested
    750          */
    751 	if (*(ptr = el->el_line.cursor) == '\n')
    752 		ptr--;
    753 
    754 	for (; ptr >= el->el_line.buffer; ptr--)
    755 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    756 			break;
    757 
    758 	if (el->el_state.argument > 0)
    759 		return CC_ERROR;
    760 
    761 	/*
    762          * Move to the beginning of the line
    763          */
    764 	for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
    765 		continue;
    766 
    767 	/*
    768          * Move to the character requested
    769          */
    770 	for (ptr++;
    771 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    772 	    ptr++)
    773 		continue;
    774 
    775 	el->el_line.cursor = ptr;
    776 	return CC_CURSOR;
    777 }
    778 
    779 
    780 /* ed_next_line():
    781  *	Move down one line
    782  *	Could be [j] [^n]
    783  */
    784 protected el_action_t
    785 /*ARGSUSED*/
    786 ed_next_line(EditLine *el, wint_t c __attribute__((__unused__)))
    787 {
    788 	wchar_t *ptr;
    789 	int nchars = c_hpos(el);
    790 
    791 	/*
    792          * Move to the line requested
    793          */
    794 	for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
    795 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    796 			break;
    797 
    798 	if (el->el_state.argument > 0)
    799 		return CC_ERROR;
    800 
    801 	/*
    802          * Move to the character requested
    803          */
    804 	for (ptr++;
    805 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    806 	    ptr++)
    807 		continue;
    808 
    809 	el->el_line.cursor = ptr;
    810 	return CC_CURSOR;
    811 }
    812 
    813 
    814 /* ed_command():
    815  *	Editline extended command
    816  *	[M-X] [:]
    817  */
    818 protected el_action_t
    819 /*ARGSUSED*/
    820 ed_command(EditLine *el, wint_t c __attribute__((__unused__)))
    821 {
    822 	wchar_t tmpbuf[EL_BUFSIZ];
    823 	int tmplen;
    824 
    825 	tmplen = c_gets(el, tmpbuf, L"\n: ");
    826 	terminal__putc(el, '\n');
    827 
    828 	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
    829 		terminal_beep(el);
    830 
    831 	el->el_map.current = el->el_map.key;
    832 	re_clear_display(el);
    833 	return CC_REFRESH;
    834 }
    835