Home | History | Annotate | Line # | Download | only in libedit
chared.c revision 1.9
      1  1.9     lukem /*	$NetBSD: chared.c,v 1.9 2000/09/04 22:06:28 lukem 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.3  christos #include <sys/cdefs.h>
     40  1.1       cgd #if !defined(lint) && !defined(SCCSID)
     41  1.2     lukem #if 0
     42  1.1       cgd static char sccsid[] = "@(#)chared.c	8.1 (Berkeley) 6/4/93";
     43  1.2     lukem #else
     44  1.9     lukem __RCSID("$NetBSD: chared.c,v 1.9 2000/09/04 22:06:28 lukem Exp $");
     45  1.2     lukem #endif
     46  1.1       cgd #endif /* not lint && not SCCSID */
     47  1.1       cgd 
     48  1.7    simonb /*
     49  1.1       cgd  * chared.c: Character editor utilities
     50  1.1       cgd  */
     51  1.1       cgd #include "sys.h"
     52  1.1       cgd 
     53  1.1       cgd #include <stdlib.h>
     54  1.1       cgd #include "el.h"
     55  1.1       cgd 
     56  1.1       cgd /* cv_undo():
     57  1.1       cgd  *	Handle state for the vi undo command
     58  1.1       cgd  */
     59  1.1       cgd protected void
     60  1.9     lukem cv_undo(EditLine *el,int action, size_t size, char *ptr)
     61  1.9     lukem {
     62  1.9     lukem 	c_undo_t *vu = &el->el_chared.c_undo;
     63  1.9     lukem 	vu->action = action;
     64  1.9     lukem 	vu->ptr    = ptr;
     65  1.9     lukem 	vu->isize  = size;
     66  1.9     lukem 	(void) memcpy(vu->buf, vu->ptr, size);
     67  1.1       cgd #ifdef DEBUG_UNDO
     68  1.9     lukem 	(void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
     69  1.9     lukem 	       vu->ptr, vu->isize, vu->dsize);
     70  1.1       cgd #endif
     71  1.1       cgd }
     72  1.1       cgd 
     73  1.1       cgd 
     74  1.7    simonb /* c_insert():
     75  1.1       cgd  *	Insert num characters
     76  1.1       cgd  */
     77  1.1       cgd protected void
     78  1.9     lukem c_insert(EditLine *el, int num)
     79  1.9     lukem {
     80  1.9     lukem 	char *cp;
     81  1.9     lukem 
     82  1.9     lukem 	if (el->el_line.lastchar + num >= el->el_line.limit)
     83  1.9     lukem 		return;			/* can't go past end of buffer */
     84  1.9     lukem 
     85  1.9     lukem 	if (el->el_line.cursor < el->el_line.lastchar) {
     86  1.9     lukem 		/* if I must move chars */
     87  1.9     lukem 		for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
     88  1.9     lukem 			cp[num] = *cp;
     89  1.9     lukem 	}
     90  1.9     lukem 	el->el_line.lastchar += num;
     91  1.9     lukem }
     92  1.1       cgd 
     93  1.1       cgd 
     94  1.1       cgd /* c_delafter():
     95  1.1       cgd  *	Delete num characters after the cursor
     96  1.1       cgd  */
     97  1.1       cgd protected void
     98  1.9     lukem c_delafter(EditLine *el, int num)
     99  1.1       cgd {
    100  1.1       cgd 
    101  1.9     lukem 	if (el->el_line.cursor + num > el->el_line.lastchar)
    102  1.9     lukem 		num = el->el_line.lastchar - el->el_line.cursor;
    103  1.1       cgd 
    104  1.9     lukem 	if (num > 0) {
    105  1.9     lukem 		char *cp;
    106  1.1       cgd 
    107  1.9     lukem 		if (el->el_map.current != el->el_map.emacs)
    108  1.9     lukem 			cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
    109  1.1       cgd 
    110  1.9     lukem 		for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
    111  1.9     lukem 			*cp = cp[num];
    112  1.1       cgd 
    113  1.9     lukem 		el->el_line.lastchar -= num;
    114  1.9     lukem 	}
    115  1.1       cgd }
    116  1.1       cgd 
    117  1.1       cgd 
    118  1.1       cgd /* c_delbefore():
    119  1.1       cgd  *	Delete num characters before the cursor
    120  1.1       cgd  */
    121  1.1       cgd protected void
    122  1.9     lukem c_delbefore(EditLine *el, int num)
    123  1.1       cgd {
    124  1.1       cgd 
    125  1.9     lukem 	if (el->el_line.cursor - num < el->el_line.buffer)
    126  1.9     lukem 		num = el->el_line.cursor - el->el_line.buffer;
    127  1.1       cgd 
    128  1.9     lukem 	if (num > 0) {
    129  1.9     lukem 		char *cp;
    130  1.1       cgd 
    131  1.9     lukem 		if (el->el_map.current != el->el_map.emacs)
    132  1.9     lukem 			cv_undo(el, INSERT, (size_t)num,
    133  1.9     lukem 			    el->el_line.cursor - num);
    134  1.1       cgd 
    135  1.9     lukem 		for (cp = el->el_line.cursor - num;
    136  1.9     lukem 		    cp <= el->el_line.lastchar;
    137  1.9     lukem 		    cp++)
    138  1.9     lukem 			*cp = cp[num];
    139  1.1       cgd 
    140  1.9     lukem 		el->el_line.lastchar -= num;
    141  1.9     lukem 	}
    142  1.1       cgd }
    143  1.1       cgd 
    144  1.1       cgd 
    145  1.1       cgd /* ce__isword():
    146  1.1       cgd  *	Return if p is part of a word according to emacs
    147  1.1       cgd  */
    148  1.1       cgd protected int
    149  1.9     lukem ce__isword(int p)
    150  1.1       cgd {
    151  1.9     lukem 	return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
    152  1.1       cgd }
    153  1.1       cgd 
    154  1.1       cgd 
    155  1.1       cgd /* cv__isword():
    156  1.1       cgd  *	Return if p is part of a word according to vi
    157  1.1       cgd  */
    158  1.1       cgd protected int
    159  1.9     lukem cv__isword(int p)
    160  1.1       cgd {
    161  1.9     lukem 	return (!isspace(p));
    162  1.1       cgd }
    163  1.1       cgd 
    164  1.1       cgd 
    165  1.1       cgd /* c__prev_word():
    166  1.1       cgd  *	Find the previous word
    167  1.1       cgd  */
    168  1.1       cgd protected char *
    169  1.9     lukem c__prev_word(char *p, char *low, int n, int (*wtest)(int))
    170  1.9     lukem {
    171  1.9     lukem 	p--;
    172  1.9     lukem 
    173  1.9     lukem 	while (n--) {
    174  1.9     lukem 		while ((p >= low) && !(*wtest)((unsigned char) *p))
    175  1.9     lukem 			p--;
    176  1.9     lukem 		while ((p >= low) && (*wtest)((unsigned char) *p))
    177  1.9     lukem 			p--;
    178  1.9     lukem 	}
    179  1.9     lukem 
    180  1.9     lukem 	/* cp now points to one character before the word */
    181  1.9     lukem 	p++;
    182  1.9     lukem 	if (p < low)
    183  1.9     lukem 		p = low;
    184  1.9     lukem 	/* cp now points where we want it */
    185  1.9     lukem 	return (p);
    186  1.1       cgd }
    187  1.1       cgd 
    188  1.1       cgd 
    189  1.1       cgd /* c__next_word():
    190  1.1       cgd  *	Find the next word
    191  1.1       cgd  */
    192  1.1       cgd protected char *
    193  1.9     lukem c__next_word(char *p, char *high, int n, int (*wtest)(int))
    194  1.9     lukem {
    195  1.9     lukem 	while (n--) {
    196  1.9     lukem 		while ((p < high) && !(*wtest)((unsigned char) *p))
    197  1.9     lukem 			p++;
    198  1.9     lukem 		while ((p < high) && (*wtest)((unsigned char) *p))
    199  1.9     lukem 			p++;
    200  1.9     lukem 	}
    201  1.9     lukem 	if (p > high)
    202  1.9     lukem 		p = high;
    203  1.9     lukem 	/* p now points where we want it */
    204  1.9     lukem 	return (p);
    205  1.1       cgd }
    206  1.1       cgd 
    207  1.1       cgd /* cv_next_word():
    208  1.1       cgd  *	Find the next word vi style
    209  1.1       cgd  */
    210  1.1       cgd protected char *
    211  1.9     lukem cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
    212  1.9     lukem {
    213  1.9     lukem 	int test;
    214  1.9     lukem 
    215  1.9     lukem 	while (n--) {
    216  1.9     lukem 		test = (*wtest)((unsigned char) *p);
    217  1.9     lukem 		while ((p < high) && (*wtest)((unsigned char) *p) == test)
    218  1.9     lukem 			p++;
    219  1.9     lukem 		/*
    220  1.9     lukem 		 * vi historically deletes with cw only the word preserving the
    221  1.9     lukem 		 * trailing whitespace! This is not what 'w' does..
    222  1.9     lukem 		 */
    223  1.9     lukem 		if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
    224  1.9     lukem 			while ((p < high) && isspace((unsigned char) *p))
    225  1.9     lukem 				p++;
    226  1.9     lukem 	}
    227  1.1       cgd 
    228  1.9     lukem 	/* p now points where we want it */
    229  1.9     lukem 	if (p > high)
    230  1.9     lukem 		return (high);
    231  1.9     lukem 	else
    232  1.9     lukem 		return (p);
    233  1.1       cgd }
    234  1.1       cgd 
    235  1.1       cgd 
    236  1.1       cgd /* cv_prev_word():
    237  1.1       cgd  *	Find the previous word vi style
    238  1.1       cgd  */
    239  1.1       cgd protected char *
    240  1.9     lukem cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
    241  1.1       cgd {
    242  1.9     lukem 	int test;
    243  1.1       cgd 
    244  1.9     lukem 	while (n--) {
    245  1.1       cgd 		p--;
    246  1.9     lukem 		/*
    247  1.9     lukem 		 * vi historically deletes with cb only the word preserving the
    248  1.9     lukem 		 * leading whitespace! This is not what 'b' does..
    249  1.9     lukem 		 */
    250  1.9     lukem 		if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
    251  1.9     lukem 			while ((p > low) && isspace((unsigned char) *p))
    252  1.9     lukem 				p--;
    253  1.9     lukem 		test = (*wtest)((unsigned char) *p);
    254  1.9     lukem 		while ((p >= low) && (*wtest)((unsigned char) *p) == test)
    255  1.9     lukem 			p--;
    256  1.1       cgd 		p++;
    257  1.9     lukem 		while (isspace((unsigned char) *p))
    258  1.9     lukem 			p++;
    259  1.9     lukem 	}
    260  1.1       cgd 
    261  1.9     lukem 	/* p now points where we want it */
    262  1.9     lukem 	if (p < low)
    263  1.9     lukem 		return (low);
    264  1.9     lukem 	else
    265  1.9     lukem 		return (p);
    266  1.1       cgd }
    267  1.1       cgd 
    268  1.1       cgd 
    269  1.1       cgd #ifdef notdef
    270  1.1       cgd /* c__number():
    271  1.1       cgd  *	Ignore character p points to, return number appearing after that.
    272  1.1       cgd  * 	A '$' by itself means a big number; "$-" is for negative; '^' means 1.
    273  1.1       cgd  * 	Return p pointing to last char used.
    274  1.1       cgd  */
    275  1.1       cgd protected char *
    276  1.9     lukem c__number(
    277  1.9     lukem     char *p,	/* character position */
    278  1.9     lukem     int *num,	/* Return value	*/
    279  1.9     lukem     int dval)	/* dval is the number to subtract from like $-3 */
    280  1.9     lukem {
    281  1.9     lukem 	int i;
    282  1.9     lukem 	int sign = 1;
    283  1.9     lukem 
    284  1.9     lukem 	if (*++p == '^') {
    285  1.9     lukem 		*num = 1;
    286  1.9     lukem 		return (p);
    287  1.9     lukem 	}
    288  1.9     lukem 	if (*p == '$') {
    289  1.9     lukem 		if (*++p != '-') {
    290  1.9     lukem 			*num = 0x7fffffff;	/* Handle $ */
    291  1.9     lukem 			return (--p);
    292  1.9     lukem 		}
    293  1.9     lukem 		sign = -1;			/* Handle $- */
    294  1.9     lukem 		++p;
    295  1.9     lukem 	}
    296  1.9     lukem 	for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
    297  1.9     lukem 		continue;
    298  1.9     lukem 	*num = (sign < 0 ? dval - i : i);
    299  1.9     lukem 	return (--p);
    300  1.1       cgd }
    301  1.1       cgd #endif
    302  1.1       cgd 
    303  1.1       cgd /* cv_delfini():
    304  1.1       cgd  *	Finish vi delete action
    305  1.1       cgd  */
    306  1.1       cgd protected void
    307  1.9     lukem cv_delfini(EditLine *el)
    308  1.1       cgd {
    309  1.9     lukem 	int size;
    310  1.9     lukem 	int oaction;
    311  1.1       cgd 
    312  1.9     lukem 	if (el->el_chared.c_vcmd.action & INSERT)
    313  1.9     lukem 		el->el_map.current = el->el_map.key;
    314  1.1       cgd 
    315  1.9     lukem 	oaction = el->el_chared.c_vcmd.action;
    316  1.9     lukem 	el->el_chared.c_vcmd.action = NOP;
    317  1.9     lukem 
    318  1.9     lukem 	if (el->el_chared.c_vcmd.pos == 0)
    319  1.9     lukem 		return;
    320  1.9     lukem 
    321  1.9     lukem 
    322  1.9     lukem 	if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
    323  1.9     lukem 		size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
    324  1.9     lukem 		c_delbefore(el, size);
    325  1.9     lukem 		el->el_line.cursor = el->el_chared.c_vcmd.pos;
    326  1.9     lukem 		re_refresh_cursor(el);
    327  1.9     lukem 	} else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
    328  1.9     lukem 		size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
    329  1.9     lukem 		c_delafter(el, size);
    330  1.9     lukem 	} else {
    331  1.9     lukem 		size = 1;
    332  1.9     lukem 		c_delafter(el, size);
    333  1.9     lukem 	}
    334  1.9     lukem 	switch (oaction) {
    335  1.9     lukem 	case DELETE|INSERT:
    336  1.9     lukem 		el->el_chared.c_undo.action = DELETE|INSERT;
    337  1.9     lukem 		break;
    338  1.9     lukem 	case DELETE:
    339  1.9     lukem 		el->el_chared.c_undo.action = INSERT;
    340  1.9     lukem 		break;
    341  1.9     lukem 	case NOP:
    342  1.9     lukem 	case INSERT:
    343  1.9     lukem 	default:
    344  1.9     lukem 		abort();
    345  1.9     lukem 		break;
    346  1.9     lukem 	}
    347  1.7    simonb 
    348  1.1       cgd 
    349  1.9     lukem 	el->el_chared.c_undo.ptr = el->el_line.cursor;
    350  1.9     lukem 	el->el_chared.c_undo.dsize = size;
    351  1.1       cgd }
    352  1.1       cgd 
    353  1.1       cgd 
    354  1.1       cgd #ifdef notdef
    355  1.1       cgd /* ce__endword():
    356  1.1       cgd  *	Go to the end of this word according to emacs
    357  1.1       cgd  */
    358  1.1       cgd protected char *
    359  1.9     lukem ce__endword(char *p, char *high, int n)
    360  1.9     lukem {
    361  1.9     lukem 	p++;
    362  1.1       cgd 
    363  1.9     lukem 	while (n--) {
    364  1.9     lukem 		while ((p < high) && isspace((unsigned char) *p))
    365  1.9     lukem 			p++;
    366  1.9     lukem 		while ((p < high) && !isspace((unsigned char) *p))
    367  1.9     lukem 			p++;
    368  1.9     lukem 	}
    369  1.9     lukem 
    370  1.9     lukem 	p--;
    371  1.9     lukem 	return (p);
    372  1.1       cgd }
    373  1.1       cgd #endif
    374  1.1       cgd 
    375  1.1       cgd 
    376  1.1       cgd /* cv__endword():
    377  1.1       cgd  *	Go to the end of this word according to vi
    378  1.1       cgd  */
    379  1.1       cgd protected char *
    380  1.9     lukem cv__endword(char *p, char *high, int n)
    381  1.9     lukem {
    382  1.9     lukem 	p++;
    383  1.1       cgd 
    384  1.9     lukem 	while (n--) {
    385  1.9     lukem 		while ((p < high) && isspace((unsigned char) *p))
    386  1.9     lukem 			p++;
    387  1.9     lukem 
    388  1.9     lukem 		if (isalnum((unsigned char) *p))
    389  1.9     lukem 			while ((p < high) && isalnum((unsigned char) *p))
    390  1.9     lukem 				p++;
    391  1.9     lukem 		else
    392  1.9     lukem 			while ((p < high) && !(isspace((unsigned char) *p) ||
    393  1.9     lukem 			    isalnum((unsigned char) *p)))
    394  1.9     lukem 				p++;
    395  1.9     lukem 	}
    396  1.9     lukem 	p--;
    397  1.9     lukem 	return (p);
    398  1.1       cgd }
    399  1.1       cgd 
    400  1.1       cgd /* ch_init():
    401  1.1       cgd  *	Initialize the character editor
    402  1.1       cgd  */
    403  1.1       cgd protected int
    404  1.9     lukem ch_init(EditLine *el)
    405  1.1       cgd {
    406  1.9     lukem 	el->el_line.buffer		= (char *)  el_malloc(EL_BUFSIZ);
    407  1.9     lukem 	(void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
    408  1.9     lukem 	el->el_line.cursor		= el->el_line.buffer;
    409  1.9     lukem 	el->el_line.lastchar		= el->el_line.buffer;
    410  1.9     lukem 	el->el_line.limit		= &el->el_line.buffer[EL_BUFSIZ - 2];
    411  1.9     lukem 
    412  1.9     lukem 	el->el_chared.c_undo.buf	= (char *)  el_malloc(EL_BUFSIZ);
    413  1.9     lukem 	(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
    414  1.9     lukem 	el->el_chared.c_undo.action	= NOP;
    415  1.9     lukem 	el->el_chared.c_undo.isize	= 0;
    416  1.9     lukem 	el->el_chared.c_undo.dsize	= 0;
    417  1.9     lukem 	el->el_chared.c_undo.ptr	= el->el_line.buffer;
    418  1.9     lukem 
    419  1.9     lukem 	el->el_chared.c_vcmd.action	= NOP;
    420  1.9     lukem 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
    421  1.9     lukem 	el->el_chared.c_vcmd.ins	= el->el_line.buffer;
    422  1.9     lukem 
    423  1.9     lukem 	el->el_chared.c_kill.buf	= (char *) el_malloc(EL_BUFSIZ);
    424  1.9     lukem 	(void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
    425  1.9     lukem 	el->el_chared.c_kill.mark	= el->el_line.buffer;
    426  1.9     lukem 	el->el_chared.c_kill.last	= el->el_chared.c_kill.buf;
    427  1.9     lukem 
    428  1.9     lukem 	el->el_map.current		= el->el_map.key;
    429  1.9     lukem 
    430  1.9     lukem 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
    431  1.9     lukem 	el->el_state.doingarg		= 0;
    432  1.9     lukem 	el->el_state.metanext		= 0;
    433  1.9     lukem 	el->el_state.argument		= 1;
    434  1.9     lukem 	el->el_state.lastcmd		= ED_UNASSIGNED;
    435  1.9     lukem 
    436  1.9     lukem 	el->el_chared.c_macro.nline	= NULL;
    437  1.9     lukem 	el->el_chared.c_macro.level	= -1;
    438  1.9     lukem 	el->el_chared.c_macro.macro	= (char **) el_malloc(EL_MAXMACRO *
    439  1.9     lukem 								sizeof(char *));
    440  1.9     lukem 	return (0);
    441  1.1       cgd }
    442  1.1       cgd 
    443  1.1       cgd /* ch_reset():
    444  1.1       cgd  *	Reset the character editor
    445  1.1       cgd  */
    446  1.1       cgd protected void
    447  1.9     lukem ch_reset(EditLine *el)
    448  1.1       cgd {
    449  1.9     lukem 	el->el_line.cursor		= el->el_line.buffer;
    450  1.9     lukem 	el->el_line.lastchar		= el->el_line.buffer;
    451  1.1       cgd 
    452  1.9     lukem 	el->el_chared.c_undo.action	= NOP;
    453  1.9     lukem 	el->el_chared.c_undo.isize	= 0;
    454  1.9     lukem 	el->el_chared.c_undo.dsize	= 0;
    455  1.9     lukem 	el->el_chared.c_undo.ptr	= el->el_line.buffer;
    456  1.1       cgd 
    457  1.9     lukem 	el->el_chared.c_vcmd.action	= NOP;
    458  1.9     lukem 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
    459  1.9     lukem 	el->el_chared.c_vcmd.ins	= el->el_line.buffer;
    460  1.1       cgd 
    461  1.9     lukem 	el->el_chared.c_kill.mark	= el->el_line.buffer;
    462  1.1       cgd 
    463  1.9     lukem 	el->el_map.current		= el->el_map.key;
    464  1.1       cgd 
    465  1.9     lukem 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
    466  1.9     lukem 	el->el_state.doingarg		= 0;
    467  1.9     lukem 	el->el_state.metanext		= 0;
    468  1.9     lukem 	el->el_state.argument		= 1;
    469  1.9     lukem 	el->el_state.lastcmd		= ED_UNASSIGNED;
    470  1.1       cgd 
    471  1.9     lukem 	el->el_chared.c_macro.level	= -1;
    472  1.1       cgd 
    473  1.9     lukem 	el->el_history.eventno		= 0;
    474  1.1       cgd }
    475  1.1       cgd 
    476  1.1       cgd 
    477  1.1       cgd /* ch_end():
    478  1.1       cgd  *	Free the data structures used by the editor
    479  1.1       cgd  */
    480  1.1       cgd protected void
    481  1.9     lukem ch_end(EditLine *el)
    482  1.1       cgd {
    483  1.9     lukem 	el_free((ptr_t) el->el_line.buffer);
    484  1.9     lukem 	el->el_line.buffer = NULL;
    485  1.9     lukem 	el->el_line.limit = NULL;
    486  1.9     lukem 	el_free((ptr_t) el->el_chared.c_undo.buf);
    487  1.9     lukem 	el->el_chared.c_undo.buf = NULL;
    488  1.9     lukem 	el_free((ptr_t) el->el_chared.c_kill.buf);
    489  1.9     lukem 	el->el_chared.c_kill.buf = NULL;
    490  1.9     lukem 	el_free((ptr_t) el->el_chared.c_macro.macro);
    491  1.9     lukem 	el->el_chared.c_macro.macro = NULL;
    492  1.9     lukem 	ch_reset(el);
    493  1.1       cgd }
    494  1.1       cgd 
    495  1.1       cgd 
    496  1.1       cgd /* el_insertstr():
    497  1.1       cgd  *	Insert string at cursorI
    498  1.1       cgd  */
    499  1.1       cgd public int
    500  1.9     lukem el_insertstr(EditLine *el, const char *s)
    501  1.9     lukem {
    502  1.9     lukem 	int len;
    503  1.9     lukem 
    504  1.9     lukem 	if ((len = strlen(s)) == 0)
    505  1.9     lukem 		return (-1);
    506  1.9     lukem 	if (el->el_line.lastchar + len >= el->el_line.limit)
    507  1.9     lukem 		return (-1);
    508  1.9     lukem 
    509  1.9     lukem 	c_insert(el, len);
    510  1.9     lukem 	while (*s)
    511  1.9     lukem 		*el->el_line.cursor++ = *s++;
    512  1.9     lukem 	return (0);
    513  1.1       cgd }
    514  1.1       cgd 
    515  1.1       cgd 
    516  1.1       cgd /* el_deletestr():
    517  1.1       cgd  *	Delete num characters before the cursor
    518  1.1       cgd  */
    519  1.1       cgd public void
    520  1.9     lukem el_deletestr(EditLine *el, int n)
    521  1.9     lukem {
    522  1.9     lukem 	if (n <= 0)
    523  1.9     lukem 		return;
    524  1.9     lukem 
    525  1.9     lukem 	if (el->el_line.cursor < &el->el_line.buffer[n])
    526  1.9     lukem 		return;
    527  1.9     lukem 
    528  1.9     lukem 	c_delbefore(el, n);		/* delete before dot */
    529  1.9     lukem 	el->el_line.cursor -= n;
    530  1.9     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    531  1.9     lukem 		el->el_line.cursor = el->el_line.buffer;
    532  1.1       cgd }
    533  1.1       cgd 
    534  1.1       cgd /* c_gets():
    535  1.1       cgd  *	Get a string
    536  1.1       cgd  */
    537  1.1       cgd protected int
    538  1.9     lukem c_gets(EditLine *el, char *buf)
    539  1.9     lukem {
    540  1.9     lukem 	char ch;
    541  1.9     lukem 	int len = 0;
    542  1.1       cgd 
    543  1.9     lukem 	for (ch = 0; ch == 0;) {
    544  1.9     lukem 		if (el_getc(el, &ch) != 1)
    545  1.9     lukem 			return (ed_end_of_file(el, 0));
    546  1.9     lukem 		switch (ch) {
    547  1.9     lukem 		case 0010:	/* Delete and backspace */
    548  1.9     lukem 		case 0177:
    549  1.9     lukem 			if (len > 1) {
    550  1.9     lukem 				*el->el_line.cursor-- = '\0';
    551  1.9     lukem 				el->el_line.lastchar = el->el_line.cursor;
    552  1.9     lukem 				buf[len--] = '\0';
    553  1.9     lukem 			} else {
    554  1.9     lukem 				el->el_line.buffer[0] = '\0';
    555  1.9     lukem 				el->el_line.lastchar = el->el_line.buffer;
    556  1.9     lukem 				el->el_line.cursor = el->el_line.buffer;
    557  1.9     lukem 				return (CC_REFRESH);
    558  1.9     lukem 			}
    559  1.9     lukem 			re_refresh(el);
    560  1.9     lukem 			ch = 0;
    561  1.9     lukem 			break;
    562  1.9     lukem 
    563  1.9     lukem 		case 0033:	/* ESC */
    564  1.9     lukem 		case '\r':	/* Newline */
    565  1.9     lukem 		case '\n':
    566  1.9     lukem 			break;
    567  1.9     lukem 
    568  1.9     lukem 		default:
    569  1.9     lukem 			if (len >= EL_BUFSIZ)
    570  1.9     lukem 				term_beep(el);
    571  1.9     lukem 			else {
    572  1.9     lukem 				buf[len++] = ch;
    573  1.9     lukem 				*el->el_line.cursor++ = ch;
    574  1.9     lukem 				el->el_line.lastchar = el->el_line.cursor;
    575  1.9     lukem 			}
    576  1.9     lukem 			re_refresh(el);
    577  1.9     lukem 			ch = 0;
    578  1.9     lukem 			break;
    579  1.9     lukem 		}
    580  1.9     lukem 	}
    581  1.9     lukem 	buf[len] = ch;
    582  1.9     lukem 	return (len);
    583  1.1       cgd }
    584  1.1       cgd 
    585  1.1       cgd 
    586  1.1       cgd /* c_hpos():
    587  1.1       cgd  *	Return the current horizontal position of the cursor
    588  1.1       cgd  */
    589  1.1       cgd protected int
    590  1.9     lukem c_hpos(EditLine *el)
    591  1.1       cgd {
    592  1.9     lukem 	char *ptr;
    593  1.1       cgd 
    594  1.9     lukem 	/*
    595  1.9     lukem 	 * Find how many characters till the beginning of this line.
    596  1.9     lukem 	 */
    597  1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    598  1.9     lukem 		return (0);
    599  1.9     lukem 	else {
    600  1.9     lukem 		for (ptr = el->el_line.cursor - 1;
    601  1.9     lukem 		     ptr >= el->el_line.buffer && *ptr != '\n';
    602  1.9     lukem 		     ptr--)
    603  1.9     lukem 			continue;
    604  1.9     lukem 		return (el->el_line.cursor - ptr - 1);
    605  1.9     lukem 	}
    606  1.1       cgd }
    607