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