chared.c revision 1.45 1 1.45 christos /* $NetBSD: chared.c,v 1.45 2016/02/16 19:08:41 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.19 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.15 christos #include "config.h"
36 1.1 cgd #if !defined(lint) && !defined(SCCSID)
37 1.2 lukem #if 0
38 1.1 cgd static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
39 1.2 lukem #else
40 1.45 christos __RCSID("$NetBSD: chared.c,v 1.45 2016/02/16 19:08:41 christos Exp $");
41 1.2 lukem #endif
42 1.1 cgd #endif /* not lint && not SCCSID */
43 1.1 cgd
44 1.7 simonb /*
45 1.1 cgd * chared.c: Character editor utilities
46 1.1 cgd */
47 1.1 cgd #include <stdlib.h>
48 1.45 christos
49 1.1 cgd #include "el.h"
50 1.45 christos #include "common.h"
51 1.1 cgd
52 1.26 sketch private void ch__clearmacro (EditLine *);
53 1.24 christos
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.17 christos cv_undo(EditLine *el)
62 1.9 lukem {
63 1.9 lukem c_undo_t *vu = &el->el_chared.c_undo;
64 1.17 christos c_redo_t *r = &el->el_chared.c_redo;
65 1.27 christos size_t size;
66 1.16 christos
67 1.17 christos /* Save entire line for undo */
68 1.35 christos size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
69 1.35 christos vu->len = (ssize_t)size;
70 1.27 christos vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
71 1.28 christos (void)memcpy(vu->buf, el->el_line.buffer, size * sizeof(*vu->buf));
72 1.16 christos
73 1.17 christos /* save command info for redo */
74 1.17 christos r->count = el->el_state.doingarg ? el->el_state.argument : 0;
75 1.17 christos r->action = el->el_chared.c_vcmd.action;
76 1.17 christos r->pos = r->buf;
77 1.17 christos r->cmd = el->el_state.thiscmd;
78 1.17 christos r->ch = el->el_state.thisch;
79 1.17 christos }
80 1.17 christos
81 1.17 christos /* cv_yank():
82 1.17 christos * Save yank/delete data for paste
83 1.17 christos */
84 1.17 christos protected void
85 1.28 christos cv_yank(EditLine *el, const Char *ptr, int size)
86 1.17 christos {
87 1.17 christos c_kill_t *k = &el->el_chared.c_kill;
88 1.17 christos
89 1.35 christos (void)memcpy(k->buf, ptr, (size_t)size * sizeof(*k->buf));
90 1.17 christos k->last = k->buf + size;
91 1.1 cgd }
92 1.1 cgd
93 1.1 cgd
94 1.7 simonb /* c_insert():
95 1.1 cgd * Insert num characters
96 1.1 cgd */
97 1.1 cgd protected void
98 1.9 lukem c_insert(EditLine *el, int num)
99 1.9 lukem {
100 1.28 christos Char *cp;
101 1.9 lukem
102 1.17 christos if (el->el_line.lastchar + num >= el->el_line.limit) {
103 1.27 christos if (!ch_enlargebufs(el, (size_t)num))
104 1.17 christos return; /* can't go past end of buffer */
105 1.17 christos }
106 1.9 lukem
107 1.9 lukem if (el->el_line.cursor < el->el_line.lastchar) {
108 1.9 lukem /* if I must move chars */
109 1.9 lukem for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
110 1.9 lukem cp[num] = *cp;
111 1.9 lukem }
112 1.9 lukem el->el_line.lastchar += num;
113 1.9 lukem }
114 1.1 cgd
115 1.1 cgd
116 1.1 cgd /* c_delafter():
117 1.1 cgd * Delete num characters after the cursor
118 1.1 cgd */
119 1.1 cgd protected void
120 1.9 lukem c_delafter(EditLine *el, int num)
121 1.1 cgd {
122 1.1 cgd
123 1.9 lukem if (el->el_line.cursor + num > el->el_line.lastchar)
124 1.27 christos num = (int)(el->el_line.lastchar - el->el_line.cursor);
125 1.1 cgd
126 1.17 christos if (el->el_map.current != el->el_map.emacs) {
127 1.17 christos cv_undo(el);
128 1.17 christos cv_yank(el, el->el_line.cursor, num);
129 1.17 christos }
130 1.16 christos
131 1.9 lukem if (num > 0) {
132 1.28 christos Char *cp;
133 1.1 cgd
134 1.9 lukem for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
135 1.9 lukem *cp = cp[num];
136 1.1 cgd
137 1.9 lukem el->el_line.lastchar -= num;
138 1.9 lukem }
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd
142 1.22 mycroft /* c_delafter1():
143 1.22 mycroft * Delete the character after the cursor, do not yank
144 1.22 mycroft */
145 1.22 mycroft protected void
146 1.22 mycroft c_delafter1(EditLine *el)
147 1.22 mycroft {
148 1.28 christos Char *cp;
149 1.22 mycroft
150 1.22 mycroft for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
151 1.22 mycroft *cp = cp[1];
152 1.22 mycroft
153 1.22 mycroft el->el_line.lastchar--;
154 1.22 mycroft }
155 1.22 mycroft
156 1.22 mycroft
157 1.1 cgd /* c_delbefore():
158 1.1 cgd * Delete num characters before the cursor
159 1.1 cgd */
160 1.1 cgd protected void
161 1.9 lukem c_delbefore(EditLine *el, int num)
162 1.1 cgd {
163 1.1 cgd
164 1.9 lukem if (el->el_line.cursor - num < el->el_line.buffer)
165 1.27 christos num = (int)(el->el_line.cursor - el->el_line.buffer);
166 1.1 cgd
167 1.17 christos if (el->el_map.current != el->el_map.emacs) {
168 1.17 christos cv_undo(el);
169 1.17 christos cv_yank(el, el->el_line.cursor - num, num);
170 1.17 christos }
171 1.16 christos
172 1.9 lukem if (num > 0) {
173 1.28 christos Char *cp;
174 1.1 cgd
175 1.9 lukem for (cp = el->el_line.cursor - num;
176 1.9 lukem cp <= el->el_line.lastchar;
177 1.9 lukem cp++)
178 1.9 lukem *cp = cp[num];
179 1.1 cgd
180 1.9 lukem el->el_line.lastchar -= num;
181 1.9 lukem }
182 1.1 cgd }
183 1.1 cgd
184 1.1 cgd
185 1.22 mycroft /* c_delbefore1():
186 1.22 mycroft * Delete the character before the cursor, do not yank
187 1.22 mycroft */
188 1.22 mycroft protected void
189 1.22 mycroft c_delbefore1(EditLine *el)
190 1.22 mycroft {
191 1.28 christos Char *cp;
192 1.22 mycroft
193 1.22 mycroft for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
194 1.22 mycroft *cp = cp[1];
195 1.22 mycroft
196 1.22 mycroft el->el_line.lastchar--;
197 1.22 mycroft }
198 1.22 mycroft
199 1.22 mycroft
200 1.1 cgd /* ce__isword():
201 1.1 cgd * Return if p is part of a word according to emacs
202 1.1 cgd */
203 1.1 cgd protected int
204 1.41 christos ce__isword(wint_t p)
205 1.1 cgd {
206 1.36 christos return Isalnum(p) || Strchr(STR("*?_-.[]~="), p) != NULL;
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd
210 1.1 cgd /* cv__isword():
211 1.1 cgd * Return if p is part of a word according to vi
212 1.1 cgd */
213 1.1 cgd protected int
214 1.41 christos cv__isword(wint_t p)
215 1.1 cgd {
216 1.28 christos if (Isalnum(p) || p == '_')
217 1.17 christos return 1;
218 1.28 christos if (Isgraph(p))
219 1.17 christos return 2;
220 1.17 christos return 0;
221 1.16 christos }
222 1.16 christos
223 1.16 christos
224 1.16 christos /* cv__isWord():
225 1.16 christos * Return if p is part of a big word according to vi
226 1.16 christos */
227 1.16 christos protected int
228 1.41 christos cv__isWord(wint_t p)
229 1.16 christos {
230 1.32 christos return !Isspace(p);
231 1.1 cgd }
232 1.1 cgd
233 1.1 cgd
234 1.1 cgd /* c__prev_word():
235 1.1 cgd * Find the previous word
236 1.1 cgd */
237 1.28 christos protected Char *
238 1.41 christos c__prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
239 1.9 lukem {
240 1.9 lukem p--;
241 1.9 lukem
242 1.9 lukem while (n--) {
243 1.28 christos while ((p >= low) && !(*wtest)(*p))
244 1.9 lukem p--;
245 1.28 christos while ((p >= low) && (*wtest)(*p))
246 1.9 lukem p--;
247 1.9 lukem }
248 1.9 lukem
249 1.9 lukem /* cp now points to one character before the word */
250 1.9 lukem p++;
251 1.9 lukem if (p < low)
252 1.9 lukem p = low;
253 1.9 lukem /* cp now points where we want it */
254 1.32 christos return p;
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd
258 1.1 cgd /* c__next_word():
259 1.1 cgd * Find the next word
260 1.1 cgd */
261 1.28 christos protected Char *
262 1.41 christos c__next_word(Char *p, Char *high, int n, int (*wtest)(wint_t))
263 1.9 lukem {
264 1.9 lukem while (n--) {
265 1.28 christos while ((p < high) && !(*wtest)(*p))
266 1.9 lukem p++;
267 1.28 christos while ((p < high) && (*wtest)(*p))
268 1.9 lukem p++;
269 1.9 lukem }
270 1.9 lukem if (p > high)
271 1.9 lukem p = high;
272 1.9 lukem /* p now points where we want it */
273 1.32 christos return p;
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd /* cv_next_word():
277 1.1 cgd * Find the next word vi style
278 1.1 cgd */
279 1.28 christos protected Char *
280 1.41 christos cv_next_word(EditLine *el, Char *p, Char *high, int n, int (*wtest)(wint_t))
281 1.9 lukem {
282 1.9 lukem int test;
283 1.9 lukem
284 1.9 lukem while (n--) {
285 1.28 christos test = (*wtest)(*p);
286 1.28 christos while ((p < high) && (*wtest)(*p) == test)
287 1.9 lukem p++;
288 1.9 lukem /*
289 1.9 lukem * vi historically deletes with cw only the word preserving the
290 1.9 lukem * trailing whitespace! This is not what 'w' does..
291 1.9 lukem */
292 1.16 christos if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
293 1.28 christos while ((p < high) && Isspace(*p))
294 1.9 lukem p++;
295 1.9 lukem }
296 1.1 cgd
297 1.9 lukem /* p now points where we want it */
298 1.9 lukem if (p > high)
299 1.32 christos return high;
300 1.9 lukem else
301 1.32 christos return p;
302 1.1 cgd }
303 1.1 cgd
304 1.1 cgd
305 1.1 cgd /* cv_prev_word():
306 1.1 cgd * Find the previous word vi style
307 1.1 cgd */
308 1.28 christos protected Char *
309 1.41 christos cv_prev_word(Char *p, Char *low, int n, int (*wtest)(wint_t))
310 1.1 cgd {
311 1.9 lukem int test;
312 1.1 cgd
313 1.16 christos p--;
314 1.9 lukem while (n--) {
315 1.28 christos while ((p > low) && Isspace(*p))
316 1.16 christos p--;
317 1.28 christos test = (*wtest)(*p);
318 1.28 christos while ((p >= low) && (*wtest)(*p) == test)
319 1.9 lukem p--;
320 1.9 lukem }
321 1.16 christos p++;
322 1.1 cgd
323 1.9 lukem /* p now points where we want it */
324 1.9 lukem if (p < low)
325 1.32 christos return low;
326 1.9 lukem else
327 1.32 christos return p;
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd
331 1.1 cgd /* cv_delfini():
332 1.1 cgd * Finish vi delete action
333 1.1 cgd */
334 1.1 cgd protected void
335 1.9 lukem cv_delfini(EditLine *el)
336 1.1 cgd {
337 1.9 lukem int size;
338 1.17 christos int action = el->el_chared.c_vcmd.action;
339 1.1 cgd
340 1.17 christos if (action & INSERT)
341 1.9 lukem el->el_map.current = el->el_map.key;
342 1.1 cgd
343 1.9 lukem if (el->el_chared.c_vcmd.pos == 0)
344 1.17 christos /* sanity */
345 1.9 lukem return;
346 1.9 lukem
347 1.27 christos size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
348 1.17 christos if (size == 0)
349 1.17 christos size = 1;
350 1.16 christos el->el_line.cursor = el->el_chared.c_vcmd.pos;
351 1.17 christos if (action & YANK) {
352 1.17 christos if (size > 0)
353 1.17 christos cv_yank(el, el->el_line.cursor, size);
354 1.17 christos else
355 1.17 christos cv_yank(el, el->el_line.cursor + size, -size);
356 1.9 lukem } else {
357 1.17 christos if (size > 0) {
358 1.17 christos c_delafter(el, size);
359 1.17 christos re_refresh_cursor(el);
360 1.17 christos } else {
361 1.17 christos c_delbefore(el, -size);
362 1.17 christos el->el_line.cursor += size;
363 1.17 christos }
364 1.9 lukem }
365 1.17 christos el->el_chared.c_vcmd.action = NOP;
366 1.1 cgd }
367 1.1 cgd
368 1.1 cgd
369 1.1 cgd /* cv__endword():
370 1.1 cgd * Go to the end of this word according to vi
371 1.1 cgd */
372 1.28 christos protected Char *
373 1.41 christos cv__endword(Char *p, Char *high, int n, int (*wtest)(wint_t))
374 1.9 lukem {
375 1.16 christos int test;
376 1.16 christos
377 1.9 lukem p++;
378 1.1 cgd
379 1.9 lukem while (n--) {
380 1.28 christos while ((p < high) && Isspace(*p))
381 1.9 lukem p++;
382 1.9 lukem
383 1.28 christos test = (*wtest)(*p);
384 1.28 christos while ((p < high) && (*wtest)(*p) == test)
385 1.16 christos p++;
386 1.9 lukem }
387 1.9 lukem p--;
388 1.32 christos return p;
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd /* ch_init():
392 1.1 cgd * Initialize the character editor
393 1.1 cgd */
394 1.1 cgd protected int
395 1.9 lukem ch_init(EditLine *el)
396 1.1 cgd {
397 1.24 christos c_macro_t *ma = &el->el_chared.c_macro;
398 1.24 christos
399 1.28 christos el->el_line.buffer = el_malloc(EL_BUFSIZ *
400 1.28 christos sizeof(*el->el_line.buffer));
401 1.11 christos if (el->el_line.buffer == NULL)
402 1.32 christos return -1;
403 1.11 christos
404 1.28 christos (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
405 1.28 christos sizeof(*el->el_line.buffer));
406 1.9 lukem el->el_line.cursor = el->el_line.buffer;
407 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
408 1.16 christos el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
409 1.9 lukem
410 1.28 christos el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
411 1.28 christos sizeof(*el->el_chared.c_undo.buf));
412 1.11 christos if (el->el_chared.c_undo.buf == NULL)
413 1.32 christos return -1;
414 1.28 christos (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
415 1.28 christos sizeof(*el->el_chared.c_undo.buf));
416 1.16 christos el->el_chared.c_undo.len = -1;
417 1.16 christos el->el_chared.c_undo.cursor = 0;
418 1.28 christos el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
419 1.28 christos sizeof(*el->el_chared.c_redo.buf));
420 1.17 christos if (el->el_chared.c_redo.buf == NULL)
421 1.32 christos return -1;
422 1.17 christos el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
423 1.17 christos el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
424 1.17 christos el->el_chared.c_redo.cmd = ED_UNASSIGNED;
425 1.9 lukem
426 1.9 lukem el->el_chared.c_vcmd.action = NOP;
427 1.9 lukem el->el_chared.c_vcmd.pos = el->el_line.buffer;
428 1.9 lukem
429 1.28 christos el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
430 1.28 christos sizeof(*el->el_chared.c_kill.buf));
431 1.11 christos if (el->el_chared.c_kill.buf == NULL)
432 1.32 christos return -1;
433 1.28 christos (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
434 1.28 christos sizeof(*el->el_chared.c_kill.buf));
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.29 christos el->el_chared.c_resizefun = NULL;
438 1.29 christos el->el_chared.c_resizearg = NULL;
439 1.40 christos el->el_chared.c_aliasfun = NULL;
440 1.40 christos el->el_chared.c_aliasarg = NULL;
441 1.9 lukem
442 1.9 lukem el->el_map.current = el->el_map.key;
443 1.9 lukem
444 1.9 lukem el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
445 1.9 lukem el->el_state.doingarg = 0;
446 1.9 lukem el->el_state.metanext = 0;
447 1.9 lukem el->el_state.argument = 1;
448 1.9 lukem el->el_state.lastcmd = ED_UNASSIGNED;
449 1.9 lukem
450 1.24 christos ma->level = -1;
451 1.24 christos ma->offset = 0;
452 1.28 christos ma->macro = el_malloc(EL_MAXMACRO * sizeof(*ma->macro));
453 1.24 christos if (ma->macro == NULL)
454 1.32 christos return -1;
455 1.32 christos return 0;
456 1.1 cgd }
457 1.1 cgd
458 1.1 cgd /* ch_reset():
459 1.1 cgd * Reset the character editor
460 1.1 cgd */
461 1.1 cgd protected void
462 1.24 christos ch_reset(EditLine *el, int mclear)
463 1.1 cgd {
464 1.9 lukem el->el_line.cursor = el->el_line.buffer;
465 1.9 lukem el->el_line.lastchar = el->el_line.buffer;
466 1.1 cgd
467 1.16 christos el->el_chared.c_undo.len = -1;
468 1.16 christos el->el_chared.c_undo.cursor = 0;
469 1.1 cgd
470 1.9 lukem el->el_chared.c_vcmd.action = NOP;
471 1.9 lukem el->el_chared.c_vcmd.pos = 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.25 christos el->el_history.eventno = 0;
484 1.25 christos
485 1.24 christos if (mclear)
486 1.24 christos ch__clearmacro(el);
487 1.24 christos }
488 1.1 cgd
489 1.24 christos private void
490 1.27 christos ch__clearmacro(EditLine *el)
491 1.24 christos {
492 1.24 christos c_macro_t *ma = &el->el_chared.c_macro;
493 1.24 christos while (ma->level >= 0)
494 1.31 christos el_free(ma->macro[ma->level--]);
495 1.1 cgd }
496 1.1 cgd
497 1.12 jdolecek /* ch_enlargebufs():
498 1.12 jdolecek * Enlarge line buffer to be able to hold twice as much characters.
499 1.12 jdolecek * Returns 1 if successful, 0 if not.
500 1.12 jdolecek */
501 1.12 jdolecek protected int
502 1.27 christos ch_enlargebufs(EditLine *el, size_t addlen)
503 1.12 jdolecek {
504 1.13 lukem size_t sz, newsz;
505 1.28 christos Char *newbuffer, *oldbuf, *oldkbuf;
506 1.12 jdolecek
507 1.35 christos sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
508 1.13 lukem newsz = sz * 2;
509 1.13 lukem /*
510 1.13 lukem * If newly required length is longer than current buffer, we need
511 1.13 lukem * to make the buffer big enough to hold both old and new stuff.
512 1.13 lukem */
513 1.13 lukem if (addlen > sz) {
514 1.13 lukem while(newsz - sz < addlen)
515 1.13 lukem newsz *= 2;
516 1.13 lukem }
517 1.13 lukem
518 1.13 lukem /*
519 1.13 lukem * Reallocate line buffer.
520 1.13 lukem */
521 1.28 christos newbuffer = el_realloc(el->el_line.buffer, newsz * sizeof(*newbuffer));
522 1.13 lukem if (!newbuffer)
523 1.13 lukem return 0;
524 1.13 lukem
525 1.13 lukem /* zero the newly added memory, leave old data in */
526 1.28 christos (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
527 1.13 lukem
528 1.13 lukem oldbuf = el->el_line.buffer;
529 1.13 lukem
530 1.13 lukem el->el_line.buffer = newbuffer;
531 1.13 lukem el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
532 1.13 lukem el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
533 1.16 christos /* don't set new size until all buffers are enlarged */
534 1.16 christos el->el_line.limit = &newbuffer[sz - EL_LEAVE];
535 1.13 lukem
536 1.13 lukem /*
537 1.13 lukem * Reallocate kill buffer.
538 1.13 lukem */
539 1.31 christos newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
540 1.31 christos sizeof(*newbuffer));
541 1.13 lukem if (!newbuffer)
542 1.13 lukem return 0;
543 1.12 jdolecek
544 1.13 lukem /* zero the newly added memory, leave old data in */
545 1.28 christos (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
546 1.12 jdolecek
547 1.13 lukem oldkbuf = el->el_chared.c_kill.buf;
548 1.12 jdolecek
549 1.13 lukem el->el_chared.c_kill.buf = newbuffer;
550 1.13 lukem el->el_chared.c_kill.last = newbuffer +
551 1.12 jdolecek (el->el_chared.c_kill.last - oldkbuf);
552 1.13 lukem el->el_chared.c_kill.mark = el->el_line.buffer +
553 1.12 jdolecek (el->el_chared.c_kill.mark - oldbuf);
554 1.12 jdolecek
555 1.13 lukem /*
556 1.13 lukem * Reallocate undo buffer.
557 1.13 lukem */
558 1.28 christos newbuffer = el_realloc(el->el_chared.c_undo.buf,
559 1.28 christos newsz * sizeof(*newbuffer));
560 1.13 lukem if (!newbuffer)
561 1.13 lukem return 0;
562 1.13 lukem
563 1.13 lukem /* zero the newly added memory, leave old data in */
564 1.28 christos (void) memset(&newbuffer[sz], 0, (newsz - sz) * sizeof(*newbuffer));
565 1.16 christos el->el_chared.c_undo.buf = newbuffer;
566 1.13 lukem
567 1.28 christos newbuffer = el_realloc(el->el_chared.c_redo.buf,
568 1.28 christos newsz * sizeof(*newbuffer));
569 1.16 christos if (!newbuffer)
570 1.16 christos return 0;
571 1.17 christos el->el_chared.c_redo.pos = newbuffer +
572 1.17 christos (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
573 1.17 christos el->el_chared.c_redo.lim = newbuffer +
574 1.17 christos (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
575 1.17 christos el->el_chared.c_redo.buf = newbuffer;
576 1.13 lukem
577 1.13 lukem if (!hist_enlargebuf(el, sz, newsz))
578 1.13 lukem return 0;
579 1.12 jdolecek
580 1.16 christos /* Safe to set enlarged buffer size */
581 1.21 christos el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
582 1.29 christos if (el->el_chared.c_resizefun)
583 1.29 christos (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
584 1.13 lukem return 1;
585 1.12 jdolecek }
586 1.1 cgd
587 1.1 cgd /* ch_end():
588 1.1 cgd * Free the data structures used by the editor
589 1.1 cgd */
590 1.1 cgd protected void
591 1.9 lukem ch_end(EditLine *el)
592 1.1 cgd {
593 1.31 christos el_free(el->el_line.buffer);
594 1.9 lukem el->el_line.buffer = NULL;
595 1.9 lukem el->el_line.limit = NULL;
596 1.31 christos el_free(el->el_chared.c_undo.buf);
597 1.9 lukem el->el_chared.c_undo.buf = NULL;
598 1.31 christos el_free(el->el_chared.c_redo.buf);
599 1.17 christos el->el_chared.c_redo.buf = NULL;
600 1.17 christos el->el_chared.c_redo.pos = NULL;
601 1.17 christos el->el_chared.c_redo.lim = NULL;
602 1.17 christos el->el_chared.c_redo.cmd = ED_UNASSIGNED;
603 1.31 christos el_free(el->el_chared.c_kill.buf);
604 1.9 lukem el->el_chared.c_kill.buf = NULL;
605 1.24 christos ch_reset(el, 1);
606 1.31 christos el_free(el->el_chared.c_macro.macro);
607 1.9 lukem el->el_chared.c_macro.macro = NULL;
608 1.1 cgd }
609 1.1 cgd
610 1.1 cgd
611 1.1 cgd /* el_insertstr():
612 1.1 cgd * Insert string at cursorI
613 1.1 cgd */
614 1.1 cgd public int
615 1.28 christos FUN(el,insertstr)(EditLine *el, const Char *s)
616 1.9 lukem {
617 1.14 christos size_t len;
618 1.9 lukem
619 1.37 christos if (s == NULL || (len = Strlen(s)) == 0)
620 1.32 christos return -1;
621 1.12 jdolecek if (el->el_line.lastchar + len >= el->el_line.limit) {
622 1.12 jdolecek if (!ch_enlargebufs(el, len))
623 1.32 christos return -1;
624 1.12 jdolecek }
625 1.9 lukem
626 1.14 christos c_insert(el, (int)len);
627 1.9 lukem while (*s)
628 1.9 lukem *el->el_line.cursor++ = *s++;
629 1.32 christos return 0;
630 1.1 cgd }
631 1.1 cgd
632 1.1 cgd
633 1.1 cgd /* el_deletestr():
634 1.1 cgd * Delete num characters before the cursor
635 1.1 cgd */
636 1.1 cgd public void
637 1.9 lukem el_deletestr(EditLine *el, int n)
638 1.9 lukem {
639 1.9 lukem if (n <= 0)
640 1.9 lukem return;
641 1.9 lukem
642 1.9 lukem if (el->el_line.cursor < &el->el_line.buffer[n])
643 1.9 lukem return;
644 1.9 lukem
645 1.9 lukem c_delbefore(el, n); /* delete before dot */
646 1.9 lukem el->el_line.cursor -= n;
647 1.9 lukem if (el->el_line.cursor < el->el_line.buffer)
648 1.9 lukem el->el_line.cursor = el->el_line.buffer;
649 1.1 cgd }
650 1.1 cgd
651 1.38 christos /* el_cursor():
652 1.38 christos * Move the cursor to the left or the right of the current position
653 1.38 christos */
654 1.38 christos public int
655 1.38 christos el_cursor(EditLine *el, int n)
656 1.38 christos {
657 1.38 christos if (n == 0)
658 1.38 christos goto out;
659 1.38 christos
660 1.38 christos el->el_line.cursor += n;
661 1.38 christos
662 1.38 christos if (el->el_line.cursor < el->el_line.buffer)
663 1.38 christos el->el_line.cursor = el->el_line.buffer;
664 1.38 christos if (el->el_line.cursor > el->el_line.lastchar)
665 1.38 christos el->el_line.cursor = el->el_line.lastchar;
666 1.38 christos out:
667 1.39 christos return (int)(el->el_line.cursor - el->el_line.buffer);
668 1.38 christos }
669 1.38 christos
670 1.1 cgd /* c_gets():
671 1.1 cgd * Get a string
672 1.1 cgd */
673 1.1 cgd protected int
674 1.28 christos c_gets(EditLine *el, Char *buf, const Char *prompt)
675 1.9 lukem {
676 1.28 christos Char ch;
677 1.27 christos ssize_t len;
678 1.28 christos Char *cp = el->el_line.buffer;
679 1.18 christos
680 1.18 christos if (prompt) {
681 1.35 christos len = (ssize_t)Strlen(prompt);
682 1.35 christos (void)memcpy(cp, prompt, (size_t)len * sizeof(*cp));
683 1.18 christos cp += len;
684 1.18 christos }
685 1.18 christos len = 0;
686 1.18 christos
687 1.18 christos for (;;) {
688 1.18 christos el->el_line.cursor = cp;
689 1.18 christos *cp = ' ';
690 1.18 christos el->el_line.lastchar = cp + 1;
691 1.18 christos re_refresh(el);
692 1.18 christos
693 1.28 christos if (FUN(el,getc)(el, &ch) != 1) {
694 1.18 christos ed_end_of_file(el, 0);
695 1.18 christos len = -1;
696 1.18 christos break;
697 1.18 christos }
698 1.1 cgd
699 1.9 lukem switch (ch) {
700 1.18 christos
701 1.9 lukem case 0010: /* Delete and backspace */
702 1.9 lukem case 0177:
703 1.27 christos if (len == 0) {
704 1.18 christos len = -1;
705 1.18 christos break;
706 1.9 lukem }
707 1.18 christos cp--;
708 1.18 christos continue;
709 1.9 lukem
710 1.9 lukem case 0033: /* ESC */
711 1.9 lukem case '\r': /* Newline */
712 1.9 lukem case '\n':
713 1.18 christos buf[len] = ch;
714 1.9 lukem break;
715 1.9 lukem
716 1.9 lukem default:
717 1.34 christos if (len >= (ssize_t)(EL_BUFSIZ - 16))
718 1.30 christos terminal_beep(el);
719 1.9 lukem else {
720 1.9 lukem buf[len++] = ch;
721 1.18 christos *cp++ = ch;
722 1.9 lukem }
723 1.18 christos continue;
724 1.9 lukem }
725 1.18 christos break;
726 1.9 lukem }
727 1.18 christos
728 1.18 christos el->el_line.buffer[0] = '\0';
729 1.18 christos el->el_line.lastchar = el->el_line.buffer;
730 1.18 christos el->el_line.cursor = el->el_line.buffer;
731 1.27 christos return (int)len;
732 1.1 cgd }
733 1.1 cgd
734 1.1 cgd
735 1.1 cgd /* c_hpos():
736 1.1 cgd * Return the current horizontal position of the cursor
737 1.1 cgd */
738 1.1 cgd protected int
739 1.9 lukem c_hpos(EditLine *el)
740 1.1 cgd {
741 1.28 christos Char *ptr;
742 1.1 cgd
743 1.9 lukem /*
744 1.9 lukem * Find how many characters till the beginning of this line.
745 1.9 lukem */
746 1.9 lukem if (el->el_line.cursor == el->el_line.buffer)
747 1.32 christos return 0;
748 1.9 lukem else {
749 1.9 lukem for (ptr = el->el_line.cursor - 1;
750 1.9 lukem ptr >= el->el_line.buffer && *ptr != '\n';
751 1.9 lukem ptr--)
752 1.9 lukem continue;
753 1.27 christos return (int)(el->el_line.cursor - ptr - 1);
754 1.9 lukem }
755 1.1 cgd }
756 1.29 christos
757 1.29 christos protected int
758 1.29 christos ch_resizefun(EditLine *el, el_zfunc_t f, void *a)
759 1.29 christos {
760 1.29 christos el->el_chared.c_resizefun = f;
761 1.29 christos el->el_chared.c_resizearg = a;
762 1.29 christos return 0;
763 1.29 christos }
764 1.40 christos
765 1.40 christos protected int
766 1.40 christos ch_aliasfun(EditLine *el, el_afunc_t f, void *a)
767 1.40 christos {
768 1.40 christos el->el_chared.c_aliasfun = f;
769 1.40 christos el->el_chared.c_aliasarg = a;
770 1.40 christos return 0;
771 1.40 christos }
772