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