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