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