NetBSD/lib/libedit/vi.c

886 lines
18 KiB
C
Raw Normal View History

/* $NetBSD: vi.c,v 1.11 2002/10/31 02:01:47 christos Exp $ */
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.
*/
#include "config.h"
1994-05-06 10:01:42 +04:00
#if !defined(lint) && !defined(SCCSID)
#if 0
1994-05-06 10:01:42 +04:00
static char sccsid[] = "@(#)vi.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: vi.c,v 1.11 2002/10/31 02:01:47 christos Exp $");
#endif
1994-05-06 10:01:42 +04:00
#endif /* not lint && not SCCSID */
/*
* vi.c: Vi mode commands.
*/
#include "el.h"
private el_action_t cv_action(EditLine *, int);
private el_action_t cv_paste(EditLine *, int);
1994-05-06 10:01:42 +04:00
/* cv_action():
* Handle vi actions.
*/
private el_action_t
cv_action(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_chared.c_vcmd.action & DELETE) {
/* 'dd' and (possibly) friends */
el->el_chared.c_vcmd.action = NOP;
el->el_chared.c_vcmd.pos = 0;
cv_undo(el, el->el_line.lastchar - el->el_line.buffer,
el->el_line.buffer );
el->el_line.lastchar = el->el_line.buffer;
el->el_line.cursor = el->el_line.buffer;
if (c & INSERT)
el->el_map.current = el->el_map.key;
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
el->el_chared.c_vcmd.pos = el->el_line.cursor;
el->el_chared.c_vcmd.action = c;
return (CC_ARGHACK);
#ifdef notdef
/*
* I don't think that this is needed. But we keep it for now
*/
else
if (el_chared.c_vcmd.action == NOP) {
el->el_chared.c_vcmd.pos = el->el_line.cursor;
el->el_chared.c_vcmd.action = c;
return (CC_ARGHACK);
} else {
el->el_chared.c_vcmd.action = 0;
el->el_chared.c_vcmd.pos = 0;
return (CC_ERROR);
}
1994-05-06 10:01:42 +04:00
#endif
}
/* cv_paste():
* Paste previous deletion before or after the cursor
*/
1997-07-06 22:25:21 +04:00
private el_action_t
cv_paste(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char *ptr;
c_undo_t *un = &el->el_chared.c_undo;
1994-05-06 10:01:42 +04:00
#ifdef DEBUG_PASTE
(void) fprintf(el->el_errfile, "Paste: \"%.*s\"\n",
(int)un->paste_len, un->paste ? un->paste : "<null>" );
1994-05-06 10:01:42 +04:00
#endif
if (!un->paste || !un->paste_len)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
cv_undo(el, 0, 0);
if (!c && el->el_line.cursor < el->el_line.lastchar)
el->el_line.cursor++;
ptr = el->el_line.cursor;
1999-07-02 19:14:07 +04:00
c_insert(el, (int) un->paste_len);
if (el->el_line.cursor + un->paste_len > el->el_line.lastchar)
return (CC_ERROR);
(void) memcpy(ptr, un->paste, un->paste_len);
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_paste_next():
1994-05-06 10:01:42 +04:00
* Vi paste previous deletion to the right of the cursor
* [p]
*/
protected el_action_t
/*ARGSUSED*/
vi_paste_next(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
return (cv_paste(el, 0));
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_paste_prev():
1994-05-06 10:01:42 +04:00
* Vi paste previous deletion to the left of the cursor
* [P]
*/
protected el_action_t
/*ARGSUSED*/
vi_paste_prev(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
return (cv_paste(el, 1));
1994-05-06 10:01:42 +04:00
}
/* vi_prev_big_word():
1994-05-06 10:01:42 +04:00
* Vi move to the previous space delimited word
* [B]
*/
protected el_action_t
/*ARGSUSED*/
vi_prev_big_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.buffer)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv_prev_word(el->el_line.cursor,
el->el_line.buffer,
el->el_state.argument,
cv__isWord);
1994-05-06 10:01:42 +04:00
if (el->el_chared.c_vcmd.action & DELETE) {
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_prev_word():
1994-05-06 10:01:42 +04:00
* Vi move to the previous word
* [b]
1994-05-06 10:01:42 +04:00
*/
protected el_action_t
/*ARGSUSED*/
vi_prev_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.buffer)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv_prev_word(el->el_line.cursor,
el->el_line.buffer,
el->el_state.argument,
cv__isword);
1994-05-06 10:01:42 +04:00
if (el->el_chared.c_vcmd.action & DELETE) {
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
/* vi_next_big_word():
1994-05-06 10:01:42 +04:00
* Vi move to the next space delimited word
* [W]
*/
protected el_action_t
/*ARGSUSED*/
vi_next_big_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.lastchar)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
el->el_line.lastchar,
el->el_state.argument,
cv__isWord);
1994-05-06 10:01:42 +04:00
if (el->el_map.type == MAP_VI)
if (el->el_chared.c_vcmd.action & DELETE) {
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_next_word():
1994-05-06 10:01:42 +04:00
* Vi move to the next word
* [w]
*/
protected el_action_t
/*ARGSUSED*/
vi_next_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.lastchar)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
el->el_line.lastchar,
el->el_state.argument,
cv__isword);
1994-05-06 10:01:42 +04:00
if (el->el_map.type == MAP_VI)
if (el->el_chared.c_vcmd.action & DELETE) {
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_change_case():
1994-05-06 10:01:42 +04:00
* Vi change case of character under the cursor and advance one character
* [~]
*/
protected el_action_t
vi_change_case(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
int i;
if (el->el_line.cursor >= el->el_line.lastchar)
return (CC_ERROR);
cv_undo(el, 0, 0);
for (i = 0; i < el->el_state.argument; i++) {
if (el->el_line.cursor >= el->el_line.lastchar)
break;
c = *el->el_line.cursor;
if (isupper(c))
*el->el_line.cursor++ = tolower(c);
else if (islower(c))
*el->el_line.cursor++ = toupper(c);
else
el->el_line.cursor++;
re_fastaddc(el);
}
return (CC_NORM);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_change_meta():
1994-05-06 10:01:42 +04:00
* Vi change prefix command
* [c]
*/
protected el_action_t
/*ARGSUSED*/
vi_change_meta(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
/*
* Delete with insert == change: first we delete and then we leave in
* insert mode.
*/
return (cv_action(el, DELETE | INSERT));
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_insert_at_bol():
1994-05-06 10:01:42 +04:00
* Vi enter insert mode at the beginning of line
* [I]
*/
protected el_action_t
/*ARGSUSED*/
vi_insert_at_bol(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
el->el_line.cursor = el->el_line.buffer;
cv_undo(el, 0, 0);
el->el_map.current = el->el_map.key;
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_replace_char():
1994-05-06 10:01:42 +04:00
* Vi replace character under the cursor with the next character typed
* [r]
*/
protected el_action_t
/*ARGSUSED*/
vi_replace_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_state.argument > el->el_line.lastchar - el->el_line.cursor)
return CC_ERROR;
el->el_map.current = el->el_map.key;
el->el_state.inputmode = MODE_REPLACE_1;
cv_undo(el, 0, 0);
return (CC_ARGHACK);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_replace_mode():
1994-05-06 10:01:42 +04:00
* Vi enter replace mode
* [R]
*/
protected el_action_t
/*ARGSUSED*/
vi_replace_mode(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
el->el_map.current = el->el_map.key;
el->el_state.inputmode = MODE_REPLACE;
cv_undo(el, 0, 0);
return (CC_NORM);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_substitute_char():
1994-05-06 10:01:42 +04:00
* Vi replace character under the cursor and enter insert mode
* [s]
1994-05-06 10:01:42 +04:00
*/
protected el_action_t
/*ARGSUSED*/
vi_substitute_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
c_delafter(el, el->el_state.argument);
el->el_map.current = el->el_map.key;
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_substitute_line():
1994-05-06 10:01:42 +04:00
* Vi substitute entire line
* [S]
*/
protected el_action_t
/*ARGSUSED*/
vi_substitute_line(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
cv_undo(el, el->el_line.lastchar - el->el_line.buffer,
el->el_line.buffer);
(void) em_kill_line(el, 0);
el->el_map.current = el->el_map.key;
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
1999-07-02 19:14:07 +04:00
/* vi_change_to_eol():
1994-05-06 10:01:42 +04:00
* Vi change to end of line
* [C]
*/
protected el_action_t
/*ARGSUSED*/
vi_change_to_eol(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
cv_undo(el, el->el_line.lastchar - el->el_line.cursor,
el->el_line.cursor);
(void) ed_kill_line(el, 0);
el->el_map.current = el->el_map.key;
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
/* vi_insert():
* Vi enter insert mode
* [i]
*/
protected el_action_t
/*ARGSUSED*/
vi_insert(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
el->el_map.current = el->el_map.key;
cv_undo(el, 0, 0);
return (CC_NORM);
1994-05-06 10:01:42 +04:00
}
/* vi_add():
1999-07-02 19:14:07 +04:00
* Vi enter insert mode after the cursor
1994-05-06 10:01:42 +04:00
* [a]
*/
protected el_action_t
/*ARGSUSED*/
vi_add(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
int ret;
1994-05-06 10:01:42 +04:00
el->el_map.current = el->el_map.key;
if (el->el_line.cursor < el->el_line.lastchar) {
el->el_line.cursor++;
if (el->el_line.cursor > el->el_line.lastchar)
el->el_line.cursor = el->el_line.lastchar;
ret = CC_CURSOR;
} else
ret = CC_NORM;
1994-05-06 10:01:42 +04:00
cv_undo(el, 0, 0);
return (ret);
1994-05-06 10:01:42 +04:00
}
/* vi_add_at_eol():
* Vi enter insert mode at end of line
* [A]
*/
protected el_action_t
/*ARGSUSED*/
vi_add_at_eol(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
el->el_map.current = el->el_map.key;
el->el_line.cursor = el->el_line.lastchar;
cv_undo(el, 0, 0);
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
/* vi_delete_meta():
1999-07-02 19:14:07 +04:00
* Vi delete prefix command
1994-05-06 10:01:42 +04:00
* [d]
*/
protected el_action_t
/*ARGSUSED*/
vi_delete_meta(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
return (cv_action(el, DELETE));
1994-05-06 10:01:42 +04:00
}
/* vi_end_big_word():
1999-07-02 19:14:07 +04:00
* Vi move to the end of the current space delimited word
* [E]
1994-05-06 10:01:42 +04:00
*/
protected el_action_t
/*ARGSUSED*/
vi_end_big_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.lastchar)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv__endword(el->el_line.cursor,
el->el_line.lastchar, el->el_state.argument, cv__isWord);
1994-05-06 10:01:42 +04:00
if (el->el_chared.c_vcmd.action & DELETE) {
el->el_line.cursor++;
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
/* vi_end_word():
1994-05-06 10:01:42 +04:00
* Vi move to the end of the current word
* [e]
*/
protected el_action_t
/*ARGSUSED*/
vi_end_word(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_line.cursor == el->el_line.lastchar)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
el->el_line.cursor = cv__endword(el->el_line.cursor,
el->el_line.lastchar, el->el_state.argument, cv__isword);
1994-05-06 10:01:42 +04:00
if (el->el_chared.c_vcmd.action & DELETE) {
el->el_line.cursor++;
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
/* vi_undo():
* Vi undo last change
* [u]
*/
protected el_action_t
/*ARGSUSED*/
vi_undo(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
c_undo_t un = el->el_chared.c_undo;
1994-05-06 10:01:42 +04:00
if (un.len == -1)
return CC_ERROR;
/* switch line buffer and undo buffer */
el->el_chared.c_undo.buf = el->el_line.buffer;
el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
el->el_chared.c_undo.cursor = el->el_line.cursor - el->el_line.buffer;
el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
el->el_line.buffer = un.buf;
el->el_line.cursor = un.buf + un.cursor;
el->el_line.lastchar = un.buf + un.len;
1994-05-06 10:01:42 +04:00
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
/* vi_command_mode():
* Vi enter command mode (use alternative key bindings)
* [<ESC>]
*/
protected el_action_t
/*ARGSUSED*/
vi_command_mode(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
/* [Esc] cancels pending action */
el->el_chared.c_vcmd.action = NOP;
el->el_chared.c_vcmd.pos = 0;
1994-05-06 10:01:42 +04:00
el->el_state.doingarg = 0;
1994-05-06 10:01:42 +04:00
el->el_state.inputmode = MODE_INSERT;
el->el_map.current = el->el_map.alt;
1994-05-06 10:01:42 +04:00
#ifdef VI_MOVE
if (el->el_line.cursor > el->el_line.buffer)
el->el_line.cursor--;
1994-05-06 10:01:42 +04:00
#endif
return (CC_CURSOR);
1994-05-06 10:01:42 +04:00
}
1994-05-06 10:01:42 +04:00
/* vi_zero():
1999-07-02 19:14:07 +04:00
* Vi move to the beginning of line
1994-05-06 10:01:42 +04:00
* [0]
*/
protected el_action_t
vi_zero(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_state.doingarg) {
if (el->el_state.argument > 1000000)
return (CC_ERROR);
el->el_state.argument =
(el->el_state.argument * 10) + (c - '0');
return (CC_ARGHACK);
} else {
el->el_line.cursor = el->el_line.buffer;
if (el->el_chared.c_vcmd.action & DELETE) {
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
}
1994-05-06 10:01:42 +04:00
}
/* vi_delete_prev_char():
1999-07-02 19:14:07 +04:00
* Vi move to previous character (backspace)
* [^H] in insert mode only
1999-07-02 19:14:07 +04:00
*/
1994-05-06 10:01:42 +04:00
protected el_action_t
/*ARGSUSED*/
vi_delete_prev_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char *cp;
1994-05-06 10:01:42 +04:00
cp = el->el_line.cursor;
if (cp <= el->el_line.buffer)
return (CC_ERROR);
1994-05-06 10:01:42 +04:00
/* do the delete here so we dont mess up the undo and paste buffers */
el->el_line.cursor = --cp;
for (; cp < el->el_line.lastchar; cp++)
cp[0] = cp[1];
el->el_line.lastchar = cp - 1;
return (CC_REFRESH);
}
1994-05-06 10:01:42 +04:00
/* vi_list_or_eof():
* Vi list choices for completion or indicate end of file if empty line
* [^D]
*/
protected el_action_t
/*ARGSUSED*/
vi_list_or_eof(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
1994-05-06 10:01:42 +04:00
#ifdef notyet
if (el->el_line.cursor == el->el_line.lastchar &&
el->el_line.cursor == el->el_line.buffer) {
1994-05-06 10:01:42 +04:00
#endif
term_overwrite(el, STReof, 4); /* then do a EOF */
term__flush();
return (CC_EOF);
1994-05-06 10:01:42 +04:00
#ifdef notyet
} else {
re_goto_bottom(el);
*el->el_line.lastchar = '\0'; /* just in case */
return (CC_LIST_CHOICES);
}
1994-05-06 10:01:42 +04:00
#endif
}
/* vi_kill_line_prev():
1999-07-02 19:14:07 +04:00
* Vi cut from beginning of line to cursor
1994-05-06 10:01:42 +04:00
* [^U]
*/
protected el_action_t
/*ARGSUSED*/
vi_kill_line_prev(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char *kp, *cp;
1994-05-06 10:01:42 +04:00
cp = el->el_line.buffer;
kp = el->el_chared.c_kill.buf;
while (cp < el->el_line.cursor)
*kp++ = *cp++; /* copy it */
el->el_chared.c_kill.last = kp;
c_delbefore(el, el->el_line.cursor - el->el_line.buffer);
el->el_line.cursor = el->el_line.buffer; /* zap! */
return (CC_REFRESH);
1994-05-06 10:01:42 +04:00
}
/* vi_search_prev():
* Vi search history previous
* [?]
*/
protected el_action_t
/*ARGSUSED*/
vi_search_prev(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
return (cv_search(el, ED_SEARCH_PREV_HISTORY));
1994-05-06 10:01:42 +04:00
}
/* vi_search_next():
* Vi search history next
* [/]
*/
protected el_action_t
/*ARGSUSED*/
vi_search_next(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
return (cv_search(el, ED_SEARCH_NEXT_HISTORY));
1994-05-06 10:01:42 +04:00
}
/* vi_repeat_search_next():
* Vi repeat current search in the same search direction
* [n]
*/
protected el_action_t
/*ARGSUSED*/
vi_repeat_search_next(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_search.patlen == 0)
return (CC_ERROR);
else
return (cv_repeat_srch(el, el->el_search.patdir));
1994-05-06 10:01:42 +04:00
}
/* vi_repeat_search_prev():
* Vi repeat current search in the opposite search direction
* [N]
*/
/*ARGSUSED*/
protected el_action_t
vi_repeat_search_prev(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_search.patlen == 0)
return (CC_ERROR);
else
return (cv_repeat_srch(el,
el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
1994-05-06 10:01:42 +04:00
}
/* vi_next_char():
* Vi move to the character specified next
* [f]
*/
protected el_action_t
/*ARGSUSED*/
vi_next_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char ch;
1994-05-06 10:01:42 +04:00
if (el_getc(el, &ch) != 1)
return (ed_end_of_file(el, 0));
1994-05-06 10:01:42 +04:00
el->el_search.chadir = CHAR_FWD;
el->el_search.chacha = ch;
1994-05-06 10:01:42 +04:00
return (cv_csearch_fwd(el, ch, el->el_state.argument, 0));
1994-05-06 10:01:42 +04:00
}
/* vi_prev_char():
* Vi move to the character specified previous
* [F]
*/
protected el_action_t
/*ARGSUSED*/
vi_prev_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char ch;
1994-05-06 10:01:42 +04:00
if (el_getc(el, &ch) != 1)
return (ed_end_of_file(el, 0));
1994-05-06 10:01:42 +04:00
el->el_search.chadir = CHAR_BACK;
el->el_search.chacha = ch;
1994-05-06 10:01:42 +04:00
return (cv_csearch_back(el, ch, el->el_state.argument, 0));
1994-05-06 10:01:42 +04:00
}
/* vi_to_next_char():
* Vi move up to the character specified next
* [t]
*/
protected el_action_t
/*ARGSUSED*/
vi_to_next_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char ch;
1994-05-06 10:01:42 +04:00
if (el_getc(el, &ch) != 1)
return (ed_end_of_file(el, 0));
1994-05-06 10:01:42 +04:00
return (cv_csearch_fwd(el, ch, el->el_state.argument, 1));
1994-05-06 10:01:42 +04:00
}
/* vi_to_prev_char():
* Vi move up to the character specified previous
* [T]
*/
protected el_action_t
/*ARGSUSED*/
vi_to_prev_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
char ch;
if (el_getc(el, &ch) != 1)
return (ed_end_of_file(el, 0));
1994-05-06 10:01:42 +04:00
return (cv_csearch_back(el, ch, el->el_state.argument, 1));
1994-05-06 10:01:42 +04:00
}
/* vi_repeat_next_char():
* Vi repeat current character search in the same search direction
* [;]
*/
protected el_action_t
/*ARGSUSED*/
vi_repeat_next_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_search.chacha == 0)
return (CC_ERROR);
return (el->el_search.chadir == CHAR_FWD
? cv_csearch_fwd(el, el->el_search.chacha,
el->el_state.argument, 0)
: cv_csearch_back(el, el->el_search.chacha,
el->el_state.argument, 0));
1994-05-06 10:01:42 +04:00
}
/* vi_repeat_prev_char():
* Vi repeat current character search in the opposite search direction
* [,]
*/
protected el_action_t
/*ARGSUSED*/
vi_repeat_prev_char(EditLine *el, int c)
1994-05-06 10:01:42 +04:00
{
if (el->el_search.chacha == 0)
return (CC_ERROR);
return el->el_search.chadir == CHAR_BACK ?
cv_csearch_fwd(el, el->el_search.chacha, el->el_state.argument, 0) :
cv_csearch_back(el, el->el_search.chacha, el->el_state.argument, 0);
1994-05-06 10:01:42 +04:00
}
/* vi_match():
* Vi go to matching () {} or []
* [%]
*/
protected el_action_t
/*ARGSUSED*/
vi_match(EditLine *el, int c)
{
const char match_chars[] = "()[]{}";
char *cp;
int delta, i, count;
char o_ch, c_ch;
*el->el_line.lastchar = '\0'; /* just in case */
i = strcspn(el->el_line.cursor, match_chars);
o_ch = el->el_line.cursor[i];
if (o_ch == 0)
return CC_ERROR;
delta = strchr(match_chars, o_ch) - match_chars;
c_ch = match_chars[delta ^ 1];
count = 1;
delta = 1 - (delta & 1) * 2;
for (cp = &el->el_line.cursor[i]; count; ) {
cp += delta;
if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
return CC_ERROR;
if (*cp == o_ch)
count++;
else if (*cp == c_ch)
count--;
}
el->el_line.cursor = cp;
if (el->el_chared.c_vcmd.action & DELETE) {
if (delta > 0)
el->el_line.cursor++;
else
el->el_chared.c_vcmd.pos++;
cv_delfini(el);
return (CC_REFRESH);
}
return (CC_CURSOR);
}