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