Merge from vendor branch LUKEMFTP:
[dragonfly.git] / lib / libedit / chared.c
CommitLineData
984263bc
MD
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
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
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.
1de703da
MD
35 *
36 * @(#)chared.c 8.1 (Berkeley) 6/4/93
2c3b12ff 37 * $DragonFly: src/lib/libedit/chared.c,v 1.6 2005/08/04 17:27:09 drhodus Exp $
984263bc
MD
38 */
39
984263bc
MD
40/*
41 * chared.c: Character editor utilities
42 */
43#include "sys.h"
44
45#include <stdlib.h>
46#include "el.h"
47
48/* cv_undo():
49 * Handle state for the vi undo command
50 */
51protected void
2c3b12ff 52cv_undo(EditLine *el, int action, int size, char *ptr)
984263bc
MD
53{
54 c_undo_t *vu = &el->el_chared.c_undo;
55 vu->action = action;
56 vu->ptr = ptr;
57 vu->isize = size;
58 (void) memcpy(vu->buf, vu->ptr, size);
59#ifdef DEBUG_UNDO
60 (void) fprintf(el->el_errfile, "Undo buffer \"%s\" size = +%d -%d\n",
61 vu->ptr, vu->isize, vu->dsize);
62#endif
63}
64
65
66/* c_insert():
67 * Insert num characters
68 */
69protected void
2c3b12ff 70c_insert(EditLine *el, int num)
984263bc
MD
71{
72 char *cp;
73
74 if (el->el_line.lastchar + num >= el->el_line.limit)
75 return; /* can't go past end of buffer */
76
77 if (el->el_line.cursor < el->el_line.lastchar) {
78 /* if I must move chars */
79 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
80 cp[num] = *cp;
81 }
82 el->el_line.lastchar += num;
83} /* end c_insert */
84
85
86/* c_delafter():
87 * Delete num characters after the cursor
88 */
89protected void
2c3b12ff 90c_delafter(EditLine *el, int num)
984263bc
MD
91{
92
93 if (el->el_line.cursor + num > el->el_line.lastchar)
94 num = el->el_line.lastchar - el->el_line.cursor;
95
96 if (num > 0) {
97 char *cp;
98
99 if (el->el_map.current != el->el_map.emacs)
100 cv_undo(el, INSERT, num, el->el_line.cursor);
101
102 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
103 *cp = cp[num];
104
105 el->el_line.lastchar -= num;
106 }
107}
108
109
110/* c_delbefore():
111 * Delete num characters before the cursor
112 */
113protected void
2c3b12ff 114c_delbefore(EditLine *el, int num)
984263bc
MD
115{
116
117 if (el->el_line.cursor - num < el->el_line.buffer)
118 num = el->el_line.cursor - el->el_line.buffer;
119
120 if (num > 0) {
121 char *cp;
122
123 if (el->el_map.current != el->el_map.emacs)
124 cv_undo(el, INSERT, num, el->el_line.cursor - num);
125
126 for (cp = el->el_line.cursor - num; cp <= el->el_line.lastchar; cp++)
127 *cp = cp[num];
128
129 el->el_line.lastchar -= num;
130 }
131}
132
133
134/* ce__isword():
135 * Return if p is part of a word according to emacs
136 */
137protected int
2c3b12ff 138ce__isword(int p)
984263bc
MD
139{
140 return isalpha((unsigned char) p) || isdigit((unsigned char) p) || strchr("*?_-.[]~=", p) != NULL;
141}
142
143
144/* cv__isword():
145 * Return type of word for p according to vi
146 */
147protected int
2c3b12ff 148cv__isword(int p)
984263bc
MD
149{
150 if (isspace((unsigned char) p))
151 return 0;
152 if ((unsigned char) p == '_' || isalnum((unsigned char) p))
153 return 1;
154 return 2;
155}
156
157
158/* c___isword():
159 * Return if p is part of a space-delimited word (!isspace)
160 */
161protected int
2c3b12ff 162c___isword(int p)
984263bc
MD
163{
164 return !isspace((unsigned char) p);
165}
166
167
168/* c__prev_word():
169 * Find the previous word
170 */
171protected char *
2c3b12ff 172c__prev_word(char *p, char *low, int n, int (*wtest)(int))
984263bc
MD
173{
174 p--;
175
176 while (n--) {
177 while ((p >= low) && !(*wtest)((unsigned char) *p))
178 p--;
179 while ((p >= low) && (*wtest)((unsigned char) *p))
180 p--;
181 }
182
183 /* cp now points to one character before the word */
184 p++;
185 if (p < low)
186 p = low;
187 /* cp now points where we want it */
188 return p;
189}
190
191
192/* c__next_word():
193 * Find the next word
194 */
195protected char *
2c3b12ff 196c__next_word(char *p, char *high, int n, int (*wtest)(int))
984263bc
MD
197{
198 while (n--) {
199 while ((p < high) && !(*wtest)((unsigned char) *p))
200 p++;
201 while ((p < high) && (*wtest)((unsigned char) *p))
202 p++;
203 }
204 if (p > high)
205 p = high;
206 /* p now points where we want it */
207 return p;
208}
209
210/* cv_next_word():
211 * Find the next word vi style
212 */
213protected char *
2c3b12ff 214cv_next_word(EditLine *el, char *p, char *high, int n, int (*wtest)(int))
984263bc
MD
215{
216 int test;
217
218 while (n--) {
219 test = (*wtest)((unsigned char) *p);
220 while ((p < high) && (*wtest)((unsigned char) *p) == test)
221 p++;
222 /*
223 * vi historically deletes with cw only the word preserving the
224 * trailing whitespace! This is not what 'w' does..
225 */
226 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
227 while ((p < high) && isspace((unsigned char) *p))
228 p++;
229 }
230
231 /* p now points where we want it */
232 if (p > high)
233 return high;
234 else
235 return p;
236}
237
238
239/* cv_prev_word():
240 * Find the previous word vi style
241 */
242protected char *
2c3b12ff 243cv_prev_word(EditLine *el, char *p, char *low, int n, int (*wtest)(int))
984263bc
MD
244{
245 int test;
246
247 while (n--) {
248 p--;
249 /*
250 * vi historically deletes with cb only the word preserving the
251 * leading whitespace! This is not what 'b' does..
252 */
253 if (el->el_chared.c_vcmd.action != (DELETE|INSERT))
254 while ((p > low) && isspace((unsigned char) *p))
255 p--;
256 test = (*wtest)((unsigned char) *p);
257 while ((p >= low) && (*wtest)((unsigned char) *p) == test)
258 p--;
259 p++;
260 while (isspace((unsigned char) *p))
261 p++;
262 }
263
264 /* p now points where we want it */
265 if (p < low)
266 return low;
267 else
268 return p;
269}
270
271
272#ifdef notdef
273/* c__number():
274 * Ignore character p points to, return number appearing after that.
275 * A '$' by itself means a big number; "$-" is for negative; '^' means 1.
276 * Return p pointing to last char used.
277 */
278protected char *
2c3b12ff
DR
279c__number(
280 char *p, /* character position */
281 int *num, /* Return value */
282 int dval, /* dval is the number to subtract from like $-3 */
283)
984263bc 284{
660c873b
DR
285 int i;
286 int sign = 1;
984263bc
MD
287
288 if (*++p == '^') {
289 *num = 1;
290 return p;
291 }
292 if (*p == '$') {
293 if (*++p != '-') {
294 *num = 0x7fffffff; /* Handle $ */
295 return --p;
296 }
297 sign = -1; /* Handle $- */
298 ++p;
299 }
300 for (i = 0; isdigit((unsigned char) *p); i = 10 * i + *p++ - '0')
301 continue;
302 *num = (sign < 0 ? dval - i : i);
303 return --p;
304}
305#endif
306
307/* cv_delfini():
308 * Finish vi delete action
309 */
310protected void
2c3b12ff 311cv_delfini(EditLine *el)
984263bc 312{
660c873b 313 int size;
984263bc
MD
314 int oaction;
315
316 if (el->el_chared.c_vcmd.action & INSERT)
317 el->el_map.current = el->el_map.key;
318
319 oaction = el->el_chared.c_vcmd.action;
320 el->el_chared.c_vcmd.action = NOP;
321
322 if (el->el_chared.c_vcmd.pos == 0)
323 return;
324
325
326 if (el->el_line.cursor > el->el_chared.c_vcmd.pos) {
327 size = (int) (el->el_line.cursor - el->el_chared.c_vcmd.pos);
328 c_delbefore(el, size);
329 el->el_line.cursor = el->el_chared.c_vcmd.pos;
330 re_refresh_cursor(el);
331 }
332 else if (el->el_line.cursor < el->el_chared.c_vcmd.pos) {
333 size = (int)(el->el_chared.c_vcmd.pos - el->el_line.cursor);
334 c_delafter(el, size);
335 }
336 else {
337 size = 1;
338 c_delafter(el, size);
339 }
340 switch (oaction) {
341 case DELETE|INSERT:
342 el->el_chared.c_undo.action = DELETE|INSERT;
343 break;
344 case DELETE:
345 el->el_chared.c_undo.action = INSERT;
346 break;
347 case NOP:
348 case INSERT:
349 default:
350 abort();
351 break;
352 }
353
354
355 el->el_chared.c_undo.ptr = el->el_line.cursor;
356 el->el_chared.c_undo.dsize = size;
357}
358
359
360#ifdef notdef
361/* ce__endword():
362 * Go to the end of this word according to emacs
363 */
364protected char *
2c3b12ff 365ce__endword(char *p, char *high, int n)
984263bc
MD
366{
367 p++;
368
369 while (n--) {
370 while ((p < high) && isspace((unsigned char) *p))
371 p++;
372 while ((p < high) && !isspace((unsigned char) *p))
373 p++;
374 }
375
376 p--;
377 return p;
378}
379#endif
380
381
382/* cv__endword():
383 * Go to the end of this word according to vi
384 */
385protected char *
2c3b12ff 386cv__endword(char *p, char *high, int n)
984263bc
MD
387{
388 p++;
389
390 while (n--) {
391 while ((p < high) && isspace((unsigned char) *p))
392 p++;
393
394 if (isalnum((unsigned char) *p))
395 while ((p < high) && isalnum((unsigned char) *p))
396 p++;
397 else
398 while ((p < high) && !(isspace((unsigned char) *p) ||
399 isalnum((unsigned char) *p)))
400 p++;
401 }
402 p--;
403 return p;
404}
405
406/* ch_init():
407 * Initialize the character editor
408 */
409protected int
2c3b12ff 410ch_init(EditLine *el)
984263bc
MD
411{
412 el->el_line.buffer = (char *) el_malloc(EL_BUFSIZ);
413 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ);
414 el->el_line.cursor = el->el_line.buffer;
415 el->el_line.lastchar = el->el_line.buffer;
416 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - 2];
417
418 el->el_chared.c_undo.buf = (char *) el_malloc(EL_BUFSIZ);
419 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ);
420 el->el_chared.c_undo.action = NOP;
421 el->el_chared.c_undo.isize = 0;
422 el->el_chared.c_undo.dsize = 0;
423 el->el_chared.c_undo.ptr = el->el_line.buffer;
424
425 el->el_chared.c_vcmd.action = NOP;
426 el->el_chared.c_vcmd.pos = el->el_line.buffer;
427 el->el_chared.c_vcmd.ins = el->el_line.buffer;
428
429 el->el_chared.c_kill.buf = (char *) el_malloc(EL_BUFSIZ);
430 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ);
431 el->el_chared.c_kill.mark = el->el_line.buffer;
432 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
433
434 el->el_map.current = el->el_map.key;
435
436 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
437 el->el_state.doingarg = 0;
438 el->el_state.metanext = 0;
439 el->el_state.argument = 1;
440 el->el_state.lastcmd = ED_UNASSIGNED;
441
442 el->el_chared.c_macro.nline = NULL;
443 el->el_chared.c_macro.level = -1;
444 el->el_chared.c_macro.macro = (char **) el_malloc(EL_MAXMACRO *
445 sizeof(char *));
446 return 0;
447}
448
449/* ch_reset():
450 * Reset the character editor
451 */
452protected void
2c3b12ff 453ch_reset(EditLine *el)
984263bc
MD
454{
455 el->el_line.cursor = el->el_line.buffer;
456 el->el_line.lastchar = el->el_line.buffer;
457
458 el->el_chared.c_undo.action = NOP;
459 el->el_chared.c_undo.isize = 0;
460 el->el_chared.c_undo.dsize = 0;
461 el->el_chared.c_undo.ptr = el->el_line.buffer;
462
463 el->el_chared.c_vcmd.action = NOP;
464 el->el_chared.c_vcmd.pos = el->el_line.buffer;
465 el->el_chared.c_vcmd.ins = el->el_line.buffer;
466
467 el->el_chared.c_kill.mark = el->el_line.buffer;
468
469 el->el_map.current = el->el_map.key;
470
471 el->el_state.inputmode = MODE_INSERT; /* XXX: save a default */
472 el->el_state.doingarg = 0;
473 el->el_state.metanext = 0;
474 el->el_state.argument = 1;
475 el->el_state.lastcmd = ED_UNASSIGNED;
476
477 el->el_chared.c_macro.level = -1;
478
479 el->el_history.eventno = 0;
480}
481
482
483/* ch_end():
484 * Free the data structures used by the editor
485 */
486protected void
2c3b12ff 487ch_end(EditLine *el)
984263bc
MD
488{
489 el_free((ptr_t) el->el_line.buffer);
490 el->el_line.buffer = NULL;
491 el->el_line.limit = NULL;
492 el_free((ptr_t) el->el_chared.c_undo.buf);
493 el->el_chared.c_undo.buf = NULL;
494 el_free((ptr_t) el->el_chared.c_kill.buf);
495 el->el_chared.c_kill.buf = NULL;
496 el_free((ptr_t) el->el_chared.c_macro.macro);
497 el->el_chared.c_macro.macro = NULL;
498 ch_reset(el);
499}
500
501
502/* el_insertstr():
503 * Insert string at cursorI
504 */
505public int
2c3b12ff 506el_insertstr(EditLine *el, char *s)
984263bc
MD
507{
508 int len;
509
510 if ((len = strlen(s)) == 0)
511 return -1;
512 if (el->el_line.lastchar + len >= el->el_line.limit)
513 return -1;
514
515 c_insert(el, len);
516 while (*s)
517 *el->el_line.cursor++ = *s++;
518 return 0;
519}
520
521
522/* el_deletestr():
523 * Delete num characters before the cursor
524 */
525public void
2c3b12ff 526el_deletestr(EditLine *el, int n)
984263bc
MD
527{
528 if (n <= 0)
529 return;
530
531 if (el->el_line.cursor < &el->el_line.buffer[n])
532 return;
533
534 c_delbefore(el, n); /* delete before dot */
535 el->el_line.cursor -= n;
536 if (el->el_line.cursor < el->el_line.buffer)
537 el->el_line.cursor = el->el_line.buffer;
538}
539
540/* c_gets():
541 * Get a string
542 */
543protected int
2c3b12ff 544c_gets(EditLine *el, char *buf)
984263bc
MD
545{
546 char ch;
547 int len = 0;
548
549 for (ch = 0; ch == 0;) {
550 if (el_getc(el, &ch) != 1)
551 return ed_end_of_file(el, 0);
552 switch (ch) {
553 case '\010': /* Delete and backspace */
554 case '\177':
555 if (len > 1) {
556 *el->el_line.cursor-- = '\0';
557 el->el_line.lastchar = el->el_line.cursor;
558 buf[len--] = '\0';
559 }
560 else {
561 el->el_line.buffer[0] = '\0';
562 el->el_line.lastchar = el->el_line.buffer;
563 el->el_line.cursor = el->el_line.buffer;
564 return CC_REFRESH;
565 }
566 re_refresh(el);
567 ch = 0;
568 break;
569
570 case '\033': /* ESC */
571 case '\r': /* Newline */
572 case '\n':
573 break;
574
575 default:
576 if (len >= EL_BUFSIZ)
577 term_beep(el);
578 else {
579 buf[len++] = ch;
580 *el->el_line.cursor++ = ch;
581 el->el_line.lastchar = el->el_line.cursor;
582 }
583 re_refresh(el);
584 ch = 0;
585 break;
586 }
587 }
588 buf[len] = ch;
589 return len;
590}
591
592
593/* c_hpos():
594 * Return the current horizontal position of the cursor
595 */
596protected int
2c3b12ff 597c_hpos(EditLine *el)
984263bc
MD
598{
599 char *ptr;
600
601 /*
602 * Find how many characters till the beginning of this line.
603 */
604 if (el->el_line.cursor == el->el_line.buffer)
605 return 0;
606 else {
607 for (ptr = el->el_line.cursor - 1;
608 ptr >= el->el_line.buffer && *ptr != '\n';
609 ptr--)
610 continue;
611 return el->el_line.cursor - ptr - 1;
612 }
613}