Home | History | Annotate | Line # | Download | only in libedit
common.c revision 1.14
      1  1.14  christos /*	$NetBSD: common.c,v 1.14 2002/11/20 16:50:08 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.14  christos __RCSID("$NetBSD: common.c,v 1.14 2002/11/20 16:50:08 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.13  christos 	int count = el->el_state.argument;
     76   1.1       cgd 
     77   1.9     lukem 	if (c == '\0')
     78   1.9     lukem 		return (CC_ERROR);
     79   1.1       cgd 
     80   1.9     lukem 	if (el->el_line.lastchar + el->el_state.argument >=
     81  1.10  jdolecek 	    el->el_line.limit) {
     82  1.10  jdolecek 		/* end of buffer space, try to allocate more */
     83  1.13  christos 		if (!ch_enlargebufs(el, (size_t) count))
     84  1.10  jdolecek 			return CC_ERROR;	/* error allocating more */
     85  1.10  jdolecek 	}
     86   1.9     lukem 
     87  1.13  christos 	if (count == 1) {
     88  1.12  christos 		if (el->el_state.inputmode == MODE_INSERT
     89  1.12  christos 		    || el->el_line.cursor >= el->el_line.lastchar)
     90  1.12  christos 			c_insert(el, 1);
     91   1.9     lukem 
     92   1.9     lukem 		*el->el_line.cursor++ = c;
     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.13  christos 		while (count-- && el->el_line.cursor < el->el_line.lastchar)
     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.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    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.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    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.13  christos 	char *lim = el->el_line.lastchar;
    291   1.1       cgd 
    292  1.13  christos 	if (el->el_line.cursor >= lim ||
    293  1.13  christos 	    (el->el_line.cursor == lim - 1 &&
    294  1.13  christos 	    el->el_map.type == MAP_VI &&
    295  1.13  christos 	    el->el_chared.c_vcmd.action == NOP))
    296   1.9     lukem 		return (CC_ERROR);
    297   1.1       cgd 
    298   1.9     lukem 	el->el_line.cursor += el->el_state.argument;
    299  1.13  christos 	if (el->el_line.cursor > lim)
    300  1.13  christos 		el->el_line.cursor = lim;
    301   1.1       cgd 
    302   1.9     lukem 	if (el->el_map.type == MAP_VI)
    303  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    304   1.9     lukem 			cv_delfini(el);
    305   1.9     lukem 			return (CC_REFRESH);
    306   1.9     lukem 		}
    307   1.9     lukem 	return (CC_CURSOR);
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.1       cgd 
    311   1.8    simonb /* ed_prev_word():
    312   1.1       cgd  *	Move to the beginning of the current word
    313   1.1       cgd  *	[M-b] [b]
    314   1.1       cgd  */
    315   1.1       cgd protected el_action_t
    316   1.1       cgd /*ARGSUSED*/
    317   1.9     lukem ed_prev_word(EditLine *el, int c)
    318   1.1       cgd {
    319   1.1       cgd 
    320   1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    321   1.9     lukem 		return (CC_ERROR);
    322   1.1       cgd 
    323   1.9     lukem 	el->el_line.cursor = c__prev_word(el->el_line.cursor,
    324   1.9     lukem 	    el->el_line.buffer,
    325   1.9     lukem 	    el->el_state.argument,
    326   1.9     lukem 	    ce__isword);
    327   1.1       cgd 
    328   1.9     lukem 	if (el->el_map.type == MAP_VI)
    329  1.13  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    330   1.9     lukem 			cv_delfini(el);
    331   1.9     lukem 			return (CC_REFRESH);
    332   1.9     lukem 		}
    333   1.9     lukem 	return (CC_CURSOR);
    334   1.1       cgd }
    335   1.1       cgd 
    336   1.1       cgd 
    337   1.8    simonb /* ed_prev_char():
    338   1.1       cgd  *	Move to the left one character
    339   1.1       cgd  *	[^B] [^B]
    340   1.1       cgd  */
    341   1.1       cgd protected el_action_t
    342   1.1       cgd /*ARGSUSED*/
    343   1.9     lukem ed_prev_char(EditLine *el, int c)
    344   1.1       cgd {
    345   1.1       cgd 
    346   1.9     lukem 	if (el->el_line.cursor > el->el_line.buffer) {
    347   1.9     lukem 		el->el_line.cursor -= el->el_state.argument;
    348   1.9     lukem 		if (el->el_line.cursor < el->el_line.buffer)
    349   1.9     lukem 			el->el_line.cursor = el->el_line.buffer;
    350   1.9     lukem 
    351   1.9     lukem 		if (el->el_map.type == MAP_VI)
    352  1.13  christos 			if (el->el_chared.c_vcmd.action != NOP) {
    353   1.9     lukem 				cv_delfini(el);
    354   1.9     lukem 				return (CC_REFRESH);
    355   1.9     lukem 			}
    356   1.9     lukem 		return (CC_CURSOR);
    357   1.9     lukem 	} else
    358   1.9     lukem 		return (CC_ERROR);
    359   1.1       cgd }
    360   1.1       cgd 
    361   1.1       cgd 
    362   1.8    simonb /* ed_quoted_insert():
    363   1.1       cgd  *	Add the next character typed verbatim
    364   1.1       cgd  *	[^V] [^V]
    365   1.1       cgd  */
    366   1.1       cgd protected el_action_t
    367   1.9     lukem ed_quoted_insert(EditLine *el, int c)
    368   1.9     lukem {
    369   1.9     lukem 	int num;
    370   1.9     lukem 	char tc;
    371   1.9     lukem 
    372   1.9     lukem 	tty_quotemode(el);
    373   1.9     lukem 	num = el_getc(el, &tc);
    374   1.9     lukem 	c = (unsigned char) tc;
    375   1.9     lukem 	tty_noquotemode(el);
    376   1.9     lukem 	if (num == 1)
    377   1.9     lukem 		return (ed_insert(el, c));
    378   1.9     lukem 	else
    379   1.9     lukem 		return (ed_end_of_file(el, 0));
    380   1.1       cgd }
    381   1.1       cgd 
    382   1.1       cgd 
    383   1.8    simonb /* ed_digit():
    384   1.1       cgd  *	Adds to argument or enters a digit
    385   1.1       cgd  */
    386   1.1       cgd protected el_action_t
    387   1.9     lukem ed_digit(EditLine *el, int c)
    388   1.9     lukem {
    389   1.9     lukem 
    390   1.9     lukem 	if (!isdigit(c))
    391   1.9     lukem 		return (CC_ERROR);
    392   1.9     lukem 
    393   1.9     lukem 	if (el->el_state.doingarg) {
    394   1.9     lukem 			/* if doing an arg, add this in... */
    395   1.9     lukem 		if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
    396   1.9     lukem 			el->el_state.argument = c - '0';
    397   1.9     lukem 		else {
    398   1.9     lukem 			if (el->el_state.argument > 1000000)
    399   1.9     lukem 				return (CC_ERROR);
    400   1.9     lukem 			el->el_state.argument =
    401   1.9     lukem 			    (el->el_state.argument * 10) + (c - '0');
    402   1.9     lukem 		}
    403   1.9     lukem 		return (CC_ARGHACK);
    404  1.12  christos 	}
    405   1.9     lukem 
    406  1.12  christos 	return ed_insert(el, c);
    407   1.1       cgd }
    408   1.1       cgd 
    409   1.1       cgd 
    410   1.8    simonb /* ed_argument_digit():
    411   1.1       cgd  *	Digit that starts argument
    412   1.1       cgd  *	For ESC-n
    413   1.1       cgd  */
    414   1.1       cgd protected el_action_t
    415   1.9     lukem ed_argument_digit(EditLine *el, int c)
    416   1.9     lukem {
    417   1.9     lukem 
    418   1.9     lukem 	if (!isdigit(c))
    419   1.9     lukem 		return (CC_ERROR);
    420   1.9     lukem 
    421   1.9     lukem 	if (el->el_state.doingarg) {
    422   1.9     lukem 		if (el->el_state.argument > 1000000)
    423   1.9     lukem 			return (CC_ERROR);
    424   1.9     lukem 		el->el_state.argument = (el->el_state.argument * 10) +
    425   1.9     lukem 		    (c - '0');
    426   1.9     lukem 	} else {		/* else starting an argument */
    427   1.9     lukem 		el->el_state.argument = c - '0';
    428   1.9     lukem 		el->el_state.doingarg = 1;
    429   1.9     lukem 	}
    430   1.9     lukem 	return (CC_ARGHACK);
    431   1.1       cgd }
    432   1.1       cgd 
    433   1.1       cgd 
    434   1.8    simonb /* ed_unassigned():
    435   1.1       cgd  *	Indicates unbound character
    436   1.1       cgd  *	Bound to keys that are not assigned
    437   1.1       cgd  */
    438   1.1       cgd protected el_action_t
    439   1.1       cgd /*ARGSUSED*/
    440   1.9     lukem ed_unassigned(EditLine *el, int c)
    441   1.9     lukem {
    442   1.9     lukem 
    443  1.13  christos 	return (CC_ERROR);
    444   1.1       cgd }
    445   1.1       cgd 
    446   1.1       cgd 
    447   1.1       cgd /**
    448   1.1       cgd  ** TTY key handling.
    449   1.1       cgd  **/
    450   1.1       cgd 
    451   1.8    simonb /* ed_tty_sigint():
    452   1.1       cgd  *	Tty interrupt character
    453   1.1       cgd  *	[^C]
    454   1.1       cgd  */
    455   1.1       cgd protected el_action_t
    456   1.1       cgd /*ARGSUSED*/
    457   1.9     lukem ed_tty_sigint(EditLine *el, int c)
    458   1.8    simonb {
    459   1.9     lukem 
    460   1.9     lukem 	return (CC_NORM);
    461   1.1       cgd }
    462   1.1       cgd 
    463   1.1       cgd 
    464   1.8    simonb /* ed_tty_dsusp():
    465   1.1       cgd  *	Tty delayed suspend character
    466   1.1       cgd  *	[^Y]
    467   1.1       cgd  */
    468   1.1       cgd protected el_action_t
    469   1.1       cgd /*ARGSUSED*/
    470   1.9     lukem ed_tty_dsusp(EditLine *el, int c)
    471   1.1       cgd {
    472   1.9     lukem 
    473   1.9     lukem 	return (CC_NORM);
    474   1.1       cgd }
    475   1.1       cgd 
    476   1.1       cgd 
    477   1.8    simonb /* ed_tty_flush_output():
    478   1.1       cgd  *	Tty flush output characters
    479   1.1       cgd  *	[^O]
    480   1.1       cgd  */
    481   1.1       cgd protected el_action_t
    482   1.1       cgd /*ARGSUSED*/
    483   1.9     lukem ed_tty_flush_output(EditLine *el, int c)
    484   1.1       cgd {
    485   1.9     lukem 
    486   1.9     lukem 	return (CC_NORM);
    487   1.1       cgd }
    488   1.1       cgd 
    489   1.1       cgd 
    490   1.8    simonb /* ed_tty_sigquit():
    491   1.1       cgd  *	Tty quit character
    492   1.1       cgd  *	[^\]
    493   1.1       cgd  */
    494   1.1       cgd protected el_action_t
    495   1.1       cgd /*ARGSUSED*/
    496   1.9     lukem ed_tty_sigquit(EditLine *el, int c)
    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.9     lukem ed_tty_sigtstp(EditLine *el, int c)
    510   1.1       cgd {
    511   1.9     lukem 
    512   1.9     lukem 	return (CC_NORM);
    513   1.1       cgd }
    514   1.1       cgd 
    515   1.1       cgd 
    516   1.8    simonb /* ed_tty_stop_output():
    517   1.1       cgd  *	Tty disallow output characters
    518   1.1       cgd  *	[^S]
    519   1.1       cgd  */
    520   1.1       cgd protected el_action_t
    521   1.1       cgd /*ARGSUSED*/
    522   1.9     lukem ed_tty_stop_output(EditLine *el, int c)
    523   1.1       cgd {
    524   1.9     lukem 
    525   1.9     lukem 	return (CC_NORM);
    526   1.1       cgd }
    527   1.1       cgd 
    528   1.1       cgd 
    529   1.8    simonb /* ed_tty_start_output():
    530   1.1       cgd  *	Tty allow output characters
    531   1.1       cgd  *	[^Q]
    532   1.1       cgd  */
    533   1.1       cgd protected el_action_t
    534   1.1       cgd /*ARGSUSED*/
    535   1.9     lukem ed_tty_start_output(EditLine *el, int c)
    536   1.1       cgd {
    537   1.9     lukem 
    538   1.9     lukem 	return (CC_NORM);
    539   1.1       cgd }
    540   1.1       cgd 
    541   1.1       cgd 
    542   1.8    simonb /* ed_newline():
    543   1.1       cgd  *	Execute command
    544   1.1       cgd  *	[^J]
    545   1.1       cgd  */
    546   1.1       cgd protected el_action_t
    547   1.1       cgd /*ARGSUSED*/
    548   1.9     lukem ed_newline(EditLine *el, int c)
    549   1.9     lukem {
    550   1.9     lukem 
    551   1.9     lukem 	re_goto_bottom(el);
    552   1.9     lukem 	*el->el_line.lastchar++ = '\n';
    553   1.9     lukem 	*el->el_line.lastchar = '\0';
    554   1.9     lukem 	return (CC_NEWLINE);
    555   1.1       cgd }
    556   1.1       cgd 
    557   1.1       cgd 
    558   1.8    simonb /* ed_delete_prev_char():
    559   1.1       cgd  *	Delete the character to the left of the cursor
    560   1.1       cgd  *	[^?]
    561   1.1       cgd  */
    562   1.1       cgd protected el_action_t
    563   1.1       cgd /*ARGSUSED*/
    564   1.9     lukem ed_delete_prev_char(EditLine *el, int c)
    565   1.9     lukem {
    566   1.9     lukem 
    567   1.9     lukem 	if (el->el_line.cursor <= el->el_line.buffer)
    568   1.9     lukem 		return (CC_ERROR);
    569   1.9     lukem 
    570   1.9     lukem 	c_delbefore(el, el->el_state.argument);
    571   1.9     lukem 	el->el_line.cursor -= el->el_state.argument;
    572   1.9     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    573   1.9     lukem 		el->el_line.cursor = el->el_line.buffer;
    574   1.9     lukem 	return (CC_REFRESH);
    575   1.1       cgd }
    576   1.1       cgd 
    577   1.1       cgd 
    578   1.8    simonb /* ed_clear_screen():
    579   1.1       cgd  *	Clear screen leaving current line at the top
    580   1.1       cgd  *	[^L]
    581   1.1       cgd  */
    582   1.1       cgd protected el_action_t
    583   1.1       cgd /*ARGSUSED*/
    584   1.9     lukem ed_clear_screen(EditLine *el, int c)
    585   1.9     lukem {
    586   1.9     lukem 
    587   1.9     lukem 	term_clear_screen(el);	/* clear the whole real screen */
    588   1.9     lukem 	re_clear_display(el);	/* reset everything */
    589   1.9     lukem 	return (CC_REFRESH);
    590   1.1       cgd }
    591   1.1       cgd 
    592   1.1       cgd 
    593   1.8    simonb /* ed_redisplay():
    594   1.1       cgd  *	Redisplay everything
    595   1.1       cgd  *	^R
    596   1.1       cgd  */
    597   1.1       cgd protected el_action_t
    598   1.1       cgd /*ARGSUSED*/
    599   1.9     lukem ed_redisplay(EditLine *el, int c)
    600   1.1       cgd {
    601   1.9     lukem 
    602   1.9     lukem 	return (CC_REDISPLAY);
    603   1.1       cgd }
    604   1.1       cgd 
    605   1.1       cgd 
    606   1.8    simonb /* ed_start_over():
    607   1.1       cgd  *	Erase current line and start from scratch
    608   1.1       cgd  *	[^G]
    609   1.1       cgd  */
    610   1.1       cgd protected el_action_t
    611   1.1       cgd /*ARGSUSED*/
    612   1.9     lukem ed_start_over(EditLine *el, int c)
    613   1.1       cgd {
    614   1.9     lukem 
    615   1.9     lukem 	ch_reset(el);
    616   1.9     lukem 	return (CC_REFRESH);
    617   1.1       cgd }
    618   1.1       cgd 
    619   1.1       cgd 
    620   1.8    simonb /* ed_sequence_lead_in():
    621   1.1       cgd  *	First character in a bound sequence
    622   1.1       cgd  *	Placeholder for external keys
    623   1.1       cgd  */
    624   1.1       cgd protected el_action_t
    625   1.1       cgd /*ARGSUSED*/
    626   1.9     lukem ed_sequence_lead_in(EditLine *el, int c)
    627   1.1       cgd {
    628   1.9     lukem 
    629   1.9     lukem 	return (CC_NORM);
    630   1.1       cgd }
    631   1.1       cgd 
    632   1.1       cgd 
    633   1.8    simonb /* ed_prev_history():
    634   1.1       cgd  *	Move to the previous history line
    635   1.1       cgd  *	[^P] [k]
    636   1.1       cgd  */
    637   1.1       cgd protected el_action_t
    638   1.1       cgd /*ARGSUSED*/
    639   1.9     lukem ed_prev_history(EditLine *el, int c)
    640   1.9     lukem {
    641   1.9     lukem 	char beep = 0;
    642  1.13  christos 	int sv_event = el->el_history.eventno;
    643   1.9     lukem 
    644  1.12  christos 	el->el_chared.c_undo.len = -1;
    645   1.9     lukem 	*el->el_line.lastchar = '\0';		/* just in case */
    646   1.9     lukem 
    647   1.9     lukem 	if (el->el_history.eventno == 0) {	/* save the current buffer
    648   1.9     lukem 						 * away */
    649   1.9     lukem 		(void) strncpy(el->el_history.buf, el->el_line.buffer,
    650   1.9     lukem 		    EL_BUFSIZ);
    651   1.9     lukem 		el->el_history.last = el->el_history.buf +
    652   1.9     lukem 		    (el->el_line.lastchar - el->el_line.buffer);
    653   1.9     lukem 	}
    654   1.9     lukem 	el->el_history.eventno += el->el_state.argument;
    655   1.9     lukem 
    656   1.9     lukem 	if (hist_get(el) == CC_ERROR) {
    657  1.13  christos 		if (el->el_map.type == MAP_VI) {
    658  1.13  christos 			el->el_history.eventno = sv_event;
    659  1.13  christos 			return CC_ERROR;
    660  1.13  christos 		}
    661   1.9     lukem 		beep = 1;
    662   1.9     lukem 		/* el->el_history.eventno was fixed by first call */
    663   1.9     lukem 		(void) hist_get(el);
    664   1.9     lukem 	}
    665   1.9     lukem 	if (beep)
    666  1.13  christos 		return CC_REFRESH_BEEP;
    667  1.13  christos 	return CC_REFRESH;
    668   1.1       cgd }
    669   1.1       cgd 
    670   1.1       cgd 
    671   1.8    simonb /* ed_next_history():
    672   1.1       cgd  *	Move to the next history line
    673   1.1       cgd  *	[^N] [j]
    674   1.1       cgd  */
    675   1.1       cgd protected el_action_t
    676   1.1       cgd /*ARGSUSED*/
    677   1.9     lukem ed_next_history(EditLine *el, int c)
    678   1.1       cgd {
    679  1.13  christos 	el_action_t beep = CC_REFRESH, rval;
    680   1.1       cgd 
    681  1.12  christos 	el->el_chared.c_undo.len = -1;
    682   1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    683   1.1       cgd 
    684   1.9     lukem 	el->el_history.eventno -= el->el_state.argument;
    685   1.1       cgd 
    686   1.9     lukem 	if (el->el_history.eventno < 0) {
    687   1.9     lukem 		el->el_history.eventno = 0;
    688  1.13  christos 		beep = CC_REFRESH_BEEP;
    689   1.9     lukem 	}
    690  1.13  christos 	rval = hist_get(el);
    691  1.13  christos 	if (rval == CC_REFRESH)
    692  1.13  christos 		return beep;
    693  1.13  christos 	return rval;
    694  1.13  christos 
    695   1.1       cgd }
    696   1.1       cgd 
    697   1.1       cgd 
    698   1.8    simonb /* ed_search_prev_history():
    699   1.1       cgd  *	Search previous in history for a line matching the current
    700   1.1       cgd  *	next search history [M-P] [K]
    701   1.1       cgd  */
    702   1.1       cgd protected el_action_t
    703   1.1       cgd /*ARGSUSED*/
    704   1.9     lukem ed_search_prev_history(EditLine *el, int c)
    705   1.9     lukem {
    706   1.9     lukem 	const char *hp;
    707   1.9     lukem 	int h;
    708   1.9     lukem 	bool_t found = 0;
    709   1.9     lukem 
    710   1.9     lukem 	el->el_chared.c_vcmd.action = NOP;
    711  1.12  christos 	el->el_chared.c_undo.len = -1;
    712   1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    713   1.9     lukem 	if (el->el_history.eventno < 0) {
    714   1.1       cgd #ifdef DEBUG_EDIT
    715   1.9     lukem 		(void) fprintf(el->el_errfile,
    716   1.9     lukem 		    "e_prev_search_hist(): eventno < 0;\n");
    717   1.1       cgd #endif
    718   1.9     lukem 		el->el_history.eventno = 0;
    719   1.9     lukem 		return (CC_ERROR);
    720   1.9     lukem 	}
    721   1.9     lukem 	if (el->el_history.eventno == 0) {
    722   1.9     lukem 		(void) strncpy(el->el_history.buf, el->el_line.buffer,
    723   1.9     lukem 		    EL_BUFSIZ);
    724   1.9     lukem 		el->el_history.last = el->el_history.buf +
    725   1.9     lukem 		    (el->el_line.lastchar - el->el_line.buffer);
    726   1.9     lukem 	}
    727   1.9     lukem 	if (el->el_history.ref == NULL)
    728   1.9     lukem 		return (CC_ERROR);
    729   1.1       cgd 
    730   1.9     lukem 	hp = HIST_FIRST(el);
    731   1.9     lukem 	if (hp == NULL)
    732   1.9     lukem 		return (CC_ERROR);
    733   1.1       cgd 
    734   1.9     lukem 	c_setpat(el);		/* Set search pattern !! */
    735   1.1       cgd 
    736   1.9     lukem 	for (h = 1; h <= el->el_history.eventno; h++)
    737   1.9     lukem 		hp = HIST_NEXT(el);
    738   1.1       cgd 
    739   1.9     lukem 	while (hp != NULL) {
    740   1.1       cgd #ifdef SDEBUG
    741   1.9     lukem 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    742   1.1       cgd #endif
    743   1.9     lukem 		if ((strncmp(hp, el->el_line.buffer, (size_t)
    744   1.9     lukem 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    745   1.9     lukem 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    746   1.9     lukem 		    c_hmatch(el, hp)) {
    747   1.9     lukem 			found++;
    748   1.9     lukem 			break;
    749   1.9     lukem 		}
    750   1.9     lukem 		h++;
    751   1.9     lukem 		hp = HIST_NEXT(el);
    752   1.9     lukem 	}
    753   1.1       cgd 
    754   1.9     lukem 	if (!found) {
    755   1.1       cgd #ifdef SDEBUG
    756   1.9     lukem 		(void) fprintf(el->el_errfile, "not found\n");
    757   1.1       cgd #endif
    758   1.9     lukem 		return (CC_ERROR);
    759   1.9     lukem 	}
    760   1.9     lukem 	el->el_history.eventno = h;
    761   1.1       cgd 
    762   1.9     lukem 	return (hist_get(el));
    763   1.1       cgd }
    764   1.1       cgd 
    765   1.1       cgd 
    766   1.8    simonb /* ed_search_next_history():
    767   1.1       cgd  *	Search next in history for a line matching the current
    768   1.1       cgd  *	[M-N] [J]
    769   1.1       cgd  */
    770   1.1       cgd protected el_action_t
    771   1.1       cgd /*ARGSUSED*/
    772   1.9     lukem ed_search_next_history(EditLine *el, int c)
    773   1.1       cgd {
    774   1.9     lukem 	const char *hp;
    775   1.9     lukem 	int h;
    776   1.9     lukem 	bool_t found = 0;
    777   1.1       cgd 
    778   1.9     lukem 	el->el_chared.c_vcmd.action = NOP;
    779  1.12  christos 	el->el_chared.c_undo.len = -1;
    780   1.9     lukem 	*el->el_line.lastchar = '\0';	/* just in case */
    781   1.1       cgd 
    782   1.9     lukem 	if (el->el_history.eventno == 0)
    783   1.9     lukem 		return (CC_ERROR);
    784   1.1       cgd 
    785   1.9     lukem 	if (el->el_history.ref == NULL)
    786   1.9     lukem 		return (CC_ERROR);
    787   1.1       cgd 
    788   1.9     lukem 	hp = HIST_FIRST(el);
    789   1.9     lukem 	if (hp == NULL)
    790   1.9     lukem 		return (CC_ERROR);
    791   1.1       cgd 
    792   1.9     lukem 	c_setpat(el);		/* Set search pattern !! */
    793   1.1       cgd 
    794   1.9     lukem 	for (h = 1; h < el->el_history.eventno && hp; h++) {
    795   1.1       cgd #ifdef SDEBUG
    796   1.9     lukem 		(void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
    797   1.1       cgd #endif
    798   1.9     lukem 		if ((strncmp(hp, el->el_line.buffer, (size_t)
    799   1.9     lukem 			    (el->el_line.lastchar - el->el_line.buffer)) ||
    800   1.9     lukem 			hp[el->el_line.lastchar - el->el_line.buffer]) &&
    801   1.9     lukem 		    c_hmatch(el, hp))
    802   1.9     lukem 			found = h;
    803   1.9     lukem 		hp = HIST_NEXT(el);
    804   1.9     lukem 	}
    805   1.1       cgd 
    806   1.9     lukem 	if (!found) {		/* is it the current history number? */
    807   1.9     lukem 		if (!c_hmatch(el, el->el_history.buf)) {
    808   1.1       cgd #ifdef SDEBUG
    809   1.9     lukem 			(void) fprintf(el->el_errfile, "not found\n");
    810   1.1       cgd #endif
    811   1.9     lukem 			return (CC_ERROR);
    812   1.9     lukem 		}
    813   1.1       cgd 	}
    814   1.9     lukem 	el->el_history.eventno = found;
    815   1.1       cgd 
    816   1.9     lukem 	return (hist_get(el));
    817   1.1       cgd }
    818   1.1       cgd 
    819   1.1       cgd 
    820   1.1       cgd /* ed_prev_line():
    821   1.1       cgd  *	Move up one line
    822   1.1       cgd  *	Could be [k] [^p]
    823   1.1       cgd  */
    824   1.1       cgd protected el_action_t
    825   1.1       cgd /*ARGSUSED*/
    826   1.9     lukem ed_prev_line(EditLine *el, int c)
    827   1.9     lukem {
    828   1.9     lukem 	char *ptr;
    829   1.9     lukem 	int nchars = c_hpos(el);
    830   1.8    simonb 
    831   1.9     lukem 	/*
    832   1.9     lukem          * Move to the line requested
    833   1.9     lukem          */
    834   1.9     lukem 	if (*(ptr = el->el_line.cursor) == '\n')
    835   1.9     lukem 		ptr--;
    836   1.9     lukem 
    837   1.9     lukem 	for (; ptr >= el->el_line.buffer; ptr--)
    838   1.9     lukem 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    839   1.9     lukem 			break;
    840   1.9     lukem 
    841   1.9     lukem 	if (el->el_state.argument > 0)
    842   1.9     lukem 		return (CC_ERROR);
    843   1.9     lukem 
    844   1.9     lukem 	/*
    845   1.9     lukem          * Move to the beginning of the line
    846   1.9     lukem          */
    847   1.9     lukem 	for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
    848   1.9     lukem 		continue;
    849   1.9     lukem 
    850   1.9     lukem 	/*
    851   1.9     lukem          * Move to the character requested
    852   1.9     lukem          */
    853   1.9     lukem 	for (ptr++;
    854   1.9     lukem 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    855   1.9     lukem 	    ptr++)
    856   1.9     lukem 		continue;
    857   1.9     lukem 
    858   1.9     lukem 	el->el_line.cursor = ptr;
    859   1.9     lukem 	return (CC_CURSOR);
    860   1.1       cgd }
    861   1.1       cgd 
    862   1.1       cgd 
    863   1.1       cgd /* ed_next_line():
    864   1.1       cgd  *	Move down one line
    865   1.1       cgd  *	Could be [j] [^n]
    866   1.1       cgd  */
    867   1.1       cgd protected el_action_t
    868   1.1       cgd /*ARGSUSED*/
    869   1.9     lukem ed_next_line(EditLine *el, int c)
    870   1.9     lukem {
    871   1.9     lukem 	char *ptr;
    872   1.9     lukem 	int nchars = c_hpos(el);
    873   1.8    simonb 
    874   1.9     lukem 	/*
    875   1.9     lukem          * Move to the line requested
    876   1.9     lukem          */
    877   1.9     lukem 	for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
    878   1.9     lukem 		if (*ptr == '\n' && --el->el_state.argument <= 0)
    879   1.9     lukem 			break;
    880   1.9     lukem 
    881   1.9     lukem 	if (el->el_state.argument > 0)
    882   1.9     lukem 		return (CC_ERROR);
    883   1.9     lukem 
    884   1.9     lukem 	/*
    885   1.9     lukem          * Move to the character requested
    886   1.9     lukem          */
    887   1.9     lukem 	for (ptr++;
    888   1.9     lukem 	    nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
    889   1.9     lukem 	    ptr++)
    890   1.9     lukem 		continue;
    891   1.9     lukem 
    892   1.9     lukem 	el->el_line.cursor = ptr;
    893   1.9     lukem 	return (CC_CURSOR);
    894   1.1       cgd }
    895   1.1       cgd 
    896   1.1       cgd 
    897   1.8    simonb /* ed_command():
    898   1.1       cgd  *	Editline extended command
    899   1.1       cgd  *	[M-X] [:]
    900   1.1       cgd  */
    901   1.1       cgd protected el_action_t
    902   1.1       cgd /*ARGSUSED*/
    903   1.9     lukem ed_command(EditLine *el, int c)
    904   1.9     lukem {
    905   1.9     lukem 	char tmpbuf[EL_BUFSIZ];
    906   1.9     lukem 	int tmplen;
    907   1.9     lukem 
    908  1.14  christos 	tmplen = c_gets(el, tmpbuf, "\n: ");
    909  1.14  christos 	term__putc('\n');
    910   1.9     lukem 
    911  1.14  christos 	if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
    912  1.14  christos 		term_beep(el);
    913   1.9     lukem 
    914  1.14  christos 	el->el_map.current = el->el_map.key;
    915  1.14  christos 	re_clear_display(el);
    916  1.14  christos 	return CC_REFRESH;
    917   1.1       cgd }
    918