chared.c revision 1.14 1 1.14 christos /* $NetBSD: chared.c,v 1.14 2001/05/17 01:02:17 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.14 christos __RCSID("$NetBSD: chared.c,v 1.14 2001/05/17 01:02:17 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.12 jdolecek /* value to leave unused in line buffer */
57 1.12 jdolecek #define EL_LEAVE 2
58 1.12 jdolecek
59 1.1 cgd /* cv_undo():
60 1.1 cgd * Handle state for the vi undo command
61 1.1 cgd */
62 1.1 cgd protected void
63 1.9 lukem cv_undo(EditLine *el,int action, size_t size, char *ptr)
64 1.9 lukem {
65 1.9 lukem c_undo_t *vu = &el->el_chared.c_undo;
66 1.9 lukem vu->action = action;
67 1.9 lukem vu->ptr = ptr;
68 1.9 lukem vu->isize = size;
69 1.9 lukem (void) memcpy(vu->buf, vu->ptr, size);
70 1.1 cgd #ifdef DEBUG_UNDO
71 1.9 lukem (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
72 1.9 lukem vu->ptr, vu->isize, vu->dsize);
73 1.1 cgd #endif
74 1.1 cgd }
75 1.1 cgd
76 1.1 cgd
77 1.7 simonb /* c_insert():
78 1.1 cgd * Insert num characters
79 1.1 cgd */
80 1.1 cgd protected void
81 1.9 lukem c_insert(EditLine *el, int num)
82 1.9 lukem {
83 1.9 lukem char *cp;
84 1.9 lukem
85 1.9 lukem if (el->el_line.lastchar + num >= el->el_line.limit)
86 1.9 lukem return; /* can't go past end of buffer */
87 1.9 lukem
88 1.9 lukem if (el->el_line.cursor < el->el_line.lastchar) {
89 1.9 lukem /* if I must move chars */
90 1.9 lukem for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
91 1.9 lukem cp[num] = *cp;
92 1.9 lukem }
93 1.9 lukem el->el_line.lastchar += num;
94 1.9 lukem }
95 1.1 cgd
96 1.1 cgd
97 1.1 cgd /* c_delafter():
98 1.1 cgd * Delete num characters after the cursor
99 1.1 cgd */
100 1.1 cgd protected void
101 1.9 lukem c_delafter(EditLine *el, int num)
102 1.1 cgd {
103 1.1 cgd
104 1.9 lukem if (el->el_line.cursor + num > el->el_line.lastchar)
105 1.9 lukem num = el->el_line.lastchar - el->el_line.cursor;
106 1.1 cgd
107 1.9 lukem if (num > 0) {
108 1.9 lukem char *cp;
109 1.1 cgd
110 1.9 lukem if (el->el_map.current != el->el_map.emacs)
111 1.9 lukem cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
112 1.1 cgd
113 1.9 lukem for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
114 1.9 lukem *cp = cp[num];
115 1.1 cgd
116 1.9 lukem el->el_line.lastchar -= num;
117 1.9 lukem }
118 1.1 cgd }
119 1.1 cgd
120 1.1 cgd
121 1.1 cgd /* c_delbefore():
122 1.1 cgd * Delete num characters before the cursor
123 1.1 cgd */
124 1.1 cgd protected void
125 1.9 lukem c_delbefore(EditLine *el, int num)
126 1.1 cgd {
127 1.1 cgd
128 1.9 lukem if (el->el_line.cursor - num < el->el_line.buffer)
129 1.9 lukem num = el->el_line.cursor - el->el_line.buffer;
130 1.1 cgd
131 1.9 lukem if (num > 0) {
132 1.9 lukem char *cp;
133 1.1 cgd
134 1.9 lukem if (el->el_map.current != el->el_map.emacs)
135 1.9 lukem cv_undo(el, INSERT, (size_t)num,
136 1.9 lukem el->el_line.cursor - num);
137 1.1 cgd
138 1.9 lukem for (cp = el->el_line.cursor - num;
139 1.9 lukem cp <= el->el_line.lastchar;
140 1.9 lukem cp++)
141 1.9 lukem *cp = cp[num];
142 1.1 cgd
143 1.9 lukem el->el_line.lastchar -= num;
144 1.9 lukem }
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd
148 1.1 cgd /* ce__isword():
149 1.1 cgd * Return if p is part of a word according to emacs
150 1.1 cgd */
151 1.1 cgd protected int
152 1.9 lukem ce__isword(int p)
153 1.1 cgd {
154 1.9 lukem return (isalpha(p) || isdigit(p) || strchr("*?_-.[]~=", p) != NULL);
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd
158 1.1 cgd /* cv__isword():
159 1.1 cgd * Return if p is part of a word according to vi
160 1.1 cgd */
161 1.1 cgd protected int
162 1.9 lukem cv__isword(int p)
163 1.1 cgd {
164 1.9 lukem return (!isspace(p));
165 1.1 cgd }
166 1.1 cgd
167 1.1 cgd
168 1.1 cgd /* c__prev_word():
169 1.1 cgd * Find the previous word
170 1.1 cgd */
171 1.1 cgd protected char *
172 1.9 lukem c__prev_word(char *p, char *low, int n, int (*wtest)(int))
173 1.9 lukem {
174 1.9 lukem p--;
175 1.9 lukem
176 1.9 lukem while (n--) {
177 1.9 lukem while ((p >= low) && !(*wtest)((unsigned char) *p))
178 1.9 lukem p--;
179 1.9 lukem while ((p >= low) && (*wtest)((unsigned char) *p))
180 1.9 lukem p--;
181 1.9 lukem }
182 1.9 lukem
183 1.9 lukem /* cp now points to one character before the word */
184 1.9 lukem p++;
185 1.9 lukem if (p < low)
186 1.9 lukem p = low;
187 1.9 lukem /* cp now points where we want it */
188 1.9 lukem return (p);
189 1.1 cgd }
190 1.1 cgd
191 1.1 cgd
192 1.1 cgd /* c__next_word():
193 1.1 cgd * Find the next word
194 1.1 cgd */
195 1.1 cgd protected char *
196 1.9 lukem c__next_word(char *p, char *high, int n, int (*wtest)(int))
197 1.9 lukem {
198 1.9 lukem while (n--) {
199 1.9 lukem while ((p < high) && !(*wtest)((unsigned char) *p))
200 1.9 lukem p++;
201 1.9 lukem while ((p < high) && (*wtest)((unsigned char) *p))
202 1.9 lukem p++;
203 1.9 lukem }
204 1.9 lukem if (p > high)
205 1.9 lukem p = high;
206 1.9 lukem /* p now points where we want it */
207 1.9 lukem return (p);
208 1.1 cgd }
209 1.1 cgd
210 1.1 cgd /* cv_next_word():
211 1.1 cgd * Find the next word vi style
212 1.1 cgd */
213 1.1 cgd protected char *
214 1.9 lukem cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
215 1.9 lukem {
216 1.9 lukem int test;
217 1.9 lukem
218 1.9 lukem while (n--) {
219 1.9 lukem test = (*wtest)((unsigned char) *p);
220 1.9 lukem while ((p < high) && (*wtest)((unsigned char) *p) == test)
221 1.9 lukem p++;
222 1.9 lukem /*
223 1.9 lukem * vi historically deletes with cw only the word preserving the
224 1.9 lukem * trailing whitespace! This is not what 'w' does..
225 1.9 lukem */
226 1.9 lukem if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
227 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
228 1.9 lukem p++;
229 1.9 lukem }
230 1.1 cgd
231 1.9 lukem /* p now points where we want it */
232 1.9 lukem if (p > high)
233 1.9 lukem return (high);
234 1.9 lukem else
235 1.9 lukem return (p);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd
239 1.1 cgd /* cv_prev_word():
240 1.1 cgd * Find the previous word vi style
241 1.1 cgd */
242 1.1 cgd protected char *
243 1.9 lukem cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
244 1.1 cgd {
245 1.9 lukem int test;
246 1.1 cgd
247 1.9 lukem while (n--) {
248 1.1 cgd p--;
249 1.9 lukem /*
250 1.9 lukem * vi historically deletes with cb only the word preserving the
251 1.9 lukem * leading whitespace! This is not what 'b' does..
252 1.9 lukem */
253 1.9 lukem if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
254 1.9 lukem while ((p > low) && isspace((unsigned char) *p))
255 1.9 lukem p--;
256 1.9 lukem test = (*wtest)((unsigned char) *p);
257 1.9 lukem while ((p >= low) && (*wtest)((unsigned char) *p) == test)
258 1.9 lukem p--;
259 1.1 cgd p++;
260 1.9 lukem while (isspace((unsigned char) *p))
261 1.9 lukem p++;
262 1.9 lukem }
263 1.1 cgd
264 1.9 lukem /* p now points where we want it */
265 1.9 lukem if (p < low)
266 1.9 lukem return (low);
267 1.9 lukem else
268 1.9 lukem return (p);
269 1.1 cgd }
270 1.1 cgd
271 1.1 cgd
272 1.1 cgd #ifdef notdef
273 1.1 cgd /* c__number():
274 1.1 cgd * Ignore character p points to, return number appearing after that.
275 1.1 cgd * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
276 1.1 cgd * Return p pointing to last char used.
277 1.1 cgd */
278 1.1 cgd protected char *
279 1.9 lukem c__number(
280 1.9 lukem char *p, /* character position */
281 1.9 lukem int *num, /* Return value */
282 1.9 lukem int dval) /* dval is the number to subtract from like $-3 */
283 1.9 lukem {
284 1.9 lukem int i;
285 1.9 lukem int sign = 1;
286 1.9 lukem
287 1.9 lukem if (*++p == '^') {
288 1.9 lukem *num = 1;
289 1.9 lukem return (p);
290 1.9 lukem }
291 1.9 lukem if (*p == '$') {
292 1.9 lukem if (*++p != '-') {
293 1.9 lukem *num = 0x7fffffff; /* Handle $ */
294 1.9 lukem return (--p);
295 1.9 lukem }
296 1.9 lukem sign = -1; /* Handle $- */
297 1.9 lukem ++p;
298 1.9 lukem }
299 1.9 lukem for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
300 1.9 lukem continue;
301 1.9 lukem *num = (sign < 0 ? dval - i : i);
302 1.9 lukem return (--p);
303 1.1 cgd }
304 1.1 cgd #endif
305 1.1 cgd
306 1.1 cgd /* cv_delfini():
307 1.1 cgd * Finish vi delete action
308 1.1 cgd */
309 1.1 cgd protected void
310 1.9 lukem cv_delfini(EditLine *el)
311 1.1 cgd {
312 1.9 lukem int size;
313 1.9 lukem int oaction;
314 1.1 cgd
315 1.9 lukem if (el->el_chared.c_vcmd.action & INSERT)
316 1.9 lukem el->el_map.current = el->el_map.key;
317 1.1 cgd
318 1.9 lukem oaction = el->el_chared.c_vcmd.action;
319 1.9 lukem el->el_chared.c_vcmd.action = NOP;
320 1.9 lukem
321 1.9 lukem if (el->el_chared.c_vcmd.pos == 0)
322 1.9 lukem return;
323 1.9 lukem
324 1.9 lukem
325 1.9 lukem if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
326 1.9 lukem size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
327 1.9 lukem c_delbefore(el, size);
328 1.9 lukem el->el_line.cursor = el->el_chared.c_vcmd.pos;
329 1.9 lukem re_refresh_cursor(el);
330 1.9 lukem } else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
331 1.9 lukem size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
332 1.9 lukem c_delafter(el, size);
333 1.9 lukem } else {
334 1.9 lukem size = 1;
335 1.9 lukem c_delafter(el, size);
336 1.9 lukem }
337 1.9 lukem switch (oaction) {
338 1.9 lukem case DELETE|INSERT:
339 1.9 lukem el->el_chared.c_undo.action = DELETE|INSERT;
340 1.9 lukem break;
341 1.9 lukem case DELETE:
342 1.9 lukem el->el_chared.c_undo.action = INSERT;
343 1.9 lukem break;
344 1.9 lukem case NOP:
345 1.9 lukem case INSERT:
346 1.9 lukem default:
347 1.10 christos EL_ABORT((el->el_errfile, "Bad oaction %d\n", oaction));
348 1.9 lukem break;
349 1.9 lukem }
350 1.7 simonb
351 1.1 cgd
352 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.cursor;
353 1.9 lukem el->el_chared.c_undo.dsize = size;
354 1.1 cgd }
355 1.1 cgd
356 1.1 cgd
357 1.1 cgd #ifdef notdef
358 1.1 cgd /* ce__endword():
359 1.1 cgd * Go to the end of this word according to emacs
360 1.1 cgd */
361 1.1 cgd protected char *
362 1.9 lukem ce__endword(char *p, char *high, int n)
363 1.9 lukem {
364 1.9 lukem p++;
365 1.1 cgd
366 1.9 lukem while (n--) {
367 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
368 1.9 lukem p++;
369 1.9 lukem while ((p < high) && !isspace((unsigned char) *p))
370 1.9 lukem p++;
371 1.9 lukem }
372 1.9 lukem
373 1.9 lukem p--;
374 1.9 lukem return (p);
375 1.1 cgd }
376 1.1 cgd #endif
377 1.1 cgd
378 1.1 cgd
379 1.1 cgd /* cv__endword():
380 1.1 cgd * Go to the end of this word according to vi
381 1.1 cgd */
382 1.1 cgd protected char *
383 1.9 lukem cv__endword(char *p, char *high, int n)
384 1.9 lukem {
385 1.9 lukem p++;
386 1.1 cgd
387 1.9 lukem while (n--) {
388 1.9 lukem while ((p < high) && isspace((unsigned char) *p))
389 1.9 lukem p++;
390 1.9 lukem
391 1.9 lukem if (isalnum((unsigned char) *p))
392 1.9 lukem while ((p < high) && isalnum((unsigned char) *p))
393 1.9 lukem p++;
394 1.9 lukem else
395 1.9 lukem while ((p < high) && !(isspace((unsigned char) *p) ||
396 1.9 lukem isalnum((unsigned char) *p)))
397 1.9 lukem p++;
398 1.9 lukem }
399 1.9 lukem p--;
400 1.9 lukem return (p);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /* ch_init():
404 1.1 cgd * Initialize the character editor
405 1.1 cgd */
406 1.1 cgd protected int
407 1.9 lukem ch_init(EditLine *el)
408 1.1 cgd {
409 1.11 christos el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
410 1.11 christos if (el->el_line.buffer == NULL)
411 1.11 christos return (-1);
412 1.11 christos
413 1.9 lukem (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
414 1.9 lukem el->el_line.cursor = el->el_line.buffer;
415 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
416 1.9 lukem el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
417 1.9 lukem
418 1.11 christos el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
419 1.11 christos if (el->el_chared.c_undo.buf == NULL)
420 1.11 christos return (-1);
421 1.9 lukem (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
422 1.9 lukem el->el_chared.c_undo.action = NOP;
423 1.9 lukem el->el_chared.c_undo.isize = 0;
424 1.9 lukem el->el_chared.c_undo.dsize = 0;
425 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.buffer;
426 1.9 lukem
427 1.9 lukem el->el_chared.c_vcmd.action = NOP;
428 1.9 lukem el->el_chared.c_vcmd.pos = el->el_line.buffer;
429 1.9 lukem el->el_chared.c_vcmd.ins = el->el_line.buffer;
430 1.9 lukem
431 1.9 lukem el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
432 1.11 christos if (el->el_chared.c_kill.buf == NULL)
433 1.11 christos return (-1);
434 1.9 lukem (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
435 1.9 lukem el->el_chared.c_kill.mark = el->el_line.buffer;
436 1.9 lukem el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
437 1.9 lukem
438 1.9 lukem el->el_map.current = el->el_map.key;
439 1.9 lukem
440 1.9 lukem el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
441 1.9 lukem el->el_state.doingarg = 0;
442 1.9 lukem el->el_state.metanext = 0;
443 1.9 lukem el->el_state.argument = 1;
444 1.9 lukem el->el_state.lastcmd = ED_UNASSIGNED;
445 1.9 lukem
446 1.9 lukem el->el_chared.c_macro.nline = NULL;
447 1.9 lukem el->el_chared.c_macro.level = -1;
448 1.9 lukem el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
449 1.11 christos sizeof(char *));
450 1.11 christos if (el->el_chared.c_macro.macro == NULL)
451 1.11 christos return (-1);
452 1.9 lukem return (0);
453 1.1 cgd }
454 1.1 cgd
455 1.1 cgd /* ch_reset():
456 1.1 cgd * Reset the character editor
457 1.1 cgd */
458 1.1 cgd protected void
459 1.9 lukem ch_reset(EditLine *el)
460 1.1 cgd {
461 1.9 lukem el->el_line.cursor = el->el_line.buffer;
462 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
463 1.1 cgd
464 1.9 lukem el->el_chared.c_undo.action = NOP;
465 1.9 lukem el->el_chared.c_undo.isize = 0;
466 1.9 lukem el->el_chared.c_undo.dsize = 0;
467 1.9 lukem el->el_chared.c_undo.ptr = el->el_line.buffer;
468 1.1 cgd
469 1.9 lukem el->el_chared.c_vcmd.action = NOP;
470 1.9 lukem el->el_chared.c_vcmd.pos = el->el_line.buffer;
471 1.9 lukem el->el_chared.c_vcmd.ins = el->el_line.buffer;
472 1.1 cgd
473 1.9 lukem el->el_chared.c_kill.mark = el->el_line.buffer;
474 1.1 cgd
475 1.9 lukem el->el_map.current = el->el_map.key;
476 1.1 cgd
477 1.9 lukem el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
478 1.9 lukem el->el_state.doingarg = 0;
479 1.9 lukem el->el_state.metanext = 0;
480 1.9 lukem el->el_state.argument = 1;
481 1.9 lukem el->el_state.lastcmd = ED_UNASSIGNED;
482 1.1 cgd
483 1.9 lukem el->el_chared.c_macro.level = -1;
484 1.1 cgd
485 1.9 lukem el->el_history.eventno = 0;
486 1.1 cgd }
487 1.1 cgd
488 1.12 jdolecek /* ch_enlargebufs():
489 1.12 jdolecek * Enlarge line buffer to be able to hold twice as much characters.
490 1.12 jdolecek * Returns 1 if successful, 0 if not.
491 1.12 jdolecek */
492 1.12 jdolecek protected int
493 1.12 jdolecek ch_enlargebufs(el, addlen)
494 1.13 lukem EditLine *el;
495 1.13 lukem size_t addlen;
496 1.12 jdolecek {
497 1.13 lukem size_t sz, newsz;
498 1.13 lukem char *newbuffer, *oldbuf, *oldkbuf;
499 1.12 jdolecek
500 1.13 lukem sz = el->el_line.limit - el->el_line.buffer + EL_LEAVE;
501 1.13 lukem newsz = sz * 2;
502 1.13 lukem /*
503 1.13 lukem * If newly required length is longer than current buffer, we need
504 1.13 lukem * to make the buffer big enough to hold both old and new stuff.
505 1.13 lukem */
506 1.13 lukem if (addlen > sz) {
507 1.13 lukem while(newsz - sz < addlen)
508 1.13 lukem newsz *= 2;
509 1.13 lukem }
510 1.13 lukem
511 1.13 lukem /*
512 1.13 lukem * Reallocate line buffer.
513 1.13 lukem */
514 1.13 lukem newbuffer = el_realloc(el->el_line.buffer, newsz);
515 1.13 lukem if (!newbuffer)
516 1.13 lukem return 0;
517 1.13 lukem
518 1.13 lukem /* zero the newly added memory, leave old data in */
519 1.13 lukem (void) memset(&newbuffer[sz], 0, newsz - sz);
520 1.13 lukem
521 1.13 lukem oldbuf = el->el_line.buffer;
522 1.13 lukem
523 1.13 lukem el->el_line.buffer = newbuffer;
524 1.13 lukem el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
525 1.13 lukem el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
526 1.13 lukem el->el_line.limit = &newbuffer[newsz - EL_LEAVE];
527 1.13 lukem
528 1.13 lukem /*
529 1.13 lukem * Reallocate kill buffer.
530 1.13 lukem */
531 1.13 lukem newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz);
532 1.13 lukem if (!newbuffer)
533 1.13 lukem return 0;
534 1.12 jdolecek
535 1.13 lukem /* zero the newly added memory, leave old data in */
536 1.13 lukem (void) memset(&newbuffer[sz], 0, newsz - sz);
537 1.12 jdolecek
538 1.13 lukem oldkbuf = el->el_chared.c_kill.buf;
539 1.12 jdolecek
540 1.13 lukem el->el_chared.c_kill.buf = newbuffer;
541 1.13 lukem el->el_chared.c_kill.last = newbuffer +
542 1.12 jdolecek (el->el_chared.c_kill.last - oldkbuf);
543 1.13 lukem el->el_chared.c_kill.mark = el->el_line.buffer +
544 1.12 jdolecek (el->el_chared.c_kill.mark - oldbuf);
545 1.12 jdolecek
546 1.13 lukem /*
547 1.13 lukem * Reallocate undo buffer.
548 1.13 lukem */
549 1.13 lukem newbuffer = el_realloc(el->el_chared.c_undo.buf, newsz);
550 1.13 lukem if (!newbuffer)
551 1.13 lukem return 0;
552 1.13 lukem
553 1.13 lukem /* zero the newly added memory, leave old data in */
554 1.13 lukem (void) memset(&newbuffer[sz], 0, newsz - sz);
555 1.13 lukem
556 1.13 lukem el->el_chared.c_undo.ptr = el->el_line.buffer +
557 1.13 lukem (el->el_chared.c_undo.ptr - oldbuf);
558 1.13 lukem el->el_chared.c_undo.buf = newbuffer;
559 1.13 lukem
560 1.13 lukem if (!hist_enlargebuf(el, sz, newsz))
561 1.13 lukem return 0;
562 1.12 jdolecek
563 1.13 lukem return 1;
564 1.12 jdolecek }
565 1.1 cgd
566 1.1 cgd /* ch_end():
567 1.1 cgd * Free the data structures used by the editor
568 1.1 cgd */
569 1.1 cgd protected void
570 1.9 lukem ch_end(EditLine *el)
571 1.1 cgd {
572 1.9 lukem el_free((ptr_t) el->el_line.buffer);
573 1.9 lukem el->el_line.buffer = NULL;
574 1.9 lukem el->el_line.limit = NULL;
575 1.9 lukem el_free((ptr_t) el->el_chared.c_undo.buf);
576 1.9 lukem el->el_chared.c_undo.buf = NULL;
577 1.9 lukem el_free((ptr_t) el->el_chared.c_kill.buf);
578 1.9 lukem el->el_chared.c_kill.buf = NULL;
579 1.9 lukem el_free((ptr_t) el->el_chared.c_macro.macro);
580 1.9 lukem el->el_chared.c_macro.macro = NULL;
581 1.9 lukem ch_reset(el);
582 1.1 cgd }
583 1.1 cgd
584 1.1 cgd
585 1.1 cgd /* el_insertstr():
586 1.1 cgd * Insert string at cursorI
587 1.1 cgd */
588 1.1 cgd public int
589 1.9 lukem el_insertstr(EditLine *el, const char *s)
590 1.9 lukem {
591 1.14 christos size_t len;
592 1.9 lukem
593 1.9 lukem if ((len = strlen(s)) == 0)
594 1.9 lukem return (-1);
595 1.12 jdolecek if (el->el_line.lastchar + len >= el->el_line.limit) {
596 1.12 jdolecek if (!ch_enlargebufs(el, len))
597 1.12 jdolecek return (-1);
598 1.12 jdolecek }
599 1.9 lukem
600 1.14 christos c_insert(el, (int)len);
601 1.9 lukem while (*s)
602 1.9 lukem *el->el_line.cursor++ = *s++;
603 1.9 lukem return (0);
604 1.1 cgd }
605 1.1 cgd
606 1.1 cgd
607 1.1 cgd /* el_deletestr():
608 1.1 cgd * Delete num characters before the cursor
609 1.1 cgd */
610 1.1 cgd public void
611 1.9 lukem el_deletestr(EditLine *el, int n)
612 1.9 lukem {
613 1.9 lukem if (n <= 0)
614 1.9 lukem return;
615 1.9 lukem
616 1.9 lukem if (el->el_line.cursor < &el->el_line.buffer[n])
617 1.9 lukem return;
618 1.9 lukem
619 1.9 lukem c_delbefore(el, n); /* delete before dot */
620 1.9 lukem el->el_line.cursor -= n;
621 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
622 1.9 lukem el->el_line.cursor = el->el_line.buffer;
623 1.1 cgd }
624 1.1 cgd
625 1.1 cgd /* c_gets():
626 1.1 cgd * Get a string
627 1.1 cgd */
628 1.1 cgd protected int
629 1.9 lukem c_gets(EditLine *el, char *buf)
630 1.9 lukem {
631 1.9 lukem char ch;
632 1.9 lukem int len = 0;
633 1.1 cgd
634 1.9 lukem for (ch = 0; ch == 0;) {
635 1.9 lukem if (el_getc(el, &ch) != 1)
636 1.9 lukem return (ed_end_of_file(el, 0));
637 1.9 lukem switch (ch) {
638 1.9 lukem case 0010: /* Delete and backspace */
639 1.9 lukem case 0177:
640 1.9 lukem if (len > 1) {
641 1.9 lukem *el->el_line.cursor-- = '\0';
642 1.9 lukem el->el_line.lastchar = el->el_line.cursor;
643 1.9 lukem buf[len--] = '\0';
644 1.9 lukem } else {
645 1.9 lukem el->el_line.buffer[0] = '\0';
646 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
647 1.9 lukem el->el_line.cursor = el->el_line.buffer;
648 1.9 lukem return (CC_REFRESH);
649 1.9 lukem }
650 1.9 lukem re_refresh(el);
651 1.9 lukem ch = 0;
652 1.9 lukem break;
653 1.9 lukem
654 1.9 lukem case 0033: /* ESC */
655 1.9 lukem case '\r': /* Newline */
656 1.9 lukem case '\n':
657 1.9 lukem break;
658 1.9 lukem
659 1.9 lukem default:
660 1.9 lukem if (len >= EL_BUFSIZ)
661 1.9 lukem term_beep(el);
662 1.9 lukem else {
663 1.9 lukem buf[len++] = ch;
664 1.9 lukem *el->el_line.cursor++ = ch;
665 1.9 lukem el->el_line.lastchar = el->el_line.cursor;
666 1.9 lukem }
667 1.9 lukem re_refresh(el);
668 1.9 lukem ch = 0;
669 1.9 lukem break;
670 1.9 lukem }
671 1.9 lukem }
672 1.9 lukem buf[len] = ch;
673 1.9 lukem return (len);
674 1.1 cgd }
675 1.1 cgd
676 1.1 cgd
677 1.1 cgd /* c_hpos():
678 1.1 cgd * Return the current horizontal position of the cursor
679 1.1 cgd */
680 1.1 cgd protected int
681 1.9 lukem c_hpos(EditLine *el)
682 1.1 cgd {
683 1.9 lukem char *ptr;
684 1.1 cgd
685 1.9 lukem /*
686 1.9 lukem * Find how many characters till the beginning of this line.
687 1.9 lukem */
688 1.9 lukem if (el->el_line.cursor == el->el_line.buffer)
689 1.9 lukem return (0);
690 1.9 lukem else {
691 1.9 lukem for (ptr = el->el_line.cursor - 1;
692 1.9 lukem ptr >= el->el_line.buffer && *ptr != '\n';
693 1.9 lukem ptr--)
694 1.9 lukem continue;
695 1.9 lukem return (el->el_line.cursor - ptr - 1);
696 1.9 lukem }
697 1.1 cgd }
698