version 1.15, 2002/03/18 16:00:50 |
version 1.16, 2002/10/27 21:41:50 |
Line 58 __RCSID("$NetBSD$"); |
|
Line 58 __RCSID("$NetBSD$"); |
|
* Handle state for the vi undo command |
* Handle state for the vi undo command |
*/ |
*/ |
protected void |
protected void |
cv_undo(EditLine *el,int action, size_t size, char *ptr) |
cv_undo(EditLine *el, int size, char *ptr) |
{ |
{ |
c_undo_t *vu = &el->el_chared.c_undo; |
c_undo_t *vu = &el->el_chared.c_undo; |
vu->action = action; |
|
vu->ptr = ptr; |
vu->len = el->el_line.lastchar - el->el_line.buffer; |
vu->isize = size; |
vu->cursor = el->el_line.cursor - el->el_line.buffer; |
(void) memcpy(vu->buf, vu->ptr, size); |
memcpy(vu->buf, el->el_line.buffer, vu->len + 1u); |
#ifdef DEBUG_UNDO |
|
(void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n", |
if (ptr) { |
vu->ptr, vu->isize, vu->dsize); |
memcpy(vu->paste, ptr, size +0u); |
#endif |
vu->paste_len = size; |
|
} |
} |
} |
|
|
|
|
Line 102 c_delafter(EditLine *el, int num) |
|
Line 103 c_delafter(EditLine *el, int num) |
|
if (el->el_line.cursor + num > el->el_line.lastchar) |
if (el->el_line.cursor + num > el->el_line.lastchar) |
num = el->el_line.lastchar - el->el_line.cursor; |
num = el->el_line.lastchar - el->el_line.cursor; |
|
|
|
if (el->el_map.current != el->el_map.emacs) |
|
cv_undo(el, num, el->el_line.cursor); |
|
|
if (num > 0) { |
if (num > 0) { |
char *cp; |
char *cp; |
|
|
if (el->el_map.current != el->el_map.emacs) |
|
cv_undo(el, INSERT, (size_t)num, el->el_line.cursor); |
|
|
|
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) |
for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++) |
*cp = cp[num]; |
*cp = cp[num]; |
|
|
Line 126 c_delbefore(EditLine *el, int num) |
|
Line 127 c_delbefore(EditLine *el, int num) |
|
if (el->el_line.cursor - num < el->el_line.buffer) |
if (el->el_line.cursor - num < el->el_line.buffer) |
num = el->el_line.cursor - el->el_line.buffer; |
num = el->el_line.cursor - el->el_line.buffer; |
|
|
|
if (el->el_map.current != el->el_map.emacs) |
|
cv_undo(el, num, el->el_line.cursor - num); |
|
|
if (num > 0) { |
if (num > 0) { |
char *cp; |
char *cp; |
|
|
if (el->el_map.current != el->el_map.emacs) |
|
cv_undo(el, INSERT, (size_t)num, |
|
el->el_line.cursor - num); |
|
|
|
for (cp = el->el_line.cursor - num; |
for (cp = el->el_line.cursor - num; |
cp <= el->el_line.lastchar; |
cp <= el->el_line.lastchar; |
cp++) |
cp++) |
Line 149 c_delbefore(EditLine *el, int num) |
|
Line 149 c_delbefore(EditLine *el, int num) |
|
protected int |
protected int |
ce__isword(int p) |
ce__isword(int p) |
{ |
{ |
return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL); |
return (isalnum(p) || strchr("*?_-.[]~=", p) != NULL); |
} |
} |
|
|
|
|
Line 159 ce__isword(int p) |
|
Line 159 ce__isword(int p) |
|
protected int |
protected int |
cv__isword(int p) |
cv__isword(int p) |
{ |
{ |
|
return (isalnum(p)); |
|
} |
|
|
|
|
|
/* cv__isWord(): |
|
* Return if p is part of a big word according to vi |
|
*/ |
|
protected int |
|
cv__isWord(int p) |
|
{ |
return (!isspace(p)); |
return (!isspace(p)); |
} |
} |
|
|
Line 221 cv_next_word(EditLine *el, char *p, char |
|
Line 231 cv_next_word(EditLine *el, char *p, char |
|
* vi historically deletes with cw only the word preserving the |
* vi historically deletes with cw only the word preserving the |
* trailing whitespace! This is not what 'w' does.. |
* trailing whitespace! This is not what 'w' does.. |
*/ |
*/ |
if (el->el_chared.c_vcmd.action != (DELETE|INSERT)) |
if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT)) |
while ((p < high) && isspace((unsigned char) *p)) |
while ((p < high) && isspace((unsigned char) *p)) |
p++; |
p++; |
} |
} |
Line 238 cv_next_word(EditLine *el, char *p, char |
|
Line 248 cv_next_word(EditLine *el, char *p, char |
|
* Find the previous word vi style |
* Find the previous word vi style |
*/ |
*/ |
protected char * |
protected char * |
cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int)) |
cv_prev_word(char *p, char *low, int n, int (*wtest)(int)) |
{ |
{ |
int test; |
int test; |
|
|
|
p--; |
while (n--) { |
while (n--) { |
p--; |
while ((p > low) && isspace((unsigned char) *p)) |
/* |
p--; |
* vi historically deletes with cb only the word preserving the |
|
* leading whitespace! This is not what 'b' does.. |
|
*/ |
|
if (el->el_chared.c_vcmd.action != (DELETE|INSERT)) |
|
while ((p > low) && isspace((unsigned char) *p)) |
|
p--; |
|
test = (*wtest)((unsigned char) *p); |
test = (*wtest)((unsigned char) *p); |
while ((p >= low) && (*wtest)((unsigned char) *p) == test) |
while ((p >= low) && (*wtest)((unsigned char) *p) == test) |
p--; |
p--; |
p++; |
|
while (isspace((unsigned char) *p)) |
|
p++; |
|
} |
} |
|
p++; |
|
|
/* p now points where we want it */ |
/* p now points where we want it */ |
if (p < low) |
if (p < low) |
|
|
cv_delfini(EditLine *el) |
cv_delfini(EditLine *el) |
{ |
{ |
int size; |
int size; |
int oaction; |
|
|
|
if (el->el_chared.c_vcmd.action & INSERT) |
if (el->el_chared.c_vcmd.action & INSERT) |
el->el_map.current = el->el_map.key; |
el->el_map.current = el->el_map.key; |
|
|
oaction = el->el_chared.c_vcmd.action; |
|
el->el_chared.c_vcmd.action = NOP; |
el->el_chared.c_vcmd.action = NOP; |
|
|
if (el->el_chared.c_vcmd.pos == 0) |
if (el->el_chared.c_vcmd.pos == 0) |
return; |
return; |
|
|
|
size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos); |
if (el->el_line.cursor > el->el_chared.c_vcmd.pos) { |
el->el_line.cursor = el->el_chared.c_vcmd.pos; |
size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos); |
if (size > 0) { |
c_delbefore(el, size); |
|
el->el_line.cursor = el->el_chared.c_vcmd.pos; |
|
re_refresh_cursor(el); |
|
} else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) { |
|
size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor); |
|
c_delafter(el, size); |
c_delafter(el, size); |
|
re_refresh_cursor(el); |
|
} else if (size < 0) { |
|
c_delbefore(el, -size); |
|
el->el_line.cursor += size; |
} else { |
} else { |
size = 1; |
c_delafter(el, 1); |
c_delafter(el, size); |
el->el_line.cursor--; |
} |
} |
switch (oaction) { |
|
case DELETE|INSERT: |
|
el->el_chared.c_undo.action = DELETE|INSERT; |
|
break; |
|
case DELETE: |
|
el->el_chared.c_undo.action = INSERT; |
|
break; |
|
case NOP: |
|
case INSERT: |
|
default: |
|
EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction)); |
|
break; |
|
} |
|
|
|
|
|
el->el_chared.c_undo.ptr = el->el_line.cursor; |
|
el->el_chared.c_undo.dsize = size; |
|
} |
} |
|
|
|
|
Line 378 ce__endword(char *p, char *high, int n) |
|
Line 361 ce__endword(char *p, char *high, int n) |
|
* Go to the end of this word according to vi |
* Go to the end of this word according to vi |
*/ |
*/ |
protected char * |
protected char * |
cv__endword(char *p, char *high, int n) |
cv__endword(char *p, char *high, int n, int (*wtest)(int)) |
{ |
{ |
|
int test; |
|
|
p++; |
p++; |
|
|
while (n--) { |
while (n--) { |
while ((p < high) && isspace((unsigned char) *p)) |
while ((p < high) && isspace((unsigned char) *p)) |
p++; |
p++; |
|
|
if (isalnum((unsigned char) *p)) |
test = (*wtest)((unsigned char) *p); |
while ((p < high) && isalnum((unsigned char) *p)) |
while ((p < high) && (*wtest)((unsigned char) *p) == test) |
p++; |
p++; |
else |
|
while ((p < high) && !(isspace((unsigned char) *p) || |
|
isalnum((unsigned char) *p))) |
|
p++; |
|
} |
} |
p--; |
p--; |
return (p); |
return (p); |
Line 411 ch_init(EditLine *el) |
|
Line 392 ch_init(EditLine *el) |
|
(void) memset(el->el_line.buffer, 0, EL_BUFSIZ); |
(void) memset(el->el_line.buffer, 0, EL_BUFSIZ); |
el->el_line.cursor = el->el_line.buffer; |
el->el_line.cursor = el->el_line.buffer; |
el->el_line.lastchar = el->el_line.buffer; |
el->el_line.lastchar = el->el_line.buffer; |
el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2]; |
el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE]; |
|
|
el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ); |
el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ); |
if (el->el_chared.c_undo.buf == NULL) |
if (el->el_chared.c_undo.buf == NULL) |
return (-1); |
return (-1); |
(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ); |
(void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ); |
el->el_chared.c_undo.action = NOP; |
el->el_chared.c_undo.len = -1; |
el->el_chared.c_undo.isize = 0; |
el->el_chared.c_undo.cursor = 0; |
el->el_chared.c_undo.dsize = 0; |
el->el_chared.c_undo.paste = (char *) el_malloc(EL_BUFSIZ); |
el->el_chared.c_undo.ptr = el->el_line.buffer; |
if (el->el_chared.c_undo.paste == NULL) |
|
return (-1); |
|
el->el_chared.c_undo.paste_len = 0; |
|
|
el->el_chared.c_vcmd.action = NOP; |
el->el_chared.c_vcmd.action = NOP; |
el->el_chared.c_vcmd.pos = el->el_line.buffer; |
el->el_chared.c_vcmd.pos = el->el_line.buffer; |
el->el_chared.c_vcmd.ins = el->el_line.buffer; |
|
|
|
el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ); |
el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ); |
if (el->el_chared.c_kill.buf == NULL) |
if (el->el_chared.c_kill.buf == NULL) |
Line 459 ch_reset(EditLine *el) |
|
Line 441 ch_reset(EditLine *el) |
|
el->el_line.cursor = el->el_line.buffer; |
el->el_line.cursor = el->el_line.buffer; |
el->el_line.lastchar = el->el_line.buffer; |
el->el_line.lastchar = el->el_line.buffer; |
|
|
el->el_chared.c_undo.action = NOP; |
el->el_chared.c_undo.len = -1; |
el->el_chared.c_undo.isize = 0; |
el->el_chared.c_undo.cursor = 0; |
el->el_chared.c_undo.dsize = 0; |
|
el->el_chared.c_undo.ptr = el->el_line.buffer; |
|
|
|
el->el_chared.c_vcmd.action = NOP; |
el->el_chared.c_vcmd.action = NOP; |
el->el_chared.c_vcmd.pos = el->el_line.buffer; |
el->el_chared.c_vcmd.pos = el->el_line.buffer; |
el->el_chared.c_vcmd.ins = el->el_line.buffer; |
|
|
|
el->el_chared.c_kill.mark = el->el_line.buffer; |
el->el_chared.c_kill.mark = el->el_line.buffer; |
|
|
Line 521 ch_enlargebufs(el, addlen) |
|
Line 500 ch_enlargebufs(el, addlen) |
|
el->el_line.buffer = newbuffer; |
el->el_line.buffer = newbuffer; |
el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); |
el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf); |
el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); |
el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf); |
el->el_line.limit = &newbuffer[newsz - EL_LEAVE]; |
/* don't set new size until all buffers are enlarged */ |
|
el->el_line.limit = &newbuffer[sz - EL_LEAVE]; |
|
|
/* |
/* |
* Reallocate kill buffer. |
* Reallocate kill buffer. |
Line 550 ch_enlargebufs(el, addlen) |
|
Line 530 ch_enlargebufs(el, addlen) |
|
|
|
/* zero the newly added memory, leave old data in */ |
/* zero the newly added memory, leave old data in */ |
(void) memset(&newbuffer[sz], 0, newsz - sz); |
(void) memset(&newbuffer[sz], 0, newsz - sz); |
|
|
el->el_chared.c_undo.ptr = el->el_line.buffer + |
|
(el->el_chared.c_undo.ptr - oldbuf); |
|
el->el_chared.c_undo.buf = newbuffer; |
el->el_chared.c_undo.buf = newbuffer; |
|
|
|
newbuffer = el_realloc(el->el_chared.c_undo.paste, newsz); |
|
if (!newbuffer) |
|
return 0; |
|
el->el_chared.c_undo.paste = newbuffer; |
|
|
if (!hist_enlargebuf(el, sz, newsz)) |
if (!hist_enlargebuf(el, sz, newsz)) |
return 0; |
return 0; |
|
|
|
/* Safe to set enlarged buffer size */ |
|
el->el_line.limit = &newbuffer[newsz - EL_LEAVE]; |
return 1; |
return 1; |
} |
} |
|
|
Line 572 ch_end(EditLine *el) |
|
Line 556 ch_end(EditLine *el) |
|
el->el_line.limit = NULL; |
el->el_line.limit = NULL; |
el_free((ptr_t) el->el_chared.c_undo.buf); |
el_free((ptr_t) el->el_chared.c_undo.buf); |
el->el_chared.c_undo.buf = NULL; |
el->el_chared.c_undo.buf = NULL; |
|
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; |
el_free((ptr_t) el->el_chared.c_kill.buf); |
el_free((ptr_t) el->el_chared.c_kill.buf); |
el->el_chared.c_kill.buf = NULL; |
el->el_chared.c_kill.buf = NULL; |
el_free((ptr_t) el->el_chared.c_macro.macro); |
el_free((ptr_t) el->el_chared.c_macro.macro); |