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