Home | History | Annotate | Line # | Download | only in libedit
chared.c revision 1.19
      1  1.19       agc /*	$NetBSD: chared.c,v 1.19 2003/08/07 16:44:30 agc 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.19       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.15  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[] = "@(#)chared.c	8.1 (Berkeley) 6/4/93";
     39   1.2     lukem #else
     40  1.19       agc __RCSID("$NetBSD: chared.c,v 1.19 2003/08/07 16:44:30 agc Exp $");
     41   1.2     lukem #endif
     42   1.1       cgd #endif /* not lint && not SCCSID */
     43   1.1       cgd 
     44   1.7    simonb /*
     45   1.1       cgd  * chared.c: Character editor utilities
     46   1.1       cgd  */
     47   1.1       cgd #include <stdlib.h>
     48   1.1       cgd #include "el.h"
     49   1.1       cgd 
     50  1.12  jdolecek /* value to leave unused in line buffer */
     51  1.12  jdolecek #define	EL_LEAVE	2
     52  1.12  jdolecek 
     53   1.1       cgd /* cv_undo():
     54   1.1       cgd  *	Handle state for the vi undo command
     55   1.1       cgd  */
     56   1.1       cgd protected void
     57  1.17  christos cv_undo(EditLine *el)
     58   1.9     lukem {
     59   1.9     lukem 	c_undo_t *vu = &el->el_chared.c_undo;
     60  1.17  christos 	c_redo_t *r = &el->el_chared.c_redo;
     61  1.17  christos 	uint size;
     62  1.16  christos 
     63  1.17  christos 	/* Save entire line for undo */
     64  1.17  christos 	size = el->el_line.lastchar - el->el_line.buffer;
     65  1.17  christos 	vu->len = size;
     66  1.16  christos 	vu->cursor = el->el_line.cursor - el->el_line.buffer;
     67  1.17  christos 	memcpy(vu->buf, el->el_line.buffer, size);
     68  1.16  christos 
     69  1.17  christos 	/* save command info for redo */
     70  1.17  christos 	r->count = el->el_state.doingarg ? el->el_state.argument : 0;
     71  1.17  christos 	r->action = el->el_chared.c_vcmd.action;
     72  1.17  christos 	r->pos = r->buf;
     73  1.17  christos 	r->cmd = el->el_state.thiscmd;
     74  1.17  christos 	r->ch = el->el_state.thisch;
     75  1.17  christos }
     76  1.17  christos 
     77  1.17  christos /* cv_yank():
     78  1.17  christos  *	Save yank/delete data for paste
     79  1.17  christos  */
     80  1.17  christos protected void
     81  1.17  christos cv_yank(EditLine *el, const char *ptr, int size)
     82  1.17  christos {
     83  1.17  christos 	c_kill_t *k = &el->el_chared.c_kill;
     84  1.17  christos 
     85  1.17  christos 	memcpy(k->buf, ptr, size +0u);
     86  1.17  christos 	k->last = k->buf + size;
     87   1.1       cgd }
     88   1.1       cgd 
     89   1.1       cgd 
     90   1.7    simonb /* c_insert():
     91   1.1       cgd  *	Insert num characters
     92   1.1       cgd  */
     93   1.1       cgd protected void
     94   1.9     lukem c_insert(EditLine *el, int num)
     95   1.9     lukem {
     96   1.9     lukem 	char *cp;
     97   1.9     lukem 
     98  1.17  christos 	if (el->el_line.lastchar + num >= el->el_line.limit) {
     99  1.17  christos 		if (!ch_enlargebufs(el, num +0u))
    100  1.17  christos 			return;		/* can't go past end of buffer */
    101  1.17  christos 	}
    102   1.9     lukem 
    103   1.9     lukem 	if (el->el_line.cursor < el->el_line.lastchar) {
    104   1.9     lukem 		/* if I must move chars */
    105   1.9     lukem 		for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
    106   1.9     lukem 			cp[num] = *cp;
    107   1.9     lukem 	}
    108   1.9     lukem 	el->el_line.lastchar += num;
    109   1.9     lukem }
    110   1.1       cgd 
    111   1.1       cgd 
    112   1.1       cgd /* c_delafter():
    113   1.1       cgd  *	Delete num characters after the cursor
    114   1.1       cgd  */
    115   1.1       cgd protected void
    116   1.9     lukem c_delafter(EditLine *el, int num)
    117   1.1       cgd {
    118   1.1       cgd 
    119   1.9     lukem 	if (el->el_line.cursor + num > el->el_line.lastchar)
    120   1.9     lukem 		num = el->el_line.lastchar - el->el_line.cursor;
    121   1.1       cgd 
    122  1.17  christos 	if (el->el_map.current != el->el_map.emacs) {
    123  1.17  christos 		cv_undo(el);
    124  1.17  christos 		cv_yank(el, el->el_line.cursor, num);
    125  1.17  christos 	}
    126  1.16  christos 
    127   1.9     lukem 	if (num > 0) {
    128   1.9     lukem 		char *cp;
    129   1.1       cgd 
    130   1.9     lukem 		for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
    131   1.9     lukem 			*cp = cp[num];
    132   1.1       cgd 
    133   1.9     lukem 		el->el_line.lastchar -= num;
    134   1.9     lukem 	}
    135   1.1       cgd }
    136   1.1       cgd 
    137   1.1       cgd 
    138   1.1       cgd /* c_delbefore():
    139   1.1       cgd  *	Delete num characters before the cursor
    140   1.1       cgd  */
    141   1.1       cgd protected void
    142   1.9     lukem c_delbefore(EditLine *el, int num)
    143   1.1       cgd {
    144   1.1       cgd 
    145   1.9     lukem 	if (el->el_line.cursor - num < el->el_line.buffer)
    146   1.9     lukem 		num = el->el_line.cursor - el->el_line.buffer;
    147   1.1       cgd 
    148  1.17  christos 	if (el->el_map.current != el->el_map.emacs) {
    149  1.17  christos 		cv_undo(el);
    150  1.17  christos 		cv_yank(el, el->el_line.cursor - num, num);
    151  1.17  christos 	}
    152  1.16  christos 
    153   1.9     lukem 	if (num > 0) {
    154   1.9     lukem 		char *cp;
    155   1.1       cgd 
    156   1.9     lukem 		for (cp = el->el_line.cursor - num;
    157   1.9     lukem 		    cp <= el->el_line.lastchar;
    158   1.9     lukem 		    cp++)
    159   1.9     lukem 			*cp = cp[num];
    160   1.1       cgd 
    161   1.9     lukem 		el->el_line.lastchar -= num;
    162   1.9     lukem 	}
    163   1.1       cgd }
    164   1.1       cgd 
    165   1.1       cgd 
    166   1.1       cgd /* ce__isword():
    167   1.1       cgd  *	Return if p is part of a word according to emacs
    168   1.1       cgd  */
    169   1.1       cgd protected int
    170   1.9     lukem ce__isword(int p)
    171   1.1       cgd {
    172  1.16  christos 	return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
    173   1.1       cgd }
    174   1.1       cgd 
    175   1.1       cgd 
    176   1.1       cgd /* cv__isword():
    177   1.1       cgd  *	Return if p is part of a word according to vi
    178   1.1       cgd  */
    179   1.1       cgd protected int
    180   1.9     lukem cv__isword(int p)
    181   1.1       cgd {
    182  1.17  christos 	if (isalnum(p) || p == '_')
    183  1.17  christos 		return 1;
    184  1.17  christos 	if (isgraph(p))
    185  1.17  christos 		return 2;
    186  1.17  christos 	return 0;
    187  1.16  christos }
    188  1.16  christos 
    189  1.16  christos 
    190  1.16  christos /* cv__isWord():
    191  1.16  christos  *	Return if p is part of a big word according to vi
    192  1.16  christos  */
    193  1.16  christos protected int
    194  1.16  christos cv__isWord(int p)
    195  1.16  christos {
    196   1.9     lukem 	return (!isspace(p));
    197   1.1       cgd }
    198   1.1       cgd 
    199   1.1       cgd 
    200   1.1       cgd /* c__prev_word():
    201   1.1       cgd  *	Find the previous word
    202   1.1       cgd  */
    203   1.1       cgd protected char *
    204   1.9     lukem c__prev_word(char *p, char *low, int n, int (*wtest)(int))
    205   1.9     lukem {
    206   1.9     lukem 	p--;
    207   1.9     lukem 
    208   1.9     lukem 	while (n--) {
    209   1.9     lukem 		while ((p >= low) && !(*wtest)((unsigned char) *p))
    210   1.9     lukem 			p--;
    211   1.9     lukem 		while ((p >= low) && (*wtest)((unsigned char) *p))
    212   1.9     lukem 			p--;
    213   1.9     lukem 	}
    214   1.9     lukem 
    215   1.9     lukem 	/* cp now points to one character before the word */
    216   1.9     lukem 	p++;
    217   1.9     lukem 	if (p < low)
    218   1.9     lukem 		p = low;
    219   1.9     lukem 	/* cp now points where we want it */
    220   1.9     lukem 	return (p);
    221   1.1       cgd }
    222   1.1       cgd 
    223   1.1       cgd 
    224   1.1       cgd /* c__next_word():
    225   1.1       cgd  *	Find the next word
    226   1.1       cgd  */
    227   1.1       cgd protected char *
    228   1.9     lukem c__next_word(char *p, char *high, int n, int (*wtest)(int))
    229   1.9     lukem {
    230   1.9     lukem 	while (n--) {
    231   1.9     lukem 		while ((p < high) && !(*wtest)((unsigned char) *p))
    232   1.9     lukem 			p++;
    233   1.9     lukem 		while ((p < high) && (*wtest)((unsigned char) *p))
    234   1.9     lukem 			p++;
    235   1.9     lukem 	}
    236   1.9     lukem 	if (p > high)
    237   1.9     lukem 		p = high;
    238   1.9     lukem 	/* p now points where we want it */
    239   1.9     lukem 	return (p);
    240   1.1       cgd }
    241   1.1       cgd 
    242   1.1       cgd /* cv_next_word():
    243   1.1       cgd  *	Find the next word vi style
    244   1.1       cgd  */
    245   1.1       cgd protected char *
    246   1.9     lukem cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
    247   1.9     lukem {
    248   1.9     lukem 	int test;
    249   1.9     lukem 
    250   1.9     lukem 	while (n--) {
    251   1.9     lukem 		test = (*wtest)((unsigned char) *p);
    252   1.9     lukem 		while ((p < high) && (*wtest)((unsigned char) *p) == test)
    253   1.9     lukem 			p++;
    254   1.9     lukem 		/*
    255   1.9     lukem 		 * vi historically deletes with cw only the word preserving the
    256   1.9     lukem 		 * trailing whitespace! This is not what 'w' does..
    257   1.9     lukem 		 */
    258  1.16  christos 		if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
    259   1.9     lukem 			while ((p < high) && isspace((unsigned char) *p))
    260   1.9     lukem 				p++;
    261   1.9     lukem 	}
    262   1.1       cgd 
    263   1.9     lukem 	/* p now points where we want it */
    264   1.9     lukem 	if (p > high)
    265   1.9     lukem 		return (high);
    266   1.9     lukem 	else
    267   1.9     lukem 		return (p);
    268   1.1       cgd }
    269   1.1       cgd 
    270   1.1       cgd 
    271   1.1       cgd /* cv_prev_word():
    272   1.1       cgd  *	Find the previous word vi style
    273   1.1       cgd  */
    274   1.1       cgd protected char *
    275  1.16  christos cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
    276   1.1       cgd {
    277   1.9     lukem 	int test;
    278   1.1       cgd 
    279  1.16  christos 	p--;
    280   1.9     lukem 	while (n--) {
    281  1.16  christos 		while ((p > low) && isspace((unsigned char) *p))
    282  1.16  christos 			p--;
    283   1.9     lukem 		test = (*wtest)((unsigned char) *p);
    284   1.9     lukem 		while ((p >= low) && (*wtest)((unsigned char) *p) == test)
    285   1.9     lukem 			p--;
    286   1.9     lukem 	}
    287  1.16  christos 	p++;
    288   1.1       cgd 
    289   1.9     lukem 	/* p now points where we want it */
    290   1.9     lukem 	if (p < low)
    291   1.9     lukem 		return (low);
    292   1.9     lukem 	else
    293   1.9     lukem 		return (p);
    294   1.1       cgd }
    295   1.1       cgd 
    296   1.1       cgd 
    297   1.1       cgd #ifdef notdef
    298   1.1       cgd /* c__number():
    299   1.1       cgd  *	Ignore character p points to, return number appearing after that.
    300   1.1       cgd  * 	A '$' by itself means a big number; "$-" is for negative; '^' means 1.
    301   1.1       cgd  * 	Return p pointing to last char used.
    302   1.1       cgd  */
    303   1.1       cgd protected char *
    304   1.9     lukem c__number(
    305   1.9     lukem     char *p,	/* character position */
    306   1.9     lukem     int *num,	/* Return value	*/
    307   1.9     lukem     int dval)	/* dval is the number to subtract from like $-3 */
    308   1.9     lukem {
    309   1.9     lukem 	int i;
    310   1.9     lukem 	int sign = 1;
    311   1.9     lukem 
    312   1.9     lukem 	if (*++p == '^') {
    313   1.9     lukem 		*num = 1;
    314   1.9     lukem 		return (p);
    315   1.9     lukem 	}
    316   1.9     lukem 	if (*p == '$') {
    317   1.9     lukem 		if (*++p != '-') {
    318   1.9     lukem 			*num = 0x7fffffff;	/* Handle $ */
    319   1.9     lukem 			return (--p);
    320   1.9     lukem 		}
    321   1.9     lukem 		sign = -1;			/* Handle $- */
    322   1.9     lukem 		++p;
    323   1.9     lukem 	}
    324   1.9     lukem 	for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
    325   1.9     lukem 		continue;
    326   1.9     lukem 	*num = (sign < 0 ? dval - i : i);
    327   1.9     lukem 	return (--p);
    328   1.1       cgd }
    329   1.1       cgd #endif
    330   1.1       cgd 
    331   1.1       cgd /* cv_delfini():
    332   1.1       cgd  *	Finish vi delete action
    333   1.1       cgd  */
    334   1.1       cgd protected void
    335   1.9     lukem cv_delfini(EditLine *el)
    336   1.1       cgd {
    337   1.9     lukem 	int size;
    338  1.17  christos 	int action = el->el_chared.c_vcmd.action;
    339   1.1       cgd 
    340  1.17  christos 	if (action & INSERT)
    341   1.9     lukem 		el->el_map.current = el->el_map.key;
    342   1.1       cgd 
    343   1.9     lukem 	if (el->el_chared.c_vcmd.pos == 0)
    344  1.17  christos 		/* sanity */
    345   1.9     lukem 		return;
    346   1.9     lukem 
    347  1.17  christos 	size = el->el_line.cursor - el->el_chared.c_vcmd.pos;
    348  1.17  christos 	if (size == 0)
    349  1.17  christos 		size = 1;
    350  1.16  christos 	el->el_line.cursor = el->el_chared.c_vcmd.pos;
    351  1.17  christos 	if (action & YANK) {
    352  1.17  christos 		if (size > 0)
    353  1.17  christos 			cv_yank(el, el->el_line.cursor, size);
    354  1.17  christos 		else
    355  1.17  christos 			cv_yank(el, el->el_line.cursor + size, -size);
    356   1.9     lukem 	} else {
    357  1.17  christos 		if (size > 0) {
    358  1.17  christos 			c_delafter(el, size);
    359  1.17  christos 			re_refresh_cursor(el);
    360  1.17  christos 		} else  {
    361  1.17  christos 			c_delbefore(el, -size);
    362  1.17  christos 			el->el_line.cursor += size;
    363  1.17  christos 		}
    364   1.9     lukem 	}
    365  1.17  christos 	el->el_chared.c_vcmd.action = NOP;
    366   1.1       cgd }
    367   1.1       cgd 
    368   1.1       cgd 
    369   1.1       cgd #ifdef notdef
    370   1.1       cgd /* ce__endword():
    371   1.1       cgd  *	Go to the end of this word according to emacs
    372   1.1       cgd  */
    373   1.1       cgd protected char *
    374   1.9     lukem ce__endword(char *p, char *high, int n)
    375   1.9     lukem {
    376   1.9     lukem 	p++;
    377   1.1       cgd 
    378   1.9     lukem 	while (n--) {
    379   1.9     lukem 		while ((p < high) && isspace((unsigned char) *p))
    380   1.9     lukem 			p++;
    381   1.9     lukem 		while ((p < high) && !isspace((unsigned char) *p))
    382   1.9     lukem 			p++;
    383   1.9     lukem 	}
    384   1.9     lukem 
    385   1.9     lukem 	p--;
    386   1.9     lukem 	return (p);
    387   1.1       cgd }
    388   1.1       cgd #endif
    389   1.1       cgd 
    390   1.1       cgd 
    391   1.1       cgd /* cv__endword():
    392   1.1       cgd  *	Go to the end of this word according to vi
    393   1.1       cgd  */
    394   1.1       cgd protected char *
    395  1.16  christos cv__endword(char *p, char *high, int n, int (*wtest)(int))
    396   1.9     lukem {
    397  1.16  christos 	int test;
    398  1.16  christos 
    399   1.9     lukem 	p++;
    400   1.1       cgd 
    401   1.9     lukem 	while (n--) {
    402   1.9     lukem 		while ((p < high) && isspace((unsigned char) *p))
    403   1.9     lukem 			p++;
    404   1.9     lukem 
    405  1.16  christos 		test = (*wtest)((unsigned char) *p);
    406  1.16  christos 		while ((p < high) && (*wtest)((unsigned char) *p) == test)
    407  1.16  christos 			p++;
    408   1.9     lukem 	}
    409   1.9     lukem 	p--;
    410   1.9     lukem 	return (p);
    411   1.1       cgd }
    412   1.1       cgd 
    413   1.1       cgd /* ch_init():
    414   1.1       cgd  *	Initialize the character editor
    415   1.1       cgd  */
    416   1.1       cgd protected int
    417   1.9     lukem ch_init(EditLine *el)
    418   1.1       cgd {
    419  1.11  christos 	el->el_line.buffer		= (char *) el_malloc(EL_BUFSIZ);
    420  1.11  christos 	if (el->el_line.buffer == NULL)
    421  1.11  christos 		return (-1);
    422  1.11  christos 
    423   1.9     lukem 	(void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
    424   1.9     lukem 	el->el_line.cursor		= el->el_line.buffer;
    425   1.9     lukem 	el->el_line.lastchar		= el->el_line.buffer;
    426  1.16  christos 	el->el_line.limit		= &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
    427   1.9     lukem 
    428  1.11  christos 	el->el_chared.c_undo.buf	= (char *) el_malloc(EL_BUFSIZ);
    429  1.11  christos 	if (el->el_chared.c_undo.buf == NULL)
    430  1.11  christos 		return (-1);
    431   1.9     lukem 	(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
    432  1.16  christos 	el->el_chared.c_undo.len	= -1;
    433  1.16  christos 	el->el_chared.c_undo.cursor	= 0;
    434  1.17  christos 	el->el_chared.c_redo.buf	= (char *) el_malloc(EL_BUFSIZ);
    435  1.17  christos 	if (el->el_chared.c_redo.buf == NULL)
    436  1.16  christos 		return (-1);
    437  1.17  christos 	el->el_chared.c_redo.pos	= el->el_chared.c_redo.buf;
    438  1.17  christos 	el->el_chared.c_redo.lim	= el->el_chared.c_redo.buf + EL_BUFSIZ;
    439  1.17  christos 	el->el_chared.c_redo.cmd	= ED_UNASSIGNED;
    440   1.9     lukem 
    441   1.9     lukem 	el->el_chared.c_vcmd.action	= NOP;
    442   1.9     lukem 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
    443   1.9     lukem 
    444   1.9     lukem 	el->el_chared.c_kill.buf	= (char *) el_malloc(EL_BUFSIZ);
    445  1.11  christos 	if (el->el_chared.c_kill.buf == NULL)
    446  1.11  christos 		return (-1);
    447   1.9     lukem 	(void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
    448   1.9     lukem 	el->el_chared.c_kill.mark	= el->el_line.buffer;
    449   1.9     lukem 	el->el_chared.c_kill.last	= el->el_chared.c_kill.buf;
    450   1.9     lukem 
    451   1.9     lukem 	el->el_map.current		= el->el_map.key;
    452   1.9     lukem 
    453   1.9     lukem 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
    454   1.9     lukem 	el->el_state.doingarg		= 0;
    455   1.9     lukem 	el->el_state.metanext		= 0;
    456   1.9     lukem 	el->el_state.argument		= 1;
    457   1.9     lukem 	el->el_state.lastcmd		= ED_UNASSIGNED;
    458   1.9     lukem 
    459   1.9     lukem 	el->el_chared.c_macro.nline	= NULL;
    460   1.9     lukem 	el->el_chared.c_macro.level	= -1;
    461   1.9     lukem 	el->el_chared.c_macro.macro	= (char **) el_malloc(EL_MAXMACRO *
    462  1.11  christos 	    sizeof(char *));
    463  1.11  christos 	if (el->el_chared.c_macro.macro == NULL)
    464  1.11  christos 		return (-1);
    465   1.9     lukem 	return (0);
    466   1.1       cgd }
    467   1.1       cgd 
    468   1.1       cgd /* ch_reset():
    469   1.1       cgd  *	Reset the character editor
    470   1.1       cgd  */
    471   1.1       cgd protected void
    472   1.9     lukem ch_reset(EditLine *el)
    473   1.1       cgd {
    474   1.9     lukem 	el->el_line.cursor		= el->el_line.buffer;
    475   1.9     lukem 	el->el_line.lastchar		= el->el_line.buffer;
    476   1.1       cgd 
    477  1.16  christos 	el->el_chared.c_undo.len	= -1;
    478  1.16  christos 	el->el_chared.c_undo.cursor	= 0;
    479   1.1       cgd 
    480   1.9     lukem 	el->el_chared.c_vcmd.action	= NOP;
    481   1.9     lukem 	el->el_chared.c_vcmd.pos	= el->el_line.buffer;
    482   1.1       cgd 
    483   1.9     lukem 	el->el_chared.c_kill.mark	= el->el_line.buffer;
    484   1.1       cgd 
    485   1.9     lukem 	el->el_map.current		= el->el_map.key;
    486   1.1       cgd 
    487   1.9     lukem 	el->el_state.inputmode		= MODE_INSERT; /* XXX: save a default */
    488   1.9     lukem 	el->el_state.doingarg		= 0;
    489   1.9     lukem 	el->el_state.metanext		= 0;
    490   1.9     lukem 	el->el_state.argument		= 1;
    491   1.9     lukem 	el->el_state.lastcmd		= ED_UNASSIGNED;
    492   1.1       cgd 
    493   1.9     lukem 	el->el_chared.c_macro.level	= -1;
    494   1.1       cgd 
    495   1.9     lukem 	el->el_history.eventno		= 0;
    496   1.1       cgd }
    497   1.1       cgd 
    498  1.12  jdolecek /* ch_enlargebufs():
    499  1.12  jdolecek  *	Enlarge line buffer to be able to hold twice as much characters.
    500  1.12  jdolecek  *	Returns 1 if successful, 0 if not.
    501  1.12  jdolecek  */
    502  1.12  jdolecek protected int
    503  1.12  jdolecek ch_enlargebufs(el, addlen)
    504  1.13     lukem 	EditLine *el;
    505  1.13     lukem 	size_t addlen;
    506  1.12  jdolecek {
    507  1.13     lukem 	size_t sz, newsz;
    508  1.13     lukem 	char *newbuffer, *oldbuf, *oldkbuf;
    509  1.12  jdolecek 
    510  1.13     lukem 	sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
    511  1.13     lukem 	newsz = sz * 2;
    512  1.13     lukem 	/*
    513  1.13     lukem 	 * If newly required length is longer than current buffer, we need
    514  1.13     lukem 	 * to make the buffer big enough to hold both old and new stuff.
    515  1.13     lukem 	 */
    516  1.13     lukem 	if (addlen > sz) {
    517  1.13     lukem 		while(newsz - sz < addlen)
    518  1.13     lukem 			newsz *= 2;
    519  1.13     lukem 	}
    520  1.13     lukem 
    521  1.13     lukem 	/*
    522  1.13     lukem 	 * Reallocate line buffer.
    523  1.13     lukem 	 */
    524  1.13     lukem 	newbuffer = el_realloc(el->el_line.buffer, newsz);
    525  1.13     lukem 	if (!newbuffer)
    526  1.13     lukem 		return 0;
    527  1.13     lukem 
    528  1.13     lukem 	/* zero the newly added memory, leave old data in */
    529  1.13     lukem 	(void) memset(&newbuffer[sz], 0, newsz - sz);
    530  1.13     lukem 
    531  1.13     lukem 	oldbuf = el->el_line.buffer;
    532  1.13     lukem 
    533  1.13     lukem 	el->el_line.buffer = newbuffer;
    534  1.13     lukem 	el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
    535  1.13     lukem 	el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
    536  1.16  christos 	/* don't set new size until all buffers are enlarged */
    537  1.16  christos 	el->el_line.limit  = &newbuffer[sz - EL_LEAVE];
    538  1.13     lukem 
    539  1.13     lukem 	/*
    540  1.13     lukem 	 * Reallocate kill buffer.
    541  1.13     lukem 	 */
    542  1.13     lukem 	newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
    543  1.13     lukem 	if (!newbuffer)
    544  1.13     lukem 		return 0;
    545  1.12  jdolecek 
    546  1.13     lukem 	/* zero the newly added memory, leave old data in */
    547  1.13     lukem 	(void) memset(&newbuffer[sz], 0, newsz - sz);
    548  1.12  jdolecek 
    549  1.13     lukem 	oldkbuf = el->el_chared.c_kill.buf;
    550  1.12  jdolecek 
    551  1.13     lukem 	el->el_chared.c_kill.buf = newbuffer;
    552  1.13     lukem 	el->el_chared.c_kill.last = newbuffer +
    553  1.12  jdolecek 					(el->el_chared.c_kill.last - oldkbuf);
    554  1.13     lukem 	el->el_chared.c_kill.mark = el->el_line.buffer +
    555  1.12  jdolecek 					(el->el_chared.c_kill.mark - oldbuf);
    556  1.12  jdolecek 
    557  1.13     lukem 	/*
    558  1.13     lukem 	 * Reallocate undo buffer.
    559  1.13     lukem 	 */
    560  1.13     lukem 	newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
    561  1.13     lukem 	if (!newbuffer)
    562  1.13     lukem 		return 0;
    563  1.13     lukem 
    564  1.13     lukem 	/* zero the newly added memory, leave old data in */
    565  1.13     lukem 	(void) memset(&newbuffer[sz], 0, newsz - sz);
    566  1.16  christos 	el->el_chared.c_undo.buf = newbuffer;
    567  1.13     lukem 
    568  1.17  christos 	newbuffer = el_realloc(el->el_chared.c_redo.buf, newsz);
    569  1.16  christos 	if (!newbuffer)
    570  1.16  christos 		return 0;
    571  1.17  christos 	el->el_chared.c_redo.pos = newbuffer +
    572  1.17  christos 			(el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
    573  1.17  christos 	el->el_chared.c_redo.lim = newbuffer +
    574  1.17  christos 			(el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
    575  1.17  christos 	el->el_chared.c_redo.buf = newbuffer;
    576  1.13     lukem 
    577  1.13     lukem 	if (!hist_enlargebuf(el, sz, newsz))
    578  1.13     lukem 		return 0;
    579  1.12  jdolecek 
    580  1.16  christos 	/* Safe to set enlarged buffer size */
    581  1.16  christos 	el->el_line.limit  = &newbuffer[newsz - EL_LEAVE];
    582  1.13     lukem 	return 1;
    583  1.12  jdolecek }
    584   1.1       cgd 
    585   1.1       cgd /* ch_end():
    586   1.1       cgd  *	Free the data structures used by the editor
    587   1.1       cgd  */
    588   1.1       cgd protected void
    589   1.9     lukem ch_end(EditLine *el)
    590   1.1       cgd {
    591   1.9     lukem 	el_free((ptr_t) el->el_line.buffer);
    592   1.9     lukem 	el->el_line.buffer = NULL;
    593   1.9     lukem 	el->el_line.limit = NULL;
    594   1.9     lukem 	el_free((ptr_t) el->el_chared.c_undo.buf);
    595   1.9     lukem 	el->el_chared.c_undo.buf = NULL;
    596  1.17  christos 	el_free((ptr_t) el->el_chared.c_redo.buf);
    597  1.17  christos 	el->el_chared.c_redo.buf = NULL;
    598  1.17  christos 	el->el_chared.c_redo.pos = NULL;
    599  1.17  christos 	el->el_chared.c_redo.lim = NULL;
    600  1.17  christos 	el->el_chared.c_redo.cmd = ED_UNASSIGNED;
    601   1.9     lukem 	el_free((ptr_t) el->el_chared.c_kill.buf);
    602   1.9     lukem 	el->el_chared.c_kill.buf = NULL;
    603   1.9     lukem 	el_free((ptr_t) el->el_chared.c_macro.macro);
    604   1.9     lukem 	el->el_chared.c_macro.macro = NULL;
    605   1.9     lukem 	ch_reset(el);
    606   1.1       cgd }
    607   1.1       cgd 
    608   1.1       cgd 
    609   1.1       cgd /* el_insertstr():
    610   1.1       cgd  *	Insert string at cursorI
    611   1.1       cgd  */
    612   1.1       cgd public int
    613   1.9     lukem el_insertstr(EditLine *el, const char *s)
    614   1.9     lukem {
    615  1.14  christos 	size_t len;
    616   1.9     lukem 
    617   1.9     lukem 	if ((len = strlen(s)) == 0)
    618   1.9     lukem 		return (-1);
    619  1.12  jdolecek 	if (el->el_line.lastchar + len >= el->el_line.limit) {
    620  1.12  jdolecek 		if (!ch_enlargebufs(el, len))
    621  1.12  jdolecek 			return (-1);
    622  1.12  jdolecek 	}
    623   1.9     lukem 
    624  1.14  christos 	c_insert(el, (int)len);
    625   1.9     lukem 	while (*s)
    626   1.9     lukem 		*el->el_line.cursor++ = *s++;
    627   1.9     lukem 	return (0);
    628   1.1       cgd }
    629   1.1       cgd 
    630   1.1       cgd 
    631   1.1       cgd /* el_deletestr():
    632   1.1       cgd  *	Delete num characters before the cursor
    633   1.1       cgd  */
    634   1.1       cgd public void
    635   1.9     lukem el_deletestr(EditLine *el, int n)
    636   1.9     lukem {
    637   1.9     lukem 	if (n <= 0)
    638   1.9     lukem 		return;
    639   1.9     lukem 
    640   1.9     lukem 	if (el->el_line.cursor < &el->el_line.buffer[n])
    641   1.9     lukem 		return;
    642   1.9     lukem 
    643   1.9     lukem 	c_delbefore(el, n);		/* delete before dot */
    644   1.9     lukem 	el->el_line.cursor -= n;
    645   1.9     lukem 	if (el->el_line.cursor < el->el_line.buffer)
    646   1.9     lukem 		el->el_line.cursor = el->el_line.buffer;
    647   1.1       cgd }
    648   1.1       cgd 
    649   1.1       cgd /* c_gets():
    650   1.1       cgd  *	Get a string
    651   1.1       cgd  */
    652   1.1       cgd protected int
    653  1.18  christos c_gets(EditLine *el, char *buf, const char *prompt)
    654   1.9     lukem {
    655   1.9     lukem 	char ch;
    656  1.18  christos 	int len;
    657  1.18  christos 	char *cp = el->el_line.buffer;
    658  1.18  christos 
    659  1.18  christos 	if (prompt) {
    660  1.18  christos 		len = strlen(prompt);
    661  1.18  christos 		memcpy(cp, prompt, len + 0u);
    662  1.18  christos 		cp += len;
    663  1.18  christos 	}
    664  1.18  christos 	len = 0;
    665  1.18  christos 
    666  1.18  christos 	for (;;) {
    667  1.18  christos 		el->el_line.cursor = cp;
    668  1.18  christos 		*cp = ' ';
    669  1.18  christos 		el->el_line.lastchar = cp + 1;
    670  1.18  christos 		re_refresh(el);
    671  1.18  christos 
    672  1.18  christos 		if (el_getc(el, &ch) != 1) {
    673  1.18  christos 			ed_end_of_file(el, 0);
    674  1.18  christos 			len = -1;
    675  1.18  christos 			break;
    676  1.18  christos 		}
    677   1.1       cgd 
    678   1.9     lukem 		switch (ch) {
    679  1.18  christos 
    680   1.9     lukem 		case 0010:	/* Delete and backspace */
    681   1.9     lukem 		case 0177:
    682  1.18  christos 			if (len <= 0) {
    683  1.18  christos 				len = -1;
    684  1.18  christos 				break;
    685   1.9     lukem 			}
    686  1.18  christos 			cp--;
    687  1.18  christos 			continue;
    688   1.9     lukem 
    689   1.9     lukem 		case 0033:	/* ESC */
    690   1.9     lukem 		case '\r':	/* Newline */
    691   1.9     lukem 		case '\n':
    692  1.18  christos 			buf[len] = ch;
    693   1.9     lukem 			break;
    694   1.9     lukem 
    695   1.9     lukem 		default:
    696  1.18  christos 			if (len >= EL_BUFSIZ - 16)
    697   1.9     lukem 				term_beep(el);
    698   1.9     lukem 			else {
    699   1.9     lukem 				buf[len++] = ch;
    700  1.18  christos 				*cp++ = ch;
    701   1.9     lukem 			}
    702  1.18  christos 			continue;
    703   1.9     lukem 		}
    704  1.18  christos 		break;
    705   1.9     lukem 	}
    706  1.18  christos 
    707  1.18  christos 	el->el_line.buffer[0] = '\0';
    708  1.18  christos 	el->el_line.lastchar = el->el_line.buffer;
    709  1.18  christos 	el->el_line.cursor = el->el_line.buffer;
    710  1.18  christos 	return len;
    711   1.1       cgd }
    712   1.1       cgd 
    713   1.1       cgd 
    714   1.1       cgd /* c_hpos():
    715   1.1       cgd  *	Return the current horizontal position of the cursor
    716   1.1       cgd  */
    717   1.1       cgd protected int
    718   1.9     lukem c_hpos(EditLine *el)
    719   1.1       cgd {
    720   1.9     lukem 	char *ptr;
    721   1.1       cgd 
    722   1.9     lukem 	/*
    723   1.9     lukem 	 * Find how many characters till the beginning of this line.
    724   1.9     lukem 	 */
    725   1.9     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    726   1.9     lukem 		return (0);
    727   1.9     lukem 	else {
    728   1.9     lukem 		for (ptr = el->el_line.cursor - 1;
    729   1.9     lukem 		     ptr >= el->el_line.buffer && *ptr != '\n';
    730   1.9     lukem 		     ptr--)
    731   1.9     lukem 			continue;
    732   1.9     lukem 		return (el->el_line.cursor - ptr - 1);
    733   1.9     lukem 	}
    734   1.1       cgd }
    735