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