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