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_ins
= term
->insert
= FALSE
;
152 term
->alt_wnext
= term
->wrapnext
= term
->save_wnext
= FALSE
;
153 term
->alt_wrap
= term
->wrap
= cfg
.wrap_mode
;
154 term
->alt_cset
= term
->cset
= term
->save_cset
= 0;
155 term
->alt_utf
= term
->utf
= term
->save_utf
= 0;
157 term
->alt_sco_acs
= term
->sco_acs
= term
->save_sco_acs
= 0;
158 term
->cset_attr
[0] = term
->cset_attr
[1] = term
->save_csattr
= ATTR_ASCII
;
160 term
->in_vbell
= FALSE
;
162 term
->big_cursor
= 0;
163 term
->save_attr
= term
->curr_attr
= ATTR_DEFAULT
;
164 term
->term_editing
= term
->term_echoing
= FALSE
;
165 term
->app_cursor_keys
= cfg
.app_cursor
;
166 term
->app_keypad_keys
= cfg
.app_keypad
;
167 term
->use_bce
= cfg
.bce
;
168 term
->blink_is_real
= cfg
.blinktext
;
169 term
->erase_char
= ERASE_CHAR
;
171 term_print_finish(term
);
174 for (i
= 0; i
< 256; i
++)
175 term
->wordness
[i
] = cfg
.wordness
[i
];
178 swap_screen(term
, 1, FALSE
, FALSE
);
179 erase_lots(term
, FALSE
, TRUE
, TRUE
);
180 swap_screen(term
, 0, FALSE
, FALSE
);
181 erase_lots(term
, FALSE
, TRUE
, TRUE
);
186 * Force a screen update.
188 void term_update(Terminal
*term
)
193 int need_sbar_update
= term
->seen_disp_event
;
194 if (term
->seen_disp_event
&& cfg
.scroll_on_disp
) {
195 term
->disptop
= 0; /* return to main screen */
196 term
->seen_disp_event
= 0;
197 need_sbar_update
= TRUE
;
199 if (need_sbar_update
)
201 do_paint(term
, ctx
, TRUE
);
202 sys_cursor(term
->curs
.x
, term
->curs
.y
- term
->disptop
);
208 * Called from front end when a keypress occurs, to trigger
209 * anything magical that needs to happen in that situation.
211 void term_seen_key_event(Terminal
*term
)
214 * On any keypress, clear the bell overload mechanism
215 * completely, on the grounds that large numbers of
216 * beeps coming from deliberate key action are likely
217 * to be intended (e.g. beeps from filename completion
218 * blocking repeatedly).
220 term
->beep_overloaded
= FALSE
;
221 while (term
->beephead
) {
222 struct beeptime
*tmp
= term
->beephead
;
223 term
->beephead
= tmp
->next
;
226 term
->beeptail
= NULL
;
230 * Reset the scrollback on keypress, if we're doing that.
232 if (cfg
.scroll_on_key
) {
233 term
->disptop
= 0; /* return to main screen */
234 term
->seen_disp_event
= 1;
239 * Same as power_on(), but an external function.
241 void term_pwron(Terminal
*term
)
244 if (term
->ldisc
) /* cause ldisc to notice changes */
245 ldisc_send(term
->ldisc
, NULL
, 0, 0);
253 * When the user reconfigures us, we need to check the forbidden-
254 * alternate-screen config option, disable raw mouse mode if the
255 * user has disabled mouse reporting, and abandon a print job if
256 * the user has disabled printing.
258 void term_reconfig(Terminal
*term
)
260 if (cfg
.no_alt_screen
)
261 swap_screen(term
, 0, FALSE
, FALSE
);
262 if (cfg
.no_mouse_rep
) {
263 term
->xterm_mouse
= 0;
264 set_raw_mouse_mode(0);
266 if (cfg
.no_remote_charset
) {
267 term
->cset_attr
[0] = term
->cset_attr
[1] = ATTR_ASCII
;
268 term
->sco_acs
= term
->alt_sco_acs
= 0;
272 term_print_finish(term
);
277 * Clear the scrollback.
279 void term_clrsb(Terminal
*term
)
283 while ((line
= delpos234(term
->scrollback
, 0)) != NULL
) {
290 * Initialise the terminal.
292 Terminal
*term_init(void)
297 * Allocate a new Terminal structure and initialise the fields
300 term
= smalloc(sizeof(Terminal
));
301 term
->compatibility_level
= TM_PUTTY
;
302 strcpy(term
->id_string
, "\033[?6c");
303 term
->last_blink
= term
->last_tblink
= 0;
304 term
->paste_buffer
= NULL
;
305 term
->last_paste
= 0;
306 bufchain_init(&term
->inbuf
);
307 bufchain_init(&term
->printer_buf
);
308 term
->printing
= term
->only_printing
= FALSE
;
309 term
->print_job
= NULL
;
310 term
->vt52_mode
= FALSE
;
311 term
->cr_lf_return
= FALSE
;
312 term
->seen_disp_event
= FALSE
;
313 term
->xterm_mouse
= term
->mouse_is_down
= FALSE
;
314 term
->reset_132
= FALSE
;
315 term
->blinker
= term
->tblinker
= 0;
317 term
->repeat_off
= FALSE
;
318 term
->termstate
= TOPLEVEL
;
319 term
->selstate
= NO_SELECTION
;
321 term
->screen
= term
->alt_screen
= term
->scrollback
= NULL
;
323 term
->disptext
= term
->dispcurs
= NULL
;
326 term
->rows
= term
->cols
= -1;
328 term
->beephead
= term
->beeptail
= NULL
;
330 term
->lastbeep
= FALSE
;
331 term
->beep_overloaded
= FALSE
;
332 term
->resize_fn
= NULL
;
333 term
->resize_ctx
= NULL
;
339 * Set up the terminal for a given size.
341 void term_size(Terminal
*term
, int newrows
, int newcols
, int newsavelines
)
344 unsigned long *newdisp
, *line
;
347 int save_alt_which
= term
->alt_which
;
349 if (newrows
== term
->rows
&& newcols
== term
->cols
&&
350 newsavelines
== term
->savelines
)
351 return; /* nothing to do */
354 swap_screen(term
, 0, FALSE
, FALSE
);
356 term
->alt_t
= term
->marg_t
= 0;
357 term
->alt_b
= term
->marg_b
= newrows
- 1;
359 if (term
->rows
== -1) {
360 term
->scrollback
= newtree234(NULL
);
361 term
->screen
= newtree234(NULL
);
366 * Resize the screen and scrollback. We only need to shift
367 * lines around within our data structures, because lineptr()
368 * will take care of resizing each individual line if
371 * - If the new screen and the old screen differ in length, we
372 * must shunt some lines in from the scrollback or out to
375 * - If doing that fails to provide us with enough material to
376 * fill the new screen (i.e. the number of rows needed in
377 * the new screen exceeds the total number in the previous
378 * screen+scrollback), we must invent some blank lines to
381 * - Then, if the new scrollback length is less than the
382 * amount of scrollback we actually have, we must throw some
385 sblen
= count234(term
->scrollback
);
386 /* Do this loop to expand the screen if newrows > rows */
387 for (i
= term
->rows
; i
< newrows
; i
++) {
389 line
= delpos234(term
->scrollback
, --sblen
);
391 line
= smalloc(TSIZE
* (newcols
+ 2));
393 for (j
= 0; j
<= newcols
; j
++)
394 line
[j
+ 1] = ERASE_CHAR
;
396 addpos234(term
->screen
, line
, 0);
398 /* Do this loop to shrink the screen if newrows < rows */
399 for (i
= newrows
; i
< term
->rows
; i
++) {
400 line
= delpos234(term
->screen
, 0);
401 addpos234(term
->scrollback
, line
, sblen
++);
403 assert(count234(term
->screen
) == newrows
);
404 while (sblen
> newsavelines
) {
405 line
= delpos234(term
->scrollback
, 0);
409 assert(count234(term
->scrollback
) <= newsavelines
);
412 newdisp
= smalloc(newrows
* (newcols
+ 1) * TSIZE
);
413 for (i
= 0; i
< newrows
* (newcols
+ 1); i
++)
414 newdisp
[i
] = ATTR_INVALID
;
415 sfree(term
->disptext
);
416 term
->disptext
= newdisp
;
417 term
->dispcurs
= NULL
;
419 newalt
= newtree234(NULL
);
420 for (i
= 0; i
< newrows
; i
++) {
421 line
= smalloc(TSIZE
* (newcols
+ 2));
423 for (j
= 0; j
<= newcols
; j
++)
424 line
[j
+ 1] = term
->erase_char
;
425 addpos234(newalt
, line
, i
);
427 if (term
->alt_screen
) {
428 while (NULL
!= (line
= delpos234(term
->alt_screen
, 0)))
430 freetree234(term
->alt_screen
);
432 term
->alt_screen
= newalt
;
434 term
->tabs
= srealloc(term
->tabs
, newcols
* sizeof(*term
->tabs
));
437 for (i
= (term
->cols
> 0 ? term
->cols
: 0); i
< newcols
; i
++)
438 term
->tabs
[i
] = (i
% 8 == 0 ? TRUE
: FALSE
);
442 term
->curs
.y
+= newrows
- term
->rows
;
443 if (term
->curs
.y
< 0)
445 if (term
->curs
.y
>= newrows
)
446 term
->curs
.y
= newrows
- 1;
447 if (term
->curs
.x
>= newcols
)
448 term
->curs
.x
= newcols
- 1;
449 term
->alt_x
= term
->alt_y
= 0;
450 term
->wrapnext
= term
->alt_wnext
= FALSE
;
452 term
->rows
= newrows
;
453 term
->cols
= newcols
;
454 term
->savelines
= newsavelines
;
457 swap_screen(term
, save_alt_which
, FALSE
, FALSE
);
462 term
->resize_fn(term
->resize_ctx
, term
->cols
, term
->rows
);
466 * Hand a function and context pointer to the terminal which it can
467 * use to notify a back end of resizes.
469 void term_provide_resize_fn(Terminal
*term
,
470 void (*resize_fn
)(void *, int, int),
473 term
->resize_fn
= resize_fn
;
474 term
->resize_ctx
= resize_ctx
;
475 if (term
->cols
> 0 && term
->rows
> 0)
476 resize_fn(resize_ctx
, term
->cols
, term
->rows
);
480 * Swap screens. If `reset' is TRUE and we have been asked to
481 * switch to the alternate screen, we must bring most of its
482 * configuration from the main screen and erase the contents of the
483 * alternate screen completely. (This is even true if we're already
484 * on it! Blame xterm.)
486 static void swap_screen(Terminal
*term
, int which
, int reset
, int keep_cur_pos
)
492 reset
= FALSE
; /* do no weird resetting if which==0 */
494 if (which
!= term
->alt_which
) {
495 term
->alt_which
= which
;
497 ttr
= term
->alt_screen
;
498 term
->alt_screen
= term
->screen
;
501 if (!reset
&& !keep_cur_pos
)
502 term
->curs
.x
= term
->alt_x
;
505 if (!reset
&& !keep_cur_pos
)
506 term
->curs
.y
= term
->alt_y
;
509 if (!reset
) term
->marg_t
= term
->alt_t
;
512 if (!reset
) term
->marg_b
= term
->alt_b
;
515 if (!reset
) term
->dec_om
= term
->alt_om
;
518 if (!reset
) term
->wrap
= term
->alt_wrap
;
521 if (!reset
) term
->wrapnext
= term
->alt_wnext
;
524 if (!reset
) term
->insert
= term
->alt_ins
;
527 if (!reset
) term
->cset
= term
->alt_cset
;
530 if (!reset
) term
->utf
= term
->alt_utf
;
533 if (!reset
) term
->sco_acs
= term
->alt_sco_acs
;
534 term
->alt_sco_acs
= t
;
537 if (reset
&& term
->screen
) {
539 * Yes, this _is_ supposed to honour background-colour-erase.
541 erase_lots(term
, FALSE
, TRUE
, TRUE
);
545 * This might not be possible if we're called during
553 * Update the scroll bar.
555 static void update_sbar(Terminal
*term
)
559 nscroll
= count234(term
->scrollback
);
561 set_sbar(nscroll
+ term
->rows
, nscroll
+ term
->disptop
, term
->rows
);
565 * Check whether the region bounded by the two pointers intersects
566 * the scroll region, and de-select the on-screen selection if so.
568 static void check_selection(Terminal
*term
, pos from
, pos to
)
570 if (poslt(from
, term
->selend
) && poslt(term
->selstart
, to
))
575 * Scroll the screen. (`lines' is +ve for scrolling forward, -ve
576 * for backward.) `sb' is TRUE if the scrolling is permitted to
577 * affect the scrollback buffer.
579 * NB this function invalidates all pointers into lines of the
580 * screen data structures. In particular, you MUST call fix_cpos
581 * after calling scroll() and before doing anything else that
582 * uses the cpos shortcut pointer.
584 static void scroll(Terminal
*term
, int topline
, int botline
, int lines
, int sb
)
586 unsigned long *line
, *line2
;
589 if (topline
!= 0 || term
->alt_which
!= 0)
594 line
= delpos234(term
->screen
, botline
);
595 line
= resizeline(line
, term
->cols
);
596 for (i
= 0; i
< term
->cols
; i
++)
597 line
[i
+ 1] = term
->erase_char
;
598 line
[term
->cols
+ 1] = 0;
599 addpos234(term
->screen
, line
, topline
);
601 if (term
->selstart
.y
>= topline
&& term
->selstart
.y
<= botline
) {
603 if (term
->selstart
.y
> botline
) {
604 term
->selstart
.y
= botline
;
605 term
->selstart
.x
= 0;
608 if (term
->selend
.y
>= topline
&& term
->selend
.y
<= botline
) {
610 if (term
->selend
.y
> botline
) {
611 term
->selend
.y
= botline
;
620 line
= delpos234(term
->screen
, topline
);
621 if (sb
&& term
->savelines
> 0) {
622 int sblen
= count234(term
->scrollback
);
624 * We must add this line to the scrollback. We'll
625 * remove a line from the top of the scrollback to
626 * replace it, or allocate a new one if the
627 * scrollback isn't full.
629 if (sblen
== term
->savelines
) {
630 sblen
--, line2
= delpos234(term
->scrollback
, 0);
632 line2
= smalloc(TSIZE
* (term
->cols
+ 2));
633 line2
[0] = term
->cols
;
635 addpos234(term
->scrollback
, line
, sblen
);
639 * If the user is currently looking at part of the
640 * scrollback, and they haven't enabled any options
641 * that are going to reset the scrollback as a
642 * result of this movement, then the chances are
643 * they'd like to keep looking at the same line. So
644 * we move their viewpoint at the same rate as the
645 * scroll, at least until their viewpoint hits the
646 * top end of the scrollback buffer, at which point
647 * we don't have the choice any more.
649 * Thanks to Jan Holmen Holsten for the idea and
650 * initial implementation.
652 if (term
->disptop
> -term
->savelines
&& term
->disptop
< 0)
655 line
= resizeline(line
, term
->cols
);
656 for (i
= 0; i
< term
->cols
; i
++)
657 line
[i
+ 1] = term
->erase_char
;
658 line
[term
->cols
+ 1] = 0;
659 addpos234(term
->screen
, line
, botline
);
662 * If the selection endpoints move into the scrollback,
663 * we keep them moving until they hit the top. However,
664 * of course, if the line _hasn't_ moved into the
665 * scrollback then we don't do this, and cut them off
666 * at the top of the scroll region.
668 * This applies to selstart and selend (for an existing
669 * selection), and also selanchor (for one being
670 * selected as we speak).
672 seltop
= sb ?
-term
->savelines
: topline
;
674 if (term
->selstart
.y
>= seltop
&&
675 term
->selstart
.y
<= botline
) {
677 if (term
->selstart
.y
< seltop
) {
678 term
->selstart
.y
= seltop
;
679 term
->selstart
.x
= 0;
682 if (term
->selend
.y
>= seltop
&& term
->selend
.y
<= botline
) {
684 if (term
->selend
.y
< seltop
) {
685 term
->selend
.y
= seltop
;
689 if (term
->selanchor
.y
>= seltop
&& term
->selanchor
.y
<= botline
) {
691 if (term
->selanchor
.y
< seltop
) {
692 term
->selanchor
.y
= seltop
;
693 term
->selanchor
.x
= 0;
703 * Move the cursor to a given position, clipping at boundaries. We
704 * may or may not want to clip at the scroll margin: marg_clip is 0
705 * not to, 1 to disallow _passing_ the margins, and 2 to disallow
706 * even _being_ outside the margins.
708 static void move(Terminal
*term
, int x
, int y
, int marg_clip
)
715 if ((term
->curs
.y
>= term
->marg_t
|| marg_clip
== 2) &&
718 if ((term
->curs
.y
<= term
->marg_b
|| marg_clip
== 2) &&
729 term
->wrapnext
= FALSE
;
733 * Save or restore the cursor and SGR mode.
735 static void save_cursor(Terminal
*term
, int save
)
738 term
->savecurs
= term
->curs
;
739 term
->save_attr
= term
->curr_attr
;
740 term
->save_cset
= term
->cset
;
741 term
->save_utf
= term
->utf
;
742 term
->save_wnext
= term
->wrapnext
;
743 term
->save_csattr
= term
->cset_attr
[term
->cset
];
744 term
->save_sco_acs
= term
->sco_acs
;
746 term
->curs
= term
->savecurs
;
747 /* Make sure the window hasn't shrunk since the save */
748 if (term
->curs
.x
>= term
->cols
)
749 term
->curs
.x
= term
->cols
- 1;
750 if (term
->curs
.y
>= term
->rows
)
751 term
->curs
.y
= term
->rows
- 1;
753 term
->curr_attr
= term
->save_attr
;
754 term
->cset
= term
->save_cset
;
755 term
->utf
= term
->save_utf
;
756 term
->wrapnext
= term
->save_wnext
;
758 * wrapnext might reset to False if the x position is no
759 * longer at the rightmost edge.
761 if (term
->wrapnext
&& term
->curs
.x
< term
->cols
-1)
762 term
->wrapnext
= FALSE
;
763 term
->cset_attr
[term
->cset
] = term
->save_csattr
;
764 term
->sco_acs
= term
->save_sco_acs
;
767 term
->erase_char
= (' ' | ATTR_ASCII
|
769 (ATTR_FGMASK
| ATTR_BGMASK
)));
774 * Erase a large portion of the screen: the whole screen, or the
775 * whole line, or parts thereof.
777 static void erase_lots(Terminal
*term
,
778 int line_only
, int from_begin
, int to_end
)
782 unsigned long *ldata
;
785 start
.y
= term
->curs
.y
;
787 end
.y
= term
->curs
.y
+ 1;
804 check_selection(term
, start
, end
);
806 /* Clear screen also forces a full window redraw, just in case. */
807 if (start
.y
== 0 && start
.x
== 0 && end
.y
== term
->rows
)
808 term_invalidate(term
);
810 ldata
= lineptr(start
.y
);
811 while (poslt(start
, end
)) {
812 if (start
.x
== term
->cols
&& !erase_lattr
)
813 ldata
[start
.x
] &= ~LATTR_WRAPPED
;
815 ldata
[start
.x
] = term
->erase_char
;
816 if (incpos(start
) && start
.y
< term
->rows
)
817 ldata
= lineptr(start
.y
);
822 * Insert or delete characters within the current line. n is +ve if
823 * insertion is desired, and -ve for deletion.
825 static void insch(Terminal
*term
, int n
)
827 int dir
= (n
< 0 ?
-1 : +1);
830 unsigned long *ldata
;
832 n
= (n
< 0 ?
-n
: n
);
833 if (n
> term
->cols
- term
->curs
.x
)
834 n
= term
->cols
- term
->curs
.x
;
835 m
= term
->cols
- term
->curs
.x
- n
;
836 cursplus
.y
= term
->curs
.y
;
837 cursplus
.x
= term
->curs
.x
+ n
;
838 check_selection(term
, term
->curs
, cursplus
);
839 ldata
= lineptr(term
->curs
.y
);
841 memmove(ldata
+ term
->curs
.x
, ldata
+ term
->curs
.x
+ n
, m
* TSIZE
);
843 ldata
[term
->curs
.x
+ m
++] = term
->erase_char
;
845 memmove(ldata
+ term
->curs
.x
+ n
, ldata
+ term
->curs
.x
, m
* TSIZE
);
847 ldata
[term
->curs
.x
+ n
] = term
->erase_char
;
852 * Toggle terminal mode `mode' to state `state'. (`query' indicates
853 * whether the mode is a DEC private one or a normal one.)
855 static void toggle_mode(Terminal
*term
, int mode
, int query
, int state
)
861 case 1: /* application cursor keys */
862 term
->app_cursor_keys
= state
;
864 case 2: /* VT52 mode */
865 term
->vt52_mode
= !state
;
866 if (term
->vt52_mode
) {
867 term
->blink_is_real
= FALSE
;
868 term
->vt52_bold
= FALSE
;
870 term
->blink_is_real
= cfg
.blinktext
;
873 case 3: /* 80/132 columns */
875 if (!cfg
.no_remote_resize
)
876 request_resize(state ?
132 : 80, term
->rows
);
877 term
->reset_132
= state
;
879 case 5: /* reverse video */
881 * Toggle reverse video. If we receive an OFF within the
882 * visual bell timeout period after an ON, we trigger an
883 * effective visual bell, so that ESC[?5hESC[?5l will
884 * always be an actually _visible_ visual bell.
886 ticks
= GETTICKCOUNT();
887 /* turn off a previous vbell to avoid inconsistencies */
888 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
889 term
->in_vbell
= FALSE
;
890 if (term
->rvideo
&& !state
&& /* we're turning it off... */
891 (ticks
- term
->rvbell_startpoint
) < VBELL_TIMEOUT
) {/*...soon*/
892 /* If there's no vbell timeout already, or this one lasts
893 * longer, replace vbell_timeout with ours. */
894 if (!term
->in_vbell
||
895 (term
->rvbell_startpoint
- term
->vbell_startpoint
<
897 term
->vbell_startpoint
= term
->rvbell_startpoint
;
898 term
->in_vbell
= TRUE
; /* may clear rvideo but set in_vbell */
899 } else if (!term
->rvideo
&& state
) {
900 /* This is an ON, so we notice the time and save it. */
901 term
->rvbell_startpoint
= ticks
;
903 term
->rvideo
= state
;
904 term
->seen_disp_event
= TRUE
;
908 case 6: /* DEC origin mode */
909 term
->dec_om
= state
;
911 case 7: /* auto wrap */
914 case 8: /* auto key repeat */
915 term
->repeat_off
= !state
;
917 case 10: /* set local edit mode */
918 term
->term_editing
= state
;
919 if (term
->ldisc
) /* cause ldisc to notice changes */
920 ldisc_send(term
->ldisc
, NULL
, 0, 0);
922 case 25: /* enable/disable cursor */
923 compatibility2(OTHER
, VT220
);
924 term
->cursor_on
= state
;
925 term
->seen_disp_event
= TRUE
;
927 case 47: /* alternate screen */
928 compatibility(OTHER
);
930 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, FALSE
, FALSE
);
933 case 1000: /* xterm mouse 1 */
934 term
->xterm_mouse
= state ?
1 : 0;
935 set_raw_mouse_mode(state
);
937 case 1002: /* xterm mouse 2 */
938 term
->xterm_mouse
= state ?
2 : 0;
939 set_raw_mouse_mode(state
);
941 case 1047: /* alternate screen */
942 compatibility(OTHER
);
944 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, TRUE
, TRUE
);
947 case 1048: /* save/restore cursor */
948 save_cursor(term
, state
);
949 if (!state
) term
->seen_disp_event
= TRUE
;
951 case 1049: /* cursor & alternate screen */
953 save_cursor(term
, state
);
954 if (!state
) term
->seen_disp_event
= TRUE
;
955 compatibility(OTHER
);
957 swap_screen(term
, cfg
.no_alt_screen ?
0 : state
, TRUE
, FALSE
);
959 save_cursor(term
, state
);
964 case 4: /* set insert mode */
965 compatibility(VT102
);
966 term
->insert
= state
;
968 case 12: /* set echo mode */
969 term
->term_echoing
= !state
;
970 if (term
->ldisc
) /* cause ldisc to notice changes */
971 ldisc_send(term
->ldisc
, NULL
, 0, 0);
973 case 20: /* Return sends ... */
974 term
->cr_lf_return
= state
;
976 case 34: /* Make cursor BIG */
977 compatibility2(OTHER
, VT220
);
978 term
->big_cursor
= !state
;
983 * Process an OSC sequence: set window title or icon name.
985 static void do_osc(Terminal
*term
)
988 while (term
->osc_strlen
--)
989 term
->wordness
[(unsigned char)
990 term
->osc_string
[term
->osc_strlen
]] = term
->esc_args
[0];
992 term
->osc_string
[term
->osc_strlen
] = '\0';
993 switch (term
->esc_args
[0]) {
996 if (!cfg
.no_remote_wintitle
)
997 set_icon(term
->osc_string
);
998 if (term
->esc_args
[0] == 1)
1000 /* fall through: parameter 0 means set both */
1003 if (!cfg
.no_remote_wintitle
)
1004 set_title(term
->osc_string
);
1011 * ANSI printing routines.
1013 static void term_print_setup(Terminal
*term
)
1015 bufchain_clear(&term
->printer_buf
);
1016 term
->print_job
= printer_start_job(cfg
.printer
);
1018 static void term_print_flush(Terminal
*term
)
1023 while ((size
= bufchain_size(&term
->printer_buf
)) > 5) {
1024 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1027 printer_job_data(term
->print_job
, data
, len
);
1028 bufchain_consume(&term
->printer_buf
, len
);
1031 static void term_print_finish(Terminal
*term
)
1037 if (!term
->printing
&& !term
->only_printing
)
1038 return; /* we need do nothing */
1040 term_print_flush(term
);
1041 while ((size
= bufchain_size(&term
->printer_buf
)) > 0) {
1042 bufchain_prefix(&term
->printer_buf
, &data
, &len
);
1044 if (c
== '\033' || c
== '\233') {
1045 bufchain_consume(&term
->printer_buf
, size
);
1048 printer_job_data(term
->print_job
, &c
, 1);
1049 bufchain_consume(&term
->printer_buf
, 1);
1052 printer_finish_job(term
->print_job
);
1053 term
->print_job
= NULL
;
1054 term
->printing
= term
->only_printing
= FALSE
;
1058 * Remove everything currently in `inbuf' and stick it up on the
1059 * in-memory display. There's a big state machine in here to
1060 * process escape sequences...
1062 void term_out(Terminal
*term
)
1065 unsigned char localbuf
[256], *chars
;
1070 chars
= NULL
; /* placate compiler warnings */
1071 while (nchars
> 0 || bufchain_size(&term
->inbuf
) > 0) {
1075 bufchain_prefix(&term
->inbuf
, &ret
, &nchars
);
1076 if (nchars
> sizeof(localbuf
))
1077 nchars
= sizeof(localbuf
);
1078 memcpy(localbuf
, ret
, nchars
);
1079 bufchain_consume(&term
->inbuf
, nchars
);
1081 assert(chars
!= NULL
);
1087 * Optionally log the session traffic to a file. Useful for
1088 * debugging and possibly also useful for actual logging.
1090 if (cfg
.logtype
== LGTYP_DEBUG
)
1091 logtraffic((unsigned char) c
, LGTYP_DEBUG
);
1097 /* Note only VT220+ are 8-bit VT102 is seven bit, it shouldn't even
1098 * be able to display 8-bit characters, but I'll let that go 'cause
1103 * If we're printing, add the character to the printer
1106 if (term
->printing
) {
1107 bufchain_add(&term
->printer_buf
, &c
, 1);
1110 * If we're in print-only mode, we use a much simpler
1111 * state machine designed only to recognise the ESC[4i
1112 * termination sequence.
1114 if (term
->only_printing
) {
1116 term
->print_state
= 1;
1117 else if (c
== (unsigned char)'\233')
1118 term
->print_state
= 2;
1119 else if (c
== '[' && term
->print_state
== 1)
1120 term
->print_state
= 2;
1121 else if (c
== '4' && term
->print_state
== 2)
1122 term
->print_state
= 3;
1123 else if (c
== 'i' && term
->print_state
== 3)
1124 term
->print_state
= 4;
1126 term
->print_state
= 0;
1127 if (term
->print_state
== 4) {
1128 term_print_finish(term
);
1134 /* First see about all those translations. */
1135 if (term
->termstate
== TOPLEVEL
) {
1137 switch (term
->utf_state
) {
1140 /* UTF-8 must be stateless so we ignore iso2022. */
1141 if (unitab_ctrl
[c
] != 0xFF)
1143 else c
= ((unsigned char)c
) | ATTR_ASCII
;
1145 } else if ((c
& 0xe0) == 0xc0) {
1146 term
->utf_size
= term
->utf_state
= 1;
1147 term
->utf_char
= (c
& 0x1f);
1148 } else if ((c
& 0xf0) == 0xe0) {
1149 term
->utf_size
= term
->utf_state
= 2;
1150 term
->utf_char
= (c
& 0x0f);
1151 } else if ((c
& 0xf8) == 0xf0) {
1152 term
->utf_size
= term
->utf_state
= 3;
1153 term
->utf_char
= (c
& 0x07);
1154 } else if ((c
& 0xfc) == 0xf8) {
1155 term
->utf_size
= term
->utf_state
= 4;
1156 term
->utf_char
= (c
& 0x03);
1157 } else if ((c
& 0xfe) == 0xfc) {
1158 term
->utf_size
= term
->utf_state
= 5;
1159 term
->utf_char
= (c
& 0x01);
1170 if ((c
& 0xC0) != 0x80) {
1173 term
->utf_state
= 0;
1176 term
->utf_char
= (term
->utf_char
<< 6) | (c
& 0x3f);
1177 if (--term
->utf_state
)
1182 /* Is somebody trying to be evil! */
1184 (c
< 0x800 && term
->utf_size
>= 2) ||
1185 (c
< 0x10000 && term
->utf_size
>= 3) ||
1186 (c
< 0x200000 && term
->utf_size
>= 4) ||
1187 (c
< 0x4000000 && term
->utf_size
>= 5))
1190 /* Unicode line separator and paragraph separator are CR-LF */
1191 if (c
== 0x2028 || c
== 0x2029)
1194 /* High controls are probably a Baaad idea too. */
1198 /* The UTF-16 surrogates are not nice either. */
1199 /* The standard give the option of decoding these:
1200 * I don't want to! */
1201 if (c
>= 0xD800 && c
< 0xE000)
1204 /* ISO 10646 characters now limited to UTF-16 range. */
1208 /* This is currently a TagPhobic application.. */
1209 if (c
>= 0xE0000 && c
<= 0xE007F)
1212 /* U+FEFF is best seen as a null. */
1215 /* But U+FFFE is an error. */
1216 if (c
== 0xFFFE || c
== 0xFFFF)
1219 /* Oops this is a 16bit implementation */
1224 /* Are we in the nasty ACS mode? Note: no sco in utf mode. */
1225 else if(term
->sco_acs
&&
1226 (c
!='\033' && c
!='\n' && c
!='\r' && c
!='\b'))
1228 if (term
->sco_acs
== 2) c
^= 0x80;
1231 switch (term
->cset_attr
[term
->cset
]) {
1233 * Linedraw characters are different from 'ESC ( B'
1234 * only for a small range. For ones outside that
1235 * range, make sure we use the same font as well as
1236 * the same encoding.
1239 if (unitab_ctrl
[c
] != 0xFF)
1242 c
= ((unsigned char) c
) | ATTR_LINEDRW
;
1246 /* If UK-ASCII, make the '#' a LineDraw Pound */
1248 c
= '}' | ATTR_LINEDRW
;
1251 /*FALLTHROUGH*/ case ATTR_ASCII
:
1252 if (unitab_ctrl
[c
] != 0xFF)
1255 c
= ((unsigned char) c
) | ATTR_ASCII
;
1258 if (c
>=' ') c
= ((unsigned char)c
) | ATTR_SCOACS
;
1264 /* How about C1 controls ? */
1265 if ((c
& -32) == 0x80 && term
->termstate
< DO_CTRLS
&&
1266 !term
->vt52_mode
&& has_compat(VT220
)) {
1267 term
->termstate
= SEEN_ESC
;
1268 term
->esc_query
= FALSE
;
1269 c
= '@' + (c
& 0x1F);
1272 /* Or the GL control. */
1273 if (c
== '\177' && term
->termstate
< DO_CTRLS
&& has_compat(OTHER
)) {
1274 if (term
->curs
.x
&& !term
->wrapnext
)
1276 term
->wrapnext
= FALSE
;
1278 if (!cfg
.no_dbackspace
) /* destructive bksp might be disabled */
1279 *term
->cpos
= (' ' | term
->curr_attr
| ATTR_ASCII
);
1281 /* Or normal C0 controls. */
1282 if ((c
& -32) == 0 && term
->termstate
< DO_CTRLS
) {
1284 case '\005': /* terminal type query */
1285 /* Strictly speaking this is VT100 but a VT100 defaults to
1286 * no response. Other terminals respond at their option.
1288 * Don't put a CR in the default string as this tends to
1289 * upset some weird software.
1291 * An xterm returns "xterm" (5 characters)
1293 compatibility(ANSIMIN
);
1295 char abuf
[256], *s
, *d
;
1297 for (s
= cfg
.answerback
, d
= abuf
; *s
; s
++) {
1299 if (*s
>= 'a' && *s
<= 'z')
1300 *d
++ = (*s
- ('a' - 1));
1301 else if ((*s
>= '@' && *s
<= '_') ||
1302 *s
== '?' || (*s
& 0x80))
1307 } else if (*s
== '^') {
1312 lpage_send(term
->ldisc
, DEFAULT_CODEPAGE
,
1318 struct beeptime
*newbeep
;
1319 unsigned long ticks
;
1321 ticks
= GETTICKCOUNT();
1323 if (!term
->beep_overloaded
) {
1324 newbeep
= smalloc(sizeof(struct beeptime
));
1325 newbeep
->ticks
= ticks
;
1326 newbeep
->next
= NULL
;
1327 if (!term
->beephead
)
1328 term
->beephead
= newbeep
;
1330 term
->beeptail
->next
= newbeep
;
1331 term
->beeptail
= newbeep
;
1336 * Throw out any beeps that happened more than
1339 while (term
->beephead
&&
1340 term
->beephead
->ticks
< ticks
- cfg
.bellovl_t
) {
1341 struct beeptime
*tmp
= term
->beephead
;
1342 term
->beephead
= tmp
->next
;
1344 if (!term
->beephead
)
1345 term
->beeptail
= NULL
;
1349 if (cfg
.bellovl
&& term
->beep_overloaded
&&
1350 ticks
- term
->lastbeep
>= (unsigned)cfg
.bellovl_s
) {
1352 * If we're currently overloaded and the
1353 * last beep was more than s seconds ago,
1354 * leave overload mode.
1356 term
->beep_overloaded
= FALSE
;
1357 } else if (cfg
.bellovl
&& !term
->beep_overloaded
&&
1358 term
->nbeeps
>= cfg
.bellovl_n
) {
1360 * Now, if we have n or more beeps
1361 * remaining in the queue, go into overload
1364 term
->beep_overloaded
= TRUE
;
1366 term
->lastbeep
= ticks
;
1369 * Perform an actual beep if we're not overloaded.
1371 if (!cfg
.bellovl
|| !term
->beep_overloaded
) {
1372 if (cfg
.beep
== BELL_VISUAL
) {
1373 term
->in_vbell
= TRUE
;
1374 term
->vbell_startpoint
= ticks
;
1383 if (term
->curs
.x
== 0 &&
1384 (term
->curs
.y
== 0 || term
->wrap
== 0))
1386 else if (term
->curs
.x
== 0 && term
->curs
.y
> 0)
1387 term
->curs
.x
= term
->cols
- 1, term
->curs
.y
--;
1388 else if (term
->wrapnext
)
1389 term
->wrapnext
= FALSE
;
1393 term
->seen_disp_event
= TRUE
;
1396 compatibility(VT100
);
1400 compatibility(VT100
);
1404 if (term
->vt52_mode
)
1405 term
->termstate
= VT52_ESC
;
1407 compatibility(ANSIMIN
);
1408 term
->termstate
= SEEN_ESC
;
1409 term
->esc_query
= FALSE
;
1414 term
->wrapnext
= FALSE
;
1416 term
->seen_disp_event
= TRUE
;
1417 term
->paste_hold
= 0;
1418 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1421 if (has_compat(SCOANSI
)) {
1422 move(term
, 0, 0, 0);
1423 erase_lots(term
, FALSE
, FALSE
, TRUE
);
1425 term
->wrapnext
= FALSE
;
1426 term
->seen_disp_event
= 1;
1430 compatibility(VT100
);
1432 if (term
->curs
.y
== term
->marg_b
)
1433 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1434 else if (term
->curs
.y
< term
->rows
- 1)
1439 term
->wrapnext
= FALSE
;
1440 term
->seen_disp_event
= 1;
1441 term
->paste_hold
= 0;
1442 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1446 pos old_curs
= term
->curs
;
1447 unsigned long *ldata
= lineptr(term
->curs
.y
);
1451 } while (term
->curs
.x
< term
->cols
- 1 &&
1452 !term
->tabs
[term
->curs
.x
]);
1454 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
) {
1455 if (term
->curs
.x
>= term
->cols
/ 2)
1456 term
->curs
.x
= term
->cols
/ 2 - 1;
1458 if (term
->curs
.x
>= term
->cols
)
1459 term
->curs
.x
= term
->cols
- 1;
1463 check_selection(term
, old_curs
, term
->curs
);
1465 term
->seen_disp_event
= TRUE
;
1469 switch (term
->termstate
) {
1471 /* Only graphic characters get this far;
1472 * ctrls are stripped above */
1473 if (term
->wrapnext
&& term
->wrap
) {
1474 term
->cpos
[1] |= LATTR_WRAPPED
;
1475 if (term
->curs
.y
== term
->marg_b
)
1476 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1477 else if (term
->curs
.y
< term
->rows
- 1)
1481 term
->wrapnext
= FALSE
;
1485 if (term
->selstate
!= NO_SELECTION
) {
1486 pos cursplus
= term
->curs
;
1488 check_selection(term
, term
->curs
, cursplus
);
1490 if ((c
& CSET_MASK
) == ATTR_ASCII
|| (c
& CSET_MASK
) == 0)
1491 logtraffic((unsigned char) c
, LGTYP_ASCII
);
1493 extern int wcwidth(wchar_t ucs
);
1498 width
= wcwidth((wchar_t) c
);
1501 *term
->cpos
++ = c
| term
->curr_attr
;
1502 if (++term
->curs
.x
== term
->cols
) {
1503 *term
->cpos
|= LATTR_WRAPPED
;
1504 if (term
->curs
.y
== term
->marg_b
)
1505 scroll(term
, term
->marg_t
, term
->marg_b
,
1507 else if (term
->curs
.y
< term
->rows
- 1)
1512 *term
->cpos
++ = UCSWIDE
| term
->curr_attr
;
1515 *term
->cpos
++ = c
| term
->curr_attr
;
1522 if (term
->curs
.x
== term
->cols
) {
1525 term
->wrapnext
= TRUE
;
1526 if (term
->wrap
&& term
->vt52_mode
) {
1527 term
->cpos
[1] |= LATTR_WRAPPED
;
1528 if (term
->curs
.y
== term
->marg_b
)
1529 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1530 else if (term
->curs
.y
< term
->rows
- 1)
1534 term
->wrapnext
= FALSE
;
1537 term
->seen_disp_event
= 1;
1542 * This state is virtually identical to SEEN_ESC, with the
1543 * exception that we have an OSC sequence in the pipeline,
1544 * and _if_ we see a backslash, we process it.
1548 term
->termstate
= TOPLEVEL
;
1551 /* else fall through */
1553 if (c
>= ' ' && c
<= '/') {
1554 if (term
->esc_query
)
1555 term
->esc_query
= -1;
1557 term
->esc_query
= c
;
1560 term
->termstate
= TOPLEVEL
;
1561 switch (ANSI(c
, term
->esc_query
)) {
1562 case '[': /* enter CSI mode */
1563 term
->termstate
= SEEN_CSI
;
1564 term
->esc_nargs
= 1;
1565 term
->esc_args
[0] = ARG_DEFAULT
;
1566 term
->esc_query
= FALSE
;
1568 case ']': /* xterm escape sequences */
1569 /* Compatibility is nasty here, xterm, linux, decterm yuk! */
1570 compatibility(OTHER
);
1571 term
->termstate
= SEEN_OSC
;
1572 term
->esc_args
[0] = 0;
1574 case '7': /* save cursor */
1575 compatibility(VT100
);
1576 save_cursor(term
, TRUE
);
1578 case '8': /* restore cursor */
1579 compatibility(VT100
);
1580 save_cursor(term
, FALSE
);
1581 term
->seen_disp_event
= TRUE
;
1584 compatibility(VT100
);
1585 term
->app_keypad_keys
= TRUE
;
1588 compatibility(VT100
);
1589 term
->app_keypad_keys
= FALSE
;
1591 case 'D': /* exactly equivalent to LF */
1592 compatibility(VT100
);
1593 if (term
->curs
.y
== term
->marg_b
)
1594 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1595 else if (term
->curs
.y
< term
->rows
- 1)
1598 term
->wrapnext
= FALSE
;
1599 term
->seen_disp_event
= TRUE
;
1601 case 'E': /* exactly equivalent to CR-LF */
1602 compatibility(VT100
);
1604 if (term
->curs
.y
== term
->marg_b
)
1605 scroll(term
, term
->marg_t
, term
->marg_b
, 1, TRUE
);
1606 else if (term
->curs
.y
< term
->rows
- 1)
1609 term
->wrapnext
= FALSE
;
1610 term
->seen_disp_event
= TRUE
;
1612 case 'M': /* reverse index - backwards LF */
1613 compatibility(VT100
);
1614 if (term
->curs
.y
== term
->marg_t
)
1615 scroll(term
, term
->marg_t
, term
->marg_b
, -1, TRUE
);
1616 else if (term
->curs
.y
> 0)
1619 term
->wrapnext
= FALSE
;
1620 term
->seen_disp_event
= TRUE
;
1622 case 'Z': /* terminal type query */
1623 compatibility(VT100
);
1625 ldisc_send(term
->ldisc
, term
->id_string
,
1626 strlen(term
->id_string
), 0);
1628 case 'c': /* restore power-on settings */
1629 compatibility(VT100
);
1631 if (term
->ldisc
) /* cause ldisc to notice changes */
1632 ldisc_send(term
->ldisc
, NULL
, 0, 0);
1633 if (term
->reset_132
) {
1634 if (!cfg
.no_remote_resize
)
1635 request_resize(80, term
->rows
);
1636 term
->reset_132
= 0;
1640 term
->seen_disp_event
= TRUE
;
1642 case 'H': /* set a tab */
1643 compatibility(VT100
);
1644 term
->tabs
[term
->curs
.x
] = TRUE
;
1647 case ANSI('8', '#'): /* ESC # 8 fills screen with Es :-) */
1648 compatibility(VT100
);
1650 unsigned long *ldata
;
1654 for (i
= 0; i
< term
->rows
; i
++) {
1656 for (j
= 0; j
< term
->cols
; j
++)
1657 ldata
[j
] = ATTR_DEFAULT
| 'E';
1658 ldata
[term
->cols
] = 0;
1661 term
->seen_disp_event
= TRUE
;
1662 scrtop
.x
= scrtop
.y
= 0;
1664 scrbot
.y
= term
->rows
;
1665 check_selection(term
, scrtop
, scrbot
);
1669 case ANSI('3', '#'):
1670 case ANSI('4', '#'):
1671 case ANSI('5', '#'):
1672 case ANSI('6', '#'):
1673 compatibility(VT100
);
1675 unsigned long nlattr
;
1676 unsigned long *ldata
;
1677 switch (ANSI(c
, term
->esc_query
)) {
1678 case ANSI('3', '#'):
1681 case ANSI('4', '#'):
1684 case ANSI('5', '#'):
1685 nlattr
= LATTR_NORM
;
1687 default: /* spiritually case ANSI('6', '#'): */
1688 nlattr
= LATTR_WIDE
;
1691 ldata
= lineptr(term
->curs
.y
);
1692 ldata
[term
->cols
] &= ~LATTR_MODE
;
1693 ldata
[term
->cols
] |= nlattr
;
1697 case ANSI('A', '('):
1698 compatibility(VT100
);
1699 if (!cfg
.no_remote_charset
)
1700 term
->cset_attr
[0] = ATTR_GBCHR
;
1702 case ANSI('B', '('):
1703 compatibility(VT100
);
1704 if (!cfg
.no_remote_charset
)
1705 term
->cset_attr
[0] = ATTR_ASCII
;
1707 case ANSI('0', '('):
1708 compatibility(VT100
);
1709 if (!cfg
.no_remote_charset
)
1710 term
->cset_attr
[0] = ATTR_LINEDRW
;
1712 case ANSI('U', '('):
1713 compatibility(OTHER
);
1714 if (!cfg
.no_remote_charset
)
1715 term
->cset_attr
[0] = ATTR_SCOACS
;
1718 case ANSI('A', ')'):
1719 compatibility(VT100
);
1720 if (!cfg
.no_remote_charset
)
1721 term
->cset_attr
[1] = ATTR_GBCHR
;
1723 case ANSI('B', ')'):
1724 compatibility(VT100
);
1725 if (!cfg
.no_remote_charset
)
1726 term
->cset_attr
[1] = ATTR_ASCII
;
1728 case ANSI('0', ')'):
1729 compatibility(VT100
);
1730 if (!cfg
.no_remote_charset
)
1731 term
->cset_attr
[1] = ATTR_LINEDRW
;
1733 case ANSI('U', ')'):
1734 compatibility(OTHER
);
1735 if (!cfg
.no_remote_charset
)
1736 term
->cset_attr
[1] = ATTR_SCOACS
;
1739 case ANSI('8', '%'): /* Old Linux code */
1740 case ANSI('G', '%'):
1741 compatibility(OTHER
);
1742 if (!cfg
.no_remote_charset
)
1745 case ANSI('@', '%'):
1746 compatibility(OTHER
);
1747 if (!cfg
.no_remote_charset
)
1753 term
->termstate
= TOPLEVEL
; /* default */
1755 if (term
->esc_nargs
<= ARGS_MAX
) {
1756 if (term
->esc_args
[term
->esc_nargs
- 1] == ARG_DEFAULT
)
1757 term
->esc_args
[term
->esc_nargs
- 1] = 0;
1758 term
->esc_args
[term
->esc_nargs
- 1] =
1759 10 * term
->esc_args
[term
->esc_nargs
- 1] + c
- '0';
1761 term
->termstate
= SEEN_CSI
;
1762 } else if (c
== ';') {
1763 if (++term
->esc_nargs
<= ARGS_MAX
)
1764 term
->esc_args
[term
->esc_nargs
- 1] = ARG_DEFAULT
;
1765 term
->termstate
= SEEN_CSI
;
1766 } else if (c
< '@') {
1767 if (term
->esc_query
)
1768 term
->esc_query
= -1;
1770 term
->esc_query
= TRUE
;
1772 term
->esc_query
= c
;
1773 term
->termstate
= SEEN_CSI
;
1775 switch (ANSI(c
, term
->esc_query
)) {
1776 case 'A': /* move up N lines */
1777 move(term
, term
->curs
.x
,
1778 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1779 term
->seen_disp_event
= TRUE
;
1781 case 'e': /* move down N lines */
1782 compatibility(ANSI
);
1785 move(term
, term
->curs
.x
,
1786 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1787 term
->seen_disp_event
= TRUE
;
1789 case ANSI('c', '>'): /* report xterm version */
1790 compatibility(OTHER
);
1791 /* this reports xterm version 136 so that VIM can
1792 use the drag messages from the mouse reporting */
1794 ldisc_send(term
->ldisc
, "\033[>0;136;0c", 11, 0);
1796 case 'a': /* move right N cols */
1797 compatibility(ANSI
);
1800 move(term
, term
->curs
.x
+ def(term
->esc_args
[0], 1),
1802 term
->seen_disp_event
= TRUE
;
1804 case 'D': /* move left N cols */
1805 move(term
, term
->curs
.x
- def(term
->esc_args
[0], 1),
1807 term
->seen_disp_event
= TRUE
;
1809 case 'E': /* move down N lines and CR */
1810 compatibility(ANSI
);
1812 term
->curs
.y
+ def(term
->esc_args
[0], 1), 1);
1813 term
->seen_disp_event
= TRUE
;
1815 case 'F': /* move up N lines and CR */
1816 compatibility(ANSI
);
1818 term
->curs
.y
- def(term
->esc_args
[0], 1), 1);
1819 term
->seen_disp_event
= TRUE
;
1822 case '`': /* set horizontal posn */
1823 compatibility(ANSI
);
1824 move(term
, def(term
->esc_args
[0], 1) - 1,
1826 term
->seen_disp_event
= TRUE
;
1828 case 'd': /* set vertical posn */
1829 compatibility(ANSI
);
1830 move(term
, term
->curs
.x
,
1831 ((term
->dec_om ? term
->marg_t
: 0) +
1832 def(term
->esc_args
[0], 1) - 1),
1833 (term
->dec_om ?
2 : 0));
1834 term
->seen_disp_event
= TRUE
;
1837 case 'f': /* set horz and vert posns at once */
1838 if (term
->esc_nargs
< 2)
1839 term
->esc_args
[1] = ARG_DEFAULT
;
1840 move(term
, def(term
->esc_args
[1], 1) - 1,
1841 ((term
->dec_om ? term
->marg_t
: 0) +
1842 def(term
->esc_args
[0], 1) - 1),
1843 (term
->dec_om ?
2 : 0));
1844 term
->seen_disp_event
= TRUE
;
1846 case 'J': /* erase screen or parts of it */
1848 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1851 erase_lots(term
, FALSE
, !!(i
& 2), !!(i
& 1));
1854 term
->seen_disp_event
= TRUE
;
1856 case 'K': /* erase line or parts of it */
1858 unsigned int i
= def(term
->esc_args
[0], 0) + 1;
1861 erase_lots(term
, TRUE
, !!(i
& 2), !!(i
& 1));
1863 term
->seen_disp_event
= TRUE
;
1865 case 'L': /* insert lines */
1866 compatibility(VT102
);
1867 if (term
->curs
.y
<= term
->marg_b
)
1868 scroll(term
, term
->curs
.y
, term
->marg_b
,
1869 -def(term
->esc_args
[0], 1), FALSE
);
1871 term
->seen_disp_event
= TRUE
;
1873 case 'M': /* delete lines */
1874 compatibility(VT102
);
1875 if (term
->curs
.y
<= term
->marg_b
)
1876 scroll(term
, term
->curs
.y
, term
->marg_b
,
1877 def(term
->esc_args
[0], 1),
1880 term
->seen_disp_event
= TRUE
;
1882 case '@': /* insert chars */
1883 /* XXX VTTEST says this is vt220, vt510 manual says vt102 */
1884 compatibility(VT102
);
1885 insch(term
, def(term
->esc_args
[0], 1));
1886 term
->seen_disp_event
= TRUE
;
1888 case 'P': /* delete chars */
1889 compatibility(VT102
);
1890 insch(term
, -def(term
->esc_args
[0], 1));
1891 term
->seen_disp_event
= TRUE
;
1893 case 'c': /* terminal type query */
1894 compatibility(VT100
);
1895 /* This is the response for a VT102 */
1897 ldisc_send(term
->ldisc
, term
->id_string
,
1898 strlen(term
->id_string
), 0);
1900 case 'n': /* cursor position query */
1902 if (term
->esc_args
[0] == 6) {
1904 sprintf(buf
, "\033[%d;%dR", term
->curs
.y
+ 1,
1906 ldisc_send(term
->ldisc
, buf
, strlen(buf
), 0);
1907 } else if (term
->esc_args
[0] == 5) {
1908 ldisc_send(term
->ldisc
, "\033[0n", 4, 0);
1912 case 'h': /* toggle modes to high */
1914 compatibility(VT100
);
1917 for (i
= 0; i
< term
->esc_nargs
; i
++)
1918 toggle_mode(term
, term
->esc_args
[i
],
1919 term
->esc_query
, TRUE
);
1924 compatibility(VT100
);
1926 if (term
->esc_nargs
!= 1) break;
1927 if (term
->esc_args
[0] == 5 && *cfg
.printer
) {
1928 term
->printing
= TRUE
;
1929 term
->only_printing
= !term
->esc_query
;
1930 term
->print_state
= 0;
1931 term_print_setup(term
);
1932 } else if (term
->esc_args
[0] == 4 &&
1934 term_print_finish(term
);
1938 case 'l': /* toggle modes to low */
1940 compatibility(VT100
);
1943 for (i
= 0; i
< term
->esc_nargs
; i
++)
1944 toggle_mode(term
, term
->esc_args
[i
],
1945 term
->esc_query
, FALSE
);
1948 case 'g': /* clear tabs */
1949 compatibility(VT100
);
1950 if (term
->esc_nargs
== 1) {
1951 if (term
->esc_args
[0] == 0) {
1952 term
->tabs
[term
->curs
.x
] = FALSE
;
1953 } else if (term
->esc_args
[0] == 3) {
1955 for (i
= 0; i
< term
->cols
; i
++)
1956 term
->tabs
[i
] = FALSE
;
1960 case 'r': /* set scroll margins */
1961 compatibility(VT100
);
1962 if (term
->esc_nargs
<= 2) {
1964 top
= def(term
->esc_args
[0], 1) - 1;
1965 bot
= (term
->esc_nargs
<= 1
1966 || term
->esc_args
[1] == 0 ?
1968 def(term
->esc_args
[1], term
->rows
)) - 1;
1969 if (bot
>= term
->rows
)
1970 bot
= term
->rows
- 1;
1971 /* VTTEST Bug 9 - if region is less than 2 lines
1972 * don't change region.
1974 if (bot
- top
> 0) {
1979 * I used to think the cursor should be
1980 * placed at the top of the newly marginned
1981 * area. Apparently not: VMS TPU falls over
1984 * Well actually it should for
1987 term
->curs
.y
= (term
->dec_om ?
1990 term
->seen_disp_event
= TRUE
;
1994 case 'm': /* set graphics rendition */
1997 * A VT100 without the AVO only had one
1998 * attribute, either underline or
1999 * reverse video depending on the
2000 * cursor type, this was selected by
2004 * This is sometimes DIM, eg on the
2007 * This is sometimes INVIS various ANSI.
2009 * This like 22 disables BOLD, DIM and INVIS
2011 * The ANSI colours appear on any
2012 * terminal that has colour (obviously)
2013 * but the interaction between sgr0 and
2014 * the colours varies but is usually
2015 * related to the background colour
2016 * erase item. The interaction between
2017 * colour attributes and the mono ones
2018 * is also very implementation
2021 * The 39 and 49 attributes are likely
2022 * to be unimplemented.
2025 for (i
= 0; i
< term
->esc_nargs
; i
++) {
2026 switch (def(term
->esc_args
[i
], 0)) {
2027 case 0: /* restore defaults */
2028 term
->curr_attr
= ATTR_DEFAULT
;
2030 case 1: /* enable bold */
2031 compatibility(VT100AVO
);
2032 term
->curr_attr
|= ATTR_BOLD
;
2034 case 21: /* (enable double underline) */
2035 compatibility(OTHER
);
2036 case 4: /* enable underline */
2037 compatibility(VT100AVO
);
2038 term
->curr_attr
|= ATTR_UNDER
;
2040 case 5: /* enable blink */
2041 compatibility(VT100AVO
);
2042 term
->curr_attr
|= ATTR_BLINK
;
2044 case 7: /* enable reverse video */
2045 term
->curr_attr
|= ATTR_REVERSE
;
2047 case 10: /* SCO acs off */
2048 compatibility(SCOANSI
);
2049 if (cfg
.no_remote_charset
) break;
2050 term
->sco_acs
= 0; break;
2051 case 11: /* SCO acs on */
2052 compatibility(SCOANSI
);
2053 if (cfg
.no_remote_charset
) break;
2054 term
->sco_acs
= 1; break;
2055 case 12: /* SCO acs on flipped */
2056 compatibility(SCOANSI
);
2057 if (cfg
.no_remote_charset
) break;
2058 term
->sco_acs
= 2; break;
2059 case 22: /* disable bold */
2060 compatibility2(OTHER
, VT220
);
2061 term
->curr_attr
&= ~ATTR_BOLD
;
2063 case 24: /* disable underline */
2064 compatibility2(OTHER
, VT220
);
2065 term
->curr_attr
&= ~ATTR_UNDER
;
2067 case 25: /* disable blink */
2068 compatibility2(OTHER
, VT220
);
2069 term
->curr_attr
&= ~ATTR_BLINK
;
2071 case 27: /* disable reverse video */
2072 compatibility2(OTHER
, VT220
);
2073 term
->curr_attr
&= ~ATTR_REVERSE
;
2084 term
->curr_attr
&= ~ATTR_FGMASK
;
2086 (term
->esc_args
[i
] - 30)<<ATTR_FGSHIFT
;
2088 case 39: /* default-foreground */
2089 term
->curr_attr
&= ~ATTR_FGMASK
;
2090 term
->curr_attr
|= ATTR_DEFFG
;
2101 term
->curr_attr
&= ~ATTR_BGMASK
;
2103 (term
->esc_args
[i
] - 40)<<ATTR_BGSHIFT
;
2105 case 49: /* default-background */
2106 term
->curr_attr
&= ~ATTR_BGMASK
;
2107 term
->curr_attr
|= ATTR_DEFBG
;
2112 term
->erase_char
= (' ' | ATTR_ASCII
|
2118 case 's': /* save cursor */
2119 save_cursor(term
, TRUE
);
2121 case 'u': /* restore cursor */
2122 save_cursor(term
, FALSE
);
2123 term
->seen_disp_event
= TRUE
;
2125 case 't': /* set page size - ie window height */
2127 * VT340/VT420 sequence DECSLPP, DEC only allows values
2128 * 24/25/36/48/72/144 other emulators (eg dtterm) use
2129 * illegal values (eg first arg 1..9) for window changing
2132 if (term
->esc_nargs
<= 1
2133 && (term
->esc_args
[0] < 1 ||
2134 term
->esc_args
[0] >= 24)) {
2135 compatibility(VT340TEXT
);
2136 if (!cfg
.no_remote_resize
)
2137 request_resize(term
->cols
,
2138 def(term
->esc_args
[0], 24));
2140 } else if (term
->esc_nargs
>= 1 &&
2141 term
->esc_args
[0] >= 1 &&
2142 term
->esc_args
[0] < 24) {
2143 compatibility(OTHER
);
2145 switch (term
->esc_args
[0]) {
2155 if (term
->esc_nargs
>= 3) {
2156 if (!cfg
.no_remote_resize
)
2157 move_window(def(term
->esc_args
[1], 0),
2158 def(term
->esc_args
[2], 0));
2162 /* We should resize the window to a given
2163 * size in pixels here, but currently our
2164 * resizing code isn't healthy enough to
2168 set_zorder(TRUE
); /* move to top */
2171 set_zorder(FALSE
); /* move to bottom */
2177 if (term
->esc_nargs
>= 3) {
2178 if (!cfg
.no_remote_resize
)
2179 request_resize(def(term
->esc_args
[2], cfg
.width
),
2180 def(term
->esc_args
[1], cfg
.height
));
2184 if (term
->esc_nargs
>= 2)
2185 set_zoomed(term
->esc_args
[1] ?
2190 ldisc_send(term
->ldisc
,
2191 is_iconic() ?
"\033[1t" :
2196 get_window_pos(&x
, &y
);
2197 len
= sprintf(buf
, "\033[3;%d;%dt", x
, y
);
2198 ldisc_send(term
->ldisc
, buf
, len
, 0);
2203 get_window_pixels(&x
, &y
);
2204 len
= sprintf(buf
, "\033[4;%d;%dt", x
, y
);
2205 ldisc_send(term
->ldisc
, buf
, len
, 0);
2210 len
= sprintf(buf
, "\033[8;%d;%dt",
2211 term
->rows
, term
->cols
);
2212 ldisc_send(term
->ldisc
, buf
, len
, 0);
2217 * Hmmm. Strictly speaking we
2218 * should return `the size of the
2219 * screen in characters', but
2220 * that's not easy: (a) window
2221 * furniture being what it is it's
2222 * hard to compute, and (b) in
2223 * resize-font mode maximising the
2224 * window wouldn't change the
2225 * number of characters. *shrug*. I
2226 * think we'll ignore it for the
2227 * moment and see if anyone
2228 * complains, and then ask them
2229 * what they would like it to do.
2234 p
= get_window_title(TRUE
);
2236 ldisc_send(term
->ldisc
, "\033]L", 3, 0);
2237 ldisc_send(term
->ldisc
, p
, len
, 0);
2238 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2243 p
= get_window_title(FALSE
);
2245 ldisc_send(term
->ldisc
, "\033]l", 3, 0);
2246 ldisc_send(term
->ldisc
, p
, len
, 0);
2247 ldisc_send(term
->ldisc
, "\033\\", 2, 0);
2254 compatibility(SCOANSI
);
2255 scroll(term
, term
->marg_t
, term
->marg_b
,
2256 def(term
->esc_args
[0], 1), TRUE
);
2258 term
->wrapnext
= FALSE
;
2259 term
->seen_disp_event
= TRUE
;
2262 compatibility(SCOANSI
);
2263 scroll(term
, term
->marg_t
, term
->marg_b
,
2264 -def(term
->esc_args
[0], 1), TRUE
);
2266 term
->wrapnext
= FALSE
;
2267 term
->seen_disp_event
= TRUE
;
2269 case ANSI('|', '*'):
2270 /* VT420 sequence DECSNLS
2271 * Set number of lines on screen
2272 * VT420 uses VGA like hardware and can support any size in
2273 * reasonable range (24..49 AIUI) with no default specified.
2275 compatibility(VT420
);
2276 if (term
->esc_nargs
== 1 && term
->esc_args
[0] > 0) {
2277 if (!cfg
.no_remote_resize
)
2278 request_resize(term
->cols
,
2279 def(term
->esc_args
[0],
2284 case ANSI('|', '$'):
2285 /* VT340/VT420 sequence DECSCPP
2286 * Set number of columns per page
2287 * Docs imply range is only 80 or 132, but I'll allow any.
2289 compatibility(VT340TEXT
);
2290 if (term
->esc_nargs
<= 1) {
2291 if (!cfg
.no_remote_resize
)
2292 request_resize(def(term
->esc_args
[0],
2293 cfg
.width
), term
->rows
);
2297 case 'X': /* write N spaces w/o moving cursor */
2298 /* XXX VTTEST says this is vt220, vt510 manual says vt100 */
2299 compatibility(ANSIMIN
);
2301 int n
= def(term
->esc_args
[0], 1);
2303 unsigned long *p
= term
->cpos
;
2304 if (n
> term
->cols
- term
->curs
.x
)
2305 n
= term
->cols
- term
->curs
.x
;
2306 cursplus
= term
->curs
;
2308 check_selection(term
, term
->curs
, cursplus
);
2310 *p
++ = term
->erase_char
;
2311 term
->seen_disp_event
= TRUE
;
2314 case 'x': /* report terminal characteristics */
2315 compatibility(VT100
);
2318 int i
= def(term
->esc_args
[0], 0);
2319 if (i
== 0 || i
== 1) {
2320 strcpy(buf
, "\033[2;1;1;112;112;1;0x");
2322 ldisc_send(term
->ldisc
, buf
, 20, 0);
2326 case 'Z': /* BackTab for xterm */
2327 compatibility(OTHER
);
2329 int i
= def(term
->esc_args
[0], 1);
2330 pos old_curs
= term
->curs
;
2332 for(;i
>0 && term
->curs
.x
>0; i
--) {
2335 } while (term
->curs
.x
>0 &&
2336 !term
->tabs
[term
->curs
.x
]);
2339 check_selection(term
, old_curs
, term
->curs
);
2342 case ANSI('L', '='):
2343 compatibility(OTHER
);
2344 term
->use_bce
= (term
->esc_args
[0] <= 0);
2345 term
->erase_char
= ERASE_CHAR
;
2347 term
->erase_char
= (' ' | ATTR_ASCII
|
2349 (ATTR_FGMASK
| ATTR_BGMASK
)));
2351 case ANSI('E', '='):
2352 compatibility(OTHER
);
2353 term
->blink_is_real
= (term
->esc_args
[0] >= 1);
2355 case ANSI('p', '"'):
2357 * Allow the host to make this emulator a
2358 * 'perfect' VT102. This first appeared in
2359 * the VT220, but we do need to get back to
2360 * PuTTY mode so I won't check it.
2362 * The arg in 40..42,50 are a PuTTY extension.
2363 * The 2nd arg, 8bit vs 7bit is not checked.
2365 * Setting VT102 mode should also change
2366 * the Fkeys to generate PF* codes as a
2367 * real VT102 has no Fkeys. The VT220 does
2368 * this, F11..F13 become ESC,BS,LF other
2369 * Fkeys send nothing.
2371 * Note ESC c will NOT change this!
2374 switch (term
->esc_args
[0]) {
2376 term
->compatibility_level
&= ~TM_VTXXX
;
2377 term
->compatibility_level
|= TM_VT102
;
2380 term
->compatibility_level
&= ~TM_VTXXX
;
2381 term
->compatibility_level
|= TM_VT220
;
2385 if (term
->esc_args
[0] > 60 &&
2386 term
->esc_args
[0] < 70)
2387 term
->compatibility_level
|= TM_VTXXX
;
2391 term
->compatibility_level
&= TM_VTXXX
;
2394 term
->compatibility_level
= TM_PUTTY
;
2397 term
->compatibility_level
= TM_SCOANSI
;
2401 term
->compatibility_level
= TM_PUTTY
;
2407 /* Change the response to CSI c */
2408 if (term
->esc_args
[0] == 50) {
2411 strcpy(term
->id_string
, "\033[?");
2412 for (i
= 1; i
< term
->esc_nargs
; i
++) {
2414 strcat(term
->id_string
, ";");
2415 sprintf(lbuf
, "%d", term
->esc_args
[i
]);
2416 strcat(term
->id_string
, lbuf
);
2418 strcat(term
->id_string
, "c");
2421 /* Is this a good idea ?
2422 * Well we should do a soft reset at this point ...
2424 if (!has_compat(VT420
) && has_compat(VT100
)) {
2425 if (!cfg
.no_remote_resize
) {
2426 if (term
->reset_132
)
2427 request_resize(132, 24);
2429 request_resize(80, 24);
2437 term
->osc_w
= FALSE
;
2439 case 'P': /* Linux palette sequence */
2440 term
->termstate
= SEEN_OSC_P
;
2441 term
->osc_strlen
= 0;
2443 case 'R': /* Linux palette reset */
2445 term_invalidate(term
);
2446 term
->termstate
= TOPLEVEL
;
2448 case 'W': /* word-set */
2449 term
->termstate
= SEEN_OSC_W
;
2462 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2466 * Grotty hack to support xterm and DECterm title
2467 * sequences concurrently.
2469 if (term
->esc_args
[0] == 2) {
2470 term
->esc_args
[0] = 1;
2473 /* else fall through */
2475 term
->termstate
= OSC_STRING
;
2476 term
->osc_strlen
= 0;
2481 * This OSC stuff is EVIL. It takes just one character to get into
2482 * sysline mode and it's not initially obvious how to get out.
2483 * So I've added CR and LF as string aborts.
2484 * This shouldn't effect compatibility as I believe embedded
2485 * control characters are supposed to be interpreted (maybe?)
2486 * and they don't display anything useful anyway.
2490 if (c
== '\n' || c
== '\r') {
2491 term
->termstate
= TOPLEVEL
;
2492 } else if (c
== 0234 || c
== '\007') {
2494 * These characters terminate the string; ST and BEL
2495 * terminate the sequence and trigger instant
2496 * processing of it, whereas ESC goes back to SEEN_ESC
2497 * mode unless it is followed by \, in which case it is
2498 * synonymous with ST in the first place.
2501 term
->termstate
= TOPLEVEL
;
2502 } else if (c
== '\033')
2503 term
->termstate
= OSC_MAYBE_ST
;
2504 else if (term
->osc_strlen
< OSC_STR_MAX
)
2505 term
->osc_string
[term
->osc_strlen
++] = c
;
2509 int max
= (term
->osc_strlen
== 0 ?
21 : 16);
2511 if (c
>= '0' && c
<= '9')
2513 else if (c
>= 'A' && c
<= 'A' + max
- 10)
2515 else if (c
>= 'a' && c
<= 'a' + max
- 10)
2518 term
->termstate
= TOPLEVEL
;
2521 term
->osc_string
[term
->osc_strlen
++] = val
;
2522 if (term
->osc_strlen
>= 7) {
2523 palette_set(term
->osc_string
[0],
2524 term
->osc_string
[1] * 16 + term
->osc_string
[2],
2525 term
->osc_string
[3] * 16 + term
->osc_string
[4],
2526 term
->osc_string
[5] * 16 + term
->osc_string
[6]);
2527 term_invalidate(term
);
2528 term
->termstate
= TOPLEVEL
;
2544 term
->esc_args
[0] = 10 * term
->esc_args
[0] + c
- '0';
2547 term
->termstate
= OSC_STRING
;
2548 term
->osc_strlen
= 0;
2552 term
->termstate
= TOPLEVEL
;
2553 term
->seen_disp_event
= TRUE
;
2556 move(term
, term
->curs
.x
, term
->curs
.y
- 1, 1);
2559 move(term
, term
->curs
.x
, term
->curs
.y
+ 1, 1);
2562 move(term
, term
->curs
.x
+ 1, term
->curs
.y
, 1);
2565 move(term
, term
->curs
.x
- 1, term
->curs
.y
, 1);
2568 * From the VT100 Manual
2569 * NOTE: The special graphics characters in the VT100
2570 * are different from those in the VT52
2572 * From VT102 manual:
2573 * 137 _ Blank - Same
2574 * 140 ` Reserved - Humm.
2575 * 141 a Solid rectangle - Similar
2576 * 142 b 1/ - Top half of fraction for the
2577 * 143 c 3/ - subscript numbers below.
2580 * 146 f Degrees - Same
2581 * 147 g Plus or minus - Same
2583 * 151 i Ellipsis (dots)
2586 * 154 l Bar at scan 0
2587 * 155 m Bar at scan 1
2588 * 156 n Bar at scan 2
2589 * 157 o Bar at scan 3 - Similar
2590 * 160 p Bar at scan 4 - Similar
2591 * 161 q Bar at scan 5 - Similar
2592 * 162 r Bar at scan 6 - Same
2593 * 163 s Bar at scan 7 - Similar
2608 term
->cset_attr
[term
->cset
= 0] = ATTR_LINEDRW
;
2611 term
->cset_attr
[term
->cset
= 0] = ATTR_ASCII
;
2614 move(term
, 0, 0, 0);
2617 if (term
->curs
.y
== 0)
2618 scroll(term
, 0, term
->rows
- 1, -1, TRUE
);
2619 else if (term
->curs
.y
> 0)
2622 term
->wrapnext
= FALSE
;
2625 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2629 erase_lots(term
, TRUE
, FALSE
, TRUE
);
2633 /* XXX Print cursor line */
2636 /* XXX Start controller mode */
2639 /* XXX Stop controller mode */
2643 term
->termstate
= VT52_Y1
;
2647 ldisc_send(term
->ldisc
, "\033/Z", 3, 0);
2650 term
->app_keypad_keys
= TRUE
;
2653 term
->app_keypad_keys
= FALSE
;
2656 /* XXX This should switch to VT100 mode not current or default
2657 * VT mode. But this will only have effect in a VT220+
2660 term
->vt52_mode
= FALSE
;
2661 term
->blink_is_real
= cfg
.blinktext
;
2665 /* XXX Enter auto print mode */
2668 /* XXX Exit auto print mode */
2671 /* XXX Print screen */
2677 /* compatibility(ATARI) */
2678 move(term
, 0, 0, 0);
2679 erase_lots(term
, FALSE
, FALSE
, TRUE
);
2683 /* compatibility(ATARI) */
2684 if (term
->curs
.y
<= term
->marg_b
)
2685 scroll(term
, term
->curs
.y
, term
->marg_b
, -1, FALSE
);
2688 /* compatibility(ATARI) */
2689 if (term
->curs
.y
<= term
->marg_b
)
2690 scroll(term
, term
->curs
.y
, term
->marg_b
, 1, TRUE
);
2693 /* compatibility(ATARI) */
2694 term
->termstate
= VT52_FG
;
2697 /* compatibility(ATARI) */
2698 term
->termstate
= VT52_BG
;
2701 /* compatibility(ATARI) */
2702 erase_lots(term
, FALSE
, TRUE
, FALSE
);
2706 /* compatibility(ATARI) */
2707 term
->cursor_on
= TRUE
;
2710 /* compatibility(ATARI) */
2711 term
->cursor_on
= FALSE
;
2713 /* case 'j': Save cursor position - broken on ST */
2714 /* case 'k': Restore cursor position */
2716 /* compatibility(ATARI) */
2717 erase_lots(term
, TRUE
, TRUE
, TRUE
);
2719 term
->wrapnext
= FALSE
;
2723 /* compatibility(ATARI) */
2724 erase_lots(term
, TRUE
, TRUE
, FALSE
);
2727 /* compatibility(ATARI) */
2728 term
->curr_attr
|= ATTR_REVERSE
;
2731 /* compatibility(ATARI) */
2732 term
->curr_attr
&= ~ATTR_REVERSE
;
2734 case 'v': /* wrap Autowrap on - Wyse style */
2735 /* compatibility(ATARI) */
2738 case 'w': /* Autowrap off */
2739 /* compatibility(ATARI) */
2744 /* compatibility(OTHER) */
2745 term
->vt52_bold
= FALSE
;
2746 term
->curr_attr
= ATTR_DEFAULT
;
2748 term
->erase_char
= (' ' | ATTR_ASCII
|
2750 (ATTR_FGMASK
| ATTR_BGMASK
)));
2753 /* compatibility(VI50) */
2754 term
->curr_attr
|= ATTR_UNDER
;
2757 /* compatibility(VI50) */
2758 term
->curr_attr
&= ~ATTR_UNDER
;
2761 /* compatibility(VI50) */
2762 term
->vt52_bold
= TRUE
;
2763 term
->curr_attr
|= ATTR_BOLD
;
2766 /* compatibility(VI50) */
2767 term
->vt52_bold
= FALSE
;
2768 term
->curr_attr
&= ~ATTR_BOLD
;
2774 term
->termstate
= VT52_Y2
;
2775 move(term
, term
->curs
.x
, c
- ' ', 0);
2778 term
->termstate
= TOPLEVEL
;
2779 move(term
, c
- ' ', term
->curs
.y
, 0);
2784 term
->termstate
= TOPLEVEL
;
2785 term
->curr_attr
&= ~ATTR_FGMASK
;
2786 term
->curr_attr
&= ~ATTR_BOLD
;
2787 term
->curr_attr
|= (c
& 0x7) << ATTR_FGSHIFT
;
2788 if ((c
& 0x8) || term
->vt52_bold
)
2789 term
->curr_attr
|= ATTR_BOLD
;
2792 term
->erase_char
= (' ' | ATTR_ASCII
|
2794 (ATTR_FGMASK
| ATTR_BGMASK
)));
2797 term
->termstate
= TOPLEVEL
;
2798 term
->curr_attr
&= ~ATTR_BGMASK
;
2799 term
->curr_attr
&= ~ATTR_BLINK
;
2800 term
->curr_attr
|= (c
& 0x7) << ATTR_BGSHIFT
;
2802 /* Note: bold background */
2804 term
->curr_attr
|= ATTR_BLINK
;
2807 term
->erase_char
= (' ' | ATTR_ASCII
|
2809 (ATTR_FGMASK
| ATTR_BGMASK
)));
2812 default: break; /* placate gcc warning about enum use */
2814 if (term
->selstate
!= NO_SELECTION
) {
2815 pos cursplus
= term
->curs
;
2817 check_selection(term
, term
->curs
, cursplus
);
2821 term_print_flush(term
);
2826 * Compare two lines to determine whether they are sufficiently
2827 * alike to scroll-optimise one to the other. Return the degree of
2830 static int linecmp(Terminal
*term
, unsigned long *a
, unsigned long *b
)
2834 for (i
= n
= 0; i
< term
->cols
; i
++)
2835 n
+= (*a
++ == *b
++);
2841 * Given a context, update the window. Out of paranoia, we don't
2842 * allow WM_PAINT responses to do scrolling optimisations.
2844 static void do_paint(Terminal
*term
, Context ctx
, int may_optimise
)
2846 int i
, j
, our_curs_y
;
2847 unsigned long rv
, cursor
;
2850 long cursor_background
= ERASE_CHAR
;
2851 unsigned long ticks
;
2854 * Check the visual bell state.
2856 if (term
->in_vbell
) {
2857 ticks
= GETTICKCOUNT();
2858 if (ticks
- term
->vbell_startpoint
>= VBELL_TIMEOUT
)
2859 term
->in_vbell
= FALSE
;
2862 rv
= (!term
->rvideo
^ !term
->in_vbell ? ATTR_REVERSE
: 0);
2865 * screen array, disptop, scrtop,
2867 * cfg.blinkpc, blink_is_real, tblinker,
2868 * curs.y, curs.x, blinker, cfg.blink_cur, cursor_on, has_focus, wrapnext
2871 /* Has the cursor position or type changed ? */
2872 if (term
->cursor_on
) {
2873 if (term
->has_focus
) {
2874 if (term
->blinker
|| !cfg
.blink_cur
)
2875 cursor
= TATTR_ACTCURS
;
2879 cursor
= TATTR_PASCURS
;
2881 cursor
|= TATTR_RIGHTCURS
;
2884 our_curs_y
= term
->curs
.y
- term
->disptop
;
2886 if (term
->dispcurs
&& (term
->curstype
!= cursor
||
2888 term
->disptext
+ our_curs_y
* (term
->cols
+ 1) +
2890 if (term
->dispcurs
> term
->disptext
&&
2891 (*term
->dispcurs
& (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2892 term
->dispcurs
[-1] |= ATTR_INVALID
;
2893 if ( (term
->dispcurs
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2894 term
->dispcurs
[1] |= ATTR_INVALID
;
2895 *term
->dispcurs
|= ATTR_INVALID
;
2898 term
->dispcurs
= NULL
;
2900 /* The normal screen data */
2901 for (i
= 0; i
< term
->rows
; i
++) {
2902 unsigned long *ldata
;
2904 int idx
, dirty_line
, dirty_run
, selected
;
2905 unsigned long attr
= 0;
2906 int updated_line
= 0;
2909 int last_run_dirty
= 0;
2911 scrpos
.y
= i
+ term
->disptop
;
2912 ldata
= lineptr(scrpos
.y
);
2913 lattr
= (ldata
[term
->cols
] & LATTR_MODE
);
2915 idx
= i
* (term
->cols
+ 1);
2916 dirty_run
= dirty_line
= (ldata
[term
->cols
] !=
2917 term
->disptext
[idx
+ term
->cols
]);
2918 term
->disptext
[idx
+ term
->cols
] = ldata
[term
->cols
];
2920 for (j
= 0; j
< term
->cols
; j
++, idx
++) {
2921 unsigned long tattr
, tchar
;
2922 unsigned long *d
= ldata
+ j
;
2926 tchar
= (*d
& (CHAR_MASK
| CSET_MASK
));
2927 tattr
= (*d
& (ATTR_MASK
^ CSET_MASK
));
2928 switch (tchar
& CSET_MASK
) {
2930 tchar
= unitab_line
[tchar
& 0xFF];
2933 tchar
= unitab_xterm
[tchar
& 0xFF];
2936 tchar
= unitab_scoacs
[tchar
&0xFF];
2939 tattr
|= (tchar
& CSET_MASK
);
2941 if ((d
[1] & (CHAR_MASK
| CSET_MASK
)) == UCSWIDE
)
2944 /* Video reversing things */
2945 if (term
->selstate
== DRAGGING
|| term
->selstate
== SELECTED
) {
2946 if (term
->seltype
== LEXICOGRAPHIC
)
2947 selected
= (posle(term
->selstart
, scrpos
) &&
2948 poslt(scrpos
, term
->selend
));
2950 selected
= (posPle(term
->selstart
, scrpos
) &&
2951 posPlt(scrpos
, term
->selend
));
2955 ^ (selected ? ATTR_REVERSE
: 0));
2957 /* 'Real' blinking ? */
2958 if (term
->blink_is_real
&& (tattr
& ATTR_BLINK
)) {
2959 if (term
->has_focus
&& term
->tblinker
) {
2961 tattr
&= ~CSET_MASK
;
2964 tattr
&= ~ATTR_BLINK
;
2968 * Check the font we'll _probably_ be using to see if
2969 * the character is wide when we don't want it to be.
2971 if ((tchar
| tattr
) != (term
->disptext
[idx
]& ~ATTR_NARROW
)) {
2972 if ((tattr
& ATTR_WIDE
) == 0 &&
2973 CharWidth(ctx
, (tchar
| tattr
) & 0xFFFF) == 2)
2974 tattr
|= ATTR_NARROW
;
2975 } else if (term
->disptext
[idx
]&ATTR_NARROW
)
2976 tattr
|= ATTR_NARROW
;
2978 /* Cursor here ? Save the 'background' */
2979 if (i
== our_curs_y
&& j
== term
->curs
.x
) {
2980 cursor_background
= tattr
| tchar
;
2981 term
->dispcurs
= term
->disptext
+ idx
;
2984 if ((term
->disptext
[idx
] ^ tattr
) & ATTR_WIDE
)
2987 break_run
= (tattr
!= attr
|| j
- start
>= sizeof(ch
));
2989 /* Special hack for VT100 Linedraw glyphs */
2990 if ((attr
& CSET_MASK
) == 0x2300 && tchar
>= 0xBA
2991 && tchar
<= 0xBD) break_run
= TRUE
;
2993 if (!dbcs_screenfont
&& !dirty_line
) {
2994 if ((tchar
| tattr
) == term
->disptext
[idx
])
2996 else if (!dirty_run
&& ccount
== 1)
3001 if ((dirty_run
|| last_run_dirty
) && ccount
> 0) {
3002 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3008 if (dbcs_screenfont
)
3009 last_run_dirty
= dirty_run
;
3010 dirty_run
= dirty_line
;
3013 if ((tchar
| tattr
) != term
->disptext
[idx
])
3015 ch
[ccount
++] = (char) tchar
;
3016 term
->disptext
[idx
] = tchar
| tattr
;
3018 /* If it's a wide char step along to the next one. */
3019 if (tattr
& ATTR_WIDE
) {
3020 if (++j
< term
->cols
) {
3023 /* Cursor is here ? Ouch! */
3024 if (i
== our_curs_y
&& j
== term
->curs
.x
) {
3025 cursor_background
= *d
;
3026 term
->dispcurs
= term
->disptext
+ idx
;
3028 if (term
->disptext
[idx
] != *d
)
3030 term
->disptext
[idx
] = *d
;
3034 if (dirty_run
&& ccount
> 0) {
3035 do_text(ctx
, start
, i
, ch
, ccount
, attr
, lattr
);
3039 /* Cursor on this line ? (and changed) */
3040 if (i
== our_curs_y
&& (term
->curstype
!= cursor
|| updated_line
)) {
3041 ch
[0] = (char) (cursor_background
& CHAR_MASK
);
3042 attr
= (cursor_background
& ATTR_MASK
) | cursor
;
3043 do_cursor(ctx
, term
->curs
.x
, i
, ch
, 1, attr
, lattr
);
3044 term
->curstype
= cursor
;
3050 * Flick the switch that says if blinking things should be shown or hidden.
3053 void term_blink(Terminal
*term
, int flg
)
3055 long now
, blink_diff
;
3057 now
= GETTICKCOUNT();
3058 blink_diff
= now
- term
->last_tblink
;
3060 /* Make sure the text blinks no more than 2Hz; we'll use 0.45 s period. */
3061 if (blink_diff
< 0 || blink_diff
> (TICKSPERSEC
* 9 / 20)) {
3062 term
->last_tblink
= now
;
3063 term
->tblinker
= !term
->tblinker
;
3068 term
->last_blink
= now
;
3072 blink_diff
= now
- term
->last_blink
;
3074 /* Make sure the cursor blinks no faster than system blink rate */
3075 if (blink_diff
>= 0 && blink_diff
< (long) CURSORBLINK
)
3078 term
->last_blink
= now
;
3079 term
->blinker
= !term
->blinker
;
3083 * Invalidate the whole screen so it will be repainted in full.
3085 void term_invalidate(Terminal
*term
)
3089 for (i
= 0; i
< term
->rows
* (term
->cols
+ 1); i
++)
3090 term
->disptext
[i
] = ATTR_INVALID
;
3094 * Paint the window in response to a WM_PAINT message.
3096 void term_paint(Terminal
*term
, Context ctx
,
3097 int left
, int top
, int right
, int bottom
)
3100 if (left
< 0) left
= 0;
3101 if (top
< 0) top
= 0;
3102 if (right
>= term
->cols
) right
= term
->cols
-1;
3103 if (bottom
>= term
->rows
) bottom
= term
->rows
-1;
3105 for (i
= top
; i
<= bottom
&& i
< term
->rows
; i
++) {
3106 if ((term
->disptext
[i
* (term
->cols
+ 1) + term
->cols
] &
3107 LATTR_MODE
) == LATTR_NORM
)
3108 for (j
= left
; j
<= right
&& j
< term
->cols
; j
++)
3109 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3111 for (j
= left
/ 2; j
<= right
/ 2 + 1 && j
< term
->cols
; j
++)
3112 term
->disptext
[i
* (term
->cols
+ 1) + j
] = ATTR_INVALID
;
3115 /* This should happen soon enough, also for some reason it sometimes
3116 * fails to actually do anything when re-sizing ... painting the wrong
3120 do_paint (term
, ctx
, FALSE
);
3124 * Attempt to scroll the scrollback. The second parameter gives the
3125 * position we want to scroll to; the first is +1 to denote that
3126 * this position is relative to the beginning of the scrollback, -1
3127 * to denote it is relative to the end, and 0 to denote that it is
3128 * relative to the current position.
3130 void term_scroll(Terminal
*term
, int rel
, int where
)
3132 int sbtop
= -count234(term
->scrollback
);
3134 term
->disptop
= (rel
< 0 ?
0 : rel
> 0 ? sbtop
: term
->disptop
) + where
;
3135 if (term
->disptop
< sbtop
)
3136 term
->disptop
= sbtop
;
3137 if (term
->disptop
> 0)
3143 static void clipme(Terminal
*term
, pos top
, pos bottom
, int rect
)
3146 wchar_t *wbptr
; /* where next char goes within workbuf */
3148 int wblen
= 0; /* workbuf len */
3149 int buflen
; /* amount of memory allocated to workbuf */
3151 buflen
= 5120; /* Default size */
3152 workbuf
= smalloc(buflen
* sizeof(wchar_t));
3153 wbptr
= workbuf
; /* start filling here */
3154 old_top_x
= top
.x
; /* needed for rect==1 */
3156 while (poslt(top
, bottom
)) {
3158 unsigned long *ldata
= lineptr(top
.y
);
3162 * nlpos will point at the maximum position on this line we
3163 * should copy up to. So we start it at the end of the
3167 nlpos
.x
= term
->cols
;
3170 * ... move it backwards if there's unused space at the end
3171 * of the line (and also set `nl' if this is the case,
3172 * because in normal selection mode this means we need a
3173 * newline at the end)...
3175 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3176 while (((ldata
[nlpos
.x
- 1] & 0xFF) == 0x20 ||
3177 (DIRECT_CHAR(ldata
[nlpos
.x
- 1]) &&
3178 (ldata
[nlpos
.x
- 1] & CHAR_MASK
) == 0x20))
3179 && poslt(top
, nlpos
))
3181 if (poslt(nlpos
, bottom
))
3186 * ... and then clip it to the terminal x coordinate if
3187 * we're doing rectangular selection. (In this case we
3188 * still did the above, so that copying e.g. the right-hand
3189 * column from a table doesn't fill with spaces on the
3193 if (nlpos
.x
> bottom
.x
)
3195 nl
= (top
.y
< bottom
.y
);
3198 while (poslt(top
, bottom
) && poslt(top
, nlpos
)) {
3201 sprintf(cbuf
, "<U+%04x>", (ldata
[top
.x
] & 0xFFFF));
3203 wchar_t cbuf
[16], *p
;
3204 int uc
= (ldata
[top
.x
] & 0xFFFF);
3207 if (uc
== UCSWIDE
) {
3212 switch (uc
& CSET_MASK
) {
3215 uc
= unitab_xterm
[uc
& 0xFF];
3219 uc
= unitab_line
[uc
& 0xFF];
3222 uc
= unitab_scoacs
[uc
&0xFF];
3225 switch (uc
& CSET_MASK
) {
3227 uc
= unitab_font
[uc
& 0xFF];
3230 uc
= unitab_oemcp
[uc
& 0xFF];
3234 set
= (uc
& CSET_MASK
);
3235 c
= (uc
& CHAR_MASK
);
3239 if (DIRECT_FONT(uc
)) {
3240 if (c
>= ' ' && c
!= 0x7F) {
3241 unsigned char buf
[4];
3244 if (is_dbcs_leadbyte(font_codepage
, (BYTE
) c
)) {
3246 buf
[1] = (unsigned char) ldata
[top
.x
+ 1];
3247 rv
= mb_to_wc(font_codepage
, 0, buf
, 2, wbuf
, 4);
3251 rv
= mb_to_wc(font_codepage
, 0, buf
, 1, wbuf
, 4);
3255 memcpy(cbuf
, wbuf
, rv
* sizeof(wchar_t));
3262 for (p
= cbuf
; *p
; p
++) {
3263 /* Enough overhead for trailing NL and nul */
3264 if (wblen
>= buflen
- 16) {
3267 sizeof(wchar_t) * (buflen
+= 100));
3268 wbptr
= workbuf
+ wblen
;
3277 for (i
= 0; i
< sel_nl_sz
; i
++) {
3279 *wbptr
++ = sel_nl
[i
];
3283 top
.x
= rect ? old_top_x
: 0;
3285 #if SELECTION_NUL_TERMINATED
3289 write_clip(workbuf
, wblen
, FALSE
); /* transfer to clipboard */
3290 if (buflen
> 0) /* indicates we allocated this buffer */
3294 void term_copyall(Terminal
*term
)
3297 top
.y
= -count234(term
->scrollback
);
3299 clipme(term
, top
, term
->curs
, 0);
3303 * The wordness array is mainly for deciding the disposition of the
3304 * US-ASCII characters.
3306 static int wordtype(Terminal
*term
, int uc
)
3309 int start
, end
, ctype
;
3311 static const struct ucsword ucs_words
[] = {
3317 0x037e, 0x037e, 1}, /* Greek question mark */
3319 0x0387, 0x0387, 1}, /* Greek ano teleia */
3321 0x055a, 0x055f, 1}, /* Armenian punctuation */
3323 0x0589, 0x0589, 1}, /* Armenian full stop */
3325 0x0700, 0x070d, 1}, /* Syriac punctuation */
3327 0x104a, 0x104f, 1}, /* Myanmar punctuation */
3329 0x10fb, 0x10fb, 1}, /* Georgian punctuation */
3331 0x1361, 0x1368, 1}, /* Ethiopic punctuation */
3333 0x166d, 0x166e, 1}, /* Canadian Syl. punctuation */
3335 0x17d4, 0x17dc, 1}, /* Khmer punctuation */
3337 0x1800, 0x180a, 1}, /* Mongolian punctuation */
3339 0x2000, 0x200a, 0}, /* Various spaces */
3341 0x2070, 0x207f, 2}, /* superscript */
3343 0x2080, 0x208f, 2}, /* subscript */
3345 0x200b, 0x27ff, 1}, /* punctuation and symbols */
3347 0x3000, 0x3000, 0}, /* ideographic space */
3349 0x3001, 0x3020, 1}, /* ideographic punctuation */
3351 0x303f, 0x309f, 3}, /* Hiragana */
3353 0x30a0, 0x30ff, 3}, /* Katakana */
3355 0x3300, 0x9fff, 3}, /* CJK Ideographs */
3357 0xac00, 0xd7a3, 3}, /* Hangul Syllables */
3359 0xf900, 0xfaff, 3}, /* CJK Ideographs */
3361 0xfe30, 0xfe6b, 1}, /* punctuation forms */
3363 0xff00, 0xff0f, 1}, /* half/fullwidth ASCII */
3365 0xff1a, 0xff20, 1}, /* half/fullwidth ASCII */
3367 0xff3b, 0xff40, 1}, /* half/fullwidth ASCII */
3369 0xff5b, 0xff64, 1}, /* half/fullwidth ASCII */
3371 0xfff0, 0xffff, 0}, /* half/fullwidth ASCII */
3375 const struct ucsword
*wptr
;
3377 uc
&= (CSET_MASK
| CHAR_MASK
);
3379 switch (uc
& CSET_MASK
) {
3381 uc
= unitab_xterm
[uc
& 0xFF];
3384 uc
= unitab_line
[uc
& 0xFF];
3387 uc
= unitab_scoacs
[uc
&0xFF];
3390 switch (uc
& CSET_MASK
) {
3392 uc
= unitab_font
[uc
& 0xFF];
3395 uc
= unitab_oemcp
[uc
& 0xFF];
3399 /* For DBCS font's I can't do anything usefull. Even this will sometimes
3400 * fail as there's such a thing as a double width space. :-(
3402 if (dbcs_screenfont
&& font_codepage
== line_codepage
)
3406 return term
->wordness
[uc
];
3408 for (wptr
= ucs_words
; wptr
->start
; wptr
++) {
3409 if (uc
>= wptr
->start
&& uc
<= wptr
->end
)
3417 * Spread the selection outwards according to the selection mode.
3419 static pos
sel_spread_half(Terminal
*term
, pos p
, int dir
)
3421 unsigned long *ldata
;
3423 int topy
= -count234(term
->scrollback
);
3425 ldata
= lineptr(p
.y
);
3427 switch (term
->selmode
) {
3430 * In this mode, every character is a separate unit, except
3431 * for runs of spaces at the end of a non-wrapping line.
3433 if (!(ldata
[term
->cols
] & LATTR_WRAPPED
)) {
3434 unsigned long *q
= ldata
+ term
->cols
;
3435 while (q
> ldata
&& (q
[-1] & CHAR_MASK
) == 0x20)
3437 if (q
== ldata
+ term
->cols
)
3439 if (p
.x
>= q
- ldata
)
3440 p
.x
= (dir
== -1 ? q
- ldata
: term
->cols
- 1);
3445 * In this mode, the units are maximal runs of characters
3446 * whose `wordness' has the same value.
3448 wvalue
= wordtype(term
, ldata
[p
.x
]);
3451 if (p
.x
< term
->cols
-1) {
3452 if (wordtype(term
, ldata
[p
.x
+ 1]) == wvalue
)
3457 if (ldata
[term
->cols
] & LATTR_WRAPPED
) {
3458 unsigned long *ldata2
;
3459 ldata2
= lineptr(p
.y
+1);
3460 if (wordtype(term
, ldata2
[0]) == wvalue
) {
3473 if (wordtype(term
, ldata
[p
.x
- 1]) == wvalue
)
3478 unsigned long *ldata2
;
3481 ldata2
= lineptr(p
.y
-1);
3482 if ((ldata2
[term
->cols
] & LATTR_WRAPPED
) &&
3483 wordtype(term
, ldata2
[term
->cols
-1]) == wvalue
) {
3495 * In this mode, every line is a unit.
3497 p
.x
= (dir
== -1 ?
0 : term
->cols
- 1);
3503 static void sel_spread(Terminal
*term
)
3505 if (term
->seltype
== LEXICOGRAPHIC
) {
3506 term
->selstart
= sel_spread_half(term
, term
->selstart
, -1);
3507 decpos(term
->selend
);
3508 term
->selend
= sel_spread_half(term
, term
->selend
, +1);
3509 incpos(term
->selend
);
3513 void term_do_paste(Terminal
*term
)
3518 get_clip(&data
, &len
);
3519 if (data
&& len
> 0) {
3522 term_seen_key_event(term
); /* pasted data counts */
3524 if (term
->paste_buffer
)
3525 sfree(term
->paste_buffer
);
3526 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3527 term
->paste_buffer
= smalloc(len
* sizeof(wchar_t));
3530 while (p
< data
+ len
) {
3531 while (p
< data
+ len
&&
3532 !(p
<= data
+ len
- sel_nl_sz
&&
3533 !memcmp(p
, sel_nl
, sizeof(sel_nl
))))
3538 for (i
= 0; i
< p
- q
; i
++) {
3539 term
->paste_buffer
[term
->paste_len
++] = q
[i
];
3543 if (p
<= data
+ len
- sel_nl_sz
&&
3544 !memcmp(p
, sel_nl
, sizeof(sel_nl
))) {
3545 term
->paste_buffer
[term
->paste_len
++] = '\r';
3551 /* Assume a small paste will be OK in one go. */
3552 if (term
->paste_len
< 256) {
3554 luni_send(term
->ldisc
, term
->paste_buffer
, term
->paste_len
, 0);
3555 if (term
->paste_buffer
)
3556 sfree(term
->paste_buffer
);
3557 term
->paste_buffer
= 0;
3558 term
->paste_pos
= term
->paste_hold
= term
->paste_len
= 0;
3561 get_clip(NULL
, NULL
);
3564 void term_mouse(Terminal
*term
, Mouse_Button b
, Mouse_Action a
, int x
, int y
,
3565 int shift
, int ctrl
, int alt
)
3568 unsigned long *ldata
;
3569 int raw_mouse
= (term
->xterm_mouse
&&
3570 !cfg
.no_mouse_rep
&&
3571 !(cfg
.mouse_override
&& shift
));
3572 int default_seltype
;
3576 if (a
== MA_DRAG
&& !raw_mouse
)
3577 term_scroll(term
, 0, -1);
3579 if (y
>= term
->rows
) {
3581 if (a
== MA_DRAG
&& !raw_mouse
)
3582 term_scroll(term
, 0, +1);
3591 if (x
>= term
->cols
)
3594 selpoint
.y
= y
+ term
->disptop
;
3596 ldata
= lineptr(selpoint
.y
);
3597 if ((ldata
[term
->cols
] & LATTR_MODE
) != LATTR_NORM
)
3601 int encstate
= 0, r
, c
;
3608 encstate
= 0x20; /* left button down */
3619 case MBT_WHEEL_DOWN
:
3622 default: break; /* placate gcc warning about enum use */
3626 if (term
->xterm_mouse
== 1)
3632 term
->mouse_is_down
= 0;
3635 if (term
->mouse_is_down
== b
)
3637 term
->mouse_is_down
= b
;
3639 default: break; /* placate gcc warning about enum use */
3648 sprintf(abuf
, "\033[M%c%c%c", encstate
, c
, r
);
3649 ldisc_send(term
->ldisc
, abuf
, 6, 0);
3654 b
= translate_button(b
);
3657 * Set the selection type (rectangular or normal) at the start
3658 * of a selection attempt, from the state of Alt.
3660 if (!alt
^ !cfg
.rect_select
)
3661 default_seltype
= RECTANGULAR
;
3663 default_seltype
= LEXICOGRAPHIC
;
3665 if (term
->selstate
== NO_SELECTION
) {
3666 term
->seltype
= default_seltype
;
3669 if (b
== MBT_SELECT
&& a
== MA_CLICK
) {
3671 term
->selstate
= ABOUT_TO
;
3672 term
->seltype
= default_seltype
;
3673 term
->selanchor
= selpoint
;
3674 term
->selmode
= SM_CHAR
;
3675 } else if (b
== MBT_SELECT
&& (a
== MA_2CLK
|| a
== MA_3CLK
)) {
3677 term
->selmode
= (a
== MA_2CLK ? SM_WORD
: SM_LINE
);
3678 term
->selstate
= DRAGGING
;
3679 term
->selstart
= term
->selanchor
= selpoint
;
3680 term
->selend
= term
->selstart
;
3681 incpos(term
->selend
);
3683 } else if ((b
== MBT_SELECT
&& a
== MA_DRAG
) ||
3684 (b
== MBT_EXTEND
&& a
!= MA_RELEASE
)) {
3685 if (term
->selstate
== ABOUT_TO
&& poseq(term
->selanchor
, selpoint
))
3687 if (b
== MBT_EXTEND
&& a
!= MA_DRAG
&& term
->selstate
== SELECTED
) {
3688 if (term
->seltype
== LEXICOGRAPHIC
) {
3690 * For normal selection, we extend by moving
3691 * whichever end of the current selection is closer
3694 if (posdiff(selpoint
, term
->selstart
) <
3695 posdiff(term
->selend
, term
->selstart
) / 2) {
3696 term
->selanchor
= term
->selend
;
3697 decpos(term
->selanchor
);
3699 term
->selanchor
= term
->selstart
;
3703 * For rectangular selection, we have a choice of
3704 * _four_ places to put selanchor and selpoint: the
3705 * four corners of the selection.
3707 if (2*selpoint
.x
< term
->selstart
.x
+ term
->selend
.x
)
3708 term
->selanchor
.x
= term
->selend
.x
-1;
3710 term
->selanchor
.x
= term
->selstart
.x
;
3712 if (2*selpoint
.y
< term
->selstart
.y
+ term
->selend
.y
)
3713 term
->selanchor
.y
= term
->selend
.y
;
3715 term
->selanchor
.y
= term
->selstart
.y
;
3717 term
->selstate
= DRAGGING
;
3719 if (term
->selstate
!= ABOUT_TO
&& term
->selstate
!= DRAGGING
)
3720 term
->selanchor
= selpoint
;
3721 term
->selstate
= DRAGGING
;
3722 if (term
->seltype
== LEXICOGRAPHIC
) {
3724 * For normal selection, we set (selstart,selend) to
3725 * (selpoint,selanchor) in some order.
3727 if (poslt(selpoint
, term
->selanchor
)) {
3728 term
->selstart
= selpoint
;
3729 term
->selend
= term
->selanchor
;
3730 incpos(term
->selend
);
3732 term
->selstart
= term
->selanchor
;
3733 term
->selend
= selpoint
;
3734 incpos(term
->selend
);
3738 * For rectangular selection, we may need to
3739 * interchange x and y coordinates (if the user has
3740 * dragged in the -x and +y directions, or vice versa).
3742 term
->selstart
.x
= min(term
->selanchor
.x
, selpoint
.x
);
3743 term
->selend
.x
= 1+max(term
->selanchor
.x
, selpoint
.x
);
3744 term
->selstart
.y
= min(term
->selanchor
.y
, selpoint
.y
);
3745 term
->selend
.y
= max(term
->selanchor
.y
, selpoint
.y
);
3748 } else if ((b
== MBT_SELECT
|| b
== MBT_EXTEND
) && a
== MA_RELEASE
) {
3749 if (term
->selstate
== DRAGGING
) {
3751 * We've completed a selection. We now transfer the
3752 * data to the clipboard.
3754 clipme(term
, term
->selstart
, term
->selend
,
3755 (term
->seltype
== RECTANGULAR
));
3756 term
->selstate
= SELECTED
;
3758 term
->selstate
= NO_SELECTION
;
3759 } else if (b
== MBT_PASTE
3761 #if MULTICLICK_ONLY_EVENT
3762 || a
== MA_2CLK
|| a
== MA_3CLK
3771 void term_nopaste(Terminal
*term
)
3773 if (term
->paste_len
== 0)
3775 sfree(term
->paste_buffer
);
3776 term
->paste_buffer
= NULL
;
3777 term
->paste_len
= 0;
3780 int term_paste_pending(Terminal
*term
)
3782 return term
->paste_len
!= 0;
3785 void term_paste(Terminal
*term
)
3787 long now
, paste_diff
;
3789 if (term
->paste_len
== 0)
3792 /* Don't wait forever to paste */
3793 if (term
->paste_hold
) {
3794 now
= GETTICKCOUNT();
3795 paste_diff
= now
- term
->last_paste
;
3796 if (paste_diff
>= 0 && paste_diff
< 450)
3799 term
->paste_hold
= 0;
3801 while (term
->paste_pos
< term
->paste_len
) {
3803 while (n
+ term
->paste_pos
< term
->paste_len
) {
3804 if (term
->paste_buffer
[term
->paste_pos
+ n
++] == '\r')
3808 luni_send(term
->ldisc
, term
->paste_buffer
+ term
->paste_pos
, n
, 0);
3809 term
->paste_pos
+= n
;
3811 if (term
->paste_pos
< term
->paste_len
) {
3812 term
->paste_hold
= 1;
3816 sfree(term
->paste_buffer
);
3817 term
->paste_buffer
= NULL
;
3818 term
->paste_len
= 0;
3821 static void deselect(Terminal
*term
)
3823 term
->selstate
= NO_SELECTION
;
3824 term
->selstart
.x
= term
->selstart
.y
= term
->selend
.x
= term
->selend
.y
= 0;
3827 void term_deselect(Terminal
*term
)
3833 int term_ldisc(Terminal
*term
, int option
)
3835 if (option
== LD_ECHO
)
3836 return term
->term_echoing
;
3837 if (option
== LD_EDIT
)
3838 return term
->term_editing
;
3843 * from_backend(), to get data from the backend for the terminal.
3845 int from_backend(void *vterm
, int is_stderr
, char *data
, int len
)
3847 Terminal
*term
= (Terminal
*)vterm
;
3851 bufchain_add(&term
->inbuf
, data
, len
);
3854 * term_out() always completely empties inbuf. Therefore,
3855 * there's no reason at all to return anything other than zero
3856 * from this function, because there _can't_ be a question of
3857 * the remote side needing to wait until term_out() has cleared
3860 * This is a slightly suboptimal way to deal with SSH2 - in
3861 * principle, the window mechanism would allow us to continue
3862 * to accept data on forwarded ports and X connections even
3863 * while the terminal processing was going slowly - but we
3864 * can't do the 100% right thing without moving the terminal
3865 * processing into a separate thread, and that might hurt
3866 * portability. So we manage stdout buffering the old SSH1 way:
3867 * if the terminal processing goes slowly, the whole SSH
3868 * connection stops accepting data until it's ready.
3870 * In practice, I can't imagine this causing serious trouble.