1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
"=============================================================================
" FILE: terminal.vim
" AUTHOR:  Shougo Matsushita <Shougo.Matsu@gmail.com>
" License: MIT license  {{{
"     Permission is hereby granted, free of charge, to any person obtaining
"     a copy of this software and associated documentation files (the
"     "Software"), to deal in the Software without restriction, including
"     without limitation the rights to use, copy, modify, merge, publish,
"     distribute, sublicense, and/or sell copies of the Software, and to
"     permit persons to whom the Software is furnished to do so, subject to
"     the following conditions:
"
"     The above copyright notice and this permission notice shall be included
"     in all copies or substantial portions of the Software.
"
"     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
"     OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
"     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
"     IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
"     CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
"     TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
"     SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
"=============================================================================

function! vimshell#terminal#init() abort "{{{
  let b:interactive.terminal = {
        \ 'syntax_names' : {},
        \ 'syntax_commands' : {},
        \ 'syntax_highlights' : {},
        \ 'titlestring' : &titlestring,
        \ 'titlestring_save' : &titlestring,
        \ 'save_pos' : getpos('.')[1 : 2],
        \ 'region_top' : 0,
        \ 'region_bottom' : 0,
        \ 'standard_character_set' : 'United States',
        \ 'alternate_character_set' : 'United States',
        \ 'current_character_set' : 'United States',
        \ 'is_error' : 0,
        \ 'wrap' : &l:wrap,
        \ 'buffer' : '',
        \}

  call vimshell#terminal#init_highlight()
endfunction"}}}
function! vimshell#terminal#init_highlight() abort "{{{
  if s:use_conceal()
    syntax match vimshellEscapeSequenceConceal
          \ contained conceal    '\e\[[0-9;]*m'
    syntax match vimshellEscapeSequenceMarker
          \ conceal               '\e\[0*m\|\e0m\[\|\e]0;'
  endif

  if !exists('b:interactive.terminal')
    return
  endif

  let terminal = b:interactive.terminal
  for syntax_name in values(terminal.syntax_names)
    execute 'syntax region' syntax_name
          \ terminal.syntax_commands[syntax_name]
    execute 'highlight' syntax_name
          \ terminal.syntax_highlights[syntax_name]
  endfor
endfunction"}}}
function! vimshell#terminal#print(string, is_error) abort "{{{
  if !has_key(b:interactive, 'terminal')
    call vimshell#terminal#init()
  endif

  setlocal modifiable
  if g:vimshell_enable_debug
    echomsg 'print string = ' . string(a:string)
  endif

  call vimshell#util#disable_auto_complete()

  if &filetype ==# 'vimshell' &&
        \ empty(b:vimshell.continuation) && (vimshell#check_prompt()
        \ || vimshell#util#head_match(getline('.'),
        \     vimshell#get_secondary_prompt()))
    " Move line.
    call append(line('.'), '')
    call cursor(line('.')+1, 0)
  endif

  let current_line = getline('.')

  let s:virtual = {
        \ 'lines' : {},
        \ 'col' : 0,
        \ 'line' : 0,
        \ }
  let s:virtual.lines = {}
  let [s:virtual.line, s:virtual.col] =
        \ s:get_virtual_col(line('.'), col('.'))
  if g:vimshell_enable_debug
    echomsg '[s:virtual.line, s:virtual.col] = ' .
          \ string([s:virtual.line, s:virtual.col])
  endif
  let s:virtual.lines[s:virtual.line] = current_line

  if b:interactive.type !=# 'terminal' && a:string !~ '[\e\b]'
    call s:optimized_print(a:string, a:is_error)
    return
  endif

  let b:interactive.terminal.is_error = a:is_error

  let newstr = s:check_str(a:string)

  " Print rest string.
  call s:output_string(newstr)

  " Set lines.
  for linenr in sort(map(keys(s:virtual.lines),
        \ 'str2nr(v:val)'), 's:sortfunc')
    call setline(linenr, substitute(
          \ s:virtual.lines[linenr], '[^!]\zs!\{6}\ze[^!]', '', 'g'))
  endfor

  call s:set_cursor()
endfunction"}}}
function! vimshell#terminal#set_title() abort "{{{
  if !has_key(b:interactive, 'terminal')
    call vimshell#terminal#init()
  endif

  let &titlestring = b:interactive.terminal.titlestring
endfunction"}}}
function! vimshell#terminal#restore_title() abort "{{{
  if !has_key(b:interactive, 'terminal')
    call vimshell#terminal#init()
  endif

  let &titlestring = b:interactive.terminal.titlestring_save
endfunction"}}}
function! vimshell#terminal#clear_highlight() abort "{{{
  if !s:use_conceal()
    return
  endif

  if !has_key(b:interactive, 'terminal')
    call vimshell#terminal#init()
  endif

  for syntax_names in values(b:interactive.terminal.syntax_names)
    execute 'highlight clear' syntax_names
    execute 'syntax clear' syntax_names
  endfor

  let b:interactive.terminal.syntax_names = {}
  let b:interactive.terminal.syntax_commands = {}
  let b:interactive.terminal.syntax_highlights = {}

  " Restore wrap.
  let &l:wrap = b:interactive.terminal.wrap
endfunction"}}}

function! s:check_str(string) abort "{{{
  " Optimize checkstr.
  let string = b:interactive.terminal.buffer . a:string
  let b:interactive.terminal.buffer = ''
  while string =~ '\%(\e\[[0-9;]*m\)\{2,}'
    let string = substitute(string,
          \ '\e\[[0-9;]*\zsm\e\[\ze[0-9;]*m', ';', 'g')
  endwhile

  let max = len(string)
  let newstr = ''
  let pos = 0

  while pos < max
    let char = string[pos]

    if char !~ '[[:cntrl:]]' "{{{
      let newstr .= char
      let pos += 1
      continue
      "}}}
    elseif char == "\<C-h>" "{{{
      " Print rest string.
      call s:output_string(newstr)
      let newstr = ''

      if pos + 1 < max && string[pos+1] == "\<C-h>"
        " <C-h><C-h>
        call s:control.delete_multi_backword_char()
        let pos += 2
      else
        " <C-h>
        call s:control.delete_backword_char()
        let pos += 1
      endif

      continue
      "}}}
    elseif char == "\<ESC>" "{{{
      " Check escape sequence.
      let checkstr = string[pos+1 :]
      if checkstr == ''
        " Incomplete sequence.
        let b:interactive.terminal.buffer = string[pos :]
        break
      endif

      " Check CSI pattern.
      if checkstr =~ '^\[[0-9;]*.'
        let matchstr = matchstr(checkstr, '^\[[0-9;]*.')

        if has_key(s:escape_sequence_csi, matchstr[-1:])
          call s:output_string(newstr)
          let newstr = ''

          call call(s:escape_sequence_csi[matchstr[-1:]],
                \ [matchstr], s:escape)

          let pos += len(matchstr) + 1
          continue
        endif
      endif

      " Check simple pattern.
      let checkchar1 = checkstr[0]
      if has_key(s:escape_sequence_simple_char1, checkchar1) "{{{
        call s:output_string(newstr)
        let newstr = ''

        call call(s:escape_sequence_simple_char1[checkchar1], [''], s:escape)

        let pos += 2
        continue
      endif"}}}
      let checkchar2 = checkstr[: 1]
      if checkchar2 != '' &&
            \ has_key(s:escape_sequence_simple_char2, checkchar2) "{{{
        call s:output_string(newstr)
        let newstr = ''

        call call(s:escape_sequence_simple_char2[checkchar2], [''], s:escape)

        let pos += 3
        continue
      endif"}}}

      let matched = 0
      " Check match pattern.
      for pattern in keys(s:escape_sequence_match) "{{{
        if checkstr =~ pattern
          let matched = 1

          " Print rest string.
          call s:output_string(newstr)
          let newstr = ''

          let matchstr = matchstr(checkstr, pattern)

          call call(s:escape_sequence_match[pattern], [matchstr], s:escape)

          let pos += len(matchstr) + 1
          break
        endif
      endfor"}}}

      if matched
        continue
      elseif checkstr !~ '\r\|\n'
        " Incomplete sequence.
        let b:interactive.terminal.buffer = string[pos :]
        break
      endif"}}}
    elseif has_key(s:control_sequence, char) "{{{
      " Check other pattern.
      " Print rest string.
      call s:output_string(newstr)
      let newstr = ''

      call call(s:control_sequence[char], [], s:control)

      let pos += 1
      continue
    endif"}}}

    let newstr .= char
    let pos += 1
  endwhile

  return newstr
endfunction "}}}
function! s:optimized_print(string, is_error) abort "{{{
  " Strip <CR>.
  let string = substitute(substitute(
        \ a:string, "\<C-g>", '', 'g'), '\r\+\n', '\n', 'g')

  if g:vimshell_enable_debug
    echomsg 'print optimized output string = ' . string(string)
  endif

  let lines = split(string, '\n', 1)

  if string =~ '\r'
    call s:print_with_redraw(a:is_error, lines)
  else
    call s:print_simple(a:is_error, lines)
  endif

  call cursor(0, col('$'))
  let [s:virtual.line, s:virtual.col] =
        \ s:get_virtual_col(line('.'), col('.'))
  call s:set_cursor()
endfunction"}}}
function! s:print_with_redraw(is_error, lines) abort "{{{
  let cnt = 1
  for line in a:lines
    if cnt != 1 ||
          \ (s:is_no_echoback() && getline('$') != '')
      call append('.', '')
      call cursor(line('.')+1, 0)
    endif

    let ls = map(split(line, '\r\ze.', 1), "substitute(v:val, '\\r', '', '')")

    if a:is_error
      call map(ls, "v:val != '' ? ('!!!'.v:val.'!!!') : v:val")
    endif

    for l in ls
      call setline('.', l)
      sleep 1m
      redraw
    endfor

    let cnt += 1
  endfor
endfunction"}}}
function! s:print_simple(is_error, lines) abort "{{{
  let lines = a:lines

  if a:is_error
    call map(lines, "v:val != '' ? ('!!!'.v:val.'!!!') : v:val")
  endif

  " Optimized print.
  if s:is_no_echoback()
    if line('$') == 1 && getline('$') == ''
      call setline('.', lines[0])
    else
      call append('.', lines[0])
    endif

    call cursor(line('.')+1, 0)
    call cursor(0, col('$'))
  elseif line('.') != b:interactive.echoback_linenr
    call setline('.', substitute(
          \ getline('.') . lines[0], '[^!]\zs!\{6}\ze[^!]', '', 'g'))
  endif

  let lines = lines[1:]

  call append('.', lines)
  call cursor(line('.') + len(lines), 0)
  call cursor(0, col('$'))
endfunction"}}}
function! s:set_cursor() abort "{{{
  " Get real pos(0 origin).
  let [line, col] = s:get_real_pos(
        \ s:virtual.line, s:virtual.col)
  call s:set_screen_pos(line, col)

  " Convert to 1 origin.
  let col += 1

  if g:vimshell_enable_debug
    echomsg 'set cursor = ' . string([line, col])
  endif

  " Move pos.
  call cursor(line, col)

  if b:interactive.type ==# 'terminal'
    let b:interactive.save_cursor = getpos('.')
  endif

  redraw
endfunction"}}}
function! s:is_no_echoback() abort "{{{
  return b:interactive.type ==# 'interactive'
          \ && vimshell#util#is_windows()
          \ && has_key(b:interactive, 'command')
          \ && !get(g:vimshell_interactive_echoback_commands,
          \        b:interactive.command, 0)
endfunction"}}}

function! s:init_terminal() abort "{{{
endfunction"}}}
function! s:output_string(string) abort "{{{
  if s:virtual.line == b:interactive.echoback_linenr
    if s:is_no_echoback()
      " no echoback command.
      let s:virtual.line += 1
      let s:virtual.lines[s:virtual.line] = a:string
      let s:virtual.col = len(a:string)
    endif

    return
  endif

  if g:vimshell_enable_debug
    echomsg 'print output string = ' . string(a:string)
  endif

  if a:string == ''
    return
  endif

  let string = a:string != '' && b:interactive.terminal.is_error ?
        \ '!!!' . a:string . '!!!' : a:string

  if b:interactive.terminal.current_character_set
        \ ==# 'Line Drawing'
    " Convert characters.
    let string = ''
    for c in split(a:string, '\zs')
      let string .= has_key(s:drawing_character_table, c)?
            \ s:drawing_character_table[c] : c
    endfor
  endif

  call s:set_screen_string(s:virtual.line, s:virtual.col, string)
endfunction"}}}
function! s:sortfunc(i1, i2) abort "{{{
  return a:i1 == a:i2 ? 0 : a:i1 > a:i2 ? 1 : -1
endfunction"}}}
function! s:scroll_up(number) abort "{{{
  let line = b:interactive.terminal.region_bottom
  let end = b:interactive.terminal.region_top - a:number
  while line >= end
    let s:virtual.lines[line] = has_key(s:virtual.lines, line - a:number) ?
          \ s:virtual.lines[line - a:number] : getline(line - a:number)

    let line -= 1
  endwhile

  let i = 0
  while i < a:number
    let s:virtual.lines[b:interactive.terminal.region_top + i] = ''
    let i += 1
  endwhile
endfunction"}}}
function! s:scroll_down(number) abort "{{{
  let line = b:interactive.terminal.region_top
  let end = b:interactive.terminal.region_bottom - a:number
  while line <= end
    let s:virtual.lines[line] = has_key(s:virtual.lines, line + a:number) ?
          \ s:virtual.lines[line + a:number] : getline(line + a:number)

    let line += 1
  endwhile

  let i = 0
  while i < a:number
    let s:virtual.lines[b:interactive.terminal.region_bottom - i] = ''
    let i += 1
  endwhile
endfunction"}}}
function! s:use_conceal() abort "{{{
  return has('conceal')
endfunction"}}}

" Note: Real pos is 0 origin.
function! s:get_real_pos(line, col) abort "{{{
  let current_line = get(s:virtual.lines, a:line, getline(a:line))
  if a:col <= 1 && current_line !~ '\e\[[0-9;]*m'
    return [a:line, 0]
  endif

  return [a:line, vimshell#terminal#get_col(
        \ get(s:virtual.lines, a:line, getline(a:line)), -1, -1, a:col, 0)]
endfunction"}}}
function! s:get_virtual_col(line, col) abort "{{{
  let current_line = get(s:virtual.lines, a:line, getline(a:line))
  if a:col <= 0 && current_line !~ '\e\[[0-9;]*m'
    return [a:line, 1]
  endif

  return [a:line, vimshell#terminal#get_col(
        \ get(s:virtual.lines, a:line, getline(a:line)), -1, -1, a:col, 1)]
endfunction"}}}
function! s:set_screen_string(line, col, string) abort "{{{
  let [line, col] = s:get_real_pos(a:line, a:col)
  call s:set_screen_pos(line, col)

  let len = strwidth(a:string)
  let s:virtual.lines[line] =
        \ (col > 1 ? s:virtual.lines[line][: col-1] : '')
        \ . a:string
        \ . s:virtual.lines[line][col+len :]
  let current_line = s:virtual.lines[line]

  let s:virtual.col =
        \ vimshell#terminal#get_col(current_line, s:virtual.col, col, col+len, 1)
  if g:vimshell_enable_debug
    echomsg 'current_line = ' . string(current_line)
    echomsg 'current_line[col:] = ' . string(current_line[col :])
    echomsg '[old_virt_col, real_col, new_virt_col, string] = ' .
          \ string([a:col, col, s:virtual.col, a:string])
  endif
endfunction"}}}
function! s:set_screen_pos(line, col) abort "{{{
  if a:line == ''
    return
  endif

  if !has_key(s:virtual.lines, a:line)
    let s:virtual.lines[a:line] = ''
  endif
  if a:col > len(s:virtual.lines[a:line])
    let s:virtual.lines[a:line] .=
          \ repeat(' ', a:col - len(s:virtual.lines[a:line]))
  endif
endfunction"}}}
function! vimshell#terminal#get_col(line, start_virtual, start_real, max_col, is_virtual) abort "{{{
  " is_virtual -> a:col : real col.
  " not -> a:col : virtual col.
  let virtual_col = a:start_virtual < 0 ? 1 : a:start_virtual
  let real_col = a:start_real < 0 ? 0 : a:start_virtual

  let current_line = a:line

  if current_line !~ '\e\[[0-9;]*m'
    " Optimized.
    for c in split(current_line[: a:max_col*3], '\zs')
      let real_col += len(c)
      let virtual_col += strwidth(c)

      let check_col = a:is_virtual ? real_col : virtual_col
      if check_col > a:max_col
        break
      endif
    endfor
  else
    let skip_cnt = 0
    for c in split(current_line[real_col :], '\zs')
      if skip_cnt > 0
        let skip_cnt -= 1
        continue
      endif

      if c == "\<ESC>"
            \ && current_line[real_col :] =~ '^\e\[[0-9;]*m'
        " Skip.
        let sequence = matchstr(current_line,
              \ '^\e\[[0-9;]*m', real_col)
        let skip_cnt = len(sequence)-1
        let real_col += len(sequence)
      else
        let real_col += len(c)
        let virtual_col += strwidth(c)
      endif

      let check_col = a:is_virtual ? real_col : virtual_col
      if check_col > a:max_col
        break
      endif
    endfor
  endif

  return (a:is_virtual ? virtual_col : real_col)
endfunction"}}}

" Escape sequence functions.
let s:escape = {}
function! s:escape.ignore(matchstr) abort "{{{
endfunction"}}}

" Color table. "{{{
let s:color_table = [ 0x00, 0x5F, 0x87, 0xAF, 0xD7, 0xFF ]
let s:grey_table = [
      \ 0x08, 0x12, 0x1C, 0x26, 0x30, 0x3A, 0x44, 0x4E,
      \ 0x58, 0x62, 0x6C, 0x76, 0x80, 0x8A, 0x94, 0x9E,
      \ 0xA8, 0xB2, 0xBC, 0xC6, 0xD0, 0xDA, 0xE4, 0xEE
      \]
let s:highlight_table = {
      \ '0' : ' cterm=NONE ctermfg=NONE ctermbg=NONE gui=NONE guifg=NONE guibg=NONE',
      \ '1' : ' cterm=BOLD gui=BOLD',
      \ '3' : ' cterm=ITALIC gui=ITALIC',
      \ '4' : ' cterm=UNDERLINE gui=UNDERLINE',
      \ '7' : ' cterm=REVERSE gui=REVERSE',
      \ '8' : ' ctermfg=0 ctermbg=0 guifg=#000000 guibg=#000000',
      \ '9' : ' gui=UNDERCURL',
      \ '21' : ' cterm=UNDERLINE gui=UNDERLINE',
      \ '22' : ' gui=NONE',
      \ '23' : ' gui=NONE',
      \ '24' : ' gui=NONE',
      \ '25' : ' gui=NONE',
      \ '27' : ' gui=NONE',
      \ '28' : ' ctermfg=NONE ctermbg=NONE guifg=NONE guibg=NONE',
      \ '29' : ' gui=NONE',
      \ '39' : ' ctermfg=NONE guifg=NONE',
      \ '49' : ' ctermbg=NONE guibg=NONE',
      \}"}}}
function! s:escape.highlight(matchstr) abort "{{{
  if g:vimshell_disable_escape_highlight
        \ || (b:interactive.type == 'interactive' &&
        \     get(g:vimshell_interactive_monochrome_commands,
        \         b:interactive.command, 0))
        \ || b:interactive.terminal.is_error
    return
  endif

  call s:output_string("\<ESC>" . a:matchstr)

  " Check cached highlight.
  if a:matchstr =~ '^\[0\?m$'
        \ || has_key(b:interactive.terminal.syntax_names, a:matchstr)
    return
  endif

  let highlight = ''
  let highlight_list =
        \ split(matchstr(a:matchstr, '^\[\zs[0-9;]\+'), ';', 1)
  let cnt = 0
  if empty(highlight_list)
    " Default.
    let highlight_list = [ 0 ]
  endif
  for color_code in map(highlight_list, 'str2nr(v:val)')
    if has_key(s:highlight_table, color_code) "{{{
      " Use table.
      let highlight .= s:highlight_table[color_code]
    elseif 30 <= color_code && color_code <= 37
      " Foreground color.
      let highlight .= printf(' ctermfg=%d guifg=%s',
            \ color_code - 30, g:vimshell_escape_colors[color_code - 30])
    elseif color_code == 38
      if len(highlight_list) - cnt < 3
        " Error.
        break
      endif

      " Foreground 256 colors.
      let color = highlight_list[cnt + 2]
      if color >= 232
        " Grey scale.
        let gcolor = s:grey_table[(color - 232)]
        let highlight .= printf(' ctermfg=%d guifg=#%02x%02x%02x',
              \ color, gcolor, gcolor, gcolor)
      elseif color >= 16
        " RGB.
        let gcolor = color - 16
        let red = s:color_table[gcolor / 36]
        let green = s:color_table[(gcolor % 36) / 6]
        let blue = s:color_table[gcolor % 6]

        let highlight .= printf(' ctermfg=%d guifg=#%02x%02x%02x',
              \ color, red, green, blue)
      else
        let highlight .= printf(' ctermfg=%d guifg=%s',
              \ color, g:vimshell_escape_colors[color])
      endif
      break
    elseif 40 <= color_code && color_code <= 47
      " Background color.
      let highlight .= printf(' ctermbg=%d guibg=%s',
            \ color_code - 40, g:vimshell_escape_colors[color_code - 40])
    elseif color_code == 48
      if len(highlight_list) - cnt < 3
        " Error.
        break
      endif

      " Background 256 colors.
      let color = highlight_list[cnt + 2]
      if color >= 232
        " Grey scale.
        let gcolor = s:grey_table[(color - 232)]
        let highlight .= printf(' ctermbg=%d guibg=#%02x%02x%02x',
              \ color, gcolor, gcolor, gcolor)
      elseif color >= 16
        " RGB.
        let gcolor = color - 16
        let red = s:color_table[gcolor / 36]
        let green = s:color_table[(gcolor % 36) / 6]
        let blue = s:color_table[gcolor % 6]

        let highlight .= printf(' ctermbg=%d guibg=#%02x%02x%02x',
              \ color, red, green, blue)
      else
        let highlight .= printf(' ctermbg=%d guibg=%s',
              \ color, g:vimshell_escape_colors[color])
      endif
      break
    elseif 90 <= color_code && color_code <= 97
      " Foreground color(high intensity).
      let highlight .= printf(' ctermfg=%d guifg=%s',
            \ color_code - 82, g:vimshell_escape_colors[color_code - 82])
    elseif 100 <= color_code && color_code <= 107
      " Background color(high intensity).
      let highlight .= printf(' ctermbg=%d guibg=%s',
            \ color_code - 92, g:vimshell_escape_colors[color_code - 92])
    endif"}}}

    let cnt += 1
  endfor

  if highlight == ''
    return
  endif

  let [line, col] = s:get_real_pos(s:virtual.line, s:virtual.col)
  let col += 1
  let syntax_name = 'EscapeSequenceAt_' . bufnr('%')
        \ . '_' . line . '_' . col
  let syntax_command = printf('start=+\e\%s+ end=+\ze\e[\[0*m]\|$+ ' .
        \ 'contains=vimshellEscapeSequenceConceal oneline', a:matchstr)

  execute 'syntax region' syntax_name syntax_command
  execute 'highlight' syntax_name highlight

  let terminal = b:interactive.terminal
  let terminal.syntax_names[a:matchstr] = syntax_name
  let terminal.syntax_commands[syntax_name] = syntax_command
  let terminal.syntax_highlights[syntax_name] = highlight

  " Note: When use concealed text, wrapped text is wrong...
  setlocal nowrap
endfunction"}}}
function! s:escape.move_cursor(matchstr) abort "{{{
  let params = matchstr(a:matchstr, '[0-9;]\+')
  if params == ''
    let args = [1, 1]
  else
    let args = split(params, ';', 1)
  endif

  let s:virtual.line = get(args, 0, 1)
  let s:virtual.col = get(args, 1, 1)
  if s:virtual.line !~ '^\d\+$' || s:virtual.col !~ '^\d\+$'
    call vimshell#echo_error(
          \ 'Move cursor escape sequence format error: str = "'
          \ . a:matchstr . '"')
    return
  endif

  let [line, col] = s:get_real_pos(s:virtual.line, s:virtual.col)
  call s:set_screen_pos(line, col)
endfunction"}}}
function! s:escape.move_cursor_column(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  let s:virtual.col = n

  let [line, col] = s:get_real_pos(s:virtual.line, s:virtual.col)
  call s:set_screen_pos(line, col)
endfunction"}}}
function! s:escape.setup_scrolling_region(matchstr) abort "{{{
  let args = split(matchstr(a:matchstr, '[0-9;]\+'), ';', 1)

  let top = empty(args) ? 0 : args[0]
  let bottom = empty(args) ? 0 : args[1]

  if top == 1
    if (vimshell#util#is_windows() && bottom == 25)
          \|| (!vimshell#util#is_windows() && bottom == b:interactive.height)
      " Clear scrolling region.
      let [top, bottom] = [0, 0]
    endif
  endif

  let b:interactive.terminal.region_top = top
  let b:interactive.terminal.region_bottom = bottom
endfunction"}}}
function! s:escape.clear_line(matchstr) abort "{{{
  let [line, col] = s:get_real_pos(s:virtual.line, s:virtual.col)
  call s:set_screen_pos(line, col)

  let param = matchstr(a:matchstr, '\d\+')
  if param == '' || param == '0'
    " Clear right line.
    let s:virtual.lines[line] = (col <= 0) ? '' : s:virtual.lines[line][ : col - 1]
  elseif param == '1'
    " Clear left line.
    let s:virtual.lines[line] = s:virtual.lines[line][col :]
    let s:virtual.col = 1
  elseif param == '2'
    " Clear whole line.
    let s:virtual.lines[line] = ''
    let s:virtual.col = 1
  endif
endfunction"}}}
function! s:escape.clear_screen(matchstr) abort "{{{
  let param = matchstr(a:matchstr, '\d\+')
  if param == '' || param == '0'
    " Clear screen from cursor down.
    call s:escape.clear_line(0)
    for linenr in filter(keys(s:virtual.lines), 'v:val > s:virtual.line')
      " Clear line.
      let s:virtual.lines[linenr] = ''
    endfor
  elseif param == '1'
    " Clear screen from cursor up.
    call s:escape.clear_line(1)
    for linenr in filter(keys(s:virtual.lines), 'v:val < s:virtual.line')
      " Clear line.
      let s:virtual.lines[linenr] = ''
    endfor
  elseif param == '2'
    " Clear entire screen.
    let reg = @x
    1,$ delete x
    let @x = reg

    let s:virtual.lines = {}
    let s:virtual.line = 1
    let s:virtual.col = 1

    call vimshell#terminal#clear_highlight()
  endif
endfunction"}}}
function! s:escape.move_up(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  if b:interactive.terminal.region_top <= s:virtual.line
        \ && s:virtual.line <= b:interactive.terminal.region_bottom
    " Scroll up n lines.
    call s:scroll_up(n)
  else
    let s:virtual.line -= n
    if s:virtual.line < 1
      let s:virtual.line = 1
    endif
  endif
endfunction"}}}
function! s:escape.move_down(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  if b:interactive.terminal.region_top <= s:virtual.line
        \ && s:virtual.line <= b:interactive.terminal.region_bottom
    " Scroll down n lines.
    call s:scroll_down(n)
  else
    let s:virtual.line += n
  endif
endfunction"}}}
function! s:escape.move_right(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  let s:virtual.col += n
endfunction"}}}
function! s:escape.move_left(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  let s:virtual.col -= n
  if s:virtual.col < 1
    let s:virtual.col = 1
  endif
endfunction"}}}
function! s:escape.move_down_head1(matchstr) abort "{{{
  call s:control.newline()
endfunction"}}}
function! s:escape.move_down_head(matchstr) abort "{{{
  call s:scroll_down(a:matchstr)
  let s:virtual.col = 1
endfunction"}}}
function! s:escape.move_up_head(matchstr) abort "{{{
  let param = matchstr(a:matchstr, '\d\+')
  if param != '0'
    call s:scroll_up(a:matchstr)
  endif
  let s:virtual.col = 1
endfunction"}}}
function! s:escape.scroll_up1(matchstr) abort "{{{
  call s:scroll_up(1)
endfunction"}}}
function! s:escape.scroll_down1(matchstr) abort "{{{
  call s:scroll_down(1)
endfunction"}}}
function! s:escape.move_col(matchstr) abort "{{{
  let num = matchstr(a:matchstr, '\d\+')
  let s:virtual.col = num
  if s:virtual.col < 1
    let s:virtual.col = 1
  endif
endfunction"}}}
function! s:escape.save_pos(matchstr) abort "{{{
  let b:interactive.terminal.save_pos = [s:virtual.line, s:virtual.col]
endfunction"}}}
function! s:escape.restore_pos(matchstr) abort "{{{
  let [s:virtual.line, s:virtual.col] = b:interactive.terminal.save_pos
endfunction"}}}
function! s:escape.change_title(matchstr) abort "{{{
  let title = matchstr(a:matchstr, '^k\zs.\{-}\ze\e\\')
  if empty(title)
    let title = matchstr(a:matchstr, '^][02];\zs.\{-}\ze'."\<C-g>")
  endif

  let &titlestring = title
  let b:interactive.terminal.titlestring = title
endfunction"}}}
function! s:escape.print_control_sequence(matchstr) abort "{{{
  call s:output_string("\<ESC>")
endfunction"}}}
function! s:escape.change_cursor_shape(matchstr) abort "{{{
  if !exists('+guicursor') || b:interactive.type !=# 'terminal'
    return
  endif

  let arg = matchstr(a:matchstr, '\d\+')

  if arg == 0 || arg == 1
    set guicursor=i:block-Cursor/lCursor
  elseif arg == 2
    set guicursor=i:block-Cursor/lCursor-blinkon0
  elseif arg == 3
    set guicursor=i:hor20-Cursor/lCursor
  elseif arg == 4
    set guicursor=i:hor20-Cursor/lCursor-blinkon0
  endif
endfunction"}}}
function! s:escape.change_character_set(matchstr) abort "{{{
  if a:matchstr =~ '^[()]0'
    " Line drawing set.
    if a:matchstr =~ '^('
      let b:interactive.terminal.standard_character_set = 'Line Drawing'
    else
      let b:interactive.terminal.alternate_character_set = 'Line Drawing'
    endif
  endif
endfunction"}}}
function! s:escape.reset(matchstr) abort "{{{
  call vimshell#terminal#init()
endfunction"}}}
function! s:escape.delete_chars(matchstr) abort "{{{
  let n = matchstr(a:matchstr, '\d\+')
  if n == ''
    let n = 1
  endif

  call s:escape.move_left(n)
  call s:output_string(repeat(' ', n))
endfunction"}}}

" Control sequence functions.
let s:control = {}
function! s:control.ignore() abort "{{{
endfunction"}}}
function! s:control.newline() abort "{{{
  let s:virtual.col = 1

  if b:interactive.type !=# 'terminal'
    " New line.
    call append(s:virtual.line, '')
  endif

  call s:escape.move_down(1)
endfunction"}}}
function! s:control.delete_backword_char() abort "{{{
  if s:virtual.line == b:interactive.echoback_linenr
    return
  endif

  if s:virtual.col == 1
    " Wrap above line.
    if s:virtual.line > 1
      let s:virtual.line -= 1
    endif

    if !has_key(s:virtual.lines, s:virtual.line)
      let s:virtual.lines[s:virtual.line] = getline(s:virtual.line)
    endif

    let s:virtual.col = s:get_virtual_col(
          \ s:virtual.line, len(s:virtual.lines[s:virtual.line]))[1]
    return
  endif

  call s:escape.move_left(1)
endfunction"}}}
function! s:control.delete_multi_backword_char() abort "{{{
  if s:virtual.line == b:interactive.echoback_linenr
    return
  endif

  if s:virtual.col == 1
    " Wrap above line.
    if s:virtual.line > 1
      let s:virtual.line -= 1
    endif

    if !has_key(s:virtual.lines, s:virtual.line)
      let s:virtual.lines[s:virtual.line] = getline(s:virtual.line)
    endif

    let s:virtual.col = len(s:virtual.lines[s:virtual.line])
    return
  endif

  call s:escape.move_left(2)
endfunction"}}}
function! s:control.carriage_return() abort "{{{
  let s:virtual.col = 1
endfunction"}}}
function! s:control.bell() abort "{{{
  echo 'Ring!'
endfunction"}}}
function! s:control.shift_in() abort "{{{
  let b:interactive.terminal.current_character_set = b:interactive.terminal.standard_character_set
endfunction"}}}
function! s:control.shift_out() abort "{{{
  let b:interactive.terminal.current_character_set = b:interactive.terminal.alternate_character_set
endfunction"}}}

let s:drawing_character_table = {
      \ 'j' : '+', 'k' : '+', 'l' : '+', 'm' : '+', 'n' : '+',
      \ 'o' : '-', 'p' : '-', 'q' : '-',
      \ 'r' : '_', 's' : '_',
      \ 't' : '+', 'u' : '+', 'v' : '+', 'w' : '+',
      \ 'x' : '|', 'a' : '#', '+' : '^', ',' : '<',
      \ '.' : 'v', 'I' : '0', '-' : '>', '''' : '*',
      \ 'h' : '#', '~' : 'O',
      \ }

" escape sequence list. {{{
" pattern: function
let s:escape_sequence_csi = {
      \ 'l' : s:escape.ignore,
      \ 'h' : s:escape.ignore,
      \
      \ 'm' : s:escape.highlight,
      \ 'r' : s:escape.setup_scrolling_region,
      \ 'A' : s:escape.move_up,
      \ 'B' : s:escape.move_down,
      \ 'C' : s:escape.move_right,
      \ 'D' : s:escape.move_left,
      \ 'E' : s:escape.move_down_head,
      \ 'F' : s:escape.move_up_head,
      \ 'G' : s:escape.move_col,
      \ 'H' : s:escape.move_cursor,
      \ 'f' : s:escape.move_cursor,
      \ 'J' : s:escape.clear_screen,
      \ 'K' : s:escape.clear_line,
      \ 'P' : s:escape.delete_chars,
      \
      \ 'g' : s:escape.ignore,
      \ 'c' : s:escape.ignore,
      \ 'd' : s:escape.move_cursor_column,
      \ 'y' : s:escape.ignore,
      \ 'q' : s:escape.ignore,
      \}
let s:escape_sequence_match = {
      \ '^\[?\d\+[hl]' : s:escape.ignore,
      \ '^[()][AB012UK]' : s:escape.change_character_set,
      \ '^k.\{-}\e\\' : s:escape.change_title,
      \ '^\][02];.\{-}'."\<C-g>" : s:escape.change_title,
      \ '^#\d' : s:escape.ignore,
      \ '^\dn' : s:escape.ignore,
      \ '^\[?1;\d\+0c' : s:escape.ignore,
      \ '^\d q' : s:escape.change_cursor_shape,
      \ '^\]4;\d\+;rgb:\x\x/\x\x/\x\x\e\\' : s:escape.ignore,
      \}
let s:escape_sequence_simple_char1 = {
      \ 'N' : s:escape.ignore,
      \ 'O' : s:escape.ignore,
      \
      \ '7' : s:escape.save_pos,
      \ '8' : s:escape.restore_pos,
      \ '(' : s:escape.ignore,
      \
      \ 'c' : s:escape.reset,
      \
      \ '<' : s:escape.ignore,
      \ '=' : s:escape.ignore,
      \ '>' : s:escape.ignore,
      \
      \ 'E' : s:escape.move_down_head1,
      \ 'G' : s:escape.ignore,
      \ 'I' : s:escape.ignore,
      \ 'J' : s:escape.ignore,
      \ 'K' : s:escape.ignore,
      \ 'D' : s:escape.scroll_up1,
      \ 'M' : s:escape.scroll_down1,
      \
      \ 'Z' : s:escape.ignore,
      \ '%' : s:escape.ignore,
      \}
let s:escape_sequence_simple_char2 = {
      \ '/Z' : s:escape.ignore,
      \ '%@' : s:escape.ignore,
      \ '%G' : s:escape.ignore,
      \ '%8' : s:escape.ignore,
      \ '#8' : s:escape.ignore,
      \}
"}}}
" control sequence list. {{{
" pattern: function
let s:control_sequence = {
      \ "\<LF>" : s:control.newline,
      \ "\<CR>" : s:control.carriage_return,
      \ "\<C-h>" : s:control.delete_backword_char,
      \ "\<Del>" : s:control.ignore,
      \ "\<C-g>" : s:control.bell,
      \ "\<C-o>" : s:control.shift_in,
      \ "\<C-n>" : s:control.shift_out,
      \ "\<C-a>" : s:control.ignore,
      \ "\<C-b>" : s:control.ignore,
      \}
"}}}

" vim: foldmethod=marker