1 /* $Header: /src/pub/tcsh/ed.screen.c,v 3.63 2005/01/18 20:43:30 christos Exp $ */
3 * ed.screen.c: Editor/termcap-curses interface
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$Id: ed.screen.c,v 3.63 2005/01/18 20:43:30 christos Exp $")
41 /* #define DEBUG_LITERAL */
44 * IMPORTANT NOTE: these routines are allowed to look at the current screen
45 * and the current possition assuming that it is correct. If this is not
46 * true, then the update will be WRONG! This is (should be) a valid
50 #define TC_BUFSIZE 2048
52 #define GoodStr(a) (tstr[a].str != NULL && tstr[a].str[0] != '\0')
53 #define Str(a) tstr[a].str
54 #define Val(a) tval[a].val
166 static struct termcapstr {
168 const char *long_name;
180 static struct termcapval {
182 const char *long_name;
192 for (i = 0; i < T_str + 1; i++)
193 xfree((ptr_t) tstr[i].long_name);
195 for (i = 0; i < T_val + 1; i++)
196 xfree((ptr_t) tval[i].long_name);
199 tstr[T_al].name = "al";
200 tstr[T_al].long_name = CSAVS(4, 1, "add new blank line");
202 tstr[T_bl].name = "bl";
203 tstr[T_bl].long_name = CSAVS(4, 2, "audible bell");
205 tstr[T_cd].name = "cd";
206 tstr[T_cd].long_name = CSAVS(4, 3, "clear to bottom");
208 tstr[T_ce].name = "ce";
209 tstr[T_ce].long_name = CSAVS(4, 4, "clear to end of line");
211 tstr[T_ch].name = "ch";
212 tstr[T_ch].long_name = CSAVS(4, 5, "cursor to horiz pos");
214 tstr[T_cl].name = "cl";
215 tstr[T_cl].long_name = CSAVS(4, 6, "clear screen");
217 tstr[T_dc].name = "dc";
218 tstr[T_dc].long_name = CSAVS(4, 7, "delete a character");
220 tstr[T_dl].name = "dl";
221 tstr[T_dl].long_name = CSAVS(4, 8, "delete a line");
223 tstr[T_dm].name = "dm";
224 tstr[T_dm].long_name = CSAVS(4, 9, "start delete mode");
226 tstr[T_ed].name = "ed";
227 tstr[T_ed].long_name = CSAVS(4, 10, "end delete mode");
229 tstr[T_ei].name = "ei";
230 tstr[T_ei].long_name = CSAVS(4, 11, "end insert mode");
232 tstr[T_fs].name = "fs";
233 tstr[T_fs].long_name = CSAVS(4, 12, "cursor from status line");
235 tstr[T_ho].name = "ho";
236 tstr[T_ho].long_name = CSAVS(4, 13, "home cursor");
238 tstr[T_ic].name = "ic";
239 tstr[T_ic].long_name = CSAVS(4, 14, "insert character");
241 tstr[T_im].name = "im";
242 tstr[T_im].long_name = CSAVS(4, 15, "start insert mode");
244 tstr[T_ip].name = "ip";
245 tstr[T_ip].long_name = CSAVS(4, 16, "insert padding");
247 tstr[T_kd].name = "kd";
248 tstr[T_kd].long_name = CSAVS(4, 17, "sends cursor down");
250 tstr[T_kl].name = "kl";
251 tstr[T_kl].long_name = CSAVS(4, 18, "sends cursor left");
253 tstr[T_kr].name = "kr";
254 tstr[T_kr].long_name = CSAVS(4, 19, "sends cursor right");
256 tstr[T_ku].name = "ku";
257 tstr[T_ku].long_name = CSAVS(4, 20, "sends cursor up");
259 tstr[T_md].name = "md";
260 tstr[T_md].long_name = CSAVS(4, 21, "begin bold");
262 tstr[T_me].name = "me";
263 tstr[T_me].long_name = CSAVS(4, 22, "end attributes");
265 tstr[T_nd].name = "nd";
266 tstr[T_nd].long_name = CSAVS(4, 23, "non destructive space");
268 tstr[T_se].name = "se";
269 tstr[T_se].long_name = CSAVS(4, 24, "end standout");
271 tstr[T_so].name = "so";
272 tstr[T_so].long_name = CSAVS(4, 25, "begin standout");
274 tstr[T_ts].name = "ts";
275 tstr[T_ts].long_name = CSAVS(4, 26, "cursor to status line");
277 tstr[T_up].name = "up";
278 tstr[T_up].long_name = CSAVS(4, 27, "cursor up one");
280 tstr[T_us].name = "us";
281 tstr[T_us].long_name = CSAVS(4, 28, "begin underline");
283 tstr[T_ue].name = "ue";
284 tstr[T_ue].long_name = CSAVS(4, 29, "end underline");
286 tstr[T_vb].name = "vb";
287 tstr[T_vb].long_name = CSAVS(4, 30, "visible bell");
289 tstr[T_DC].name = "DC";
290 tstr[T_DC].long_name = CSAVS(4, 31, "delete multiple chars");
292 tstr[T_DO].name = "DO";
293 tstr[T_DO].long_name = CSAVS(4, 32, "cursor down multiple");
295 tstr[T_IC].name = "IC";
296 tstr[T_IC].long_name = CSAVS(4, 33, "insert multiple chars");
298 tstr[T_LE].name = "LE";
299 tstr[T_LE].long_name = CSAVS(4, 34, "cursor left multiple");
301 tstr[T_RI].name = "RI";
302 tstr[T_RI].long_name = CSAVS(4, 35, "cursor right multiple");
304 tstr[T_UP].name = "UP";
305 tstr[T_UP].long_name = CSAVS(4, 36, "cursor up multiple");
307 tstr[T_kh].name = "kh";
308 tstr[T_kh].long_name = CSAVS(4, 37, "send cursor home");
310 tstr[T_at7].name = "@7";
311 tstr[T_at7].long_name = CSAVS(4, 38, "send cursor end");
313 tstr[T_str].name = NULL;
314 tstr[T_str].long_name = NULL;
317 tval[T_am].name = "am";
318 tval[T_am].long_name = CSAVS(4, 37, "Has automatic margins");
320 tval[T_pt].name = "pt";
321 tval[T_pt].long_name = CSAVS(4, 38, "Can use physical tabs");
323 tval[T_li].name = "li";
324 tval[T_li].long_name = CSAVS(4, 39, "Number of lines");
326 tval[T_co].name = "co";
327 tval[T_co].long_name = CSAVS(4, 40, "Number of columns");
329 tval[T_km].name = "km";
330 tval[T_km].long_name = CSAVS(4, 41, "Has meta key");
332 tval[T_xn].name = "xn";
333 tval[T_xn].long_name = CSAVS(4, 42, "Newline ignored at right margin");
335 tval[T_val].name = NULL;
336 tval[T_val].long_name = NULL;
340 * A very useful table from justin@crim.ca (Justin Bur) :-)
341 * (Modified by per@erix.ericsson.se (Per Hedeland)
342 * - first (and second:-) case fixed)
344 * Description Termcap variables tcsh behavior
345 * am xn UseRightmost SendCRLF
346 * -------------- ------- ------- ------------ ------------
347 * Automargins yes no yes no
348 * Magic Margins yes yes yes no
349 * No Wrap no -- yes yes
352 static int me_all = 0; /* does two or more of the attributes use me */
354 static void ReBufferDisplay __P((void));
355 static void TCalloc __P((struct termcapstr *, char *));
360 struct termcapstr *t;
363 static char termcap_alloc[TC_BUFSIZE];
364 char termbuf[TC_BUFSIZE];
365 struct termcapstr *ts;
369 if (cap == NULL || *cap == '\0') {
379 tlen = strlen(t->str);
382 * New string is shorter; no need to allocate space
385 (void) strcpy(t->str, cap);
390 * New string is longer; see if we have enough space to append
392 if (tloc + 3 < TC_BUFSIZE) {
393 (void) strcpy(t->str = &termcap_alloc[tloc], cap);
394 tloc += clen + 1; /* one for \0 */
399 * Compact our buffer; no need to check compaction, cause we know it
403 for (ts = tstr; ts->name != NULL; ts++)
404 if (t != ts && ts->str != NULL && ts->str[0] != '\0') {
407 for (ptr = ts->str; *ptr != '\0'; termbuf[tlen++] = *ptr++)
409 termbuf[tlen++] = '\0';
411 (void) memmove((ptr_t) termcap_alloc, (ptr_t) termbuf, (size_t) TC_BUFSIZE);
413 if (tloc + 3 >= TC_BUFSIZE) {
414 stderror(ERR_NAME | ERR_TCNOSTR);
417 (void) strcpy(t->str = &termcap_alloc[tloc], cap);
418 tloc += clen + 1; /* one for \0 */
427 struct termcapstr *t;
430 xprintf(CGETS(7, 1, "\n\tTcsh thinks your terminal has the\n"));
431 xprintf(CGETS(7, 2, "\tfollowing characteristics:\n\n"));
432 xprintf(CGETS(7, 3, "\tIt has %d columns and %d lines\n"),
433 Val(T_co), Val(T_li));
434 s = strsave(T_HasMeta ? CGETS(7, 5, "a") : CGETS(7, 6, "no"));
435 xprintf(CGETS(7, 4, "\tIt has %s meta key\n"), s);
437 s = strsave(T_Tabs ? "" : CGETS(7, 8, " not"));
438 xprintf(CGETS(7, 7, "\tIt can%s use tabs\n"), s);
440 s = strsave((T_Margin&MARGIN_AUTO) ?
441 CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
442 xprintf(CGETS(7, 9, "\tIt %s automatic margins\n"), s);
444 if (T_Margin & MARGIN_AUTO) {
445 s = strsave((T_Margin & MARGIN_MAGIC) ?
446 CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
447 xprintf(CGETS(7, 12, "\tIt %s magic margins\n"), s);
450 for (t = tstr; t->name != NULL; t++) {
451 s = strsave(t->str && *t->str ? t->str : CGETS(7, 13, "(empty)"));
452 xprintf("\t%36s (%s) == %s\n", t->long_name, t->name, s);
469 for (bufp = b; *bufp != NULL; bufp++)
470 xfree((ptr_t) * bufp);
476 for (bufp = b; *bufp != NULL; bufp++)
477 xfree((ptr_t) * bufp);
481 TermV = (INBUFSIZE * 4) / TermH + 1;
482 b = (Char **) xmalloc((size_t) (sizeof(*b) * (TermV + 1)));
483 for (i = 0; i < TermV; i++)
484 b[i] = (Char *) xmalloc((size_t) (sizeof(*b[i]) * (TermH + 1)));
487 b = (Char **) xmalloc((size_t) (sizeof(*b) * (TermV + 1)));
488 for (i = 0; i < TermV; i++)
489 b[i] = (Char *) xmalloc((size_t) (sizeof(*b[i]) * (TermH + 1)));
498 struct termcapstr *ts;
499 struct termcapval *tv;
502 * Do the strings first
505 for (ts = tstr; ts->name != NULL; ts++)
506 if (strcmp(ts->name, what) == 0)
508 if (ts->name != NULL) {
513 if (GoodStr(T_me) && GoodStr(T_ue))
514 me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
517 if (GoodStr(T_me) && GoodStr(T_se))
518 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
520 T_CanCEOL = GoodStr(T_ce);
521 T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
522 T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
523 T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
528 * Do the numeric ones second
530 for (tv = tval; tv->name != NULL; tv++)
531 if (strcmp(tv->name, what) == 0)
534 if (tv->name != NULL) {
535 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
536 tv == &tval[T_am] || tv == &tval[T_xn]) {
537 if (strcmp(how, "yes") == 0)
539 else if (strcmp(how, "no") == 0)
542 stderror(ERR_SETTCUS, tv->name);
545 T_Tabs = (Char) Val(T_pt);
546 T_HasMeta = (Char) Val(T_km);
547 T_Margin = (Char) Val(T_am) ? MARGIN_AUTO : 0;
548 T_Margin |= (Char) Val(T_xn) ? MARGIN_MAGIC : 0;
549 if (tv == &tval[T_am] || tv == &tval[T_xn])
550 ChangeSize(Val(T_li), Val(T_co));
555 T_Cols = (Char) Val(T_co);
556 T_Lines = (Char) Val(T_li);
557 if (tv == &tval[T_co] || tv == &tval[T_li])
558 ChangeSize(Val(T_li), Val(T_co));
562 stderror(ERR_NAME | ERR_TCCAP, what);
568 * Print the termcap string out with variable substitution
574 char *cap, *scap, cv[BUFSIZE];
575 int arg_need, arg_cols, arg_rows;
576 int verbose = 0, silent = 0;
578 static const char *fmts = "%s\n", *fmtd = "%d\n";
579 struct termcapstr *t;
580 char buf[TC_BUFSIZE];
590 stderror(ERR_NAME | ERR_NOMATCH);
593 v = gargv = saveblk(v);
596 if (!*v || *v[0] == '\0')
598 if (v[0][0] == '-') {
607 stderror(ERR_NAME | ERR_TCUSAGE);
612 if (!*v || *v[0] == '\0')
614 (void) strcpy(cv, short2str(*v));
615 if (strcmp(cv, "tabs") == 0) {
616 xprintf(fmts, T_Tabs ? CGETS(7, 14, "yes") :
621 else if (strcmp(cv, "meta") == 0) {
622 xprintf(fmts, Val(T_km) ? CGETS(7, 14, "yes") :
627 else if (strcmp(cv, "xn") == 0) {
628 xprintf(fmts, T_Margin & MARGIN_MAGIC ? CGETS(7, 14, "yes") :
633 else if (strcmp(cv, "am") == 0) {
634 xprintf(fmts, T_Margin & MARGIN_AUTO ? CGETS(7, 14, "yes") :
639 else if (strcmp(cv, "baud") == 0) {
642 for (i = 0; baud_rate[i].b_name != NULL; i++)
643 if (T_Speed == baud_rate[i].b_rate) {
644 xprintf(fmts, baud_rate[i].b_name);
652 else if (strcmp(cv, "rows") == 0 || strcmp(cv, "lines") == 0) {
653 xprintf(fmtd, Val(T_li));
657 else if (strcmp(cv, "cols") == 0) {
658 xprintf(fmtd, Val(T_co));
664 * Try to use our local definition first
667 for (t = tstr; t->name != NULL; t++)
668 if (strcmp(t->name, cv) == 0) {
673 scap = tgetstr(cv, &area);
674 if (!scap || scap[0] == '\0') {
676 xprintf(CGETS(7, 14, "yes\n"));
682 stderror(ERR_NAME | ERR_TCCAP, cv);
686 * Count home many values we need for this capability.
688 for (cap = scap, arg_need = 0; *cap; cap++)
708 * hpux has lot's of them...
711 stderror(ERR_NAME | ERR_TCPARM, *cap);
712 /* This is bad, but I won't complain */
723 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
725 (void) tputs(scap, 1, PUTRAW);
729 if (!*v || *v[0] == '\0')
730 stderror(ERR_NAME | ERR_TCNARGS, cv, 1);
732 arg_rows = atoi(short2str(*v));
738 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
740 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, PUTRAW);
743 /* This is wrong, but I will ignore it... */
745 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
749 if (!*v || *v[0] == '\0') {
753 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
755 arg_cols = atoi(short2str(*v));
757 if (!*v || *v[0] == '\0') {
761 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
763 arg_rows = atoi(short2str(*v));
769 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
771 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, PUTRAW);
790 { STRdown, T_kd, { 0 }, 0 },
792 { STRup, T_ku, { 0 }, 0 },
794 { STRleft, T_kl, { 0 }, 0 },
796 { STRright, T_kr, { 0 }, 0 },
798 { STRhome, T_kh, { 0 }, 0 },
800 { STRend, T_at7, { 0 }, 0}
807 arrow[A_K_DN].fun.cmd = F_DOWN_HIST;
808 arrow[A_K_DN].type = XK_CMD;
810 arrow[A_K_UP].fun.cmd = F_UP_HIST;
811 arrow[A_K_UP].type = XK_CMD;
813 arrow[A_K_LT].fun.cmd = F_CHARBACK;
814 arrow[A_K_LT].type = XK_CMD;
816 arrow[A_K_RT].fun.cmd = F_CHARFWD;
817 arrow[A_K_RT].type = XK_CMD;
819 arrow[A_K_HO].fun.cmd = F_TOBEG;
820 arrow[A_K_HO].type = XK_CMD;
822 arrow[A_K_EN].fun.cmd = F_TOEND;
823 arrow[A_K_EN].type = XK_CMD;
829 static Char strA[] = {033, '[', 'A', '\0'};
830 static Char strB[] = {033, '[', 'B', '\0'};
831 static Char strC[] = {033, '[', 'C', '\0'};
832 static Char strD[] = {033, '[', 'D', '\0'};
833 static Char strH[] = {033, '[', 'H', '\0'};
834 static Char strF[] = {033, '[', 'F', '\0'};
835 static Char stOA[] = {033, 'O', 'A', '\0'};
836 static Char stOB[] = {033, 'O', 'B', '\0'};
837 static Char stOC[] = {033, 'O', 'C', '\0'};
838 static Char stOD[] = {033, 'O', 'D', '\0'};
839 static Char stOH[] = {033, 'O', 'H', '\0'};
840 static Char stOF[] = {033, 'O', 'F', '\0'};
846 strA[0] = CTL_ESC('\033');
847 strB[0] = CTL_ESC('\033');
848 strC[0] = CTL_ESC('\033');
849 strD[0] = CTL_ESC('\033');
850 strH[0] = CTL_ESC('\033');
851 strF[0] = CTL_ESC('\033');
852 stOA[0] = CTL_ESC('\033');
853 stOB[0] = CTL_ESC('\033');
854 stOC[0] = CTL_ESC('\033');
855 stOD[0] = CTL_ESC('\033');
856 stOH[0] = CTL_ESC('\033');
857 stOF[0] = CTL_ESC('\033');
863 cs.buf = strA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
864 cs.buf = strB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
865 cs.buf = strC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
866 cs.buf = strD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
867 cs.buf = strH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
868 cs.buf = strF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
869 cs.buf = stOA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
870 cs.buf = stOB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
871 cs.buf = stOC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
872 cs.buf = stOD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
873 cs.buf = stOH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
874 cs.buf = stOF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
878 cs.buf = &strA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
879 cs.buf = &strB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
880 cs.buf = &strC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
881 cs.buf = &strD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
882 cs.buf = &strH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
883 cs.buf = &strF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
884 cs.buf = &stOA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
885 cs.buf = &stOB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
886 cs.buf = &stOC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
887 cs.buf = &stOD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
888 cs.buf = &stOH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
889 cs.buf = &stOF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
895 SetArrowKeys(name, fun, type)
901 for (i = 0; i < A_K_NKEYS; i++)
902 if (Strcmp(name->buf, arrow[i].name) == 0) {
904 arrow[i].type = type;
915 for (i = 0; i < A_K_NKEYS; i++)
916 if (Strcmp(name, arrow[i].name) == 0)
926 for (i = 0; i < A_K_NKEYS; i++)
927 if (Strcmp(name->buf, arrow[i].name) == 0) {
928 arrow[i].type = XK_NOD;
940 for (i = 0; i < A_K_NKEYS; i++)
941 if (name->len == 0 || Strcmp(name->buf, arrow[i].name) == 0)
942 if (arrow[i].type != XK_NOD) {
944 cs.buf = arrow[i].name;
945 cs.len = Strlen(cs.buf);
946 (void) printOne(&cs, &arrow[i].fun, arrow[i].type);
961 map = VImode ? CcAltMap : CcKeyMap;
962 dmap = VImode ? CcViCmdMap : CcEmacsMap;
966 for (i = 0; i < A_K_NKEYS; i++) {
967 p = tstr[arrow[i].key].str;
969 j = (unsigned char) *p;
970 cs.buf = str2short(p);
971 cs.len = Strlen(cs.buf);
973 * Assign the arrow keys only if:
975 * 1. They are multi-character arrow keys and the user
976 * has not re-assigned the leading character, or
977 * has re-assigned the leading character to be F_XKEY
978 * 2. They are single arrow keys pointing to an unassigned key.
980 if (arrow[i].type == XK_NOD) {
984 if (p[1] && (dmap[j] == map[j] || map[j] == F_XKEY)) {
985 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
988 else if (map[j] == F_UNASSIGNED) {
990 if (arrow[i].type == XK_CMD)
991 map[j] = arrow[i].fun.cmd;
993 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
1000 static Char cur_atr = 0; /* current attributes */
1007 if (atr != cur_atr) {
1008 if (me_all && GoodStr(T_me)) {
1009 if (((cur_atr & BOLD) && !(atr & BOLD)) ||
1010 ((cur_atr & UNDER) && !(atr & UNDER)) ||
1011 ((cur_atr & STANDOUT) && !(atr & STANDOUT))) {
1012 (void) tputs(Str(T_me), 1, PUTPURE);
1016 if ((atr & BOLD) != (cur_atr & BOLD)) {
1018 if (GoodStr(T_md) && GoodStr(T_me)) {
1019 (void) tputs(Str(T_md), 1, PUTPURE);
1024 if (GoodStr(T_md) && GoodStr(T_me)) {
1025 (void) tputs(Str(T_me), 1, PUTPURE);
1026 if ((cur_atr & STANDOUT) && GoodStr(T_se)) {
1027 (void) tputs(Str(T_se), 1, PUTPURE);
1028 cur_atr &= ~STANDOUT;
1030 if ((cur_atr & UNDER) && GoodStr(T_ue)) {
1031 (void) tputs(Str(T_ue), 1, PUTPURE);
1038 if ((atr & STANDOUT) != (cur_atr & STANDOUT)) {
1039 if (atr & STANDOUT) {
1040 if (GoodStr(T_so) && GoodStr(T_se)) {
1041 (void) tputs(Str(T_so), 1, PUTPURE);
1042 cur_atr |= STANDOUT;
1046 if (GoodStr(T_se)) {
1047 (void) tputs(Str(T_se), 1, PUTPURE);
1048 cur_atr &= ~STANDOUT;
1052 if ((atr & UNDER) != (cur_atr & UNDER)) {
1054 if (GoodStr(T_us) && GoodStr(T_ue)) {
1055 (void) tputs(Str(T_us), 1, PUTPURE);
1060 if (GoodStr(T_ue)) {
1061 (void) tputs(Str(T_ue), 1, PUTPURE);
1069 /* PWP 6-27-88 -- if the tty driver thinks that we can tab, we ask termcap */
1077 MoveToLine(where) /* move to line <where> (first line == 0) */
1078 int where; /* as efficiently as possible; */
1082 if (where == CursorV)
1085 if (where > TermV) {
1087 xprintf("MoveToLine: where is ridiculous: %d\r\n", where);
1089 #endif /* DEBUG_SCREEN */
1093 del = where - CursorV;
1097 if ((T_Margin & MARGIN_AUTO) && Display[CursorV][0] != '\0') {
1100 for (h = TermH - 1; h > 0 && Display[CursorV][h] == CHAR_DBWIDTH;
1103 /* move without newline */
1105 so_write(&Display[CursorV][CursorH], TermH - CursorH); /* updates CursorH/V*/
1109 if ((del > 1) && GoodStr(T_DO)) {
1110 (void) tputs(tgoto(Str(T_DO), del, del), del, PUTPURE);
1114 for ( ; del > 0; del--)
1115 (void) putraw('\n');
1116 CursorH = 0; /* because the \n will become \r\n */
1121 else { /* del < 0 */
1122 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
1123 (void) tputs(tgoto(Str(T_UP), -del, -del), -del, PUTPURE);
1127 for (i = 0; i < -del; i++)
1128 (void) tputs(Str(T_up), 1, PUTPURE);
1131 CursorV = where; /* now where is here */
1135 MoveToChar(where) /* move to character position (where) */
1137 { /* as efficiently as possible */
1141 if (where == CursorH)
1144 if (where >= TermH) {
1146 xprintf("MoveToChar: where is riduculous: %d\r\n", where);
1148 #endif /* DEBUG_SCREEN */
1152 if (!where) { /* if where is first column */
1153 (void) putraw('\r'); /* do a CR */
1158 del = where - CursorH;
1160 if ((del < -4 || del > 4) && GoodStr(T_ch))
1161 /* go there directly */
1162 (void) tputs(tgoto(Str(T_ch), where, where), where, PUTPURE);
1165 if (del > 0) { /* moving forward */
1166 if ((del > 4) && GoodStr(T_RI))
1167 (void) tputs(tgoto(Str(T_RI), del, del), del, PUTPURE);
1169 /* if I can do tabs, use them */
1171 if ((CursorH & 0370) != (where & ~0x7)
1172 && Display[CursorV][where & ~0x7] != CHAR_DBWIDTH) {
1173 /* if not within tab stop */
1174 for (i = (CursorH & 0370); i < (where & ~0x7); i += 8)
1175 (void) putraw('\t'); /* then tab over */
1176 CursorH = where & ~0x7;
1177 /* Note: considering that we often want to go to
1178 TermH - 1 for the wrapping, it would be nice to
1179 optimize this case by tabbing to the last column
1180 - but this doesn't work for all terminals! */
1183 /* it's usually cheaper to just write the chars, so we do. */
1185 /* NOTE THAT so_write() WILL CHANGE CursorH!!! */
1186 so_write(&Display[CursorV][CursorH], where - CursorH);
1190 else { /* del < 0 := moving backward */
1191 if ((-del > 4) && GoodStr(T_LE))
1192 (void) tputs(tgoto(Str(T_LE), -del, -del), -del, PUTPURE);
1193 else { /* can't go directly there */
1194 /* if the "cost" is greater than the "cost" from col 0 */
1195 if (T_Tabs ? (-del > ((where >> 3) + (where & 07)))
1197 (void) putraw('\r'); /* do a CR */
1199 goto mc_again; /* and try again */
1201 for (i = 0; i < -del; i++)
1202 (void) putraw('\b');
1206 CursorH = where; /* now where is here */
1215 return; /* catch bugs */
1219 xprintf("so_write: n is riduculous: %d\r\n", n);
1221 #endif /* DEBUG_SCREEN */
1226 if (*cp != CHAR_DBWIDTH) {
1227 if (*cp & LITERAL) {
1229 #ifdef DEBUG_LITERAL
1230 xprintf("so: litnum %d\r\n", (int)(*cp & ~LITERAL));
1231 #endif /* DEBUG_LITERAL */
1232 for (d = litptr + (*cp & ~LITERAL) * LIT_FACTOR; *d; d++)
1236 (void) putwraw(*cp);
1242 if (CursorH >= TermH) { /* wrap? */
1243 if (T_Margin & MARGIN_AUTO) { /* yes */
1246 if (T_Margin & MARGIN_MAGIC) {
1247 /* force the wrap to avoid the "magic" situation */
1249 if ((c = Display[CursorV][CursorH]) != '\0') {
1251 while(Display[CursorV][CursorH] == CHAR_DBWIDTH)
1260 else /* no wrap, but cursor stays on screen */
1261 CursorH = TermH - 1;
1267 DeleteChars(num) /* deletes <num> characters */
1275 xprintf(CGETS(7, 16, "ERROR: cannot delete\r\n"));
1276 #endif /* DEBUG_EDIT */
1283 xprintf(CGETS(7, 17, "DeleteChars: num is riduculous: %d\r\n"), num);
1285 #endif /* DEBUG_SCREEN */
1289 if (GoodStr(T_DC)) /* if I have multiple delete */
1290 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more expen. */
1291 (void) tputs(tgoto(Str(T_DC), num, num), num, PUTPURE);
1295 if (GoodStr(T_dm)) /* if I have delete mode */
1296 (void) tputs(Str(T_dm), 1, PUTPURE);
1298 if (GoodStr(T_dc)) /* else do one at a time */
1300 (void) tputs(Str(T_dc), 1, PUTPURE);
1302 if (GoodStr(T_ed)) /* if I have delete mode */
1303 (void) tputs(Str(T_ed), 1, PUTPURE);
1307 Insert_write(cp, num) /* Puts terminal in insert character mode, */
1309 int num; /* or inserts num characters in the line */
1315 xprintf(CGETS(7, 18, "ERROR: cannot insert\r\n"));
1316 #endif /* DEBUG_EDIT */
1323 xprintf(CGETS(7, 19, "StartInsert: num is riduculous: %d\r\n"), num);
1325 #endif /* DEBUG_SCREEN */
1329 if (GoodStr(T_IC)) /* if I have multiple insert */
1330 if ((num > 1) || !GoodStr(T_ic)) { /* if ic would be more expen. */
1331 (void) tputs(tgoto(Str(T_IC), num, num), num, PUTPURE);
1332 so_write(cp, num); /* this updates CursorH/V */
1336 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
1337 (void) tputs(Str(T_im), 1, PUTPURE);
1339 so_write(cp, num); /* this updates CursorH/V */
1341 if (GoodStr(T_ip)) /* have to make num chars insert */
1342 (void) tputs(Str(T_ip), 1, PUTPURE);
1344 (void) tputs(Str(T_ei), 1, PUTPURE);
1349 if (GoodStr(T_ic)) /* have to make num chars insert */
1350 (void) tputs(Str(T_ic), 1, PUTPURE); /* insert a char */
1352 so_write(cp++, 1); /* this updates CursorH/V */
1354 if (GoodStr(T_ip)) /* have to make num chars insert */
1355 (void) tputs(Str(T_ip), 1, PUTPURE);/* pad the inserted char */
1362 ClearEOL(num) /* clear to end of line. There are num */
1363 int num; /* characters to clear */
1370 if (T_CanCEOL && GoodStr(T_ce))
1371 (void) tputs(Str(T_ce), 1, PUTPURE);
1373 for (i = 0; i < num; i++)
1375 CursorH += num; /* have written num spaces */
1381 { /* clear the whole screen and home */
1383 /* send the clear screen code */
1384 (void) tputs(Str(T_cl), Val(T_li), PUTPURE);
1385 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
1386 (void) tputs(Str(T_ho), Val(T_li), PUTPURE); /* home */
1387 /* clear to bottom of screen */
1388 (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1391 (void) putraw('\r');
1392 (void) putraw('\n');
1398 { /* produce a sound */
1400 if (adrof(STRnobeep))
1403 if (GoodStr(T_vb) && adrof(STRvisiblebell))
1404 (void) tputs(Str(T_vb), 1, PUTPURE); /* visible bell */
1405 else if (GoodStr(T_bl))
1406 /* what termcap says we should use */
1407 (void) tputs(Str(T_bl), 1, PUTPURE);
1409 (void) putraw(CTL_ESC('\007')); /* an ASCII bell; ^G */
1414 { /* clear to the bottom of the screen */
1416 (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1417 else if (GoodStr(T_ce))
1418 (void) tputs(Str(T_ce), Val(T_li), PUTPURE);
1423 { /* read in the needed terminal capabilites */
1426 char buf[TC_BUFSIZE];
1427 static char bp[TC_BUFSIZE];
1429 struct termcapstr *t;
1435 # endif /* BSDSIGS */
1438 /* don't want to confuse things here */
1440 omask = sigblock(sigmask(SIG_WINDOW)) & ~sigmask(SIG_WINDOW);
1441 # else /* BSDSIGS */
1442 (void) sighold(SIG_WINDOW);
1443 # endif /* BSDSIGS */
1444 #endif /* SIG_WINDOW */
1449 setname("gettermcaps");
1450 ptr = getenv("TERM");
1454 * If we are on a pad, we pretend that we are dumb. Otherwise the termcap
1455 * library will put us in a weird screen mode, thinking that we are going
1462 if (!ptr || !ptr[0] || !strcmp(ptr, "wm") || !strcmp(ptr,"dmx"))
1465 setzero(bp, TC_BUFSIZE);
1467 i = tgetent(bp, ptr);
1470 #if (SYSVREL == 0) || defined(IRIS3D)
1471 xprintf(CGETS(7, 20, "%s: Cannot open /etc/termcap.\n"), progname);
1474 #endif /* SYSVREL */
1475 xprintf(CGETS(7, 21,
1476 "%s: No entry for terminal type \"%s\"\n"), progname,
1479 xprintf(CGETS(7, 22, "%s: using dumb terminal settings.\n"), progname);
1480 Val(T_co) = 80; /* do a dumb terminal */
1481 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
1482 for (t = tstr; t->name != NULL; t++)
1487 Val(T_pt) = tgetflag("pt") && !tgetflag("xt");
1488 /* do we have a meta? */
1489 Val(T_km) = (tgetflag("km") || tgetflag("MT"));
1490 Val(T_am) = tgetflag("am");
1491 Val(T_xn) = tgetflag("xn");
1492 Val(T_co) = tgetnum("co");
1493 Val(T_li) = tgetnum("li");
1494 for (t = tstr; t->name != NULL; t++)
1495 TCalloc(t, tgetstr(t->name, &area));
1498 Val(T_co) = 80; /* just in case */
1502 T_Cols = (Char) Val(T_co);
1503 T_Lines = (Char) Val(T_li);
1505 T_Tabs = (Char) Val(T_pt);
1506 T_HasMeta = (Char) Val(T_km);
1507 T_Margin = (Char) Val(T_am) ? MARGIN_AUTO : 0;
1508 T_Margin |= (Char) Val(T_xn) ? MARGIN_MAGIC : 0;
1509 T_CanCEOL = GoodStr(T_ce);
1510 T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
1511 T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
1512 T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
1513 if (GoodStr(T_me) && GoodStr(T_ue))
1514 me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
1517 if (GoodStr(T_me) && GoodStr(T_se))
1518 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
1523 xprintf(CGETS(7, 23, "%s: WARNING: Your terminal cannot move up.\n",
1525 xprintf(CGETS(7, 24, "Editing may be odd for long lines.\n"));
1528 xprintf(CGETS(7, 25, "no clear EOL capability.\n"));
1530 xprintf(CGETS(7, 26, "no delete char capability.\n"));
1532 xprintf(CGETS(7, 27, "no insert char capability.\n"));
1533 #endif /* DEBUG_SCREEN */
1538 (void) GetSize(&lins, &cols); /* get the correct window size */
1539 ChangeSize(lins, cols);
1542 (void) sigsetmask(omask); /* can change it again */
1543 # else /* BSDSIGS */
1544 (void) sigrelse(SIG_WINDOW);
1545 # endif /* BSDSIGS */
1546 #else /* SIG_WINDOW */
1547 ChangeSize(Val(T_li), Val(T_co));
1548 #endif /* SIG_WINDOW */
1555 * Return the new window size in lines and cols, and
1556 * true if the size was changed. This can fail if SHIN
1557 * is not a tty, but it will work in most cases.
1570 struct winsize ws; /* from 4.3 */
1572 if (ioctl(SHIN, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
1580 #else /* TIOCGWINSZ */
1584 struct ttysize ts; /* from Sun */
1586 if (ioctl(SHIN, TIOCGSIZE, (ioctl_t) &ts) != -1) {
1590 *lins = ts.ts_lines;
1593 # endif /* TIOCGSIZE */
1594 #endif /* TIOCGWINSZ */
1596 return (Val(T_co) != *cols || Val(T_li) != *lins);
1599 #endif /* SIGWINDOW */
1602 ChangeSize(lins, cols)
1608 Val(T_co) = (cols < 2) ? 80 : cols;
1609 Val(T_li) = (lins < 1) ? 24 : lins;
1613 * We want to affect the environment only when we have a valid
1614 * setup, not when we get bad settings. Consider the following scenario:
1615 * We just logged in, and we have not initialized the editor yet.
1616 * We reset termcap with tset, and not $TERMCAP has the right
1617 * terminal size. But since the editor is not initialized yet, and
1618 * the kernel's notion of the terminal size might be wrong we arrive
1619 * here with lines = columns = 0. If we reset the environment we lose
1620 * our only chance to get the window size right.
1622 if (Val(T_co) == cols && Val(T_li) == lins) {
1626 if (getenv("COLUMNS")) {
1627 (void) Itoa(Val(T_co), buf, 0, 0);
1628 tsetenv(STRCOLUMNS, buf);
1631 if (getenv("LINES")) {
1632 (void) Itoa(Val(T_li), buf, 0, 0);
1633 tsetenv(STRLINES, buf);
1636 if ((tptr = getenv("TERMCAP")) != NULL) {
1637 /* Leave 64 characters slop in case we enlarge the termcap string */
1638 Char termcap[1024+64], backup[1024+64], *ptr;
1640 ptr = str2short(tptr);
1641 (void) Strncpy(termcap, ptr, 1024);
1642 termcap[1023] = '\0';
1644 /* update termcap string; first do columns */
1649 if ((ptr = Strstr(termcap, buf)) == NULL) {
1650 (void) Strcpy(backup, termcap);
1653 size_t len = (ptr - termcap) + Strlen(buf);
1654 (void) Strncpy(backup, termcap, len);
1656 (void) Itoa(Val(T_co), buf, 0, 0);
1657 (void) Strcat(backup + len, buf);
1658 ptr = Strchr(ptr, ':');
1659 (void) Strcat(backup, ptr);
1667 if ((ptr = Strstr(backup, buf)) == NULL) {
1668 (void) Strcpy(termcap, backup);
1671 size_t len = (ptr - backup) + Strlen(buf);
1672 (void) Strncpy(termcap, backup, len);
1673 termcap[len] = '\0';
1674 (void) Itoa(Val(T_li), buf, 0, 0);
1675 (void) Strcat(termcap, buf);
1676 ptr = Strchr(ptr, ':');
1677 (void) Strcat(termcap, ptr);
1680 * Chop the termcap string at 1024 characters to avoid core-dumps
1681 * in the termcap routines
1683 termcap[1023] = '\0';
1684 tsetenv(STRTERMCAP, termcap);
1687 #endif /* KNOWsize */
1689 ReBufferDisplay(); /* re-make display buffers */