]> git.proxmox.com Git - mirror_xterm.js.git/blame - src/InputHandler.ts
Add character sets for most languages
[mirror_xterm.js.git] / src / InputHandler.ts
CommitLineData
ef60e50e
DI
1/**
2 * @license MIT
3 */
4
04b1ebf1 5import { IInputHandler, ITerminal } from './Interfaces';
db81c28b 6import { C0 } from './EscapeSequences';
c43c3b41 7import { CHARSETS } from './Charsets';
04b1ebf1 8
ef60e50e
DI
9/**
10 * The terminal's standard implementation of IInputHandler, this handles all
11 * input from the Parser.
12 *
13 * Refer to http://invisible-island.net/xterm/ctlseqs/ctlseqs.html to understand
14 * each function's header comment.
15 */
04b1ebf1
DI
16export class InputHandler implements IInputHandler {
17 // TODO: We want to type _terminal when it's pulled into TS
18 constructor(private _terminal: any) { }
19
fa3484cd
DI
20 public addChar(char: string, code: number): void {
21 if (char >= ' ') {
22 // calculate print space
23 // expensive call, therefore we save width in line buffer
24 const ch_width = wcwidth(code);
25
26 if (this._terminal.charset && this._terminal.charset[char]) {
27 char = this._terminal.charset[char];
28 }
29
30 let row = this._terminal.y + this._terminal.ybase;
31
32 // insert combining char in last cell
33 // FIXME: needs handling after cursor jumps
34 if (!ch_width && this._terminal.x) {
35 // dont overflow left
36 if (this._terminal.lines.get(row)[this._terminal.x - 1]) {
37 if (!this._terminal.lines.get(row)[this._terminal.x - 1][2]) {
38
39 // found empty cell after fullwidth, need to go 2 cells back
40 if (this._terminal.lines.get(row)[this._terminal.x - 2])
41 this._terminal.lines.get(row)[this._terminal.x - 2][1] += char;
42
43 } else {
44 this._terminal.lines.get(row)[this._terminal.x - 1][1] += char;
45 }
46 this._terminal.updateRange(this._terminal.y);
47 }
48 return;
49 }
50
51 // goto next line if ch would overflow
52 // TODO: needs a global min terminal width of 2
53 if (this._terminal.x + ch_width - 1 >= this._terminal.cols) {
54 // autowrap - DECAWM
55 if (this._terminal.wraparoundMode) {
56 this._terminal.x = 0;
57 this._terminal.y++;
58 if (this._terminal.y > this._terminal.scrollBottom) {
59 this._terminal.y--;
60 this._terminal.scroll();
61 }
62 } else {
63 this._terminal.x = this._terminal.cols - 1;
64 if (ch_width === 2) // FIXME: check for xterm behavior
65 return;
66 }
67 }
68 row = this._terminal.y + this._terminal.ybase;
69
70 // insert mode: move characters to right
71 if (this._terminal.insertMode) {
72 // do this twice for a fullwidth char
73 for (let moves = 0; moves < ch_width; ++moves) {
74 // remove last cell, if it's width is 0
75 // we have to adjust the second last cell as well
76 const removed = this._terminal.lines.get(this._terminal.y + this._terminal.ybase).pop();
77 if (removed[2] === 0
78 && this._terminal.lines.get(row)[this._terminal.cols - 2]
79 && this._terminal.lines.get(row)[this._terminal.cols - 2][2] === 2)
80 this._terminal.lines.get(row)[this._terminal.cols - 2] = [this._terminal.curAttr, ' ', 1];
81
82 // insert empty cell at cursor
83 this._terminal.lines.get(row).splice(this._terminal.x, 0, [this._terminal.curAttr, ' ', 1]);
84 }
85 }
86
87 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, char, ch_width];
88 this._terminal.x++;
89 this._terminal.updateRange(this._terminal.y);
90
91 // fullwidth char - set next cell width to zero and advance cursor
92 if (ch_width === 2) {
93 this._terminal.lines.get(row)[this._terminal.x] = [this._terminal.curAttr, '', 0];
94 this._terminal.x++;
95 }
96 }
97 }
98
db81c28b
DI
99 /**
100 * BEL
101 * Bell (Ctrl-G).
102 */
04b1ebf1
DI
103 public bell(): void {
104 if (!this._terminal.visualBell) {
105 return;
106 }
107 this._terminal.element.style.borderColor = 'white';
108 setTimeout(() => this._terminal.element.style.borderColor = '', 10);
109 if (this._terminal.popOnBell) {
110 this._terminal.focus();
111 }
112 }
113
db81c28b
DI
114 /**
115 * LF
116 * Line Feed or New Line (NL). (LF is Ctrl-J).
117 */
04b1ebf1
DI
118 public lineFeed(): void {
119 if (this._terminal.convertEol) {
120 this._terminal.x = 0;
121 }
122 this._terminal.y++;
123 if (this._terminal.y > this._terminal.scrollBottom) {
124 this._terminal.y--;
125 this._terminal.scroll();
126 }
127 }
128
db81c28b
DI
129 /**
130 * CR
131 * Carriage Return (Ctrl-M).
132 */
04b1ebf1
DI
133 public carriageReturn(): void {
134 this._terminal.x = 0;
135 }
136
db81c28b
DI
137 /**
138 * BS
139 * Backspace (Ctrl-H).
140 */
04b1ebf1
DI
141 public backspace(): void {
142 if (this._terminal.x > 0) {
143 this._terminal.x--;
144 }
145 }
146
db81c28b
DI
147 /**
148 * TAB
149 * Horizontal Tab (HT) (Ctrl-I).
150 */
04b1ebf1
DI
151 public tab(): void {
152 this._terminal.x = this._terminal.nextStop();
153 }
154
db81c28b
DI
155 /**
156 * SO
157 * Shift Out (Ctrl-N) -> Switch to Alternate Character Set. This invokes the
158 * G1 character set.
159 */
04b1ebf1
DI
160 public shiftOut(): void {
161 this._terminal.setgLevel(1);
162 }
163
db81c28b
DI
164 /**
165 * SI
166 * Shift In (Ctrl-O) -> Switch to Standard Character Set. This invokes the G0
167 * character set (the default).
168 */
04b1ebf1
DI
169 public shiftIn(): void {
170 this._terminal.setgLevel(0);
171 }
9942477b 172
411b80cd
DI
173 /**
174 * CSI Ps @
175 * Insert Ps (Blank) Character(s) (default = 1) (ICH).
176 */
177 public insertChars(params: number[]): void {
178 let param, row, j, ch;
179
180 param = params[0];
181 if (param < 1) param = 1;
182
183 row = this._terminal.y + this._terminal.ybase;
184 j = this._terminal.x;
185 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
186
187 while (param-- && j < this._terminal.cols) {
188 this._terminal.lines.get(row).splice(j++, 0, ch);
189 this._terminal.lines.get(row).pop();
190 }
191 }
192
db81c28b
DI
193 /**
194 * CSI Ps A
195 * Cursor Up Ps Times (default = 1) (CUU).
196 */
9942477b
DI
197 public cursorUp(params: number[]): void {
198 let param = params[0];
199 if (param < 1) {
200 param = 1;
201 }
202 this._terminal.y -= param;
203 if (this._terminal.y < 0) {
204 this._terminal.y = 0;
205 }
206 }
207
db81c28b
DI
208 /**
209 * CSI Ps B
210 * Cursor Down Ps Times (default = 1) (CUD).
211 */
9942477b
DI
212 public cursorDown(params: number[]) {
213 let param = params[0];
214 if (param < 1) {
215 param = 1;
216 }
217 this._terminal.y += param;
218 if (this._terminal.y >= this._terminal.rows) {
219 this._terminal.y = this._terminal.rows - 1;
220 }
221 }
222
db81c28b
DI
223 /**
224 * CSI Ps C
225 * Cursor Forward Ps Times (default = 1) (CUF).
226 */
9942477b
DI
227 public cursorForward(params: number[]) {
228 let param = params[0];
229 if (param < 1) {
230 param = 1;
231 }
232 this._terminal.x += param;
233 if (this._terminal.x >= this._terminal.cols) {
234 this._terminal.x = this._terminal.cols - 1;
235 }
236 }
237
db81c28b
DI
238 /**
239 * CSI Ps D
240 * Cursor Backward Ps Times (default = 1) (CUB).
241 */
9942477b
DI
242 public cursorBackward(params: number[]) {
243 let param = params[0];
244 if (param < 1) {
245 param = 1;
246 }
247 this._terminal.x -= param;
248 if (this._terminal.x < 0) {
249 this._terminal.x = 0;
250 }
251 }
db81c28b 252
411b80cd
DI
253 /**
254 * CSI Ps E
255 * Cursor Next Line Ps Times (default = 1) (CNL).
256 * same as CSI Ps B ?
257 */
258 public cursorNextLine(params: number[]): void {
259 let param = params[0];
260 if (param < 1) {
261 param = 1;
262 }
263 this._terminal.y += param;
264 if (this._terminal.y >= this._terminal.rows) {
265 this._terminal.y = this._terminal.rows - 1;
266 }
267 this._terminal.x = 0;
268 };
269
270
271 /**
272 * CSI Ps F
273 * Cursor Preceding Line Ps Times (default = 1) (CNL).
274 * reuse CSI Ps A ?
275 */
276 public cursorPrecedingLine(params: number[]): void {
277 let param = params[0];
278 if (param < 1) {
279 param = 1;
280 }
281 this._terminal.y -= param;
282 if (this._terminal.y < 0) {
283 this._terminal.y = 0;
284 }
285 this._terminal.x = 0;
286 };
287
288
289 /**
290 * CSI Ps G
291 * Cursor Character Absolute [column] (default = [row,1]) (CHA).
292 */
293 public cursorCharAbsolute(params: number[]): void {
294 let param = params[0];
295 if (param < 1) {
296 param = 1;
297 }
298 this._terminal.x = param - 1;
299 }
300
db81c28b
DI
301 /**
302 * CSI Ps ; Ps H
303 * Cursor Position [row;column] (default = [1,1]) (CUP).
304 */
411b80cd 305 public cursorPosition(params: number[]): void {
db81c28b
DI
306 let row, col;
307
308 row = params[0] - 1;
309
310 if (params.length >= 2) {
311 col = params[1] - 1;
312 } else {
313 col = 0;
314 }
315
316 if (row < 0) {
317 row = 0;
318 } else if (row >= this._terminal.rows) {
319 row = this._terminal.rows - 1;
320 }
321
322 if (col < 0) {
323 col = 0;
324 } else if (col >= this._terminal.cols) {
325 col = this._terminal.cols - 1;
326 }
327
328 this._terminal.x = col;
329 this._terminal.y = row;
330 }
331
9b662080
DI
332 /**
333 * CSI Ps I
334 * Cursor Forward Tabulation Ps tab stops (default = 1) (CHT).
335 */
336 public cursorForwardTab(params: number[]): void {
337 let param = params[0] || 1;
338 while (param--) {
339 this._terminal.x = this._terminal.nextStop();
340 }
341 }
342
db81c28b
DI
343 /**
344 * CSI Ps J Erase in Display (ED).
345 * Ps = 0 -> Erase Below (default).
346 * Ps = 1 -> Erase Above.
347 * Ps = 2 -> Erase All.
348 * Ps = 3 -> Erase Saved Lines (xterm).
349 * CSI ? Ps J
350 * Erase in Display (DECSED).
351 * Ps = 0 -> Selective Erase Below (default).
352 * Ps = 1 -> Selective Erase Above.
353 * Ps = 2 -> Selective Erase All.
354 */
411b80cd 355 public eraseInDisplay(params: number[]): void {
db81c28b
DI
356 let j;
357 switch (params[0]) {
358 case 0:
359 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
360 j = this._terminal.y + 1;
361 for (; j < this._terminal.rows; j++) {
362 this._terminal.eraseLine(j);
363 }
364 break;
365 case 1:
366 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
367 j = this._terminal.y;
368 while (j--) {
369 this._terminal.eraseLine(j);
370 }
371 break;
372 case 2:
373 j = this._terminal.rows;
374 while (j--) this._terminal.eraseLine(j);
375 break;
376 case 3:
377 ; // no saved lines
378 break;
379 }
380 }
381
382 /**
383 * CSI Ps K Erase in Line (EL).
384 * Ps = 0 -> Erase to Right (default).
385 * Ps = 1 -> Erase to Left.
386 * Ps = 2 -> Erase All.
387 * CSI ? Ps K
388 * Erase in Line (DECSEL).
389 * Ps = 0 -> Selective Erase to Right (default).
390 * Ps = 1 -> Selective Erase to Left.
391 * Ps = 2 -> Selective Erase All.
392 */
411b80cd 393 public eraseInLine(params: number[]): void {
db81c28b
DI
394 switch (params[0]) {
395 case 0:
396 this._terminal.eraseRight(this._terminal.x, this._terminal.y);
397 break;
398 case 1:
399 this._terminal.eraseLeft(this._terminal.x, this._terminal.y);
400 break;
401 case 2:
402 this._terminal.eraseLine(this._terminal.y);
403 break;
404 }
405 }
406
f9a286a8
DI
407 /**
408 * CSI Ps L
409 * Insert Ps Line(s) (default = 1) (IL).
410 */
411 public insertLines(params: number[]): void {
412 let param, row, j;
413
414 param = params[0];
415 if (param < 1) {
416 param = 1;
417 }
418 row = this._terminal.y + this._terminal.ybase;
419
420 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
421 j = this._terminal.rows - 1 + this._terminal.ybase - j + 1;
422
423 while (param--) {
424 if (this._terminal.lines.length === this._terminal.lines.maxLength) {
425 // Trim the start of lines to make room for the new line
426 this._terminal.lines.trimStart(1);
427 this._terminal.ybase--;
428 this._terminal.ydisp--;
429 row--;
430 j--;
431 }
432 // test: echo -e '\e[44m\e[1L\e[0m'
433 // blankLine(true) - xterm/linux behavior
434 this._terminal.lines.splice(row, 0, this._terminal.blankLine(true));
435 this._terminal.lines.splice(j, 1);
436 }
437
438 // this.maxRange();
439 this._terminal.updateRange(this._terminal.y);
440 this._terminal.updateRange(this._terminal.scrollBottom);
441 }
442
443 /**
444 * CSI Ps M
445 * Delete Ps Line(s) (default = 1) (DL).
446 */
447 public deleteLines(params: number[]): void {
448 let param, row, j;
449
450 param = params[0];
451 if (param < 1) {
452 param = 1;
453 }
454 row = this._terminal.y + this._terminal.ybase;
455
456 j = this._terminal.rows - 1 - this._terminal.scrollBottom;
457 j = this._terminal.rows - 1 + this._terminal.ybase - j;
458
459 while (param--) {
460 if (this._terminal.lines.length === this._terminal.lines.maxLength) {
461 // Trim the start of lines to make room for the new line
462 this._terminal.lines.trimStart(1);
463 this._terminal.ybase -= 1;
464 this._terminal.ydisp -= 1;
465 }
466 // test: echo -e '\e[44m\e[1M\e[0m'
467 // blankLine(true) - xterm/linux behavior
468 this._terminal.lines.splice(j + 1, 0, this._terminal.blankLine(true));
469 this._terminal.lines.splice(row, 1);
470 }
471
472 // this.maxRange();
473 this._terminal.updateRange(this._terminal.y);
474 this._terminal.updateRange(this._terminal.scrollBottom);
475 }
476
477 /**
478 * CSI Ps P
479 * Delete Ps Character(s) (default = 1) (DCH).
480 */
481 public deleteChars(params: number[]): void {
482 let param, row, ch;
483
484 param = params[0];
485 if (param < 1) {
486 param = 1;
487 }
488
489 row = this._terminal.y + this._terminal.ybase;
490 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
491
492 while (param--) {
493 this._terminal.lines.get(row).splice(this._terminal.x, 1);
494 this._terminal.lines.get(row).push(ch);
495 }
496 }
497
f4846aa1
DI
498 /**
499 * CSI Ps S Scroll up Ps lines (default = 1) (SU).
500 */
501 public scrollUp(params: number[]): void {
502 let param = params[0] || 1;
503 while (param--) {
504 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 1);
505 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 0, this._terminal.blankLine());
506 }
507 // this.maxRange();
508 this._terminal.updateRange(this._terminal.scrollTop);
509 this._terminal.updateRange(this._terminal.scrollBottom);
510 }
511
512 /**
513 * CSI Ps T Scroll down Ps lines (default = 1) (SD).
514 */
515 public scrollDown(params: number[]): void {
516 let param = params[0] || 1;
517 while (param--) {
518 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollBottom, 1);
519 this._terminal.lines.splice(this._terminal.ybase + this._terminal.scrollTop, 0, this._terminal.blankLine());
520 }
521 // this.maxRange();
522 this._terminal.updateRange(this._terminal.scrollTop);
523 this._terminal.updateRange(this._terminal.scrollBottom);
524 }
525
f9a286a8
DI
526 /**
527 * CSI Ps X
528 * Erase Ps Character(s) (default = 1) (ECH).
529 */
530 public eraseChars(params: number[]): void {
531 let param, row, j, ch;
532
533 param = params[0];
534 if (param < 1) {
535 param = 1;
536 }
537
538 row = this._terminal.y + this._terminal.ybase;
539 j = this._terminal.x;
540 ch = [this._terminal.eraseAttr(), ' ', 1]; // xterm
541
542 while (param-- && j < this._terminal.cols) {
543 this._terminal.lines.get(row)[j++] = ch;
544 }
545 }
546
f4846aa1
DI
547 /**
548 * CSI Ps Z Cursor Backward Tabulation Ps tab stops (default = 1) (CBT).
549 */
550 public cursorBackwardTab(params: number[]): void {
551 let param = params[0] || 1;
552 while (param--) {
553 this._terminal.x = this._terminal.prevStop();
554 }
555 }
556
f9a286a8
DI
557 /**
558 * CSI Pm ` Character Position Absolute
559 * [column] (default = [row,1]) (HPA).
560 */
561 public charPosAbsolute(params: number[]): void {
562 let param = params[0];
563 if (param < 1) {
564 param = 1;
565 }
566 this._terminal.x = param - 1;
567 if (this._terminal.x >= this._terminal.cols) {
568 this._terminal.x = this._terminal.cols - 1;
569 }
570 }
571
572 /**
573 * CSI Pm a Character Position Relative
574 * [columns] (default = [row,col+1]) (HPR)
575 * reuse CSI Ps C ?
576 */
577 public HPositionRelative(params: number[]): void {
578 let param = params[0];
579 if (param < 1) {
580 param = 1;
581 }
582 this._terminal.x += param;
583 if (this._terminal.x >= this._terminal.cols) {
584 this._terminal.x = this._terminal.cols - 1;
585 }
586 }
587
f4846aa1
DI
588 /**
589 * CSI Ps b Repeat the preceding graphic character Ps times (REP).
590 */
591 public repeatPrecedingCharacter(params: number[]): void {
592 let param = params[0] || 1
593 , line = this._terminal.lines.get(this._terminal.ybase + this._terminal.y)
594 , ch = line[this._terminal.x - 1] || [this._terminal.defAttr, ' ', 1];
595
596 while (param--) {
597 line[this._terminal.x++] = ch;
598 }
599 }
600
f9a286a8
DI
601 /**
602 * CSI Ps c Send Device Attributes (Primary DA).
603 * Ps = 0 or omitted -> request attributes from terminal. The
604 * response depends on the decTerminalID resource setting.
605 * -> CSI ? 1 ; 2 c (``VT100 with Advanced Video Option'')
606 * -> CSI ? 1 ; 0 c (``VT101 with No Options'')
607 * -> CSI ? 6 c (``VT102'')
608 * -> CSI ? 6 0 ; 1 ; 2 ; 6 ; 8 ; 9 ; 1 5 ; c (``VT220'')
609 * The VT100-style response parameters do not mean anything by
610 * themselves. VT220 parameters do, telling the host what fea-
611 * tures the terminal supports:
612 * Ps = 1 -> 132-columns.
613 * Ps = 2 -> Printer.
614 * Ps = 6 -> Selective erase.
615 * Ps = 8 -> User-defined keys.
616 * Ps = 9 -> National replacement character sets.
617 * Ps = 1 5 -> Technical characters.
618 * Ps = 2 2 -> ANSI color, e.g., VT525.
619 * Ps = 2 9 -> ANSI text locator (i.e., DEC Locator mode).
620 * CSI > Ps c
621 * Send Device Attributes (Secondary DA).
622 * Ps = 0 or omitted -> request the terminal's identification
623 * code. The response depends on the decTerminalID resource set-
624 * ting. It should apply only to VT220 and up, but xterm extends
625 * this to VT100.
626 * -> CSI > Pp ; Pv ; Pc c
627 * where Pp denotes the terminal type
628 * Pp = 0 -> ``VT100''.
629 * Pp = 1 -> ``VT220''.
630 * and Pv is the firmware version (for xterm, this was originally
631 * the XFree86 patch number, starting with 95). In a DEC termi-
632 * nal, Pc indicates the ROM cartridge registration number and is
633 * always zero.
634 * More information:
635 * xterm/charproc.c - line 2012, for more information.
636 * vim responds with ^[[?0c or ^[[?1c after the terminal's response (?)
637 */
638 public sendDeviceAttributes(params: number[]): void {
639 if (params[0] > 0) {
640 return;
641 }
642
643 if (!this._terminal.prefix) {
644 if (this._terminal.is('xterm') || this._terminal.is('rxvt-unicode') || this._terminal.is('screen')) {
645 this._terminal.send(C0.ESC + '[?1;2c');
646 } else if (this._terminal.is('linux')) {
647 this._terminal.send(C0.ESC + '[?6c');
648 }
649 } else if (this._terminal.prefix === '>') {
650 // xterm and urxvt
651 // seem to spit this
652 // out around ~370 times (?).
653 if (this._terminal.is('xterm')) {
654 this._terminal.send(C0.ESC + '[>0;276;0c');
655 } else if (this._terminal.is('rxvt-unicode')) {
656 this._terminal.send(C0.ESC + '[>85;95;0c');
657 } else if (this._terminal.is('linux')) {
658 // not supported by linux console.
659 // linux console echoes parameters.
660 this._terminal.send(params[0] + 'c');
661 } else if (this._terminal.is('screen')) {
662 this._terminal.send(C0.ESC + '[>83;40003;0c');
663 }
664 }
665 }
666
c43c3b41
DI
667 /**
668 * CSI Pm d Vertical Position Absolute (VPA)
669 * [row] (default = [1,column])
670 */
671 public linePosAbsolute(params: number[]): void {
672 let param = params[0];
673 if (param < 1) {
674 param = 1;
675 }
676 this._terminal.y = param - 1;
677 if (this._terminal.y >= this._terminal.rows) {
678 this._terminal.y = this._terminal.rows - 1;
679 }
680 }
681
682 /**
683 * CSI Pm e Vertical Position Relative (VPR)
684 * [rows] (default = [row+1,column])
685 * reuse CSI Ps B ?
686 */
687 public VPositionRelative(params: number[]): void {
688 let param = params[0];
689 if (param < 1) {
690 param = 1;
691 }
692 this._terminal.y += param;
693 if (this._terminal.y >= this._terminal.rows) {
694 this._terminal.y = this._terminal.rows - 1;
695 }
696 }
697
698 /**
699 * CSI Ps ; Ps f
700 * Horizontal and Vertical Position [row;column] (default =
701 * [1,1]) (HVP).
702 */
703 public HVPosition(params: number[]): void {
704 if (params[0] < 1) params[0] = 1;
705 if (params[1] < 1) params[1] = 1;
706
707 this._terminal.y = params[0] - 1;
708 if (this._terminal.y >= this._terminal.rows) {
709 this._terminal.y = this._terminal.rows - 1;
710 }
711
712 this._terminal.x = params[1] - 1;
713 if (this._terminal.x >= this._terminal.cols) {
714 this._terminal.x = this._terminal.cols - 1;
715 }
716 }
717
f4846aa1
DI
718 /**
719 * CSI Ps g Tab Clear (TBC).
720 * Ps = 0 -> Clear Current Column (default).
721 * Ps = 3 -> Clear All.
722 * Potentially:
723 * Ps = 2 -> Clear Stops on Line.
724 * http://vt100.net/annarbor/aaa-ug/section6.html
725 */
726 public tabClear(params: number[]): void {
727 let param = params[0];
728 if (param <= 0) {
729 delete this._terminal.tabs[this._terminal.x];
730 } else if (param === 3) {
731 this._terminal.tabs = {};
732 }
733 }
c43c3b41
DI
734
735 /**
736 * CSI Pm h Set Mode (SM).
737 * Ps = 2 -> Keyboard Action Mode (AM).
738 * Ps = 4 -> Insert Mode (IRM).
739 * Ps = 1 2 -> Send/receive (SRM).
740 * Ps = 2 0 -> Automatic Newline (LNM).
741 * CSI ? Pm h
742 * DEC Private Mode Set (DECSET).
743 * Ps = 1 -> Application Cursor Keys (DECCKM).
744 * Ps = 2 -> Designate USASCII for character sets G0-G3
745 * (DECANM), and set VT100 mode.
746 * Ps = 3 -> 132 Column Mode (DECCOLM).
747 * Ps = 4 -> Smooth (Slow) Scroll (DECSCLM).
748 * Ps = 5 -> Reverse Video (DECSCNM).
749 * Ps = 6 -> Origin Mode (DECOM).
750 * Ps = 7 -> Wraparound Mode (DECAWM).
751 * Ps = 8 -> Auto-repeat Keys (DECARM).
752 * Ps = 9 -> Send Mouse X & Y on button press. See the sec-
753 * tion Mouse Tracking.
754 * Ps = 1 0 -> Show toolbar (rxvt).
755 * Ps = 1 2 -> Start Blinking Cursor (att610).
756 * Ps = 1 8 -> Print form feed (DECPFF).
757 * Ps = 1 9 -> Set print extent to full screen (DECPEX).
758 * Ps = 2 5 -> Show Cursor (DECTCEM).
759 * Ps = 3 0 -> Show scrollbar (rxvt).
760 * Ps = 3 5 -> Enable font-shifting functions (rxvt).
761 * Ps = 3 8 -> Enter Tektronix Mode (DECTEK).
762 * Ps = 4 0 -> Allow 80 -> 132 Mode.
763 * Ps = 4 1 -> more(1) fix (see curses resource).
764 * Ps = 4 2 -> Enable Nation Replacement Character sets (DECN-
765 * RCM).
766 * Ps = 4 4 -> Turn On Margin Bell.
767 * Ps = 4 5 -> Reverse-wraparound Mode.
768 * Ps = 4 6 -> Start Logging. This is normally disabled by a
769 * compile-time option.
770 * Ps = 4 7 -> Use Alternate Screen Buffer. (This may be dis-
771 * abled by the titeInhibit resource).
772 * Ps = 6 6 -> Application keypad (DECNKM).
773 * Ps = 6 7 -> Backarrow key sends backspace (DECBKM).
774 * Ps = 1 0 0 0 -> Send Mouse X & Y on button press and
775 * release. See the section Mouse Tracking.
776 * Ps = 1 0 0 1 -> Use Hilite Mouse Tracking.
777 * Ps = 1 0 0 2 -> Use Cell Motion Mouse Tracking.
778 * Ps = 1 0 0 3 -> Use All Motion Mouse Tracking.
779 * Ps = 1 0 0 4 -> Send FocusIn/FocusOut events.
780 * Ps = 1 0 0 5 -> Enable Extended Mouse Mode.
781 * Ps = 1 0 1 0 -> Scroll to bottom on tty output (rxvt).
782 * Ps = 1 0 1 1 -> Scroll to bottom on key press (rxvt).
783 * Ps = 1 0 3 4 -> Interpret "meta" key, sets eighth bit.
784 * (enables the eightBitInput resource).
785 * Ps = 1 0 3 5 -> Enable special modifiers for Alt and Num-
786 * Lock keys. (This enables the numLock resource).
787 * Ps = 1 0 3 6 -> Send ESC when Meta modifies a key. (This
788 * enables the metaSendsEscape resource).
789 * Ps = 1 0 3 7 -> Send DEL from the editing-keypad Delete
790 * key.
791 * Ps = 1 0 3 9 -> Send ESC when Alt modifies a key. (This
792 * enables the altSendsEscape resource).
793 * Ps = 1 0 4 0 -> Keep selection even if not highlighted.
794 * (This enables the keepSelection resource).
795 * Ps = 1 0 4 1 -> Use the CLIPBOARD selection. (This enables
796 * the selectToClipboard resource).
797 * Ps = 1 0 4 2 -> Enable Urgency window manager hint when
798 * Control-G is received. (This enables the bellIsUrgent
799 * resource).
800 * Ps = 1 0 4 3 -> Enable raising of the window when Control-G
801 * is received. (enables the popOnBell resource).
802 * Ps = 1 0 4 7 -> Use Alternate Screen Buffer. (This may be
803 * disabled by the titeInhibit resource).
804 * Ps = 1 0 4 8 -> Save cursor as in DECSC. (This may be dis-
805 * abled by the titeInhibit resource).
806 * Ps = 1 0 4 9 -> Save cursor as in DECSC and use Alternate
807 * Screen Buffer, clearing it first. (This may be disabled by
808 * the titeInhibit resource). This combines the effects of the 1
809 * 0 4 7 and 1 0 4 8 modes. Use this with terminfo-based
810 * applications rather than the 4 7 mode.
811 * Ps = 1 0 5 0 -> Set terminfo/termcap function-key mode.
812 * Ps = 1 0 5 1 -> Set Sun function-key mode.
813 * Ps = 1 0 5 2 -> Set HP function-key mode.
814 * Ps = 1 0 5 3 -> Set SCO function-key mode.
815 * Ps = 1 0 6 0 -> Set legacy keyboard emulation (X11R6).
816 * Ps = 1 0 6 1 -> Set VT220 keyboard emulation.
817 * Ps = 2 0 0 4 -> Set bracketed paste mode.
818 * Modes:
819 * http: *vt100.net/docs/vt220-rm/chapter4.html
820 */
821 public setMode(params: number[]): void {
822 if (params.length > 1) {
823 for (let i = 0; i < params.length; i++) {
824 this._terminal.setMode(params[i]);
825 }
826
827 return;
828 }
829
830 if (!this._terminal.prefix) {
831 switch (params[0]) {
832 case 4:
833 this._terminal.insertMode = true;
834 break;
835 case 20:
836 // this._terminal.convertEol = true;
837 break;
838 }
839 } else if (this._terminal.prefix === '?') {
840 switch (params[0]) {
841 case 1:
842 this._terminal.applicationCursor = true;
843 break;
844 case 2:
845 this._terminal.setgCharset(0, CHARSETS.US);
846 this._terminal.setgCharset(1, CHARSETS.US);
847 this._terminal.setgCharset(2, CHARSETS.US);
848 this._terminal.setgCharset(3, CHARSETS.US);
849 // set VT100 mode here
850 break;
851 case 3: // 132 col mode
852 this._terminal.savedCols = this._terminal.cols;
853 this._terminal.resize(132, this._terminal.rows);
854 break;
855 case 6:
856 this._terminal.originMode = true;
857 break;
858 case 7:
859 this._terminal.wraparoundMode = true;
860 break;
861 case 12:
862 // this.cursorBlink = true;
863 break;
864 case 66:
865 this._terminal.log('Serial port requested application keypad.');
866 this._terminal.applicationKeypad = true;
867 this._terminal.viewport.syncScrollArea();
868 break;
869 case 9: // X10 Mouse
870 // no release, no motion, no wheel, no modifiers.
871 case 1000: // vt200 mouse
872 // no motion.
873 // no modifiers, except control on the wheel.
874 case 1002: // button event mouse
875 case 1003: // any event mouse
876 // any event - sends motion events,
877 // even if there is no button held down.
878
879 // TODO: Why are params[0] compares nested within a switch for params[0]?
880
881 this._terminal.x10Mouse = params[0] === 9;
882 this._terminal.vt200Mouse = params[0] === 1000;
883 this._terminal.normalMouse = params[0] > 1000;
884 this._terminal.mouseEvents = true;
885 this._terminal.element.style.cursor = 'default';
886 this._terminal.log('Binding to mouse events.');
887 break;
888 case 1004: // send focusin/focusout events
889 // focusin: ^[[I
890 // focusout: ^[[O
891 this._terminal.sendFocus = true;
892 break;
893 case 1005: // utf8 ext mode mouse
894 this._terminal.utfMouse = true;
895 // for wide terminals
896 // simply encodes large values as utf8 characters
897 break;
898 case 1006: // sgr ext mode mouse
899 this._terminal.sgrMouse = true;
900 // for wide terminals
901 // does not add 32 to fields
902 // press: ^[[<b;x;yM
903 // release: ^[[<b;x;ym
904 break;
905 case 1015: // urxvt ext mode mouse
906 this._terminal.urxvtMouse = true;
907 // for wide terminals
908 // numbers for fields
909 // press: ^[[b;x;yM
910 // motion: ^[[b;x;yT
911 break;
912 case 25: // show cursor
913 this._terminal.cursorHidden = false;
914 break;
915 case 1049: // alt screen buffer cursor
916 // this._terminal.saveCursor();
917 ; // FALL-THROUGH
918 case 47: // alt screen buffer
919 case 1047: // alt screen buffer
920 if (!this._terminal.normal) {
921 let normal = {
922 lines: this._terminal.lines,
923 ybase: this._terminal.ybase,
924 ydisp: this._terminal.ydisp,
925 x: this._terminal.x,
926 y: this._terminal.y,
927 scrollTop: this._terminal.scrollTop,
928 scrollBottom: this._terminal.scrollBottom,
929 tabs: this._terminal.tabs
930 // XXX save charset(s) here?
931 // charset: this._terminal.charset,
932 // glevel: this._terminal.glevel,
933 // charsets: this._terminal.charsets
934 };
935 this._terminal.reset();
936 this._terminal.viewport.syncScrollArea();
937 this._terminal.normal = normal;
938 this._terminal.showCursor();
939 }
940 break;
941 }
942 }
943 }
944
9b662080
DI
945 /**
946 * CSI Pm l Reset Mode (RM).
947 * Ps = 2 -> Keyboard Action Mode (AM).
948 * Ps = 4 -> Replace Mode (IRM).
949 * Ps = 1 2 -> Send/receive (SRM).
950 * Ps = 2 0 -> Normal Linefeed (LNM).
951 * CSI ? Pm l
952 * DEC Private Mode Reset (DECRST).
953 * Ps = 1 -> Normal Cursor Keys (DECCKM).
954 * Ps = 2 -> Designate VT52 mode (DECANM).
955 * Ps = 3 -> 80 Column Mode (DECCOLM).
956 * Ps = 4 -> Jump (Fast) Scroll (DECSCLM).
957 * Ps = 5 -> Normal Video (DECSCNM).
958 * Ps = 6 -> Normal Cursor Mode (DECOM).
959 * Ps = 7 -> No Wraparound Mode (DECAWM).
960 * Ps = 8 -> No Auto-repeat Keys (DECARM).
961 * Ps = 9 -> Don't send Mouse X & Y on button press.
962 * Ps = 1 0 -> Hide toolbar (rxvt).
963 * Ps = 1 2 -> Stop Blinking Cursor (att610).
964 * Ps = 1 8 -> Don't print form feed (DECPFF).
965 * Ps = 1 9 -> Limit print to scrolling region (DECPEX).
966 * Ps = 2 5 -> Hide Cursor (DECTCEM).
967 * Ps = 3 0 -> Don't show scrollbar (rxvt).
968 * Ps = 3 5 -> Disable font-shifting functions (rxvt).
969 * Ps = 4 0 -> Disallow 80 -> 132 Mode.
970 * Ps = 4 1 -> No more(1) fix (see curses resource).
971 * Ps = 4 2 -> Disable Nation Replacement Character sets (DEC-
972 * NRCM).
973 * Ps = 4 4 -> Turn Off Margin Bell.
974 * Ps = 4 5 -> No Reverse-wraparound Mode.
975 * Ps = 4 6 -> Stop Logging. (This is normally disabled by a
976 * compile-time option).
977 * Ps = 4 7 -> Use Normal Screen Buffer.
978 * Ps = 6 6 -> Numeric keypad (DECNKM).
979 * Ps = 6 7 -> Backarrow key sends delete (DECBKM).
980 * Ps = 1 0 0 0 -> Don't send Mouse X & Y on button press and
981 * release. See the section Mouse Tracking.
982 * Ps = 1 0 0 1 -> Don't use Hilite Mouse Tracking.
983 * Ps = 1 0 0 2 -> Don't use Cell Motion Mouse Tracking.
984 * Ps = 1 0 0 3 -> Don't use All Motion Mouse Tracking.
985 * Ps = 1 0 0 4 -> Don't send FocusIn/FocusOut events.
986 * Ps = 1 0 0 5 -> Disable Extended Mouse Mode.
987 * Ps = 1 0 1 0 -> Don't scroll to bottom on tty output
988 * (rxvt).
989 * Ps = 1 0 1 1 -> Don't scroll to bottom on key press (rxvt).
990 * Ps = 1 0 3 4 -> Don't interpret "meta" key. (This disables
991 * the eightBitInput resource).
992 * Ps = 1 0 3 5 -> Disable special modifiers for Alt and Num-
993 * Lock keys. (This disables the numLock resource).
994 * Ps = 1 0 3 6 -> Don't send ESC when Meta modifies a key.
995 * (This disables the metaSendsEscape resource).
996 * Ps = 1 0 3 7 -> Send VT220 Remove from the editing-keypad
997 * Delete key.
998 * Ps = 1 0 3 9 -> Don't send ESC when Alt modifies a key.
999 * (This disables the altSendsEscape resource).
1000 * Ps = 1 0 4 0 -> Do not keep selection when not highlighted.
1001 * (This disables the keepSelection resource).
1002 * Ps = 1 0 4 1 -> Use the PRIMARY selection. (This disables
1003 * the selectToClipboard resource).
1004 * Ps = 1 0 4 2 -> Disable Urgency window manager hint when
1005 * Control-G is received. (This disables the bellIsUrgent
1006 * resource).
1007 * Ps = 1 0 4 3 -> Disable raising of the window when Control-
1008 * G is received. (This disables the popOnBell resource).
1009 * Ps = 1 0 4 7 -> Use Normal Screen Buffer, clearing screen
1010 * first if in the Alternate Screen. (This may be disabled by
1011 * the titeInhibit resource).
1012 * Ps = 1 0 4 8 -> Restore cursor as in DECRC. (This may be
1013 * disabled by the titeInhibit resource).
1014 * Ps = 1 0 4 9 -> Use Normal Screen Buffer and restore cursor
1015 * as in DECRC. (This may be disabled by the titeInhibit
1016 * resource). This combines the effects of the 1 0 4 7 and 1 0
1017 * 4 8 modes. Use this with terminfo-based applications rather
1018 * than the 4 7 mode.
1019 * Ps = 1 0 5 0 -> Reset terminfo/termcap function-key mode.
1020 * Ps = 1 0 5 1 -> Reset Sun function-key mode.
1021 * Ps = 1 0 5 2 -> Reset HP function-key mode.
1022 * Ps = 1 0 5 3 -> Reset SCO function-key mode.
1023 * Ps = 1 0 6 0 -> Reset legacy keyboard emulation (X11R6).
1024 * Ps = 1 0 6 1 -> Reset keyboard emulation to Sun/PC style.
1025 * Ps = 2 0 0 4 -> Reset bracketed paste mode.
1026 */
1027 public resetMode(params: number[]): void {
1028 if (params.length > 1) {
1029 for (let i = 0; i < params.length; i++) {
1030 this._terminal.resetMode(params[i]);
1031 }
1032
1033 return;
1034 }
1035
1036 if (!this._terminal.prefix) {
1037 switch (params[0]) {
1038 case 4:
1039 this._terminal.insertMode = false;
1040 break;
1041 case 20:
1042 // this._terminal.convertEol = false;
1043 break;
1044 }
1045 } else if (this._terminal.prefix === '?') {
1046 switch (params[0]) {
1047 case 1:
1048 this._terminal.applicationCursor = false;
1049 break;
1050 case 3:
1051 if (this._terminal.cols === 132 && this._terminal.savedCols) {
1052 this._terminal.resize(this._terminal.savedCols, this._terminal.rows);
1053 }
1054 delete this._terminal.savedCols;
1055 break;
1056 case 6:
1057 this._terminal.originMode = false;
1058 break;
1059 case 7:
1060 this._terminal.wraparoundMode = false;
1061 break;
1062 case 12:
1063 // this.cursorBlink = false;
1064 break;
1065 case 66:
1066 this._terminal.log('Switching back to normal keypad.');
1067 this._terminal.applicationKeypad = false;
1068 this._terminal.viewport.syncScrollArea();
1069 break;
1070 case 9: // X10 Mouse
1071 case 1000: // vt200 mouse
1072 case 1002: // button event mouse
1073 case 1003: // any event mouse
1074 this._terminal.x10Mouse = false;
1075 this._terminal.vt200Mouse = false;
1076 this._terminal.normalMouse = false;
1077 this._terminal.mouseEvents = false;
1078 this._terminal.element.style.cursor = '';
1079 break;
1080 case 1004: // send focusin/focusout events
1081 this._terminal.sendFocus = false;
1082 break;
1083 case 1005: // utf8 ext mode mouse
1084 this._terminal.utfMouse = false;
1085 break;
1086 case 1006: // sgr ext mode mouse
1087 this._terminal.sgrMouse = false;
1088 break;
1089 case 1015: // urxvt ext mode mouse
1090 this._terminal.urxvtMouse = false;
1091 break;
1092 case 25: // hide cursor
1093 this._terminal.cursorHidden = true;
1094 break;
1095 case 1049: // alt screen buffer cursor
1096 ; // FALL-THROUGH
1097 case 47: // normal screen buffer
1098 case 1047: // normal screen buffer - clearing it first
1099 if (this._terminal.normal) {
1100 this._terminal.lines = this._terminal.normal.lines;
1101 this._terminal.ybase = this._terminal.normal.ybase;
1102 this._terminal.ydisp = this._terminal.normal.ydisp;
1103 this._terminal.x = this._terminal.normal.x;
1104 this._terminal.y = this._terminal.normal.y;
1105 this._terminal.scrollTop = this._terminal.normal.scrollTop;
1106 this._terminal.scrollBottom = this._terminal.normal.scrollBottom;
1107 this._terminal.tabs = this._terminal.normal.tabs;
1108 this._terminal.normal = null;
1109 // if (params === 1049) {
1110 // this.x = this.savedX;
1111 // this.y = this.savedY;
1112 // }
1113 this._terminal.queueRefresh(0, this._terminal.rows - 1);
1114 this._terminal.viewport.syncScrollArea();
1115 this._terminal.showCursor();
1116 }
1117 break;
1118 }
1119 }
1120 }
1121
db81c28b
DI
1122 /**
1123 * CSI Pm m Character Attributes (SGR).
1124 * Ps = 0 -> Normal (default).
1125 * Ps = 1 -> Bold.
1126 * Ps = 4 -> Underlined.
1127 * Ps = 5 -> Blink (appears as Bold).
1128 * Ps = 7 -> Inverse.
1129 * Ps = 8 -> Invisible, i.e., hidden (VT300).
1130 * Ps = 2 2 -> Normal (neither bold nor faint).
1131 * Ps = 2 4 -> Not underlined.
1132 * Ps = 2 5 -> Steady (not blinking).
1133 * Ps = 2 7 -> Positive (not inverse).
1134 * Ps = 2 8 -> Visible, i.e., not hidden (VT300).
1135 * Ps = 3 0 -> Set foreground color to Black.
1136 * Ps = 3 1 -> Set foreground color to Red.
1137 * Ps = 3 2 -> Set foreground color to Green.
1138 * Ps = 3 3 -> Set foreground color to Yellow.
1139 * Ps = 3 4 -> Set foreground color to Blue.
1140 * Ps = 3 5 -> Set foreground color to Magenta.
1141 * Ps = 3 6 -> Set foreground color to Cyan.
1142 * Ps = 3 7 -> Set foreground color to White.
1143 * Ps = 3 9 -> Set foreground color to default (original).
1144 * Ps = 4 0 -> Set background color to Black.
1145 * Ps = 4 1 -> Set background color to Red.
1146 * Ps = 4 2 -> Set background color to Green.
1147 * Ps = 4 3 -> Set background color to Yellow.
1148 * Ps = 4 4 -> Set background color to Blue.
1149 * Ps = 4 5 -> Set background color to Magenta.
1150 * Ps = 4 6 -> Set background color to Cyan.
1151 * Ps = 4 7 -> Set background color to White.
1152 * Ps = 4 9 -> Set background color to default (original).
1153 *
1154 * If 16-color support is compiled, the following apply. Assume
1155 * that xterm's resources are set so that the ISO color codes are
1156 * the first 8 of a set of 16. Then the aixterm colors are the
1157 * bright versions of the ISO colors:
1158 * Ps = 9 0 -> Set foreground color to Black.
1159 * Ps = 9 1 -> Set foreground color to Red.
1160 * Ps = 9 2 -> Set foreground color to Green.
1161 * Ps = 9 3 -> Set foreground color to Yellow.
1162 * Ps = 9 4 -> Set foreground color to Blue.
1163 * Ps = 9 5 -> Set foreground color to Magenta.
1164 * Ps = 9 6 -> Set foreground color to Cyan.
1165 * Ps = 9 7 -> Set foreground color to White.
1166 * Ps = 1 0 0 -> Set background color to Black.
1167 * Ps = 1 0 1 -> Set background color to Red.
1168 * Ps = 1 0 2 -> Set background color to Green.
1169 * Ps = 1 0 3 -> Set background color to Yellow.
1170 * Ps = 1 0 4 -> Set background color to Blue.
1171 * Ps = 1 0 5 -> Set background color to Magenta.
1172 * Ps = 1 0 6 -> Set background color to Cyan.
1173 * Ps = 1 0 7 -> Set background color to White.
1174 *
1175 * If xterm is compiled with the 16-color support disabled, it
1176 * supports the following, from rxvt:
1177 * Ps = 1 0 0 -> Set foreground and background color to
1178 * default.
1179 *
1180 * If 88- or 256-color support is compiled, the following apply.
1181 * Ps = 3 8 ; 5 ; Ps -> Set foreground color to the second
1182 * Ps.
1183 * Ps = 4 8 ; 5 ; Ps -> Set background color to the second
1184 * Ps.
1185 */
411b80cd 1186 public charAttributes(params: number[]): void {
db81c28b
DI
1187 // Optimize a single SGR0.
1188 if (params.length === 1 && params[0] === 0) {
1189 this._terminal.curAttr = this._terminal.defAttr;
1190 return;
1191 }
1192
1193 let l = params.length
1194 , i = 0
1195 , flags = this._terminal.curAttr >> 18
1196 , fg = (this._terminal.curAttr >> 9) & 0x1ff
1197 , bg = this._terminal.curAttr & 0x1ff
1198 , p;
1199
1200 for (; i < l; i++) {
1201 p = params[i];
1202 if (p >= 30 && p <= 37) {
1203 // fg color 8
1204 fg = p - 30;
1205 } else if (p >= 40 && p <= 47) {
1206 // bg color 8
1207 bg = p - 40;
1208 } else if (p >= 90 && p <= 97) {
1209 // fg color 16
1210 p += 8;
1211 fg = p - 90;
1212 } else if (p >= 100 && p <= 107) {
1213 // bg color 16
1214 p += 8;
1215 bg = p - 100;
1216 } else if (p === 0) {
1217 // default
1218 flags = this._terminal.defAttr >> 18;
1219 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1220 bg = this._terminal.defAttr & 0x1ff;
1221 // flags = 0;
1222 // fg = 0x1ff;
1223 // bg = 0x1ff;
1224 } else if (p === 1) {
1225 // bold text
1226 flags |= 1;
1227 } else if (p === 4) {
1228 // underlined text
1229 flags |= 2;
1230 } else if (p === 5) {
1231 // blink
1232 flags |= 4;
1233 } else if (p === 7) {
1234 // inverse and positive
1235 // test with: echo -e '\e[31m\e[42mhello\e[7mworld\e[27mhi\e[m'
1236 flags |= 8;
1237 } else if (p === 8) {
1238 // invisible
1239 flags |= 16;
1240 } else if (p === 22) {
1241 // not bold
1242 flags &= ~1;
1243 } else if (p === 24) {
1244 // not underlined
1245 flags &= ~2;
1246 } else if (p === 25) {
1247 // not blink
1248 flags &= ~4;
1249 } else if (p === 27) {
1250 // not inverse
1251 flags &= ~8;
1252 } else if (p === 28) {
1253 // not invisible
1254 flags &= ~16;
1255 } else if (p === 39) {
1256 // reset fg
1257 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1258 } else if (p === 49) {
1259 // reset bg
1260 bg = this._terminal.defAttr & 0x1ff;
1261 } else if (p === 38) {
1262 // fg color 256
1263 if (params[i + 1] === 2) {
1264 i += 2;
1265 fg = this._terminal.matchColor(
1266 params[i] & 0xff,
1267 params[i + 1] & 0xff,
1268 params[i + 2] & 0xff);
1269 if (fg === -1) fg = 0x1ff;
1270 i += 2;
1271 } else if (params[i + 1] === 5) {
1272 i += 2;
1273 p = params[i] & 0xff;
1274 fg = p;
1275 }
1276 } else if (p === 48) {
1277 // bg color 256
1278 if (params[i + 1] === 2) {
1279 i += 2;
1280 bg = this._terminal.matchColor(
1281 params[i] & 0xff,
1282 params[i + 1] & 0xff,
1283 params[i + 2] & 0xff);
1284 if (bg === -1) bg = 0x1ff;
1285 i += 2;
1286 } else if (params[i + 1] === 5) {
1287 i += 2;
1288 p = params[i] & 0xff;
1289 bg = p;
1290 }
1291 } else if (p === 100) {
1292 // reset fg/bg
1293 fg = (this._terminal.defAttr >> 9) & 0x1ff;
1294 bg = this._terminal.defAttr & 0x1ff;
1295 } else {
1296 this._terminal.error('Unknown SGR attribute: %d.', p);
1297 }
1298 }
1299
1300 this._terminal.curAttr = (flags << 18) | (fg << 9) | bg;
1301 }
1302
1303 /**
1304 * CSI Ps n Device Status Report (DSR).
1305 * Ps = 5 -> Status Report. Result (``OK'') is
1306 * CSI 0 n
1307 * Ps = 6 -> Report Cursor Position (CPR) [row;column].
1308 * Result is
1309 * CSI r ; c R
1310 * CSI ? Ps n
1311 * Device Status Report (DSR, DEC-specific).
1312 * Ps = 6 -> Report Cursor Position (CPR) [row;column] as CSI
1313 * ? r ; c R (assumes page is zero).
1314 * Ps = 1 5 -> Report Printer status as CSI ? 1 0 n (ready).
1315 * or CSI ? 1 1 n (not ready).
1316 * Ps = 2 5 -> Report UDK status as CSI ? 2 0 n (unlocked)
1317 * or CSI ? 2 1 n (locked).
1318 * Ps = 2 6 -> Report Keyboard status as
1319 * CSI ? 2 7 ; 1 ; 0 ; 0 n (North American).
1320 * The last two parameters apply to VT400 & up, and denote key-
1321 * board ready and LK01 respectively.
1322 * Ps = 5 3 -> Report Locator status as
1323 * CSI ? 5 3 n Locator available, if compiled-in, or
1324 * CSI ? 5 0 n No Locator, if not.
1325 */
411b80cd 1326 public deviceStatus(params: number[]): void {
db81c28b
DI
1327 if (!this._terminal.prefix) {
1328 switch (params[0]) {
1329 case 5:
1330 // status report
1331 this._terminal.send(C0.ESC + '[0n');
1332 break;
1333 case 6:
1334 // cursor position
1335 this._terminal.send(C0.ESC + '['
1336 + (this._terminal.y + 1)
1337 + ';'
1338 + (this._terminal.x + 1)
1339 + 'R');
1340 break;
1341 }
1342 } else if (this._terminal.prefix === '?') {
1343 // modern xterm doesnt seem to
1344 // respond to any of these except ?6, 6, and 5
1345 switch (params[0]) {
1346 case 6:
1347 // cursor position
1348 this._terminal.send(C0.ESC + '[?'
1349 + (this._terminal.y + 1)
1350 + ';'
1351 + (this._terminal.x + 1)
1352 + 'R');
1353 break;
1354 case 15:
1355 // no printer
1356 // this.send(C0.ESC + '[?11n');
1357 break;
1358 case 25:
1359 // dont support user defined keys
1360 // this.send(C0.ESC + '[?21n');
1361 break;
1362 case 26:
1363 // north american keyboard
1364 // this.send(C0.ESC + '[?27;1;0;0n');
1365 break;
1366 case 53:
1367 // no dec locator/mouse
1368 // this.send(C0.ESC + '[?50n');
1369 break;
1370 }
1371 }
1372 }
1373
f4846aa1
DI
1374 /**
1375 * CSI ! p Soft terminal reset (DECSTR).
1376 * http://vt100.net/docs/vt220-rm/table4-10.html
1377 */
1378 public softReset(params: number[]): void {
1379 this._terminal.cursorHidden = false;
1380 this._terminal.insertMode = false;
1381 this._terminal.originMode = false;
1382 this._terminal.wraparoundMode = false; // autowrap
1383 this._terminal.applicationKeypad = false; // ?
1384 this._terminal.viewport.syncScrollArea();
1385 this._terminal.applicationCursor = false;
1386 this._terminal.scrollTop = 0;
1387 this._terminal.scrollBottom = this._terminal.rows - 1;
1388 this._terminal.curAttr = this._terminal.defAttr;
1389 this._terminal.x = this._terminal.y = 0; // ?
1390 this._terminal.charset = null;
1391 this._terminal.glevel = 0; // ??
1392 this._terminal.charsets = [null]; // ??
1393 }
1394
0bd469f5
DI
1395 /**
1396 * CSI Ps SP q Set cursor style (DECSCUSR, VT520).
1397 * Ps = 0 -> blinking block.
1398 * Ps = 1 -> blinking block (default).
1399 * Ps = 2 -> steady block.
1400 * Ps = 3 -> blinking underline.
1401 * Ps = 4 -> steady underline.
1402 * Ps = 5 -> blinking bar (xterm).
1403 * Ps = 6 -> steady bar (xterm).
1404 */
1405 public setCursorStyle(params?: number[]): void {
1406 const param = params[0] < 1 ? 1 : params[0];
1407 switch (param) {
1408 case 1:
1409 case 2:
1410 this._terminal.setOption('cursorStyle', 'block');
1411 break;
1412 case 3:
1413 case 4:
1414 this._terminal.setOption('cursorStyle', 'underline');
1415 break;
1416 case 5:
1417 case 6:
1418 this._terminal.setOption('cursorStyle', 'bar');
1419 break;
1420 }
1421 const isBlinking = param % 2 === 1;
1422 this._terminal.setOption('cursorBlink', isBlinking);
1423 }
1424
9b662080
DI
1425 /**
1426 * CSI Ps ; Ps r
1427 * Set Scrolling Region [top;bottom] (default = full size of win-
1428 * dow) (DECSTBM).
1429 * CSI ? Pm r
1430 */
1431 public setScrollRegion(params: number[]): void {
1432 if (this._terminal.prefix) return;
1433 this._terminal.scrollTop = (params[0] || 1) - 1;
1434 this._terminal.scrollBottom = (params[1] || this._terminal.rows) - 1;
1435 this._terminal.x = 0;
1436 this._terminal.y = 0;
1437 }
1438
1439
1440 /**
1441 * CSI s
1442 * Save cursor (ANSI.SYS).
1443 */
1444 public saveCursor(params: number[]): void {
1445 this._terminal.savedX = this._terminal.x;
1446 this._terminal.savedY = this._terminal.y;
1447 }
1448
1449
1450 /**
1451 * CSI u
1452 * Restore cursor (ANSI.SYS).
1453 */
1454 public restoreCursor(params: number[]): void {
1455 this._terminal.x = this._terminal.savedX || 0;
1456 this._terminal.y = this._terminal.savedY || 0;
1457 }
04b1ebf1 1458}
fa3484cd
DI
1459
1460const wcwidth = (function(opts) {
1461 // extracted from https://www.cl.cam.ac.uk/%7Emgk25/ucs/wcwidth.c
1462 // combining characters
1463 const COMBINING = [
1464 [0x0300, 0x036F], [0x0483, 0x0486], [0x0488, 0x0489],
1465 [0x0591, 0x05BD], [0x05BF, 0x05BF], [0x05C1, 0x05C2],
1466 [0x05C4, 0x05C5], [0x05C7, 0x05C7], [0x0600, 0x0603],
1467 [0x0610, 0x0615], [0x064B, 0x065E], [0x0670, 0x0670],
1468 [0x06D6, 0x06E4], [0x06E7, 0x06E8], [0x06EA, 0x06ED],
1469 [0x070F, 0x070F], [0x0711, 0x0711], [0x0730, 0x074A],
1470 [0x07A6, 0x07B0], [0x07EB, 0x07F3], [0x0901, 0x0902],
1471 [0x093C, 0x093C], [0x0941, 0x0948], [0x094D, 0x094D],
1472 [0x0951, 0x0954], [0x0962, 0x0963], [0x0981, 0x0981],
1473 [0x09BC, 0x09BC], [0x09C1, 0x09C4], [0x09CD, 0x09CD],
1474 [0x09E2, 0x09E3], [0x0A01, 0x0A02], [0x0A3C, 0x0A3C],
1475 [0x0A41, 0x0A42], [0x0A47, 0x0A48], [0x0A4B, 0x0A4D],
1476 [0x0A70, 0x0A71], [0x0A81, 0x0A82], [0x0ABC, 0x0ABC],
1477 [0x0AC1, 0x0AC5], [0x0AC7, 0x0AC8], [0x0ACD, 0x0ACD],
1478 [0x0AE2, 0x0AE3], [0x0B01, 0x0B01], [0x0B3C, 0x0B3C],
1479 [0x0B3F, 0x0B3F], [0x0B41, 0x0B43], [0x0B4D, 0x0B4D],
1480 [0x0B56, 0x0B56], [0x0B82, 0x0B82], [0x0BC0, 0x0BC0],
1481 [0x0BCD, 0x0BCD], [0x0C3E, 0x0C40], [0x0C46, 0x0C48],
1482 [0x0C4A, 0x0C4D], [0x0C55, 0x0C56], [0x0CBC, 0x0CBC],
1483 [0x0CBF, 0x0CBF], [0x0CC6, 0x0CC6], [0x0CCC, 0x0CCD],
1484 [0x0CE2, 0x0CE3], [0x0D41, 0x0D43], [0x0D4D, 0x0D4D],
1485 [0x0DCA, 0x0DCA], [0x0DD2, 0x0DD4], [0x0DD6, 0x0DD6],
1486 [0x0E31, 0x0E31], [0x0E34, 0x0E3A], [0x0E47, 0x0E4E],
1487 [0x0EB1, 0x0EB1], [0x0EB4, 0x0EB9], [0x0EBB, 0x0EBC],
1488 [0x0EC8, 0x0ECD], [0x0F18, 0x0F19], [0x0F35, 0x0F35],
1489 [0x0F37, 0x0F37], [0x0F39, 0x0F39], [0x0F71, 0x0F7E],
1490 [0x0F80, 0x0F84], [0x0F86, 0x0F87], [0x0F90, 0x0F97],
1491 [0x0F99, 0x0FBC], [0x0FC6, 0x0FC6], [0x102D, 0x1030],
1492 [0x1032, 0x1032], [0x1036, 0x1037], [0x1039, 0x1039],
1493 [0x1058, 0x1059], [0x1160, 0x11FF], [0x135F, 0x135F],
1494 [0x1712, 0x1714], [0x1732, 0x1734], [0x1752, 0x1753],
1495 [0x1772, 0x1773], [0x17B4, 0x17B5], [0x17B7, 0x17BD],
1496 [0x17C6, 0x17C6], [0x17C9, 0x17D3], [0x17DD, 0x17DD],
1497 [0x180B, 0x180D], [0x18A9, 0x18A9], [0x1920, 0x1922],
1498 [0x1927, 0x1928], [0x1932, 0x1932], [0x1939, 0x193B],
1499 [0x1A17, 0x1A18], [0x1B00, 0x1B03], [0x1B34, 0x1B34],
1500 [0x1B36, 0x1B3A], [0x1B3C, 0x1B3C], [0x1B42, 0x1B42],
1501 [0x1B6B, 0x1B73], [0x1DC0, 0x1DCA], [0x1DFE, 0x1DFF],
1502 [0x200B, 0x200F], [0x202A, 0x202E], [0x2060, 0x2063],
1503 [0x206A, 0x206F], [0x20D0, 0x20EF], [0x302A, 0x302F],
1504 [0x3099, 0x309A], [0xA806, 0xA806], [0xA80B, 0xA80B],
1505 [0xA825, 0xA826], [0xFB1E, 0xFB1E], [0xFE00, 0xFE0F],
1506 [0xFE20, 0xFE23], [0xFEFF, 0xFEFF], [0xFFF9, 0xFFFB],
1507 [0x10A01, 0x10A03], [0x10A05, 0x10A06], [0x10A0C, 0x10A0F],
1508 [0x10A38, 0x10A3A], [0x10A3F, 0x10A3F], [0x1D167, 0x1D169],
1509 [0x1D173, 0x1D182], [0x1D185, 0x1D18B], [0x1D1AA, 0x1D1AD],
1510 [0x1D242, 0x1D244], [0xE0001, 0xE0001], [0xE0020, 0xE007F],
1511 [0xE0100, 0xE01EF]
1512 ];
1513 // binary search
1514 function bisearch(ucs) {
1515 let min = 0;
1516 let max = COMBINING.length - 1;
1517 let mid;
1518 if (ucs < COMBINING[0][0] || ucs > COMBINING[max][1])
1519 return false;
1520 while (max >= min) {
1521 mid = Math.floor((min + max) / 2);
1522 if (ucs > COMBINING[mid][1])
1523 min = mid + 1;
1524 else if (ucs < COMBINING[mid][0])
1525 max = mid - 1;
1526 else
1527 return true;
1528 }
1529 return false;
1530 }
1531 function wcwidth(ucs) {
1532 // test for 8-bit control characters
1533 if (ucs === 0)
1534 return opts.nul;
1535 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1536 return opts.control;
1537 // binary search in table of non-spacing characters
1538 if (bisearch(ucs))
1539 return 0;
1540 // if we arrive here, ucs is not a combining or C0/C1 control character
1541 if (isWide(ucs)) {
1542 return 2;
1543 }
1544 return 1;
1545 }
1546 function isWide(ucs) {
1547 return (
1548 ucs >= 0x1100 && (
1549 ucs <= 0x115f || // Hangul Jamo init. consonants
1550 ucs === 0x2329 ||
1551 ucs === 0x232a ||
1552 (ucs >= 0x2e80 && ucs <= 0xa4cf && ucs !== 0x303f) || // CJK..Yi
1553 (ucs >= 0xac00 && ucs <= 0xd7a3) || // Hangul Syllables
1554 (ucs >= 0xf900 && ucs <= 0xfaff) || // CJK Compat Ideographs
1555 (ucs >= 0xfe10 && ucs <= 0xfe19) || // Vertical forms
1556 (ucs >= 0xfe30 && ucs <= 0xfe6f) || // CJK Compat Forms
1557 (ucs >= 0xff00 && ucs <= 0xff60) || // Fullwidth Forms
1558 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1559 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1560 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1561 }
1562 return wcwidth;
1563})({nul: 0, control: 0}); // configurable options