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