1 /* $NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Christos Zoulas of Cornell University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #if !defined(lint) && !defined(SCCSID)
42 static char sccsid[] = "@(#)term.c 8.2 (Berkeley) 4/30/95";
44 __RCSID("$NetBSD: term.c,v 1.35 2002/03/18 16:00:59 christos Exp $");
46 #endif /* not lint && not SCCSID */
49 * term.c: Editor/termcap-curses interface
50 * We have to declare a static variable here, since the
51 * termcap putchar routine does not take an argument!
67 /* Solaris's term.h does horrid things. */
68 #if (defined(HAVE_TERM_H) && !defined(SUNOS))
71 #include <sys/types.h>
72 #include <sys/ioctl.h>
77 * IMPORTANT NOTE: these routines are allowed to look at the current screen
78 * and the current possition assuming that it is correct. If this is not
79 * true, then the update will be WRONG! This is (should be) a valid
83 #define TC_BUFSIZE 2048
85 #define GoodStr(a) (el->el_term.t_str[a] != NULL && \
86 el->el_term.t_str[a][0] != '\0')
87 #define Str(a) el->el_term.t_str[a]
88 #define Val(a) el->el_term.t_val[a]
91 private const struct {
162 private const struct termcapstr {
164 const char *long_name;
167 { "al", "add new blank line" },
169 { "bl", "audible bell" },
171 { "cd", "clear to bottom" },
173 { "ce", "clear to end of line" },
175 { "ch", "cursor to horiz pos" },
177 { "cl", "clear screen" },
179 { "dc", "delete a character" },
181 { "dl", "delete a line" },
183 { "dm", "start delete mode" },
185 { "ed", "end delete mode" },
187 { "ei", "end insert mode" },
189 { "fs", "cursor from status line" },
191 { "ho", "home cursor" },
193 { "ic", "insert character" },
195 { "im", "start insert mode" },
197 { "ip", "insert padding" },
199 { "kd", "sends cursor down" },
201 { "kl", "sends cursor left" },
203 { "kr", "sends cursor right" },
205 { "ku", "sends cursor up" },
207 { "md", "begin bold" },
209 { "me", "end attributes" },
211 { "nd", "non destructive space" },
213 { "se", "end standout" },
215 { "so", "begin standout" },
217 { "ts", "cursor to status line" },
219 { "up", "cursor up one" },
221 { "us", "begin underline" },
223 { "ue", "end underline" },
225 { "vb", "visible bell" },
227 { "DC", "delete multiple chars" },
229 { "DO", "cursor down multiple" },
231 { "IC", "insert multiple chars" },
233 { "LE", "cursor left multiple" },
235 { "RI", "cursor right multiple" },
237 { "UP", "cursor up multiple" },
239 { "kh", "send cursor home" },
241 { "@7", "send cursor end" },
246 private const struct termcapval {
248 const char *long_name;
251 { "am", "has automatic margins" },
253 { "pt", "has physical tabs" },
255 { "li", "Number of lines" },
257 { "co", "Number of columns" },
259 { "km", "Has meta key" },
261 { "xt", "Tab chars destructive" },
263 { "xn", "newline ignored at right margin" },
265 { "MT", "Has meta key" }, /* XXX? */
269 /* do two or more of the attributes use me */
271 private void term_setflags(EditLine *);
272 private int term_rebuffer_display(EditLine *);
273 private void term_free_display(EditLine *);
274 private int term_alloc_display(EditLine *);
275 private void term_alloc(EditLine *, const struct termcapstr *, const char *);
276 private void term_init_arrow(EditLine *);
277 private void term_reset_arrow(EditLine *);
280 private FILE *term_outfile = NULL; /* XXX: How do we fix that? */
284 * Set the terminal capability flags
287 term_setflags(EditLine *el)
290 if (el->el_tty.t_tabs)
291 EL_FLAGS |= (Val(T_pt) && !Val(T_xt)) ? TERM_CAN_TAB : 0;
293 EL_FLAGS |= (Val(T_km) || Val(T_MT)) ? TERM_HAS_META : 0;
294 EL_FLAGS |= GoodStr(T_ce) ? TERM_CAN_CEOL : 0;
295 EL_FLAGS |= (GoodStr(T_dc) || GoodStr(T_DC)) ? TERM_CAN_DELETE : 0;
296 EL_FLAGS |= (GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC)) ?
298 EL_FLAGS |= (GoodStr(T_up) || GoodStr(T_UP)) ? TERM_CAN_UP : 0;
299 EL_FLAGS |= Val(T_am) ? TERM_HAS_AUTO_MARGINS : 0;
300 EL_FLAGS |= Val(T_xn) ? TERM_HAS_MAGIC_MARGINS : 0;
302 if (GoodStr(T_me) && GoodStr(T_ue))
303 EL_FLAGS |= (strcmp(Str(T_me), Str(T_ue)) == 0) ?
306 EL_FLAGS &= ~TERM_CAN_ME;
307 if (GoodStr(T_me) && GoodStr(T_se))
308 EL_FLAGS |= (strcmp(Str(T_me), Str(T_se)) == 0) ?
314 (void) fprintf(el->el_errfile,
315 "WARNING: Your terminal cannot move up.\n");
316 (void) fprintf(el->el_errfile,
317 "Editing may be odd for long lines.\n");
320 (void) fprintf(el->el_errfile, "no clear EOL capability.\n");
322 (void) fprintf(el->el_errfile, "no delete char capability.\n");
324 (void) fprintf(el->el_errfile, "no insert char capability.\n");
325 #endif /* DEBUG_SCREEN */
330 * Initialize the terminal stuff
333 term_init(EditLine *el)
336 el->el_term.t_buf = (char *) el_malloc(TC_BUFSIZE);
337 if (el->el_term.t_buf == NULL)
339 el->el_term.t_cap = (char *) el_malloc(TC_BUFSIZE);
340 if (el->el_term.t_cap == NULL)
342 el->el_term.t_fkey = (fkey_t *) el_malloc(A_K_NKEYS * sizeof(fkey_t));
343 if (el->el_term.t_fkey == NULL)
345 el->el_term.t_loc = 0;
346 el->el_term.t_str = (char **) el_malloc(T_str * sizeof(char *));
347 if (el->el_term.t_str == NULL)
349 (void) memset(el->el_term.t_str, 0, T_str * sizeof(char *));
350 el->el_term.t_val = (int *) el_malloc(T_val * sizeof(int));
351 if (el->el_term.t_val == NULL)
353 (void) memset(el->el_term.t_val, 0, T_val * sizeof(int));
354 term_outfile = el->el_outfile;
355 (void) term_set(el, NULL);
360 * Clean up the terminal stuff
363 term_end(EditLine *el)
366 el_free((ptr_t) el->el_term.t_buf);
367 el->el_term.t_buf = NULL;
368 el_free((ptr_t) el->el_term.t_cap);
369 el->el_term.t_cap = NULL;
370 el->el_term.t_loc = 0;
371 el_free((ptr_t) el->el_term.t_str);
372 el->el_term.t_str = NULL;
373 el_free((ptr_t) el->el_term.t_val);
374 el->el_term.t_val = NULL;
375 term_free_display(el);
380 * Maintain a string pool for termcap strings
383 term_alloc(EditLine *el, const struct termcapstr *t, const char *cap)
385 char termbuf[TC_BUFSIZE];
387 char **tlist = el->el_term.t_str;
388 char **tmp, **str = &tlist[t - tstr];
390 if (cap == NULL || *cap == '\0') {
396 tlen = *str == NULL ? 0 : strlen(*str);
399 * New string is shorter; no need to allocate space
402 (void) strcpy(*str, cap); /* XXX strcpy is safe */
406 * New string is longer; see if we have enough space to append
408 if (el->el_term.t_loc + 3 < TC_BUFSIZE) {
409 /* XXX strcpy is safe */
410 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc],
412 el->el_term.t_loc += clen + 1; /* one for \0 */
416 * Compact our buffer; no need to check compaction, cause we know it
420 for (tmp = tlist; tmp < &tlist[T_str]; tmp++)
421 if (*tmp != NULL && *tmp != '\0' && *tmp != *str) {
424 for (ptr = *tmp; *ptr != '\0'; termbuf[tlen++] = *ptr++)
426 termbuf[tlen++] = '\0';
428 memcpy(el->el_term.t_buf, termbuf, TC_BUFSIZE);
429 el->el_term.t_loc = tlen;
430 if (el->el_term.t_loc + 3 >= TC_BUFSIZE) {
431 (void) fprintf(el->el_errfile,
432 "Out of termcap string space.\n");
435 /* XXX strcpy is safe */
436 (void) strcpy(*str = &el->el_term.t_buf[el->el_term.t_loc], cap);
437 el->el_term.t_loc += clen + 1; /* one for \0 */
442 /* term_rebuffer_display():
443 * Rebuffer the display after the screen changed size
446 term_rebuffer_display(EditLine *el)
448 coord_t *c = &el->el_term.t_size;
450 term_free_display(el);
455 if (term_alloc_display(el) == -1)
461 /* term_alloc_display():
462 * Allocate a new display.
465 term_alloc_display(EditLine *el)
469 coord_t *c = &el->el_term.t_size;
471 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
474 for (i = 0; i < c->v; i++) {
475 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
482 b = (char **) el_malloc((size_t) (sizeof(char *) * (c->v + 1)));
485 for (i = 0; i < c->v; i++) {
486 b[i] = (char *) el_malloc((size_t) (sizeof(char) * (c->h + 1)));
496 /* term_free_display():
497 * Free the display buffers
500 term_free_display(EditLine *el)
506 el->el_display = NULL;
508 for (bufp = b; *bufp != NULL; bufp++)
509 el_free((ptr_t) * bufp);
513 el->el_vdisplay = NULL;
515 for (bufp = b; *bufp != NULL; bufp++)
516 el_free((ptr_t) * bufp);
522 /* term_move_to_line():
523 * move to line <where> (first line == 0)
524 * as efficiently as possible
527 term_move_to_line(EditLine *el, int where)
531 if (where == el->el_cursor.v)
534 if (where > el->el_term.t_size.v) {
536 (void) fprintf(el->el_errfile,
537 "term_move_to_line: where is ridiculous: %d\r\n", where);
538 #endif /* DEBUG_SCREEN */
541 if ((del = where - el->el_cursor.v) > 0) {
543 if (EL_HAS_AUTO_MARGINS &&
544 el->el_display[el->el_cursor.v][0] != '\0') {
545 /* move without newline */
546 term_move_to_char(el, el->el_term.t_size.h - 1);
548 &el->el_display[el->el_cursor.v][el->el_cursor.h],
553 if ((del > 1) && GoodStr(T_DO)) {
554 (void) tputs(tgoto(Str(T_DO), del, del),
558 for (; del > 0; del--)
560 /* because the \n will become \r\n */
565 } else { /* del < 0 */
566 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
567 (void) tputs(tgoto(Str(T_UP), -del, -del), -del,
571 for (; del < 0; del++)
572 (void) tputs(Str(T_up), 1, term__putc);
575 el->el_cursor.v = where;/* now where is here */
579 /* term_move_to_char():
580 * Move to the character position specified
583 term_move_to_char(EditLine *el, int where)
588 if (where == el->el_cursor.h)
591 if (where > el->el_term.t_size.h) {
593 (void) fprintf(el->el_errfile,
594 "term_move_to_char: where is riduculous: %d\r\n", where);
595 #endif /* DEBUG_SCREEN */
598 if (!where) { /* if where is first column */
599 term__putc('\r'); /* do a CR */
603 del = where - el->el_cursor.h;
605 if ((del < -4 || del > 4) && GoodStr(T_ch))
606 /* go there directly */
607 (void) tputs(tgoto(Str(T_ch), where, where), where, term__putc);
609 if (del > 0) { /* moving forward */
610 if ((del > 4) && GoodStr(T_RI))
611 (void) tputs(tgoto(Str(T_RI), del, del),
614 /* if I can do tabs, use them */
616 if ((el->el_cursor.h & 0370) !=
618 /* if not within tab stop */
620 (el->el_cursor.h & 0370);
625 el->el_cursor.h = where & 0370;
629 * it's usually cheaper to just write the
633 * NOTE THAT term_overwrite() WILL CHANGE
637 &el->el_display[el->el_cursor.v][el->el_cursor.h],
638 where - el->el_cursor.h);
641 } else { /* del < 0 := moving backward */
642 if ((-del > 4) && GoodStr(T_LE))
643 (void) tputs(tgoto(Str(T_LE), -del, -del),
645 else { /* can't go directly there */
647 * if the "cost" is greater than the "cost"
651 (-del > (((unsigned int) where >> 3) +
654 term__putc('\r'); /* do a CR */
656 goto mc_again; /* and try again */
658 for (i = 0; i < -del; i++)
663 el->el_cursor.h = where; /* now where is here */
668 * Overstrike num characters
671 term_overwrite(EditLine *el, const char *cp, int n)
674 return; /* catch bugs */
676 if (n > el->el_term.t_size.h) {
678 (void) fprintf(el->el_errfile,
679 "term_overwrite: n is riduculous: %d\r\n", n);
680 #endif /* DEBUG_SCREEN */
688 if (el->el_cursor.h >= el->el_term.t_size.h) { /* wrap? */
689 if (EL_HAS_AUTO_MARGINS) { /* yes */
692 if (EL_HAS_MAGIC_MARGINS) {
693 /* force the wrap to avoid the "magic"
696 if ((c = el->el_display[el->el_cursor.v][el->el_cursor.h])
698 term_overwrite(el, &c, 1);
703 } else /* no wrap, but cursor stays on screen */
704 el->el_cursor.h = el->el_term.t_size.h;
709 /* term_deletechars():
710 * Delete num characters
713 term_deletechars(EditLine *el, int num)
718 if (!EL_CAN_DELETE) {
720 (void) fprintf(el->el_errfile, " ERROR: cannot delete \n");
721 #endif /* DEBUG_EDIT */
724 if (num > el->el_term.t_size.h) {
726 (void) fprintf(el->el_errfile,
727 "term_deletechars: num is riduculous: %d\r\n", num);
728 #endif /* DEBUG_SCREEN */
731 if (GoodStr(T_DC)) /* if I have multiple delete */
732 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more
734 (void) tputs(tgoto(Str(T_DC), num, num),
738 if (GoodStr(T_dm)) /* if I have delete mode */
739 (void) tputs(Str(T_dm), 1, term__putc);
741 if (GoodStr(T_dc)) /* else do one at a time */
743 (void) tputs(Str(T_dc), 1, term__putc);
745 if (GoodStr(T_ed)) /* if I have delete mode */
746 (void) tputs(Str(T_ed), 1, term__putc);
750 /* term_insertwrite():
751 * Puts terminal in insert character mode or inserts num
752 * characters in the line
755 term_insertwrite(EditLine *el, char *cp, int num)
759 if (!EL_CAN_INSERT) {
761 (void) fprintf(el->el_errfile, " ERROR: cannot insert \n");
762 #endif /* DEBUG_EDIT */
765 if (num > el->el_term.t_size.h) {
767 (void) fprintf(el->el_errfile,
768 "StartInsert: num is riduculous: %d\r\n", num);
769 #endif /* DEBUG_SCREEN */
772 if (GoodStr(T_IC)) /* if I have multiple insert */
773 if ((num > 1) || !GoodStr(T_ic)) {
774 /* if ic would be more expensive */
775 (void) tputs(tgoto(Str(T_IC), num, num),
777 term_overwrite(el, cp, num);
778 /* this updates el_cursor.h */
781 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
782 (void) tputs(Str(T_im), 1, term__putc);
784 el->el_cursor.h += num;
789 if (GoodStr(T_ip)) /* have to make num chars insert */
790 (void) tputs(Str(T_ip), 1, term__putc);
792 (void) tputs(Str(T_ei), 1, term__putc);
796 if (GoodStr(T_ic)) /* have to make num chars insert */
797 (void) tputs(Str(T_ic), 1, term__putc);
804 if (GoodStr(T_ip)) /* have to make num chars insert */
805 (void) tputs(Str(T_ip), 1, term__putc);
806 /* pad the inserted char */
813 * clear to end of line. There are num characters to clear
816 term_clear_EOL(EditLine *el, int num)
820 if (EL_CAN_CEOL && GoodStr(T_ce))
821 (void) tputs(Str(T_ce), 1, term__putc);
823 for (i = 0; i < num; i++)
825 el->el_cursor.h += num; /* have written num spaces */
830 /* term_clear_screen():
834 term_clear_screen(EditLine *el)
835 { /* clear the whole screen and home */
838 /* send the clear screen code */
839 (void) tputs(Str(T_cl), Val(T_li), term__putc);
840 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
841 (void) tputs(Str(T_ho), Val(T_li), term__putc); /* home */
842 /* clear to bottom of screen */
843 (void) tputs(Str(T_cd), Val(T_li), term__putc);
852 * Beep the way the terminal wants us
855 term_beep(EditLine *el)
858 /* what termcap says we should use */
859 (void) tputs(Str(T_bl), 1, term__putc);
861 term__putc('\007'); /* an ASCII bell; ^G */
866 /* term_clear_to_bottom():
867 * Clear to the bottom of the screen
870 term_clear_to_bottom(EditLine *el)
873 (void) tputs(Str(T_cd), Val(T_li), term__putc);
874 else if (GoodStr(T_ce))
875 (void) tputs(Str(T_ce), Val(T_li), term__putc);
881 * Read in the terminal capabilities from the requested terminal
884 term_set(EditLine *el, const char *term)
887 char buf[TC_BUFSIZE];
889 const struct termcapstr *t;
893 (void) sigemptyset(&nset);
894 (void) sigaddset(&nset, SIGWINCH);
895 (void) sigprocmask(SIG_BLOCK, &nset, &oset);
901 term = getenv("TERM");
903 if (!term || !term[0])
906 if (strcmp(term, "emacs") == 0)
907 el->el_flags |= EDIT_DISABLED;
909 memset(el->el_term.t_cap, 0, TC_BUFSIZE);
911 i = tgetent(el->el_term.t_cap, term);
915 (void) fprintf(el->el_errfile,
916 "Cannot read termcap database;\n");
918 (void) fprintf(el->el_errfile,
919 "No entry for terminal type \"%s\";\n", term);
920 (void) fprintf(el->el_errfile,
921 "using dumb terminal settings.\n");
922 Val(T_co) = 80; /* do a dumb terminal */
923 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
924 Val(T_xt) = Val(T_MT);
925 for (t = tstr; t->name != NULL; t++)
926 term_alloc(el, t, NULL);
928 /* auto/magic margins */
929 Val(T_am) = tgetflag("am");
930 Val(T_xn) = tgetflag("xn");
932 Val(T_pt) = tgetflag("pt");
933 Val(T_xt) = tgetflag("xt");
934 /* do we have a meta? */
935 Val(T_km) = tgetflag("km");
936 Val(T_MT) = tgetflag("MT");
938 Val(T_co) = tgetnum("co");
939 Val(T_li) = tgetnum("li");
940 for (t = tstr; t->name != NULL; t++)
941 term_alloc(el, t, tgetstr((char *)t->name, &area));
945 Val(T_co) = 80; /* just in case */
949 el->el_term.t_size.v = Val(T_co);
950 el->el_term.t_size.h = Val(T_li);
954 /* get the correct window size */
955 (void) term_get_size(el, &lins, &cols);
956 if (term_change_size(el, lins, cols) == -1)
958 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
960 return (i <= 0 ? -1 : 0);
965 * Return the new window size in lines and cols, and
966 * true if the size was changed.
969 term_get_size(EditLine *el, int *lins, int *cols)
978 if (ioctl(el->el_infd, TIOCGWINSZ, (ioctl_t) & ws) != -1) {
989 if (ioctl(el->el_infd, TIOCGSIZE, (ioctl_t) & ts) != -1) {
997 return (Val(T_co) != *cols || Val(T_li) != *lins);
1001 /* term_change_size():
1002 * Change the size of the terminal
1005 term_change_size(EditLine *el, int lins, int cols)
1010 Val(T_co) = (cols < 2) ? 80 : cols;
1011 Val(T_li) = (lins < 1) ? 24 : lins;
1013 /* re-make display buffers */
1014 if (term_rebuffer_display(el) == -1)
1016 re_clear_display(el);
1021 /* term_init_arrow():
1022 * Initialize the arrow key bindings from termcap
1025 term_init_arrow(EditLine *el)
1027 fkey_t *arrow = el->el_term.t_fkey;
1029 arrow[A_K_DN].name = "down";
1030 arrow[A_K_DN].key = T_kd;
1031 arrow[A_K_DN].fun.cmd = ED_NEXT_HISTORY;
1032 arrow[A_K_DN].type = XK_CMD;
1034 arrow[A_K_UP].name = "up";
1035 arrow[A_K_UP].key = T_ku;
1036 arrow[A_K_UP].fun.cmd = ED_PREV_HISTORY;
1037 arrow[A_K_UP].type = XK_CMD;
1039 arrow[A_K_LT].name = "left";
1040 arrow[A_K_LT].key = T_kl;
1041 arrow[A_K_LT].fun.cmd = ED_PREV_CHAR;
1042 arrow[A_K_LT].type = XK_CMD;
1044 arrow[A_K_RT].name = "right";
1045 arrow[A_K_RT].key = T_kr;
1046 arrow[A_K_RT].fun.cmd = ED_NEXT_CHAR;
1047 arrow[A_K_RT].type = XK_CMD;
1049 arrow[A_K_HO].name = "home";
1050 arrow[A_K_HO].key = T_kh;
1051 arrow[A_K_HO].fun.cmd = ED_MOVE_TO_BEG;
1052 arrow[A_K_HO].type = XK_CMD;
1054 arrow[A_K_EN].name = "end";
1055 arrow[A_K_EN].key = T_at7;
1056 arrow[A_K_EN].fun.cmd = ED_MOVE_TO_END;
1057 arrow[A_K_EN].type = XK_CMD;
1061 /* term_reset_arrow():
1062 * Reset arrow key bindings
1065 term_reset_arrow(EditLine *el)
1067 fkey_t *arrow = el->el_term.t_fkey;
1068 static const char strA[] = {033, '[', 'A', '\0'};
1069 static const char strB[] = {033, '[', 'B', '\0'};
1070 static const char strC[] = {033, '[', 'C', '\0'};
1071 static const char strD[] = {033, '[', 'D', '\0'};
1072 static const char strH[] = {033, '[', 'H', '\0'};
1073 static const char strF[] = {033, '[', 'F', '\0'};
1074 static const char stOA[] = {033, 'O', 'A', '\0'};
1075 static const char stOB[] = {033, 'O', 'B', '\0'};
1076 static const char stOC[] = {033, 'O', 'C', '\0'};
1077 static const char stOD[] = {033, 'O', 'D', '\0'};
1078 static const char stOH[] = {033, 'O', 'H', '\0'};
1079 static const char stOF[] = {033, 'O', 'F', '\0'};
1081 key_add(el, strA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1082 key_add(el, strB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1083 key_add(el, strC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1084 key_add(el, strD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1085 key_add(el, strH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1086 key_add(el, strF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1087 key_add(el, stOA, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1088 key_add(el, stOB, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1089 key_add(el, stOC, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1090 key_add(el, stOD, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1091 key_add(el, stOH, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1092 key_add(el, stOF, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1094 if (el->el_map.type == MAP_VI) {
1095 key_add(el, &strA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1096 key_add(el, &strB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1097 key_add(el, &strC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1098 key_add(el, &strD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1099 key_add(el, &strH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1100 key_add(el, &strF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1101 key_add(el, &stOA[1], &arrow[A_K_UP].fun, arrow[A_K_UP].type);
1102 key_add(el, &stOB[1], &arrow[A_K_DN].fun, arrow[A_K_DN].type);
1103 key_add(el, &stOC[1], &arrow[A_K_RT].fun, arrow[A_K_RT].type);
1104 key_add(el, &stOD[1], &arrow[A_K_LT].fun, arrow[A_K_LT].type);
1105 key_add(el, &stOH[1], &arrow[A_K_HO].fun, arrow[A_K_HO].type);
1106 key_add(el, &stOF[1], &arrow[A_K_EN].fun, arrow[A_K_EN].type);
1111 /* term_set_arrow():
1112 * Set an arrow key binding
1115 term_set_arrow(EditLine *el, const char *name, key_value_t *fun, int type)
1117 fkey_t *arrow = el->el_term.t_fkey;
1120 for (i = 0; i < A_K_NKEYS; i++)
1121 if (strcmp(name, arrow[i].name) == 0) {
1122 arrow[i].fun = *fun;
1123 arrow[i].type = type;
1130 /* term_clear_arrow():
1131 * Clear an arrow key binding
1134 term_clear_arrow(EditLine *el, const char *name)
1136 fkey_t *arrow = el->el_term.t_fkey;
1139 for (i = 0; i < A_K_NKEYS; i++)
1140 if (strcmp(name, arrow[i].name) == 0) {
1141 arrow[i].type = XK_NOD;
1148 /* term_print_arrow():
1149 * Print the arrow key bindings
1152 term_print_arrow(EditLine *el, const char *name)
1155 fkey_t *arrow = el->el_term.t_fkey;
1157 for (i = 0; i < A_K_NKEYS; i++)
1158 if (*name == '\0' || strcmp(name, arrow[i].name) == 0)
1159 if (arrow[i].type != XK_NOD)
1160 key_kprint(el, arrow[i].name, &arrow[i].fun,
1165 /* term_bind_arrow():
1166 * Bind the arrow keys
1169 term_bind_arrow(EditLine *el)
1172 const el_action_t *dmap;
1175 fkey_t *arrow = el->el_term.t_fkey;
1177 /* Check if the components needed are initialized */
1178 if (el->el_term.t_buf == NULL || el->el_map.key == NULL)
1181 map = el->el_map.type == MAP_VI ? el->el_map.alt : el->el_map.key;
1182 dmap = el->el_map.type == MAP_VI ? el->el_map.vic : el->el_map.emacs;
1184 term_reset_arrow(el);
1186 for (i = 0; i < A_K_NKEYS; i++) {
1187 p = el->el_term.t_str[arrow[i].key];
1189 j = (unsigned char) *p;
1191 * Assign the arrow keys only if:
1193 * 1. They are multi-character arrow keys and the user
1194 * has not re-assigned the leading character, or
1195 * has re-assigned the leading character to be
1196 * ED_SEQUENCE_LEAD_IN
1197 * 2. They are single arrow keys pointing to an
1200 if (arrow[i].type == XK_NOD)
1201 key_clear(el, map, p);
1203 if (p[1] && (dmap[j] == map[j] ||
1204 map[j] == ED_SEQUENCE_LEAD_IN)) {
1205 key_add(el, p, &arrow[i].fun,
1207 map[j] = ED_SEQUENCE_LEAD_IN;
1208 } else if (map[j] == ED_UNASSIGNED) {
1209 key_clear(el, map, p);
1210 if (arrow[i].type == XK_CMD)
1211 map[j] = arrow[i].fun.cmd;
1213 key_add(el, p, &arrow[i].fun,
1229 return (fputc(c, term_outfile));
1240 (void) fflush(term_outfile);
1245 * Print the current termcap characteristics
1249 term_telltc(EditLine *el, int argc, const char **argv)
1251 const struct termcapstr *t;
1253 char upbuf[EL_BUFSIZ];
1255 (void) fprintf(el->el_outfile, "\n\tYour terminal has the\n");
1256 (void) fprintf(el->el_outfile, "\tfollowing characteristics:\n\n");
1257 (void) fprintf(el->el_outfile, "\tIt has %d columns and %d lines\n",
1258 Val(T_co), Val(T_li));
1259 (void) fprintf(el->el_outfile,
1260 "\tIt has %s meta key\n", EL_HAS_META ? "a" : "no");
1261 (void) fprintf(el->el_outfile,
1262 "\tIt can%suse tabs\n", EL_CAN_TAB ? " " : "not ");
1263 (void) fprintf(el->el_outfile, "\tIt %s automatic margins\n",
1264 EL_HAS_AUTO_MARGINS ? "has" : "does not have");
1265 if (EL_HAS_AUTO_MARGINS)
1266 (void) fprintf(el->el_outfile, "\tIt %s magic margins\n",
1267 EL_HAS_MAGIC_MARGINS ? "has" : "does not have");
1269 for (t = tstr, ts = el->el_term.t_str; t->name != NULL; t++, ts++)
1270 (void) fprintf(el->el_outfile, "\t%25s (%s) == %s\n",
1272 t->name, *ts && **ts ?
1273 key__decode_str(*ts, upbuf, "") : "(empty)");
1274 (void) fputc('\n', el->el_outfile);
1280 * Change the current terminal characteristics
1284 term_settc(EditLine *el, int argc, const char **argv)
1286 const struct termcapstr *ts;
1287 const struct termcapval *tv;
1288 const char *what, *how;
1290 if (argv == NULL || argv[1] == NULL || argv[2] == NULL)
1297 * Do the strings first
1299 for (ts = tstr; ts->name != NULL; ts++)
1300 if (strcmp(ts->name, what) == 0)
1303 if (ts->name != NULL) {
1304 term_alloc(el, ts, how);
1309 * Do the numeric ones second
1311 for (tv = tval; tv->name != NULL; tv++)
1312 if (strcmp(tv->name, what) == 0)
1315 if (tv->name != NULL) {
1316 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
1317 tv == &tval[T_am] || tv == &tval[T_xn]) {
1318 if (strcmp(how, "yes") == 0)
1319 el->el_term.t_val[tv - tval] = 1;
1320 else if (strcmp(how, "no") == 0)
1321 el->el_term.t_val[tv - tval] = 0;
1323 (void) fprintf(el->el_errfile,
1324 "settc: Bad value `%s'.\n", how);
1328 if (term_change_size(el, Val(T_li), Val(T_co)) == -1)
1335 i = strtol(how, &ep, 10);
1337 (void) fprintf(el->el_errfile,
1338 "settc: Bad value `%s'.\n", how);
1341 el->el_term.t_val[tv - tval] = (int) i;
1342 el->el_term.t_size.v = Val(T_co);
1343 el->el_term.t_size.h = Val(T_li);
1344 if (tv == &tval[T_co] || tv == &tval[T_li])
1345 if (term_change_size(el, Val(T_li), Val(T_co))
1356 * Print the termcap string out with variable substitution
1360 term_echotc(EditLine *el, int argc, const char **argv)
1362 char *cap, *scap, *ep;
1363 int arg_need, arg_cols, arg_rows;
1364 int verbose = 0, silent = 0;
1366 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
1367 const struct termcapstr *t;
1368 char buf[TC_BUFSIZE];
1373 if (argv == NULL || argv[1] == NULL)
1377 if (argv[0][0] == '-') {
1378 switch (argv[0][1]) {
1386 /* stderror(ERR_NAME | ERR_TCUSAGE); */
1391 if (!*argv || *argv[0] == '\0')
1393 if (strcmp(*argv, "tabs") == 0) {
1394 (void) fprintf(el->el_outfile, fmts, EL_CAN_TAB ? "yes" : "no");
1396 } else if (strcmp(*argv, "meta") == 0) {
1397 (void) fprintf(el->el_outfile, fmts, Val(T_km) ? "yes" : "no");
1399 } else if (strcmp(*argv, "xn") == 0) {
1400 (void) fprintf(el->el_outfile, fmts, EL_HAS_MAGIC_MARGINS ?
1403 } else if (strcmp(*argv, "am") == 0) {
1404 (void) fprintf(el->el_outfile, fmts, EL_HAS_AUTO_MARGINS ?
1407 } else if (strcmp(*argv, "baud") == 0) {
1411 for (i = 0; baud_rate[i].b_name != NULL; i++)
1412 if (el->el_tty.t_speed == baud_rate[i].b_rate) {
1413 (void) fprintf(el->el_outfile, fmts,
1414 baud_rate[i].b_name);
1417 (void) fprintf(el->el_outfile, fmtd, 0);
1419 (void) fprintf(el->el_outfile, fmtd, el->el_tty.t_speed);
1422 } else if (strcmp(*argv, "rows") == 0 || strcmp(*argv, "lines") == 0) {
1423 (void) fprintf(el->el_outfile, fmtd, Val(T_li));
1425 } else if (strcmp(*argv, "cols") == 0) {
1426 (void) fprintf(el->el_outfile, fmtd, Val(T_co));
1430 * Try to use our local definition first
1433 for (t = tstr; t->name != NULL; t++)
1434 if (strcmp(t->name, *argv) == 0) {
1435 scap = el->el_term.t_str[t - tstr];
1438 if (t->name == NULL)
1439 scap = tgetstr((char *)argv, &area);
1440 if (!scap || scap[0] == '\0') {
1442 (void) fprintf(el->el_errfile,
1443 "echotc: Termcap parameter `%s' not found.\n",
1448 * Count home many values we need for this capability.
1450 for (cap = scap, arg_need = 0; *cap; cap++)
1470 * hpux has lot's of them...
1473 (void) fprintf(el->el_errfile,
1474 "echotc: Warning: unknown termcap %% `%c'.\n",
1476 /* This is bad, but I won't complain */
1483 if (*argv && *argv[0]) {
1485 (void) fprintf(el->el_errfile,
1486 "echotc: Warning: Extra argument `%s'.\n",
1490 (void) tputs(scap, 1, term__putc);
1494 if (!*argv || *argv[0] == '\0') {
1496 (void) fprintf(el->el_errfile,
1497 "echotc: Warning: Missing argument.\n");
1501 i = strtol(*argv, &ep, 10);
1502 if (*ep != '\0' || i < 0) {
1504 (void) fprintf(el->el_errfile,
1505 "echotc: Bad value `%s' for rows.\n",
1511 if (*argv && *argv[0]) {
1513 (void) fprintf(el->el_errfile,
1514 "echotc: Warning: Extra argument `%s'.\n",
1518 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, term__putc);
1521 /* This is wrong, but I will ignore it... */
1523 (void) fprintf(el->el_errfile,
1524 "echotc: Warning: Too many required arguments (%d).\n",
1529 if (!*argv || *argv[0] == '\0') {
1531 (void) fprintf(el->el_errfile,
1532 "echotc: Warning: Missing argument.\n");
1535 i = strtol(*argv, &ep, 10);
1536 if (*ep != '\0' || i < 0) {
1538 (void) fprintf(el->el_errfile,
1539 "echotc: Bad value `%s' for cols.\n",
1545 if (!*argv || *argv[0] == '\0') {
1547 (void) fprintf(el->el_errfile,
1548 "echotc: Warning: Missing argument.\n");
1551 i = strtol(*argv, &ep, 10);
1552 if (*ep != '\0' || i < 0) {
1554 (void) fprintf(el->el_errfile,
1555 "echotc: Bad value `%s' for rows.\n",
1562 (void) fprintf(el->el_errfile,
1563 "echotc: Bad value `%s'.\n", *argv);
1567 if (*argv && *argv[0]) {
1569 (void) fprintf(el->el_errfile,
1570 "echotc: Warning: Extra argument `%s'.\n",
1574 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows,