package vt10x

func isControlCode( rune) bool {
	return  < 0x20 ||  == 0177
}

func ( *State) ( rune) {
	.logf("%q", string())
	if isControlCode() {
		if .handleControlCodes() || .cur.Attr.Mode&attrGfx == 0 {
			return
		}
	}
	// TODO: update selection; see st.c:2450

	if .mode&ModeWrap != 0 && .cur.State&cursorWrapNext != 0 {
		.lines[.cur.Y][.cur.X].Mode |= attrWrap
		.newline(true)
	}

	if .mode&ModeInsert != 0 && .cur.X+1 < .cols {
		// TODO: move shiz, look at st.c:2458
		.logln("insert mode not implemented")
	}

	.setChar(, &.cur.Attr, .cur.X, .cur.Y)
	if .cur.X+1 < .cols {
		.moveTo(.cur.X+1, .cur.Y)
	} else {
		.cur.State |= cursorWrapNext
	}
}

func ( *State) ( rune) {
	if .handleControlCodes() {
		return
	}
	 := .parse
	.logf("%q", string())
	switch  {
	case '[':
		 = .parseEscCSI
	case '#':
		 = .parseEscTest
	case 'P', // DCS - Device Control String
		'_', // APC - Application Program Command
		'^', // PM - Privacy Message
		']', // OSC - Operating System Command
		'k': // old title set compatibility
		.str.reset()
		.str.typ = 
		 = .parseEscStr
	case '(': // set primary charset G0
		 = .parseEscAltCharset
	case ')', // set secondary charset G1 (ignored)
		'*', // set tertiary charset G2 (ignored)
		'+': // set quaternary charset G3 (ignored)
	case 'D': // IND - linefeed
		if .cur.Y == .bottom {
			.scrollUp(.top, 1)
		} else {
			.moveTo(.cur.X, .cur.Y+1)
		}
	case 'E': // NEL - next line
		.newline(true)
	case 'H': // HTS - horizontal tab stop
		.tabs[.cur.X] = true
	case 'M': // RI - reverse index
		if .cur.Y == .top {
			.scrollDown(.top, 1)
		} else {
			.moveTo(.cur.X, .cur.Y-1)
		}
	case 'Z': // DECID - identify terminal
		// TODO: write to our writer our id
	case 'c': // RIS - reset to initial state
		.reset()
	case '=': // DECPAM - application keypad
		.mode |= ModeAppKeypad
	case '>': // DECPNM - normal keypad
		.mode &^= ModeAppKeypad
	case '7': // DECSC - save cursor
		.saveCursor()
	case '8': // DECRC - restore cursor
		.restoreCursor()
	case '\\': // ST - stop
	default:
		.logf("unknown ESC sequence '%c'\n", )
	}
	.state = 
}

func ( *State) ( rune) {
	if .handleControlCodes() {
		return
	}
	.logf("%q", string())
	if .csi.put(byte()) {
		.state = .parse
		.handleCSI()
	}
}

func ( *State) ( rune) {
	.logf("%q", string())
	switch  {
	case '\033':
		.state = .parseEscStrEnd
	case '\a': // backwards compatiblity to xterm
		.state = .parse
		.handleSTR()
	default:
		.str.put()
	}
}

func ( *State) ( rune) {
	if .handleControlCodes() {
		return
	}
	.logf("%q", string())
	.state = .parse
	if  == '\\' {
		.handleSTR()
	}
}

func ( *State) ( rune) {
	if .handleControlCodes() {
		return
	}
	.logf("%q", string())
	switch  {
	case '0': // line drawing set
		.cur.Attr.Mode |= attrGfx
	case 'B': // USASCII
		.cur.Attr.Mode &^= attrGfx
	case 'A', // UK (ignored)
		'<', // multinational (ignored)
		'5', // Finnish (ignored)
		'C', // Finnish (ignored)
		'K': // German (ignored)
	default:
		.logf("unknown alt. charset '%c'\n", )
	}
	.state = .parse
}

func ( *State) ( rune) {
	if .handleControlCodes() {
		return
	}
	// DEC screen alignment test
	if  == '8' {
		for  := 0;  < .rows; ++ {
			for  := 0;  < .cols; ++ {
				.setChar('E', &.cur.Attr, , )
			}
		}
	}
	.state = .parse
}

func ( *State) ( rune) bool {
	if !isControlCode() {
		return false
	}
	switch  {
	// HT
	case '\t':
		.putTab(true)
	// BS
	case '\b':
		.moveTo(.cur.X-1, .cur.Y)
	// CR
	case '\r':
		.moveTo(0, .cur.Y)
	// LF, VT, LF
	case '\f', '\v', '\n':
		// go to first col if mode is set
		.newline(.mode&ModeCRLF != 0)
	// BEL
	case '\a':
		// TODO: emit sound
		// TODO: window alert if not focused
	// ESC
	case 033:
		.csi.reset()
		.state = .parseEsc
	// SO, SI
	case 016, 017:
		// different charsets not supported. apps should use the correct
		// alt charset escapes, probably for line drawing
	// SUB, CAN
	case 032, 030:
		.csi.reset()
	// ignore ENQ, NUL, XON, XOFF, DEL
	case 005, 000, 021, 023, 0177:
	default:
		return false
	}
	return true
}