2002-10-28 00:41:50 +03:00
|
|
|
/* $NetBSD: chared.c,v 1.16 2002/10/27 21:41:50 christos Exp $ */
|
1997-01-11 09:47:47 +03:00
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Christos Zoulas of Cornell University.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2002-03-18 19:00:50 +03:00
|
|
|
#include "config.h"
|
1994-05-06 10:01:42 +04:00
|
|
|
#if !defined(lint) && !defined(SCCSID)
|
1997-01-11 09:47:47 +03:00
|
|
|
#if 0
|
1994-05-06 10:01:42 +04:00
|
|
|
static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
|
1997-01-11 09:47:47 +03:00
|
|
|
#else
|
2002-10-28 00:41:50 +03:00
|
|
|
__RCSID("$NetBSD: chared.c,v 1.16 2002/10/27 21:41:50 christos Exp $");
|
1997-01-11 09:47:47 +03:00
|
|
|
#endif
|
1994-05-06 10:01:42 +04:00
|
|
|
#endif /* not lint && not SCCSID */
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
/*
|
1994-05-06 10:01:42 +04:00
|
|
|
* chared.c: Character editor utilities
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "el.h"
|
|
|
|
|
2001-01-10 10:45:41 +03:00
|
|
|
/* value to leave unused in line buffer */
|
|
|
|
#define EL_LEAVE 2
|
|
|
|
|
1994-05-06 10:01:42 +04:00
|
|
|
/* cv_undo():
|
|
|
|
* Handle state for the vi undo command
|
|
|
|
*/
|
|
|
|
protected void
|
2002-10-28 00:41:50 +03:00
|
|
|
cv_undo(EditLine *el, int size, char *ptr)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
c_undo_t *vu = &el->el_chared.c_undo;
|
2002-10-28 00:41:50 +03:00
|
|
|
|
|
|
|
vu->len = el->el_line.lastchar - el->el_line.buffer;
|
|
|
|
vu->cursor = el->el_line.cursor - el->el_line.buffer;
|
|
|
|
memcpy(vu->buf, el->el_line.buffer, vu->len + 1u);
|
|
|
|
|
|
|
|
if (ptr) {
|
|
|
|
memcpy(vu->paste, ptr, size +0u);
|
|
|
|
vu->paste_len = size;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-07-02 19:14:07 +04:00
|
|
|
/* c_insert():
|
1994-05-06 10:01:42 +04:00
|
|
|
* Insert num characters
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_insert(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.lastchar + num >= el->el_line.limit)
|
|
|
|
return; /* can't go past end of buffer */
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor < el->el_line.lastchar) {
|
|
|
|
/* if I must move chars */
|
|
|
|
for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
|
|
|
|
cp[num] = *cp;
|
|
|
|
}
|
|
|
|
el->el_line.lastchar += num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* c_delafter():
|
|
|
|
* Delete num characters after the cursor
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delafter(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor + num > el->el_line.lastchar)
|
|
|
|
num = el->el_line.lastchar - el->el_line.cursor;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
if (el->el_map.current != el->el_map.emacs)
|
|
|
|
cv_undo(el, num, el->el_line.cursor);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (num > 0) {
|
|
|
|
char *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
|
|
|
|
*cp = cp[num];
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.lastchar -= num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c_delbefore():
|
|
|
|
* Delete num characters before the cursor
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delbefore(EditLine *el, int num)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor - num < el->el_line.buffer)
|
|
|
|
num = el->el_line.cursor - el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
if (el->el_map.current != el->el_map.emacs)
|
|
|
|
cv_undo(el, num, el->el_line.cursor - num);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (num > 0) {
|
|
|
|
char *cp;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
for (cp = el->el_line.cursor - num;
|
|
|
|
cp <= el->el_line.lastchar;
|
|
|
|
cp++)
|
|
|
|
*cp = cp[num];
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.lastchar -= num;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ce__isword():
|
|
|
|
* Return if p is part of a word according to emacs
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
ce__isword(int p)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-10-28 00:41:50 +03:00
|
|
|
return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__isword():
|
|
|
|
* Return if p is part of a word according to vi
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
cv__isword(int p)
|
2002-10-28 00:41:50 +03:00
|
|
|
{
|
|
|
|
return (isalnum(p));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__isWord():
|
|
|
|
* Return if p is part of a big word according to vi
|
|
|
|
*/
|
|
|
|
protected int
|
|
|
|
cv__isWord(int p)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
return (!isspace(p));
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c__prev_word():
|
|
|
|
* Find the previous word
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
c__prev_word(char *p, char *low, int n, int (*wtest)(int))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
while ((p >= low) && !(*wtest)((unsigned char) *p))
|
|
|
|
p--;
|
|
|
|
while ((p >= low) && (*wtest)((unsigned char) *p))
|
|
|
|
p--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cp now points to one character before the word */
|
|
|
|
p++;
|
|
|
|
if (p < low)
|
|
|
|
p = low;
|
|
|
|
/* cp now points where we want it */
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c__next_word():
|
|
|
|
* Find the next word
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
c__next_word(char *p, char *high, int n, int (*wtest)(int))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
|
|
|
while ((p < high) && !(*wtest)((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
while ((p < high) && (*wtest)((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p > high)
|
|
|
|
p = high;
|
|
|
|
/* p now points where we want it */
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* cv_next_word():
|
|
|
|
* Find the next word vi style
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int test;
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
test = (*wtest)((unsigned char) *p);
|
|
|
|
while ((p < high) && (*wtest)((unsigned char) *p) == test)
|
|
|
|
p++;
|
|
|
|
/*
|
|
|
|
* vi historically deletes with cw only the word preserving the
|
|
|
|
* trailing whitespace! This is not what 'w' does..
|
|
|
|
*/
|
2002-10-28 00:41:50 +03:00
|
|
|
if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
|
2000-09-05 02:06:28 +04:00
|
|
|
while ((p < high) && isspace((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
/* p now points where we want it */
|
|
|
|
if (p > high)
|
|
|
|
return (high);
|
|
|
|
else
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* cv_prev_word():
|
|
|
|
* Find the previous word vi style
|
|
|
|
*/
|
|
|
|
protected char *
|
2002-10-28 00:41:50 +03:00
|
|
|
cv_prev_word(char *p, char *low, int n, int (*wtest)(int))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int test;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
p--;
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
2002-10-28 00:41:50 +03:00
|
|
|
while ((p > low) && isspace((unsigned char) *p))
|
|
|
|
p--;
|
2000-09-05 02:06:28 +04:00
|
|
|
test = (*wtest)((unsigned char) *p);
|
|
|
|
while ((p >= low) && (*wtest)((unsigned char) *p) == test)
|
|
|
|
p--;
|
|
|
|
}
|
2002-10-28 00:41:50 +03:00
|
|
|
p++;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
/* p now points where we want it */
|
|
|
|
if (p < low)
|
|
|
|
return (low);
|
|
|
|
else
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef notdef
|
|
|
|
/* c__number():
|
|
|
|
* Ignore character p points to, return number appearing after that.
|
|
|
|
* A '$' by itself means a big number; "$-" is for negative; '^' means 1.
|
|
|
|
* Return p pointing to last char used.
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
c__number(
|
|
|
|
char *p, /* character position */
|
|
|
|
int *num, /* Return value */
|
|
|
|
int dval) /* dval is the number to subtract from like $-3 */
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int i;
|
|
|
|
int sign = 1;
|
|
|
|
|
|
|
|
if (*++p == '^') {
|
|
|
|
*num = 1;
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
if (*p == '$') {
|
|
|
|
if (*++p != '-') {
|
|
|
|
*num = 0x7fffffff; /* Handle $ */
|
|
|
|
return (--p);
|
|
|
|
}
|
|
|
|
sign = -1; /* Handle $- */
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
|
|
|
|
continue;
|
|
|
|
*num = (sign < 0 ? dval - i : i);
|
|
|
|
return (--p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* cv_delfini():
|
|
|
|
* Finish vi delete action
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
cv_delfini(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
int size;
|
|
|
|
|
|
|
|
if (el->el_chared.c_vcmd.action & INSERT)
|
|
|
|
el->el_map.current = el->el_map.key;
|
|
|
|
|
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
|
|
|
|
|
|
|
if (el->el_chared.c_vcmd.pos == 0)
|
|
|
|
return;
|
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
|
|
|
|
el->el_line.cursor = el->el_chared.c_vcmd.pos;
|
|
|
|
if (size > 0) {
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delafter(el, size);
|
2002-10-28 00:41:50 +03:00
|
|
|
re_refresh_cursor(el);
|
|
|
|
} else if (size < 0) {
|
|
|
|
c_delbefore(el, -size);
|
|
|
|
el->el_line.cursor += size;
|
2000-09-05 02:06:28 +04:00
|
|
|
} else {
|
2002-10-28 00:41:50 +03:00
|
|
|
c_delafter(el, 1);
|
|
|
|
el->el_line.cursor--;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef notdef
|
|
|
|
/* ce__endword():
|
|
|
|
* Go to the end of this word according to emacs
|
|
|
|
*/
|
|
|
|
protected char *
|
2000-09-05 02:06:28 +04:00
|
|
|
ce__endword(char *p, char *high, int n)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
|
|
|
while ((p < high) && isspace((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
while ((p < high) && !isspace((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
p--;
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* cv__endword():
|
|
|
|
* Go to the end of this word according to vi
|
|
|
|
*/
|
|
|
|
protected char *
|
2002-10-28 00:41:50 +03:00
|
|
|
cv__endword(char *p, char *high, int n, int (*wtest)(int))
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2002-10-28 00:41:50 +03:00
|
|
|
int test;
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
p++;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
while (n--) {
|
|
|
|
while ((p < high) && isspace((unsigned char) *p))
|
|
|
|
p++;
|
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
test = (*wtest)((unsigned char) *p);
|
|
|
|
while ((p < high) && (*wtest)((unsigned char) *p) == test)
|
|
|
|
p++;
|
2000-09-05 02:06:28 +04:00
|
|
|
}
|
|
|
|
p--;
|
|
|
|
return (p);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ch_init():
|
|
|
|
* Initialize the character editor
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
ch_init(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2001-01-04 18:56:31 +03:00
|
|
|
el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
|
|
|
|
if (el->el_line.buffer == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
|
2000-09-05 02:06:28 +04:00
|
|
|
|
2001-01-04 18:56:31 +03:00
|
|
|
el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
|
|
|
|
if (el->el_chared.c_undo.buf == NULL)
|
|
|
|
return (-1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_chared.c_undo.len = -1;
|
|
|
|
el->el_chared.c_undo.cursor = 0;
|
|
|
|
el->el_chared.c_undo.paste = (char *) el_malloc(EL_BUFSIZ);
|
|
|
|
if (el->el_chared.c_undo.paste == NULL)
|
|
|
|
return (-1);
|
|
|
|
el->el_chared.c_undo.paste_len = 0;
|
2000-09-05 02:06:28 +04:00
|
|
|
|
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
|
|
|
el->el_chared.c_vcmd.pos = el->el_line.buffer;
|
|
|
|
|
|
|
|
el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
|
2001-01-04 18:56:31 +03:00
|
|
|
if (el->el_chared.c_kill.buf == NULL)
|
|
|
|
return (-1);
|
2000-09-05 02:06:28 +04:00
|
|
|
(void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
|
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer;
|
|
|
|
el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
|
|
|
|
|
|
|
|
el->el_map.current = el->el_map.key;
|
|
|
|
|
|
|
|
el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
|
|
|
|
el->el_state.doingarg = 0;
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
el->el_state.argument = 1;
|
|
|
|
el->el_state.lastcmd = ED_UNASSIGNED;
|
|
|
|
|
|
|
|
el->el_chared.c_macro.nline = NULL;
|
|
|
|
el->el_chared.c_macro.level = -1;
|
|
|
|
el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
|
2001-01-04 18:56:31 +03:00
|
|
|
sizeof(char *));
|
|
|
|
if (el->el_chared.c_macro.macro == NULL)
|
|
|
|
return (-1);
|
2000-09-05 02:06:28 +04:00
|
|
|
return (0);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ch_reset():
|
|
|
|
* Reset the character editor
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
ch_reset(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
el->el_chared.c_undo.len = -1;
|
|
|
|
el->el_chared.c_undo.cursor = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_vcmd.action = NOP;
|
|
|
|
el->el_chared.c_vcmd.pos = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_map.current = el->el_map.key;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
|
|
|
|
el->el_state.doingarg = 0;
|
|
|
|
el->el_state.metanext = 0;
|
|
|
|
el->el_state.argument = 1;
|
|
|
|
el->el_state.lastcmd = ED_UNASSIGNED;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_chared.c_macro.level = -1;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
el->el_history.eventno = 0;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
2001-01-10 10:45:41 +03:00
|
|
|
/* ch_enlargebufs():
|
|
|
|
* Enlarge line buffer to be able to hold twice as much characters.
|
|
|
|
* Returns 1 if successful, 0 if not.
|
|
|
|
*/
|
|
|
|
protected int
|
|
|
|
ch_enlargebufs(el, addlen)
|
2001-04-13 05:04:19 +04:00
|
|
|
EditLine *el;
|
|
|
|
size_t addlen;
|
2001-01-10 10:45:41 +03:00
|
|
|
{
|
2001-04-13 05:04:19 +04:00
|
|
|
size_t sz, newsz;
|
|
|
|
char *newbuffer, *oldbuf, *oldkbuf;
|
|
|
|
|
|
|
|
sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
|
|
|
|
newsz = sz * 2;
|
|
|
|
/*
|
|
|
|
* If newly required length is longer than current buffer, we need
|
|
|
|
* to make the buffer big enough to hold both old and new stuff.
|
|
|
|
*/
|
|
|
|
if (addlen > sz) {
|
|
|
|
while(newsz - sz < addlen)
|
|
|
|
newsz *= 2;
|
|
|
|
}
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/*
|
|
|
|
* Reallocate line buffer.
|
|
|
|
*/
|
|
|
|
newbuffer = el_realloc(el->el_line.buffer, newsz);
|
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* zero the newly added memory, leave old data in */
|
|
|
|
(void) memset(&newbuffer[sz], 0, newsz - sz);
|
|
|
|
|
|
|
|
oldbuf = el->el_line.buffer;
|
|
|
|
|
|
|
|
el->el_line.buffer = newbuffer;
|
|
|
|
el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
|
|
|
|
el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
|
2002-10-28 00:41:50 +03:00
|
|
|
/* don't set new size until all buffers are enlarged */
|
|
|
|
el->el_line.limit = &newbuffer[sz - EL_LEAVE];
|
2001-04-13 05:04:19 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Reallocate kill buffer.
|
|
|
|
*/
|
|
|
|
newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
|
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* zero the newly added memory, leave old data in */
|
|
|
|
(void) memset(&newbuffer[sz], 0, newsz - sz);
|
|
|
|
|
|
|
|
oldkbuf = el->el_chared.c_kill.buf;
|
|
|
|
|
|
|
|
el->el_chared.c_kill.buf = newbuffer;
|
|
|
|
el->el_chared.c_kill.last = newbuffer +
|
2001-01-10 10:45:41 +03:00
|
|
|
(el->el_chared.c_kill.last - oldkbuf);
|
2001-04-13 05:04:19 +04:00
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer +
|
2001-01-10 10:45:41 +03:00
|
|
|
(el->el_chared.c_kill.mark - oldbuf);
|
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/*
|
|
|
|
* Reallocate undo buffer.
|
|
|
|
*/
|
|
|
|
newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
|
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2001-04-13 05:04:19 +04:00
|
|
|
/* zero the newly added memory, leave old data in */
|
|
|
|
(void) memset(&newbuffer[sz], 0, newsz - sz);
|
|
|
|
el->el_chared.c_undo.buf = newbuffer;
|
2002-10-28 00:41:50 +03:00
|
|
|
|
|
|
|
newbuffer = el_realloc(el->el_chared.c_undo.paste, newsz);
|
|
|
|
if (!newbuffer)
|
|
|
|
return 0;
|
|
|
|
el->el_chared.c_undo.paste = newbuffer;
|
2001-04-13 05:04:19 +04:00
|
|
|
|
|
|
|
if (!hist_enlargebuf(el, sz, newsz))
|
|
|
|
return 0;
|
2001-01-10 10:45:41 +03:00
|
|
|
|
2002-10-28 00:41:50 +03:00
|
|
|
/* Safe to set enlarged buffer size */
|
|
|
|
el->el_line.limit = &newbuffer[newsz - EL_LEAVE];
|
2001-04-13 05:04:19 +04:00
|
|
|
return 1;
|
2001-01-10 10:45:41 +03:00
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
|
|
|
/* ch_end():
|
|
|
|
* Free the data structures used by the editor
|
|
|
|
*/
|
|
|
|
protected void
|
2000-09-05 02:06:28 +04:00
|
|
|
ch_end(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
el_free((ptr_t) el->el_line.buffer);
|
|
|
|
el->el_line.buffer = NULL;
|
|
|
|
el->el_line.limit = NULL;
|
|
|
|
el_free((ptr_t) el->el_chared.c_undo.buf);
|
|
|
|
el->el_chared.c_undo.buf = NULL;
|
2002-10-28 00:41:50 +03:00
|
|
|
el_free((ptr_t) el->el_chared.c_undo.paste);
|
|
|
|
el->el_chared.c_undo.paste = NULL;
|
|
|
|
el->el_chared.c_undo.paste_len = 0;
|
2000-09-05 02:06:28 +04:00
|
|
|
el_free((ptr_t) el->el_chared.c_kill.buf);
|
|
|
|
el->el_chared.c_kill.buf = NULL;
|
|
|
|
el_free((ptr_t) el->el_chared.c_macro.macro);
|
|
|
|
el->el_chared.c_macro.macro = NULL;
|
|
|
|
ch_reset(el);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_insertstr():
|
|
|
|
* Insert string at cursorI
|
|
|
|
*/
|
|
|
|
public int
|
2000-09-05 02:06:28 +04:00
|
|
|
el_insertstr(EditLine *el, const char *s)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2001-05-17 05:02:17 +04:00
|
|
|
size_t len;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if ((len = strlen(s)) == 0)
|
|
|
|
return (-1);
|
2001-01-10 10:45:41 +03:00
|
|
|
if (el->el_line.lastchar + len >= el->el_line.limit) {
|
|
|
|
if (!ch_enlargebufs(el, len))
|
|
|
|
return (-1);
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2001-05-17 05:02:17 +04:00
|
|
|
c_insert(el, (int)len);
|
2000-09-05 02:06:28 +04:00
|
|
|
while (*s)
|
|
|
|
*el->el_line.cursor++ = *s++;
|
|
|
|
return (0);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* el_deletestr():
|
|
|
|
* Delete num characters before the cursor
|
|
|
|
*/
|
|
|
|
public void
|
2000-09-05 02:06:28 +04:00
|
|
|
el_deletestr(EditLine *el, int n)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
if (n <= 0)
|
|
|
|
return;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
if (el->el_line.cursor < &el->el_line.buffer[n])
|
|
|
|
return;
|
1994-05-06 10:01:42 +04:00
|
|
|
|
2000-09-05 02:06:28 +04:00
|
|
|
c_delbefore(el, n); /* delete before dot */
|
|
|
|
el->el_line.cursor -= n;
|
|
|
|
if (el->el_line.cursor < el->el_line.buffer)
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* c_gets():
|
|
|
|
* Get a string
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
c_gets(EditLine *el, char *buf)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char ch;
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
for (ch = 0; ch == 0;) {
|
|
|
|
if (el_getc(el, &ch) != 1)
|
|
|
|
return (ed_end_of_file(el, 0));
|
|
|
|
switch (ch) {
|
|
|
|
case 0010: /* Delete and backspace */
|
|
|
|
case 0177:
|
|
|
|
if (len > 1) {
|
|
|
|
*el->el_line.cursor-- = '\0';
|
|
|
|
el->el_line.lastchar = el->el_line.cursor;
|
|
|
|
buf[len--] = '\0';
|
|
|
|
} else {
|
|
|
|
el->el_line.buffer[0] = '\0';
|
|
|
|
el->el_line.lastchar = el->el_line.buffer;
|
|
|
|
el->el_line.cursor = el->el_line.buffer;
|
|
|
|
return (CC_REFRESH);
|
|
|
|
}
|
|
|
|
re_refresh(el);
|
|
|
|
ch = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 0033: /* ESC */
|
|
|
|
case '\r': /* Newline */
|
|
|
|
case '\n':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (len >= EL_BUFSIZ)
|
|
|
|
term_beep(el);
|
|
|
|
else {
|
|
|
|
buf[len++] = ch;
|
|
|
|
*el->el_line.cursor++ = ch;
|
|
|
|
el->el_line.lastchar = el->el_line.cursor;
|
|
|
|
}
|
|
|
|
re_refresh(el);
|
|
|
|
ch = 0;
|
|
|
|
break;
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
2000-09-05 02:06:28 +04:00
|
|
|
buf[len] = ch;
|
|
|
|
return (len);
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* c_hpos():
|
|
|
|
* Return the current horizontal position of the cursor
|
|
|
|
*/
|
|
|
|
protected int
|
2000-09-05 02:06:28 +04:00
|
|
|
c_hpos(EditLine *el)
|
1994-05-06 10:01:42 +04:00
|
|
|
{
|
2000-09-05 02:06:28 +04:00
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find how many characters till the beginning of this line.
|
|
|
|
*/
|
|
|
|
if (el->el_line.cursor == el->el_line.buffer)
|
|
|
|
return (0);
|
|
|
|
else {
|
|
|
|
for (ptr = el->el_line.cursor - 1;
|
|
|
|
ptr >= el->el_line.buffer && *ptr != '\n';
|
|
|
|
ptr--)
|
|
|
|
continue;
|
|
|
|
return (el->el_line.cursor - ptr - 1);
|
|
|
|
}
|
1994-05-06 10:01:42 +04:00
|
|
|
}
|