2 * ed.screen.c: Editor/termcap-curses interface
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 /* #define DEBUG_LITERAL */
40 * IMPORTANT NOTE: these routines are allowed to look at the current screen
41 * and the current possition assuming that it is correct. If this is not
42 * true, then the update will be WRONG! This is (should be) a valid
46 #define TC_BUFSIZE 2048
48 #define GoodStr(a) (tstr[a].str != NULL && tstr[a].str[0] != '\0')
49 #define Str(a) tstr[a].str
50 #define Val(a) tval[a].val
164 static struct termcapstr {
166 const char *long_name;
178 static struct termcapval {
180 const char *long_name;
190 for (i = 0; i < T_str + 1; i++)
191 xfree((ptr_t)(intptr_t)tstr[i].long_name);
193 for (i = 0; i < T_val + 1; i++)
194 xfree((ptr_t)(intptr_t)tval[i].long_name);
197 tstr[T_al].name = "al";
198 tstr[T_al].long_name = CSAVS(4, 1, "add new blank line");
200 tstr[T_bl].name = "bl";
201 tstr[T_bl].long_name = CSAVS(4, 2, "audible bell");
203 tstr[T_cd].name = "cd";
204 tstr[T_cd].long_name = CSAVS(4, 3, "clear to bottom");
206 tstr[T_ce].name = "ce";
207 tstr[T_ce].long_name = CSAVS(4, 4, "clear to end of line");
209 tstr[T_ch].name = "ch";
210 tstr[T_ch].long_name = CSAVS(4, 5, "cursor to horiz pos");
212 tstr[T_cl].name = "cl";
213 tstr[T_cl].long_name = CSAVS(4, 6, "clear screen");
215 tstr[T_dc].name = "dc";
216 tstr[T_dc].long_name = CSAVS(4, 7, "delete a character");
218 tstr[T_dl].name = "dl";
219 tstr[T_dl].long_name = CSAVS(4, 8, "delete a line");
221 tstr[T_dm].name = "dm";
222 tstr[T_dm].long_name = CSAVS(4, 9, "start delete mode");
224 tstr[T_ed].name = "ed";
225 tstr[T_ed].long_name = CSAVS(4, 10, "end delete mode");
227 tstr[T_ei].name = "ei";
228 tstr[T_ei].long_name = CSAVS(4, 11, "end insert mode");
230 tstr[T_fs].name = "fs";
231 tstr[T_fs].long_name = CSAVS(4, 12, "cursor from status line");
233 tstr[T_ho].name = "ho";
234 tstr[T_ho].long_name = CSAVS(4, 13, "home cursor");
236 tstr[T_ic].name = "ic";
237 tstr[T_ic].long_name = CSAVS(4, 14, "insert character");
239 tstr[T_im].name = "im";
240 tstr[T_im].long_name = CSAVS(4, 15, "start insert mode");
242 tstr[T_ip].name = "ip";
243 tstr[T_ip].long_name = CSAVS(4, 16, "insert padding");
245 tstr[T_kd].name = "kd";
246 tstr[T_kd].long_name = CSAVS(4, 17, "sends cursor down");
248 tstr[T_kl].name = "kl";
249 tstr[T_kl].long_name = CSAVS(4, 18, "sends cursor left");
251 tstr[T_kr].name = "kr";
252 tstr[T_kr].long_name = CSAVS(4, 19, "sends cursor right");
254 tstr[T_ku].name = "ku";
255 tstr[T_ku].long_name = CSAVS(4, 20, "sends cursor up");
257 tstr[T_md].name = "md";
258 tstr[T_md].long_name = CSAVS(4, 21, "begin bold");
260 tstr[T_me].name = "me";
261 tstr[T_me].long_name = CSAVS(4, 22, "end attributes");
263 tstr[T_nd].name = "nd";
264 tstr[T_nd].long_name = CSAVS(4, 23, "non destructive space");
266 tstr[T_se].name = "se";
267 tstr[T_se].long_name = CSAVS(4, 24, "end standout");
269 tstr[T_so].name = "so";
270 tstr[T_so].long_name = CSAVS(4, 25, "begin standout");
272 tstr[T_ts].name = "ts";
273 tstr[T_ts].long_name = CSAVS(4, 26, "cursor to status line");
275 tstr[T_up].name = "up";
276 tstr[T_up].long_name = CSAVS(4, 27, "cursor up one");
278 tstr[T_us].name = "us";
279 tstr[T_us].long_name = CSAVS(4, 28, "begin underline");
281 tstr[T_ue].name = "ue";
282 tstr[T_ue].long_name = CSAVS(4, 29, "end underline");
284 tstr[T_vb].name = "vb";
285 tstr[T_vb].long_name = CSAVS(4, 30, "visible bell");
287 tstr[T_DC].name = "DC";
288 tstr[T_DC].long_name = CSAVS(4, 31, "delete multiple chars");
290 tstr[T_DO].name = "DO";
291 tstr[T_DO].long_name = CSAVS(4, 32, "cursor down multiple");
293 tstr[T_IC].name = "IC";
294 tstr[T_IC].long_name = CSAVS(4, 33, "insert multiple chars");
296 tstr[T_LE].name = "LE";
297 tstr[T_LE].long_name = CSAVS(4, 34, "cursor left multiple");
299 tstr[T_RI].name = "RI";
300 tstr[T_RI].long_name = CSAVS(4, 35, "cursor right multiple");
302 tstr[T_UP].name = "UP";
303 tstr[T_UP].long_name = CSAVS(4, 36, "cursor up multiple");
305 tstr[T_kh].name = "kh";
306 tstr[T_kh].long_name = CSAVS(4, 43, "send cursor home");
308 tstr[T_at7].name = "@7";
309 tstr[T_at7].long_name = CSAVS(4, 44, "send cursor end");
311 tstr[T_mr].name = "mr";
312 tstr[T_mr].long_name = CSAVS(4, 45, "begin reverse video");
314 tstr[T_str].name = NULL;
315 tstr[T_str].long_name = NULL;
318 tval[T_am].name = "am";
319 tval[T_am].long_name = CSAVS(4, 37, "Has automatic margins");
321 tval[T_pt].name = "pt";
322 tval[T_pt].long_name = CSAVS(4, 38, "Can use physical tabs");
324 tval[T_li].name = "li";
325 tval[T_li].long_name = CSAVS(4, 39, "Number of lines");
327 tval[T_co].name = "co";
328 tval[T_co].long_name = CSAVS(4, 40, "Number of columns");
330 tval[T_km].name = "km";
331 tval[T_km].long_name = CSAVS(4, 41, "Has meta key");
333 tval[T_xn].name = "xn";
334 tval[T_xn].long_name = CSAVS(4, 42, "Newline ignored at right margin");
336 tval[T_val].name = NULL;
337 tval[T_val].long_name = NULL;
341 * A very useful table from justin@crim.ca (Justin Bur) :-)
342 * (Modified by per@erix.ericsson.se (Per Hedeland)
343 * - first (and second:-) case fixed)
345 * Description Termcap variables tcsh behavior
346 * am xn UseRightmost SendCRLF
347 * -------------- ------- ------- ------------ ------------
348 * Automargins yes no yes no
349 * Magic Margins yes yes yes no
350 * No Wrap no -- yes yes
353 static int me_all = 0; /* does two or more of the attributes use me */
355 static void ReBufferDisplay (void);
356 static void TCset (struct termcapstr *, const char *);
360 TCset(struct termcapstr *t, const char *cap)
362 if (cap == NULL || *cap == '\0') {
368 size = strlen(cap) + 1;
369 t->str = xrealloc(t->str, size);
370 memcpy(t->str, cap, size);
379 struct termcapstr *t;
382 xprintf("%s", CGETS(7, 1, "\n\tTcsh thinks your terminal has the\n"));
383 xprintf("%s", CGETS(7, 2, "\tfollowing characteristics:\n\n"));
384 xprintf(CGETS(7, 3, "\tIt has %d columns and %d lines\n"),
385 Val(T_co), Val(T_li));
386 s = strsave(T_HasMeta ? CGETS(7, 5, "a") : CGETS(7, 6, "no"));
387 cleanup_push(s, xfree);
389 xprintf(CGETS(7, 4, "\tIt has %s meta key\n"), s);
390 s = strsave(T_Tabs ? "" : CGETS(7, 8, " not"));
391 cleanup_push(s, xfree);
392 xprintf(CGETS(7, 7, "\tIt can%s use tabs\n"), s);
393 s = strsave((T_Margin&MARGIN_AUTO) ?
394 CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
395 cleanup_push(s, xfree);
396 xprintf(CGETS(7, 9, "\tIt %s automatic margins\n"), s);
397 if (T_Margin & MARGIN_AUTO) {
398 s = strsave((T_Margin & MARGIN_MAGIC) ?
399 CGETS(7, 10, "has") : CGETS(7, 11, "does not have"));
400 cleanup_push(s, xfree);
401 xprintf(CGETS(7, 12, "\tIt %s magic margins\n"), s);
403 for (t = tstr; t->name != NULL; t++) {
404 s = strsave(t->str && *t->str ? t->str : CGETS(7, 13, "(empty)"));
405 cleanup_push(s, xfree);
406 xprintf("\t%36s (%s) == %s\n", t->long_name, t->name, s);
410 cleanup_until(first);
415 ReBufferDisplay(void)
427 TermV = (INBUFSIZE * 4) / TermH + 1;/*FIXBUF*/
428 b = xmalloc(sizeof(*b) * (TermV + 1));
429 for (i = 0; i < TermV; i++)
430 b[i] = xmalloc(sizeof(*b[i]) * (TermH + 1));
433 b = xmalloc(sizeof(*b) * (TermV + 1));
434 for (i = 0; i < TermV; i++)
435 b[i] = xmalloc(sizeof(*b[i]) * (TermH + 1));
441 SetTC(char *what, char *how)
443 struct termcapstr *ts;
444 struct termcapval *tv;
447 * Do the strings first
450 for (ts = tstr; ts->name != NULL; ts++)
451 if (strcmp(ts->name, what) == 0)
453 if (ts->name != NULL) {
458 if (GoodStr(T_me) && GoodStr(T_ue))
459 me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
462 if (GoodStr(T_me) && GoodStr(T_se))
463 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
465 T_CanCEOL = GoodStr(T_ce);
466 T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
467 T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
468 T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
473 * Do the numeric ones second
475 for (tv = tval; tv->name != NULL; tv++)
476 if (strcmp(tv->name, what) == 0)
479 if (tv->name != NULL) {
480 if (tv == &tval[T_pt] || tv == &tval[T_km] ||
481 tv == &tval[T_am] || tv == &tval[T_xn]) {
482 if (strcmp(how, "yes") == 0)
484 else if (strcmp(how, "no") == 0)
487 stderror(ERR_SETTCUS, tv->name);
491 T_HasMeta = Val(T_km);
492 T_Margin = Val(T_am) ? MARGIN_AUTO : 0;
493 T_Margin |= Val(T_xn) ? MARGIN_MAGIC : 0;
494 if (tv == &tval[T_am] || tv == &tval[T_xn])
495 ChangeSize(Val(T_li), Val(T_co));
500 T_Cols = (Char) Val(T_co);
501 T_Lines = (Char) Val(T_li);
502 if (tv == &tval[T_co] || tv == &tval[T_li])
503 ChangeSize(Val(T_li), Val(T_co));
507 stderror(ERR_NAME | ERR_TCCAP, what);
513 * Print the termcap string out with variable substitution
518 char *cap, *scap, *cv;
519 int arg_need, arg_cols, arg_rows;
520 int verbose = 0, silent = 0;
522 static const char fmts[] = "%s\n", fmtd[] = "%d\n";
523 struct termcapstr *t;
524 char buf[TC_BUFSIZE];
531 v = glob_all_or_error(v);
533 cleanup_push(globbed, blk_cleanup);
535 if (!*v || *v[0] == '\0')
537 if (v[0][0] == '-') {
546 stderror(ERR_NAME | ERR_TCUSAGE);
551 if (!*v || *v[0] == '\0')
553 cv = strsave(short2str(*v));
554 cleanup_push(cv, xfree);
555 if (strcmp(cv, "tabs") == 0) {
556 xprintf(fmts, T_Tabs ? CGETS(7, 14, "yes") :
560 else if (strcmp(cv, "meta") == 0) {
561 xprintf(fmts, Val(T_km) ? CGETS(7, 14, "yes") :
565 else if (strcmp(cv, "xn") == 0) {
566 xprintf(fmts, T_Margin & MARGIN_MAGIC ? CGETS(7, 14, "yes") :
570 else if (strcmp(cv, "am") == 0) {
571 xprintf(fmts, T_Margin & MARGIN_AUTO ? CGETS(7, 14, "yes") :
575 else if (strcmp(cv, "baud") == 0) {
578 for (i = 0; baud_rate[i].b_name != NULL; i++)
579 if (T_Speed == baud_rate[i].b_rate) {
580 xprintf(fmts, baud_rate[i].b_name);
586 else if (strcmp(cv, "rows") == 0 || strcmp(cv, "lines") == 0 ||
587 strcmp(cv, "li") == 0) {
588 xprintf(fmtd, Val(T_li));
591 else if (strcmp(cv, "cols") == 0 || strcmp(cv, "co") == 0) {
592 xprintf(fmtd, Val(T_co));
597 * Try to use our local definition first
600 for (t = tstr; t->name != NULL; t++)
601 if (strcmp(t->name, cv) == 0) {
606 scap = tgetstr(cv, &area);
607 if (!scap || scap[0] == '\0') {
609 xprintf("%s", CGETS(7, 14, "yes\n"));
615 stderror(ERR_NAME | ERR_TCCAP, cv);
619 * Count home many values we need for this capability.
621 for (cap = scap, arg_need = 0; *cap; cap++)
641 * hpux has lot's of them...
644 stderror(ERR_NAME | ERR_TCPARM, *cap);
645 /* This is bad, but I won't complain */
656 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
658 (void) tputs(scap, 1, PUTRAW);
662 if (!*v || *v[0] == '\0')
663 stderror(ERR_NAME | ERR_TCNARGS, cv, 1);
665 arg_rows = atoi(short2str(*v));
671 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
673 (void) tputs(tgoto(scap, arg_cols, arg_rows), 1, PUTRAW);
676 /* This is wrong, but I will ignore it... */
678 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
682 if (!*v || *v[0] == '\0') {
686 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
688 arg_cols = atoi(short2str(*v));
690 if (!*v || *v[0] == '\0') {
694 stderror(ERR_NAME | ERR_TCNARGS, cv, 2);
696 arg_rows = atoi(short2str(*v));
702 stderror(ERR_NAME | ERR_TCARGS, cv, arg_need);
704 (void) tputs(tgoto(scap, arg_cols, arg_rows), arg_rows, PUTRAW);
710 cleanup_until(globbed);
722 { STRdown, T_kd, { 0 }, 0 },
724 { STRup, T_ku, { 0 }, 0 },
726 { STRleft, T_kl, { 0 }, 0 },
728 { STRright, T_kr, { 0 }, 0 },
730 { STRhome, T_kh, { 0 }, 0 },
732 { STRend, T_at7, { 0 }, 0}
739 arrow[A_K_DN].fun.cmd = F_DOWN_HIST;
740 arrow[A_K_DN].type = XK_CMD;
742 arrow[A_K_UP].fun.cmd = F_UP_HIST;
743 arrow[A_K_UP].type = XK_CMD;
745 arrow[A_K_LT].fun.cmd = F_CHARBACK;
746 arrow[A_K_LT].type = XK_CMD;
748 arrow[A_K_RT].fun.cmd = F_CHARFWD;
749 arrow[A_K_RT].type = XK_CMD;
751 arrow[A_K_HO].fun.cmd = F_TOBEG;
752 arrow[A_K_HO].type = XK_CMD;
754 arrow[A_K_EN].fun.cmd = F_TOEND;
755 arrow[A_K_EN].type = XK_CMD;
759 DefaultArrowKeys(void)
761 static Char strA[] = {033, '[', 'A', '\0'};
762 static Char strB[] = {033, '[', 'B', '\0'};
763 static Char strC[] = {033, '[', 'C', '\0'};
764 static Char strD[] = {033, '[', 'D', '\0'};
765 static Char strH[] = {033, '[', 'H', '\0'};
766 static Char strF[] = {033, '[', 'F', '\0'};
767 static Char stOA[] = {033, 'O', 'A', '\0'};
768 static Char stOB[] = {033, 'O', 'B', '\0'};
769 static Char stOC[] = {033, 'O', 'C', '\0'};
770 static Char stOD[] = {033, 'O', 'D', '\0'};
771 static Char stOH[] = {033, 'O', 'H', '\0'};
772 static Char stOF[] = {033, 'O', 'F', '\0'};
778 strA[0] = CTL_ESC('\033');
779 strB[0] = CTL_ESC('\033');
780 strC[0] = CTL_ESC('\033');
781 strD[0] = CTL_ESC('\033');
782 strH[0] = CTL_ESC('\033');
783 strF[0] = CTL_ESC('\033');
784 stOA[0] = CTL_ESC('\033');
785 stOB[0] = CTL_ESC('\033');
786 stOC[0] = CTL_ESC('\033');
787 stOD[0] = CTL_ESC('\033');
788 stOH[0] = CTL_ESC('\033');
789 stOF[0] = CTL_ESC('\033');
795 cs.buf = strA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
796 cs.buf = strB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
797 cs.buf = strC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
798 cs.buf = strD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
799 cs.buf = strH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
800 cs.buf = strF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
801 cs.buf = stOA; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
802 cs.buf = stOB; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
803 cs.buf = stOC; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
804 cs.buf = stOD; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
805 cs.buf = stOH; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
806 cs.buf = stOF; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
810 cs.buf = &strA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
811 cs.buf = &strB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
812 cs.buf = &strC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
813 cs.buf = &strD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
814 cs.buf = &strH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
815 cs.buf = &strF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
816 cs.buf = &stOA[1]; AddXkey(&cs, &arrow[A_K_UP].fun, arrow[A_K_UP].type);
817 cs.buf = &stOB[1]; AddXkey(&cs, &arrow[A_K_DN].fun, arrow[A_K_DN].type);
818 cs.buf = &stOC[1]; AddXkey(&cs, &arrow[A_K_RT].fun, arrow[A_K_RT].type);
819 cs.buf = &stOD[1]; AddXkey(&cs, &arrow[A_K_LT].fun, arrow[A_K_LT].type);
820 cs.buf = &stOH[1]; AddXkey(&cs, &arrow[A_K_HO].fun, arrow[A_K_HO].type);
821 cs.buf = &stOF[1]; AddXkey(&cs, &arrow[A_K_EN].fun, arrow[A_K_EN].type);
827 SetArrowKeys(const CStr *name, XmapVal *fun, int type)
830 for (i = 0; i < A_K_NKEYS; i++)
831 if (Strcmp(name->buf, arrow[i].name) == 0) {
833 arrow[i].type = type;
840 IsArrowKey(Char *name)
843 for (i = 0; i < A_K_NKEYS; i++)
844 if (Strcmp(name, arrow[i].name) == 0)
850 ClearArrowKeys(const CStr *name)
853 for (i = 0; i < A_K_NKEYS; i++)
854 if (Strcmp(name->buf, arrow[i].name) == 0) {
855 arrow[i].type = XK_NOD;
862 PrintArrowKeys(const CStr *name)
866 for (i = 0; i < A_K_NKEYS; i++)
867 if (name->len == 0 || Strcmp(name->buf, arrow[i].name) == 0)
868 if (arrow[i].type != XK_NOD)
869 printOne(arrow[i].name, &arrow[i].fun, arrow[i].type);
883 map = VImode ? CcAltMap : CcKeyMap;
884 dmap = VImode ? CcViCmdMap : CcEmacsMap;
888 for (i = 0; i < A_K_NKEYS; i++) {
889 p = tstr[arrow[i].key].str;
891 j = (unsigned char) *p;
892 cs.buf = str2short(p);
893 cs.len = Strlen(cs.buf);
895 * Assign the arrow keys only if:
897 * 1. They are multi-character arrow keys and the user
898 * has not re-assigned the leading character, or
899 * has re-assigned the leading character to be F_XKEY
900 * 2. They are single arrow keys pointing to an unassigned key.
902 if (arrow[i].type == XK_NOD) {
906 if (p[1] && (dmap[j] == map[j] || map[j] == F_XKEY)) {
907 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
910 else if (map[j] == F_UNASSIGNED) {
912 if (arrow[i].type == XK_CMD)
913 map[j] = arrow[i].fun.cmd;
915 AddXkey(&cs, &arrow[i].fun, arrow[i].type);
922 static Char cur_atr = 0; /* current attributes */
925 SetAttributes(Char atr)
928 if (atr != cur_atr) {
929 if (me_all && GoodStr(T_me)) {
930 if (((cur_atr & BOLD) && !(atr & BOLD)) ||
931 ((cur_atr & UNDER) && !(atr & UNDER)) ||
932 ((cur_atr & STANDOUT) && !(atr & STANDOUT))) {
933 (void) tputs(Str(T_me), 1, PUTPURE);
937 if ((atr & BOLD) != (cur_atr & BOLD)) {
939 if (GoodStr(T_md) && GoodStr(T_me)) {
940 (void) tputs(Str(T_md), 1, PUTPURE);
945 if (GoodStr(T_md) && GoodStr(T_me)) {
946 (void) tputs(Str(T_me), 1, PUTPURE);
947 if ((cur_atr & STANDOUT) && GoodStr(T_se)) {
948 (void) tputs(Str(T_se), 1, PUTPURE);
949 cur_atr &= ~STANDOUT;
951 if ((cur_atr & UNDER) && GoodStr(T_ue)) {
952 (void) tputs(Str(T_ue), 1, PUTPURE);
959 if ((atr & STANDOUT) != (cur_atr & STANDOUT)) {
960 if (atr & STANDOUT) {
961 if (GoodStr(T_so) && GoodStr(T_se)) {
962 (void) tputs(Str(T_so), 1, PUTPURE);
968 (void) tputs(Str(T_se), 1, PUTPURE);
969 cur_atr &= ~STANDOUT;
973 if ((atr & UNDER) != (cur_atr & UNDER)) {
975 if (GoodStr(T_us) && GoodStr(T_ue)) {
976 (void) tputs(Str(T_us), 1, PUTPURE);
982 (void) tputs(Str(T_ue), 1, PUTPURE);
990 int highlighting = 0;
995 (void) tputs(Str(T_mr), 1, PUTPURE);
1002 (void) tputs(Str(T_me), 1, PUTPURE);
1006 /* PWP 6-27-88 -- if the tty driver thinks that we can tab, we ask termcap */
1013 /* move to line <where> (first line == 0) as efficiently as possible; */
1015 MoveToLine(int where)
1019 if (where == CursorV)
1022 if (where > TermV) {
1024 xprintf("MoveToLine: where is ridiculous: %d\r\n", where);
1026 #endif /* DEBUG_SCREEN */
1030 del = where - CursorV;
1034 if ((T_Margin & MARGIN_AUTO) && Display[CursorV][0] != '\0') {
1037 for (h = TermH - 1; h > 0 && Display[CursorV][h] == CHAR_DBWIDTH;
1040 /* move without newline */
1042 so_write(&Display[CursorV][CursorH], TermH - CursorH); /* updates CursorH/V*/
1046 if ((del > 1) && GoodStr(T_DO)) {
1047 (void) tputs(tgoto(Str(T_DO), del, del), del, PUTPURE);
1051 for ( ; del > 0; del--)
1052 (void) putraw('\n');
1053 CursorH = 0; /* because the \n will become \r\n */
1058 else { /* del < 0 */
1059 if (GoodStr(T_UP) && (-del > 1 || !GoodStr(T_up)))
1060 (void) tputs(tgoto(Str(T_UP), -del, -del), -del, PUTPURE);
1064 for (i = 0; i < -del; i++)
1065 (void) tputs(Str(T_up), 1, PUTPURE);
1068 CursorV = where; /* now where is here */
1072 MoveToChar(int where) /* move to character position (where) */
1073 { /* as efficiently as possible */
1077 if (where == CursorH)
1080 if (where >= TermH) {
1082 xprintf("MoveToChar: where is riduculous: %d\r\n", where);
1084 #endif /* DEBUG_SCREEN */
1088 if (!where) { /* if where is first column */
1089 (void) putraw('\r'); /* do a CR */
1094 del = where - CursorH;
1096 if ((del < -4 || del > 4) && GoodStr(T_ch))
1097 /* go there directly */
1098 (void) tputs(tgoto(Str(T_ch), where, where), where, PUTPURE);
1101 if (del > 0) { /* moving forward */
1102 if ((del > 4) && GoodStr(T_RI))
1103 (void) tputs(tgoto(Str(T_RI), del, del), del, PUTPURE);
1105 /* if I can do tabs, use them */
1107 if ((CursorH & 0370) != (where & ~0x7)
1108 && Display[CursorV][where & ~0x7] != CHAR_DBWIDTH) {
1109 /* if not within tab stop */
1110 for (i = (CursorH & 0370); i < (where & ~0x7); i += 8)
1111 (void) putraw('\t'); /* then tab over */
1112 CursorH = where & ~0x7;
1113 /* Note: considering that we often want to go to
1114 TermH - 1 for the wrapping, it would be nice to
1115 optimize this case by tabbing to the last column
1116 - but this doesn't work for all terminals! */
1119 /* it's usually cheaper to just write the chars, so we do. */
1121 /* NOTE THAT so_write() WILL CHANGE CursorH!!! */
1122 so_write(&Display[CursorV][CursorH], where - CursorH);
1126 else { /* del < 0 := moving backward */
1127 if ((-del > 4) && GoodStr(T_LE))
1128 (void) tputs(tgoto(Str(T_LE), -del, -del), -del, PUTPURE);
1129 else { /* can't go directly there */
1130 /* if the "cost" is greater than the "cost" from col 0 */
1131 if (T_Tabs ? (-del > ((where >> 3) + (where & 07)))
1133 (void) putraw('\r'); /* do a CR */
1135 goto mc_again; /* and try again */
1137 for (i = 0; i < -del; i++)
1138 (void) putraw('\b');
1142 CursorH = where; /* now where is here */
1146 so_write(Char *cp, int n)
1148 int cur_pos, prompt_len = 0, region_start = 0, region_end = 0;
1151 return; /* catch bugs */
1155 xprintf("so_write: n is riduculous: %d\r\n", n);
1157 #endif /* DEBUG_SCREEN */
1161 if (adrof(STRhighlight)) {
1162 /* find length of prompt */
1164 for (promptc = Prompt; *promptc; promptc++);
1165 prompt_len = promptc - Prompt;
1167 /* find region start and end points */
1169 region_start = (Cursor - InputBuf) + prompt_len;
1170 region_end = region_start + IncMatchLen;
1171 } else if (MarkIsSet) {
1172 region_start = (min(Cursor, Mark) - InputBuf) + prompt_len;
1173 region_end = (max(Cursor, Mark) - InputBuf) + prompt_len;
1178 if (adrof(STRhighlight)) {
1179 cur_pos = CursorV * TermH + CursorH;
1180 if (!highlighting &&
1181 cur_pos >= region_start && cur_pos < region_end)
1183 else if (highlighting && cur_pos >= region_end)
1186 /* don't highlight over the cursor. the highlighting's reverse
1187 * video would cancel it out. :P */
1188 if (highlighting && cur_pos == (Cursor - InputBuf) + prompt_len)
1192 if (*cp != CHAR_DBWIDTH) {
1193 if (*cp & LITERAL) {
1195 #ifdef DEBUG_LITERAL
1196 xprintf("so: litnum %d\r\n", (int)(*cp & ~LITERAL));
1197 #endif /* DEBUG_LITERAL */
1198 for (d = litptr + (*cp & ~LITERAL) * LIT_FACTOR; *d; d++)
1202 (void) putwraw(*cp);
1208 if (adrof(STRhighlight) && highlighting)
1211 if (CursorH >= TermH) { /* wrap? */
1212 if (T_Margin & MARGIN_AUTO) { /* yes */
1215 if (T_Margin & MARGIN_MAGIC) {
1216 /* force the wrap to avoid the "magic" situation */
1218 if ((xc = Display[CursorV][CursorH]) != '\0') {
1220 while(Display[CursorV][CursorH] == CHAR_DBWIDTH)
1229 else /* no wrap, but cursor stays on screen */
1230 CursorH = TermH - 1;
1236 DeleteChars(int num) /* deletes <num> characters */
1243 xprintf(CGETS(7, 16, "ERROR: cannot delete\r\n"));
1244 #endif /* DEBUG_EDIT */
1251 xprintf(CGETS(7, 17, "DeleteChars: num is riduculous: %d\r\n"), num);
1253 #endif /* DEBUG_SCREEN */
1257 if (GoodStr(T_DC)) /* if I have multiple delete */
1258 if ((num > 1) || !GoodStr(T_dc)) { /* if dc would be more expen. */
1259 (void) tputs(tgoto(Str(T_DC), num, num), num, PUTPURE);
1263 if (GoodStr(T_dm)) /* if I have delete mode */
1264 (void) tputs(Str(T_dm), 1, PUTPURE);
1266 if (GoodStr(T_dc)) /* else do one at a time */
1268 (void) tputs(Str(T_dc), 1, PUTPURE);
1270 if (GoodStr(T_ed)) /* if I have delete mode */
1271 (void) tputs(Str(T_ed), 1, PUTPURE);
1274 /* Puts terminal in insert character mode, or inserts num characters in the
1277 Insert_write(Char *cp, int num)
1283 xprintf(CGETS(7, 18, "ERROR: cannot insert\r\n"));
1284 #endif /* DEBUG_EDIT */
1291 xprintf(CGETS(7, 19, "StartInsert: num is riduculous: %d\r\n"), num);
1293 #endif /* DEBUG_SCREEN */
1297 if (GoodStr(T_IC)) /* if I have multiple insert */
1298 if ((num > 1) || !GoodStr(T_ic)) { /* if ic would be more expen. */
1299 (void) tputs(tgoto(Str(T_IC), num, num), num, PUTPURE);
1300 so_write(cp, num); /* this updates CursorH/V */
1304 if (GoodStr(T_im) && GoodStr(T_ei)) { /* if I have insert mode */
1305 (void) tputs(Str(T_im), 1, PUTPURE);
1307 so_write(cp, num); /* this updates CursorH/V */
1309 if (GoodStr(T_ip)) /* have to make num chars insert */
1310 (void) tputs(Str(T_ip), 1, PUTPURE);
1312 (void) tputs(Str(T_ei), 1, PUTPURE);
1317 if (GoodStr(T_ic)) /* have to make num chars insert */
1318 (void) tputs(Str(T_ic), 1, PUTPURE); /* insert a char */
1320 so_write(cp++, 1); /* this updates CursorH/V */
1322 if (GoodStr(T_ip)) /* have to make num chars insert */
1323 (void) tputs(Str(T_ip), 1, PUTPURE);/* pad the inserted char */
1329 /* clear to end of line. There are num characters to clear */
1338 if (T_CanCEOL && GoodStr(T_ce))
1339 (void) tputs(Str(T_ce), 1, PUTPURE);
1341 for (i = 0; i < num; i++)
1343 CursorH += num; /* have written num spaces */
1349 { /* clear the whole screen and home */
1351 /* send the clear screen code */
1352 (void) tputs(Str(T_cl), Val(T_li), PUTPURE);
1353 else if (GoodStr(T_ho) && GoodStr(T_cd)) {
1354 (void) tputs(Str(T_ho), Val(T_li), PUTPURE); /* home */
1355 /* clear to bottom of screen */
1356 (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1359 (void) putraw('\r');
1360 (void) putraw('\n');
1366 { /* produce a sound */
1368 if (adrof(STRnobeep))
1371 if (GoodStr(T_vb) && adrof(STRvisiblebell))
1372 (void) tputs(Str(T_vb), 1, PUTPURE); /* visible bell */
1373 else if (GoodStr(T_bl))
1374 /* what termcap says we should use */
1375 (void) tputs(Str(T_bl), 1, PUTPURE);
1377 (void) putraw(CTL_ESC('\007')); /* an ASCII bell; ^G */
1382 { /* clear to the bottom of the screen */
1384 (void) tputs(Str(T_cd), Val(T_li), PUTPURE);
1385 else if (GoodStr(T_ce))
1386 (void) tputs(Str(T_ce), Val(T_li), PUTPURE);
1391 { /* read in the needed terminal capabilites */
1394 char buf[TC_BUFSIZE];
1395 static char bp[TC_BUFSIZE];
1397 struct termcapstr *t;
1404 /* don't want to confuse things here */
1406 sigaddset(&set, SIG_WINDOW);
1407 (void)sigprocmask(SIG_BLOCK, &set, &oset);
1408 cleanup_push(&oset, sigprocmask_cleanup);
1409 #endif /* SIG_WINDOW */
1414 setname("gettermcaps");
1415 ptr = getenv("TERM");
1419 * If we are on a pad, we pretend that we are dumb. Otherwise the termcap
1420 * library will put us in a weird screen mode, thinking that we are going
1427 if (!ptr || !ptr[0] || !strcmp(ptr, "wm") || !strcmp(ptr,"dmx"))
1430 setzero(bp, TC_BUFSIZE);
1432 i = tgetent(bp, ptr);
1435 #if (SYSVREL == 0) || defined(IRIS3D)
1436 xprintf(CGETS(7, 20,
1437 "%s: The terminal database could not be opened.\n"), progname);
1440 #endif /* SYSVREL */
1441 xprintf(CGETS(7, 21,
1442 "%s: No entry for terminal type \"%s\"\n"), progname,
1445 xprintf(CGETS(7, 22, "%s: using dumb terminal settings.\n"), progname);
1446 Val(T_co) = 80; /* do a dumb terminal */
1447 Val(T_pt) = Val(T_km) = Val(T_li) = 0;
1448 for (t = tstr; t->name != NULL; t++)
1453 Val(T_pt) = tgetflag("pt") && !tgetflag("xt");
1454 /* do we have a meta? */
1455 Val(T_km) = (tgetflag("km") || tgetflag("MT"));
1456 Val(T_am) = tgetflag("am");
1457 Val(T_xn) = tgetflag("xn");
1458 Val(T_co) = tgetnum("co");
1459 Val(T_li) = tgetnum("li");
1460 for (t = tstr; t->name != NULL; t++)
1461 TCset(t, tgetstr(t->name, &area));
1464 Val(T_co) = 80; /* just in case */
1468 T_Cols = (Char) Val(T_co);
1469 T_Lines = (Char) Val(T_li);
1472 T_HasMeta = Val(T_km);
1473 T_Margin = Val(T_am) ? MARGIN_AUTO : 0;
1474 T_Margin |= Val(T_xn) ? MARGIN_MAGIC : 0;
1475 T_CanCEOL = GoodStr(T_ce);
1476 T_CanDel = GoodStr(T_dc) || GoodStr(T_DC);
1477 T_CanIns = GoodStr(T_im) || GoodStr(T_ic) || GoodStr(T_IC);
1478 T_CanUP = GoodStr(T_up) || GoodStr(T_UP);
1479 if (GoodStr(T_me) && GoodStr(T_ue))
1480 me_all = (strcmp(Str(T_me), Str(T_ue)) == 0);
1483 if (GoodStr(T_me) && GoodStr(T_se))
1484 me_all |= (strcmp(Str(T_me), Str(T_se)) == 0);
1489 xprintf(CGETS(7, 23, "%s: WARNING: Your terminal cannot move up.\n",
1491 xprintf(CGETS(7, 24, "Editing may be odd for long lines.\n"));
1494 xprintf(CGETS(7, 25, "no clear EOL capability.\n"));
1496 xprintf(CGETS(7, 26, "no delete char capability.\n"));
1498 xprintf(CGETS(7, 27, "no insert char capability.\n"));
1499 #endif /* DEBUG_SCREEN */
1504 (void) GetSize(&lins, &cols); /* get the correct window size */
1505 ChangeSize(lins, cols);
1507 cleanup_until(&oset); /* can change it again */
1508 #else /* SIG_WINDOW */
1509 ChangeSize(Val(T_li), Val(T_co));
1510 #endif /* SIG_WINDOW */
1517 * Return the new window size in lines and cols, and
1518 * true if the size was changed. This can fail if SHIN
1519 * is not a tty, but it will work in most cases.
1522 GetSize(int *lins, int *cols)
1531 struct winsize ws; /* from 4.3 */
1533 if (ioctl(SHIN, TIOCGWINSZ, (ioctl_t) &ws) != -1) {
1541 #else /* TIOCGWINSZ */
1545 struct ttysize ts; /* from Sun */
1547 if (ioctl(SHIN, TIOCGSIZE, (ioctl_t) &ts) != -1) {
1551 *lins = ts.ts_lines;
1554 # endif /* TIOCGSIZE */
1555 #endif /* TIOCGWINSZ */
1557 return (Val(T_co) != *cols || Val(T_li) != *lins);
1560 #endif /* SIG_WINDOW */
1564 UpdateVal(const Char *tag, int value, Char *termcap, Char *backup)
1567 if ((ptr = Strstr(termcap, tag)) == NULL) {
1568 (void)Strcpy(backup, termcap);
1571 size_t len = (ptr - termcap) + Strlen(tag);
1572 (void)Strncpy(backup, termcap, len);
1574 p = Itoa(value, 0, 0);
1575 (void) Strcat(backup + len, p);
1577 ptr = Strchr(ptr, ':');
1579 (void) Strcat(backup, ptr);
1586 ChangeSize(int lins, int cols)
1591 Val(T_co) = (cols < 2) ? 80 : cols;
1592 Val(T_li) = (lins < 1) ? 24 : lins;
1596 * We want to affect the environment only when we have a valid
1597 * setup, not when we get bad settings. Consider the following scenario:
1598 * We just logged in, and we have not initialized the editor yet.
1599 * We reset termcap with tset, and not $TERMCAP has the right
1600 * terminal size. But since the editor is not initialized yet, and
1601 * the kernel's notion of the terminal size might be wrong we arrive
1602 * here with lines = columns = 0. If we reset the environment we lose
1603 * our only chance to get the window size right.
1605 if (Val(T_co) == cols && Val(T_li) == lins) {
1609 if (getenv("COLUMNS")) {
1610 p = Itoa(Val(T_co), 0, 0);
1611 cleanup_push(p, xfree);
1612 tsetenv(STRCOLUMNS, p);
1616 if (getenv("LINES")) {
1617 p = Itoa(Val(T_li), 0, 0);
1618 cleanup_push(p, xfree);
1619 tsetenv(STRLINES, p);
1623 if ((tptr = getenv("TERMCAP")) != NULL) {
1624 /* Leave 64 characters slop in case we enlarge the termcap string */
1625 Char termcap[TC_BUFSIZE+64], backup[TC_BUFSIZE+64], *ptr;
1628 ptr = str2short(tptr);
1629 (void) Strncpy(termcap, ptr, TC_BUFSIZE);
1630 termcap[TC_BUFSIZE-1] = '\0';
1632 changed = UpdateVal(STRco, Val(T_co), termcap, backup);
1633 changed |= UpdateVal(STRli, Val(T_li), termcap, backup);
1637 * Chop the termcap string at TC_BUFSIZE-1 characters to avoid
1638 * core-dumps in the termcap routines
1640 termcap[TC_BUFSIZE - 1] = '\0';
1641 tsetenv(STRTERMCAP, termcap);
1645 #endif /* KNOWsize */
1647 ReBufferDisplay(); /* re-make display buffers */