+ # clear the lines we scrolled off (and put back in as "new")
+ for y in [@y...@y+lines]
+ for x in [0...@width]
+ @text[y][x] = ' '
+ @attributes[y][x] = 0x07
+
+ # move lines upwards (arg is how far)
+ # this obliterates the line under the cursor and arg-1 following it
+ csi_M: (lines) ->
+ lines = parseInt @fix_esc_arg lines, '1'
+
+ rearrange = (a) =>
+ return [
+ a[0 ... @y]..., # keep everything above cursor
+ a[@y + lines .. @scroll_bottom]..., # lines we're moving up
+ a[@y ... @y + lines]..., # recycle these
+ a[@scroll_bottom + 1 ... @height]... # keep the rest
+ ]
+ @text = rearrange @text
+ @attributes = rearrange @attributes
+
+ # clear the lines we're recycling
+ for y in [@scroll_bottom - lines + 1 .. @scroll_bottom]
+ for x in [0...@width]
+ @text[y][x] = ' '
+ @attributes[y][x] = 0x07
+
+ # delete chars (without moving the cursor)
+ csi_X: (chars) ->
+ chars = parseInt @fix_esc_arg chars, '1'
+ x = @x
+ for c in [0...chars]
+ if x >= @width
+ return
+ @text[@y][x] = ' '
+ @attributes[@y][x] = @a
+ x += 1
+ return
+ "csi_@": (chars) ->
+ chars = parseInt @fix_esc_arg chars, '1'
+ if chars < 1
+ return
+ if chars > @width - @x
+ chars = @width - @x
+ else
+ dest = @width - 1
+ if dest >= @width
+ dest = @width - 1
+ while dest - chars >= @x
+ @text[@y][dest] = @text[@y][dest - chars]
+ @attributes[@y][dest] = @attributes[@y][dest - chars]
+ dest -= 1
+ @csi_X chars # clear
+ csi_P: (chars) ->
+ chars = parseInt @fix_esc_arg chars, '1'
+ if chars < 1
+ return
+ if chars > @width - @x
+ chars = @width - @x
+ else
+ dest = @x
+ while dest < @width - chars
+ @text[@y][dest] = @text[@y][dest + chars]
+ @attributes[@y][dest] = @attributes[@y][dest + chars]
+ dest += 1
+ # clear the space moved out of
+ x = @width - chars
+ while x < @width
+ @text[@y][x] = ' '
+ @attributes[@y][x] = @a
+ x += 1
+
+ # set modes not starting with "?"
+ csi_h: (args...) ->
+ for i in args
+ arg = @fix_esc_arg i, ''
+ switch arg
+ when '0' # error (ignored
+ return
+ when '2' # KAM -- keyboard action
+ return
+ when '4' # put cursor in "INSERT" mode
+ return
+ when '12' # turn local echo off (or on?)
+ return
+ # TODO when '20' LNM linefeed/newline
+ else
+ log "Unimplemented arg for csi_h: #{arg}"
+ # set modes starting with "?"
+ csiq_h: (args...) ->
+ for i in args
+ arg = @fix_esc_arg i, ''
+ switch arg
+ when '1' # mode ?1
+ # numlock on
+ return
+ when '25'
+ @cursor_visible = true
+ when '1000'
+ # x11 normal mouse tracking
+ return
+ when '1049'
+ if @saved_normal_screen?
+ log "ignoring request to switch to the alt screen because we're already on the alt screen"
+ return
+ @saved_normal_screen = x: @x, y: @y, text: @text, attributes: @attributes
+ @text = []
+ @attributes = []
+ for y in [0...@height]
+ @text[y] = []
+ @attributes[y] = []
+ for x in [0...@width]
+ @text[y].push ' '
+ @attributes[y].push 0x07
+ else
+ log "Unimplemented arg for csiq_h: #{arg}"
+ return
+
+ # reset (turn off) modes
+ # note that modes starting with ? call csiq_l, not this
+ csi_l: ->
+ for i in arguments
+ arg = @fix_esc_arg i, ''
+ switch arg
+ when '0' # error (ignored
+ return
+ when '2' # KAM -- keyboard action
+ return
+ when '4' # put cursor in "REPLACE" mode
+ return
+ when '12' # turn local echo on (or off?)
+ return
+ # TODO when '20' LNM linefeed/newline
+
+ # reset (turn off) modes starting with ?
+ # in st source, these are priv=true
+ csiq_l: ->
+ args = []
+ for i in arguments
+ arg = @fix_esc_arg i, ''
+ switch arg
+ when '1' # mode ?1 reset
+ # numlock off
+ return
+ when '12' # mode ?12
+ # stop blinking the cursor (ignored)
+ return
+ when '25' # mode ?25 reset
+ @cursor_visible = false
+ when '0', '2', '3', '4', '8', '18', '19', '42', '12'
+ # st ignores all of these
+ return
+ when '9', '1000', '1002', '1003', '1004', '1006'
+ # mouse reporting and such (ignored)
+ return
+ when '1049'
+ if not @saved_normal_screen?
+ log "ignoring request to switch to the normal screen because we're already on the normal screen"
+ return
+ @x = @saved_normal_screen.x
+ @y = @saved_normal_screen.y
+ @text = @saved_normal_screen.text
+ @attributes = @saved_normal_screen.attributes
+ @saved_normal_screen = null
+ else
+ log "Unimplemented arg for csiq_l: #{arg}"
+ return
+
+ # set color, bold, underline, etc
+ csi_m: ->
+ args = []
+ for i in arguments
+ args.push @fix_esc_arg i, '0'
+
+ while args.length > 0
+ arg = args.shift()
+ switch arg
+ # remove all style/color
+ when '0'
+ @a = 0x07
+
+ # style attributes
+ when '1' # bold
+ @set_attribute_bits 0x010000, 0x010000
+ when '3' # italic (rare)
+ @set_attribute_bits 0x200000, 0x200000
+ when '4' # underline
+ @set_attribute_bits 0x020000, 0x020000
+ when '5' # blink
+ @set_attribute_bits 0x040000, 0x040000
+ when '7' # inverse
+ @set_attribute_bits 0x080000, 0x080000
+ when '8' # invisible. urivt ignores this
+ @set_attribute_bits 0x100000, 0x100000
+
+ # disable style attributes
+ when '21' # not bold (rare)
+ @set_attribute_bits 0x010000, 0
+ when '22' # not bold
+ @set_attribute_bits 0x010000, 0
+ when '23' # not italic (rare)
+ @set_attribute_bits 0x200000, 0
+ when '24' # not underline
+ @set_attribute_bits 0x020000, 0
+ when '25' # not blink
+ @set_attribute_bits 0x040000, 0
+ when '27' # not inverse
+ @set_attribute_bits 0x080000, 0
+ when '28' # not invisible
+ @set_attribute_bits 0x100000, 0
+
+ when '100' # reset colors but not other attributes
+ @set_attribute_bits 0xffff, 0x0007
+
+ # 8 fg colors
+ when '30' # fg black
+ @set_attribute_bits 0xff, 0x00
+ when '31' # fg red
+ @set_attribute_bits 0xff, 0x01
+ when '32' # fg green
+ @set_attribute_bits 0xff, 0x02
+ when '33' # fg yellow
+ @set_attribute_bits 0xff, 0x03
+ when '34' # fg blue
+ @set_attribute_bits 0xff, 0x04
+ when '35' # fg magenta
+ @set_attribute_bits 0xff, 0x05
+ when '36' # fg cyan
+ @set_attribute_bits 0xff, 0x06
+ when '37', '39' # fg white (39 is default)
+ @set_attribute_bits 0xff, 0x07
+
+ when '38'
+ if args.length >= 2 and args[0] is '5'
+ args.shift()
+ @set_attribute_bits 0xff, (0xff & args.shift())
+ else
+ @set_attribute_bits 0x20000, 0x20000
+
+ # 8 bg colors
+ when '40' # bg black
+ @set_attribute_bits 0xff00, 0x0000
+ when '41' # bg red
+ @set_attribute_bits 0xff00, 0x0100
+ when '42' # bg green
+ @set_attribute_bits 0xff00, 0x0200
+ when '43' # bg yellow
+ @set_attribute_bits 0xff00, 0x0300
+ when '44' # bg blue
+ @set_attribute_bits 0xff00, 0x0400
+ when '45' # bg magenta
+ @set_attribute_bits 0xff00, 0x0500
+ when '46' # bg cyan
+ @set_attribute_bits 0xff00, 0x0600
+ when '47' # bg white
+ @set_attribute_bits 0xff00, 0x0700
+ when '49' # bg default
+ @set_attribute_bits 0xff00, 0x0000
+
+ when '48'
+ if args.length >= 2 and args[0] is '5'
+ args.shift()
+ @set_attribute_bits 0xff00, ((0xff & args.shift()) << 8)
+ else
+ @set_attribute_bits 0x20000, 0x20000
+
+ # bright fg colors
+ when '90' # fg bright black
+ @set_attribute_bits 0xff, 0x08
+ when '91' # fg bright red
+ @set_attribute_bits 0xff, 0x09
+ when '92' # fg bright green
+ @set_attribute_bits 0xff, 0x0a
+ when '93' # fg bright yellow
+ @set_attribute_bits 0xff, 0x0b
+ when '94' # fg bright blue
+ @set_attribute_bits 0xff, 0x0c
+ when '95' # fg bright magenta
+ @set_attribute_bits 0xff, 0x0d
+ when '96' # fg bright cyan
+ @set_attribute_bits 0xff, 0x0e
+ when '97' # fg bright white
+ @set_attribute_bits 0xff, 0x0f
+
+ # bright bg colors
+ when '100' # bg bright black
+ @set_attribute_bits 0xff00, 0x0800
+ when '101' # bg bright red
+ @set_attribute_bits 0xff00, 0x0900
+ when '102' # bg bright green
+ @set_attribute_bits 0xff00, 0x0a00
+ when '103' # bg bright yellow
+ @set_attribute_bits 0xff00, 0x0b00
+ when '104' # bg bright blue
+ @set_attribute_bits 0xff00, 0x0c00
+ when '105' # bg bright magenta
+ @set_attribute_bits 0xff00, 0x0d00
+ when '106' # bg bright cyan
+ @set_attribute_bits 0xff00, 0x0e00
+ when '107' # bg bright white
+ @set_attribute_bits 0xff00, 0x0f00
+
+ else
+ # if we don't recognize the style, go back to default
+ log "unrecognized csi_m arg: \"#{arg}\""
+ @a = 0
+ return
+
+ # set scroll region
+ csi_r: (top, bottom) ->
+ top = -1 + parseInt @fix_esc_arg top, '1'
+ bottom = -1 + parseInt @fix_esc_arg bottom, '10000'
+ if top < 0
+ top = 0
+ if bottom >= @height
+ bottom = @height - 1
+ @scroll_top = top
+ @scroll_bottom = bottom
+ return
+
+ # move cursor up one line, if it's at the top, scroll everything down
+ esc_M: ->
+ if @y > 0
+ @csi_A '1'
+ else
+ @csi_L '1'
+
+ # esc ]
+ "esc_]": (seq) ->
+ sem = seq.indexOf ';'
+ if sem < 1 || seq[0] isnt '0'
+ log "Unimplemented esc_] (OS command) sequence: #{seq}"
+ return
+ if window?
+ title = seq.substr sem + 1
+ title = title.substr 0, title.length - 1
+ window.document.title = title
+
+ # "The auxiliary keypad keys will transmit control sequences."
+ "esc_=": ->
+
+ # esc )
+ "esc_)": ->
+ @alternate_charset = true
+ # esc ( 0
+ gzd4_0: ->
+ @alternate_charset = true
+ # esc ( B
+ gzd4_B: ->
+ @alternate_charset = false
+
+ esc_n: ->
+ @alternate_charset = false
+
+ esc_o: ->
+ @alternate_charset = true
+
+ # str is the whole escape sequence (minus the esc character)