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