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
"=============================================================================
" FILE: view.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.
" }}}
"=============================================================================

let s:save_cpo = &cpo
set cpo&vim

function! unite#view#_redraw_prompt() abort "{{{
  let unite = unite#get_current_unite()
  if unite.prompt_linenr < 0
    return
  endif

  let modifiable_save = &l:modifiable
  try
    setlocal modifiable
    silent! call setline(unite.prompt_linenr,
          \ unite.context.prompt . unite.context.input)

    silent! syntax clear uniteInputLine
    silent! syntax clear uniteInputPrompt
    execute 'syntax match uniteInputLine'
          \ '/\%'.unite.prompt_linenr.'l.*/'
          \ 'contains=uniteInputCommand,uniteInputPrompt'
    execute 'syntax match uniteInputPrompt'
          \ '/\%'.unite.prompt_linenr.'l.*\%'.len(unite.context.prompt).'c/'
  finally
    let &l:modifiable = modifiable_save
  endtry
endfunction"}}}
function! unite#view#_redraw_candidates(...) abort "{{{
  let is_gather_all = get(a:000, 0, 0)

  call unite#view#_resize_window()

  let unite = unite#get_current_unite()
  let context = unite.context

  let candidates = unite#candidates#gather(is_gather_all)
  if context.prompt_direction ==# 'below'
    let unite.init_prompt_linenr = len(candidates) + 1
  endif

  let pos = getpos('.')
  let modifiable_save = &l:modifiable
  try
    setlocal modifiable

    if context.prompt_direction !=# 'below'
      call unite#view#_redraw_prompt()
    endif

    call unite#view#_set_candidates_lines(
          \ unite#view#_convert_lines(candidates))

    if context.prompt_direction ==# 'below' && unite.prompt_linenr != 0
      if empty(candidates)
        let unite.prompt_linenr = 1
      else
        call append(unite.prompt_linenr, '')
        let unite.prompt_linenr += 1
      endif
      call unite#view#_redraw_prompt()
    endif

    let unite.current_candidates = candidates
  finally
    let &l:modifiable = l:modifiable_save
    if pos != getpos('.')
      call setpos('.', pos)
    endif
  endtry

  if context.input == '' && context.log
        \ || context.prompt_direction ==# 'below'
    " Move to bottom.
    call cursor(line('$'), 0)
  endif

  if context.prompt_direction ==# 'below' && mode() ==# 'i'
    call unite#view#_bottom_cursor()
  endif

  " Set syntax.
  call s:set_syntax()
endfunction"}}}
function! unite#view#_redraw_line(...) abort "{{{
  let prompt_linenr = unite#get_current_unite().prompt_linenr
  let linenr = a:0 > 0 ? a:1 : line('.')
  if linenr ==# prompt_linenr
    let linenr += 1
  endif

  if &filetype !=# 'unite'
    " Ignore.
    return
  endif

  let modifiable_save = &l:modifiable
  setlocal modifiable

  let candidate = unite#helper#get_current_candidate(linenr)
  call setline(linenr, unite#view#_convert_lines([candidate])[0])

  let &l:modifiable = modifiable_save
endfunction"}}}
function! unite#view#_quick_match_redraw(quick_match_table, is_define) abort "{{{
  for [key, number] in items(a:quick_match_table)
    if a:is_define
      execute printf(
            \ 'silent! sign define unite_quick_match_%d text=%s texthl=uniteQuickMatchText',
            \ number, key)
      execute printf(
            \ 'silent! sign place %d name=unite_quick_match_%d line=%d buffer=%d',
            \ 2000 + number, number, number, bufnr('%'))
    else
      execute printf(
            \ 'silent! sign unplace %d buffer=%d',
            \ 2000 + number, bufnr('%'))
    endif
  endfor
endfunction"}}}
function! unite#view#_set_candidates_lines(lines) abort "{{{
  let unite = unite#get_current_unite()
  let modifiable_save = &l:modifiable
  try
    let pos = getpos('.')
    setlocal modifiable

    " Clear candidates
    if unite.context.prompt_direction ==# 'below'
      silent! execute '1,'.(unite.prompt_linenr-1).'$delete _'
      call setline(1, a:lines)
      let start = (unite.prompt_linenr == 0) ?
            \ len(a:lines)+1 : unite.prompt_linenr+1
      silent! execute start.',$delete _'
    else
      silent! execute (unite.prompt_linenr+1).',$delete _'
      call setline(unite.prompt_linenr+1, a:lines)
    endif
  finally
    call setpos('.', pos)
    let &l:modifiable = modifiable_save
  endtry
endfunction"}}}

function! unite#view#_redraw(is_force, winnr, is_gather_all) abort "{{{
  if unite#util#is_cmdwin()
    return
  endif

  let unite_save = unite#variables#current_unite()
  let winnr_save = winnr()
  if a:winnr > 0
    " Set current unite.
    let unite = getbufvar(winbufnr(a:winnr), 'unite')

    execute a:winnr 'wincmd w'
  endif

  let pos = getpos('.')
  let unite = unite#get_current_unite()
  let context = unite.context
  let current_candidate = (line('.') == unite.prompt_linenr) ?
        \ {} : unite#helper#get_current_candidate()

  try
    if &filetype !=# 'unite'
      return
    endif

    if !context.is_redraw
      let context.is_redraw = a:is_force
    endif

    if context.is_redraw
      call unite#clear_message()
    endif

    let input = unite#helper#get_input(1)
    if !context.is_redraw
          \ && input ==# unite.last_input
          \ && context.path ==# unite.last_path
          \ && !unite.is_async
          \ && !context.unite__is_resize
          \ && !a:is_gather_all
      return
    endif

    let is_gather_all = a:is_gather_all || context.log

    if context.is_redraw
          \ || input !=# unite.last_input
          \ || context.path !=# unite.last_path
          \ || unite.is_async
          \ || empty(unite.args)
      " Recaching.
      call unite#candidates#_recache(input, a:is_force)
    endif

    let unite.last_input = input
    let unite.last_path = context.path

    " Redraw.
    call unite#view#_redraw_candidates(is_gather_all)
    call unite#view#_change_highlight()
    let unite.context.is_redraw = 0
  finally
    if empty(unite.args) && getpos('.') !=# pos
      call setpos('.', pos)

      if context.prompt_direction ==# 'below'
        call cursor(line('$'), 0)
        call unite#view#_bottom_cursor()
      endif
    elseif !context.log
      call unite#view#_search_cursor(current_candidate)
    endif

    if a:winnr > 0
      " Restore current unite.
      call unite#set_current_unite(unite_save)
      execute winnr_save 'wincmd w'
    endif
  endtry

  if context.immediately && len(unite.current_candidates) == 1
    " Immediately action.
    call unite#action#do(
          \ context.default_action, [unite.current_candidates[0]])

    " Note: It is workaround
    stopinsert
  endif
  if context.auto_preview
    call unite#view#_do_auto_preview()
  endif
  if context.auto_highlight
    call unite#view#_do_auto_highlight()
  endif
endfunction"}}}
function! unite#view#_redraw_all_candidates() abort "{{{
  let unite = unite#get_current_unite()
  if len(unite.candidates) != len(unite.current_candidates)
    call unite#redraw(0, 1)
  endif
endfunction"}}}

function! unite#view#_set_syntax() abort "{{{
  syntax clear

  syntax match uniteInputCommand /\\\@<! :\S\+/ contained

  let unite = unite#get_current_unite()
  let context = unite.context

  let candidate_icon = unite#util#escape_pattern(
        \ context.candidate_icon)
  execute 'syntax region uniteNonMarkedLine start=/^'.
        \ candidate_icon.'/ end=''$'' keepend'.
        \ ' contains=uniteCandidateIcon,'.
        \ 'uniteCandidateSourceName'
  execute 'syntax match uniteCandidateIcon /^'.
        \ candidate_icon.'/ contained '
        \ . (context.hide_icon ? 'conceal' : '')

  let marked_icon = unite#util#escape_pattern(
        \ context.marked_icon)
  execute 'syntax region uniteMarkedLine start=/^'.
        \ marked_icon.'/ end=''$'' keepend'
        \ ' contains=uniteMarkedIcon'
  execute 'syntax match uniteMarkedIcon /^'.
        \ marked_icon.'/ contained '
        \ . (context.hide_icon ? 'conceal' : '')

  silent! syntax clear uniteCandidateSourceName
  if unite.max_source_name > 0
    execute 'syntax match uniteCandidateSourceName
          \ /\%'.(2+strwidth(unite.context.prompt)).'c[[:alnum:]_\/-]\+/ contained'
  endif

  " Set syntax.
  let syntax = {}
  for source in filter(copy(unite.sources), 'v:val.syntax != ""')
    " Skip previous syntax
    if has_key(syntax, source.name)
      continue
    endif
    let syntax[source.name] = 1

    let name = unite.max_source_name > 0 ?
          \ unite#helper#convert_source_name(source.name) : ''

    execute 'highlight default link'
          \ source.syntax unite.context.abbr_highlight

    execute printf('syntax match %s "^['.
          \ unite.context.candidate_icon.' ]\+%s" '.
          \ 'nextgroup='.source.syntax. ' keepend
          \ contains=uniteCandidateIcon,%s',
          \ 'uniteSourceLine__'.source.syntax,
          \ (name == '' ? '' : name . '\>'),
          \ (name == '' ? '' : 'uniteCandidateSourceName')
          \ )

    call unite#helper#call_hook([source], 'on_syntax')
  endfor

  call s:set_syntax()

  call unite#view#_redraw_prompt()

  let b:current_syntax = 'unite'
endfunction"}}}
function! unite#view#_change_highlight() abort  "{{{
  if &filetype !=# 'unite'
        \ || !exists('b:current_syntax')
    return
  endif

  let unite = unite#get_current_unite()
  if empty(unite)
    return
  endif

  call unite#view#_set_cursor_line()

  let context = unite.context
  if !context.match_input
    return
  endif

  silent! syntax clear uniteCandidateInputKeyword

  syntax case ignore

  for input_str in unite#helper#get_substitute_input(
        \ unite#helper#get_input())
    if input_str == ''
      continue
    endif

    let input_list = unite#helper#get_input_list(input_str)

    for source in filter(copy(unite.sources), "v:val.syntax != ''")
      for matcher in filter(copy(map(filter(
            \ copy(source.matchers),
            \ "type(v:val) == type('')"), 'unite#get_filters(v:val)')),
            \ "has_key(v:val, 'pattern')")
        let patterns = map(copy(input_list),
              \ "escape(matcher.pattern(v:val), '/~')")

        silent! execute 'syntax match uniteCandidateInputKeyword'
              \ '/'.join(patterns, '\|').'/'
              \ 'containedin='.source.syntax.' contained'
      endfor
    endfor
  endfor

  syntax case match
endfunction"}}}

function! unite#view#_resize_window() abort "{{{
  if &filetype !=# 'unite'
    return
  endif

  let context = unite#get_context()
  let unite = unite#get_current_unite()

  if (winheight(0) + &cmdheight + 2 >= &lines
        \ && !context.vertical)
        \ || !context.resize
        \ || !context.split
    " Cannot resize.
    let context.unite__is_resize = 0
    return
  endif

  if context.unite__old_winwidth != 0
        \ && context.unite__old_winheight != 0
        \ && winwidth(0) != context.unite__old_winwidth
        \ && winheight(0) != context.unite__old_winheight
    " Disabled resize.
    let context.winwidth = 0
    let context.winheight = 0
    let context.unite__is_resize = 1
    return
  endif

  if context.auto_resize
    " Auto resize.
    let max_len = unite.candidates_len
    if unite.prompt_linenr > 0
      let max_len += 1
    endif

    let winheight = winheight(0)

    silent! execute 'resize' min([max_len, context.winheight])

    if line('.') == unite.prompt_linenr
          \ || line('$') < winheight
      call unite#view#_bottom_cursor()
    endif

    let context.unite__is_resize = winheight != winheight(0)
  elseif context.vertical
        \ && context.unite__old_winwidth == 0
    execute 'vertical resize' context.winwidth

    let context.unite__is_resize = 1
  elseif !context.vertical
        \ && (context.unite__old_winheight == 0 || context.auto_preview)
    execute 'resize' context.winheight

    let context.unite__is_resize = 1
  else
    let context.unite__is_resize = 0
  endif

  let context.unite__old_winheight = winheight(winnr())
  let context.unite__old_winwidth = winwidth(winnr())
endfunction"}}}

function! unite#view#_do_auto_preview() abort "{{{
  let unite = unite#get_current_unite()

  if unite.preview_candidate == unite#helper#get_current_candidate()
    return
  endif

  let unite.preview_candidate = unite#helper#get_current_candidate()

  call unite#action#do('preview', [], {})

  " Restore window size.
  if s:has_preview_window()
    call unite#view#_resize_window()
  endif
endfunction"}}}
function! unite#view#_do_auto_highlight() abort "{{{
  let unite = unite#get_current_unite()

  if unite.highlight_candidate == unite#helper#get_current_candidate()
    return
  endif
  let unite.highlight_candidate = unite#helper#get_current_candidate()

  call unite#action#do('highlight', [], {})
endfunction"}}}

function! unite#view#_switch_unite_buffer(buffer_name, context) abort "{{{
  " Search unite window.
  let winnr = unite#helper#get_unite_winnr(a:buffer_name)
  if a:context.split && winnr > 0
    silent execute winnr 'wincmd w'
    return
  endif

  " Search unite buffer.
  let bufnr = unite#helper#get_unite_bufnr(a:buffer_name)

  if a:context.split && !a:context.unite__direct_switch
    " Split window.
    noautocmd execute s:get_buffer_direction(a:context) ((bufnr > 0) ?
          \ ((a:context.vertical) ? 'vsplit' : 'split') :
          \ ((a:context.vertical) ? 'vnew' : 'new'))
  endif

  if bufnr > 0
    silent noautocmd execute bufnr 'buffer'
  else
    if bufname('%') == ''
      noautocmd silent enew
    endif
    silent! execute 'noautocmd edit'
          \ fnameescape(a:context.real_buffer_name)
  endif

  call unite#handlers#_on_bufwin_enter(bufnr('%'))
  doautocmd WinEnter
  doautocmd BufWinEnter
endfunction"}}}

function! unite#view#_close(buffer_name) abort  "{{{
  let buffer_name = a:buffer_name

  if buffer_name == ''
    " Use last unite buffer.
    if !exists('t:unite') ||
          \ !bufexists(t:unite.last_unite_bufnr)
      call unite#util#print_error('No unite buffer.')
      return
    endif

    let buffer_name = getbufvar(
          \ t:unite.last_unite_bufnr, 'unite').buffer_name
  endif

  " Search unite window.
  let quit_winnr = unite#helper#get_unite_winnr(buffer_name)

  if buffer_name !~ '@\d\+$'
    " Add postfix.
    let prefix = '[unite] - '
    let prefix .= buffer_name
    let buffer_name .= unite#helper#get_postfix(
          \ prefix, 0, tabpagebuflist(tabpagenr()))
  endif

  if quit_winnr > 0
    " Quit unite buffer.
    silent execute quit_winnr 'wincmd w'
    call unite#force_quit_session()
  endif

  return quit_winnr > 0
endfunction"}}}

function! unite#view#_init_cursor() abort "{{{
  let unite = unite#get_current_unite()
  let context = unite.context

  let positions = unite#custom#get_profile(
        \ unite.profile_name, 'unite__save_pos')
  let position = get(positions, unite#loaded_source_names_string(), {})
  let is_restore = context.restore
        \ && !empty(position) && context.select <= 0
        \ && (context.resume || position.candidate ==#
        \       unite#helper#get_current_candidate(position.pos[1]))

  if context.start_insert && !context.auto_quit
    let unite.is_insert = 1

    if is_restore && position.pos[1] != unite.prompt_linenr
      call s:restore_position(position)
      startinsert
    else
      call unite#helper#cursor_prompt()
      startinsert!
    endif
  else
    let unite.is_insert = 0

    if is_restore
      call s:restore_position(position)
    else
      call unite#helper#cursor_prompt()
    endif

    call cursor(0, 1)
    stopinsert
  endif

  if context.select > 0
    " Select specified candidate.
    call cursor(unite#helper#get_current_candidate_linenr(
          \ context.select), 0)
  elseif context.input == '' && context.log
    call unite#view#_redraw_candidates(1)
  endif

  if !is_restore &&
        \ (line('.') <= winheight(0)
        \ || (context.prompt_direction ==# 'below'
        \     && (line('$') - line('.')) <= winheight(0)))
    call unite#view#_bottom_cursor()
  endif

  if !context.focus
    if winbufnr(winnr('#')) > 0
      wincmd p
    else
      execute bufwinnr(unite.prev_bufnr).'wincmd w'
    endif
  endif

  let unite.prev_line = line('.')
  call unite#view#_set_cursor_line()
  call unite#handlers#_on_cursor_moved()

  if context.quick_match
    call unite#helper#cursor_prompt()
    call unite#view#_bottom_cursor()

    call unite#mappings#_quick_match(0)
  endif
endfunction"}}}

function! unite#view#_quit(is_force, ...) abort  "{{{
  if &filetype !=# 'unite'
    return
  endif

  let is_all = get(a:000, 0, 0)

  " Save unite value.
  let unite_save = unite#variables#current_unite()
  call unite#set_current_unite(b:unite)
  let unite = b:unite
  let context = unite.context

  " Clear mark.
  for source in unite#loaded_sources_list()
    for candidate in source.unite__cached_candidates
      let candidate.unite__is_marked = 0
    endfor
  endfor

  if unite.context.unite__is_manual
    call unite#sources#history_unite#add(unite)
  endif

  call unite#view#_save_position()

  if a:is_force || context.quit
    let bufname = bufname('%')

    if winnr('$') == 1 || !context.split
      call unite#util#alternate_buffer()
    elseif is_all || !context.temporary
      close!
      if unite.winnr != winnr() && unite.winnr <= winnr('$')
        execute unite.winnr . 'wincmd w'
      endif
      call unite#view#_resize_window()
    endif

    call unite#handlers#_on_buf_unload(bufname)

    call unite#view#_close_preview_window()

    if winnr('$') != 1 && winnr('$') == unite.winmax
          \ && !context.temporary
      execute unite.win_rest_cmd
      noautocmd execute unite.prev_winnr 'wincmd w'
    endif
    if context.quit
      call setpos('.', unite.prev_pos)
    endif
  else
    call unite#view#_close_preview_window()

    let winnr = get(filter(range(1, winnr('$')),
          \ "winbufnr(v:val) == unite.prev_bufnr"), 0, unite.prev_winnr)

    if winnr == winnr()
      new
    else
      execute winnr 'wincmd w'
    endif

    let unite.prev_winnr = winnr()

    " Resize window.
    try
      execute bufwinnr(unite.bufnr) 'wincmd w'

      call unite#view#_resize_window()
    finally
      execute unite.prev_winnr 'wincmd w'
    endtry
  endif

  if g:unite_restore_alternate_file
        \ && bufexists(unite.alternate_bufnr)
        \ && bufnr('%') != unite.alternate_bufnr
        \ && unite.alternate_bufnr > 0
    silent! execute 'buffer!' unite.alternate_bufnr
    silent! buffer! #
  endif

  if context.complete
    if context.col < col('$')
      startinsert
    else
      startinsert!
    endif
  else
    redraw
    stopinsert
  endif

  " Restore unite.
  call unite#set_current_unite(unite_save)
endfunction"}}}

function! unite#view#_set_cursor_line() abort "{{{
  if !exists('b:current_syntax') || &filetype !=# 'unite'
    return
  endif

  let unite = unite#get_current_unite()
  let context = unite.context
  if !context.cursor_line
    return
  endif

  let prompt_linenr = unite.prompt_linenr

  call unite#view#_clear_match()

  if line('.') != prompt_linenr
    call unite#view#_match_line(context.cursor_line_highlight,
          \ line('.'), unite.match_id)
  endif
  let unite.cursor_line_time = reltime()
endfunction"}}}

function! unite#view#_bottom_cursor() abort "{{{
  let pos = getpos('.')
  try
    normal! zb
  finally
    call setpos('.', pos)
  endtry
endfunction"}}}
function! unite#view#_clear_match() abort "{{{
  if &filetype ==# 'unite'
    setlocal nocursorline
  endif
endfunction"}}}

function! unite#view#_save_position() abort "{{{
  let unite = b:unite
  let context = unite.context

  let key = unite#loaded_source_names_string()
  if key == ''
    return
  endif

  " Save position.
  let positions = unite#custom#get_profile(
        \ unite.profile_name, 'unite__save_pos')

  let positions[key] = {
        \ 'pos' : getpos('.'),
        \ 'winline' : winline(),
        \ 'candidate' : unite#helper#get_current_candidate(),
        \ }

  if context.input == ''
    return
  endif

  " Save input.
  let inputs = unite#custom#get_profile(
        \ unite.profile_name, 'unite__inputs')
  if !has_key(inputs, key)
    let inputs[key] = []
  endif
  call insert(filter(inputs[key],
        \ 'v:val !=# unite.context.input'), context.input)
endfunction"}}}

" Message output.
function! unite#view#_print_error(message) abort "{{{
  let message = map(s:msg2list(a:message), '"[unite.vim] " . v:val')
  let unite = unite#get_current_unite()
  if !empty(unite)
    let unite.err_msgs += message
  endif
  for mes in message
    echohl WarningMsg | echomsg mes | echohl None
  endfor
endfunction"}}}
function! unite#view#_print_warning(message) abort "{{{
  let message = map(s:msg2list(a:message), '"[unite.vim] " . v:val')
  for mes in message
    echohl WarningMsg | echon mes | echohl None
  endfor
endfunction"}}}
function! unite#view#_print_source_error(message, source_name) abort "{{{
  call unite#view#_print_error(
        \ map(copy(s:msg2list(a:message)),
        \   "printf('[%s] %s', a:source_name, v:val)"))
endfunction"}}}
function! unite#view#_print_message(message, ...) abort "{{{
  let context = unite#get_context()
  let unite = unite#get_current_unite()
  let message = s:msg2list(a:message)
  let is_silent = get(a:000, 0, get(context, 'silent', 0))
  if !empty(unite)
    let unite.msgs += message
  endif

  if !is_silent
    echohl Comment | call unite#view#_redraw_echo(message[: &cmdheight-1]) | echohl None
  endif
endfunction"}}}
function! unite#view#_print_source_message(message, source_name) abort "{{{
  call unite#view#_print_message(
        \ map(copy(s:msg2list(a:message)),
        \    "printf('[%s] %s', a:source_name, v:val)"))
endfunction"}}}
function! unite#view#_add_source_message(message, source_name) abort "{{{
  call unite#view#_print_message(
        \ map(copy(s:msg2list(a:message)),
        \    "printf('[%s] %s', a:source_name, v:val)"), 1)
endfunction"}}}
function! unite#view#_clear_message() abort "{{{
  let unite = unite#get_current_unite()
  let unite.msgs = []
  redraw
endfunction"}}}
function! unite#view#_redraw_echo(expr) abort "{{{
  if has('vim_starting')
    echo join(s:msg2list(a:expr), "\n")
    return
  endif

  let more_save = &more
  let showcmd_save = &showcmd
  let ruler_save = &ruler
  try
    set nomore
    set noshowcmd
    set noruler

    let msg = map(s:msg2list(a:expr), "unite#util#truncate_smart(
          \ v:val, &columns - 1 + len(v:val) - strdisplaywidth(v:val),
          \ &columns/2, '...')")
    let height = max([1, &cmdheight])
    for i in range(0, len(msg)-1, height)
      redraw
      echo join(msg[i : i+height-1], "\n")
    endfor
  finally
    let &more = more_save
    let &showcmd = showcmd_save
    let &ruler = ruler_save
  endtry
endfunction"}}}

function! unite#view#_match_line(highlight, line, id) abort "{{{
  if &filetype ==# 'unite'
    setlocal cursorline
    return
  endif

  " For compatibility
  return exists('*matchaddpos') ?
        \ matchaddpos(a:highlight, [a:line], 10, a:id) :
        \ matchadd(a:highlight, '^\%'.a:line.'l.*', 10, a:id)
endfunction"}}}
function! unite#view#_clear_match_highlight() abort "{{{
  silent! call matchdelete(10)
endfunction"}}}

function! unite#view#_get_status_plane_string() abort "{{{
  return (b:unite.is_async ? '[async] ' : '') .
        \ join(map(copy(unite#loaded_sources_list()), "
        \ (v:val.unite__len_candidates == 0) ? '_' :
        \ join(insert(filter(copy(v:val.args),
        \  'type(v:val) <= 1'),
        \   unite#helper#convert_source_name(v:val.name)), ':')
        \ . (v:val.unite__len_candidates == 0 ? '' :
        \      v:val.unite__orig_len_candidates ==
        \            v:val.unite__len_candidates ?
        \            '(' .  v:val.unite__len_candidates . ')' :
        \      printf('(%s/%s)', v:val.unite__len_candidates,
        \      v:val.unite__orig_len_candidates))
        \ "))
endfunction"}}}

function! unite#view#_get_status_head_string() abort "{{{
  if !exists('b:unite')
    return ''
  endif

  return b:unite.is_async ? '[async] ' : ''
endfunction"}}}
function! unite#view#_get_status_tail_string() abort "{{{
  if !exists('b:unite')
    return ''
  endif

  return b:unite.context.path != '' ? '['. b:unite.context.path.']' :
        \    (get(b:unite.msgs, 0, '') == '') ? '' :
        \    substitute(get(b:unite.msgs, 0, ''), '^\[.\{-}\]\s*', '', '')
endfunction"}}}

function! unite#view#_get_source_name_string(source) abort "{{{
  return (a:source.unite__orig_len_candidates == 0) ? '_' :
        \ join(insert(filter(copy(a:source.args),
        \  'type(v:val) <= 1'),
        \   unite#helper#convert_source_name(a:source.name)), ':')
endfunction"}}}
function! unite#view#_get_source_candidates_string(source) abort "{{{
  return a:source.unite__orig_len_candidates == 0 ? '' :
        \      a:source.unite__orig_len_candidates ==
        \            a:source.unite__len_candidates ?
        \            '(' . a:source.unite__len_candidates . ')' :
        \      printf('(%s/%s)', a:source.unite__len_candidates,
        \      a:source.unite__orig_len_candidates)
endfunction"}}}

function! unite#view#_get_status_string(unite) abort "{{{
  let statusline = "%#uniteStatusHead# %{unite#view#_get_status_head_string()}%*"
  let cnt = 0
  if empty(a:unite.sources)
    let statusline .= "%#uniteStatusSourceNames#interactive%*"
    let statusline .= "%#uniteStatusSourceCandidates#%{"
    let statusline .= "unite#view#_get_source_candidates_string("
    let statusline .= "unite#loaded_sources_list()[0])} %*"
  else
    for cnt in range(0, len(a:unite.sources)-1)
      let statusline .= "%#uniteStatusSourceNames#%{"
      let statusline .= "unite#view#_get_source_name_string("
      let statusline .= "b:unite.sources[".cnt."])}"
      let statusline .= "%#uniteStatusSourceCandidates#%{"
      let statusline .= "unite#view#_get_source_candidates_string("
      let statusline .= "b:unite.sources[".cnt."])} %*"
    endfor
  endif

  let statusline .= "%=%#uniteStatusMessage# %{unite#view#_get_status_tail_string()} %*"
  let statusline .= "%#uniteStatusLineNR#%{printf('%'.len(b:unite.candidates_len"
  let statusline .= "+b:unite.prompt_linenr).'d/%d',line('.'),"
  let statusline .= "b:unite.candidates_len+b:unite.prompt_linenr)}%*"
  return statusline
endfunction"}}}

function! unite#view#_add_previewed_buffer_list(bufnr) abort "{{{
  call s:clear_previewed_buffer_list()

  let unite = unite#get_current_unite()
  call add(unite.previewed_buffer_list, a:bufnr)
endfunction"}}}
function! unite#view#_remove_previewed_buffer_list(bufnr) abort "{{{
  let unite = unite#get_current_unite()
  call filter(unite.previewed_buffer_list, 'v:val != a:bufnr')
endfunction"}}}

function! unite#view#_preview_file(filename) abort "{{{
  let context = unite#get_context()
  if context.vertical_preview
    let unite_winwidth = winwidth(0)
    silent execute 'vertical pedit!' fnameescape(a:filename)
    wincmd P
    let target_winwidth = (unite_winwidth + winwidth(0)) / 2
    execute 'wincmd p | vert resize ' . target_winwidth
  else
    let previewheight_save = &previewheight
    try
      let &previewheight = context.previewheight
      silent execute 'pedit!' fnameescape(a:filename)
    finally
      let &previewheight = previewheight_save
    endtry
  endif
endfunction"}}}

function! unite#view#_close_preview_window() abort "{{{
  let unite = unite#get_current_unite()

  if !unite.has_preview_window
    let preview_windows = filter(range(1, winnr('$')),
          \ 'getwinvar(v:val, "&previewwindow") != 0')
    if !empty(preview_windows)
      " Close preview window.
      noautocmd pclose!
    endif
  endif

  call s:clear_previewed_buffer_list()

  let unite.preview_candidate = {}
endfunction"}}}
function! s:clear_previewed_buffer_list() abort "{{{
  let unite = unite#get_current_unite()

  " Clear previewed buffer list
  for bufnr in unite.previewed_buffer_list
    if buflisted(bufnr)
      if bufnr == bufnr('%')
        call unite#util#alternate_buffer()
      endif
      silent! execute 'bdelete!' bufnr
    endif
  endfor

  let unite.previewed_buffer_list = []
endfunction"}}}

" @vimlint(EVL102, 1, l:max_source_name)
" @vimlint(EVL102, 1, l:context)
" @vimlint(EVL102, 1, l:padding)
function! unite#view#_convert_lines(candidates) abort "{{{
  let unite = unite#get_current_unite()
  let context = unite#get_context()
  let [max_width, max_source_name] = unite#helper#adjustments(
        \ winwidth(0), unite.max_source_name, 4)

  let padding_width = strwidth(context.prompt)
  if !unite.context.hide_icon
    let padding_width -= strwidth(context.candidate_icon)
  endif
  let padding = repeat(' ', padding_width)

  let truncate_width = (max_width*context.truncate_width) / 100

  return map(copy(a:candidates),
        \ "(v:val.is_dummy ? ' ' :
        \   v:val.unite__is_marked ? context.marked_icon :
        \   context.candidate_icon) . padding
        \ . (unite.max_source_name == 0 ? ''
        \   : unite#util#truncate(unite#helper#convert_source_name(
        \     (v:val.is_dummy ? '' : v:val.source)), max_source_name))
        \ . ((strwidth(v:val.unite__abbr) < max_width || !context.truncate) ?
        \     v:val.unite__abbr
        \   : unite#util#truncate_wrap(v:val.unite__abbr, max_width
        \    , truncate_width, '..'))")
endfunction"}}}
" @vimlint(EVL102, 0, l:max_source_name)
" @vimlint(EVL102, 0, l:context)
" @vimlint(EVL102, 0, l:padding)

function! unite#view#_search_cursor(candidate) abort "{{{
  " Optimized
  if empty(a:candidate) ||
        \ unite#helper#get_current_candidate() ==# a:candidate
    return
  endif

  call unite#view#_redraw_all_candidates()

  let max = line('$')
  let cnt = 1
  while cnt <= max
    let candidate = unite#helper#get_current_candidate(cnt)

    if candidate ==# a:candidate
      " Move cursor.
      call cursor(cnt, 0)
      return
    endif

    let cnt += 1
  endwhile
endfunction"}}}

function! s:set_syntax() abort "{{{
  let unite = unite#get_current_unite()

  " Set syntax.
  for source in filter(copy(unite.sources), 'v:val.syntax != ""')
    silent! execute 'syntax clear' source.syntax
    execute 'syntax region' source.syntax
          \ 'start=// end=/$/ keepend contained'
  endfor

  call unite#view#_change_highlight()
endfunction"}}}

function! s:has_preview_window() abort "{{{
  return len(filter(range(1, winnr('$')),
        \    'getwinvar(v:val, "&previewwindow")')) > 0
endfunction"}}}

function! s:msg2list(expr) abort "{{{
  return type(a:expr) ==# type([]) ? a:expr : split(a:expr, '\n')
endfunction"}}}

function! s:get_buffer_direction(context) abort "{{{
  let direction = a:context.direction
  if direction ==# 'dynamictop' || direction ==# 'dynamicbottom'
    " Use dynamic direction calculation
    let unite = unite#get_current_unite()
    let [max_width, _] = unite#helper#adjustments(
          \ winwidth(0), unite.max_source_name, 4)
    let is_fit = empty(filter(copy(unite#candidates#gather()),
          \ 'strwidth(v:val.unite__abbr) > max_width'))

    if direction ==# 'dynamictop'
      let direction = is_fit ? 'aboveleft' : 'topleft'
    else
      let direction = is_fit ? 'belowright' : 'botright'
    endif
  endif
  return direction
endfunction"}}}

function! s:restore_position(position) abort "{{{
  call setpos('.', a:position.pos)
  if winline() < a:position.winline
    execute 'normal!' (a:position.winline - winline())."\<C-y>"
  elseif winline() > a:position.winline
    execute 'normal!' (winline() - a:position.winline)."\<C-e>"
  endif
endfunction"}}}

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: foldmethod=marker