Skip to content

Commit bb0ee4e

Browse files
committed
Color fixes. Don't send "ESC[m" unless --use-color is enabled.
Also let LINES/COLUMNS env variables override screen size derived from system.
1 parent c763f86 commit bb0ee4e

File tree

1 file changed

+75
-60
lines changed

1 file changed

+75
-60
lines changed

screen.c

Lines changed: 75 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public int missing_cap = 0; /* Some capability is missing */
227227
public char *kent = NULL; /* Keypad ENTER sequence */
228228

229229
static int attrmode = AT_NORMAL;
230+
static int attrcolor = -1;
230231
static int termcap_debug = -1;
231232
extern int binattr;
232233
extern int one_screen;
@@ -831,21 +832,21 @@ scrsize(VOID_PARAM)
831832
#endif
832833
#endif
833834

834-
if (sys_height > 0)
835-
sc_height = sys_height;
836-
else if ((s = lgetenv("LINES")) != NULL)
835+
if ((s = lgetenv("LINES")) != NULL)
837836
sc_height = atoi(s);
837+
else if (sys_height > 0)
838+
sc_height = sys_height;
838839
#if !MSDOS_COMPILER
839840
else if ((n = ltgetnum("li")) > 0)
840841
sc_height = n;
841842
#endif
842843
if (sc_height <= 0)
843844
sc_height = DEF_SC_HEIGHT;
844845

845-
if (sys_width > 0)
846-
sc_width = sys_width;
847-
else if ((s = lgetenv("COLUMNS")) != NULL)
846+
if ((s = lgetenv("COLUMNS")) != NULL)
848847
sc_width = atoi(s);
848+
else if (sys_width > 0)
849+
sc_width = sys_width;
849850
#if !MSDOS_COMPILER
850851
else if ((n = ltgetnum("co")) > 0)
851852
sc_width = n;
@@ -1593,6 +1594,19 @@ win32_deinit_term(VOID_PARAM)
15931594
#endif
15941595

15951596
#if !MSDOS_COMPILER
1597+
static void
1598+
do_tputs(str, affcnt, f_putc)
1599+
char *str;
1600+
int affcnt;
1601+
int (*f_putc)(int);
1602+
{
1603+
#if LESSTEST
1604+
putstr(str);
1605+
#else
1606+
tputs(str, affcnt, f_putc);
1607+
#endif
1608+
}
1609+
15961610
/*
15971611
* Like tputs but we handle $<...> delay strings here because
15981612
* some implementations of tputs don't perform delays correctly.
@@ -1617,7 +1631,7 @@ ltputs(str, affcnt, f_putc)
16171631
/* Output first part of string (before "$<"). */
16181632
memcpy(str2, str, slen);
16191633
str2[slen] = '\0';
1620-
tputs(str2, affcnt, f_putc);
1634+
do_tputs(str2, affcnt, f_putc);
16211635
str += slen + 2;
16221636
/* Perform the delay. */
16231637
delay = lstrtoi(str, &str);
@@ -1634,7 +1648,7 @@ ltputs(str, affcnt, f_putc)
16341648
}
16351649
#endif
16361650
/* Pass the rest of the string to tputs and we're done. */
1637-
tputs(str, affcnt, f_putc);
1651+
do_tputs(str, affcnt, f_putc);
16381652
break;
16391653
}
16401654
}
@@ -2494,82 +2508,96 @@ sgr_color(color)
24942508
}
24952509
}
24962510

2497-
static int
2498-
tput_fmt(fmt, val, f_putc)
2511+
static void
2512+
tput_fmt(fmt, color, f_putc)
24992513
char *fmt;
2500-
int val;
2514+
int color;
25012515
int (*f_putc)(int);
25022516
{
25032517
char buf[16];
2504-
SNPRINTF1(buf, sizeof(buf), fmt, val);
2518+
if (color == attrcolor)
2519+
return;
2520+
SNPRINTF1(buf, sizeof(buf), fmt, color);
25052521
ltputs(buf, 1, f_putc);
2506-
return TRUE;
2522+
attrcolor = color;
25072523
}
25082524

2509-
public int
2525+
static void
25102526
tput_color(str, f_putc)
25112527
char *str;
25122528
int (*f_putc)(int);
25132529
{
25142530
char buf[16];
25152531
int fg;
25162532
int bg;
2517-
int out = FALSE;
25182533

25192534
if (str != NULL && strcmp(str, "*") == 0)
25202535
{
25212536
/* Special case: reset to normal */
2522-
ltputs(ESCS"[m", 1, f_putc);
2523-
return TRUE;
2537+
tput_fmt(ESCS"[m", -1, f_putc);
2538+
return;
25242539
}
25252540
switch (parse_color(str, &fg, &bg))
25262541
{
25272542
case CT_4BIT:
25282543
if (fg >= 0)
2529-
out = tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc);
2544+
tput_fmt(ESCS"[%dm", sgr_color(fg), f_putc);
25302545
if (bg >= 0)
2531-
out = tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc);
2546+
tput_fmt(ESCS"[%dm", sgr_color(bg)+10, f_putc);
25322547
break;
25332548
case CT_6BIT:
25342549
if (fg >= 0)
2535-
out = tput_fmt(ESCS"[38;5;%dm", fg, f_putc);
2550+
tput_fmt(ESCS"[38;5;%dm", fg, f_putc);
25362551
if (bg >= 0)
2537-
out = tput_fmt(ESCS"[48;5;%dm", bg, f_putc);
2552+
tput_fmt(ESCS"[48;5;%dm", bg, f_putc);
25382553
break;
25392554
default:
25402555
break;
25412556
}
2542-
return out;
25432557
}
25442558

2545-
static int
2546-
tput_mode(mode_str, str, f_putc)
2559+
static void
2560+
tput_inmode(mode_str, attr, attr_bit, f_putc)
25472561
char *mode_str;
2548-
char *str;
2562+
int attr;
2563+
int attr_bit;
25492564
int (*f_putc)(int);
25502565
{
2551-
if (str == NULL || *str == '\0' || *str == '+')
2566+
char *color_str;
2567+
if ((attr & attr_bit) == 0)
2568+
return;
2569+
color_str = get_color_map(attr_bit);
2570+
if (color_str == NULL || *color_str == '\0' || *color_str == '+')
25522571
{
25532572
ltputs(mode_str, 1, f_putc);
2554-
if (*str != '+')
2555-
return TRUE;
2556-
str++;
2573+
if (color_str == NULL || *color_str++ != '+')
2574+
return;
25572575
}
25582576
/* Color overrides mode string */
2559-
return tput_color(str, f_putc);
2577+
tput_color(color_str, f_putc);
25602578
}
25612579

2580+
static void
2581+
tput_outmode(mode_str, attr_bit, f_putc)
2582+
char *mode_str;
2583+
int attr_bit;
2584+
int (*f_putc)(int);
2585+
{
2586+
if ((attrmode & attr_bit) == 0)
2587+
return;
2588+
ltputs(mode_str, 1, f_putc);
2589+
}
25622590

25632591
#else /* MSDOS_COMPILER */
25642592

25652593
#if MSDOS_COMPILER==WIN32C
25662594
static int
2567-
WIN32put_fmt(fmt, val)
2595+
WIN32put_fmt(fmt, color)
25682596
char *fmt;
2569-
int val;
2597+
int color;
25702598
{
25712599
char buf[16];
2572-
int len = SNPRINTF1(buf, sizeof(buf), fmt, val);
2600+
int len = SNPRINTF1(buf, sizeof(buf), fmt, color);
25732601
WIN32textout(buf, len);
25742602
return TRUE;
25752603
}
@@ -2626,67 +2654,54 @@ at_enter(attr)
26262654
int attr;
26272655
{
26282656
attr = apply_at_specials(attr);
2629-
26302657
#if !MSDOS_COMPILER
26312658
/* The one with the most priority is last. */
2632-
if ((attr & AT_UNDERLINE) && tput_mode(sc_u_in, get_color_map(AT_UNDERLINE), putchr))
2633-
attrmode |= AT_UNDERLINE;
2634-
if ((attr & AT_BOLD) && tput_mode(sc_b_in, get_color_map(AT_BOLD), putchr))
2635-
attrmode |= AT_BOLD;
2636-
if ((attr & AT_BLINK) && tput_mode(sc_bl_in, get_color_map(AT_BLINK), putchr))
2637-
attrmode |= AT_BLINK;
2659+
tput_inmode(sc_u_in, attr, AT_UNDERLINE, putchr);
2660+
tput_inmode(sc_b_in, attr, AT_BOLD, putchr);
2661+
tput_inmode(sc_bl_in, attr, AT_BLINK, putchr);
26382662
/* Don't use standout and color at the same time. */
2639-
if ((attr & AT_COLOR) && use_color && tput_color(get_color_map(attr), putchr))
2640-
attrmode |= (attr & AT_COLOR);
2641-
else if ((attr & AT_STANDOUT) && tput_mode(sc_s_in, get_color_map(AT_STANDOUT), putchr))
2642-
attrmode |= AT_STANDOUT;
2663+
if (use_color && (attr & AT_COLOR))
2664+
tput_color(get_color_map(attr), putchr);
2665+
else
2666+
tput_inmode(sc_s_in, attr, AT_STANDOUT, putchr);
26432667
#else
26442668
flush();
26452669
/* The one with the most priority is first. */
26462670
if ((attr & AT_COLOR) && use_color)
26472671
{
26482672
win_set_color(attr);
2649-
attrmode = AT_COLOR;
26502673
} else if (attr & AT_STANDOUT)
26512674
{
26522675
SETCOLORS(so_fg_color, so_bg_color);
2653-
attrmode = AT_STANDOUT;
26542676
} else if (attr & AT_BLINK)
26552677
{
26562678
SETCOLORS(bl_fg_color, bl_bg_color);
2657-
attrmode = AT_BLINK;
26582679
} else if (attr & AT_BOLD)
26592680
{
26602681
SETCOLORS(bo_fg_color, bo_bg_color);
2661-
attrmode = AT_BOLD;
26622682
} else if (attr & AT_UNDERLINE)
26632683
{
26642684
SETCOLORS(ul_fg_color, ul_bg_color);
2665-
attrmode = AT_UNDERLINE;
26662685
}
26672686
#endif
2687+
attrmode = attr;
26682688
}
26692689

26702690
public void
26712691
at_exit(VOID_PARAM)
26722692
{
26732693
#if !MSDOS_COMPILER
26742694
/* Undo things in the reverse order we did them. */
2675-
if ((attrmode & AT_COLOR) && tput_color("*", putchr))
2676-
attrmode &= ~AT_COLOR;
2677-
if ((attrmode & AT_STANDOUT) && tput_mode(sc_s_out, "*", putchr))
2678-
attrmode &= ~AT_STANDOUT;
2679-
if ((attrmode & AT_BLINK) && tput_mode(sc_bl_out, "*", putchr))
2680-
attrmode &= ~AT_BLINK;
2681-
if ((attrmode & AT_BOLD) && tput_mode(sc_b_out, "*", putchr))
2682-
attrmode &= ~AT_BOLD;
2683-
if ((attrmode & AT_UNDERLINE) && tput_mode(sc_u_out, "*", putchr))
2684-
attrmode &= ~AT_UNDERLINE;
2695+
tput_color("*", putchr);
2696+
tput_outmode(sc_s_out, AT_STANDOUT, putchr);
2697+
tput_outmode(sc_bl_out, AT_BLINK, putchr);
2698+
tput_outmode(sc_b_out, AT_BOLD, putchr);
2699+
tput_outmode(sc_u_out, AT_UNDERLINE, putchr);
26852700
#else
26862701
flush();
26872702
SETCOLORS(nm_fg_color, nm_bg_color);
2688-
attrmode = AT_NORMAL;
26892703
#endif
2704+
attrmode = AT_NORMAL;
26902705
}
26912706

26922707
public void

0 commit comments

Comments
 (0)