Deleted Added
full compact
chared.c (84201) chared.c (84260)
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 18 unchanged lines hidden (view full) ---

27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
1/*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Christos Zoulas of Cornell University.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 18 unchanged lines hidden (view full) ---

27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $NetBSD: chared.c,v 1.13 2001/04/13 01:04:19 lukem Exp $
35 */
36
37#include <sys/cdefs.h>
37 */
38
39#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libedit/chared.c 84201 2001-09-30 21:21:36Z dillon $");
40__FBSDID("$FreeBSD: head/lib/libedit/chared.c 84260 2001-10-01 08:41:27Z obrien $");
39#if !defined(lint) && !defined(SCCSID)
40static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
41#endif /* not lint && not SCCSID */
41#if !defined(lint) && !defined(SCCSID)
42static char sccsid[] = "@(#)chared.c 8.1 (Berkeley) 6/4/93";
43#endif /* not lint && not SCCSID */
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/lib/libedit/chared.c 84260 2001-10-01 08:41:27Z obrien $");
42
43/*
44 * chared.c: Character editor utilities
45 */
46#include "sys.h"
47
48#include <stdlib.h>
49#include "el.h"
50
46
47/*
48 * chared.c: Character editor utilities
49 */
50#include "sys.h"
51
52#include <stdlib.h>
53#include "el.h"
54
55/* value to leave unused in line buffer */
56#define EL_LEAVE 2
57
51/* cv_undo():
52 * Handle state for the vi undo command
53 */
54protected void
58/* cv_undo():
59 * Handle state for the vi undo command
60 */
61protected void
55cv_undo(el, action, size, ptr)
56 EditLine *el;
57 int action, size;
58 char *ptr;
62cv_undo(EditLine *el,int action, size_t size, char *ptr)
59{
63{
60 c_undo_t *vu = &el->el_chared.c_undo;
61 vu->action = action;
62 vu->ptr = ptr;
63 vu->isize = size;
64 (void) memcpy(vu->buf, vu->ptr, size);
64 c_undo_t *vu = &el->el_chared.c_undo;
65 vu->action = action;
66 vu->ptr = ptr;
67 vu->isize = size;
68 (void) memcpy(vu->buf, vu->ptr, size);
65#ifdef DEBUG_UNDO
69#ifdef DEBUG_UNDO
66 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
67 vu->ptr, vu->isize, vu->dsize);
70 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
71 vu->ptr, vu->isize, vu->dsize);
68#endif
69}
70
71
72/* c_insert():
73 * Insert num characters
74 */
75protected void
72#endif
73}
74
75
76/* c_insert():
77 * Insert num characters
78 */
79protected void
76c_insert(el, num)
77 EditLine *el;
78 int num;
80c_insert(EditLine *el, int num)
79{
81{
80 char *cp;
82 char *cp;
81
83
82 if (el->el_line.lastchar + num >= el->el_line.limit)
83 return; /* can't go past end of buffer */
84 if (el->el_line.lastchar + num >= el->el_line.limit)
85 return; /* can't go past end of buffer */
84
86
85 if (el->el_line.cursor < el->el_line.lastchar) {
86 /* if I must move chars */
87 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
88 cp[num] = *cp;
89 }
90 el->el_line.lastchar += num;
91} /* end c_insert */
87 if (el->el_line.cursor < el->el_line.lastchar) {
88 /* if I must move chars */
89 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
90 cp[num] = *cp;
91 }
92 el->el_line.lastchar += num;
93}
92
93
94/* c_delafter():
95 * Delete num characters after the cursor
96 */
97protected void
94
95
96/* c_delafter():
97 * Delete num characters after the cursor
98 */
99protected void
98c_delafter(el, num)
99 EditLine *el;
100 int num;
100c_delafter(EditLine *el, int num)
101{
102
101{
102
103 if (el->el_line.cursor + num > el->el_line.lastchar)
104 num = el->el_line.lastchar - el->el_line.cursor;
103 if (el->el_line.cursor + num > el->el_line.lastchar)
104 num = el->el_line.lastchar - el->el_line.cursor;
105
105
106 if (num > 0) {
107 char *cp;
106 if (num > 0) {
107 char *cp;
108
108
109 if (el->el_map.current != el->el_map.emacs)
110 cv_undo(el, INSERT, num, el->el_line.cursor);
109 if (el->el_map.current != el->el_map.emacs)
110 cv_undo(el, INSERT, (size_t)num, el->el_line.cursor);
111
111
112 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
113 *cp = cp[num];
112 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
113 *cp = cp[num];
114
114
115 el->el_line.lastchar -= num;
116 }
115 el->el_line.lastchar -= num;
116 }
117}
118
119
120/* c_delbefore():
121 * Delete num characters before the cursor
122 */
123protected void
117}
118
119
120/* c_delbefore():
121 * Delete num characters before the cursor
122 */
123protected void
124c_delbefore(el, num)
125 EditLine *el;
126 int num;
124c_delbefore(EditLine *el, int num)
127{
128
125{
126
129 if (el->el_line.cursor - num < el->el_line.buffer)
130 num = el->el_line.cursor - el->el_line.buffer;
127 if (el->el_line.cursor - num < el->el_line.buffer)
128 num = el->el_line.cursor - el->el_line.buffer;
131
129
132 if (num > 0) {
133 char *cp;
130 if (num > 0) {
131 char *cp;
134
132
135 if (el->el_map.current != el->el_map.emacs)
136 cv_undo(el, INSERT, num, el->el_line.cursor - num);
133 if (el->el_map.current != el->el_map.emacs)
134 cv_undo(el, INSERT, (size_t)num,
135 el->el_line.cursor - num);
137
136
138 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
139 *cp = cp[num];
137 for (cp = el->el_line.cursor - num;
138 cp <= el->el_line.lastchar;
139 cp++)
140 *cp = cp[num];
140
141
141 el->el_line.lastchar -= num;
142 }
142 el->el_line.lastchar -= num;
143 }
143}
144
145
146/* ce__isword():
147 * Return if p is part of a word according to emacs
148 */
149protected int
144}
145
146
147/* ce__isword():
148 * Return if p is part of a word according to emacs
149 */
150protected int
150ce__isword(p)
151 int p;
151ce__isword(int p)
152{
152{
153 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
153 return (isalpha((unsigned char)p) || isdigit((unsigned char)p) || strchr("*?_-.[]~=", p) != NULL);
154}
155
156
157/* cv__isword():
158 * Return type of word for p according to vi
159 */
160protected int
154}
155
156
157/* cv__isword():
158 * Return type of word for p according to vi
159 */
160protected int
161cv__isword(p)
162 int p;
161cv__isword(int p)
163{
164 if (isspace((unsigned char) p))
165 return 0;
166 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
167 return 1;
168 return 2;
169}
170

--- 8 unchanged lines hidden (view full) ---

179 return !isspace((unsigned char) p);
180}
181
182
183/* c__prev_word():
184 * Find the previous word
185 */
186protected char *
162{
163 if (isspace((unsigned char) p))
164 return 0;
165 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
166 return 1;
167 return 2;
168}
169

--- 8 unchanged lines hidden (view full) ---

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