emacs.c revision 1.25 1 /* $NetBSD: emacs.c,v 1.25 2004/07/07 19:46:57 mycroft Exp $ */
2
3 /*
4 * Emacs-like command line editing and history
5 *
6 * created by Ron Natalie at BRL
7 * modified by Doug Kingston, Doug Gwyn, and Lou Salkind
8 * adapted to PD ksh by Eric Gisin
9 */
10 #include <sys/cdefs.h>
11
12 #ifndef lint
13 __RCSID("$NetBSD: emacs.c,v 1.25 2004/07/07 19:46:57 mycroft Exp $");
14 #endif
15
16
17 #include "config.h"
18 #ifdef EMACS
19
20 #include "sh.h"
21 #include "ksh_stat.h"
22 #include "ksh_dir.h"
23 #include <ctype.h>
24 #include <locale.h>
25 #include "edit.h"
26
27 static Area aedit;
28 #define AEDIT &aedit /* area for kill ring and macro defns */
29
30 #undef CTRL /* _BSD brain damage */
31 #define CTRL(x) ((x) == '?' ? 0x7F : (x) & 0x1F) /* ASCII */
32 #define UNCTRL(x) ((x) == 0x7F ? '?' : (x) | 0x40) /* ASCII */
33 #define META(x) ((x) & 0x7f)
34 #define ISMETA(x) (Flag(FEMACSUSEMETA) && ((x) & 0x80))
35
36
37 /* values returned by keyboard functions */
38 #define KSTD 0
39 #define KEOL 1 /* ^M, ^J */
40 #define KINTR 2 /* ^G, ^C */
41
42 struct x_ftab {
43 int (*xf_func) ARGS((int c));
44 const char *xf_name;
45 short xf_flags;
46 };
47
48 /* index into struct x_ftab x_ftab[] - small is good */
49 typedef unsigned char Findex;
50
51 struct x_defbindings {
52 Findex xdb_func; /* XFUNC_* */
53 unsigned char xdb_tab;
54 unsigned char xdb_char;
55 };
56
57 #define XF_ARG 1 /* command takes number prefix */
58 #define XF_NOBIND 2 /* not allowed to bind to function */
59 #define XF_PREFIX 4 /* function sets prefix */
60
61 /* Separator for completion */
62 #define is_cfs(c) (c == ' ' || c == '\t' || c == '"' || c == '\'')
63 #define is_mfs(c) (!(isalnum((unsigned char)c) || c == '_' || c == '$')) /* Separator for motion */
64
65 #ifdef OS2
66 /* Deal with 8 bit chars & an extra prefix for function key (these two
67 * changes increase memory usage from 9,216 bytes to 24,416 bytes...)
68 */
69 # define CHARMASK 0xFF /* 8-bit ASCII character mask */
70 # define X_NTABS 4 /* normal, meta1, meta2, meta3 */
71 static int x_prefix3 = 0xE0;
72 #else /* OS2 */
73 # define CHARMASK 0xFF /* 8-bit character mask */
74 # define X_NTABS 3 /* normal, meta1, meta2 */
75 #endif /* OS2 */
76 #define X_TABSZ (CHARMASK+1) /* size of keydef tables etc */
77
78 /* Arguments for do_complete()
79 * 0 = enumerate M-= complete as much as possible and then list
80 * 1 = complete M-Esc
81 * 2 = list M-?
82 */
83 typedef enum { CT_LIST, /* list the possible completions */
84 CT_COMPLETE, /* complete to longest prefix */
85 CT_COMPLIST /* complete and then list (if non-exact) */
86 } Comp_type;
87
88 /* { from 4.9 edit.h */
89 /*
90 * The following are used for my horizontal scrolling stuff
91 */
92 static char *xbuf; /* beg input buffer */
93 static char *xend; /* end input buffer */
94 static char *xcp; /* current position */
95 static char *xep; /* current end */
96 static char *xbp; /* start of visible portion of input buffer */
97 static char *xlp; /* last char visible on screen */
98 static int x_adj_ok;
99 /*
100 * we use x_adj_done so that functions can tell
101 * whether x_adjust() has been called while they are active.
102 */
103 static int x_adj_done;
104
105 static int xx_cols;
106 static int x_col;
107 static int x_displen;
108 static int x_arg; /* general purpose arg */
109 static int x_arg_defaulted;/* x_arg not explicitly set; defaulted to 1 */
110
111 static int xlp_valid;
112 /* end from 4.9 edit.h } */
113
114 static int x_prefix1 = CTRL('['), x_prefix2 = CTRL('X');
115 static char **x_histp; /* history position */
116 static int x_nextcmd; /* for newline-and-next */
117 static char *xmp; /* mark pointer */
118 static Findex x_last_command;
119 static Findex (*x_tab)[X_TABSZ]; /* key definition */
120 static char *(*x_atab)[X_TABSZ]; /* macro definitions */
121 static unsigned char x_bound[(X_TABSZ * X_NTABS + 7) / 8];
122 #define KILLSIZE 20
123 static char *killstack[KILLSIZE];
124 static int killsp, killtp;
125 static int x_curprefix;
126 static char *macroptr;
127 static int prompt_trunc;
128 static int prompt_skip;
129
130 static int x_ins ARGS((char *cp));
131 static void x_delete ARGS((int nc, int push));
132 static int x_bword ARGS((void));
133 static int x_fword ARGS((void));
134 static void x_goto ARGS((char *cp));
135 static void x_bs ARGS((int c));
136 static int x_size_str ARGS((char *cp));
137 static int x_size ARGS((int c));
138 static void x_zots ARGS((char *str));
139 static void x_zotc ARGS((int c));
140 static void x_load_hist ARGS((char **hp));
141 static int x_search ARGS((char *pat, int sameline, int offset));
142 static int x_match ARGS((char *str, char *pat));
143 static void x_redraw ARGS((int limit));
144 static void x_push ARGS((int nchars));
145 static char * x_mapin ARGS((const char *cp));
146 static char * x_mapout ARGS((int c));
147 static void x_print ARGS((int prefix, int key));
148 static void x_adjust ARGS((void));
149 static void x_e_ungetc ARGS((int c));
150 static int x_e_getc ARGS((void));
151 static void x_e_putc ARGS((int c));
152 static void x_e_puts ARGS((const char *s));
153 static int x_comment ARGS((int c));
154 static int x_fold_case ARGS((int c));
155 static char *x_lastcp ARGS((void));
156 static void do_complete ARGS((int flags, Comp_type type));
157 static int x_emacs_putbuf ARGS((const char *s, size_t len));
158
159
160 /* The lines between START-FUNC-TAB .. END-FUNC-TAB are run through a
161 * script (emacs-gen.sh) that generates emacs.out which contains:
162 * - function declarations for x_* functions
163 * - defines of the form XFUNC_<name> where <name> is function
164 * name, sans leading x_.
165 * Note that the script treats #ifdef and { 0, 0, 0} specially - use with
166 * caution.
167 */
168 #include "emacs.out"
169 static const struct x_ftab x_ftab[] = {
170 /* @START-FUNC-TAB@ */
171 { x_abort, "abort", 0 },
172 { x_beg_hist, "beginning-of-history", 0 },
173 { x_comp_comm, "complete-command", 0 },
174 { x_comp_file, "complete-file", 0 },
175 { x_complete, "complete", 0 },
176 { x_del_back, "delete-char-backward", XF_ARG },
177 { x_del_bword, "delete-word-backward", XF_ARG },
178 { x_del_char, "delete-char-forward", XF_ARG },
179 { x_del_fword, "delete-word-forward", XF_ARG },
180 { x_del_line, "kill-line", 0 },
181 { x_draw_line, "redraw", 0 },
182 { x_end_hist, "end-of-history", 0 },
183 { x_end_of_text, "eot", 0 },
184 { x_enumerate, "list", 0 },
185 { x_eot_del, "eot-or-delete", XF_ARG },
186 { x_error, "error", 0 },
187 { x_goto_hist, "goto-history", XF_ARG },
188 { x_ins_string, "macro-string", XF_NOBIND },
189 { x_insert, "auto-insert", XF_ARG },
190 { x_kill, "kill-to-eol", XF_ARG },
191 { x_kill_region, "kill-region", 0 },
192 { x_list_comm, "list-command", 0 },
193 { x_list_file, "list-file", 0 },
194 { x_literal, "quote", 0 },
195 { x_meta1, "prefix-1", XF_PREFIX },
196 { x_meta2, "prefix-2", XF_PREFIX },
197 { x_meta_yank, "yank-pop", 0 },
198 { x_mv_back, "backward-char", XF_ARG },
199 { x_mv_begin, "beginning-of-line", 0 },
200 { x_mv_bword, "backward-word", XF_ARG },
201 { x_mv_end, "end-of-line", 0 },
202 { x_mv_forw, "forward-char", XF_ARG },
203 { x_mv_fword, "forward-word", XF_ARG },
204 { x_newline, "newline", 0 },
205 { x_next_com, "down-history", XF_ARG },
206 { x_nl_next_com, "newline-and-next", 0 },
207 { x_noop, "no-op", 0 },
208 { x_prev_com, "up-history", XF_ARG },
209 { x_prev_histword, "prev-hist-word", XF_ARG },
210 { x_search_char_forw, "search-character-forward", XF_ARG },
211 { x_search_char_back, "search-character-backward", XF_ARG },
212 { x_search_hist, "search-history", 0 },
213 { x_set_mark, "set-mark-command", 0 },
214 { x_stuff, "stuff", 0 },
215 { x_stuffreset, "stuff-reset", 0 },
216 { x_transpose, "transpose-chars", 0 },
217 { x_version, "version", 0 },
218 { x_xchg_point_mark, "exchange-point-and-mark", 0 },
219 { x_yank, "yank", 0 },
220 { x_comp_list, "complete-list", 0 },
221 { x_expand, "expand-file", 0 },
222 { x_fold_capitalize, "capitalize-word", XF_ARG },
223 { x_fold_lower, "downcase-word", XF_ARG },
224 { x_fold_upper, "upcase-word", XF_ARG },
225 { x_set_arg, "set-arg", XF_NOBIND },
226 { x_comment, "comment", 0 },
227 #ifdef SILLY
228 { x_game_of_life, "play-game-of-life", 0 },
229 #else
230 { 0, 0, 0 },
231 #endif
232 #ifdef DEBUG
233 { x_debug_info, "debug-info", 0 },
234 #else
235 { 0, 0, 0 },
236 #endif
237 #ifdef OS2
238 { x_meta3, "prefix-3", XF_PREFIX },
239 #else
240 { 0, 0, 0 },
241 #endif
242 /* @END-FUNC-TAB@ */
243 };
244
245 static struct x_defbindings const x_defbindings[] = {
246 { XFUNC_del_back, 0, CTRL('?') },
247 { XFUNC_del_bword, 1, CTRL('?') },
248 { XFUNC_eot_del, 0, CTRL('D') },
249 { XFUNC_del_back, 0, CTRL('H') },
250 { XFUNC_del_bword, 1, CTRL('H') },
251 { XFUNC_del_bword, 1, 'h' },
252 { XFUNC_mv_bword, 1, 'b' },
253 { XFUNC_mv_fword, 1, 'f' },
254 { XFUNC_del_fword, 1, 'd' },
255 { XFUNC_mv_back, 0, CTRL('B') },
256 { XFUNC_mv_forw, 0, CTRL('F') },
257 { XFUNC_search_char_forw, 0, CTRL(']') },
258 { XFUNC_search_char_back, 1, CTRL(']') },
259 { XFUNC_newline, 0, CTRL('M') },
260 { XFUNC_newline, 0, CTRL('J') },
261 { XFUNC_end_of_text, 0, CTRL('_') },
262 { XFUNC_abort, 0, CTRL('G') },
263 { XFUNC_prev_com, 0, CTRL('P') },
264 { XFUNC_next_com, 0, CTRL('N') },
265 { XFUNC_nl_next_com, 0, CTRL('O') },
266 { XFUNC_search_hist, 0, CTRL('R') },
267 { XFUNC_beg_hist, 1, '<' },
268 { XFUNC_end_hist, 1, '>' },
269 { XFUNC_goto_hist, 1, 'g' },
270 { XFUNC_mv_end, 0, CTRL('E') },
271 { XFUNC_mv_begin, 0, CTRL('A') },
272 { XFUNC_draw_line, 0, CTRL('L') },
273 { XFUNC_meta1, 0, CTRL('[') },
274 { XFUNC_meta2, 0, CTRL('X') },
275 { XFUNC_kill, 0, CTRL('K') },
276 { XFUNC_yank, 0, CTRL('Y') },
277 { XFUNC_meta_yank, 1, 'y' },
278 { XFUNC_literal, 0, CTRL('^') },
279 { XFUNC_comment, 1, '#' },
280 #if defined(BRL) && defined(TIOCSTI)
281 { XFUNC_stuff, 0, CTRL('T') },
282 #else
283 { XFUNC_transpose, 0, CTRL('T') },
284 #endif
285 { XFUNC_complete, 1, CTRL('[') },
286 { XFUNC_comp_list, 0, CTRL('I') },
287 { XFUNC_comp_list, 1, '=' },
288 { XFUNC_enumerate, 1, '?' },
289 { XFUNC_expand, 1, '*' },
290 { XFUNC_comp_file, 1, CTRL('X') },
291 { XFUNC_comp_comm, 2, CTRL('[') },
292 { XFUNC_list_comm, 2, '?' },
293 { XFUNC_list_file, 2, CTRL('Y') },
294 { XFUNC_set_mark, 1, ' ' },
295 { XFUNC_kill_region, 0, CTRL('W') },
296 { XFUNC_xchg_point_mark, 2, CTRL('X') },
297 { XFUNC_version, 0, CTRL('V') },
298 #ifdef DEBUG
299 { XFUNC_debug_info, 1, CTRL('H') },
300 #endif
301 { XFUNC_prev_histword, 1, '.' },
302 { XFUNC_prev_histword, 1, '_' },
303 { XFUNC_set_arg, 1, '0' },
304 { XFUNC_set_arg, 1, '1' },
305 { XFUNC_set_arg, 1, '2' },
306 { XFUNC_set_arg, 1, '3' },
307 { XFUNC_set_arg, 1, '4' },
308 { XFUNC_set_arg, 1, '5' },
309 { XFUNC_set_arg, 1, '6' },
310 { XFUNC_set_arg, 1, '7' },
311 { XFUNC_set_arg, 1, '8' },
312 { XFUNC_set_arg, 1, '9' },
313 { XFUNC_fold_upper, 1, 'U' },
314 { XFUNC_fold_upper, 1, 'u' },
315 { XFUNC_fold_lower, 1, 'L' },
316 { XFUNC_fold_lower, 1, 'l' },
317 { XFUNC_fold_capitalize, 1, 'C' },
318 { XFUNC_fold_capitalize, 1, 'c' },
319 #ifdef OS2
320 { XFUNC_meta3, 0, 0xE0 },
321 { XFUNC_mv_back, 3, 'K' },
322 { XFUNC_mv_forw, 3, 'M' },
323 { XFUNC_next_com, 3, 'P' },
324 { XFUNC_prev_com, 3, 'H' },
325 #endif /* OS2 */
326 /* These for ansi arrow keys: arguablely shouldn't be here by
327 * default, but its simpler/faster/smaller than using termcap
328 * entries.
329 */
330 { XFUNC_meta2, 1, '[' },
331 { XFUNC_meta2, 1, 'O' },
332 { XFUNC_prev_com, 2, 'A' },
333 { XFUNC_next_com, 2, 'B' },
334 { XFUNC_mv_forw, 2, 'C' },
335 { XFUNC_mv_back, 2, 'D' },
336 };
337
338 int
339 x_emacs(buf, len)
340 char *buf;
341 size_t len;
342 {
343 int c;
344 const char *p;
345 int i;
346 Findex f;
347
348 xbp = xbuf = buf; xend = buf + len;
349 xlp = xcp = xep = buf;
350 *xcp = 0;
351 xlp_valid = TRUE;
352 xmp = NULL;
353 x_curprefix = 0;
354 macroptr = (char *) 0;
355 x_histp = histptr + 1;
356 x_last_command = XFUNC_error;
357
358 xx_cols = x_cols;
359 x_col = promptlen(prompt, &p);
360 prompt_skip = p - prompt;
361 prompt_trunc = x_col - (x_cols - 3 - MIN_EDIT_SPACE);
362 if (prompt_trunc > 0)
363 x_col -= prompt_trunc;
364 else
365 prompt_trunc = 0;
366 x_adj_ok = 1;
367 x_displen = xx_cols - 2 - x_col;
368 x_adj_done = 0;
369
370 pprompt(prompt, prompt_trunc);
371
372 if (x_nextcmd >= 0) {
373 int off = source->line - x_nextcmd;
374 if (histptr - histlist >= off)
375 x_load_hist(histptr - off);
376 x_nextcmd = -1;
377 }
378
379 while (1) {
380 x_flush();
381 if ((c = x_e_getc()) < 0)
382 return 0;
383
384 if (ISMETA(c)) {
385 c = META(c);
386 x_curprefix = 1;
387 }
388
389 f = x_curprefix == -1 ? XFUNC_insert
390 : x_tab[x_curprefix][c&CHARMASK];
391
392 if (!(x_ftab[f].xf_flags & XF_PREFIX)
393 && x_last_command != XFUNC_set_arg)
394 {
395 x_arg = 1;
396 x_arg_defaulted = 1;
397 }
398 i = c | (x_curprefix << 8);
399 x_curprefix = 0;
400 switch (i = (*x_ftab[f].xf_func)(i)) {
401 case KSTD:
402 if (!(x_ftab[f].xf_flags & XF_PREFIX))
403 x_last_command = f;
404 break;
405 case KEOL:
406 i = xep - xbuf;
407 return i;
408 case KINTR: /* special case for interrupt */
409 trapsig(SIGINT);
410 x_mode(FALSE);
411 unwind(LSHELL);
412 }
413 }
414 }
415
416 static int
417 x_insert(c)
418 int c;
419 {
420 char str[2];
421
422 /*
423 * Should allow tab and control chars.
424 */
425 if (c == 0) {
426 x_e_putc(BEL);
427 return KSTD;
428 }
429 str[0] = c;
430 str[1] = '\0';
431 while (x_arg--)
432 x_ins(str);
433 return KSTD;
434 }
435
436 static int
437 x_ins_string(c)
438 int c;
439 {
440 if (macroptr) {
441 x_e_putc(BEL);
442 return KSTD;
443 }
444 macroptr = x_atab[c>>8][c & CHARMASK];
445 if (macroptr && !*macroptr) {
446 /* XXX bell? */
447 macroptr = (char *) 0;
448 }
449 return KSTD;
450 }
451
452 static int x_do_ins(const char *cp, int len);
453
454 static int
455 x_do_ins(cp, len)
456 const char *cp;
457 int len;
458 {
459 if (xep+len >= xend) {
460 x_e_putc(BEL);
461 return -1;
462 }
463
464 memmove(xcp+len, xcp, xep - xcp + 1);
465 memmove(xcp, cp, len);
466 xcp += len;
467 xep += len;
468 return 0;
469 }
470
471 static int
472 x_ins(s)
473 char *s;
474 {
475 char *cp = xcp;
476 register int adj = x_adj_done;
477
478 if (x_do_ins(s, strlen(s)) < 0)
479 return -1;
480 /*
481 * x_zots() may result in a call to x_adjust()
482 * we want xcp to reflect the new position.
483 */
484 xlp_valid = FALSE;
485 x_lastcp();
486 x_adj_ok = (xcp >= xlp);
487 x_zots(cp);
488 if (adj == x_adj_done) /* has x_adjust() been called? */
489 {
490 /* no */
491 for (cp = xlp; cp > xcp; )
492 x_bs(*--cp);
493 }
494
495 x_adj_ok = 1;
496 return 0;
497 }
498
499 /*
500 * this is used for x_escape() in do_complete()
501 */
502 static int
503 x_emacs_putbuf(s, len)
504 const char *s;
505 size_t len;
506 {
507 int rval;
508
509 if ((rval = x_do_ins(s, len)) != 0)
510 return (rval);
511 return (rval);
512 }
513
514 static int
515 x_del_back(c)
516 int c;
517 {
518 int col = xcp - xbuf;
519
520 if (col == 0) {
521 x_e_putc(BEL);
522 return KSTD;
523 }
524 if (x_arg > col)
525 x_arg = col;
526 x_goto(xcp - x_arg);
527 x_delete(x_arg, FALSE);
528 return KSTD;
529 }
530
531 static int
532 x_del_char(c)
533 int c;
534 {
535 int nleft = xep - xcp;
536
537 if (!nleft) {
538 x_e_putc(BEL);
539 return KSTD;
540 }
541 if (x_arg > nleft)
542 x_arg = nleft;
543 x_delete(x_arg, FALSE);
544 return KSTD;
545 }
546
547 /* Delete nc chars to the right of the cursor (including cursor position) */
548 static void
549 x_delete(nc, push)
550 int nc;
551 int push;
552 {
553 int i,j;
554 char *cp;
555
556 if (nc == 0)
557 return;
558 if (xmp != NULL && xmp > xcp) {
559 if (xcp + nc > xmp)
560 xmp = xcp;
561 else
562 xmp -= nc;
563 }
564
565 /*
566 * This lets us yank a word we have deleted.
567 */
568 if (push)
569 x_push(nc);
570
571 xep -= nc;
572 cp = xcp;
573 j = 0;
574 i = nc;
575 while (i--) {
576 j += x_size(*cp++);
577 }
578 memmove(xcp, xcp+nc, xep - xcp + 1); /* Copies the null */
579 x_adj_ok = 0; /* don't redraw */
580 x_zots(xcp);
581 /*
582 * if we are already filling the line,
583 * there is no need to ' ','\b'.
584 * But if we must, make sure we do the minimum.
585 */
586 if ((i = x_displen) > 0)
587 {
588 j = (j < i) ? j : i;
589 i = j;
590 while (i--)
591 x_e_putc(' ');
592 i = j;
593 while (i--)
594 x_e_putc('\b');
595 }
596 /*x_goto(xcp);*/
597 x_adj_ok = 1;
598 xlp_valid = FALSE;
599 for (cp = x_lastcp(); cp > xcp; )
600 x_bs(*--cp);
601
602 return;
603 }
604
605 static int
606 x_del_bword(c)
607 int c;
608 {
609 x_delete(x_bword(), TRUE);
610 return KSTD;
611 }
612
613 static int
614 x_mv_bword(c)
615 int c;
616 {
617 (void)x_bword();
618 return KSTD;
619 }
620
621 static int
622 x_mv_fword(c)
623 int c;
624 {
625 x_goto(xcp + x_fword());
626 return KSTD;
627 }
628
629 static int
630 x_del_fword(c)
631 int c;
632 {
633 x_delete(x_fword(), TRUE);
634 return KSTD;
635 }
636
637 static int
638 x_bword()
639 {
640 int nc = 0;
641 register char *cp = xcp;
642
643 if (cp == xbuf) {
644 x_e_putc(BEL);
645 return 0;
646 }
647 while (x_arg--)
648 {
649 while (cp != xbuf && is_mfs(cp[-1]))
650 {
651 cp--;
652 nc++;
653 }
654 while (cp != xbuf && !is_mfs(cp[-1]))
655 {
656 cp--;
657 nc++;
658 }
659 }
660 x_goto(cp);
661 return nc;
662 }
663
664 static int
665 x_fword()
666 {
667 int nc = 0;
668 register char *cp = xcp;
669
670 if (cp == xep) {
671 x_e_putc(BEL);
672 return 0;
673 }
674 while (x_arg--)
675 {
676 while (cp != xep && is_mfs(*cp))
677 {
678 cp++;
679 nc++;
680 }
681 while (cp != xep && !is_mfs(*cp))
682 {
683 cp++;
684 nc++;
685 }
686 }
687 return nc;
688 }
689
690 static void
691 x_goto(cp)
692 register char *cp;
693 {
694 if (cp < xbp || cp >= (xbp + x_displen))
695 {
696 /* we are heading off screen */
697 xcp = cp;
698 x_adjust();
699 }
700 else
701 {
702 if (cp < xcp) /* move back */
703 {
704 while (cp < xcp)
705 x_bs(*--xcp);
706 }
707 else
708 {
709 if (cp > xcp) /* move forward */
710 {
711 while (cp > xcp)
712 x_zotc(*xcp++);
713 }
714 }
715 }
716 }
717
718 static void
719 x_bs(c)
720 int c;
721 {
722 register int i;
723 i = x_size(c);
724 while (i--)
725 x_e_putc('\b');
726 }
727
728 static int
729 x_size_str(cp)
730 register char *cp;
731 {
732 register int size = 0;
733 while (*cp)
734 size += x_size(*cp++);
735 return size;
736 }
737
738 static int
739 x_size(c)
740 int c;
741 {
742 if (c=='\t')
743 return 4; /* Kludge, tabs are always four spaces. */
744 if (iscntrl(c)) /* control char */
745 return 2;
746 return 1;
747 }
748
749 static void
750 x_zots(str)
751 register char *str;
752 {
753 register int adj = x_adj_done;
754
755 x_lastcp();
756 while (*str && str < xlp && adj == x_adj_done)
757 x_zotc(*str++);
758 }
759
760 static void
761 x_zotc(c)
762 int c;
763 {
764 if (c == '\t') {
765 /* Kludge, tabs are always four spaces. */
766 x_e_puts(" ");
767 } else if (iscntrl(c)) {
768 x_e_putc('^');
769 x_e_putc(UNCTRL(c));
770 } else
771 x_e_putc(c);
772 }
773
774 static int
775 x_mv_back(c)
776 int c;
777 {
778 int col = xcp - xbuf;
779
780 if (col == 0) {
781 x_e_putc(BEL);
782 return KSTD;
783 }
784 if (x_arg > col)
785 x_arg = col;
786 x_goto(xcp - x_arg);
787 return KSTD;
788 }
789
790 static int
791 x_mv_forw(c)
792 int c;
793 {
794 int nleft = xep - xcp;
795
796 if (!nleft) {
797 x_e_putc(BEL);
798 return KSTD;
799 }
800 if (x_arg > nleft)
801 x_arg = nleft;
802 x_goto(xcp + x_arg);
803 return KSTD;
804 }
805
806 static int
807 x_search_char_forw(c)
808 int c;
809 {
810 char *cp = xcp;
811
812 *xep = '\0';
813 c = x_e_getc();
814 while (x_arg--) {
815 if (c < 0
816 || ((cp = (cp == xep) ? NULL : strchr(cp + 1, c)) == NULL
817 && (cp = strchr(xbuf, c)) == NULL))
818 {
819 x_e_putc(BEL);
820 return KSTD;
821 }
822 }
823 x_goto(cp);
824 return KSTD;
825 }
826
827 static int
828 x_search_char_back(c)
829 int c;
830 {
831 char *cp = xcp, *p;
832
833 c = x_e_getc();
834 for (; x_arg--; cp = p)
835 for (p = cp; ; ) {
836 if (p-- == xbuf)
837 p = xep;
838 if (c < 0 || p == cp) {
839 x_e_putc(BEL);
840 return KSTD;
841 }
842 if (*p == c)
843 break;
844 }
845 x_goto(cp);
846 return KSTD;
847 }
848
849 static int
850 x_newline(c)
851 int c;
852 {
853 x_e_putc('\r');
854 x_e_putc('\n');
855 x_flush();
856 *xep++ = '\n';
857 return KEOL;
858 }
859
860 static int
861 x_end_of_text(c)
862 int c;
863 {
864 return KEOL;
865 }
866
867 static int x_beg_hist(c) int c; { x_load_hist(histlist); return KSTD;}
868
869 static int x_end_hist(c) int c; { x_load_hist(histptr); return KSTD;}
870
871 static int x_prev_com(c) int c; { x_load_hist(x_histp - x_arg); return KSTD;}
872
873 static int x_next_com(c) int c; { x_load_hist(x_histp + x_arg); return KSTD;}
874
875 /* Goto a particular history number obtained from argument.
876 * If no argument is given history 1 is probably not what you
877 * want so we'll simply go to the oldest one.
878 */
879 static int
880 x_goto_hist(c)
881 int c;
882 {
883 if (x_arg_defaulted)
884 x_load_hist(histlist);
885 else
886 x_load_hist(histptr + x_arg - source->line);
887 return KSTD;
888 }
889
890 static void
891 x_load_hist(hp)
892 register char **hp;
893 {
894 int oldsize;
895
896 if (hp < histlist || hp > histptr) {
897 x_e_putc(BEL);
898 return;
899 }
900 x_histp = hp;
901 oldsize = x_size_str(xbuf);
902 strlcpy(xbuf, *hp, xend - xbuf);
903 xbp = xbuf;
904 xep = xcp = xbuf + strlen(xbuf);
905 xlp_valid = FALSE;
906 if (xep > x_lastcp())
907 x_goto(xep);
908 else
909 x_redraw(oldsize);
910 }
911
912 static int
913 x_nl_next_com(c)
914 int c;
915 {
916 x_nextcmd = source->line - (histptr - x_histp) + 1;
917 return (x_newline(c));
918 }
919
920 static int
921 x_eot_del(c)
922 int c;
923 {
924 if (xep == xbuf && x_arg_defaulted)
925 return (x_end_of_text(c));
926 else
927 return (x_del_char(c));
928 }
929
930 /* reverse incremental history search */
931 static int
932 x_search_hist(c)
933 int c;
934 {
935 int offset = -1; /* offset of match in xbuf, else -1 */
936 char pat [256+1]; /* pattern buffer */
937 register char *p = pat;
938 Findex f;
939
940 *p = '\0';
941 while (1) {
942 if (offset < 0) {
943 x_e_puts("\nI-search: ");
944 x_e_puts(pat);
945 }
946 x_flush();
947 if ((c = x_e_getc()) < 0)
948 return KSTD;
949 f = x_tab[0][c&CHARMASK];
950 if (c == CTRL('['))
951 break;
952 else if (f == XFUNC_search_hist)
953 offset = x_search(pat, 0, offset);
954 else if (f == XFUNC_del_back) {
955 if (p == pat) {
956 offset = -1;
957 break;
958 }
959 if (p > pat)
960 *--p = '\0';
961 if (p == pat)
962 offset = -1;
963 else
964 offset = x_search(pat, 1, offset);
965 continue;
966 } else if (f == XFUNC_insert) {
967 /* add char to pattern */
968 /* overflow check... */
969 if (p >= &pat[sizeof(pat) - 1]) {
970 x_e_putc(BEL);
971 continue;
972 }
973 *p++ = c, *p = '\0';
974 if (offset >= 0) {
975 /* already have partial match */
976 offset = x_match(xbuf, pat);
977 if (offset >= 0) {
978 x_goto(xbuf + offset + (p - pat) - (*pat == '^'));
979 continue;
980 }
981 }
982 offset = x_search(pat, 0, offset);
983 } else { /* other command */
984 x_e_ungetc(c);
985 break;
986 }
987 }
988 if (offset < 0)
989 x_redraw(-1);
990 return KSTD;
991 }
992
993 /* search backward from current line */
994 static int
995 x_search(pat, sameline, offset)
996 char *pat;
997 int sameline;
998 int offset;
999 {
1000 register char **hp;
1001 int i;
1002
1003 for (hp = x_histp - (sameline ? 0 : 1) ; hp >= histlist; --hp) {
1004 i = x_match(*hp, pat);
1005 if (i >= 0) {
1006 if (offset < 0)
1007 x_e_putc('\n');
1008 x_load_hist(hp);
1009 x_goto(xbuf + i + strlen(pat) - (*pat == '^'));
1010 return i;
1011 }
1012 }
1013 x_e_putc(BEL);
1014 x_histp = histptr;
1015 return -1;
1016 }
1017
1018 /* return position of first match of pattern in string, else -1 */
1019 static int
1020 x_match(str, pat)
1021 char *str, *pat;
1022 {
1023 if (*pat == '^') {
1024 return (strncmp(str, pat+1, strlen(pat+1)) == 0) ? 0 : -1;
1025 } else {
1026 char *q = strstr(str, pat);
1027 return (q == NULL) ? -1 : q - str;
1028 }
1029 }
1030
1031 static int
1032 x_del_line(c)
1033 int c;
1034 {
1035 int i, j;
1036
1037 *xep = 0;
1038 i = xep - xbuf;
1039 j = x_size_str(xbuf);
1040 xcp = xbuf;
1041 x_push(i);
1042 xlp = xbp = xep = xbuf;
1043 xlp_valid = TRUE;
1044 *xcp = 0;
1045 xmp = NULL;
1046 x_redraw(j);
1047 return KSTD;
1048 }
1049
1050 static int
1051 x_mv_end(c)
1052 int c;
1053 {
1054 x_goto(xep);
1055 return KSTD;
1056 }
1057
1058 static int
1059 x_mv_begin(c)
1060 int c;
1061 {
1062 x_goto(xbuf);
1063 return KSTD;
1064 }
1065
1066 static int
1067 x_draw_line(c)
1068 int c;
1069 {
1070 x_redraw(-1);
1071 return KSTD;
1072
1073 }
1074
1075 /* Redraw (part of) the line. If limit is < 0, the everything is redrawn
1076 * on a NEW line, otherwise limit is the screen column up to which needs
1077 * redrawing.
1078 */
1079 static void
1080 x_redraw(limit)
1081 int limit;
1082 {
1083 int i, j;
1084 char *cp;
1085
1086 x_adj_ok = 0;
1087 if (limit == -1)
1088 x_e_putc('\n');
1089 else
1090 x_e_putc('\r');
1091 x_flush();
1092 if (xbp == xbuf)
1093 {
1094 pprompt(prompt + prompt_skip, 0);
1095 x_col = promptlen(prompt, (const char **) 0);
1096 }
1097 x_displen = xx_cols - 2 - x_col;
1098 xlp_valid = FALSE;
1099 cp = x_lastcp();
1100 x_zots(xbp);
1101 if (xbp != xbuf || xep > xlp)
1102 limit = xx_cols;
1103 if (limit >= 0)
1104 {
1105 if (xep > xlp)
1106 i = 0; /* we fill the line */
1107 else
1108 i = limit - (xlp - xbp);
1109
1110 for (j = 0; j < i && x_col < (xx_cols - 2); j++)
1111 x_e_putc(' ');
1112 i = ' ';
1113 if (xep > xlp) /* more off screen */
1114 {
1115 if (xbp > xbuf)
1116 i = '*';
1117 else
1118 i = '>';
1119 }
1120 else
1121 if (xbp > xbuf)
1122 i = '<';
1123 x_e_putc(i);
1124 j++;
1125 while (j--)
1126 x_e_putc('\b');
1127 }
1128 for (cp = xlp; cp > xcp; )
1129 x_bs(*--cp);
1130 x_adj_ok = 1;
1131 D__(x_flush();)
1132 return;
1133 }
1134
1135 static int
1136 x_transpose(c)
1137 int c;
1138 {
1139 char tmp;
1140
1141 /* What transpose is meant to do seems to be up for debate. This
1142 * is a general summary of the options; the text is abcd with the
1143 * upper case character or underscore indicating the cursor position:
1144 * Who Before After Before After
1145 * at&t ksh in emacs mode: abCd abdC abcd_ (bell)
1146 * at&t ksh in gmacs mode: abCd baCd abcd_ abdc_
1147 * gnu emacs: abCd acbD abcd_ abdc_
1148 * Pdksh currently goes with GNU behavior since I believe this is the
1149 * most common version of emacs, unless in gmacs mode, in which case
1150 * it does the at&t ksh gmacs mode.
1151 * This should really be broken up into 3 functions so users can bind
1152 * to the one they want.
1153 */
1154 if (xcp == xbuf) {
1155 x_e_putc(BEL);
1156 return KSTD;
1157 } else if (xcp == xep || Flag(FGMACS)) {
1158 if (xcp - xbuf == 1) {
1159 x_e_putc(BEL);
1160 return KSTD;
1161 }
1162 /* Gosling/Unipress emacs style: Swap two characters before the
1163 * cursor, do not change cursor position
1164 */
1165 x_bs(xcp[-1]);
1166 x_bs(xcp[-2]);
1167 x_zotc(xcp[-1]);
1168 x_zotc(xcp[-2]);
1169 tmp = xcp[-1];
1170 xcp[-1] = xcp[-2];
1171 xcp[-2] = tmp;
1172 } else {
1173 /* GNU emacs style: Swap the characters before and under the
1174 * cursor, move cursor position along one.
1175 */
1176 x_bs(xcp[-1]);
1177 x_zotc(xcp[0]);
1178 x_zotc(xcp[-1]);
1179 tmp = xcp[-1];
1180 xcp[-1] = xcp[0];
1181 xcp[0] = tmp;
1182 x_bs(xcp[0]);
1183 x_goto(xcp + 1);
1184 }
1185 return KSTD;
1186 }
1187
1188 static int
1189 x_literal(c)
1190 int c;
1191 {
1192 x_curprefix = -1;
1193 return KSTD;
1194 }
1195
1196 static int
1197 x_meta1(c)
1198 int c;
1199 {
1200 x_curprefix = 1;
1201 return KSTD;
1202 }
1203
1204 static int
1205 x_meta2(c)
1206 int c;
1207 {
1208 x_curprefix = 2;
1209 return KSTD;
1210 }
1211
1212 #ifdef OS2
1213 static int
1214 x_meta3(c)
1215 int c;
1216 {
1217 x_curprefix = 3;
1218 return KSTD;
1219 }
1220 #endif /* OS2 */
1221
1222 static int
1223 x_kill(c)
1224 int c;
1225 {
1226 int col = xcp - xbuf;
1227 int lastcol = xep - xbuf;
1228 int ndel;
1229
1230 if (x_arg_defaulted)
1231 x_arg = lastcol;
1232 else if (x_arg > lastcol)
1233 x_arg = lastcol;
1234 ndel = x_arg - col;
1235 if (ndel < 0) {
1236 x_goto(xbuf + x_arg);
1237 ndel = -ndel;
1238 }
1239 x_delete(ndel, TRUE);
1240 return KSTD;
1241 }
1242
1243 static void
1244 x_push(nchars)
1245 int nchars;
1246 {
1247 char *cp = str_nsave(xcp, nchars, AEDIT);
1248 if (killstack[killsp])
1249 afree((void *)killstack[killsp], AEDIT);
1250 killstack[killsp] = cp;
1251 killsp = (killsp + 1) % KILLSIZE;
1252 }
1253
1254 static int
1255 x_yank(c)
1256 int c;
1257 {
1258 if (killsp == 0)
1259 killtp = KILLSIZE;
1260 else
1261 killtp = killsp;
1262 killtp --;
1263 if (killstack[killtp] == 0) {
1264 x_e_puts("\nnothing to yank");
1265 x_redraw(-1);
1266 return KSTD;
1267 }
1268 xmp = xcp;
1269 x_ins(killstack[killtp]);
1270 return KSTD;
1271 }
1272
1273 static int
1274 x_meta_yank(c)
1275 int c;
1276 {
1277 int len;
1278 if ((x_last_command != XFUNC_yank && x_last_command != XFUNC_meta_yank)
1279 || killstack[killtp] == 0) {
1280 killtp = killsp;
1281 x_e_puts("\nyank something first");
1282 x_redraw(-1);
1283 return KSTD;
1284 }
1285 len = strlen(killstack[killtp]);
1286 x_goto(xcp - len);
1287 x_delete(len, FALSE);
1288 do {
1289 if (killtp == 0)
1290 killtp = KILLSIZE - 1;
1291 else
1292 killtp--;
1293 } while (killstack[killtp] == 0);
1294 x_ins(killstack[killtp]);
1295 return KSTD;
1296 }
1297
1298 static int
1299 x_abort(c)
1300 int c;
1301 {
1302 /* x_zotc(c); */
1303 xlp = xep = xcp = xbp = xbuf;
1304 xlp_valid = TRUE;
1305 *xcp = 0;
1306 return KINTR;
1307 }
1308
1309 static int
1310 x_error(c)
1311 int c;
1312 {
1313 x_e_putc(BEL);
1314 return KSTD;
1315 }
1316
1317 static int
1318 x_stuffreset(c)
1319 int c;
1320 {
1321 #ifdef TIOCSTI
1322 (void)x_stuff(c);
1323 return KINTR;
1324 #else
1325 x_zotc(c);
1326 xlp = xcp = xep = xbp = xbuf;
1327 xlp_valid = TRUE;
1328 *xcp = 0;
1329 x_redraw(-1);
1330 return KSTD;
1331 #endif
1332 }
1333
1334 static int
1335 x_stuff(c)
1336 int c;
1337 {
1338 #if 0 || defined TIOCSTI
1339 char ch = c;
1340 bool_t savmode = x_mode(FALSE);
1341
1342 (void)ioctl(TTY, TIOCSTI, &ch);
1343 (void)x_mode(savmode);
1344 x_redraw(-1);
1345 #endif
1346 return KSTD;
1347 }
1348
1349 static char *
1350 x_mapin(cp)
1351 const char *cp;
1352 {
1353 char *new, *op;
1354
1355 op = new = str_save(cp, ATEMP);
1356 while (*cp) {
1357 /* XXX -- should handle \^ escape? */
1358 if (*cp == '^') {
1359 cp++;
1360 #ifdef OS2
1361 if (*cp == '0') /* To define function keys */
1362 *op++ = 0xE0;
1363 else
1364 #endif /* OS2 */
1365 if (*cp >= '?') /* includes '?'; ASCII */
1366 *op++ = CTRL(*cp);
1367 else {
1368 *op++ = '^';
1369 cp--;
1370 }
1371 } else
1372 *op++ = *cp;
1373 cp++;
1374 }
1375 *op = '\0';
1376
1377 return new;
1378 }
1379
1380 static char *
1381 x_mapout(c)
1382 int c;
1383 {
1384 static char buf[8];
1385 register char *p = buf;
1386
1387 #ifdef OS2
1388 if (c == 0xE0) {
1389 *p++ = '^';
1390 *p++ = '0';
1391 } else
1392 #endif /* OS2 */
1393 if (iscntrl(c)) {
1394 *p++ = '^';
1395 *p++ = UNCTRL(c);
1396 } else
1397 *p++ = c;
1398 *p = 0;
1399 return buf;
1400 }
1401
1402 static void
1403 x_print(prefix, key)
1404 int prefix, key;
1405 {
1406 if (prefix == 1)
1407 shprintf("%s", x_mapout(x_prefix1));
1408 if (prefix == 2)
1409 shprintf("%s", x_mapout(x_prefix2));
1410 #ifdef OS2
1411 if (prefix == 3)
1412 shprintf("%s", x_mapout(x_prefix3));
1413 #endif /* OS2 */
1414 shprintf("%s = ", x_mapout(key));
1415 if (x_tab[prefix][key] != XFUNC_ins_string)
1416 shprintf("%s\n", x_ftab[x_tab[prefix][key]].xf_name);
1417 else
1418 shprintf("'%s'\n", x_atab[prefix][key]);
1419 }
1420
1421 int
1422 x_bind(a1, a2, macro, list)
1423 const char *a1, *a2;
1424 int macro; /* bind -m */
1425 int list; /* bind -l */
1426 {
1427 Findex f;
1428 int prefix, key;
1429 char *sp = NULL;
1430 char *m1, *m2;
1431
1432 if (x_tab == NULL) {
1433 bi_errorf("cannot bind, not a tty");
1434 return 1;
1435 }
1436
1437 /* List function names */
1438 if (list) {
1439 for (f = 0; f < NELEM(x_ftab); f++)
1440 if (x_ftab[f].xf_name
1441 && !(x_ftab[f].xf_flags & XF_NOBIND))
1442 shprintf("%s\n", x_ftab[f].xf_name);
1443 return 0;
1444 }
1445
1446 if (a1 == NULL) {
1447 for (prefix = 0; prefix < X_NTABS; prefix++)
1448 for (key = 0; key < X_TABSZ; key++) {
1449 f = x_tab[prefix][key];
1450 if (f == XFUNC_insert || f == XFUNC_error
1451 || (macro && f != XFUNC_ins_string))
1452 continue;
1453 x_print(prefix, key);
1454 }
1455 return 0;
1456 }
1457
1458 m1 = x_mapin(a1);
1459 prefix = key = 0;
1460 for (;; m1++) {
1461 key = *m1 & CHARMASK;
1462 if (x_tab[prefix][key] == XFUNC_meta1)
1463 prefix = 1;
1464 else if (x_tab[prefix][key] == XFUNC_meta2)
1465 prefix = 2;
1466 #ifdef OS2
1467 else if (x_tab[prefix][key] == XFUNC_meta3)
1468 prefix = 3;
1469 #endif /* OS2 */
1470 else
1471 break;
1472 }
1473
1474 if (a2 == NULL) {
1475 x_print(prefix, key);
1476 return 0;
1477 }
1478
1479 if (*a2 == 0)
1480 f = XFUNC_insert;
1481 else if (!macro) {
1482 for (f = 0; f < NELEM(x_ftab); f++)
1483 if (x_ftab[f].xf_name
1484 && strcmp(x_ftab[f].xf_name, a2) == 0)
1485 break;
1486 if (f == NELEM(x_ftab) || x_ftab[f].xf_flags & XF_NOBIND) {
1487 bi_errorf("%s: no such function", a2);
1488 return 1;
1489 }
1490 #if 0 /* This breaks the bind commands that map arrow keys */
1491 if (f == XFUNC_meta1)
1492 x_prefix1 = key;
1493 if (f == XFUNC_meta2)
1494 x_prefix2 = key;
1495 #endif /* 0 */
1496 } else {
1497 f = XFUNC_ins_string;
1498 m2 = x_mapin(a2);
1499 sp = str_save(m2, AEDIT);
1500 }
1501
1502 if (x_tab[prefix][key] == XFUNC_ins_string && x_atab[prefix][key])
1503 afree((void *)x_atab[prefix][key], AEDIT);
1504 x_tab[prefix][key] = f;
1505 x_atab[prefix][key] = sp;
1506
1507 /* Track what the user has bound so x_emacs_keys() won't toast things */
1508 if (f == XFUNC_insert)
1509 x_bound[(prefix * X_TABSZ + key) / 8] &=
1510 ~(1 << ((prefix * X_TABSZ + key) % 8));
1511 else
1512 x_bound[(prefix * X_TABSZ + key) / 8] |=
1513 (1 << ((prefix * X_TABSZ + key) % 8));
1514
1515 return 0;
1516 }
1517
1518 void
1519 x_init_emacs()
1520 {
1521 register int i, j;
1522 char *locale;
1523
1524 ainit(AEDIT);
1525 x_nextcmd = -1;
1526
1527 x_tab = (Findex (*)[X_TABSZ]) alloc(sizeofN(*x_tab, X_NTABS), AEDIT);
1528 for (j = 0; j < X_TABSZ; j++)
1529 x_tab[0][j] = XFUNC_insert;
1530 for (i = 1; i < X_NTABS; i++)
1531 for (j = 0; j < X_TABSZ; j++)
1532 x_tab[i][j] = XFUNC_error;
1533 for (i = 0; i < NELEM(x_defbindings); i++)
1534 x_tab[(unsigned char)x_defbindings[i].xdb_tab][x_defbindings[i].xdb_char]
1535 = x_defbindings[i].xdb_func;
1536
1537 x_atab = (char *(*)[X_TABSZ]) alloc(sizeofN(*x_atab, X_NTABS), AEDIT);
1538 for (i = 1; i < X_NTABS; i++)
1539 for (j = 0; j < X_TABSZ; j++)
1540 x_atab[i][j] = NULL;
1541
1542 /* Determine if we can translate meta key or use 8-bit AscII
1543 * XXX - It would be nice if there was a locale attribute to
1544 * determine if the locale is 7-bit or not.
1545 */
1546 locale = setlocale(LC_CTYPE, NULL);
1547 if (locale != NULL || !strcmp(locale, "C") || !strcmp(locale, "POSIX"))
1548 Flag(FEMACSUSEMETA) = 0;
1549 }
1550
1551 static void bind_if_not_bound(int p, int k, int func);
1552
1553 static void
1554 bind_if_not_bound(p, k, func)
1555 int p, k;
1556 int func;
1557 {
1558 /* Has user already bound this key? If so, don't override it */
1559 if (x_bound[((p) * X_TABSZ + (k)) / 8]
1560 & (1 << (((p) * X_TABSZ + (k)) % 8)))
1561 return;
1562
1563 x_tab[p][k] = func;
1564 }
1565
1566 void
1567 x_emacs_keys(ec)
1568 X_chars *ec;
1569 {
1570 if (ec->erase >= 0) {
1571 bind_if_not_bound(0, ec->erase, XFUNC_del_back);
1572 bind_if_not_bound(1, ec->erase, XFUNC_del_bword);
1573 }
1574 if (ec->kill >= 0)
1575 bind_if_not_bound(0, ec->kill, XFUNC_del_line);
1576 if (ec->werase >= 0)
1577 bind_if_not_bound(0, ec->werase, XFUNC_del_bword);
1578 if (ec->intr >= 0)
1579 bind_if_not_bound(0, ec->intr, XFUNC_abort);
1580 if (ec->quit >= 0)
1581 bind_if_not_bound(0, ec->quit, XFUNC_noop);
1582 }
1583
1584 static int
1585 x_set_mark(c)
1586 int c;
1587 {
1588 xmp = xcp;
1589 return KSTD;
1590 }
1591
1592 static int
1593 x_kill_region(c)
1594 int c;
1595 {
1596 int rsize;
1597 char *xr;
1598
1599 if (xmp == NULL) {
1600 x_e_putc(BEL);
1601 return KSTD;
1602 }
1603 if (xmp > xcp) {
1604 rsize = xmp - xcp;
1605 xr = xcp;
1606 } else {
1607 rsize = xcp - xmp;
1608 xr = xmp;
1609 }
1610 x_goto(xr);
1611 x_delete(rsize, TRUE);
1612 xmp = xr;
1613 return KSTD;
1614 }
1615
1616 static int
1617 x_xchg_point_mark(c)
1618 int c;
1619 {
1620 char *tmp;
1621
1622 if (xmp == NULL) {
1623 x_e_putc(BEL);
1624 return KSTD;
1625 }
1626 tmp = xmp;
1627 xmp = xcp;
1628 x_goto( tmp );
1629 return KSTD;
1630 }
1631
1632 static int
1633 x_version(c)
1634 int c;
1635 {
1636 char *o_xbuf = xbuf, *o_xend = xend;
1637 char *o_xbp = xbp, *o_xep = xep, *o_xcp = xcp;
1638 int lim = x_lastcp() - xbp;
1639
1640 xbuf = xbp = xcp = (char *) ksh_version + 4;
1641 xend = xep = (char *) ksh_version + 4 + strlen(ksh_version + 4);
1642 x_redraw(lim);
1643 x_flush();
1644
1645 c = x_e_getc();
1646 xbuf = o_xbuf;
1647 xend = o_xend;
1648 xbp = o_xbp;
1649 xep = o_xep;
1650 xcp = o_xcp;
1651 x_redraw(strlen(ksh_version));
1652
1653 if (c < 0)
1654 return KSTD;
1655 /* This is what at&t ksh seems to do... Very bizarre */
1656 if (c != ' ')
1657 x_e_ungetc(c);
1658
1659 return KSTD;
1660 }
1661
1662 static int
1663 x_noop(c)
1664 int c;
1665 {
1666 return KSTD;
1667 }
1668
1669 #ifdef SILLY
1670 static int
1671 x_game_of_life(c)
1672 int c;
1673 {
1674 char newbuf [256+1];
1675 register char *ip, *op;
1676 int i, len;
1677
1678 i = xep - xbuf;
1679 *xep = 0;
1680 len = x_size_str(xbuf);
1681 xcp = xbp = xbuf;
1682 memmove(newbuf+1, xbuf, i);
1683 newbuf[0] = 'A';
1684 newbuf[i] = 'A';
1685 for (ip = newbuf+1, op = xbuf; --i >= 0; ip++, op++) {
1686 /* Empty space */
1687 if (*ip < '@' || *ip == '_' || *ip == 0x7F) {
1688 /* Two adults, make whoopee */
1689 if (ip[-1] < '_' && ip[1] < '_') {
1690 /* Make kid look like parents. */
1691 *op = '`' + ((ip[-1] + ip[1])/2)%32;
1692 if (*op == 0x7F) /* Birth defect */
1693 *op = '`';
1694 }
1695 else
1696 *op = ' '; /* nothing happens */
1697 continue;
1698 }
1699 /* Child */
1700 if (*ip > '`') {
1701 /* All alone, dies */
1702 if (ip[-1] == ' ' && ip[1] == ' ')
1703 *op = ' ';
1704 else /* Gets older */
1705 *op = *ip-'`'+'@';
1706 continue;
1707 }
1708 /* Adult */
1709 /* Overcrowded, dies */
1710 if (ip[-1] >= '@' && ip[1] >= '@') {
1711 *op = ' ';
1712 continue;
1713 }
1714 *op = *ip;
1715 }
1716 *op = 0;
1717 x_redraw(len);
1718 return KSTD;
1719 }
1720 #endif
1721
1722 /*
1723 * File/command name completion routines
1724 */
1725
1726
1727 static int
1728 x_comp_comm(c)
1729 int c;
1730 {
1731 do_complete(XCF_COMMAND, CT_COMPLETE);
1732 return KSTD;
1733 }
1734 static int
1735 x_list_comm(c)
1736 int c;
1737 {
1738 do_complete(XCF_COMMAND, CT_LIST);
1739 return KSTD;
1740 }
1741 static int
1742 x_complete(c)
1743 int c;
1744 {
1745 do_complete(XCF_COMMAND_FILE, CT_COMPLETE);
1746 return KSTD;
1747 }
1748 static int
1749 x_enumerate(c)
1750 int c;
1751 {
1752 do_complete(XCF_COMMAND_FILE, CT_LIST);
1753 return KSTD;
1754 }
1755 static int
1756 x_comp_file(c)
1757 int c;
1758 {
1759 do_complete(XCF_FILE, CT_COMPLETE);
1760 return KSTD;
1761 }
1762 static int
1763 x_list_file(c)
1764 int c;
1765 {
1766 do_complete(XCF_FILE, CT_LIST);
1767 return KSTD;
1768 }
1769 static int
1770 x_comp_list(c)
1771 int c;
1772 {
1773 do_complete(XCF_COMMAND_FILE, CT_COMPLIST);
1774 return KSTD;
1775 }
1776 static int
1777 x_expand(c)
1778 int c;
1779 {
1780 char **words;
1781 int nwords = 0;
1782 int start, end;
1783 int is_command;
1784 int i;
1785
1786 nwords = x_cf_glob(XCF_FILE,
1787 xbuf, xep - xbuf, xcp - xbuf,
1788 &start, &end, &words, &is_command);
1789
1790 if (nwords == 0) {
1791 x_e_putc(BEL);
1792 return KSTD;
1793 }
1794
1795 x_goto(xbuf + start);
1796 x_delete(end - start, FALSE);
1797 for (i = 0; i < nwords;) {
1798 if (x_escape(words[i], strlen(words[i]), x_emacs_putbuf) < 0 ||
1799 (++i < nwords && x_ins(space) < 0))
1800 {
1801 x_e_putc(BEL);
1802 return KSTD;
1803 }
1804 }
1805 x_adjust();
1806
1807 return KSTD;
1808 }
1809
1810 /* type == 0 for list, 1 for complete and 2 for complete-list */
1811 static void
1812 do_complete(flags, type)
1813 int flags; /* XCF_{COMMAND,FILE,COMMAND_FILE} */
1814 Comp_type type;
1815 {
1816 char **words;
1817 int nwords;
1818 int start, end, nlen, olen;
1819 int is_command;
1820 int completed = 0;
1821
1822 nwords = x_cf_glob(flags, xbuf, xep - xbuf, xcp - xbuf,
1823 &start, &end, &words, &is_command);
1824 /* no match */
1825 if (nwords == 0) {
1826 x_e_putc(BEL);
1827 return;
1828 }
1829
1830 if (type == CT_LIST) {
1831 x_print_expansions(nwords, words, is_command);
1832 x_redraw(0);
1833 x_free_words(nwords, words);
1834 return;
1835 }
1836
1837 olen = end - start;
1838 nlen = x_longest_prefix(nwords, words);
1839 /* complete */
1840 if (nwords == 1 || nlen > olen) {
1841 x_goto(xbuf + start);
1842 x_delete(olen, FALSE);
1843 x_escape(words[0], nlen, x_emacs_putbuf);
1844 x_adjust();
1845 completed = 1;
1846 }
1847 /* add space if single non-dir match */
1848 if ((nwords == 1) && (!ISDIRSEP(words[0][nlen - 1]))) {
1849 x_ins(space);
1850 completed = 1;
1851 }
1852
1853 if (type == CT_COMPLIST && !completed) {
1854 x_print_expansions(nwords, words, is_command);
1855 completed = 1;
1856 }
1857
1858 if (completed)
1859 x_redraw(0);
1860
1861 x_free_words(nwords, words);
1862 }
1863
1864 /* NAME:
1865 * x_adjust - redraw the line adjusting starting point etc.
1866 *
1867 * DESCRIPTION:
1868 * This function is called when we have exceeded the bounds
1869 * of the edit window. It increments x_adj_done so that
1870 * functions like x_ins and x_delete know that we have been
1871 * called and can skip the x_bs() stuff which has already
1872 * been done by x_redraw.
1873 *
1874 * RETURN VALUE:
1875 * None
1876 */
1877
1878 static void
1879 x_adjust()
1880 {
1881 x_adj_done++; /* flag the fact that we were called. */
1882 /*
1883 * we had a problem if the prompt length > xx_cols / 2
1884 */
1885 if ((xbp = xcp - (x_displen / 2)) < xbuf)
1886 xbp = xbuf;
1887 xlp_valid = FALSE;
1888 x_redraw(xx_cols);
1889 x_flush();
1890 }
1891
1892 static int unget_char = -1;
1893
1894 static void
1895 x_e_ungetc(c)
1896 int c;
1897 {
1898 unget_char = c;
1899 }
1900
1901 static int
1902 x_e_getc()
1903 {
1904 int c;
1905
1906 if (unget_char >= 0) {
1907 c = unget_char;
1908 unget_char = -1;
1909 } else {
1910 if (macroptr) {
1911 c = *macroptr++;
1912 if (!*macroptr)
1913 macroptr = (char *) 0;
1914 } else
1915 c = x_getc();
1916 }
1917
1918 return c <= CHARMASK ? c : (c & CHARMASK);
1919 }
1920
1921 static void
1922 x_e_putc(c)
1923 int c;
1924 {
1925 if (c == '\r' || c == '\n')
1926 x_col = 0;
1927 if (x_col < xx_cols)
1928 {
1929 x_putc(c);
1930 switch(c)
1931 {
1932 case BEL:
1933 break;
1934 case '\r':
1935 case '\n':
1936 break;
1937 case '\b':
1938 x_col--;
1939 break;
1940 default:
1941 x_col++;
1942 break;
1943 }
1944 }
1945 if (x_adj_ok && (x_col < 0 || x_col >= (xx_cols - 2)))
1946 {
1947 x_adjust();
1948 }
1949 }
1950
1951 #ifdef DEBUG
1952 static int
1953 x_debug_info(c)
1954 int c;
1955 {
1956 x_flush();
1957 shellf("\nksh debug:\n");
1958 shellf("\tx_col == %d,\t\tx_cols == %d,\tx_displen == %d\n",
1959 x_col, xx_cols, x_displen);
1960 shellf("\txcp == 0x%lx,\txep == 0x%lx\n", (long) xcp, (long) xep);
1961 shellf("\txbp == 0x%lx,\txbuf == 0x%lx\n", (long) xbp, (long) xbuf);
1962 shellf("\txlp == 0x%lx\n", (long) xlp);
1963 shellf("\txlp == 0x%lx\n", (long) x_lastcp());
1964 shellf(newline);
1965 x_redraw(-1);
1966 return 0;
1967 }
1968 #endif
1969
1970 static void
1971 x_e_puts(s)
1972 const char *s;
1973 {
1974 register int adj = x_adj_done;
1975
1976 while (*s && adj == x_adj_done)
1977 x_e_putc(*s++);
1978 }
1979
1980 /* NAME:
1981 * x_set_arg - set an arg value for next function
1982 *
1983 * DESCRIPTION:
1984 * This is a simple implementation of M-[0-9].
1985 *
1986 * RETURN VALUE:
1987 * KSTD
1988 */
1989
1990 static int
1991 x_set_arg(c)
1992 int c;
1993 {
1994 int n = 0;
1995 int first = 1;
1996
1997 c &= CHARMASK; /* strip command prefix */
1998 for (; c >= 0 && isdigit(c); c = x_e_getc(), first = 0)
1999 n = n * 10 + (c - '0');
2000 if (c < 0 || first) {
2001 x_e_putc(BEL);
2002 x_arg = 1;
2003 x_arg_defaulted = 1;
2004 } else {
2005 x_e_ungetc(c);
2006 x_arg = n;
2007 x_arg_defaulted = 0;
2008 }
2009 return KSTD;
2010 }
2011
2012
2013 /* Comment or uncomment the current line. */
2014 static int
2015 x_comment(c)
2016 int c;
2017 {
2018 int oldsize = x_size_str(xbuf);
2019 int len = xep - xbuf;
2020 int ret = x_do_comment(xbuf, xend - xbuf, &len);
2021
2022 if (ret < 0)
2023 x_e_putc(BEL);
2024 else {
2025 xep = xbuf + len;
2026 *xep = '\0';
2027 xcp = xbp = xbuf;
2028 x_redraw(oldsize);
2029 if (ret > 0)
2030 return x_newline('\n');
2031 }
2032 return KSTD;
2033 }
2034
2035
2036 /* NAME:
2037 * x_prev_histword - recover word from prev command
2038 *
2039 * DESCRIPTION:
2040 * This function recovers the last word from the previous
2041 * command and inserts it into the current edit line. If a
2042 * numeric arg is supplied then the n'th word from the
2043 * start of the previous command is used.
2044 *
2045 * Bound to M-.
2046 *
2047 * RETURN VALUE:
2048 * KSTD
2049 */
2050
2051 static int
2052 x_prev_histword(c)
2053 int c;
2054 {
2055 register char *rcp;
2056 char *cp;
2057
2058 cp = *histptr;
2059 if (!cp)
2060 x_e_putc(BEL);
2061 else if (x_arg_defaulted) {
2062 rcp = &cp[strlen(cp) - 1];
2063 /*
2064 * ignore white-space after the last word
2065 */
2066 while (rcp > cp && is_cfs(*rcp))
2067 rcp--;
2068 while (rcp > cp && !is_cfs(*rcp))
2069 rcp--;
2070 if (is_cfs(*rcp))
2071 rcp++;
2072 x_ins(rcp);
2073 } else {
2074 int c;
2075
2076 rcp = cp;
2077 /*
2078 * ignore white-space at start of line
2079 */
2080 while (*rcp && is_cfs(*rcp))
2081 rcp++;
2082 while (x_arg-- > 1)
2083 {
2084 while (*rcp && !is_cfs(*rcp))
2085 rcp++;
2086 while (*rcp && is_cfs(*rcp))
2087 rcp++;
2088 }
2089 cp = rcp;
2090 while (*rcp && !is_cfs(*rcp))
2091 rcp++;
2092 c = *rcp;
2093 *rcp = '\0';
2094 x_ins(cp);
2095 *rcp = c;
2096 }
2097 return KSTD;
2098 }
2099
2100 /* Uppercase N(1) words */
2101 static int
2102 x_fold_upper(c)
2103 int c;
2104 {
2105 return x_fold_case('U');
2106 }
2107
2108 /* Lowercase N(1) words */
2109 static int
2110 x_fold_lower(c)
2111 int c;
2112 {
2113 return x_fold_case('L');
2114 }
2115
2116 /* Lowercase N(1) words */
2117 static int
2118 x_fold_capitalize(c)
2119 int c;
2120 {
2121 return x_fold_case('C');
2122 }
2123
2124 /* NAME:
2125 * x_fold_case - convert word to UPPER/lower/Capital case
2126 *
2127 * DESCRIPTION:
2128 * This function is used to implement M-U,M-u,M-L,M-l,M-C and M-c
2129 * to UPPER case, lower case or Capitalize words.
2130 *
2131 * RETURN VALUE:
2132 * None
2133 */
2134
2135 static int
2136 x_fold_case(c)
2137 int c;
2138 {
2139 char *cp = xcp;
2140
2141 if (cp == xep) {
2142 x_e_putc(BEL);
2143 return KSTD;
2144 }
2145 while (x_arg--) {
2146 /*
2147 * first skip over any white-space
2148 */
2149 while (cp != xep && is_mfs(*cp))
2150 cp++;
2151 /*
2152 * do the first char on its own since it may be
2153 * a different action than for the rest.
2154 */
2155 if (cp != xep) {
2156 if (c == 'L') { /* lowercase */
2157 if (isupper((unsigned char)*cp))
2158 *cp = tolower(*cp);
2159 } else { /* uppercase, capitialize */
2160 if (islower((unsigned char)*cp))
2161 *cp = toupper(*cp);
2162 }
2163 cp++;
2164 }
2165 /*
2166 * now for the rest of the word
2167 */
2168 while (cp != xep && !is_mfs((unsigned char)*cp)) {
2169 if (c == 'U') { /* uppercase */
2170 if (islower((unsigned char)*cp))
2171 *cp = toupper(*cp);
2172 } else { /* lowercase, capitialize */
2173 if (isupper((unsigned char)*cp))
2174 *cp = tolower(*cp);
2175 }
2176 cp++;
2177 }
2178 }
2179 x_goto(cp);
2180 return KSTD;
2181 }
2182
2183 /* NAME:
2184 * x_lastcp - last visible char
2185 *
2186 * SYNOPSIS:
2187 * x_lastcp()
2188 *
2189 * DESCRIPTION:
2190 * This function returns a pointer to that char in the
2191 * edit buffer that will be the last displayed on the
2192 * screen. The sequence:
2193 *
2194 * for (cp = x_lastcp(); cp > xcp; cp)
2195 * x_bs(*--cp);
2196 *
2197 * Will position the cursor correctly on the screen.
2198 *
2199 * RETURN VALUE:
2200 * cp or NULL
2201 */
2202
2203 static char *
2204 x_lastcp()
2205 {
2206 register char *rcp;
2207 register int i;
2208
2209 if (!xlp_valid)
2210 {
2211 for (i = 0, rcp = xbp; rcp < xep && i < x_displen; rcp++)
2212 i += x_size(*rcp);
2213 xlp = rcp;
2214 }
2215 xlp_valid = TRUE;
2216 return (xlp);
2217 }
2218
2219 #endif /* EDIT */
2220