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