/*-
* 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. 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.
*/
/* cv_paste():
* Paste previous deletion before or after the cursor
*/
static el_action_t
cv_paste(EditLine *el, wint_t c)
{
c_kill_t *k = &el->el_chared.c_kill;
size_t len = (size_t)(k->last - k->buf);
if (k->buf == NULL || len == 0)
return CC_ERROR;
#ifdef DEBUG_PASTE
(void) fprintf(el->el_errfile, "Paste: \"%.*ls\"\n", (int)len,
k->buf);
#endif
cv_undo(el);
if (!c && el->el_line.cursor < el->el_line.lastchar)
el->el_line.cursor++;
c_insert(el, (int)len);
if (el->el_line.cursor + len > el->el_line.lastchar)
return CC_ERROR;
(void) memcpy(el->el_line.cursor, k->buf, len *
sizeof(*el->el_line.cursor));
return CC_REFRESH;
}
/* vi_paste_next():
* Vi paste previous deletion to the right of the cursor
* [p]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_paste_next(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_paste(el, 0);
}
/* vi_paste_prev():
* Vi paste previous deletion to the left of the cursor
* [P]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_paste_prev(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_paste(el, 1);
}
/* vi_prev_big_word():
* Vi move to the previous space delimited word
* [B]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_prev_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor == el->el_line.buffer)
return CC_ERROR;
/* vi_prev_word():
* Vi move to the previous word
* [b]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor == el->el_line.buffer)
return CC_ERROR;
/* vi_next_big_word():
* Vi move to the next space delimited word
* [W]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_next_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor >= el->el_line.lastchar - 1)
return CC_ERROR;
if (el->el_map.type == MAP_VI)
if (el->el_chared.c_vcmd.action != NOP) {
cv_delfini(el);
return CC_REFRESH;
}
return CC_CURSOR;
}
/* vi_next_word():
* Vi move to the next word
* [w]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_next_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor >= el->el_line.lastchar - 1)
return CC_ERROR;
if (el->el_map.type == MAP_VI)
if (el->el_chared.c_vcmd.action != NOP) {
cv_delfini(el);
return CC_REFRESH;
}
return CC_CURSOR;
}
/* vi_change_case():
* Vi change case of character under the cursor and advance one character
* [~]
*/
libedit_private el_action_t
vi_change_case(EditLine *el, wint_t c)
{
int i;
if (el->el_line.cursor >= el->el_line.lastchar)
return CC_ERROR;
cv_undo(el);
for (i = 0; i < el->el_state.argument; i++) {
c = *el->el_line.cursor;
if (iswupper(c))
*el->el_line.cursor = towlower(c);
else if (iswlower(c))
*el->el_line.cursor = towupper(c);
/* vi_change_meta():
* Vi change prefix command
* [c]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_change_meta(EditLine *el, wint_t c __attribute__((__unused__)))
{
/*
* Delete with insert == change: first we delete and then we leave in
* insert mode.
*/
return cv_action(el, DELETE | INSERT);
}
/* vi_insert_at_bol():
* Vi enter insert mode at the beginning of line
* [I]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_insert_at_bol(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_replace_char():
* Vi replace character under the cursor with the next character typed
* [r]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_replace_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor >= el->el_line.lastchar)
return CC_ERROR;
/* vi_substitute_char():
* Vi replace character under the cursor and enter insert mode
* [s]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_substitute_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_change_to_eol():
* Vi change to end of line
* [C]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_change_to_eol(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_add():
* Vi enter insert mode after the cursor
* [a]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_add(EditLine *el, wint_t c __attribute__((__unused__)))
{
int ret;
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;
cv_undo(el);
return (el_action_t)ret;
}
/* vi_add_at_eol():
* Vi enter insert mode at end of line
* [A]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_add_at_eol(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_delete_meta():
* Vi delete prefix command
* [d]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_delete_meta(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_action(el, DELETE);
}
/* vi_end_big_word():
* Vi move to the end of the current space delimited word
* [E]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_end_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor == el->el_line.lastchar)
return CC_ERROR;
/* vi_end_word():
* Vi move to the end of the current word
* [e]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_end_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor == el->el_line.lastchar)
return CC_ERROR;
/* vi_delete_prev_char():
* Vi move to previous character (backspace)
* [^H] in insert mode only
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
if (el->el_line.cursor <= el->el_line.buffer)
return CC_ERROR;
/* vi_list_or_eof():
* Vi list choices for completion or indicate end of file if empty line
* [^D]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_list_or_eof(EditLine *el, wint_t c)
{
if (el->el_line.cursor == el->el_line.lastchar) {
if (el->el_line.cursor == el->el_line.buffer) {
terminal_writec(el, c); /* then do a EOF */
return CC_EOF;
} else {
/*
* Here we could list completions, but it is an
* error right now
*/
terminal_beep(el);
return CC_ERROR;
}
} else {
#ifdef notyet
re_goto_bottom(el);
*el->el_line.lastchar = '\0'; /* just in case */
return CC_LIST_CHOICES;
#else
/*
* Just complain for now.
*/
terminal_beep(el);
return CC_ERROR;
#endif
}
}
/* vi_kill_line_prev():
* Vi cut from beginning of line to cursor
* [^U]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_kill_line_prev(EditLine *el, wint_t c __attribute__((__unused__)))
{
wchar_t *kp, *cp;
/* vi_search_prev():
* Vi search history previous
* [?]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_search(el, ED_SEARCH_PREV_HISTORY);
}
/* vi_search_next():
* Vi search history next
* [/]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_search(el, ED_SEARCH_NEXT_HISTORY);
}
/* vi_repeat_search_next():
* Vi repeat current search in the same search direction
* [n]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_repeat_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_repeat_search_prev():
* Vi repeat current search in the opposite search direction
* [N]
*/
/*ARGSUSED*/
libedit_private el_action_t
vi_repeat_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_next_char():
* Vi move to the character specified next
* [f]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
}
/* vi_prev_char():
* Vi move to the character specified previous
* [F]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
}
/* vi_to_next_char():
* Vi move up to the character specified next
* [t]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_to_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
}
/* vi_to_prev_char():
* Vi move up to the character specified previous
* [T]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_to_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
}
/* vi_repeat_next_char():
* Vi repeat current character search in the same search direction
* [;]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_repeat_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_repeat_prev_char():
* Vi repeat current character search in the opposite search direction
* [,]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_repeat_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
{
el_action_t r;
int dir = el->el_search.chadir;
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 != NOP) {
/* NB posix says char under cursor should NOT be deleted
for -ve delta - this is different to netbsd vi. */
if (delta > 0)
el->el_line.cursor++;
cv_delfini(el);
return CC_REFRESH;
}
return CC_CURSOR;
}
/* vi_undo_line():
* Vi undo all changes to line
* [U]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_undo_line(EditLine *el, wint_t c __attribute__((__unused__)))
{
cv_undo(el);
return hist_get(el);
}
/* vi_to_column():
* Vi go to specified column
* [|]
* NB netbsd vi goes to screen column 'n', posix says nth character
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_to_column(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_yank_end():
* Vi yank to end of line
* [Y]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_yank_end(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_yank():
* Vi yank
* [y]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_yank(EditLine *el, wint_t c __attribute__((__unused__)))
{
return cv_action(el, YANK);
}
/* vi_comment_out():
* Vi comment out current command
* [#]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_comment_out(EditLine *el, wint_t c __attribute__((__unused__)))
{
/* vi_alias():
* Vi include shell alias
* [@]
* NB: posix implies that we should enter insert mode, however
* this is against historical precedent...
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_alias(EditLine *el, wint_t c __attribute__((__unused__)))
{
char alias_name[3];
const char *alias_text;
if (el->el_chared.c_aliasfun == NULL)
return CC_ERROR;
/* vi_to_history_line():
* Vi go to specified history file line.
* [G]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_to_history_line(EditLine *el, wint_t c __attribute__((__unused__)))
{
int sv_event_no = el->el_history.eventno;
el_action_t rval;
/* Lack of a 'count' means oldest, not 1 */
if (!el->el_state.doingarg) {
el->el_history.eventno = 0x7fffffff;
hist_get(el);
} else {
/* This is brain dead, all the rest of this code counts
* upwards going into the past. Here we need count in the
* other direction (to match the output of fc -l).
* I could change the world, but this seems to suffice.
*/
el->el_history.eventno = 1;
if (hist_get(el) == CC_ERROR)
return CC_ERROR;
el->el_history.eventno = 1 + el->el_history.ev.num
- el->el_state.argument;
if (el->el_history.eventno < 0) {
el->el_history.eventno = sv_event_no;
return CC_ERROR;
}
}
rval = hist_get(el);
if (rval == CC_ERROR)
el->el_history.eventno = sv_event_no;
return rval;
}
/* vi_histedit():
* Vi edit history line with vi
* [v]
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_histedit(EditLine *el, wint_t c __attribute__((__unused__)))
{
int fd;
pid_t pid;
ssize_t st;
int status;
char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
char *cp = NULL;
size_t len;
wchar_t *line = NULL;
const char *editor;
if (el->el_state.doingarg) {
if (vi_to_history_line(el, 0) == CC_ERROR)
return CC_ERROR;
}
if ((editor = getenv("EDITOR")) == NULL)
editor = "vi";
fd = mkstemp(tempfile);
if (fd < 0)
return CC_ERROR;
len = (size_t)(el->el_line.lastchar - el->el_line.buffer);
#define TMP_BUFSIZ (EL_BUFSIZ * MB_LEN_MAX)
cp = el_calloc(TMP_BUFSIZ, sizeof(*cp));
if (cp == NULL)
goto error;
line = el_calloc(len + 1, sizeof(*line));
if (line == NULL)
goto error;
wcsncpy(line, el->el_line.buffer, len);
line[len] = '\0';
wcstombs(cp, line, TMP_BUFSIZ - 1);
cp[TMP_BUFSIZ - 1] = '\0';
len = strlen(cp);
write(fd, cp, len);
write(fd, "\n", (size_t)1);
pid = fork();
switch (pid) {
case -1:
goto error;
case 0:
close(fd);
execlp(editor, editor, tempfile, (char *)NULL);
exit(0);
/*NOTREACHED*/
default:
while (waitpid(pid, &status, 0) != pid)
continue;
lseek(fd, (off_t)0, SEEK_SET);
st = read(fd, cp, TMP_BUFSIZ - 1);
if (st > 0) {
cp[st] = '\0';
len = (size_t)(el->el_line.limit - el->el_line.buffer);
len = mbstowcs(el->el_line.buffer, cp, len);
if (len > 0 && el->el_line.buffer[len - 1] == '\n')
--len;
}
else
len = 0;
el->el_line.cursor = el->el_line.buffer;
el->el_line.lastchar = el->el_line.buffer + len;
el_free(cp);
el_free(line);
break;
}
/* vi_history_word():
* Vi append word from previous input line
* [_]
* Who knows where this one came from!
* '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
*/
libedit_private el_action_t
/*ARGSUSED*/
vi_history_word(EditLine *el, wint_t c __attribute__((__unused__)))
{
const wchar_t *wp = HIST_FIRST(el);
const wchar_t *wep, *wsp;
int len;
wchar_t *cp;
const wchar_t *lim;
if (wp == NULL)
return CC_ERROR;
wep = wsp = NULL;
do {
while (iswspace(*wp))
wp++;
if (*wp == 0)
break;
wsp = wp;
while (*wp && !iswspace(*wp))
wp++;
wep = wp;
} while ((!el->el_state.doingarg || --el->el_state.argument > 0)
&& *wp != 0);