10 #define poslt(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x < (p2).x ) )
11 #define posle(p1,p2) ( (p1).y < (p2).y || ( (p1).y == (p2).y && (p1).x <= (p2).x ) )
12 #define poseq(p1,p2) ( (p1).y == (p2).y && (p1).x == (p2).x )
13 #define posdiff(p1,p2) ( ((p1).y - (p2).y) * (term->cols+1) + (p1).x - (p2).x )
15 /* Product-order comparisons for rectangular block selection. */
16 #define posPlt(p1,p2) ( (p1).y <= (p2).y && (p1).x < (p2).x )
17 #define posPle(p1,p2) ( (p1).y <= (p2).y && (p1).x <= (p2).x )
19 #define incpos(p) ( (p).x == term->cols ? ((p).x = 0, (p).y++, 1) : ((p).x++, 0) )
20 #define decpos(p) ( (p).x == 0 ? ((p).x = term->cols, (p).y--, 1) : ((p).x--, 0) )
24 #define CL_ANSIMIN 0x0001 /* Codes in all ANSI like terminals. */
25 #define CL_VT100 0x0002 /* VT100 */
26 #define CL_VT100AVO 0x0004 /* VT100 +AVO; 132x24 (not 132x14) & attrs */
27 #define CL_VT102 0x0008 /* VT102 */
28 #define CL_VT220 0x0010 /* VT220 */
29 #define CL_VT320 0x0020 /* VT320 */
30 #define CL_VT420 0x0040 /* VT420 */
31 #define CL_VT510 0x0080 /* VT510, NB VT510 includes ANSI */
32 #define CL_VT340TEXT 0x0100 /* VT340 extensions that appear in the VT420 */
33 #define CL_SCOANSI 0x1000 /* SCOANSI not in ANSIMIN. */
34 #define CL_ANSI 0x2000 /* ANSI ECMA-48 not in the VT100..VT420 */
35 #define CL_OTHER 0x4000 /* Others, Xterm, linux, putty, dunno, etc */
37 #define TM_VT100 (CL_ANSIMIN|CL_VT100)
38 #define TM_VT100AVO (TM_VT100|CL_VT100AVO)
39 #define TM_VT102 (TM_VT100AVO|CL_VT102)
40 #define TM_VT220 (TM_VT102|CL_VT220)
41 #define TM_VTXXX (TM_VT220|CL_VT340TEXT|CL_VT510|CL_VT420|CL_VT320)
42 #define TM_SCOANSI (CL_ANSIMIN|CL_SCOANSI)
44 #define TM_PUTTY (0xFFFF)
46 #define compatibility(x) \
47 if ( ((CL_##x)&term->compatibility_level) == 0 ) { \
48 term->termstate=TOPLEVEL; \
51 #define compatibility2(x,y) \
52 if ( ((CL_##x|CL_##y)&term->compatibility_level) == 0 ) { \
53 term->termstate=TOPLEVEL; \
57 #define has_compat(x) ( ((CL_##x)&term->compatibility_level) != 0 )
59 const char sco2ansicolour
[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
61 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
62 const wchar_t sel_nl
[] = SEL_NL
;
65 * Fetch the character at a particular position in a line array,
66 * for purposes of `wordtype'. The reason this isn't just a simple
67 * array reference is that if the character we find is UCSWIDE,
68 * then we must look one space further to the left.
70 #define UCSGET(a, x) \
71 ( (x)>0 && (a)[(x)].chr == UCSWIDE ? (a)[(x)-1].chr : (a)[(x)].chr )
74 * Detect the various aliases of U+0020 SPACE.
76 #define IS_SPACE_CHR(chr) \
77 ((chr) == 0x20 || (DIRECT_CHAR(chr) && ((chr) & 0xFF) == 0x20))
82 #define CSET_OF(chr) (DIRECT_CHAR(chr)||DIRECT_FONT(chr) ? (chr)&CSET_MASK : 0)
85 * Internal prototypes.
87 static void resizeline(Terminal
*, termline
*, int);
88 static termline
*lineptr(Terminal
*, int, int, int);
89 static void unlineptr(termline
*);
90 static void do_paint(Terminal
*, Context
, int);
91 static void erase_lots(Terminal
*, int, int, int);
92 static void swap_screen(Terminal
*, int, int, int);
93 static void update_sbar(Terminal
*);
94 static void deselect(Terminal
*);
95 static void term_print_finish(Terminal
*);
96 #ifdef OPTIMISE_SCROLL
97 static void scroll_display(Terminal
*, int, int, int);
98 #endif /* OPTIMISE_SCROLL */
100 static termline
*newline(Terminal
*term
, int cols
, int bce
)
105 line
= snew(termline
);
106 line
->chars
= snewn(cols
, termchar
);
107 for (j
= 0; j
< cols
; j
++)
108 line
->chars
[j
] = (bce ? term
->erase_char
: term
->basic_erase_char
);
109 line
->cols
= line
->size
= cols
;
110 line
->lattr
= LATTR_NORM
;
111 line
->temporary
= FALSE
;
117 static void freeline(termline
*line
)
125 static void unlineptr(termline
*line
)
132 * Diagnostic function: verify that a termline has a correct
133 * combining character structure.
135 * XXX-REMOVE-BEFORE-RELEASE: This is a performance-intensive
136 * check. Although it's currently really useful for getting all the
137 * bugs out of the new cc stuff, it will want to be absent when we
138 * make a proper release.
140 static void cc_check(termline
*line
)
142 unsigned char *flags
;
145 assert(line
->size
>= line
->cols
);
147 flags
= snewn(line
->size
, unsigned char);
149 for (i
= 0; i
< line
->size
; i
++)
150 flags
[i
] = (i
< line
->cols
);
152 for (i
= 0; i
< line
->cols
; i
++) {
154 while (line
->chars
[j
].cc_next
) {
155 j
+= line
->chars
[j
].cc_next
;
156 assert(j
>= line
->cols
&& j
< line
->size
);
165 assert(j
>= line
->cols
&& j
< line
->size
);
168 if (line
->chars
[j
].cc_next
)
169 j
+= line
->chars
[j
].cc_next
;
176 for (i
= 0; i
< line
->size
; i
++)
177 j
+= (flags
[i
] != 0);
179 assert(j
== line
->size
);
183 * Add a combining character to a character cell.
185 static void add_cc(termline
*line
, int col
, unsigned long chr
)
189 assert(col
>= 0 && col
< line
->cols
);
192 * Start by extending the cols array if the free list is empty.
194 if (!line
->cc_free
) {
196 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
197 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
199 while (n
< line
->size
) {
200 if (n
+1 < line
->size
)
201 line
->chars
[n
].cc_next
= 1;
203 line
->chars
[n
].cc_next
= 0;
209 * Now walk the cc list of the cell in question.
211 while (line
->chars
[col
].cc_next
)
212 col
+= line
->chars
[col
].cc_next
;
215 * `col' now points at the last cc currently in this cell; so
216 * we simply add another one.
218 newcc
= line
->cc_free
;
219 if (line
->chars
[newcc
].cc_next
)
220 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
223 line
->chars
[newcc
].cc_next
= 0;
224 line
->chars
[newcc
].chr
= chr
;
225 line
->chars
[col
].cc_next
= newcc
- col
;
227 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
231 * Clear the combining character list in a character cell.
233 static void clear_cc(termline
*line
, int col
)
235 int oldfree
, origcol
= col
;
237 assert(col
>= 0 && col
< line
->cols
);
239 if (!line
->chars
[col
].cc_next
)
240 return; /* nothing needs doing */
242 oldfree
= line
->cc_free
;
243 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
244 while (line
->chars
[col
].cc_next
)
245 col
+= line
->chars
[col
].cc_next
;
247 line
->chars
[col
].cc_next
= oldfree
- col
;
249 line
->chars
[col
].cc_next
= 0;
251 line
->chars
[origcol
].cc_next
= 0;
253 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
257 * Compare two character cells for equality. Special case required
258 * in do_paint() where we override what we expect the chr and attr
261 static int termchars_equal_override(termchar
*a
, termchar
*b
,
262 unsigned long bchr
, unsigned long battr
)
267 if (a
->attr
!= battr
)
269 while (a
->cc_next
|| b
->cc_next
) {
270 if (!a
->cc_next
|| !b
->cc_next
)
271 return FALSE
; /* one cc-list ends, other does not */
274 if (a
->chr
!= b
->chr
)
280 static int termchars_equal(termchar
*a
, termchar
*b
)
282 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
286 * Copy a character cell. (Requires a pointer to the destination
287 * termline, so as to access its free list.)
289 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
291 clear_cc(destline
, x
);
293 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
294 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
296 while (src
->cc_next
) {
298 add_cc(destline
, x
, src
->chr
);
301 cc_check(destline
); /* XXX-REMOVE-BEFORE-RELEASE */
305 * Move a character cell within its termline.
307 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
309 /* First clear the cc list from the original char, just in case. */
310 clear_cc(line
, dest
- line
->chars
);
312 /* Move the character cell and adjust its cc_next. */
313 *dest
= *src
; /* copy everything except cc-list */
315 dest
->cc_next
= src
->cc_next
- (dest
-src
);
317 /* Ensure the original cell doesn't have a cc list. */
320 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
324 * Compress and decompress a termline into an RLE-based format for
325 * storing in scrollback. (Since scrollback almost never needs to
326 * be modified and exists in huge quantities, this is a sensible
327 * tradeoff, particularly since it allows us to continue adding
328 * features to the main termchar structure without proportionally
329 * bloating the terminal emulator's memory footprint unless those
330 * features are in constant use.)
336 static void add(struct buf
*b
, unsigned char c
)
338 if (b
->len
>= b
->size
) {
339 b
->size
= (b
->len
* 3 / 2) + 512;
340 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
342 b
->data
[b
->len
++] = c
;
344 static int get(struct buf
*b
)
346 return b
->data
[b
->len
++];
348 static void makerle(struct buf
*b
, termline
*ldata
,
349 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
350 unsigned long *state
))
352 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
353 termchar
*c
= ldata
->chars
;
354 unsigned long state
= 0, oldstate
;
361 prevlen
= prevpos
= 0;
366 makeliteral(b
, c
++, &state
);
367 thislen
= b
->len
- thispos
;
368 if (thislen
== prevlen
&&
369 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
371 * This literal precisely matches the previous one.
372 * Turn it into a run if it's worthwhile.
374 * With one-byte literals, it costs us two bytes to
375 * encode a run, plus another byte to write the header
376 * to resume normal output; so a three-element run is
377 * neutral, and anything beyond that is unconditionally
378 * worthwhile. With two-byte literals or more, even a
381 if (thislen
> 1 || prev2
) {
385 * It's worth encoding a run. Start at prevpos,
386 * unless hdrsize==0 in which case we can back up
387 * another one and start by overwriting hdrpos.
390 hdrsize
--; /* remove the literal at prevpos */
394 prevpos
-= prevlen
;/* and possibly another one */
398 assert(prevpos
== hdrpos
+ 1);
400 b
->len
= prevpos
+prevlen
;
402 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
404 b
->len
= prevpos
+prevlen
+1;
406 * Terminate the previous run of ordinary
409 assert(hdrsize
>= 1 && hdrsize
<= 128);
410 b
->data
[hdrpos
] = hdrsize
- 1;
413 runlen
= prev2 ?
3 : 2;
415 while (n
> 0 && runlen
< 129) {
419 makeliteral(b
, c
, &state
);
420 tmplen
= b
->len
- tmppos
;
422 if (tmplen
!= thislen
||
423 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
425 break; /* run over */
430 assert(runlen
>= 2 && runlen
<= 129);
431 b
->data
[runpos
] = runlen
+ 0x80 - 2;
440 * Just flag that the previous two literals were
441 * identical, in case we find a third identical one
442 * we want to turn into a run.
455 * This character isn't (yet) part of a run. Add it to
459 if (hdrsize
== 128) {
460 b
->data
[hdrpos
] = hdrsize
- 1;
464 prevlen
= prevpos
= 0;
473 assert(hdrsize
<= 128);
474 b
->data
[hdrpos
] = hdrsize
- 1;
479 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
482 * My encoding for characters is UTF-8-like, in that it stores
483 * 7-bit ASCII in one byte and uses high-bit-set bytes as
484 * introducers to indicate a longer sequence. However, it's
485 * unlike UTF-8 in that it doesn't need to be able to
486 * resynchronise, and therefore I don't want to waste two bits
487 * per byte on having recognisable continuation characters.
488 * Also I don't want to rule out the possibility that I may one
489 * day use values 0x80000000-0xFFFFFFFF for interesting
490 * purposes, so unlike UTF-8 I need a full 32-bit range.
491 * Accordingly, here is my encoding:
493 * 00000000-0000007F: 0xxxxxxx (but see below)
494 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
495 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
496 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
497 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
499 * (`Z' is like `x' but is always going to be zero since the
500 * values I'm encoding don't go above 2^32. In principle the
501 * five-byte form of the encoding could extend to 2^35, and
502 * there could be six-, seven-, eight- and nine-byte forms as
503 * well to allow up to 64-bit values to be encoded. But that's
504 * completely unnecessary for these purposes!)
506 * The encoding as written above would be very simple, except
507 * that 7-bit ASCII can occur in several different ways in the
508 * terminal data; sometimes it crops up in the D800 page
509 * (CSET_ASCII) but at other times it's in the 0000 page (real
510 * Unicode). Therefore, this encoding is actually _stateful_:
511 * the one-byte encoding of 00-7F actually indicates `reuse the
512 * upper three bytes of the last character', and to encode an
513 * absolute value of 00-7F you need to use the two-byte form
516 if ((c
->chr
& ~0x7F) == *state
) {
517 add(b
, (unsigned char)(c
->chr
& 0x7F));
518 } else if (c
->chr
< 0x4000) {
519 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
520 add(b
, (unsigned char)(c
->chr
& 0xFF));
521 } else if (c
->chr
< 0x200000) {
522 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
523 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
524 add(b
, (unsigned char)(c
->chr
& 0xFF));
525 } else if (c
->chr
< 0x10000000) {
526 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
527 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
528 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
529 add(b
, (unsigned char)(c
->chr
& 0xFF));
532 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
533 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
534 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
535 add(b
, (unsigned char)(c
->chr
& 0xFF));
537 *state
= c
->chr
& ~0xFF;
539 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
542 * My encoding for attributes is 16-bit-granular and assumes
543 * that the top bit of the word is never required. I either
544 * store a two-byte value with the top bit clear (indicating
545 * just that value), or a four-byte value with the top bit set
546 * (indicating the same value with its top bit clear).
548 if (c
->attr
< 0x8000) {
549 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
550 add(b
, (unsigned char)(c
->attr
& 0xFF));
552 add(b
, (unsigned char)(((c
->attr
>> 24) & 0x7F) | 0x80));
553 add(b
, (unsigned char)((c
->attr
>> 16) & 0xFF));
554 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
555 add(b
, (unsigned char)(c
->attr
& 0xFF));
558 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
561 * For combining characters, I just encode a bunch of ordinary
562 * chars using makeliteral_chr, and terminate with a \0
563 * character (which I know won't come up as a combining char
566 * I don't use the stateful encoding in makeliteral_chr.
568 unsigned long zstate
;
577 makeliteral_chr(b
, c
, &zstate
);
582 makeliteral_chr(b
, &z
, &zstate
);
585 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
587 static unsigned char *compressline(termline
*ldata
)
589 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
592 * First, store the column count, 7 bits at a time, least
593 * significant `digit' first, with the high bit set on all but
599 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
602 add(b
, (unsigned char)(n
));
606 * Next store the lattrs; same principle.
609 int n
= ldata
->lattr
;
611 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
614 add(b
, (unsigned char)(n
));
618 * Now we store a sequence of separate run-length encoded
619 * fragments, each containing exactly as many symbols as there
620 * are columns in the ldata.
622 * All of these have a common basic format:
624 * - a byte 00-7F indicates that X+1 literals follow it
625 * - a byte 80-FF indicates that a single literal follows it
626 * and expects to be repeated (X-0x80)+2 times.
628 * The format of the `literals' varies between the fragments.
630 makerle(b
, ldata
, makeliteral_chr
);
631 makerle(b
, ldata
, makeliteral_attr
);
632 makerle(b
, ldata
, makeliteral_cc
);
635 * Diagnostics: ensure that the compressed data really does
636 * decompress to the right thing.
638 #ifndef CHECK_SB_COMPRESSION
644 #ifdef DIAGNOSTIC_SB_COMPRESSION
645 for (i
= 0; i
< b
->len
; i
++) {
646 printf(" %02x ", b
->data
[i
]);
651 dcl
= decompressline(b
->data
, &dused
);
652 assert(b
->len
== dused
);
653 assert(ldata
->cols
== dcl
->cols
);
654 assert(ldata
->lattr
== dcl
->lattr
);
655 for (i
= 0; i
< ldata
->cols
; i
++)
656 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
658 #ifdef DIAGNOSTIC_SB_COMPRESSION
659 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
660 ldata
->cols
, 4 * ldata
->cols
, dused
,
661 (double)dused
/ (4 * ldata
->cols
));
669 * Trim the allocated memory so we don't waste any, and return.
671 return sresize(b
->data
, b
->len
, unsigned char);
674 static void readrle(struct buf
*b
, termline
*ldata
,
675 void (*readliteral
)(struct buf
*b
, termchar
*c
,
676 termline
*ldata
, unsigned long *state
))
679 unsigned long state
= 0;
681 while (n
< ldata
->cols
) {
687 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
689 assert(n
< ldata
->cols
);
691 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
695 /* Just a sequence of consecutive literals. */
699 assert(n
< ldata
->cols
);
700 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
706 assert(n
== ldata
->cols
);
708 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
709 unsigned long *state
)
714 * 00000000-0000007F: 0xxxxxxx
715 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
716 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
717 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
718 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
723 c
->chr
= byte
| *state
;
724 } else if (byte
< 0xC0) {
725 c
->chr
= (byte
&~ 0xC0) << 8;
727 } else if (byte
< 0xE0) {
728 c
->chr
= (byte
&~ 0xE0) << 16;
729 c
->chr
|= get(b
) << 8;
731 } else if (byte
< 0xF0) {
732 c
->chr
= (byte
&~ 0xF0) << 24;
733 c
->chr
|= get(b
) << 16;
734 c
->chr
|= get(b
) << 8;
737 assert(byte
== 0xF0);
738 c
->chr
= get(b
) << 24;
739 c
->chr
|= get(b
) << 16;
740 c
->chr
|= get(b
) << 8;
743 *state
= c
->chr
& ~0xFF;
745 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
746 unsigned long *state
)
761 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
762 unsigned long *state
)
765 unsigned long zstate
;
766 int x
= c
- ldata
->chars
;
772 readliteral_chr(b
, &n
, ldata
, &zstate
);
775 add_cc(ldata
, x
, n
.chr
);
779 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
781 int ncols
, byte
, shift
;
782 struct buf buffer
, *b
= &buffer
;
789 * First read in the column count.
794 ncols
|= (byte
& 0x7F) << shift
;
796 } while (byte
& 0x80);
799 * Now create the output termline.
801 ldata
= snew(termline
);
802 ldata
->chars
= snewn(ncols
, termchar
);
803 ldata
->cols
= ldata
->size
= ncols
;
804 ldata
->temporary
= TRUE
;
808 * We must set all the cc pointers in ldata->chars to 0 right
809 * now, so that cc diagnostics that verify the integrity of the
810 * whole line will make sense while we're in the middle of
815 for (i
= 0; i
< ldata
->cols
; i
++)
816 ldata
->chars
[i
].cc_next
= 0;
820 * Now read in the lattr.
822 ldata
->lattr
= shift
= 0;
825 ldata
->lattr
|= (byte
& 0x7F) << shift
;
827 } while (byte
& 0x80);
830 * Now we read in each of the RLE streams in turn.
832 readrle(b
, ldata
, readliteral_chr
);
833 readrle(b
, ldata
, readliteral_attr
);
834 readrle(b
, ldata
, readliteral_cc
);
836 /* Return the number of bytes read, for diagnostic purposes. */
838 *bytes_used
= b
->len
;
844 * Resize a line to make it `cols' columns wide.
846 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
850 if (line
->cols
!= cols
) {
852 oldcols
= line
->cols
;
855 * This line is the wrong length, which probably means it
856 * hasn't been accessed since a resize. Resize it now.
858 * First, go through all the characters that will be thrown
859 * out in the resize (if we're shrinking the line) and
860 * return their cc lists to the cc free list.
862 for (i
= cols
; i
< oldcols
; i
++)
866 * If we're shrinking the line, we now bodily move the
867 * entire cc section from where it started to where it now
868 * needs to be. (We have to do this before the resize, so
869 * that the data we're copying is still there. However, if
870 * we're expanding, we have to wait until _after_ the
871 * resize so that the space we're copying into is there.)
874 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
875 (line
->size
- line
->cols
) * TSIZE
);
878 * Now do the actual resize, leaving the _same_ amount of
879 * cc space as there was to begin with.
881 line
->size
+= cols
- oldcols
;
882 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
886 * If we're expanding the line, _now_ we move the cc
890 memmove(line
->chars
+ cols
, line
->chars
+ oldcols
,
891 (line
->size
- line
->cols
) * TSIZE
);
894 * Go through what's left of the original line, and adjust
895 * the first cc_next pointer in each list. (All the
896 * subsequent ones are still valid because they are
897 * relative offsets within the cc block.) Also do the same
898 * to the head of the cc_free list.
900 for (i
= 0; i
< oldcols
&& i
< cols
; i
++)
901 if (line
->chars
[i
].cc_next
)
902 line
->chars
[i
].cc_next
+= cols
- oldcols
;
904 line
->cc_free
+= cols
- oldcols
;
907 * And finally fill in the new space with erase chars. (We
908 * don't have to worry about cc lists here, because we
909 * _know_ the erase char doesn't have one.)
911 for (i
= oldcols
; i
< cols
; i
++)
912 line
->chars
[i
] = term
->basic_erase_char
;
914 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
919 * Get the number of lines in the scrollback.
921 static int sblines(Terminal
*term
)
923 int sblines
= count234(term
->scrollback
);
924 if (term
->cfg
.erase_to_scrollback
&&
925 term
->alt_which
&& term
->alt_screen
) {
926 sblines
+= term
->alt_sblines
;
932 * Retrieve a line of the screen or of the scrollback, according to
933 * whether the y coordinate is non-negative or negative
936 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
943 whichtree
= term
->screen
;
950 if (term
->cfg
.erase_to_scrollback
&&
951 term
->alt_which
&& term
->alt_screen
) {
952 altlines
= term
->alt_sblines
;
955 whichtree
= term
->scrollback
;
956 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
958 whichtree
= term
->alt_screen
;
959 treeindex
= y
+ term
->alt_sblines
;
960 /* treeindex = y + count234(term->alt_screen); */
963 if (whichtree
== term
->scrollback
) {
964 unsigned char *cline
= index234(whichtree
, treeindex
);
965 line
= decompressline(cline
, NULL
);
967 line
= index234(whichtree
, treeindex
);
970 /* We assume that we don't screw up and retrieve something out of range. */
971 assert(line
!= NULL
);
973 resizeline(term
, line
, term
->cols
);
974 /* FIXME: should we sort the compressed scrollback out here? */
979 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
980 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
983 * Set up power-on settings for the terminal.
985 static void power_on(Terminal
*term
)
987 term
->curs
.x
= term
->curs
.y
= 0;
988 term
->alt_x
= term
->alt_y
= 0;
989 term
->savecurs
.x
= term
->savecurs
.y
= 0;
990 term
->alt_t
= term
->marg_t
= 0;
991 if (term
->rows
!= -1)
992 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
994 term
->alt_b
= term
->marg_b
= 0;
995 if (term
->cols
!= -1) {
997 for (i
= 0; i
< term
->cols
; i
++)
998 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1000 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1001 term
->alt_ins
= term
->insert
= FALSE
;
1002 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
1003 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1004 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
1005 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
1006 term
->utf_state
= 0;
1007 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
1008 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= CSET_ASCII
;
1010 term
->in_vbell
= FALSE
;
1011 term
->cursor_on
= 1;
1012 term
->big_cursor
= 0;
1013 term
->default_attr
= term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
1014 term
->term_editing
= term
->term_echoing
= FALSE
;
1015 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
1016 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
1017 term
->use_bce
= term
->cfg
.bce
;
1018 term
->blink_is_real
= term
->cfg
.blinktext
;
1019 term
->erase_char
= term
->basic_erase_char
;
1020 term
->alt_which
= 0;
1021 term_print_finish(term
);
1024 for (i
= 0; i
< 256; i
++)
1025 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1028 swap_screen(term
, 1, FALSE
, FALSE
);
1029 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1030 swap_screen(term
, 0, FALSE
, FALSE
);
1031 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1036 * Force a screen update.
1038 void term_update(Terminal
*term
)
1041 ctx
= get_ctx(term
->frontend
);
1043 int need_sbar_update
= term
->seen_disp_event
;
1044 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
1045 term
->disptop
= 0; /* return to main screen */
1046 term
->seen_disp_event
= 0;
1047 need_sbar_update
= TRUE
;
1050 if (need_sbar_update
)
1052 do_paint(term
, ctx
, TRUE
);
1053 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1059 * Called from front end when a keypress occurs, to trigger
1060 * anything magical that needs to happen in that situation.
1062 void term_seen_key_event(Terminal
*term
)
1065 * On any keypress, clear the bell overload mechanism
1066 * completely, on the grounds that large numbers of
1067 * beeps coming from deliberate key action are likely
1068 * to be intended (e.g. beeps from filename completion
1069 * blocking repeatedly).
1071 term
->beep_overloaded
= FALSE
;
1072 while (term
->beephead
) {
1073 struct beeptime
*tmp
= term
->beephead
;
1074 term
->beephead
= tmp
->next
;
1077 term
->beeptail
= NULL
;
1081 * Reset the scrollback on keypress, if we're doing that.
1083 if (term
->cfg
.scroll_on_key
) {
1084 term
->disptop
= 0; /* return to main screen */
1085 term
->seen_disp_event
= 1;
1090 * Same as power_on(), but an external function.
1092 void term_pwron(Terminal
*term
)
1095 if (term
->ldisc
) /* cause ldisc to notice changes */
1096 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1102 static void set_erase_char(Terminal
*term
)
1104 term
->erase_char
= term
->basic_erase_char
;
1106 term
->erase_char
.attr
= (term
->curr_attr
&
1107 (ATTR_FGMASK
| ATTR_BGMASK
));
1111 * When the user reconfigures us, we need to check the forbidden-
1112 * alternate-screen config option, disable raw mouse mode if the
1113 * user has disabled mouse reporting, and abandon a print job if
1114 * the user has disabled printing.
1116 void term_reconfig(Terminal
*term
, Config
*cfg
)
1119 * Before adopting the new config, check all those terminal
1120 * settings which control power-on defaults; and if they've
1121 * changed, we will modify the current state as well as the
1122 * default one. The full list is: Auto wrap mode, DEC Origin
1123 * Mode, BCE, blinking text, character classes.
1125 int reset_wrap
, reset_decom
, reset_bce
, reset_blink
, reset_charclass
;
1128 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
1129 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
1130 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
1131 reset_blink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
1132 reset_charclass
= 0;
1133 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
1134 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
1135 reset_charclass
= 1;
1137 term
->cfg
= *cfg
; /* STRUCTURE COPY */
1140 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1142 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1144 term
->use_bce
= term
->cfg
.bce
;
1145 set_erase_char(term
);
1148 term
->blink_is_real
= term
->cfg
.blinktext
;
1149 if (reset_charclass
)
1150 for (i
= 0; i
< 256; i
++)
1151 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1153 if (term
->cfg
.no_alt_screen
)
1154 swap_screen(term
, 0, FALSE
, FALSE
);
1155 if (term
->cfg
.no_mouse_rep
) {
1156 term
->xterm_mouse
= 0;
1157 set_raw_mouse_mode(term
->frontend
, 0);
1159 if (term
->cfg
.no_remote_charset
) {
1160 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1161 term
->sco_acs
= term
->alt_sco_acs
= 0;
1164 if (!*term
->cfg
.printer
) {
1165 term_print_finish(term
);
1170 * Clear the scrollback.
1172 void term_clrsb(Terminal
*term
)
1176 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1177 sfree(line
); /* this is compressed data, not a termline */
1179 term
->tempsblines
= 0;
1180 term
->alt_sblines
= 0;
1185 * Initialise the terminal.
1187 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
1193 * Allocate a new Terminal structure and initialise the fields
1196 term
= snew(Terminal
);
1197 term
->frontend
= frontend
;
1198 term
->ucsdata
= ucsdata
;
1199 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
1200 term
->logctx
= NULL
;
1201 term
->compatibility_level
= TM_PUTTY
;
1202 strcpy(term
->id_string
, "\033[?6c");
1203 term
->last_blink
= term
->last_tblink
= 0;
1204 term
->paste_buffer
= NULL
;
1205 term
->paste_len
= 0;
1206 term
->last_paste
= 0;
1207 bufchain_init(&term
->inbuf
);
1208 bufchain_init(&term
->printer_buf
);
1209 term
->printing
= term
->only_printing
= FALSE
;
1210 term
->print_job
= NULL
;
1211 term
->vt52_mode
= FALSE
;
1212 term
->cr_lf_return
= FALSE
;
1213 term
->seen_disp_event
= FALSE
;
1214 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
1215 term
->reset_132
= FALSE
;
1216 term
->blinker
= term
->tblinker
= 0;
1217 term
->has_focus
= 1;
1218 term
->repeat_off
= FALSE
;
1219 term
->termstate
= TOPLEVEL
;
1220 term
->selstate
= NO_SELECTION
;
1223 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1224 term
->tempsblines
= 0;
1225 term
->alt_sblines
= 0;
1227 term
->disptext
= NULL
;
1228 term
->dispcursx
= term
->dispcursy
= -1;
1231 term
->rows
= term
->cols
= -1;
1233 term
->beephead
= term
->beeptail
= NULL
;
1234 #ifdef OPTIMISE_SCROLL
1235 term
->scrollhead
= term
->scrolltail
= NULL
;
1236 #endif /* OPTIMISE_SCROLL */
1238 term
->lastbeep
= FALSE
;
1239 term
->beep_overloaded
= FALSE
;
1240 term
->attr_mask
= 0xffffffff;
1241 term
->resize_fn
= NULL
;
1242 term
->resize_ctx
= NULL
;
1243 term
->in_term_out
= FALSE
;
1245 term
->ltemp_size
= 0;
1246 term
->wcFrom
= NULL
;
1248 term
->wcFromTo_size
= 0;
1250 term
->bidi_cache_size
= 0;
1251 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1254 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1255 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1256 term
->basic_erase_char
.cc_next
= 0;
1257 term
->erase_char
= term
->basic_erase_char
;
1262 void term_free(Terminal
*term
)
1265 struct beeptime
*beep
;
1268 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1269 sfree(line
); /* compressed data, not a termline */
1270 freetree234(term
->scrollback
);
1271 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1273 freetree234(term
->screen
);
1274 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1276 freetree234(term
->alt_screen
);
1277 if (term
->disptext
) {
1278 for (i
= 0; i
< term
->rows
; i
++)
1279 freeline(term
->disptext
[i
]);
1281 sfree(term
->disptext
);
1282 while (term
->beephead
) {
1283 beep
= term
->beephead
;
1284 term
->beephead
= beep
->next
;
1287 bufchain_clear(&term
->inbuf
);
1289 printer_finish_job(term
->print_job
);
1290 bufchain_clear(&term
->printer_buf
);
1291 sfree(term
->paste_buffer
);
1293 sfree(term
->wcFrom
);
1296 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1297 sfree(term
->pre_bidi_cache
[i
]);
1298 sfree(term
->post_bidi_cache
[i
]);
1300 sfree(term
->pre_bidi_cache
);
1301 sfree(term
->post_bidi_cache
);
1307 * Set up the terminal for a given size.
1309 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1312 termline
**newdisp
, *line
;
1313 int i
, j
, oldrows
= term
->rows
;
1315 int save_alt_which
= term
->alt_which
;
1317 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1318 newsavelines
== term
->savelines
)
1319 return; /* nothing to do */
1322 swap_screen(term
, 0, FALSE
, FALSE
);
1324 term
->alt_t
= term
->marg_t
= 0;
1325 term
->alt_b
= term
->marg_b
= newrows
- 1;
1327 if (term
->rows
== -1) {
1328 term
->scrollback
= newtree234(NULL
);
1329 term
->screen
= newtree234(NULL
);
1330 term
->tempsblines
= 0;
1335 * Resize the screen and scrollback. We only need to shift
1336 * lines around within our data structures, because lineptr()
1337 * will take care of resizing each individual line if
1340 * - If the new screen is longer, we shunt lines in from temporary
1341 * scrollback if possible, otherwise we add new blank lines at
1344 * - If the new screen is shorter, we remove any blank lines at
1345 * the bottom if possible, otherwise shunt lines above the cursor
1346 * to scrollback if possible, otherwise delete lines below the
1349 * - Then, if the new scrollback length is less than the
1350 * amount of scrollback we actually have, we must throw some
1353 sblen
= count234(term
->scrollback
);
1354 /* Do this loop to expand the screen if newrows > rows */
1355 assert(term
->rows
== count234(term
->screen
));
1356 while (term
->rows
< newrows
) {
1357 if (term
->tempsblines
> 0) {
1358 unsigned char *cline
;
1359 /* Insert a line from the scrollback at the top of the screen. */
1360 assert(sblen
>= term
->tempsblines
);
1361 cline
= delpos234(term
->scrollback
, --sblen
);
1362 line
= decompressline(cline
, NULL
);
1364 line
->temporary
= FALSE
; /* reconstituted line is now real */
1365 term
->tempsblines
-= 1;
1366 addpos234(term
->screen
, line
, 0);
1368 term
->savecurs
.y
+= 1;
1370 /* Add a new blank line at the bottom of the screen. */
1371 line
= newline(term
, newcols
, FALSE
);
1372 addpos234(term
->screen
, line
, count234(term
->screen
));
1376 /* Do this loop to shrink the screen if newrows < rows */
1377 while (term
->rows
> newrows
) {
1378 if (term
->curs
.y
< term
->rows
- 1) {
1379 /* delete bottom row, unless it contains the cursor */
1380 sfree(delpos234(term
->screen
, term
->rows
- 1));
1382 /* push top row to scrollback */
1383 line
= delpos234(term
->screen
, 0);
1384 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1386 term
->tempsblines
+= 1;
1388 term
->savecurs
.y
-= 1;
1392 assert(term
->rows
== newrows
);
1393 assert(count234(term
->screen
) == newrows
);
1395 /* Delete any excess lines from the scrollback. */
1396 while (sblen
> newsavelines
) {
1397 line
= delpos234(term
->scrollback
, 0);
1401 if (sblen
< term
->tempsblines
)
1402 term
->tempsblines
= sblen
;
1403 assert(count234(term
->scrollback
) <= newsavelines
);
1404 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1407 /* Make a new displayed text buffer. */
1408 newdisp
= snewn(newrows
, termline
*);
1409 for (i
= 0; i
< newrows
; i
++) {
1410 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1411 for (j
= 0; j
< newcols
; j
++)
1412 newdisp
[i
]->chars
[i
].attr
= ATTR_INVALID
;
1414 if (term
->disptext
) {
1415 for (i
= 0; i
< oldrows
; i
++)
1416 freeline(term
->disptext
[i
]);
1418 sfree(term
->disptext
);
1419 term
->disptext
= newdisp
;
1420 term
->dispcursx
= term
->dispcursy
= -1;
1422 /* Make a new alternate screen. */
1423 newalt
= newtree234(NULL
);
1424 for (i
= 0; i
< newrows
; i
++) {
1425 line
= newline(term
, newcols
, TRUE
);
1426 addpos234(newalt
, line
, i
);
1428 if (term
->alt_screen
) {
1429 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1431 freetree234(term
->alt_screen
);
1433 term
->alt_screen
= newalt
;
1434 term
->alt_sblines
= 0;
1436 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1439 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1440 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1443 /* Check that the cursor positions are still valid. */
1444 if (term
->savecurs
.y
< 0)
1445 term
->savecurs
.y
= 0;
1446 if (term
->savecurs
.y
>= newrows
)
1447 term
->savecurs
.y
= newrows
- 1;
1448 if (term
->curs
.y
< 0)
1450 if (term
->curs
.y
>= newrows
)
1451 term
->curs
.y
= newrows
- 1;
1452 if (term
->curs
.x
>= newcols
)
1453 term
->curs
.x
= newcols
- 1;
1454 term
->alt_x
= term
->alt_y
= 0;
1455 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1457 term
->rows
= newrows
;
1458 term
->cols
= newcols
;
1459 term
->savelines
= newsavelines
;
1461 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1465 if (term
->resize_fn
)
1466 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1470 * Hand a function and context pointer to the terminal which it can
1471 * use to notify a back end of resizes.
1473 void term_provide_resize_fn(Terminal
*term
,
1474 void (*resize_fn
)(void *, int, int),
1477 term
->resize_fn
= resize_fn
;
1478 term
->resize_ctx
= resize_ctx
;
1479 if (term
->cols
> 0 && term
->rows
> 0)
1480 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1483 /* Find the bottom line on the screen that has any content.
1484 * If only the top line has content, returns 0.
1485 * If no lines have content, return -1.
1487 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1490 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1491 termline
*line
= index234(screen
, i
);
1493 for (j
= 0; j
< line
->cols
; j
++)
1494 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1496 if (j
!= line
->cols
) break;
1502 * Swap screens. If `reset' is TRUE and we have been asked to
1503 * switch to the alternate screen, we must bring most of its
1504 * configuration from the main screen and erase the contents of the
1505 * alternate screen completely. (This is even true if we're already
1506 * on it! Blame xterm.)
1508 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1514 reset
= FALSE
; /* do no weird resetting if which==0 */
1516 if (which
!= term
->alt_which
) {
1517 term
->alt_which
= which
;
1519 ttr
= term
->alt_screen
;
1520 term
->alt_screen
= term
->screen
;
1522 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1524 if (!reset
&& !keep_cur_pos
)
1525 term
->curs
.x
= term
->alt_x
;
1528 if (!reset
&& !keep_cur_pos
)
1529 term
->curs
.y
= term
->alt_y
;
1532 if (!reset
) term
->marg_t
= term
->alt_t
;
1535 if (!reset
) term
->marg_b
= term
->alt_b
;
1538 if (!reset
) term
->dec_om
= term
->alt_om
;
1541 if (!reset
) term
->wrap
= term
->alt_wrap
;
1544 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1545 term
->alt_wnext
= t
;
1547 if (!reset
) term
->insert
= term
->alt_ins
;
1550 if (!reset
) term
->cset
= term
->alt_cset
;
1553 if (!reset
) term
->utf
= term
->alt_utf
;
1556 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1557 term
->alt_sco_acs
= t
;
1560 if (reset
&& term
->screen
) {
1562 * Yes, this _is_ supposed to honour background-colour-erase.
1564 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1569 * Update the scroll bar.
1571 static void update_sbar(Terminal
*term
)
1573 int nscroll
= sblines(term
);
1574 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1575 nscroll
+ term
->disptop
, term
->rows
);
1579 * Check whether the region bounded by the two pointers intersects
1580 * the scroll region, and de-select the on-screen selection if so.
1582 static void check_selection(Terminal
*term
, pos from
, pos to
)
1584 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1589 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1590 * for backward.) `sb' is TRUE if the scrolling is permitted to
1591 * affect the scrollback buffer.
1593 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1596 int i
, seltop
, olddisptop
, shift
;
1598 if (topline
!= 0 || term
->alt_which
!= 0)
1601 olddisptop
= term
->disptop
;
1605 line
= delpos234(term
->screen
, botline
);
1606 resizeline(term
, line
, term
->cols
);
1607 for (i
= 0; i
< term
->cols
; i
++)
1608 copy_termchar(line
, i
, &term
->erase_char
);
1609 line
->lattr
= LATTR_NORM
;
1610 addpos234(term
->screen
, line
, topline
);
1612 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
1614 if (term
->selstart
.y
> botline
) {
1615 term
->selstart
.y
= botline
+ 1;
1616 term
->selstart
.x
= 0;
1619 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
1621 if (term
->selend
.y
> botline
) {
1622 term
->selend
.y
= botline
+ 1;
1631 line
= delpos234(term
->screen
, topline
);
1632 cc_check(line
); /* XXX-REMOVE-BEFORE-RELEASE */
1633 if (sb
&& term
->savelines
> 0) {
1634 int sblen
= count234(term
->scrollback
);
1636 * We must add this line to the scrollback. We'll
1637 * remove a line from the top of the scrollback if
1638 * the scrollback is full.
1640 if (sblen
== term
->savelines
) {
1641 unsigned char *cline
;
1644 cline
= delpos234(term
->scrollback
, 0);
1647 term
->tempsblines
+= 1;
1649 addpos234(term
->scrollback
, compressline(line
), sblen
);
1651 line
= newline(term
, term
->cols
, TRUE
);
1654 * If the user is currently looking at part of the
1655 * scrollback, and they haven't enabled any options
1656 * that are going to reset the scrollback as a
1657 * result of this movement, then the chances are
1658 * they'd like to keep looking at the same line. So
1659 * we move their viewpoint at the same rate as the
1660 * scroll, at least until their viewpoint hits the
1661 * top end of the scrollback buffer, at which point
1662 * we don't have the choice any more.
1664 * Thanks to Jan Holmen Holsten for the idea and
1665 * initial implementation.
1667 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
1670 resizeline(term
, line
, term
->cols
);
1671 for (i
= 0; i
< term
->cols
; i
++)
1672 copy_termchar(line
, i
, &term
->erase_char
);
1673 line
->lattr
= LATTR_NORM
;
1674 addpos234(term
->screen
, line
, botline
);
1677 * If the selection endpoints move into the scrollback,
1678 * we keep them moving until they hit the top. However,
1679 * of course, if the line _hasn't_ moved into the
1680 * scrollback then we don't do this, and cut them off
1681 * at the top of the scroll region.
1683 * This applies to selstart and selend (for an existing
1684 * selection), and also selanchor (for one being
1685 * selected as we speak).
1687 seltop
= sb ?
-term
->savelines
: topline
;
1689 if (term
->selstate
!= NO_SELECTION
) {
1690 if (term
->selstart
.y
>= seltop
&&
1691 term
->selstart
.y
<= botline
) {
1693 if (term
->selstart
.y
< seltop
) {
1694 term
->selstart
.y
= seltop
;
1695 term
->selstart
.x
= 0;
1698 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
1700 if (term
->selend
.y
< seltop
) {
1701 term
->selend
.y
= seltop
;
1705 if (term
->selanchor
.y
>= seltop
&&
1706 term
->selanchor
.y
<= botline
) {
1707 term
->selanchor
.y
--;
1708 if (term
->selanchor
.y
< seltop
) {
1709 term
->selanchor
.y
= seltop
;
1710 term
->selanchor
.x
= 0;
1718 #ifdef OPTIMISE_SCROLL
1719 shift
+= term
->disptop
- olddisptop
;
1720 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
1721 scroll_display(term
, topline
, botline
, shift
);
1722 #endif /* OPTIMISE_SCROLL */
1725 #ifdef OPTIMISE_SCROLL
1727 * Add a scroll of a region on the screen into the pending scroll list.
1728 * `lines' is +ve for scrolling forward, -ve for backward.
1730 * If the scroll is on the same area as the last scroll in the list,
1733 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
1735 struct scrollregion
*newscroll
;
1736 if (term
->scrolltail
&&
1737 term
->scrolltail
->topline
== topline
&&
1738 term
->scrolltail
->botline
== botline
) {
1739 term
->scrolltail
->lines
+= lines
;
1741 newscroll
= snew(struct scrollregion
);
1742 newscroll
->topline
= topline
;
1743 newscroll
->botline
= botline
;
1744 newscroll
->lines
= lines
;
1745 newscroll
->next
= NULL
;
1747 if (!term
->scrollhead
)
1748 term
->scrollhead
= newscroll
;
1750 term
->scrolltail
->next
= newscroll
;
1751 term
->scrolltail
= newscroll
;
1756 * Scroll the physical display, and our conception of it in disptext.
1758 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
1760 int distance
, nlines
, i
, j
;
1762 distance
= lines
> 0 ? lines
: -lines
;
1763 nlines
= botline
- topline
+ 1 - distance
;
1765 for (i
= 0; i
< nlines
; i
++)
1766 for (j
= 0; j
< term
->cols
; j
++)
1767 copy_termchar(term
->disptext
[start
+i
], j
,
1768 term
->disptext
[start
+i
+distance
]->chars
+j
);
1769 if (term
->dispcursy
>= 0 &&
1770 term
->dispcursy
>= topline
+ distance
&&
1771 term
->dispcursy
< topline
+ distance
+ nlines
)
1772 term
->dispcursy
-= distance
;
1773 for (i
= 0; i
< distance
; i
++)
1774 for (j
= 0; j
< term
->cols
; j
++)
1775 term
->disptext
[start
+nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1777 for (i
= nlines
; i
-- ;)
1778 for (j
= 0; j
< term
->cols
; j
++)
1779 copy_termchar(term
->disptext
[start
+i
+distance
], j
,
1780 term
->disptext
[start
+i
]->chars
+j
);
1781 if (term
->dispcursy
>= 0 &&
1782 term
->dispcursy
>= topline
&&
1783 term
->dispcursy
< topline
+ nlines
)
1784 term
->dispcursy
+= distance
;
1785 for (i
= 0; i
< distance
; i
++)
1786 for (j
= 0; j
< term
->cols
; j
++)
1787 term
->disptext
[start
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1789 save_scroll(term
, topline
, botline
, lines
);
1791 #endif /* OPTIMISE_SCROLL */
1794 * Move the cursor to a given position, clipping at boundaries. We
1795 * may or may not want to clip at the scroll margin: marg_clip is 0
1796 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1797 * even _being_ outside the margins.
1799 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
1803 if (x
>= term
->cols
)
1806 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
1809 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
1815 if (y
>= term
->rows
)
1819 term
->wrapnext
= FALSE
;
1823 * Save or restore the cursor and SGR mode.
1825 static void save_cursor(Terminal
*term
, int save
)
1828 term
->savecurs
= term
->curs
;
1829 term
->save_attr
= term
->curr_attr
;
1830 term
->save_cset
= term
->cset
;
1831 term
->save_utf
= term
->utf
;
1832 term
->save_wnext
= term
->wrapnext
;
1833 term
->save_csattr
= term
->cset_attr
[term
->cset
];
1834 term
->save_sco_acs
= term
->sco_acs
;
1836 term
->curs
= term
->savecurs
;
1837 /* Make sure the window hasn't shrunk since the save */
1838 if (term
->curs
.x
>= term
->cols
)
1839 term
->curs
.x
= term
->cols
- 1;
1840 if (term
->curs
.y
>= term
->rows
)
1841 term
->curs
.y
= term
->rows
- 1;
1843 term
->curr_attr
= term
->save_attr
;
1844 term
->cset
= term
->save_cset
;
1845 term
->utf
= term
->save_utf
;
1846 term
->wrapnext
= term
->save_wnext
;
1848 * wrapnext might reset to False if the x position is no
1849 * longer at the rightmost edge.
1851 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
1852 term
->wrapnext
= FALSE
;
1853 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
1854 term
->sco_acs
= term
->save_sco_acs
;
1855 set_erase_char(term
);
1860 * This function is called before doing _anything_ which affects
1861 * only part of a line of text. It is used to mark the boundary
1862 * between two character positions, and it indicates that some sort
1863 * of effect is going to happen on only one side of that boundary.
1865 * The effect of this function is to check whether a CJK
1866 * double-width character is straddling the boundary, and to remove
1867 * it and replace it with two spaces if so. (Of course, one or
1868 * other of those spaces is then likely to be replaced with
1869 * something else again, as a result of whatever happens next.)
1871 * Also, if the boundary is at the right-hand _edge_ of the screen,
1872 * it implies something deliberate is being done to the rightmost
1873 * column position; hence we must clear LATTR_WRAPPED2.
1875 * The input to the function is the coordinates of the _second_
1876 * character of the pair.
1878 static void check_boundary(Terminal
*term
, int x
, int y
)
1882 /* Validate input coordinates, just in case. */
1883 if (x
== 0 || x
> term
->cols
)
1886 ldata
= scrlineptr(y
);
1887 if (x
== term
->cols
) {
1888 ldata
->lattr
&= ~LATTR_WRAPPED2
;
1890 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
1891 clear_cc(ldata
, x
-1);
1893 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
1894 ldata
->chars
[x
] = ldata
->chars
[x
-1];
1900 * Erase a large portion of the screen: the whole screen, or the
1901 * whole line, or parts thereof.
1903 static void erase_lots(Terminal
*term
,
1904 int line_only
, int from_begin
, int to_end
)
1908 int erasing_lines_from_top
= 0;
1911 start
.y
= term
->curs
.y
;
1913 end
.y
= term
->curs
.y
+ 1;
1915 erase_lattr
= FALSE
;
1930 if (!from_begin
|| !to_end
)
1931 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1932 check_selection(term
, start
, end
);
1934 /* Clear screen also forces a full window redraw, just in case. */
1935 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
1936 term_invalidate(term
);
1938 /* Lines scrolled away shouldn't be brought back on if the terminal
1940 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
1941 erasing_lines_from_top
= 1;
1943 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
1944 /* If it's a whole number of lines, starting at the top, and
1945 * we're fully erasing them, erase by scrolling and keep the
1946 * lines in the scrollback. */
1947 int scrolllines
= end
.y
;
1948 if (end
.y
== term
->rows
) {
1949 /* Shrink until we find a non-empty row.*/
1950 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
1952 if (scrolllines
> 0)
1953 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
1955 termline
*ldata
= scrlineptr(start
.y
);
1956 while (poslt(start
, end
)) {
1957 if (start
.x
== term
->cols
) {
1959 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
1961 ldata
->lattr
= LATTR_NORM
;
1963 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
1965 if (incpos(start
) && start
.y
< term
->rows
) {
1966 ldata
= scrlineptr(start
.y
);
1971 /* After an erase of lines from the top of the screen, we shouldn't
1972 * bring the lines back again if the terminal enlarges (since the user or
1973 * application has explictly thrown them away). */
1974 if (erasing_lines_from_top
&& !(term
->alt_which
))
1975 term
->tempsblines
= 0;
1979 * Insert or delete characters within the current line. n is +ve if
1980 * insertion is desired, and -ve for deletion.
1982 static void insch(Terminal
*term
, int n
)
1984 int dir
= (n
< 0 ?
-1 : +1);
1989 n
= (n
< 0 ?
-n
: n
);
1990 if (n
> term
->cols
- term
->curs
.x
)
1991 n
= term
->cols
- term
->curs
.x
;
1992 m
= term
->cols
- term
->curs
.x
- n
;
1993 cursplus
.y
= term
->curs
.y
;
1994 cursplus
.x
= term
->curs
.x
+ n
;
1995 check_selection(term
, term
->curs
, cursplus
);
1996 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1998 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
1999 ldata
= scrlineptr(term
->curs
.y
);
2001 for (j
= 0; j
< m
; j
++)
2002 move_termchar(ldata
,
2003 ldata
->chars
+ term
->curs
.x
+ j
,
2004 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
2006 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
2009 move_termchar(ldata
,
2010 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
2011 ldata
->chars
+ term
->curs
.x
+ j
);
2013 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
2018 * Toggle terminal mode `mode' to state `state'. (`query' indicates
2019 * whether the mode is a DEC private one or a normal one.)
2021 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
2023 unsigned long ticks
;
2027 case 1: /* DECCKM: application cursor keys */
2028 term
->app_cursor_keys
= state
;
2030 case 2: /* DECANM: VT52 mode */
2031 term
->vt52_mode
= !state
;
2032 if (term
->vt52_mode
) {
2033 term
->blink_is_real
= FALSE
;
2034 term
->vt52_bold
= FALSE
;
2036 term
->blink_is_real
= term
->cfg
.blinktext
;
2039 case 3: /* DECCOLM: 80/132 columns */
2041 if (!term
->cfg
.no_remote_resize
)
2042 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2043 term
->reset_132
= state
;
2044 term
->alt_t
= term
->marg_t
= 0;
2045 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2046 move(term
, 0, 0, 0);
2047 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2049 case 5: /* DECSCNM: reverse video */
2051 * Toggle reverse video. If we receive an OFF within the
2052 * visual bell timeout period after an ON, we trigger an
2053 * effective visual bell, so that ESC[?5hESC[?5l will
2054 * always be an actually _visible_ visual bell.
2056 ticks
= GETTICKCOUNT();
2057 /* turn off a previous vbell to avoid inconsistencies */
2058 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2059 term
->in_vbell
= FALSE
;
2060 if (term
->rvideo
&& !state
&& /* we're turning it off... */
2061 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
2062 /* If there's no vbell timeout already, or this one lasts
2063 * longer, replace vbell_timeout with ours. */
2064 if (!term
->in_vbell
||
2065 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
2067 term
->vbell_startpoint
= term
->rvbell_startpoint
;
2068 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
2069 } else if (!term
->rvideo
&& state
) {
2070 /* This is an ON, so we notice the time and save it. */
2071 term
->rvbell_startpoint
= ticks
;
2073 term
->rvideo
= state
;
2074 term
->seen_disp_event
= TRUE
;
2078 case 6: /* DECOM: DEC origin mode */
2079 term
->dec_om
= state
;
2081 case 7: /* DECAWM: auto wrap */
2084 case 8: /* DECARM: auto key repeat */
2085 term
->repeat_off
= !state
;
2087 case 10: /* DECEDM: set local edit mode */
2088 term
->term_editing
= state
;
2089 if (term
->ldisc
) /* cause ldisc to notice changes */
2090 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2092 case 25: /* DECTCEM: enable/disable cursor */
2093 compatibility2(OTHER
, VT220
);
2094 term
->cursor_on
= state
;
2095 term
->seen_disp_event
= TRUE
;
2097 case 47: /* alternate screen */
2098 compatibility(OTHER
);
2100 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2103 case 1000: /* xterm mouse 1 */
2104 term
->xterm_mouse
= state ?
1 : 0;
2105 set_raw_mouse_mode(term
->frontend
, state
);
2107 case 1002: /* xterm mouse 2 */
2108 term
->xterm_mouse
= state ?
2 : 0;
2109 set_raw_mouse_mode(term
->frontend
, state
);
2111 case 1047: /* alternate screen */
2112 compatibility(OTHER
);
2114 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2117 case 1048: /* save/restore cursor */
2118 if (!term
->cfg
.no_alt_screen
)
2119 save_cursor(term
, state
);
2120 if (!state
) term
->seen_disp_event
= TRUE
;
2122 case 1049: /* cursor & alternate screen */
2123 if (state
&& !term
->cfg
.no_alt_screen
)
2124 save_cursor(term
, state
);
2125 if (!state
) term
->seen_disp_event
= TRUE
;
2126 compatibility(OTHER
);
2128 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2129 if (!state
&& !term
->cfg
.no_alt_screen
)
2130 save_cursor(term
, state
);
2135 case 4: /* IRM: set insert mode */
2136 compatibility(VT102
);
2137 term
->insert
= state
;
2139 case 12: /* SRM: set echo mode */
2140 term
->term_echoing
= !state
;
2141 if (term
->ldisc
) /* cause ldisc to notice changes */
2142 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2144 case 20: /* LNM: Return sends ... */
2145 term
->cr_lf_return
= state
;
2147 case 34: /* WYULCURM: Make cursor BIG */
2148 compatibility2(OTHER
, VT220
);
2149 term
->big_cursor
= !state
;
2154 * Process an OSC sequence: set window title or icon name.
2156 static void do_osc(Terminal
*term
)
2159 while (term
->osc_strlen
--)
2160 term
->wordness
[(unsigned char)
2161 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2163 term
->osc_string
[term
->osc_strlen
] = '\0';
2164 switch (term
->esc_args
[0]) {
2167 if (!term
->cfg
.no_remote_wintitle
)
2168 set_icon(term
->frontend
, term
->osc_string
);
2169 if (term
->esc_args
[0] == 1)
2171 /* fall through: parameter 0 means set both */
2174 if (!term
->cfg
.no_remote_wintitle
)
2175 set_title(term
->frontend
, term
->osc_string
);
2182 * ANSI printing routines.
2184 static void term_print_setup(Terminal
*term
)
2186 bufchain_clear(&term
->printer_buf
);
2187 term
->print_job
= printer_start_job(term
->cfg
.printer
);
2189 static void term_print_flush(Terminal
*term
)
2194 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2195 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2198 printer_job_data(term
->print_job
, data
, len
);
2199 bufchain_consume(&term
->printer_buf
, len
);
2202 static void term_print_finish(Terminal
*term
)
2208 if (!term
->printing
&& !term
->only_printing
)
2209 return; /* we need do nothing */
2211 term_print_flush(term
);
2212 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2213 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2215 if (c
== '\033' || c
== '\233') {
2216 bufchain_consume(&term
->printer_buf
, size
);
2219 printer_job_data(term
->print_job
, &c
, 1);
2220 bufchain_consume(&term
->printer_buf
, 1);
2223 printer_finish_job(term
->print_job
);
2224 term
->print_job
= NULL
;
2225 term
->printing
= term
->only_printing
= FALSE
;
2229 * Remove everything currently in `inbuf' and stick it up on the
2230 * in-memory display. There's a big state machine in here to
2231 * process escape sequences...
2233 void term_out(Terminal
*term
)
2237 unsigned char localbuf
[256], *chars
;
2242 chars
= NULL
; /* placate compiler warnings */
2243 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
2247 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2248 if (nchars
> sizeof(localbuf
))
2249 nchars
= sizeof(localbuf
);
2250 memcpy(localbuf
, ret
, nchars
);
2251 bufchain_consume(&term
->inbuf
, nchars
);
2253 assert(chars
!= NULL
);
2259 * Optionally log the session traffic to a file. Useful for
2260 * debugging and possibly also useful for actual logging.
2262 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
2263 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2269 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2270 * be able to display 8-bit characters, but I'll let that go 'cause
2275 * If we're printing, add the character to the printer
2278 if (term
->printing
) {
2279 bufchain_add(&term
->printer_buf
, &c
, 1);
2282 * If we're in print-only mode, we use a much simpler
2283 * state machine designed only to recognise the ESC[4i
2284 * termination sequence.
2286 if (term
->only_printing
) {
2288 term
->print_state
= 1;
2289 else if (c
== (unsigned char)'\233')
2290 term
->print_state
= 2;
2291 else if (c
== '[' && term
->print_state
== 1)
2292 term
->print_state
= 2;
2293 else if (c
== '4' && term
->print_state
== 2)
2294 term
->print_state
= 3;
2295 else if (c
== 'i' && term
->print_state
== 3)
2296 term
->print_state
= 4;
2298 term
->print_state
= 0;
2299 if (term
->print_state
== 4) {
2300 term_print_finish(term
);
2306 /* First see about all those translations. */
2307 if (term
->termstate
== TOPLEVEL
) {
2309 switch (term
->utf_state
) {
2312 /* UTF-8 must be stateless so we ignore iso2022. */
2313 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2314 c
= term
->ucsdata
->unitab_ctrl
[c
];
2315 else c
= ((unsigned char)c
) | CSET_ASCII
;
2317 } else if ((c
& 0xe0) == 0xc0) {
2318 term
->utf_size
= term
->utf_state
= 1;
2319 term
->utf_char
= (c
& 0x1f);
2320 } else if ((c
& 0xf0) == 0xe0) {
2321 term
->utf_size
= term
->utf_state
= 2;
2322 term
->utf_char
= (c
& 0x0f);
2323 } else if ((c
& 0xf8) == 0xf0) {
2324 term
->utf_size
= term
->utf_state
= 3;
2325 term
->utf_char
= (c
& 0x07);
2326 } else if ((c
& 0xfc) == 0xf8) {
2327 term
->utf_size
= term
->utf_state
= 4;
2328 term
->utf_char
= (c
& 0x03);
2329 } else if ((c
& 0xfe) == 0xfc) {
2330 term
->utf_size
= term
->utf_state
= 5;
2331 term
->utf_char
= (c
& 0x01);
2342 if ((c
& 0xC0) != 0x80) {
2345 term
->utf_state
= 0;
2348 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2349 if (--term
->utf_state
)
2354 /* Is somebody trying to be evil! */
2356 (c
< 0x800 && term
->utf_size
>= 2) ||
2357 (c
< 0x10000 && term
->utf_size
>= 3) ||
2358 (c
< 0x200000 && term
->utf_size
>= 4) ||
2359 (c
< 0x4000000 && term
->utf_size
>= 5))
2362 /* Unicode line separator and paragraph separator are CR-LF */
2363 if (c
== 0x2028 || c
== 0x2029)
2366 /* High controls are probably a Baaad idea too. */
2370 /* The UTF-16 surrogates are not nice either. */
2371 /* The standard give the option of decoding these:
2372 * I don't want to! */
2373 if (c
>= 0xD800 && c
< 0xE000)
2376 /* ISO 10646 characters now limited to UTF-16 range. */
2380 /* This is currently a TagPhobic application.. */
2381 if (c
>= 0xE0000 && c
<= 0xE007F)
2384 /* U+FEFF is best seen as a null. */
2387 /* But U+FFFE is an error. */
2388 if (c
== 0xFFFE || c
== 0xFFFF)
2393 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2394 else if(term
->sco_acs
&&
2395 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2397 if (term
->sco_acs
== 2) c
|= 0x80;
2400 switch (term
->cset_attr
[term
->cset
]) {
2402 * Linedraw characters are different from 'ESC ( B'
2403 * only for a small range. For ones outside that
2404 * range, make sure we use the same font as well as
2405 * the same encoding.
2408 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2409 c
= term
->ucsdata
->unitab_ctrl
[c
];
2411 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2415 /* If UK-ASCII, make the '#' a LineDraw Pound */
2417 c
= '}' | CSET_LINEDRW
;
2420 /*FALLTHROUGH*/ case CSET_ASCII
:
2421 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2422 c
= term
->ucsdata
->unitab_ctrl
[c
];
2424 c
= ((unsigned char) c
) | CSET_ASCII
;
2427 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2433 /* How about C1 controls ? */
2434 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2435 !term
->vt52_mode
&& has_compat(VT220
)) {
2436 term
->termstate
= SEEN_ESC
;
2437 term
->esc_query
= FALSE
;
2438 c
= '@' + (c
& 0x1F);
2441 /* Or the GL control. */
2442 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2443 if (term
->curs
.x
&& !term
->wrapnext
)
2445 term
->wrapnext
= FALSE
;
2446 /* destructive backspace might be disabled */
2447 if (!term
->cfg
.no_dbackspace
) {
2448 copy_termchar(scrlineptr(term
->curs
.y
),
2449 term
->curs
.x
, &term
->erase_char
);
2452 /* Or normal C0 controls. */
2453 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2455 case '\005': /* ENQ: terminal type query */
2456 /* Strictly speaking this is VT100 but a VT100 defaults to
2457 * no response. Other terminals respond at their option.
2459 * Don't put a CR in the default string as this tends to
2460 * upset some weird software.
2462 * An xterm returns "xterm" (5 characters)
2464 compatibility(ANSIMIN
);
2466 char abuf
[256], *s
, *d
;
2468 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
; s
++) {
2470 if (*s
>= 'a' && *s
<= 'z')
2471 *d
++ = (*s
- ('a' - 1));
2472 else if ((*s
>= '@' && *s
<= '_') ||
2473 *s
== '?' || (*s
& 0x80))
2478 } else if (*s
== '^') {
2483 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2487 case '\007': /* BEL: Bell */
2489 struct beeptime
*newbeep
;
2490 unsigned long ticks
;
2492 ticks
= GETTICKCOUNT();
2494 if (!term
->beep_overloaded
) {
2495 newbeep
= snew(struct beeptime
);
2496 newbeep
->ticks
= ticks
;
2497 newbeep
->next
= NULL
;
2498 if (!term
->beephead
)
2499 term
->beephead
= newbeep
;
2501 term
->beeptail
->next
= newbeep
;
2502 term
->beeptail
= newbeep
;
2507 * Throw out any beeps that happened more than
2510 while (term
->beephead
&&
2511 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
2512 struct beeptime
*tmp
= term
->beephead
;
2513 term
->beephead
= tmp
->next
;
2515 if (!term
->beephead
)
2516 term
->beeptail
= NULL
;
2520 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
2521 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
2523 * If we're currently overloaded and the
2524 * last beep was more than s seconds ago,
2525 * leave overload mode.
2527 term
->beep_overloaded
= FALSE
;
2528 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
2529 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
2531 * Now, if we have n or more beeps
2532 * remaining in the queue, go into overload
2535 term
->beep_overloaded
= TRUE
;
2537 term
->lastbeep
= ticks
;
2540 * Perform an actual beep if we're not overloaded.
2542 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
2543 beep(term
->frontend
, term
->cfg
.beep
);
2544 if (term
->cfg
.beep
== BELL_VISUAL
) {
2545 term
->in_vbell
= TRUE
;
2546 term
->vbell_startpoint
= ticks
;
2550 term
->seen_disp_event
= TRUE
;
2553 case '\b': /* BS: Back space */
2554 if (term
->curs
.x
== 0 &&
2555 (term
->curs
.y
== 0 || term
->wrap
== 0))
2557 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2558 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2559 else if (term
->wrapnext
)
2560 term
->wrapnext
= FALSE
;
2563 term
->seen_disp_event
= TRUE
;
2565 case '\016': /* LS1: Locking-shift one */
2566 compatibility(VT100
);
2569 case '\017': /* LS0: Locking-shift zero */
2570 compatibility(VT100
);
2573 case '\033': /* ESC: Escape */
2574 if (term
->vt52_mode
)
2575 term
->termstate
= VT52_ESC
;
2577 compatibility(ANSIMIN
);
2578 term
->termstate
= SEEN_ESC
;
2579 term
->esc_query
= FALSE
;
2582 case '\015': /* CR: Carriage return */
2584 term
->wrapnext
= FALSE
;
2585 term
->seen_disp_event
= TRUE
;
2586 term
->paste_hold
= 0;
2588 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2590 case '\014': /* FF: Form feed */
2591 if (has_compat(SCOANSI
)) {
2592 move(term
, 0, 0, 0);
2593 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2595 term
->wrapnext
= FALSE
;
2596 term
->seen_disp_event
= 1;
2599 case '\013': /* VT: Line tabulation */
2600 compatibility(VT100
);
2601 case '\012': /* LF: Line feed */
2602 if (term
->curs
.y
== term
->marg_b
)
2603 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2604 else if (term
->curs
.y
< term
->rows
- 1)
2606 if (term
->cfg
.lfhascr
)
2608 term
->wrapnext
= FALSE
;
2609 term
->seen_disp_event
= 1;
2610 term
->paste_hold
= 0;
2612 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2614 case '\t': /* HT: Character tabulation */
2616 pos old_curs
= term
->curs
;
2617 termline
*ldata
= scrlineptr(term
->curs
.y
);
2621 } while (term
->curs
.x
< term
->cols
- 1 &&
2622 !term
->tabs
[term
->curs
.x
]);
2624 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
2625 if (term
->curs
.x
>= term
->cols
/ 2)
2626 term
->curs
.x
= term
->cols
/ 2 - 1;
2628 if (term
->curs
.x
>= term
->cols
)
2629 term
->curs
.x
= term
->cols
- 1;
2632 check_selection(term
, old_curs
, term
->curs
);
2634 term
->seen_disp_event
= TRUE
;
2638 switch (term
->termstate
) {
2640 /* Only graphic characters get this far;
2641 * ctrls are stripped above */
2643 termline
*cline
= scrlineptr(term
->curs
.y
);
2648 width
= wcwidth((wchar_t) c
);
2650 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
2651 cline
->lattr
|= LATTR_WRAPPED
;
2652 if (term
->curs
.y
== term
->marg_b
)
2653 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2654 else if (term
->curs
.y
< term
->rows
- 1)
2657 term
->wrapnext
= FALSE
;
2658 cline
= scrlineptr(term
->curs
.y
);
2660 if (term
->insert
&& width
> 0)
2662 if (term
->selstate
!= NO_SELECTION
) {
2663 pos cursplus
= term
->curs
;
2665 check_selection(term
, term
->curs
, cursplus
);
2667 if (((c
& CSET_MASK
) == CSET_ASCII
||
2668 (c
& CSET_MASK
) == 0) &&
2670 logtraffic(term
->logctx
, (unsigned char) c
,
2676 * If we're about to display a double-width
2677 * character starting in the rightmost
2678 * column, then we do something special
2679 * instead. We must print a space in the
2680 * last column of the screen, then wrap;
2681 * and we also set LATTR_WRAPPED2 which
2682 * instructs subsequent cut-and-pasting not
2683 * only to splice this line to the one
2684 * after it, but to ignore the space in the
2685 * last character position as well.
2686 * (Because what was actually output to the
2687 * terminal was presumably just a sequence
2688 * of CJK characters, and we don't want a
2689 * space to be pasted in the middle of
2690 * those just because they had the
2691 * misfortune to start in the wrong parity
2692 * column. xterm concurs.)
2694 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2695 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2696 if (term
->curs
.x
== term
->cols
-1) {
2697 copy_termchar(cline
, term
->curs
.x
,
2699 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
2700 if (term
->curs
.y
== term
->marg_b
)
2701 scroll(term
, term
->marg_t
, term
->marg_b
,
2703 else if (term
->curs
.y
< term
->rows
- 1)
2706 /* Now we must check_boundary again, of course. */
2707 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2708 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2712 clear_cc(cline
, term
->curs
.x
);
2713 cline
->chars
[term
->curs
.x
].chr
= c
;
2714 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2719 clear_cc(cline
, term
->curs
.x
);
2720 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
2721 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2725 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2726 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2729 clear_cc(cline
, term
->curs
.x
);
2730 cline
->chars
[term
->curs
.x
].chr
= c
;
2731 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2735 if (term
->curs
.x
> 0) {
2736 int x
= term
->curs
.x
- 1;
2738 /* If we're in wrapnext state, the character
2739 * to combine with is _here_, not to our left. */
2744 * If the previous character is
2745 * UCSWIDE, back up another one.
2747 if (cline
->chars
[x
].chr
== UCSWIDE
) {
2752 add_cc(cline
, x
, c
);
2753 term
->seen_disp_event
= 1;
2760 if (term
->curs
.x
== term
->cols
) {
2762 term
->wrapnext
= TRUE
;
2763 if (term
->wrap
&& term
->vt52_mode
) {
2764 cline
->lattr
|= LATTR_WRAPPED
;
2765 if (term
->curs
.y
== term
->marg_b
)
2766 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2767 else if (term
->curs
.y
< term
->rows
- 1)
2770 term
->wrapnext
= FALSE
;
2773 term
->seen_disp_event
= 1;
2779 * This state is virtually identical to SEEN_ESC, with the
2780 * exception that we have an OSC sequence in the pipeline,
2781 * and _if_ we see a backslash, we process it.
2785 term
->termstate
= TOPLEVEL
;
2788 /* else fall through */
2790 if (c
>= ' ' && c
<= '/') {
2791 if (term
->esc_query
)
2792 term
->esc_query
= -1;
2794 term
->esc_query
= c
;
2797 term
->termstate
= TOPLEVEL
;
2798 switch (ANSI(c
, term
->esc_query
)) {
2799 case '[': /* enter CSI mode */
2800 term
->termstate
= SEEN_CSI
;
2801 term
->esc_nargs
= 1;
2802 term
->esc_args
[0] = ARG_DEFAULT
;
2803 term
->esc_query
= FALSE
;
2805 case ']': /* OSC: xterm escape sequences */
2806 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2807 compatibility(OTHER
);
2808 term
->termstate
= SEEN_OSC
;
2809 term
->esc_args
[0] = 0;
2811 case '7': /* DECSC: save cursor */
2812 compatibility(VT100
);
2813 save_cursor(term
, TRUE
);
2815 case '8': /* DECRC: restore cursor */
2816 compatibility(VT100
);
2817 save_cursor(term
, FALSE
);
2818 term
->seen_disp_event
= TRUE
;
2820 case '=': /* DECKPAM: Keypad application mode */
2821 compatibility(VT100
);
2822 term
->app_keypad_keys
= TRUE
;
2824 case '>': /* DECKPNM: Keypad numeric mode */
2825 compatibility(VT100
);
2826 term
->app_keypad_keys
= FALSE
;
2828 case 'D': /* IND: exactly equivalent to LF */
2829 compatibility(VT100
);
2830 if (term
->curs
.y
== term
->marg_b
)
2831 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2832 else if (term
->curs
.y
< term
->rows
- 1)
2834 term
->wrapnext
= FALSE
;
2835 term
->seen_disp_event
= TRUE
;
2837 case 'E': /* NEL: exactly equivalent to CR-LF */
2838 compatibility(VT100
);
2840 if (term
->curs
.y
== term
->marg_b
)
2841 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2842 else if (term
->curs
.y
< term
->rows
- 1)
2844 term
->wrapnext
= FALSE
;
2845 term
->seen_disp_event
= TRUE
;
2847 case 'M': /* RI: reverse index - backwards LF */
2848 compatibility(VT100
);
2849 if (term
->curs
.y
== term
->marg_t
)
2850 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
2851 else if (term
->curs
.y
> 0)
2853 term
->wrapnext
= FALSE
;
2854 term
->seen_disp_event
= TRUE
;
2856 case 'Z': /* DECID: terminal type query */
2857 compatibility(VT100
);
2859 ldisc_send(term
->ldisc
, term
->id_string
,
2860 strlen(term
->id_string
), 0);
2862 case 'c': /* RIS: restore power-on settings */
2863 compatibility(VT100
);
2865 if (term
->ldisc
) /* cause ldisc to notice changes */
2866 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2867 if (term
->reset_132
) {
2868 if (!term
->cfg
.no_remote_resize
)
2869 request_resize(term
->frontend
, 80, term
->rows
);
2870 term
->reset_132
= 0;
2873 term
->seen_disp_event
= TRUE
;
2875 case 'H': /* HTS: set a tab */
2876 compatibility(VT100
);
2877 term
->tabs
[term
->curs
.x
] = TRUE
;
2880 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2881 compatibility(VT100
);
2887 for (i
= 0; i
< term
->rows
; i
++) {
2888 ldata
= scrlineptr(i
);
2889 for (j
= 0; j
< term
->cols
; j
++) {
2890 copy_termchar(ldata
, j
,
2891 &term
->basic_erase_char
);
2892 ldata
->chars
[j
].chr
= 'E';
2894 ldata
->lattr
= LATTR_NORM
;
2897 term
->seen_disp_event
= TRUE
;
2898 scrtop
.x
= scrtop
.y
= 0;
2900 scrbot
.y
= term
->rows
;
2901 check_selection(term
, scrtop
, scrbot
);
2905 case ANSI('3', '#'):
2906 case ANSI('4', '#'):
2907 case ANSI('5', '#'):
2908 case ANSI('6', '#'):
2909 compatibility(VT100
);
2913 switch (ANSI(c
, term
->esc_query
)) {
2914 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2917 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2920 case ANSI('5', '#'): /* DECSWL: normal */
2921 nlattr
= LATTR_NORM
;
2923 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2924 nlattr
= LATTR_WIDE
;
2927 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
2930 /* GZD4: G0 designate 94-set */
2931 case ANSI('A', '('):
2932 compatibility(VT100
);
2933 if (!term
->cfg
.no_remote_charset
)
2934 term
->cset_attr
[0] = CSET_GBCHR
;
2936 case ANSI('B', '('):
2937 compatibility(VT100
);
2938 if (!term
->cfg
.no_remote_charset
)
2939 term
->cset_attr
[0] = CSET_ASCII
;
2941 case ANSI('0', '('):
2942 compatibility(VT100
);
2943 if (!term
->cfg
.no_remote_charset
)
2944 term
->cset_attr
[0] = CSET_LINEDRW
;
2946 case ANSI('U', '('):
2947 compatibility(OTHER
);
2948 if (!term
->cfg
.no_remote_charset
)
2949 term
->cset_attr
[0] = CSET_SCOACS
;
2951 /* G1D4: G1-designate 94-set */
2952 case ANSI('A', ')'):
2953 compatibility(VT100
);
2954 if (!term
->cfg
.no_remote_charset
)
2955 term
->cset_attr
[1] = CSET_GBCHR
;
2957 case ANSI('B', ')'):
2958 compatibility(VT100
);
2959 if (!term
->cfg
.no_remote_charset
)
2960 term
->cset_attr
[1] = CSET_ASCII
;
2962 case ANSI('0', ')'):
2963 compatibility(VT100
);
2964 if (!term
->cfg
.no_remote_charset
)
2965 term
->cset_attr
[1] = CSET_LINEDRW
;
2967 case ANSI('U', ')'):
2968 compatibility(OTHER
);
2969 if (!term
->cfg
.no_remote_charset
)
2970 term
->cset_attr
[1] = CSET_SCOACS
;
2972 /* DOCS: Designate other coding system */
2973 case ANSI('8', '%'): /* Old Linux code */
2974 case ANSI('G', '%'):
2975 compatibility(OTHER
);
2976 if (!term
->cfg
.no_remote_charset
)
2979 case ANSI('@', '%'):
2980 compatibility(OTHER
);
2981 if (!term
->cfg
.no_remote_charset
)
2987 term
->termstate
= TOPLEVEL
; /* default */
2989 if (term
->esc_nargs
<= ARGS_MAX
) {
2990 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
2991 term
->esc_args
[term
->esc_nargs
- 1] = 0;
2992 term
->esc_args
[term
->esc_nargs
- 1] =
2993 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
2995 term
->termstate
= SEEN_CSI
;
2996 } else if (c
== ';') {
2997 if (++term
->esc_nargs
<= ARGS_MAX
)
2998 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
2999 term
->termstate
= SEEN_CSI
;
3000 } else if (c
< '@') {
3001 if (term
->esc_query
)
3002 term
->esc_query
= -1;
3004 term
->esc_query
= TRUE
;
3006 term
->esc_query
= c
;
3007 term
->termstate
= SEEN_CSI
;
3009 switch (ANSI(c
, term
->esc_query
)) {
3010 case 'A': /* CUU: move up N lines */
3011 move(term
, term
->curs
.x
,
3012 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3013 term
->seen_disp_event
= TRUE
;
3015 case 'e': /* VPR: move down N lines */
3016 compatibility(ANSI
);
3018 case 'B': /* CUD: Cursor down */
3019 move(term
, term
->curs
.x
,
3020 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3021 term
->seen_disp_event
= TRUE
;
3023 case ANSI('c', '>'): /* DA: report xterm version */
3024 compatibility(OTHER
);
3025 /* this reports xterm version 136 so that VIM can
3026 use the drag messages from the mouse reporting */
3028 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
3030 case 'a': /* HPR: move right N cols */
3031 compatibility(ANSI
);
3033 case 'C': /* CUF: Cursor right */
3034 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
3036 term
->seen_disp_event
= TRUE
;
3038 case 'D': /* CUB: move left N cols */
3039 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
3041 term
->seen_disp_event
= TRUE
;
3043 case 'E': /* CNL: move down N lines and CR */
3044 compatibility(ANSI
);
3046 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3047 term
->seen_disp_event
= TRUE
;
3049 case 'F': /* CPL: move up N lines and CR */
3050 compatibility(ANSI
);
3052 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3053 term
->seen_disp_event
= TRUE
;
3056 case '`': /* HPA: set horizontal posn */
3057 compatibility(ANSI
);
3058 move(term
, def(term
->esc_args
[0], 1) - 1,
3060 term
->seen_disp_event
= TRUE
;
3062 case 'd': /* VPA: set vertical posn */
3063 compatibility(ANSI
);
3064 move(term
, term
->curs
.x
,
3065 ((term
->dec_om ? term
->marg_t
: 0) +
3066 def(term
->esc_args
[0], 1) - 1),
3067 (term
->dec_om ?
2 : 0));
3068 term
->seen_disp_event
= TRUE
;
3071 case 'f': /* HVP: set horz and vert posns at once */
3072 if (term
->esc_nargs
< 2)
3073 term
->esc_args
[1] = ARG_DEFAULT
;
3074 move(term
, def(term
->esc_args
[1], 1) - 1,
3075 ((term
->dec_om ? term
->marg_t
: 0) +
3076 def(term
->esc_args
[0], 1) - 1),
3077 (term
->dec_om ?
2 : 0));
3078 term
->seen_disp_event
= TRUE
;
3080 case 'J': /* ED: erase screen or parts of it */
3082 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3085 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3088 term
->seen_disp_event
= TRUE
;
3090 case 'K': /* EL: erase line or parts of it */
3092 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3095 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3097 term
->seen_disp_event
= TRUE
;
3099 case 'L': /* IL: insert lines */
3100 compatibility(VT102
);
3101 if (term
->curs
.y
<= term
->marg_b
)
3102 scroll(term
, term
->curs
.y
, term
->marg_b
,
3103 -def(term
->esc_args
[0], 1), FALSE
);
3104 term
->seen_disp_event
= TRUE
;
3106 case 'M': /* DL: delete lines */
3107 compatibility(VT102
);
3108 if (term
->curs
.y
<= term
->marg_b
)
3109 scroll(term
, term
->curs
.y
, term
->marg_b
,
3110 def(term
->esc_args
[0], 1),
3112 term
->seen_disp_event
= TRUE
;
3114 case '@': /* ICH: insert chars */
3115 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3116 compatibility(VT102
);
3117 insch(term
, def(term
->esc_args
[0], 1));
3118 term
->seen_disp_event
= TRUE
;
3120 case 'P': /* DCH: delete chars */
3121 compatibility(VT102
);
3122 insch(term
, -def(term
->esc_args
[0], 1));
3123 term
->seen_disp_event
= TRUE
;
3125 case 'c': /* DA: terminal type query */
3126 compatibility(VT100
);
3127 /* This is the response for a VT102 */
3129 ldisc_send(term
->ldisc
, term
->id_string
,
3130 strlen(term
->id_string
), 0);
3132 case 'n': /* DSR: cursor position query */
3134 if (term
->esc_args
[0] == 6) {
3136 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3138 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3139 } else if (term
->esc_args
[0] == 5) {
3140 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3144 case 'h': /* SM: toggle modes to high */
3146 compatibility(VT100
);
3149 for (i
= 0; i
< term
->esc_nargs
; i
++)
3150 toggle_mode(term
, term
->esc_args
[i
],
3151 term
->esc_query
, TRUE
);
3154 case 'i': /* MC: Media copy */
3156 compatibility(VT100
);
3158 if (term
->esc_nargs
!= 1) break;
3159 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
3160 term
->printing
= TRUE
;
3161 term
->only_printing
= !term
->esc_query
;
3162 term
->print_state
= 0;
3163 term_print_setup(term
);
3164 } else if (term
->esc_args
[0] == 4 &&
3166 term_print_finish(term
);
3170 case 'l': /* RM: toggle modes to low */
3172 compatibility(VT100
);
3175 for (i
= 0; i
< term
->esc_nargs
; i
++)
3176 toggle_mode(term
, term
->esc_args
[i
],
3177 term
->esc_query
, FALSE
);
3180 case 'g': /* TBC: clear tabs */
3181 compatibility(VT100
);
3182 if (term
->esc_nargs
== 1) {
3183 if (term
->esc_args
[0] == 0) {
3184 term
->tabs
[term
->curs
.x
] = FALSE
;
3185 } else if (term
->esc_args
[0] == 3) {
3187 for (i
= 0; i
< term
->cols
; i
++)
3188 term
->tabs
[i
] = FALSE
;
3192 case 'r': /* DECSTBM: set scroll margins */
3193 compatibility(VT100
);
3194 if (term
->esc_nargs
<= 2) {
3196 top
= def(term
->esc_args
[0], 1) - 1;
3197 bot
= (term
->esc_nargs
<= 1
3198 || term
->esc_args
[1] == 0 ?
3200 def(term
->esc_args
[1], term
->rows
)) - 1;
3201 if (bot
>= term
->rows
)
3202 bot
= term
->rows
- 1;
3203 /* VTTEST Bug 9 - if region is less than 2 lines
3204 * don't change region.
3206 if (bot
- top
> 0) {
3211 * I used to think the cursor should be
3212 * placed at the top of the newly marginned
3213 * area. Apparently not: VMS TPU falls over
3216 * Well actually it should for
3219 term
->curs
.y
= (term
->dec_om ?
3221 term
->seen_disp_event
= TRUE
;
3225 case 'm': /* SGR: set graphics rendition */
3228 * A VT100 without the AVO only had one
3229 * attribute, either underline or
3230 * reverse video depending on the
3231 * cursor type, this was selected by
3235 * This is sometimes DIM, eg on the
3238 * This is sometimes INVIS various ANSI.
3240 * This like 22 disables BOLD, DIM and INVIS
3242 * The ANSI colours appear on any
3243 * terminal that has colour (obviously)
3244 * but the interaction between sgr0 and
3245 * the colours varies but is usually
3246 * related to the background colour
3247 * erase item. The interaction between
3248 * colour attributes and the mono ones
3249 * is also very implementation
3252 * The 39 and 49 attributes are likely
3253 * to be unimplemented.
3256 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3257 switch (def(term
->esc_args
[i
], 0)) {
3258 case 0: /* restore defaults */
3259 term
->curr_attr
= term
->default_attr
;
3261 case 1: /* enable bold */
3262 compatibility(VT100AVO
);
3263 term
->curr_attr
|= ATTR_BOLD
;
3265 case 21: /* (enable double underline) */
3266 compatibility(OTHER
);
3267 case 4: /* enable underline */
3268 compatibility(VT100AVO
);
3269 term
->curr_attr
|= ATTR_UNDER
;
3271 case 5: /* enable blink */
3272 compatibility(VT100AVO
);
3273 term
->curr_attr
|= ATTR_BLINK
;
3275 case 6: /* SCO light bkgrd */
3276 compatibility(SCOANSI
);
3277 term
->blink_is_real
= FALSE
;
3278 term
->curr_attr
|= ATTR_BLINK
;
3280 case 7: /* enable reverse video */
3281 term
->curr_attr
|= ATTR_REVERSE
;
3283 case 10: /* SCO acs off */
3284 compatibility(SCOANSI
);
3285 if (term
->cfg
.no_remote_charset
) break;
3286 term
->sco_acs
= 0; break;
3287 case 11: /* SCO acs on */
3288 compatibility(SCOANSI
);
3289 if (term
->cfg
.no_remote_charset
) break;
3290 term
->sco_acs
= 1; break;
3291 case 12: /* SCO acs on, |0x80 */
3292 compatibility(SCOANSI
);
3293 if (term
->cfg
.no_remote_charset
) break;
3294 term
->sco_acs
= 2; break;
3295 case 22: /* disable bold */
3296 compatibility2(OTHER
, VT220
);
3297 term
->curr_attr
&= ~ATTR_BOLD
;
3299 case 24: /* disable underline */
3300 compatibility2(OTHER
, VT220
);
3301 term
->curr_attr
&= ~ATTR_UNDER
;
3303 case 25: /* disable blink */
3304 compatibility2(OTHER
, VT220
);
3305 term
->curr_attr
&= ~ATTR_BLINK
;
3307 case 27: /* disable reverse video */
3308 compatibility2(OTHER
, VT220
);
3309 term
->curr_attr
&= ~ATTR_REVERSE
;
3320 term
->curr_attr
&= ~ATTR_FGMASK
;
3322 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3332 /* xterm-style bright foreground */
3333 term
->curr_attr
&= ~ATTR_FGMASK
;
3335 ((term
->esc_args
[i
] - 90 + 16)
3338 case 39: /* default-foreground */
3339 term
->curr_attr
&= ~ATTR_FGMASK
;
3340 term
->curr_attr
|= ATTR_DEFFG
;
3351 term
->curr_attr
&= ~ATTR_BGMASK
;
3353 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3363 /* xterm-style bright background */
3364 term
->curr_attr
&= ~ATTR_BGMASK
;
3366 ((term
->esc_args
[i
] - 100 + 16)
3369 case 49: /* default-background */
3370 term
->curr_attr
&= ~ATTR_BGMASK
;
3371 term
->curr_attr
|= ATTR_DEFBG
;
3375 set_erase_char(term
);
3378 case 's': /* save cursor */
3379 save_cursor(term
, TRUE
);
3381 case 'u': /* restore cursor */
3382 save_cursor(term
, FALSE
);
3383 term
->seen_disp_event
= TRUE
;
3385 case 't': /* DECSLPP: set page size - ie window height */
3387 * VT340/VT420 sequence DECSLPP, DEC only allows values
3388 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3389 * illegal values (eg first arg 1..9) for window changing
3392 if (term
->esc_nargs
<= 1
3393 && (term
->esc_args
[0] < 1 ||
3394 term
->esc_args
[0] >= 24)) {
3395 compatibility(VT340TEXT
);
3396 if (!term
->cfg
.no_remote_resize
)
3397 request_resize(term
->frontend
, term
->cols
,
3398 def(term
->esc_args
[0], 24));
3400 } else if (term
->esc_nargs
>= 1 &&
3401 term
->esc_args
[0] >= 1 &&
3402 term
->esc_args
[0] < 24) {
3403 compatibility(OTHER
);
3405 switch (term
->esc_args
[0]) {
3409 set_iconic(term
->frontend
, FALSE
);
3412 set_iconic(term
->frontend
, TRUE
);
3415 if (term
->esc_nargs
>= 3) {
3416 if (!term
->cfg
.no_remote_resize
)
3417 move_window(term
->frontend
,
3418 def(term
->esc_args
[1], 0),
3419 def(term
->esc_args
[2], 0));
3423 /* We should resize the window to a given
3424 * size in pixels here, but currently our
3425 * resizing code isn't healthy enough to
3430 set_zorder(term
->frontend
, TRUE
);
3433 /* move to bottom */
3434 set_zorder(term
->frontend
, FALSE
);
3437 refresh_window(term
->frontend
);
3440 if (term
->esc_nargs
>= 3) {
3441 if (!term
->cfg
.no_remote_resize
)
3442 request_resize(term
->frontend
,
3443 def(term
->esc_args
[2], term
->cfg
.width
),
3444 def(term
->esc_args
[1], term
->cfg
.height
));
3448 if (term
->esc_nargs
>= 2)
3449 set_zoomed(term
->frontend
,
3455 ldisc_send(term
->ldisc
,
3456 is_iconic(term
->frontend
) ?
3457 "\033[1t" : "\033[2t", 4, 0);
3461 get_window_pos(term
->frontend
, &x
, &y
);
3462 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3463 ldisc_send(term
->ldisc
, buf
, len
, 0);
3468 get_window_pixels(term
->frontend
, &x
, &y
);
3469 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
3470 ldisc_send(term
->ldisc
, buf
, len
, 0);
3475 len
= sprintf(buf
, "\033[8;%d;%dt",
3476 term
->rows
, term
->cols
);
3477 ldisc_send(term
->ldisc
, buf
, len
, 0);
3482 * Hmmm. Strictly speaking we
3483 * should return `the size of the
3484 * screen in characters', but
3485 * that's not easy: (a) window
3486 * furniture being what it is it's
3487 * hard to compute, and (b) in
3488 * resize-font mode maximising the
3489 * window wouldn't change the
3490 * number of characters. *shrug*. I
3491 * think we'll ignore it for the
3492 * moment and see if anyone
3493 * complains, and then ask them
3494 * what they would like it to do.
3499 !term
->cfg
.no_remote_qtitle
) {
3500 p
= get_window_title(term
->frontend
, TRUE
);
3502 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3503 ldisc_send(term
->ldisc
, p
, len
, 0);
3504 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3509 !term
->cfg
.no_remote_qtitle
) {
3510 p
= get_window_title(term
->frontend
,FALSE
);
3512 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3513 ldisc_send(term
->ldisc
, p
, len
, 0);
3514 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3520 case 'S': /* SU: Scroll up */
3521 compatibility(SCOANSI
);
3522 scroll(term
, term
->marg_t
, term
->marg_b
,
3523 def(term
->esc_args
[0], 1), TRUE
);
3524 term
->wrapnext
= FALSE
;
3525 term
->seen_disp_event
= TRUE
;
3527 case 'T': /* SD: Scroll down */
3528 compatibility(SCOANSI
);
3529 scroll(term
, term
->marg_t
, term
->marg_b
,
3530 -def(term
->esc_args
[0], 1), TRUE
);
3531 term
->wrapnext
= FALSE
;
3532 term
->seen_disp_event
= TRUE
;
3534 case ANSI('|', '*'): /* DECSNLS */
3536 * Set number of lines on screen
3537 * VT420 uses VGA like hardware and can
3538 * support any size in reasonable range
3539 * (24..49 AIUI) with no default specified.
3541 compatibility(VT420
);
3542 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3543 if (!term
->cfg
.no_remote_resize
)
3544 request_resize(term
->frontend
, term
->cols
,
3545 def(term
->esc_args
[0],
3550 case ANSI('|', '$'): /* DECSCPP */
3552 * Set number of columns per page
3553 * Docs imply range is only 80 or 132, but
3556 compatibility(VT340TEXT
);
3557 if (term
->esc_nargs
<= 1) {
3558 if (!term
->cfg
.no_remote_resize
)
3559 request_resize(term
->frontend
,
3560 def(term
->esc_args
[0],
3561 term
->cfg
.width
), term
->rows
);
3565 case 'X': /* ECH: write N spaces w/o moving cursor */
3566 /* XXX VTTEST says this is vt220, vt510 manual
3568 compatibility(ANSIMIN
);
3570 int n
= def(term
->esc_args
[0], 1);
3572 int p
= term
->curs
.x
;
3573 termline
*cline
= scrlineptr(term
->curs
.y
);
3575 if (n
> term
->cols
- term
->curs
.x
)
3576 n
= term
->cols
- term
->curs
.x
;
3577 cursplus
= term
->curs
;
3579 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3580 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
3581 check_selection(term
, term
->curs
, cursplus
);
3583 copy_termchar(cline
, p
++,
3585 term
->seen_disp_event
= TRUE
;
3588 case 'x': /* DECREQTPARM: report terminal characteristics */
3589 compatibility(VT100
);
3592 int i
= def(term
->esc_args
[0], 0);
3593 if (i
== 0 || i
== 1) {
3594 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
3596 ldisc_send(term
->ldisc
, buf
, 20, 0);
3600 case 'Z': /* CBT: BackTab for xterm */
3601 compatibility(OTHER
);
3603 int i
= def(term
->esc_args
[0], 1);
3604 pos old_curs
= term
->curs
;
3606 for(;i
>0 && term
->curs
.x
>0; i
--) {
3609 } while (term
->curs
.x
>0 &&
3610 !term
->tabs
[term
->curs
.x
]);
3612 check_selection(term
, old_curs
, term
->curs
);
3615 case ANSI('c', '='): /* Hide or Show Cursor */
3616 compatibility(SCOANSI
);
3617 switch(term
->esc_args
[0]) {
3618 case 0: /* hide cursor */
3619 term
->cursor_on
= FALSE
;
3621 case 1: /* restore cursor */
3622 term
->big_cursor
= FALSE
;
3623 term
->cursor_on
= TRUE
;
3625 case 2: /* block cursor */
3626 term
->big_cursor
= TRUE
;
3627 term
->cursor_on
= TRUE
;
3631 case ANSI('C', '='):
3633 * set cursor start on scanline esc_args[0] and
3634 * end on scanline esc_args[1].If you set
3635 * the bottom scan line to a value less than
3636 * the top scan line, the cursor will disappear.
3638 compatibility(SCOANSI
);
3639 if (term
->esc_nargs
>= 2) {
3640 if (term
->esc_args
[0] > term
->esc_args
[1])
3641 term
->cursor_on
= FALSE
;
3643 term
->cursor_on
= TRUE
;
3646 case ANSI('D', '='):
3647 compatibility(SCOANSI
);
3648 term
->blink_is_real
= FALSE
;
3649 if (term
->esc_args
[0]>=1)
3650 term
->curr_attr
|= ATTR_BLINK
;
3652 term
->curr_attr
&= ~ATTR_BLINK
;
3654 case ANSI('E', '='):
3655 compatibility(SCOANSI
);
3656 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
3658 case ANSI('F', '='): /* set normal foreground */
3659 compatibility(SCOANSI
);
3660 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3662 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3663 ((term
->esc_args
[0] & 0x8) << 1)) <<
3665 term
->curr_attr
&= ~ATTR_FGMASK
;
3666 term
->curr_attr
|= colour
;
3667 term
->default_attr
&= ~ATTR_FGMASK
;
3668 term
->default_attr
|= colour
;
3671 case ANSI('G', '='): /* set normal background */
3672 compatibility(SCOANSI
);
3673 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3675 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3676 ((term
->esc_args
[0] & 0x8) << 1)) <<
3678 term
->curr_attr
&= ~ATTR_BGMASK
;
3679 term
->curr_attr
|= colour
;
3680 term
->default_attr
&= ~ATTR_BGMASK
;
3681 term
->default_attr
|= colour
;
3684 case ANSI('L', '='):
3685 compatibility(SCOANSI
);
3686 term
->use_bce
= (term
->esc_args
[0] <= 0);
3687 set_erase_char(term
);
3689 case ANSI('p', '"'): /* DECSCL: set compat level */
3691 * Allow the host to make this emulator a
3692 * 'perfect' VT102. This first appeared in
3693 * the VT220, but we do need to get back to
3694 * PuTTY mode so I won't check it.
3696 * The arg in 40..42,50 are a PuTTY extension.
3697 * The 2nd arg, 8bit vs 7bit is not checked.
3699 * Setting VT102 mode should also change
3700 * the Fkeys to generate PF* codes as a
3701 * real VT102 has no Fkeys. The VT220 does
3702 * this, F11..F13 become ESC,BS,LF other
3703 * Fkeys send nothing.
3705 * Note ESC c will NOT change this!
3708 switch (term
->esc_args
[0]) {
3710 term
->compatibility_level
&= ~TM_VTXXX
;
3711 term
->compatibility_level
|= TM_VT102
;
3714 term
->compatibility_level
&= ~TM_VTXXX
;
3715 term
->compatibility_level
|= TM_VT220
;
3719 if (term
->esc_args
[0] > 60 &&
3720 term
->esc_args
[0] < 70)
3721 term
->compatibility_level
|= TM_VTXXX
;
3725 term
->compatibility_level
&= TM_VTXXX
;
3728 term
->compatibility_level
= TM_PUTTY
;
3731 term
->compatibility_level
= TM_SCOANSI
;
3735 term
->compatibility_level
= TM_PUTTY
;
3741 /* Change the response to CSI c */
3742 if (term
->esc_args
[0] == 50) {
3745 strcpy(term
->id_string
, "\033[?");
3746 for (i
= 1; i
< term
->esc_nargs
; i
++) {
3748 strcat(term
->id_string
, ";");
3749 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
3750 strcat(term
->id_string
, lbuf
);
3752 strcat(term
->id_string
, "c");
3755 /* Is this a good idea ?
3756 * Well we should do a soft reset at this point ...
3758 if (!has_compat(VT420
) && has_compat(VT100
)) {
3759 if (!term
->cfg
.no_remote_resize
) {
3760 if (term
->reset_132
)
3761 request_resize(132, 24);
3763 request_resize(80, 24);
3771 term
->osc_w
= FALSE
;
3773 case 'P': /* Linux palette sequence */
3774 term
->termstate
= SEEN_OSC_P
;
3775 term
->osc_strlen
= 0;
3777 case 'R': /* Linux palette reset */
3778 palette_reset(term
->frontend
);
3779 term_invalidate(term
);
3780 term
->termstate
= TOPLEVEL
;
3782 case 'W': /* word-set */
3783 term
->termstate
= SEEN_OSC_W
;
3796 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3800 * Grotty hack to support xterm and DECterm title
3801 * sequences concurrently.
3803 if (term
->esc_args
[0] == 2) {
3804 term
->esc_args
[0] = 1;
3807 /* else fall through */
3809 term
->termstate
= OSC_STRING
;
3810 term
->osc_strlen
= 0;
3815 * This OSC stuff is EVIL. It takes just one character to get into
3816 * sysline mode and it's not initially obvious how to get out.
3817 * So I've added CR and LF as string aborts.
3818 * This shouldn't effect compatibility as I believe embedded
3819 * control characters are supposed to be interpreted (maybe?)
3820 * and they don't display anything useful anyway.
3824 if (c
== '\012' || c
== '\015') {
3825 term
->termstate
= TOPLEVEL
;
3826 } else if (c
== 0234 || c
== '\007') {
3828 * These characters terminate the string; ST and BEL
3829 * terminate the sequence and trigger instant
3830 * processing of it, whereas ESC goes back to SEEN_ESC
3831 * mode unless it is followed by \, in which case it is
3832 * synonymous with ST in the first place.
3835 term
->termstate
= TOPLEVEL
;
3836 } else if (c
== '\033')
3837 term
->termstate
= OSC_MAYBE_ST
;
3838 else if (term
->osc_strlen
< OSC_STR_MAX
)
3839 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
3843 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
3845 if ((int)c
>= '0' && (int)c
<= '9')
3847 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
3849 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
3852 term
->termstate
= TOPLEVEL
;
3855 term
->osc_string
[term
->osc_strlen
++] = val
;
3856 if (term
->osc_strlen
>= 7) {
3857 palette_set(term
->frontend
, term
->osc_string
[0],
3858 term
->osc_string
[1] * 16 + term
->osc_string
[2],
3859 term
->osc_string
[3] * 16 + term
->osc_string
[4],
3860 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
3861 term_invalidate(term
);
3862 term
->termstate
= TOPLEVEL
;
3878 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3881 term
->termstate
= OSC_STRING
;
3882 term
->osc_strlen
= 0;
3886 term
->termstate
= TOPLEVEL
;
3887 term
->seen_disp_event
= TRUE
;
3890 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
3893 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
3896 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
3899 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
3902 * From the VT100 Manual
3903 * NOTE: The special graphics characters in the VT100
3904 * are different from those in the VT52
3906 * From VT102 manual:
3907 * 137 _ Blank - Same
3908 * 140 ` Reserved - Humm.
3909 * 141 a Solid rectangle - Similar
3910 * 142 b 1/ - Top half of fraction for the
3911 * 143 c 3/ - subscript numbers below.
3914 * 146 f Degrees - Same
3915 * 147 g Plus or minus - Same
3917 * 151 i Ellipsis (dots)
3920 * 154 l Bar at scan 0
3921 * 155 m Bar at scan 1
3922 * 156 n Bar at scan 2
3923 * 157 o Bar at scan 3 - Similar
3924 * 160 p Bar at scan 4 - Similar
3925 * 161 q Bar at scan 5 - Similar
3926 * 162 r Bar at scan 6 - Same
3927 * 163 s Bar at scan 7 - Similar
3942 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
3945 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
3948 move(term
, 0, 0, 0);
3951 if (term
->curs
.y
== 0)
3952 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
3953 else if (term
->curs
.y
> 0)
3955 term
->wrapnext
= FALSE
;
3958 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3962 erase_lots(term
, TRUE
, FALSE
, TRUE
);
3966 /* XXX Print cursor line */
3969 /* XXX Start controller mode */
3972 /* XXX Stop controller mode */
3976 term
->termstate
= VT52_Y1
;
3980 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
3983 term
->app_keypad_keys
= TRUE
;
3986 term
->app_keypad_keys
= FALSE
;
3989 /* XXX This should switch to VT100 mode not current or default
3990 * VT mode. But this will only have effect in a VT220+
3993 term
->vt52_mode
= FALSE
;
3994 term
->blink_is_real
= term
->cfg
.blinktext
;
3998 /* XXX Enter auto print mode */
4001 /* XXX Exit auto print mode */
4004 /* XXX Print screen */
4010 /* compatibility(ATARI) */
4011 move(term
, 0, 0, 0);
4012 erase_lots(term
, FALSE
, FALSE
, TRUE
);
4016 /* compatibility(ATARI) */
4017 if (term
->curs
.y
<= term
->marg_b
)
4018 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
4021 /* compatibility(ATARI) */
4022 if (term
->curs
.y
<= term
->marg_b
)
4023 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
4026 /* compatibility(ATARI) */
4027 term
->termstate
= VT52_FG
;
4030 /* compatibility(ATARI) */
4031 term
->termstate
= VT52_BG
;
4034 /* compatibility(ATARI) */
4035 erase_lots(term
, FALSE
, TRUE
, FALSE
);
4039 /* compatibility(ATARI) */
4040 term
->cursor_on
= TRUE
;
4043 /* compatibility(ATARI) */
4044 term
->cursor_on
= FALSE
;
4046 /* case 'j': Save cursor position - broken on ST */
4047 /* case 'k': Restore cursor position */
4049 /* compatibility(ATARI) */
4050 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4052 term
->wrapnext
= FALSE
;
4055 /* compatibility(ATARI) */
4056 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4059 /* compatibility(ATARI) */
4060 term
->curr_attr
|= ATTR_REVERSE
;
4063 /* compatibility(ATARI) */
4064 term
->curr_attr
&= ~ATTR_REVERSE
;
4066 case 'v': /* wrap Autowrap on - Wyse style */
4067 /* compatibility(ATARI) */
4070 case 'w': /* Autowrap off */
4071 /* compatibility(ATARI) */
4076 /* compatibility(OTHER) */
4077 term
->vt52_bold
= FALSE
;
4078 term
->curr_attr
= ATTR_DEFAULT
;
4079 set_erase_char(term
);
4082 /* compatibility(VI50) */
4083 term
->curr_attr
|= ATTR_UNDER
;
4086 /* compatibility(VI50) */
4087 term
->curr_attr
&= ~ATTR_UNDER
;
4090 /* compatibility(VI50) */
4091 term
->vt52_bold
= TRUE
;
4092 term
->curr_attr
|= ATTR_BOLD
;
4095 /* compatibility(VI50) */
4096 term
->vt52_bold
= FALSE
;
4097 term
->curr_attr
&= ~ATTR_BOLD
;
4103 term
->termstate
= VT52_Y2
;
4104 move(term
, term
->curs
.x
, c
- ' ', 0);
4107 term
->termstate
= TOPLEVEL
;
4108 move(term
, c
- ' ', term
->curs
.y
, 0);
4113 term
->termstate
= TOPLEVEL
;
4114 term
->curr_attr
&= ~ATTR_FGMASK
;
4115 term
->curr_attr
&= ~ATTR_BOLD
;
4116 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
4117 if ((c
& 0x8) || term
->vt52_bold
)
4118 term
->curr_attr
|= ATTR_BOLD
;
4120 set_erase_char(term
);
4123 term
->termstate
= TOPLEVEL
;
4124 term
->curr_attr
&= ~ATTR_BGMASK
;
4125 term
->curr_attr
&= ~ATTR_BLINK
;
4126 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
4128 /* Note: bold background */
4130 term
->curr_attr
|= ATTR_BLINK
;
4132 set_erase_char(term
);
4135 default: break; /* placate gcc warning about enum use */
4137 if (term
->selstate
!= NO_SELECTION
) {
4138 pos cursplus
= term
->curs
;
4140 check_selection(term
, term
->curs
, cursplus
);
4144 term_print_flush(term
);
4145 logflush(term
->logctx
);
4149 * To prevent having to run the reasonably tricky bidi algorithm
4150 * too many times, we maintain a cache of the last lineful of data
4151 * fed to the algorithm on each line of the display.
4153 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4154 termchar
*lbefore
, int width
)
4158 if (!term
->pre_bidi_cache
)
4159 return FALSE
; /* cache doesn't even exist yet! */
4161 if (line
>= term
->bidi_cache_size
)
4162 return FALSE
; /* cache doesn't have this many lines */
4164 if (!term
->pre_bidi_cache
[line
])
4165 return FALSE
; /* cache doesn't contain _this_ line */
4167 for (i
= 0; i
< width
; i
++)
4168 if (!termchars_equal(term
->pre_bidi_cache
[line
] + i
, lbefore
+ i
))
4169 return FALSE
; /* line doesn't match cache */
4171 return TRUE
; /* it didn't match. */
4174 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4175 termchar
*lafter
, int width
)
4177 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4178 int j
= term
->bidi_cache_size
;
4179 term
->bidi_cache_size
= line
+1;
4180 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4181 term
->bidi_cache_size
,
4183 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4184 term
->bidi_cache_size
,
4186 while (j
< term
->bidi_cache_size
) {
4187 term
->pre_bidi_cache
[j
] = term
->post_bidi_cache
[j
] = NULL
;
4192 sfree(term
->pre_bidi_cache
[line
]);
4193 sfree(term
->post_bidi_cache
[line
]);
4195 term
->pre_bidi_cache
[line
] = snewn(width
, termchar
);
4196 term
->post_bidi_cache
[line
] = snewn(width
, termchar
);
4198 memcpy(term
->pre_bidi_cache
[line
], lbefore
, width
* TSIZE
);
4199 memcpy(term
->post_bidi_cache
[line
], lafter
, width
* TSIZE
);
4203 * Given a context, update the window. Out of paranoia, we don't
4204 * allow WM_PAINT responses to do scrolling optimisations.
4206 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4208 int i
, it
, j
, our_curs_y
, our_curs_x
;
4213 termchar cursor_background
;
4214 unsigned long ticks
;
4215 #ifdef OPTIMISE_SCROLL
4216 struct scrollregion
*sr
;
4217 #endif /* OPTIMISE_SCROLL */
4219 cursor_background
= term
->basic_erase_char
;
4222 ch
= snewn(chlen
, wchar_t);
4225 * Check the visual bell state.
4227 if (term
->in_vbell
) {
4228 ticks
= GETTICKCOUNT();
4229 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
4230 term
->in_vbell
= FALSE
;
4233 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4236 * screen array, disptop, scrtop,
4238 * cfg.blinkpc, blink_is_real, tblinker,
4239 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4242 /* Has the cursor position or type changed ? */
4243 if (term
->cursor_on
) {
4244 if (term
->has_focus
) {
4245 if (term
->blinker
|| !term
->cfg
.blink_cur
)
4246 cursor
= TATTR_ACTCURS
;
4250 cursor
= TATTR_PASCURS
;
4252 cursor
|= TATTR_RIGHTCURS
;
4255 our_curs_y
= term
->curs
.y
- term
->disptop
;
4258 * Adjust the cursor position in the case where it's
4259 * resting on the right-hand half of a CJK wide character.
4260 * xterm's behaviour here, which seems adequate to me, is
4261 * to display the cursor covering the _whole_ character,
4262 * exactly as if it were one space to the left.
4264 termline
*ldata
= lineptr(term
->curs
.y
);
4265 our_curs_x
= term
->curs
.x
;
4266 if (our_curs_x
> 0 &&
4267 ldata
->chars
[our_curs_x
].chr
== UCSWIDE
)
4273 * If the cursor is not where it was last time we painted, and
4274 * its previous position is visible on screen, invalidate its
4275 * previous position.
4277 if (term
->dispcursy
>= 0 &&
4278 (term
->curstype
!= cursor
||
4279 term
->dispcursy
!= our_curs_y
||
4280 term
->dispcursx
!= our_curs_x
)) {
4281 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4284 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4285 dispcurs
[-1].attr
|= ATTR_INVALID
;
4286 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4287 dispcurs
[1].attr
|= ATTR_INVALID
;
4288 dispcurs
->attr
|= ATTR_INVALID
;
4292 term
->dispcursx
= term
->dispcursy
= -1;
4294 #ifdef OPTIMISE_SCROLL
4296 sr
= term
->scrollhead
;
4298 struct scrollregion
*next
= sr
->next
;
4299 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4303 term
->scrollhead
= term
->scrolltail
= NULL
;
4304 #endif /* OPTIMISE_SCROLL */
4306 /* The normal screen data */
4307 for (i
= 0; i
< term
->rows
; i
++) {
4310 int dirty_line
, dirty_run
, selected
;
4311 unsigned long attr
= 0, cset
= 0;
4312 int updated_line
= 0;
4315 int last_run_dirty
= 0;
4317 scrpos
.y
= i
+ term
->disptop
;
4318 ldata
= lineptr(scrpos
.y
);
4320 dirty_run
= dirty_line
= (ldata
->lattr
!=
4321 term
->disptext
[i
]->lattr
);
4322 term
->disptext
[i
]->lattr
= ldata
->lattr
;
4324 /* Do Arabic shaping and bidi. */
4325 if(!term
->cfg
.bidi
|| !term
->cfg
.arabicshaping
) {
4327 if (!term_bidi_cache_hit(term
, i
, ldata
->chars
, term
->cols
)) {
4329 if (term
->wcFromTo_size
< term
->cols
) {
4330 term
->wcFromTo_size
= term
->cols
;
4331 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4333 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4337 for(it
=0; it
<term
->cols
; it
++)
4339 unsigned long uc
= (ldata
->chars
[it
].chr
);
4341 switch (uc
& CSET_MASK
) {
4343 if (!term
->cfg
.rawcnp
) {
4344 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4348 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4351 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4354 switch (uc
& CSET_MASK
) {
4356 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4359 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4363 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4365 term
->wcFrom
[it
].index
= it
;
4369 do_bidi(term
->wcFrom
, term
->cols
);
4371 /* this is saved iff done from inside the shaping */
4372 if(!term
->cfg
.bidi
&& term
->cfg
.arabicshaping
)
4373 for(it
=0; it
<term
->cols
; it
++)
4374 term
->wcTo
[it
] = term
->wcFrom
[it
];
4376 if(!term
->cfg
.arabicshaping
)
4377 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4379 if (term
->ltemp_size
< ldata
->size
) {
4380 term
->ltemp_size
= ldata
->size
;
4381 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4385 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4387 for(it
=0; it
<term
->cols
; it
++)
4389 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4390 if (term
->ltemp
[it
].cc_next
)
4391 term
->ltemp
[it
].cc_next
-=
4392 it
- term
->wcTo
[it
].index
;
4394 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4395 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4397 term_bidi_cache_store(term
, i
, ldata
->chars
,
4398 term
->ltemp
, ldata
->size
);
4400 lchars
= term
->ltemp
;
4402 lchars
= term
->post_bidi_cache
[i
];
4405 lchars
= ldata
->chars
;
4407 for (j
= 0; j
< term
->cols
; j
++) {
4408 unsigned long tattr
, tchar
;
4409 termchar
*d
= lchars
+ j
;
4410 int break_run
, do_copy
;
4416 switch (tchar
& CSET_MASK
) {
4418 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4421 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4424 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4427 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4430 /* Video reversing things */
4431 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4432 if (term
->seltype
== LEXICOGRAPHIC
)
4433 selected
= (posle(term
->selstart
, scrpos
) &&
4434 poslt(scrpos
, term
->selend
));
4436 selected
= (posPle(term
->selstart
, scrpos
) &&
4437 posPlt(scrpos
, term
->selend
));
4441 ^ (selected ? ATTR_REVERSE
: 0));
4443 /* 'Real' blinking ? */
4444 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4445 if (term
->has_focus
&& term
->tblinker
) {
4446 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4448 tattr
&= ~ATTR_BLINK
;
4452 * Check the font we'll _probably_ be using to see if
4453 * the character is wide when we don't want it to be.
4455 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4456 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4458 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4459 tattr
|= ATTR_NARROW
;
4460 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4461 tattr
|= ATTR_NARROW
;
4463 /* Cursor here ? Save the 'background' */
4464 if (i
== our_curs_y
&& j
== our_curs_x
) {
4466 cursor_background
.chr
= tchar
;
4467 cursor_background
.attr
= tattr
;
4468 /* For once, this cc_next field is an absolute index in lchars */
4470 cursor_background
.cc_next
= d
->cc_next
+ j
;
4472 cursor_background
.cc_next
= 0;
4473 term
->dispcursx
= j
;
4474 term
->dispcursy
= i
;
4477 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
4480 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
4482 /* Special hack for VT100 Linedraw glyphs */
4483 if (tchar
>= 0x23BA && tchar
<= 0x23BD)
4487 * Separate out sequences of characters that have the
4488 * same CSET, if that CSET is a magic one.
4490 if (CSET_OF(tchar
) != cset
)
4494 * Break on both sides of any combined-character cell.
4496 if (d
->cc_next
!= 0 ||
4497 (j
> 0 && d
[-1].cc_next
!= 0))
4500 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
4501 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
4502 term
->disptext
[i
]->chars
[j
].attr
== tattr
)
4504 else if (!dirty_run
&& ccount
== 1)
4509 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
4510 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4516 cset
= CSET_OF(tchar
);
4517 if (term
->ucsdata
->dbcs_screenfont
)
4518 last_run_dirty
= dirty_run
;
4519 dirty_run
= dirty_line
;
4523 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
4529 if (ccount
>= chlen
) {
4530 chlen
= ccount
+ 256;
4531 ch
= sresize(ch
, chlen
, wchar_t);
4533 ch
[ccount
++] = (wchar_t) tchar
;
4538 while (dd
->cc_next
) {
4539 unsigned long schar
;
4544 switch (schar
& CSET_MASK
) {
4546 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4549 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4552 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4556 if (ccount
>= chlen
) {
4557 chlen
= ccount
+ 256;
4558 ch
= sresize(ch
, chlen
, wchar_t);
4560 ch
[ccount
++] = (wchar_t) schar
;
4563 attr
|= TATTR_COMBINING
;
4567 copy_termchar(term
->disptext
[i
], j
, d
);
4568 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
4569 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
4572 /* If it's a wide char step along to the next one. */
4573 if (tattr
& ATTR_WIDE
) {
4574 if (++j
< term
->cols
) {
4577 * By construction above, the cursor should not
4578 * be on the right-hand half of this character.
4581 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
4582 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
4584 copy_termchar(term
->disptext
[i
], j
, d
);
4588 if (dirty_run
&& ccount
> 0) {
4589 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4593 /* Cursor on this line ? (and changed) */
4594 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
4595 ch
[0] = (wchar_t) cursor_background
.chr
;
4596 attr
= cursor_background
.attr
| cursor
;
4599 if (cursor_background
.cc_next
) {
4600 termchar
*dd
= ldata
->chars
+ cursor_background
.cc_next
;
4603 unsigned long schar
;
4606 switch (schar
& CSET_MASK
) {
4608 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4611 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4614 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4618 if (ccount
>= chlen
) {
4619 chlen
= ccount
+ 256;
4620 ch
= sresize(ch
, chlen
, wchar_t);
4622 ch
[ccount
++] = (wchar_t) schar
;
4630 attr
|= TATTR_COMBINING
;
4633 do_cursor(ctx
, our_curs_x
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4634 term
->curstype
= cursor
;
4644 * Flick the switch that says if blinking things should be shown or hidden.
4647 void term_blink(Terminal
*term
, int flg
)
4649 long now
, blink_diff
;
4651 now
= GETTICKCOUNT();
4652 blink_diff
= now
- term
->last_tblink
;
4654 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4655 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
4656 term
->last_tblink
= now
;
4657 term
->tblinker
= !term
->tblinker
;
4662 term
->last_blink
= now
;
4666 blink_diff
= now
- term
->last_blink
;
4668 /* Make sure the cursor blinks no faster than system blink rate */
4669 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
4672 term
->last_blink
= now
;
4673 term
->blinker
= !term
->blinker
;
4677 * Invalidate the whole screen so it will be repainted in full.
4679 void term_invalidate(Terminal
*term
)
4683 for (i
= 0; i
< term
->rows
; i
++)
4684 for (j
= 0; j
< term
->cols
; j
++)
4685 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4689 * Paint the window in response to a WM_PAINT message.
4691 void term_paint(Terminal
*term
, Context ctx
,
4692 int left
, int top
, int right
, int bottom
, int immediately
)
4695 if (left
< 0) left
= 0;
4696 if (top
< 0) top
= 0;
4697 if (right
>= term
->cols
) right
= term
->cols
-1;
4698 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
4700 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
4701 if ((term
->disptext
[i
]->lattr
& LATTR_MODE
) == LATTR_NORM
)
4702 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
4703 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4705 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
4706 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4709 /* This should happen soon enough, also for some reason it sometimes
4710 * fails to actually do anything when re-sizing ... painting the wrong
4714 do_paint (term
, ctx
, FALSE
);
4718 * Attempt to scroll the scrollback. The second parameter gives the
4719 * position we want to scroll to; the first is +1 to denote that
4720 * this position is relative to the beginning of the scrollback, -1
4721 * to denote it is relative to the end, and 0 to denote that it is
4722 * relative to the current position.
4724 void term_scroll(Terminal
*term
, int rel
, int where
)
4726 int sbtop
= -sblines(term
);
4727 #ifdef OPTIMISE_SCROLL
4728 int olddisptop
= term
->disptop
;
4730 #endif /* OPTIMISE_SCROLL */
4732 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
4733 if (term
->disptop
< sbtop
)
4734 term
->disptop
= sbtop
;
4735 if (term
->disptop
> 0)
4738 #ifdef OPTIMISE_SCROLL
4739 shift
= (term
->disptop
- olddisptop
);
4740 if (shift
< term
->rows
&& shift
> -term
->rows
)
4741 scroll_display(term
, 0, term
->rows
- 1, shift
);
4742 #endif /* OPTIMISE_SCROLL */
4746 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
4749 wchar_t *wbptr
; /* where next char goes within workbuf */
4751 int wblen
= 0; /* workbuf len */
4752 int buflen
; /* amount of memory allocated to workbuf */
4754 buflen
= 5120; /* Default size */
4755 workbuf
= snewn(buflen
, wchar_t);
4756 wbptr
= workbuf
; /* start filling here */
4757 old_top_x
= top
.x
; /* needed for rect==1 */
4759 while (poslt(top
, bottom
)) {
4761 termline
*ldata
= lineptr(top
.y
);
4765 * nlpos will point at the maximum position on this line we
4766 * should copy up to. So we start it at the end of the
4770 nlpos
.x
= term
->cols
;
4773 * ... move it backwards if there's unused space at the end
4774 * of the line (and also set `nl' if this is the case,
4775 * because in normal selection mode this means we need a
4776 * newline at the end)...
4778 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
4779 while (IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
4782 if (poslt(nlpos
, bottom
))
4784 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
4785 /* Ignore the last char on the line in a WRAPPED2 line. */
4790 * ... and then clip it to the terminal x coordinate if
4791 * we're doing rectangular selection. (In this case we
4792 * still did the above, so that copying e.g. the right-hand
4793 * column from a table doesn't fill with spaces on the
4797 if (nlpos
.x
> bottom
.x
)
4799 nl
= (top
.y
< bottom
.y
);
4802 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
4805 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
4807 wchar_t cbuf
[16], *p
;
4811 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
4817 int uc
= ldata
->chars
[x
].chr
;
4819 switch (uc
& CSET_MASK
) {
4821 if (!term
->cfg
.rawcnp
) {
4822 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4826 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4829 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4832 switch (uc
& CSET_MASK
) {
4834 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4837 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4841 set
= (uc
& CSET_MASK
);
4842 c
= (uc
& ~CSET_MASK
);
4846 if (DIRECT_FONT(uc
)) {
4847 if (c
>= ' ' && c
!= 0x7F) {
4851 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
4853 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
4854 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
4858 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
4862 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
4869 for (p
= cbuf
; *p
; p
++) {
4870 /* Enough overhead for trailing NL and nul */
4871 if (wblen
>= buflen
- 16) {
4873 workbuf
= sresize(workbuf
, buflen
, wchar_t);
4874 wbptr
= workbuf
+ wblen
;
4880 if (ldata
->chars
[x
].cc_next
)
4881 x
+= ldata
->chars
[x
].cc_next
;
4889 for (i
= 0; i
< sel_nl_sz
; i
++) {
4891 *wbptr
++ = sel_nl
[i
];
4895 top
.x
= rect ? old_top_x
: 0;
4899 #if SELECTION_NUL_TERMINATED
4903 write_clip(term
->frontend
, workbuf
, wblen
, desel
); /* transfer to clipbd */
4904 if (buflen
> 0) /* indicates we allocated this buffer */
4908 void term_copyall(Terminal
*term
)
4912 tree234
*screen
= term
->screen
;
4913 top
.y
= -sblines(term
);
4915 bottom
.y
= find_last_nonempty_line(term
, screen
);
4916 bottom
.x
= term
->cols
;
4917 clipme(term
, top
, bottom
, 0, TRUE
);
4921 * The wordness array is mainly for deciding the disposition of the
4922 * US-ASCII characters.
4924 static int wordtype(Terminal
*term
, int uc
)
4927 int start
, end
, ctype
;
4929 static const struct ucsword ucs_words
[] = {
4935 0x037e, 0x037e, 1}, /* Greek question mark */
4937 0x0387, 0x0387, 1}, /* Greek ano teleia */
4939 0x055a, 0x055f, 1}, /* Armenian punctuation */
4941 0x0589, 0x0589, 1}, /* Armenian full stop */
4943 0x0700, 0x070d, 1}, /* Syriac punctuation */
4945 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4947 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4949 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4951 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4953 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4955 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4957 0x2000, 0x200a, 0}, /* Various spaces */
4959 0x2070, 0x207f, 2}, /* superscript */
4961 0x2080, 0x208f, 2}, /* subscript */
4963 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4965 0x3000, 0x3000, 0}, /* ideographic space */
4967 0x3001, 0x3020, 1}, /* ideographic punctuation */
4969 0x303f, 0x309f, 3}, /* Hiragana */
4971 0x30a0, 0x30ff, 3}, /* Katakana */
4973 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4975 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4977 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4979 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4981 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4983 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4985 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4987 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4989 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4993 const struct ucsword
*wptr
;
4995 switch (uc
& CSET_MASK
) {
4997 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
5000 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
5003 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
5006 switch (uc
& CSET_MASK
) {
5008 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
5011 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
5015 /* For DBCS fonts I can't do anything useful. Even this will sometimes
5016 * fail as there's such a thing as a double width space. :-(
5018 if (term
->ucsdata
->dbcs_screenfont
&&
5019 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
5023 return term
->wordness
[uc
];
5025 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
5026 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
5034 * Spread the selection outwards according to the selection mode.
5036 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
5040 int topy
= -sblines(term
);
5042 ldata
= lineptr(p
.y
);
5044 switch (term
->selmode
) {
5047 * In this mode, every character is a separate unit, except
5048 * for runs of spaces at the end of a non-wrapping line.
5050 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5051 termchar
*q
= ldata
->chars
+ term
->cols
;
5052 while (q
> ldata
->chars
&& IS_SPACE_CHR(q
[-1].chr
))
5054 if (q
== ldata
->chars
+ term
->cols
)
5056 if (p
.x
>= q
- ldata
->chars
)
5057 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5062 * In this mode, the units are maximal runs of characters
5063 * whose `wordness' has the same value.
5065 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5068 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5069 term
->cols
-1 : term
->cols
);
5070 if (p
.x
< maxcols
-1) {
5071 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5076 if (ldata
->lattr
& LATTR_WRAPPED
) {
5078 ldata2
= lineptr(p
.y
+1);
5079 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5096 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5105 ldata2
= lineptr(p
.y
-1);
5106 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5107 term
->cols
-1 : term
->cols
);
5108 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5109 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5127 * In this mode, every line is a unit.
5129 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5137 static void sel_spread(Terminal
*term
)
5139 if (term
->seltype
== LEXICOGRAPHIC
) {
5140 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5141 decpos(term
->selend
);
5142 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5143 incpos(term
->selend
);
5147 void term_do_paste(Terminal
*term
)
5152 get_clip(term
->frontend
, &data
, &len
);
5153 if (data
&& len
> 0) {
5156 term_seen_key_event(term
); /* pasted data counts */
5158 if (term
->paste_buffer
)
5159 sfree(term
->paste_buffer
);
5160 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5161 term
->paste_buffer
= snewn(len
, wchar_t);
5164 while (p
< data
+ len
) {
5165 while (p
< data
+ len
&&
5166 !(p
<= data
+ len
- sel_nl_sz
&&
5167 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5172 for (i
= 0; i
< p
- q
; i
++) {
5173 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5177 if (p
<= data
+ len
- sel_nl_sz
&&
5178 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5179 term
->paste_buffer
[term
->paste_len
++] = '\015';
5185 /* Assume a small paste will be OK in one go. */
5186 if (term
->paste_len
< 256) {
5188 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5189 if (term
->paste_buffer
)
5190 sfree(term
->paste_buffer
);
5191 term
->paste_buffer
= 0;
5192 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5195 get_clip(term
->frontend
, NULL
, NULL
);
5198 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5199 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5203 int raw_mouse
= (term
->xterm_mouse
&&
5204 !term
->cfg
.no_mouse_rep
&&
5205 !(term
->cfg
.mouse_override
&& shift
));
5206 int default_seltype
;
5210 if (a
== MA_DRAG
&& !raw_mouse
)
5211 term_scroll(term
, 0, -1);
5213 if (y
>= term
->rows
) {
5215 if (a
== MA_DRAG
&& !raw_mouse
)
5216 term_scroll(term
, 0, +1);
5225 if (x
>= term
->cols
)
5228 selpoint
.y
= y
+ term
->disptop
;
5230 ldata
= lineptr(selpoint
.y
);
5231 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5236 int encstate
= 0, r
, c
;
5243 encstate
= 0x20; /* left button down */
5254 case MBT_WHEEL_DOWN
:
5257 default: break; /* placate gcc warning about enum use */
5261 if (term
->xterm_mouse
== 1)
5267 term
->mouse_is_down
= 0;
5270 if (term
->mouse_is_down
== braw
)
5272 term
->mouse_is_down
= braw
;
5274 default: break; /* placate gcc warning about enum use */
5283 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5284 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5290 * Set the selection type (rectangular or normal) at the start
5291 * of a selection attempt, from the state of Alt.
5293 if (!alt
^ !term
->cfg
.rect_select
)
5294 default_seltype
= RECTANGULAR
;
5296 default_seltype
= LEXICOGRAPHIC
;
5298 if (term
->selstate
== NO_SELECTION
) {
5299 term
->seltype
= default_seltype
;
5302 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5304 term
->selstate
= ABOUT_TO
;
5305 term
->seltype
= default_seltype
;
5306 term
->selanchor
= selpoint
;
5307 term
->selmode
= SM_CHAR
;
5308 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5310 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5311 term
->selstate
= DRAGGING
;
5312 term
->selstart
= term
->selanchor
= selpoint
;
5313 term
->selend
= term
->selstart
;
5314 incpos(term
->selend
);
5316 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5317 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5318 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5320 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5321 term
->selstate
== SELECTED
) {
5322 if (term
->seltype
== LEXICOGRAPHIC
) {
5324 * For normal selection, we extend by moving
5325 * whichever end of the current selection is closer
5328 if (posdiff(selpoint
, term
->selstart
) <
5329 posdiff(term
->selend
, term
->selstart
) / 2) {
5330 term
->selanchor
= term
->selend
;
5331 decpos(term
->selanchor
);
5333 term
->selanchor
= term
->selstart
;
5337 * For rectangular selection, we have a choice of
5338 * _four_ places to put selanchor and selpoint: the
5339 * four corners of the selection.
5341 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5342 term
->selanchor
.x
= term
->selend
.x
-1;
5344 term
->selanchor
.x
= term
->selstart
.x
;
5346 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5347 term
->selanchor
.y
= term
->selend
.y
;
5349 term
->selanchor
.y
= term
->selstart
.y
;
5351 term
->selstate
= DRAGGING
;
5353 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5354 term
->selanchor
= selpoint
;
5355 term
->selstate
= DRAGGING
;
5356 if (term
->seltype
== LEXICOGRAPHIC
) {
5358 * For normal selection, we set (selstart,selend) to
5359 * (selpoint,selanchor) in some order.
5361 if (poslt(selpoint
, term
->selanchor
)) {
5362 term
->selstart
= selpoint
;
5363 term
->selend
= term
->selanchor
;
5364 incpos(term
->selend
);
5366 term
->selstart
= term
->selanchor
;
5367 term
->selend
= selpoint
;
5368 incpos(term
->selend
);
5372 * For rectangular selection, we may need to
5373 * interchange x and y coordinates (if the user has
5374 * dragged in the -x and +y directions, or vice versa).
5376 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5377 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5378 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5379 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5382 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5384 if (term
->selstate
== DRAGGING
) {
5386 * We've completed a selection. We now transfer the
5387 * data to the clipboard.
5389 clipme(term
, term
->selstart
, term
->selend
,
5390 (term
->seltype
== RECTANGULAR
), FALSE
);
5391 term
->selstate
= SELECTED
;
5393 term
->selstate
= NO_SELECTION
;
5394 } else if (bcooked
== MBT_PASTE
5396 #if MULTICLICK_ONLY_EVENT
5397 || a
== MA_2CLK
|| a
== MA_3CLK
5400 request_paste(term
->frontend
);
5406 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
5407 unsigned int modifiers
, unsigned int flags
)
5411 int prependesc
= FALSE
;
5415 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
5416 for (i
= 0; i
< tlen
; i
++)
5417 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
5418 fprintf(stderr
, "\n");
5422 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
5425 /* Currently, Meta always just prefixes everything with ESC. */
5426 if (modifiers
& PKM_META
)
5428 modifiers
&= ~PKM_META
;
5431 * Alt is only used for Alt+keypad, which isn't supported yet, so
5434 modifiers
&= ~PKM_ALT
;
5436 /* Standard local function keys */
5437 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
5439 if (keysym
== PK_PAGEUP
)
5440 /* scroll up one page */;
5441 if (keysym
== PK_PAGEDOWN
)
5442 /* scroll down on page */;
5443 if (keysym
== PK_INSERT
)
5444 term_do_paste(term
);
5447 if (keysym
== PK_PAGEUP
)
5448 /* scroll up one line */;
5449 if (keysym
== PK_PAGEDOWN
)
5450 /* scroll down one line */;
5451 /* Control-Numlock for app-keypad mode switch */
5452 if (keysym
== PK_PF1
)
5453 term
->app_keypad_keys
^= 1;
5457 if (modifiers
& PKM_ALT
) {
5458 /* Alt+F4 (close) */
5459 /* Alt+Return (full screen) */
5460 /* Alt+Space (system menu) */
5463 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
5464 text
[0] >= 0x20 && text
[0] <= 0x7e) {
5465 /* ASCII chars + Control */
5466 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
5467 (text
[0] >= 0x61 && text
[0] <= 0x7a))
5471 * Control-2 should return ^@ (0x00), Control-6 should return
5472 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5473 * the DOS keyboard handling did it, and we have nothing better
5474 * to do with the key combo in question, we'll also map
5475 * Control-Backquote to ^\ (0x1C).
5478 case ' ': text
[0] = 0x00; break;
5479 case '-': text
[0] = 0x1f; break;
5480 case '/': text
[0] = 0x1f; break;
5481 case '2': text
[0] = 0x00; break;
5482 case '3': text
[0] = 0x1b; break;
5483 case '4': text
[0] = 0x1c; break;
5484 case '5': text
[0] = 0x1d; break;
5485 case '6': text
[0] = 0x1e; break;
5486 case '7': text
[0] = 0x1f; break;
5487 case '8': text
[0] = 0x7f; break;
5488 case '`': text
[0] = 0x1c; break;
5493 /* Nethack keypad */
5494 if (term
->cfg
.nethack_keypad
) {
5497 case PK_KP1
: c
= 'b'; break;
5498 case PK_KP2
: c
= 'j'; break;
5499 case PK_KP3
: c
= 'n'; break;
5500 case PK_KP4
: c
= 'h'; break;
5501 case PK_KP5
: c
= '.'; break;
5502 case PK_KP6
: c
= 'l'; break;
5503 case PK_KP7
: c
= 'y'; break;
5504 case PK_KP8
: c
= 'k'; break;
5505 case PK_KP9
: c
= 'u'; break;
5506 default: break; /* else gcc warns `enum value not used' */
5510 if (modifiers
& PKM_CONTROL
)
5512 else if (modifiers
& PKM_SHIFT
)
5520 /* Numeric Keypad */
5521 if (PK_ISKEYPAD(keysym
)) {
5525 * In VT400 mode, PFn always emits an escape sequence. In
5526 * Linux and tilde modes, this only happens in app keypad mode.
5528 if (term
->cfg
.funky_type
== FUNKY_VT400
||
5529 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
5530 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
5531 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
5533 case PK_PF1
: xkey
= 'P'; break;
5534 case PK_PF2
: xkey
= 'Q'; break;
5535 case PK_PF3
: xkey
= 'R'; break;
5536 case PK_PF4
: xkey
= 'S'; break;
5537 default: break; /* else gcc warns `enum value not used' */
5540 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
5542 case PK_KP0
: xkey
= 'p'; break;
5543 case PK_KP1
: xkey
= 'q'; break;
5544 case PK_KP2
: xkey
= 'r'; break;
5545 case PK_KP3
: xkey
= 's'; break;
5546 case PK_KP4
: xkey
= 't'; break;
5547 case PK_KP5
: xkey
= 'u'; break;
5548 case PK_KP6
: xkey
= 'v'; break;
5549 case PK_KP7
: xkey
= 'w'; break;
5550 case PK_KP8
: xkey
= 'x'; break;
5551 case PK_KP9
: xkey
= 'y'; break;
5552 case PK_KPDECIMAL
: xkey
= 'n'; break;
5553 case PK_KPENTER
: xkey
= 'M'; break;
5554 default: break; /* else gcc warns `enum value not used' */
5556 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
5558 * xterm can't see the layout of the keypad, so it has
5559 * to rely on the X keysyms returned by the keys.
5560 * Hence, we look at the strings here, not the PuTTY
5561 * keysyms (which describe the layout).
5565 if (modifiers
& PKM_SHIFT
)
5570 case '/': xkey
= 'o'; break;
5571 case '*': xkey
= 'j'; break;
5572 case '-': xkey
= 'm'; break;
5576 * In all other modes, we try to retain the layout of
5577 * the DEC keypad in application mode.
5581 /* This key covers the '-' and ',' keys on a VT220 */
5582 if (modifiers
& PKM_SHIFT
)
5583 xkey
= 'm'; /* VT220 '-' */
5585 xkey
= 'l'; /* VT220 ',' */
5587 case PK_KPMINUS
: xkey
= 'm'; break;
5588 case PK_KPCOMMA
: xkey
= 'l'; break;
5589 default: break; /* else gcc warns `enum value not used' */
5594 if (term
->vt52_mode
) {
5595 if (xkey
>= 'P' && xkey
<= 'S')
5596 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5598 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
5600 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5603 /* Not in application mode -- treat the number pad as arrow keys? */
5604 if ((flags
& PKF_NUMLOCK
) == 0) {
5606 case PK_KP0
: keysym
= PK_INSERT
; break;
5607 case PK_KP1
: keysym
= PK_END
; break;
5608 case PK_KP2
: keysym
= PK_DOWN
; break;
5609 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
5610 case PK_KP4
: keysym
= PK_LEFT
; break;
5611 case PK_KP5
: keysym
= PK_REST
; break;
5612 case PK_KP6
: keysym
= PK_RIGHT
; break;
5613 case PK_KP7
: keysym
= PK_HOME
; break;
5614 case PK_KP8
: keysym
= PK_UP
; break;
5615 case PK_KP9
: keysym
= PK_PAGEUP
; break;
5616 default: break; /* else gcc warns `enum value not used' */
5621 /* Miscellaneous keys */
5628 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
5629 else if (modifiers
== PKM_SHIFT
)
5630 /* We do the opposite of what is configured */
5631 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
5637 else if (modifiers
== PKM_SHIFT
)
5638 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
5641 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5643 if (modifiers
== PKM_CONTROL
)
5648 case PK_KPENTER
: /* Odd keypad modes handled above */
5649 if (modifiers
== 0) {
5651 if (term
->cr_lf_return
)
5655 default: break; /* else gcc warns `enum value not used' */
5658 /* SCO function keys and editing keys */
5659 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
5660 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
5661 static char const codes
[] =
5662 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5663 int index
= keysym
- PK_F1
;
5665 if (modifiers
& PKM_SHIFT
) index
+= 12;
5666 if (modifiers
& PKM_CONTROL
) index
+= 24;
5667 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
5670 if (PK_ISEDITING(keysym
)) {
5674 case PK_DELETE
: *p
++ = 0x7f; goto done
;
5675 case PK_HOME
: xkey
= 'H'; break;
5676 case PK_INSERT
: xkey
= 'L'; break;
5677 case PK_END
: xkey
= 'F'; break;
5678 case PK_PAGEUP
: xkey
= 'I'; break;
5679 case PK_PAGEDOWN
: xkey
= 'G'; break;
5680 default: break; /* else gcc warns `enum value not used' */
5682 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5686 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
5689 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
5690 /* Xterm shuffles these keys, apparently. */
5692 case PK_HOME
: keysym
= PK_INSERT
; break;
5693 case PK_INSERT
: keysym
= PK_HOME
; break;
5694 case PK_DELETE
: keysym
= PK_END
; break;
5695 case PK_END
: keysym
= PK_PAGEUP
; break;
5696 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
5697 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
5698 default: break; /* else gcc warns `enum value not used' */
5703 if (term
->cfg
.rxvt_homeend
&&
5704 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
5705 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
5709 if (term
->vt52_mode
) {
5713 * A real VT52 doesn't have these, and a VT220 doesn't
5714 * send anything for them in VT52 mode.
5717 case PK_HOME
: xkey
= 'H'; break;
5718 case PK_INSERT
: xkey
= 'L'; break;
5719 case PK_DELETE
: xkey
= 'M'; break;
5720 case PK_END
: xkey
= 'E'; break;
5721 case PK_PAGEUP
: xkey
= 'I'; break;
5722 case PK_PAGEDOWN
: xkey
= 'G'; break;
5723 default: xkey
=0; break; /* else gcc warns `enum value not used'*/
5725 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5730 case PK_HOME
: code
= 1; break;
5731 case PK_INSERT
: code
= 2; break;
5732 case PK_DELETE
: code
= 3; break;
5733 case PK_END
: code
= 4; break;
5734 case PK_PAGEUP
: code
= 5; break;
5735 case PK_PAGEDOWN
: code
= 6; break;
5736 default: code
= 0; break; /* else gcc warns `enum value not used' */
5738 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
5742 if (PK_ISFKEY(keysym
)) {
5743 /* Map Shift+F1-F10 to F11-F20 */
5744 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
5746 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
5748 /* XXX This overrides the XTERM/VT52 mode below */
5750 if (keysym
>= PK_F6
) offt
++;
5751 if (keysym
>= PK_F12
) offt
++;
5752 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
5753 'P' + keysym
- PK_F1
- offt
);
5756 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
5757 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
5760 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
5761 if (term
->vt52_mode
)
5762 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
5764 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
5767 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
5771 if (PK_ISCURSOR(keysym
)) {
5775 case PK_UP
: xkey
= 'A'; break;
5776 case PK_DOWN
: xkey
= 'B'; break;
5777 case PK_RIGHT
: xkey
= 'C'; break;
5778 case PK_LEFT
: xkey
= 'D'; break;
5779 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
5780 default: xkey
= 0; break; /* else gcc warns `enum value not used' */
5782 if (term
->vt52_mode
)
5783 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5785 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
5787 /* Useful mapping of Ctrl-arrows */
5788 if (modifiers
== PKM_CONTROL
)
5792 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5794 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5800 if (p
> output
|| tlen
> 0) {
5802 * Interrupt an ongoing paste. I'm not sure
5803 * this is sensible, but for the moment it's
5804 * preferable to having to faff about buffering
5810 * We need not bother about stdin backlogs
5811 * here, because in GUI PuTTY we can't do
5812 * anything about it anyway; there's no means
5813 * of asking Windows to hold off on KEYDOWN
5814 * messages. We _have_ to buffer everything
5817 term_seen_key_event(term
);
5821 fprintf(stderr
, "sending ESC\n");
5823 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
5828 fprintf(stderr
, "sending %d bytes:", p
- output
);
5829 for (i
= 0; i
< p
- output
; i
++)
5830 fprintf(stderr
, " %02x", output
[i
]);
5831 fprintf(stderr
, "\n");
5833 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
5834 } else if (tlen
> 0) {
5836 fprintf(stderr
, "sending %d unichars:", tlen
);
5837 for (i
= 0; i
< tlen
; i
++)
5838 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
5839 fprintf(stderr
, "\n");
5841 luni_send(term
->ldisc
, text
, tlen
, 1);
5846 void term_nopaste(Terminal
*term
)
5848 if (term
->paste_len
== 0)
5850 sfree(term
->paste_buffer
);
5851 term
->paste_buffer
= NULL
;
5852 term
->paste_len
= 0;
5855 int term_paste_pending(Terminal
*term
)
5857 return term
->paste_len
!= 0;
5860 void term_paste(Terminal
*term
)
5862 long now
, paste_diff
;
5864 if (term
->paste_len
== 0)
5867 /* Don't wait forever to paste */
5868 if (term
->paste_hold
) {
5869 now
= GETTICKCOUNT();
5870 paste_diff
= now
- term
->last_paste
;
5871 if (paste_diff
>= 0 && paste_diff
< 450)
5874 term
->paste_hold
= 0;
5876 while (term
->paste_pos
< term
->paste_len
) {
5878 while (n
+ term
->paste_pos
< term
->paste_len
) {
5879 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
5883 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
5884 term
->paste_pos
+= n
;
5886 if (term
->paste_pos
< term
->paste_len
) {
5887 term
->paste_hold
= 1;
5891 sfree(term
->paste_buffer
);
5892 term
->paste_buffer
= NULL
;
5893 term
->paste_len
= 0;
5896 static void deselect(Terminal
*term
)
5898 term
->selstate
= NO_SELECTION
;
5899 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
5902 void term_deselect(Terminal
*term
)
5908 int term_ldisc(Terminal
*term
, int option
)
5910 if (option
== LD_ECHO
)
5911 return term
->term_echoing
;
5912 if (option
== LD_EDIT
)
5913 return term
->term_editing
;
5917 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
5919 bufchain_add(&term
->inbuf
, data
, len
);
5921 if (!term
->in_term_out
) {
5922 term
->in_term_out
= TRUE
;
5923 term_blink(term
, 1);
5925 term
->in_term_out
= FALSE
;
5929 * term_out() always completely empties inbuf. Therefore,
5930 * there's no reason at all to return anything other than zero
5931 * from this function, because there _can't_ be a question of
5932 * the remote side needing to wait until term_out() has cleared
5935 * This is a slightly suboptimal way to deal with SSH2 - in
5936 * principle, the window mechanism would allow us to continue
5937 * to accept data on forwarded ports and X connections even
5938 * while the terminal processing was going slowly - but we
5939 * can't do the 100% right thing without moving the terminal
5940 * processing into a separate thread, and that might hurt
5941 * portability. So we manage stdout buffering the old SSH1 way:
5942 * if the terminal processing goes slowly, the whole SSH
5943 * connection stops accepting data until it's ready.
5945 * In practice, I can't imagine this causing serious trouble.
5950 void term_provide_logctx(Terminal
*term
, void *logctx
)
5952 term
->logctx
= logctx
;