| 1 |
package terminal |
| 2 |
|
| 3 |
func isControlCode(c rune) bool { |
| 4 |
return c < 0x20 || c == 0177 |
| 5 |
} |
| 6 |
|
| 7 |
func (t *State) parse(c rune) { |
| 8 |
if isControlCode(c) { |
| 9 |
if t.handleControlCodes(c) || t.cur.attr.mode&attrGfx == 0 { |
| 10 |
return |
| 11 |
} |
| 12 |
} |
| 13 |
// TODO: update selection; see st.c:2450 |
| 14 |
|
| 15 |
if t.mode&ModeWrap != 0 && t.cur.state&cursorWrapNext != 0 { |
| 16 |
t.lines[t.cur.y][t.cur.x].mode |= attrWrap |
| 17 |
t.newline(true) |
| 18 |
} |
| 19 |
|
| 20 |
if t.mode&ModeInsert != 0 && t.cur.x+1 < t.cols { |
| 21 |
// TODO: move shiz, look at st.c:2458 |
| 22 |
t.logln("insert mode not implemented") |
| 23 |
} |
| 24 |
|
| 25 |
t.setChar(c, &t.cur.attr, t.cur.x, t.cur.y) |
| 26 |
if t.cur.x+1 < t.cols { |
| 27 |
t.moveTo(t.cur.x+1, t.cur.y) |
| 28 |
} else { |
| 29 |
t.cur.state |= cursorWrapNext |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
func (t *State) parseEsc(c rune) { |
| 34 |
if t.handleControlCodes(c) { |
| 35 |
return |
| 36 |
} |
| 37 |
next := t.parse |
| 38 |
switch c { |
| 39 |
case '[': |
| 40 |
next = t.parseEscCSI |
| 41 |
case '#': |
| 42 |
next = t.parseEscTest |
| 43 |
case 'P', // DCS - Device Control String |
| 44 |
'_', // APC - Application Program Command |
| 45 |
'^', // PM - Privacy Message |
| 46 |
']', // OSC - Operating System Command |
| 47 |
'k': // old title set compatibility |
| 48 |
t.str.reset() |
| 49 |
t.str.typ = c |
| 50 |
next = t.parseEscStr |
| 51 |
case '(': // set primary charset G0 |
| 52 |
next = t.parseEscAltCharset |
| 53 |
case ')', // set secondary charset G1 (ignored) |
| 54 |
'*', // set tertiary charset G2 (ignored) |
| 55 |
'+': // set quaternary charset G3 (ignored) |
| 56 |
case 'D': // IND - linefeed |
| 57 |
if t.cur.y == t.bottom { |
| 58 |
t.ScrollUp(t.top, 1) |
| 59 |
} else { |
| 60 |
t.moveTo(t.cur.x, t.cur.y+1) |
| 61 |
} |
| 62 |
case 'E': // NEL - next line |
| 63 |
t.newline(true) |
| 64 |
case 'H': // HTS - horizontal tab stop |
| 65 |
t.tabs[t.cur.x] = true |
| 66 |
case 'M': // RI - reverse index |
| 67 |
if t.cur.y == t.top { |
| 68 |
t.ScrollDown(t.top, 1) |
| 69 |
} else { |
| 70 |
t.moveTo(t.cur.x, t.cur.y-1) |
| 71 |
} |
| 72 |
case 'Z': // DECID - identify terminal |
| 73 |
// TODO: write to our writer our id |
| 74 |
case 'c': // RIS - reset to initial state |
| 75 |
t.reset() |
| 76 |
case '=': // DECPAM - application keypad |
| 77 |
t.mode |= ModeAppKeypad |
| 78 |
case '>': // DECPNM - normal keypad |
| 79 |
t.mode &^= ModeAppKeypad |
| 80 |
case '7': // DECSC - save cursor |
| 81 |
t.saveCursor() |
| 82 |
case '8': // DECRC - restore cursor |
| 83 |
t.restoreCursor() |
| 84 |
case '\\': // ST - stop |
| 85 |
default: |
| 86 |
t.logf("unknown ESC sequence '%c'\n", c) |
| 87 |
} |
| 88 |
t.state = next |
| 89 |
} |
| 90 |
|
| 91 |
func (t *State) parseEscCSI(c rune) { |
| 92 |
if t.handleControlCodes(c) { |
| 93 |
return |
| 94 |
} |
| 95 |
if t.csi.put(byte(c)) { |
| 96 |
t.state = t.parse |
| 97 |
t.handleCSI() |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
func (t *State) parseEscStr(c rune) { |
| 102 |
switch c { |
| 103 |
case '\033': |
| 104 |
t.state = t.parseEscStrEnd |
| 105 |
case '\a': // backwards compatiblity to xterm |
| 106 |
t.state = t.parse |
| 107 |
t.handleSTR() |
| 108 |
default: |
| 109 |
t.str.put(c) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func (t *State) parseEscStrEnd(c rune) { |
| 114 |
if t.handleControlCodes(c) { |
| 115 |
return |
| 116 |
} |
| 117 |
t.state = t.parse |
| 118 |
if c == '\\' { |
| 119 |
t.handleSTR() |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func (t *State) parseEscAltCharset(c rune) { |
| 124 |
if t.handleControlCodes(c) { |
| 125 |
return |
| 126 |
} |
| 127 |
switch c { |
| 128 |
case '0': // line drawing set |
| 129 |
t.cur.attr.mode |= attrGfx |
| 130 |
case 'B': // USASCII |
| 131 |
t.cur.attr.mode &^= attrGfx |
| 132 |
case 'A', // UK (ignored) |
| 133 |
'<', // multinational (ignored) |
| 134 |
'5', // Finnish (ignored) |
| 135 |
'C', // Finnish (ignored) |
| 136 |
'K': // German (ignored) |
| 137 |
default: |
| 138 |
t.logf("unknown alt. charset '%c'\n", c) |
| 139 |
} |
| 140 |
t.state = t.parse |
| 141 |
} |
| 142 |
|
| 143 |
func (t *State) parseEscTest(c rune) { |
| 144 |
if t.handleControlCodes(c) { |
| 145 |
return |
| 146 |
} |
| 147 |
// DEC screen alignment test |
| 148 |
if c == '8' { |
| 149 |
for y := 0; y < t.rows; y++ { |
| 150 |
for x := 0; x < t.cols; x++ { |
| 151 |
t.setChar('E', &t.cur.attr, x, y) |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
t.state = t.parse |
| 156 |
} |
| 157 |
|
| 158 |
func (t *State) handleControlCodes(c rune) bool { |
| 159 |
if !isControlCode(c) { |
| 160 |
return false |
| 161 |
} |
| 162 |
switch c { |
| 163 |
// HT |
| 164 |
case '\t': |
| 165 |
t.putTab(true) |
| 166 |
// BS |
| 167 |
case '\b': |
| 168 |
t.moveTo(t.cur.x-1, t.cur.y) |
| 169 |
// CR |
| 170 |
case '\r': |
| 171 |
t.moveTo(0, t.cur.y) |
| 172 |
// LF, VT, LF |
| 173 |
case '\f', '\v', '\n': |
| 174 |
// go to first col if mode is set |
| 175 |
t.newline(t.mode&ModeCRLF != 0) |
| 176 |
// BEL |
| 177 |
case '\a': |
| 178 |
// TODO: emit sound |
| 179 |
// TODO: window alert if not focused |
| 180 |
// ESC |
| 181 |
case 033: |
| 182 |
t.csi.reset() |
| 183 |
t.state = t.parseEsc |
| 184 |
// SO, SI |
| 185 |
case 016, 017: |
| 186 |
// different charsets not supported. apps should use the correct |
| 187 |
// alt charset escapes, probably for line drawing |
| 188 |
// SUB, CAN |
| 189 |
case 032, 030: |
| 190 |
t.csi.reset() |
| 191 |
// ignore ENQ, NUL, XON, XOFF, DEL |
| 192 |
case 005, 000, 021, 023, 0177: |
| 193 |
default: |
| 194 |
return false |
| 195 |
} |
| 196 |
return true |
| 197 |
} |