14 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
15 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
16 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
17 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
19 /* Product-order comparisons for rectangular block selection. */
20 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
21 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
23 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
24 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
28 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
29 #define CL_VT100 0x0002 /* VT100 */
30 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
31 #define CL_VT102 0x0008 /* VT102 */
32 #define CL_VT220 0x0010 /* VT220 */
33 #define CL_VT320 0x0020 /* VT320 */
34 #define CL_VT420 0x0040 /* VT420 */
35 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
36 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
37 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
38 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
39 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
41 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
42 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
43 #define TM_VT102 (TM_VT100AVO|CL_VT102)
44 #define TM_VT220 (TM_VT102|CL_VT220)
45 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
46 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
48 #define TM_PUTTY (0xFFFF)
50 #define UPDATE_DELAY ((TICKSPERSEC+49)/50)/* ticks to defer window update */
51 #define TBLINK_DELAY ((TICKSPERSEC*9+19)/20)/* ticks between text blinks*/
52 #define CBLINK_DELAY (CURSORBLINK) /* ticks between cursor blinks */
53 #define VBELL_DELAY (VBELL_TIMEOUT) /* visual bell timeout in ticks */
55 #define compatibility(x) \
56 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
57 term->termstate=TOPLEVEL; \
60 #define compatibility2(x,y) \
61 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
62 term->termstate=TOPLEVEL; \
66 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
68 char *EMPTY_WINDOW_TITLE
= "";
70 const char sco2ansicolour
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
72 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
73 const wchar_t sel_nl
[] = SEL_NL
;
76 * Fetch the character at a particular position in a line array,
77 * for purposes of `wordtype'. The reason this isn't just a simple
78 * array reference is that if the character we find is UCSWIDE,
79 * then we must look one space further to the left.
81 #define UCSGET(a, x) \
82 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
85 * Detect the various aliases of U+0020 SPACE.
87 #define IS_SPACE_CHR(chr) \
88 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
93 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
96 * Internal prototypes.
98 static void resizeline(Terminal
*, termline
*, int);
99 static termline
*lineptr(Terminal
*, int, int, int);
100 static void unlineptr(termline
*);
101 static void do_paint(Terminal
*, Context
, int);
102 static void erase_lots(Terminal
*, int, int, int);
103 static int find_last_nonempty_line(Terminal
*, tree234
*);
104 static void swap_screen(Terminal
*, int, int, int);
105 static void update_sbar(Terminal
*);
106 static void deselect(Terminal
*);
107 static void term_print_finish(Terminal
*);
108 static void scroll(Terminal
*, int, int, int, int);
109 #ifdef OPTIMISE_SCROLL
110 static void scroll_display(Terminal
*, int, int, int);
111 #endif /* OPTIMISE_SCROLL */
113 static termline
*newline(Terminal
*term
, int cols
, int bce
)
118 line
= snew(termline
);
119 line
->chars
= snewn(cols
, termchar
);
120 for (j
= 0; j
< cols
; j
++)
121 line
->chars
[j
] = (bce ? term
->erase_char
: term
->basic_erase_char
);
122 line
->cols
= line
->size
= cols
;
123 line
->lattr
= LATTR_NORM
;
124 line
->temporary
= FALSE
;
130 static void freeline(termline
*line
)
138 static void unlineptr(termline
*line
)
146 * Diagnostic function: verify that a termline has a correct
147 * combining character structure.
149 * This is a performance-intensive check, so it's no longer enabled
152 static void cc_check(termline
*line
)
154 unsigned char *flags
;
157 assert(line
->size
>= line
->cols
);
159 flags
= snewn(line
->size
, unsigned char);
161 for (i
= 0; i
< line
->size
; i
++)
162 flags
[i
] = (i
< line
->cols
);
164 for (i
= 0; i
< line
->cols
; i
++) {
166 while (line
->chars
[j
].cc_next
) {
167 j
+= line
->chars
[j
].cc_next
;
168 assert(j
>= line
->cols
&& j
< line
->size
);
177 assert(j
>= line
->cols
&& j
< line
->size
);
180 if (line
->chars
[j
].cc_next
)
181 j
+= line
->chars
[j
].cc_next
;
188 for (i
= 0; i
< line
->size
; i
++)
189 j
+= (flags
[i
] != 0);
191 assert(j
== line
->size
);
198 * Add a combining character to a character cell.
200 static void add_cc(termline
*line
, int col
, unsigned long chr
)
204 assert(col
>= 0 && col
< line
->cols
);
207 * Start by extending the cols array if the free list is empty.
209 if (!line
->cc_free
) {
211 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
212 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
214 while (n
< line
->size
) {
215 if (n
+1 < line
->size
)
216 line
->chars
[n
].cc_next
= 1;
218 line
->chars
[n
].cc_next
= 0;
224 * Now walk the cc list of the cell in question.
226 while (line
->chars
[col
].cc_next
)
227 col
+= line
->chars
[col
].cc_next
;
230 * `col' now points at the last cc currently in this cell; so
231 * we simply add another one.
233 newcc
= line
->cc_free
;
234 if (line
->chars
[newcc
].cc_next
)
235 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
238 line
->chars
[newcc
].cc_next
= 0;
239 line
->chars
[newcc
].chr
= chr
;
240 line
->chars
[col
].cc_next
= newcc
- col
;
248 * Clear the combining character list in a character cell.
250 static void clear_cc(termline
*line
, int col
)
252 int oldfree
, origcol
= col
;
254 assert(col
>= 0 && col
< line
->cols
);
256 if (!line
->chars
[col
].cc_next
)
257 return; /* nothing needs doing */
259 oldfree
= line
->cc_free
;
260 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
261 while (line
->chars
[col
].cc_next
)
262 col
+= line
->chars
[col
].cc_next
;
264 line
->chars
[col
].cc_next
= oldfree
- col
;
266 line
->chars
[col
].cc_next
= 0;
268 line
->chars
[origcol
].cc_next
= 0;
276 * Compare two character cells for equality. Special case required
277 * in do_paint() where we override what we expect the chr and attr
280 static int termchars_equal_override(termchar
*a
, termchar
*b
,
281 unsigned long bchr
, unsigned long battr
)
286 if ((a
->attr
&~ DATTR_MASK
) != (battr
&~ DATTR_MASK
))
288 while (a
->cc_next
|| b
->cc_next
) {
289 if (!a
->cc_next
|| !b
->cc_next
)
290 return FALSE
; /* one cc-list ends, other does not */
293 if (a
->chr
!= b
->chr
)
299 static int termchars_equal(termchar
*a
, termchar
*b
)
301 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
305 * Copy a character cell. (Requires a pointer to the destination
306 * termline, so as to access its free list.)
308 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
310 clear_cc(destline
, x
);
312 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
313 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
315 while (src
->cc_next
) {
317 add_cc(destline
, x
, src
->chr
);
326 * Move a character cell within its termline.
328 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
330 /* First clear the cc list from the original char, just in case. */
331 clear_cc(line
, dest
- line
->chars
);
333 /* Move the character cell and adjust its cc_next. */
334 *dest
= *src
; /* copy everything except cc-list */
336 dest
->cc_next
= src
->cc_next
- (dest
-src
);
338 /* Ensure the original cell doesn't have a cc list. */
347 * Compress and decompress a termline into an RLE-based format for
348 * storing in scrollback. (Since scrollback almost never needs to
349 * be modified and exists in huge quantities, this is a sensible
350 * tradeoff, particularly since it allows us to continue adding
351 * features to the main termchar structure without proportionally
352 * bloating the terminal emulator's memory footprint unless those
353 * features are in constant use.)
359 static void add(struct buf
*b
, unsigned char c
)
361 if (b
->len
>= b
->size
) {
362 b
->size
= (b
->len
* 3 / 2) + 512;
363 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
365 b
->data
[b
->len
++] = c
;
367 static int get(struct buf
*b
)
369 return b
->data
[b
->len
++];
371 static void makerle(struct buf
*b
, termline
*ldata
,
372 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
373 unsigned long *state
))
375 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
376 termchar
*c
= ldata
->chars
;
377 unsigned long state
= 0, oldstate
;
384 prevlen
= prevpos
= 0;
389 makeliteral(b
, c
++, &state
);
390 thislen
= b
->len
- thispos
;
391 if (thislen
== prevlen
&&
392 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
394 * This literal precisely matches the previous one.
395 * Turn it into a run if it's worthwhile.
397 * With one-byte literals, it costs us two bytes to
398 * encode a run, plus another byte to write the header
399 * to resume normal output; so a three-element run is
400 * neutral, and anything beyond that is unconditionally
401 * worthwhile. With two-byte literals or more, even a
404 if (thislen
> 1 || prev2
) {
408 * It's worth encoding a run. Start at prevpos,
409 * unless hdrsize==0 in which case we can back up
410 * another one and start by overwriting hdrpos.
413 hdrsize
--; /* remove the literal at prevpos */
417 prevpos
-= prevlen
;/* and possibly another one */
421 assert(prevpos
== hdrpos
+ 1);
423 b
->len
= prevpos
+prevlen
;
425 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
427 b
->len
= prevpos
+prevlen
+1;
429 * Terminate the previous run of ordinary
432 assert(hdrsize
>= 1 && hdrsize
<= 128);
433 b
->data
[hdrpos
] = hdrsize
- 1;
436 runlen
= prev2 ?
3 : 2;
438 while (n
> 0 && runlen
< 129) {
442 makeliteral(b
, c
, &state
);
443 tmplen
= b
->len
- tmppos
;
445 if (tmplen
!= thislen
||
446 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
448 break; /* run over */
453 assert(runlen
>= 2 && runlen
<= 129);
454 b
->data
[runpos
] = runlen
+ 0x80 - 2;
459 /* And ensure this run doesn't interfere with the next. */
460 prevlen
= prevpos
= 0;
466 * Just flag that the previous two literals were
467 * identical, in case we find a third identical one
468 * we want to turn into a run.
481 * This character isn't (yet) part of a run. Add it to
485 if (hdrsize
== 128) {
486 b
->data
[hdrpos
] = hdrsize
- 1;
490 prevlen
= prevpos
= 0;
499 assert(hdrsize
<= 128);
500 b
->data
[hdrpos
] = hdrsize
- 1;
505 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
508 * My encoding for characters is UTF-8-like, in that it stores
509 * 7-bit ASCII in one byte and uses high-bit-set bytes as
510 * introducers to indicate a longer sequence. However, it's
511 * unlike UTF-8 in that it doesn't need to be able to
512 * resynchronise, and therefore I don't want to waste two bits
513 * per byte on having recognisable continuation characters.
514 * Also I don't want to rule out the possibility that I may one
515 * day use values 0x80000000-0xFFFFFFFF for interesting
516 * purposes, so unlike UTF-8 I need a full 32-bit range.
517 * Accordingly, here is my encoding:
519 * 00000000-0000007F: 0xxxxxxx (but see below)
520 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
521 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
522 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
523 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
525 * (`Z' is like `x' but is always going to be zero since the
526 * values I'm encoding don't go above 2^32. In principle the
527 * five-byte form of the encoding could extend to 2^35, and
528 * there could be six-, seven-, eight- and nine-byte forms as
529 * well to allow up to 64-bit values to be encoded. But that's
530 * completely unnecessary for these purposes!)
532 * The encoding as written above would be very simple, except
533 * that 7-bit ASCII can occur in several different ways in the
534 * terminal data; sometimes it crops up in the D800 page
535 * (CSET_ASCII) but at other times it's in the 0000 page (real
536 * Unicode). Therefore, this encoding is actually _stateful_:
537 * the one-byte encoding of 00-7F actually indicates `reuse the
538 * upper three bytes of the last character', and to encode an
539 * absolute value of 00-7F you need to use the two-byte form
542 if ((c
->chr
& ~0x7F) == *state
) {
543 add(b
, (unsigned char)(c
->chr
& 0x7F));
544 } else if (c
->chr
< 0x4000) {
545 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
546 add(b
, (unsigned char)(c
->chr
& 0xFF));
547 } else if (c
->chr
< 0x200000) {
548 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
549 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
550 add(b
, (unsigned char)(c
->chr
& 0xFF));
551 } else if (c
->chr
< 0x10000000) {
552 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
553 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
554 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
555 add(b
, (unsigned char)(c
->chr
& 0xFF));
558 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
559 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
560 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
561 add(b
, (unsigned char)(c
->chr
& 0xFF));
563 *state
= c
->chr
& ~0xFF;
565 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
568 * My encoding for attributes is 16-bit-granular and assumes
569 * that the top bit of the word is never required. I either
570 * store a two-byte value with the top bit clear (indicating
571 * just that value), or a four-byte value with the top bit set
572 * (indicating the same value with its top bit clear).
574 * However, first I permute the bits of the attribute value, so
575 * that the eight bits of colour (four in each of fg and bg)
576 * which are never non-zero unless xterm 256-colour mode is in
577 * use are placed higher up the word than everything else. This
578 * ensures that attribute values remain 16-bit _unless_ the
579 * user uses extended colour.
581 unsigned attr
, colourbits
;
585 assert(ATTR_BGSHIFT
> ATTR_FGSHIFT
);
587 colourbits
= (attr
>> (ATTR_BGSHIFT
+ 4)) & 0xF;
589 colourbits
|= (attr
>> (ATTR_FGSHIFT
+ 4)) & 0xF;
591 attr
= (((attr
>> (ATTR_BGSHIFT
+ 8)) << (ATTR_BGSHIFT
+ 4)) |
592 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
593 attr
= (((attr
>> (ATTR_FGSHIFT
+ 8)) << (ATTR_FGSHIFT
+ 4)) |
594 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
596 attr
|= (colourbits
<< (32-9));
599 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
600 add(b
, (unsigned char)(attr
& 0xFF));
602 add(b
, (unsigned char)(((attr
>> 24) & 0x7F) | 0x80));
603 add(b
, (unsigned char)((attr
>> 16) & 0xFF));
604 add(b
, (unsigned char)((attr
>> 8) & 0xFF));
605 add(b
, (unsigned char)(attr
& 0xFF));
608 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
611 * For combining characters, I just encode a bunch of ordinary
612 * chars using makeliteral_chr, and terminate with a \0
613 * character (which I know won't come up as a combining char
616 * I don't use the stateful encoding in makeliteral_chr.
618 unsigned long zstate
;
627 makeliteral_chr(b
, c
, &zstate
);
632 makeliteral_chr(b
, &z
, &zstate
);
635 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
637 static unsigned char *compressline(termline
*ldata
)
639 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
642 * First, store the column count, 7 bits at a time, least
643 * significant `digit' first, with the high bit set on all but
649 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
652 add(b
, (unsigned char)(n
));
656 * Next store the lattrs; same principle.
659 int n
= ldata
->lattr
;
661 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
664 add(b
, (unsigned char)(n
));
668 * Now we store a sequence of separate run-length encoded
669 * fragments, each containing exactly as many symbols as there
670 * are columns in the ldata.
672 * All of these have a common basic format:
674 * - a byte 00-7F indicates that X+1 literals follow it
675 * - a byte 80-FF indicates that a single literal follows it
676 * and expects to be repeated (X-0x80)+2 times.
678 * The format of the `literals' varies between the fragments.
680 makerle(b
, ldata
, makeliteral_chr
);
681 makerle(b
, ldata
, makeliteral_attr
);
682 makerle(b
, ldata
, makeliteral_cc
);
685 * Diagnostics: ensure that the compressed data really does
686 * decompress to the right thing.
688 * This is a bit performance-heavy for production code.
691 #ifndef CHECK_SB_COMPRESSION
697 #ifdef DIAGNOSTIC_SB_COMPRESSION
698 for (i
= 0; i
< b
->len
; i
++) {
699 printf(" %02x ", b
->data
[i
]);
704 dcl
= decompressline(b
->data
, &dused
);
705 assert(b
->len
== dused
);
706 assert(ldata
->cols
== dcl
->cols
);
707 assert(ldata
->lattr
== dcl
->lattr
);
708 for (i
= 0; i
< ldata
->cols
; i
++)
709 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
711 #ifdef DIAGNOSTIC_SB_COMPRESSION
712 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
713 ldata
->cols
, 4 * ldata
->cols
, dused
,
714 (double)dused
/ (4 * ldata
->cols
));
720 #endif /* TERM_CC_DIAGS */
723 * Trim the allocated memory so we don't waste any, and return.
725 return sresize(b
->data
, b
->len
, unsigned char);
728 static void readrle(struct buf
*b
, termline
*ldata
,
729 void (*readliteral
)(struct buf
*b
, termchar
*c
,
730 termline
*ldata
, unsigned long *state
))
733 unsigned long state
= 0;
735 while (n
< ldata
->cols
) {
741 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
743 assert(n
< ldata
->cols
);
745 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
749 /* Just a sequence of consecutive literals. */
753 assert(n
< ldata
->cols
);
754 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
760 assert(n
== ldata
->cols
);
762 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
763 unsigned long *state
)
768 * 00000000-0000007F: 0xxxxxxx
769 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
770 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
771 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
772 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
777 c
->chr
= byte
| *state
;
778 } else if (byte
< 0xC0) {
779 c
->chr
= (byte
&~ 0xC0) << 8;
781 } else if (byte
< 0xE0) {
782 c
->chr
= (byte
&~ 0xE0) << 16;
783 c
->chr
|= get(b
) << 8;
785 } else if (byte
< 0xF0) {
786 c
->chr
= (byte
&~ 0xF0) << 24;
787 c
->chr
|= get(b
) << 16;
788 c
->chr
|= get(b
) << 8;
791 assert(byte
== 0xF0);
792 c
->chr
= get(b
) << 24;
793 c
->chr
|= get(b
) << 16;
794 c
->chr
|= get(b
) << 8;
797 *state
= c
->chr
& ~0xFF;
799 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
800 unsigned long *state
)
802 unsigned val
, attr
, colourbits
;
814 colourbits
= (val
>> (32-9)) & 0xFF;
815 attr
= (val
& ((1<<(32-9))-1));
817 attr
= (((attr
>> (ATTR_FGSHIFT
+ 4)) << (ATTR_FGSHIFT
+ 8)) |
818 (attr
& ((1 << (ATTR_FGSHIFT
+ 4))-1)));
819 attr
= (((attr
>> (ATTR_BGSHIFT
+ 4)) << (ATTR_BGSHIFT
+ 8)) |
820 (attr
& ((1 << (ATTR_BGSHIFT
+ 4))-1)));
822 attr
|= (colourbits
>> 4) << (ATTR_BGSHIFT
+ 4);
823 attr
|= (colourbits
& 0xF) << (ATTR_FGSHIFT
+ 4);
827 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
828 unsigned long *state
)
831 unsigned long zstate
;
832 int x
= c
- ldata
->chars
;
838 readliteral_chr(b
, &n
, ldata
, &zstate
);
841 add_cc(ldata
, x
, n
.chr
);
845 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
847 int ncols
, byte
, shift
;
848 struct buf buffer
, *b
= &buffer
;
855 * First read in the column count.
860 ncols
|= (byte
& 0x7F) << shift
;
862 } while (byte
& 0x80);
865 * Now create the output termline.
867 ldata
= snew(termline
);
868 ldata
->chars
= snewn(ncols
, termchar
);
869 ldata
->cols
= ldata
->size
= ncols
;
870 ldata
->temporary
= TRUE
;
874 * We must set all the cc pointers in ldata->chars to 0 right
875 * now, so that cc diagnostics that verify the integrity of the
876 * whole line will make sense while we're in the middle of
881 for (i
= 0; i
< ldata
->cols
; i
++)
882 ldata
->chars
[i
].cc_next
= 0;
886 * Now read in the lattr.
888 ldata
->lattr
= shift
= 0;
891 ldata
->lattr
|= (byte
& 0x7F) << shift
;
893 } while (byte
& 0x80);
896 * Now we read in each of the RLE streams in turn.
898 readrle(b
, ldata
, readliteral_chr
);
899 readrle(b
, ldata
, readliteral_attr
);
900 readrle(b
, ldata
, readliteral_cc
);
902 /* Return the number of bytes read, for diagnostic purposes. */
904 *bytes_used
= b
->len
;
910 * Resize a line to make it `cols' columns wide.
912 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
916 if (line
->cols
!= cols
) {
918 oldcols
= line
->cols
;
921 * This line is the wrong length, which probably means it
922 * hasn't been accessed since a resize. Resize it now.
924 * First, go through all the characters that will be thrown
925 * out in the resize (if we're shrinking the line) and
926 * return their cc lists to the cc free list.
928 for (i
= cols
; i
< oldcols
; i
++)
932 * If we're shrinking the line, we now bodily move the
933 * entire cc section from where it started to where it now
934 * needs to be. (We have to do this before the resize, so
935 * that the data we're copying is still there. However, if
936 * we're expanding, we have to wait until _after_ the
937 * resize so that the space we're copying into is there.)
940 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
941 (line
->size
- line
->cols
) * TSIZE
);
944 * Now do the actual resize, leaving the _same_ amount of
945 * cc space as there was to begin with.
947 line
->size
+= cols
- oldcols
;
948 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
952 * If we're expanding the line, _now_ we move the cc
956 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
957 (line
->size
- line
->cols
) * TSIZE
);
960 * Go through what's left of the original line, and adjust
961 * the first cc_next pointer in each list. (All the
962 * subsequent ones are still valid because they are
963 * relative offsets within the cc block.) Also do the same
964 * to the head of the cc_free list.
966 for (i
= 0; i
< oldcols
&& i
< cols
; i
++)
967 if (line
->chars
[i
].cc_next
)
968 line
->chars
[i
].cc_next
+= cols
- oldcols
;
970 line
->cc_free
+= cols
- oldcols
;
973 * And finally fill in the new space with erase chars. (We
974 * don't have to worry about cc lists here, because we
975 * _know_ the erase char doesn't have one.)
977 for (i
= oldcols
; i
< cols
; i
++)
978 line
->chars
[i
] = term
->basic_erase_char
;
987 * Get the number of lines in the scrollback.
989 static int sblines(Terminal
*term
)
991 int sblines
= count234(term
->scrollback
);
992 if (term
->erase_to_scrollback
&&
993 term
->alt_which
&& term
->alt_screen
) {
994 sblines
+= term
->alt_sblines
;
1000 * Retrieve a line of the screen or of the scrollback, according to
1001 * whether the y coordinate is non-negative or negative
1004 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
1011 whichtree
= term
->screen
;
1018 if (term
->erase_to_scrollback
&&
1019 term
->alt_which
&& term
->alt_screen
) {
1020 altlines
= term
->alt_sblines
;
1022 if (y
< -altlines
) {
1023 whichtree
= term
->scrollback
;
1024 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
1026 whichtree
= term
->alt_screen
;
1027 treeindex
= y
+ term
->alt_sblines
;
1028 /* treeindex = y + count234(term->alt_screen); */
1031 if (whichtree
== term
->scrollback
) {
1032 unsigned char *cline
= index234(whichtree
, treeindex
);
1033 line
= decompressline(cline
, NULL
);
1035 line
= index234(whichtree
, treeindex
);
1038 /* We assume that we don't screw up and retrieve something out of range. */
1040 fatalbox("line==NULL in terminal.c\n"
1041 "lineno=%d y=%d w=%d h=%d\n"
1042 "count(scrollback=%p)=%d\n"
1043 "count(screen=%p)=%d\n"
1044 "count(alt=%p)=%d alt_sblines=%d\n"
1045 "whichtree=%p treeindex=%d\n\n"
1046 "Please contact <putty@projects.tartarus.org> "
1047 "and pass on the above information.",
1048 lineno
, y
, term
->cols
, term
->rows
,
1049 term
->scrollback
, count234(term
->scrollback
),
1050 term
->screen
, count234(term
->screen
),
1051 term
->alt_screen
, count234(term
->alt_screen
), term
->alt_sblines
,
1052 whichtree
, treeindex
);
1054 assert(line
!= NULL
);
1056 resizeline(term
, line
, term
->cols
);
1057 /* FIXME: should we sort the compressed scrollback out here? */
1062 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
1063 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
1065 static void term_schedule_tblink(Terminal
*term
);
1066 static void term_schedule_cblink(Terminal
*term
);
1068 static void term_timer(void *ctx
, long now
)
1070 Terminal
*term
= (Terminal
*)ctx
;
1073 if (term
->tblink_pending
&& now
- term
->next_tblink
>= 0) {
1074 term
->tblinker
= !term
->tblinker
;
1075 term
->tblink_pending
= FALSE
;
1076 term_schedule_tblink(term
);
1080 if (term
->cblink_pending
&& now
- term
->next_cblink
>= 0) {
1081 term
->cblinker
= !term
->cblinker
;
1082 term
->cblink_pending
= FALSE
;
1083 term_schedule_cblink(term
);
1087 if (term
->in_vbell
&& now
- term
->vbell_end
>= 0) {
1088 term
->in_vbell
= FALSE
;
1093 (term
->window_update_pending
&& now
- term
->next_update
>= 0))
1097 static void term_schedule_update(Terminal
*term
)
1099 if (!term
->window_update_pending
) {
1100 term
->window_update_pending
= TRUE
;
1101 term
->next_update
= schedule_timer(UPDATE_DELAY
, term_timer
, term
);
1106 * Call this whenever the terminal window state changes, to queue
1109 static void seen_disp_event(Terminal
*term
)
1111 term
->seen_disp_event
= TRUE
; /* for scrollback-reset-on-activity */
1112 term_schedule_update(term
);
1116 * Call when the terminal's blinking-text settings change, or when
1117 * a text blink has just occurred.
1119 static void term_schedule_tblink(Terminal
*term
)
1121 if (term
->blink_is_real
) {
1122 if (!term
->tblink_pending
)
1123 term
->next_tblink
= schedule_timer(TBLINK_DELAY
, term_timer
, term
);
1124 term
->tblink_pending
= TRUE
;
1126 term
->tblinker
= 1; /* reset when not in use */
1127 term
->tblink_pending
= FALSE
;
1132 * Likewise with cursor blinks.
1134 static void term_schedule_cblink(Terminal
*term
)
1136 if (term
->blink_cur
&& term
->has_focus
) {
1137 if (!term
->cblink_pending
)
1138 term
->next_cblink
= schedule_timer(CBLINK_DELAY
, term_timer
, term
);
1139 term
->cblink_pending
= TRUE
;
1141 term
->cblinker
= 1; /* reset when not in use */
1142 term
->cblink_pending
= FALSE
;
1147 * Call to reset cursor blinking on new output.
1149 static void term_reset_cblink(Terminal
*term
)
1151 seen_disp_event(term
);
1153 term
->cblink_pending
= FALSE
;
1154 term_schedule_cblink(term
);
1158 * Call to begin a visual bell.
1160 static void term_schedule_vbell(Terminal
*term
, int already_started
,
1163 long ticks_already_gone
;
1165 if (already_started
)
1166 ticks_already_gone
= GETTICKCOUNT() - startpoint
;
1168 ticks_already_gone
= 0;
1170 if (ticks_already_gone
< VBELL_DELAY
) {
1171 term
->in_vbell
= TRUE
;
1172 term
->vbell_end
= schedule_timer(VBELL_DELAY
- ticks_already_gone
,
1175 term
->in_vbell
= FALSE
;
1180 * Set up power-on settings for the terminal.
1181 * If 'clear' is false, don't actually clear the primary screen, and
1182 * position the cursor below the last non-blank line (scrolling if
1185 static void power_on(Terminal
*term
, int clear
)
1187 term
->alt_x
= term
->alt_y
= 0;
1188 term
->savecurs
.x
= term
->savecurs
.y
= 0;
1189 term
->alt_savecurs
.x
= term
->alt_savecurs
.y
= 0;
1190 term
->alt_t
= term
->marg_t
= 0;
1191 if (term
->rows
!= -1)
1192 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
1194 term
->alt_b
= term
->marg_b
= 0;
1195 if (term
->cols
!= -1) {
1197 for (i
= 0; i
< term
->cols
; i
++)
1198 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1200 term
->alt_om
= term
->dec_om
= conf_get_int(term
->conf
, CONF_dec_om
);
1201 term
->alt_ins
= term
->insert
= FALSE
;
1202 term
->alt_wnext
= term
->wrapnext
=
1203 term
->save_wnext
= term
->alt_save_wnext
= FALSE
;
1204 term
->alt_wrap
= term
->wrap
= conf_get_int(term
->conf
, CONF_wrap_mode
);
1205 term
->alt_cset
= term
->cset
= term
->save_cset
= term
->alt_save_cset
= 0;
1206 term
->alt_utf
= term
->utf
= term
->save_utf
= term
->alt_save_utf
= 0;
1207 term
->utf_state
= 0;
1208 term
->alt_sco_acs
= term
->sco_acs
=
1209 term
->save_sco_acs
= term
->alt_save_sco_acs
= 0;
1210 term
->cset_attr
[0] = term
->cset_attr
[1] =
1211 term
->save_csattr
= term
->alt_save_csattr
= CSET_ASCII
;
1213 term
->in_vbell
= FALSE
;
1214 term
->cursor_on
= 1;
1215 term
->big_cursor
= 0;
1216 term
->default_attr
= term
->save_attr
=
1217 term
->alt_save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
1218 term
->term_editing
= term
->term_echoing
= FALSE
;
1219 term
->app_cursor_keys
= conf_get_int(term
->conf
, CONF_app_cursor
);
1220 term
->app_keypad_keys
= conf_get_int(term
->conf
, CONF_app_keypad
);
1221 term
->use_bce
= conf_get_int(term
->conf
, CONF_bce
);
1222 term
->blink_is_real
= conf_get_int(term
->conf
, CONF_blinktext
);
1223 term
->erase_char
= term
->basic_erase_char
;
1224 term
->alt_which
= 0;
1225 term_print_finish(term
);
1226 term
->xterm_mouse
= 0;
1227 set_raw_mouse_mode(term
->frontend
, FALSE
);
1228 term
->bracketed_paste
= FALSE
;
1231 for (i
= 0; i
< 256; i
++)
1232 term
->wordness
[i
] = conf_get_int_int(term
->conf
, CONF_wordness
, i
);
1235 swap_screen(term
, 1, FALSE
, FALSE
);
1236 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1237 swap_screen(term
, 0, FALSE
, FALSE
);
1239 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1240 term
->curs
.y
= find_last_nonempty_line(term
, term
->screen
) + 1;
1241 if (term
->curs
.y
== term
->rows
) {
1243 scroll(term
, 0, term
->rows
- 1, 1, TRUE
);
1249 term_schedule_tblink(term
);
1250 term_schedule_cblink(term
);
1254 * Force a screen update.
1256 void term_update(Terminal
*term
)
1260 term
->window_update_pending
= FALSE
;
1262 ctx
= get_ctx(term
->frontend
);
1264 int need_sbar_update
= term
->seen_disp_event
;
1265 if (term
->seen_disp_event
&& term
->scroll_on_disp
) {
1266 term
->disptop
= 0; /* return to main screen */
1267 term
->seen_disp_event
= 0;
1268 need_sbar_update
= TRUE
;
1271 if (need_sbar_update
)
1273 do_paint(term
, ctx
, TRUE
);
1274 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1280 * Called from front end when a keypress occurs, to trigger
1281 * anything magical that needs to happen in that situation.
1283 void term_seen_key_event(Terminal
*term
)
1286 * On any keypress, clear the bell overload mechanism
1287 * completely, on the grounds that large numbers of
1288 * beeps coming from deliberate key action are likely
1289 * to be intended (e.g. beeps from filename completion
1290 * blocking repeatedly).
1292 term
->beep_overloaded
= FALSE
;
1293 while (term
->beephead
) {
1294 struct beeptime
*tmp
= term
->beephead
;
1295 term
->beephead
= tmp
->next
;
1298 term
->beeptail
= NULL
;
1302 * Reset the scrollback on keypress, if we're doing that.
1304 if (term
->scroll_on_key
) {
1305 term
->disptop
= 0; /* return to main screen */
1306 seen_disp_event(term
);
1311 * Same as power_on(), but an external function.
1313 void term_pwron(Terminal
*term
, int clear
)
1315 power_on(term
, clear
);
1316 if (term
->ldisc
) /* cause ldisc to notice changes */
1317 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1323 static void set_erase_char(Terminal
*term
)
1325 term
->erase_char
= term
->basic_erase_char
;
1327 term
->erase_char
.attr
= (term
->curr_attr
&
1328 (ATTR_FGMASK
| ATTR_BGMASK
));
1332 * We copy a bunch of stuff out of the Conf structure into local
1333 * fields in the Terminal structure, to avoid the repeated tree234
1334 * lookups which would be involved in fetching them from the former
1337 void term_copy_stuff_from_conf(Terminal
*term
)
1339 term
->ansi_colour
= conf_get_int(term
->conf
, CONF_ansi_colour
);
1340 term
->arabicshaping
= conf_get_int(term
->conf
, CONF_arabicshaping
);
1341 term
->beep
= conf_get_int(term
->conf
, CONF_beep
);
1342 term
->bellovl
= conf_get_int(term
->conf
, CONF_bellovl
);
1343 term
->bellovl_n
= conf_get_int(term
->conf
, CONF_bellovl_n
);
1344 term
->bellovl_s
= conf_get_int(term
->conf
, CONF_bellovl_s
);
1345 term
->bellovl_t
= conf_get_int(term
->conf
, CONF_bellovl_t
);
1346 term
->bidi
= conf_get_int(term
->conf
, CONF_bidi
);
1347 term
->bksp_is_delete
= conf_get_int(term
->conf
, CONF_bksp_is_delete
);
1348 term
->blink_cur
= conf_get_int(term
->conf
, CONF_blink_cur
);
1349 term
->blinktext
= conf_get_int(term
->conf
, CONF_blinktext
);
1350 term
->cjk_ambig_wide
= conf_get_int(term
->conf
, CONF_cjk_ambig_wide
);
1351 term
->conf_height
= conf_get_int(term
->conf
, CONF_height
);
1352 term
->conf_width
= conf_get_int(term
->conf
, CONF_width
);
1353 term
->crhaslf
= conf_get_int(term
->conf
, CONF_crhaslf
);
1354 term
->erase_to_scrollback
= conf_get_int(term
->conf
, CONF_erase_to_scrollback
);
1355 term
->funky_type
= conf_get_int(term
->conf
, CONF_funky_type
);
1356 term
->lfhascr
= conf_get_int(term
->conf
, CONF_lfhascr
);
1357 term
->logflush
= conf_get_int(term
->conf
, CONF_logflush
);
1358 term
->logtype
= conf_get_int(term
->conf
, CONF_logtype
);
1359 term
->mouse_override
= conf_get_int(term
->conf
, CONF_mouse_override
);
1360 term
->nethack_keypad
= conf_get_int(term
->conf
, CONF_nethack_keypad
);
1361 term
->no_alt_screen
= conf_get_int(term
->conf
, CONF_no_alt_screen
);
1362 term
->no_applic_c
= conf_get_int(term
->conf
, CONF_no_applic_c
);
1363 term
->no_applic_k
= conf_get_int(term
->conf
, CONF_no_applic_k
);
1364 term
->no_dbackspace
= conf_get_int(term
->conf
, CONF_no_dbackspace
);
1365 term
->no_mouse_rep
= conf_get_int(term
->conf
, CONF_no_mouse_rep
);
1366 term
->no_remote_charset
= conf_get_int(term
->conf
, CONF_no_remote_charset
);
1367 term
->no_remote_resize
= conf_get_int(term
->conf
, CONF_no_remote_resize
);
1368 term
->no_remote_wintitle
= conf_get_int(term
->conf
, CONF_no_remote_wintitle
);
1369 term
->rawcnp
= conf_get_int(term
->conf
, CONF_rawcnp
);
1370 term
->rect_select
= conf_get_int(term
->conf
, CONF_rect_select
);
1371 term
->remote_qtitle_action
= conf_get_int(term
->conf
, CONF_remote_qtitle_action
);
1372 term
->rxvt_homeend
= conf_get_int(term
->conf
, CONF_rxvt_homeend
);
1373 term
->scroll_on_disp
= conf_get_int(term
->conf
, CONF_scroll_on_disp
);
1374 term
->scroll_on_key
= conf_get_int(term
->conf
, CONF_scroll_on_key
);
1375 term
->xterm_256_colour
= conf_get_int(term
->conf
, CONF_xterm_256_colour
);
1378 * Parse the control-character escapes in the configured
1379 * answerback string.
1382 char *answerback
= conf_get_str(term
->conf
, CONF_answerback
);
1383 int maxlen
= strlen(answerback
);
1385 term
->answerback
= snewn(maxlen
, char);
1386 term
->answerbacklen
= 0;
1388 while (*answerback
) {
1390 char c
= ctrlparse(answerback
, &n
);
1392 term
->answerback
[term
->answerbacklen
++] = c
;
1395 term
->answerback
[term
->answerbacklen
++] = *answerback
++;
1402 * When the user reconfigures us, we need to check the forbidden-
1403 * alternate-screen config option, disable raw mouse mode if the
1404 * user has disabled mouse reporting, and abandon a print job if
1405 * the user has disabled printing.
1407 void term_reconfig(Terminal
*term
, Conf
*conf
)
1410 * Before adopting the new config, check all those terminal
1411 * settings which control power-on defaults; and if they've
1412 * changed, we will modify the current state as well as the
1413 * default one. The full list is: Auto wrap mode, DEC Origin
1414 * Mode, BCE, blinking text, character classes.
1416 int reset_wrap
, reset_decom
, reset_bce
, reset_tblink
, reset_charclass
;
1419 reset_wrap
= (conf_get_int(term
->conf
, CONF_wrap_mode
) !=
1420 conf_get_int(conf
, CONF_wrap_mode
));
1421 reset_decom
= (conf_get_int(term
->conf
, CONF_dec_om
) !=
1422 conf_get_int(conf
, CONF_dec_om
));
1423 reset_bce
= (conf_get_int(term
->conf
, CONF_bce
) !=
1424 conf_get_int(conf
, CONF_bce
));
1425 reset_tblink
= (conf_get_int(term
->conf
, CONF_blinktext
) !=
1426 conf_get_int(conf
, CONF_blinktext
));
1427 reset_charclass
= 0;
1428 for (i
= 0; i
< 256; i
++)
1429 if (conf_get_int_int(term
->conf
, CONF_wordness
, i
) !=
1430 conf_get_int_int(conf
, CONF_wordness
, i
))
1431 reset_charclass
= 1;
1434 * If the bidi or shaping settings have changed, flush the bidi
1437 if (conf_get_int(term
->conf
, CONF_arabicshaping
) !=
1438 conf_get_int(conf
, CONF_arabicshaping
) ||
1439 conf_get_int(term
->conf
, CONF_bidi
) !=
1440 conf_get_int(conf
, CONF_bidi
)) {
1441 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1442 sfree(term
->pre_bidi_cache
[i
].chars
);
1443 sfree(term
->post_bidi_cache
[i
].chars
);
1444 term
->pre_bidi_cache
[i
].width
= -1;
1445 term
->pre_bidi_cache
[i
].chars
= NULL
;
1446 term
->post_bidi_cache
[i
].width
= -1;
1447 term
->post_bidi_cache
[i
].chars
= NULL
;
1451 conf_free(term
->conf
);
1452 term
->conf
= conf_copy(conf
);
1455 term
->alt_wrap
= term
->wrap
= conf_get_int(term
->conf
, CONF_wrap_mode
);
1457 term
->alt_om
= term
->dec_om
= conf_get_int(term
->conf
, CONF_dec_om
);
1459 term
->use_bce
= conf_get_int(term
->conf
, CONF_bce
);
1460 set_erase_char(term
);
1463 term
->blink_is_real
= conf_get_int(term
->conf
, CONF_blinktext
);
1465 if (reset_charclass
)
1466 for (i
= 0; i
< 256; i
++)
1467 term
->wordness
[i
] = conf_get_int_int(term
->conf
, CONF_wordness
, i
);
1469 if (conf_get_int(term
->conf
, CONF_no_alt_screen
))
1470 swap_screen(term
, 0, FALSE
, FALSE
);
1471 if (conf_get_int(term
->conf
, CONF_no_mouse_rep
)) {
1472 term
->xterm_mouse
= 0;
1473 set_raw_mouse_mode(term
->frontend
, 0);
1475 if (conf_get_int(term
->conf
, CONF_no_remote_charset
)) {
1476 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1477 term
->sco_acs
= term
->alt_sco_acs
= 0;
1480 if (!conf_get_str(term
->conf
, CONF_printer
)) {
1481 term_print_finish(term
);
1483 term_schedule_tblink(term
);
1484 term_schedule_cblink(term
);
1485 term_copy_stuff_from_conf(term
);
1489 * Clear the scrollback.
1491 void term_clrsb(Terminal
*term
)
1493 unsigned char *line
;
1495 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1496 sfree(line
); /* this is compressed data, not a termline */
1498 term
->tempsblines
= 0;
1499 term
->alt_sblines
= 0;
1504 * Initialise the terminal.
1506 Terminal
*term_init(Conf
*myconf
, struct unicode_data
*ucsdata
,
1512 * Allocate a new Terminal structure and initialise the fields
1515 term
= snew(Terminal
);
1516 term
->frontend
= frontend
;
1517 term
->ucsdata
= ucsdata
;
1518 term
->conf
= conf_copy(myconf
);
1519 term
->logctx
= NULL
;
1520 term
->compatibility_level
= TM_PUTTY
;
1521 strcpy(term
->id_string
, "\033[?6c");
1522 term
->cblink_pending
= term
->tblink_pending
= FALSE
;
1523 term
->paste_buffer
= NULL
;
1524 term
->paste_len
= 0;
1525 term
->last_paste
= 0;
1526 bufchain_init(&term
->inbuf
);
1527 bufchain_init(&term
->printer_buf
);
1528 term
->printing
= term
->only_printing
= FALSE
;
1529 term
->print_job
= NULL
;
1530 term
->vt52_mode
= FALSE
;
1531 term
->cr_lf_return
= FALSE
;
1532 term
->seen_disp_event
= FALSE
;
1533 term
->mouse_is_down
= FALSE
;
1534 term
->reset_132
= FALSE
;
1535 term
->cblinker
= term
->tblinker
= 0;
1536 term
->has_focus
= 1;
1537 term
->repeat_off
= FALSE
;
1538 term
->termstate
= TOPLEVEL
;
1539 term
->selstate
= NO_SELECTION
;
1542 term_copy_stuff_from_conf(term
);
1544 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1545 term
->tempsblines
= 0;
1546 term
->alt_sblines
= 0;
1548 term
->disptext
= NULL
;
1549 term
->dispcursx
= term
->dispcursy
= -1;
1552 term
->rows
= term
->cols
= -1;
1553 power_on(term
, TRUE
);
1554 term
->beephead
= term
->beeptail
= NULL
;
1555 #ifdef OPTIMISE_SCROLL
1556 term
->scrollhead
= term
->scrolltail
= NULL
;
1557 #endif /* OPTIMISE_SCROLL */
1559 term
->lastbeep
= FALSE
;
1560 term
->beep_overloaded
= FALSE
;
1561 term
->attr_mask
= 0xffffffff;
1562 term
->resize_fn
= NULL
;
1563 term
->resize_ctx
= NULL
;
1564 term
->in_term_out
= FALSE
;
1566 term
->ltemp_size
= 0;
1567 term
->wcFrom
= NULL
;
1569 term
->wcFromTo_size
= 0;
1571 term
->window_update_pending
= FALSE
;
1573 term
->bidi_cache_size
= 0;
1574 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1577 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1578 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1579 term
->basic_erase_char
.cc_next
= 0;
1580 term
->erase_char
= term
->basic_erase_char
;
1585 void term_free(Terminal
*term
)
1588 struct beeptime
*beep
;
1591 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1592 sfree(line
); /* compressed data, not a termline */
1593 freetree234(term
->scrollback
);
1594 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1596 freetree234(term
->screen
);
1597 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1599 freetree234(term
->alt_screen
);
1600 if (term
->disptext
) {
1601 for (i
= 0; i
< term
->rows
; i
++)
1602 freeline(term
->disptext
[i
]);
1604 sfree(term
->disptext
);
1605 while (term
->beephead
) {
1606 beep
= term
->beephead
;
1607 term
->beephead
= beep
->next
;
1610 bufchain_clear(&term
->inbuf
);
1612 printer_finish_job(term
->print_job
);
1613 bufchain_clear(&term
->printer_buf
);
1614 sfree(term
->paste_buffer
);
1616 sfree(term
->wcFrom
);
1619 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1620 sfree(term
->pre_bidi_cache
[i
].chars
);
1621 sfree(term
->post_bidi_cache
[i
].chars
);
1622 sfree(term
->post_bidi_cache
[i
].forward
);
1623 sfree(term
->post_bidi_cache
[i
].backward
);
1625 sfree(term
->pre_bidi_cache
);
1626 sfree(term
->post_bidi_cache
);
1630 expire_timer_context(term
);
1632 conf_free(term
->conf
);
1638 * Set up the terminal for a given size.
1640 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1643 termline
**newdisp
, *line
;
1644 int i
, j
, oldrows
= term
->rows
;
1646 int save_alt_which
= term
->alt_which
;
1648 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1649 newsavelines
== term
->savelines
)
1650 return; /* nothing to do */
1652 /* Behave sensibly if we're given zero (or negative) rows/cols */
1654 if (newrows
< 1) newrows
= 1;
1655 if (newcols
< 1) newcols
= 1;
1658 swap_screen(term
, 0, FALSE
, FALSE
);
1660 term
->alt_t
= term
->marg_t
= 0;
1661 term
->alt_b
= term
->marg_b
= newrows
- 1;
1663 if (term
->rows
== -1) {
1664 term
->scrollback
= newtree234(NULL
);
1665 term
->screen
= newtree234(NULL
);
1666 term
->tempsblines
= 0;
1671 * Resize the screen and scrollback. We only need to shift
1672 * lines around within our data structures, because lineptr()
1673 * will take care of resizing each individual line if
1676 * - If the new screen is longer, we shunt lines in from temporary
1677 * scrollback if possible, otherwise we add new blank lines at
1680 * - If the new screen is shorter, we remove any blank lines at
1681 * the bottom if possible, otherwise shunt lines above the cursor
1682 * to scrollback if possible, otherwise delete lines below the
1685 * - Then, if the new scrollback length is less than the
1686 * amount of scrollback we actually have, we must throw some
1689 sblen
= count234(term
->scrollback
);
1690 /* Do this loop to expand the screen if newrows > rows */
1691 assert(term
->rows
== count234(term
->screen
));
1692 while (term
->rows
< newrows
) {
1693 if (term
->tempsblines
> 0) {
1694 unsigned char *cline
;
1695 /* Insert a line from the scrollback at the top of the screen. */
1696 assert(sblen
>= term
->tempsblines
);
1697 cline
= delpos234(term
->scrollback
, --sblen
);
1698 line
= decompressline(cline
, NULL
);
1700 line
->temporary
= FALSE
; /* reconstituted line is now real */
1701 term
->tempsblines
-= 1;
1702 addpos234(term
->screen
, line
, 0);
1704 term
->savecurs
.y
+= 1;
1706 term
->alt_savecurs
.y
+= 1;
1708 /* Add a new blank line at the bottom of the screen. */
1709 line
= newline(term
, newcols
, FALSE
);
1710 addpos234(term
->screen
, line
, count234(term
->screen
));
1714 /* Do this loop to shrink the screen if newrows < rows */
1715 while (term
->rows
> newrows
) {
1716 if (term
->curs
.y
< term
->rows
- 1) {
1717 /* delete bottom row, unless it contains the cursor */
1718 line
= delpos234(term
->screen
, term
->rows
- 1);
1721 /* push top row to scrollback */
1722 line
= delpos234(term
->screen
, 0);
1723 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1725 term
->tempsblines
+= 1;
1727 term
->savecurs
.y
-= 1;
1729 term
->alt_savecurs
.y
-= 1;
1733 assert(term
->rows
== newrows
);
1734 assert(count234(term
->screen
) == newrows
);
1736 /* Delete any excess lines from the scrollback. */
1737 while (sblen
> newsavelines
) {
1738 line
= delpos234(term
->scrollback
, 0);
1742 if (sblen
< term
->tempsblines
)
1743 term
->tempsblines
= sblen
;
1744 assert(count234(term
->scrollback
) <= newsavelines
);
1745 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1748 /* Make a new displayed text buffer. */
1749 newdisp
= snewn(newrows
, termline
*);
1750 for (i
= 0; i
< newrows
; i
++) {
1751 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1752 for (j
= 0; j
< newcols
; j
++)
1753 newdisp
[i
]->chars
[j
].attr
= ATTR_INVALID
;
1755 if (term
->disptext
) {
1756 for (i
= 0; i
< oldrows
; i
++)
1757 freeline(term
->disptext
[i
]);
1759 sfree(term
->disptext
);
1760 term
->disptext
= newdisp
;
1761 term
->dispcursx
= term
->dispcursy
= -1;
1763 /* Make a new alternate screen. */
1764 newalt
= newtree234(NULL
);
1765 for (i
= 0; i
< newrows
; i
++) {
1766 line
= newline(term
, newcols
, TRUE
);
1767 addpos234(newalt
, line
, i
);
1769 if (term
->alt_screen
) {
1770 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1772 freetree234(term
->alt_screen
);
1774 term
->alt_screen
= newalt
;
1775 term
->alt_sblines
= 0;
1777 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1780 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1781 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1784 /* Check that the cursor positions are still valid. */
1785 if (term
->savecurs
.y
< 0)
1786 term
->savecurs
.y
= 0;
1787 if (term
->savecurs
.y
>= newrows
)
1788 term
->savecurs
.y
= newrows
- 1;
1789 if (term
->savecurs
.x
>= newcols
)
1790 term
->savecurs
.x
= newcols
- 1;
1791 if (term
->alt_savecurs
.y
< 0)
1792 term
->alt_savecurs
.y
= 0;
1793 if (term
->alt_savecurs
.y
>= newrows
)
1794 term
->alt_savecurs
.y
= newrows
- 1;
1795 if (term
->alt_savecurs
.x
>= newcols
)
1796 term
->alt_savecurs
.x
= newcols
- 1;
1797 if (term
->curs
.y
< 0)
1799 if (term
->curs
.y
>= newrows
)
1800 term
->curs
.y
= newrows
- 1;
1801 if (term
->curs
.x
>= newcols
)
1802 term
->curs
.x
= newcols
- 1;
1803 if (term
->alt_y
< 0)
1805 if (term
->alt_y
>= newrows
)
1806 term
->alt_y
= newrows
- 1;
1807 if (term
->alt_x
>= newcols
)
1808 term
->alt_x
= newcols
- 1;
1809 term
->alt_x
= term
->alt_y
= 0;
1810 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1812 term
->rows
= newrows
;
1813 term
->cols
= newcols
;
1814 term
->savelines
= newsavelines
;
1816 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1820 if (term
->resize_fn
)
1821 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1825 * Hand a function and context pointer to the terminal which it can
1826 * use to notify a back end of resizes.
1828 void term_provide_resize_fn(Terminal
*term
,
1829 void (*resize_fn
)(void *, int, int),
1832 term
->resize_fn
= resize_fn
;
1833 term
->resize_ctx
= resize_ctx
;
1834 if (resize_fn
&& term
->cols
> 0 && term
->rows
> 0)
1835 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1838 /* Find the bottom line on the screen that has any content.
1839 * If only the top line has content, returns 0.
1840 * If no lines have content, return -1.
1842 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1845 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1846 termline
*line
= index234(screen
, i
);
1848 for (j
= 0; j
< line
->cols
; j
++)
1849 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1851 if (j
!= line
->cols
) break;
1857 * Swap screens. If `reset' is TRUE and we have been asked to
1858 * switch to the alternate screen, we must bring most of its
1859 * configuration from the main screen and erase the contents of the
1860 * alternate screen completely. (This is even true if we're already
1861 * on it! Blame xterm.)
1863 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1870 reset
= FALSE
; /* do no weird resetting if which==0 */
1872 if (which
!= term
->alt_which
) {
1873 term
->alt_which
= which
;
1875 ttr
= term
->alt_screen
;
1876 term
->alt_screen
= term
->screen
;
1878 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1880 if (!reset
&& !keep_cur_pos
)
1881 term
->curs
.x
= term
->alt_x
;
1884 if (!reset
&& !keep_cur_pos
)
1885 term
->curs
.y
= term
->alt_y
;
1888 if (!reset
) term
->marg_t
= term
->alt_t
;
1891 if (!reset
) term
->marg_b
= term
->alt_b
;
1894 if (!reset
) term
->dec_om
= term
->alt_om
;
1897 if (!reset
) term
->wrap
= term
->alt_wrap
;
1900 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1901 term
->alt_wnext
= t
;
1903 if (!reset
) term
->insert
= term
->alt_ins
;
1906 if (!reset
) term
->cset
= term
->alt_cset
;
1909 if (!reset
) term
->utf
= term
->alt_utf
;
1912 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1913 term
->alt_sco_acs
= t
;
1915 tp
= term
->savecurs
;
1916 if (!reset
&& !keep_cur_pos
)
1917 term
->savecurs
= term
->alt_savecurs
;
1918 term
->alt_savecurs
= tp
;
1919 t
= term
->save_cset
;
1920 if (!reset
&& !keep_cur_pos
)
1921 term
->save_cset
= term
->alt_save_cset
;
1922 term
->alt_save_cset
= t
;
1923 t
= term
->save_csattr
;
1924 if (!reset
&& !keep_cur_pos
)
1925 term
->save_csattr
= term
->alt_save_csattr
;
1926 term
->alt_save_csattr
= t
;
1927 t
= term
->save_attr
;
1928 if (!reset
&& !keep_cur_pos
)
1929 term
->save_attr
= term
->alt_save_attr
;
1930 term
->alt_save_attr
= t
;
1932 if (!reset
&& !keep_cur_pos
)
1933 term
->save_utf
= term
->alt_save_utf
;
1934 term
->alt_save_utf
= t
;
1935 t
= term
->save_wnext
;
1936 if (!reset
&& !keep_cur_pos
)
1937 term
->save_wnext
= term
->alt_save_wnext
;
1938 term
->alt_save_wnext
= t
;
1939 t
= term
->save_sco_acs
;
1940 if (!reset
&& !keep_cur_pos
)
1941 term
->save_sco_acs
= term
->alt_save_sco_acs
;
1942 term
->alt_save_sco_acs
= t
;
1945 if (reset
&& term
->screen
) {
1947 * Yes, this _is_ supposed to honour background-colour-erase.
1949 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1954 * Update the scroll bar.
1956 static void update_sbar(Terminal
*term
)
1958 int nscroll
= sblines(term
);
1959 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1960 nscroll
+ term
->disptop
, term
->rows
);
1964 * Check whether the region bounded by the two pointers intersects
1965 * the scroll region, and de-select the on-screen selection if so.
1967 static void check_selection(Terminal
*term
, pos from
, pos to
)
1969 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1974 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1975 * for backward.) `sb' is TRUE if the scrolling is permitted to
1976 * affect the scrollback buffer.
1978 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1982 #ifdef OPTIMISE_SCROLL
1983 int olddisptop
, shift
;
1984 #endif /* OPTIMISE_SCROLL */
1986 if (topline
!= 0 || term
->alt_which
!= 0)
1989 #ifdef OPTIMISE_SCROLL
1990 olddisptop
= term
->disptop
;
1992 #endif /* OPTIMISE_SCROLL */
1995 line
= delpos234(term
->screen
, botline
);
1996 resizeline(term
, line
, term
->cols
);
1997 for (i
= 0; i
< term
->cols
; i
++)
1998 copy_termchar(line
, i
, &term
->erase_char
);
1999 line
->lattr
= LATTR_NORM
;
2000 addpos234(term
->screen
, line
, topline
);
2002 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
2004 if (term
->selstart
.y
> botline
) {
2005 term
->selstart
.y
= botline
+ 1;
2006 term
->selstart
.x
= 0;
2009 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
2011 if (term
->selend
.y
> botline
) {
2012 term
->selend
.y
= botline
+ 1;
2021 line
= delpos234(term
->screen
, topline
);
2022 #ifdef TERM_CC_DIAGS
2025 if (sb
&& term
->savelines
> 0) {
2026 int sblen
= count234(term
->scrollback
);
2028 * We must add this line to the scrollback. We'll
2029 * remove a line from the top of the scrollback if
2030 * the scrollback is full.
2032 if (sblen
== term
->savelines
) {
2033 unsigned char *cline
;
2036 cline
= delpos234(term
->scrollback
, 0);
2039 term
->tempsblines
+= 1;
2041 addpos234(term
->scrollback
, compressline(line
), sblen
);
2043 /* now `line' itself can be reused as the bottom line */
2046 * If the user is currently looking at part of the
2047 * scrollback, and they haven't enabled any options
2048 * that are going to reset the scrollback as a
2049 * result of this movement, then the chances are
2050 * they'd like to keep looking at the same line. So
2051 * we move their viewpoint at the same rate as the
2052 * scroll, at least until their viewpoint hits the
2053 * top end of the scrollback buffer, at which point
2054 * we don't have the choice any more.
2056 * Thanks to Jan Holmen Holsten for the idea and
2057 * initial implementation.
2059 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
2062 resizeline(term
, line
, term
->cols
);
2063 for (i
= 0; i
< term
->cols
; i
++)
2064 copy_termchar(line
, i
, &term
->erase_char
);
2065 line
->lattr
= LATTR_NORM
;
2066 addpos234(term
->screen
, line
, botline
);
2069 * If the selection endpoints move into the scrollback,
2070 * we keep them moving until they hit the top. However,
2071 * of course, if the line _hasn't_ moved into the
2072 * scrollback then we don't do this, and cut them off
2073 * at the top of the scroll region.
2075 * This applies to selstart and selend (for an existing
2076 * selection), and also selanchor (for one being
2077 * selected as we speak).
2079 seltop
= sb ?
-term
->savelines
: topline
;
2081 if (term
->selstate
!= NO_SELECTION
) {
2082 if (term
->selstart
.y
>= seltop
&&
2083 term
->selstart
.y
<= botline
) {
2085 if (term
->selstart
.y
< seltop
) {
2086 term
->selstart
.y
= seltop
;
2087 term
->selstart
.x
= 0;
2090 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
2092 if (term
->selend
.y
< seltop
) {
2093 term
->selend
.y
= seltop
;
2097 if (term
->selanchor
.y
>= seltop
&&
2098 term
->selanchor
.y
<= botline
) {
2099 term
->selanchor
.y
--;
2100 if (term
->selanchor
.y
< seltop
) {
2101 term
->selanchor
.y
= seltop
;
2102 term
->selanchor
.x
= 0;
2110 #ifdef OPTIMISE_SCROLL
2111 shift
+= term
->disptop
- olddisptop
;
2112 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
2113 scroll_display(term
, topline
, botline
, shift
);
2114 #endif /* OPTIMISE_SCROLL */
2117 #ifdef OPTIMISE_SCROLL
2119 * Add a scroll of a region on the screen into the pending scroll list.
2120 * `lines' is +ve for scrolling forward, -ve for backward.
2122 * If the scroll is on the same area as the last scroll in the list,
2125 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
2127 struct scrollregion
*newscroll
;
2128 if (term
->scrolltail
&&
2129 term
->scrolltail
->topline
== topline
&&
2130 term
->scrolltail
->botline
== botline
) {
2131 term
->scrolltail
->lines
+= lines
;
2133 newscroll
= snew(struct scrollregion
);
2134 newscroll
->topline
= topline
;
2135 newscroll
->botline
= botline
;
2136 newscroll
->lines
= lines
;
2137 newscroll
->next
= NULL
;
2139 if (!term
->scrollhead
)
2140 term
->scrollhead
= newscroll
;
2142 term
->scrolltail
->next
= newscroll
;
2143 term
->scrolltail
= newscroll
;
2148 * Scroll the physical display, and our conception of it in disptext.
2150 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
2152 int distance
, nlines
, i
, j
;
2154 distance
= lines
> 0 ? lines
: -lines
;
2155 nlines
= botline
- topline
+ 1 - distance
;
2157 for (i
= 0; i
< nlines
; i
++)
2158 for (j
= 0; j
< term
->cols
; j
++)
2159 copy_termchar(term
->disptext
[i
], j
,
2160 term
->disptext
[i
+distance
]->chars
+j
);
2161 if (term
->dispcursy
>= 0 &&
2162 term
->dispcursy
>= topline
+ distance
&&
2163 term
->dispcursy
< topline
+ distance
+ nlines
)
2164 term
->dispcursy
-= distance
;
2165 for (i
= 0; i
< distance
; i
++)
2166 for (j
= 0; j
< term
->cols
; j
++)
2167 term
->disptext
[nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
2169 for (i
= nlines
; i
-- ;)
2170 for (j
= 0; j
< term
->cols
; j
++)
2171 copy_termchar(term
->disptext
[i
+distance
], j
,
2172 term
->disptext
[i
]->chars
+j
);
2173 if (term
->dispcursy
>= 0 &&
2174 term
->dispcursy
>= topline
&&
2175 term
->dispcursy
< topline
+ nlines
)
2176 term
->dispcursy
+= distance
;
2177 for (i
= 0; i
< distance
; i
++)
2178 for (j
= 0; j
< term
->cols
; j
++)
2179 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
2181 save_scroll(term
, topline
, botline
, lines
);
2183 #endif /* OPTIMISE_SCROLL */
2186 * Move the cursor to a given position, clipping at boundaries. We
2187 * may or may not want to clip at the scroll margin: marg_clip is 0
2188 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
2189 * even _being_ outside the margins.
2191 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
2195 if (x
>= term
->cols
)
2198 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
2201 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
2207 if (y
>= term
->rows
)
2211 term
->wrapnext
= FALSE
;
2215 * Save or restore the cursor and SGR mode.
2217 static void save_cursor(Terminal
*term
, int save
)
2220 term
->savecurs
= term
->curs
;
2221 term
->save_attr
= term
->curr_attr
;
2222 term
->save_cset
= term
->cset
;
2223 term
->save_utf
= term
->utf
;
2224 term
->save_wnext
= term
->wrapnext
;
2225 term
->save_csattr
= term
->cset_attr
[term
->cset
];
2226 term
->save_sco_acs
= term
->sco_acs
;
2228 term
->curs
= term
->savecurs
;
2229 /* Make sure the window hasn't shrunk since the save */
2230 if (term
->curs
.x
>= term
->cols
)
2231 term
->curs
.x
= term
->cols
- 1;
2232 if (term
->curs
.y
>= term
->rows
)
2233 term
->curs
.y
= term
->rows
- 1;
2235 term
->curr_attr
= term
->save_attr
;
2236 term
->cset
= term
->save_cset
;
2237 term
->utf
= term
->save_utf
;
2238 term
->wrapnext
= term
->save_wnext
;
2240 * wrapnext might reset to False if the x position is no
2241 * longer at the rightmost edge.
2243 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
2244 term
->wrapnext
= FALSE
;
2245 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
2246 term
->sco_acs
= term
->save_sco_acs
;
2247 set_erase_char(term
);
2252 * This function is called before doing _anything_ which affects
2253 * only part of a line of text. It is used to mark the boundary
2254 * between two character positions, and it indicates that some sort
2255 * of effect is going to happen on only one side of that boundary.
2257 * The effect of this function is to check whether a CJK
2258 * double-width character is straddling the boundary, and to remove
2259 * it and replace it with two spaces if so. (Of course, one or
2260 * other of those spaces is then likely to be replaced with
2261 * something else again, as a result of whatever happens next.)
2263 * Also, if the boundary is at the right-hand _edge_ of the screen,
2264 * it implies something deliberate is being done to the rightmost
2265 * column position; hence we must clear LATTR_WRAPPED2.
2267 * The input to the function is the coordinates of the _second_
2268 * character of the pair.
2270 static void check_boundary(Terminal
*term
, int x
, int y
)
2274 /* Validate input coordinates, just in case. */
2275 if (x
== 0 || x
> term
->cols
)
2278 ldata
= scrlineptr(y
);
2279 if (x
== term
->cols
) {
2280 ldata
->lattr
&= ~LATTR_WRAPPED2
;
2282 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
2283 clear_cc(ldata
, x
-1);
2285 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
2286 ldata
->chars
[x
] = ldata
->chars
[x
-1];
2292 * Erase a large portion of the screen: the whole screen, or the
2293 * whole line, or parts thereof.
2295 static void erase_lots(Terminal
*term
,
2296 int line_only
, int from_begin
, int to_end
)
2300 int erasing_lines_from_top
= 0;
2303 start
.y
= term
->curs
.y
;
2305 end
.y
= term
->curs
.y
+ 1;
2307 erase_lattr
= FALSE
;
2322 if (!from_begin
|| !to_end
)
2323 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2324 check_selection(term
, start
, end
);
2326 /* Clear screen also forces a full window redraw, just in case. */
2327 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
2328 term_invalidate(term
);
2330 /* Lines scrolled away shouldn't be brought back on if the terminal
2332 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
2333 erasing_lines_from_top
= 1;
2335 if (term
->erase_to_scrollback
&& erasing_lines_from_top
) {
2336 /* If it's a whole number of lines, starting at the top, and
2337 * we're fully erasing them, erase by scrolling and keep the
2338 * lines in the scrollback. */
2339 int scrolllines
= end
.y
;
2340 if (end
.y
== term
->rows
) {
2341 /* Shrink until we find a non-empty row.*/
2342 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
2344 if (scrolllines
> 0)
2345 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
2347 termline
*ldata
= scrlineptr(start
.y
);
2348 while (poslt(start
, end
)) {
2349 if (start
.x
== term
->cols
) {
2351 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
2353 ldata
->lattr
= LATTR_NORM
;
2355 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
2357 if (incpos(start
) && start
.y
< term
->rows
) {
2358 ldata
= scrlineptr(start
.y
);
2363 /* After an erase of lines from the top of the screen, we shouldn't
2364 * bring the lines back again if the terminal enlarges (since the user or
2365 * application has explictly thrown them away). */
2366 if (erasing_lines_from_top
&& !(term
->alt_which
))
2367 term
->tempsblines
= 0;
2371 * Insert or delete characters within the current line. n is +ve if
2372 * insertion is desired, and -ve for deletion.
2374 static void insch(Terminal
*term
, int n
)
2376 int dir
= (n
< 0 ?
-1 : +1);
2381 n
= (n
< 0 ?
-n
: n
);
2382 if (n
> term
->cols
- term
->curs
.x
)
2383 n
= term
->cols
- term
->curs
.x
;
2384 m
= term
->cols
- term
->curs
.x
- n
;
2385 cursplus
.y
= term
->curs
.y
;
2386 cursplus
.x
= term
->curs
.x
+ n
;
2387 check_selection(term
, term
->curs
, cursplus
);
2388 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2390 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
2391 ldata
= scrlineptr(term
->curs
.y
);
2393 for (j
= 0; j
< m
; j
++)
2394 move_termchar(ldata
,
2395 ldata
->chars
+ term
->curs
.x
+ j
,
2396 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
2398 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
2401 move_termchar(ldata
,
2402 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
2403 ldata
->chars
+ term
->curs
.x
+ j
);
2405 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
2410 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2411 * whether the mode is a DEC private one or a normal one.)
2413 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
2417 case 1: /* DECCKM: application cursor keys */
2418 term
->app_cursor_keys
= state
;
2420 case 2: /* DECANM: VT52 mode */
2421 term
->vt52_mode
= !state
;
2422 if (term
->vt52_mode
) {
2423 term
->blink_is_real
= FALSE
;
2424 term
->vt52_bold
= FALSE
;
2426 term
->blink_is_real
= term
->blinktext
;
2428 term_schedule_tblink(term
);
2430 case 3: /* DECCOLM: 80/132 columns */
2432 if (!term
->no_remote_resize
)
2433 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2434 term
->reset_132
= state
;
2435 term
->alt_t
= term
->marg_t
= 0;
2436 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2437 move(term
, 0, 0, 0);
2438 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2440 case 5: /* DECSCNM: reverse video */
2442 * Toggle reverse video. If we receive an OFF within the
2443 * visual bell timeout period after an ON, we trigger an
2444 * effective visual bell, so that ESC[?5hESC[?5l will
2445 * always be an actually _visible_ visual bell.
2447 if (term
->rvideo
&& !state
) {
2448 /* This is an OFF, so set up a vbell */
2449 term_schedule_vbell(term
, TRUE
, term
->rvbell_startpoint
);
2450 } else if (!term
->rvideo
&& state
) {
2451 /* This is an ON, so we notice the time and save it. */
2452 term
->rvbell_startpoint
= GETTICKCOUNT();
2454 term
->rvideo
= state
;
2455 seen_disp_event(term
);
2457 case 6: /* DECOM: DEC origin mode */
2458 term
->dec_om
= state
;
2460 case 7: /* DECAWM: auto wrap */
2463 case 8: /* DECARM: auto key repeat */
2464 term
->repeat_off
= !state
;
2466 case 10: /* DECEDM: set local edit mode */
2467 term
->term_editing
= state
;
2468 if (term
->ldisc
) /* cause ldisc to notice changes */
2469 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2471 case 25: /* DECTCEM: enable/disable cursor */
2472 compatibility2(OTHER
, VT220
);
2473 term
->cursor_on
= state
;
2474 seen_disp_event(term
);
2476 case 47: /* alternate screen */
2477 compatibility(OTHER
);
2479 swap_screen(term
, term
->no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2482 case 1000: /* xterm mouse 1 (normal) */
2483 term
->xterm_mouse
= state ?
1 : 0;
2484 set_raw_mouse_mode(term
->frontend
, state
);
2486 case 1002: /* xterm mouse 2 (inc. button drags) */
2487 term
->xterm_mouse
= state ?
2 : 0;
2488 set_raw_mouse_mode(term
->frontend
, state
);
2490 case 1047: /* alternate screen */
2491 compatibility(OTHER
);
2493 swap_screen(term
, term
->no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2496 case 1048: /* save/restore cursor */
2497 if (!term
->no_alt_screen
)
2498 save_cursor(term
, state
);
2499 if (!state
) seen_disp_event(term
);
2501 case 1049: /* cursor & alternate screen */
2502 if (state
&& !term
->no_alt_screen
)
2503 save_cursor(term
, state
);
2504 if (!state
) seen_disp_event(term
);
2505 compatibility(OTHER
);
2507 swap_screen(term
, term
->no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2508 if (!state
&& !term
->no_alt_screen
)
2509 save_cursor(term
, state
);
2512 case 2004: /* xterm bracketed paste */
2513 term
->bracketed_paste
= state ? TRUE
: FALSE
;
2517 case 4: /* IRM: set insert mode */
2518 compatibility(VT102
);
2519 term
->insert
= state
;
2521 case 12: /* SRM: set echo mode */
2522 term
->term_echoing
= !state
;
2523 if (term
->ldisc
) /* cause ldisc to notice changes */
2524 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2526 case 20: /* LNM: Return sends ... */
2527 term
->cr_lf_return
= state
;
2529 case 34: /* WYULCURM: Make cursor BIG */
2530 compatibility2(OTHER
, VT220
);
2531 term
->big_cursor
= !state
;
2536 * Process an OSC sequence: set window title or icon name.
2538 static void do_osc(Terminal
*term
)
2541 while (term
->osc_strlen
--)
2542 term
->wordness
[(unsigned char)
2543 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2545 term
->osc_string
[term
->osc_strlen
] = '\0';
2546 switch (term
->esc_args
[0]) {
2549 if (!term
->no_remote_wintitle
)
2550 set_icon(term
->frontend
, term
->osc_string
);
2551 if (term
->esc_args
[0] == 1)
2553 /* fall through: parameter 0 means set both */
2556 if (!term
->no_remote_wintitle
)
2557 set_title(term
->frontend
, term
->osc_string
);
2564 * ANSI printing routines.
2566 static void term_print_setup(Terminal
*term
, char *printer
)
2568 bufchain_clear(&term
->printer_buf
);
2569 term
->print_job
= printer_start_job(printer
);
2571 static void term_print_flush(Terminal
*term
)
2576 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2577 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2580 printer_job_data(term
->print_job
, data
, len
);
2581 bufchain_consume(&term
->printer_buf
, len
);
2584 static void term_print_finish(Terminal
*term
)
2590 if (!term
->printing
&& !term
->only_printing
)
2591 return; /* we need do nothing */
2593 term_print_flush(term
);
2594 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2595 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2597 if (c
== '\033' || c
== '\233') {
2598 bufchain_consume(&term
->printer_buf
, size
);
2601 printer_job_data(term
->print_job
, &c
, 1);
2602 bufchain_consume(&term
->printer_buf
, 1);
2605 printer_finish_job(term
->print_job
);
2606 term
->print_job
= NULL
;
2607 term
->printing
= term
->only_printing
= FALSE
;
2611 * Remove everything currently in `inbuf' and stick it up on the
2612 * in-memory display. There's a big state machine in here to
2613 * process escape sequences...
2615 static void term_out(Terminal
*term
)
2619 unsigned char localbuf
[256], *chars
;
2624 chars
= NULL
; /* placate compiler warnings */
2625 while (nchars
> 0 || unget
!= -1 || bufchain_size(&term
->inbuf
) > 0) {
2629 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2630 if (nchars
> sizeof(localbuf
))
2631 nchars
= sizeof(localbuf
);
2632 memcpy(localbuf
, ret
, nchars
);
2633 bufchain_consume(&term
->inbuf
, nchars
);
2635 assert(chars
!= NULL
);
2641 * Optionally log the session traffic to a file. Useful for
2642 * debugging and possibly also useful for actual logging.
2644 if (term
->logtype
== LGTYP_DEBUG
&& term
->logctx
)
2645 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2651 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2652 * be able to display 8-bit characters, but I'll let that go 'cause
2657 * If we're printing, add the character to the printer
2660 if (term
->printing
) {
2661 bufchain_add(&term
->printer_buf
, &c
, 1);
2664 * If we're in print-only mode, we use a much simpler
2665 * state machine designed only to recognise the ESC[4i
2666 * termination sequence.
2668 if (term
->only_printing
) {
2670 term
->print_state
= 1;
2671 else if (c
== (unsigned char)'\233')
2672 term
->print_state
= 2;
2673 else if (c
== '[' && term
->print_state
== 1)
2674 term
->print_state
= 2;
2675 else if (c
== '4' && term
->print_state
== 2)
2676 term
->print_state
= 3;
2677 else if (c
== 'i' && term
->print_state
== 3)
2678 term
->print_state
= 4;
2680 term
->print_state
= 0;
2681 if (term
->print_state
== 4) {
2682 term_print_finish(term
);
2688 /* First see about all those translations. */
2689 if (term
->termstate
== TOPLEVEL
) {
2691 switch (term
->utf_state
) {
2694 /* UTF-8 must be stateless so we ignore iso2022. */
2695 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2696 c
= term
->ucsdata
->unitab_ctrl
[c
];
2697 else c
= ((unsigned char)c
) | CSET_ASCII
;
2699 } else if ((c
& 0xe0) == 0xc0) {
2700 term
->utf_size
= term
->utf_state
= 1;
2701 term
->utf_char
= (c
& 0x1f);
2702 } else if ((c
& 0xf0) == 0xe0) {
2703 term
->utf_size
= term
->utf_state
= 2;
2704 term
->utf_char
= (c
& 0x0f);
2705 } else if ((c
& 0xf8) == 0xf0) {
2706 term
->utf_size
= term
->utf_state
= 3;
2707 term
->utf_char
= (c
& 0x07);
2708 } else if ((c
& 0xfc) == 0xf8) {
2709 term
->utf_size
= term
->utf_state
= 4;
2710 term
->utf_char
= (c
& 0x03);
2711 } else if ((c
& 0xfe) == 0xfc) {
2712 term
->utf_size
= term
->utf_state
= 5;
2713 term
->utf_char
= (c
& 0x01);
2724 if ((c
& 0xC0) != 0x80) {
2727 term
->utf_state
= 0;
2730 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2731 if (--term
->utf_state
)
2736 /* Is somebody trying to be evil! */
2738 (c
< 0x800 && term
->utf_size
>= 2) ||
2739 (c
< 0x10000 && term
->utf_size
>= 3) ||
2740 (c
< 0x200000 && term
->utf_size
>= 4) ||
2741 (c
< 0x4000000 && term
->utf_size
>= 5))
2744 /* Unicode line separator and paragraph separator are CR-LF */
2745 if (c
== 0x2028 || c
== 0x2029)
2748 /* High controls are probably a Baaad idea too. */
2752 /* The UTF-16 surrogates are not nice either. */
2753 /* The standard give the option of decoding these:
2754 * I don't want to! */
2755 if (c
>= 0xD800 && c
< 0xE000)
2758 /* ISO 10646 characters now limited to UTF-16 range. */
2762 /* This is currently a TagPhobic application.. */
2763 if (c
>= 0xE0000 && c
<= 0xE007F)
2766 /* U+FEFF is best seen as a null. */
2769 /* But U+FFFE is an error. */
2770 if (c
== 0xFFFE || c
== 0xFFFF)
2775 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2776 else if(term
->sco_acs
&&
2777 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2779 if (term
->sco_acs
== 2) c
|= 0x80;
2782 switch (term
->cset_attr
[term
->cset
]) {
2784 * Linedraw characters are different from 'ESC ( B'
2785 * only for a small range. For ones outside that
2786 * range, make sure we use the same font as well as
2787 * the same encoding.
2790 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2791 c
= term
->ucsdata
->unitab_ctrl
[c
];
2793 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2797 /* If UK-ASCII, make the '#' a LineDraw Pound */
2799 c
= '}' | CSET_LINEDRW
;
2802 /*FALLTHROUGH*/ case CSET_ASCII
:
2803 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2804 c
= term
->ucsdata
->unitab_ctrl
[c
];
2806 c
= ((unsigned char) c
) | CSET_ASCII
;
2809 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2816 * How about C1 controls?
2817 * Explicitly ignore SCI (0x9a), which we don't translate to DECID.
2819 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2820 !term
->vt52_mode
&& has_compat(VT220
)) {
2824 term
->termstate
= SEEN_ESC
;
2825 term
->esc_query
= FALSE
;
2826 c
= '@' + (c
& 0x1F);
2830 /* Or the GL control. */
2831 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2832 if (term
->curs
.x
&& !term
->wrapnext
)
2834 term
->wrapnext
= FALSE
;
2835 /* destructive backspace might be disabled */
2836 if (!term
->no_dbackspace
) {
2837 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2838 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2839 copy_termchar(scrlineptr(term
->curs
.y
),
2840 term
->curs
.x
, &term
->erase_char
);
2843 /* Or normal C0 controls. */
2844 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2846 case '\005': /* ENQ: terminal type query */
2848 * Strictly speaking this is VT100 but a VT100 defaults to
2849 * no response. Other terminals respond at their option.
2851 * Don't put a CR in the default string as this tends to
2852 * upset some weird software.
2854 compatibility(ANSIMIN
);
2856 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2857 term
->answerback
, term
->answerbacklen
, 0);
2860 case '\007': /* BEL: Bell */
2862 struct beeptime
*newbeep
;
2863 unsigned long ticks
;
2865 ticks
= GETTICKCOUNT();
2867 if (!term
->beep_overloaded
) {
2868 newbeep
= snew(struct beeptime
);
2869 newbeep
->ticks
= ticks
;
2870 newbeep
->next
= NULL
;
2871 if (!term
->beephead
)
2872 term
->beephead
= newbeep
;
2874 term
->beeptail
->next
= newbeep
;
2875 term
->beeptail
= newbeep
;
2880 * Throw out any beeps that happened more than
2883 while (term
->beephead
&&
2884 term
->beephead
->ticks
< ticks
- term
->bellovl_t
) {
2885 struct beeptime
*tmp
= term
->beephead
;
2886 term
->beephead
= tmp
->next
;
2888 if (!term
->beephead
)
2889 term
->beeptail
= NULL
;
2893 if (term
->bellovl
&& term
->beep_overloaded
&&
2894 ticks
- term
->lastbeep
>= (unsigned)term
->bellovl_s
) {
2896 * If we're currently overloaded and the
2897 * last beep was more than s seconds ago,
2898 * leave overload mode.
2900 term
->beep_overloaded
= FALSE
;
2901 } else if (term
->bellovl
&& !term
->beep_overloaded
&&
2902 term
->nbeeps
>= term
->bellovl_n
) {
2904 * Now, if we have n or more beeps
2905 * remaining in the queue, go into overload
2908 term
->beep_overloaded
= TRUE
;
2910 term
->lastbeep
= ticks
;
2913 * Perform an actual beep if we're not overloaded.
2915 if (!term
->bellovl
|| !term
->beep_overloaded
) {
2916 do_beep(term
->frontend
, term
->beep
);
2918 if (term
->beep
== BELL_VISUAL
) {
2919 term_schedule_vbell(term
, FALSE
, 0);
2922 seen_disp_event(term
);
2925 case '\b': /* BS: Back space */
2926 if (term
->curs
.x
== 0 &&
2927 (term
->curs
.y
== 0 || term
->wrap
== 0))
2929 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2930 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2931 else if (term
->wrapnext
)
2932 term
->wrapnext
= FALSE
;
2935 seen_disp_event(term
);
2937 case '\016': /* LS1: Locking-shift one */
2938 compatibility(VT100
);
2941 case '\017': /* LS0: Locking-shift zero */
2942 compatibility(VT100
);
2945 case '\033': /* ESC: Escape */
2946 if (term
->vt52_mode
)
2947 term
->termstate
= VT52_ESC
;
2949 compatibility(ANSIMIN
);
2950 term
->termstate
= SEEN_ESC
;
2951 term
->esc_query
= FALSE
;
2954 case '\015': /* CR: Carriage return */
2956 term
->wrapnext
= FALSE
;
2957 seen_disp_event(term
);
2958 term
->paste_hold
= 0;
2960 if (term
->crhaslf
) {
2961 if (term
->curs
.y
== term
->marg_b
)
2962 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2963 else if (term
->curs
.y
< term
->rows
- 1)
2967 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2969 case '\014': /* FF: Form feed */
2970 if (has_compat(SCOANSI
)) {
2971 move(term
, 0, 0, 0);
2972 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2974 term
->wrapnext
= FALSE
;
2975 seen_disp_event(term
);
2978 case '\013': /* VT: Line tabulation */
2979 compatibility(VT100
);
2980 case '\012': /* LF: Line feed */
2981 if (term
->curs
.y
== term
->marg_b
)
2982 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2983 else if (term
->curs
.y
< term
->rows
- 1)
2987 term
->wrapnext
= FALSE
;
2988 seen_disp_event(term
);
2989 term
->paste_hold
= 0;
2991 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2993 case '\t': /* HT: Character tabulation */
2995 pos old_curs
= term
->curs
;
2996 termline
*ldata
= scrlineptr(term
->curs
.y
);
3000 } while (term
->curs
.x
< term
->cols
- 1 &&
3001 !term
->tabs
[term
->curs
.x
]);
3003 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
3004 if (term
->curs
.x
>= term
->cols
/ 2)
3005 term
->curs
.x
= term
->cols
/ 2 - 1;
3007 if (term
->curs
.x
>= term
->cols
)
3008 term
->curs
.x
= term
->cols
- 1;
3011 check_selection(term
, old_curs
, term
->curs
);
3013 seen_disp_event(term
);
3017 switch (term
->termstate
) {
3019 /* Only graphic characters get this far;
3020 * ctrls are stripped above */
3022 termline
*cline
= scrlineptr(term
->curs
.y
);
3027 width
= (term
->cjk_ambig_wide ?
3028 mk_wcwidth_cjk((unsigned int) c
) :
3029 mk_wcwidth((unsigned int) c
));
3031 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
3032 cline
->lattr
|= LATTR_WRAPPED
;
3033 if (term
->curs
.y
== term
->marg_b
)
3034 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3035 else if (term
->curs
.y
< term
->rows
- 1)
3038 term
->wrapnext
= FALSE
;
3039 cline
= scrlineptr(term
->curs
.y
);
3041 if (term
->insert
&& width
> 0)
3043 if (term
->selstate
!= NO_SELECTION
) {
3044 pos cursplus
= term
->curs
;
3046 check_selection(term
, term
->curs
, cursplus
);
3048 if (((c
& CSET_MASK
) == CSET_ASCII
||
3049 (c
& CSET_MASK
) == 0) &&
3051 logtraffic(term
->logctx
, (unsigned char) c
,
3057 * If we're about to display a double-width
3058 * character starting in the rightmost
3059 * column, then we do something special
3060 * instead. We must print a space in the
3061 * last column of the screen, then wrap;
3062 * and we also set LATTR_WRAPPED2 which
3063 * instructs subsequent cut-and-pasting not
3064 * only to splice this line to the one
3065 * after it, but to ignore the space in the
3066 * last character position as well.
3067 * (Because what was actually output to the
3068 * terminal was presumably just a sequence
3069 * of CJK characters, and we don't want a
3070 * space to be pasted in the middle of
3071 * those just because they had the
3072 * misfortune to start in the wrong parity
3073 * column. xterm concurs.)
3075 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3076 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
3077 if (term
->curs
.x
== term
->cols
-1) {
3078 copy_termchar(cline
, term
->curs
.x
,
3080 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
3081 if (term
->curs
.y
== term
->marg_b
)
3082 scroll(term
, term
->marg_t
, term
->marg_b
,
3084 else if (term
->curs
.y
< term
->rows
- 1)
3087 cline
= scrlineptr(term
->curs
.y
);
3088 /* Now we must check_boundary again, of course. */
3089 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3090 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
3094 clear_cc(cline
, term
->curs
.x
);
3095 cline
->chars
[term
->curs
.x
].chr
= c
;
3096 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
3101 clear_cc(cline
, term
->curs
.x
);
3102 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
3103 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
3107 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3108 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
3111 clear_cc(cline
, term
->curs
.x
);
3112 cline
->chars
[term
->curs
.x
].chr
= c
;
3113 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
3117 if (term
->curs
.x
> 0) {
3118 int x
= term
->curs
.x
- 1;
3120 /* If we're in wrapnext state, the character
3121 * to combine with is _here_, not to our left. */
3126 * If the previous character is
3127 * UCSWIDE, back up another one.
3129 if (cline
->chars
[x
].chr
== UCSWIDE
) {
3134 add_cc(cline
, x
, c
);
3135 seen_disp_event(term
);
3142 if (term
->curs
.x
== term
->cols
) {
3144 term
->wrapnext
= TRUE
;
3145 if (term
->wrap
&& term
->vt52_mode
) {
3146 cline
->lattr
|= LATTR_WRAPPED
;
3147 if (term
->curs
.y
== term
->marg_b
)
3148 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3149 else if (term
->curs
.y
< term
->rows
- 1)
3152 term
->wrapnext
= FALSE
;
3155 seen_disp_event(term
);
3161 * This state is virtually identical to SEEN_ESC, with the
3162 * exception that we have an OSC sequence in the pipeline,
3163 * and _if_ we see a backslash, we process it.
3167 term
->termstate
= TOPLEVEL
;
3170 /* else fall through */
3172 if (c
>= ' ' && c
<= '/') {
3173 if (term
->esc_query
)
3174 term
->esc_query
= -1;
3176 term
->esc_query
= c
;
3179 term
->termstate
= TOPLEVEL
;
3180 switch (ANSI(c
, term
->esc_query
)) {
3181 case '[': /* enter CSI mode */
3182 term
->termstate
= SEEN_CSI
;
3183 term
->esc_nargs
= 1;
3184 term
->esc_args
[0] = ARG_DEFAULT
;
3185 term
->esc_query
= FALSE
;
3187 case ']': /* OSC: xterm escape sequences */
3188 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
3189 compatibility(OTHER
);
3190 term
->termstate
= SEEN_OSC
;
3191 term
->esc_args
[0] = 0;
3193 case '7': /* DECSC: save cursor */
3194 compatibility(VT100
);
3195 save_cursor(term
, TRUE
);
3197 case '8': /* DECRC: restore cursor */
3198 compatibility(VT100
);
3199 save_cursor(term
, FALSE
);
3200 seen_disp_event(term
);
3202 case '=': /* DECKPAM: Keypad application mode */
3203 compatibility(VT100
);
3204 term
->app_keypad_keys
= TRUE
;
3206 case '>': /* DECKPNM: Keypad numeric mode */
3207 compatibility(VT100
);
3208 term
->app_keypad_keys
= FALSE
;
3210 case 'D': /* IND: exactly equivalent to LF */
3211 compatibility(VT100
);
3212 if (term
->curs
.y
== term
->marg_b
)
3213 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3214 else if (term
->curs
.y
< term
->rows
- 1)
3216 term
->wrapnext
= FALSE
;
3217 seen_disp_event(term
);
3219 case 'E': /* NEL: exactly equivalent to CR-LF */
3220 compatibility(VT100
);
3222 if (term
->curs
.y
== term
->marg_b
)
3223 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
3224 else if (term
->curs
.y
< term
->rows
- 1)
3226 term
->wrapnext
= FALSE
;
3227 seen_disp_event(term
);
3229 case 'M': /* RI: reverse index - backwards LF */
3230 compatibility(VT100
);
3231 if (term
->curs
.y
== term
->marg_t
)
3232 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
3233 else if (term
->curs
.y
> 0)
3235 term
->wrapnext
= FALSE
;
3236 seen_disp_event(term
);
3238 case 'Z': /* DECID: terminal type query */
3239 compatibility(VT100
);
3241 ldisc_send(term
->ldisc
, term
->id_string
,
3242 strlen(term
->id_string
), 0);
3244 case 'c': /* RIS: restore power-on settings */
3245 compatibility(VT100
);
3246 power_on(term
, TRUE
);
3247 if (term
->ldisc
) /* cause ldisc to notice changes */
3248 ldisc_send(term
->ldisc
, NULL
, 0, 0);
3249 if (term
->reset_132
) {
3250 if (!term
->no_remote_resize
)
3251 request_resize(term
->frontend
, 80, term
->rows
);
3252 term
->reset_132
= 0;
3255 seen_disp_event(term
);
3257 case 'H': /* HTS: set a tab */
3258 compatibility(VT100
);
3259 term
->tabs
[term
->curs
.x
] = TRUE
;
3262 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
3263 compatibility(VT100
);
3269 for (i
= 0; i
< term
->rows
; i
++) {
3270 ldata
= scrlineptr(i
);
3271 for (j
= 0; j
< term
->cols
; j
++) {
3272 copy_termchar(ldata
, j
,
3273 &term
->basic_erase_char
);
3274 ldata
->chars
[j
].chr
= 'E';
3276 ldata
->lattr
= LATTR_NORM
;
3279 seen_disp_event(term
);
3280 scrtop
.x
= scrtop
.y
= 0;
3282 scrbot
.y
= term
->rows
;
3283 check_selection(term
, scrtop
, scrbot
);
3287 case ANSI('3', '#'):
3288 case ANSI('4', '#'):
3289 case ANSI('5', '#'):
3290 case ANSI('6', '#'):
3291 compatibility(VT100
);
3295 switch (ANSI(c
, term
->esc_query
)) {
3296 case ANSI('3', '#'): /* DECDHL: 2*height, top */
3299 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
3302 case ANSI('5', '#'): /* DECSWL: normal */
3303 nlattr
= LATTR_NORM
;
3305 default: /* case ANSI('6', '#'): DECDWL: 2*width */
3306 nlattr
= LATTR_WIDE
;
3309 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
3312 /* GZD4: G0 designate 94-set */
3313 case ANSI('A', '('):
3314 compatibility(VT100
);
3315 if (!term
->no_remote_charset
)
3316 term
->cset_attr
[0] = CSET_GBCHR
;
3318 case ANSI('B', '('):
3319 compatibility(VT100
);
3320 if (!term
->no_remote_charset
)
3321 term
->cset_attr
[0] = CSET_ASCII
;
3323 case ANSI('0', '('):
3324 compatibility(VT100
);
3325 if (!term
->no_remote_charset
)
3326 term
->cset_attr
[0] = CSET_LINEDRW
;
3328 case ANSI('U', '('):
3329 compatibility(OTHER
);
3330 if (!term
->no_remote_charset
)
3331 term
->cset_attr
[0] = CSET_SCOACS
;
3333 /* G1D4: G1-designate 94-set */
3334 case ANSI('A', ')'):
3335 compatibility(VT100
);
3336 if (!term
->no_remote_charset
)
3337 term
->cset_attr
[1] = CSET_GBCHR
;
3339 case ANSI('B', ')'):
3340 compatibility(VT100
);
3341 if (!term
->no_remote_charset
)
3342 term
->cset_attr
[1] = CSET_ASCII
;
3344 case ANSI('0', ')'):
3345 compatibility(VT100
);
3346 if (!term
->no_remote_charset
)
3347 term
->cset_attr
[1] = CSET_LINEDRW
;
3349 case ANSI('U', ')'):
3350 compatibility(OTHER
);
3351 if (!term
->no_remote_charset
)
3352 term
->cset_attr
[1] = CSET_SCOACS
;
3354 /* DOCS: Designate other coding system */
3355 case ANSI('8', '%'): /* Old Linux code */
3356 case ANSI('G', '%'):
3357 compatibility(OTHER
);
3358 if (!term
->no_remote_charset
)
3361 case ANSI('@', '%'):
3362 compatibility(OTHER
);
3363 if (!term
->no_remote_charset
)
3369 term
->termstate
= TOPLEVEL
; /* default */
3371 if (term
->esc_nargs
<= ARGS_MAX
) {
3372 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
3373 term
->esc_args
[term
->esc_nargs
- 1] = 0;
3374 term
->esc_args
[term
->esc_nargs
- 1] =
3375 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
3377 term
->termstate
= SEEN_CSI
;
3378 } else if (c
== ';') {
3379 if (term
->esc_nargs
< ARGS_MAX
)
3380 term
->esc_args
[term
->esc_nargs
++] = ARG_DEFAULT
;
3381 term
->termstate
= SEEN_CSI
;
3382 } else if (c
< '@') {
3383 if (term
->esc_query
)
3384 term
->esc_query
= -1;
3386 term
->esc_query
= TRUE
;
3388 term
->esc_query
= c
;
3389 term
->termstate
= SEEN_CSI
;
3391 switch (ANSI(c
, term
->esc_query
)) {
3392 case 'A': /* CUU: move up N lines */
3393 move(term
, term
->curs
.x
,
3394 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3395 seen_disp_event(term
);
3397 case 'e': /* VPR: move down N lines */
3398 compatibility(ANSI
);
3400 case 'B': /* CUD: Cursor down */
3401 move(term
, term
->curs
.x
,
3402 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3403 seen_disp_event(term
);
3405 case ANSI('c', '>'): /* DA: report xterm version */
3406 compatibility(OTHER
);
3407 /* this reports xterm version 136 so that VIM can
3408 use the drag messages from the mouse reporting */
3410 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
3412 case 'a': /* HPR: move right N cols */
3413 compatibility(ANSI
);
3415 case 'C': /* CUF: Cursor right */
3416 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
3418 seen_disp_event(term
);
3420 case 'D': /* CUB: move left N cols */
3421 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
3423 seen_disp_event(term
);
3425 case 'E': /* CNL: move down N lines and CR */
3426 compatibility(ANSI
);
3428 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3429 seen_disp_event(term
);
3431 case 'F': /* CPL: move up N lines and CR */
3432 compatibility(ANSI
);
3434 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3435 seen_disp_event(term
);
3438 case '`': /* HPA: set horizontal posn */
3439 compatibility(ANSI
);
3440 move(term
, def(term
->esc_args
[0], 1) - 1,
3442 seen_disp_event(term
);
3444 case 'd': /* VPA: set vertical posn */
3445 compatibility(ANSI
);
3446 move(term
, term
->curs
.x
,
3447 ((term
->dec_om ? term
->marg_t
: 0) +
3448 def(term
->esc_args
[0], 1) - 1),
3449 (term
->dec_om ?
2 : 0));
3450 seen_disp_event(term
);
3453 case 'f': /* HVP: set horz and vert posns at once */
3454 if (term
->esc_nargs
< 2)
3455 term
->esc_args
[1] = ARG_DEFAULT
;
3456 move(term
, def(term
->esc_args
[1], 1) - 1,
3457 ((term
->dec_om ? term
->marg_t
: 0) +
3458 def(term
->esc_args
[0], 1) - 1),
3459 (term
->dec_om ?
2 : 0));
3460 seen_disp_event(term
);
3462 case 'J': /* ED: erase screen or parts of it */
3464 unsigned int i
= def(term
->esc_args
[0], 0);
3466 /* Erase Saved Lines (xterm)
3467 * This follows Thomas Dickey's xterm. */
3473 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3477 seen_disp_event(term
);
3479 case 'K': /* EL: erase line or parts of it */
3481 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3484 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3486 seen_disp_event(term
);
3488 case 'L': /* IL: insert lines */
3489 compatibility(VT102
);
3490 if (term
->curs
.y
<= term
->marg_b
)
3491 scroll(term
, term
->curs
.y
, term
->marg_b
,
3492 -def(term
->esc_args
[0], 1), FALSE
);
3493 seen_disp_event(term
);
3495 case 'M': /* DL: delete lines */
3496 compatibility(VT102
);
3497 if (term
->curs
.y
<= term
->marg_b
)
3498 scroll(term
, term
->curs
.y
, term
->marg_b
,
3499 def(term
->esc_args
[0], 1),
3501 seen_disp_event(term
);
3503 case '@': /* ICH: insert chars */
3504 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3505 compatibility(VT102
);
3506 insch(term
, def(term
->esc_args
[0], 1));
3507 seen_disp_event(term
);
3509 case 'P': /* DCH: delete chars */
3510 compatibility(VT102
);
3511 insch(term
, -def(term
->esc_args
[0], 1));
3512 seen_disp_event(term
);
3514 case 'c': /* DA: terminal type query */
3515 compatibility(VT100
);
3516 /* This is the response for a VT102 */
3518 ldisc_send(term
->ldisc
, term
->id_string
,
3519 strlen(term
->id_string
), 0);
3521 case 'n': /* DSR: cursor position query */
3523 if (term
->esc_args
[0] == 6) {
3525 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3527 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3528 } else if (term
->esc_args
[0] == 5) {
3529 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3533 case 'h': /* SM: toggle modes to high */
3535 compatibility(VT100
);
3538 for (i
= 0; i
< term
->esc_nargs
; i
++)
3539 toggle_mode(term
, term
->esc_args
[i
],
3540 term
->esc_query
, TRUE
);
3543 case 'i': /* MC: Media copy */
3545 compatibility(VT100
);
3548 if (term
->esc_nargs
!= 1) break;
3549 if (term
->esc_args
[0] == 5 &&
3550 (printer
= conf_get_str(term
->conf
,
3551 CONF_printer
))[0]) {
3552 term
->printing
= TRUE
;
3553 term
->only_printing
= !term
->esc_query
;
3554 term
->print_state
= 0;
3555 term_print_setup(term
, printer
);
3556 } else if (term
->esc_args
[0] == 4 &&
3558 term_print_finish(term
);
3562 case 'l': /* RM: toggle modes to low */
3564 compatibility(VT100
);
3567 for (i
= 0; i
< term
->esc_nargs
; i
++)
3568 toggle_mode(term
, term
->esc_args
[i
],
3569 term
->esc_query
, FALSE
);
3572 case 'g': /* TBC: clear tabs */
3573 compatibility(VT100
);
3574 if (term
->esc_nargs
== 1) {
3575 if (term
->esc_args
[0] == 0) {
3576 term
->tabs
[term
->curs
.x
] = FALSE
;
3577 } else if (term
->esc_args
[0] == 3) {
3579 for (i
= 0; i
< term
->cols
; i
++)
3580 term
->tabs
[i
] = FALSE
;
3584 case 'r': /* DECSTBM: set scroll margins */
3585 compatibility(VT100
);
3586 if (term
->esc_nargs
<= 2) {
3588 top
= def(term
->esc_args
[0], 1) - 1;
3589 bot
= (term
->esc_nargs
<= 1
3590 || term
->esc_args
[1] == 0 ?
3592 def(term
->esc_args
[1], term
->rows
)) - 1;
3593 if (bot
>= term
->rows
)
3594 bot
= term
->rows
- 1;
3595 /* VTTEST Bug 9 - if region is less than 2 lines
3596 * don't change region.
3598 if (bot
- top
> 0) {
3603 * I used to think the cursor should be
3604 * placed at the top of the newly marginned
3605 * area. Apparently not: VMS TPU falls over
3608 * Well actually it should for
3611 term
->curs
.y
= (term
->dec_om ?
3613 seen_disp_event(term
);
3617 case 'm': /* SGR: set graphics rendition */
3620 * A VT100 without the AVO only had one
3621 * attribute, either underline or
3622 * reverse video depending on the
3623 * cursor type, this was selected by
3627 * This is sometimes DIM, eg on the
3630 * This is sometimes INVIS various ANSI.
3632 * This like 22 disables BOLD, DIM and INVIS
3634 * The ANSI colours appear on any
3635 * terminal that has colour (obviously)
3636 * but the interaction between sgr0 and
3637 * the colours varies but is usually
3638 * related to the background colour
3639 * erase item. The interaction between
3640 * colour attributes and the mono ones
3641 * is also very implementation
3644 * The 39 and 49 attributes are likely
3645 * to be unimplemented.
3648 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3649 switch (def(term
->esc_args
[i
], 0)) {
3650 case 0: /* restore defaults */
3651 term
->curr_attr
= term
->default_attr
;
3653 case 1: /* enable bold */
3654 compatibility(VT100AVO
);
3655 term
->curr_attr
|= ATTR_BOLD
;
3657 case 21: /* (enable double underline) */
3658 compatibility(OTHER
);
3659 case 4: /* enable underline */
3660 compatibility(VT100AVO
);
3661 term
->curr_attr
|= ATTR_UNDER
;
3663 case 5: /* enable blink */
3664 compatibility(VT100AVO
);
3665 term
->curr_attr
|= ATTR_BLINK
;
3667 case 6: /* SCO light bkgrd */
3668 compatibility(SCOANSI
);
3669 term
->blink_is_real
= FALSE
;
3670 term
->curr_attr
|= ATTR_BLINK
;
3671 term_schedule_tblink(term
);
3673 case 7: /* enable reverse video */
3674 term
->curr_attr
|= ATTR_REVERSE
;
3676 case 10: /* SCO acs off */
3677 compatibility(SCOANSI
);
3678 if (term
->no_remote_charset
) break;
3679 term
->sco_acs
= 0; break;
3680 case 11: /* SCO acs on */
3681 compatibility(SCOANSI
);
3682 if (term
->no_remote_charset
) break;
3683 term
->sco_acs
= 1; break;
3684 case 12: /* SCO acs on, |0x80 */
3685 compatibility(SCOANSI
);
3686 if (term
->no_remote_charset
) break;
3687 term
->sco_acs
= 2; break;
3688 case 22: /* disable bold */
3689 compatibility2(OTHER
, VT220
);
3690 term
->curr_attr
&= ~ATTR_BOLD
;
3692 case 24: /* disable underline */
3693 compatibility2(OTHER
, VT220
);
3694 term
->curr_attr
&= ~ATTR_UNDER
;
3696 case 25: /* disable blink */
3697 compatibility2(OTHER
, VT220
);
3698 term
->curr_attr
&= ~ATTR_BLINK
;
3700 case 27: /* disable reverse video */
3701 compatibility2(OTHER
, VT220
);
3702 term
->curr_attr
&= ~ATTR_REVERSE
;
3713 term
->curr_attr
&= ~ATTR_FGMASK
;
3715 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3725 /* aixterm-style bright foreground */
3726 term
->curr_attr
&= ~ATTR_FGMASK
;
3728 ((term
->esc_args
[i
] - 90 + 8)
3731 case 39: /* default-foreground */
3732 term
->curr_attr
&= ~ATTR_FGMASK
;
3733 term
->curr_attr
|= ATTR_DEFFG
;
3744 term
->curr_attr
&= ~ATTR_BGMASK
;
3746 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3756 /* aixterm-style bright background */
3757 term
->curr_attr
&= ~ATTR_BGMASK
;
3759 ((term
->esc_args
[i
] - 100 + 8)
3762 case 49: /* default-background */
3763 term
->curr_attr
&= ~ATTR_BGMASK
;
3764 term
->curr_attr
|= ATTR_DEFBG
;
3766 case 38: /* xterm 256-colour mode */
3767 if (i
+2 < term
->esc_nargs
&&
3768 term
->esc_args
[i
+1] == 5) {
3769 term
->curr_attr
&= ~ATTR_FGMASK
;
3771 ((term
->esc_args
[i
+2] & 0xFF)
3776 case 48: /* xterm 256-colour mode */
3777 if (i
+2 < term
->esc_nargs
&&
3778 term
->esc_args
[i
+1] == 5) {
3779 term
->curr_attr
&= ~ATTR_BGMASK
;
3781 ((term
->esc_args
[i
+2] & 0xFF)
3788 set_erase_char(term
);
3791 case 's': /* save cursor */
3792 save_cursor(term
, TRUE
);
3794 case 'u': /* restore cursor */
3795 save_cursor(term
, FALSE
);
3796 seen_disp_event(term
);
3798 case 't': /* DECSLPP: set page size - ie window height */
3800 * VT340/VT420 sequence DECSLPP, DEC only allows values
3801 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3802 * illegal values (eg first arg 1..9) for window changing
3805 if (term
->esc_nargs
<= 1
3806 && (term
->esc_args
[0] < 1 ||
3807 term
->esc_args
[0] >= 24)) {
3808 compatibility(VT340TEXT
);
3809 if (!term
->no_remote_resize
)
3810 request_resize(term
->frontend
, term
->cols
,
3811 def(term
->esc_args
[0], 24));
3813 } else if (term
->esc_nargs
>= 1 &&
3814 term
->esc_args
[0] >= 1 &&
3815 term
->esc_args
[0] < 24) {
3816 compatibility(OTHER
);
3818 switch (term
->esc_args
[0]) {
3822 set_iconic(term
->frontend
, FALSE
);
3825 set_iconic(term
->frontend
, TRUE
);
3828 if (term
->esc_nargs
>= 3) {
3829 if (!term
->no_remote_resize
)
3830 move_window(term
->frontend
,
3831 def(term
->esc_args
[1], 0),
3832 def(term
->esc_args
[2], 0));
3836 /* We should resize the window to a given
3837 * size in pixels here, but currently our
3838 * resizing code isn't healthy enough to
3843 set_zorder(term
->frontend
, TRUE
);
3846 /* move to bottom */
3847 set_zorder(term
->frontend
, FALSE
);
3850 refresh_window(term
->frontend
);
3853 if (term
->esc_nargs
>= 3) {
3854 if (!term
->no_remote_resize
)
3855 request_resize(term
->frontend
,
3856 def(term
->esc_args
[2], term
->conf_width
),
3857 def(term
->esc_args
[1], term
->conf_height
));
3861 if (term
->esc_nargs
>= 2)
3862 set_zoomed(term
->frontend
,
3868 ldisc_send(term
->ldisc
,
3869 is_iconic(term
->frontend
) ?
3870 "\033[2t" : "\033[1t", 4, 0);
3874 get_window_pos(term
->frontend
, &x
, &y
);
3875 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3876 ldisc_send(term
->ldisc
, buf
, len
, 0);
3881 get_window_pixels(term
->frontend
, &x
, &y
);
3882 len
= sprintf(buf
, "\033[4;%d;%dt", y
, x
);
3883 ldisc_send(term
->ldisc
, buf
, len
, 0);
3888 len
= sprintf(buf
, "\033[8;%d;%dt",
3889 term
->rows
, term
->cols
);
3890 ldisc_send(term
->ldisc
, buf
, len
, 0);
3895 * Hmmm. Strictly speaking we
3896 * should return `the size of the
3897 * screen in characters', but
3898 * that's not easy: (a) window
3899 * furniture being what it is it's
3900 * hard to compute, and (b) in
3901 * resize-font mode maximising the
3902 * window wouldn't change the
3903 * number of characters. *shrug*. I
3904 * think we'll ignore it for the
3905 * moment and see if anyone
3906 * complains, and then ask them
3907 * what they would like it to do.
3912 term
->remote_qtitle_action
!= TITLE_NONE
) {
3913 if(term
->remote_qtitle_action
== TITLE_REAL
)
3914 p
= get_window_title(term
->frontend
, TRUE
);
3916 p
= EMPTY_WINDOW_TITLE
;
3918 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3919 ldisc_send(term
->ldisc
, p
, len
, 0);
3920 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3925 term
->remote_qtitle_action
!= TITLE_NONE
) {
3926 if(term
->remote_qtitle_action
== TITLE_REAL
)
3927 p
= get_window_title(term
->frontend
, FALSE
);
3929 p
= EMPTY_WINDOW_TITLE
;
3931 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3932 ldisc_send(term
->ldisc
, p
, len
, 0);
3933 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3939 case 'S': /* SU: Scroll up */
3940 compatibility(SCOANSI
);
3941 scroll(term
, term
->marg_t
, term
->marg_b
,
3942 def(term
->esc_args
[0], 1), TRUE
);
3943 term
->wrapnext
= FALSE
;
3944 seen_disp_event(term
);
3946 case 'T': /* SD: Scroll down */
3947 compatibility(SCOANSI
);
3948 scroll(term
, term
->marg_t
, term
->marg_b
,
3949 -def(term
->esc_args
[0], 1), TRUE
);
3950 term
->wrapnext
= FALSE
;
3951 seen_disp_event(term
);
3953 case ANSI('|', '*'): /* DECSNLS */
3955 * Set number of lines on screen
3956 * VT420 uses VGA like hardware and can
3957 * support any size in reasonable range
3958 * (24..49 AIUI) with no default specified.
3960 compatibility(VT420
);
3961 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3962 if (!term
->no_remote_resize
)
3963 request_resize(term
->frontend
, term
->cols
,
3964 def(term
->esc_args
[0],
3965 term
->conf_height
));
3969 case ANSI('|', '$'): /* DECSCPP */
3971 * Set number of columns per page
3972 * Docs imply range is only 80 or 132, but
3975 compatibility(VT340TEXT
);
3976 if (term
->esc_nargs
<= 1) {
3977 if (!term
->no_remote_resize
)
3978 request_resize(term
->frontend
,
3979 def(term
->esc_args
[0],
3985 case 'X': /* ECH: write N spaces w/o moving cursor */
3986 /* XXX VTTEST says this is vt220, vt510 manual
3988 compatibility(ANSIMIN
);
3990 int n
= def(term
->esc_args
[0], 1);
3992 int p
= term
->curs
.x
;
3993 termline
*cline
= scrlineptr(term
->curs
.y
);
3995 if (n
> term
->cols
- term
->curs
.x
)
3996 n
= term
->cols
- term
->curs
.x
;
3997 cursplus
= term
->curs
;
3999 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
4000 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
4001 check_selection(term
, term
->curs
, cursplus
);
4003 copy_termchar(cline
, p
++,
4005 seen_disp_event(term
);
4008 case 'x': /* DECREQTPARM: report terminal characteristics */
4009 compatibility(VT100
);
4012 int i
= def(term
->esc_args
[0], 0);
4013 if (i
== 0 || i
== 1) {
4014 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
4016 ldisc_send(term
->ldisc
, buf
, 20, 0);
4021 compatibility(OTHER
);
4023 int i
= def(term
->esc_args
[0], 1);
4024 pos old_curs
= term
->curs
;
4026 for(;i
>0 && term
->curs
.x
>0; i
--) {
4029 } while (term
->curs
.x
>0 &&
4030 !term
->tabs
[term
->curs
.x
]);
4032 check_selection(term
, old_curs
, term
->curs
);
4035 case ANSI('c', '='): /* Hide or Show Cursor */
4036 compatibility(SCOANSI
);
4037 switch(term
->esc_args
[0]) {
4038 case 0: /* hide cursor */
4039 term
->cursor_on
= FALSE
;
4041 case 1: /* restore cursor */
4042 term
->big_cursor
= FALSE
;
4043 term
->cursor_on
= TRUE
;
4045 case 2: /* block cursor */
4046 term
->big_cursor
= TRUE
;
4047 term
->cursor_on
= TRUE
;
4051 case ANSI('C', '='):
4053 * set cursor start on scanline esc_args[0] and
4054 * end on scanline esc_args[1].If you set
4055 * the bottom scan line to a value less than
4056 * the top scan line, the cursor will disappear.
4058 compatibility(SCOANSI
);
4059 if (term
->esc_nargs
>= 2) {
4060 if (term
->esc_args
[0] > term
->esc_args
[1])
4061 term
->cursor_on
= FALSE
;
4063 term
->cursor_on
= TRUE
;
4066 case ANSI('D', '='):
4067 compatibility(SCOANSI
);
4068 term
->blink_is_real
= FALSE
;
4069 term_schedule_tblink(term
);
4070 if (term
->esc_args
[0]>=1)
4071 term
->curr_attr
|= ATTR_BLINK
;
4073 term
->curr_attr
&= ~ATTR_BLINK
;
4075 case ANSI('E', '='):
4076 compatibility(SCOANSI
);
4077 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
4078 term_schedule_tblink(term
);
4080 case ANSI('F', '='): /* set normal foreground */
4081 compatibility(SCOANSI
);
4082 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
4084 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
4085 (term
->esc_args
[0] & 0x8)) <<
4087 term
->curr_attr
&= ~ATTR_FGMASK
;
4088 term
->curr_attr
|= colour
;
4089 term
->default_attr
&= ~ATTR_FGMASK
;
4090 term
->default_attr
|= colour
;
4091 set_erase_char(term
);
4094 case ANSI('G', '='): /* set normal background */
4095 compatibility(SCOANSI
);
4096 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
4098 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
4099 (term
->esc_args
[0] & 0x8)) <<
4101 term
->curr_attr
&= ~ATTR_BGMASK
;
4102 term
->curr_attr
|= colour
;
4103 term
->default_attr
&= ~ATTR_BGMASK
;
4104 term
->default_attr
|= colour
;
4105 set_erase_char(term
);
4108 case ANSI('L', '='):
4109 compatibility(SCOANSI
);
4110 term
->use_bce
= (term
->esc_args
[0] <= 0);
4111 set_erase_char(term
);
4113 case ANSI('p', '"'): /* DECSCL: set compat level */
4115 * Allow the host to make this emulator a
4116 * 'perfect' VT102. This first appeared in
4117 * the VT220, but we do need to get back to
4118 * PuTTY mode so I won't check it.
4120 * The arg in 40..42,50 are a PuTTY extension.
4121 * The 2nd arg, 8bit vs 7bit is not checked.
4123 * Setting VT102 mode should also change
4124 * the Fkeys to generate PF* codes as a
4125 * real VT102 has no Fkeys. The VT220 does
4126 * this, F11..F13 become ESC,BS,LF other
4127 * Fkeys send nothing.
4129 * Note ESC c will NOT change this!
4132 switch (term
->esc_args
[0]) {
4134 term
->compatibility_level
&= ~TM_VTXXX
;
4135 term
->compatibility_level
|= TM_VT102
;
4138 term
->compatibility_level
&= ~TM_VTXXX
;
4139 term
->compatibility_level
|= TM_VT220
;
4143 if (term
->esc_args
[0] > 60 &&
4144 term
->esc_args
[0] < 70)
4145 term
->compatibility_level
|= TM_VTXXX
;
4149 term
->compatibility_level
&= TM_VTXXX
;
4152 term
->compatibility_level
= TM_PUTTY
;
4155 term
->compatibility_level
= TM_SCOANSI
;
4159 term
->compatibility_level
= TM_PUTTY
;
4165 /* Change the response to CSI c */
4166 if (term
->esc_args
[0] == 50) {
4169 strcpy(term
->id_string
, "\033[?");
4170 for (i
= 1; i
< term
->esc_nargs
; i
++) {
4172 strcat(term
->id_string
, ";");
4173 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
4174 strcat(term
->id_string
, lbuf
);
4176 strcat(term
->id_string
, "c");
4179 /* Is this a good idea ?
4180 * Well we should do a soft reset at this point ...
4182 if (!has_compat(VT420
) && has_compat(VT100
)) {
4183 if (!term
->no_remote_resize
) {
4184 if (term
->reset_132
)
4185 request_resize(132, 24);
4187 request_resize(80, 24);
4195 term
->osc_w
= FALSE
;
4197 case 'P': /* Linux palette sequence */
4198 term
->termstate
= SEEN_OSC_P
;
4199 term
->osc_strlen
= 0;
4201 case 'R': /* Linux palette reset */
4202 palette_reset(term
->frontend
);
4203 term_invalidate(term
);
4204 term
->termstate
= TOPLEVEL
;
4206 case 'W': /* word-set */
4207 term
->termstate
= SEEN_OSC_W
;
4220 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4224 * Grotty hack to support xterm and DECterm title
4225 * sequences concurrently.
4227 if (term
->esc_args
[0] == 2) {
4228 term
->esc_args
[0] = 1;
4231 /* else fall through */
4233 term
->termstate
= OSC_STRING
;
4234 term
->osc_strlen
= 0;
4239 * This OSC stuff is EVIL. It takes just one character to get into
4240 * sysline mode and it's not initially obvious how to get out.
4241 * So I've added CR and LF as string aborts.
4242 * This shouldn't effect compatibility as I believe embedded
4243 * control characters are supposed to be interpreted (maybe?)
4244 * and they don't display anything useful anyway.
4248 if (c
== '\012' || c
== '\015') {
4249 term
->termstate
= TOPLEVEL
;
4250 } else if (c
== 0234 || c
== '\007') {
4252 * These characters terminate the string; ST and BEL
4253 * terminate the sequence and trigger instant
4254 * processing of it, whereas ESC goes back to SEEN_ESC
4255 * mode unless it is followed by \, in which case it is
4256 * synonymous with ST in the first place.
4259 term
->termstate
= TOPLEVEL
;
4260 } else if (c
== '\033')
4261 term
->termstate
= OSC_MAYBE_ST
;
4262 else if (term
->osc_strlen
< OSC_STR_MAX
)
4263 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
4267 int max
= (term
->osc_strlen
== 0 ?
21 : 15);
4269 if ((int)c
>= '0' && (int)c
<= '9')
4271 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
4273 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
4276 term
->termstate
= TOPLEVEL
;
4279 term
->osc_string
[term
->osc_strlen
++] = val
;
4280 if (term
->osc_strlen
>= 7) {
4281 palette_set(term
->frontend
, term
->osc_string
[0],
4282 term
->osc_string
[1] * 16 + term
->osc_string
[2],
4283 term
->osc_string
[3] * 16 + term
->osc_string
[4],
4284 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
4285 term_invalidate(term
);
4286 term
->termstate
= TOPLEVEL
;
4302 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
4305 term
->termstate
= OSC_STRING
;
4306 term
->osc_strlen
= 0;
4310 term
->termstate
= TOPLEVEL
;
4311 seen_disp_event(term
);
4314 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
4317 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
4320 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
4323 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
4326 * From the VT100 Manual
4327 * NOTE: The special graphics characters in the VT100
4328 * are different from those in the VT52
4330 * From VT102 manual:
4331 * 137 _ Blank - Same
4332 * 140 ` Reserved - Humm.
4333 * 141 a Solid rectangle - Similar
4334 * 142 b 1/ - Top half of fraction for the
4335 * 143 c 3/ - subscript numbers below.
4338 * 146 f Degrees - Same
4339 * 147 g Plus or minus - Same
4341 * 151 i Ellipsis (dots)
4344 * 154 l Bar at scan 0
4345 * 155 m Bar at scan 1
4346 * 156 n Bar at scan 2
4347 * 157 o Bar at scan 3 - Similar
4348 * 160 p Bar at scan 4 - Similar
4349 * 161 q Bar at scan 5 - Similar
4350 * 162 r Bar at scan 6 - Same
4351 * 163 s Bar at scan 7 - Similar
4366 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
4369 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
4372 move(term
, 0, 0, 0);
4375 if (term
->curs
.y
== 0)
4376 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
4377 else if (term
->curs
.y
> 0)
4379 term
->wrapnext
= FALSE
;
4382 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4386 erase_lots(term
, TRUE
, FALSE
, TRUE
);
4390 /* XXX Print cursor line */
4393 /* XXX Start controller mode */
4396 /* XXX Stop controller mode */
4400 term
->termstate
= VT52_Y1
;
4404 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
4407 term
->app_keypad_keys
= TRUE
;
4410 term
->app_keypad_keys
= FALSE
;
4413 /* XXX This should switch to VT100 mode not current or default
4414 * VT mode. But this will only have effect in a VT220+
4417 term
->vt52_mode
= FALSE
;
4418 term
->blink_is_real
= term
->blinktext
;
4419 term_schedule_tblink(term
);
4423 /* XXX Enter auto print mode */
4426 /* XXX Exit auto print mode */
4429 /* XXX Print screen */
4435 /* compatibility(ATARI) */
4436 move(term
, 0, 0, 0);
4437 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4441 /* compatibility(ATARI) */
4442 if (term
->curs
.y
<= term
->marg_b
)
4443 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
4446 /* compatibility(ATARI) */
4447 if (term
->curs
.y
<= term
->marg_b
)
4448 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
4451 /* compatibility(ATARI) */
4452 term
->termstate
= VT52_FG
;
4455 /* compatibility(ATARI) */
4456 term
->termstate
= VT52_BG
;
4459 /* compatibility(ATARI) */
4460 erase_lots(term
, FALSE
, TRUE
, FALSE
);
4464 /* compatibility(ATARI) */
4465 term
->cursor_on
= TRUE
;
4468 /* compatibility(ATARI) */
4469 term
->cursor_on
= FALSE
;
4471 /* case 'j': Save cursor position - broken on ST */
4472 /* case 'k': Restore cursor position */
4474 /* compatibility(ATARI) */
4475 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4477 term
->wrapnext
= FALSE
;
4480 /* compatibility(ATARI) */
4481 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4484 /* compatibility(ATARI) */
4485 term
->curr_attr
|= ATTR_REVERSE
;
4488 /* compatibility(ATARI) */
4489 term
->curr_attr
&= ~ATTR_REVERSE
;
4491 case 'v': /* wrap Autowrap on - Wyse style */
4492 /* compatibility(ATARI) */
4495 case 'w': /* Autowrap off */
4496 /* compatibility(ATARI) */
4501 /* compatibility(OTHER) */
4502 term
->vt52_bold
= FALSE
;
4503 term
->curr_attr
= ATTR_DEFAULT
;
4504 set_erase_char(term
);
4507 /* compatibility(VI50) */
4508 term
->curr_attr
|= ATTR_UNDER
;
4511 /* compatibility(VI50) */
4512 term
->curr_attr
&= ~ATTR_UNDER
;
4515 /* compatibility(VI50) */
4516 term
->vt52_bold
= TRUE
;
4517 term
->curr_attr
|= ATTR_BOLD
;
4520 /* compatibility(VI50) */
4521 term
->vt52_bold
= FALSE
;
4522 term
->curr_attr
&= ~ATTR_BOLD
;
4528 term
->termstate
= VT52_Y2
;
4529 move(term
, term
->curs
.x
, c
- ' ', 0);
4532 term
->termstate
= TOPLEVEL
;
4533 move(term
, c
- ' ', term
->curs
.y
, 0);
4538 term
->termstate
= TOPLEVEL
;
4539 term
->curr_attr
&= ~ATTR_FGMASK
;
4540 term
->curr_attr
&= ~ATTR_BOLD
;
4541 term
->curr_attr
|= (c
& 0xF) << ATTR_FGSHIFT
;
4542 set_erase_char(term
);
4545 term
->termstate
= TOPLEVEL
;
4546 term
->curr_attr
&= ~ATTR_BGMASK
;
4547 term
->curr_attr
&= ~ATTR_BLINK
;
4548 term
->curr_attr
|= (c
& 0xF) << ATTR_BGSHIFT
;
4549 set_erase_char(term
);
4552 default: break; /* placate gcc warning about enum use */
4554 if (term
->selstate
!= NO_SELECTION
) {
4555 pos cursplus
= term
->curs
;
4557 check_selection(term
, term
->curs
, cursplus
);
4561 term_print_flush(term
);
4563 logflush(term
->logctx
);
4567 * To prevent having to run the reasonably tricky bidi algorithm
4568 * too many times, we maintain a cache of the last lineful of data
4569 * fed to the algorithm on each line of the display.
4571 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4572 termchar
*lbefore
, int width
)
4576 if (!term
->pre_bidi_cache
)
4577 return FALSE
; /* cache doesn't even exist yet! */
4579 if (line
>= term
->bidi_cache_size
)
4580 return FALSE
; /* cache doesn't have this many lines */
4582 if (!term
->pre_bidi_cache
[line
].chars
)
4583 return FALSE
; /* cache doesn't contain _this_ line */
4585 if (term
->pre_bidi_cache
[line
].width
!= width
)
4586 return FALSE
; /* line is wrong width */
4588 for (i
= 0; i
< width
; i
++)
4589 if (!termchars_equal(term
->pre_bidi_cache
[line
].chars
+i
, lbefore
+i
))
4590 return FALSE
; /* line doesn't match cache */
4592 return TRUE
; /* it didn't match. */
4595 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4596 termchar
*lafter
, bidi_char
*wcTo
,
4597 int width
, int size
)
4601 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4602 int j
= term
->bidi_cache_size
;
4603 term
->bidi_cache_size
= line
+1;
4604 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4605 term
->bidi_cache_size
,
4606 struct bidi_cache_entry
);
4607 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4608 term
->bidi_cache_size
,
4609 struct bidi_cache_entry
);
4610 while (j
< term
->bidi_cache_size
) {
4611 term
->pre_bidi_cache
[j
].chars
=
4612 term
->post_bidi_cache
[j
].chars
= NULL
;
4613 term
->pre_bidi_cache
[j
].width
=
4614 term
->post_bidi_cache
[j
].width
= -1;
4615 term
->pre_bidi_cache
[j
].forward
=
4616 term
->post_bidi_cache
[j
].forward
= NULL
;
4617 term
->pre_bidi_cache
[j
].backward
=
4618 term
->post_bidi_cache
[j
].backward
= NULL
;
4623 sfree(term
->pre_bidi_cache
[line
].chars
);
4624 sfree(term
->post_bidi_cache
[line
].chars
);
4625 sfree(term
->post_bidi_cache
[line
].forward
);
4626 sfree(term
->post_bidi_cache
[line
].backward
);
4628 term
->pre_bidi_cache
[line
].width
= width
;
4629 term
->pre_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4630 term
->post_bidi_cache
[line
].width
= width
;
4631 term
->post_bidi_cache
[line
].chars
= snewn(size
, termchar
);
4632 term
->post_bidi_cache
[line
].forward
= snewn(width
, int);
4633 term
->post_bidi_cache
[line
].backward
= snewn(width
, int);
4635 memcpy(term
->pre_bidi_cache
[line
].chars
, lbefore
, size
* TSIZE
);
4636 memcpy(term
->post_bidi_cache
[line
].chars
, lafter
, size
* TSIZE
);
4637 memset(term
->post_bidi_cache
[line
].forward
, 0, width
* sizeof(int));
4638 memset(term
->post_bidi_cache
[line
].backward
, 0, width
* sizeof(int));
4640 for (i
= 0; i
< width
; i
++) {
4641 int p
= wcTo
[i
].index
;
4643 assert(0 <= p
&& p
< width
);
4645 term
->post_bidi_cache
[line
].backward
[i
] = p
;
4646 term
->post_bidi_cache
[line
].forward
[p
] = i
;
4651 * Prepare the bidi information for a screen line. Returns the
4652 * transformed list of termchars, or NULL if no transformation at
4653 * all took place (because bidi is disabled). If return was
4654 * non-NULL, auxiliary information such as the forward and reverse
4655 * mappings of permutation position are available in
4656 * term->post_bidi_cache[scr_y].*.
4658 static termchar
*term_bidi_line(Terminal
*term
, struct termline
*ldata
,
4664 /* Do Arabic shaping and bidi. */
4665 if(!term
->bidi
|| !term
->arabicshaping
) {
4667 if (!term_bidi_cache_hit(term
, scr_y
, ldata
->chars
, term
->cols
)) {
4669 if (term
->wcFromTo_size
< term
->cols
) {
4670 term
->wcFromTo_size
= term
->cols
;
4671 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4673 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4677 for(it
=0; it
<term
->cols
; it
++)
4679 unsigned long uc
= (ldata
->chars
[it
].chr
);
4681 switch (uc
& CSET_MASK
) {
4683 if (!term
->rawcnp
) {
4684 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4688 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4691 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4694 switch (uc
& CSET_MASK
) {
4696 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4699 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4703 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4705 term
->wcFrom
[it
].index
= it
;
4709 do_bidi(term
->wcFrom
, term
->cols
);
4711 /* this is saved iff done from inside the shaping */
4712 if(!term
->bidi
&& term
->arabicshaping
)
4713 for(it
=0; it
<term
->cols
; it
++)
4714 term
->wcTo
[it
] = term
->wcFrom
[it
];
4716 if(!term
->arabicshaping
)
4717 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4719 if (term
->ltemp_size
< ldata
->size
) {
4720 term
->ltemp_size
= ldata
->size
;
4721 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4725 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4727 for(it
=0; it
<term
->cols
; it
++)
4729 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4730 if (term
->ltemp
[it
].cc_next
)
4731 term
->ltemp
[it
].cc_next
-=
4732 it
- term
->wcTo
[it
].index
;
4734 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4735 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4737 term_bidi_cache_store(term
, scr_y
, ldata
->chars
,
4738 term
->ltemp
, term
->wcTo
,
4739 term
->cols
, ldata
->size
);
4741 lchars
= term
->ltemp
;
4743 lchars
= term
->post_bidi_cache
[scr_y
].chars
;
4753 * Given a context, update the window. Out of paranoia, we don't
4754 * allow WM_PAINT responses to do scrolling optimisations.
4756 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4758 int i
, j
, our_curs_y
, our_curs_x
;
4763 #ifdef OPTIMISE_SCROLL
4764 struct scrollregion
*sr
;
4765 #endif /* OPTIMISE_SCROLL */
4769 ch
= snewn(chlen
, wchar_t);
4771 newline
= snewn(term
->cols
, termchar
);
4773 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4776 * screen array, disptop, scrtop,
4778 * blinkpc, blink_is_real, tblinker,
4779 * curs.y, curs.x, cblinker, blink_cur, cursor_on, has_focus, wrapnext
4782 /* Has the cursor position or type changed ? */
4783 if (term
->cursor_on
) {
4784 if (term
->has_focus
) {
4785 if (term
->cblinker
|| !term
->blink_cur
)
4786 cursor
= TATTR_ACTCURS
;
4790 cursor
= TATTR_PASCURS
;
4792 cursor
|= TATTR_RIGHTCURS
;
4795 our_curs_y
= term
->curs
.y
- term
->disptop
;
4798 * Adjust the cursor position:
4800 * - in the case where it's resting on the right-hand half
4801 * of a CJK wide character. xterm's behaviour here,
4802 * which seems adequate to me, is to display the cursor
4803 * covering the _whole_ character, exactly as if it were
4804 * one space to the left.
4806 termline
*ldata
= lineptr(term
->curs
.y
);
4809 our_curs_x
= term
->curs
.x
;
4811 if ( (lchars
= term_bidi_line(term
, ldata
, our_curs_y
)) != NULL
) {
4812 our_curs_x
= term
->post_bidi_cache
[our_curs_y
].forward
[our_curs_x
];
4814 lchars
= ldata
->chars
;
4816 if (our_curs_x
> 0 &&
4817 lchars
[our_curs_x
].chr
== UCSWIDE
)
4824 * If the cursor is not where it was last time we painted, and
4825 * its previous position is visible on screen, invalidate its
4826 * previous position.
4828 if (term
->dispcursy
>= 0 &&
4829 (term
->curstype
!= cursor
||
4830 term
->dispcursy
!= our_curs_y
||
4831 term
->dispcursx
!= our_curs_x
)) {
4832 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4835 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4836 dispcurs
[-1].attr
|= ATTR_INVALID
;
4837 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4838 dispcurs
[1].attr
|= ATTR_INVALID
;
4839 dispcurs
->attr
|= ATTR_INVALID
;
4843 term
->dispcursx
= term
->dispcursy
= -1;
4845 #ifdef OPTIMISE_SCROLL
4847 sr
= term
->scrollhead
;
4849 struct scrollregion
*next
= sr
->next
;
4850 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4854 term
->scrollhead
= term
->scrolltail
= NULL
;
4855 #endif /* OPTIMISE_SCROLL */
4857 /* The normal screen data */
4858 for (i
= 0; i
< term
->rows
; i
++) {
4861 int dirty_line
, dirty_run
, selected
;
4862 unsigned long attr
= 0, cset
= 0;
4865 int last_run_dirty
= 0;
4866 int laststart
, dirtyrect
;
4869 scrpos
.y
= i
+ term
->disptop
;
4870 ldata
= lineptr(scrpos
.y
);
4872 /* Do Arabic shaping and bidi. */
4873 lchars
= term_bidi_line(term
, ldata
, i
);
4875 backward
= term
->post_bidi_cache
[i
].backward
;
4877 lchars
= ldata
->chars
;
4882 * First loop: work along the line deciding what we want
4883 * each character cell to look like.
4885 for (j
= 0; j
< term
->cols
; j
++) {
4886 unsigned long tattr
, tchar
;
4887 termchar
*d
= lchars
+ j
;
4888 scrpos
.x
= backward ? backward
[j
] : j
;
4893 if (!term
->ansi_colour
)
4894 tattr
= (tattr
& ~(ATTR_FGMASK
| ATTR_BGMASK
)) |
4895 ATTR_DEFFG
| ATTR_DEFBG
;
4897 if (!term
->xterm_256_colour
) {
4899 colour
= (tattr
& ATTR_FGMASK
) >> ATTR_FGSHIFT
;
4900 if (colour
>= 16 && colour
< 256)
4901 tattr
= (tattr
&~ ATTR_FGMASK
) | ATTR_DEFFG
;
4902 colour
= (tattr
& ATTR_BGMASK
) >> ATTR_BGSHIFT
;
4903 if (colour
>= 16 && colour
< 256)
4904 tattr
= (tattr
&~ ATTR_BGMASK
) | ATTR_DEFBG
;
4907 switch (tchar
& CSET_MASK
) {
4909 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4912 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4915 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4918 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4921 /* Video reversing things */
4922 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4923 if (term
->seltype
== LEXICOGRAPHIC
)
4924 selected
= (posle(term
->selstart
, scrpos
) &&
4925 poslt(scrpos
, term
->selend
));
4927 selected
= (posPle(term
->selstart
, scrpos
) &&
4928 posPlt(scrpos
, term
->selend
));
4932 ^ (selected ? ATTR_REVERSE
: 0));
4934 /* 'Real' blinking ? */
4935 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4936 if (term
->has_focus
&& term
->tblinker
) {
4937 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4939 tattr
&= ~ATTR_BLINK
;
4943 * Check the font we'll _probably_ be using to see if
4944 * the character is wide when we don't want it to be.
4946 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4947 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4948 (ATTR_NARROW
| DATTR_MASK
))) {
4949 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4950 tattr
|= ATTR_NARROW
;
4951 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4952 tattr
|= ATTR_NARROW
;
4954 if (i
== our_curs_y
&& j
== our_curs_x
) {
4956 term
->curstype
= cursor
;
4957 term
->dispcursx
= j
;
4958 term
->dispcursy
= i
;
4962 newline
[j
].attr
= tattr
;
4963 newline
[j
].chr
= tchar
;
4964 /* Combining characters are still read from lchars */
4965 newline
[j
].cc_next
= 0;
4969 * Now loop over the line again, noting where things have
4972 * During this loop, we keep track of where we last saw
4973 * DATTR_STARTRUN. Any mismatch automatically invalidates
4974 * _all_ of the containing run that was last printed: that
4975 * is, any rectangle that was drawn in one go in the
4976 * previous update should be either left completely alone
4977 * or overwritten in its entirety. This, along with the
4978 * expectation that front ends clip all text runs to their
4979 * bounding rectangle, should solve any possible problems
4980 * with fonts that overflow their character cells.
4984 for (j
= 0; j
< term
->cols
; j
++) {
4985 if (term
->disptext
[i
]->chars
[j
].attr
& DATTR_STARTRUN
) {
4990 if (term
->disptext
[i
]->chars
[j
].chr
!= newline
[j
].chr
||
4991 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
)
4992 != newline
[j
].attr
) {
4996 for (k
= laststart
; k
< j
; k
++)
4997 term
->disptext
[i
]->chars
[k
].attr
|= ATTR_INVALID
;
5004 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5008 * Finally, loop once more and actually do the drawing.
5010 dirty_run
= dirty_line
= (ldata
->lattr
!=
5011 term
->disptext
[i
]->lattr
);
5012 term
->disptext
[i
]->lattr
= ldata
->lattr
;
5014 for (j
= 0; j
< term
->cols
; j
++) {
5015 unsigned long tattr
, tchar
;
5016 int break_run
, do_copy
;
5017 termchar
*d
= lchars
+ j
;
5019 tattr
= newline
[j
].attr
;
5020 tchar
= newline
[j
].chr
;
5022 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
5025 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
5027 #ifdef USES_VTLINE_HACK
5028 /* Special hack for VT100 Linedraw glyphs */
5029 if ((tchar
>= 0x23BA && tchar
<= 0x23BD) ||
5030 (j
> 0 && (newline
[j
-1].chr
>= 0x23BA &&
5031 newline
[j
-1].chr
<= 0x23BD)))
5036 * Separate out sequences of characters that have the
5037 * same CSET, if that CSET is a magic one.
5039 if (CSET_OF(tchar
) != cset
)
5043 * Break on both sides of any combined-character cell.
5045 if (d
->cc_next
!= 0 ||
5046 (j
> 0 && d
[-1].cc_next
!= 0))
5049 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
5050 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
5051 (term
->disptext
[i
]->chars
[j
].attr
&~ DATTR_MASK
) == tattr
)
5053 else if (!dirty_run
&& ccount
== 1)
5058 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
5059 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
5061 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
5062 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
5068 cset
= CSET_OF(tchar
);
5069 if (term
->ucsdata
->dbcs_screenfont
)
5070 last_run_dirty
= dirty_run
;
5071 dirty_run
= dirty_line
;
5075 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
5081 if (ccount
+2 > chlen
) {
5082 chlen
= ccount
+ 256;
5083 ch
= sresize(ch
, chlen
, wchar_t);
5086 #ifdef PLATFORM_IS_UTF16
5087 if (tchar
> 0x10000 && tchar
< 0x110000) {
5088 ch
[ccount
++] = (wchar_t) HIGH_SURROGATE_OF(tchar
);
5089 ch
[ccount
++] = (wchar_t) LOW_SURROGATE_OF(tchar
);
5091 #endif /* PLATFORM_IS_UTF16 */
5092 ch
[ccount
++] = (wchar_t) tchar
;
5097 while (dd
->cc_next
) {
5098 unsigned long schar
;
5103 switch (schar
& CSET_MASK
) {
5105 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
5108 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
5111 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
5115 if (ccount
+2 > chlen
) {
5116 chlen
= ccount
+ 256;
5117 ch
= sresize(ch
, chlen
, wchar_t);
5120 #ifdef PLATFORM_IS_UTF16
5121 if (schar
> 0x10000 && schar
< 0x110000) {
5122 ch
[ccount
++] = (wchar_t) HIGH_SURROGATE_OF(schar
);
5123 ch
[ccount
++] = (wchar_t) LOW_SURROGATE_OF(schar
);
5125 #endif /* PLATFORM_IS_UTF16 */
5126 ch
[ccount
++] = (wchar_t) schar
;
5129 attr
|= TATTR_COMBINING
;
5133 copy_termchar(term
->disptext
[i
], j
, d
);
5134 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
5135 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
5137 term
->disptext
[i
]->chars
[j
].attr
|= DATTR_STARTRUN
;
5140 /* If it's a wide char step along to the next one. */
5141 if (tattr
& ATTR_WIDE
) {
5142 if (++j
< term
->cols
) {
5145 * By construction above, the cursor should not
5146 * be on the right-hand half of this character.
5149 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
5150 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
5152 copy_termchar(term
->disptext
[i
], j
, d
);
5156 if (dirty_run
&& ccount
> 0) {
5157 do_text(ctx
, start
, i
, ch
, ccount
, attr
,
5159 if (attr
& (TATTR_ACTCURS
| TATTR_PASCURS
))
5160 do_cursor(ctx
, start
, i
, ch
, ccount
, attr
,
5172 * Invalidate the whole screen so it will be repainted in full.
5174 void term_invalidate(Terminal
*term
)
5178 for (i
= 0; i
< term
->rows
; i
++)
5179 for (j
= 0; j
< term
->cols
; j
++)
5180 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5182 term_schedule_update(term
);
5186 * Paint the window in response to a WM_PAINT message.
5188 void term_paint(Terminal
*term
, Context ctx
,
5189 int left
, int top
, int right
, int bottom
, int immediately
)
5192 if (left
< 0) left
= 0;
5193 if (top
< 0) top
= 0;
5194 if (right
>= term
->cols
) right
= term
->cols
-1;
5195 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
5197 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
5198 if ((term
->disptext
[i
]->lattr
& LATTR_MODE
) == LATTR_NORM
)
5199 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
5200 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5202 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
5203 term
->disptext
[i
]->chars
[j
].attr
|= ATTR_INVALID
;
5207 do_paint (term
, ctx
, FALSE
);
5209 term_schedule_update(term
);
5214 * Attempt to scroll the scrollback. The second parameter gives the
5215 * position we want to scroll to; the first is +1 to denote that
5216 * this position is relative to the beginning of the scrollback, -1
5217 * to denote it is relative to the end, and 0 to denote that it is
5218 * relative to the current position.
5220 void term_scroll(Terminal
*term
, int rel
, int where
)
5222 int sbtop
= -sblines(term
);
5223 #ifdef OPTIMISE_SCROLL
5224 int olddisptop
= term
->disptop
;
5226 #endif /* OPTIMISE_SCROLL */
5228 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
5229 if (term
->disptop
< sbtop
)
5230 term
->disptop
= sbtop
;
5231 if (term
->disptop
> 0)
5234 #ifdef OPTIMISE_SCROLL
5235 shift
= (term
->disptop
- olddisptop
);
5236 if (shift
< term
->rows
&& shift
> -term
->rows
)
5237 scroll_display(term
, 0, term
->rows
- 1, shift
);
5238 #endif /* OPTIMISE_SCROLL */
5243 * Scroll the scrollback to centre it on the beginning or end of the
5244 * current selection, if any.
5246 void term_scroll_to_selection(Terminal
*term
, int which_end
)
5250 int sbtop
= -sblines(term
);
5252 if (term
->selstate
!= SELECTED
)
5255 target
= term
->selend
;
5257 target
= term
->selstart
;
5259 y
= target
.y
- term
->rows
/2;
5264 term_scroll(term
, -1, y
);
5268 * Helper routine for clipme(): growing buffer.
5271 int buflen
; /* amount of allocated space in textbuf/attrbuf */
5272 int bufpos
; /* amount of actual data */
5273 wchar_t *textbuf
; /* buffer for copied text */
5274 wchar_t *textptr
; /* = textbuf + bufpos (current insertion point) */
5275 int *attrbuf
; /* buffer for copied attributes */
5276 int *attrptr
; /* = attrbuf + bufpos */
5279 static void clip_addchar(clip_workbuf
*b
, wchar_t chr
, int attr
)
5281 if (b
->bufpos
>= b
->buflen
) {
5283 b
->textbuf
= sresize(b
->textbuf
, b
->buflen
, wchar_t);
5284 b
->textptr
= b
->textbuf
+ b
->bufpos
;
5285 b
->attrbuf
= sresize(b
->attrbuf
, b
->buflen
, int);
5286 b
->attrptr
= b
->attrbuf
+ b
->bufpos
;
5288 *b
->textptr
++ = chr
;
5289 *b
->attrptr
++ = attr
;
5293 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
5301 buf
.textptr
= buf
.textbuf
= snewn(buf
.buflen
, wchar_t);
5302 buf
.attrptr
= buf
.attrbuf
= snewn(buf
.buflen
, int);
5304 old_top_x
= top
.x
; /* needed for rect==1 */
5306 while (poslt(top
, bottom
)) {
5308 termline
*ldata
= lineptr(top
.y
);
5312 * nlpos will point at the maximum position on this line we
5313 * should copy up to. So we start it at the end of the
5317 nlpos
.x
= term
->cols
;
5320 * ... move it backwards if there's unused space at the end
5321 * of the line (and also set `nl' if this is the case,
5322 * because in normal selection mode this means we need a
5323 * newline at the end)...
5325 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5327 IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
5328 !ldata
->chars
[nlpos
.x
- 1].cc_next
&&
5331 if (poslt(nlpos
, bottom
))
5333 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
5334 /* Ignore the last char on the line in a WRAPPED2 line. */
5339 * ... and then clip it to the terminal x coordinate if
5340 * we're doing rectangular selection. (In this case we
5341 * still did the above, so that copying e.g. the right-hand
5342 * column from a table doesn't fill with spaces on the
5346 if (nlpos
.x
> bottom
.x
)
5348 nl
= (top
.y
< bottom
.y
);
5351 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
5354 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
5356 wchar_t cbuf
[16], *p
;
5360 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
5366 int uc
= ldata
->chars
[x
].chr
;
5367 attr
= ldata
->chars
[x
].attr
;
5369 switch (uc
& CSET_MASK
) {
5371 if (!term
->rawcnp
) {
5372 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5376 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5379 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5382 switch (uc
& CSET_MASK
) {
5384 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5387 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5391 c
= (uc
& ~CSET_MASK
);
5392 #ifdef PLATFORM_IS_UTF16
5393 if (uc
> 0x10000 && uc
< 0x110000) {
5394 cbuf
[0] = 0xD800 | ((uc
- 0x10000) >> 10);
5395 cbuf
[1] = 0xDC00 | ((uc
- 0x10000) & 0x3FF);
5404 if (DIRECT_FONT(uc
)) {
5405 if (c
>= ' ' && c
!= 0x7F) {
5409 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
5411 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
5412 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
5416 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
5420 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
5427 for (p
= cbuf
; *p
; p
++)
5428 clip_addchar(&buf
, *p
, attr
);
5430 if (ldata
->chars
[x
].cc_next
)
5431 x
+= ldata
->chars
[x
].cc_next
;
5439 for (i
= 0; i
< sel_nl_sz
; i
++)
5440 clip_addchar(&buf
, sel_nl
[i
], 0);
5443 top
.x
= rect ? old_top_x
: 0;
5447 #if SELECTION_NUL_TERMINATED
5448 clip_addchar(&buf
, 0, 0);
5450 /* Finally, transfer all that to the clipboard. */
5451 write_clip(term
->frontend
, buf
.textbuf
, buf
.attrbuf
, buf
.bufpos
, desel
);
5456 void term_copyall(Terminal
*term
)
5460 tree234
*screen
= term
->screen
;
5461 top
.y
= -sblines(term
);
5463 bottom
.y
= find_last_nonempty_line(term
, screen
);
5464 bottom
.x
= term
->cols
;
5465 clipme(term
, top
, bottom
, 0, TRUE
);
5469 * The wordness array is mainly for deciding the disposition of the
5470 * US-ASCII characters.
5472 static int wordtype(Terminal
*term
, int uc
)
5475 int start
, end
, ctype
;
5477 static const struct ucsword ucs_words
[] = {
5483 0x037e, 0x037e, 1}, /* Greek question mark */
5485 0x0387, 0x0387, 1}, /* Greek ano teleia */
5487 0x055a, 0x055f, 1}, /* Armenian punctuation */
5489 0x0589, 0x0589, 1}, /* Armenian full stop */
5491 0x0700, 0x070d, 1}, /* Syriac punctuation */
5493 0x104a, 0x104f, 1}, /* Myanmar punctuation */
5495 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
5497 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
5499 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
5501 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
5503 0x1800, 0x180a, 1}, /* Mongolian punctuation */
5505 0x2000, 0x200a, 0}, /* Various spaces */
5507 0x2070, 0x207f, 2}, /* superscript */
5509 0x2080, 0x208f, 2}, /* subscript */
5511 0x200b, 0x27ff, 1}, /* punctuation and symbols */
5513 0x3000, 0x3000, 0}, /* ideographic space */
5515 0x3001, 0x3020, 1}, /* ideographic punctuation */
5517 0x303f, 0x309f, 3}, /* Hiragana */
5519 0x30a0, 0x30ff, 3}, /* Katakana */
5521 0x3300, 0x9fff, 3}, /* CJK Ideographs */
5523 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
5525 0xf900, 0xfaff, 3}, /* CJK Ideographs */
5527 0xfe30, 0xfe6b, 1}, /* punctuation forms */
5529 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
5531 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
5533 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
5535 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
5537 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
5541 const struct ucsword
*wptr
;
5543 switch (uc
& CSET_MASK
) {
5545 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5548 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5551 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5554 switch (uc
& CSET_MASK
) {
5556 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5559 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5563 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5564 * fail as there's such a thing as a double width space. :-(
5566 if (term
->ucsdata
->dbcs_screenfont
&&
5567 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
5571 return term
->wordness
[uc
];
5573 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
5574 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
5582 * Spread the selection outwards according to the selection mode.
5584 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
5588 int topy
= -sblines(term
);
5590 ldata
= lineptr(p
.y
);
5592 switch (term
->selmode
) {
5595 * In this mode, every character is a separate unit, except
5596 * for runs of spaces at the end of a non-wrapping line.
5598 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5599 termchar
*q
= ldata
->chars
+ term
->cols
;
5600 while (q
> ldata
->chars
&&
5601 IS_SPACE_CHR(q
[-1].chr
) && !q
[-1].cc_next
)
5603 if (q
== ldata
->chars
+ term
->cols
)
5605 if (p
.x
>= q
- ldata
->chars
)
5606 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5611 * In this mode, the units are maximal runs of characters
5612 * whose `wordness' has the same value.
5614 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5617 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5618 term
->cols
-1 : term
->cols
);
5619 if (p
.x
< maxcols
-1) {
5620 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5625 if (ldata
->lattr
& LATTR_WRAPPED
) {
5627 ldata2
= lineptr(p
.y
+1);
5628 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5645 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5654 ldata2
= lineptr(p
.y
-1);
5655 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5656 term
->cols
-1 : term
->cols
);
5657 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5658 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5676 * In this mode, every line is a unit.
5678 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5686 static void sel_spread(Terminal
*term
)
5688 if (term
->seltype
== LEXICOGRAPHIC
) {
5689 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5690 decpos(term
->selend
);
5691 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5692 incpos(term
->selend
);
5696 void term_do_paste(Terminal
*term
)
5701 get_clip(term
->frontend
, &data
, &len
);
5702 if (data
&& len
> 0) {
5705 term_seen_key_event(term
); /* pasted data counts */
5707 if (term
->paste_buffer
)
5708 sfree(term
->paste_buffer
);
5709 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5710 term
->paste_buffer
= snewn(len
+ 12, wchar_t);
5712 if (term
->bracketed_paste
) {
5713 memcpy(term
->paste_buffer
, L
"\033[200~", 6 * sizeof(wchar_t));
5714 term
->paste_len
+= 6;
5718 while (p
< data
+ len
) {
5719 while (p
< data
+ len
&&
5720 !(p
<= data
+ len
- sel_nl_sz
&&
5721 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5726 for (i
= 0; i
< p
- q
; i
++) {
5727 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5731 if (p
<= data
+ len
- sel_nl_sz
&&
5732 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5733 term
->paste_buffer
[term
->paste_len
++] = '\015';
5739 if (term
->bracketed_paste
) {
5740 memcpy(term
->paste_buffer
+ term
->paste_len
,
5741 L
"\033[201~", 6 * sizeof(wchar_t));
5742 term
->paste_len
+= 6;
5745 /* Assume a small paste will be OK in one go. */
5746 if (term
->paste_len
< 256) {
5748 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5749 if (term
->paste_buffer
)
5750 sfree(term
->paste_buffer
);
5751 term
->paste_buffer
= 0;
5752 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5755 get_clip(term
->frontend
, NULL
, NULL
);
5758 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5759 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5763 int raw_mouse
= (term
->xterm_mouse
&&
5764 !term
->no_mouse_rep
&&
5765 !(term
->mouse_override
&& shift
));
5766 int default_seltype
;
5770 if (a
== MA_DRAG
&& !raw_mouse
)
5771 term_scroll(term
, 0, -1);
5773 if (y
>= term
->rows
) {
5775 if (a
== MA_DRAG
&& !raw_mouse
)
5776 term_scroll(term
, 0, +1);
5785 if (x
>= term
->cols
)
5788 selpoint
.y
= y
+ term
->disptop
;
5789 ldata
= lineptr(selpoint
.y
);
5791 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5795 * Transform x through the bidi algorithm to find the _logical_
5796 * click point from the physical one.
5798 if (term_bidi_line(term
, ldata
, y
) != NULL
) {
5799 x
= term
->post_bidi_cache
[y
].backward
[x
];
5806 * If we're in the middle of a selection operation, we ignore raw
5807 * mouse mode until it's done (we must have been not in raw mouse
5808 * mode when it started).
5809 * This makes use of Shift for selection reliable, and avoids the
5810 * host seeing mouse releases for which they never saw corresponding
5814 (term
->selstate
!= ABOUT_TO
) && (term
->selstate
!= DRAGGING
)) {
5815 int encstate
= 0, r
, c
;
5822 encstate
= 0x20; /* left button down */
5833 case MBT_WHEEL_DOWN
:
5836 default: break; /* placate gcc warning about enum use */
5840 if (term
->xterm_mouse
== 1)
5846 term
->mouse_is_down
= 0;
5849 if (term
->mouse_is_down
== braw
)
5851 term
->mouse_is_down
= braw
;
5853 default: break; /* placate gcc warning about enum use */
5862 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5863 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5869 * Set the selection type (rectangular or normal) at the start
5870 * of a selection attempt, from the state of Alt.
5872 if (!alt
^ !term
->rect_select
)
5873 default_seltype
= RECTANGULAR
;
5875 default_seltype
= LEXICOGRAPHIC
;
5877 if (term
->selstate
== NO_SELECTION
) {
5878 term
->seltype
= default_seltype
;
5881 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5883 term
->selstate
= ABOUT_TO
;
5884 term
->seltype
= default_seltype
;
5885 term
->selanchor
= selpoint
;
5886 term
->selmode
= SM_CHAR
;
5887 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5889 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5890 term
->selstate
= DRAGGING
;
5891 term
->selstart
= term
->selanchor
= selpoint
;
5892 term
->selend
= term
->selstart
;
5893 incpos(term
->selend
);
5895 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5896 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5897 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5899 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5900 term
->selstate
== SELECTED
) {
5901 if (term
->seltype
== LEXICOGRAPHIC
) {
5903 * For normal selection, we extend by moving
5904 * whichever end of the current selection is closer
5907 if (posdiff(selpoint
, term
->selstart
) <
5908 posdiff(term
->selend
, term
->selstart
) / 2) {
5909 term
->selanchor
= term
->selend
;
5910 decpos(term
->selanchor
);
5912 term
->selanchor
= term
->selstart
;
5916 * For rectangular selection, we have a choice of
5917 * _four_ places to put selanchor and selpoint: the
5918 * four corners of the selection.
5920 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5921 term
->selanchor
.x
= term
->selend
.x
-1;
5923 term
->selanchor
.x
= term
->selstart
.x
;
5925 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5926 term
->selanchor
.y
= term
->selend
.y
;
5928 term
->selanchor
.y
= term
->selstart
.y
;
5930 term
->selstate
= DRAGGING
;
5932 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5933 term
->selanchor
= selpoint
;
5934 term
->selstate
= DRAGGING
;
5935 if (term
->seltype
== LEXICOGRAPHIC
) {
5937 * For normal selection, we set (selstart,selend) to
5938 * (selpoint,selanchor) in some order.
5940 if (poslt(selpoint
, term
->selanchor
)) {
5941 term
->selstart
= selpoint
;
5942 term
->selend
= term
->selanchor
;
5943 incpos(term
->selend
);
5945 term
->selstart
= term
->selanchor
;
5946 term
->selend
= selpoint
;
5947 incpos(term
->selend
);
5951 * For rectangular selection, we may need to
5952 * interchange x and y coordinates (if the user has
5953 * dragged in the -x and +y directions, or vice versa).
5955 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5956 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5957 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5958 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5961 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5963 if (term
->selstate
== DRAGGING
) {
5965 * We've completed a selection. We now transfer the
5966 * data to the clipboard.
5968 clipme(term
, term
->selstart
, term
->selend
,
5969 (term
->seltype
== RECTANGULAR
), FALSE
);
5970 term
->selstate
= SELECTED
;
5972 term
->selstate
= NO_SELECTION
;
5973 } else if (bcooked
== MBT_PASTE
5975 #if MULTICLICK_ONLY_EVENT
5976 || a
== MA_2CLK
|| a
== MA_3CLK
5979 request_paste(term
->frontend
);
5985 int format_arrow_key(char *buf
, Terminal
*term
, int xkey
, int ctrl
)
5989 if (term
->vt52_mode
)
5990 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5992 int app_flg
= (term
->app_cursor_keys
&& !term
->no_applic_c
);
5995 * RDB: VT100 & VT102 manuals both state the app cursor
5996 * keys only work if the app keypad is on.
5998 * SGT: That may well be true, but xterm disagrees and so
5999 * does at least one application, so I've #if'ed this out
6000 * and the behaviour is back to PuTTY's original: app
6001 * cursor and app keypad are independently switchable
6002 * modes. If anyone complains about _this_ I'll have to
6003 * put in a configurable option.
6005 if (!term
->app_keypad_keys
)
6008 /* Useful mapping of Ctrl-arrows */
6013 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
6015 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
6021 void term_nopaste(Terminal
*term
)
6023 if (term
->paste_len
== 0)
6025 sfree(term
->paste_buffer
);
6026 term
->paste_buffer
= NULL
;
6027 term
->paste_len
= 0;
6030 int term_paste_pending(Terminal
*term
)
6032 return term
->paste_len
!= 0;
6035 void term_paste(Terminal
*term
)
6037 long now
, paste_diff
;
6039 if (term
->paste_len
== 0)
6042 /* Don't wait forever to paste */
6043 if (term
->paste_hold
) {
6044 now
= GETTICKCOUNT();
6045 paste_diff
= now
- term
->last_paste
;
6046 if (paste_diff
>= 0 && paste_diff
< 450)
6049 term
->paste_hold
= 0;
6051 while (term
->paste_pos
< term
->paste_len
) {
6053 while (n
+ term
->paste_pos
< term
->paste_len
) {
6054 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
6058 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
6059 term
->paste_pos
+= n
;
6061 if (term
->paste_pos
< term
->paste_len
) {
6062 term
->paste_hold
= 1;
6066 sfree(term
->paste_buffer
);
6067 term
->paste_buffer
= NULL
;
6068 term
->paste_len
= 0;
6071 static void deselect(Terminal
*term
)
6073 term
->selstate
= NO_SELECTION
;
6074 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
6077 void term_deselect(Terminal
*term
)
6083 int term_ldisc(Terminal
*term
, int option
)
6085 if (option
== LD_ECHO
)
6086 return term
->term_echoing
;
6087 if (option
== LD_EDIT
)
6088 return term
->term_editing
;
6092 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
6094 bufchain_add(&term
->inbuf
, data
, len
);
6096 if (!term
->in_term_out
) {
6097 term
->in_term_out
= TRUE
;
6098 term_reset_cblink(term
);
6100 * During drag-selects, we do not process terminal input,
6101 * because the user will want the screen to hold still to
6104 if (term
->selstate
!= DRAGGING
)
6106 term
->in_term_out
= FALSE
;
6110 * term_out() always completely empties inbuf. Therefore,
6111 * there's no reason at all to return anything other than zero
6112 * from this function, because there _can't_ be a question of
6113 * the remote side needing to wait until term_out() has cleared
6116 * This is a slightly suboptimal way to deal with SSH-2 - in
6117 * principle, the window mechanism would allow us to continue
6118 * to accept data on forwarded ports and X connections even
6119 * while the terminal processing was going slowly - but we
6120 * can't do the 100% right thing without moving the terminal
6121 * processing into a separate thread, and that might hurt
6122 * portability. So we manage stdout buffering the old SSH-1 way:
6123 * if the terminal processing goes slowly, the whole SSH
6124 * connection stops accepting data until it's ready.
6126 * In practice, I can't imagine this causing serious trouble.
6132 * Write untrusted data to the terminal.
6133 * The only control character that should be honoured is \n (which
6134 * will behave as a CRLF).
6136 int term_data_untrusted(Terminal
*term
, const char *data
, int len
)
6139 /* FIXME: more sophisticated checking? */
6140 for (i
= 0; i
< len
; i
++) {
6141 if (data
[i
] == '\n')
6142 term_data(term
, 1, "\r\n", 2);
6143 else if (data
[i
] & 0x60)
6144 term_data(term
, 1, data
+ i
, 1);
6146 return 0; /* assumes that term_data() always returns 0 */
6149 void term_provide_logctx(Terminal
*term
, void *logctx
)
6151 term
->logctx
= logctx
;
6154 void term_set_focus(Terminal
*term
, int has_focus
)
6156 term
->has_focus
= has_focus
;
6157 term_schedule_cblink(term
);
6161 * Provide "auto" settings for remote tty modes, suitable for an
6162 * application with a terminal window.
6164 char *term_get_ttymode(Terminal
*term
, const char *mode
)
6167 if (strcmp(mode
, "ERASE") == 0) {
6168 val
= term
->bksp_is_delete ?
"^?" : "^H";
6170 /* FIXME: perhaps we should set ONLCR based on lfhascr as well? */
6171 /* FIXME: or ECHO and friends based on local echo state? */
6175 struct term_userpass_state
{
6177 int done_prompt
; /* printed out prompt yet? */
6178 size_t pos
; /* cursor position */
6182 * Process some terminal data in the course of username/password
6185 int term_get_userpass_input(Terminal
*term
, prompts_t
*p
,
6186 unsigned char *in
, int inlen
)
6188 struct term_userpass_state
*s
= (struct term_userpass_state
*)p
->data
;
6191 * First call. Set some stuff up.
6193 p
->data
= s
= snew(struct term_userpass_state
);
6196 /* We only print the `name' caption if we have to... */
6197 if (p
->name_reqd
&& p
->name
) {
6198 size_t l
= strlen(p
->name
);
6199 term_data_untrusted(term
, p
->name
, l
);
6200 if (p
->name
[l
-1] != '\n')
6201 term_data_untrusted(term
, "\n", 1);
6203 /* ...but we always print any `instruction'. */
6204 if (p
->instruction
) {
6205 size_t l
= strlen(p
->instruction
);
6206 term_data_untrusted(term
, p
->instruction
, l
);
6207 if (p
->instruction
[l
-1] != '\n')
6208 term_data_untrusted(term
, "\n", 1);
6211 * Zero all the results, in case we abort half-way through.
6215 for (i
= 0; i
< (int)p
->n_prompts
; i
++)
6216 prompt_set_result(p
->prompts
[i
], "");
6220 while (s
->curr_prompt
< p
->n_prompts
) {
6222 prompt_t
*pr
= p
->prompts
[s
->curr_prompt
];
6223 int finished_prompt
= 0;
6225 if (!s
->done_prompt
) {
6226 term_data_untrusted(term
, pr
->prompt
, strlen(pr
->prompt
));
6231 /* Breaking out here ensures that the prompt is printed even
6232 * if we're now waiting for user data. */
6233 if (!in
|| !inlen
) break;
6235 /* FIXME: should we be using local-line-editing code instead? */
6236 while (!finished_prompt
&& inlen
) {
6242 term_data(term
, 0, "\r\n", 2);
6243 prompt_ensure_result_size(pr
, s
->pos
+ 1);
6244 pr
->result
[s
->pos
] = '\0';
6245 /* go to next prompt, if any */
6248 finished_prompt
= 1; /* break out */
6254 term_data(term
, 0, "\b \b", 3);
6260 while (s
->pos
> 0) {
6262 term_data(term
, 0, "\b \b", 3);
6268 /* Immediate abort. */
6269 term_data(term
, 0, "\r\n", 2);
6272 return 0; /* user abort */
6275 * This simplistic check for printability is disabled
6276 * when we're doing password input, because some people
6277 * have control characters in their passwords.
6279 if (!pr
->echo
|| (c
>= ' ' && c
<= '~') ||
6280 ((unsigned char) c
>= 160)) {
6281 prompt_ensure_result_size(pr
, s
->pos
+ 1);
6282 pr
->result
[s
->pos
++] = c
;
6284 term_data(term
, 0, &c
, 1);
6292 if (s
->curr_prompt
< p
->n_prompts
) {
6293 return -1; /* more data required */
6297 return +1; /* all done */