Home | History | Annotate | Line # | Download | only in libedit
common.c revision 1.16.6.1
      1  1.16.6.1       snj /*	$NetBSD: common.c,v 1.16.6.1 2009/04/23 02:00:19 snj 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.16.6.1       snj __RCSID("$NetBSD: common.c,v 1.16.6.1 2009/04/23 02:00:19 snj 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.15  christos ed_delete_next_char(EditLine *el, int c __attribute__((__unused__)))
    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.9     lukem 				term_overwrite(el, STReof, 4);
    158       1.9     lukem 					/* then do a EOF */
    159       1.9     lukem 				term__flush();
    160       1.9     lukem 				return (CC_EOF);
    161       1.1       cgd #endif
    162       1.9     lukem 			} else {
    163       1.1       cgd #ifdef KSHVI
    164       1.9     lukem 				el->el_line.cursor--;
    165       1.1       cgd #else
    166       1.9     lukem 				return (CC_ERROR);
    167       1.1       cgd #endif
    168       1.9     lukem 			}
    169       1.9     lukem 		} else {
    170       1.9     lukem 			if (el->el_line.cursor != el->el_line.buffer)
    171       1.9     lukem 				el->el_line.cursor--;
    172       1.9     lukem 			else
    173       1.9     lukem 				return (CC_ERROR);
    174       1.9     lukem 		}
    175       1.1       cgd 	}
    176       1.9     lukem 	c_delafter(el, el->el_state.argument);	/* delete after dot */
    177       1.9     lukem 	if (el->el_line.cursor >= el->el_line.lastchar &&
    178       1.9     lukem 	    el->el_line.cursor > el->el_line.buffer)
    179       1.9     lukem 			/* bounds check */
    180       1.9     lukem 		el->el_line.cursor = el->el_line.lastchar - 1;
    181       1.9     lukem 	return (CC_REFRESH);
    182       1.1       cgd }
    183       1.1       cgd 
    184       1.1       cgd 
    185       1.8    simonb /* ed_kill_line():
    186       1.1       cgd  *	Cut to the end of line
    187       1.1       cgd  *	[^K] [^K]
    188       1.1       cgd  */
    189       1.1       cgd protected el_action_t
    190       1.1       cgd /*ARGSUSED*/
    191      1.15  christos ed_kill_line(EditLine *el, int c __attribute__((__unused__)))
    192       1.9     lukem {
    193       1.9     lukem 	char *kp, *cp;
    194       1.9     lukem 
    195       1.9     lukem 	cp = el->el_line.cursor;
    196       1.9     lukem 	kp = el->el_chared.c_kill.buf;
    197       1.9     lukem 	while (cp < el->el_line.lastchar)
    198       1.9     lukem 		*kp++ = *cp++;	/* copy it */
    199       1.9     lukem 	el->el_chared.c_kill.last = kp;
    200       1.9     lukem 			/* zap! -- delete to end */
    201       1.9     lukem 	el->el_line.lastchar = el->el_line.cursor;
    202       1.9     lukem 	return (CC_REFRESH);
    203       1.1       cgd }
    204       1.1       cgd 
    205       1.1       cgd 
    206       1.8    simonb /* ed_move_to_end():
    207       1.1       cgd  *	Move cursor to the end of line
    208       1.1       cgd  *	[^E] [^E]
    209       1.1       cgd  */
    210       1.1       cgd protected el_action_t
    211       1.1       cgd /*ARGSUSED*/
    212      1.15  christos ed_move_to_end(EditLine *el, int c __attribute__((__unused__)))
    213       1.1       cgd {
    214       1.9     lukem 
    215       1.9     lukem 	el->el_line.cursor = el->el_line.lastchar;
    216       1.9     lukem 	if (el->el_map.type == MAP_VI) {
    217       1.1       cgd #ifdef VI_MOVE
    218       1.9     lukem 		el->el_line.cursor--;
    219       1.1       cgd #endif
    220      1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    221       1.9     lukem 			cv_delfini(el);
    222       1.9     lukem 			return (CC_REFRESH);
    223       1.9     lukem 		}
    224       1.1       cgd 	}
    225       1.9     lukem 	return (CC_CURSOR);
    226       1.1       cgd }
    227       1.1       cgd 
    228       1.1       cgd 
    229       1.8    simonb /* ed_move_to_beg():
    230       1.1       cgd  *	Move cursor to the beginning of line
    231       1.1       cgd  *	[^A] [^A]
    232       1.1       cgd  */
    233       1.1       cgd protected el_action_t
    234       1.1       cgd /*ARGSUSED*/
    235      1.15  christos ed_move_to_beg(EditLine *el, int c __attribute__((__unused__)))
    236       1.9     lukem {
    237       1.9     lukem 
    238       1.9     lukem 	el->el_line.cursor = el->el_line.buffer;
    239       1.9     lukem 
    240       1.9     lukem 	if (el->el_map.type == MAP_VI) {
    241       1.9     lukem 			/* We want FIRST non space character */
    242       1.9     lukem 		while (isspace((unsigned char) *el->el_line.cursor))
    243       1.9     lukem 			el->el_line.cursor++;
    244      1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    245       1.9     lukem 			cv_delfini(el);
    246       1.9     lukem 			return (CC_REFRESH);
    247       1.9     lukem 		}
    248       1.1       cgd 	}
    249       1.9     lukem 	return (CC_CURSOR);
    250       1.1       cgd }
    251       1.1       cgd 
    252       1.1       cgd 
    253       1.8    simonb /* ed_transpose_chars():
    254       1.1       cgd  *	Exchange the character to the left of the cursor with the one under it
    255       1.1       cgd  *	[^T] [^T]
    256       1.1       cgd  */
    257       1.1       cgd protected el_action_t
    258       1.9     lukem ed_transpose_chars(EditLine *el, int c)
    259       1.9     lukem {
    260       1.9     lukem 
    261       1.9     lukem 	if (el->el_line.cursor < el->el_line.lastchar) {
    262       1.9     lukem 		if (el->el_line.lastchar <= &el->el_line.buffer[1])
    263       1.9     lukem 			return (CC_ERROR);
    264       1.9     lukem 		else
    265       1.9     lukem 			el->el_line.cursor++;
    266       1.9     lukem 	}
    267       1.9     lukem 	if (el->el_line.cursor > &el->el_line.buffer[1]) {
    268       1.9     lukem 		/* must have at least two chars entered */
    269       1.9     lukem 		c = el->el_line.cursor[-2];
    270       1.9     lukem 		el->el_line.cursor[-2] = el->el_line.cursor[-1];
    271       1.9     lukem 		el->el_line.cursor[-1] = c;
    272       1.9     lukem 		return (CC_REFRESH);
    273       1.9     lukem 	} else
    274       1.9     lukem 		return (CC_ERROR);
    275       1.1       cgd }
    276       1.1       cgd 
    277       1.1       cgd 
    278       1.8    simonb /* ed_next_char():
    279       1.1       cgd  *	Move to the right one character
    280       1.1       cgd  *	[^F] [^F]
    281       1.1       cgd  */
    282       1.1       cgd protected el_action_t
    283       1.1       cgd /*ARGSUSED*/
    284      1.15  christos ed_next_char(EditLine *el, int c __attribute__((__unused__)))
    285       1.1       cgd {
    286      1.13  christos 	char *lim = el->el_line.lastchar;
    287       1.1       cgd 
    288      1.13  christos 	if (el->el_line.cursor >= lim ||
    289      1.13  christos 	    (el->el_line.cursor == lim - 1 &&
    290      1.13  christos 	    el->el_map.type == MAP_VI &&
    291      1.13  christos 	    el->el_chared.c_vcmd.action == NOP))
    292       1.9     lukem 		return (CC_ERROR);
    293       1.1       cgd 
    294       1.9     lukem 	el->el_line.cursor += el->el_state.argument;
    295      1.13  christos 	if (el->el_line.cursor > lim)
    296      1.13  christos 		el->el_line.cursor = lim;
    297       1.1       cgd 
    298       1.9     lukem 	if (el->el_map.type == MAP_VI)
    299      1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    300       1.9     lukem 			cv_delfini(el);
    301       1.9     lukem 			return (CC_REFRESH);
    302       1.9     lukem 		}
    303       1.9     lukem 	return (CC_CURSOR);
    304       1.1       cgd }
    305       1.1       cgd 
    306       1.1       cgd 
    307       1.8    simonb /* ed_prev_word():
    308       1.1       cgd  *	Move to the beginning of the current word
    309       1.1       cgd  *	[M-b] [b]
    310       1.1       cgd  */
    311       1.1       cgd protected el_action_t
    312       1.1       cgd /*ARGSUSED*/
    313      1.15  christos ed_prev_word(EditLine *el, int c __attribute__((__unused__)))
    314       1.1       cgd {
    315       1.1       cgd 
    316       1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    317       1.9     lukem 		return (CC_ERROR);
    318       1.1       cgd 
    319       1.9     lukem 	el->el_line.cursor = c__prev_word(el->el_line.cursor,
    320       1.9     lukem 	    el->el_line.buffer,
    321       1.9     lukem 	    el->el_state.argument,
    322       1.9     lukem 	    ce__isword);
    323       1.1       cgd 
    324       1.9     lukem 	if (el->el_map.type == MAP_VI)
    325      1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    326       1.9     lukem 			cv_delfini(el);
    327       1.9     lukem 			return (CC_REFRESH);
    328       1.9     lukem 		}
    329       1.9     lukem 	return (CC_CURSOR);
    330       1.1       cgd }
    331       1.1       cgd 
    332       1.1       cgd 
    333       1.8    simonb /* ed_prev_char():
    334       1.1       cgd  *	Move to the left one character
    335       1.1       cgd  *	[^B] [^B]
    336       1.1       cgd  */
    337       1.1       cgd protected el_action_t
    338       1.1       cgd /*ARGSUSED*/
    339      1.15  christos ed_prev_char(EditLine *el, int c __attribute__((__unused__)))
    340       1.1       cgd {
    341       1.1       cgd 
    342       1.9     lukem 	if (el->el_line.cursor > el->el_line.buffer) {
    343       1.9     lukem 		el->el_line.cursor -= el->el_state.argument;
    344       1.9     lukem 		if (el->el_line.cursor < el->el_line.buffer)
    345       1.9     lukem 			el->el_line.cursor = el->el_line.buffer;
    346       1.9     lukem 
    347       1.9     lukem 		if (el->el_map.type == MAP_VI)
    348      1.13  christos 			if (el->el_chared.c_vcmd.action != NOP) {
    349       1.9     lukem 				cv_delfini(el);
    350       1.9     lukem 				return (CC_REFRESH);
    351       1.9     lukem 			}
    352       1.9     lukem 		return (CC_CURSOR);
    353       1.9     lukem 	} else
    354       1.9     lukem 		return (CC_ERROR);
    355       1.1       cgd }
    356       1.1       cgd 
    357       1.1       cgd 
    358       1.8    simonb /* ed_quoted_insert():
    359       1.1       cgd  *	Add the next character typed verbatim
    360       1.1       cgd  *	[^V] [^V]
    361       1.1       cgd  */
    362       1.1       cgd protected el_action_t
    363       1.9     lukem ed_quoted_insert(EditLine *el, int c)
    364       1.9     lukem {
    365       1.9     lukem 	int num;
    366       1.9     lukem 	char tc;
    367       1.9     lukem 
    368       1.9     lukem 	tty_quotemode(el);
    369       1.9     lukem 	num = el_getc(el, &tc);
    370       1.9     lukem 	c = (unsigned char) tc;
    371       1.9     lukem 	tty_noquotemode(el);
    372       1.9     lukem 	if (num == 1)
    373       1.9     lukem 		return (ed_insert(el, c));
    374       1.9     lukem 	else
    375       1.9     lukem 		return (ed_end_of_file(el, 0));
    376       1.1       cgd }
    377       1.1       cgd 
    378       1.1       cgd 
    379       1.8    simonb /* ed_digit():
    380       1.1       cgd  *	Adds to argument or enters a digit
    381       1.1       cgd  */
    382       1.1       cgd protected el_action_t
    383       1.9     lukem ed_digit(EditLine *el, int c)
    384       1.9     lukem {
    385       1.9     lukem 
    386  1.16.6.1       snj 	if (!isdigit((unsigned char)c))
    387       1.9     lukem 		return (CC_ERROR);
    388       1.9     lukem 
    389       1.9     lukem 	if (el->el_state.doingarg) {
    390       1.9     lukem 			/* if doing an arg, add this in... */
    391       1.9     lukem 		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
    392       1.9     lukem 			el->el_state.argument = c - '0';
    393       1.9     lukem 		else {
    394       1.9     lukem 			if (el->el_state.argument > 1000000)
    395       1.9     lukem 				return (CC_ERROR);
    396       1.9     lukem 			el->el_state.argument =
    397       1.9     lukem 			    (el->el_state.argument * 10) + (c - '0');
    398       1.9     lukem 		}
    399       1.9     lukem 		return (CC_ARGHACK);
    400      1.12  christos 	}
    401       1.9     lukem 
    402      1.12  christos 	return ed_insert(el, c);
    403       1.1       cgd }
    404       1.1       cgd 
    405       1.1       cgd 
    406       1.8    simonb /* ed_argument_digit():
    407       1.1       cgd  *	Digit that starts argument
    408       1.1       cgd  *	For ESC-n
    409       1.1       cgd  */
    410       1.1       cgd protected el_action_t
    411       1.9     lukem ed_argument_digit(EditLine *el, int c)
    412       1.9     lukem {
    413       1.9     lukem 
    414  1.16.6.1       snj 	if (!isdigit((unsigned char)c))
    415       1.9     lukem 		return (CC_ERROR);
    416       1.9     lukem 
    417       1.9     lukem 	if (el->el_state.doingarg) {
    418       1.9     lukem 		if (el->el_state.argument > 1000000)
    419       1.9     lukem 			return (CC_ERROR);
    420       1.9     lukem 		el->el_state.argument = (el->el_state.argument * 10) +
    421       1.9     lukem 		    (c - '0');
    422       1.9     lukem 	} else {		/* else starting an argument */
    423       1.9     lukem 		el->el_state.argument = c - '0';
    424       1.9     lukem 		el->el_state.doingarg = 1;
    425       1.9     lukem 	}
    426       1.9     lukem 	return (CC_ARGHACK);
    427       1.1       cgd }
    428       1.1       cgd 
    429       1.1       cgd 
    430       1.8    simonb /* ed_unassigned():
    431       1.1       cgd  *	Indicates unbound character
    432       1.1       cgd  *	Bound to keys that are not assigned
    433       1.1       cgd  */
    434       1.1       cgd protected el_action_t
    435       1.1       cgd /*ARGSUSED*/
    436      1.15  christos ed_unassigned(EditLine *el, int c __attribute__((__unused__)))
    437       1.9     lukem {
    438       1.9     lukem 
    439      1.13  christos 	return (CC_ERROR);
    440       1.1       cgd }
    441       1.1       cgd 
    442       1.1       cgd 
    443       1.1       cgd /**
    444       1.1       cgd  ** TTY key handling.
    445       1.1       cgd  **/
    446       1.1       cgd 
    447       1.8    simonb /* ed_tty_sigint():
    448       1.1       cgd  *	Tty interrupt character
    449       1.1       cgd  *	[^C]
    450       1.1       cgd  */
    451       1.1       cgd protected el_action_t
    452       1.1       cgd /*ARGSUSED*/
    453      1.15  christos ed_tty_sigint(EditLine *el __attribute__((__unused__)),
    454      1.15  christos 	      int c __attribute__((__unused__)))
    455       1.8    simonb {
    456       1.9     lukem 
    457       1.9     lukem 	return (CC_NORM);
    458       1.1       cgd }
    459       1.1       cgd 
    460       1.1       cgd 
    461       1.8    simonb /* ed_tty_dsusp():
    462       1.1       cgd  *	Tty delayed suspend character
    463       1.1       cgd  *	[^Y]
    464       1.1       cgd  */
    465       1.1       cgd protected el_action_t
    466       1.1       cgd /*ARGSUSED*/
    467      1.15  christos ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
    468      1.15  christos 	     int c __attribute__((__unused__)))
    469       1.1       cgd {
    470       1.9     lukem 
    471       1.9     lukem 	return (CC_NORM);
    472       1.1       cgd }
    473       1.1       cgd 
    474       1.1       cgd 
    475       1.8    simonb /* ed_tty_flush_output():
    476       1.1       cgd  *	Tty flush output characters
    477       1.1       cgd  *	[^O]
    478       1.1       cgd  */
    479       1.1       cgd protected el_action_t
    480       1.1       cgd /*ARGSUSED*/
    481      1.15  christos ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
    482      1.15  christos 		    int c __attribute__((__unused__)))
    483       1.1       cgd {
    484       1.9     lukem 
    485       1.9     lukem 	return (CC_NORM);
    486       1.1       cgd }
    487       1.1       cgd 
    488       1.1       cgd 
    489       1.8    simonb /* ed_tty_sigquit():
    490       1.1       cgd  *	Tty quit character
    491       1.1       cgd  *	[^\]
    492       1.1       cgd  */
    493       1.1       cgd protected el_action_t
    494       1.1       cgd /*ARGSUSED*/
    495      1.15  christos ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
    496      1.15  christos 	       int c __attribute__((__unused__)))
    497       1.1       cgd {
    498       1.9     lukem 
    499       1.9     lukem 	return (CC_NORM);
    500       1.1       cgd }
    501       1.1       cgd 
    502       1.1       cgd 
    503       1.8    simonb /* ed_tty_sigtstp():
    504       1.1       cgd  *	Tty suspend character
    505       1.1       cgd  *	[^Z]
    506       1.1       cgd  */
    507       1.1       cgd protected el_action_t
    508       1.1       cgd /*ARGSUSED*/
    509      1.15  christos ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
    510      1.15  christos 	       int c __attribute__((__unused__)))
    511       1.1       cgd {
    512       1.9     lukem 
    513       1.9     lukem 	return (CC_NORM);
    514       1.1       cgd }
    515       1.1       cgd 
    516       1.1       cgd 
    517       1.8    simonb /* ed_tty_stop_output():
    518       1.1       cgd  *	Tty disallow output characters
    519       1.1       cgd  *	[^S]
    520       1.1       cgd  */
    521       1.1       cgd protected el_action_t
    522       1.1       cgd /*ARGSUSED*/
    523      1.15  christos ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
    524      1.15  christos 		   int c __attribute__((__unused__)))
    525       1.1       cgd {
    526       1.9     lukem 
    527       1.9     lukem 	return (CC_NORM);
    528       1.1       cgd }
    529       1.1       cgd 
    530       1.1       cgd 
    531       1.8    simonb /* ed_tty_start_output():
    532       1.1       cgd  *	Tty allow output characters
    533       1.1       cgd  *	[^Q]
    534       1.1       cgd  */
    535       1.1       cgd protected el_action_t
    536       1.1       cgd /*ARGSUSED*/
    537      1.15  christos ed_tty_start_output(EditLine *el __attribute__((__unused__)),
    538      1.15  christos 		    int c __attribute__((__unused__)))
    539       1.1       cgd {
    540       1.9     lukem 
    541       1.9     lukem 	return (CC_NORM);
    542       1.1       cgd }
    543       1.1       cgd 
    544       1.1       cgd 
    545       1.8    simonb /* ed_newline():
    546       1.1       cgd  *	Execute command
    547       1.1       cgd  *	[^J]
    548       1.1       cgd  */
    549       1.1       cgd protected el_action_t
    550       1.1       cgd /*ARGSUSED*/
    551      1.15  christos ed_newline(EditLine *el, int c __attribute__((__unused__)))
    552       1.9     lukem {
    553       1.9     lukem 
    554       1.9     lukem 	re_goto_bottom(el);
    555       1.9     lukem 	*el->el_line.lastchar++ = '\n';
    556       1.9     lukem 	*el->el_line.lastchar = '\0';
    557       1.9     lukem 	return (CC_NEWLINE);
    558       1.1       cgd }
    559       1.1       cgd 
    560       1.1       cgd 
    561       1.8    simonb /* ed_delete_prev_char():
    562       1.1       cgd  *	Delete the character to the left of the cursor
    563       1.1       cgd  *	[^?]
    564       1.1       cgd  */
    565       1.1       cgd protected el_action_t
    566       1.1       cgd /*ARGSUSED*/
    567      1.15  christos ed_delete_prev_char(EditLine *el, int c __attribute__((__unused__)))
    568       1.9     lukem {
    569       1.9     lukem 
    570       1.9     lukem 	if (el->el_line.cursor <= el->el_line.buffer)
    571       1.9     lukem 		return (CC_ERROR);
    572       1.9     lukem 
    573       1.9     lukem 	c_delbefore(el, el->el_state.argument);
    574       1.9     lukem 	el->el_line.cursor -= el->el_state.argument;
    575       1.9     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    576       1.9     lukem 		el->el_line.cursor = el->el_line.buffer;
    577       1.9     lukem 	return (CC_REFRESH);
    578       1.1       cgd }
    579       1.1       cgd 
    580       1.1       cgd 
    581       1.8    simonb /* ed_clear_screen():
    582       1.1       cgd  *	Clear screen leaving current line at the top
    583       1.1       cgd  *	[^L]
    584       1.1       cgd  */
    585       1.1       cgd protected el_action_t
    586       1.1       cgd /*ARGSUSED*/
    587      1.15  christos ed_clear_screen(EditLine *el, int c __attribute__((__unused__)))
    588       1.9     lukem {
    589       1.9     lukem 
    590       1.9     lukem 	term_clear_screen(el);	/* clear the whole real screen */
    591       1.9     lukem 	re_clear_display(el);	/* reset everything */
    592       1.9     lukem 	return (CC_REFRESH);
    593       1.1       cgd }
    594       1.1       cgd 
    595       1.1       cgd 
    596       1.8    simonb /* ed_redisplay():
    597       1.1       cgd  *	Redisplay everything
    598       1.1       cgd  *	^R
    599       1.1       cgd  */
    600       1.1       cgd protected el_action_t
    601       1.1       cgd /*ARGSUSED*/
    602      1.15  christos ed_redisplay(EditLine *el __attribute__((__unused__)),
    603      1.15  christos 	     int c __attribute__((__unused__)))
    604       1.1       cgd {
    605       1.9     lukem 
    606       1.9     lukem 	return (CC_REDISPLAY);
    607       1.1       cgd }
    608       1.1       cgd 
    609       1.1       cgd 
    610       1.8    simonb /* ed_start_over():
    611       1.1       cgd  *	Erase current line and start from scratch
    612       1.1       cgd  *	[^G]
    613       1.1       cgd  */
    614       1.1       cgd protected el_action_t
    615       1.1       cgd /*ARGSUSED*/
    616      1.15  christos ed_start_over(EditLine *el, int c __attribute__((__unused__)))
    617       1.1       cgd {
    618       1.9     lukem 
    619       1.9     lukem 	ch_reset(el);
    620       1.9     lukem 	return (CC_REFRESH);
    621       1.1       cgd }
    622       1.1       cgd 
    623       1.1       cgd 
    624       1.8    simonb /* ed_sequence_lead_in():
    625       1.1       cgd  *	First character in a bound sequence
    626       1.1       cgd  *	Placeholder for external keys
    627       1.1       cgd  */
    628       1.1       cgd protected el_action_t
    629       1.1       cgd /*ARGSUSED*/
    630      1.15  christos ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
    631      1.15  christos 		    int c __attribute__((__unused__)))
    632       1.1       cgd {
    633       1.9     lukem 
    634       1.9     lukem 	return (CC_NORM);
    635       1.1       cgd }
    636       1.1       cgd 
    637       1.1       cgd 
    638       1.8    simonb /* ed_prev_history():
    639       1.1       cgd  *	Move to the previous history line
    640       1.1       cgd  *	[^P] [k]
    641       1.1       cgd  */
    642       1.1       cgd protected el_action_t
    643       1.1       cgd /*ARGSUSED*/
    644      1.15  christos ed_prev_history(EditLine *el, int c __attribute__((__unused__)))
    645       1.9     lukem {
    646       1.9     lukem 	char beep = 0;
    647      1.13  christos 	int sv_event = el->el_history.eventno;
    648       1.9     lukem 
    649      1.12  christos 	el->el_chared.c_undo.len = -1;
    650       1.9     lukem 	*el->el_line.lastchar = '\0';		/* just in case */
    651       1.9     lukem 
    652       1.9     lukem 	if (el->el_history.eventno == 0) {	/* save the current buffer
    653       1.9     lukem 						 * away */
    654       1.9     lukem 		(void) strncpy(el->el_history.buf, el->el_line.buffer,
    655       1.9     lukem 		    EL_BUFSIZ);
    656       1.9     lukem 		el->el_history.last = el->el_history.buf +
    657       1.9     lukem 		    (el->el_line.lastchar - el->el_line.buffer);
    658       1.9     lukem 	}
    659       1.9     lukem 	el->el_history.eventno += el->el_state.argument;
    660       1.9     lukem 
    661       1.9     lukem 	if (hist_get(el) == CC_ERROR) {
    662      1.13  christos 		if (el->el_map.type == MAP_VI) {
    663      1.13  christos 			el->el_history.eventno = sv_event;
    664      1.13  christos 			return CC_ERROR;
    665      1.13  christos 		}
    666       1.9     lukem 		beep = 1;
    667       1.9     lukem 		/* el->el_history.eventno was fixed by first call */
    668       1.9     lukem 		(void) hist_get(el);
    669       1.9     lukem 	}
    670       1.9     lukem 	if (beep)
    671      1.13  christos 		return CC_REFRESH_BEEP;
    672      1.13  christos 	return CC_REFRESH;
    673       1.1       cgd }
    674       1.1       cgd 
    675       1.1       cgd 
    676       1.8    simonb /* ed_next_history():
    677       1.1       cgd  *	Move to the next history line
    678       1.1       cgd  *	[^N] [j]
    679       1.1       cgd  */
    680       1.1       cgd protected el_action_t
    681       1.1       cgd /*ARGSUSED*/
    682      1.15  christos ed_next_history(EditLine *el, int c __attribute__((__unused__)))
    683       1.1       cgd {
    684      1.13  christos 	el_action_t beep = CC_REFRESH, rval;
    685       1.1       cgd 
    686      1.12  christos 	el->el_chared.c_undo.len = -1;
    687       1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    688       1.1       cgd 
    689       1.9     lukem 	el->el_history.eventno -= el->el_state.argument;
    690       1.1       cgd 
    691       1.9     lukem 	if (el->el_history.eventno < 0) {
    692       1.9     lukem 		el->el_history.eventno = 0;
    693      1.13  christos 		beep = CC_REFRESH_BEEP;
    694       1.9     lukem 	}
    695      1.13  christos 	rval = hist_get(el);
    696      1.13  christos 	if (rval == CC_REFRESH)
    697      1.13  christos 		return beep;
    698      1.13  christos 	return rval;
    699      1.13  christos 
    700       1.1       cgd }
    701       1.1       cgd 
    702       1.1       cgd 
    703       1.8    simonb /* ed_search_prev_history():
    704       1.1       cgd  *	Search previous in history for a line matching the current
    705       1.1       cgd  *	next search history [M-P] [K]
    706       1.1       cgd  */
    707       1.1       cgd protected el_action_t
    708       1.1       cgd /*ARGSUSED*/
    709      1.15  christos ed_search_prev_history(EditLine *el, int c __attribute__((__unused__)))
    710       1.9     lukem {
    711       1.9     lukem 	const char *hp;
    712       1.9     lukem 	int h;
    713       1.9     lukem 	bool_t found = 0;
    714       1.9     lukem 
    715       1.9     lukem 	el->el_chared.c_vcmd.action = NOP;
    716      1.12  christos 	el->el_chared.c_undo.len = -1;
    717       1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    718       1.9     lukem 	if (el->el_history.eventno < 0) {
    719       1.1       cgd #ifdef DEBUG_EDIT
    720       1.9     lukem 		(void) fprintf(el->el_errfile,
    721       1.9     lukem 		    "e_prev_search_hist(): eventno < 0;\n");
    722       1.1       cgd #endif
    723       1.9     lukem 		el->el_history.eventno = 0;
    724       1.9     lukem 		return (CC_ERROR);
    725       1.9     lukem 	}
    726       1.9     lukem 	if (el->el_history.eventno == 0) {
    727       1.9     lukem 		(void) strncpy(el->el_history.buf, el->el_line.buffer,
    728       1.9     lukem 		    EL_BUFSIZ);
    729       1.9     lukem 		el->el_history.last = el->el_history.buf +
    730       1.9     lukem 		    (el->el_line.lastchar - el->el_line.buffer);
    731       1.9     lukem 	}
    732       1.9     lukem 	if (el->el_history.ref == NULL)
    733       1.9     lukem 		return (CC_ERROR);
    734       1.1       cgd 
    735       1.9     lukem 	hp = HIST_FIRST(el);
    736       1.9     lukem 	if (hp == NULL)
    737       1.9     lukem 		return (CC_ERROR);
    738       1.1       cgd 
    739       1.9     lukem 	c_setpat(el);		/* Set search pattern !! */
    740       1.1       cgd 
    741       1.9     lukem 	for (h = 1; h <= el->el_history.eventno; h++)
    742       1.9     lukem 		hp = HIST_NEXT(el);
    743       1.1       cgd 
    744       1.9     lukem 	while (hp != NULL) {
    745       1.1       cgd #ifdef SDEBUG
    746       1.9     lukem 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    747       1.1       cgd #endif
    748       1.9     lukem 		if ((strncmp(hp, el->el_line.buffer, (size_t)
    749       1.9     lukem 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    750       1.9     lukem 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    751       1.9     lukem 		    c_hmatch(el, hp)) {
    752       1.9     lukem 			found++;
    753       1.9     lukem 			break;
    754       1.9     lukem 		}
    755       1.9     lukem 		h++;
    756       1.9     lukem 		hp = HIST_NEXT(el);
    757       1.9     lukem 	}
    758       1.1       cgd 
    759       1.9     lukem 	if (!found) {
    760       1.1       cgd #ifdef SDEBUG
    761       1.9     lukem 		(void) fprintf(el->el_errfile, "not found\n");
    762       1.1       cgd #endif
    763       1.9     lukem 		return (CC_ERROR);
    764       1.9     lukem 	}
    765       1.9     lukem 	el->el_history.eventno = h;
    766       1.1       cgd 
    767       1.9     lukem 	return (hist_get(el));
    768       1.1       cgd }
    769       1.1       cgd 
    770       1.1       cgd 
    771       1.8    simonb /* ed_search_next_history():
    772       1.1       cgd  *	Search next in history for a line matching the current
    773       1.1       cgd  *	[M-N] [J]
    774       1.1       cgd  */
    775       1.1       cgd protected el_action_t
    776       1.1       cgd /*ARGSUSED*/
    777      1.15  christos ed_search_next_history(EditLine *el, int c __attribute__((__unused__)))
    778       1.1       cgd {
    779       1.9     lukem 	const char *hp;
    780       1.9     lukem 	int h;
    781       1.9     lukem 	bool_t found = 0;
    782       1.1       cgd 
    783       1.9     lukem 	el->el_chared.c_vcmd.action = NOP;
    784      1.12  christos 	el->el_chared.c_undo.len = -1;
    785       1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    786       1.1       cgd 
    787       1.9     lukem 	if (el->el_history.eventno == 0)
    788       1.9     lukem 		return (CC_ERROR);
    789       1.1       cgd 
    790       1.9     lukem 	if (el->el_history.ref == NULL)
    791       1.9     lukem 		return (CC_ERROR);
    792       1.1       cgd 
    793       1.9     lukem 	hp = HIST_FIRST(el);
    794       1.9     lukem 	if (hp == NULL)
    795       1.9     lukem 		return (CC_ERROR);
    796       1.1       cgd 
    797       1.9     lukem 	c_setpat(el);		/* Set search pattern !! */
    798       1.1       cgd 
    799       1.9     lukem 	for (h = 1; h < el->el_history.eventno && hp; h++) {
    800       1.1       cgd #ifdef SDEBUG
    801       1.9     lukem 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    802       1.1       cgd #endif
    803       1.9     lukem 		if ((strncmp(hp, el->el_line.buffer, (size_t)
    804       1.9     lukem 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    805       1.9     lukem 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    806       1.9     lukem 		    c_hmatch(el, hp))
    807       1.9     lukem 			found = h;
    808       1.9     lukem 		hp = HIST_NEXT(el);
    809       1.9     lukem 	}
    810       1.1       cgd 
    811       1.9     lukem 	if (!found) {		/* is it the current history number? */
    812       1.9     lukem 		if (!c_hmatch(el, el->el_history.buf)) {
    813       1.1       cgd #ifdef SDEBUG
    814       1.9     lukem 			(void) fprintf(el->el_errfile, "not found\n");
    815       1.1       cgd #endif
    816       1.9     lukem 			return (CC_ERROR);
    817       1.9     lukem 		}
    818       1.1       cgd 	}
    819       1.9     lukem 	el->el_history.eventno = found;
    820       1.1       cgd 
    821       1.9     lukem 	return (hist_get(el));
    822       1.1       cgd }
    823       1.1       cgd 
    824       1.1       cgd 
    825       1.1       cgd /* ed_prev_line():
    826       1.1       cgd  *	Move up one line
    827       1.1       cgd  *	Could be [k] [^p]
    828       1.1       cgd  */
    829       1.1       cgd protected el_action_t
    830       1.1       cgd /*ARGSUSED*/
    831      1.15  christos ed_prev_line(EditLine *el, int c __attribute__((__unused__)))
    832       1.9     lukem {
    833       1.9     lukem 	char *ptr;
    834       1.9     lukem 	int nchars = c_hpos(el);
    835       1.8    simonb 
    836       1.9     lukem 	/*
    837       1.9     lukem          * Move to the line requested
    838       1.9     lukem          */
    839       1.9     lukem 	if (*(ptr = el->el_line.cursor) == '\n')
    840       1.9     lukem 		ptr--;
    841       1.9     lukem 
    842       1.9     lukem 	for (; ptr >= el->el_line.buffer; ptr--)
    843       1.9     lukem 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    844       1.9     lukem 			break;
    845       1.9     lukem 
    846       1.9     lukem 	if (el->el_state.argument > 0)
    847       1.9     lukem 		return (CC_ERROR);
    848       1.9     lukem 
    849       1.9     lukem 	/*
    850       1.9     lukem          * Move to the beginning of the line
    851       1.9     lukem          */
    852       1.9     lukem 	for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
    853       1.9     lukem 		continue;
    854       1.9     lukem 
    855       1.9     lukem 	/*
    856       1.9     lukem          * Move to the character requested
    857       1.9     lukem          */
    858       1.9     lukem 	for (ptr++;
    859       1.9     lukem 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    860       1.9     lukem 	    ptr++)
    861       1.9     lukem 		continue;
    862       1.9     lukem 
    863       1.9     lukem 	el->el_line.cursor = ptr;
    864       1.9     lukem 	return (CC_CURSOR);
    865       1.1       cgd }
    866       1.1       cgd 
    867       1.1       cgd 
    868       1.1       cgd /* ed_next_line():
    869       1.1       cgd  *	Move down one line
    870       1.1       cgd  *	Could be [j] [^n]
    871       1.1       cgd  */
    872       1.1       cgd protected el_action_t
    873       1.1       cgd /*ARGSUSED*/
    874      1.15  christos ed_next_line(EditLine *el, int c __attribute__((__unused__)))
    875       1.9     lukem {
    876       1.9     lukem 	char *ptr;
    877       1.9     lukem 	int nchars = c_hpos(el);
    878       1.8    simonb 
    879       1.9     lukem 	/*
    880       1.9     lukem          * Move to the line requested
    881       1.9     lukem          */
    882       1.9     lukem 	for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
    883       1.9     lukem 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    884       1.9     lukem 			break;
    885       1.9     lukem 
    886       1.9     lukem 	if (el->el_state.argument > 0)
    887       1.9     lukem 		return (CC_ERROR);
    888       1.9     lukem 
    889       1.9     lukem 	/*
    890       1.9     lukem          * Move to the character requested
    891       1.9     lukem          */
    892       1.9     lukem 	for (ptr++;
    893       1.9     lukem 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    894       1.9     lukem 	    ptr++)
    895       1.9     lukem 		continue;
    896       1.9     lukem 
    897       1.9     lukem 	el->el_line.cursor = ptr;
    898       1.9     lukem 	return (CC_CURSOR);
    899       1.1       cgd }
    900       1.1       cgd 
    901       1.1       cgd 
    902       1.8    simonb /* ed_command():
    903       1.1       cgd  *	Editline extended command
    904       1.1       cgd  *	[M-X] [:]
    905       1.1       cgd  */
    906       1.1       cgd protected el_action_t
    907       1.1       cgd /*ARGSUSED*/
    908      1.15  christos ed_command(EditLine *el, int c __attribute__((__unused__)))
    909       1.9     lukem {
    910       1.9     lukem 	char tmpbuf[EL_BUFSIZ];
    911       1.9     lukem 	int tmplen;
    912       1.9     lukem 
    913      1.14  christos 	tmplen = c_gets(el, tmpbuf, "\n: ");
    914      1.14  christos 	term__putc('\n');
    915       1.9     lukem 
    916      1.14  christos 	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
    917      1.14  christos 		term_beep(el);
    918       1.9     lukem 
    919      1.14  christos 	el->el_map.current = el->el_map.key;
    920      1.14  christos 	re_clear_display(el);
    921      1.14  christos 	return CC_REFRESH;
    922       1.1       cgd }
    923