chared.c revision 1.11 1 1.11 christos /* $NetBSD: chared.c,v 1.11 2001/01/04 15:56:31 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.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.3 christos #include <sys/cdefs.h>
40 1.1 cgd #if !defined(lint) && !defined(SCCSID)
41 1.2 lukem #if 0
42 1.1 cgd static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
43 1.2 lukem #else
44 1.11 christos __RCSID("$NetBSD: chared.c,v 1.11 2001/01/04 15:56:31 christos Exp $");
45 1.2 lukem #endif
46 1.1 cgd #endif /* not lint && not SCCSID */
47 1.1 cgd
48 1.7 simonb /*
49 1.1 cgd * chared.c: Character editor utilities
50 1.1 cgd */
51 1.1 cgd #include "sys.h"
52 1.1 cgd
53 1.1 cgd #include <stdlib.h>
54 1.1 cgd #include "el.h"
55 1.1 cgd
56 1.1 cgd /* cv_undo():
57 1.1 cgd * Handle state for the vi undo command
58 1.1 cgd */
59 1.1 cgd protected void
60 1.9 lukem cv_undo(EditLine *el,int action, size_t size, char *ptr)
61 1.9 lukem {
62 1.9 lukem c_undo_t *vu = &el->el_chared.c_undo;
63 1.9 lukem vu->action = action;
64 1.9 lukem vu->ptr = ptr;
65 1.9 lukem vu->isize = size;
66 1.9 lukem (void) memcpy(vu->buf, vu->ptr, size);
67 1.1 cgd #ifdef DEBUG_UNDO
68 1.9 lukem (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
69 1.9 lukem vu->ptr, vu->isize, vu->dsize);
70 1.1 cgd #endif
71 1.1 cgd }
72 1.1 cgd
73 1.1 cgd
74 1.7 simonb /* c_insert():
75 1.1 cgd * Insert num characters
76 1.1 cgd */
77 1.1 cgd protected void
78 1.9 lukem c_insert(EditLine *el, int num)
79 1.9 lukem {
80 1.9 lukem char *cp;
81 1.9 lukem
82 1.9 lukem if (el->el_line.lastchar + num >= el->el_line.limit)
83 1.9 lukem return; /* can't go past end of buffer */
84 1.9 lukem
85 1.9 lukem if (el->el_line.cursor < el->el_line.lastchar) {
86 1.9 lukem /* if I must move chars */
87 1.9 lukem for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
88 1.9 lukem cp[num] = *cp;
89 1.9 lukem }
90 1.9 lukem el->el_line.lastchar += num;
91 1.9 lukem }
92 1.1 cgd
93 1.1 cgd
94 1.1 cgd /* c_delafter():
95 1.1 cgd * Delete num characters after the cursor
96 1.1 cgd */
97 1.1 cgd protected void
98 1.9 lukem c_delafter(EditLine *el, int num)
99 1.1 cgd {
100 1.1 cgd
101 1.9 lukem if (el->el_line.cursor + num > el->el_line.lastchar)
102 1.9 lukem num = el->el_line.lastchar - el->el_line.cursor;
103 1.1 cgd
104 1.9 lukem if (num > 0) {
105 1.9 lukem char *cp;
106 1.1 cgd
107 1.9 lukem if (el->el_map.current != el->el_map.emacs)
108 1.9 lukem cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
109 1.1 cgd
110 1.9 lukem for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
111 1.9 lukem *cp = cp[num];
112 1.1 cgd
113 1.9 lukem el->el_line.lastchar -= num;
114 1.9 lukem }
115 1.1 cgd }
116 1.1 cgd
117 1.1 cgd
118 1.1 cgd /* c_delbefore():
119 1.1 cgd * Delete num characters before the cursor
120 1.1 cgd */
121 1.1 cgd protected void
122 1.9 lukem c_delbefore(EditLine *el, int num)
123 1.1 cgd {
124 1.1 cgd
125 1.9 lukem if (el->el_line.cursor - num < el->el_line.buffer)
126 1.9 lukem num = el->el_line.cursor - el->el_line.buffer;
127 1.1 cgd
128 1.9 lukem if (num > 0) {
129 1.9 lukem char *cp;
130 1.1 cgd
131 1.9 lukem if (el->el_map.current != el->el_map.emacs)
132 1.9 lukem cv_undo(el, INSERT, (size_t)num,
133 1.9 lukem el->el_line.cursor - num);
134 1.1 cgd
135 1.9 lukem for (cp = el->el_line.cursor - num;
136 1.9 lukem cp <= el->el_line.lastchar;
137 1.9 lukem cp++)
138 1.9 lukem *cp = cp[num];
139 1.1 cgd
140 1.9 lukem el->el_line.lastchar -= num;
141 1.9 lukem }
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd
145 1.1 cgd /* ce__isword():
146 1.1 cgd * Return if p is part of a word according to emacs
147 1.1 cgd */
148 1.1 cgd protected int
149 1.9 lukem ce__isword(int p)
150 1.1 cgd {
151 1.9 lukem return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
152 1.1 cgd }
153 1.1 cgd
154 1.1 cgd
155 1.1 cgd /* cv__isword():
156 1.1 cgd * Return if p is part of a word according to vi
157 1.1 cgd */
158 1.1 cgd protected int
159 1.9 lukem cv__isword(int p)
160 1.1 cgd {
161 1.9 lukem return (!isspace(p));
162 1.1 cgd }
163 1.1 cgd
164 1.1 cgd
165 1.1 cgd /* c__prev_word():
166 1.1 cgd * Find the previous word
167 1.1 cgd */
168 1.1 cgd protected char *
169 1.9 lukem c__prev_word(char *p, char *low, int n, int (*wtest)(int))
170 1.9 lukem {
171 1.9 lukem p--;
172 1.9 lukem
173 1.9 lukem while (n--) {
174 1.9 lukem while ((p >= low) && !(*wtest)((unsigned char) *p))
175 1.9 lukem p--;
176 1.9 lukem while ((p >= low) && (*wtest)((unsigned char) *p))
177 1.9 lukem p--;
178 1.9 lukem }
179 1.9 lukem
180 1.9 lukem /* cp now points to one character before the word */
181 1.9 lukem p++;
182 1.9 lukem if (p < low)
183 1.9 lukem p = low;
184 1.9 lukem /* cp now points where we want it */
185 1.9 lukem return (p);
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd
189 1.1 cgd /* c__next_word():
190 1.1 cgd * Find the next word
191 1.1 cgd */
192 1.1 cgd protected char *
193 1.9 lukem c__next_word(char *p, char *high, int n, int (*wtest)(int))
194 1.9 lukem {
195 1.9 lukem while (n--) {
196 1.9 lukem while ((p < high) && !(*wtest)((unsigned char) *p))
197 1.9 lukem p++;
198 1.9 lukem while ((p < high) && (*wtest)((unsigned char) *p))
199 1.9 lukem p++;
200 1.9 lukem }
201 1.9 lukem if (p > high)
202 1.9 lukem p = high;
203 1.9 lukem /* p now points where we want it */
204 1.9 lukem return (p);
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd /* cv_next_word():
208 1.1 cgd * Find the next word vi style
209 1.1 cgd */
210 1.1 cgd protected char *
211 1.9 lukem cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
212 1.9 lukem {
213 1.9 lukem int test;
214 1.9 lukem
215 1.9 lukem while (n--) {
216 1.9 lukem test = (*wtest)((unsigned char) *p);
217 1.9 lukem while ((p < high) && (*wtest)((unsigned char) *p) == test)
218 1.9 lukem p++;
219 1.9 lukem /*
220 1.9 lukem * vi historically deletes with cw only the word preserving the
221 1.9 lukem * trailing whitespace! This is not what 'w' does..
222 1.9 lukem */
223 1.9 lukem if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
224 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
225 1.9 lukem p++;
226 1.9 lukem }
227 1.1 cgd
228 1.9 lukem /* p now points where we want it */
229 1.9 lukem if (p > high)
230 1.9 lukem return (high);
231 1.9 lukem else
232 1.9 lukem return (p);
233 1.1 cgd }
234 1.1 cgd
235 1.1 cgd
236 1.1 cgd /* cv_prev_word():
237 1.1 cgd * Find the previous word vi style
238 1.1 cgd */
239 1.1 cgd protected char *
240 1.9 lukem cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
241 1.1 cgd {
242 1.9 lukem int test;
243 1.1 cgd
244 1.9 lukem while (n--) {
245 1.1 cgd p--;
246 1.9 lukem /*
247 1.9 lukem * vi historically deletes with cb only the word preserving the
248 1.9 lukem * leading whitespace! This is not what 'b' does..
249 1.9 lukem */
250 1.9 lukem if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
251 1.9 lukem while ((p > low) && isspace((unsigned char) *p))
252 1.9 lukem p--;
253 1.9 lukem test = (*wtest)((unsigned char) *p);
254 1.9 lukem while ((p >= low) && (*wtest)((unsigned char) *p) == test)
255 1.9 lukem p--;
256 1.1 cgd p++;
257 1.9 lukem while (isspace((unsigned char) *p))
258 1.9 lukem p++;
259 1.9 lukem }
260 1.1 cgd
261 1.9 lukem /* p now points where we want it */
262 1.9 lukem if (p < low)
263 1.9 lukem return (low);
264 1.9 lukem else
265 1.9 lukem return (p);
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd
269 1.1 cgd #ifdef notdef
270 1.1 cgd /* c__number():
271 1.1 cgd * Ignore character p points to, return number appearing after that.
272 1.1 cgd * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
273 1.1 cgd * Return p pointing to last char used.
274 1.1 cgd */
275 1.1 cgd protected char *
276 1.9 lukem c__number(
277 1.9 lukem char *p, /* character position */
278 1.9 lukem int *num, /* Return value */
279 1.9 lukem int dval) /* dval is the number to subtract from like $-3 */
280 1.9 lukem {
281 1.9 lukem int i;
282 1.9 lukem int sign = 1;
283 1.9 lukem
284 1.9 lukem if (*++p == '^') {
285 1.9 lukem *num = 1;
286 1.9 lukem return (p);
287 1.9 lukem }
288 1.9 lukem if (*p == '$') {
289 1.9 lukem if (*++p != '-') {
290 1.9 lukem *num = 0x7fffffff; /* Handle $ */
291 1.9 lukem return (--p);
292 1.9 lukem }
293 1.9 lukem sign = -1; /* Handle $- */
294 1.9 lukem ++p;
295 1.9 lukem }
296 1.9 lukem for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
297 1.9 lukem continue;
298 1.9 lukem *num = (sign < 0 ? dval - i : i);
299 1.9 lukem return (--p);
300 1.1 cgd }
301 1.1 cgd #endif
302 1.1 cgd
303 1.1 cgd /* cv_delfini():
304 1.1 cgd * Finish vi delete action
305 1.1 cgd */
306 1.1 cgd protected void
307 1.9 lukem cv_delfini(EditLine *el)
308 1.1 cgd {
309 1.9 lukem int size;
310 1.9 lukem int oaction;
311 1.1 cgd
312 1.9 lukem if (el->el_chared.c_vcmd.action & INSERT)
313 1.9 lukem el->el_map.current = el->el_map.key;
314 1.1 cgd
315 1.9 lukem oaction = el->el_chared.c_vcmd.action;
316 1.9 lukem el->el_chared.c_vcmd.action = NOP;
317 1.9 lukem
318 1.9 lukem if (el->el_chared.c_vcmd.pos == 0)
319 1.9 lukem return;
320 1.9 lukem
321 1.9 lukem
322 1.9 lukem if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
323 1.9 lukem size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
324 1.9 lukem c_delbefore(el, size);
325 1.9 lukem el->el_line.cursor = el->el_chared.c_vcmd.pos;
326 1.9 lukem re_refresh_cursor(el);
327 1.9 lukem } else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
328 1.9 lukem size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
329 1.9 lukem c_delafter(el, size);
330 1.9 lukem } else {
331 1.9 lukem size = 1;
332 1.9 lukem c_delafter(el, size);
333 1.9 lukem }
334 1.9 lukem switch (oaction) {
335 1.9 lukem case DELETE|INSERT:
336 1.9 lukem el->el_chared.c_undo.action = DELETE|INSERT;
337 1.9 lukem break;
338 1.9 lukem case DELETE:
339 1.9 lukem el->el_chared.c_undo.action = INSERT;
340 1.9 lukem break;
341 1.9 lukem case NOP:
342 1.9 lukem case INSERT:
343 1.9 lukem default:
344 1.10 christos EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction));
345 1.9 lukem break;
346 1.9 lukem }
347 1.7 simonb
348 1.1 cgd
349 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
350 1.9 lukem el->el_chared.c_undo.dsize = size;
351 1.1 cgd }
352 1.1 cgd
353 1.1 cgd
354 1.1 cgd #ifdef notdef
355 1.1 cgd /* ce__endword():
356 1.1 cgd * Go to the end of this word according to emacs
357 1.1 cgd */
358 1.1 cgd protected char *
359 1.9 lukem ce__endword(char *p, char *high, int n)
360 1.9 lukem {
361 1.9 lukem p++;
362 1.1 cgd
363 1.9 lukem while (n--) {
364 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
365 1.9 lukem p++;
366 1.9 lukem while ((p < high) && !isspace((unsigned char) *p))
367 1.9 lukem p++;
368 1.9 lukem }
369 1.9 lukem
370 1.9 lukem p--;
371 1.9 lukem return (p);
372 1.1 cgd }
373 1.1 cgd #endif
374 1.1 cgd
375 1.1 cgd
376 1.1 cgd /* cv__endword():
377 1.1 cgd * Go to the end of this word according to vi
378 1.1 cgd */
379 1.1 cgd protected char *
380 1.9 lukem cv__endword(char *p, char *high, int n)
381 1.9 lukem {
382 1.9 lukem p++;
383 1.1 cgd
384 1.9 lukem while (n--) {
385 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
386 1.9 lukem p++;
387 1.9 lukem
388 1.9 lukem if (isalnum((unsigned char) *p))
389 1.9 lukem while ((p < high) && isalnum((unsigned char) *p))
390 1.9 lukem p++;
391 1.9 lukem else
392 1.9 lukem while ((p < high) && !(isspace((unsigned char) *p) ||
393 1.9 lukem isalnum((unsigned char) *p)))
394 1.9 lukem p++;
395 1.9 lukem }
396 1.9 lukem p--;
397 1.9 lukem return (p);
398 1.1 cgd }
399 1.1 cgd
400 1.1 cgd /* ch_init():
401 1.1 cgd * Initialize the character editor
402 1.1 cgd */
403 1.1 cgd protected int
404 1.9 lukem ch_init(EditLine *el)
405 1.1 cgd {
406 1.11 christos el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
407 1.11 christos if (el->el_line.buffer == NULL)
408 1.11 christos return (-1);
409 1.11 christos
410 1.9 lukem (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
411 1.9 lukem el->el_line.cursor = el->el_line.buffer;
412 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
413 1.9 lukem el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
414 1.9 lukem
415 1.11 christos el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
416 1.11 christos if (el->el_chared.c_undo.buf == NULL)
417 1.11 christos return (-1);
418 1.9 lukem (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
419 1.9 lukem el->el_chared.c_undo.action = NOP;
420 1.9 lukem el->el_chared.c_undo.isize = 0;
421 1.9 lukem el->el_chared.c_undo.dsize = 0;
422 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.buffer;
423 1.9 lukem
424 1.9 lukem el->el_chared.c_vcmd.action = NOP;
425 1.9 lukem el->el_chared.c_vcmd.pos = el->el_line.buffer;
426 1.9 lukem el->el_chared.c_vcmd.ins = el->el_line.buffer;
427 1.9 lukem
428 1.9 lukem el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
429 1.11 christos if (el->el_chared.c_kill.buf == NULL)
430 1.11 christos return (-1);
431 1.9 lukem (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
432 1.9 lukem el->el_chared.c_kill.mark = el->el_line.buffer;
433 1.9 lukem el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
434 1.9 lukem
435 1.9 lukem el->el_map.current = el->el_map.key;
436 1.9 lukem
437 1.9 lukem el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
438 1.9 lukem el->el_state.doingarg = 0;
439 1.9 lukem el->el_state.metanext = 0;
440 1.9 lukem el->el_state.argument = 1;
441 1.9 lukem el->el_state.lastcmd = ED_UNASSIGNED;
442 1.9 lukem
443 1.9 lukem el->el_chared.c_macro.nline = NULL;
444 1.9 lukem el->el_chared.c_macro.level = -1;
445 1.9 lukem el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
446 1.11 christos sizeof(char *));
447 1.11 christos if (el->el_chared.c_macro.macro == NULL)
448 1.11 christos return (-1);
449 1.9 lukem return (0);
450 1.1 cgd }
451 1.1 cgd
452 1.1 cgd /* ch_reset():
453 1.1 cgd * Reset the character editor
454 1.1 cgd */
455 1.1 cgd protected void
456 1.9 lukem ch_reset(EditLine *el)
457 1.1 cgd {
458 1.9 lukem el->el_line.cursor = el->el_line.buffer;
459 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
460 1.1 cgd
461 1.9 lukem el->el_chared.c_undo.action = NOP;
462 1.9 lukem el->el_chared.c_undo.isize = 0;
463 1.9 lukem el->el_chared.c_undo.dsize = 0;
464 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.buffer;
465 1.1 cgd
466 1.9 lukem el->el_chared.c_vcmd.action = NOP;
467 1.9 lukem el->el_chared.c_vcmd.pos = el->el_line.buffer;
468 1.9 lukem el->el_chared.c_vcmd.ins = el->el_line.buffer;
469 1.1 cgd
470 1.9 lukem el->el_chared.c_kill.mark = el->el_line.buffer;
471 1.1 cgd
472 1.9 lukem el->el_map.current = el->el_map.key;
473 1.1 cgd
474 1.9 lukem el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
475 1.9 lukem el->el_state.doingarg = 0;
476 1.9 lukem el->el_state.metanext = 0;
477 1.9 lukem el->el_state.argument = 1;
478 1.9 lukem el->el_state.lastcmd = ED_UNASSIGNED;
479 1.1 cgd
480 1.9 lukem el->el_chared.c_macro.level = -1;
481 1.1 cgd
482 1.9 lukem el->el_history.eventno = 0;
483 1.1 cgd }
484 1.1 cgd
485 1.1 cgd
486 1.1 cgd /* ch_end():
487 1.1 cgd * Free the data structures used by the editor
488 1.1 cgd */
489 1.1 cgd protected void
490 1.9 lukem ch_end(EditLine *el)
491 1.1 cgd {
492 1.9 lukem el_free((ptr_t) el->el_line.buffer);
493 1.9 lukem el->el_line.buffer = NULL;
494 1.9 lukem el->el_line.limit = NULL;
495 1.9 lukem el_free((ptr_t) el->el_chared.c_undo.buf);
496 1.9 lukem el->el_chared.c_undo.buf = NULL;
497 1.9 lukem el_free((ptr_t) el->el_chared.c_kill.buf);
498 1.9 lukem el->el_chared.c_kill.buf = NULL;
499 1.9 lukem el_free((ptr_t) el->el_chared.c_macro.macro);
500 1.9 lukem el->el_chared.c_macro.macro = NULL;
501 1.9 lukem ch_reset(el);
502 1.1 cgd }
503 1.1 cgd
504 1.1 cgd
505 1.1 cgd /* el_insertstr():
506 1.1 cgd * Insert string at cursorI
507 1.1 cgd */
508 1.1 cgd public int
509 1.9 lukem el_insertstr(EditLine *el, const char *s)
510 1.9 lukem {
511 1.9 lukem int len;
512 1.9 lukem
513 1.9 lukem if ((len = strlen(s)) == 0)
514 1.9 lukem return (-1);
515 1.9 lukem if (el->el_line.lastchar + len >= el->el_line.limit)
516 1.9 lukem return (-1);
517 1.9 lukem
518 1.9 lukem c_insert(el, len);
519 1.9 lukem while (*s)
520 1.9 lukem *el->el_line.cursor++ = *s++;
521 1.9 lukem return (0);
522 1.1 cgd }
523 1.1 cgd
524 1.1 cgd
525 1.1 cgd /* el_deletestr():
526 1.1 cgd * Delete num characters before the cursor
527 1.1 cgd */
528 1.1 cgd public void
529 1.9 lukem el_deletestr(EditLine *el, int n)
530 1.9 lukem {
531 1.9 lukem if (n <= 0)
532 1.9 lukem return;
533 1.9 lukem
534 1.9 lukem if (el->el_line.cursor < &el->el_line.buffer[n])
535 1.9 lukem return;
536 1.9 lukem
537 1.9 lukem c_delbefore(el, n); /* delete before dot */
538 1.9 lukem el->el_line.cursor -= n;
539 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
540 1.9 lukem el->el_line.cursor = el->el_line.buffer;
541 1.1 cgd }
542 1.1 cgd
543 1.1 cgd /* c_gets():
544 1.1 cgd * Get a string
545 1.1 cgd */
546 1.1 cgd protected int
547 1.9 lukem c_gets(EditLine *el, char *buf)
548 1.9 lukem {
549 1.9 lukem char ch;
550 1.9 lukem int len = 0;
551 1.1 cgd
552 1.9 lukem for (ch = 0; ch == 0;) {
553 1.9 lukem if (el_getc(el, &ch) != 1)
554 1.9 lukem return (ed_end_of_file(el, 0));
555 1.9 lukem switch (ch) {
556 1.9 lukem case 0010: /* Delete and backspace */
557 1.9 lukem case 0177:
558 1.9 lukem if (len > 1) {
559 1.9 lukem *el->el_line.cursor-- = '\0';
560 1.9 lukem el->el_line.lastchar = el->el_line.cursor;
561 1.9 lukem buf[len--] = '\0';
562 1.9 lukem } else {
563 1.9 lukem el->el_line.buffer[0] = '\0';
564 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
565 1.9 lukem el->el_line.cursor = el->el_line.buffer;
566 1.9 lukem return (CC_REFRESH);
567 1.9 lukem }
568 1.9 lukem re_refresh(el);
569 1.9 lukem ch = 0;
570 1.9 lukem break;
571 1.9 lukem
572 1.9 lukem case 0033: /* ESC */
573 1.9 lukem case '\r': /* Newline */
574 1.9 lukem case '\n':
575 1.9 lukem break;
576 1.9 lukem
577 1.9 lukem default:
578 1.9 lukem if (len >= EL_BUFSIZ)
579 1.9 lukem term_beep(el);
580 1.9 lukem else {
581 1.9 lukem buf[len++] = ch;
582 1.9 lukem *el->el_line.cursor++ = ch;
583 1.9 lukem el->el_line.lastchar = el->el_line.cursor;
584 1.9 lukem }
585 1.9 lukem re_refresh(el);
586 1.9 lukem ch = 0;
587 1.9 lukem break;
588 1.9 lukem }
589 1.9 lukem }
590 1.9 lukem buf[len] = ch;
591 1.9 lukem return (len);
592 1.1 cgd }
593 1.1 cgd
594 1.1 cgd
595 1.1 cgd /* c_hpos():
596 1.1 cgd * Return the current horizontal position of the cursor
597 1.1 cgd */
598 1.1 cgd protected int
599 1.9 lukem c_hpos(EditLine *el)
600 1.1 cgd {
601 1.9 lukem char *ptr;
602 1.1 cgd
603 1.9 lukem /*
604 1.9 lukem * Find how many characters till the beginning of this line.
605 1.9 lukem */
606 1.9 lukem if (el->el_line.cursor == el->el_line.buffer)
607 1.9 lukem return (0);
608 1.9 lukem else {
609 1.9 lukem for (ptr = el->el_line.cursor - 1;
610 1.9 lukem ptr >= el->el_line.buffer && *ptr != '\n';
611 1.9 lukem ptr--)
612 1.9 lukem continue;
613 1.9 lukem return (el->el_line.cursor - ptr - 1);
614 1.9 lukem }
615 1.1 cgd }
616