Home | History | Annotate | Line # | Download | only in libedit
common.c revision 1.33
      1  1.33  christos /*	$NetBSD: common.c,v 1.33 2016/02/16 14:08:25 christos 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.33  christos __RCSID("$NetBSD: common.c,v 1.33 2016/02/16 14:08:25 christos 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.32  christos #include "histedit.h"
     48  1.33  christos #include "chartype.h"
     49   1.1       cgd #include "el.h"
     50   1.1       cgd 
     51   1.8    simonb /* ed_end_of_file():
     52   1.1       cgd  *	Indicate end of file
     53   1.1       cgd  *	[^D]
     54   1.1       cgd  */
     55   1.1       cgd protected el_action_t
     56   1.1       cgd /*ARGSUSED*/
     57  1.31  christos ed_end_of_file(EditLine *el, wint_t c __attribute__((__unused__)))
     58   1.9     lukem {
     59   1.9     lukem 
     60   1.9     lukem 	re_goto_bottom(el);
     61   1.9     lukem 	*el->el_line.lastchar = '\0';
     62  1.26  christos 	return CC_EOF;
     63   1.1       cgd }
     64   1.1       cgd 
     65   1.1       cgd 
     66   1.8    simonb /* ed_insert():
     67   1.1       cgd  *	Add character to the line
     68   1.1       cgd  *	Insert a character [bound to all insert keys]
     69   1.1       cgd  */
     70   1.1       cgd protected el_action_t
     71  1.31  christos ed_insert(EditLine *el, wint_t c)
     72   1.9     lukem {
     73  1.13  christos 	int count = el->el_state.argument;
     74   1.1       cgd 
     75   1.9     lukem 	if (c == '\0')
     76  1.26  christos 		return CC_ERROR;
     77   1.1       cgd 
     78   1.9     lukem 	if (el->el_line.lastchar + el->el_state.argument >=
     79  1.10  jdolecek 	    el->el_line.limit) {
     80  1.10  jdolecek 		/* end of buffer space, try to allocate more */
     81  1.13  christos 		if (!ch_enlargebufs(el, (size_t) count))
     82  1.10  jdolecek 			return CC_ERROR;	/* error allocating more */
     83  1.10  jdolecek 	}
     84   1.9     lukem 
     85  1.13  christos 	if (count == 1) {
     86  1.12  christos 		if (el->el_state.inputmode == MODE_INSERT
     87  1.12  christos 		    || el->el_line.cursor >= el->el_line.lastchar)
     88  1.12  christos 			c_insert(el, 1);
     89   1.9     lukem 
     90  1.30  christos 		*el->el_line.cursor++ = (Char)c;
     91   1.9     lukem 		re_fastaddc(el);		/* fast refresh for one char. */
     92   1.9     lukem 	} else {
     93  1.12  christos 		if (el->el_state.inputmode != MODE_REPLACE_1)
     94  1.12  christos 			c_insert(el, el->el_state.argument);
     95   1.9     lukem 
     96  1.13  christos 		while (count-- && el->el_line.cursor < el->el_line.lastchar)
     97  1.30  christos 			*el->el_line.cursor++ = (Char)c;
     98   1.9     lukem 		re_refresh(el);
     99   1.9     lukem 	}
    100   1.1       cgd 
    101   1.9     lukem 	if (el->el_state.inputmode == MODE_REPLACE_1)
    102  1.12  christos 		return vi_command_mode(el, 0);
    103   1.1       cgd 
    104  1.26  christos 	return CC_NORM;
    105   1.1       cgd }
    106   1.1       cgd 
    107   1.1       cgd 
    108   1.8    simonb /* ed_delete_prev_word():
    109   1.1       cgd  *	Delete from beginning of current word to cursor
    110   1.1       cgd  *	[M-^?] [^W]
    111   1.1       cgd  */
    112   1.1       cgd protected el_action_t
    113   1.1       cgd /*ARGSUSED*/
    114  1.31  christos ed_delete_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
    115   1.9     lukem {
    116  1.24  christos 	Char *cp, *p, *kp;
    117   1.9     lukem 
    118   1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    119  1.26  christos 		return CC_ERROR;
    120   1.9     lukem 
    121   1.9     lukem 	cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
    122   1.9     lukem 	    el->el_state.argument, ce__isword);
    123   1.9     lukem 
    124   1.9     lukem 	for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
    125   1.9     lukem 		*kp++ = *p;
    126   1.9     lukem 	el->el_chared.c_kill.last = kp;
    127   1.9     lukem 
    128  1.22  christos 	c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
    129   1.9     lukem 	el->el_line.cursor = cp;
    130   1.9     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    131   1.9     lukem 		el->el_line.cursor = el->el_line.buffer; /* bounds check */
    132  1.26  christos 	return CC_REFRESH;
    133   1.1       cgd }
    134   1.1       cgd 
    135   1.1       cgd 
    136   1.8    simonb /* ed_delete_next_char():
    137   1.1       cgd  *	Delete character under cursor
    138   1.1       cgd  *	[^D] [x]
    139   1.1       cgd  */
    140   1.1       cgd protected el_action_t
    141   1.1       cgd /*ARGSUSED*/
    142  1.31  christos ed_delete_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
    143   1.1       cgd {
    144  1.27  christos #ifdef DEBUG_EDIT
    145   1.9     lukem #define	EL	el->el_line
    146   1.6  christos 	(void) fprintf(el->el_errlfile,
    147   1.8    simonb 	    "\nD(b: %x(%s)  c: %x(%s) last: %x(%s) limit: %x(%s)\n",
    148   1.6  christos 	    EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
    149   1.6  christos 	    EL.lastchar, EL.limit, EL.limit);
    150   1.1       cgd #endif
    151   1.9     lukem 	if (el->el_line.cursor == el->el_line.lastchar) {
    152   1.9     lukem 			/* if I'm at the end */
    153   1.9     lukem 		if (el->el_map.type == MAP_VI) {
    154   1.9     lukem 			if (el->el_line.cursor == el->el_line.buffer) {
    155   1.9     lukem 				/* if I'm also at the beginning */
    156   1.1       cgd #ifdef KSHVI
    157  1.26  christos 				return CC_ERROR;
    158   1.1       cgd #else
    159  1.19  christos 				/* then do an EOF */
    160  1.25  christos 				terminal_writec(el, c);
    161  1.26  christos 				return CC_EOF;
    162   1.1       cgd #endif
    163   1.9     lukem 			} else {
    164   1.1       cgd #ifdef KSHVI
    165   1.9     lukem 				el->el_line.cursor--;
    166   1.1       cgd #else
    167  1.26  christos 				return CC_ERROR;
    168   1.1       cgd #endif
    169   1.9     lukem 			}
    170  1.29  christos 		} else
    171  1.26  christos 				return CC_ERROR;
    172   1.1       cgd 	}
    173   1.9     lukem 	c_delafter(el, el->el_state.argument);	/* delete after dot */
    174  1.29  christos 	if (el->el_map.type == MAP_VI &&
    175  1.29  christos 	    el->el_line.cursor >= el->el_line.lastchar &&
    176   1.9     lukem 	    el->el_line.cursor > el->el_line.buffer)
    177   1.9     lukem 			/* bounds check */
    178   1.9     lukem 		el->el_line.cursor = el->el_line.lastchar - 1;
    179  1.26  christos 	return CC_REFRESH;
    180   1.1       cgd }
    181   1.1       cgd 
    182   1.1       cgd 
    183   1.8    simonb /* ed_kill_line():
    184   1.1       cgd  *	Cut to the end of line
    185   1.1       cgd  *	[^K] [^K]
    186   1.1       cgd  */
    187   1.1       cgd protected el_action_t
    188   1.1       cgd /*ARGSUSED*/
    189  1.31  christos ed_kill_line(EditLine *el, wint_t c __attribute__((__unused__)))
    190   1.9     lukem {
    191  1.24  christos 	Char *kp, *cp;
    192   1.9     lukem 
    193   1.9     lukem 	cp = el->el_line.cursor;
    194   1.9     lukem 	kp = el->el_chared.c_kill.buf;
    195   1.9     lukem 	while (cp < el->el_line.lastchar)
    196   1.9     lukem 		*kp++ = *cp++;	/* copy it */
    197   1.9     lukem 	el->el_chared.c_kill.last = kp;
    198   1.9     lukem 			/* zap! -- delete to end */
    199   1.9     lukem 	el->el_line.lastchar = el->el_line.cursor;
    200  1.26  christos 	return CC_REFRESH;
    201   1.1       cgd }
    202   1.1       cgd 
    203   1.1       cgd 
    204   1.8    simonb /* ed_move_to_end():
    205   1.1       cgd  *	Move cursor to the end of line
    206   1.1       cgd  *	[^E] [^E]
    207   1.1       cgd  */
    208   1.1       cgd protected el_action_t
    209   1.1       cgd /*ARGSUSED*/
    210  1.31  christos ed_move_to_end(EditLine *el, wint_t c __attribute__((__unused__)))
    211   1.1       cgd {
    212   1.9     lukem 
    213   1.9     lukem 	el->el_line.cursor = el->el_line.lastchar;
    214   1.9     lukem 	if (el->el_map.type == MAP_VI) {
    215  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    216   1.9     lukem 			cv_delfini(el);
    217  1.26  christos 			return CC_REFRESH;
    218   1.9     lukem 		}
    219  1.21   aymeric #ifdef VI_MOVE
    220  1.21   aymeric 		el->el_line.cursor--;
    221  1.21   aymeric #endif
    222   1.1       cgd 	}
    223  1.26  christos 	return CC_CURSOR;
    224   1.1       cgd }
    225   1.1       cgd 
    226   1.1       cgd 
    227   1.8    simonb /* ed_move_to_beg():
    228   1.1       cgd  *	Move cursor to the beginning of line
    229   1.1       cgd  *	[^A] [^A]
    230   1.1       cgd  */
    231   1.1       cgd protected el_action_t
    232   1.1       cgd /*ARGSUSED*/
    233  1.31  christos ed_move_to_beg(EditLine *el, wint_t c __attribute__((__unused__)))
    234   1.9     lukem {
    235   1.9     lukem 
    236   1.9     lukem 	el->el_line.cursor = el->el_line.buffer;
    237   1.9     lukem 
    238   1.9     lukem 	if (el->el_map.type == MAP_VI) {
    239   1.9     lukem 			/* We want FIRST non space character */
    240  1.24  christos 		while (Isspace(*el->el_line.cursor))
    241   1.9     lukem 			el->el_line.cursor++;
    242  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    243   1.9     lukem 			cv_delfini(el);
    244  1.26  christos 			return CC_REFRESH;
    245   1.9     lukem 		}
    246   1.1       cgd 	}
    247  1.26  christos 	return CC_CURSOR;
    248   1.1       cgd }
    249   1.1       cgd 
    250   1.1       cgd 
    251   1.8    simonb /* ed_transpose_chars():
    252   1.1       cgd  *	Exchange the character to the left of the cursor with the one under it
    253   1.1       cgd  *	[^T] [^T]
    254   1.1       cgd  */
    255   1.1       cgd protected el_action_t
    256  1.31  christos ed_transpose_chars(EditLine *el, wint_t c)
    257   1.9     lukem {
    258   1.9     lukem 
    259   1.9     lukem 	if (el->el_line.cursor < el->el_line.lastchar) {
    260   1.9     lukem 		if (el->el_line.lastchar <= &el->el_line.buffer[1])
    261  1.26  christos 			return CC_ERROR;
    262   1.9     lukem 		else
    263   1.9     lukem 			el->el_line.cursor++;
    264   1.9     lukem 	}
    265   1.9     lukem 	if (el->el_line.cursor > &el->el_line.buffer[1]) {
    266   1.9     lukem 		/* must have at least two chars entered */
    267   1.9     lukem 		c = el->el_line.cursor[-2];
    268   1.9     lukem 		el->el_line.cursor[-2] = el->el_line.cursor[-1];
    269  1.30  christos 		el->el_line.cursor[-1] = (Char)c;
    270  1.26  christos 		return CC_REFRESH;
    271   1.9     lukem 	} else
    272  1.26  christos 		return CC_ERROR;
    273   1.1       cgd }
    274   1.1       cgd 
    275   1.1       cgd 
    276   1.8    simonb /* ed_next_char():
    277   1.1       cgd  *	Move to the right one character
    278   1.1       cgd  *	[^F] [^F]
    279   1.1       cgd  */
    280   1.1       cgd protected el_action_t
    281   1.1       cgd /*ARGSUSED*/
    282  1.31  christos ed_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
    283   1.1       cgd {
    284  1.24  christos 	Char *lim = el->el_line.lastchar;
    285   1.1       cgd 
    286  1.13  christos 	if (el->el_line.cursor >= lim ||
    287  1.13  christos 	    (el->el_line.cursor == lim - 1 &&
    288  1.13  christos 	    el->el_map.type == MAP_VI &&
    289  1.13  christos 	    el->el_chared.c_vcmd.action == NOP))
    290  1.26  christos 		return CC_ERROR;
    291   1.1       cgd 
    292   1.9     lukem 	el->el_line.cursor += el->el_state.argument;
    293  1.13  christos 	if (el->el_line.cursor > lim)
    294  1.13  christos 		el->el_line.cursor = lim;
    295   1.1       cgd 
    296   1.9     lukem 	if (el->el_map.type == MAP_VI)
    297  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    298   1.9     lukem 			cv_delfini(el);
    299  1.26  christos 			return CC_REFRESH;
    300   1.9     lukem 		}
    301  1.26  christos 	return CC_CURSOR;
    302   1.1       cgd }
    303   1.1       cgd 
    304   1.1       cgd 
    305   1.8    simonb /* ed_prev_word():
    306   1.1       cgd  *	Move to the beginning of the current word
    307   1.1       cgd  *	[M-b] [b]
    308   1.1       cgd  */
    309   1.1       cgd protected el_action_t
    310   1.1       cgd /*ARGSUSED*/
    311  1.31  christos ed_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
    312   1.1       cgd {
    313   1.1       cgd 
    314   1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    315  1.26  christos 		return CC_ERROR;
    316   1.1       cgd 
    317   1.9     lukem 	el->el_line.cursor = c__prev_word(el->el_line.cursor,
    318   1.9     lukem 	    el->el_line.buffer,
    319   1.9     lukem 	    el->el_state.argument,
    320   1.9     lukem 	    ce__isword);
    321   1.1       cgd 
    322   1.9     lukem 	if (el->el_map.type == MAP_VI)
    323  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    324   1.9     lukem 			cv_delfini(el);
    325  1.26  christos 			return CC_REFRESH;
    326   1.9     lukem 		}
    327  1.26  christos 	return CC_CURSOR;
    328   1.1       cgd }
    329   1.1       cgd 
    330   1.1       cgd 
    331   1.8    simonb /* ed_prev_char():
    332   1.1       cgd  *	Move to the left one character
    333   1.1       cgd  *	[^B] [^B]
    334   1.1       cgd  */
    335   1.1       cgd protected el_action_t
    336   1.1       cgd /*ARGSUSED*/
    337  1.31  christos ed_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    338   1.1       cgd {
    339   1.1       cgd 
    340   1.9     lukem 	if (el->el_line.cursor > el->el_line.buffer) {
    341   1.9     lukem 		el->el_line.cursor -= el->el_state.argument;
    342   1.9     lukem 		if (el->el_line.cursor < el->el_line.buffer)
    343   1.9     lukem 			el->el_line.cursor = el->el_line.buffer;
    344   1.9     lukem 
    345   1.9     lukem 		if (el->el_map.type == MAP_VI)
    346  1.13  christos 			if (el->el_chared.c_vcmd.action != NOP) {
    347   1.9     lukem 				cv_delfini(el);
    348  1.26  christos 				return CC_REFRESH;
    349   1.9     lukem 			}
    350  1.26  christos 		return CC_CURSOR;
    351   1.9     lukem 	} else
    352  1.26  christos 		return CC_ERROR;
    353   1.1       cgd }
    354   1.1       cgd 
    355   1.1       cgd 
    356   1.8    simonb /* ed_quoted_insert():
    357   1.1       cgd  *	Add the next character typed verbatim
    358   1.1       cgd  *	[^V] [^V]
    359   1.1       cgd  */
    360   1.1       cgd protected el_action_t
    361  1.31  christos ed_quoted_insert(EditLine *el, wint_t c)
    362   1.9     lukem {
    363   1.9     lukem 	int num;
    364  1.24  christos 	Char tc;
    365   1.9     lukem 
    366   1.9     lukem 	tty_quotemode(el);
    367  1.24  christos 	num = FUN(el,getc)(el, &tc);
    368  1.24  christos 	c = tc;
    369   1.9     lukem 	tty_noquotemode(el);
    370   1.9     lukem 	if (num == 1)
    371  1.26  christos 		return ed_insert(el, c);
    372   1.9     lukem 	else
    373  1.26  christos 		return ed_end_of_file(el, 0);
    374   1.1       cgd }
    375   1.1       cgd 
    376   1.1       cgd 
    377   1.8    simonb /* ed_digit():
    378   1.1       cgd  *	Adds to argument or enters a digit
    379   1.1       cgd  */
    380   1.1       cgd protected el_action_t
    381  1.31  christos ed_digit(EditLine *el, wint_t c)
    382   1.9     lukem {
    383   1.9     lukem 
    384  1.24  christos 	if (!Isdigit(c))
    385  1.26  christos 		return CC_ERROR;
    386   1.9     lukem 
    387   1.9     lukem 	if (el->el_state.doingarg) {
    388   1.9     lukem 			/* if doing an arg, add this in... */
    389   1.9     lukem 		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
    390   1.9     lukem 			el->el_state.argument = c - '0';
    391   1.9     lukem 		else {
    392   1.9     lukem 			if (el->el_state.argument > 1000000)
    393  1.26  christos 				return CC_ERROR;
    394   1.9     lukem 			el->el_state.argument =
    395   1.9     lukem 			    (el->el_state.argument * 10) + (c - '0');
    396   1.9     lukem 		}
    397  1.26  christos 		return CC_ARGHACK;
    398  1.12  christos 	}
    399   1.9     lukem 
    400  1.12  christos 	return ed_insert(el, c);
    401   1.1       cgd }
    402   1.1       cgd 
    403   1.1       cgd 
    404   1.8    simonb /* ed_argument_digit():
    405   1.1       cgd  *	Digit that starts argument
    406   1.1       cgd  *	For ESC-n
    407   1.1       cgd  */
    408   1.1       cgd protected el_action_t
    409  1.31  christos ed_argument_digit(EditLine *el, wint_t c)
    410   1.9     lukem {
    411   1.9     lukem 
    412  1.24  christos 	if (!Isdigit(c))
    413  1.26  christos 		return CC_ERROR;
    414   1.9     lukem 
    415   1.9     lukem 	if (el->el_state.doingarg) {
    416   1.9     lukem 		if (el->el_state.argument > 1000000)
    417  1.26  christos 			return CC_ERROR;
    418   1.9     lukem 		el->el_state.argument = (el->el_state.argument * 10) +
    419   1.9     lukem 		    (c - '0');
    420   1.9     lukem 	} else {		/* else starting an argument */
    421   1.9     lukem 		el->el_state.argument = c - '0';
    422   1.9     lukem 		el->el_state.doingarg = 1;
    423   1.9     lukem 	}
    424  1.26  christos 	return CC_ARGHACK;
    425   1.1       cgd }
    426   1.1       cgd 
    427   1.1       cgd 
    428   1.8    simonb /* ed_unassigned():
    429   1.1       cgd  *	Indicates unbound character
    430   1.1       cgd  *	Bound to keys that are not assigned
    431   1.1       cgd  */
    432   1.1       cgd protected el_action_t
    433   1.1       cgd /*ARGSUSED*/
    434  1.28  christos ed_unassigned(EditLine *el __attribute__((__unused__)),
    435  1.31  christos     wint_t c __attribute__((__unused__)))
    436   1.9     lukem {
    437   1.9     lukem 
    438  1.26  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.31  christos 	      wint_t c __attribute__((__unused__)))
    454   1.8    simonb {
    455   1.9     lukem 
    456  1.26  christos 	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.31  christos 	     wint_t c __attribute__((__unused__)))
    468   1.1       cgd {
    469   1.9     lukem 
    470  1.26  christos 	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.31  christos 		    wint_t c __attribute__((__unused__)))
    482   1.1       cgd {
    483   1.9     lukem 
    484  1.26  christos 	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.31  christos 	       wint_t c __attribute__((__unused__)))
    496   1.1       cgd {
    497   1.9     lukem 
    498  1.26  christos 	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.31  christos 	       wint_t c __attribute__((__unused__)))
    510   1.1       cgd {
    511   1.9     lukem 
    512  1.26  christos 	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.31  christos 		   wint_t c __attribute__((__unused__)))
    524   1.1       cgd {
    525   1.9     lukem 
    526  1.26  christos 	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.31  christos 		    wint_t c __attribute__((__unused__)))
    538   1.1       cgd {
    539   1.9     lukem 
    540  1.26  christos 	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.31  christos ed_newline(EditLine *el, wint_t 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.26  christos 	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.31  christos ed_delete_prev_char(EditLine *el, wint_t 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.26  christos 		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.26  christos 	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.31  christos ed_clear_screen(EditLine *el, wint_t c __attribute__((__unused__)))
    587   1.9     lukem {
    588   1.9     lukem 
    589  1.25  christos 	terminal_clear_screen(el);	/* clear the whole real screen */
    590   1.9     lukem 	re_clear_display(el);	/* reset everything */
    591  1.26  christos 	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.31  christos 	     wint_t c __attribute__((__unused__)))
    603   1.1       cgd {
    604   1.9     lukem 
    605  1.26  christos 	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.31  christos ed_start_over(EditLine *el, wint_t c __attribute__((__unused__)))
    616   1.1       cgd {
    617   1.9     lukem 
    618  1.17  christos 	ch_reset(el, 0);
    619  1.26  christos 	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.31  christos 		    wint_t c __attribute__((__unused__)))
    631   1.1       cgd {
    632   1.9     lukem 
    633  1.26  christos 	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.31  christos ed_prev_history(EditLine *el, wint_t 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.24  christos 		(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.24  christos 
    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.31  christos ed_next_history(EditLine *el, wint_t 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.31  christos ed_search_prev_history(EditLine *el, wint_t c __attribute__((__unused__)))
    709   1.9     lukem {
    710  1.24  christos 	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.26  christos 		return CC_ERROR;
    724   1.9     lukem 	}
    725   1.9     lukem 	if (el->el_history.eventno == 0) {
    726  1.24  christos 		(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.26  christos 		return CC_ERROR;
    733   1.1       cgd 
    734   1.9     lukem 	hp = HIST_FIRST(el);
    735   1.9     lukem 	if (hp == NULL)
    736  1.26  christos 		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.24  christos 		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.26  christos 		return CC_ERROR;
    763   1.9     lukem 	}
    764   1.9     lukem 	el->el_history.eventno = h;
    765   1.1       cgd 
    766  1.26  christos 	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.31  christos ed_search_next_history(EditLine *el, wint_t c __attribute__((__unused__)))
    777   1.1       cgd {
    778  1.24  christos 	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.26  christos 		return CC_ERROR;
    788   1.1       cgd 
    789   1.9     lukem 	if (el->el_history.ref == NULL)
    790  1.26  christos 		return CC_ERROR;
    791   1.1       cgd 
    792   1.9     lukem 	hp = HIST_FIRST(el);
    793   1.9     lukem 	if (hp == NULL)
    794  1.26  christos 		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.24  christos 		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.26  christos 			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.26  christos 	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.31  christos ed_prev_line(EditLine *el, wint_t c __attribute__((__unused__)))
    831   1.9     lukem {
    832  1.24  christos 	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.26  christos 		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.26  christos 	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.31  christos ed_next_line(EditLine *el, wint_t c __attribute__((__unused__)))
    874   1.9     lukem {
    875  1.24  christos 	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.26  christos 		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.26  christos 	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.31  christos ed_command(EditLine *el, wint_t c __attribute__((__unused__)))
    908   1.9     lukem {
    909  1.24  christos 	Char tmpbuf[EL_BUFSIZ];
    910   1.9     lukem 	int tmplen;
    911   1.9     lukem 
    912  1.24  christos 	tmplen = c_gets(el, tmpbuf, STR("\n: "));
    913  1.25  christos 	terminal__putc(el, '\n');
    914   1.9     lukem 
    915  1.14  christos 	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
    916  1.25  christos 		terminal_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