Home | History | Annotate | Line # | Download | only in libedit
vi.c revision 1.62.16.1
      1  1.62.16.1    martin /*	$NetBSD: vi.c,v 1.62.16.1 2020/04/13 08:03:12 martin 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.9  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[] = "@(#)vi.c	8.1 (Berkeley) 6/4/93";
     39        1.2     lukem #else
     40  1.62.16.1    martin __RCSID("$NetBSD: vi.c,v 1.62.16.1 2020/04/13 08:03:12 martin Exp $");
     41        1.2     lukem #endif
     42        1.1       cgd #endif /* not lint && not SCCSID */
     43        1.1       cgd 
     44        1.1       cgd /*
     45        1.1       cgd  * vi.c: Vi mode commands.
     46        1.1       cgd  */
     47       1.53  christos #include <sys/wait.h>
     48       1.53  christos #include <ctype.h>
     49       1.53  christos #include <limits.h>
     50       1.53  christos #include <stdlib.h>
     51       1.53  christos #include <string.h>
     52       1.53  christos #include <unistd.h>
     53       1.53  christos 
     54        1.1       cgd #include "el.h"
     55       1.52  christos #include "common.h"
     56       1.52  christos #include "emacs.h"
     57       1.61  christos #include "fcns.h"
     58       1.52  christos #include "vi.h"
     59        1.1       cgd 
     60       1.60  christos static el_action_t	cv_action(EditLine *, wint_t);
     61       1.60  christos static el_action_t	cv_paste(EditLine *, wint_t);
     62        1.1       cgd 
     63        1.1       cgd /* cv_action():
     64        1.1       cgd  *	Handle vi actions.
     65        1.1       cgd  */
     66       1.60  christos static el_action_t
     67       1.48  christos cv_action(EditLine *el, wint_t c)
     68        1.1       cgd {
     69        1.1       cgd 
     70       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
     71       1.12  christos 		/* 'cc', 'dd' and (possibly) friends */
     72       1.48  christos 		if (c != (wint_t)el->el_chared.c_vcmd.action)
     73       1.12  christos 			return CC_ERROR;
     74       1.12  christos 
     75       1.12  christos 		if (!(c & YANK))
     76       1.12  christos 			cv_undo(el);
     77       1.12  christos 		cv_yank(el, el->el_line.buffer,
     78       1.29  christos 		    (int)(el->el_line.lastchar - el->el_line.buffer));
     79        1.8     lukem 		el->el_chared.c_vcmd.action = NOP;
     80        1.8     lukem 		el->el_chared.c_vcmd.pos = 0;
     81       1.23  christos 		if (!(c & YANK)) {
     82       1.23  christos 			el->el_line.lastchar = el->el_line.buffer;
     83       1.23  christos 			el->el_line.cursor = el->el_line.buffer;
     84       1.23  christos 		}
     85        1.8     lukem 		if (c & INSERT)
     86        1.8     lukem 			el->el_map.current = el->el_map.key;
     87        1.7    simonb 
     88       1.37  christos 		return CC_REFRESH;
     89        1.1       cgd 	}
     90        1.8     lukem 	el->el_chared.c_vcmd.pos = el->el_line.cursor;
     91        1.8     lukem 	el->el_chared.c_vcmd.action = c;
     92       1.37  christos 	return CC_ARGHACK;
     93        1.1       cgd }
     94        1.1       cgd 
     95        1.1       cgd /* cv_paste():
     96        1.1       cgd  *	Paste previous deletion before or after the cursor
     97        1.1       cgd  */
     98       1.60  christos static el_action_t
     99       1.48  christos cv_paste(EditLine *el, wint_t c)
    100        1.1       cgd {
    101       1.12  christos 	c_kill_t *k = &el->el_chared.c_kill;
    102       1.30  christos 	size_t len = (size_t)(k->last - k->buf);
    103        1.8     lukem 
    104       1.12  christos 	if (k->buf == NULL || len == 0)
    105       1.37  christos 		return CC_ERROR;
    106        1.1       cgd #ifdef DEBUG_PASTE
    107       1.56  christos 	(void) fprintf(el->el_errfile, "Paste: \"%.*ls\"\n", (int)len,
    108       1.55  christos 	    k->buf);
    109        1.1       cgd #endif
    110        1.1       cgd 
    111       1.12  christos 	cv_undo(el);
    112       1.10  christos 
    113        1.8     lukem 	if (!c && el->el_line.cursor < el->el_line.lastchar)
    114        1.8     lukem 		el->el_line.cursor++;
    115        1.8     lukem 
    116       1.30  christos 	c_insert(el, (int)len);
    117       1.12  christos 	if (el->el_line.cursor + len > el->el_line.lastchar)
    118       1.37  christos 		return CC_ERROR;
    119       1.31  christos 	(void) memcpy(el->el_line.cursor, k->buf, len *
    120       1.31  christos 	    sizeof(*el->el_line.cursor));
    121       1.24  christos 
    122       1.37  christos 	return CC_REFRESH;
    123        1.1       cgd }
    124        1.1       cgd 
    125        1.1       cgd 
    126        1.7    simonb /* vi_paste_next():
    127        1.1       cgd  *	Vi paste previous deletion to the right of the cursor
    128        1.1       cgd  *	[p]
    129        1.1       cgd  */
    130       1.62  christos libedit_private el_action_t
    131        1.1       cgd /*ARGSUSED*/
    132       1.48  christos vi_paste_next(EditLine *el, wint_t c __attribute__((__unused__)))
    133        1.1       cgd {
    134        1.8     lukem 
    135       1.37  christos 	return cv_paste(el, 0);
    136        1.1       cgd }
    137        1.1       cgd 
    138        1.1       cgd 
    139        1.7    simonb /* vi_paste_prev():
    140        1.1       cgd  *	Vi paste previous deletion to the left of the cursor
    141        1.1       cgd  *	[P]
    142        1.1       cgd  */
    143       1.62  christos libedit_private el_action_t
    144        1.1       cgd /*ARGSUSED*/
    145       1.48  christos vi_paste_prev(EditLine *el, wint_t c __attribute__((__unused__)))
    146        1.1       cgd {
    147        1.8     lukem 
    148       1.37  christos 	return cv_paste(el, 1);
    149        1.1       cgd }
    150        1.1       cgd 
    151        1.1       cgd 
    152       1.10  christos /* vi_prev_big_word():
    153        1.1       cgd  *	Vi move to the previous space delimited word
    154        1.1       cgd  *	[B]
    155        1.1       cgd  */
    156       1.62  christos libedit_private el_action_t
    157        1.1       cgd /*ARGSUSED*/
    158       1.48  christos vi_prev_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
    159        1.8     lukem {
    160        1.8     lukem 
    161        1.8     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    162       1.37  christos 		return CC_ERROR;
    163        1.1       cgd 
    164       1.10  christos 	el->el_line.cursor = cv_prev_word(el->el_line.cursor,
    165        1.8     lukem 	    el->el_line.buffer,
    166        1.8     lukem 	    el->el_state.argument,
    167       1.10  christos 	    cv__isWord);
    168        1.8     lukem 
    169       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    170        1.8     lukem 		cv_delfini(el);
    171       1.37  christos 		return CC_REFRESH;
    172        1.8     lukem 	}
    173       1.37  christos 	return CC_CURSOR;
    174        1.1       cgd }
    175        1.1       cgd 
    176        1.1       cgd 
    177        1.7    simonb /* vi_prev_word():
    178        1.1       cgd  *	Vi move to the previous word
    179       1.10  christos  *	[b]
    180        1.1       cgd  */
    181       1.62  christos libedit_private el_action_t
    182        1.1       cgd /*ARGSUSED*/
    183       1.48  christos vi_prev_word(EditLine *el, wint_t c __attribute__((__unused__)))
    184        1.8     lukem {
    185        1.1       cgd 
    186        1.8     lukem 	if (el->el_line.cursor == el->el_line.buffer)
    187       1.37  christos 		return CC_ERROR;
    188        1.8     lukem 
    189       1.10  christos 	el->el_line.cursor = cv_prev_word(el->el_line.cursor,
    190        1.8     lukem 	    el->el_line.buffer,
    191        1.8     lukem 	    el->el_state.argument,
    192       1.10  christos 	    cv__isword);
    193        1.8     lukem 
    194       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    195        1.8     lukem 		cv_delfini(el);
    196       1.37  christos 		return CC_REFRESH;
    197        1.8     lukem 	}
    198       1.37  christos 	return CC_CURSOR;
    199        1.1       cgd }
    200        1.1       cgd 
    201        1.1       cgd 
    202       1.10  christos /* vi_next_big_word():
    203        1.1       cgd  *	Vi move to the next space delimited word
    204        1.1       cgd  *	[W]
    205        1.1       cgd  */
    206       1.62  christos libedit_private el_action_t
    207        1.1       cgd /*ARGSUSED*/
    208       1.48  christos vi_next_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
    209        1.8     lukem {
    210        1.1       cgd 
    211       1.12  christos 	if (el->el_line.cursor >= el->el_line.lastchar - 1)
    212       1.37  christos 		return CC_ERROR;
    213        1.1       cgd 
    214        1.8     lukem 	el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
    215       1.12  christos 	    el->el_line.lastchar, el->el_state.argument, cv__isWord);
    216        1.8     lukem 
    217        1.8     lukem 	if (el->el_map.type == MAP_VI)
    218       1.12  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    219        1.8     lukem 			cv_delfini(el);
    220       1.37  christos 			return CC_REFRESH;
    221        1.8     lukem 		}
    222       1.37  christos 	return CC_CURSOR;
    223        1.1       cgd }
    224        1.1       cgd 
    225        1.8     lukem 
    226        1.7    simonb /* vi_next_word():
    227        1.1       cgd  *	Vi move to the next word
    228        1.1       cgd  *	[w]
    229        1.1       cgd  */
    230       1.62  christos libedit_private el_action_t
    231        1.1       cgd /*ARGSUSED*/
    232       1.48  christos vi_next_word(EditLine *el, wint_t c __attribute__((__unused__)))
    233        1.8     lukem {
    234        1.1       cgd 
    235       1.12  christos 	if (el->el_line.cursor >= el->el_line.lastchar - 1)
    236       1.37  christos 		return CC_ERROR;
    237        1.1       cgd 
    238        1.8     lukem 	el->el_line.cursor = cv_next_word(el, el->el_line.cursor,
    239       1.12  christos 	    el->el_line.lastchar, el->el_state.argument, cv__isword);
    240        1.8     lukem 
    241        1.8     lukem 	if (el->el_map.type == MAP_VI)
    242       1.12  christos 		if (el->el_chared.c_vcmd.action != NOP) {
    243        1.8     lukem 			cv_delfini(el);
    244       1.37  christos 			return CC_REFRESH;
    245        1.8     lukem 		}
    246       1.37  christos 	return CC_CURSOR;
    247        1.1       cgd }
    248        1.1       cgd 
    249        1.1       cgd 
    250        1.7    simonb /* vi_change_case():
    251        1.1       cgd  *	Vi change case of character under the cursor and advance one character
    252        1.1       cgd  *	[~]
    253        1.1       cgd  */
    254       1.62  christos libedit_private el_action_t
    255       1.48  christos vi_change_case(EditLine *el, wint_t c)
    256        1.8     lukem {
    257       1.10  christos 	int i;
    258       1.10  christos 
    259       1.10  christos 	if (el->el_line.cursor >= el->el_line.lastchar)
    260       1.37  christos 		return CC_ERROR;
    261       1.12  christos 	cv_undo(el);
    262       1.10  christos 	for (i = 0; i < el->el_state.argument; i++) {
    263        1.8     lukem 
    264       1.31  christos 		c = *el->el_line.cursor;
    265       1.56  christos 		if (iswupper(c))
    266       1.56  christos 			*el->el_line.cursor = towlower(c);
    267       1.56  christos 		else if (iswlower(c))
    268       1.56  christos 			*el->el_line.cursor = towupper(c);
    269       1.12  christos 
    270       1.12  christos 		if (++el->el_line.cursor >= el->el_line.lastchar) {
    271       1.12  christos 			el->el_line.cursor--;
    272       1.12  christos 			re_fastaddc(el);
    273       1.12  christos 			break;
    274       1.12  christos 		}
    275        1.8     lukem 		re_fastaddc(el);
    276        1.8     lukem 	}
    277       1.12  christos 	return CC_NORM;
    278        1.1       cgd }
    279        1.1       cgd 
    280        1.1       cgd 
    281        1.7    simonb /* vi_change_meta():
    282        1.1       cgd  *	Vi change prefix command
    283        1.1       cgd  *	[c]
    284        1.1       cgd  */
    285       1.62  christos libedit_private el_action_t
    286        1.1       cgd /*ARGSUSED*/
    287       1.48  christos vi_change_meta(EditLine *el, wint_t c __attribute__((__unused__)))
    288        1.8     lukem {
    289        1.8     lukem 
    290        1.8     lukem 	/*
    291        1.8     lukem          * Delete with insert == change: first we delete and then we leave in
    292        1.8     lukem          * insert mode.
    293        1.8     lukem          */
    294       1.37  christos 	return cv_action(el, DELETE | INSERT);
    295        1.1       cgd }
    296        1.1       cgd 
    297        1.1       cgd 
    298        1.7    simonb /* vi_insert_at_bol():
    299        1.1       cgd  *	Vi enter insert mode at the beginning of line
    300        1.1       cgd  *	[I]
    301        1.1       cgd  */
    302       1.62  christos libedit_private el_action_t
    303        1.1       cgd /*ARGSUSED*/
    304       1.48  christos vi_insert_at_bol(EditLine *el, wint_t c __attribute__((__unused__)))
    305        1.1       cgd {
    306        1.1       cgd 
    307        1.8     lukem 	el->el_line.cursor = el->el_line.buffer;
    308       1.12  christos 	cv_undo(el);
    309        1.8     lukem 	el->el_map.current = el->el_map.key;
    310       1.37  christos 	return CC_CURSOR;
    311        1.1       cgd }
    312        1.1       cgd 
    313        1.1       cgd 
    314        1.7    simonb /* vi_replace_char():
    315        1.1       cgd  *	Vi replace character under the cursor with the next character typed
    316        1.1       cgd  *	[r]
    317        1.1       cgd  */
    318       1.62  christos libedit_private el_action_t
    319        1.1       cgd /*ARGSUSED*/
    320       1.48  christos vi_replace_char(EditLine *el, wint_t c __attribute__((__unused__)))
    321        1.8     lukem {
    322        1.8     lukem 
    323       1.12  christos 	if (el->el_line.cursor >= el->el_line.lastchar)
    324       1.10  christos 		return CC_ERROR;
    325       1.10  christos 
    326        1.8     lukem 	el->el_map.current = el->el_map.key;
    327        1.8     lukem 	el->el_state.inputmode = MODE_REPLACE_1;
    328       1.12  christos 	cv_undo(el);
    329       1.37  christos 	return CC_ARGHACK;
    330        1.1       cgd }
    331        1.1       cgd 
    332        1.1       cgd 
    333        1.7    simonb /* vi_replace_mode():
    334        1.1       cgd  *	Vi enter replace mode
    335        1.1       cgd  *	[R]
    336        1.1       cgd  */
    337       1.62  christos libedit_private el_action_t
    338        1.1       cgd /*ARGSUSED*/
    339       1.48  christos vi_replace_mode(EditLine *el, wint_t c __attribute__((__unused__)))
    340        1.8     lukem {
    341        1.8     lukem 
    342        1.8     lukem 	el->el_map.current = el->el_map.key;
    343        1.8     lukem 	el->el_state.inputmode = MODE_REPLACE;
    344       1.12  christos 	cv_undo(el);
    345       1.37  christos 	return CC_NORM;
    346        1.1       cgd }
    347        1.1       cgd 
    348        1.1       cgd 
    349        1.7    simonb /* vi_substitute_char():
    350        1.1       cgd  *	Vi replace character under the cursor and enter insert mode
    351       1.10  christos  *	[s]
    352        1.1       cgd  */
    353       1.62  christos libedit_private el_action_t
    354        1.1       cgd /*ARGSUSED*/
    355       1.48  christos vi_substitute_char(EditLine *el, wint_t c __attribute__((__unused__)))
    356        1.8     lukem {
    357        1.8     lukem 
    358        1.8     lukem 	c_delafter(el, el->el_state.argument);
    359        1.8     lukem 	el->el_map.current = el->el_map.key;
    360       1.37  christos 	return CC_REFRESH;
    361        1.1       cgd }
    362        1.1       cgd 
    363        1.1       cgd 
    364        1.7    simonb /* vi_substitute_line():
    365        1.1       cgd  *	Vi substitute entire line
    366        1.1       cgd  *	[S]
    367        1.1       cgd  */
    368       1.62  christos libedit_private el_action_t
    369        1.1       cgd /*ARGSUSED*/
    370       1.48  christos vi_substitute_line(EditLine *el, wint_t c __attribute__((__unused__)))
    371        1.8     lukem {
    372        1.8     lukem 
    373       1.12  christos 	cv_undo(el);
    374       1.12  christos 	cv_yank(el, el->el_line.buffer,
    375       1.29  christos 	    (int)(el->el_line.lastchar - el->el_line.buffer));
    376        1.8     lukem 	(void) em_kill_line(el, 0);
    377        1.8     lukem 	el->el_map.current = el->el_map.key;
    378       1.37  christos 	return CC_REFRESH;
    379        1.1       cgd }
    380        1.1       cgd 
    381        1.1       cgd 
    382        1.7    simonb /* vi_change_to_eol():
    383        1.1       cgd  *	Vi change to end of line
    384        1.1       cgd  *	[C]
    385        1.1       cgd  */
    386       1.62  christos libedit_private el_action_t
    387        1.1       cgd /*ARGSUSED*/
    388       1.48  christos vi_change_to_eol(EditLine *el, wint_t c __attribute__((__unused__)))
    389        1.8     lukem {
    390        1.8     lukem 
    391       1.12  christos 	cv_undo(el);
    392       1.12  christos 	cv_yank(el, el->el_line.cursor,
    393       1.29  christos 	    (int)(el->el_line.lastchar - el->el_line.cursor));
    394        1.8     lukem 	(void) ed_kill_line(el, 0);
    395        1.8     lukem 	el->el_map.current = el->el_map.key;
    396       1.37  christos 	return CC_REFRESH;
    397        1.1       cgd }
    398        1.1       cgd 
    399        1.1       cgd 
    400        1.1       cgd /* vi_insert():
    401        1.1       cgd  *	Vi enter insert mode
    402        1.1       cgd  *	[i]
    403        1.1       cgd  */
    404       1.62  christos libedit_private el_action_t
    405        1.1       cgd /*ARGSUSED*/
    406       1.48  christos vi_insert(EditLine *el, wint_t c __attribute__((__unused__)))
    407        1.1       cgd {
    408        1.1       cgd 
    409        1.8     lukem 	el->el_map.current = el->el_map.key;
    410       1.12  christos 	cv_undo(el);
    411       1.37  christos 	return CC_NORM;
    412        1.1       cgd }
    413        1.1       cgd 
    414        1.1       cgd 
    415        1.1       cgd /* vi_add():
    416        1.7    simonb  *	Vi enter insert mode after the cursor
    417        1.1       cgd  *	[a]
    418        1.1       cgd  */
    419       1.62  christos libedit_private el_action_t
    420        1.1       cgd /*ARGSUSED*/
    421       1.48  christos vi_add(EditLine *el, wint_t c __attribute__((__unused__)))
    422        1.8     lukem {
    423        1.8     lukem 	int ret;
    424        1.8     lukem 
    425        1.8     lukem 	el->el_map.current = el->el_map.key;
    426        1.8     lukem 	if (el->el_line.cursor < el->el_line.lastchar) {
    427        1.8     lukem 		el->el_line.cursor++;
    428        1.8     lukem 		if (el->el_line.cursor > el->el_line.lastchar)
    429        1.8     lukem 			el->el_line.cursor = el->el_line.lastchar;
    430        1.8     lukem 		ret = CC_CURSOR;
    431        1.8     lukem 	} else
    432        1.8     lukem 		ret = CC_NORM;
    433        1.8     lukem 
    434       1.12  christos 	cv_undo(el);
    435        1.1       cgd 
    436       1.40  christos 	return (el_action_t)ret;
    437        1.1       cgd }
    438        1.1       cgd 
    439        1.1       cgd 
    440        1.1       cgd /* vi_add_at_eol():
    441        1.1       cgd  *	Vi enter insert mode at end of line
    442        1.1       cgd  *	[A]
    443        1.1       cgd  */
    444       1.62  christos libedit_private el_action_t
    445        1.1       cgd /*ARGSUSED*/
    446       1.48  christos vi_add_at_eol(EditLine *el, wint_t c __attribute__((__unused__)))
    447        1.8     lukem {
    448        1.8     lukem 
    449        1.8     lukem 	el->el_map.current = el->el_map.key;
    450        1.8     lukem 	el->el_line.cursor = el->el_line.lastchar;
    451       1.12  christos 	cv_undo(el);
    452       1.37  christos 	return CC_CURSOR;
    453        1.1       cgd }
    454        1.1       cgd 
    455        1.1       cgd 
    456        1.1       cgd /* vi_delete_meta():
    457        1.7    simonb  *	Vi delete prefix command
    458        1.1       cgd  *	[d]
    459        1.1       cgd  */
    460       1.62  christos libedit_private el_action_t
    461        1.1       cgd /*ARGSUSED*/
    462       1.48  christos vi_delete_meta(EditLine *el, wint_t c __attribute__((__unused__)))
    463        1.1       cgd {
    464        1.8     lukem 
    465       1.37  christos 	return cv_action(el, DELETE);
    466        1.1       cgd }
    467        1.1       cgd 
    468        1.1       cgd 
    469       1.10  christos /* vi_end_big_word():
    470        1.7    simonb  *	Vi move to the end of the current space delimited word
    471        1.7    simonb  *	[E]
    472        1.1       cgd  */
    473       1.62  christos libedit_private el_action_t
    474        1.1       cgd /*ARGSUSED*/
    475       1.48  christos vi_end_big_word(EditLine *el, wint_t c __attribute__((__unused__)))
    476        1.8     lukem {
    477        1.8     lukem 
    478        1.8     lukem 	if (el->el_line.cursor == el->el_line.lastchar)
    479       1.37  christos 		return CC_ERROR;
    480        1.1       cgd 
    481        1.8     lukem 	el->el_line.cursor = cv__endword(el->el_line.cursor,
    482       1.10  christos 	    el->el_line.lastchar, el->el_state.argument, cv__isWord);
    483        1.8     lukem 
    484       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    485        1.8     lukem 		el->el_line.cursor++;
    486        1.8     lukem 		cv_delfini(el);
    487       1.37  christos 		return CC_REFRESH;
    488        1.8     lukem 	}
    489       1.37  christos 	return CC_CURSOR;
    490        1.1       cgd }
    491        1.1       cgd 
    492        1.1       cgd 
    493       1.10  christos /* vi_end_word():
    494        1.1       cgd  *	Vi move to the end of the current word
    495        1.1       cgd  *	[e]
    496        1.1       cgd  */
    497       1.62  christos libedit_private el_action_t
    498        1.1       cgd /*ARGSUSED*/
    499       1.48  christos vi_end_word(EditLine *el, wint_t c __attribute__((__unused__)))
    500        1.8     lukem {
    501        1.8     lukem 
    502        1.8     lukem 	if (el->el_line.cursor == el->el_line.lastchar)
    503       1.37  christos 		return CC_ERROR;
    504        1.8     lukem 
    505        1.8     lukem 	el->el_line.cursor = cv__endword(el->el_line.cursor,
    506       1.10  christos 	    el->el_line.lastchar, el->el_state.argument, cv__isword);
    507        1.1       cgd 
    508       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    509        1.8     lukem 		el->el_line.cursor++;
    510        1.8     lukem 		cv_delfini(el);
    511       1.37  christos 		return CC_REFRESH;
    512        1.8     lukem 	}
    513       1.37  christos 	return CC_CURSOR;
    514        1.1       cgd }
    515        1.1       cgd 
    516        1.1       cgd 
    517        1.1       cgd /* vi_undo():
    518        1.1       cgd  *	Vi undo last change
    519        1.1       cgd  *	[u]
    520        1.1       cgd  */
    521       1.62  christos libedit_private el_action_t
    522        1.1       cgd /*ARGSUSED*/
    523       1.48  christos vi_undo(EditLine *el, wint_t c __attribute__((__unused__)))
    524        1.8     lukem {
    525       1.10  christos 	c_undo_t un = el->el_chared.c_undo;
    526        1.1       cgd 
    527       1.10  christos 	if (un.len == -1)
    528       1.10  christos 		return CC_ERROR;
    529        1.1       cgd 
    530       1.10  christos 	/* switch line buffer and undo buffer */
    531       1.10  christos 	el->el_chared.c_undo.buf = el->el_line.buffer;
    532       1.10  christos 	el->el_chared.c_undo.len = el->el_line.lastchar - el->el_line.buffer;
    533       1.29  christos 	el->el_chared.c_undo.cursor =
    534       1.29  christos 	    (int)(el->el_line.cursor - el->el_line.buffer);
    535       1.10  christos 	el->el_line.limit = un.buf + (el->el_line.limit - el->el_line.buffer);
    536       1.10  christos 	el->el_line.buffer = un.buf;
    537       1.10  christos 	el->el_line.cursor = un.buf + un.cursor;
    538       1.10  christos 	el->el_line.lastchar = un.buf + un.len;
    539        1.1       cgd 
    540       1.37  christos 	return CC_REFRESH;
    541        1.1       cgd }
    542        1.1       cgd 
    543        1.1       cgd 
    544        1.1       cgd /* vi_command_mode():
    545        1.1       cgd  *	Vi enter command mode (use alternative key bindings)
    546        1.1       cgd  *	[<ESC>]
    547        1.1       cgd  */
    548       1.62  christos libedit_private el_action_t
    549        1.1       cgd /*ARGSUSED*/
    550       1.48  christos vi_command_mode(EditLine *el, wint_t c __attribute__((__unused__)))
    551        1.8     lukem {
    552        1.8     lukem 
    553        1.8     lukem 	/* [Esc] cancels pending action */
    554        1.8     lukem 	el->el_chared.c_vcmd.action = NOP;
    555        1.8     lukem 	el->el_chared.c_vcmd.pos = 0;
    556        1.8     lukem 
    557        1.8     lukem 	el->el_state.doingarg = 0;
    558        1.1       cgd 
    559        1.8     lukem 	el->el_state.inputmode = MODE_INSERT;
    560        1.8     lukem 	el->el_map.current = el->el_map.alt;
    561        1.1       cgd #ifdef VI_MOVE
    562        1.8     lukem 	if (el->el_line.cursor > el->el_line.buffer)
    563        1.8     lukem 		el->el_line.cursor--;
    564        1.1       cgd #endif
    565       1.37  christos 	return CC_CURSOR;
    566        1.1       cgd }
    567        1.1       cgd 
    568        1.8     lukem 
    569        1.1       cgd /* vi_zero():
    570        1.7    simonb  *	Vi move to the beginning of line
    571        1.1       cgd  *	[0]
    572        1.1       cgd  */
    573       1.62  christos libedit_private el_action_t
    574       1.48  christos vi_zero(EditLine *el, wint_t c)
    575        1.8     lukem {
    576        1.8     lukem 
    577       1.12  christos 	if (el->el_state.doingarg)
    578       1.12  christos 		return ed_argument_digit(el, c);
    579       1.12  christos 
    580       1.12  christos 	el->el_line.cursor = el->el_line.buffer;
    581       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    582       1.12  christos 		cv_delfini(el);
    583       1.37  christos 		return CC_REFRESH;
    584        1.8     lukem 	}
    585       1.37  christos 	return CC_CURSOR;
    586        1.1       cgd }
    587        1.1       cgd 
    588        1.1       cgd 
    589        1.1       cgd /* vi_delete_prev_char():
    590       1.54  christos  *	Vi move to previous character (backspace)
    591       1.10  christos  *	[^H] in insert mode only
    592        1.7    simonb  */
    593       1.62  christos libedit_private el_action_t
    594        1.1       cgd /*ARGSUSED*/
    595       1.48  christos vi_delete_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    596        1.8     lukem {
    597        1.1       cgd 
    598       1.20   mycroft 	if (el->el_line.cursor <= el->el_line.buffer)
    599       1.37  christos 		return CC_ERROR;
    600        1.8     lukem 
    601       1.20   mycroft 	c_delbefore1(el);
    602       1.20   mycroft 	el->el_line.cursor--;
    603       1.37  christos 	return CC_REFRESH;
    604        1.8     lukem }
    605        1.1       cgd 
    606        1.1       cgd 
    607        1.1       cgd /* vi_list_or_eof():
    608        1.1       cgd  *	Vi list choices for completion or indicate end of file if empty line
    609        1.1       cgd  *	[^D]
    610        1.1       cgd  */
    611       1.62  christos libedit_private el_action_t
    612        1.1       cgd /*ARGSUSED*/
    613       1.48  christos vi_list_or_eof(EditLine *el, wint_t c)
    614        1.1       cgd {
    615        1.8     lukem 
    616       1.17      matt 	if (el->el_line.cursor == el->el_line.lastchar) {
    617       1.17      matt 		if (el->el_line.cursor == el->el_line.buffer) {
    618       1.35  christos 			terminal_writec(el, c);	/* then do a EOF */
    619       1.37  christos 			return CC_EOF;
    620       1.17      matt 		} else {
    621       1.17      matt 			/*
    622       1.17      matt 			 * Here we could list completions, but it is an
    623       1.17      matt 			 * error right now
    624       1.17      matt 			 */
    625       1.35  christos 			terminal_beep(el);
    626       1.37  christos 			return CC_ERROR;
    627       1.17      matt 		}
    628       1.17      matt 	} else {
    629        1.1       cgd #ifdef notyet
    630        1.8     lukem 		re_goto_bottom(el);
    631        1.8     lukem 		*el->el_line.lastchar = '\0';	/* just in case */
    632       1.37  christos 		return CC_LIST_CHOICES;
    633       1.17      matt #else
    634       1.17      matt 		/*
    635       1.17      matt 		 * Just complain for now.
    636       1.17      matt 		 */
    637       1.35  christos 		terminal_beep(el);
    638       1.37  christos 		return CC_ERROR;
    639       1.17      matt #endif
    640        1.8     lukem 	}
    641        1.1       cgd }
    642        1.1       cgd 
    643        1.1       cgd 
    644        1.1       cgd /* vi_kill_line_prev():
    645        1.7    simonb  *	Vi cut from beginning of line to cursor
    646        1.1       cgd  *	[^U]
    647        1.1       cgd  */
    648       1.62  christos libedit_private el_action_t
    649        1.1       cgd /*ARGSUSED*/
    650       1.48  christos vi_kill_line_prev(EditLine *el, wint_t c __attribute__((__unused__)))
    651        1.8     lukem {
    652       1.59  christos 	wchar_t *kp, *cp;
    653        1.8     lukem 
    654        1.8     lukem 	cp = el->el_line.buffer;
    655        1.8     lukem 	kp = el->el_chared.c_kill.buf;
    656        1.8     lukem 	while (cp < el->el_line.cursor)
    657        1.8     lukem 		*kp++ = *cp++;	/* copy it */
    658        1.8     lukem 	el->el_chared.c_kill.last = kp;
    659       1.29  christos 	c_delbefore(el, (int)(el->el_line.cursor - el->el_line.buffer));
    660        1.8     lukem 	el->el_line.cursor = el->el_line.buffer;	/* zap! */
    661       1.37  christos 	return CC_REFRESH;
    662        1.1       cgd }
    663        1.1       cgd 
    664        1.1       cgd 
    665        1.1       cgd /* vi_search_prev():
    666        1.1       cgd  *	Vi search history previous
    667        1.1       cgd  *	[?]
    668        1.1       cgd  */
    669       1.62  christos libedit_private el_action_t
    670        1.1       cgd /*ARGSUSED*/
    671       1.48  christos vi_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
    672        1.1       cgd {
    673        1.8     lukem 
    674       1.37  christos 	return cv_search(el, ED_SEARCH_PREV_HISTORY);
    675        1.1       cgd }
    676        1.1       cgd 
    677        1.1       cgd 
    678        1.1       cgd /* vi_search_next():
    679        1.1       cgd  *	Vi search history next
    680        1.1       cgd  *	[/]
    681        1.1       cgd  */
    682       1.62  christos libedit_private el_action_t
    683        1.1       cgd /*ARGSUSED*/
    684       1.48  christos vi_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
    685        1.1       cgd {
    686        1.8     lukem 
    687       1.37  christos 	return cv_search(el, ED_SEARCH_NEXT_HISTORY);
    688        1.1       cgd }
    689        1.1       cgd 
    690        1.1       cgd 
    691        1.1       cgd /* vi_repeat_search_next():
    692        1.1       cgd  *	Vi repeat current search in the same search direction
    693        1.1       cgd  *	[n]
    694        1.1       cgd  */
    695       1.62  christos libedit_private el_action_t
    696        1.1       cgd /*ARGSUSED*/
    697       1.48  christos vi_repeat_search_next(EditLine *el, wint_t c __attribute__((__unused__)))
    698        1.8     lukem {
    699        1.8     lukem 
    700        1.8     lukem 	if (el->el_search.patlen == 0)
    701       1.37  christos 		return CC_ERROR;
    702        1.8     lukem 	else
    703       1.37  christos 		return cv_repeat_srch(el, el->el_search.patdir);
    704        1.1       cgd }
    705        1.1       cgd 
    706        1.1       cgd 
    707        1.1       cgd /* vi_repeat_search_prev():
    708        1.1       cgd  *	Vi repeat current search in the opposite search direction
    709        1.1       cgd  *	[N]
    710        1.1       cgd  */
    711        1.1       cgd /*ARGSUSED*/
    712       1.62  christos libedit_private el_action_t
    713       1.48  christos vi_repeat_search_prev(EditLine *el, wint_t c __attribute__((__unused__)))
    714        1.8     lukem {
    715        1.8     lukem 
    716        1.8     lukem 	if (el->el_search.patlen == 0)
    717       1.37  christos 		return CC_ERROR;
    718        1.8     lukem 	else
    719        1.8     lukem 		return (cv_repeat_srch(el,
    720        1.8     lukem 		    el->el_search.patdir == ED_SEARCH_PREV_HISTORY ?
    721        1.8     lukem 		    ED_SEARCH_NEXT_HISTORY : ED_SEARCH_PREV_HISTORY));
    722        1.1       cgd }
    723        1.1       cgd 
    724        1.1       cgd 
    725        1.1       cgd /* vi_next_char():
    726        1.1       cgd  *	Vi move to the character specified next
    727        1.1       cgd  *	[f]
    728        1.1       cgd  */
    729       1.62  christos libedit_private el_action_t
    730        1.1       cgd /*ARGSUSED*/
    731       1.48  christos vi_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
    732        1.1       cgd {
    733       1.12  christos 	return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 0);
    734        1.1       cgd }
    735        1.1       cgd 
    736        1.1       cgd 
    737        1.1       cgd /* vi_prev_char():
    738        1.1       cgd  *	Vi move to the character specified previous
    739        1.1       cgd  *	[F]
    740        1.1       cgd  */
    741       1.62  christos libedit_private el_action_t
    742        1.1       cgd /*ARGSUSED*/
    743       1.48  christos vi_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    744        1.1       cgd {
    745       1.12  christos 	return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 0);
    746        1.1       cgd }
    747        1.1       cgd 
    748        1.1       cgd 
    749        1.1       cgd /* vi_to_next_char():
    750        1.1       cgd  *	Vi move up to the character specified next
    751        1.1       cgd  *	[t]
    752        1.1       cgd  */
    753       1.62  christos libedit_private el_action_t
    754        1.1       cgd /*ARGSUSED*/
    755       1.48  christos vi_to_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
    756        1.1       cgd {
    757       1.12  christos 	return cv_csearch(el, CHAR_FWD, -1, el->el_state.argument, 1);
    758        1.1       cgd }
    759        1.1       cgd 
    760        1.1       cgd 
    761        1.1       cgd /* vi_to_prev_char():
    762        1.1       cgd  *	Vi move up to the character specified previous
    763        1.1       cgd  *	[T]
    764        1.1       cgd  */
    765       1.62  christos libedit_private el_action_t
    766        1.1       cgd /*ARGSUSED*/
    767       1.48  christos vi_to_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    768        1.8     lukem {
    769       1.12  christos 	return cv_csearch(el, CHAR_BACK, -1, el->el_state.argument, 1);
    770        1.1       cgd }
    771        1.1       cgd 
    772        1.1       cgd 
    773        1.1       cgd /* vi_repeat_next_char():
    774        1.1       cgd  *	Vi repeat current character search in the same search direction
    775        1.1       cgd  *	[;]
    776        1.1       cgd  */
    777       1.62  christos libedit_private el_action_t
    778        1.1       cgd /*ARGSUSED*/
    779       1.48  christos vi_repeat_next_char(EditLine *el, wint_t c __attribute__((__unused__)))
    780        1.8     lukem {
    781        1.8     lukem 
    782       1.12  christos 	return cv_csearch(el, el->el_search.chadir, el->el_search.chacha,
    783       1.12  christos 		el->el_state.argument, el->el_search.chatflg);
    784        1.1       cgd }
    785        1.1       cgd 
    786        1.1       cgd 
    787        1.1       cgd /* vi_repeat_prev_char():
    788        1.1       cgd  *	Vi repeat current character search in the opposite search direction
    789        1.1       cgd  *	[,]
    790        1.1       cgd  */
    791       1.62  christos libedit_private el_action_t
    792        1.1       cgd /*ARGSUSED*/
    793       1.48  christos vi_repeat_prev_char(EditLine *el, wint_t c __attribute__((__unused__)))
    794        1.8     lukem {
    795       1.12  christos 	el_action_t r;
    796       1.12  christos 	int dir = el->el_search.chadir;
    797        1.8     lukem 
    798       1.12  christos 	r = cv_csearch(el, -dir, el->el_search.chacha,
    799       1.12  christos 		el->el_state.argument, el->el_search.chatflg);
    800       1.12  christos 	el->el_search.chadir = dir;
    801       1.12  christos 	return r;
    802       1.11  christos }
    803       1.11  christos 
    804       1.11  christos 
    805       1.11  christos /* vi_match():
    806       1.11  christos  *	Vi go to matching () {} or []
    807       1.11  christos  *	[%]
    808       1.11  christos  */
    809       1.62  christos libedit_private el_action_t
    810       1.11  christos /*ARGSUSED*/
    811       1.48  christos vi_match(EditLine *el, wint_t c __attribute__((__unused__)))
    812       1.11  christos {
    813       1.59  christos 	const wchar_t match_chars[] = L"()[]{}";
    814       1.59  christos 	wchar_t *cp;
    815       1.29  christos 	size_t delta, i, count;
    816       1.59  christos 	wchar_t o_ch, c_ch;
    817       1.11  christos 
    818       1.11  christos 	*el->el_line.lastchar = '\0';		/* just in case */
    819       1.11  christos 
    820       1.56  christos 	i = wcscspn(el->el_line.cursor, match_chars);
    821       1.11  christos 	o_ch = el->el_line.cursor[i];
    822       1.11  christos 	if (o_ch == 0)
    823       1.11  christos 		return CC_ERROR;
    824       1.58  christos 	delta = (size_t)(wcschr(match_chars, o_ch) - match_chars);
    825       1.11  christos 	c_ch = match_chars[delta ^ 1];
    826       1.11  christos 	count = 1;
    827       1.11  christos 	delta = 1 - (delta & 1) * 2;
    828       1.11  christos 
    829       1.11  christos 	for (cp = &el->el_line.cursor[i]; count; ) {
    830       1.11  christos 		cp += delta;
    831       1.11  christos 		if (cp < el->el_line.buffer || cp >= el->el_line.lastchar)
    832       1.11  christos 			return CC_ERROR;
    833       1.11  christos 		if (*cp == o_ch)
    834       1.11  christos 			count++;
    835       1.11  christos 		else if (*cp == c_ch)
    836       1.11  christos 			count--;
    837       1.11  christos 	}
    838       1.11  christos 
    839       1.11  christos 	el->el_line.cursor = cp;
    840       1.11  christos 
    841       1.12  christos 	if (el->el_chared.c_vcmd.action != NOP) {
    842       1.12  christos 		/* NB posix says char under cursor should NOT be deleted
    843       1.12  christos 		   for -ve delta - this is different to netbsd vi. */
    844       1.11  christos 		if (delta > 0)
    845       1.11  christos 			el->el_line.cursor++;
    846       1.11  christos 		cv_delfini(el);
    847       1.37  christos 		return CC_REFRESH;
    848       1.11  christos 	}
    849       1.37  christos 	return CC_CURSOR;
    850       1.12  christos }
    851       1.12  christos 
    852       1.12  christos /* vi_undo_line():
    853       1.12  christos  *	Vi undo all changes to line
    854       1.12  christos  *	[U]
    855       1.12  christos  */
    856       1.62  christos libedit_private el_action_t
    857       1.12  christos /*ARGSUSED*/
    858       1.48  christos vi_undo_line(EditLine *el, wint_t c __attribute__((__unused__)))
    859       1.12  christos {
    860       1.12  christos 
    861       1.12  christos 	cv_undo(el);
    862       1.12  christos 	return hist_get(el);
    863       1.12  christos }
    864       1.12  christos 
    865       1.12  christos /* vi_to_column():
    866       1.12  christos  *	Vi go to specified column
    867       1.12  christos  *	[|]
    868       1.12  christos  * NB netbsd vi goes to screen column 'n', posix says nth character
    869       1.12  christos  */
    870       1.62  christos libedit_private el_action_t
    871       1.12  christos /*ARGSUSED*/
    872       1.48  christos vi_to_column(EditLine *el, wint_t c __attribute__((__unused__)))
    873       1.12  christos {
    874       1.12  christos 
    875       1.12  christos 	el->el_line.cursor = el->el_line.buffer;
    876       1.12  christos 	el->el_state.argument--;
    877       1.12  christos 	return ed_next_char(el, 0);
    878       1.12  christos }
    879       1.12  christos 
    880       1.12  christos /* vi_yank_end():
    881       1.12  christos  *	Vi yank to end of line
    882       1.12  christos  *	[Y]
    883       1.12  christos  */
    884       1.62  christos libedit_private el_action_t
    885       1.12  christos /*ARGSUSED*/
    886       1.48  christos vi_yank_end(EditLine *el, wint_t c __attribute__((__unused__)))
    887       1.12  christos {
    888       1.12  christos 
    889       1.12  christos 	cv_yank(el, el->el_line.cursor,
    890       1.29  christos 	    (int)(el->el_line.lastchar - el->el_line.cursor));
    891       1.12  christos 	return CC_REFRESH;
    892       1.12  christos }
    893       1.12  christos 
    894       1.12  christos /* vi_yank():
    895       1.12  christos  *	Vi yank
    896       1.12  christos  *	[y]
    897       1.12  christos  */
    898       1.62  christos libedit_private el_action_t
    899       1.12  christos /*ARGSUSED*/
    900       1.48  christos vi_yank(EditLine *el, wint_t c __attribute__((__unused__)))
    901       1.12  christos {
    902       1.12  christos 
    903       1.12  christos 	return cv_action(el, YANK);
    904       1.12  christos }
    905       1.12  christos 
    906       1.12  christos /* vi_comment_out():
    907       1.12  christos  *	Vi comment out current command
    908       1.22  christos  *	[#]
    909       1.12  christos  */
    910       1.62  christos libedit_private el_action_t
    911       1.12  christos /*ARGSUSED*/
    912       1.48  christos vi_comment_out(EditLine *el, wint_t c __attribute__((__unused__)))
    913       1.12  christos {
    914       1.12  christos 
    915       1.12  christos 	el->el_line.cursor = el->el_line.buffer;
    916       1.12  christos 	c_insert(el, 1);
    917       1.12  christos 	*el->el_line.cursor = '#';
    918       1.12  christos 	re_refresh(el);
    919       1.12  christos 	return ed_newline(el, 0);
    920       1.12  christos }
    921       1.12  christos 
    922       1.12  christos /* vi_alias():
    923       1.12  christos  *	Vi include shell alias
    924       1.12  christos  *	[@]
    925       1.22  christos  * NB: posix implies that we should enter insert mode, however
    926       1.12  christos  * this is against historical precedent...
    927       1.12  christos  */
    928       1.62  christos libedit_private el_action_t
    929       1.12  christos /*ARGSUSED*/
    930       1.48  christos vi_alias(EditLine *el, wint_t c __attribute__((__unused__)))
    931       1.12  christos {
    932       1.12  christos 	char alias_name[3];
    933       1.45  christos 	const char *alias_text;
    934       1.12  christos 
    935       1.45  christos 	if (el->el_chared.c_aliasfun == NULL)
    936       1.33     joerg 		return CC_ERROR;
    937       1.14  christos 
    938       1.12  christos 	alias_name[0] = '_';
    939       1.12  christos 	alias_name[2] = 0;
    940       1.12  christos 	if (el_getc(el, &alias_name[1]) != 1)
    941       1.12  christos 		return CC_ERROR;
    942       1.12  christos 
    943       1.45  christos 	alias_text = (*el->el_chared.c_aliasfun)(el->el_chared.c_aliasarg,
    944       1.45  christos 	    alias_name);
    945       1.12  christos 	if (alias_text != NULL)
    946       1.58  christos 		el_wpush(el, ct_decode_string(alias_text, &el->el_scratch));
    947       1.12  christos 	return CC_NORM;
    948       1.12  christos }
    949       1.12  christos 
    950       1.12  christos /* vi_to_history_line():
    951       1.12  christos  *	Vi go to specified history file line.
    952       1.12  christos  *	[G]
    953       1.12  christos  */
    954       1.62  christos libedit_private el_action_t
    955       1.12  christos /*ARGSUSED*/
    956       1.48  christos vi_to_history_line(EditLine *el, wint_t c __attribute__((__unused__)))
    957       1.12  christos {
    958       1.12  christos 	int sv_event_no = el->el_history.eventno;
    959       1.12  christos 	el_action_t rval;
    960       1.12  christos 
    961       1.12  christos 
    962       1.12  christos 	if (el->el_history.eventno == 0) {
    963       1.58  christos 		 (void) wcsncpy(el->el_history.buf, el->el_line.buffer,
    964       1.12  christos 		     EL_BUFSIZ);
    965       1.12  christos 		 el->el_history.last = el->el_history.buf +
    966       1.12  christos 			 (el->el_line.lastchar - el->el_line.buffer);
    967       1.12  christos 	}
    968       1.12  christos 
    969       1.12  christos 	/* Lack of a 'count' means oldest, not 1 */
    970       1.12  christos 	if (!el->el_state.doingarg) {
    971       1.12  christos 		el->el_history.eventno = 0x7fffffff;
    972       1.12  christos 		hist_get(el);
    973       1.12  christos 	} else {
    974       1.12  christos 		/* This is brain dead, all the rest of this code counts
    975       1.12  christos 		 * upwards going into the past.  Here we need count in the
    976       1.12  christos 		 * other direction (to match the output of fc -l).
    977       1.12  christos 		 * I could change the world, but this seems to suffice.
    978       1.12  christos 		 */
    979       1.12  christos 		el->el_history.eventno = 1;
    980       1.12  christos 		if (hist_get(el) == CC_ERROR)
    981       1.12  christos 			return CC_ERROR;
    982       1.54  christos 		el->el_history.eventno = 1 + el->el_history.ev.num
    983       1.12  christos 					- el->el_state.argument;
    984       1.12  christos 		if (el->el_history.eventno < 0) {
    985       1.12  christos 			el->el_history.eventno = sv_event_no;
    986       1.12  christos 			return CC_ERROR;
    987       1.12  christos 		}
    988       1.12  christos 	}
    989       1.12  christos 	rval = hist_get(el);
    990       1.12  christos 	if (rval == CC_ERROR)
    991       1.12  christos 		el->el_history.eventno = sv_event_no;
    992       1.12  christos 	return rval;
    993       1.12  christos }
    994       1.12  christos 
    995       1.12  christos /* vi_histedit():
    996       1.12  christos  *	Vi edit history line with vi
    997       1.12  christos  *	[v]
    998       1.12  christos  */
    999       1.62  christos libedit_private el_action_t
   1000       1.12  christos /*ARGSUSED*/
   1001       1.48  christos vi_histedit(EditLine *el, wint_t c __attribute__((__unused__)))
   1002       1.12  christos {
   1003       1.12  christos 	int fd;
   1004       1.12  christos 	pid_t pid;
   1005       1.29  christos 	ssize_t st;
   1006       1.29  christos 	int status;
   1007       1.12  christos 	char tempfile[] = "/tmp/histedit.XXXXXXXXXX";
   1008       1.43  christos 	char *cp = NULL;
   1009       1.31  christos 	size_t len;
   1010       1.59  christos 	wchar_t *line = NULL;
   1011       1.12  christos 
   1012       1.12  christos 	if (el->el_state.doingarg) {
   1013       1.12  christos 		if (vi_to_history_line(el, 0) == CC_ERROR)
   1014       1.12  christos 			return CC_ERROR;
   1015       1.12  christos 	}
   1016       1.12  christos 
   1017       1.12  christos 	fd = mkstemp(tempfile);
   1018       1.12  christos 	if (fd < 0)
   1019       1.12  christos 		return CC_ERROR;
   1020       1.31  christos 	len = (size_t)(el->el_line.lastchar - el->el_line.buffer);
   1021       1.31  christos #define TMP_BUFSIZ (EL_BUFSIZ * MB_LEN_MAX)
   1022  1.62.16.1    martin 	cp = el_calloc(TMP_BUFSIZ, sizeof(*cp));
   1023       1.43  christos 	if (cp == NULL)
   1024       1.43  christos 		goto error;
   1025  1.62.16.1    martin 	line = el_calloc(len + 1, sizeof(*line));
   1026       1.43  christos 	if (line == NULL)
   1027       1.43  christos 		goto error;
   1028       1.58  christos 	wcsncpy(line, el->el_line.buffer, len);
   1029       1.31  christos 	line[len] = '\0';
   1030       1.56  christos 	wcstombs(cp, line, TMP_BUFSIZ - 1);
   1031       1.31  christos 	cp[TMP_BUFSIZ - 1] = '\0';
   1032       1.31  christos 	len = strlen(cp);
   1033       1.31  christos 	write(fd, cp, len);
   1034       1.39  christos 	write(fd, "\n", (size_t)1);
   1035       1.12  christos 	pid = fork();
   1036       1.12  christos 	switch (pid) {
   1037       1.12  christos 	case -1:
   1038       1.43  christos 		goto error;
   1039       1.12  christos 	case 0:
   1040       1.12  christos 		close(fd);
   1041       1.28    sketch 		execlp("vi", "vi", tempfile, (char *)NULL);
   1042       1.12  christos 		exit(0);
   1043       1.12  christos 		/*NOTREACHED*/
   1044       1.12  christos 	default:
   1045       1.29  christos 		while (waitpid(pid, &status, 0) != pid)
   1046       1.12  christos 			continue;
   1047       1.29  christos 		lseek(fd, (off_t)0, SEEK_SET);
   1048       1.46  christos 		st = read(fd, cp, TMP_BUFSIZ - 1);
   1049       1.31  christos 		if (st > 0) {
   1050       1.46  christos 			cp[st] = '\0';
   1051       1.47  christos 			len = (size_t)(el->el_line.limit - el->el_line.buffer);
   1052       1.56  christos 			len = mbstowcs(el->el_line.buffer, cp, len);
   1053       1.46  christos 			if (len > 0 && el->el_line.buffer[len - 1] == '\n')
   1054       1.31  christos 				--len;
   1055       1.31  christos 		}
   1056       1.31  christos 		else
   1057       1.31  christos 			len = 0;
   1058       1.31  christos                 el->el_line.cursor = el->el_line.buffer;
   1059       1.31  christos                 el->el_line.lastchar = el->el_line.buffer + len;
   1060       1.31  christos 		el_free(cp);
   1061       1.31  christos                 el_free(line);
   1062       1.12  christos 		break;
   1063       1.12  christos 	}
   1064       1.12  christos 
   1065       1.12  christos 	close(fd);
   1066       1.12  christos 	unlink(tempfile);
   1067       1.12  christos 	/* return CC_REFRESH; */
   1068       1.12  christos 	return ed_newline(el, 0);
   1069       1.43  christos error:
   1070       1.43  christos 	el_free(line);
   1071       1.43  christos 	el_free(cp);
   1072       1.43  christos 	close(fd);
   1073       1.43  christos 	unlink(tempfile);
   1074       1.43  christos 	return CC_ERROR;
   1075       1.12  christos }
   1076       1.12  christos 
   1077       1.12  christos /* vi_history_word():
   1078       1.12  christos  *	Vi append word from previous input line
   1079       1.12  christos  *	[_]
   1080       1.12  christos  * Who knows where this one came from!
   1081       1.12  christos  * '_' in vi means 'entire current line', so 'cc' is a synonym for 'c_'
   1082       1.12  christos  */
   1083       1.62  christos libedit_private el_action_t
   1084       1.12  christos /*ARGSUSED*/
   1085       1.48  christos vi_history_word(EditLine *el, wint_t c __attribute__((__unused__)))
   1086       1.12  christos {
   1087       1.59  christos 	const wchar_t *wp = HIST_FIRST(el);
   1088       1.59  christos 	const wchar_t *wep, *wsp;
   1089       1.12  christos 	int len;
   1090       1.59  christos 	wchar_t *cp;
   1091       1.59  christos 	const wchar_t *lim;
   1092       1.12  christos 
   1093       1.12  christos 	if (wp == NULL)
   1094       1.12  christos 		return CC_ERROR;
   1095       1.12  christos 
   1096       1.57  christos 	wep = wsp = NULL;
   1097       1.12  christos 	do {
   1098       1.56  christos 		while (iswspace(*wp))
   1099       1.12  christos 			wp++;
   1100       1.12  christos 		if (*wp == 0)
   1101       1.12  christos 			break;
   1102       1.12  christos 		wsp = wp;
   1103       1.56  christos 		while (*wp && !iswspace(*wp))
   1104       1.12  christos 			wp++;
   1105       1.12  christos 		wep = wp;
   1106       1.31  christos 	} while ((!el->el_state.doingarg || --el->el_state.argument > 0)
   1107       1.31  christos 	    && *wp != 0);
   1108       1.12  christos 
   1109       1.57  christos 	if (wsp == NULL || (el->el_state.doingarg && el->el_state.argument != 0))
   1110       1.12  christos 		return CC_ERROR;
   1111       1.12  christos 
   1112       1.12  christos 	cv_undo(el);
   1113       1.29  christos 	len = (int)(wep - wsp);
   1114       1.12  christos 	if (el->el_line.cursor < el->el_line.lastchar)
   1115       1.12  christos 		el->el_line.cursor++;
   1116       1.12  christos 	c_insert(el, len + 1);
   1117       1.12  christos 	cp = el->el_line.cursor;
   1118       1.12  christos 	lim = el->el_line.limit;
   1119       1.12  christos 	if (cp < lim)
   1120       1.12  christos 		*cp++ = ' ';
   1121       1.12  christos 	while (wsp < wep && cp < lim)
   1122       1.12  christos 		*cp++ = *wsp++;
   1123       1.12  christos 	el->el_line.cursor = cp;
   1124       1.12  christos 
   1125       1.12  christos 	el->el_map.current = el->el_map.key;
   1126       1.12  christos 	return CC_REFRESH;
   1127       1.12  christos }
   1128       1.12  christos 
   1129       1.12  christos /* vi_redo():
   1130       1.12  christos  *	Vi redo last non-motion command
   1131       1.12  christos  *	[.]
   1132       1.12  christos  */
   1133       1.62  christos libedit_private el_action_t
   1134       1.12  christos /*ARGSUSED*/
   1135       1.48  christos vi_redo(EditLine *el, wint_t c __attribute__((__unused__)))
   1136       1.12  christos {
   1137       1.12  christos 	c_redo_t *r = &el->el_chared.c_redo;
   1138       1.12  christos 
   1139       1.12  christos 	if (!el->el_state.doingarg && r->count) {
   1140       1.12  christos 		el->el_state.doingarg = 1;
   1141       1.12  christos 		el->el_state.argument = r->count;
   1142       1.12  christos 	}
   1143       1.12  christos 
   1144       1.12  christos 	el->el_chared.c_vcmd.pos = el->el_line.cursor;
   1145       1.12  christos 	el->el_chared.c_vcmd.action = r->action;
   1146       1.12  christos 	if (r->pos != r->buf) {
   1147       1.12  christos 		if (r->pos + 1 > r->lim)
   1148       1.12  christos 			/* sanity */
   1149       1.12  christos 			r->pos = r->lim - 1;
   1150       1.12  christos 		r->pos[0] = 0;
   1151       1.58  christos 		el_wpush(el, r->buf);
   1152       1.12  christos 	}
   1153       1.12  christos 
   1154       1.12  christos 	el->el_state.thiscmd = r->cmd;
   1155       1.12  christos 	el->el_state.thisch = r->ch;
   1156       1.37  christos 	return (*el->el_map.func[r->cmd])(el, r->ch);
   1157        1.1       cgd }
   1158