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