36 #if !defined(lint) && !defined(SCCSID)
38 static char sccsid[] =
"@(#)chared.c 8.1 (Berkeley) 6/4/93";
49 private void ch__clearmacro (
EditLine *);
60 c_undo_t *vu = &el->el_chared.c_undo;
65 size = (size_t)(el->el_line.lastchar - el->el_line.buffer);
66 vu->len = (ssize_t)size;
67 vu->cursor = (int)(el->el_line.cursor - el->el_line.buffer);
68 (void)memcpy(vu->buf, el->el_line.buffer, size *
sizeof(*vu->buf));
71 r->count = el->el_state.doingarg ? el->el_state.argument : 0;
72 r->action = el->el_chared.c_vcmd.action;
74 r->cmd = el->el_state.thiscmd;
75 r->ch = el->el_state.thisch;
82 cv_yank(
EditLine *el,
const Char *ptr,
int size)
86 (void)memcpy(k->buf, ptr, (
size_t)size *
sizeof(*k->buf));
87 k->last = k->buf +
size;
99 if (el->el_line.lastchar + num >= el->el_line.limit) {
100 if (!ch_enlargebufs(el, (
size_t)num))
104 if (el->el_line.cursor < el->el_line.lastchar) {
106 for (cp = el->el_line.lastchar; cp >= el->el_line.cursor; cp--)
109 el->el_line.lastchar += num;
120 if (el->el_line.cursor + num > el->el_line.lastchar)
121 num = (int)(el->el_line.lastchar - el->el_line.cursor);
123 if (el->el_map.current != el->el_map.emacs) {
125 cv_yank(el, el->el_line.cursor, num);
131 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
134 el->el_line.lastchar -= num;
147 for (cp = el->el_line.cursor; cp <= el->el_line.lastchar; cp++)
150 el->el_line.lastchar--;
161 if (el->el_line.cursor - num < el->el_line.buffer)
162 num = (int)(el->el_line.cursor - el->el_line.buffer);
164 if (el->el_map.current != el->el_map.emacs) {
166 cv_yank(el, el->el_line.cursor - num, num);
172 for (cp = el->el_line.cursor - num;
173 cp <= el->el_line.lastchar;
177 el->el_line.lastchar -= num;
190 for (cp = el->el_line.cursor - 1; cp <= el->el_line.lastchar; cp++)
193 el->el_line.lastchar--;
203 return Isalnum(p) || Strchr(STR(
"*?_-.[]~="), p) != NULL;
213 if (Isalnum(p) || p ==
'_')
235 c__prev_word(Char *p, Char *low,
int n,
int (*wtest)(Int))
240 while ((p >= low) && !(*wtest)(*p))
242 while ((p >= low) && (*wtest)(*p))
259 c__next_word(Char *p, Char *high,
int n,
int (*wtest)(Int))
262 while ((p < high) && !(*wtest)(*p))
264 while ((p < high) && (*wtest)(*p))
277 cv_next_word(
EditLine *el, Char *p, Char *high,
int n,
int (*wtest)(Int))
283 while ((p < high) && (*wtest)(*p) == test)
289 if (n || el->el_chared.c_vcmd.action != (DELETE|INSERT))
290 while ((p < high) && Isspace(*p))
306 cv_prev_word(Char *p, Char *low,
int n,
int (*wtest)(Int))
312 while ((p > low) && Isspace(*p))
315 while ((p >= low) && (*wtest)(*p) == test)
335 int action = el->el_chared.c_vcmd.action;
338 el->el_map.current = el->el_map.key;
340 if (el->el_chared.c_vcmd.pos == 0)
344 size = (int)(el->el_line.cursor - el->el_chared.c_vcmd.pos);
347 el->el_line.cursor = el->el_chared.c_vcmd.pos;
350 cv_yank(el, el->el_line.cursor, size);
352 cv_yank(el, el->el_line.cursor + size, -size);
355 c_delafter(el, size);
356 re_refresh_cursor(el);
358 c_delbefore(el, -size);
359 el->el_line.cursor +=
size;
362 el->el_chared.c_vcmd.action = NOP;
370 cv__endword(Char *p, Char *high,
int n,
int (*wtest)(Int))
377 while ((p < high) && Isspace(*p))
381 while ((p < high) && (*wtest)(*p) == test)
396 el->el_line.buffer = el_malloc(EL_BUFSIZ *
397 sizeof(*el->el_line.buffer));
398 if (el->el_line.buffer == NULL)
401 (void) memset(el->el_line.buffer, 0, EL_BUFSIZ *
402 sizeof(*el->el_line.buffer));
403 el->el_line.cursor = el->el_line.buffer;
404 el->el_line.lastchar = el->el_line.buffer;
405 el->el_line.limit = &el->el_line.buffer[EL_BUFSIZ - EL_LEAVE];
407 el->el_chared.c_undo.buf = el_malloc(EL_BUFSIZ *
408 sizeof(*el->el_chared.c_undo.buf));
409 if (el->el_chared.c_undo.buf == NULL)
411 (void) memset(el->el_chared.c_undo.buf, 0, EL_BUFSIZ *
412 sizeof(*el->el_chared.c_undo.buf));
413 el->el_chared.c_undo.len = -1;
414 el->el_chared.c_undo.cursor = 0;
415 el->el_chared.c_redo.buf = el_malloc(EL_BUFSIZ *
416 sizeof(*el->el_chared.c_redo.buf));
417 if (el->el_chared.c_redo.buf == NULL)
419 el->el_chared.c_redo.pos = el->el_chared.c_redo.buf;
420 el->el_chared.c_redo.lim = el->el_chared.c_redo.buf + EL_BUFSIZ;
421 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
423 el->el_chared.c_vcmd.action = NOP;
424 el->el_chared.c_vcmd.pos = el->el_line.buffer;
426 el->el_chared.c_kill.buf = el_malloc(EL_BUFSIZ *
427 sizeof(*el->el_chared.c_kill.buf));
428 if (el->el_chared.c_kill.buf == NULL)
430 (void) memset(el->el_chared.c_kill.buf, 0, EL_BUFSIZ *
431 sizeof(*el->el_chared.c_kill.buf));
432 el->el_chared.c_kill.mark = el->el_line.buffer;
433 el->el_chared.c_kill.last = el->el_chared.c_kill.buf;
434 el->el_chared.c_resizefun = NULL;
435 el->el_chared.c_resizearg = NULL;
437 el->el_map.current = el->el_map.key;
439 el->el_state.inputmode = MODE_INSERT;
440 el->el_state.doingarg = 0;
441 el->el_state.metanext = 0;
442 el->el_state.argument = 1;
443 el->el_state.lastcmd = ED_UNASSIGNED;
447 ma->macro = el_malloc(EL_MAXMACRO *
sizeof(*ma->macro));
448 if (ma->macro == NULL)
459 el->el_line.cursor = el->el_line.buffer;
460 el->el_line.lastchar = el->el_line.buffer;
462 el->el_chared.c_undo.len = -1;
463 el->el_chared.c_undo.cursor = 0;
465 el->el_chared.c_vcmd.action = NOP;
466 el->el_chared.c_vcmd.pos = el->el_line.buffer;
468 el->el_chared.c_kill.mark = el->el_line.buffer;
470 el->el_map.current = el->el_map.key;
472 el->el_state.inputmode = MODE_INSERT;
473 el->el_state.doingarg = 0;
474 el->el_state.metanext = 0;
475 el->el_state.argument = 1;
476 el->el_state.lastcmd = ED_UNASSIGNED;
478 el->el_history.eventno = 0;
488 while (ma->level >= 0)
489 el_free(ma->macro[ma->level--]);
497 ch_enlargebufs(
EditLine *el,
size_t addlen)
500 Char *newbuffer, *oldbuf, *oldkbuf;
502 sz = (size_t)(el->el_line.limit - el->el_line.buffer + EL_LEAVE);
509 while(newsz - sz < addlen)
516 newbuffer = el_realloc(el->el_line.buffer, newsz *
sizeof(*newbuffer));
521 (void) memset(&newbuffer[sz], 0, (newsz - sz) *
sizeof(*newbuffer));
523 oldbuf = el->el_line.buffer;
525 el->el_line.buffer = newbuffer;
526 el->el_line.cursor = newbuffer + (el->el_line.cursor - oldbuf);
527 el->el_line.lastchar = newbuffer + (el->el_line.lastchar - oldbuf);
529 el->el_line.limit = &newbuffer[sz - EL_LEAVE];
534 newbuffer = el_realloc(el->el_chared.c_kill.buf, newsz *
540 (void) memset(&newbuffer[sz], 0, (newsz - sz) *
sizeof(*newbuffer));
542 oldkbuf = el->el_chared.c_kill.buf;
544 el->el_chared.c_kill.buf = newbuffer;
545 el->el_chared.c_kill.last = newbuffer +
546 (el->el_chared.c_kill.last - oldkbuf);
547 el->el_chared.c_kill.mark = el->el_line.buffer +
548 (el->el_chared.c_kill.mark - oldbuf);
553 newbuffer = el_realloc(el->el_chared.c_undo.buf,
554 newsz *
sizeof(*newbuffer));
559 (void) memset(&newbuffer[sz], 0, (newsz - sz) *
sizeof(*newbuffer));
560 el->el_chared.c_undo.buf = newbuffer;
562 newbuffer = el_realloc(el->el_chared.c_redo.buf,
563 newsz *
sizeof(*newbuffer));
566 el->el_chared.c_redo.pos = newbuffer +
567 (el->el_chared.c_redo.pos - el->el_chared.c_redo.buf);
568 el->el_chared.c_redo.lim = newbuffer +
569 (el->el_chared.c_redo.lim - el->el_chared.c_redo.buf);
570 el->el_chared.c_redo.buf = newbuffer;
572 if (!hist_enlargebuf(el, sz, newsz))
576 el->el_line.limit = &el->el_line.buffer[newsz - EL_LEAVE];
577 if (el->el_chared.c_resizefun)
578 (*el->el_chared.c_resizefun)(el, el->el_chared.c_resizearg);
588 el_free(el->el_line.buffer);
589 el->el_line.buffer = NULL;
590 el->el_line.limit = NULL;
591 el_free(el->el_chared.c_undo.buf);
592 el->el_chared.c_undo.buf = NULL;
593 el_free(el->el_chared.c_redo.buf);
594 el->el_chared.c_redo.buf = NULL;
595 el->el_chared.c_redo.pos = NULL;
596 el->el_chared.c_redo.lim = NULL;
597 el->el_chared.c_redo.cmd = ED_UNASSIGNED;
598 el_free(el->el_chared.c_kill.buf);
599 el->el_chared.c_kill.buf = NULL;
601 el_free(el->el_chared.c_macro.macro);
602 el->el_chared.c_macro.macro = NULL;
610 FUN(el,insertstr)(
EditLine *el,
const Char *s)
614 if ((len = Strlen(s)) == 0)
616 if (el->el_line.lastchar + len >= el->el_line.limit) {
617 if (!ch_enlargebufs(el, len))
621 c_insert(el, (
int)len);
623 *el->el_line.cursor++ = *s++;
637 if (el->el_line.cursor < &el->el_line.buffer[n])
641 el->el_line.cursor -=
n;
642 if (el->el_line.cursor < el->el_line.buffer)
643 el->el_line.cursor = el->el_line.buffer;
650 c_gets(
EditLine *el, Char *
buf,
const Char *prompt)
654 Char *cp = el->el_line.buffer;
657 len = (ssize_t)Strlen(prompt);
658 (void)memcpy(cp, prompt, (
size_t)len *
sizeof(*cp));
664 el->el_line.cursor = cp;
666 el->el_line.lastchar = cp + 1;
669 if (FUN(el,getc)(el, &ch) != 1) {
670 ed_end_of_file(el, 0);
693 if (len >= (ssize_t)(EL_BUFSIZ - 16))
704 el->el_line.buffer[0] =
'\0';
705 el->el_line.lastchar = el->el_line.buffer;
706 el->el_line.cursor = el->el_line.buffer;
722 if (el->el_line.cursor == el->el_line.buffer)
725 for (ptr = el->el_line.cursor - 1;
726 ptr >= el->el_line.buffer && *ptr !=
'\n';
729 return (
int)(el->el_line.cursor - ptr - 1);
734 ch_resizefun(
EditLine *el, el_zfunc_t f,
void *a)
736 el->el_chared.c_resizefun = f;
737 el->el_chared.c_resizearg = a;