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 static void cc_check(termline
*line
)
137 unsigned char *flags
;
140 assert(line
->size
>= line
->cols
);
142 flags
= snewn(line
->size
, unsigned char);
144 for (i
= 0; i
< line
->size
; i
++)
145 flags
[i
] = (i
< line
->cols
);
147 for (i
= 0; i
< line
->cols
; i
++) {
149 while (line
->chars
[j
].cc_next
) {
150 j
+= line
->chars
[j
].cc_next
;
151 assert(j
>= line
->cols
&& j
< line
->size
);
160 assert(j
>= line
->cols
&& j
< line
->size
);
163 if (line
->chars
[j
].cc_next
)
164 j
+= line
->chars
[j
].cc_next
;
171 for (i
= 0; i
< line
->size
; i
++)
172 j
+= (flags
[i
] != 0);
174 assert(j
== line
->size
);
178 * Add a combining character to a character cell.
180 static void add_cc(termline
*line
, int col
, unsigned long chr
)
184 assert(col
>= 0 && col
< line
->cols
);
187 * Start by extending the cols array if the free list is empty.
189 if (!line
->cc_free
) {
191 line
->size
+= 16 + (line
->size
- line
->cols
) / 2;
192 line
->chars
= sresize(line
->chars
, line
->size
, termchar
);
194 while (n
< line
->size
) {
195 if (n
+1 < line
->size
)
196 line
->chars
[n
].cc_next
= 1;
198 line
->chars
[n
].cc_next
= 0;
204 * Now walk the cc list of the cell in question.
206 while (line
->chars
[col
].cc_next
)
207 col
+= line
->chars
[col
].cc_next
;
210 * `col' now points at the last cc currently in this cell; so
211 * we simply add another one.
213 newcc
= line
->cc_free
;
214 if (line
->chars
[newcc
].cc_next
)
215 line
->cc_free
= newcc
+ line
->chars
[newcc
].cc_next
;
218 line
->chars
[newcc
].cc_next
= 0;
219 line
->chars
[newcc
].chr
= chr
;
220 line
->chars
[col
].cc_next
= newcc
- col
;
226 * Clear the combining character list in a character cell.
228 static void clear_cc(termline
*line
, int col
)
230 int oldfree
, origcol
= col
;
232 assert(col
>= 0 && col
< line
->cols
);
234 if (!line
->chars
[col
].cc_next
)
235 return; /* nothing needs doing */
237 oldfree
= line
->cc_free
;
238 line
->cc_free
= col
+ line
->chars
[col
].cc_next
;
239 while (line
->chars
[col
].cc_next
)
240 col
+= line
->chars
[col
].cc_next
;
242 line
->chars
[col
].cc_next
= oldfree
- col
;
244 line
->chars
[col
].cc_next
= 0;
246 line
->chars
[origcol
].cc_next
= 0;
252 * Compare two character cells for equality. Special case required
253 * in do_paint() where we override what we expect the chr and attr
256 static int termchars_equal_override(termchar
*a
, termchar
*b
,
257 unsigned long bchr
, unsigned long battr
)
262 if (a
->attr
!= battr
)
264 while (a
->cc_next
|| b
->cc_next
) {
265 if (!a
->cc_next
|| !b
->cc_next
)
266 return FALSE
; /* one cc-list ends, other does not */
269 if (a
->chr
!= b
->chr
)
275 static int termchars_equal(termchar
*a
, termchar
*b
)
277 return termchars_equal_override(a
, b
, b
->chr
, b
->attr
);
281 * Copy a character cell. (Requires a pointer to the destination
282 * termline, so as to access its free list.)
284 static void copy_termchar(termline
*destline
, int x
, termchar
*src
)
286 clear_cc(destline
, x
);
288 destline
->chars
[x
] = *src
; /* copy everything except cc-list */
289 destline
->chars
[x
].cc_next
= 0; /* and make sure this is zero */
291 while (src
->cc_next
) {
293 add_cc(destline
, x
, src
->chr
);
298 * Move a character cell within its termline.
300 static void move_termchar(termline
*line
, termchar
*dest
, termchar
*src
)
302 /* First clear the cc list from the original char, just in case. */
303 clear_cc(line
, dest
- line
->chars
);
305 /* Move the character cell and adjust its cc_next. */
306 *dest
= *src
; /* copy everything except cc-list */
308 dest
->cc_next
= src
->cc_next
- (dest
-src
);
310 /* Ensure the original cell doesn't have a cc list. */
315 * Compress and decompress a termline into an RLE-based format for
316 * storing in scrollback. (Since scrollback almost never needs to
317 * be modified and exists in huge quantities, this is a sensible
318 * tradeoff, particularly since it allows us to continue adding
319 * features to the main termchar structure without proportionally
320 * bloating the terminal emulator's memory footprint unless those
321 * features are in constant use.)
327 static void add(struct buf
*b
, unsigned char c
)
329 if (b
->len
>= b
->size
) {
330 b
->size
= (b
->len
* 3 / 2) + 512;
331 b
->data
= sresize(b
->data
, b
->size
, unsigned char);
333 b
->data
[b
->len
++] = c
;
335 static int get(struct buf
*b
)
337 return b
->data
[b
->len
++];
339 static void makerle(struct buf
*b
, termline
*ldata
,
340 void (*makeliteral
)(struct buf
*b
, termchar
*c
,
341 unsigned long *state
))
343 int hdrpos
, hdrsize
, n
, prevlen
, prevpos
, thislen
, thispos
, prev2
;
344 termchar
*c
= ldata
->chars
;
345 unsigned long state
= 0, oldstate
;
352 prevlen
= prevpos
= 0;
357 makeliteral(b
, c
++, &state
);
358 thislen
= b
->len
- thispos
;
359 if (thislen
== prevlen
&&
360 !memcmp(b
->data
+ prevpos
, b
->data
+ thispos
, thislen
)) {
362 * This literal precisely matches the previous one.
363 * Turn it into a run if it's worthwhile.
365 * With one-byte literals, it costs us two bytes to
366 * encode a run, plus another byte to write the header
367 * to resume normal output; so a three-element run is
368 * neutral, and anything beyond that is unconditionally
369 * worthwhile. With two-byte literals or more, even a
372 if (thislen
> 1 || prev2
) {
376 * It's worth encoding a run. Start at prevpos,
377 * unless hdrsize==0 in which case we can back up
378 * another one and start by overwriting hdrpos.
381 hdrsize
--; /* remove the literal at prevpos */
385 prevpos
-= prevlen
;/* and possibly another one */
389 assert(prevpos
== hdrpos
+ 1);
391 b
->len
= prevpos
+prevlen
;
393 memmove(b
->data
+ prevpos
+1, b
->data
+ prevpos
, prevlen
);
395 b
->len
= prevpos
+prevlen
+1;
397 * Terminate the previous run of ordinary
400 assert(hdrsize
>= 1 && hdrsize
<= 128);
401 b
->data
[hdrpos
] = hdrsize
- 1;
404 runlen
= prev2 ?
3 : 2;
406 while (n
> 0 && runlen
< 129) {
410 makeliteral(b
, c
, &state
);
411 tmplen
= b
->len
- tmppos
;
413 if (tmplen
!= thislen
||
414 memcmp(b
->data
+ runpos
+1, b
->data
+ tmppos
, tmplen
)) {
416 break; /* run over */
421 assert(runlen
>= 2 && runlen
<= 129);
422 b
->data
[runpos
] = runlen
+ 0x80 - 2;
431 * Just flag that the previous two literals were
432 * identical, in case we find a third identical one
433 * we want to turn into a run.
446 * This character isn't (yet) part of a run. Add it to
450 if (hdrsize
== 128) {
451 b
->data
[hdrpos
] = hdrsize
- 1;
455 prevlen
= prevpos
= 0;
464 assert(hdrsize
<= 128);
465 b
->data
[hdrpos
] = hdrsize
- 1;
470 static void makeliteral_chr(struct buf
*b
, termchar
*c
, unsigned long *state
)
473 * My encoding for characters is UTF-8-like, in that it stores
474 * 7-bit ASCII in one byte and uses high-bit-set bytes as
475 * introducers to indicate a longer sequence. However, it's
476 * unlike UTF-8 in that it doesn't need to be able to
477 * resynchronise, and therefore I don't want to waste two bits
478 * per byte on having recognisable continuation characters.
479 * Also I don't want to rule out the possibility that I may one
480 * day use values 0x80000000-0xFFFFFFFF for interesting
481 * purposes, so unlike UTF-8 I need a full 32-bit range.
482 * Accordingly, here is my encoding:
484 * 00000000-0000007F: 0xxxxxxx (but see below)
485 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
486 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
487 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
488 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
490 * (`Z' is like `x' but is always going to be zero since the
491 * values I'm encoding don't go above 2^32. In principle the
492 * five-byte form of the encoding could extend to 2^35, and
493 * there could be six-, seven-, eight- and nine-byte forms as
494 * well to allow up to 64-bit values to be encoded. But that's
495 * completely unnecessary for these purposes!)
497 * The encoding as written above would be very simple, except
498 * that 7-bit ASCII can occur in several different ways in the
499 * terminal data; sometimes it crops up in the D800 page
500 * (CSET_ASCII) but at other times it's in the 0000 page (real
501 * Unicode). Therefore, this encoding is actually _stateful_:
502 * the one-byte encoding of 00-7F actually indicates `reuse the
503 * upper three bytes of the last character', and to encode an
504 * absolute value of 00-7F you need to use the two-byte form
507 if ((c
->chr
& ~0x7F) == *state
) {
508 add(b
, (unsigned char)(c
->chr
& 0x7F));
509 } else if (c
->chr
< 0x4000) {
510 add(b
, (unsigned char)(((c
->chr
>> 8) & 0x3F) | 0x80));
511 add(b
, (unsigned char)(c
->chr
& 0xFF));
512 } else if (c
->chr
< 0x200000) {
513 add(b
, (unsigned char)(((c
->chr
>> 16) & 0x1F) | 0xC0));
514 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
515 add(b
, (unsigned char)(c
->chr
& 0xFF));
516 } else if (c
->chr
< 0x10000000) {
517 add(b
, (unsigned char)(((c
->chr
>> 24) & 0x0F) | 0xE0));
518 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
519 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
520 add(b
, (unsigned char)(c
->chr
& 0xFF));
523 add(b
, (unsigned char)((c
->chr
>> 24) & 0xFF));
524 add(b
, (unsigned char)((c
->chr
>> 16) & 0xFF));
525 add(b
, (unsigned char)((c
->chr
>> 8) & 0xFF));
526 add(b
, (unsigned char)(c
->chr
& 0xFF));
528 *state
= c
->chr
& ~0xFF;
530 static void makeliteral_attr(struct buf
*b
, termchar
*c
, unsigned long *state
)
533 * My encoding for attributes is 16-bit-granular and assumes
534 * that the top bit of the word is never required. I either
535 * store a two-byte value with the top bit clear (indicating
536 * just that value), or a four-byte value with the top bit set
537 * (indicating the same value with its top bit clear).
539 if (c
->attr
< 0x8000) {
540 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
541 add(b
, (unsigned char)(c
->attr
& 0xFF));
543 add(b
, (unsigned char)(((c
->attr
>> 24) & 0x7F) | 0x80));
544 add(b
, (unsigned char)((c
->attr
>> 16) & 0xFF));
545 add(b
, (unsigned char)((c
->attr
>> 8) & 0xFF));
546 add(b
, (unsigned char)(c
->attr
& 0xFF));
549 static void makeliteral_cc(struct buf
*b
, termchar
*c
, unsigned long *state
)
552 * For combining characters, I just encode a bunch of ordinary
553 * chars using makeliteral_chr, and terminate with a \0
554 * character (which I know won't come up as a combining char
557 * I don't use the stateful encoding in makeliteral_chr.
559 unsigned long zstate
;
568 makeliteral_chr(b
, c
, &zstate
);
573 makeliteral_chr(b
, &z
, &zstate
);
576 static termline
*decompressline(unsigned char *data
, int *bytes_used
);
578 static unsigned char *compressline(termline
*ldata
)
580 struct buf buffer
= { NULL
, 0, 0 }, *b
= &buffer
;
583 * First, store the column count, 7 bits at a time, least
584 * significant `digit' first, with the high bit set on all but
590 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
593 add(b
, (unsigned char)(n
));
597 * Next store the lattrs; same principle.
600 int n
= ldata
->lattr
;
602 add(b
, (unsigned char)((n
& 0x7F) | 0x80));
605 add(b
, (unsigned char)(n
));
609 * Now we store a sequence of separate run-length encoded
610 * fragments, each containing exactly as many symbols as there
611 * are columns in the ldata.
613 * All of these have a common basic format:
615 * - a byte 00-7F indicates that X+1 literals follow it
616 * - a byte 80-FF indicates that a single literal follows it
617 * and expects to be repeated (X-0x80)+2 times.
619 * The format of the `literals' varies between the fragments.
621 makerle(b
, ldata
, makeliteral_chr
);
622 makerle(b
, ldata
, makeliteral_attr
);
623 makerle(b
, ldata
, makeliteral_cc
);
626 * Diagnostics: ensure that the compressed data really does
627 * decompress to the right thing.
629 #ifndef CHECK_SB_COMPRESSION
635 #ifdef DIAGNOSTIC_SB_COMPRESSION
636 for (i
= 0; i
< b
->len
; i
++) {
637 printf(" %02x ", b
->data
[i
]);
642 dcl
= decompressline(b
->data
, &dused
);
643 assert(b
->len
== dused
);
644 assert(ldata
->cols
== dcl
->cols
);
645 assert(ldata
->lattr
== dcl
->lattr
);
646 for (i
= 0; i
< ldata
->cols
; i
++)
647 assert(termchars_equal(&ldata
->chars
[i
], &dcl
->chars
[i
]));
649 #ifdef DIAGNOSTIC_SB_COMPRESSION
650 printf("%d cols (%d bytes) -> %d bytes (factor of %g)\n",
651 ldata
->cols
, 4 * ldata
->cols
, dused
,
652 (double)dused
/ (4 * ldata
->cols
));
660 * Trim the allocated memory so we don't waste any, and return.
662 return sresize(b
->data
, b
->len
, unsigned char);
665 static void readrle(struct buf
*b
, termline
*ldata
,
666 void (*readliteral
)(struct buf
*b
, termchar
*c
,
667 termline
*ldata
, unsigned long *state
))
670 unsigned long state
= 0;
672 while (n
< ldata
->cols
) {
678 int pos
= b
->len
, count
= hdr
+ 2 - 0x80;
680 assert(n
< ldata
->cols
);
682 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
686 /* Just a sequence of consecutive literals. */
690 assert(n
< ldata
->cols
);
691 readliteral(b
, ldata
->chars
+ n
, ldata
, &state
);
697 assert(n
== ldata
->cols
);
699 static void readliteral_chr(struct buf
*b
, termchar
*c
, termline
*ldata
,
700 unsigned long *state
)
705 * 00000000-0000007F: 0xxxxxxx
706 * 00000080-00003FFF: 10xxxxxx xxxxxxxx
707 * 00004000-001FFFFF: 110xxxxx xxxxxxxx xxxxxxxx
708 * 00200000-0FFFFFFF: 1110xxxx xxxxxxxx xxxxxxxx xxxxxxxx
709 * 10000000-FFFFFFFF: 11110ZZZ xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
714 c
->chr
= byte
| *state
;
715 } else if (byte
< 0xC0) {
716 c
->chr
= (byte
&~ 0xC0) << 8;
718 } else if (byte
< 0xE0) {
719 c
->chr
= (byte
&~ 0xE0) << 16;
720 c
->chr
|= get(b
) << 8;
722 } else if (byte
< 0xF0) {
723 c
->chr
= (byte
&~ 0xF0) << 24;
724 c
->chr
|= get(b
) << 16;
725 c
->chr
|= get(b
) << 8;
728 assert(byte
== 0xF0);
729 c
->chr
= get(b
) << 24;
730 c
->chr
|= get(b
) << 16;
731 c
->chr
|= get(b
) << 8;
734 *state
= c
->chr
& ~0xFF;
736 static void readliteral_attr(struct buf
*b
, termchar
*c
, termline
*ldata
,
737 unsigned long *state
)
752 static void readliteral_cc(struct buf
*b
, termchar
*c
, termline
*ldata
,
753 unsigned long *state
)
756 unsigned long zstate
;
757 int x
= c
- ldata
->chars
;
763 readliteral_chr(b
, &n
, ldata
, &zstate
);
766 add_cc(ldata
, x
, n
.chr
);
770 static termline
*decompressline(unsigned char *data
, int *bytes_used
)
772 int ncols
, byte
, shift
;
773 struct buf buffer
, *b
= &buffer
;
780 * First read in the column count.
785 ncols
|= (byte
& 0x7F) << shift
;
787 } while (byte
& 0x80);
790 * Now create the output termline.
792 ldata
= snew(termline
);
793 ldata
->chars
= snewn(ncols
, termchar
);
794 ldata
->cols
= ldata
->size
= ncols
;
795 ldata
->temporary
= TRUE
;
799 * We must set all the cc pointers in ldata->chars to 0 right
800 * now, so that cc diagnostics that verify the integrity of the
801 * whole line will make sense while we're in the middle of
806 for (i
= 0; i
< ldata
->cols
; i
++)
807 ldata
->chars
[i
].cc_next
= 0;
811 * Now read in the lattr.
813 ldata
->lattr
= shift
= 0;
816 ldata
->lattr
|= (byte
& 0x7F) << shift
;
818 } while (byte
& 0x80);
821 * Now we read in each of the RLE streams in turn.
823 readrle(b
, ldata
, readliteral_chr
);
824 readrle(b
, ldata
, readliteral_attr
);
825 readrle(b
, ldata
, readliteral_cc
);
827 /* Return the number of bytes read, for diagnostic purposes. */
829 *bytes_used
= b
->len
;
835 * Resize a line to make it `cols' columns wide.
837 static void resizeline(Terminal
*term
, termline
*line
, int cols
)
841 if (line
->cols
!= cols
) {
843 * This line is the wrong length, which probably means it
844 * hasn't been accessed since a resize. Resize it now.
846 * First, go through all the characters that will be thrown
847 * out in the resize (if we're shrinking the line) and
848 * return their cc lists to the cc free list.
850 for (i
= cols
; i
< line
->cols
; i
++)
854 * Now do the actual resize, leaving the _same_ amount of
855 * cc space as there was to begin with.
858 line
->size
+= cols
- oldlen
;
859 line
->chars
= sresize(line
->chars
, line
->size
, TTYPE
);
863 * Bodily move the entire cc section from where it started
864 * to where it now needs to be.
866 memmove(line
->chars
+ line
->cols
, line
->chars
+ oldlen
,
867 (line
->size
- line
->cols
) * TSIZE
);
870 * Go through what's left of the original line, and adjust
871 * the first cc_next pointer in each list. (All the
872 * subsequent ones are still valid because they are
873 * relative offsets within the cc block.) Also do the same
874 * to the head of the cc_free list.
876 for (i
= 0; i
< oldlen
&& i
< line
->cols
; i
++)
877 if (line
->chars
[i
].cc_next
)
878 line
->chars
[i
].cc_next
+= cols
- oldlen
;
880 line
->cc_free
+= cols
- oldlen
;
883 * And finally fill in the new space with erase chars. (We
884 * don't have to worry about cc lists here, because we
885 * _know_ the erase char doesn't have one.)
887 for (i
= oldlen
; i
< cols
; i
++)
888 line
->chars
[i
] = term
->basic_erase_char
;
895 * Get the number of lines in the scrollback.
897 static int sblines(Terminal
*term
)
899 int sblines
= count234(term
->scrollback
);
900 if (term
->cfg
.erase_to_scrollback
&&
901 term
->alt_which
&& term
->alt_screen
) {
902 sblines
+= term
->alt_sblines
;
908 * Retrieve a line of the screen or of the scrollback, according to
909 * whether the y coordinate is non-negative or negative
912 static termline
*lineptr(Terminal
*term
, int y
, int lineno
, int screen
)
919 whichtree
= term
->screen
;
926 if (term
->cfg
.erase_to_scrollback
&&
927 term
->alt_which
&& term
->alt_screen
) {
928 altlines
= term
->alt_sblines
;
931 whichtree
= term
->scrollback
;
932 treeindex
= y
+ altlines
+ count234(term
->scrollback
);
934 whichtree
= term
->alt_screen
;
935 treeindex
= y
+ term
->alt_sblines
;
936 /* treeindex = y + count234(term->alt_screen); */
939 if (whichtree
== term
->scrollback
) {
940 unsigned char *cline
= index234(whichtree
, treeindex
);
941 line
= decompressline(cline
, NULL
);
943 line
= index234(whichtree
, treeindex
);
946 /* We assume that we don't screw up and retrieve something out of range. */
947 assert(line
!= NULL
);
949 resizeline(term
, line
, term
->cols
);
950 /* FIXME: should we sort the compressed scrollback out here? */
955 #define lineptr(x) (lineptr)(term,x,__LINE__,FALSE)
956 #define scrlineptr(x) (lineptr)(term,x,__LINE__,TRUE)
959 * Set up power-on settings for the terminal.
961 static void power_on(Terminal
*term
)
963 term
->curs
.x
= term
->curs
.y
= 0;
964 term
->alt_x
= term
->alt_y
= 0;
965 term
->savecurs
.x
= term
->savecurs
.y
= 0;
966 term
->alt_t
= term
->marg_t
= 0;
967 if (term
->rows
!= -1)
968 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
970 term
->alt_b
= term
->marg_b
= 0;
971 if (term
->cols
!= -1) {
973 for (i
= 0; i
< term
->cols
; i
++)
974 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
976 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
977 term
->alt_ins
= term
->insert
= FALSE
;
978 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
979 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
980 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
981 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
983 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
984 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= CSET_ASCII
;
986 term
->in_vbell
= FALSE
;
988 term
->big_cursor
= 0;
989 term
->default_attr
= term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
990 term
->term_editing
= term
->term_echoing
= FALSE
;
991 term
->app_cursor_keys
= term
->cfg
.app_cursor
;
992 term
->app_keypad_keys
= term
->cfg
.app_keypad
;
993 term
->use_bce
= term
->cfg
.bce
;
994 term
->blink_is_real
= term
->cfg
.blinktext
;
995 term
->erase_char
= term
->basic_erase_char
;
997 term_print_finish(term
);
1000 for (i
= 0; i
< 256; i
++)
1001 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1004 swap_screen(term
, 1, FALSE
, FALSE
);
1005 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1006 swap_screen(term
, 0, FALSE
, FALSE
);
1007 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1012 * Force a screen update.
1014 void term_update(Terminal
*term
)
1017 ctx
= get_ctx(term
->frontend
);
1019 int need_sbar_update
= term
->seen_disp_event
;
1020 if (term
->seen_disp_event
&& term
->cfg
.scroll_on_disp
) {
1021 term
->disptop
= 0; /* return to main screen */
1022 term
->seen_disp_event
= 0;
1023 need_sbar_update
= TRUE
;
1026 if (need_sbar_update
)
1028 do_paint(term
, ctx
, TRUE
);
1029 sys_cursor(term
->frontend
, term
->curs
.x
, term
->curs
.y
- term
->disptop
);
1035 * Called from front end when a keypress occurs, to trigger
1036 * anything magical that needs to happen in that situation.
1038 void term_seen_key_event(Terminal
*term
)
1041 * On any keypress, clear the bell overload mechanism
1042 * completely, on the grounds that large numbers of
1043 * beeps coming from deliberate key action are likely
1044 * to be intended (e.g. beeps from filename completion
1045 * blocking repeatedly).
1047 term
->beep_overloaded
= FALSE
;
1048 while (term
->beephead
) {
1049 struct beeptime
*tmp
= term
->beephead
;
1050 term
->beephead
= tmp
->next
;
1053 term
->beeptail
= NULL
;
1057 * Reset the scrollback on keypress, if we're doing that.
1059 if (term
->cfg
.scroll_on_key
) {
1060 term
->disptop
= 0; /* return to main screen */
1061 term
->seen_disp_event
= 1;
1066 * Same as power_on(), but an external function.
1068 void term_pwron(Terminal
*term
)
1071 if (term
->ldisc
) /* cause ldisc to notice changes */
1072 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1078 static void set_erase_char(Terminal
*term
)
1080 term
->erase_char
= term
->basic_erase_char
;
1082 term
->erase_char
.attr
= (term
->curr_attr
&
1083 (ATTR_FGMASK
| ATTR_BGMASK
));
1087 * When the user reconfigures us, we need to check the forbidden-
1088 * alternate-screen config option, disable raw mouse mode if the
1089 * user has disabled mouse reporting, and abandon a print job if
1090 * the user has disabled printing.
1092 void term_reconfig(Terminal
*term
, Config
*cfg
)
1095 * Before adopting the new config, check all those terminal
1096 * settings which control power-on defaults; and if they've
1097 * changed, we will modify the current state as well as the
1098 * default one. The full list is: Auto wrap mode, DEC Origin
1099 * Mode, BCE, blinking text, character classes.
1101 int reset_wrap
, reset_decom
, reset_bce
, reset_blink
, reset_charclass
;
1104 reset_wrap
= (term
->cfg
.wrap_mode
!= cfg
->wrap_mode
);
1105 reset_decom
= (term
->cfg
.dec_om
!= cfg
->dec_om
);
1106 reset_bce
= (term
->cfg
.bce
!= cfg
->bce
);
1107 reset_blink
= (term
->cfg
.blinktext
!= cfg
->blinktext
);
1108 reset_charclass
= 0;
1109 for (i
= 0; i
< lenof(term
->cfg
.wordness
); i
++)
1110 if (term
->cfg
.wordness
[i
] != cfg
->wordness
[i
])
1111 reset_charclass
= 1;
1113 term
->cfg
= *cfg
; /* STRUCTURE COPY */
1116 term
->alt_wrap
= term
->wrap
= term
->cfg
.wrap_mode
;
1118 term
->alt_om
= term
->dec_om
= term
->cfg
.dec_om
;
1120 term
->use_bce
= term
->cfg
.bce
;
1121 set_erase_char(term
);
1124 term
->blink_is_real
= term
->cfg
.blinktext
;
1125 if (reset_charclass
)
1126 for (i
= 0; i
< 256; i
++)
1127 term
->wordness
[i
] = term
->cfg
.wordness
[i
];
1129 if (term
->cfg
.no_alt_screen
)
1130 swap_screen(term
, 0, FALSE
, FALSE
);
1131 if (term
->cfg
.no_mouse_rep
) {
1132 term
->xterm_mouse
= 0;
1133 set_raw_mouse_mode(term
->frontend
, 0);
1135 if (term
->cfg
.no_remote_charset
) {
1136 term
->cset_attr
[0] = term
->cset_attr
[1] = CSET_ASCII
;
1137 term
->sco_acs
= term
->alt_sco_acs
= 0;
1140 if (!*term
->cfg
.printer
) {
1141 term_print_finish(term
);
1146 * Clear the scrollback.
1148 void term_clrsb(Terminal
*term
)
1152 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
1153 sfree(line
); /* this is compressed data, not a termline */
1155 term
->tempsblines
= 0;
1156 term
->alt_sblines
= 0;
1161 * Initialise the terminal.
1163 Terminal
*term_init(Config
*mycfg
, struct unicode_data
*ucsdata
,
1169 * Allocate a new Terminal structure and initialise the fields
1172 term
= snew(Terminal
);
1173 term
->frontend
= frontend
;
1174 term
->ucsdata
= ucsdata
;
1175 term
->cfg
= *mycfg
; /* STRUCTURE COPY */
1176 term
->logctx
= NULL
;
1177 term
->compatibility_level
= TM_PUTTY
;
1178 strcpy(term
->id_string
, "\033[?6c");
1179 term
->last_blink
= term
->last_tblink
= 0;
1180 term
->paste_buffer
= NULL
;
1181 term
->paste_len
= 0;
1182 term
->last_paste
= 0;
1183 bufchain_init(&term
->inbuf
);
1184 bufchain_init(&term
->printer_buf
);
1185 term
->printing
= term
->only_printing
= FALSE
;
1186 term
->print_job
= NULL
;
1187 term
->vt52_mode
= FALSE
;
1188 term
->cr_lf_return
= FALSE
;
1189 term
->seen_disp_event
= FALSE
;
1190 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
1191 term
->reset_132
= FALSE
;
1192 term
->blinker
= term
->tblinker
= 0;
1193 term
->has_focus
= 1;
1194 term
->repeat_off
= FALSE
;
1195 term
->termstate
= TOPLEVEL
;
1196 term
->selstate
= NO_SELECTION
;
1199 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
1200 term
->tempsblines
= 0;
1201 term
->alt_sblines
= 0;
1203 term
->disptext
= NULL
;
1204 term
->dispcursx
= term
->dispcursy
= -1;
1207 term
->rows
= term
->cols
= -1;
1209 term
->beephead
= term
->beeptail
= NULL
;
1210 #ifdef OPTIMISE_SCROLL
1211 term
->scrollhead
= term
->scrolltail
= NULL
;
1212 #endif /* OPTIMISE_SCROLL */
1214 term
->lastbeep
= FALSE
;
1215 term
->beep_overloaded
= FALSE
;
1216 term
->attr_mask
= 0xffffffff;
1217 term
->resize_fn
= NULL
;
1218 term
->resize_ctx
= NULL
;
1219 term
->in_term_out
= FALSE
;
1221 term
->ltemp_size
= 0;
1222 term
->wcFrom
= NULL
;
1224 term
->wcFromTo_size
= 0;
1226 term
->bidi_cache_size
= 0;
1227 term
->pre_bidi_cache
= term
->post_bidi_cache
= NULL
;
1230 term
->basic_erase_char
.chr
= CSET_ASCII
| ' ';
1231 term
->basic_erase_char
.attr
= ATTR_DEFAULT
;
1232 term
->basic_erase_char
.cc_next
= 0;
1233 term
->erase_char
= term
->basic_erase_char
;
1238 void term_free(Terminal
*term
)
1241 struct beeptime
*beep
;
1244 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
)
1245 sfree(line
); /* compressed data, not a termline */
1246 freetree234(term
->scrollback
);
1247 while ((line
= delpos234(term
->screen
, 0)) != NULL
)
1249 freetree234(term
->screen
);
1250 while ((line
= delpos234(term
->alt_screen
, 0)) != NULL
)
1252 freetree234(term
->alt_screen
);
1253 if (term
->disptext
) {
1254 for (i
= 0; i
< term
->rows
; i
++)
1255 freeline(term
->disptext
[i
]);
1257 sfree(term
->disptext
);
1258 while (term
->beephead
) {
1259 beep
= term
->beephead
;
1260 term
->beephead
= beep
->next
;
1263 bufchain_clear(&term
->inbuf
);
1265 printer_finish_job(term
->print_job
);
1266 bufchain_clear(&term
->printer_buf
);
1267 sfree(term
->paste_buffer
);
1269 sfree(term
->wcFrom
);
1272 for (i
= 0; i
< term
->bidi_cache_size
; i
++) {
1273 sfree(term
->pre_bidi_cache
[i
]);
1274 sfree(term
->post_bidi_cache
[i
]);
1276 sfree(term
->pre_bidi_cache
);
1277 sfree(term
->post_bidi_cache
);
1283 * Set up the terminal for a given size.
1285 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
1288 termline
**newdisp
, *line
;
1289 int i
, j
, oldrows
= term
->rows
;
1291 int save_alt_which
= term
->alt_which
;
1293 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
1294 newsavelines
== term
->savelines
)
1295 return; /* nothing to do */
1298 swap_screen(term
, 0, FALSE
, FALSE
);
1300 term
->alt_t
= term
->marg_t
= 0;
1301 term
->alt_b
= term
->marg_b
= newrows
- 1;
1303 if (term
->rows
== -1) {
1304 term
->scrollback
= newtree234(NULL
);
1305 term
->screen
= newtree234(NULL
);
1306 term
->tempsblines
= 0;
1311 * Resize the screen and scrollback. We only need to shift
1312 * lines around within our data structures, because lineptr()
1313 * will take care of resizing each individual line if
1316 * - If the new screen is longer, we shunt lines in from temporary
1317 * scrollback if possible, otherwise we add new blank lines at
1320 * - If the new screen is shorter, we remove any blank lines at
1321 * the bottom if possible, otherwise shunt lines above the cursor
1322 * to scrollback if possible, otherwise delete lines below the
1325 * - Then, if the new scrollback length is less than the
1326 * amount of scrollback we actually have, we must throw some
1329 sblen
= count234(term
->scrollback
);
1330 /* Do this loop to expand the screen if newrows > rows */
1331 assert(term
->rows
== count234(term
->screen
));
1332 while (term
->rows
< newrows
) {
1333 if (term
->tempsblines
> 0) {
1334 unsigned char *cline
;
1335 /* Insert a line from the scrollback at the top of the screen. */
1336 assert(sblen
>= term
->tempsblines
);
1337 cline
= delpos234(term
->scrollback
, --sblen
);
1338 line
= decompressline(cline
, NULL
);
1340 line
->temporary
= FALSE
; /* reconstituted line is now real */
1341 term
->tempsblines
-= 1;
1342 addpos234(term
->screen
, line
, 0);
1344 term
->savecurs
.y
+= 1;
1346 /* Add a new blank line at the bottom of the screen. */
1347 line
= newline(term
, newcols
, FALSE
);
1348 addpos234(term
->screen
, line
, count234(term
->screen
));
1352 /* Do this loop to shrink the screen if newrows < rows */
1353 while (term
->rows
> newrows
) {
1354 if (term
->curs
.y
< term
->rows
- 1) {
1355 /* delete bottom row, unless it contains the cursor */
1356 sfree(delpos234(term
->screen
, term
->rows
- 1));
1358 /* push top row to scrollback */
1359 line
= delpos234(term
->screen
, 0);
1360 addpos234(term
->scrollback
, compressline(line
), sblen
++);
1362 term
->tempsblines
+= 1;
1364 term
->savecurs
.y
-= 1;
1368 assert(term
->rows
== newrows
);
1369 assert(count234(term
->screen
) == newrows
);
1371 /* Delete any excess lines from the scrollback. */
1372 while (sblen
> newsavelines
) {
1373 line
= delpos234(term
->scrollback
, 0);
1377 if (sblen
< term
->tempsblines
)
1378 term
->tempsblines
= sblen
;
1379 assert(count234(term
->scrollback
) <= newsavelines
);
1380 assert(count234(term
->scrollback
) >= term
->tempsblines
);
1383 /* Make a new displayed text buffer. */
1384 newdisp
= snewn(newrows
, termline
*);
1385 for (i
= 0; i
< newrows
; i
++) {
1386 newdisp
[i
] = newline(term
, newcols
, FALSE
);
1387 for (j
= 0; j
< newcols
; j
++)
1388 newdisp
[i
]->chars
[i
].attr
= ATTR_INVALID
;
1390 if (term
->disptext
) {
1391 for (i
= 0; i
< oldrows
; i
++)
1392 freeline(term
->disptext
[i
]);
1394 sfree(term
->disptext
);
1395 term
->disptext
= newdisp
;
1396 term
->dispcursx
= term
->dispcursy
= -1;
1398 /* Make a new alternate screen. */
1399 newalt
= newtree234(NULL
);
1400 for (i
= 0; i
< newrows
; i
++) {
1401 line
= newline(term
, newcols
, TRUE
);
1402 addpos234(newalt
, line
, i
);
1404 if (term
->alt_screen
) {
1405 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
1407 freetree234(term
->alt_screen
);
1409 term
->alt_screen
= newalt
;
1410 term
->alt_sblines
= 0;
1412 term
->tabs
= sresize(term
->tabs
, newcols
, unsigned char);
1415 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
1416 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
1419 /* Check that the cursor positions are still valid. */
1420 if (term
->savecurs
.y
< 0)
1421 term
->savecurs
.y
= 0;
1422 if (term
->savecurs
.y
>= newrows
)
1423 term
->savecurs
.y
= newrows
- 1;
1424 if (term
->curs
.y
< 0)
1426 if (term
->curs
.y
>= newrows
)
1427 term
->curs
.y
= newrows
- 1;
1428 if (term
->curs
.x
>= newcols
)
1429 term
->curs
.x
= newcols
- 1;
1430 term
->alt_x
= term
->alt_y
= 0;
1431 term
->wrapnext
= term
->alt_wnext
= FALSE
;
1433 term
->rows
= newrows
;
1434 term
->cols
= newcols
;
1435 term
->savelines
= newsavelines
;
1437 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
1441 if (term
->resize_fn
)
1442 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
1446 * Hand a function and context pointer to the terminal which it can
1447 * use to notify a back end of resizes.
1449 void term_provide_resize_fn(Terminal
*term
,
1450 void (*resize_fn
)(void *, int, int),
1453 term
->resize_fn
= resize_fn
;
1454 term
->resize_ctx
= resize_ctx
;
1455 if (term
->cols
> 0 && term
->rows
> 0)
1456 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
1459 /* Find the bottom line on the screen that has any content.
1460 * If only the top line has content, returns 0.
1461 * If no lines have content, return -1.
1463 static int find_last_nonempty_line(Terminal
* term
, tree234
* screen
)
1466 for (i
= count234(screen
) - 1; i
>= 0; i
--) {
1467 termline
*line
= index234(screen
, i
);
1469 for (j
= 0; j
< line
->cols
; j
++)
1470 if (!termchars_equal(&line
->chars
[j
], &term
->erase_char
))
1472 if (j
!= line
->cols
) break;
1478 * Swap screens. If `reset' is TRUE and we have been asked to
1479 * switch to the alternate screen, we must bring most of its
1480 * configuration from the main screen and erase the contents of the
1481 * alternate screen completely. (This is even true if we're already
1482 * on it! Blame xterm.)
1484 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
1490 reset
= FALSE
; /* do no weird resetting if which==0 */
1492 if (which
!= term
->alt_which
) {
1493 term
->alt_which
= which
;
1495 ttr
= term
->alt_screen
;
1496 term
->alt_screen
= term
->screen
;
1498 term
->alt_sblines
= find_last_nonempty_line(term
, term
->alt_screen
) + 1;
1500 if (!reset
&& !keep_cur_pos
)
1501 term
->curs
.x
= term
->alt_x
;
1504 if (!reset
&& !keep_cur_pos
)
1505 term
->curs
.y
= term
->alt_y
;
1508 if (!reset
) term
->marg_t
= term
->alt_t
;
1511 if (!reset
) term
->marg_b
= term
->alt_b
;
1514 if (!reset
) term
->dec_om
= term
->alt_om
;
1517 if (!reset
) term
->wrap
= term
->alt_wrap
;
1520 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
1521 term
->alt_wnext
= t
;
1523 if (!reset
) term
->insert
= term
->alt_ins
;
1526 if (!reset
) term
->cset
= term
->alt_cset
;
1529 if (!reset
) term
->utf
= term
->alt_utf
;
1532 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
1533 term
->alt_sco_acs
= t
;
1536 if (reset
&& term
->screen
) {
1538 * Yes, this _is_ supposed to honour background-colour-erase.
1540 erase_lots(term
, FALSE
, TRUE
, TRUE
);
1545 * Update the scroll bar.
1547 static void update_sbar(Terminal
*term
)
1549 int nscroll
= sblines(term
);
1550 set_sbar(term
->frontend
, nscroll
+ term
->rows
,
1551 nscroll
+ term
->disptop
, term
->rows
);
1555 * Check whether the region bounded by the two pointers intersects
1556 * the scroll region, and de-select the on-screen selection if so.
1558 static void check_selection(Terminal
*term
, pos from
, pos to
)
1560 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
1565 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
1566 * for backward.) `sb' is TRUE if the scrolling is permitted to
1567 * affect the scrollback buffer.
1569 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
1572 int i
, seltop
, olddisptop
, shift
;
1574 if (topline
!= 0 || term
->alt_which
!= 0)
1577 olddisptop
= term
->disptop
;
1581 line
= delpos234(term
->screen
, botline
);
1582 resizeline(term
, line
, term
->cols
);
1583 for (i
= 0; i
< term
->cols
; i
++)
1584 copy_termchar(line
, i
, &term
->erase_char
);
1585 line
->lattr
= LATTR_NORM
;
1586 addpos234(term
->screen
, line
, topline
);
1588 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
1590 if (term
->selstart
.y
> botline
) {
1591 term
->selstart
.y
= botline
+ 1;
1592 term
->selstart
.x
= 0;
1595 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
1597 if (term
->selend
.y
> botline
) {
1598 term
->selend
.y
= botline
+ 1;
1607 line
= delpos234(term
->screen
, topline
);
1609 if (sb
&& term
->savelines
> 0) {
1610 int sblen
= count234(term
->scrollback
);
1612 * We must add this line to the scrollback. We'll
1613 * remove a line from the top of the scrollback if
1614 * the scrollback is full.
1616 if (sblen
== term
->savelines
) {
1617 unsigned char *cline
;
1620 cline
= delpos234(term
->scrollback
, 0);
1623 term
->tempsblines
+= 1;
1625 addpos234(term
->scrollback
, compressline(line
), sblen
);
1627 line
= newline(term
, term
->cols
, TRUE
);
1630 * If the user is currently looking at part of the
1631 * scrollback, and they haven't enabled any options
1632 * that are going to reset the scrollback as a
1633 * result of this movement, then the chances are
1634 * they'd like to keep looking at the same line. So
1635 * we move their viewpoint at the same rate as the
1636 * scroll, at least until their viewpoint hits the
1637 * top end of the scrollback buffer, at which point
1638 * we don't have the choice any more.
1640 * Thanks to Jan Holmen Holsten for the idea and
1641 * initial implementation.
1643 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
1646 resizeline(term
, line
, term
->cols
);
1647 for (i
= 0; i
< term
->cols
; i
++)
1648 copy_termchar(line
, i
, &term
->erase_char
);
1649 line
->lattr
= LATTR_NORM
;
1650 addpos234(term
->screen
, line
, botline
);
1653 * If the selection endpoints move into the scrollback,
1654 * we keep them moving until they hit the top. However,
1655 * of course, if the line _hasn't_ moved into the
1656 * scrollback then we don't do this, and cut them off
1657 * at the top of the scroll region.
1659 * This applies to selstart and selend (for an existing
1660 * selection), and also selanchor (for one being
1661 * selected as we speak).
1663 seltop
= sb ?
-term
->savelines
: topline
;
1665 if (term
->selstate
!= NO_SELECTION
) {
1666 if (term
->selstart
.y
>= seltop
&&
1667 term
->selstart
.y
<= botline
) {
1669 if (term
->selstart
.y
< seltop
) {
1670 term
->selstart
.y
= seltop
;
1671 term
->selstart
.x
= 0;
1674 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
1676 if (term
->selend
.y
< seltop
) {
1677 term
->selend
.y
= seltop
;
1681 if (term
->selanchor
.y
>= seltop
&&
1682 term
->selanchor
.y
<= botline
) {
1683 term
->selanchor
.y
--;
1684 if (term
->selanchor
.y
< seltop
) {
1685 term
->selanchor
.y
= seltop
;
1686 term
->selanchor
.x
= 0;
1694 #ifdef OPTIMISE_SCROLL
1695 shift
+= term
->disptop
- olddisptop
;
1696 if (shift
< term
->rows
&& shift
> -term
->rows
&& shift
!= 0)
1697 scroll_display(term
, topline
, botline
, shift
);
1698 #endif /* OPTIMISE_SCROLL */
1701 #ifdef OPTIMISE_SCROLL
1703 * Add a scroll of a region on the screen into the pending scroll list.
1704 * `lines' is +ve for scrolling forward, -ve for backward.
1706 * If the scroll is on the same area as the last scroll in the list,
1709 static void save_scroll(Terminal
*term
, int topline
, int botline
, int lines
)
1711 struct scrollregion
*newscroll
;
1712 if (term
->scrolltail
&&
1713 term
->scrolltail
->topline
== topline
&&
1714 term
->scrolltail
->botline
== botline
) {
1715 term
->scrolltail
->lines
+= lines
;
1717 newscroll
= snew(struct scrollregion
);
1718 newscroll
->topline
= topline
;
1719 newscroll
->botline
= botline
;
1720 newscroll
->lines
= lines
;
1721 newscroll
->next
= NULL
;
1723 if (!term
->scrollhead
)
1724 term
->scrollhead
= newscroll
;
1726 term
->scrolltail
->next
= newscroll
;
1727 term
->scrolltail
= newscroll
;
1732 * Scroll the physical display, and our conception of it in disptext.
1734 static void scroll_display(Terminal
*term
, int topline
, int botline
, int lines
)
1736 int distance
, nlines
, i
, j
;
1738 distance
= lines
> 0 ? lines
: -lines
;
1739 nlines
= botline
- topline
+ 1 - distance
;
1741 for (i
= 0; i
< nlines
; i
++)
1742 for (j
= 0; j
< term
->cols
; j
++)
1743 copy_termchar(term
->disptext
[start
+i
], j
,
1744 term
->disptext
[start
+i
+distance
]->chars
+j
);
1745 if (term
->dispcursy
>= 0 &&
1746 term
->dispcursy
>= topline
+ distance
&&
1747 term
->dispcursy
< topline
+ distance
+ nlines
)
1748 term
->dispcursy
-= distance
;
1749 for (i
= 0; i
< distance
; i
++)
1750 for (j
= 0; j
< term
->cols
; j
++)
1751 term
->disptext
[start
+nlines
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1753 for (i
= nlines
; i
-- ;)
1754 for (j
= 0; j
< term
->cols
; j
++)
1755 copy_termchar(term
->disptext
[start
+i
+distance
], j
,
1756 term
->disptext
[start
+i
]->chars
+j
);
1757 if (term
->dispcursy
>= 0 &&
1758 term
->dispcursy
>= topline
&&
1759 term
->dispcursy
< topline
+ nlines
)
1760 term
->dispcursy
+= distance
;
1761 for (i
= 0; i
< distance
; i
++)
1762 for (j
= 0; j
< term
->cols
; j
++)
1763 term
->disptext
[start
+i
]->chars
[j
].attr
|= ATTR_INVALID
;
1765 save_scroll(term
, topline
, botline
, lines
);
1767 #endif /* OPTIMISE_SCROLL */
1770 * Move the cursor to a given position, clipping at boundaries. We
1771 * may or may not want to clip at the scroll margin: marg_clip is 0
1772 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
1773 * even _being_ outside the margins.
1775 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
1779 if (x
>= term
->cols
)
1782 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
1785 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
1791 if (y
>= term
->rows
)
1795 term
->wrapnext
= FALSE
;
1799 * Save or restore the cursor and SGR mode.
1801 static void save_cursor(Terminal
*term
, int save
)
1804 term
->savecurs
= term
->curs
;
1805 term
->save_attr
= term
->curr_attr
;
1806 term
->save_cset
= term
->cset
;
1807 term
->save_utf
= term
->utf
;
1808 term
->save_wnext
= term
->wrapnext
;
1809 term
->save_csattr
= term
->cset_attr
[term
->cset
];
1810 term
->save_sco_acs
= term
->sco_acs
;
1812 term
->curs
= term
->savecurs
;
1813 /* Make sure the window hasn't shrunk since the save */
1814 if (term
->curs
.x
>= term
->cols
)
1815 term
->curs
.x
= term
->cols
- 1;
1816 if (term
->curs
.y
>= term
->rows
)
1817 term
->curs
.y
= term
->rows
- 1;
1819 term
->curr_attr
= term
->save_attr
;
1820 term
->cset
= term
->save_cset
;
1821 term
->utf
= term
->save_utf
;
1822 term
->wrapnext
= term
->save_wnext
;
1824 * wrapnext might reset to False if the x position is no
1825 * longer at the rightmost edge.
1827 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
1828 term
->wrapnext
= FALSE
;
1829 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
1830 term
->sco_acs
= term
->save_sco_acs
;
1831 set_erase_char(term
);
1836 * This function is called before doing _anything_ which affects
1837 * only part of a line of text. It is used to mark the boundary
1838 * between two character positions, and it indicates that some sort
1839 * of effect is going to happen on only one side of that boundary.
1841 * The effect of this function is to check whether a CJK
1842 * double-width character is straddling the boundary, and to remove
1843 * it and replace it with two spaces if so. (Of course, one or
1844 * other of those spaces is then likely to be replaced with
1845 * something else again, as a result of whatever happens next.)
1847 * Also, if the boundary is at the right-hand _edge_ of the screen,
1848 * it implies something deliberate is being done to the rightmost
1849 * column position; hence we must clear LATTR_WRAPPED2.
1851 * The input to the function is the coordinates of the _second_
1852 * character of the pair.
1854 static void check_boundary(Terminal
*term
, int x
, int y
)
1858 /* Validate input coordinates, just in case. */
1859 if (x
== 0 || x
> term
->cols
)
1862 ldata
= scrlineptr(y
);
1863 if (x
== term
->cols
) {
1864 ldata
->lattr
&= ~LATTR_WRAPPED2
;
1866 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
1867 clear_cc(ldata
, x
-1);
1869 ldata
->chars
[x
-1].chr
= ' ' | CSET_ASCII
;
1870 ldata
->chars
[x
] = ldata
->chars
[x
-1];
1876 * Erase a large portion of the screen: the whole screen, or the
1877 * whole line, or parts thereof.
1879 static void erase_lots(Terminal
*term
,
1880 int line_only
, int from_begin
, int to_end
)
1884 int erasing_lines_from_top
= 0;
1887 start
.y
= term
->curs
.y
;
1889 end
.y
= term
->curs
.y
+ 1;
1891 erase_lattr
= FALSE
;
1906 if (!from_begin
|| !to_end
)
1907 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1908 check_selection(term
, start
, end
);
1910 /* Clear screen also forces a full window redraw, just in case. */
1911 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
1912 term_invalidate(term
);
1914 /* Lines scrolled away shouldn't be brought back on if the terminal
1916 if (start
.y
== 0 && start
.x
== 0 && end
.x
== 0 && erase_lattr
)
1917 erasing_lines_from_top
= 1;
1919 if (term
->cfg
.erase_to_scrollback
&& erasing_lines_from_top
) {
1920 /* If it's a whole number of lines, starting at the top, and
1921 * we're fully erasing them, erase by scrolling and keep the
1922 * lines in the scrollback. */
1923 int scrolllines
= end
.y
;
1924 if (end
.y
== term
->rows
) {
1925 /* Shrink until we find a non-empty row.*/
1926 scrolllines
= find_last_nonempty_line(term
, term
->screen
) + 1;
1928 if (scrolllines
> 0)
1929 scroll(term
, 0, scrolllines
- 1, scrolllines
, TRUE
);
1931 termline
*ldata
= scrlineptr(start
.y
);
1932 while (poslt(start
, end
)) {
1933 if (start
.x
== term
->cols
) {
1935 ldata
->lattr
&= ~(LATTR_WRAPPED
| LATTR_WRAPPED2
);
1937 ldata
->lattr
= LATTR_NORM
;
1939 copy_termchar(ldata
, start
.x
, &term
->erase_char
);
1941 if (incpos(start
) && start
.y
< term
->rows
) {
1942 ldata
= scrlineptr(start
.y
);
1947 /* After an erase of lines from the top of the screen, we shouldn't
1948 * bring the lines back again if the terminal enlarges (since the user or
1949 * application has explictly thrown them away). */
1950 if (erasing_lines_from_top
&& !(term
->alt_which
))
1951 term
->tempsblines
= 0;
1955 * Insert or delete characters within the current line. n is +ve if
1956 * insertion is desired, and -ve for deletion.
1958 static void insch(Terminal
*term
, int n
)
1960 int dir
= (n
< 0 ?
-1 : +1);
1965 n
= (n
< 0 ?
-n
: n
);
1966 if (n
> term
->cols
- term
->curs
.x
)
1967 n
= term
->cols
- term
->curs
.x
;
1968 m
= term
->cols
- term
->curs
.x
- n
;
1969 cursplus
.y
= term
->curs
.y
;
1970 cursplus
.x
= term
->curs
.x
+ n
;
1971 check_selection(term
, term
->curs
, cursplus
);
1972 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
1974 check_boundary(term
, term
->curs
.x
+ n
, term
->curs
.y
);
1975 ldata
= scrlineptr(term
->curs
.y
);
1977 for (j
= 0; j
< m
; j
++)
1978 move_termchar(ldata
,
1979 ldata
->chars
+ term
->curs
.x
+ j
,
1980 ldata
->chars
+ term
->curs
.x
+ j
+ n
);
1982 copy_termchar(ldata
, term
->curs
.x
+ m
++, &term
->erase_char
);
1985 move_termchar(ldata
,
1986 ldata
->chars
+ term
->curs
.x
+ j
+ n
,
1987 ldata
->chars
+ term
->curs
.x
+ j
);
1989 copy_termchar(ldata
, term
->curs
.x
+ n
, &term
->erase_char
);
1994 * Toggle terminal mode `mode' to state `state'. (`query' indicates
1995 * whether the mode is a DEC private one or a normal one.)
1997 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
1999 unsigned long ticks
;
2003 case 1: /* DECCKM: application cursor keys */
2004 term
->app_cursor_keys
= state
;
2006 case 2: /* DECANM: VT52 mode */
2007 term
->vt52_mode
= !state
;
2008 if (term
->vt52_mode
) {
2009 term
->blink_is_real
= FALSE
;
2010 term
->vt52_bold
= FALSE
;
2012 term
->blink_is_real
= term
->cfg
.blinktext
;
2015 case 3: /* DECCOLM: 80/132 columns */
2017 if (!term
->cfg
.no_remote_resize
)
2018 request_resize(term
->frontend
, state ?
132 : 80, term
->rows
);
2019 term
->reset_132
= state
;
2020 term
->alt_t
= term
->marg_t
= 0;
2021 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
2022 move(term
, 0, 0, 0);
2023 erase_lots(term
, FALSE
, TRUE
, TRUE
);
2025 case 5: /* DECSCNM: reverse video */
2027 * Toggle reverse video. If we receive an OFF within the
2028 * visual bell timeout period after an ON, we trigger an
2029 * effective visual bell, so that ESC[?5hESC[?5l will
2030 * always be an actually _visible_ visual bell.
2032 ticks
= GETTICKCOUNT();
2033 /* turn off a previous vbell to avoid inconsistencies */
2034 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2035 term
->in_vbell
= FALSE
;
2036 if (term
->rvideo
&& !state
&& /* we're turning it off... */
2037 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
2038 /* If there's no vbell timeout already, or this one lasts
2039 * longer, replace vbell_timeout with ours. */
2040 if (!term
->in_vbell
||
2041 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
2043 term
->vbell_startpoint
= term
->rvbell_startpoint
;
2044 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
2045 } else if (!term
->rvideo
&& state
) {
2046 /* This is an ON, so we notice the time and save it. */
2047 term
->rvbell_startpoint
= ticks
;
2049 term
->rvideo
= state
;
2050 term
->seen_disp_event
= TRUE
;
2054 case 6: /* DECOM: DEC origin mode */
2055 term
->dec_om
= state
;
2057 case 7: /* DECAWM: auto wrap */
2060 case 8: /* DECARM: auto key repeat */
2061 term
->repeat_off
= !state
;
2063 case 10: /* DECEDM: set local edit mode */
2064 term
->term_editing
= state
;
2065 if (term
->ldisc
) /* cause ldisc to notice changes */
2066 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2068 case 25: /* DECTCEM: enable/disable cursor */
2069 compatibility2(OTHER
, VT220
);
2070 term
->cursor_on
= state
;
2071 term
->seen_disp_event
= TRUE
;
2073 case 47: /* alternate screen */
2074 compatibility(OTHER
);
2076 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
2079 case 1000: /* xterm mouse 1 */
2080 term
->xterm_mouse
= state ?
1 : 0;
2081 set_raw_mouse_mode(term
->frontend
, state
);
2083 case 1002: /* xterm mouse 2 */
2084 term
->xterm_mouse
= state ?
2 : 0;
2085 set_raw_mouse_mode(term
->frontend
, state
);
2087 case 1047: /* alternate screen */
2088 compatibility(OTHER
);
2090 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
2093 case 1048: /* save/restore cursor */
2094 if (!term
->cfg
.no_alt_screen
)
2095 save_cursor(term
, state
);
2096 if (!state
) term
->seen_disp_event
= TRUE
;
2098 case 1049: /* cursor & alternate screen */
2099 if (state
&& !term
->cfg
.no_alt_screen
)
2100 save_cursor(term
, state
);
2101 if (!state
) term
->seen_disp_event
= TRUE
;
2102 compatibility(OTHER
);
2104 swap_screen(term
, term
->cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
2105 if (!state
&& !term
->cfg
.no_alt_screen
)
2106 save_cursor(term
, state
);
2111 case 4: /* IRM: set insert mode */
2112 compatibility(VT102
);
2113 term
->insert
= state
;
2115 case 12: /* SRM: set echo mode */
2116 term
->term_echoing
= !state
;
2117 if (term
->ldisc
) /* cause ldisc to notice changes */
2118 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2120 case 20: /* LNM: Return sends ... */
2121 term
->cr_lf_return
= state
;
2123 case 34: /* WYULCURM: Make cursor BIG */
2124 compatibility2(OTHER
, VT220
);
2125 term
->big_cursor
= !state
;
2130 * Process an OSC sequence: set window title or icon name.
2132 static void do_osc(Terminal
*term
)
2135 while (term
->osc_strlen
--)
2136 term
->wordness
[(unsigned char)
2137 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
2139 term
->osc_string
[term
->osc_strlen
] = '\0';
2140 switch (term
->esc_args
[0]) {
2143 if (!term
->cfg
.no_remote_wintitle
)
2144 set_icon(term
->frontend
, term
->osc_string
);
2145 if (term
->esc_args
[0] == 1)
2147 /* fall through: parameter 0 means set both */
2150 if (!term
->cfg
.no_remote_wintitle
)
2151 set_title(term
->frontend
, term
->osc_string
);
2158 * ANSI printing routines.
2160 static void term_print_setup(Terminal
*term
)
2162 bufchain_clear(&term
->printer_buf
);
2163 term
->print_job
= printer_start_job(term
->cfg
.printer
);
2165 static void term_print_flush(Terminal
*term
)
2170 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
2171 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2174 printer_job_data(term
->print_job
, data
, len
);
2175 bufchain_consume(&term
->printer_buf
, len
);
2178 static void term_print_finish(Terminal
*term
)
2184 if (!term
->printing
&& !term
->only_printing
)
2185 return; /* we need do nothing */
2187 term_print_flush(term
);
2188 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
2189 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
2191 if (c
== '\033' || c
== '\233') {
2192 bufchain_consume(&term
->printer_buf
, size
);
2195 printer_job_data(term
->print_job
, &c
, 1);
2196 bufchain_consume(&term
->printer_buf
, 1);
2199 printer_finish_job(term
->print_job
);
2200 term
->print_job
= NULL
;
2201 term
->printing
= term
->only_printing
= FALSE
;
2205 * Remove everything currently in `inbuf' and stick it up on the
2206 * in-memory display. There's a big state machine in here to
2207 * process escape sequences...
2209 void term_out(Terminal
*term
)
2213 unsigned char localbuf
[256], *chars
;
2218 chars
= NULL
; /* placate compiler warnings */
2219 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
2223 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
2224 if (nchars
> sizeof(localbuf
))
2225 nchars
= sizeof(localbuf
);
2226 memcpy(localbuf
, ret
, nchars
);
2227 bufchain_consume(&term
->inbuf
, nchars
);
2229 assert(chars
!= NULL
);
2235 * Optionally log the session traffic to a file. Useful for
2236 * debugging and possibly also useful for actual logging.
2238 if (term
->cfg
.logtype
== LGTYP_DEBUG
&& term
->logctx
)
2239 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_DEBUG
);
2245 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
2246 * be able to display 8-bit characters, but I'll let that go 'cause
2251 * If we're printing, add the character to the printer
2254 if (term
->printing
) {
2255 bufchain_add(&term
->printer_buf
, &c
, 1);
2258 * If we're in print-only mode, we use a much simpler
2259 * state machine designed only to recognise the ESC[4i
2260 * termination sequence.
2262 if (term
->only_printing
) {
2264 term
->print_state
= 1;
2265 else if (c
== (unsigned char)'\233')
2266 term
->print_state
= 2;
2267 else if (c
== '[' && term
->print_state
== 1)
2268 term
->print_state
= 2;
2269 else if (c
== '4' && term
->print_state
== 2)
2270 term
->print_state
= 3;
2271 else if (c
== 'i' && term
->print_state
== 3)
2272 term
->print_state
= 4;
2274 term
->print_state
= 0;
2275 if (term
->print_state
== 4) {
2276 term_print_finish(term
);
2282 /* First see about all those translations. */
2283 if (term
->termstate
== TOPLEVEL
) {
2285 switch (term
->utf_state
) {
2288 /* UTF-8 must be stateless so we ignore iso2022. */
2289 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2290 c
= term
->ucsdata
->unitab_ctrl
[c
];
2291 else c
= ((unsigned char)c
) | CSET_ASCII
;
2293 } else if ((c
& 0xe0) == 0xc0) {
2294 term
->utf_size
= term
->utf_state
= 1;
2295 term
->utf_char
= (c
& 0x1f);
2296 } else if ((c
& 0xf0) == 0xe0) {
2297 term
->utf_size
= term
->utf_state
= 2;
2298 term
->utf_char
= (c
& 0x0f);
2299 } else if ((c
& 0xf8) == 0xf0) {
2300 term
->utf_size
= term
->utf_state
= 3;
2301 term
->utf_char
= (c
& 0x07);
2302 } else if ((c
& 0xfc) == 0xf8) {
2303 term
->utf_size
= term
->utf_state
= 4;
2304 term
->utf_char
= (c
& 0x03);
2305 } else if ((c
& 0xfe) == 0xfc) {
2306 term
->utf_size
= term
->utf_state
= 5;
2307 term
->utf_char
= (c
& 0x01);
2318 if ((c
& 0xC0) != 0x80) {
2321 term
->utf_state
= 0;
2324 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
2325 if (--term
->utf_state
)
2330 /* Is somebody trying to be evil! */
2332 (c
< 0x800 && term
->utf_size
>= 2) ||
2333 (c
< 0x10000 && term
->utf_size
>= 3) ||
2334 (c
< 0x200000 && term
->utf_size
>= 4) ||
2335 (c
< 0x4000000 && term
->utf_size
>= 5))
2338 /* Unicode line separator and paragraph separator are CR-LF */
2339 if (c
== 0x2028 || c
== 0x2029)
2342 /* High controls are probably a Baaad idea too. */
2346 /* The UTF-16 surrogates are not nice either. */
2347 /* The standard give the option of decoding these:
2348 * I don't want to! */
2349 if (c
>= 0xD800 && c
< 0xE000)
2352 /* ISO 10646 characters now limited to UTF-16 range. */
2356 /* This is currently a TagPhobic application.. */
2357 if (c
>= 0xE0000 && c
<= 0xE007F)
2360 /* U+FEFF is best seen as a null. */
2363 /* But U+FFFE is an error. */
2364 if (c
== 0xFFFE || c
== 0xFFFF)
2369 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
2370 else if(term
->sco_acs
&&
2371 (c
!='\033' && c
!='\012' && c
!='\015' && c
!='\b'))
2373 if (term
->sco_acs
== 2) c
|= 0x80;
2376 switch (term
->cset_attr
[term
->cset
]) {
2378 * Linedraw characters are different from 'ESC ( B'
2379 * only for a small range. For ones outside that
2380 * range, make sure we use the same font as well as
2381 * the same encoding.
2384 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2385 c
= term
->ucsdata
->unitab_ctrl
[c
];
2387 c
= ((unsigned char) c
) | CSET_LINEDRW
;
2391 /* If UK-ASCII, make the '#' a LineDraw Pound */
2393 c
= '}' | CSET_LINEDRW
;
2396 /*FALLTHROUGH*/ case CSET_ASCII
:
2397 if (term
->ucsdata
->unitab_ctrl
[c
] != 0xFF)
2398 c
= term
->ucsdata
->unitab_ctrl
[c
];
2400 c
= ((unsigned char) c
) | CSET_ASCII
;
2403 if (c
>=' ') c
= ((unsigned char)c
) | CSET_SCOACS
;
2409 /* How about C1 controls ? */
2410 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
2411 !term
->vt52_mode
&& has_compat(VT220
)) {
2412 term
->termstate
= SEEN_ESC
;
2413 term
->esc_query
= FALSE
;
2414 c
= '@' + (c
& 0x1F);
2417 /* Or the GL control. */
2418 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
2419 if (term
->curs
.x
&& !term
->wrapnext
)
2421 term
->wrapnext
= FALSE
;
2422 /* destructive backspace might be disabled */
2423 if (!term
->cfg
.no_dbackspace
) {
2424 copy_termchar(scrlineptr(term
->curs
.y
),
2425 term
->curs
.x
, &term
->erase_char
);
2428 /* Or normal C0 controls. */
2429 if ((c
& ~0x1F) == 0 && term
->termstate
< DO_CTRLS
) {
2431 case '\005': /* ENQ: terminal type query */
2432 /* Strictly speaking this is VT100 but a VT100 defaults to
2433 * no response. Other terminals respond at their option.
2435 * Don't put a CR in the default string as this tends to
2436 * upset some weird software.
2438 * An xterm returns "xterm" (5 characters)
2440 compatibility(ANSIMIN
);
2442 char abuf
[256], *s
, *d
;
2444 for (s
= term
->cfg
.answerback
, d
= abuf
; *s
; s
++) {
2446 if (*s
>= 'a' && *s
<= 'z')
2447 *d
++ = (*s
- ('a' - 1));
2448 else if ((*s
>= '@' && *s
<= '_') ||
2449 *s
== '?' || (*s
& 0x80))
2454 } else if (*s
== '^') {
2459 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
2463 case '\007': /* BEL: Bell */
2465 struct beeptime
*newbeep
;
2466 unsigned long ticks
;
2468 ticks
= GETTICKCOUNT();
2470 if (!term
->beep_overloaded
) {
2471 newbeep
= snew(struct beeptime
);
2472 newbeep
->ticks
= ticks
;
2473 newbeep
->next
= NULL
;
2474 if (!term
->beephead
)
2475 term
->beephead
= newbeep
;
2477 term
->beeptail
->next
= newbeep
;
2478 term
->beeptail
= newbeep
;
2483 * Throw out any beeps that happened more than
2486 while (term
->beephead
&&
2487 term
->beephead
->ticks
< ticks
- term
->cfg
.bellovl_t
) {
2488 struct beeptime
*tmp
= term
->beephead
;
2489 term
->beephead
= tmp
->next
;
2491 if (!term
->beephead
)
2492 term
->beeptail
= NULL
;
2496 if (term
->cfg
.bellovl
&& term
->beep_overloaded
&&
2497 ticks
- term
->lastbeep
>= (unsigned)term
->cfg
.bellovl_s
) {
2499 * If we're currently overloaded and the
2500 * last beep was more than s seconds ago,
2501 * leave overload mode.
2503 term
->beep_overloaded
= FALSE
;
2504 } else if (term
->cfg
.bellovl
&& !term
->beep_overloaded
&&
2505 term
->nbeeps
>= term
->cfg
.bellovl_n
) {
2507 * Now, if we have n or more beeps
2508 * remaining in the queue, go into overload
2511 term
->beep_overloaded
= TRUE
;
2513 term
->lastbeep
= ticks
;
2516 * Perform an actual beep if we're not overloaded.
2518 if (!term
->cfg
.bellovl
|| !term
->beep_overloaded
) {
2519 beep(term
->frontend
, term
->cfg
.beep
);
2520 if (term
->cfg
.beep
== BELL_VISUAL
) {
2521 term
->in_vbell
= TRUE
;
2522 term
->vbell_startpoint
= ticks
;
2526 term
->seen_disp_event
= TRUE
;
2529 case '\b': /* BS: Back space */
2530 if (term
->curs
.x
== 0 &&
2531 (term
->curs
.y
== 0 || term
->wrap
== 0))
2533 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
2534 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
2535 else if (term
->wrapnext
)
2536 term
->wrapnext
= FALSE
;
2539 term
->seen_disp_event
= TRUE
;
2541 case '\016': /* LS1: Locking-shift one */
2542 compatibility(VT100
);
2545 case '\017': /* LS0: Locking-shift zero */
2546 compatibility(VT100
);
2549 case '\033': /* ESC: Escape */
2550 if (term
->vt52_mode
)
2551 term
->termstate
= VT52_ESC
;
2553 compatibility(ANSIMIN
);
2554 term
->termstate
= SEEN_ESC
;
2555 term
->esc_query
= FALSE
;
2558 case '\015': /* CR: Carriage return */
2560 term
->wrapnext
= FALSE
;
2561 term
->seen_disp_event
= TRUE
;
2562 term
->paste_hold
= 0;
2564 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2566 case '\014': /* FF: Form feed */
2567 if (has_compat(SCOANSI
)) {
2568 move(term
, 0, 0, 0);
2569 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2571 term
->wrapnext
= FALSE
;
2572 term
->seen_disp_event
= 1;
2575 case '\013': /* VT: Line tabulation */
2576 compatibility(VT100
);
2577 case '\012': /* LF: Line feed */
2578 if (term
->curs
.y
== term
->marg_b
)
2579 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2580 else if (term
->curs
.y
< term
->rows
- 1)
2582 if (term
->cfg
.lfhascr
)
2584 term
->wrapnext
= FALSE
;
2585 term
->seen_disp_event
= 1;
2586 term
->paste_hold
= 0;
2588 logtraffic(term
->logctx
, (unsigned char) c
, LGTYP_ASCII
);
2590 case '\t': /* HT: Character tabulation */
2592 pos old_curs
= term
->curs
;
2593 termline
*ldata
= scrlineptr(term
->curs
.y
);
2597 } while (term
->curs
.x
< term
->cols
- 1 &&
2598 !term
->tabs
[term
->curs
.x
]);
2600 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
) {
2601 if (term
->curs
.x
>= term
->cols
/ 2)
2602 term
->curs
.x
= term
->cols
/ 2 - 1;
2604 if (term
->curs
.x
>= term
->cols
)
2605 term
->curs
.x
= term
->cols
- 1;
2608 check_selection(term
, old_curs
, term
->curs
);
2610 term
->seen_disp_event
= TRUE
;
2614 switch (term
->termstate
) {
2616 /* Only graphic characters get this far;
2617 * ctrls are stripped above */
2619 termline
*cline
= scrlineptr(term
->curs
.y
);
2624 width
= wcwidth((wchar_t) c
);
2626 if (term
->wrapnext
&& term
->wrap
&& width
> 0) {
2627 cline
->lattr
|= LATTR_WRAPPED
;
2628 if (term
->curs
.y
== term
->marg_b
)
2629 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2630 else if (term
->curs
.y
< term
->rows
- 1)
2633 term
->wrapnext
= FALSE
;
2635 if (term
->insert
&& width
> 0)
2637 if (term
->selstate
!= NO_SELECTION
) {
2638 pos cursplus
= term
->curs
;
2640 check_selection(term
, term
->curs
, cursplus
);
2642 if (((c
& CSET_MASK
) == CSET_ASCII
||
2643 (c
& CSET_MASK
) == 0) &&
2645 logtraffic(term
->logctx
, (unsigned char) c
,
2651 * If we're about to display a double-width
2652 * character starting in the rightmost
2653 * column, then we do something special
2654 * instead. We must print a space in the
2655 * last column of the screen, then wrap;
2656 * and we also set LATTR_WRAPPED2 which
2657 * instructs subsequent cut-and-pasting not
2658 * only to splice this line to the one
2659 * after it, but to ignore the space in the
2660 * last character position as well.
2661 * (Because what was actually output to the
2662 * terminal was presumably just a sequence
2663 * of CJK characters, and we don't want a
2664 * space to be pasted in the middle of
2665 * those just because they had the
2666 * misfortune to start in the wrong parity
2667 * column. xterm concurs.)
2669 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2670 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2671 if (term
->curs
.x
== term
->cols
-1) {
2672 copy_termchar(cline
, term
->curs
.x
,
2674 cline
->lattr
|= LATTR_WRAPPED
| LATTR_WRAPPED2
;
2675 if (term
->curs
.y
== term
->marg_b
)
2676 scroll(term
, term
->marg_t
, term
->marg_b
,
2678 else if (term
->curs
.y
< term
->rows
- 1)
2681 /* Now we must check_boundary again, of course. */
2682 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2683 check_boundary(term
, term
->curs
.x
+2, term
->curs
.y
);
2687 clear_cc(cline
, term
->curs
.x
);
2688 cline
->chars
[term
->curs
.x
].chr
= c
;
2689 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2694 clear_cc(cline
, term
->curs
.x
);
2695 cline
->chars
[term
->curs
.x
].chr
= UCSWIDE
;
2696 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2700 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
2701 check_boundary(term
, term
->curs
.x
+1, term
->curs
.y
);
2704 clear_cc(cline
, term
->curs
.x
);
2705 cline
->chars
[term
->curs
.x
].chr
= c
;
2706 cline
->chars
[term
->curs
.x
].attr
= term
->curr_attr
;
2710 add_cc(cline
, term
->curs
.x
- !term
->wrapnext
, c
);
2716 if (term
->curs
.x
== term
->cols
) {
2718 term
->wrapnext
= TRUE
;
2719 if (term
->wrap
&& term
->vt52_mode
) {
2720 cline
->lattr
|= LATTR_WRAPPED
;
2721 if (term
->curs
.y
== term
->marg_b
)
2722 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2723 else if (term
->curs
.y
< term
->rows
- 1)
2726 term
->wrapnext
= FALSE
;
2729 term
->seen_disp_event
= 1;
2735 * This state is virtually identical to SEEN_ESC, with the
2736 * exception that we have an OSC sequence in the pipeline,
2737 * and _if_ we see a backslash, we process it.
2741 term
->termstate
= TOPLEVEL
;
2744 /* else fall through */
2746 if (c
>= ' ' && c
<= '/') {
2747 if (term
->esc_query
)
2748 term
->esc_query
= -1;
2750 term
->esc_query
= c
;
2753 term
->termstate
= TOPLEVEL
;
2754 switch (ANSI(c
, term
->esc_query
)) {
2755 case '[': /* enter CSI mode */
2756 term
->termstate
= SEEN_CSI
;
2757 term
->esc_nargs
= 1;
2758 term
->esc_args
[0] = ARG_DEFAULT
;
2759 term
->esc_query
= FALSE
;
2761 case ']': /* OSC: xterm escape sequences */
2762 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
2763 compatibility(OTHER
);
2764 term
->termstate
= SEEN_OSC
;
2765 term
->esc_args
[0] = 0;
2767 case '7': /* DECSC: save cursor */
2768 compatibility(VT100
);
2769 save_cursor(term
, TRUE
);
2771 case '8': /* DECRC: restore cursor */
2772 compatibility(VT100
);
2773 save_cursor(term
, FALSE
);
2774 term
->seen_disp_event
= TRUE
;
2776 case '=': /* DECKPAM: Keypad application mode */
2777 compatibility(VT100
);
2778 term
->app_keypad_keys
= TRUE
;
2780 case '>': /* DECKPNM: Keypad numeric mode */
2781 compatibility(VT100
);
2782 term
->app_keypad_keys
= FALSE
;
2784 case 'D': /* IND: exactly equivalent to LF */
2785 compatibility(VT100
);
2786 if (term
->curs
.y
== term
->marg_b
)
2787 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2788 else if (term
->curs
.y
< term
->rows
- 1)
2790 term
->wrapnext
= FALSE
;
2791 term
->seen_disp_event
= TRUE
;
2793 case 'E': /* NEL: exactly equivalent to CR-LF */
2794 compatibility(VT100
);
2796 if (term
->curs
.y
== term
->marg_b
)
2797 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
2798 else if (term
->curs
.y
< term
->rows
- 1)
2800 term
->wrapnext
= FALSE
;
2801 term
->seen_disp_event
= TRUE
;
2803 case 'M': /* RI: reverse index - backwards LF */
2804 compatibility(VT100
);
2805 if (term
->curs
.y
== term
->marg_t
)
2806 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
2807 else if (term
->curs
.y
> 0)
2809 term
->wrapnext
= FALSE
;
2810 term
->seen_disp_event
= TRUE
;
2812 case 'Z': /* DECID: terminal type query */
2813 compatibility(VT100
);
2815 ldisc_send(term
->ldisc
, term
->id_string
,
2816 strlen(term
->id_string
), 0);
2818 case 'c': /* RIS: restore power-on settings */
2819 compatibility(VT100
);
2821 if (term
->ldisc
) /* cause ldisc to notice changes */
2822 ldisc_send(term
->ldisc
, NULL
, 0, 0);
2823 if (term
->reset_132
) {
2824 if (!term
->cfg
.no_remote_resize
)
2825 request_resize(term
->frontend
, 80, term
->rows
);
2826 term
->reset_132
= 0;
2829 term
->seen_disp_event
= TRUE
;
2831 case 'H': /* HTS: set a tab */
2832 compatibility(VT100
);
2833 term
->tabs
[term
->curs
.x
] = TRUE
;
2836 case ANSI('8', '#'): /* DECALN: fills screen with Es :-) */
2837 compatibility(VT100
);
2843 for (i
= 0; i
< term
->rows
; i
++) {
2844 ldata
= scrlineptr(i
);
2845 for (j
= 0; j
< term
->cols
; j
++) {
2846 copy_termchar(ldata
, j
,
2847 &term
->basic_erase_char
);
2848 ldata
->chars
[j
].chr
= 'E';
2850 ldata
->lattr
= LATTR_NORM
;
2853 term
->seen_disp_event
= TRUE
;
2854 scrtop
.x
= scrtop
.y
= 0;
2856 scrbot
.y
= term
->rows
;
2857 check_selection(term
, scrtop
, scrbot
);
2861 case ANSI('3', '#'):
2862 case ANSI('4', '#'):
2863 case ANSI('5', '#'):
2864 case ANSI('6', '#'):
2865 compatibility(VT100
);
2869 switch (ANSI(c
, term
->esc_query
)) {
2870 case ANSI('3', '#'): /* DECDHL: 2*height, top */
2873 case ANSI('4', '#'): /* DECDHL: 2*height, bottom */
2876 case ANSI('5', '#'): /* DECSWL: normal */
2877 nlattr
= LATTR_NORM
;
2879 default: /* case ANSI('6', '#'): DECDWL: 2*width */
2880 nlattr
= LATTR_WIDE
;
2883 scrlineptr(term
->curs
.y
)->lattr
= nlattr
;
2886 /* GZD4: G0 designate 94-set */
2887 case ANSI('A', '('):
2888 compatibility(VT100
);
2889 if (!term
->cfg
.no_remote_charset
)
2890 term
->cset_attr
[0] = CSET_GBCHR
;
2892 case ANSI('B', '('):
2893 compatibility(VT100
);
2894 if (!term
->cfg
.no_remote_charset
)
2895 term
->cset_attr
[0] = CSET_ASCII
;
2897 case ANSI('0', '('):
2898 compatibility(VT100
);
2899 if (!term
->cfg
.no_remote_charset
)
2900 term
->cset_attr
[0] = CSET_LINEDRW
;
2902 case ANSI('U', '('):
2903 compatibility(OTHER
);
2904 if (!term
->cfg
.no_remote_charset
)
2905 term
->cset_attr
[0] = CSET_SCOACS
;
2907 /* G1D4: G1-designate 94-set */
2908 case ANSI('A', ')'):
2909 compatibility(VT100
);
2910 if (!term
->cfg
.no_remote_charset
)
2911 term
->cset_attr
[1] = CSET_GBCHR
;
2913 case ANSI('B', ')'):
2914 compatibility(VT100
);
2915 if (!term
->cfg
.no_remote_charset
)
2916 term
->cset_attr
[1] = CSET_ASCII
;
2918 case ANSI('0', ')'):
2919 compatibility(VT100
);
2920 if (!term
->cfg
.no_remote_charset
)
2921 term
->cset_attr
[1] = CSET_LINEDRW
;
2923 case ANSI('U', ')'):
2924 compatibility(OTHER
);
2925 if (!term
->cfg
.no_remote_charset
)
2926 term
->cset_attr
[1] = CSET_SCOACS
;
2928 /* DOCS: Designate other coding system */
2929 case ANSI('8', '%'): /* Old Linux code */
2930 case ANSI('G', '%'):
2931 compatibility(OTHER
);
2932 if (!term
->cfg
.no_remote_charset
)
2935 case ANSI('@', '%'):
2936 compatibility(OTHER
);
2937 if (!term
->cfg
.no_remote_charset
)
2943 term
->termstate
= TOPLEVEL
; /* default */
2945 if (term
->esc_nargs
<= ARGS_MAX
) {
2946 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
2947 term
->esc_args
[term
->esc_nargs
- 1] = 0;
2948 term
->esc_args
[term
->esc_nargs
- 1] =
2949 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
2951 term
->termstate
= SEEN_CSI
;
2952 } else if (c
== ';') {
2953 if (++term
->esc_nargs
<= ARGS_MAX
)
2954 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
2955 term
->termstate
= SEEN_CSI
;
2956 } else if (c
< '@') {
2957 if (term
->esc_query
)
2958 term
->esc_query
= -1;
2960 term
->esc_query
= TRUE
;
2962 term
->esc_query
= c
;
2963 term
->termstate
= SEEN_CSI
;
2965 switch (ANSI(c
, term
->esc_query
)) {
2966 case 'A': /* CUU: move up N lines */
2967 move(term
, term
->curs
.x
,
2968 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
2969 term
->seen_disp_event
= TRUE
;
2971 case 'e': /* VPR: move down N lines */
2972 compatibility(ANSI
);
2974 case 'B': /* CUD: Cursor down */
2975 move(term
, term
->curs
.x
,
2976 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
2977 term
->seen_disp_event
= TRUE
;
2979 case ANSI('c', '>'): /* DA: report xterm version */
2980 compatibility(OTHER
);
2981 /* this reports xterm version 136 so that VIM can
2982 use the drag messages from the mouse reporting */
2984 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
2986 case 'a': /* HPR: move right N cols */
2987 compatibility(ANSI
);
2989 case 'C': /* CUF: Cursor right */
2990 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
2992 term
->seen_disp_event
= TRUE
;
2994 case 'D': /* CUB: move left N cols */
2995 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
2997 term
->seen_disp_event
= TRUE
;
2999 case 'E': /* CNL: move down N lines and CR */
3000 compatibility(ANSI
);
3002 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
3003 term
->seen_disp_event
= TRUE
;
3005 case 'F': /* CPL: move up N lines and CR */
3006 compatibility(ANSI
);
3008 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
3009 term
->seen_disp_event
= TRUE
;
3012 case '`': /* HPA: set horizontal posn */
3013 compatibility(ANSI
);
3014 move(term
, def(term
->esc_args
[0], 1) - 1,
3016 term
->seen_disp_event
= TRUE
;
3018 case 'd': /* VPA: set vertical posn */
3019 compatibility(ANSI
);
3020 move(term
, term
->curs
.x
,
3021 ((term
->dec_om ? term
->marg_t
: 0) +
3022 def(term
->esc_args
[0], 1) - 1),
3023 (term
->dec_om ?
2 : 0));
3024 term
->seen_disp_event
= TRUE
;
3027 case 'f': /* HVP: set horz and vert posns at once */
3028 if (term
->esc_nargs
< 2)
3029 term
->esc_args
[1] = ARG_DEFAULT
;
3030 move(term
, def(term
->esc_args
[1], 1) - 1,
3031 ((term
->dec_om ? term
->marg_t
: 0) +
3032 def(term
->esc_args
[0], 1) - 1),
3033 (term
->dec_om ?
2 : 0));
3034 term
->seen_disp_event
= TRUE
;
3036 case 'J': /* ED: erase screen or parts of it */
3038 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3041 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
3044 term
->seen_disp_event
= TRUE
;
3046 case 'K': /* EL: erase line or parts of it */
3048 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
3051 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
3053 term
->seen_disp_event
= TRUE
;
3055 case 'L': /* IL: insert lines */
3056 compatibility(VT102
);
3057 if (term
->curs
.y
<= term
->marg_b
)
3058 scroll(term
, term
->curs
.y
, term
->marg_b
,
3059 -def(term
->esc_args
[0], 1), FALSE
);
3060 term
->seen_disp_event
= TRUE
;
3062 case 'M': /* DL: delete lines */
3063 compatibility(VT102
);
3064 if (term
->curs
.y
<= term
->marg_b
)
3065 scroll(term
, term
->curs
.y
, term
->marg_b
,
3066 def(term
->esc_args
[0], 1),
3068 term
->seen_disp_event
= TRUE
;
3070 case '@': /* ICH: insert chars */
3071 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
3072 compatibility(VT102
);
3073 insch(term
, def(term
->esc_args
[0], 1));
3074 term
->seen_disp_event
= TRUE
;
3076 case 'P': /* DCH: delete chars */
3077 compatibility(VT102
);
3078 insch(term
, -def(term
->esc_args
[0], 1));
3079 term
->seen_disp_event
= TRUE
;
3081 case 'c': /* DA: terminal type query */
3082 compatibility(VT100
);
3083 /* This is the response for a VT102 */
3085 ldisc_send(term
->ldisc
, term
->id_string
,
3086 strlen(term
->id_string
), 0);
3088 case 'n': /* DSR: cursor position query */
3090 if (term
->esc_args
[0] == 6) {
3092 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
3094 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
3095 } else if (term
->esc_args
[0] == 5) {
3096 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
3100 case 'h': /* SM: toggle modes to high */
3102 compatibility(VT100
);
3105 for (i
= 0; i
< term
->esc_nargs
; i
++)
3106 toggle_mode(term
, term
->esc_args
[i
],
3107 term
->esc_query
, TRUE
);
3110 case 'i': /* MC: Media copy */
3112 compatibility(VT100
);
3114 if (term
->esc_nargs
!= 1) break;
3115 if (term
->esc_args
[0] == 5 && *term
->cfg
.printer
) {
3116 term
->printing
= TRUE
;
3117 term
->only_printing
= !term
->esc_query
;
3118 term
->print_state
= 0;
3119 term_print_setup(term
);
3120 } else if (term
->esc_args
[0] == 4 &&
3122 term_print_finish(term
);
3126 case 'l': /* RM: toggle modes to low */
3128 compatibility(VT100
);
3131 for (i
= 0; i
< term
->esc_nargs
; i
++)
3132 toggle_mode(term
, term
->esc_args
[i
],
3133 term
->esc_query
, FALSE
);
3136 case 'g': /* TBC: clear tabs */
3137 compatibility(VT100
);
3138 if (term
->esc_nargs
== 1) {
3139 if (term
->esc_args
[0] == 0) {
3140 term
->tabs
[term
->curs
.x
] = FALSE
;
3141 } else if (term
->esc_args
[0] == 3) {
3143 for (i
= 0; i
< term
->cols
; i
++)
3144 term
->tabs
[i
] = FALSE
;
3148 case 'r': /* DECSTBM: set scroll margins */
3149 compatibility(VT100
);
3150 if (term
->esc_nargs
<= 2) {
3152 top
= def(term
->esc_args
[0], 1) - 1;
3153 bot
= (term
->esc_nargs
<= 1
3154 || term
->esc_args
[1] == 0 ?
3156 def(term
->esc_args
[1], term
->rows
)) - 1;
3157 if (bot
>= term
->rows
)
3158 bot
= term
->rows
- 1;
3159 /* VTTEST Bug 9 - if region is less than 2 lines
3160 * don't change region.
3162 if (bot
- top
> 0) {
3167 * I used to think the cursor should be
3168 * placed at the top of the newly marginned
3169 * area. Apparently not: VMS TPU falls over
3172 * Well actually it should for
3175 term
->curs
.y
= (term
->dec_om ?
3177 term
->seen_disp_event
= TRUE
;
3181 case 'm': /* SGR: set graphics rendition */
3184 * A VT100 without the AVO only had one
3185 * attribute, either underline or
3186 * reverse video depending on the
3187 * cursor type, this was selected by
3191 * This is sometimes DIM, eg on the
3194 * This is sometimes INVIS various ANSI.
3196 * This like 22 disables BOLD, DIM and INVIS
3198 * The ANSI colours appear on any
3199 * terminal that has colour (obviously)
3200 * but the interaction between sgr0 and
3201 * the colours varies but is usually
3202 * related to the background colour
3203 * erase item. The interaction between
3204 * colour attributes and the mono ones
3205 * is also very implementation
3208 * The 39 and 49 attributes are likely
3209 * to be unimplemented.
3212 for (i
= 0; i
< term
->esc_nargs
; i
++) {
3213 switch (def(term
->esc_args
[i
], 0)) {
3214 case 0: /* restore defaults */
3215 term
->curr_attr
= term
->default_attr
;
3217 case 1: /* enable bold */
3218 compatibility(VT100AVO
);
3219 term
->curr_attr
|= ATTR_BOLD
;
3221 case 21: /* (enable double underline) */
3222 compatibility(OTHER
);
3223 case 4: /* enable underline */
3224 compatibility(VT100AVO
);
3225 term
->curr_attr
|= ATTR_UNDER
;
3227 case 5: /* enable blink */
3228 compatibility(VT100AVO
);
3229 term
->curr_attr
|= ATTR_BLINK
;
3231 case 6: /* SCO light bkgrd */
3232 compatibility(SCOANSI
);
3233 term
->blink_is_real
= FALSE
;
3234 term
->curr_attr
|= ATTR_BLINK
;
3236 case 7: /* enable reverse video */
3237 term
->curr_attr
|= ATTR_REVERSE
;
3239 case 10: /* SCO acs off */
3240 compatibility(SCOANSI
);
3241 if (term
->cfg
.no_remote_charset
) break;
3242 term
->sco_acs
= 0; break;
3243 case 11: /* SCO acs on */
3244 compatibility(SCOANSI
);
3245 if (term
->cfg
.no_remote_charset
) break;
3246 term
->sco_acs
= 1; break;
3247 case 12: /* SCO acs on, |0x80 */
3248 compatibility(SCOANSI
);
3249 if (term
->cfg
.no_remote_charset
) break;
3250 term
->sco_acs
= 2; break;
3251 case 22: /* disable bold */
3252 compatibility2(OTHER
, VT220
);
3253 term
->curr_attr
&= ~ATTR_BOLD
;
3255 case 24: /* disable underline */
3256 compatibility2(OTHER
, VT220
);
3257 term
->curr_attr
&= ~ATTR_UNDER
;
3259 case 25: /* disable blink */
3260 compatibility2(OTHER
, VT220
);
3261 term
->curr_attr
&= ~ATTR_BLINK
;
3263 case 27: /* disable reverse video */
3264 compatibility2(OTHER
, VT220
);
3265 term
->curr_attr
&= ~ATTR_REVERSE
;
3276 term
->curr_attr
&= ~ATTR_FGMASK
;
3278 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
3288 /* xterm-style bright foreground */
3289 term
->curr_attr
&= ~ATTR_FGMASK
;
3291 ((term
->esc_args
[i
] - 90 + 16)
3294 case 39: /* default-foreground */
3295 term
->curr_attr
&= ~ATTR_FGMASK
;
3296 term
->curr_attr
|= ATTR_DEFFG
;
3307 term
->curr_attr
&= ~ATTR_BGMASK
;
3309 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
3319 /* xterm-style bright background */
3320 term
->curr_attr
&= ~ATTR_BGMASK
;
3322 ((term
->esc_args
[i
] - 100 + 16)
3325 case 49: /* default-background */
3326 term
->curr_attr
&= ~ATTR_BGMASK
;
3327 term
->curr_attr
|= ATTR_DEFBG
;
3331 set_erase_char(term
);
3334 case 's': /* save cursor */
3335 save_cursor(term
, TRUE
);
3337 case 'u': /* restore cursor */
3338 save_cursor(term
, FALSE
);
3339 term
->seen_disp_event
= TRUE
;
3341 case 't': /* DECSLPP: set page size - ie window height */
3343 * VT340/VT420 sequence DECSLPP, DEC only allows values
3344 * 24/25/36/48/72/144 other emulators (eg dtterm) use
3345 * illegal values (eg first arg 1..9) for window changing
3348 if (term
->esc_nargs
<= 1
3349 && (term
->esc_args
[0] < 1 ||
3350 term
->esc_args
[0] >= 24)) {
3351 compatibility(VT340TEXT
);
3352 if (!term
->cfg
.no_remote_resize
)
3353 request_resize(term
->frontend
, term
->cols
,
3354 def(term
->esc_args
[0], 24));
3356 } else if (term
->esc_nargs
>= 1 &&
3357 term
->esc_args
[0] >= 1 &&
3358 term
->esc_args
[0] < 24) {
3359 compatibility(OTHER
);
3361 switch (term
->esc_args
[0]) {
3365 set_iconic(term
->frontend
, FALSE
);
3368 set_iconic(term
->frontend
, TRUE
);
3371 if (term
->esc_nargs
>= 3) {
3372 if (!term
->cfg
.no_remote_resize
)
3373 move_window(term
->frontend
,
3374 def(term
->esc_args
[1], 0),
3375 def(term
->esc_args
[2], 0));
3379 /* We should resize the window to a given
3380 * size in pixels here, but currently our
3381 * resizing code isn't healthy enough to
3386 set_zorder(term
->frontend
, TRUE
);
3389 /* move to bottom */
3390 set_zorder(term
->frontend
, FALSE
);
3393 refresh_window(term
->frontend
);
3396 if (term
->esc_nargs
>= 3) {
3397 if (!term
->cfg
.no_remote_resize
)
3398 request_resize(term
->frontend
,
3399 def(term
->esc_args
[2], term
->cfg
.width
),
3400 def(term
->esc_args
[1], term
->cfg
.height
));
3404 if (term
->esc_nargs
>= 2)
3405 set_zoomed(term
->frontend
,
3411 ldisc_send(term
->ldisc
,
3412 is_iconic(term
->frontend
) ?
3413 "\033[1t" : "\033[2t", 4, 0);
3417 get_window_pos(term
->frontend
, &x
, &y
);
3418 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
3419 ldisc_send(term
->ldisc
, buf
, len
, 0);
3424 get_window_pixels(term
->frontend
, &x
, &y
);
3425 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
3426 ldisc_send(term
->ldisc
, buf
, len
, 0);
3431 len
= sprintf(buf
, "\033[8;%d;%dt",
3432 term
->rows
, term
->cols
);
3433 ldisc_send(term
->ldisc
, buf
, len
, 0);
3438 * Hmmm. Strictly speaking we
3439 * should return `the size of the
3440 * screen in characters', but
3441 * that's not easy: (a) window
3442 * furniture being what it is it's
3443 * hard to compute, and (b) in
3444 * resize-font mode maximising the
3445 * window wouldn't change the
3446 * number of characters. *shrug*. I
3447 * think we'll ignore it for the
3448 * moment and see if anyone
3449 * complains, and then ask them
3450 * what they would like it to do.
3455 !term
->cfg
.no_remote_qtitle
) {
3456 p
= get_window_title(term
->frontend
, TRUE
);
3458 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
3459 ldisc_send(term
->ldisc
, p
, len
, 0);
3460 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3465 !term
->cfg
.no_remote_qtitle
) {
3466 p
= get_window_title(term
->frontend
,FALSE
);
3468 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
3469 ldisc_send(term
->ldisc
, p
, len
, 0);
3470 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
3476 case 'S': /* SU: Scroll up */
3477 compatibility(SCOANSI
);
3478 scroll(term
, term
->marg_t
, term
->marg_b
,
3479 def(term
->esc_args
[0], 1), TRUE
);
3480 term
->wrapnext
= FALSE
;
3481 term
->seen_disp_event
= TRUE
;
3483 case 'T': /* SD: Scroll down */
3484 compatibility(SCOANSI
);
3485 scroll(term
, term
->marg_t
, term
->marg_b
,
3486 -def(term
->esc_args
[0], 1), TRUE
);
3487 term
->wrapnext
= FALSE
;
3488 term
->seen_disp_event
= TRUE
;
3490 case ANSI('|', '*'): /* DECSNLS */
3492 * Set number of lines on screen
3493 * VT420 uses VGA like hardware and can
3494 * support any size in reasonable range
3495 * (24..49 AIUI) with no default specified.
3497 compatibility(VT420
);
3498 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
3499 if (!term
->cfg
.no_remote_resize
)
3500 request_resize(term
->frontend
, term
->cols
,
3501 def(term
->esc_args
[0],
3506 case ANSI('|', '$'): /* DECSCPP */
3508 * Set number of columns per page
3509 * Docs imply range is only 80 or 132, but
3512 compatibility(VT340TEXT
);
3513 if (term
->esc_nargs
<= 1) {
3514 if (!term
->cfg
.no_remote_resize
)
3515 request_resize(term
->frontend
,
3516 def(term
->esc_args
[0],
3517 term
->cfg
.width
), term
->rows
);
3521 case 'X': /* ECH: write N spaces w/o moving cursor */
3522 /* XXX VTTEST says this is vt220, vt510 manual
3524 compatibility(ANSIMIN
);
3526 int n
= def(term
->esc_args
[0], 1);
3528 int p
= term
->curs
.x
;
3529 termline
*cline
= scrlineptr(term
->curs
.y
);
3531 if (n
> term
->cols
- term
->curs
.x
)
3532 n
= term
->cols
- term
->curs
.x
;
3533 cursplus
= term
->curs
;
3535 check_boundary(term
, term
->curs
.x
, term
->curs
.y
);
3536 check_boundary(term
, term
->curs
.x
+n
, term
->curs
.y
);
3537 check_selection(term
, term
->curs
, cursplus
);
3539 copy_termchar(cline
, p
++,
3541 term
->seen_disp_event
= TRUE
;
3544 case 'x': /* DECREQTPARM: report terminal characteristics */
3545 compatibility(VT100
);
3548 int i
= def(term
->esc_args
[0], 0);
3549 if (i
== 0 || i
== 1) {
3550 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
3552 ldisc_send(term
->ldisc
, buf
, 20, 0);
3556 case 'Z': /* CBT: BackTab for xterm */
3557 compatibility(OTHER
);
3559 int i
= def(term
->esc_args
[0], 1);
3560 pos old_curs
= term
->curs
;
3562 for(;i
>0 && term
->curs
.x
>0; i
--) {
3565 } while (term
->curs
.x
>0 &&
3566 !term
->tabs
[term
->curs
.x
]);
3568 check_selection(term
, old_curs
, term
->curs
);
3571 case ANSI('c', '='): /* Hide or Show Cursor */
3572 compatibility(SCOANSI
);
3573 switch(term
->esc_args
[0]) {
3574 case 0: /* hide cursor */
3575 term
->cursor_on
= FALSE
;
3577 case 1: /* restore cursor */
3578 term
->big_cursor
= FALSE
;
3579 term
->cursor_on
= TRUE
;
3581 case 2: /* block cursor */
3582 term
->big_cursor
= TRUE
;
3583 term
->cursor_on
= TRUE
;
3587 case ANSI('C', '='):
3589 * set cursor start on scanline esc_args[0] and
3590 * end on scanline esc_args[1].If you set
3591 * the bottom scan line to a value less than
3592 * the top scan line, the cursor will disappear.
3594 compatibility(SCOANSI
);
3595 if (term
->esc_nargs
>= 2) {
3596 if (term
->esc_args
[0] > term
->esc_args
[1])
3597 term
->cursor_on
= FALSE
;
3599 term
->cursor_on
= TRUE
;
3602 case ANSI('D', '='):
3603 compatibility(SCOANSI
);
3604 term
->blink_is_real
= FALSE
;
3605 if (term
->esc_args
[0]>=1)
3606 term
->curr_attr
|= ATTR_BLINK
;
3608 term
->curr_attr
&= ~ATTR_BLINK
;
3610 case ANSI('E', '='):
3611 compatibility(SCOANSI
);
3612 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
3614 case ANSI('F', '='): /* set normal foreground */
3615 compatibility(SCOANSI
);
3616 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3618 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3619 ((term
->esc_args
[0] & 0x8) << 1)) <<
3621 term
->curr_attr
&= ~ATTR_FGMASK
;
3622 term
->curr_attr
|= colour
;
3623 term
->default_attr
&= ~ATTR_FGMASK
;
3624 term
->default_attr
|= colour
;
3627 case ANSI('G', '='): /* set normal background */
3628 compatibility(SCOANSI
);
3629 if (term
->esc_args
[0] >= 0 && term
->esc_args
[0] < 16) {
3631 (sco2ansicolour
[term
->esc_args
[0] & 0x7] |
3632 ((term
->esc_args
[0] & 0x8) << 1)) <<
3634 term
->curr_attr
&= ~ATTR_BGMASK
;
3635 term
->curr_attr
|= colour
;
3636 term
->default_attr
&= ~ATTR_BGMASK
;
3637 term
->default_attr
|= colour
;
3640 case ANSI('L', '='):
3641 compatibility(SCOANSI
);
3642 term
->use_bce
= (term
->esc_args
[0] <= 0);
3643 set_erase_char(term
);
3645 case ANSI('p', '"'): /* DECSCL: set compat level */
3647 * Allow the host to make this emulator a
3648 * 'perfect' VT102. This first appeared in
3649 * the VT220, but we do need to get back to
3650 * PuTTY mode so I won't check it.
3652 * The arg in 40..42,50 are a PuTTY extension.
3653 * The 2nd arg, 8bit vs 7bit is not checked.
3655 * Setting VT102 mode should also change
3656 * the Fkeys to generate PF* codes as a
3657 * real VT102 has no Fkeys. The VT220 does
3658 * this, F11..F13 become ESC,BS,LF other
3659 * Fkeys send nothing.
3661 * Note ESC c will NOT change this!
3664 switch (term
->esc_args
[0]) {
3666 term
->compatibility_level
&= ~TM_VTXXX
;
3667 term
->compatibility_level
|= TM_VT102
;
3670 term
->compatibility_level
&= ~TM_VTXXX
;
3671 term
->compatibility_level
|= TM_VT220
;
3675 if (term
->esc_args
[0] > 60 &&
3676 term
->esc_args
[0] < 70)
3677 term
->compatibility_level
|= TM_VTXXX
;
3681 term
->compatibility_level
&= TM_VTXXX
;
3684 term
->compatibility_level
= TM_PUTTY
;
3687 term
->compatibility_level
= TM_SCOANSI
;
3691 term
->compatibility_level
= TM_PUTTY
;
3697 /* Change the response to CSI c */
3698 if (term
->esc_args
[0] == 50) {
3701 strcpy(term
->id_string
, "\033[?");
3702 for (i
= 1; i
< term
->esc_nargs
; i
++) {
3704 strcat(term
->id_string
, ";");
3705 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
3706 strcat(term
->id_string
, lbuf
);
3708 strcat(term
->id_string
, "c");
3711 /* Is this a good idea ?
3712 * Well we should do a soft reset at this point ...
3714 if (!has_compat(VT420
) && has_compat(VT100
)) {
3715 if (!term
->cfg
.no_remote_resize
) {
3716 if (term
->reset_132
)
3717 request_resize(132, 24);
3719 request_resize(80, 24);
3727 term
->osc_w
= FALSE
;
3729 case 'P': /* Linux palette sequence */
3730 term
->termstate
= SEEN_OSC_P
;
3731 term
->osc_strlen
= 0;
3733 case 'R': /* Linux palette reset */
3734 palette_reset(term
->frontend
);
3735 term_invalidate(term
);
3736 term
->termstate
= TOPLEVEL
;
3738 case 'W': /* word-set */
3739 term
->termstate
= SEEN_OSC_W
;
3752 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3756 * Grotty hack to support xterm and DECterm title
3757 * sequences concurrently.
3759 if (term
->esc_args
[0] == 2) {
3760 term
->esc_args
[0] = 1;
3763 /* else fall through */
3765 term
->termstate
= OSC_STRING
;
3766 term
->osc_strlen
= 0;
3771 * This OSC stuff is EVIL. It takes just one character to get into
3772 * sysline mode and it's not initially obvious how to get out.
3773 * So I've added CR and LF as string aborts.
3774 * This shouldn't effect compatibility as I believe embedded
3775 * control characters are supposed to be interpreted (maybe?)
3776 * and they don't display anything useful anyway.
3780 if (c
== '\012' || c
== '\015') {
3781 term
->termstate
= TOPLEVEL
;
3782 } else if (c
== 0234 || c
== '\007') {
3784 * These characters terminate the string; ST and BEL
3785 * terminate the sequence and trigger instant
3786 * processing of it, whereas ESC goes back to SEEN_ESC
3787 * mode unless it is followed by \, in which case it is
3788 * synonymous with ST in the first place.
3791 term
->termstate
= TOPLEVEL
;
3792 } else if (c
== '\033')
3793 term
->termstate
= OSC_MAYBE_ST
;
3794 else if (term
->osc_strlen
< OSC_STR_MAX
)
3795 term
->osc_string
[term
->osc_strlen
++] = (char)c
;
3799 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
3801 if ((int)c
>= '0' && (int)c
<= '9')
3803 else if ((int)c
>= 'A' && (int)c
<= 'A' + max
- 10)
3805 else if ((int)c
>= 'a' && (int)c
<= 'a' + max
- 10)
3808 term
->termstate
= TOPLEVEL
;
3811 term
->osc_string
[term
->osc_strlen
++] = val
;
3812 if (term
->osc_strlen
>= 7) {
3813 palette_set(term
->frontend
, term
->osc_string
[0],
3814 term
->osc_string
[1] * 16 + term
->osc_string
[2],
3815 term
->osc_string
[3] * 16 + term
->osc_string
[4],
3816 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
3817 term_invalidate(term
);
3818 term
->termstate
= TOPLEVEL
;
3834 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
3837 term
->termstate
= OSC_STRING
;
3838 term
->osc_strlen
= 0;
3842 term
->termstate
= TOPLEVEL
;
3843 term
->seen_disp_event
= TRUE
;
3846 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
3849 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
3852 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
3855 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
3858 * From the VT100 Manual
3859 * NOTE: The special graphics characters in the VT100
3860 * are different from those in the VT52
3862 * From VT102 manual:
3863 * 137 _ Blank - Same
3864 * 140 ` Reserved - Humm.
3865 * 141 a Solid rectangle - Similar
3866 * 142 b 1/ - Top half of fraction for the
3867 * 143 c 3/ - subscript numbers below.
3870 * 146 f Degrees - Same
3871 * 147 g Plus or minus - Same
3873 * 151 i Ellipsis (dots)
3876 * 154 l Bar at scan 0
3877 * 155 m Bar at scan 1
3878 * 156 n Bar at scan 2
3879 * 157 o Bar at scan 3 - Similar
3880 * 160 p Bar at scan 4 - Similar
3881 * 161 q Bar at scan 5 - Similar
3882 * 162 r Bar at scan 6 - Same
3883 * 163 s Bar at scan 7 - Similar
3898 term
->cset_attr
[term
->cset
= 0] = CSET_LINEDRW
;
3901 term
->cset_attr
[term
->cset
= 0] = CSET_ASCII
;
3904 move(term
, 0, 0, 0);
3907 if (term
->curs
.y
== 0)
3908 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
3909 else if (term
->curs
.y
> 0)
3911 term
->wrapnext
= FALSE
;
3914 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3918 erase_lots(term
, TRUE
, FALSE
, TRUE
);
3922 /* XXX Print cursor line */
3925 /* XXX Start controller mode */
3928 /* XXX Stop controller mode */
3932 term
->termstate
= VT52_Y1
;
3936 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
3939 term
->app_keypad_keys
= TRUE
;
3942 term
->app_keypad_keys
= FALSE
;
3945 /* XXX This should switch to VT100 mode not current or default
3946 * VT mode. But this will only have effect in a VT220+
3949 term
->vt52_mode
= FALSE
;
3950 term
->blink_is_real
= term
->cfg
.blinktext
;
3954 /* XXX Enter auto print mode */
3957 /* XXX Exit auto print mode */
3960 /* XXX Print screen */
3966 /* compatibility(ATARI) */
3967 move(term
, 0, 0, 0);
3968 erase_lots(term
, FALSE
, FALSE
, TRUE
);
3972 /* compatibility(ATARI) */
3973 if (term
->curs
.y
<= term
->marg_b
)
3974 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
3977 /* compatibility(ATARI) */
3978 if (term
->curs
.y
<= term
->marg_b
)
3979 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
3982 /* compatibility(ATARI) */
3983 term
->termstate
= VT52_FG
;
3986 /* compatibility(ATARI) */
3987 term
->termstate
= VT52_BG
;
3990 /* compatibility(ATARI) */
3991 erase_lots(term
, FALSE
, TRUE
, FALSE
);
3995 /* compatibility(ATARI) */
3996 term
->cursor_on
= TRUE
;
3999 /* compatibility(ATARI) */
4000 term
->cursor_on
= FALSE
;
4002 /* case 'j': Save cursor position - broken on ST */
4003 /* case 'k': Restore cursor position */
4005 /* compatibility(ATARI) */
4006 erase_lots(term
, TRUE
, TRUE
, TRUE
);
4008 term
->wrapnext
= FALSE
;
4011 /* compatibility(ATARI) */
4012 erase_lots(term
, TRUE
, TRUE
, FALSE
);
4015 /* compatibility(ATARI) */
4016 term
->curr_attr
|= ATTR_REVERSE
;
4019 /* compatibility(ATARI) */
4020 term
->curr_attr
&= ~ATTR_REVERSE
;
4022 case 'v': /* wrap Autowrap on - Wyse style */
4023 /* compatibility(ATARI) */
4026 case 'w': /* Autowrap off */
4027 /* compatibility(ATARI) */
4032 /* compatibility(OTHER) */
4033 term
->vt52_bold
= FALSE
;
4034 term
->curr_attr
= ATTR_DEFAULT
;
4035 set_erase_char(term
);
4038 /* compatibility(VI50) */
4039 term
->curr_attr
|= ATTR_UNDER
;
4042 /* compatibility(VI50) */
4043 term
->curr_attr
&= ~ATTR_UNDER
;
4046 /* compatibility(VI50) */
4047 term
->vt52_bold
= TRUE
;
4048 term
->curr_attr
|= ATTR_BOLD
;
4051 /* compatibility(VI50) */
4052 term
->vt52_bold
= FALSE
;
4053 term
->curr_attr
&= ~ATTR_BOLD
;
4059 term
->termstate
= VT52_Y2
;
4060 move(term
, term
->curs
.x
, c
- ' ', 0);
4063 term
->termstate
= TOPLEVEL
;
4064 move(term
, c
- ' ', term
->curs
.y
, 0);
4069 term
->termstate
= TOPLEVEL
;
4070 term
->curr_attr
&= ~ATTR_FGMASK
;
4071 term
->curr_attr
&= ~ATTR_BOLD
;
4072 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
4073 if ((c
& 0x8) || term
->vt52_bold
)
4074 term
->curr_attr
|= ATTR_BOLD
;
4076 set_erase_char(term
);
4079 term
->termstate
= TOPLEVEL
;
4080 term
->curr_attr
&= ~ATTR_BGMASK
;
4081 term
->curr_attr
&= ~ATTR_BLINK
;
4082 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
4084 /* Note: bold background */
4086 term
->curr_attr
|= ATTR_BLINK
;
4088 set_erase_char(term
);
4091 default: break; /* placate gcc warning about enum use */
4093 if (term
->selstate
!= NO_SELECTION
) {
4094 pos cursplus
= term
->curs
;
4096 check_selection(term
, term
->curs
, cursplus
);
4100 term_print_flush(term
);
4101 logflush(term
->logctx
);
4105 * To prevent having to run the reasonably tricky bidi algorithm
4106 * too many times, we maintain a cache of the last lineful of data
4107 * fed to the algorithm on each line of the display.
4109 static int term_bidi_cache_hit(Terminal
*term
, int line
,
4110 termchar
*lbefore
, int width
)
4114 if (!term
->pre_bidi_cache
)
4115 return FALSE
; /* cache doesn't even exist yet! */
4117 if (line
>= term
->bidi_cache_size
)
4118 return FALSE
; /* cache doesn't have this many lines */
4120 if (!term
->pre_bidi_cache
[line
])
4121 return FALSE
; /* cache doesn't contain _this_ line */
4123 for (i
= 0; i
< width
; i
++)
4124 if (!termchars_equal(term
->pre_bidi_cache
[line
] + i
, lbefore
+ i
))
4125 return FALSE
; /* line doesn't match cache */
4127 return TRUE
; /* it didn't match. */
4130 static void term_bidi_cache_store(Terminal
*term
, int line
, termchar
*lbefore
,
4131 termchar
*lafter
, int width
)
4133 if (!term
->pre_bidi_cache
|| term
->bidi_cache_size
<= line
) {
4134 int j
= term
->bidi_cache_size
;
4135 term
->bidi_cache_size
= line
+1;
4136 term
->pre_bidi_cache
= sresize(term
->pre_bidi_cache
,
4137 term
->bidi_cache_size
,
4139 term
->post_bidi_cache
= sresize(term
->post_bidi_cache
,
4140 term
->bidi_cache_size
,
4142 while (j
< term
->bidi_cache_size
) {
4143 term
->pre_bidi_cache
[j
] = term
->post_bidi_cache
[j
] = NULL
;
4148 sfree(term
->pre_bidi_cache
[line
]);
4149 sfree(term
->post_bidi_cache
[line
]);
4151 term
->pre_bidi_cache
[line
] = snewn(width
, termchar
);
4152 term
->post_bidi_cache
[line
] = snewn(width
, termchar
);
4154 memcpy(term
->pre_bidi_cache
[line
], lbefore
, width
* TSIZE
);
4155 memcpy(term
->post_bidi_cache
[line
], lafter
, width
* TSIZE
);
4159 * Given a context, update the window. Out of paranoia, we don't
4160 * allow WM_PAINT responses to do scrolling optimisations.
4162 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
4164 int i
, it
, j
, our_curs_y
, our_curs_x
;
4169 termchar cursor_background
;
4170 unsigned long ticks
;
4171 #ifdef OPTIMISE_SCROLL
4172 struct scrollregion
*sr
;
4173 #endif /* OPTIMISE_SCROLL */
4175 cursor_background
= term
->basic_erase_char
;
4178 ch
= snewn(chlen
, wchar_t);
4181 * Check the visual bell state.
4183 if (term
->in_vbell
) {
4184 ticks
= GETTICKCOUNT();
4185 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
4186 term
->in_vbell
= FALSE
;
4189 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
4192 * screen array, disptop, scrtop,
4194 * cfg.blinkpc, blink_is_real, tblinker,
4195 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
4198 /* Has the cursor position or type changed ? */
4199 if (term
->cursor_on
) {
4200 if (term
->has_focus
) {
4201 if (term
->blinker
|| !term
->cfg
.blink_cur
)
4202 cursor
= TATTR_ACTCURS
;
4206 cursor
= TATTR_PASCURS
;
4208 cursor
|= TATTR_RIGHTCURS
;
4211 our_curs_y
= term
->curs
.y
- term
->disptop
;
4214 * Adjust the cursor position in the case where it's
4215 * resting on the right-hand half of a CJK wide character.
4216 * xterm's behaviour here, which seems adequate to me, is
4217 * to display the cursor covering the _whole_ character,
4218 * exactly as if it were one space to the left.
4220 termline
*ldata
= lineptr(term
->curs
.y
);
4221 our_curs_x
= term
->curs
.x
;
4222 if (our_curs_x
> 0 &&
4223 ldata
->chars
[our_curs_x
].chr
== UCSWIDE
)
4229 * If the cursor is not where it was last time we painted, and
4230 * its previous position is visible on screen, invalidate its
4231 * previous position.
4233 if (term
->dispcursy
>= 0 &&
4234 (term
->curstype
!= cursor
||
4235 term
->dispcursy
!= our_curs_y
||
4236 term
->dispcursx
!= our_curs_x
)) {
4237 termchar
*dispcurs
= term
->disptext
[term
->dispcursy
]->chars
+
4240 if (term
->dispcursx
> 0 && dispcurs
->chr
== UCSWIDE
)
4241 dispcurs
[-1].attr
|= ATTR_INVALID
;
4242 if (term
->dispcursx
< term
->cols
-1 && dispcurs
[1].chr
== UCSWIDE
)
4243 dispcurs
[1].attr
|= ATTR_INVALID
;
4244 dispcurs
->attr
|= ATTR_INVALID
;
4248 term
->dispcursx
= term
->dispcursy
= -1;
4250 #ifdef OPTIMISE_SCROLL
4252 sr
= term
->scrollhead
;
4254 struct scrollregion
*next
= sr
->next
;
4255 do_scroll(ctx
, sr
->topline
, sr
->botline
, sr
->lines
);
4259 term
->scrollhead
= term
->scrolltail
= NULL
;
4260 #endif /* OPTIMISE_SCROLL */
4262 /* The normal screen data */
4263 for (i
= 0; i
< term
->rows
; i
++) {
4266 int dirty_line
, dirty_run
, selected
;
4267 unsigned long attr
= 0, cset
= 0;
4268 int updated_line
= 0;
4271 int last_run_dirty
= 0;
4273 scrpos
.y
= i
+ term
->disptop
;
4274 ldata
= lineptr(scrpos
.y
);
4276 dirty_run
= dirty_line
= (ldata
->lattr
!=
4277 term
->disptext
[i
]->lattr
);
4278 term
->disptext
[i
]->lattr
= ldata
->lattr
;
4280 /* Do Arabic shaping and bidi. */
4281 if(!term
->cfg
.bidi
|| !term
->cfg
.arabicshaping
) {
4283 if (!term_bidi_cache_hit(term
, i
, ldata
->chars
, term
->cols
)) {
4285 if (term
->wcFromTo_size
< term
->cols
) {
4286 term
->wcFromTo_size
= term
->cols
;
4287 term
->wcFrom
= sresize(term
->wcFrom
, term
->wcFromTo_size
,
4289 term
->wcTo
= sresize(term
->wcTo
, term
->wcFromTo_size
,
4293 for(it
=0; it
<term
->cols
; it
++)
4295 unsigned long uc
= (ldata
->chars
[it
].chr
);
4297 switch (uc
& CSET_MASK
) {
4299 if (!term
->cfg
.rawcnp
) {
4300 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4304 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4307 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4310 switch (uc
& CSET_MASK
) {
4312 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4315 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4319 term
->wcFrom
[it
].origwc
= term
->wcFrom
[it
].wc
=
4321 term
->wcFrom
[it
].index
= it
;
4325 do_bidi(term
->wcFrom
, term
->cols
);
4327 /* this is saved iff done from inside the shaping */
4328 if(!term
->cfg
.bidi
&& term
->cfg
.arabicshaping
)
4329 for(it
=0; it
<term
->cols
; it
++)
4330 term
->wcTo
[it
] = term
->wcFrom
[it
];
4332 if(!term
->cfg
.arabicshaping
)
4333 do_shape(term
->wcFrom
, term
->wcTo
, term
->cols
);
4335 if (term
->ltemp_size
< ldata
->size
) {
4336 term
->ltemp_size
= ldata
->size
;
4337 term
->ltemp
= sresize(term
->ltemp
, term
->ltemp_size
,
4341 memcpy(term
->ltemp
, ldata
->chars
, ldata
->size
* TSIZE
);
4343 for(it
=0; it
<term
->cols
; it
++)
4345 term
->ltemp
[it
] = ldata
->chars
[term
->wcTo
[it
].index
];
4346 if (term
->ltemp
[it
].cc_next
)
4347 term
->ltemp
[it
].cc_next
-=
4348 it
- term
->wcTo
[it
].index
;
4350 if (term
->wcTo
[it
].origwc
!= term
->wcTo
[it
].wc
)
4351 term
->ltemp
[it
].chr
= term
->wcTo
[it
].wc
;
4353 term_bidi_cache_store(term
, i
, ldata
->chars
,
4354 term
->ltemp
, ldata
->size
);
4356 lchars
= term
->ltemp
;
4358 lchars
= term
->post_bidi_cache
[i
];
4361 lchars
= ldata
->chars
;
4363 for (j
= 0; j
< term
->cols
; j
++) {
4364 unsigned long tattr
, tchar
;
4365 termchar
*d
= lchars
+ j
;
4366 int break_run
, do_copy
;
4372 switch (tchar
& CSET_MASK
) {
4374 tchar
= term
->ucsdata
->unitab_line
[tchar
& 0xFF];
4377 tchar
= term
->ucsdata
->unitab_xterm
[tchar
& 0xFF];
4380 tchar
= term
->ucsdata
->unitab_scoacs
[tchar
&0xFF];
4383 if (j
< term
->cols
-1 && d
[1].chr
== UCSWIDE
)
4386 /* Video reversing things */
4387 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
4388 if (term
->seltype
== LEXICOGRAPHIC
)
4389 selected
= (posle(term
->selstart
, scrpos
) &&
4390 poslt(scrpos
, term
->selend
));
4392 selected
= (posPle(term
->selstart
, scrpos
) &&
4393 posPlt(scrpos
, term
->selend
));
4397 ^ (selected ? ATTR_REVERSE
: 0));
4399 /* 'Real' blinking ? */
4400 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
4401 if (term
->has_focus
&& term
->tblinker
) {
4402 tchar
= term
->ucsdata
->unitab_line
[(unsigned char)' '];
4404 tattr
&= ~ATTR_BLINK
;
4408 * Check the font we'll _probably_ be using to see if
4409 * the character is wide when we don't want it to be.
4411 if (tchar
!= term
->disptext
[i
]->chars
[j
].chr
||
4412 tattr
!= (term
->disptext
[i
]->chars
[j
].attr
&~
4414 if ((tattr
& ATTR_WIDE
) == 0 && char_width(ctx
, tchar
) == 2)
4415 tattr
|= ATTR_NARROW
;
4416 } else if (term
->disptext
[i
]->chars
[j
].attr
& ATTR_NARROW
)
4417 tattr
|= ATTR_NARROW
;
4419 /* Cursor here ? Save the 'background' */
4420 if (i
== our_curs_y
&& j
== our_curs_x
) {
4422 cursor_background
.chr
= tchar
;
4423 cursor_background
.attr
= tattr
;
4424 /* For once, this cc_next field is an absolute index in lchars */
4426 cursor_background
.cc_next
= d
->cc_next
+ j
;
4428 cursor_background
.cc_next
= 0;
4429 term
->dispcursx
= j
;
4430 term
->dispcursy
= i
;
4433 if ((term
->disptext
[i
]->chars
[j
].attr
^ tattr
) & ATTR_WIDE
)
4436 break_run
= ((tattr
^ attr
) & term
->attr_mask
) != 0;
4438 /* Special hack for VT100 Linedraw glyphs */
4439 if (tchar
>= 0x23BA && tchar
<= 0x23BD)
4443 * Separate out sequences of characters that have the
4444 * same CSET, if that CSET is a magic one.
4446 if (CSET_OF(tchar
) != cset
)
4450 * Break on both sides of any combined-character cell.
4452 if (d
->cc_next
!= 0 ||
4453 (j
> 0 && d
[-1].cc_next
!= 0))
4456 if (!term
->ucsdata
->dbcs_screenfont
&& !dirty_line
) {
4457 if (term
->disptext
[i
]->chars
[j
].chr
== tchar
&&
4458 term
->disptext
[i
]->chars
[j
].attr
== tattr
)
4460 else if (!dirty_run
&& ccount
== 1)
4465 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
4466 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4472 cset
= CSET_OF(tchar
);
4473 if (term
->ucsdata
->dbcs_screenfont
)
4474 last_run_dirty
= dirty_run
;
4475 dirty_run
= dirty_line
;
4479 if (!termchars_equal_override(&term
->disptext
[i
]->chars
[j
],
4485 if (ccount
>= chlen
) {
4486 chlen
= ccount
+ 256;
4487 ch
= sresize(ch
, chlen
, wchar_t);
4489 ch
[ccount
++] = (wchar_t) tchar
;
4494 while (dd
->cc_next
) {
4495 unsigned long schar
;
4500 switch (schar
& CSET_MASK
) {
4502 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4505 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4508 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4512 if (ccount
>= chlen
) {
4513 chlen
= ccount
+ 256;
4514 ch
= sresize(ch
, chlen
, wchar_t);
4516 ch
[ccount
++] = (wchar_t) schar
;
4519 attr
|= TATTR_COMBINING
;
4523 copy_termchar(term
->disptext
[i
], j
, d
);
4524 term
->disptext
[i
]->chars
[j
].chr
= tchar
;
4525 term
->disptext
[i
]->chars
[j
].attr
= tattr
;
4528 /* If it's a wide char step along to the next one. */
4529 if (tattr
& ATTR_WIDE
) {
4530 if (++j
< term
->cols
) {
4533 * By construction above, the cursor should not
4534 * be on the right-hand half of this character.
4537 assert(!(i
== our_curs_y
&& j
== our_curs_x
));
4538 if (!termchars_equal(&term
->disptext
[i
]->chars
[j
], d
))
4540 copy_termchar(term
->disptext
[i
], j
, d
);
4544 if (dirty_run
&& ccount
> 0) {
4545 do_text(ctx
, start
, i
, ch
, ccount
, attr
, ldata
->lattr
);
4549 /* Cursor on this line ? (and changed) */
4550 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
4551 ch
[0] = (wchar_t) cursor_background
.chr
;
4552 attr
= cursor_background
.attr
| cursor
;
4554 if (cursor_background
.cc_next
) {
4555 termchar
*dd
= ldata
->chars
+ cursor_background
.cc_next
;
4558 unsigned long schar
;
4561 switch (schar
& CSET_MASK
) {
4563 schar
= term
->ucsdata
->unitab_line
[schar
& 0xFF];
4566 schar
= term
->ucsdata
->unitab_xterm
[schar
& 0xFF];
4569 schar
= term
->ucsdata
->unitab_scoacs
[schar
&0xFF];
4573 if (ccount
>= chlen
) {
4574 chlen
= ccount
+ 256;
4575 ch
= sresize(ch
, chlen
, wchar_t);
4577 ch
[ccount
++] = (wchar_t) schar
;
4585 attr
|= TATTR_COMBINING
;
4588 do_cursor(ctx
, our_curs_x
, i
, ch
, 1, attr
, ldata
->lattr
);
4589 term
->curstype
= cursor
;
4599 * Flick the switch that says if blinking things should be shown or hidden.
4602 void term_blink(Terminal
*term
, int flg
)
4604 long now
, blink_diff
;
4606 now
= GETTICKCOUNT();
4607 blink_diff
= now
- term
->last_tblink
;
4609 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
4610 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
4611 term
->last_tblink
= now
;
4612 term
->tblinker
= !term
->tblinker
;
4617 term
->last_blink
= now
;
4621 blink_diff
= now
- term
->last_blink
;
4623 /* Make sure the cursor blinks no faster than system blink rate */
4624 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
4627 term
->last_blink
= now
;
4628 term
->blinker
= !term
->blinker
;
4632 * Invalidate the whole screen so it will be repainted in full.
4634 void term_invalidate(Terminal
*term
)
4638 for (i
= 0; i
< term
->rows
; i
++)
4639 for (j
= 0; j
< term
->cols
; j
++)
4640 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4644 * Paint the window in response to a WM_PAINT message.
4646 void term_paint(Terminal
*term
, Context ctx
,
4647 int left
, int top
, int right
, int bottom
, int immediately
)
4650 if (left
< 0) left
= 0;
4651 if (top
< 0) top
= 0;
4652 if (right
>= term
->cols
) right
= term
->cols
-1;
4653 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
4655 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
4656 if (term
->disptext
[i
]->lattr
== LATTR_NORM
)
4657 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
4658 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4660 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
4661 term
->disptext
[i
]->chars
[j
].attr
= ATTR_INVALID
;
4664 /* This should happen soon enough, also for some reason it sometimes
4665 * fails to actually do anything when re-sizing ... painting the wrong
4669 do_paint (term
, ctx
, FALSE
);
4673 * Attempt to scroll the scrollback. The second parameter gives the
4674 * position we want to scroll to; the first is +1 to denote that
4675 * this position is relative to the beginning of the scrollback, -1
4676 * to denote it is relative to the end, and 0 to denote that it is
4677 * relative to the current position.
4679 void term_scroll(Terminal
*term
, int rel
, int where
)
4681 int sbtop
= -sblines(term
);
4682 #ifdef OPTIMISE_SCROLL
4683 int olddisptop
= term
->disptop
;
4685 #endif /* OPTIMISE_SCROLL */
4687 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
4688 if (term
->disptop
< sbtop
)
4689 term
->disptop
= sbtop
;
4690 if (term
->disptop
> 0)
4693 #ifdef OPTIMISE_SCROLL
4694 shift
= (term
->disptop
- olddisptop
);
4695 if (shift
< term
->rows
&& shift
> -term
->rows
)
4696 scroll_display(term
, 0, term
->rows
- 1, shift
);
4697 #endif /* OPTIMISE_SCROLL */
4701 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
, int desel
)
4704 wchar_t *wbptr
; /* where next char goes within workbuf */
4706 int wblen
= 0; /* workbuf len */
4707 int buflen
; /* amount of memory allocated to workbuf */
4709 buflen
= 5120; /* Default size */
4710 workbuf
= snewn(buflen
, wchar_t);
4711 wbptr
= workbuf
; /* start filling here */
4712 old_top_x
= top
.x
; /* needed for rect==1 */
4714 while (poslt(top
, bottom
)) {
4716 termline
*ldata
= lineptr(top
.y
);
4720 * nlpos will point at the maximum position on this line we
4721 * should copy up to. So we start it at the end of the
4725 nlpos
.x
= term
->cols
;
4728 * ... move it backwards if there's unused space at the end
4729 * of the line (and also set `nl' if this is the case,
4730 * because in normal selection mode this means we need a
4731 * newline at the end)...
4733 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
4734 while (IS_SPACE_CHR(ldata
->chars
[nlpos
.x
- 1].chr
) &&
4737 if (poslt(nlpos
, bottom
))
4739 } else if (ldata
->lattr
& LATTR_WRAPPED2
) {
4740 /* Ignore the last char on the line in a WRAPPED2 line. */
4745 * ... and then clip it to the terminal x coordinate if
4746 * we're doing rectangular selection. (In this case we
4747 * still did the above, so that copying e.g. the right-hand
4748 * column from a table doesn't fill with spaces on the
4752 if (nlpos
.x
> bottom
.x
)
4754 nl
= (top
.y
< bottom
.y
);
4757 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
4760 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
4762 wchar_t cbuf
[16], *p
;
4766 if (ldata
->chars
[x
].chr
== UCSWIDE
) {
4772 int uc
= ldata
->chars
[x
].chr
;
4774 switch (uc
& CSET_MASK
) {
4776 if (!term
->cfg
.rawcnp
) {
4777 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4781 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4784 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4787 switch (uc
& CSET_MASK
) {
4789 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4792 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4796 set
= (uc
& CSET_MASK
);
4797 c
= (uc
& ~CSET_MASK
);
4801 if (DIRECT_FONT(uc
)) {
4802 if (c
>= ' ' && c
!= 0x7F) {
4806 if (is_dbcs_leadbyte(term
->ucsdata
->font_codepage
, (BYTE
) c
)) {
4808 buf
[1] = (char) (0xFF & ldata
->chars
[top
.x
+ 1].chr
);
4809 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 2, wbuf
, 4);
4813 rv
= mb_to_wc(term
->ucsdata
->font_codepage
, 0, buf
, 1, wbuf
, 4);
4817 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
4824 for (p
= cbuf
; *p
; p
++) {
4825 /* Enough overhead for trailing NL and nul */
4826 if (wblen
>= buflen
- 16) {
4828 workbuf
= sresize(workbuf
, buflen
, wchar_t);
4829 wbptr
= workbuf
+ wblen
;
4835 if (ldata
->chars
[x
].cc_next
)
4836 x
+= ldata
->chars
[x
].cc_next
;
4844 for (i
= 0; i
< sel_nl_sz
; i
++) {
4846 *wbptr
++ = sel_nl
[i
];
4850 top
.x
= rect ? old_top_x
: 0;
4854 #if SELECTION_NUL_TERMINATED
4858 write_clip(term
->frontend
, workbuf
, wblen
, desel
); /* transfer to clipbd */
4859 if (buflen
> 0) /* indicates we allocated this buffer */
4863 void term_copyall(Terminal
*term
)
4867 tree234
*screen
= term
->screen
;
4868 top
.y
= -sblines(term
);
4870 bottom
.y
= find_last_nonempty_line(term
, screen
);
4871 bottom
.x
= term
->cols
;
4872 clipme(term
, top
, bottom
, 0, TRUE
);
4876 * The wordness array is mainly for deciding the disposition of the
4877 * US-ASCII characters.
4879 static int wordtype(Terminal
*term
, int uc
)
4882 int start
, end
, ctype
;
4884 static const struct ucsword ucs_words
[] = {
4890 0x037e, 0x037e, 1}, /* Greek question mark */
4892 0x0387, 0x0387, 1}, /* Greek ano teleia */
4894 0x055a, 0x055f, 1}, /* Armenian punctuation */
4896 0x0589, 0x0589, 1}, /* Armenian full stop */
4898 0x0700, 0x070d, 1}, /* Syriac punctuation */
4900 0x104a, 0x104f, 1}, /* Myanmar punctuation */
4902 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
4904 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
4906 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
4908 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
4910 0x1800, 0x180a, 1}, /* Mongolian punctuation */
4912 0x2000, 0x200a, 0}, /* Various spaces */
4914 0x2070, 0x207f, 2}, /* superscript */
4916 0x2080, 0x208f, 2}, /* subscript */
4918 0x200b, 0x27ff, 1}, /* punctuation and symbols */
4920 0x3000, 0x3000, 0}, /* ideographic space */
4922 0x3001, 0x3020, 1}, /* ideographic punctuation */
4924 0x303f, 0x309f, 3}, /* Hiragana */
4926 0x30a0, 0x30ff, 3}, /* Katakana */
4928 0x3300, 0x9fff, 3}, /* CJK Ideographs */
4930 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
4932 0xf900, 0xfaff, 3}, /* CJK Ideographs */
4934 0xfe30, 0xfe6b, 1}, /* punctuation forms */
4936 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
4938 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
4940 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
4942 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
4944 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
4948 const struct ucsword
*wptr
;
4950 switch (uc
& CSET_MASK
) {
4952 uc
= term
->ucsdata
->unitab_xterm
[uc
& 0xFF];
4955 uc
= term
->ucsdata
->unitab_line
[uc
& 0xFF];
4958 uc
= term
->ucsdata
->unitab_scoacs
[uc
&0xFF];
4961 switch (uc
& CSET_MASK
) {
4963 uc
= term
->ucsdata
->unitab_font
[uc
& 0xFF];
4966 uc
= term
->ucsdata
->unitab_oemcp
[uc
& 0xFF];
4970 /* For DBCS fonts I can't do anything useful. Even this will sometimes
4971 * fail as there's such a thing as a double width space. :-(
4973 if (term
->ucsdata
->dbcs_screenfont
&&
4974 term
->ucsdata
->font_codepage
== term
->ucsdata
->line_codepage
)
4978 return term
->wordness
[uc
];
4980 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
4981 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
4989 * Spread the selection outwards according to the selection mode.
4991 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
4995 int topy
= -sblines(term
);
4997 ldata
= lineptr(p
.y
);
4999 switch (term
->selmode
) {
5002 * In this mode, every character is a separate unit, except
5003 * for runs of spaces at the end of a non-wrapping line.
5005 if (!(ldata
->lattr
& LATTR_WRAPPED
)) {
5006 termchar
*q
= ldata
->chars
+ term
->cols
;
5007 while (q
> ldata
->chars
&& IS_SPACE_CHR(q
[-1].chr
))
5009 if (q
== ldata
->chars
+ term
->cols
)
5011 if (p
.x
>= q
- ldata
->chars
)
5012 p
.x
= (dir
== -1 ? q
- ldata
->chars
: term
->cols
- 1);
5017 * In this mode, the units are maximal runs of characters
5018 * whose `wordness' has the same value.
5020 wvalue
= wordtype(term
, UCSGET(ldata
->chars
, p
.x
));
5023 int maxcols
= (ldata
->lattr
& LATTR_WRAPPED2 ?
5024 term
->cols
-1 : term
->cols
);
5025 if (p
.x
< maxcols
-1) {
5026 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
+1)) == wvalue
)
5031 if (ldata
->lattr
& LATTR_WRAPPED
) {
5033 ldata2
= lineptr(p
.y
+1);
5034 if (wordtype(term
, UCSGET(ldata2
->chars
, 0))
5051 if (wordtype(term
, UCSGET(ldata
->chars
, p
.x
-1)) == wvalue
)
5060 ldata2
= lineptr(p
.y
-1);
5061 maxcols
= (ldata2
->lattr
& LATTR_WRAPPED2 ?
5062 term
->cols
-1 : term
->cols
);
5063 if (ldata2
->lattr
& LATTR_WRAPPED
) {
5064 if (wordtype(term
, UCSGET(ldata2
->chars
, maxcols
-1))
5082 * In this mode, every line is a unit.
5084 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
5092 static void sel_spread(Terminal
*term
)
5094 if (term
->seltype
== LEXICOGRAPHIC
) {
5095 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
5096 decpos(term
->selend
);
5097 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
5098 incpos(term
->selend
);
5102 void term_do_paste(Terminal
*term
)
5107 get_clip(term
->frontend
, &data
, &len
);
5108 if (data
&& len
> 0) {
5111 term_seen_key_event(term
); /* pasted data counts */
5113 if (term
->paste_buffer
)
5114 sfree(term
->paste_buffer
);
5115 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5116 term
->paste_buffer
= snewn(len
, wchar_t);
5119 while (p
< data
+ len
) {
5120 while (p
< data
+ len
&&
5121 !(p
<= data
+ len
- sel_nl_sz
&&
5122 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
5127 for (i
= 0; i
< p
- q
; i
++) {
5128 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
5132 if (p
<= data
+ len
- sel_nl_sz
&&
5133 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
5134 term
->paste_buffer
[term
->paste_len
++] = '\015';
5140 /* Assume a small paste will be OK in one go. */
5141 if (term
->paste_len
< 256) {
5143 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
5144 if (term
->paste_buffer
)
5145 sfree(term
->paste_buffer
);
5146 term
->paste_buffer
= 0;
5147 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
5150 get_clip(term
->frontend
, NULL
, NULL
);
5153 void term_mouse(Terminal
*term
, Mouse_Button braw
, Mouse_Button bcooked
,
5154 Mouse_Action a
, int x
, int y
, int shift
, int ctrl
, int alt
)
5158 int raw_mouse
= (term
->xterm_mouse
&&
5159 !term
->cfg
.no_mouse_rep
&&
5160 !(term
->cfg
.mouse_override
&& shift
));
5161 int default_seltype
;
5165 if (a
== MA_DRAG
&& !raw_mouse
)
5166 term_scroll(term
, 0, -1);
5168 if (y
>= term
->rows
) {
5170 if (a
== MA_DRAG
&& !raw_mouse
)
5171 term_scroll(term
, 0, +1);
5180 if (x
>= term
->cols
)
5183 selpoint
.y
= y
+ term
->disptop
;
5185 ldata
= lineptr(selpoint
.y
);
5186 if ((ldata
->lattr
& LATTR_MODE
) != LATTR_NORM
)
5191 int encstate
= 0, r
, c
;
5198 encstate
= 0x20; /* left button down */
5209 case MBT_WHEEL_DOWN
:
5212 default: break; /* placate gcc warning about enum use */
5216 if (term
->xterm_mouse
== 1)
5222 term
->mouse_is_down
= 0;
5225 if (term
->mouse_is_down
== braw
)
5227 term
->mouse_is_down
= braw
;
5229 default: break; /* placate gcc warning about enum use */
5238 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
5239 ldisc_send(term
->ldisc
, abuf
, 6, 0);
5245 * Set the selection type (rectangular or normal) at the start
5246 * of a selection attempt, from the state of Alt.
5248 if (!alt
^ !term
->cfg
.rect_select
)
5249 default_seltype
= RECTANGULAR
;
5251 default_seltype
= LEXICOGRAPHIC
;
5253 if (term
->selstate
== NO_SELECTION
) {
5254 term
->seltype
= default_seltype
;
5257 if (bcooked
== MBT_SELECT
&& a
== MA_CLICK
) {
5259 term
->selstate
= ABOUT_TO
;
5260 term
->seltype
= default_seltype
;
5261 term
->selanchor
= selpoint
;
5262 term
->selmode
= SM_CHAR
;
5263 } else if (bcooked
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
5265 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
5266 term
->selstate
= DRAGGING
;
5267 term
->selstart
= term
->selanchor
= selpoint
;
5268 term
->selend
= term
->selstart
;
5269 incpos(term
->selend
);
5271 } else if ((bcooked
== MBT_SELECT
&& a
== MA_DRAG
) ||
5272 (bcooked
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
5273 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
5275 if (bcooked
== MBT_EXTEND
&& a
!= MA_DRAG
&&
5276 term
->selstate
== SELECTED
) {
5277 if (term
->seltype
== LEXICOGRAPHIC
) {
5279 * For normal selection, we extend by moving
5280 * whichever end of the current selection is closer
5283 if (posdiff(selpoint
, term
->selstart
) <
5284 posdiff(term
->selend
, term
->selstart
) / 2) {
5285 term
->selanchor
= term
->selend
;
5286 decpos(term
->selanchor
);
5288 term
->selanchor
= term
->selstart
;
5292 * For rectangular selection, we have a choice of
5293 * _four_ places to put selanchor and selpoint: the
5294 * four corners of the selection.
5296 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
5297 term
->selanchor
.x
= term
->selend
.x
-1;
5299 term
->selanchor
.x
= term
->selstart
.x
;
5301 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
5302 term
->selanchor
.y
= term
->selend
.y
;
5304 term
->selanchor
.y
= term
->selstart
.y
;
5306 term
->selstate
= DRAGGING
;
5308 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
5309 term
->selanchor
= selpoint
;
5310 term
->selstate
= DRAGGING
;
5311 if (term
->seltype
== LEXICOGRAPHIC
) {
5313 * For normal selection, we set (selstart,selend) to
5314 * (selpoint,selanchor) in some order.
5316 if (poslt(selpoint
, term
->selanchor
)) {
5317 term
->selstart
= selpoint
;
5318 term
->selend
= term
->selanchor
;
5319 incpos(term
->selend
);
5321 term
->selstart
= term
->selanchor
;
5322 term
->selend
= selpoint
;
5323 incpos(term
->selend
);
5327 * For rectangular selection, we may need to
5328 * interchange x and y coordinates (if the user has
5329 * dragged in the -x and +y directions, or vice versa).
5331 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
5332 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
5333 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
5334 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
5337 } else if ((bcooked
== MBT_SELECT
|| bcooked
== MBT_EXTEND
) &&
5339 if (term
->selstate
== DRAGGING
) {
5341 * We've completed a selection. We now transfer the
5342 * data to the clipboard.
5344 clipme(term
, term
->selstart
, term
->selend
,
5345 (term
->seltype
== RECTANGULAR
), FALSE
);
5346 term
->selstate
= SELECTED
;
5348 term
->selstate
= NO_SELECTION
;
5349 } else if (bcooked
== MBT_PASTE
5351 #if MULTICLICK_ONLY_EVENT
5352 || a
== MA_2CLK
|| a
== MA_3CLK
5355 request_paste(term
->frontend
);
5361 void term_key(Terminal
*term
, Key_Sym keysym
, wchar_t *text
, size_t tlen
,
5362 unsigned int modifiers
, unsigned int flags
)
5366 int prependesc
= FALSE
;
5370 fprintf(stderr
, "keysym = %d, %d chars:", keysym
, tlen
);
5371 for (i
= 0; i
< tlen
; i
++)
5372 fprintf(stderr
, " %04x", (unsigned)text
[i
]);
5373 fprintf(stderr
, "\n");
5377 if ((flags
& PKF_REPEAT
) && term
->repeat_off
)
5380 /* Currently, Meta always just prefixes everything with ESC. */
5381 if (modifiers
& PKM_META
)
5383 modifiers
&= ~PKM_META
;
5386 * Alt is only used for Alt+keypad, which isn't supported yet, so
5389 modifiers
&= ~PKM_ALT
;
5391 /* Standard local function keys */
5392 switch (modifiers
& (PKM_SHIFT
| PKM_CONTROL
)) {
5394 if (keysym
== PK_PAGEUP
)
5395 /* scroll up one page */;
5396 if (keysym
== PK_PAGEDOWN
)
5397 /* scroll down on page */;
5398 if (keysym
== PK_INSERT
)
5399 term_do_paste(term
);
5402 if (keysym
== PK_PAGEUP
)
5403 /* scroll up one line */;
5404 if (keysym
== PK_PAGEDOWN
)
5405 /* scroll down one line */;
5406 /* Control-Numlock for app-keypad mode switch */
5407 if (keysym
== PK_PF1
)
5408 term
->app_keypad_keys
^= 1;
5412 if (modifiers
& PKM_ALT
) {
5413 /* Alt+F4 (close) */
5414 /* Alt+Return (full screen) */
5415 /* Alt+Space (system menu) */
5418 if (keysym
== PK_NULL
&& (modifiers
& PKM_CONTROL
) && tlen
== 1 &&
5419 text
[0] >= 0x20 && text
[0] <= 0x7e) {
5420 /* ASCII chars + Control */
5421 if ((text
[0] >= 0x40 && text
[0] <= 0x5f) ||
5422 (text
[0] >= 0x61 && text
[0] <= 0x7a))
5426 * Control-2 should return ^@ (0x00), Control-6 should return
5427 * ^^ (0x1E), and Control-Minus should return ^_ (0x1F). Since
5428 * the DOS keyboard handling did it, and we have nothing better
5429 * to do with the key combo in question, we'll also map
5430 * Control-Backquote to ^\ (0x1C).
5433 case ' ': text
[0] = 0x00; break;
5434 case '-': text
[0] = 0x1f; break;
5435 case '/': text
[0] = 0x1f; break;
5436 case '2': text
[0] = 0x00; break;
5437 case '3': text
[0] = 0x1b; break;
5438 case '4': text
[0] = 0x1c; break;
5439 case '5': text
[0] = 0x1d; break;
5440 case '6': text
[0] = 0x1e; break;
5441 case '7': text
[0] = 0x1f; break;
5442 case '8': text
[0] = 0x7f; break;
5443 case '`': text
[0] = 0x1c; break;
5448 /* Nethack keypad */
5449 if (term
->cfg
.nethack_keypad
) {
5452 case PK_KP1
: c
= 'b'; break;
5453 case PK_KP2
: c
= 'j'; break;
5454 case PK_KP3
: c
= 'n'; break;
5455 case PK_KP4
: c
= 'h'; break;
5456 case PK_KP5
: c
= '.'; break;
5457 case PK_KP6
: c
= 'l'; break;
5458 case PK_KP7
: c
= 'y'; break;
5459 case PK_KP8
: c
= 'k'; break;
5460 case PK_KP9
: c
= 'u'; break;
5461 default: break; /* else gcc warns `enum value not used' */
5465 if (modifiers
& PKM_CONTROL
)
5467 else if (modifiers
& PKM_SHIFT
)
5475 /* Numeric Keypad */
5476 if (PK_ISKEYPAD(keysym
)) {
5480 * In VT400 mode, PFn always emits an escape sequence. In
5481 * Linux and tilde modes, this only happens in app keypad mode.
5483 if (term
->cfg
.funky_type
== FUNKY_VT400
||
5484 ((term
->cfg
.funky_type
== FUNKY_LINUX
||
5485 term
->cfg
.funky_type
== FUNKY_TILDE
) &&
5486 term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
)) {
5488 case PK_PF1
: xkey
= 'P'; break;
5489 case PK_PF2
: xkey
= 'Q'; break;
5490 case PK_PF3
: xkey
= 'R'; break;
5491 case PK_PF4
: xkey
= 'S'; break;
5492 default: break; /* else gcc warns `enum value not used' */
5495 if (term
->app_keypad_keys
&& !term
->cfg
.no_applic_k
) {
5497 case PK_KP0
: xkey
= 'p'; break;
5498 case PK_KP1
: xkey
= 'q'; break;
5499 case PK_KP2
: xkey
= 'r'; break;
5500 case PK_KP3
: xkey
= 's'; break;
5501 case PK_KP4
: xkey
= 't'; break;
5502 case PK_KP5
: xkey
= 'u'; break;
5503 case PK_KP6
: xkey
= 'v'; break;
5504 case PK_KP7
: xkey
= 'w'; break;
5505 case PK_KP8
: xkey
= 'x'; break;
5506 case PK_KP9
: xkey
= 'y'; break;
5507 case PK_KPDECIMAL
: xkey
= 'n'; break;
5508 case PK_KPENTER
: xkey
= 'M'; break;
5509 default: break; /* else gcc warns `enum value not used' */
5511 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& tlen
> 0) {
5513 * xterm can't see the layout of the keypad, so it has
5514 * to rely on the X keysyms returned by the keys.
5515 * Hence, we look at the strings here, not the PuTTY
5516 * keysyms (which describe the layout).
5520 if (modifiers
& PKM_SHIFT
)
5525 case '/': xkey
= 'o'; break;
5526 case '*': xkey
= 'j'; break;
5527 case '-': xkey
= 'm'; break;
5531 * In all other modes, we try to retain the layout of
5532 * the DEC keypad in application mode.
5536 /* This key covers the '-' and ',' keys on a VT220 */
5537 if (modifiers
& PKM_SHIFT
)
5538 xkey
= 'm'; /* VT220 '-' */
5540 xkey
= 'l'; /* VT220 ',' */
5542 case PK_KPMINUS
: xkey
= 'm'; break;
5543 case PK_KPCOMMA
: xkey
= 'l'; break;
5544 default: break; /* else gcc warns `enum value not used' */
5549 if (term
->vt52_mode
) {
5550 if (xkey
>= 'P' && xkey
<= 'S')
5551 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5553 p
+= sprintf((char *) p
, "\x1B?%c", xkey
);
5555 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5558 /* Not in application mode -- treat the number pad as arrow keys? */
5559 if ((flags
& PKF_NUMLOCK
) == 0) {
5561 case PK_KP0
: keysym
= PK_INSERT
; break;
5562 case PK_KP1
: keysym
= PK_END
; break;
5563 case PK_KP2
: keysym
= PK_DOWN
; break;
5564 case PK_KP3
: keysym
= PK_PAGEDOWN
; break;
5565 case PK_KP4
: keysym
= PK_LEFT
; break;
5566 case PK_KP5
: keysym
= PK_REST
; break;
5567 case PK_KP6
: keysym
= PK_RIGHT
; break;
5568 case PK_KP7
: keysym
= PK_HOME
; break;
5569 case PK_KP8
: keysym
= PK_UP
; break;
5570 case PK_KP9
: keysym
= PK_PAGEUP
; break;
5571 default: break; /* else gcc warns `enum value not used' */
5576 /* Miscellaneous keys */
5583 *p
++ = (term
->cfg
.bksp_is_delete ?
0x7F : 0x08);
5584 else if (modifiers
== PKM_SHIFT
)
5585 /* We do the opposite of what is configured */
5586 *p
++ = (term
->cfg
.bksp_is_delete ?
0x08 : 0x7F);
5592 else if (modifiers
== PKM_SHIFT
)
5593 *p
++ = 0x1B, *p
++ = '[', *p
++ = 'Z';
5596 /* XXX window.c has ctrl+shift+space sending 0xa0 */
5598 if (modifiers
== PKM_CONTROL
)
5603 case PK_KPENTER
: /* Odd keypad modes handled above */
5604 if (modifiers
== 0) {
5606 if (term
->cr_lf_return
)
5610 default: break; /* else gcc warns `enum value not used' */
5613 /* SCO function keys and editing keys */
5614 if (term
->cfg
.funky_type
== FUNKY_SCO
) {
5615 if (PK_ISFKEY(keysym
) && keysym
<= PK_F12
) {
5616 static char const codes
[] =
5617 "MNOPQRSTUVWX" "YZabcdefghij" "klmnopqrstuv" "wxyz@[\\]^_`{";
5618 int index
= keysym
- PK_F1
;
5620 if (modifiers
& PKM_SHIFT
) index
+= 12;
5621 if (modifiers
& PKM_CONTROL
) index
+= 24;
5622 p
+= sprintf((char *) p
, "\x1B[%c", codes
[index
]);
5625 if (PK_ISEDITING(keysym
)) {
5629 case PK_DELETE
: *p
++ = 0x7f; goto done
;
5630 case PK_HOME
: xkey
= 'H'; break;
5631 case PK_INSERT
: xkey
= 'L'; break;
5632 case PK_END
: xkey
= 'F'; break;
5633 case PK_PAGEUP
: xkey
= 'I'; break;
5634 case PK_PAGEDOWN
: xkey
= 'G'; break;
5635 default: break; /* else gcc warns `enum value not used' */
5637 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5641 if (PK_ISEDITING(keysym
) && (modifiers
& PKM_SHIFT
) == 0) {
5644 if (term
->cfg
.funky_type
== FUNKY_XTERM
) {
5645 /* Xterm shuffles these keys, apparently. */
5647 case PK_HOME
: keysym
= PK_INSERT
; break;
5648 case PK_INSERT
: keysym
= PK_HOME
; break;
5649 case PK_DELETE
: keysym
= PK_END
; break;
5650 case PK_END
: keysym
= PK_PAGEUP
; break;
5651 case PK_PAGEUP
: keysym
= PK_DELETE
; break;
5652 case PK_PAGEDOWN
: keysym
= PK_PAGEDOWN
; break;
5653 default: break; /* else gcc warns `enum value not used' */
5658 if (term
->cfg
.rxvt_homeend
&&
5659 (keysym
== PK_HOME
|| keysym
== PK_END
)) {
5660 p
+= sprintf((char *) p
, keysym
== PK_HOME ?
"\x1B[H" : "\x1BOw");
5664 if (term
->vt52_mode
) {
5668 * A real VT52 doesn't have these, and a VT220 doesn't
5669 * send anything for them in VT52 mode.
5672 case PK_HOME
: xkey
= 'H'; break;
5673 case PK_INSERT
: xkey
= 'L'; break;
5674 case PK_DELETE
: xkey
= 'M'; break;
5675 case PK_END
: xkey
= 'E'; break;
5676 case PK_PAGEUP
: xkey
= 'I'; break;
5677 case PK_PAGEDOWN
: xkey
= 'G'; break;
5678 default: xkey
=0; break; /* else gcc warns `enum value not used'*/
5680 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5685 case PK_HOME
: code
= 1; break;
5686 case PK_INSERT
: code
= 2; break;
5687 case PK_DELETE
: code
= 3; break;
5688 case PK_END
: code
= 4; break;
5689 case PK_PAGEUP
: code
= 5; break;
5690 case PK_PAGEDOWN
: code
= 6; break;
5691 default: code
= 0; break; /* else gcc warns `enum value not used' */
5693 p
+= sprintf((char *) p
, "\x1B[%d~", code
);
5697 if (PK_ISFKEY(keysym
)) {
5698 /* Map Shift+F1-F10 to F11-F20 */
5699 if (keysym
>= PK_F1
&& keysym
<= PK_F10
&& (modifiers
& PKM_SHIFT
))
5701 if ((term
->vt52_mode
|| term
->cfg
.funky_type
== FUNKY_VT100P
) &&
5703 /* XXX This overrides the XTERM/VT52 mode below */
5705 if (keysym
>= PK_F6
) offt
++;
5706 if (keysym
>= PK_F12
) offt
++;
5707 p
+= sprintf((char *) p
, term
->vt52_mode ?
"\x1B%c" : "\x1BO%c",
5708 'P' + keysym
- PK_F1
- offt
);
5711 if (term
->cfg
.funky_type
== FUNKY_LINUX
&& keysym
<= PK_F5
) {
5712 p
+= sprintf((char *) p
, "\x1B[[%c", 'A' + keysym
- PK_F1
);
5715 if (term
->cfg
.funky_type
== FUNKY_XTERM
&& keysym
<= PK_F4
) {
5716 if (term
->vt52_mode
)
5717 p
+= sprintf((char *) p
, "\x1B%c", 'P' + keysym
- PK_F1
);
5719 p
+= sprintf((char *) p
, "\x1BO%c", 'P' + keysym
- PK_F1
);
5722 p
+= sprintf((char *) p
, "\x1B[%d~", 11 + keysym
- PK_F1
);
5726 if (PK_ISCURSOR(keysym
)) {
5730 case PK_UP
: xkey
= 'A'; break;
5731 case PK_DOWN
: xkey
= 'B'; break;
5732 case PK_RIGHT
: xkey
= 'C'; break;
5733 case PK_LEFT
: xkey
= 'D'; break;
5734 case PK_REST
: xkey
= 'G'; break; /* centre key on number pad */
5735 default: xkey
= 0; break; /* else gcc warns `enum value not used' */
5737 if (term
->vt52_mode
)
5738 p
+= sprintf((char *) p
, "\x1B%c", xkey
);
5740 int app_flg
= (term
->app_cursor_keys
&& !term
->cfg
.no_applic_c
);
5742 /* Useful mapping of Ctrl-arrows */
5743 if (modifiers
== PKM_CONTROL
)
5747 p
+= sprintf((char *) p
, "\x1BO%c", xkey
);
5749 p
+= sprintf((char *) p
, "\x1B[%c", xkey
);
5755 if (p
> output
|| tlen
> 0) {
5757 * Interrupt an ongoing paste. I'm not sure
5758 * this is sensible, but for the moment it's
5759 * preferable to having to faff about buffering
5765 * We need not bother about stdin backlogs
5766 * here, because in GUI PuTTY we can't do
5767 * anything about it anyway; there's no means
5768 * of asking Windows to hold off on KEYDOWN
5769 * messages. We _have_ to buffer everything
5772 term_seen_key_event(term
);
5776 fprintf(stderr
, "sending ESC\n");
5778 ldisc_send(term
->ldisc
, "\x1b", 1, 1);
5783 fprintf(stderr
, "sending %d bytes:", p
- output
);
5784 for (i
= 0; i
< p
- output
; i
++)
5785 fprintf(stderr
, " %02x", output
[i
]);
5786 fprintf(stderr
, "\n");
5788 ldisc_send(term
->ldisc
, output
, p
- output
, 1);
5789 } else if (tlen
> 0) {
5791 fprintf(stderr
, "sending %d unichars:", tlen
);
5792 for (i
= 0; i
< tlen
; i
++)
5793 fprintf(stderr
, " %04x", (unsigned) text
[i
]);
5794 fprintf(stderr
, "\n");
5796 luni_send(term
->ldisc
, text
, tlen
, 1);
5801 void term_nopaste(Terminal
*term
)
5803 if (term
->paste_len
== 0)
5805 sfree(term
->paste_buffer
);
5806 term
->paste_buffer
= NULL
;
5807 term
->paste_len
= 0;
5810 int term_paste_pending(Terminal
*term
)
5812 return term
->paste_len
!= 0;
5815 void term_paste(Terminal
*term
)
5817 long now
, paste_diff
;
5819 if (term
->paste_len
== 0)
5822 /* Don't wait forever to paste */
5823 if (term
->paste_hold
) {
5824 now
= GETTICKCOUNT();
5825 paste_diff
= now
- term
->last_paste
;
5826 if (paste_diff
>= 0 && paste_diff
< 450)
5829 term
->paste_hold
= 0;
5831 while (term
->paste_pos
< term
->paste_len
) {
5833 while (n
+ term
->paste_pos
< term
->paste_len
) {
5834 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\015')
5838 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
5839 term
->paste_pos
+= n
;
5841 if (term
->paste_pos
< term
->paste_len
) {
5842 term
->paste_hold
= 1;
5846 sfree(term
->paste_buffer
);
5847 term
->paste_buffer
= NULL
;
5848 term
->paste_len
= 0;
5851 static void deselect(Terminal
*term
)
5853 term
->selstate
= NO_SELECTION
;
5854 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
5857 void term_deselect(Terminal
*term
)
5863 int term_ldisc(Terminal
*term
, int option
)
5865 if (option
== LD_ECHO
)
5866 return term
->term_echoing
;
5867 if (option
== LD_EDIT
)
5868 return term
->term_editing
;
5872 int term_data(Terminal
*term
, int is_stderr
, const char *data
, int len
)
5874 bufchain_add(&term
->inbuf
, data
, len
);
5876 if (!term
->in_term_out
) {
5877 term
->in_term_out
= TRUE
;
5878 term_blink(term
, 1);
5880 term
->in_term_out
= FALSE
;
5884 * term_out() always completely empties inbuf. Therefore,
5885 * there's no reason at all to return anything other than zero
5886 * from this function, because there _can't_ be a question of
5887 * the remote side needing to wait until term_out() has cleared
5890 * This is a slightly suboptimal way to deal with SSH2 - in
5891 * principle, the window mechanism would allow us to continue
5892 * to accept data on forwarded ports and X connections even
5893 * while the terminal processing was going slowly - but we
5894 * can't do the 100% right thing without moving the terminal
5895 * processing into a separate thread, and that might hurt
5896 * portability. So we manage stdout buffering the old SSH1 way:
5897 * if the terminal processing goes slowly, the whole SSH
5898 * connection stops accepting data until it's ready.
5900 * In practice, I can't imagine this causing serious trouble.
5905 void term_provide_logctx(Terminal
*term
, void *logctx
)
5907 term
->logctx
= logctx
;