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