1*3e1db26aSLionel Sambuc /* $NetBSD: common.c,v 1.29 2012/03/24 20:08:43 christos Exp $ */
2*3e1db26aSLionel Sambuc
3*3e1db26aSLionel Sambuc /*-
4*3e1db26aSLionel Sambuc * Copyright (c) 1992, 1993
5*3e1db26aSLionel Sambuc * The Regents of the University of California. All rights reserved.
6*3e1db26aSLionel Sambuc *
7*3e1db26aSLionel Sambuc * This code is derived from software contributed to Berkeley by
8*3e1db26aSLionel Sambuc * Christos Zoulas of Cornell University.
9*3e1db26aSLionel Sambuc *
10*3e1db26aSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*3e1db26aSLionel Sambuc * modification, are permitted provided that the following conditions
12*3e1db26aSLionel Sambuc * are met:
13*3e1db26aSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*3e1db26aSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*3e1db26aSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*3e1db26aSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*3e1db26aSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*3e1db26aSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19*3e1db26aSLionel Sambuc * may be used to endorse or promote products derived from this software
20*3e1db26aSLionel Sambuc * without specific prior written permission.
21*3e1db26aSLionel Sambuc *
22*3e1db26aSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*3e1db26aSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*3e1db26aSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*3e1db26aSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*3e1db26aSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*3e1db26aSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*3e1db26aSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*3e1db26aSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*3e1db26aSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*3e1db26aSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*3e1db26aSLionel Sambuc * SUCH DAMAGE.
33*3e1db26aSLionel Sambuc */
34*3e1db26aSLionel Sambuc
35*3e1db26aSLionel Sambuc #include "config.h"
36*3e1db26aSLionel Sambuc #if !defined(lint) && !defined(SCCSID)
37*3e1db26aSLionel Sambuc #if 0
38*3e1db26aSLionel Sambuc static char sccsid[] = "@(#)common.c 8.1 (Berkeley) 6/4/93";
39*3e1db26aSLionel Sambuc #else
40*3e1db26aSLionel Sambuc __RCSID("$NetBSD: common.c,v 1.29 2012/03/24 20:08:43 christos Exp $");
41*3e1db26aSLionel Sambuc #endif
42*3e1db26aSLionel Sambuc #endif /* not lint && not SCCSID */
43*3e1db26aSLionel Sambuc
44*3e1db26aSLionel Sambuc /*
45*3e1db26aSLionel Sambuc * common.c: Common Editor functions
46*3e1db26aSLionel Sambuc */
47*3e1db26aSLionel Sambuc #include "el.h"
48*3e1db26aSLionel Sambuc
49*3e1db26aSLionel Sambuc /* ed_end_of_file():
50*3e1db26aSLionel Sambuc * Indicate end of file
51*3e1db26aSLionel Sambuc * [^D]
52*3e1db26aSLionel Sambuc */
53*3e1db26aSLionel Sambuc protected el_action_t
54*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_end_of_file(EditLine * el,Int c)55*3e1db26aSLionel Sambuc ed_end_of_file(EditLine *el, Int c __attribute__((__unused__)))
56*3e1db26aSLionel Sambuc {
57*3e1db26aSLionel Sambuc
58*3e1db26aSLionel Sambuc re_goto_bottom(el);
59*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0';
60*3e1db26aSLionel Sambuc return CC_EOF;
61*3e1db26aSLionel Sambuc }
62*3e1db26aSLionel Sambuc
63*3e1db26aSLionel Sambuc
64*3e1db26aSLionel Sambuc /* ed_insert():
65*3e1db26aSLionel Sambuc * Add character to the line
66*3e1db26aSLionel Sambuc * Insert a character [bound to all insert keys]
67*3e1db26aSLionel Sambuc */
68*3e1db26aSLionel Sambuc protected el_action_t
ed_insert(EditLine * el,Int c)69*3e1db26aSLionel Sambuc ed_insert(EditLine *el, Int c)
70*3e1db26aSLionel Sambuc {
71*3e1db26aSLionel Sambuc int count = el->el_state.argument;
72*3e1db26aSLionel Sambuc
73*3e1db26aSLionel Sambuc if (c == '\0')
74*3e1db26aSLionel Sambuc return CC_ERROR;
75*3e1db26aSLionel Sambuc
76*3e1db26aSLionel Sambuc if (el->el_line.lastchar + el->el_state.argument >=
77*3e1db26aSLionel Sambuc el->el_line.limit) {
78*3e1db26aSLionel Sambuc /* end of buffer space, try to allocate more */
79*3e1db26aSLionel Sambuc if (!ch_enlargebufs(el, (size_t) count))
80*3e1db26aSLionel Sambuc return CC_ERROR; /* error allocating more */
81*3e1db26aSLionel Sambuc }
82*3e1db26aSLionel Sambuc
83*3e1db26aSLionel Sambuc if (count == 1) {
84*3e1db26aSLionel Sambuc if (el->el_state.inputmode == MODE_INSERT
85*3e1db26aSLionel Sambuc || el->el_line.cursor >= el->el_line.lastchar)
86*3e1db26aSLionel Sambuc c_insert(el, 1);
87*3e1db26aSLionel Sambuc
88*3e1db26aSLionel Sambuc *el->el_line.cursor++ = c;
89*3e1db26aSLionel Sambuc re_fastaddc(el); /* fast refresh for one char. */
90*3e1db26aSLionel Sambuc } else {
91*3e1db26aSLionel Sambuc if (el->el_state.inputmode != MODE_REPLACE_1)
92*3e1db26aSLionel Sambuc c_insert(el, el->el_state.argument);
93*3e1db26aSLionel Sambuc
94*3e1db26aSLionel Sambuc while (count-- && el->el_line.cursor < el->el_line.lastchar)
95*3e1db26aSLionel Sambuc *el->el_line.cursor++ = c;
96*3e1db26aSLionel Sambuc re_refresh(el);
97*3e1db26aSLionel Sambuc }
98*3e1db26aSLionel Sambuc
99*3e1db26aSLionel Sambuc if (el->el_state.inputmode == MODE_REPLACE_1)
100*3e1db26aSLionel Sambuc return vi_command_mode(el, 0);
101*3e1db26aSLionel Sambuc
102*3e1db26aSLionel Sambuc return CC_NORM;
103*3e1db26aSLionel Sambuc }
104*3e1db26aSLionel Sambuc
105*3e1db26aSLionel Sambuc
106*3e1db26aSLionel Sambuc /* ed_delete_prev_word():
107*3e1db26aSLionel Sambuc * Delete from beginning of current word to cursor
108*3e1db26aSLionel Sambuc * [M-^?] [^W]
109*3e1db26aSLionel Sambuc */
110*3e1db26aSLionel Sambuc protected el_action_t
111*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_delete_prev_word(EditLine * el,Int c)112*3e1db26aSLionel Sambuc ed_delete_prev_word(EditLine *el, Int c __attribute__((__unused__)))
113*3e1db26aSLionel Sambuc {
114*3e1db26aSLionel Sambuc Char *cp, *p, *kp;
115*3e1db26aSLionel Sambuc
116*3e1db26aSLionel Sambuc if (el->el_line.cursor == el->el_line.buffer)
117*3e1db26aSLionel Sambuc return CC_ERROR;
118*3e1db26aSLionel Sambuc
119*3e1db26aSLionel Sambuc cp = c__prev_word(el->el_line.cursor, el->el_line.buffer,
120*3e1db26aSLionel Sambuc el->el_state.argument, ce__isword);
121*3e1db26aSLionel Sambuc
122*3e1db26aSLionel Sambuc for (p = cp, kp = el->el_chared.c_kill.buf; p < el->el_line.cursor; p++)
123*3e1db26aSLionel Sambuc *kp++ = *p;
124*3e1db26aSLionel Sambuc el->el_chared.c_kill.last = kp;
125*3e1db26aSLionel Sambuc
126*3e1db26aSLionel Sambuc c_delbefore(el, (int)(el->el_line.cursor - cp));/* delete before dot */
127*3e1db26aSLionel Sambuc el->el_line.cursor = cp;
128*3e1db26aSLionel Sambuc if (el->el_line.cursor < el->el_line.buffer)
129*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.buffer; /* bounds check */
130*3e1db26aSLionel Sambuc return CC_REFRESH;
131*3e1db26aSLionel Sambuc }
132*3e1db26aSLionel Sambuc
133*3e1db26aSLionel Sambuc
134*3e1db26aSLionel Sambuc /* ed_delete_next_char():
135*3e1db26aSLionel Sambuc * Delete character under cursor
136*3e1db26aSLionel Sambuc * [^D] [x]
137*3e1db26aSLionel Sambuc */
138*3e1db26aSLionel Sambuc protected el_action_t
139*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_delete_next_char(EditLine * el,Int c)140*3e1db26aSLionel Sambuc ed_delete_next_char(EditLine *el, Int c __attribute__((__unused__)))
141*3e1db26aSLionel Sambuc {
142*3e1db26aSLionel Sambuc #ifdef DEBUG_EDIT
143*3e1db26aSLionel Sambuc #define EL el->el_line
144*3e1db26aSLionel Sambuc (void) fprintf(el->el_errlfile,
145*3e1db26aSLionel Sambuc "\nD(b: %x(%s) c: %x(%s) last: %x(%s) limit: %x(%s)\n",
146*3e1db26aSLionel Sambuc EL.buffer, EL.buffer, EL.cursor, EL.cursor, EL.lastchar,
147*3e1db26aSLionel Sambuc EL.lastchar, EL.limit, EL.limit);
148*3e1db26aSLionel Sambuc #endif
149*3e1db26aSLionel Sambuc if (el->el_line.cursor == el->el_line.lastchar) {
150*3e1db26aSLionel Sambuc /* if I'm at the end */
151*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI) {
152*3e1db26aSLionel Sambuc if (el->el_line.cursor == el->el_line.buffer) {
153*3e1db26aSLionel Sambuc /* if I'm also at the beginning */
154*3e1db26aSLionel Sambuc #ifdef KSHVI
155*3e1db26aSLionel Sambuc return CC_ERROR;
156*3e1db26aSLionel Sambuc #else
157*3e1db26aSLionel Sambuc /* then do an EOF */
158*3e1db26aSLionel Sambuc terminal_writec(el, c);
159*3e1db26aSLionel Sambuc return CC_EOF;
160*3e1db26aSLionel Sambuc #endif
161*3e1db26aSLionel Sambuc } else {
162*3e1db26aSLionel Sambuc #ifdef KSHVI
163*3e1db26aSLionel Sambuc el->el_line.cursor--;
164*3e1db26aSLionel Sambuc #else
165*3e1db26aSLionel Sambuc return CC_ERROR;
166*3e1db26aSLionel Sambuc #endif
167*3e1db26aSLionel Sambuc }
168*3e1db26aSLionel Sambuc } else
169*3e1db26aSLionel Sambuc return CC_ERROR;
170*3e1db26aSLionel Sambuc }
171*3e1db26aSLionel Sambuc c_delafter(el, el->el_state.argument); /* delete after dot */
172*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI &&
173*3e1db26aSLionel Sambuc el->el_line.cursor >= el->el_line.lastchar &&
174*3e1db26aSLionel Sambuc el->el_line.cursor > el->el_line.buffer)
175*3e1db26aSLionel Sambuc /* bounds check */
176*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.lastchar - 1;
177*3e1db26aSLionel Sambuc return CC_REFRESH;
178*3e1db26aSLionel Sambuc }
179*3e1db26aSLionel Sambuc
180*3e1db26aSLionel Sambuc
181*3e1db26aSLionel Sambuc /* ed_kill_line():
182*3e1db26aSLionel Sambuc * Cut to the end of line
183*3e1db26aSLionel Sambuc * [^K] [^K]
184*3e1db26aSLionel Sambuc */
185*3e1db26aSLionel Sambuc protected el_action_t
186*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_kill_line(EditLine * el,Int c)187*3e1db26aSLionel Sambuc ed_kill_line(EditLine *el, Int c __attribute__((__unused__)))
188*3e1db26aSLionel Sambuc {
189*3e1db26aSLionel Sambuc Char *kp, *cp;
190*3e1db26aSLionel Sambuc
191*3e1db26aSLionel Sambuc cp = el->el_line.cursor;
192*3e1db26aSLionel Sambuc kp = el->el_chared.c_kill.buf;
193*3e1db26aSLionel Sambuc while (cp < el->el_line.lastchar)
194*3e1db26aSLionel Sambuc *kp++ = *cp++; /* copy it */
195*3e1db26aSLionel Sambuc el->el_chared.c_kill.last = kp;
196*3e1db26aSLionel Sambuc /* zap! -- delete to end */
197*3e1db26aSLionel Sambuc el->el_line.lastchar = el->el_line.cursor;
198*3e1db26aSLionel Sambuc return CC_REFRESH;
199*3e1db26aSLionel Sambuc }
200*3e1db26aSLionel Sambuc
201*3e1db26aSLionel Sambuc
202*3e1db26aSLionel Sambuc /* ed_move_to_end():
203*3e1db26aSLionel Sambuc * Move cursor to the end of line
204*3e1db26aSLionel Sambuc * [^E] [^E]
205*3e1db26aSLionel Sambuc */
206*3e1db26aSLionel Sambuc protected el_action_t
207*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_move_to_end(EditLine * el,Int c)208*3e1db26aSLionel Sambuc ed_move_to_end(EditLine *el, Int c __attribute__((__unused__)))
209*3e1db26aSLionel Sambuc {
210*3e1db26aSLionel Sambuc
211*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.lastchar;
212*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI) {
213*3e1db26aSLionel Sambuc if (el->el_chared.c_vcmd.action != NOP) {
214*3e1db26aSLionel Sambuc cv_delfini(el);
215*3e1db26aSLionel Sambuc return CC_REFRESH;
216*3e1db26aSLionel Sambuc }
217*3e1db26aSLionel Sambuc #ifdef VI_MOVE
218*3e1db26aSLionel Sambuc el->el_line.cursor--;
219*3e1db26aSLionel Sambuc #endif
220*3e1db26aSLionel Sambuc }
221*3e1db26aSLionel Sambuc return CC_CURSOR;
222*3e1db26aSLionel Sambuc }
223*3e1db26aSLionel Sambuc
224*3e1db26aSLionel Sambuc
225*3e1db26aSLionel Sambuc /* ed_move_to_beg():
226*3e1db26aSLionel Sambuc * Move cursor to the beginning of line
227*3e1db26aSLionel Sambuc * [^A] [^A]
228*3e1db26aSLionel Sambuc */
229*3e1db26aSLionel Sambuc protected el_action_t
230*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_move_to_beg(EditLine * el,Int c)231*3e1db26aSLionel Sambuc ed_move_to_beg(EditLine *el, Int c __attribute__((__unused__)))
232*3e1db26aSLionel Sambuc {
233*3e1db26aSLionel Sambuc
234*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.buffer;
235*3e1db26aSLionel Sambuc
236*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI) {
237*3e1db26aSLionel Sambuc /* We want FIRST non space character */
238*3e1db26aSLionel Sambuc while (Isspace(*el->el_line.cursor))
239*3e1db26aSLionel Sambuc el->el_line.cursor++;
240*3e1db26aSLionel Sambuc if (el->el_chared.c_vcmd.action != NOP) {
241*3e1db26aSLionel Sambuc cv_delfini(el);
242*3e1db26aSLionel Sambuc return CC_REFRESH;
243*3e1db26aSLionel Sambuc }
244*3e1db26aSLionel Sambuc }
245*3e1db26aSLionel Sambuc return CC_CURSOR;
246*3e1db26aSLionel Sambuc }
247*3e1db26aSLionel Sambuc
248*3e1db26aSLionel Sambuc
249*3e1db26aSLionel Sambuc /* ed_transpose_chars():
250*3e1db26aSLionel Sambuc * Exchange the character to the left of the cursor with the one under it
251*3e1db26aSLionel Sambuc * [^T] [^T]
252*3e1db26aSLionel Sambuc */
253*3e1db26aSLionel Sambuc protected el_action_t
ed_transpose_chars(EditLine * el,Int c)254*3e1db26aSLionel Sambuc ed_transpose_chars(EditLine *el, Int c)
255*3e1db26aSLionel Sambuc {
256*3e1db26aSLionel Sambuc
257*3e1db26aSLionel Sambuc if (el->el_line.cursor < el->el_line.lastchar) {
258*3e1db26aSLionel Sambuc if (el->el_line.lastchar <= &el->el_line.buffer[1])
259*3e1db26aSLionel Sambuc return CC_ERROR;
260*3e1db26aSLionel Sambuc else
261*3e1db26aSLionel Sambuc el->el_line.cursor++;
262*3e1db26aSLionel Sambuc }
263*3e1db26aSLionel Sambuc if (el->el_line.cursor > &el->el_line.buffer[1]) {
264*3e1db26aSLionel Sambuc /* must have at least two chars entered */
265*3e1db26aSLionel Sambuc c = el->el_line.cursor[-2];
266*3e1db26aSLionel Sambuc el->el_line.cursor[-2] = el->el_line.cursor[-1];
267*3e1db26aSLionel Sambuc el->el_line.cursor[-1] = c;
268*3e1db26aSLionel Sambuc return CC_REFRESH;
269*3e1db26aSLionel Sambuc } else
270*3e1db26aSLionel Sambuc return CC_ERROR;
271*3e1db26aSLionel Sambuc }
272*3e1db26aSLionel Sambuc
273*3e1db26aSLionel Sambuc
274*3e1db26aSLionel Sambuc /* ed_next_char():
275*3e1db26aSLionel Sambuc * Move to the right one character
276*3e1db26aSLionel Sambuc * [^F] [^F]
277*3e1db26aSLionel Sambuc */
278*3e1db26aSLionel Sambuc protected el_action_t
279*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_next_char(EditLine * el,Int c)280*3e1db26aSLionel Sambuc ed_next_char(EditLine *el, Int c __attribute__((__unused__)))
281*3e1db26aSLionel Sambuc {
282*3e1db26aSLionel Sambuc Char *lim = el->el_line.lastchar;
283*3e1db26aSLionel Sambuc
284*3e1db26aSLionel Sambuc if (el->el_line.cursor >= lim ||
285*3e1db26aSLionel Sambuc (el->el_line.cursor == lim - 1 &&
286*3e1db26aSLionel Sambuc el->el_map.type == MAP_VI &&
287*3e1db26aSLionel Sambuc el->el_chared.c_vcmd.action == NOP))
288*3e1db26aSLionel Sambuc return CC_ERROR;
289*3e1db26aSLionel Sambuc
290*3e1db26aSLionel Sambuc el->el_line.cursor += el->el_state.argument;
291*3e1db26aSLionel Sambuc if (el->el_line.cursor > lim)
292*3e1db26aSLionel Sambuc el->el_line.cursor = lim;
293*3e1db26aSLionel Sambuc
294*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI)
295*3e1db26aSLionel Sambuc if (el->el_chared.c_vcmd.action != NOP) {
296*3e1db26aSLionel Sambuc cv_delfini(el);
297*3e1db26aSLionel Sambuc return CC_REFRESH;
298*3e1db26aSLionel Sambuc }
299*3e1db26aSLionel Sambuc return CC_CURSOR;
300*3e1db26aSLionel Sambuc }
301*3e1db26aSLionel Sambuc
302*3e1db26aSLionel Sambuc
303*3e1db26aSLionel Sambuc /* ed_prev_word():
304*3e1db26aSLionel Sambuc * Move to the beginning of the current word
305*3e1db26aSLionel Sambuc * [M-b] [b]
306*3e1db26aSLionel Sambuc */
307*3e1db26aSLionel Sambuc protected el_action_t
308*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_prev_word(EditLine * el,Int c)309*3e1db26aSLionel Sambuc ed_prev_word(EditLine *el, Int c __attribute__((__unused__)))
310*3e1db26aSLionel Sambuc {
311*3e1db26aSLionel Sambuc
312*3e1db26aSLionel Sambuc if (el->el_line.cursor == el->el_line.buffer)
313*3e1db26aSLionel Sambuc return CC_ERROR;
314*3e1db26aSLionel Sambuc
315*3e1db26aSLionel Sambuc el->el_line.cursor = c__prev_word(el->el_line.cursor,
316*3e1db26aSLionel Sambuc el->el_line.buffer,
317*3e1db26aSLionel Sambuc el->el_state.argument,
318*3e1db26aSLionel Sambuc ce__isword);
319*3e1db26aSLionel Sambuc
320*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI)
321*3e1db26aSLionel Sambuc if (el->el_chared.c_vcmd.action != NOP) {
322*3e1db26aSLionel Sambuc cv_delfini(el);
323*3e1db26aSLionel Sambuc return CC_REFRESH;
324*3e1db26aSLionel Sambuc }
325*3e1db26aSLionel Sambuc return CC_CURSOR;
326*3e1db26aSLionel Sambuc }
327*3e1db26aSLionel Sambuc
328*3e1db26aSLionel Sambuc
329*3e1db26aSLionel Sambuc /* ed_prev_char():
330*3e1db26aSLionel Sambuc * Move to the left one character
331*3e1db26aSLionel Sambuc * [^B] [^B]
332*3e1db26aSLionel Sambuc */
333*3e1db26aSLionel Sambuc protected el_action_t
334*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_prev_char(EditLine * el,Int c)335*3e1db26aSLionel Sambuc ed_prev_char(EditLine *el, Int c __attribute__((__unused__)))
336*3e1db26aSLionel Sambuc {
337*3e1db26aSLionel Sambuc
338*3e1db26aSLionel Sambuc if (el->el_line.cursor > el->el_line.buffer) {
339*3e1db26aSLionel Sambuc el->el_line.cursor -= el->el_state.argument;
340*3e1db26aSLionel Sambuc if (el->el_line.cursor < el->el_line.buffer)
341*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.buffer;
342*3e1db26aSLionel Sambuc
343*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI)
344*3e1db26aSLionel Sambuc if (el->el_chared.c_vcmd.action != NOP) {
345*3e1db26aSLionel Sambuc cv_delfini(el);
346*3e1db26aSLionel Sambuc return CC_REFRESH;
347*3e1db26aSLionel Sambuc }
348*3e1db26aSLionel Sambuc return CC_CURSOR;
349*3e1db26aSLionel Sambuc } else
350*3e1db26aSLionel Sambuc return CC_ERROR;
351*3e1db26aSLionel Sambuc }
352*3e1db26aSLionel Sambuc
353*3e1db26aSLionel Sambuc
354*3e1db26aSLionel Sambuc /* ed_quoted_insert():
355*3e1db26aSLionel Sambuc * Add the next character typed verbatim
356*3e1db26aSLionel Sambuc * [^V] [^V]
357*3e1db26aSLionel Sambuc */
358*3e1db26aSLionel Sambuc protected el_action_t
ed_quoted_insert(EditLine * el,Int c)359*3e1db26aSLionel Sambuc ed_quoted_insert(EditLine *el, Int c)
360*3e1db26aSLionel Sambuc {
361*3e1db26aSLionel Sambuc int num;
362*3e1db26aSLionel Sambuc Char tc;
363*3e1db26aSLionel Sambuc
364*3e1db26aSLionel Sambuc tty_quotemode(el);
365*3e1db26aSLionel Sambuc num = FUN(el,getc)(el, &tc);
366*3e1db26aSLionel Sambuc c = tc;
367*3e1db26aSLionel Sambuc tty_noquotemode(el);
368*3e1db26aSLionel Sambuc if (num == 1)
369*3e1db26aSLionel Sambuc return ed_insert(el, c);
370*3e1db26aSLionel Sambuc else
371*3e1db26aSLionel Sambuc return ed_end_of_file(el, 0);
372*3e1db26aSLionel Sambuc }
373*3e1db26aSLionel Sambuc
374*3e1db26aSLionel Sambuc
375*3e1db26aSLionel Sambuc /* ed_digit():
376*3e1db26aSLionel Sambuc * Adds to argument or enters a digit
377*3e1db26aSLionel Sambuc */
378*3e1db26aSLionel Sambuc protected el_action_t
ed_digit(EditLine * el,Int c)379*3e1db26aSLionel Sambuc ed_digit(EditLine *el, Int c)
380*3e1db26aSLionel Sambuc {
381*3e1db26aSLionel Sambuc
382*3e1db26aSLionel Sambuc if (!Isdigit(c))
383*3e1db26aSLionel Sambuc return CC_ERROR;
384*3e1db26aSLionel Sambuc
385*3e1db26aSLionel Sambuc if (el->el_state.doingarg) {
386*3e1db26aSLionel Sambuc /* if doing an arg, add this in... */
387*3e1db26aSLionel Sambuc if (el->el_state.lastcmd == EM_UNIVERSAL_ARGUMENT)
388*3e1db26aSLionel Sambuc el->el_state.argument = c - '0';
389*3e1db26aSLionel Sambuc else {
390*3e1db26aSLionel Sambuc if (el->el_state.argument > 1000000)
391*3e1db26aSLionel Sambuc return CC_ERROR;
392*3e1db26aSLionel Sambuc el->el_state.argument =
393*3e1db26aSLionel Sambuc (el->el_state.argument * 10) + (c - '0');
394*3e1db26aSLionel Sambuc }
395*3e1db26aSLionel Sambuc return CC_ARGHACK;
396*3e1db26aSLionel Sambuc }
397*3e1db26aSLionel Sambuc
398*3e1db26aSLionel Sambuc return ed_insert(el, c);
399*3e1db26aSLionel Sambuc }
400*3e1db26aSLionel Sambuc
401*3e1db26aSLionel Sambuc
402*3e1db26aSLionel Sambuc /* ed_argument_digit():
403*3e1db26aSLionel Sambuc * Digit that starts argument
404*3e1db26aSLionel Sambuc * For ESC-n
405*3e1db26aSLionel Sambuc */
406*3e1db26aSLionel Sambuc protected el_action_t
ed_argument_digit(EditLine * el,Int c)407*3e1db26aSLionel Sambuc ed_argument_digit(EditLine *el, Int c)
408*3e1db26aSLionel Sambuc {
409*3e1db26aSLionel Sambuc
410*3e1db26aSLionel Sambuc if (!Isdigit(c))
411*3e1db26aSLionel Sambuc return CC_ERROR;
412*3e1db26aSLionel Sambuc
413*3e1db26aSLionel Sambuc if (el->el_state.doingarg) {
414*3e1db26aSLionel Sambuc if (el->el_state.argument > 1000000)
415*3e1db26aSLionel Sambuc return CC_ERROR;
416*3e1db26aSLionel Sambuc el->el_state.argument = (el->el_state.argument * 10) +
417*3e1db26aSLionel Sambuc (c - '0');
418*3e1db26aSLionel Sambuc } else { /* else starting an argument */
419*3e1db26aSLionel Sambuc el->el_state.argument = c - '0';
420*3e1db26aSLionel Sambuc el->el_state.doingarg = 1;
421*3e1db26aSLionel Sambuc }
422*3e1db26aSLionel Sambuc return CC_ARGHACK;
423*3e1db26aSLionel Sambuc }
424*3e1db26aSLionel Sambuc
425*3e1db26aSLionel Sambuc
426*3e1db26aSLionel Sambuc /* ed_unassigned():
427*3e1db26aSLionel Sambuc * Indicates unbound character
428*3e1db26aSLionel Sambuc * Bound to keys that are not assigned
429*3e1db26aSLionel Sambuc */
430*3e1db26aSLionel Sambuc protected el_action_t
431*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_unassigned(EditLine * el,Int c)432*3e1db26aSLionel Sambuc ed_unassigned(EditLine *el __attribute__((__unused__)),
433*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
434*3e1db26aSLionel Sambuc {
435*3e1db26aSLionel Sambuc
436*3e1db26aSLionel Sambuc return CC_ERROR;
437*3e1db26aSLionel Sambuc }
438*3e1db26aSLionel Sambuc
439*3e1db26aSLionel Sambuc
440*3e1db26aSLionel Sambuc /**
441*3e1db26aSLionel Sambuc ** TTY key handling.
442*3e1db26aSLionel Sambuc **/
443*3e1db26aSLionel Sambuc
444*3e1db26aSLionel Sambuc /* ed_tty_sigint():
445*3e1db26aSLionel Sambuc * Tty interrupt character
446*3e1db26aSLionel Sambuc * [^C]
447*3e1db26aSLionel Sambuc */
448*3e1db26aSLionel Sambuc protected el_action_t
449*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_sigint(EditLine * el,Int c)450*3e1db26aSLionel Sambuc ed_tty_sigint(EditLine *el __attribute__((__unused__)),
451*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
452*3e1db26aSLionel Sambuc {
453*3e1db26aSLionel Sambuc
454*3e1db26aSLionel Sambuc return CC_NORM;
455*3e1db26aSLionel Sambuc }
456*3e1db26aSLionel Sambuc
457*3e1db26aSLionel Sambuc
458*3e1db26aSLionel Sambuc /* ed_tty_dsusp():
459*3e1db26aSLionel Sambuc * Tty delayed suspend character
460*3e1db26aSLionel Sambuc * [^Y]
461*3e1db26aSLionel Sambuc */
462*3e1db26aSLionel Sambuc protected el_action_t
463*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_dsusp(EditLine * el,Int c)464*3e1db26aSLionel Sambuc ed_tty_dsusp(EditLine *el __attribute__((__unused__)),
465*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
466*3e1db26aSLionel Sambuc {
467*3e1db26aSLionel Sambuc
468*3e1db26aSLionel Sambuc return CC_NORM;
469*3e1db26aSLionel Sambuc }
470*3e1db26aSLionel Sambuc
471*3e1db26aSLionel Sambuc
472*3e1db26aSLionel Sambuc /* ed_tty_flush_output():
473*3e1db26aSLionel Sambuc * Tty flush output characters
474*3e1db26aSLionel Sambuc * [^O]
475*3e1db26aSLionel Sambuc */
476*3e1db26aSLionel Sambuc protected el_action_t
477*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_flush_output(EditLine * el,Int c)478*3e1db26aSLionel Sambuc ed_tty_flush_output(EditLine *el __attribute__((__unused__)),
479*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
480*3e1db26aSLionel Sambuc {
481*3e1db26aSLionel Sambuc
482*3e1db26aSLionel Sambuc return CC_NORM;
483*3e1db26aSLionel Sambuc }
484*3e1db26aSLionel Sambuc
485*3e1db26aSLionel Sambuc
486*3e1db26aSLionel Sambuc /* ed_tty_sigquit():
487*3e1db26aSLionel Sambuc * Tty quit character
488*3e1db26aSLionel Sambuc * [^\]
489*3e1db26aSLionel Sambuc */
490*3e1db26aSLionel Sambuc protected el_action_t
491*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_sigquit(EditLine * el,Int c)492*3e1db26aSLionel Sambuc ed_tty_sigquit(EditLine *el __attribute__((__unused__)),
493*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
494*3e1db26aSLionel Sambuc {
495*3e1db26aSLionel Sambuc
496*3e1db26aSLionel Sambuc return CC_NORM;
497*3e1db26aSLionel Sambuc }
498*3e1db26aSLionel Sambuc
499*3e1db26aSLionel Sambuc
500*3e1db26aSLionel Sambuc /* ed_tty_sigtstp():
501*3e1db26aSLionel Sambuc * Tty suspend character
502*3e1db26aSLionel Sambuc * [^Z]
503*3e1db26aSLionel Sambuc */
504*3e1db26aSLionel Sambuc protected el_action_t
505*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_sigtstp(EditLine * el,Int c)506*3e1db26aSLionel Sambuc ed_tty_sigtstp(EditLine *el __attribute__((__unused__)),
507*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
508*3e1db26aSLionel Sambuc {
509*3e1db26aSLionel Sambuc
510*3e1db26aSLionel Sambuc return CC_NORM;
511*3e1db26aSLionel Sambuc }
512*3e1db26aSLionel Sambuc
513*3e1db26aSLionel Sambuc
514*3e1db26aSLionel Sambuc /* ed_tty_stop_output():
515*3e1db26aSLionel Sambuc * Tty disallow output characters
516*3e1db26aSLionel Sambuc * [^S]
517*3e1db26aSLionel Sambuc */
518*3e1db26aSLionel Sambuc protected el_action_t
519*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_stop_output(EditLine * el,Int c)520*3e1db26aSLionel Sambuc ed_tty_stop_output(EditLine *el __attribute__((__unused__)),
521*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
522*3e1db26aSLionel Sambuc {
523*3e1db26aSLionel Sambuc
524*3e1db26aSLionel Sambuc return CC_NORM;
525*3e1db26aSLionel Sambuc }
526*3e1db26aSLionel Sambuc
527*3e1db26aSLionel Sambuc
528*3e1db26aSLionel Sambuc /* ed_tty_start_output():
529*3e1db26aSLionel Sambuc * Tty allow output characters
530*3e1db26aSLionel Sambuc * [^Q]
531*3e1db26aSLionel Sambuc */
532*3e1db26aSLionel Sambuc protected el_action_t
533*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_tty_start_output(EditLine * el,Int c)534*3e1db26aSLionel Sambuc ed_tty_start_output(EditLine *el __attribute__((__unused__)),
535*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
536*3e1db26aSLionel Sambuc {
537*3e1db26aSLionel Sambuc
538*3e1db26aSLionel Sambuc return CC_NORM;
539*3e1db26aSLionel Sambuc }
540*3e1db26aSLionel Sambuc
541*3e1db26aSLionel Sambuc
542*3e1db26aSLionel Sambuc /* ed_newline():
543*3e1db26aSLionel Sambuc * Execute command
544*3e1db26aSLionel Sambuc * [^J]
545*3e1db26aSLionel Sambuc */
546*3e1db26aSLionel Sambuc protected el_action_t
547*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_newline(EditLine * el,Int c)548*3e1db26aSLionel Sambuc ed_newline(EditLine *el, Int c __attribute__((__unused__)))
549*3e1db26aSLionel Sambuc {
550*3e1db26aSLionel Sambuc
551*3e1db26aSLionel Sambuc re_goto_bottom(el);
552*3e1db26aSLionel Sambuc *el->el_line.lastchar++ = '\n';
553*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0';
554*3e1db26aSLionel Sambuc return CC_NEWLINE;
555*3e1db26aSLionel Sambuc }
556*3e1db26aSLionel Sambuc
557*3e1db26aSLionel Sambuc
558*3e1db26aSLionel Sambuc /* ed_delete_prev_char():
559*3e1db26aSLionel Sambuc * Delete the character to the left of the cursor
560*3e1db26aSLionel Sambuc * [^?]
561*3e1db26aSLionel Sambuc */
562*3e1db26aSLionel Sambuc protected el_action_t
563*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_delete_prev_char(EditLine * el,Int c)564*3e1db26aSLionel Sambuc ed_delete_prev_char(EditLine *el, Int c __attribute__((__unused__)))
565*3e1db26aSLionel Sambuc {
566*3e1db26aSLionel Sambuc
567*3e1db26aSLionel Sambuc if (el->el_line.cursor <= el->el_line.buffer)
568*3e1db26aSLionel Sambuc return CC_ERROR;
569*3e1db26aSLionel Sambuc
570*3e1db26aSLionel Sambuc c_delbefore(el, el->el_state.argument);
571*3e1db26aSLionel Sambuc el->el_line.cursor -= el->el_state.argument;
572*3e1db26aSLionel Sambuc if (el->el_line.cursor < el->el_line.buffer)
573*3e1db26aSLionel Sambuc el->el_line.cursor = el->el_line.buffer;
574*3e1db26aSLionel Sambuc return CC_REFRESH;
575*3e1db26aSLionel Sambuc }
576*3e1db26aSLionel Sambuc
577*3e1db26aSLionel Sambuc
578*3e1db26aSLionel Sambuc /* ed_clear_screen():
579*3e1db26aSLionel Sambuc * Clear screen leaving current line at the top
580*3e1db26aSLionel Sambuc * [^L]
581*3e1db26aSLionel Sambuc */
582*3e1db26aSLionel Sambuc protected el_action_t
583*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_clear_screen(EditLine * el,Int c)584*3e1db26aSLionel Sambuc ed_clear_screen(EditLine *el, Int c __attribute__((__unused__)))
585*3e1db26aSLionel Sambuc {
586*3e1db26aSLionel Sambuc
587*3e1db26aSLionel Sambuc terminal_clear_screen(el); /* clear the whole real screen */
588*3e1db26aSLionel Sambuc re_clear_display(el); /* reset everything */
589*3e1db26aSLionel Sambuc return CC_REFRESH;
590*3e1db26aSLionel Sambuc }
591*3e1db26aSLionel Sambuc
592*3e1db26aSLionel Sambuc
593*3e1db26aSLionel Sambuc /* ed_redisplay():
594*3e1db26aSLionel Sambuc * Redisplay everything
595*3e1db26aSLionel Sambuc * ^R
596*3e1db26aSLionel Sambuc */
597*3e1db26aSLionel Sambuc protected el_action_t
598*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_redisplay(EditLine * el,Int c)599*3e1db26aSLionel Sambuc ed_redisplay(EditLine *el __attribute__((__unused__)),
600*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
601*3e1db26aSLionel Sambuc {
602*3e1db26aSLionel Sambuc
603*3e1db26aSLionel Sambuc return CC_REDISPLAY;
604*3e1db26aSLionel Sambuc }
605*3e1db26aSLionel Sambuc
606*3e1db26aSLionel Sambuc
607*3e1db26aSLionel Sambuc /* ed_start_over():
608*3e1db26aSLionel Sambuc * Erase current line and start from scratch
609*3e1db26aSLionel Sambuc * [^G]
610*3e1db26aSLionel Sambuc */
611*3e1db26aSLionel Sambuc protected el_action_t
612*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_start_over(EditLine * el,Int c)613*3e1db26aSLionel Sambuc ed_start_over(EditLine *el, Int c __attribute__((__unused__)))
614*3e1db26aSLionel Sambuc {
615*3e1db26aSLionel Sambuc
616*3e1db26aSLionel Sambuc ch_reset(el, 0);
617*3e1db26aSLionel Sambuc return CC_REFRESH;
618*3e1db26aSLionel Sambuc }
619*3e1db26aSLionel Sambuc
620*3e1db26aSLionel Sambuc
621*3e1db26aSLionel Sambuc /* ed_sequence_lead_in():
622*3e1db26aSLionel Sambuc * First character in a bound sequence
623*3e1db26aSLionel Sambuc * Placeholder for external keys
624*3e1db26aSLionel Sambuc */
625*3e1db26aSLionel Sambuc protected el_action_t
626*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_sequence_lead_in(EditLine * el,Int c)627*3e1db26aSLionel Sambuc ed_sequence_lead_in(EditLine *el __attribute__((__unused__)),
628*3e1db26aSLionel Sambuc Int c __attribute__((__unused__)))
629*3e1db26aSLionel Sambuc {
630*3e1db26aSLionel Sambuc
631*3e1db26aSLionel Sambuc return CC_NORM;
632*3e1db26aSLionel Sambuc }
633*3e1db26aSLionel Sambuc
634*3e1db26aSLionel Sambuc
635*3e1db26aSLionel Sambuc /* ed_prev_history():
636*3e1db26aSLionel Sambuc * Move to the previous history line
637*3e1db26aSLionel Sambuc * [^P] [k]
638*3e1db26aSLionel Sambuc */
639*3e1db26aSLionel Sambuc protected el_action_t
640*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_prev_history(EditLine * el,Int c)641*3e1db26aSLionel Sambuc ed_prev_history(EditLine *el, Int c __attribute__((__unused__)))
642*3e1db26aSLionel Sambuc {
643*3e1db26aSLionel Sambuc char beep = 0;
644*3e1db26aSLionel Sambuc int sv_event = el->el_history.eventno;
645*3e1db26aSLionel Sambuc
646*3e1db26aSLionel Sambuc el->el_chared.c_undo.len = -1;
647*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0'; /* just in case */
648*3e1db26aSLionel Sambuc
649*3e1db26aSLionel Sambuc if (el->el_history.eventno == 0) { /* save the current buffer
650*3e1db26aSLionel Sambuc * away */
651*3e1db26aSLionel Sambuc (void) Strncpy(el->el_history.buf, el->el_line.buffer,
652*3e1db26aSLionel Sambuc EL_BUFSIZ);
653*3e1db26aSLionel Sambuc el->el_history.last = el->el_history.buf +
654*3e1db26aSLionel Sambuc (el->el_line.lastchar - el->el_line.buffer);
655*3e1db26aSLionel Sambuc }
656*3e1db26aSLionel Sambuc el->el_history.eventno += el->el_state.argument;
657*3e1db26aSLionel Sambuc
658*3e1db26aSLionel Sambuc if (hist_get(el) == CC_ERROR) {
659*3e1db26aSLionel Sambuc if (el->el_map.type == MAP_VI) {
660*3e1db26aSLionel Sambuc el->el_history.eventno = sv_event;
661*3e1db26aSLionel Sambuc
662*3e1db26aSLionel Sambuc }
663*3e1db26aSLionel Sambuc beep = 1;
664*3e1db26aSLionel Sambuc /* el->el_history.eventno was fixed by first call */
665*3e1db26aSLionel Sambuc (void) hist_get(el);
666*3e1db26aSLionel Sambuc }
667*3e1db26aSLionel Sambuc if (beep)
668*3e1db26aSLionel Sambuc return CC_REFRESH_BEEP;
669*3e1db26aSLionel Sambuc return CC_REFRESH;
670*3e1db26aSLionel Sambuc }
671*3e1db26aSLionel Sambuc
672*3e1db26aSLionel Sambuc
673*3e1db26aSLionel Sambuc /* ed_next_history():
674*3e1db26aSLionel Sambuc * Move to the next history line
675*3e1db26aSLionel Sambuc * [^N] [j]
676*3e1db26aSLionel Sambuc */
677*3e1db26aSLionel Sambuc protected el_action_t
678*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_next_history(EditLine * el,Int c)679*3e1db26aSLionel Sambuc ed_next_history(EditLine *el, Int c __attribute__((__unused__)))
680*3e1db26aSLionel Sambuc {
681*3e1db26aSLionel Sambuc el_action_t beep = CC_REFRESH, rval;
682*3e1db26aSLionel Sambuc
683*3e1db26aSLionel Sambuc el->el_chared.c_undo.len = -1;
684*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0'; /* just in case */
685*3e1db26aSLionel Sambuc
686*3e1db26aSLionel Sambuc el->el_history.eventno -= el->el_state.argument;
687*3e1db26aSLionel Sambuc
688*3e1db26aSLionel Sambuc if (el->el_history.eventno < 0) {
689*3e1db26aSLionel Sambuc el->el_history.eventno = 0;
690*3e1db26aSLionel Sambuc beep = CC_REFRESH_BEEP;
691*3e1db26aSLionel Sambuc }
692*3e1db26aSLionel Sambuc rval = hist_get(el);
693*3e1db26aSLionel Sambuc if (rval == CC_REFRESH)
694*3e1db26aSLionel Sambuc return beep;
695*3e1db26aSLionel Sambuc return rval;
696*3e1db26aSLionel Sambuc
697*3e1db26aSLionel Sambuc }
698*3e1db26aSLionel Sambuc
699*3e1db26aSLionel Sambuc
700*3e1db26aSLionel Sambuc /* ed_search_prev_history():
701*3e1db26aSLionel Sambuc * Search previous in history for a line matching the current
702*3e1db26aSLionel Sambuc * next search history [M-P] [K]
703*3e1db26aSLionel Sambuc */
704*3e1db26aSLionel Sambuc protected el_action_t
705*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_search_prev_history(EditLine * el,Int c)706*3e1db26aSLionel Sambuc ed_search_prev_history(EditLine *el, Int c __attribute__((__unused__)))
707*3e1db26aSLionel Sambuc {
708*3e1db26aSLionel Sambuc const Char *hp;
709*3e1db26aSLionel Sambuc int h;
710*3e1db26aSLionel Sambuc bool_t found = 0;
711*3e1db26aSLionel Sambuc
712*3e1db26aSLionel Sambuc el->el_chared.c_vcmd.action = NOP;
713*3e1db26aSLionel Sambuc el->el_chared.c_undo.len = -1;
714*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0'; /* just in case */
715*3e1db26aSLionel Sambuc if (el->el_history.eventno < 0) {
716*3e1db26aSLionel Sambuc #ifdef DEBUG_EDIT
717*3e1db26aSLionel Sambuc (void) fprintf(el->el_errfile,
718*3e1db26aSLionel Sambuc "e_prev_search_hist(): eventno < 0;\n");
719*3e1db26aSLionel Sambuc #endif
720*3e1db26aSLionel Sambuc el->el_history.eventno = 0;
721*3e1db26aSLionel Sambuc return CC_ERROR;
722*3e1db26aSLionel Sambuc }
723*3e1db26aSLionel Sambuc if (el->el_history.eventno == 0) {
724*3e1db26aSLionel Sambuc (void) Strncpy(el->el_history.buf, el->el_line.buffer,
725*3e1db26aSLionel Sambuc EL_BUFSIZ);
726*3e1db26aSLionel Sambuc el->el_history.last = el->el_history.buf +
727*3e1db26aSLionel Sambuc (el->el_line.lastchar - el->el_line.buffer);
728*3e1db26aSLionel Sambuc }
729*3e1db26aSLionel Sambuc if (el->el_history.ref == NULL)
730*3e1db26aSLionel Sambuc return CC_ERROR;
731*3e1db26aSLionel Sambuc
732*3e1db26aSLionel Sambuc hp = HIST_FIRST(el);
733*3e1db26aSLionel Sambuc if (hp == NULL)
734*3e1db26aSLionel Sambuc return CC_ERROR;
735*3e1db26aSLionel Sambuc
736*3e1db26aSLionel Sambuc c_setpat(el); /* Set search pattern !! */
737*3e1db26aSLionel Sambuc
738*3e1db26aSLionel Sambuc for (h = 1; h <= el->el_history.eventno; h++)
739*3e1db26aSLionel Sambuc hp = HIST_NEXT(el);
740*3e1db26aSLionel Sambuc
741*3e1db26aSLionel Sambuc while (hp != NULL) {
742*3e1db26aSLionel Sambuc #ifdef SDEBUG
743*3e1db26aSLionel Sambuc (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
744*3e1db26aSLionel Sambuc #endif
745*3e1db26aSLionel Sambuc if ((Strncmp(hp, el->el_line.buffer, (size_t)
746*3e1db26aSLionel Sambuc (el->el_line.lastchar - el->el_line.buffer)) ||
747*3e1db26aSLionel Sambuc hp[el->el_line.lastchar - el->el_line.buffer]) &&
748*3e1db26aSLionel Sambuc c_hmatch(el, hp)) {
749*3e1db26aSLionel Sambuc found++;
750*3e1db26aSLionel Sambuc break;
751*3e1db26aSLionel Sambuc }
752*3e1db26aSLionel Sambuc h++;
753*3e1db26aSLionel Sambuc hp = HIST_NEXT(el);
754*3e1db26aSLionel Sambuc }
755*3e1db26aSLionel Sambuc
756*3e1db26aSLionel Sambuc if (!found) {
757*3e1db26aSLionel Sambuc #ifdef SDEBUG
758*3e1db26aSLionel Sambuc (void) fprintf(el->el_errfile, "not found\n");
759*3e1db26aSLionel Sambuc #endif
760*3e1db26aSLionel Sambuc return CC_ERROR;
761*3e1db26aSLionel Sambuc }
762*3e1db26aSLionel Sambuc el->el_history.eventno = h;
763*3e1db26aSLionel Sambuc
764*3e1db26aSLionel Sambuc return hist_get(el);
765*3e1db26aSLionel Sambuc }
766*3e1db26aSLionel Sambuc
767*3e1db26aSLionel Sambuc
768*3e1db26aSLionel Sambuc /* ed_search_next_history():
769*3e1db26aSLionel Sambuc * Search next in history for a line matching the current
770*3e1db26aSLionel Sambuc * [M-N] [J]
771*3e1db26aSLionel Sambuc */
772*3e1db26aSLionel Sambuc protected el_action_t
773*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_search_next_history(EditLine * el,Int c)774*3e1db26aSLionel Sambuc ed_search_next_history(EditLine *el, Int c __attribute__((__unused__)))
775*3e1db26aSLionel Sambuc {
776*3e1db26aSLionel Sambuc const Char *hp;
777*3e1db26aSLionel Sambuc int h;
778*3e1db26aSLionel Sambuc bool_t found = 0;
779*3e1db26aSLionel Sambuc
780*3e1db26aSLionel Sambuc el->el_chared.c_vcmd.action = NOP;
781*3e1db26aSLionel Sambuc el->el_chared.c_undo.len = -1;
782*3e1db26aSLionel Sambuc *el->el_line.lastchar = '\0'; /* just in case */
783*3e1db26aSLionel Sambuc
784*3e1db26aSLionel Sambuc if (el->el_history.eventno == 0)
785*3e1db26aSLionel Sambuc return CC_ERROR;
786*3e1db26aSLionel Sambuc
787*3e1db26aSLionel Sambuc if (el->el_history.ref == NULL)
788*3e1db26aSLionel Sambuc return CC_ERROR;
789*3e1db26aSLionel Sambuc
790*3e1db26aSLionel Sambuc hp = HIST_FIRST(el);
791*3e1db26aSLionel Sambuc if (hp == NULL)
792*3e1db26aSLionel Sambuc return CC_ERROR;
793*3e1db26aSLionel Sambuc
794*3e1db26aSLionel Sambuc c_setpat(el); /* Set search pattern !! */
795*3e1db26aSLionel Sambuc
796*3e1db26aSLionel Sambuc for (h = 1; h < el->el_history.eventno && hp; h++) {
797*3e1db26aSLionel Sambuc #ifdef SDEBUG
798*3e1db26aSLionel Sambuc (void) fprintf(el->el_errfile, "Comparing with \"%s\"\n", hp);
799*3e1db26aSLionel Sambuc #endif
800*3e1db26aSLionel Sambuc if ((Strncmp(hp, el->el_line.buffer, (size_t)
801*3e1db26aSLionel Sambuc (el->el_line.lastchar - el->el_line.buffer)) ||
802*3e1db26aSLionel Sambuc hp[el->el_line.lastchar - el->el_line.buffer]) &&
803*3e1db26aSLionel Sambuc c_hmatch(el, hp))
804*3e1db26aSLionel Sambuc found = h;
805*3e1db26aSLionel Sambuc hp = HIST_NEXT(el);
806*3e1db26aSLionel Sambuc }
807*3e1db26aSLionel Sambuc
808*3e1db26aSLionel Sambuc if (!found) { /* is it the current history number? */
809*3e1db26aSLionel Sambuc if (!c_hmatch(el, el->el_history.buf)) {
810*3e1db26aSLionel Sambuc #ifdef SDEBUG
811*3e1db26aSLionel Sambuc (void) fprintf(el->el_errfile, "not found\n");
812*3e1db26aSLionel Sambuc #endif
813*3e1db26aSLionel Sambuc return CC_ERROR;
814*3e1db26aSLionel Sambuc }
815*3e1db26aSLionel Sambuc }
816*3e1db26aSLionel Sambuc el->el_history.eventno = found;
817*3e1db26aSLionel Sambuc
818*3e1db26aSLionel Sambuc return hist_get(el);
819*3e1db26aSLionel Sambuc }
820*3e1db26aSLionel Sambuc
821*3e1db26aSLionel Sambuc
822*3e1db26aSLionel Sambuc /* ed_prev_line():
823*3e1db26aSLionel Sambuc * Move up one line
824*3e1db26aSLionel Sambuc * Could be [k] [^p]
825*3e1db26aSLionel Sambuc */
826*3e1db26aSLionel Sambuc protected el_action_t
827*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_prev_line(EditLine * el,Int c)828*3e1db26aSLionel Sambuc ed_prev_line(EditLine *el, Int c __attribute__((__unused__)))
829*3e1db26aSLionel Sambuc {
830*3e1db26aSLionel Sambuc Char *ptr;
831*3e1db26aSLionel Sambuc int nchars = c_hpos(el);
832*3e1db26aSLionel Sambuc
833*3e1db26aSLionel Sambuc /*
834*3e1db26aSLionel Sambuc * Move to the line requested
835*3e1db26aSLionel Sambuc */
836*3e1db26aSLionel Sambuc if (*(ptr = el->el_line.cursor) == '\n')
837*3e1db26aSLionel Sambuc ptr--;
838*3e1db26aSLionel Sambuc
839*3e1db26aSLionel Sambuc for (; ptr >= el->el_line.buffer; ptr--)
840*3e1db26aSLionel Sambuc if (*ptr == '\n' && --el->el_state.argument <= 0)
841*3e1db26aSLionel Sambuc break;
842*3e1db26aSLionel Sambuc
843*3e1db26aSLionel Sambuc if (el->el_state.argument > 0)
844*3e1db26aSLionel Sambuc return CC_ERROR;
845*3e1db26aSLionel Sambuc
846*3e1db26aSLionel Sambuc /*
847*3e1db26aSLionel Sambuc * Move to the beginning of the line
848*3e1db26aSLionel Sambuc */
849*3e1db26aSLionel Sambuc for (ptr--; ptr >= el->el_line.buffer && *ptr != '\n'; ptr--)
850*3e1db26aSLionel Sambuc continue;
851*3e1db26aSLionel Sambuc
852*3e1db26aSLionel Sambuc /*
853*3e1db26aSLionel Sambuc * Move to the character requested
854*3e1db26aSLionel Sambuc */
855*3e1db26aSLionel Sambuc for (ptr++;
856*3e1db26aSLionel Sambuc nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
857*3e1db26aSLionel Sambuc ptr++)
858*3e1db26aSLionel Sambuc continue;
859*3e1db26aSLionel Sambuc
860*3e1db26aSLionel Sambuc el->el_line.cursor = ptr;
861*3e1db26aSLionel Sambuc return CC_CURSOR;
862*3e1db26aSLionel Sambuc }
863*3e1db26aSLionel Sambuc
864*3e1db26aSLionel Sambuc
865*3e1db26aSLionel Sambuc /* ed_next_line():
866*3e1db26aSLionel Sambuc * Move down one line
867*3e1db26aSLionel Sambuc * Could be [j] [^n]
868*3e1db26aSLionel Sambuc */
869*3e1db26aSLionel Sambuc protected el_action_t
870*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_next_line(EditLine * el,Int c)871*3e1db26aSLionel Sambuc ed_next_line(EditLine *el, Int c __attribute__((__unused__)))
872*3e1db26aSLionel Sambuc {
873*3e1db26aSLionel Sambuc Char *ptr;
874*3e1db26aSLionel Sambuc int nchars = c_hpos(el);
875*3e1db26aSLionel Sambuc
876*3e1db26aSLionel Sambuc /*
877*3e1db26aSLionel Sambuc * Move to the line requested
878*3e1db26aSLionel Sambuc */
879*3e1db26aSLionel Sambuc for (ptr = el->el_line.cursor; ptr < el->el_line.lastchar; ptr++)
880*3e1db26aSLionel Sambuc if (*ptr == '\n' && --el->el_state.argument <= 0)
881*3e1db26aSLionel Sambuc break;
882*3e1db26aSLionel Sambuc
883*3e1db26aSLionel Sambuc if (el->el_state.argument > 0)
884*3e1db26aSLionel Sambuc return CC_ERROR;
885*3e1db26aSLionel Sambuc
886*3e1db26aSLionel Sambuc /*
887*3e1db26aSLionel Sambuc * Move to the character requested
888*3e1db26aSLionel Sambuc */
889*3e1db26aSLionel Sambuc for (ptr++;
890*3e1db26aSLionel Sambuc nchars-- > 0 && ptr < el->el_line.lastchar && *ptr != '\n';
891*3e1db26aSLionel Sambuc ptr++)
892*3e1db26aSLionel Sambuc continue;
893*3e1db26aSLionel Sambuc
894*3e1db26aSLionel Sambuc el->el_line.cursor = ptr;
895*3e1db26aSLionel Sambuc return CC_CURSOR;
896*3e1db26aSLionel Sambuc }
897*3e1db26aSLionel Sambuc
898*3e1db26aSLionel Sambuc
899*3e1db26aSLionel Sambuc /* ed_command():
900*3e1db26aSLionel Sambuc * Editline extended command
901*3e1db26aSLionel Sambuc * [M-X] [:]
902*3e1db26aSLionel Sambuc */
903*3e1db26aSLionel Sambuc protected el_action_t
904*3e1db26aSLionel Sambuc /*ARGSUSED*/
ed_command(EditLine * el,Int c)905*3e1db26aSLionel Sambuc ed_command(EditLine *el, Int c __attribute__((__unused__)))
906*3e1db26aSLionel Sambuc {
907*3e1db26aSLionel Sambuc Char tmpbuf[EL_BUFSIZ];
908*3e1db26aSLionel Sambuc int tmplen;
909*3e1db26aSLionel Sambuc
910*3e1db26aSLionel Sambuc tmplen = c_gets(el, tmpbuf, STR("\n: "));
911*3e1db26aSLionel Sambuc terminal__putc(el, '\n');
912*3e1db26aSLionel Sambuc
913*3e1db26aSLionel Sambuc if (tmplen < 0 || (tmpbuf[tmplen] = 0, parse_line(el, tmpbuf)) == -1)
914*3e1db26aSLionel Sambuc terminal_beep(el);
915*3e1db26aSLionel Sambuc
916*3e1db26aSLionel Sambuc el->el_map.current = el->el_map.key;
917*3e1db26aSLionel Sambuc re_clear_display(el);
918*3e1db26aSLionel Sambuc return CC_REFRESH;
919*3e1db26aSLionel Sambuc }
920