Skip to content

Commit dda4dd9

Browse files
committed
At end of highlight, resend last 3 ansi sequences not just 1.
Don't resend invalid sequences originally discarded.
1 parent 0300ba1 commit dda4dd9

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

line.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,22 @@ static struct {
3333
int pfx_end; /* Number of chars in pfx */
3434
} linebuf;
3535

36+
/*
37+
* Buffer of ansi sequences which have been shifted off the left edge
38+
* of the screen.
39+
*/
3640
struct xbuffer shifted_ansi;
37-
struct xbuffer last_ansi;
41+
42+
/*
43+
* Ring buffer of last ansi sequences sent.
44+
* While sending a line, these will be resent at the end
45+
* of any hilighted string, to restore text modes.
46+
* {{ Not ideal, since we don't really know how many to resend. }}
47+
*/
48+
#define NUM_LAST_ANSIS 3
49+
static struct xbuffer last_ansi;
50+
static struct xbuffer last_ansis[NUM_LAST_ANSIS];
51+
static int curr_last_ansi;
3852

3953
public int size_linebuf = 0; /* Size of line buffer (and attr buffer) */
4054
static struct ansi_state *line_ansi = NULL;
@@ -123,6 +137,8 @@ struct ansi_state {
123137
public void
124138
init_line(VOID_PARAM)
125139
{
140+
int ax;
141+
126142
end_ansi_chars = lgetenv("LESSANSIENDCHARS");
127143
if (isnullenv(end_ansi_chars))
128144
end_ansi_chars = "m";
@@ -136,6 +152,9 @@ init_line(VOID_PARAM)
136152
size_linebuf = LINEBUF_SIZE;
137153
xbuf_init(&shifted_ansi);
138154
xbuf_init(&last_ansi);
155+
for (ax = 0; ax < NUM_LAST_ANSIS; ax++)
156+
xbuf_init(&last_ansis[ax]);
157+
curr_last_ansi = 0;
139158
}
140159

141160
/*
@@ -208,6 +227,8 @@ inc_end_column(w)
208227
public void
209228
prewind(VOID_PARAM)
210229
{
230+
int ax;
231+
211232
linebuf.print = 6; /* big enough for longest UTF-8 sequence */
212233
linebuf.pfx_end = 0;
213234
for (linebuf.end = 0; linebuf.end < linebuf.print; linebuf.end++)
@@ -231,6 +252,9 @@ prewind(VOID_PARAM)
231252
line_mark_attr = 0;
232253
xbuf_reset(&shifted_ansi);
233254
xbuf_reset(&last_ansi);
255+
for (ax = 0; ax < NUM_LAST_ANSIS; ax++)
256+
xbuf_reset(&last_ansis[ax]);
257+
curr_last_ansi = 0;
234258
}
235259

236260
/*
@@ -735,8 +759,13 @@ store_char(ch, a, rep, pos)
735759
}
736760
if (resend_last)
737761
{
738-
for (i = 0; i < last_ansi.end; i++)
739-
STORE_CHAR(last_ansi.data[i], AT_ANSI, NULL, pos);
762+
int ai;
763+
for (ai = 0; ai < NUM_LAST_ANSIS; ai++)
764+
{
765+
int ax = (curr_last_ansi + ai) % NUM_LAST_ANSIS;
766+
for (i = 0; i < last_ansis[ax].end; i++)
767+
STORE_CHAR(last_ansis[ax].data[i], AT_ANSI, NULL, pos);
768+
}
740769
}
741770
}
742771
#endif
@@ -1019,12 +1048,17 @@ store_ansi(ch, rep, pos)
10191048
STORE_CHAR(ch, AT_ANSI, rep, pos);
10201049
if (line_ansi->hlink)
10211050
hlink_in_line = 1;
1051+
xbuf_add(&last_ansi, ch);
10221052
break;
10231053
case ANSI_END:
10241054
if (!in_hilite)
10251055
STORE_CHAR(ch, AT_ANSI, rep, pos);
10261056
ansi_done(line_ansi);
10271057
line_ansi = NULL;
1058+
xbuf_add(&last_ansi, ch);
1059+
xbuf_set(&last_ansis[curr_last_ansi], &last_ansi);
1060+
xbuf_reset(&last_ansi);
1061+
curr_last_ansi = (curr_last_ansi + 1) % NUM_LAST_ANSIS;
10281062
break;
10291063
case ANSI_ERR:
10301064
if (!in_hilite)
@@ -1039,6 +1073,7 @@ store_ansi(ch, rep, pos)
10391073
} while (p > start && !IS_CSI_START(bch));
10401074
*end = (int) (p - start);
10411075
}
1076+
xbuf_reset(&last_ansi);
10421077
ansi_done(line_ansi);
10431078
line_ansi = NULL;
10441079
break;
@@ -1077,17 +1112,11 @@ do_append(ch, rep, pos)
10771112
{
10781113
line_ansi = ansi_start(ch);
10791114
if (line_ansi != NULL)
1080-
{
1081-
xbuf_reset(&last_ansi);
10821115
ansi_in_line = 1;
1083-
}
10841116
}
10851117

10861118
if (line_ansi != NULL)
1087-
{
1088-
xbuf_add(&last_ansi, ch);
10891119
return store_ansi(ch, rep, pos);
1090-
}
10911120

10921121
if (ch == '\b')
10931122
return store_bs(ch, rep, pos);

xbuf.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,15 @@ xbuf_pop(buf)
5959
return -1;
6060
return buf->data[--(buf->end)];
6161
}
62+
63+
public void
64+
xbuf_set(dst, src)
65+
struct xbuffer *dst;
66+
struct xbuffer *src;
67+
{
68+
int i;
69+
70+
xbuf_reset(dst);
71+
for (i = 0; i < src->end; i++)
72+
xbuf_add(dst, src->data[i]);
73+
}

0 commit comments

Comments
 (0)