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 #define sel_nl_sz (sizeof(sel_nl)/sizeof(wchar_t))
60 const wchar_t sel_nl
[] = SEL_NL
;
63 * Internal prototypes.
65 static void do_paint(Terminal
*, Context
, int);
66 static void erase_lots(Terminal
*, int, int, int);
67 static void swap_screen(Terminal
*, int, int, int);
68 static void update_sbar(Terminal
*);
69 static void deselect(Terminal
*);
70 static void term_print_finish(Terminal
*);
73 * Resize a line to make it `cols' columns wide.
75 unsigned long *resizeline(unsigned long *line
, int cols
)
78 unsigned long lineattrs
;
80 if (line
[0] != (unsigned long)cols
) {
82 * This line is the wrong length, which probably means it
83 * hasn't been accessed since a resize. Resize it now.
86 lineattrs
= line
[oldlen
+ 1];
87 line
= srealloc(line
, TSIZE
* (2 + cols
));
89 for (i
= oldlen
; i
< cols
; i
++)
90 line
[i
+ 1] = ERASE_CHAR
;
91 line
[cols
+ 1] = lineattrs
& LATTR_MODE
;
98 * Retrieve a line of the screen or of the scrollback, according to
99 * whether the y coordinate is non-negative or negative
102 unsigned long *lineptr(Terminal
*term
, int y
, int lineno
)
104 unsigned long *line
, *newline
;
109 whichtree
= term
->screen
;
112 whichtree
= term
->scrollback
;
113 treeindex
= y
+ count234(term
->scrollback
);
115 line
= index234(whichtree
, treeindex
);
117 /* We assume that we don't screw up and retrieve something out of range. */
118 assert(line
!= NULL
);
120 newline
= resizeline(line
, term
->cols
);
121 if (newline
!= line
) {
122 delpos234(whichtree
, treeindex
);
123 addpos234(whichtree
, newline
, treeindex
);
130 #define lineptr(x) lineptr(term,x,__LINE__)
133 * Set up power-on settings for the terminal.
135 static void power_on(Terminal
*term
)
137 term
->curs
.x
= term
->curs
.y
= 0;
138 term
->alt_x
= term
->alt_y
= 0;
139 term
->savecurs
.x
= term
->savecurs
.y
= 0;
140 term
->alt_t
= term
->marg_t
= 0;
141 if (term
->rows
!= -1)
142 term
->alt_b
= term
->marg_b
= term
->rows
- 1;
144 term
->alt_b
= term
->marg_b
= 0;
145 if (term
->cols
!= -1) {
147 for (i
= 0; i
< term
->cols
; i
++)
148 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
150 term
->alt_om
= term
->dec_om
= cfg
.dec_om
;
151 term
->alt_wnext
= term
->wrapnext
= term
->alt_ins
= term
->insert
= FALSE
;
152 term
->alt_wrap
= term
->wrap
= cfg
.wrap_mode
;
153 term
->alt_cset
= term
->cset
= 0;
154 term
->alt_utf
= term
->utf
= 0;
155 term
->alt_sco_acs
= term
->sco_acs
= 0;
156 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
158 term
->in_vbell
= FALSE
;
160 term
->big_cursor
= 0;
161 term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
162 term
->term_editing
= term
->term_echoing
= FALSE
;
163 term
->app_cursor_keys
= cfg
.app_cursor
;
164 term
->app_keypad_keys
= cfg
.app_keypad
;
165 term
->use_bce
= cfg
.bce
;
166 term
->blink_is_real
= cfg
.blinktext
;
167 term
->erase_char
= ERASE_CHAR
;
169 term_print_finish(term
);
172 for (i
= 0; i
< 256; i
++)
173 term
->wordness
[i
] = cfg
.wordness
[i
];
176 swap_screen(term
, 1, FALSE
, FALSE
);
177 erase_lots(term
, FALSE
, TRUE
, TRUE
);
178 swap_screen(term
, 0, FALSE
, FALSE
);
179 erase_lots(term
, FALSE
, TRUE
, TRUE
);
184 * Force a screen update.
186 void term_update(Terminal
*term
)
191 int need_sbar_update
= term
->seen_disp_event
;
192 if (term
->seen_disp_event
&& cfg
.scroll_on_disp
) {
193 term
->disptop
= 0; /* return to main screen */
194 term
->seen_disp_event
= 0;
195 need_sbar_update
= TRUE
;
197 if (need_sbar_update
)
199 do_paint(term
, ctx
, TRUE
);
200 sys_cursor(term
->curs
.x
, term
->curs
.y
- term
->disptop
);
206 * Called from front end when a keypress occurs, to trigger
207 * anything magical that needs to happen in that situation.
209 void term_seen_key_event(Terminal
*term
)
212 * On any keypress, clear the bell overload mechanism
213 * completely, on the grounds that large numbers of
214 * beeps coming from deliberate key action are likely
215 * to be intended (e.g. beeps from filename completion
216 * blocking repeatedly).
218 term
->beep_overloaded
= FALSE
;
219 while (term
->beephead
) {
220 struct beeptime
*tmp
= term
->beephead
;
221 term
->beephead
= tmp
->next
;
224 term
->beeptail
= NULL
;
228 * Reset the scrollback on keypress, if we're doing that.
230 if (cfg
.scroll_on_key
) {
231 term
->disptop
= 0; /* return to main screen */
232 term
->seen_disp_event
= 1;
237 * Same as power_on(), but an external function.
239 void term_pwron(Terminal
*term
)
242 ldisc_send(NULL
, 0, 0); /* cause ldisc to notice changes */
250 * When the user reconfigures us, we need to check the forbidden-
251 * alternate-screen config option, disable raw mouse mode if the
252 * user has disabled mouse reporting, and abandon a print job if
253 * the user has disabled printing.
255 void term_reconfig(Terminal
*term
)
257 if (cfg
.no_alt_screen
)
258 swap_screen(term
, 0, FALSE
, FALSE
);
259 if (cfg
.no_mouse_rep
) {
260 term
->xterm_mouse
= 0;
261 set_raw_mouse_mode(0);
263 if (cfg
.no_remote_charset
) {
264 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
265 term
->sco_acs
= term
->alt_sco_acs
= 0;
269 term_print_finish(term
);
274 * Clear the scrollback.
276 void term_clrsb(Terminal
*term
)
280 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
287 * Initialise the terminal.
289 Terminal
*term_init(void)
294 * Allocate a new Terminal structure and initialise the fields
297 term
= smalloc(sizeof(Terminal
));
298 term
->compatibility_level
= TM_PUTTY
;
299 strcpy(term
->id_string
, "\033[?6c");
300 term
->last_blink
= term
->last_tblink
= 0;
301 term
->paste_buffer
= NULL
;
302 term
->last_paste
= 0;
303 bufchain_init(&term
->inbuf
);
304 bufchain_init(&term
->printer_buf
);
306 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
308 term
->disptext
= term
->dispcurs
= NULL
;
311 term
->rows
= term
->cols
= -1;
313 term
->beephead
= term
->beeptail
= NULL
;
315 term
->lastbeep
= FALSE
;
316 term
->beep_overloaded
= FALSE
;
322 * Set up the terminal for a given size.
324 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
327 unsigned long *newdisp
, *line
;
330 int save_alt_which
= term
->alt_which
;
332 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
333 newsavelines
== term
->savelines
)
334 return; /* nothing to do */
337 swap_screen(term
, 0, FALSE
, FALSE
);
339 term
->alt_t
= term
->marg_t
= 0;
340 term
->alt_b
= term
->marg_b
= newrows
- 1;
342 if (term
->rows
== -1) {
343 term
->scrollback
= newtree234(NULL
);
344 term
->screen
= newtree234(NULL
);
349 * Resize the screen and scrollback. We only need to shift
350 * lines around within our data structures, because lineptr()
351 * will take care of resizing each individual line if
354 * - If the new screen and the old screen differ in length, we
355 * must shunt some lines in from the scrollback or out to
358 * - If doing that fails to provide us with enough material to
359 * fill the new screen (i.e. the number of rows needed in
360 * the new screen exceeds the total number in the previous
361 * screen+scrollback), we must invent some blank lines to
364 * - Then, if the new scrollback length is less than the
365 * amount of scrollback we actually have, we must throw some
368 sblen
= count234(term
->scrollback
);
369 /* Do this loop to expand the screen if newrows > rows */
370 for (i
= term
->rows
; i
< newrows
; i
++) {
372 line
= delpos234(term
->scrollback
, --sblen
);
374 line
= smalloc(TSIZE
* (newcols
+ 2));
376 for (j
= 0; j
<= newcols
; j
++)
377 line
[j
+ 1] = ERASE_CHAR
;
379 addpos234(term
->screen
, line
, 0);
381 /* Do this loop to shrink the screen if newrows < rows */
382 for (i
= newrows
; i
< term
->rows
; i
++) {
383 line
= delpos234(term
->screen
, 0);
384 addpos234(term
->scrollback
, line
, sblen
++);
386 assert(count234(term
->screen
) == newrows
);
387 while (sblen
> newsavelines
) {
388 line
= delpos234(term
->scrollback
, 0);
392 assert(count234(term
->scrollback
) <= newsavelines
);
395 newdisp
= smalloc(newrows
* (newcols
+ 1) * TSIZE
);
396 for (i
= 0; i
< newrows
* (newcols
+ 1); i
++)
397 newdisp
[i
] = ATTR_INVALID
;
398 sfree(term
->disptext
);
399 term
->disptext
= newdisp
;
400 term
->dispcurs
= NULL
;
402 newalt
= newtree234(NULL
);
403 for (i
= 0; i
< newrows
; i
++) {
404 line
= smalloc(TSIZE
* (newcols
+ 2));
406 for (j
= 0; j
<= newcols
; j
++)
407 line
[j
+ 1] = term
->erase_char
;
408 addpos234(newalt
, line
, i
);
410 if (term
->alt_screen
) {
411 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
413 freetree234(term
->alt_screen
);
415 term
->alt_screen
= newalt
;
417 term
->tabs
= srealloc(term
->tabs
, newcols
* sizeof(*term
->tabs
));
420 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
421 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
425 term
->curs
.y
+= newrows
- term
->rows
;
426 if (term
->curs
.y
< 0)
428 if (term
->curs
.y
>= newrows
)
429 term
->curs
.y
= newrows
- 1;
430 if (term
->curs
.x
>= newcols
)
431 term
->curs
.x
= newcols
- 1;
432 term
->alt_x
= term
->alt_y
= 0;
433 term
->wrapnext
= term
->alt_wnext
= FALSE
;
435 term
->rows
= newrows
;
436 term
->cols
= newcols
;
437 term
->savelines
= newsavelines
;
440 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
448 * Swap screens. If `reset' is TRUE and we have been asked to
449 * switch to the alternate screen, we must bring most of its
450 * configuration from the main screen and erase the contents of the
451 * alternate screen completely. (This is even true if we're already
452 * on it! Blame xterm.)
454 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
460 reset
= FALSE
; /* do no weird resetting if which==0 */
462 if (which
!= term
->alt_which
) {
463 term
->alt_which
= which
;
465 ttr
= term
->alt_screen
;
466 term
->alt_screen
= term
->screen
;
469 if (!reset
&& !keep_cur_pos
)
470 term
->curs
.x
= term
->alt_x
;
473 if (!reset
&& !keep_cur_pos
)
474 term
->curs
.y
= term
->alt_y
;
477 if (!reset
) term
->marg_t
= term
->alt_t
;
480 if (!reset
) term
->marg_b
= term
->alt_b
;
483 if (!reset
) term
->dec_om
= term
->alt_om
;
486 if (!reset
) term
->wrap
= term
->alt_wrap
;
489 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
492 if (!reset
) term
->insert
= term
->alt_ins
;
495 if (!reset
) term
->cset
= term
->alt_cset
;
498 if (!reset
) term
->utf
= term
->alt_utf
;
501 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
502 term
->alt_sco_acs
= t
;
505 if (reset
&& term
->screen
) {
507 * Yes, this _is_ supposed to honour background-colour-erase.
509 erase_lots(term
, FALSE
, TRUE
, TRUE
);
513 * This might not be possible if we're called during
521 * Update the scroll bar.
523 static void update_sbar(Terminal
*term
)
527 nscroll
= count234(term
->scrollback
);
529 set_sbar(nscroll
+ term
->rows
, nscroll
+ term
->disptop
, term
->rows
);
533 * Check whether the region bounded by the two pointers intersects
534 * the scroll region, and de-select the on-screen selection if so.
536 static void check_selection(Terminal
*term
, pos from
, pos to
)
538 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
543 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
544 * for backward.) `sb' is TRUE if the scrolling is permitted to
545 * affect the scrollback buffer.
547 * NB this function invalidates all pointers into lines of the
548 * screen data structures. In particular, you MUST call fix_cpos
549 * after calling scroll() and before doing anything else that
550 * uses the cpos shortcut pointer.
552 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
554 unsigned long *line
, *line2
;
557 if (topline
!= 0 || term
->alt_which
!= 0)
562 line
= delpos234(term
->screen
, botline
);
563 line
= resizeline(line
, term
->cols
);
564 for (i
= 0; i
< term
->cols
; i
++)
565 line
[i
+ 1] = term
->erase_char
;
566 line
[term
->cols
+ 1] = 0;
567 addpos234(term
->screen
, line
, topline
);
569 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
571 if (term
->selstart
.y
> botline
) {
572 term
->selstart
.y
= botline
;
573 term
->selstart
.x
= 0;
576 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
578 if (term
->selend
.y
> botline
) {
579 term
->selend
.y
= botline
;
588 line
= delpos234(term
->screen
, topline
);
589 if (sb
&& term
->savelines
> 0) {
590 int sblen
= count234(term
->scrollback
);
592 * We must add this line to the scrollback. We'll
593 * remove a line from the top of the scrollback to
594 * replace it, or allocate a new one if the
595 * scrollback isn't full.
597 if (sblen
== term
->savelines
) {
598 sblen
--, line2
= delpos234(term
->scrollback
, 0);
600 line2
= smalloc(TSIZE
* (term
->cols
+ 2));
601 line2
[0] = term
->cols
;
603 addpos234(term
->scrollback
, line
, sblen
);
607 * If the user is currently looking at part of the
608 * scrollback, and they haven't enabled any options
609 * that are going to reset the scrollback as a
610 * result of this movement, then the chances are
611 * they'd like to keep looking at the same line. So
612 * we move their viewpoint at the same rate as the
613 * scroll, at least until their viewpoint hits the
614 * top end of the scrollback buffer, at which point
615 * we don't have the choice any more.
617 * Thanks to Jan Holmen Holsten for the idea and
618 * initial implementation.
620 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
623 line
= resizeline(line
, term
->cols
);
624 for (i
= 0; i
< term
->cols
; i
++)
625 line
[i
+ 1] = term
->erase_char
;
626 line
[term
->cols
+ 1] = 0;
627 addpos234(term
->screen
, line
, botline
);
630 * If the selection endpoints move into the scrollback,
631 * we keep them moving until they hit the top. However,
632 * of course, if the line _hasn't_ moved into the
633 * scrollback then we don't do this, and cut them off
634 * at the top of the scroll region.
636 * This applies to selstart and selend (for an existing
637 * selection), and also selanchor (for one being
638 * selected as we speak).
640 seltop
= sb ?
-term
->savelines
: topline
;
642 if (term
->selstart
.y
>= seltop
&&
643 term
->selstart
.y
<= botline
) {
645 if (term
->selstart
.y
< seltop
) {
646 term
->selstart
.y
= seltop
;
647 term
->selstart
.x
= 0;
650 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
652 if (term
->selend
.y
< seltop
) {
653 term
->selend
.y
= seltop
;
657 if (term
->selanchor
.y
>= seltop
&& term
->selanchor
.y
<= botline
) {
659 if (term
->selanchor
.y
< seltop
) {
660 term
->selanchor
.y
= seltop
;
661 term
->selanchor
.x
= 0;
671 * Move the cursor to a given position, clipping at boundaries. We
672 * may or may not want to clip at the scroll margin: marg_clip is 0
673 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
674 * even _being_ outside the margins.
676 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
683 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
686 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
697 term
->wrapnext
= FALSE
;
701 * Save or restore the cursor and SGR mode.
703 static void save_cursor(Terminal
*term
, int save
)
706 term
->savecurs
= term
->curs
;
707 term
->save_attr
= term
->curr_attr
;
708 term
->save_cset
= term
->cset
;
709 term
->save_utf
= term
->utf
;
710 term
->save_wnext
= term
->wrapnext
;
711 term
->save_csattr
= term
->cset_attr
[term
->cset
];
712 term
->save_sco_acs
= term
->sco_acs
;
714 term
->curs
= term
->savecurs
;
715 /* Make sure the window hasn't shrunk since the save */
716 if (term
->curs
.x
>= term
->cols
)
717 term
->curs
.x
= term
->cols
- 1;
718 if (term
->curs
.y
>= term
->rows
)
719 term
->curs
.y
= term
->rows
- 1;
721 term
->curr_attr
= term
->save_attr
;
722 term
->cset
= term
->save_cset
;
723 term
->utf
= term
->save_utf
;
724 term
->wrapnext
= term
->save_wnext
;
726 * wrapnext might reset to False if the x position is no
727 * longer at the rightmost edge.
729 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
730 term
->wrapnext
= FALSE
;
731 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
732 term
->sco_acs
= term
->save_sco_acs
;
735 term
->erase_char
= (' ' | ATTR_ASCII
|
737 (ATTR_FGMASK
| ATTR_BGMASK
)));
742 * Erase a large portion of the screen: the whole screen, or the
743 * whole line, or parts thereof.
745 static void erase_lots(Terminal
*term
,
746 int line_only
, int from_begin
, int to_end
)
750 unsigned long *ldata
;
753 start
.y
= term
->curs
.y
;
755 end
.y
= term
->curs
.y
+ 1;
772 check_selection(term
, start
, end
);
774 /* Clear screen also forces a full window redraw, just in case. */
775 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
776 term_invalidate(term
);
778 ldata
= lineptr(start
.y
);
779 while (poslt(start
, end
)) {
780 if (start
.x
== term
->cols
&& !erase_lattr
)
781 ldata
[start
.x
] &= ~LATTR_WRAPPED
;
783 ldata
[start
.x
] = term
->erase_char
;
784 if (incpos(start
) && start
.y
< term
->rows
)
785 ldata
= lineptr(start
.y
);
790 * Insert or delete characters within the current line. n is +ve if
791 * insertion is desired, and -ve for deletion.
793 static void insch(Terminal
*term
, int n
)
795 int dir
= (n
< 0 ?
-1 : +1);
798 unsigned long *ldata
;
800 n
= (n
< 0 ?
-n
: n
);
801 if (n
> term
->cols
- term
->curs
.x
)
802 n
= term
->cols
- term
->curs
.x
;
803 m
= term
->cols
- term
->curs
.x
- n
;
804 cursplus
.y
= term
->curs
.y
;
805 cursplus
.x
= term
->curs
.x
+ n
;
806 check_selection(term
, term
->curs
, cursplus
);
807 ldata
= lineptr(term
->curs
.y
);
809 memmove(ldata
+ term
->curs
.x
, ldata
+ term
->curs
.x
+ n
, m
* TSIZE
);
811 ldata
[term
->curs
.x
+ m
++] = term
->erase_char
;
813 memmove(ldata
+ term
->curs
.x
+ n
, ldata
+ term
->curs
.x
, m
* TSIZE
);
815 ldata
[term
->curs
.x
+ n
] = term
->erase_char
;
820 * Toggle terminal mode `mode' to state `state'. (`query' indicates
821 * whether the mode is a DEC private one or a normal one.)
823 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
829 case 1: /* application cursor keys */
830 term
->app_cursor_keys
= state
;
832 case 2: /* VT52 mode */
833 term
->vt52_mode
= !state
;
834 if (term
->vt52_mode
) {
835 term
->blink_is_real
= FALSE
;
836 term
->vt52_bold
= FALSE
;
838 term
->blink_is_real
= cfg
.blinktext
;
841 case 3: /* 80/132 columns */
843 if (!cfg
.no_remote_resize
)
844 request_resize(state ?
132 : 80, term
->rows
);
845 term
->reset_132
= state
;
847 case 5: /* reverse video */
849 * Toggle reverse video. If we receive an OFF within the
850 * visual bell timeout period after an ON, we trigger an
851 * effective visual bell, so that ESC[?5hESC[?5l will
852 * always be an actually _visible_ visual bell.
854 ticks
= GETTICKCOUNT();
855 /* turn off a previous vbell to avoid inconsistencies */
856 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
857 term
->in_vbell
= FALSE
;
858 if (term
->rvideo
&& !state
&& /* we're turning it off... */
859 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
860 /* If there's no vbell timeout already, or this one lasts
861 * longer, replace vbell_timeout with ours. */
862 if (!term
->in_vbell
||
863 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
865 term
->vbell_startpoint
= term
->rvbell_startpoint
;
866 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
867 } else if (!term
->rvideo
&& state
) {
868 /* This is an ON, so we notice the time and save it. */
869 term
->rvbell_startpoint
= ticks
;
871 term
->rvideo
= state
;
872 term
->seen_disp_event
= TRUE
;
876 case 6: /* DEC origin mode */
877 term
->dec_om
= state
;
879 case 7: /* auto wrap */
882 case 8: /* auto key repeat */
883 term
->repeat_off
= !state
;
885 case 10: /* set local edit mode */
886 term
->term_editing
= state
;
887 ldisc_send(NULL
, 0, 0); /* cause ldisc to notice changes */
889 case 25: /* enable/disable cursor */
890 compatibility2(OTHER
, VT220
);
891 term
->cursor_on
= state
;
892 term
->seen_disp_event
= TRUE
;
894 case 47: /* alternate screen */
895 compatibility(OTHER
);
897 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
900 case 1000: /* xterm mouse 1 */
901 term
->xterm_mouse
= state ?
1 : 0;
902 set_raw_mouse_mode(state
);
904 case 1002: /* xterm mouse 2 */
905 term
->xterm_mouse
= state ?
2 : 0;
906 set_raw_mouse_mode(state
);
908 case 1047: /* alternate screen */
909 compatibility(OTHER
);
911 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
914 case 1048: /* save/restore cursor */
915 save_cursor(term
, state
);
916 if (!state
) term
->seen_disp_event
= TRUE
;
918 case 1049: /* cursor & alternate screen */
920 save_cursor(term
, state
);
921 if (!state
) term
->seen_disp_event
= TRUE
;
922 compatibility(OTHER
);
924 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
926 save_cursor(term
, state
);
931 case 4: /* set insert mode */
932 compatibility(VT102
);
933 term
->insert
= state
;
935 case 12: /* set echo mode */
936 term
->term_echoing
= !state
;
937 ldisc_send(NULL
, 0, 0); /* cause ldisc to notice changes */
939 case 20: /* Return sends ... */
940 term
->cr_lf_return
= state
;
942 case 34: /* Make cursor BIG */
943 compatibility2(OTHER
, VT220
);
944 term
->big_cursor
= !state
;
949 * Process an OSC sequence: set window title or icon name.
951 static void do_osc(Terminal
*term
)
954 while (term
->osc_strlen
--)
955 term
->wordness
[(unsigned char)
956 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
958 term
->osc_string
[term
->osc_strlen
] = '\0';
959 switch (term
->esc_args
[0]) {
962 if (!cfg
.no_remote_wintitle
)
963 set_icon(term
->osc_string
);
964 if (term
->esc_args
[0] == 1)
966 /* fall through: parameter 0 means set both */
969 if (!cfg
.no_remote_wintitle
)
970 set_title(term
->osc_string
);
977 * ANSI printing routines.
979 static void term_print_setup(Terminal
*term
)
981 bufchain_clear(&term
->printer_buf
);
982 term
->print_job
= printer_start_job(cfg
.printer
);
984 static void term_print_flush(Terminal
*term
)
989 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
990 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
993 printer_job_data(term
->print_job
, data
, len
);
994 bufchain_consume(&term
->printer_buf
, len
);
997 static void term_print_finish(Terminal
*term
)
1003 if (!term
->printing
&& !term
->only_printing
)
1004 return; /* we need do nothing */
1006 term_print_flush(term
);
1007 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
1008 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1010 if (c
== '\033' || c
== '\233') {
1011 bufchain_consume(&term
->printer_buf
, size
);
1014 printer_job_data(term
->print_job
, &c
, 1);
1015 bufchain_consume(&term
->printer_buf
, 1);
1018 printer_finish_job(term
->print_job
);
1019 term
->print_job
= NULL
;
1020 term
->printing
= term
->only_printing
= FALSE
;
1024 * Remove everything currently in `inbuf' and stick it up on the
1025 * in-memory display. There's a big state machine in here to
1026 * process escape sequences...
1028 void term_out(Terminal
*term
)
1031 unsigned char localbuf
[256], *chars
;
1036 chars
= NULL
; /* placate compiler warnings */
1037 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
1041 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
1042 if (nchars
> sizeof(localbuf
))
1043 nchars
= sizeof(localbuf
);
1044 memcpy(localbuf
, ret
, nchars
);
1045 bufchain_consume(&term
->inbuf
, nchars
);
1047 assert(chars
!= NULL
);
1053 * Optionally log the session traffic to a file. Useful for
1054 * debugging and possibly also useful for actual logging.
1056 if (cfg
.logtype
== LGTYP_DEBUG
)
1057 logtraffic((unsigned char) c
, LGTYP_DEBUG
);
1063 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1064 * be able to display 8-bit characters, but I'll let that go 'cause
1069 * If we're printing, add the character to the printer
1072 if (term
->printing
) {
1073 bufchain_add(&term
->printer_buf
, &c
, 1);
1076 * If we're in print-only mode, we use a much simpler
1077 * state machine designed only to recognise the ESC[4i
1078 * termination sequence.
1080 if (term
->only_printing
) {
1082 term
->print_state
= 1;
1083 else if (c
== (unsigned char)'\233')
1084 term
->print_state
= 2;
1085 else if (c
== '[' && term
->print_state
== 1)
1086 term
->print_state
= 2;
1087 else if (c
== '4' && term
->print_state
== 2)
1088 term
->print_state
= 3;
1089 else if (c
== 'i' && term
->print_state
== 3)
1090 term
->print_state
= 4;
1092 term
->print_state
= 0;
1093 if (term
->print_state
== 4) {
1094 term_print_finish(term
);
1100 /* First see about all those translations. */
1101 if (term
->termstate
== TOPLEVEL
) {
1103 switch (term
->utf_state
) {
1106 /* UTF-8 must be stateless so we ignore iso2022. */
1107 if (unitab_ctrl
[c
] != 0xFF)
1109 else c
= ((unsigned char)c
) | ATTR_ASCII
;
1111 } else if ((c
& 0xe0) == 0xc0) {
1112 term
->utf_size
= term
->utf_state
= 1;
1113 term
->utf_char
= (c
& 0x1f);
1114 } else if ((c
& 0xf0) == 0xe0) {
1115 term
->utf_size
= term
->utf_state
= 2;
1116 term
->utf_char
= (c
& 0x0f);
1117 } else if ((c
& 0xf8) == 0xf0) {
1118 term
->utf_size
= term
->utf_state
= 3;
1119 term
->utf_char
= (c
& 0x07);
1120 } else if ((c
& 0xfc) == 0xf8) {
1121 term
->utf_size
= term
->utf_state
= 4;
1122 term
->utf_char
= (c
& 0x03);
1123 } else if ((c
& 0xfe) == 0xfc) {
1124 term
->utf_size
= term
->utf_state
= 5;
1125 term
->utf_char
= (c
& 0x01);
1136 if ((c
& 0xC0) != 0x80) {
1139 term
->utf_state
= 0;
1142 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
1143 if (--term
->utf_state
)
1148 /* Is somebody trying to be evil! */
1150 (c
< 0x800 && term
->utf_size
>= 2) ||
1151 (c
< 0x10000 && term
->utf_size
>= 3) ||
1152 (c
< 0x200000 && term
->utf_size
>= 4) ||
1153 (c
< 0x4000000 && term
->utf_size
>= 5))
1156 /* Unicode line separator and paragraph separator are CR-LF */
1157 if (c
== 0x2028 || c
== 0x2029)
1160 /* High controls are probably a Baaad idea too. */
1164 /* The UTF-16 surrogates are not nice either. */
1165 /* The standard give the option of decoding these:
1166 * I don't want to! */
1167 if (c
>= 0xD800 && c
< 0xE000)
1170 /* ISO 10646 characters now limited to UTF-16 range. */
1174 /* This is currently a TagPhobic application.. */
1175 if (c
>= 0xE0000 && c
<= 0xE007F)
1178 /* U+FEFF is best seen as a null. */
1181 /* But U+FFFE is an error. */
1182 if (c
== 0xFFFE || c
== 0xFFFF)
1185 /* Oops this is a 16bit implementation */
1190 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1191 else if(term
->sco_acs
&&
1192 (c
!='\033' && c
!='\n' && c
!='\r' && c
!='\b'))
1194 if (term
->sco_acs
== 2) c
^= 0x80;
1197 switch (term
->cset_attr
[term
->cset
]) {
1199 * Linedraw characters are different from 'ESC ( B'
1200 * only for a small range. For ones outside that
1201 * range, make sure we use the same font as well as
1202 * the same encoding.
1205 if (unitab_ctrl
[c
] != 0xFF)
1208 c
= ((unsigned char) c
) | ATTR_LINEDRW
;
1212 /* If UK-ASCII, make the '#' a LineDraw Pound */
1214 c
= '}' | ATTR_LINEDRW
;
1217 /*FALLTHROUGH*/ case ATTR_ASCII
:
1218 if (unitab_ctrl
[c
] != 0xFF)
1221 c
= ((unsigned char) c
) | ATTR_ASCII
;
1224 if (c
>=' ') c
= ((unsigned char)c
) | ATTR_SCOACS
;
1230 /* How about C1 controls ? */
1231 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
1232 !term
->vt52_mode
&& has_compat(VT220
)) {
1233 term
->termstate
= SEEN_ESC
;
1234 term
->esc_query
= FALSE
;
1235 c
= '@' + (c
& 0x1F);
1238 /* Or the GL control. */
1239 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
1240 if (term
->curs
.x
&& !term
->wrapnext
)
1242 term
->wrapnext
= FALSE
;
1244 if (!cfg
.no_dbackspace
) /* destructive bksp might be disabled */
1245 *term
->cpos
= (' ' | term
->curr_attr
| ATTR_ASCII
);
1247 /* Or normal C0 controls. */
1248 if ((c
& -32) == 0 && term
->termstate
< DO_CTRLS
) {
1250 case '\005': /* terminal type query */
1251 /* Strictly speaking this is VT100 but a VT100 defaults to
1252 * no response. Other terminals respond at their option.
1254 * Don't put a CR in the default string as this tends to
1255 * upset some weird software.
1257 * An xterm returns "xterm" (5 characters)
1259 compatibility(ANSIMIN
);
1261 char abuf
[256], *s
, *d
;
1263 for (s
= cfg
.answerback
, d
= abuf
; *s
; s
++) {
1265 if (*s
>= 'a' && *s
<= 'z')
1266 *d
++ = (*s
- ('a' - 1));
1267 else if ((*s
>= '@' && *s
<= '_') ||
1268 *s
== '?' || (*s
& 0x80))
1273 } else if (*s
== '^') {
1278 lpage_send(DEFAULT_CODEPAGE
, abuf
, d
- abuf
, 0);
1283 struct beeptime
*newbeep
;
1284 unsigned long ticks
;
1286 ticks
= GETTICKCOUNT();
1288 if (!term
->beep_overloaded
) {
1289 newbeep
= smalloc(sizeof(struct beeptime
));
1290 newbeep
->ticks
= ticks
;
1291 newbeep
->next
= NULL
;
1292 if (!term
->beephead
)
1293 term
->beephead
= newbeep
;
1295 term
->beeptail
->next
= newbeep
;
1296 term
->beeptail
= newbeep
;
1301 * Throw out any beeps that happened more than
1304 while (term
->beephead
&&
1305 term
->beephead
->ticks
< ticks
- cfg
.bellovl_t
) {
1306 struct beeptime
*tmp
= term
->beephead
;
1307 term
->beephead
= tmp
->next
;
1309 if (!term
->beephead
)
1310 term
->beeptail
= NULL
;
1314 if (cfg
.bellovl
&& term
->beep_overloaded
&&
1315 ticks
- term
->lastbeep
>= (unsigned)cfg
.bellovl_s
) {
1317 * If we're currently overloaded and the
1318 * last beep was more than s seconds ago,
1319 * leave overload mode.
1321 term
->beep_overloaded
= FALSE
;
1322 } else if (cfg
.bellovl
&& !term
->beep_overloaded
&&
1323 term
->nbeeps
>= cfg
.bellovl_n
) {
1325 * Now, if we have n or more beeps
1326 * remaining in the queue, go into overload
1329 term
->beep_overloaded
= TRUE
;
1331 term
->lastbeep
= ticks
;
1334 * Perform an actual beep if we're not overloaded.
1336 if (!cfg
.bellovl
|| !term
->beep_overloaded
) {
1337 if (cfg
.beep
== BELL_VISUAL
) {
1338 term
->in_vbell
= TRUE
;
1339 term
->vbell_startpoint
= ticks
;
1348 if (term
->curs
.x
== 0 &&
1349 (term
->curs
.y
== 0 || term
->wrap
== 0))
1351 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
1352 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
1353 else if (term
->wrapnext
)
1354 term
->wrapnext
= FALSE
;
1358 term
->seen_disp_event
= TRUE
;
1361 compatibility(VT100
);
1365 compatibility(VT100
);
1369 if (term
->vt52_mode
)
1370 term
->termstate
= VT52_ESC
;
1372 compatibility(ANSIMIN
);
1373 term
->termstate
= SEEN_ESC
;
1374 term
->esc_query
= FALSE
;
1379 term
->wrapnext
= FALSE
;
1381 term
->seen_disp_event
= TRUE
;
1382 term
->paste_hold
= 0;
1383 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1386 if (has_compat(SCOANSI
)) {
1387 move(term
, 0, 0, 0);
1388 erase_lots(term
, FALSE
, FALSE
, TRUE
);
1390 term
->wrapnext
= FALSE
;
1391 term
->seen_disp_event
= 1;
1395 compatibility(VT100
);
1397 if (term
->curs
.y
== term
->marg_b
)
1398 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1399 else if (term
->curs
.y
< term
->rows
- 1)
1404 term
->wrapnext
= FALSE
;
1405 term
->seen_disp_event
= 1;
1406 term
->paste_hold
= 0;
1407 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1411 pos old_curs
= term
->curs
;
1412 unsigned long *ldata
= lineptr(term
->curs
.y
);
1416 } while (term
->curs
.x
< term
->cols
- 1 &&
1417 !term
->tabs
[term
->curs
.x
]);
1419 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
) {
1420 if (term
->curs
.x
>= term
->cols
/ 2)
1421 term
->curs
.x
= term
->cols
/ 2 - 1;
1423 if (term
->curs
.x
>= term
->cols
)
1424 term
->curs
.x
= term
->cols
- 1;
1428 check_selection(term
, old_curs
, term
->curs
);
1430 term
->seen_disp_event
= TRUE
;
1434 switch (term
->termstate
) {
1436 /* Only graphic characters get this far;
1437 * ctrls are stripped above */
1438 if (term
->wrapnext
&& term
->wrap
) {
1439 term
->cpos
[1] |= LATTR_WRAPPED
;
1440 if (term
->curs
.y
== term
->marg_b
)
1441 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1442 else if (term
->curs
.y
< term
->rows
- 1)
1446 term
->wrapnext
= FALSE
;
1450 if (term
->selstate
!= NO_SELECTION
) {
1451 pos cursplus
= term
->curs
;
1453 check_selection(term
, term
->curs
, cursplus
);
1455 if ((c
& CSET_MASK
) == ATTR_ASCII
|| (c
& CSET_MASK
) == 0)
1456 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1458 extern int wcwidth(wchar_t ucs
);
1463 width
= wcwidth((wchar_t) c
);
1466 *term
->cpos
++ = c
| term
->curr_attr
;
1467 if (++term
->curs
.x
== term
->cols
) {
1468 *term
->cpos
|= LATTR_WRAPPED
;
1469 if (term
->curs
.y
== term
->marg_b
)
1470 scroll(term
, term
->marg_t
, term
->marg_b
,
1472 else if (term
->curs
.y
< term
->rows
- 1)
1477 *term
->cpos
++ = UCSWIDE
| term
->curr_attr
;
1480 *term
->cpos
++ = c
| term
->curr_attr
;
1487 if (term
->curs
.x
== term
->cols
) {
1490 term
->wrapnext
= TRUE
;
1491 if (term
->wrap
&& term
->vt52_mode
) {
1492 term
->cpos
[1] |= LATTR_WRAPPED
;
1493 if (term
->curs
.y
== term
->marg_b
)
1494 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1495 else if (term
->curs
.y
< term
->rows
- 1)
1499 term
->wrapnext
= FALSE
;
1502 term
->seen_disp_event
= 1;
1507 * This state is virtually identical to SEEN_ESC, with the
1508 * exception that we have an OSC sequence in the pipeline,
1509 * and _if_ we see a backslash, we process it.
1513 term
->termstate
= TOPLEVEL
;
1516 /* else fall through */
1518 if (c
>= ' ' && c
<= '/') {
1519 if (term
->esc_query
)
1520 term
->esc_query
= -1;
1522 term
->esc_query
= c
;
1525 term
->termstate
= TOPLEVEL
;
1526 switch (ANSI(c
, term
->esc_query
)) {
1527 case '[': /* enter CSI mode */
1528 term
->termstate
= SEEN_CSI
;
1529 term
->esc_nargs
= 1;
1530 term
->esc_args
[0] = ARG_DEFAULT
;
1531 term
->esc_query
= FALSE
;
1533 case ']': /* xterm escape sequences */
1534 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1535 compatibility(OTHER
);
1536 term
->termstate
= SEEN_OSC
;
1537 term
->esc_args
[0] = 0;
1539 case '7': /* save cursor */
1540 compatibility(VT100
);
1541 save_cursor(term
, TRUE
);
1543 case '8': /* restore cursor */
1544 compatibility(VT100
);
1545 save_cursor(term
, FALSE
);
1546 term
->seen_disp_event
= TRUE
;
1549 compatibility(VT100
);
1550 term
->app_keypad_keys
= TRUE
;
1553 compatibility(VT100
);
1554 term
->app_keypad_keys
= FALSE
;
1556 case 'D': /* exactly equivalent to LF */
1557 compatibility(VT100
);
1558 if (term
->curs
.y
== term
->marg_b
)
1559 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1560 else if (term
->curs
.y
< term
->rows
- 1)
1563 term
->wrapnext
= FALSE
;
1564 term
->seen_disp_event
= TRUE
;
1566 case 'E': /* exactly equivalent to CR-LF */
1567 compatibility(VT100
);
1569 if (term
->curs
.y
== term
->marg_b
)
1570 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1571 else if (term
->curs
.y
< term
->rows
- 1)
1574 term
->wrapnext
= FALSE
;
1575 term
->seen_disp_event
= TRUE
;
1577 case 'M': /* reverse index - backwards LF */
1578 compatibility(VT100
);
1579 if (term
->curs
.y
== term
->marg_t
)
1580 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
1581 else if (term
->curs
.y
> 0)
1584 term
->wrapnext
= FALSE
;
1585 term
->seen_disp_event
= TRUE
;
1587 case 'Z': /* terminal type query */
1588 compatibility(VT100
);
1589 ldisc_send(term
->id_string
, strlen(term
->id_string
), 0);
1591 case 'c': /* restore power-on settings */
1592 compatibility(VT100
);
1594 ldisc_send(NULL
, 0, 0);/* cause ldisc to notice changes */
1595 if (term
->reset_132
) {
1596 if (!cfg
.no_remote_resize
)
1597 request_resize(80, term
->rows
);
1598 term
->reset_132
= 0;
1602 term
->seen_disp_event
= TRUE
;
1604 case 'H': /* set a tab */
1605 compatibility(VT100
);
1606 term
->tabs
[term
->curs
.x
] = TRUE
;
1609 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1610 compatibility(VT100
);
1612 unsigned long *ldata
;
1616 for (i
= 0; i
< term
->rows
; i
++) {
1618 for (j
= 0; j
< term
->cols
; j
++)
1619 ldata
[j
] = ATTR_DEFAULT
| 'E';
1620 ldata
[term
->cols
] = 0;
1623 term
->seen_disp_event
= TRUE
;
1624 scrtop
.x
= scrtop
.y
= 0;
1626 scrbot
.y
= term
->rows
;
1627 check_selection(term
, scrtop
, scrbot
);
1631 case ANSI('3', '#'):
1632 case ANSI('4', '#'):
1633 case ANSI('5', '#'):
1634 case ANSI('6', '#'):
1635 compatibility(VT100
);
1637 unsigned long nlattr
;
1638 unsigned long *ldata
;
1639 switch (ANSI(c
, term
->esc_query
)) {
1640 case ANSI('3', '#'):
1643 case ANSI('4', '#'):
1646 case ANSI('5', '#'):
1647 nlattr
= LATTR_NORM
;
1649 default: /* spiritually case ANSI('6', '#'): */
1650 nlattr
= LATTR_WIDE
;
1653 ldata
= lineptr(term
->curs
.y
);
1654 ldata
[term
->cols
] &= ~LATTR_MODE
;
1655 ldata
[term
->cols
] |= nlattr
;
1659 case ANSI('A', '('):
1660 compatibility(VT100
);
1661 if (!cfg
.no_remote_charset
)
1662 term
->cset_attr
[0] = ATTR_GBCHR
;
1664 case ANSI('B', '('):
1665 compatibility(VT100
);
1666 if (!cfg
.no_remote_charset
)
1667 term
->cset_attr
[0] = ATTR_ASCII
;
1669 case ANSI('0', '('):
1670 compatibility(VT100
);
1671 if (!cfg
.no_remote_charset
)
1672 term
->cset_attr
[0] = ATTR_LINEDRW
;
1674 case ANSI('U', '('):
1675 compatibility(OTHER
);
1676 if (!cfg
.no_remote_charset
)
1677 term
->cset_attr
[0] = ATTR_SCOACS
;
1680 case ANSI('A', ')'):
1681 compatibility(VT100
);
1682 if (!cfg
.no_remote_charset
)
1683 term
->cset_attr
[1] = ATTR_GBCHR
;
1685 case ANSI('B', ')'):
1686 compatibility(VT100
);
1687 if (!cfg
.no_remote_charset
)
1688 term
->cset_attr
[1] = ATTR_ASCII
;
1690 case ANSI('0', ')'):
1691 compatibility(VT100
);
1692 if (!cfg
.no_remote_charset
)
1693 term
->cset_attr
[1] = ATTR_LINEDRW
;
1695 case ANSI('U', ')'):
1696 compatibility(OTHER
);
1697 if (!cfg
.no_remote_charset
)
1698 term
->cset_attr
[1] = ATTR_SCOACS
;
1701 case ANSI('8', '%'): /* Old Linux code */
1702 case ANSI('G', '%'):
1703 compatibility(OTHER
);
1704 if (!cfg
.no_remote_charset
)
1707 case ANSI('@', '%'):
1708 compatibility(OTHER
);
1709 if (!cfg
.no_remote_charset
)
1715 term
->termstate
= TOPLEVEL
; /* default */
1717 if (term
->esc_nargs
<= ARGS_MAX
) {
1718 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
1719 term
->esc_args
[term
->esc_nargs
- 1] = 0;
1720 term
->esc_args
[term
->esc_nargs
- 1] =
1721 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
1723 term
->termstate
= SEEN_CSI
;
1724 } else if (c
== ';') {
1725 if (++term
->esc_nargs
<= ARGS_MAX
)
1726 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
1727 term
->termstate
= SEEN_CSI
;
1728 } else if (c
< '@') {
1729 if (term
->esc_query
)
1730 term
->esc_query
= -1;
1732 term
->esc_query
= TRUE
;
1734 term
->esc_query
= c
;
1735 term
->termstate
= SEEN_CSI
;
1737 switch (ANSI(c
, term
->esc_query
)) {
1738 case 'A': /* move up N lines */
1739 move(term
, term
->curs
.x
,
1740 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1741 term
->seen_disp_event
= TRUE
;
1743 case 'e': /* move down N lines */
1744 compatibility(ANSI
);
1747 move(term
, term
->curs
.x
,
1748 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1749 term
->seen_disp_event
= TRUE
;
1751 case ANSI('c', '>'): /* report xterm version */
1752 compatibility(OTHER
);
1753 /* this reports xterm version 136 so that VIM can
1754 use the drag messages from the mouse reporting */
1755 ldisc_send("\033[>0;136;0c", 11, 0);
1757 case 'a': /* move right N cols */
1758 compatibility(ANSI
);
1761 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
1763 term
->seen_disp_event
= TRUE
;
1765 case 'D': /* move left N cols */
1766 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
1768 term
->seen_disp_event
= TRUE
;
1770 case 'E': /* move down N lines and CR */
1771 compatibility(ANSI
);
1773 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1774 term
->seen_disp_event
= TRUE
;
1776 case 'F': /* move up N lines and CR */
1777 compatibility(ANSI
);
1779 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1780 term
->seen_disp_event
= TRUE
;
1783 case '`': /* set horizontal posn */
1784 compatibility(ANSI
);
1785 move(term
, def(term
->esc_args
[0], 1) - 1,
1787 term
->seen_disp_event
= TRUE
;
1789 case 'd': /* set vertical posn */
1790 compatibility(ANSI
);
1791 move(term
, term
->curs
.x
,
1792 ((term
->dec_om ? term
->marg_t
: 0) +
1793 def(term
->esc_args
[0], 1) - 1),
1794 (term
->dec_om ?
2 : 0));
1795 term
->seen_disp_event
= TRUE
;
1798 case 'f': /* set horz and vert posns at once */
1799 if (term
->esc_nargs
< 2)
1800 term
->esc_args
[1] = ARG_DEFAULT
;
1801 move(term
, def(term
->esc_args
[1], 1) - 1,
1802 ((term
->dec_om ? term
->marg_t
: 0) +
1803 def(term
->esc_args
[0], 1) - 1),
1804 (term
->dec_om ?
2 : 0));
1805 term
->seen_disp_event
= TRUE
;
1807 case 'J': /* erase screen or parts of it */
1809 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1812 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
1815 term
->seen_disp_event
= TRUE
;
1817 case 'K': /* erase line or parts of it */
1819 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1822 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
1824 term
->seen_disp_event
= TRUE
;
1826 case 'L': /* insert lines */
1827 compatibility(VT102
);
1828 if (term
->curs
.y
<= term
->marg_b
)
1829 scroll(term
, term
->curs
.y
, term
->marg_b
,
1830 -def(term
->esc_args
[0], 1), FALSE
);
1832 term
->seen_disp_event
= TRUE
;
1834 case 'M': /* delete lines */
1835 compatibility(VT102
);
1836 if (term
->curs
.y
<= term
->marg_b
)
1837 scroll(term
, term
->curs
.y
, term
->marg_b
,
1838 def(term
->esc_args
[0], 1),
1841 term
->seen_disp_event
= TRUE
;
1843 case '@': /* insert chars */
1844 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1845 compatibility(VT102
);
1846 insch(term
, def(term
->esc_args
[0], 1));
1847 term
->seen_disp_event
= TRUE
;
1849 case 'P': /* delete chars */
1850 compatibility(VT102
);
1851 insch(term
, -def(term
->esc_args
[0], 1));
1852 term
->seen_disp_event
= TRUE
;
1854 case 'c': /* terminal type query */
1855 compatibility(VT100
);
1856 /* This is the response for a VT102 */
1857 ldisc_send(term
->id_string
,
1858 strlen(term
->id_string
), 0);
1860 case 'n': /* cursor position query */
1861 if (term
->esc_args
[0] == 6) {
1863 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
1865 ldisc_send(buf
, strlen(buf
), 0);
1866 } else if (term
->esc_args
[0] == 5) {
1867 ldisc_send("\033[0n", 4, 0);
1870 case 'h': /* toggle modes to high */
1872 compatibility(VT100
);
1875 for (i
= 0; i
< term
->esc_nargs
; i
++)
1876 toggle_mode(term
, term
->esc_args
[i
],
1877 term
->esc_query
, TRUE
);
1882 compatibility(VT100
);
1884 if (term
->esc_nargs
!= 1) break;
1885 if (term
->esc_args
[0] == 5 && *cfg
.printer
) {
1886 term
->printing
= TRUE
;
1887 term
->only_printing
= !term
->esc_query
;
1888 term
->print_state
= 0;
1889 term_print_setup(term
);
1890 } else if (term
->esc_args
[0] == 4 &&
1892 term_print_finish(term
);
1896 case 'l': /* toggle modes to low */
1898 compatibility(VT100
);
1901 for (i
= 0; i
< term
->esc_nargs
; i
++)
1902 toggle_mode(term
, term
->esc_args
[i
],
1903 term
->esc_query
, FALSE
);
1906 case 'g': /* clear tabs */
1907 compatibility(VT100
);
1908 if (term
->esc_nargs
== 1) {
1909 if (term
->esc_args
[0] == 0) {
1910 term
->tabs
[term
->curs
.x
] = FALSE
;
1911 } else if (term
->esc_args
[0] == 3) {
1913 for (i
= 0; i
< term
->cols
; i
++)
1914 term
->tabs
[i
] = FALSE
;
1918 case 'r': /* set scroll margins */
1919 compatibility(VT100
);
1920 if (term
->esc_nargs
<= 2) {
1922 top
= def(term
->esc_args
[0], 1) - 1;
1923 bot
= (term
->esc_nargs
<= 1
1924 || term
->esc_args
[1] == 0 ?
1926 def(term
->esc_args
[1], term
->rows
)) - 1;
1927 if (bot
>= term
->rows
)
1928 bot
= term
->rows
- 1;
1929 /* VTTEST Bug 9 - if region is less than 2 lines
1930 * don't change region.
1932 if (bot
- top
> 0) {
1937 * I used to think the cursor should be
1938 * placed at the top of the newly marginned
1939 * area. Apparently not: VMS TPU falls over
1942 * Well actually it should for
1945 term
->curs
.y
= (term
->dec_om ?
1948 term
->seen_disp_event
= TRUE
;
1952 case 'm': /* set graphics rendition */
1955 * A VT100 without the AVO only had one
1956 * attribute, either underline or
1957 * reverse video depending on the
1958 * cursor type, this was selected by
1962 * This is sometimes DIM, eg on the
1965 * This is sometimes INVIS various ANSI.
1967 * This like 22 disables BOLD, DIM and INVIS
1969 * The ANSI colours appear on any
1970 * terminal that has colour (obviously)
1971 * but the interaction between sgr0 and
1972 * the colours varies but is usually
1973 * related to the background colour
1974 * erase item. The interaction between
1975 * colour attributes and the mono ones
1976 * is also very implementation
1979 * The 39 and 49 attributes are likely
1980 * to be unimplemented.
1983 for (i
= 0; i
< term
->esc_nargs
; i
++) {
1984 switch (def(term
->esc_args
[i
], 0)) {
1985 case 0: /* restore defaults */
1986 term
->curr_attr
= ATTR_DEFAULT
;
1988 case 1: /* enable bold */
1989 compatibility(VT100AVO
);
1990 term
->curr_attr
|= ATTR_BOLD
;
1992 case 21: /* (enable double underline) */
1993 compatibility(OTHER
);
1994 case 4: /* enable underline */
1995 compatibility(VT100AVO
);
1996 term
->curr_attr
|= ATTR_UNDER
;
1998 case 5: /* enable blink */
1999 compatibility(VT100AVO
);
2000 term
->curr_attr
|= ATTR_BLINK
;
2002 case 7: /* enable reverse video */
2003 term
->curr_attr
|= ATTR_REVERSE
;
2005 case 10: /* SCO acs off */
2006 compatibility(SCOANSI
);
2007 if (cfg
.no_remote_charset
) break;
2008 term
->sco_acs
= 0; break;
2009 case 11: /* SCO acs on */
2010 compatibility(SCOANSI
);
2011 if (cfg
.no_remote_charset
) break;
2012 term
->sco_acs
= 1; break;
2013 case 12: /* SCO acs on flipped */
2014 compatibility(SCOANSI
);
2015 if (cfg
.no_remote_charset
) break;
2016 term
->sco_acs
= 2; break;
2017 case 22: /* disable bold */
2018 compatibility2(OTHER
, VT220
);
2019 term
->curr_attr
&= ~ATTR_BOLD
;
2021 case 24: /* disable underline */
2022 compatibility2(OTHER
, VT220
);
2023 term
->curr_attr
&= ~ATTR_UNDER
;
2025 case 25: /* disable blink */
2026 compatibility2(OTHER
, VT220
);
2027 term
->curr_attr
&= ~ATTR_BLINK
;
2029 case 27: /* disable reverse video */
2030 compatibility2(OTHER
, VT220
);
2031 term
->curr_attr
&= ~ATTR_REVERSE
;
2042 term
->curr_attr
&= ~ATTR_FGMASK
;
2044 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
2046 case 39: /* default-foreground */
2047 term
->curr_attr
&= ~ATTR_FGMASK
;
2048 term
->curr_attr
|= ATTR_DEFFG
;
2059 term
->curr_attr
&= ~ATTR_BGMASK
;
2061 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
2063 case 49: /* default-background */
2064 term
->curr_attr
&= ~ATTR_BGMASK
;
2065 term
->curr_attr
|= ATTR_DEFBG
;
2070 term
->erase_char
= (' ' | ATTR_ASCII
|
2076 case 's': /* save cursor */
2077 save_cursor(term
, TRUE
);
2079 case 'u': /* restore cursor */
2080 save_cursor(term
, FALSE
);
2081 term
->seen_disp_event
= TRUE
;
2083 case 't': /* set page size - ie window height */
2085 * VT340/VT420 sequence DECSLPP, DEC only allows values
2086 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2087 * illegal values (eg first arg 1..9) for window changing
2090 if (term
->esc_nargs
<= 1
2091 && (term
->esc_args
[0] < 1 ||
2092 term
->esc_args
[0] >= 24)) {
2093 compatibility(VT340TEXT
);
2094 if (!cfg
.no_remote_resize
)
2095 request_resize(term
->cols
,
2096 def(term
->esc_args
[0], 24));
2098 } else if (term
->esc_nargs
>= 1 &&
2099 term
->esc_args
[0] >= 1 &&
2100 term
->esc_args
[0] < 24) {
2101 compatibility(OTHER
);
2103 switch (term
->esc_args
[0]) {
2113 if (term
->esc_nargs
>= 3) {
2114 if (!cfg
.no_remote_resize
)
2115 move_window(def(term
->esc_args
[1], 0),
2116 def(term
->esc_args
[2], 0));
2120 /* We should resize the window to a given
2121 * size in pixels here, but currently our
2122 * resizing code isn't healthy enough to
2126 set_zorder(TRUE
); /* move to top */
2129 set_zorder(FALSE
); /* move to bottom */
2135 if (term
->esc_nargs
>= 3) {
2136 if (!cfg
.no_remote_resize
)
2137 request_resize(def(term
->esc_args
[2], cfg
.width
),
2138 def(term
->esc_args
[1], cfg
.height
));
2142 if (term
->esc_nargs
>= 2)
2143 set_zoomed(term
->esc_args
[1] ?
2147 ldisc_send(is_iconic() ?
"\033[1t" : "\033[2t",
2151 get_window_pos(&x
, &y
);
2152 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
2153 ldisc_send(buf
, len
, 0);
2156 get_window_pixels(&x
, &y
);
2157 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
2158 ldisc_send(buf
, len
, 0);
2161 len
= sprintf(buf
, "\033[8;%d;%dt",
2162 term
->rows
, term
->cols
);
2163 ldisc_send(buf
, len
, 0);
2167 * Hmmm. Strictly speaking we
2168 * should return `the size of the
2169 * screen in characters', but
2170 * that's not easy: (a) window
2171 * furniture being what it is it's
2172 * hard to compute, and (b) in
2173 * resize-font mode maximising the
2174 * window wouldn't change the
2175 * number of characters. *shrug*. I
2176 * think we'll ignore it for the
2177 * moment and see if anyone
2178 * complains, and then ask them
2179 * what they would like it to do.
2183 p
= get_window_title(TRUE
);
2185 ldisc_send("\033]L", 3, 0);
2186 ldisc_send(p
, len
, 0);
2187 ldisc_send("\033\\", 2, 0);
2190 p
= get_window_title(FALSE
);
2192 ldisc_send("\033]l", 3, 0);
2193 ldisc_send(p
, len
, 0);
2194 ldisc_send("\033\\", 2, 0);
2200 compatibility(SCOANSI
);
2201 scroll(term
, term
->marg_t
, term
->marg_b
,
2202 def(term
->esc_args
[0], 1), TRUE
);
2204 term
->wrapnext
= FALSE
;
2205 term
->seen_disp_event
= TRUE
;
2208 compatibility(SCOANSI
);
2209 scroll(term
, term
->marg_t
, term
->marg_b
,
2210 -def(term
->esc_args
[0], 1), TRUE
);
2212 term
->wrapnext
= FALSE
;
2213 term
->seen_disp_event
= TRUE
;
2215 case ANSI('|', '*'):
2216 /* VT420 sequence DECSNLS
2217 * Set number of lines on screen
2218 * VT420 uses VGA like hardware and can support any size in
2219 * reasonable range (24..49 AIUI) with no default specified.
2221 compatibility(VT420
);
2222 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
2223 if (!cfg
.no_remote_resize
)
2224 request_resize(term
->cols
,
2225 def(term
->esc_args
[0],
2230 case ANSI('|', '$'):
2231 /* VT340/VT420 sequence DECSCPP
2232 * Set number of columns per page
2233 * Docs imply range is only 80 or 132, but I'll allow any.
2235 compatibility(VT340TEXT
);
2236 if (term
->esc_nargs
<= 1) {
2237 if (!cfg
.no_remote_resize
)
2238 request_resize(def(term
->esc_args
[0],
2239 cfg
.width
), term
->rows
);
2243 case 'X': /* write N spaces w/o moving cursor */
2244 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2245 compatibility(ANSIMIN
);
2247 int n
= def(term
->esc_args
[0], 1);
2249 unsigned long *p
= term
->cpos
;
2250 if (n
> term
->cols
- term
->curs
.x
)
2251 n
= term
->cols
- term
->curs
.x
;
2252 cursplus
= term
->curs
;
2254 check_selection(term
, term
->curs
, cursplus
);
2256 *p
++ = term
->erase_char
;
2257 term
->seen_disp_event
= TRUE
;
2260 case 'x': /* report terminal characteristics */
2261 compatibility(VT100
);
2264 int i
= def(term
->esc_args
[0], 0);
2265 if (i
== 0 || i
== 1) {
2266 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
2268 ldisc_send(buf
, 20, 0);
2272 case 'Z': /* BackTab for xterm */
2273 compatibility(OTHER
);
2275 int i
= def(term
->esc_args
[0], 1);
2276 pos old_curs
= term
->curs
;
2278 for(;i
>0 && term
->curs
.x
>0; i
--) {
2281 } while (term
->curs
.x
>0 &&
2282 !term
->tabs
[term
->curs
.x
]);
2285 check_selection(term
, old_curs
, term
->curs
);
2288 case ANSI('L', '='):
2289 compatibility(OTHER
);
2290 term
->use_bce
= (term
->esc_args
[0] <= 0);
2291 term
->erase_char
= ERASE_CHAR
;
2293 term
->erase_char
= (' ' | ATTR_ASCII
|
2295 (ATTR_FGMASK
| ATTR_BGMASK
)));
2297 case ANSI('E', '='):
2298 compatibility(OTHER
);
2299 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
2301 case ANSI('p', '"'):
2303 * Allow the host to make this emulator a
2304 * 'perfect' VT102. This first appeared in
2305 * the VT220, but we do need to get back to
2306 * PuTTY mode so I won't check it.
2308 * The arg in 40..42,50 are a PuTTY extension.
2309 * The 2nd arg, 8bit vs 7bit is not checked.
2311 * Setting VT102 mode should also change
2312 * the Fkeys to generate PF* codes as a
2313 * real VT102 has no Fkeys. The VT220 does
2314 * this, F11..F13 become ESC,BS,LF other
2315 * Fkeys send nothing.
2317 * Note ESC c will NOT change this!
2320 switch (term
->esc_args
[0]) {
2322 term
->compatibility_level
&= ~TM_VTXXX
;
2323 term
->compatibility_level
|= TM_VT102
;
2326 term
->compatibility_level
&= ~TM_VTXXX
;
2327 term
->compatibility_level
|= TM_VT220
;
2331 if (term
->esc_args
[0] > 60 &&
2332 term
->esc_args
[0] < 70)
2333 term
->compatibility_level
|= TM_VTXXX
;
2337 term
->compatibility_level
&= TM_VTXXX
;
2340 term
->compatibility_level
= TM_PUTTY
;
2343 term
->compatibility_level
= TM_SCOANSI
;
2347 term
->compatibility_level
= TM_PUTTY
;
2353 /* Change the response to CSI c */
2354 if (term
->esc_args
[0] == 50) {
2357 strcpy(term
->id_string
, "\033[?");
2358 for (i
= 1; i
< term
->esc_nargs
; i
++) {
2360 strcat(term
->id_string
, ";");
2361 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
2362 strcat(term
->id_string
, lbuf
);
2364 strcat(term
->id_string
, "c");
2367 /* Is this a good idea ?
2368 * Well we should do a soft reset at this point ...
2370 if (!has_compat(VT420
) && has_compat(VT100
)) {
2371 if (!cfg
.no_remote_resize
) {
2372 if (term
->reset_132
)
2373 request_resize(132, 24);
2375 request_resize(80, 24);
2383 term
->osc_w
= FALSE
;
2385 case 'P': /* Linux palette sequence */
2386 term
->termstate
= SEEN_OSC_P
;
2387 term
->osc_strlen
= 0;
2389 case 'R': /* Linux palette reset */
2391 term_invalidate(term
);
2392 term
->termstate
= TOPLEVEL
;
2394 case 'W': /* word-set */
2395 term
->termstate
= SEEN_OSC_W
;
2408 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2412 * Grotty hack to support xterm and DECterm title
2413 * sequences concurrently.
2415 if (term
->esc_args
[0] == 2) {
2416 term
->esc_args
[0] = 1;
2419 /* else fall through */
2421 term
->termstate
= OSC_STRING
;
2422 term
->osc_strlen
= 0;
2427 * This OSC stuff is EVIL. It takes just one character to get into
2428 * sysline mode and it's not initially obvious how to get out.
2429 * So I've added CR and LF as string aborts.
2430 * This shouldn't effect compatibility as I believe embedded
2431 * control characters are supposed to be interpreted (maybe?)
2432 * and they don't display anything useful anyway.
2436 if (c
== '\n' || c
== '\r') {
2437 term
->termstate
= TOPLEVEL
;
2438 } else if (c
== 0234 || c
== '\007') {
2440 * These characters terminate the string; ST and BEL
2441 * terminate the sequence and trigger instant
2442 * processing of it, whereas ESC goes back to SEEN_ESC
2443 * mode unless it is followed by \, in which case it is
2444 * synonymous with ST in the first place.
2447 term
->termstate
= TOPLEVEL
;
2448 } else if (c
== '\033')
2449 term
->termstate
= OSC_MAYBE_ST
;
2450 else if (term
->osc_strlen
< OSC_STR_MAX
)
2451 term
->osc_string
[term
->osc_strlen
++] = c
;
2455 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
2457 if (c
>= '0' && c
<= '9')
2459 else if (c
>= 'A' && c
<= 'A' + max
- 10)
2461 else if (c
>= 'a' && c
<= 'a' + max
- 10)
2464 term
->termstate
= TOPLEVEL
;
2467 term
->osc_string
[term
->osc_strlen
++] = val
;
2468 if (term
->osc_strlen
>= 7) {
2469 palette_set(term
->osc_string
[0],
2470 term
->osc_string
[1] * 16 + term
->osc_string
[2],
2471 term
->osc_string
[3] * 16 + term
->osc_string
[4],
2472 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
2473 term_invalidate(term
);
2474 term
->termstate
= TOPLEVEL
;
2490 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2493 term
->termstate
= OSC_STRING
;
2494 term
->osc_strlen
= 0;
2498 term
->termstate
= TOPLEVEL
;
2499 term
->seen_disp_event
= TRUE
;
2502 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
2505 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
2508 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
2511 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
2514 * From the VT100 Manual
2515 * NOTE: The special graphics characters in the VT100
2516 * are different from those in the VT52
2518 * From VT102 manual:
2519 * 137 _ Blank - Same
2520 * 140 ` Reserved - Humm.
2521 * 141 a Solid rectangle - Similar
2522 * 142 b 1/ - Top half of fraction for the
2523 * 143 c 3/ - subscript numbers below.
2526 * 146 f Degrees - Same
2527 * 147 g Plus or minus - Same
2529 * 151 i Ellipsis (dots)
2532 * 154 l Bar at scan 0
2533 * 155 m Bar at scan 1
2534 * 156 n Bar at scan 2
2535 * 157 o Bar at scan 3 - Similar
2536 * 160 p Bar at scan 4 - Similar
2537 * 161 q Bar at scan 5 - Similar
2538 * 162 r Bar at scan 6 - Same
2539 * 163 s Bar at scan 7 - Similar
2554 term
->cset_attr
[term
->cset
= 0] = ATTR_LINEDRW
;
2557 term
->cset_attr
[term
->cset
= 0] = ATTR_ASCII
;
2560 move(term
, 0, 0, 0);
2563 if (term
->curs
.y
== 0)
2564 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
2565 else if (term
->curs
.y
> 0)
2568 term
->wrapnext
= FALSE
;
2571 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2575 erase_lots(term
, TRUE
, FALSE
, TRUE
);
2579 /* XXX Print cursor line */
2582 /* XXX Start controller mode */
2585 /* XXX Stop controller mode */
2589 term
->termstate
= VT52_Y1
;
2592 ldisc_send("\033/Z", 3, 0);
2595 term
->app_keypad_keys
= TRUE
;
2598 term
->app_keypad_keys
= FALSE
;
2601 /* XXX This should switch to VT100 mode not current or default
2602 * VT mode. But this will only have effect in a VT220+
2605 term
->vt52_mode
= FALSE
;
2606 term
->blink_is_real
= cfg
.blinktext
;
2610 /* XXX Enter auto print mode */
2613 /* XXX Exit auto print mode */
2616 /* XXX Print screen */
2622 /* compatibility(ATARI) */
2623 move(term
, 0, 0, 0);
2624 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2628 /* compatibility(ATARI) */
2629 if (term
->curs
.y
<= term
->marg_b
)
2630 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
2633 /* compatibility(ATARI) */
2634 if (term
->curs
.y
<= term
->marg_b
)
2635 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
2638 /* compatibility(ATARI) */
2639 term
->termstate
= VT52_FG
;
2642 /* compatibility(ATARI) */
2643 term
->termstate
= VT52_BG
;
2646 /* compatibility(ATARI) */
2647 erase_lots(term
, FALSE
, TRUE
, FALSE
);
2651 /* compatibility(ATARI) */
2652 term
->cursor_on
= TRUE
;
2655 /* compatibility(ATARI) */
2656 term
->cursor_on
= FALSE
;
2658 /* case 'j': Save cursor position - broken on ST */
2659 /* case 'k': Restore cursor position */
2661 /* compatibility(ATARI) */
2662 erase_lots(term
, TRUE
, TRUE
, TRUE
);
2664 term
->wrapnext
= FALSE
;
2668 /* compatibility(ATARI) */
2669 erase_lots(term
, TRUE
, TRUE
, FALSE
);
2672 /* compatibility(ATARI) */
2673 term
->curr_attr
|= ATTR_REVERSE
;
2676 /* compatibility(ATARI) */
2677 term
->curr_attr
&= ~ATTR_REVERSE
;
2679 case 'v': /* wrap Autowrap on - Wyse style */
2680 /* compatibility(ATARI) */
2683 case 'w': /* Autowrap off */
2684 /* compatibility(ATARI) */
2689 /* compatibility(OTHER) */
2690 term
->vt52_bold
= FALSE
;
2691 term
->curr_attr
= ATTR_DEFAULT
;
2693 term
->erase_char
= (' ' | ATTR_ASCII
|
2695 (ATTR_FGMASK
| ATTR_BGMASK
)));
2698 /* compatibility(VI50) */
2699 term
->curr_attr
|= ATTR_UNDER
;
2702 /* compatibility(VI50) */
2703 term
->curr_attr
&= ~ATTR_UNDER
;
2706 /* compatibility(VI50) */
2707 term
->vt52_bold
= TRUE
;
2708 term
->curr_attr
|= ATTR_BOLD
;
2711 /* compatibility(VI50) */
2712 term
->vt52_bold
= FALSE
;
2713 term
->curr_attr
&= ~ATTR_BOLD
;
2719 term
->termstate
= VT52_Y2
;
2720 move(term
, term
->curs
.x
, c
- ' ', 0);
2723 term
->termstate
= TOPLEVEL
;
2724 move(term
, c
- ' ', term
->curs
.y
, 0);
2729 term
->termstate
= TOPLEVEL
;
2730 term
->curr_attr
&= ~ATTR_FGMASK
;
2731 term
->curr_attr
&= ~ATTR_BOLD
;
2732 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
2733 if ((c
& 0x8) || term
->vt52_bold
)
2734 term
->curr_attr
|= ATTR_BOLD
;
2737 term
->erase_char
= (' ' | ATTR_ASCII
|
2739 (ATTR_FGMASK
| ATTR_BGMASK
)));
2742 term
->termstate
= TOPLEVEL
;
2743 term
->curr_attr
&= ~ATTR_BGMASK
;
2744 term
->curr_attr
&= ~ATTR_BLINK
;
2745 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
2747 /* Note: bold background */
2749 term
->curr_attr
|= ATTR_BLINK
;
2752 term
->erase_char
= (' ' | ATTR_ASCII
|
2754 (ATTR_FGMASK
| ATTR_BGMASK
)));
2757 default: break; /* placate gcc warning about enum use */
2759 if (term
->selstate
!= NO_SELECTION
) {
2760 pos cursplus
= term
->curs
;
2762 check_selection(term
, term
->curs
, cursplus
);
2766 term_print_flush(term
);
2771 * Compare two lines to determine whether they are sufficiently
2772 * alike to scroll-optimise one to the other. Return the degree of
2775 static int linecmp(Terminal
*term
, unsigned long *a
, unsigned long *b
)
2779 for (i
= n
= 0; i
< term
->cols
; i
++)
2780 n
+= (*a
++ == *b
++);
2786 * Given a context, update the window. Out of paranoia, we don't
2787 * allow WM_PAINT responses to do scrolling optimisations.
2789 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
2791 int i
, j
, our_curs_y
;
2792 unsigned long rv
, cursor
;
2795 long cursor_background
= ERASE_CHAR
;
2796 unsigned long ticks
;
2799 * Check the visual bell state.
2801 if (term
->in_vbell
) {
2802 ticks
= GETTICKCOUNT();
2803 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2804 term
->in_vbell
= FALSE
;
2807 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
2810 * screen array, disptop, scrtop,
2812 * cfg.blinkpc, blink_is_real, tblinker,
2813 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2816 /* Has the cursor position or type changed ? */
2817 if (term
->cursor_on
) {
2818 if (term
->has_focus
) {
2819 if (term
->blinker
|| !cfg
.blink_cur
)
2820 cursor
= TATTR_ACTCURS
;
2824 cursor
= TATTR_PASCURS
;
2826 cursor
|= TATTR_RIGHTCURS
;
2829 our_curs_y
= term
->curs
.y
- term
->disptop
;
2831 if (term
->dispcurs
&& (term
->curstype
!= cursor
||
2833 term
->disptext
+ our_curs_y
* (term
->cols
+ 1) +
2835 if (term
->dispcurs
> term
->disptext
&&
2836 (*term
->dispcurs
& (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2837 term
->dispcurs
[-1] |= ATTR_INVALID
;
2838 if ( (term
->dispcurs
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2839 term
->dispcurs
[1] |= ATTR_INVALID
;
2840 *term
->dispcurs
|= ATTR_INVALID
;
2843 term
->dispcurs
= NULL
;
2845 /* The normal screen data */
2846 for (i
= 0; i
< term
->rows
; i
++) {
2847 unsigned long *ldata
;
2849 int idx
, dirty_line
, dirty_run
, selected
;
2850 unsigned long attr
= 0;
2851 int updated_line
= 0;
2854 int last_run_dirty
= 0;
2856 scrpos
.y
= i
+ term
->disptop
;
2857 ldata
= lineptr(scrpos
.y
);
2858 lattr
= (ldata
[term
->cols
] & LATTR_MODE
);
2860 idx
= i
* (term
->cols
+ 1);
2861 dirty_run
= dirty_line
= (ldata
[term
->cols
] !=
2862 term
->disptext
[idx
+ term
->cols
]);
2863 term
->disptext
[idx
+ term
->cols
] = ldata
[term
->cols
];
2865 for (j
= 0; j
< term
->cols
; j
++, idx
++) {
2866 unsigned long tattr
, tchar
;
2867 unsigned long *d
= ldata
+ j
;
2871 tchar
= (*d
& (CHAR_MASK
| CSET_MASK
));
2872 tattr
= (*d
& (ATTR_MASK
^ CSET_MASK
));
2873 switch (tchar
& CSET_MASK
) {
2875 tchar
= unitab_line
[tchar
& 0xFF];
2878 tchar
= unitab_xterm
[tchar
& 0xFF];
2881 tchar
= unitab_scoacs
[tchar
&0xFF];
2884 tattr
|= (tchar
& CSET_MASK
);
2886 if ((d
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2889 /* Video reversing things */
2890 if (term
->seltype
== LEXICOGRAPHIC
)
2891 selected
= (posle(term
->selstart
, scrpos
) &&
2892 poslt(scrpos
, term
->selend
));
2894 selected
= (posPle(term
->selstart
, scrpos
) &&
2895 posPlt(scrpos
, term
->selend
));
2897 ^ (selected ? ATTR_REVERSE
: 0));
2899 /* 'Real' blinking ? */
2900 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
2901 if (term
->has_focus
&& term
->tblinker
) {
2903 tattr
&= ~CSET_MASK
;
2906 tattr
&= ~ATTR_BLINK
;
2910 * Check the font we'll _probably_ be using to see if
2911 * the character is wide when we don't want it to be.
2913 if ((tchar
| tattr
) != (term
->disptext
[idx
]& ~ATTR_NARROW
)) {
2914 if ((tattr
& ATTR_WIDE
) == 0 &&
2915 CharWidth(ctx
, (tchar
| tattr
) & 0xFFFF) == 2)
2916 tattr
|= ATTR_NARROW
;
2917 } else if (term
->disptext
[idx
]&ATTR_NARROW
)
2918 tattr
|= ATTR_NARROW
;
2920 /* Cursor here ? Save the 'background' */
2921 if (i
== our_curs_y
&& j
== term
->curs
.x
) {
2922 cursor_background
= tattr
| tchar
;
2923 term
->dispcurs
= term
->disptext
+ idx
;
2926 if ((term
->disptext
[idx
] ^ tattr
) & ATTR_WIDE
)
2929 break_run
= (tattr
!= attr
|| j
- start
>= sizeof(ch
));
2931 /* Special hack for VT100 Linedraw glyphs */
2932 if ((attr
& CSET_MASK
) == 0x2300 && tchar
>= 0xBA
2933 && tchar
<= 0xBD) break_run
= TRUE
;
2935 if (!dbcs_screenfont
&& !dirty_line
) {
2936 if ((tchar
| tattr
) == term
->disptext
[idx
])
2938 else if (!dirty_run
&& ccount
== 1)
2943 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
2944 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
2950 if (dbcs_screenfont
)
2951 last_run_dirty
= dirty_run
;
2952 dirty_run
= dirty_line
;
2955 if ((tchar
| tattr
) != term
->disptext
[idx
])
2957 ch
[ccount
++] = (char) tchar
;
2958 term
->disptext
[idx
] = tchar
| tattr
;
2960 /* If it's a wide char step along to the next one. */
2961 if (tattr
& ATTR_WIDE
) {
2962 if (++j
< term
->cols
) {
2965 /* Cursor is here ? Ouch! */
2966 if (i
== our_curs_y
&& j
== term
->curs
.x
) {
2967 cursor_background
= *d
;
2968 term
->dispcurs
= term
->disptext
+ idx
;
2970 if (term
->disptext
[idx
] != *d
)
2972 term
->disptext
[idx
] = *d
;
2976 if (dirty_run
&& ccount
> 0) {
2977 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
2981 /* Cursor on this line ? (and changed) */
2982 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
2983 ch
[0] = (char) (cursor_background
& CHAR_MASK
);
2984 attr
= (cursor_background
& ATTR_MASK
) | cursor
;
2985 do_cursor(ctx
, term
->curs
.x
, i
, ch
, 1, attr
, lattr
);
2986 term
->curstype
= cursor
;
2992 * Flick the switch that says if blinking things should be shown or hidden.
2995 void term_blink(Terminal
*term
, int flg
)
2997 long now
, blink_diff
;
2999 now
= GETTICKCOUNT();
3000 blink_diff
= now
- term
->last_tblink
;
3002 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3003 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
3004 term
->last_tblink
= now
;
3005 term
->tblinker
= !term
->tblinker
;
3010 term
->last_blink
= now
;
3014 blink_diff
= now
- term
->last_blink
;
3016 /* Make sure the cursor blinks no faster than system blink rate */
3017 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
3020 term
->last_blink
= now
;
3021 term
->blinker
= !term
->blinker
;
3025 * Invalidate the whole screen so it will be repainted in full.
3027 void term_invalidate(Terminal
*term
)
3031 for (i
= 0; i
< term
->rows
* (term
->cols
+ 1); i
++)
3032 term
->disptext
[i
] = ATTR_INVALID
;
3036 * Paint the window in response to a WM_PAINT message.
3038 void term_paint(Terminal
*term
, Context ctx
,
3039 int left
, int top
, int right
, int bottom
)
3042 if (left
< 0) left
= 0;
3043 if (top
< 0) top
= 0;
3044 if (right
>= term
->cols
) right
= term
->cols
-1;
3045 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
3047 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
3048 if ((term
->disptext
[i
* (term
->cols
+ 1) + term
->cols
] &
3049 LATTR_MODE
) == LATTR_NORM
)
3050 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
3051 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3053 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
3054 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3057 /* This should happen soon enough, also for some reason it sometimes
3058 * fails to actually do anything when re-sizing ... painting the wrong
3062 do_paint (term
, ctx
, FALSE
);
3066 * Attempt to scroll the scrollback. The second parameter gives the
3067 * position we want to scroll to; the first is +1 to denote that
3068 * this position is relative to the beginning of the scrollback, -1
3069 * to denote it is relative to the end, and 0 to denote that it is
3070 * relative to the current position.
3072 void term_scroll(Terminal
*term
, int rel
, int where
)
3074 int sbtop
= -count234(term
->scrollback
);
3076 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
3077 if (term
->disptop
< sbtop
)
3078 term
->disptop
= sbtop
;
3079 if (term
->disptop
> 0)
3085 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
)
3088 wchar_t *wbptr
; /* where next char goes within workbuf */
3090 int wblen
= 0; /* workbuf len */
3091 int buflen
; /* amount of memory allocated to workbuf */
3093 buflen
= 5120; /* Default size */
3094 workbuf
= smalloc(buflen
* sizeof(wchar_t));
3095 wbptr
= workbuf
; /* start filling here */
3096 old_top_x
= top
.x
; /* needed for rect==1 */
3098 while (poslt(top
, bottom
)) {
3100 unsigned long *ldata
= lineptr(top
.y
);
3104 * nlpos will point at the maximum position on this line we
3105 * should copy up to. So we start it at the end of the
3109 nlpos
.x
= term
->cols
;
3112 * ... move it backwards if there's unused space at the end
3113 * of the line (and also set `nl' if this is the case,
3114 * because in normal selection mode this means we need a
3115 * newline at the end)...
3117 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3118 while (((ldata
[nlpos
.x
- 1] & 0xFF) == 0x20 ||
3119 (DIRECT_CHAR(ldata
[nlpos
.x
- 1]) &&
3120 (ldata
[nlpos
.x
- 1] & CHAR_MASK
) == 0x20))
3121 && poslt(top
, nlpos
))
3123 if (poslt(nlpos
, bottom
))
3128 * ... and then clip it to the terminal x coordinate if
3129 * we're doing rectangular selection. (In this case we
3130 * still did the above, so that copying e.g. the right-hand
3131 * column from a table doesn't fill with spaces on the
3135 if (nlpos
.x
> bottom
.x
)
3137 nl
= (top
.y
< bottom
.y
);
3140 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
3143 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
3145 wchar_t cbuf
[16], *p
;
3146 int uc
= (ldata
[top
.x
] & 0xFFFF);
3149 if (uc
== UCSWIDE
) {
3154 switch (uc
& CSET_MASK
) {
3157 uc
= unitab_xterm
[uc
& 0xFF];
3161 uc
= unitab_line
[uc
& 0xFF];
3164 uc
= unitab_scoacs
[uc
&0xFF];
3167 switch (uc
& CSET_MASK
) {
3169 uc
= unitab_font
[uc
& 0xFF];
3172 uc
= unitab_oemcp
[uc
& 0xFF];
3176 set
= (uc
& CSET_MASK
);
3177 c
= (uc
& CHAR_MASK
);
3181 if (DIRECT_FONT(uc
)) {
3182 if (c
>= ' ' && c
!= 0x7F) {
3183 unsigned char buf
[4];
3186 if (is_dbcs_leadbyte(font_codepage
, (BYTE
) c
)) {
3188 buf
[1] = (unsigned char) ldata
[top
.x
+ 1];
3189 rv
= mb_to_wc(font_codepage
, 0, buf
, 2, wbuf
, 4);
3193 rv
= mb_to_wc(font_codepage
, 0, buf
, 1, wbuf
, 4);
3197 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
3204 for (p
= cbuf
; *p
; p
++) {
3205 /* Enough overhead for trailing NL and nul */
3206 if (wblen
>= buflen
- 16) {
3209 sizeof(wchar_t) * (buflen
+= 100));
3210 wbptr
= workbuf
+ wblen
;
3219 for (i
= 0; i
< sel_nl_sz
; i
++) {
3221 *wbptr
++ = sel_nl
[i
];
3225 top
.x
= rect ? old_top_x
: 0;
3227 #if SELECTION_NUL_TERMINATED
3231 write_clip(workbuf
, wblen
, FALSE
); /* transfer to clipboard */
3232 if (buflen
> 0) /* indicates we allocated this buffer */
3236 void term_copyall(Terminal
*term
)
3239 top
.y
= -count234(term
->scrollback
);
3241 clipme(term
, top
, term
->curs
, 0);
3245 * The wordness array is mainly for deciding the disposition of the
3246 * US-ASCII characters.
3248 static int wordtype(Terminal
*term
, int uc
)
3251 int start
, end
, ctype
;
3252 } *wptr
, ucs_words
[] = {
3258 0x037e, 0x037e, 1}, /* Greek question mark */
3260 0x0387, 0x0387, 1}, /* Greek ano teleia */
3262 0x055a, 0x055f, 1}, /* Armenian punctuation */
3264 0x0589, 0x0589, 1}, /* Armenian full stop */
3266 0x0700, 0x070d, 1}, /* Syriac punctuation */
3268 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3270 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3272 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3274 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3276 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3278 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3280 0x2000, 0x200a, 0}, /* Various spaces */
3282 0x2070, 0x207f, 2}, /* superscript */
3284 0x2080, 0x208f, 2}, /* subscript */
3286 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3288 0x3000, 0x3000, 0}, /* ideographic space */
3290 0x3001, 0x3020, 1}, /* ideographic punctuation */
3292 0x303f, 0x309f, 3}, /* Hiragana */
3294 0x30a0, 0x30ff, 3}, /* Katakana */
3296 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3298 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3300 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3302 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3304 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3306 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3308 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3310 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3312 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3317 uc
&= (CSET_MASK
| CHAR_MASK
);
3319 switch (uc
& CSET_MASK
) {
3321 uc
= unitab_xterm
[uc
& 0xFF];
3324 uc
= unitab_line
[uc
& 0xFF];
3327 uc
= unitab_scoacs
[uc
&0xFF];
3330 switch (uc
& CSET_MASK
) {
3332 uc
= unitab_font
[uc
& 0xFF];
3335 uc
= unitab_oemcp
[uc
& 0xFF];
3339 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3340 * fail as there's such a thing as a double width space. :-(
3342 if (dbcs_screenfont
&& font_codepage
== line_codepage
)
3346 return term
->wordness
[uc
];
3348 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
3349 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
3357 * Spread the selection outwards according to the selection mode.
3359 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
3361 unsigned long *ldata
;
3363 int topy
= -count234(term
->scrollback
);
3365 ldata
= lineptr(p
.y
);
3367 switch (term
->selmode
) {
3370 * In this mode, every character is a separate unit, except
3371 * for runs of spaces at the end of a non-wrapping line.
3373 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3374 unsigned long *q
= ldata
+ term
->cols
;
3375 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
3377 if (q
== ldata
+ term
->cols
)
3379 if (p
.x
>= q
- ldata
)
3380 p
.x
= (dir
== -1 ? q
- ldata
: term
->cols
- 1);
3385 * In this mode, the units are maximal runs of characters
3386 * whose `wordness' has the same value.
3388 wvalue
= wordtype(term
, ldata
[p
.x
]);
3391 if (p
.x
< term
->cols
-1) {
3392 if (wordtype(term
, ldata
[p
.x
+ 1]) == wvalue
)
3397 if (ldata
[term
->cols
] & LATTR_WRAPPED
) {
3398 unsigned long *ldata2
;
3399 ldata2
= lineptr(p
.y
+1);
3400 if (wordtype(term
, ldata2
[0]) == wvalue
) {
3413 if (wordtype(term
, ldata
[p
.x
- 1]) == wvalue
)
3418 unsigned long *ldata2
;
3421 ldata2
= lineptr(p
.y
-1);
3422 if ((ldata2
[term
->cols
] & LATTR_WRAPPED
) &&
3423 wordtype(term
, ldata2
[term
->cols
-1]) == wvalue
) {
3435 * In this mode, every line is a unit.
3437 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
3443 static void sel_spread(Terminal
*term
)
3445 if (term
->seltype
== LEXICOGRAPHIC
) {
3446 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
3447 decpos(term
->selend
);
3448 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
3449 incpos(term
->selend
);
3453 void term_do_paste(Terminal
*term
)
3458 get_clip(&data
, &len
);
3459 if (data
&& len
> 0) {
3462 term_seen_key_event(term
); /* pasted data counts */
3464 if (term
->paste_buffer
)
3465 sfree(term
->paste_buffer
);
3466 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3467 term
->paste_buffer
= smalloc(len
* sizeof(wchar_t));
3470 while (p
< data
+ len
) {
3471 while (p
< data
+ len
&&
3472 !(p
<= data
+ len
- sel_nl_sz
&&
3473 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
3478 for (i
= 0; i
< p
- q
; i
++) {
3479 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
3483 if (p
<= data
+ len
- sel_nl_sz
&&
3484 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
3485 term
->paste_buffer
[term
->paste_len
++] = '\r';
3491 /* Assume a small paste will be OK in one go. */
3492 if (term
->paste_len
< 256) {
3493 luni_send(term
->paste_buffer
, term
->paste_len
, 0);
3494 if (term
->paste_buffer
)
3495 sfree(term
->paste_buffer
);
3496 term
->paste_buffer
= 0;
3497 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3500 get_clip(NULL
, NULL
);
3503 void term_mouse(Terminal
*term
, Mouse_Button b
, Mouse_Action a
, int x
, int y
,
3504 int shift
, int ctrl
, int alt
)
3507 unsigned long *ldata
;
3508 int raw_mouse
= (term
->xterm_mouse
&&
3509 !cfg
.no_mouse_rep
&&
3510 !(cfg
.mouse_override
&& shift
));
3511 int default_seltype
;
3515 if (a
== MA_DRAG
&& !raw_mouse
)
3516 term_scroll(term
, 0, -1);
3518 if (y
>= term
->rows
) {
3520 if (a
== MA_DRAG
&& !raw_mouse
)
3521 term_scroll(term
, 0, +1);
3530 if (x
>= term
->cols
)
3533 selpoint
.y
= y
+ term
->disptop
;
3535 ldata
= lineptr(selpoint
.y
);
3536 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
)
3540 int encstate
= 0, r
, c
;
3542 static int is_down
= 0;
3546 encstate
= 0x20; /* left button down */
3557 case MBT_WHEEL_DOWN
:
3560 default: break; /* placate gcc warning about enum use */
3564 if (term
->xterm_mouse
== 1)
3577 default: break; /* placate gcc warning about enum use */
3586 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
3587 ldisc_send(abuf
, 6, 0);
3591 b
= translate_button(b
);
3594 * Set the selection type (rectangular or normal) at the start
3595 * of a selection attempt, from the state of Alt.
3597 if (!alt
^ !cfg
.rect_select
)
3598 default_seltype
= RECTANGULAR
;
3600 default_seltype
= LEXICOGRAPHIC
;
3602 if (term
->selstate
== NO_SELECTION
) {
3603 term
->seltype
= default_seltype
;
3606 if (b
== MBT_SELECT
&& a
== MA_CLICK
) {
3608 term
->selstate
= ABOUT_TO
;
3609 term
->seltype
= default_seltype
;
3610 term
->selanchor
= selpoint
;
3611 term
->selmode
= SM_CHAR
;
3612 } else if (b
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
3614 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
3615 term
->selstate
= DRAGGING
;
3616 term
->selstart
= term
->selanchor
= selpoint
;
3617 term
->selend
= term
->selstart
;
3618 incpos(term
->selend
);
3620 } else if ((b
== MBT_SELECT
&& a
== MA_DRAG
) ||
3621 (b
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
3622 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
3624 if (b
== MBT_EXTEND
&& a
!= MA_DRAG
&& term
->selstate
== SELECTED
) {
3625 if (term
->seltype
== LEXICOGRAPHIC
) {
3627 * For normal selection, we extend by moving
3628 * whichever end of the current selection is closer
3631 if (posdiff(selpoint
, term
->selstart
) <
3632 posdiff(term
->selend
, term
->selstart
) / 2) {
3633 term
->selanchor
= term
->selend
;
3634 decpos(term
->selanchor
);
3636 term
->selanchor
= term
->selstart
;
3640 * For rectangular selection, we have a choice of
3641 * _four_ places to put selanchor and selpoint: the
3642 * four corners of the selection.
3644 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
3645 term
->selanchor
.x
= term
->selend
.x
-1;
3647 term
->selanchor
.x
= term
->selstart
.x
;
3649 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
3650 term
->selanchor
.y
= term
->selend
.y
;
3652 term
->selanchor
.y
= term
->selstart
.y
;
3654 term
->selstate
= DRAGGING
;
3656 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
3657 term
->selanchor
= selpoint
;
3658 term
->selstate
= DRAGGING
;
3659 if (term
->seltype
== LEXICOGRAPHIC
) {
3661 * For normal selection, we set (selstart,selend) to
3662 * (selpoint,selanchor) in some order.
3664 if (poslt(selpoint
, term
->selanchor
)) {
3665 term
->selstart
= selpoint
;
3666 term
->selend
= term
->selanchor
;
3667 incpos(term
->selend
);
3669 term
->selstart
= term
->selanchor
;
3670 term
->selend
= selpoint
;
3671 incpos(term
->selend
);
3675 * For rectangular selection, we may need to
3676 * interchange x and y coordinates (if the user has
3677 * dragged in the -x and +y directions, or vice versa).
3679 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
3680 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
3681 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
3682 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
3685 } else if ((b
== MBT_SELECT
|| b
== MBT_EXTEND
) && a
== MA_RELEASE
) {
3686 if (term
->selstate
== DRAGGING
) {
3688 * We've completed a selection. We now transfer the
3689 * data to the clipboard.
3691 clipme(term
, term
->selstart
, term
->selend
,
3692 (term
->seltype
== RECTANGULAR
));
3693 term
->selstate
= SELECTED
;
3695 term
->selstate
= NO_SELECTION
;
3696 } else if (b
== MBT_PASTE
3698 #if MULTICLICK_ONLY_EVENT
3699 || a
== MA_2CLK
|| a
== MA_3CLK
3708 void term_nopaste(Terminal
*term
)
3710 if (term
->paste_len
== 0)
3712 sfree(term
->paste_buffer
);
3713 term
->paste_buffer
= 0;
3714 term
->paste_len
= 0;
3717 int term_paste_pending(Terminal
*term
)
3719 return term
->paste_len
!= 0;
3722 void term_paste(Terminal
*term
)
3724 long now
, paste_diff
;
3726 if (term
->paste_len
== 0)
3729 /* Don't wait forever to paste */
3730 if (term
->paste_hold
) {
3731 now
= GETTICKCOUNT();
3732 paste_diff
= now
- term
->last_paste
;
3733 if (paste_diff
>= 0 && paste_diff
< 450)
3736 term
->paste_hold
= 0;
3738 while (term
->paste_pos
< term
->paste_len
) {
3740 while (n
+ term
->paste_pos
< term
->paste_len
) {
3741 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\r')
3744 luni_send(term
->paste_buffer
+ term
->paste_pos
, n
, 0);
3745 term
->paste_pos
+= n
;
3747 if (term
->paste_pos
< term
->paste_len
) {
3748 term
->paste_hold
= 1;
3752 sfree(term
->paste_buffer
);
3753 term
->paste_buffer
= NULL
;
3754 term
->paste_len
= 0;
3757 static void deselect(Terminal
*term
)
3759 term
->selstate
= NO_SELECTION
;
3760 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
3763 void term_deselect(Terminal
*term
)
3769 int term_ldisc(Terminal
*term
, int option
)
3771 if (option
== LD_ECHO
)
3772 return term
->term_echoing
;
3773 if (option
== LD_EDIT
)
3774 return term
->term_editing
;
3779 * from_backend(), to get data from the backend for the terminal.
3781 int from_backend(void *vterm
, int is_stderr
, char *data
, int len
)
3783 Terminal
*term
= (Terminal
*)vterm
;
3787 bufchain_add(&term
->inbuf
, data
, len
);
3790 * term_out() always completely empties inbuf. Therefore,
3791 * there's no reason at all to return anything other than zero
3792 * from this function, because there _can't_ be a question of
3793 * the remote side needing to wait until term_out() has cleared
3796 * This is a slightly suboptimal way to deal with SSH2 - in
3797 * principle, the window mechanism would allow us to continue
3798 * to accept data on forwarded ports and X connections even
3799 * while the terminal processing was going slowly - but we
3800 * can't do the 100% right thing without moving the terminal
3801 * processing into a separate thread, and that might hurt
3802 * portability. So we manage stdout buffering the old SSH1 way:
3803 * if the terminal processing goes slowly, the whole SSH
3804 * connection stops accepting data until it's ready.
3806 * In practice, I can't imagine this causing serious trouble.