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
"=============================================================================
" FILE: handlers.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#handlers#_on_insert_enter() abort  "{{{
  if &filetype !=# 'unite'
    return
  endif

  let unite = unite#get_current_unite()
  let unite.is_insert = 1

  if unite.prompt_linenr != 0
    return
  endif

  " Restore prompt
  let unite.prompt_linenr = unite.init_prompt_linenr
  let modifiable_save = &l:modifiable
  try
    setlocal modifiable
    call append((unite.context.prompt_direction ==# 'below' ?
                \ '$' : 0), '')
  finally
    let &l:modifiable = modifiable_save
  endtry
  call unite#view#_redraw_prompt()
endfunction"}}}
function! unite#handlers#_on_insert_leave() abort  "{{{
  let unite = unite#get_current_unite()

  if line('.') != unite.prompt_linenr
    call cursor(0, 1)
  endif

  let unite.is_insert = 0
  let unite.context.input = unite#helper#get_input()

  if &filetype ==# 'unite'
    setlocal nomodifiable
  endif
endfunction"}}}
function! unite#handlers#_on_cursor_hold_i() abort  "{{{
  let unite = unite#get_current_unite()

  call unite#view#_change_highlight()

  if unite.redraw_hold_candidates > 0
        \ && unite.max_source_candidates > unite.redraw_hold_candidates
    call s:check_redraw()
  endif

  if unite.is_async && &l:modifiable && !has('timers')
    " Ignore key sequences.
    call feedkeys("a\<BS>", 'n')
  endif
endfunction"}}}
function! unite#handlers#_on_cursor_moved_i() abort  "{{{
  let unite = unite#get_current_unite()
  let prompt_linenr = unite.prompt_linenr

  if unite.redraw_hold_candidates <= 0
        \ || unite.max_source_candidates <= unite.redraw_hold_candidates
    call s:check_redraw()
  endif

  " Prompt check.
  if line('.') == prompt_linenr
        \ && col('.') <= len(unite#get_context().prompt)
    startinsert!
  endif
endfunction"}}}
function! unite#handlers#_on_text_changed() abort  "{{{
  let unite = unite#get_current_unite()
  if unite#helper#get_input(1) !=# unite.last_input
    call s:check_redraw()
  endif
endfunction"}}}
function! unite#handlers#_on_bufwin_enter(bufnr) abort  "{{{
  silent! let unite = getbufvar(a:bufnr, 'unite')
  if type(unite) != type({})
        \ || bufwinnr(a:bufnr) < 1
    return
  endif

  if bufwinnr(a:bufnr) != winnr()
    let winnr = winnr()
    execute bufwinnr(a:bufnr) 'wincmd w'
  endif

  call unite#handlers#_init_timer()
  call unite#handlers#_save_updatetime()

  call s:restore_statusline()

  if unite.context.split && winnr('$') != 1
    call unite#view#_resize_window()
  endif

  setlocal nomodified

  if exists('winnr')
    execute winnr.'wincmd w'
  endif

  call unite#init#_tab_variables()
  let t:unite.last_unite_bufnr = a:bufnr
endfunction"}}}
function! unite#handlers#_on_cursor_hold() abort  "{{{
  let is_async = 0

  call s:restore_statusline()

  if &filetype ==# 'unite'
    " Redraw.
    call unite#redraw()

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

    if !unite.is_async && unite.context.auto_quit
      call unite#force_quit_session()
    endif
  else
    " Search other unite window.
    for winnr in filter(range(1, winnr('$')),
          \ "getbufvar(winbufnr(v:val), '&filetype') ==# 'unite'")
      let unite = getbufvar(winbufnr(winnr), 'unite')
      if unite.is_async
        " Redraw unite buffer.
        call unite#redraw(winnr)

        let is_async = unite.is_async
      endif
    endfor
  endif

  if is_async && !has('timers')
    " Ignore key sequences.
    call feedkeys("g\<ESC>" . (v:count > 0 ? v:count : ''), 'n')
  endif
endfunction"}}}
function! unite#handlers#_on_cursor_moved() abort  "{{{
  if &filetype !=# 'unite'
    return
  endif

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

  let &l:modifiable =
        \ line('.') == prompt_linenr && col('.') >= 1

  if line('.') == 1
    nnoremap <silent><buffer> <Plug>(unite_loop_cursor_up)
          \ :call unite#mappings#loop_cursor_up('n')<CR>
    nnoremap <silent><buffer> <Plug>(unite_skip_cursor_up)
          \ :call unite#mappings#loop_cursor_up('n')<CR>
    inoremap <silent><buffer> <Plug>(unite_select_previous_line)
          \ <ESC>:call unite#mappings#loop_cursor_up('i')<CR>
    inoremap <silent><buffer> <Plug>(unite_skip_previous_line)
          \ <ESC>:call unite#mappings#loop_cursor_up('i')<CR>

    call s:cursor_down()
  elseif line('.') == line('$')
    nnoremap <silent><buffer> <Plug>(unite_loop_cursor_down)
          \ :call unite#mappings#loop_cursor_down('n')<CR>
    nnoremap <silent><buffer> <Plug>(unite_skip_cursor_down)
          \ :call unite#mappings#loop_cursor_down('n')<CR>
    inoremap <silent><buffer> <Plug>(unite_select_next_line)
          \ <ESC>:call unite#mappings#loop_cursor_down('i')<CR>
    inoremap <silent><buffer> <Plug>(unite_skip_next_line)
          \ <ESC>:call unite#mappings#loop_cursor_down('i')<CR>

    call s:cursor_up()
  else
    call s:cursor_up()
    call s:cursor_down()
  endif

  if exists('b:current_syntax')
    call unite#view#_clear_match()

    let is_prompt = (prompt_linenr == 0 &&
          \ (context.prompt_direction == 'below'
          \   && line('.') == line('$') || line('.') == 1))
          \ || line('.') == prompt_linenr
    if is_prompt || mode('.') == 'i' || unite.is_async
          \ || abs(line('.') - unite.prev_line) != 1
          \ || str2float(split(reltimestr(reltime(unite.cursor_line_time)))[0])
          \    > str2float(context.cursor_line_time)
      call unite#view#_set_cursor_line()
    endif

    let unite.cursor_line_time = reltime()
    let unite.prev_line = line('.')
  endif

  if context.auto_preview
    call unite#view#_do_auto_preview()
  endif
  if context.auto_highlight
    call unite#view#_do_auto_highlight()
  endif

  call s:restore_statusline()

  " Check lines. "{{{
  if !context.auto_resize &&
        \ winheight(0) < line('$') && line('.') + winheight(0) < line('$')
    return
  endif

  let height =
        \ (!unite.context.split
        \  || unite.context.winheight == 0) ?
        \ winheight(0) : unite.context.winheight
  let candidates = unite#candidates#_gather_pos(height)
  if !context.auto_resize && empty(candidates)
    " Nothing.
    return
  endif

  call unite#view#_resize_window()

  let modifiable_save = &l:modifiable
  try
    setlocal modifiable
    let pos = getpos('.')
    let lines = unite#view#_convert_lines(candidates)
    silent! call append('$', lines)
  finally
    let &l:modifiable = l:modifiable_save
  endtry

  let context = unite.context
  let unite.current_candidates += candidates

  if pos != getpos('.')
    call setpos('.', pos)
  endif"}}}
endfunction"}}}
function! unite#handlers#_on_buf_unload(bufname) abort  "{{{
  call unite#view#_clear_match()
  call unite#view#_clear_match_highlight()

  " Save unite value.
  silent! let unite = getbufvar(a:bufname, 'unite')
  if type(unite) != type({})
    " Invalid unite.
    return
  endif

  if &l:statusline == unite.statusline
    " Restore statusline.
    let &l:statusline = &g:statusline
  endif

  if unite.is_finalized
    return
  endif

  " Restore options.
  if has_key(unite, 'redrawtime_save')
    let &redrawtime = unite.redrawtime_save
  endif
  let &sidescrolloff = unite.sidescrolloff_save

  call unite#handlers#_restore_updatetime()

  " Call finalize functions.
  call unite#helper#call_hook(unite#loaded_sources_list(), 'on_close')
  let unite.is_finalized = 1
endfunction"}}}
function! unite#handlers#_on_insert_char_pre() abort  "{{{
  let prompt_linenr = unite#get_current_unite().prompt_linenr

  if line('.') == prompt_linenr
    return
  endif

  call cursor(prompt_linenr, 0)
  startinsert!
  call unite#handlers#_on_cursor_moved()
endfunction"}}}

function! unite#handlers#_save_updatetime() abort  "{{{
  let unite = unite#get_current_unite()

  if unite.is_async && unite.context.update_time > 0
        \ && &updatetime > unite.context.update_time
        \ && !has('timers')
    let unite.update_time_save = &updatetime
    let &updatetime = unite.context.update_time
  endif
endfunction"}}}
function! unite#handlers#_restore_updatetime() abort  "{{{
  let unite = unite#get_current_unite()

  if !has_key(unite, 'update_time_save')
    return
  endif

  if unite.context.update_time > 0
        \ && &updatetime < unite.update_time_save
        \ && !has('timers')
    let &updatetime = unite.update_time_save
  endif
endfunction"}}}
function! s:restore_statusline() abort  "{{{
  if &filetype !=# 'unite' || !g:unite_force_overwrite_statusline
    return
  endif

  let unite = unite#get_current_unite()

  if &l:statusline != unite.statusline
    " Restore statusline.
    let &l:statusline = unite.statusline
  endif
endfunction"}}}

function! s:timer_handler(timer) abort "{{{
  if mode() ==# 'i'
    if &filetype ==# 'unite'
      call unite#handlers#_on_cursor_hold_i()
    endif
  else
    call unite#handlers#_on_cursor_hold()
  endif

  if !empty(filter(range(1, winnr('$')),
          \ "getbufvar(winbufnr(v:val), '&filetype') ==# 'unite'"))
          \ || !exists('s:timer')
    return
  endif

  call timer_stop(s:timer)
  unlet s:timer
endfunction"}}}
function! unite#handlers#_init_timer() abort  "{{{
  if has('timers') && !exists('s:timer')
    let s:timer = timer_start(500,
          \ function('s:timer_handler'), {'repeat': -1})
    autocmd plugin-unite VimLeavePre *
          \ if exists('s:timer') | call timer_stop(s:timer) | endif
  endif
endfunction"}}}

function! s:check_redraw() abort "{{{
  let unite = unite#get_current_unite()
  let prompt_linenr = unite.prompt_linenr
  if line('.') == prompt_linenr || unite.context.is_redraw
    " Redraw.
    call unite#redraw()
  endif
endfunction"}}}

function! s:cursor_up() abort "{{{
  nnoremap <expr><buffer> <Plug>(unite_loop_cursor_up)
        \ unite#mappings#cursor_up(0)
  nnoremap <expr><buffer> <Plug>(unite_skip_cursor_up)
        \ unite#mappings#cursor_up(1)
  inoremap <expr><buffer> <Plug>(unite_select_previous_line)
        \ unite#mappings#cursor_up(0)
  inoremap <expr><buffer> <Plug>(unite_skip_previous_line)
        \ unite#mappings#cursor_up(1)
endfunction"}}}
function! s:cursor_down() abort "{{{
  nnoremap <expr><buffer> <Plug>(unite_loop_cursor_down)
        \ unite#mappings#cursor_down(0)
  nnoremap <expr><buffer> <Plug>(unite_skip_cursor_down)
        \ unite#mappings#cursor_down(1)
  inoremap <expr><buffer> <Plug>(unite_select_next_line)
        \ unite#mappings#cursor_down(0)
  inoremap <expr><buffer> <Plug>(unite_skip_next_line)
        \ unite#mappings#cursor_down(1)
endfunction"}}}

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: foldmethod=marker