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