common.c 19.4 KB
Newer Older
1
/*	$NetBSD: common.c,v 1.21 2008/09/30 08:37:42 aymeric Exp $	*/
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Christos Zoulas of Cornell University.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
unknown's avatar
unknown committed
18
 * 3. Neither the name of the University nor the names of its contributors
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

35 36 37 38 39 40 41
#include "config.h"
#if !defined(lint) && !defined(SCCSID)
#if 0
static char sccsid[] = "@(#)common.c	8.1 (Berkeley) 6/4/93";
#else
#endif
#endif /* not lint && not SCCSID */
42 43 44 45 46 47 48 49 50 51 52 53

/*
 * common.c: Common Editor functions
 */
#include "el.h"

/* ed_end_of_file():
 *	Indicate end of file
 *	[^D]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
54
ed_end_of_file(EditLine *el, int c __attribute__((__unused__)))
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
{

	re_goto_bottom(el);
	*el->el_line.lastchar = '\0';
	return (CC_EOF);
}


/* ed_insert():
 *	Add character to the line
 *	Insert a character [bound to all insert keys]
 */
protected el_action_t
ed_insert(EditLine *el, int c)
{
unknown's avatar
unknown committed
70
	int count = el->el_state.argument;
71 72 73 74 75 76 77

	if (c == '\0')
		return (CC_ERROR);

	if (el->el_line.lastchar + el->el_state.argument >=
	    el->el_line.limit) {
		/* end of buffer space, try to allocate more */
unknown's avatar
unknown committed
78
		if (!ch_enlargebufs(el, (size_t) count))
79 80 81
			return CC_ERROR;	/* error allocating more */
	}

unknown's avatar
unknown committed
82 83 84 85
	if (count == 1) {
		if (el->el_state.inputmode == MODE_INSERT
		    || el->el_line.cursor >= el->el_line.lastchar)
			c_insert(el, 1);
86 87 88 89

		*el->el_line.cursor++ = c;
		re_fastaddc(el);		/* fast refresh for one char. */
	} else {
unknown's avatar
unknown committed
90 91
		if (el->el_state.inputmode != MODE_REPLACE_1)
			c_insert(el, el->el_state.argument);
92

unknown's avatar
unknown committed
93
		while (count-- && el->el_line.cursor < el->el_line.lastchar)
94 95 96 97 98
			*el->el_line.cursor++ = c;
		re_refresh(el);
	}

	if (el->el_state.inputmode == MODE_REPLACE_1)
unknown's avatar
unknown committed
99
		return vi_command_mode(el, 0);
100 101 102 103 104 105 106 107 108 109 110

	return (CC_NORM);
}


/* ed_delete_prev_word():
 *	Delete from beginning of current word to cursor
 *	[M-^?] [^W]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
111
ed_delete_prev_word(EditLine *el, int c __attribute__((__unused__)))
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
{
	char *cp, *p, *kp;

	if (el->el_line.cursor == el->el_line.buffer)
		return (CC_ERROR);

	cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
	    el->el_state.argument, ce__isword);

	for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
		*kp++ = *p;
	el->el_chared.c_kill.last = kp;

	c_delbefore(el, el->el_line.cursor - cp);	/* delete before dot */
	el->el_line.cursor = cp;
	if (el->el_line.cursor < el->el_line.buffer)
		el->el_line.cursor = el->el_line.buffer; /* bounds check */
	return (CC_REFRESH);
}


/* ed_delete_next_char():
 *	Delete character under cursor
 *	[^D] [x]
 */
protected el_action_t
/*ARGSUSED*/
139
ed_delete_next_char(EditLine *el, int c)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
{
#ifdef notdef			/* XXX */
#define	EL	el->el_line
	(void) fprintf(el->el_errlfile,
	    "\nD(b: %x(%s)  c: %x(%s) last: %x(%s) limit: %x(%s)\n",
	    EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
	    EL.lastchar, EL.limit, EL.limit);
#endif
	if (el->el_line.cursor == el->el_line.lastchar) {
			/* if I'm at the end */
		if (el->el_map.type == MAP_VI) {
			if (el->el_line.cursor == el->el_line.buffer) {
				/* if I'm also at the beginning */
#ifdef KSHVI
				return (CC_ERROR);
#else
156 157
				/* then do an EOF */
				term_writechar(el, c);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
				return (CC_EOF);
#endif
			} else {
#ifdef KSHVI
				el->el_line.cursor--;
#else
				return (CC_ERROR);
#endif
			}
		} else {
			if (el->el_line.cursor != el->el_line.buffer)
				el->el_line.cursor--;
			else
				return (CC_ERROR);
		}
	}
	c_delafter(el, el->el_state.argument);	/* delete after dot */
	if (el->el_line.cursor >= el->el_line.lastchar &&
	    el->el_line.cursor > el->el_line.buffer)
			/* bounds check */
		el->el_line.cursor = el->el_line.lastchar - 1;
	return (CC_REFRESH);
}


/* ed_kill_line():
 *	Cut to the end of line
 *	[^K] [^K]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
189
ed_kill_line(EditLine *el, int c __attribute__((__unused__)))
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
{
	char *kp, *cp;

	cp = el->el_line.cursor;
	kp = el->el_chared.c_kill.buf;
	while (cp < el->el_line.lastchar)
		*kp++ = *cp++;	/* copy it */
	el->el_chared.c_kill.last = kp;
			/* zap! -- delete to end */
	el->el_line.lastchar = el->el_line.cursor;
	return (CC_REFRESH);
}


/* ed_move_to_end():
 *	Move cursor to the end of line
 *	[^E] [^E]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
210
ed_move_to_end(EditLine *el, int c __attribute__((__unused__)))
211 212 213 214
{

	el->el_line.cursor = el->el_line.lastchar;
	if (el->el_map.type == MAP_VI) {
unknown's avatar
unknown committed
215
		if (el->el_chared.c_vcmd.action != NOP) {
216 217 218
			cv_delfini(el);
			return (CC_REFRESH);
		}
219 220 221
#ifdef VI_MOVE
		el->el_line.cursor--;
#endif
222 223 224 225 226 227 228 229 230 231 232
	}
	return (CC_CURSOR);
}


/* ed_move_to_beg():
 *	Move cursor to the beginning of line
 *	[^A] [^A]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
233
ed_move_to_beg(EditLine *el, int c __attribute__((__unused__)))
234 235 236 237 238 239 240 241
{

	el->el_line.cursor = el->el_line.buffer;

	if (el->el_map.type == MAP_VI) {
			/* We want FIRST non space character */
		while (isspace((unsigned char) *el->el_line.cursor))
			el->el_line.cursor++;
unknown's avatar
unknown committed
242
		if (el->el_chared.c_vcmd.action != NOP) {
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
			cv_delfini(el);
			return (CC_REFRESH);
		}
	}
	return (CC_CURSOR);
}


/* ed_transpose_chars():
 *	Exchange the character to the left of the cursor with the one under it
 *	[^T] [^T]
 */
protected el_action_t
ed_transpose_chars(EditLine *el, int c)
{

	if (el->el_line.cursor < el->el_line.lastchar) {
		if (el->el_line.lastchar <= &el->el_line.buffer[1])
			return (CC_ERROR);
		else
			el->el_line.cursor++;
	}
	if (el->el_line.cursor > &el->el_line.buffer[1]) {
		/* must have at least two chars entered */
		c = el->el_line.cursor[-2];
		el->el_line.cursor[-2] = el->el_line.cursor[-1];
		el->el_line.cursor[-1] = c;
		return (CC_REFRESH);
	} else
		return (CC_ERROR);
}


/* ed_next_char():
 *	Move to the right one character
 *	[^F] [^F]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
282
ed_next_char(EditLine *el, int c __attribute__((__unused__)))
283
{
unknown's avatar
unknown committed
284
	char *lim = el->el_line.lastchar;
285

unknown's avatar
unknown committed
286 287 288 289
	if (el->el_line.cursor >= lim ||
	    (el->el_line.cursor == lim - 1 &&
	    el->el_map.type == MAP_VI &&
	    el->el_chared.c_vcmd.action == NOP))
290 291 292
		return (CC_ERROR);

	el->el_line.cursor += el->el_state.argument;
unknown's avatar
unknown committed
293 294
	if (el->el_line.cursor > lim)
		el->el_line.cursor = lim;
295 296

	if (el->el_map.type == MAP_VI)
unknown's avatar
unknown committed
297
		if (el->el_chared.c_vcmd.action != NOP) {
298 299 300 301 302 303 304 305 306 307 308 309 310
			cv_delfini(el);
			return (CC_REFRESH);
		}
	return (CC_CURSOR);
}


/* ed_prev_word():
 *	Move to the beginning of the current word
 *	[M-b] [b]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
311
ed_prev_word(EditLine *el, int c __attribute__((__unused__)))
312 313 314 315 316 317 318 319 320 321 322
{

	if (el->el_line.cursor == el->el_line.buffer)
		return (CC_ERROR);

	el->el_line.cursor = c__prev_word(el->el_line.cursor,
	    el->el_line.buffer,
	    el->el_state.argument,
	    ce__isword);

	if (el->el_map.type == MAP_VI)
unknown's avatar
unknown committed
323
		if (el->el_chared.c_vcmd.action != NOP) {
324 325 326 327 328 329 330 331 332 333 334 335 336
			cv_delfini(el);
			return (CC_REFRESH);
		}
	return (CC_CURSOR);
}


/* ed_prev_char():
 *	Move to the left one character
 *	[^B] [^B]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
337
ed_prev_char(EditLine *el, int c __attribute__((__unused__)))
338 339 340 341 342 343 344 345
{

	if (el->el_line.cursor > el->el_line.buffer) {
		el->el_line.cursor -= el->el_state.argument;
		if (el->el_line.cursor < el->el_line.buffer)
			el->el_line.cursor = el->el_line.buffer;

		if (el->el_map.type == MAP_VI)
unknown's avatar
unknown committed
346
			if (el->el_chared.c_vcmd.action != NOP) {
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
				cv_delfini(el);
				return (CC_REFRESH);
			}
		return (CC_CURSOR);
	} else
		return (CC_ERROR);
}


/* ed_quoted_insert():
 *	Add the next character typed verbatim
 *	[^V] [^V]
 */
protected el_action_t
ed_quoted_insert(EditLine *el, int c)
{
	int num;
	char tc;

	tty_quotemode(el);
	num = el_getc(el, &tc);
	c = (unsigned char) tc;
	tty_noquotemode(el);
	if (num == 1)
		return (ed_insert(el, c));
	else
		return (ed_end_of_file(el, 0));
}


/* ed_digit():
 *	Adds to argument or enters a digit
 */
protected el_action_t
ed_digit(EditLine *el, int c)
{

	if (!isdigit(c))
		return (CC_ERROR);

	if (el->el_state.doingarg) {
			/* if doing an arg, add this in... */
		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
			el->el_state.argument = c - '0';
		else {
			if (el->el_state.argument > 1000000)
				return (CC_ERROR);
			el->el_state.argument =
			    (el->el_state.argument * 10) + (c - '0');
		}
		return (CC_ARGHACK);
	}
unknown's avatar
unknown committed
399 400

	return ed_insert(el, c);
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
}


/* ed_argument_digit():
 *	Digit that starts argument
 *	For ESC-n
 */
protected el_action_t
ed_argument_digit(EditLine *el, int c)
{

	if (!isdigit(c))
		return (CC_ERROR);

	if (el->el_state.doingarg) {
		if (el->el_state.argument > 1000000)
			return (CC_ERROR);
		el->el_state.argument = (el->el_state.argument * 10) +
		    (c - '0');
	} else {		/* else starting an argument */
		el->el_state.argument = c - '0';
		el->el_state.doingarg = 1;
	}
	return (CC_ARGHACK);
}


/* ed_unassigned():
 *	Indicates unbound character
 *	Bound to keys that are not assigned
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
434
ed_unassigned(EditLine *el, int c __attribute__((__unused__)))
435 436
{

unknown's avatar
unknown committed
437
	return (CC_ERROR);
438 439 440 441 442 443 444 445 446 447 448 449 450
}


/**
 ** TTY key handling.
 **/

/* ed_tty_sigint():
 *	Tty interrupt character
 *	[^C]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
451 452
ed_tty_sigint(EditLine *el __attribute__((__unused__)), 
	      int c __attribute__((__unused__)))
453 454 455 456 457 458 459 460 461 462 463 464
{

	return (CC_NORM);
}


/* ed_tty_dsusp():
 *	Tty delayed suspend character
 *	[^Y]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
465 466
ed_tty_dsusp(EditLine *el __attribute__((__unused__)), 
	     int c __attribute__((__unused__)))
467 468 469 470 471 472 473 474 475 476 477 478
{

	return (CC_NORM);
}


/* ed_tty_flush_output():
 *	Tty flush output characters
 *	[^O]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
479 480
ed_tty_flush_output(EditLine *el __attribute__((__unused__)), 
		    int c __attribute__((__unused__)))
481 482 483 484 485 486 487 488 489 490 491 492
{

	return (CC_NORM);
}


/* ed_tty_sigquit():
 *	Tty quit character
 *	[^\]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
493 494
ed_tty_sigquit(EditLine *el __attribute__((__unused__)), 
	       int c __attribute__((__unused__)))
495 496 497 498 499 500 501 502 503 504 505 506
{

	return (CC_NORM);
}


/* ed_tty_sigtstp():
 *	Tty suspend character
 *	[^Z]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
507 508
ed_tty_sigtstp(EditLine *el __attribute__((__unused__)), 
	       int c __attribute__((__unused__)))
509 510 511 512 513 514 515 516 517 518 519 520
{

	return (CC_NORM);
}


/* ed_tty_stop_output():
 *	Tty disallow output characters
 *	[^S]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
521 522
ed_tty_stop_output(EditLine *el __attribute__((__unused__)), 
		   int c __attribute__((__unused__)))
523 524 525 526 527 528 529 530 531 532 533 534
{

	return (CC_NORM);
}


/* ed_tty_start_output():
 *	Tty allow output characters
 *	[^Q]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
535 536
ed_tty_start_output(EditLine *el __attribute__((__unused__)), 
		    int c __attribute__((__unused__)))
537 538 539 540 541 542 543 544 545 546 547 548
{

	return (CC_NORM);
}


/* ed_newline():
 *	Execute command
 *	[^J]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
549
ed_newline(EditLine *el, int c __attribute__((__unused__)))
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
{

	re_goto_bottom(el);
	*el->el_line.lastchar++ = '\n';
	*el->el_line.lastchar = '\0';
	return (CC_NEWLINE);
}


/* ed_delete_prev_char():
 *	Delete the character to the left of the cursor
 *	[^?]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
565
ed_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
{

	if (el->el_line.cursor <= el->el_line.buffer)
		return (CC_ERROR);

	c_delbefore(el, el->el_state.argument);
	el->el_line.cursor -= el->el_state.argument;
	if (el->el_line.cursor < el->el_line.buffer)
		el->el_line.cursor = el->el_line.buffer;
	return (CC_REFRESH);
}


/* ed_clear_screen():
 *	Clear screen leaving current line at the top
 *	[^L]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
585
ed_clear_screen(EditLine *el, int c __attribute__((__unused__)))
586 587 588 589 590 591 592 593 594 595 596 597 598 599
{

	term_clear_screen(el);	/* clear the whole real screen */
	re_clear_display(el);	/* reset everything */
	return (CC_REFRESH);
}


/* ed_redisplay():
 *	Redisplay everything
 *	^R
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
600 601
ed_redisplay(EditLine *el __attribute__((__unused__)), 
	     int c __attribute__((__unused__)))
602 603 604 605 606 607 608 609 610 611 612 613
{

	return (CC_REDISPLAY);
}


/* ed_start_over():
 *	Erase current line and start from scratch
 *	[^G]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
614
ed_start_over(EditLine *el, int c __attribute__((__unused__)))
615 616
{

617
	ch_reset(el, 0);
618 619 620 621 622 623 624 625 626 627
	return (CC_REFRESH);
}


/* ed_sequence_lead_in():
 *	First character in a bound sequence
 *	Placeholder for external keys
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
628 629
ed_sequence_lead_in(EditLine *el __attribute__((__unused__)), 
		    int c __attribute__((__unused__)))
630 631 632 633 634 635 636 637 638 639 640 641
{

	return (CC_NORM);
}


/* ed_prev_history():
 *	Move to the previous history line
 *	[^P] [k]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
642
ed_prev_history(EditLine *el, int c __attribute__((__unused__)))
643 644
{
	char beep = 0;
unknown's avatar
unknown committed
645
	int sv_event = el->el_history.eventno;
646

unknown's avatar
unknown committed
647
	el->el_chared.c_undo.len = -1;
648 649 650 651 652 653 654 655 656 657 658 659
	*el->el_line.lastchar = '\0';		/* just in case */

	if (el->el_history.eventno == 0) {	/* save the current buffer
						 * away */
		(void) strncpy(el->el_history.buf, el->el_line.buffer,
		    EL_BUFSIZ);
		el->el_history.last = el->el_history.buf +
		    (el->el_line.lastchar - el->el_line.buffer);
	}
	el->el_history.eventno += el->el_state.argument;

	if (hist_get(el) == CC_ERROR) {
unknown's avatar
unknown committed
660 661 662 663
		if (el->el_map.type == MAP_VI) {
			el->el_history.eventno = sv_event;
			return CC_ERROR;
		}
664 665 666 667 668
		beep = 1;
		/* el->el_history.eventno was fixed by first call */
		(void) hist_get(el);
	}
	if (beep)
unknown's avatar
unknown committed
669 670
		return CC_REFRESH_BEEP;
	return CC_REFRESH;
671 672 673 674 675 676 677 678 679
}


/* ed_next_history():
 *	Move to the next history line
 *	[^N] [j]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
680
ed_next_history(EditLine *el, int c __attribute__((__unused__)))
681
{
unknown's avatar
unknown committed
682
	el_action_t beep = CC_REFRESH, rval;
683

unknown's avatar
unknown committed
684
	el->el_chared.c_undo.len = -1;
685 686 687 688 689 690
	*el->el_line.lastchar = '\0';	/* just in case */

	el->el_history.eventno -= el->el_state.argument;

	if (el->el_history.eventno < 0) {
		el->el_history.eventno = 0;
unknown's avatar
unknown committed
691
		beep = CC_REFRESH_BEEP;
692
	}
unknown's avatar
unknown committed
693 694 695 696 697
	rval = hist_get(el);
	if (rval == CC_REFRESH)
		return beep;
	return rval;

698 699 700 701 702 703 704 705 706
}


/* ed_search_prev_history():
 *	Search previous in history for a line matching the current
 *	next search history [M-P] [K]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
707
ed_search_prev_history(EditLine *el, int c __attribute__((__unused__)))
708 709 710 711 712 713
{
	const char *hp;
	int h;
	bool_t found = 0;

	el->el_chared.c_vcmd.action = NOP;
unknown's avatar
unknown committed
714
	el->el_chared.c_undo.len = -1;
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	*el->el_line.lastchar = '\0';	/* just in case */
	if (el->el_history.eventno < 0) {
#ifdef DEBUG_EDIT
		(void) fprintf(el->el_errfile,
		    "e_prev_search_hist(): eventno < 0;\n");
#endif
		el->el_history.eventno = 0;
		return (CC_ERROR);
	}
	if (el->el_history.eventno == 0) {
		(void) strncpy(el->el_history.buf, el->el_line.buffer,
		    EL_BUFSIZ);
		el->el_history.last = el->el_history.buf +
		    (el->el_line.lastchar - el->el_line.buffer);
	}
	if (el->el_history.ref == NULL)
		return (CC_ERROR);

	hp = HIST_FIRST(el);
	if (hp == NULL)
		return (CC_ERROR);

	c_setpat(el);		/* Set search pattern !! */

	for (h = 1; h <= el->el_history.eventno; h++)
		hp = HIST_NEXT(el);

	while (hp != NULL) {
#ifdef SDEBUG
		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
#endif
		if ((strncmp(hp, el->el_line.buffer, (size_t)
			    (el->el_line.lastchar - el->el_line.buffer)) ||
			hp[el->el_line.lastchar - el->el_line.buffer]) &&
		    c_hmatch(el, hp)) {
			found++;
			break;
		}
		h++;
		hp = HIST_NEXT(el);
	}

	if (!found) {
#ifdef SDEBUG
		(void) fprintf(el->el_errfile, "not found\n");
#endif
		return (CC_ERROR);
	}
	el->el_history.eventno = h;

	return (hist_get(el));
}


/* ed_search_next_history():
 *	Search next in history for a line matching the current
 *	[M-N] [J]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
775
ed_search_next_history(EditLine *el, int c __attribute__((__unused__)))
776 777 778 779 780 781
{
	const char *hp;
	int h;
	bool_t found = 0;

	el->el_chared.c_vcmd.action = NOP;
unknown's avatar
unknown committed
782
	el->el_chared.c_undo.len = -1;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
	*el->el_line.lastchar = '\0';	/* just in case */

	if (el->el_history.eventno == 0)
		return (CC_ERROR);

	if (el->el_history.ref == NULL)
		return (CC_ERROR);

	hp = HIST_FIRST(el);
	if (hp == NULL)
		return (CC_ERROR);

	c_setpat(el);		/* Set search pattern !! */

	for (h = 1; h < el->el_history.eventno && hp; h++) {
#ifdef SDEBUG
		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
#endif
		if ((strncmp(hp, el->el_line.buffer, (size_t)
			    (el->el_line.lastchar - el->el_line.buffer)) ||
			hp[el->el_line.lastchar - el->el_line.buffer]) &&
		    c_hmatch(el, hp))
			found = h;
		hp = HIST_NEXT(el);
	}

	if (!found) {		/* is it the current history number? */
		if (!c_hmatch(el, el->el_history.buf)) {
#ifdef SDEBUG
			(void) fprintf(el->el_errfile, "not found\n");
#endif
			return (CC_ERROR);
		}
	}
	el->el_history.eventno = found;

	return (hist_get(el));
}


/* ed_prev_line():
 *	Move up one line
 *	Could be [k] [^p]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
829
ed_prev_line(EditLine *el, int c __attribute__((__unused__)))
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
{
	char *ptr;
	int nchars = c_hpos(el);

	/*
         * Move to the line requested
         */
	if (*(ptr = el->el_line.cursor) == '\n')
		ptr--;

	for (; ptr >= el->el_line.buffer; ptr--)
		if (*ptr == '\n' && --el->el_state.argument <= 0)
			break;

	if (el->el_state.argument > 0)
		return (CC_ERROR);

	/*
         * Move to the beginning of the line
         */
	for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
		continue;

	/*
         * Move to the character requested
         */
	for (ptr++;
	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
	    ptr++)
		continue;

	el->el_line.cursor = ptr;
	return (CC_CURSOR);
}


/* ed_next_line():
 *	Move down one line
 *	Could be [j] [^n]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
872
ed_next_line(EditLine *el, int c __attribute__((__unused__)))
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
{
	char *ptr;
	int nchars = c_hpos(el);

	/*
         * Move to the line requested
         */
	for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
		if (*ptr == '\n' && --el->el_state.argument <= 0)
			break;

	if (el->el_state.argument > 0)
		return (CC_ERROR);

	/*
         * Move to the character requested
         */
	for (ptr++;
	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
	    ptr++)
		continue;

	el->el_line.cursor = ptr;
	return (CC_CURSOR);
}


/* ed_command():
 *	Editline extended command
 *	[M-X] [:]
 */
protected el_action_t
/*ARGSUSED*/
unknown's avatar
unknown committed
906
ed_command(EditLine *el, int c __attribute__((__unused__)))
907 908 909 910
{
	char tmpbuf[EL_BUFSIZ];
	int tmplen;

unknown's avatar
unknown committed
911
	tmplen = c_gets(el, tmpbuf, "\n: ");
912
	term__putc(el, '\n');
913

unknown's avatar
unknown committed
914 915
	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
		term_beep(el);
916

unknown's avatar
unknown committed
917 918 919
	el->el_map.current = el->el_map.key;
	re_clear_display(el);
	return CC_REFRESH;
920
}