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
"=============================================================================
" FILE: candidates.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#candidates#_recache(input, is_force) abort "{{{
  let unite = unite#get_current_unite()

  " Save options.
  let ignorecase_save = &ignorecase

  let context = unite.context

  try
    if context.smartcase
      let &ignorecase = a:input !~ '\u'
    else
      let &ignorecase = context.ignorecase
    endif

    let context.is_redraw = a:is_force || context.is_redraw
    let context.is_changed = a:input !=# unite.last_input
          \ || context.path !=# unite.last_path

    if empty(unite.args)
      if a:input !~ '^.\{-}\%(\\\@<!\s\)\+'
        " Use interactive source.
        let sources = unite#init#_loaded_sources(['interactive'], context)
      else
        " Use specified source.
        let args = unite#helper#parse_options_args(
              \ matchstr(a:input, '^.\{-}\%(\\\@<!\s\)\+'))[0]
        try
          " Ignore source name
          let context.input = matchstr(context.input,
                \ '^.\{-}\%(\\\@<!\s\)\+\zs.*')

          let sources = unite#init#_loaded_sources(args, context)
        catch
          let sources = []
        finally
          let context.input = a:input
        endtry
      endif

      if get(unite.sources, 0, {'name' : ''}).name
            \   !=# get(sources, 0, {'name' : ''}).name
        " Finalize previous sources.
        call unite#helper#call_hook(unite.sources, 'on_close')

        let unite.sources = sources
        let unite.source_names = unite#helper#get_source_names(sources)

        let prev_winnr = winnr()
        try
          execute bufwinnr(unite.prev_bufnr).'wincmd w'

          " Initialize.
          call unite#helper#call_hook(sources, 'on_init')
        finally
          if winnr() != prev_winnr
            execute prev_winnr . 'wincmd w'
          endif
        endtry

        if &filetype ==# 'unite'
          call unite#view#_set_syntax()
        endif
      endif
    endif

    for source in unite.sources
      let source.unite__candidates = []
    endfor

    let inputs = unite#helper#get_substitute_input(a:input)
    let context.is_list_input = len(inputs) > 1
    for input in inputs
      let context.input = input
      call s:recache_candidates_loop(context, a:is_force)
    endfor

    " Restore prompt input
    let context.input = a:input

    let filtered_count = 0

    for source in unite.sources
      let source.unite__is_invalidate = 0

      if !context.unite__not_buffer && source.max_candidates != 0
            \ && context.unite__is_interactive
            \ && !unite.disabled_max_candidates
            \ && len(source.unite__candidates) > source.max_candidates
        " Filtering too many candidates.
        let source.unite__candidates =
              \ source.unite__candidates[: source.max_candidates - 1]

        if context.verbose && filtered_count < &cmdheight
          echohl WarningMsg | echomsg printf(
                \ '[%s] Filtering too many candidates.', source.name)
                \ | echohl None
          let filtered_count += 1
        endif
      endif

      " Call post_filter hook.
      let source.unite__context.candidates =
            \ source.unite__candidates
      call unite#helper#call_hook([source], 'on_post_filter')

      let source.unite__candidates =
            \ unite#init#_candidates_source(
            \   source.unite__context.candidates, source.name)
      let source.unite__len_candidates = len(source.unite__candidates)
    endfor

    " Update async state.
    let unite.is_async =
          \ len(filter(copy(unite.sources),
          \           'v:val.unite__context.is_async')) > 0
  finally
    let &ignorecase = ignorecase_save
    let context.is_redraw = 0
  endtry

  call unite#handlers#_save_updatetime()
endfunction"}}}

function! unite#candidates#gather(...) abort "{{{
  let is_gather_all = get(a:000, 0, 0)

  let unite = unite#get_current_unite()
  let unite.candidates = []
  for source in unite.sources
    let unite.candidates += source.unite__candidates
  endfor

  if unite.context.prompt_direction ==# 'below'
    let unite.candidates = reverse(unite.candidates)
  endif

  if unite.context.unique
    " Uniq filter.
    let unite.candidates = unite#util#uniq_by(unite.candidates,
          \ "string(v:val.kind) . ' ' . v:val.word")
  endif

  if is_gather_all || unite.context.prompt_direction ==# 'below'
        \ || unite.context.quick_match
    let unite.candidates_pos = len(unite.candidates)
  elseif unite.context.is_redraw || unite.candidates_pos == 0
    let unite.candidates_pos = line('.') + winheight(0)
  endif

  let candidates = unite#init#_candidates(
        \ unite.candidates[: unite.candidates_pos-1])

  if empty(candidates) && unite.prompt_linenr == 0
    let unite.prompt_linenr = 1
  endif

  let unite.context.unite__max_candidates = 0
  let unite.context.input_list =
        \ split(unite.context.input, '\\\@<! ', 1)

  let unite.candidates_len = len(candidates) +
        \ len(unite.candidates[unite.candidates_pos :])

  if unite.context.prompt_direction ==# 'below'
    if unite.prompt_linenr == 0
      let unite.init_prompt_linenr = unite.candidates_len + 1
    else
      let unite.prompt_linenr = unite.candidates_len
      if unite.prompt_linenr == 0
        let unite.prompt_linenr = 1
      endif
    endif
  endif

  return candidates
endfunction"}}}

function! unite#candidates#_gather_pos(offset) abort "{{{
  let unite = unite#get_current_unite()
  if unite.context.is_redraw || unite.candidates_pos == 0
    return []
  endif

  let unite = unite#get_current_unite()
  let candidates = unite.candidates[unite.candidates_pos :
        \ unite.candidates_pos + a:offset - 1]

  let unite.candidates_pos += len(candidates)

  return unite#init#_candidates(candidates)
endfunction"}}}

function! s:recache_candidates_loop(context, is_force) abort "{{{
  let unite = unite#get_current_unite()

  let input_len = unite#util#strchars(a:context.input)

  let candidate_sources = []
  let unite.max_source_candidates = 0
  for source in unite.sources
    " Set context.
    let context = source.unite__context
    let context.input = a:context.input
    let context.path = a:context.path
    let context.source_name = source.name

    " Check required pattern length.
    if input_len < source.required_pattern_length
          \ && !context.unite__is_vimfiler
      continue
    endif

    if source.required_pattern_length > 0
          \ && !source.is_forced
      " Forced redraw.
      let context.is_redraw = 1
      let source.is_forced = 1
    else
      let context.is_redraw = a:context.is_redraw
    endif
    let context.is_changed = a:context.is_changed
    let context.is_invalidate = source.unite__is_invalidate
    let context.is_list_input = a:context.is_list_input
    let context.input_list =
          \ unite#helper#get_input_list(context.input)
    let context.unite__max_candidates =
          \ (unite.disabled_max_candidates ? 0 : source.max_candidates)
    if context.unite__is_vimfiler
      " Disable ignore feature.
      let source.ignore_pattern = ''
      let source.ignore_globs = []
    endif

    " let start = reltime()
    let source_candidates = s:get_source_candidates(source)
    " echomsg string(reltimestr(reltime(start)))

    " Call pre_filter hook.
    let context.candidates = source_candidates
    call unite#helper#call_hook([source], 'on_pre_filter')

    " Restore current filters.
    if empty(unite.current_matchers)
      let unite.current_matchers = unite#util#convert2list(
            \ unite#custom#get_profile(unite.profile_name, 'matchers'))
    endif
    if empty(unite.current_sorters)
      let unite.current_sorters = unite#util#convert2list(
            \ unite#custom#get_profile(unite.profile_name, 'sorters'))
    endif
    if empty(unite.current_converters)
      let unite.current_converters = unite#util#convert2list(
            \ unite#custom#get_profile(unite.profile_name, 'converters'))
    endif

    " Set filters.
    let matchers = !empty(unite.current_matchers) ?
          \ unite.current_matchers : source.matchers
    let sorters = !empty(unite.current_sorters) ?
          \ unite.current_sorters : source.sorters
    let converters = !empty(unite.current_converters) ?
          \ unite.current_converters : source.converters
    if sorters ==# ['sorter_nothing']
          \ || unite.context.unite__is_vimfiler
      let sorters = []
    endif

    let context.unite__is_sort_nothing =
          \ empty(sorters) && context.unite__is_interactive
    let source.unite__orig_len_candidates = len(source_candidates)
    let unite.max_source_candidates +=
          \ (context.unite__is_sort_nothing
          \    && context.unite__max_candidates > 0) ?
          \ source.max_candidates : source.unite__orig_len_candidates

    if !unite.context.unite__is_vimfiler
      " Call filters.
      let source_candidates = unite#helper#call_source_filters(
            \ matchers + sorters + converters,
            \ source_candidates, context, source)
      if context.unite__max_candidates > 0
        let source_candidates = source_candidates[:
              \ context.unite__max_candidates - 1]
      endif
    endif

    " Get execute_command.
    let a:context.execute_command = context.execute_command

    let source.unite__candidates += source_candidates
    if !empty(source_candidates)
      call add(candidate_sources,
            \ unite#helper#convert_source_name(source.name))
    endif
  endfor

  if !a:context.hide_source_names
    let unite.max_source_name =
          \ max(map(candidate_sources, 'len(v:val)')) + 1
  endif
endfunction"}}}

function! s:get_source_candidates(source) abort "{{{
  let custom_source = get(unite#custom#get().sources, a:source.name, {})
  let context_ignore = {
        \ 'path' : a:source.unite__context.path,
        \ 'ignore_pattern' : get(custom_source,
        \    'ignore_pattern', a:source.ignore_pattern),
        \ 'ignore_globs' : get(custom_source,
        \    'ignore_globs', a:source.ignore_globs),
        \ 'white_globs' : get(custom_source,
        \    'white_globs', a:source.white_globs),
        \ }
  let context = extend(a:source.unite__context, context_ignore)

  let funcname = 's:get_source_candidates()'
  try
    if context.unite__is_vimfiler
      if context.vimfiler__is_dummy
        let funcname = 'vimfiler_dummy_candidates'
        return has_key(a:source, 'vimfiler_dummy_candidates') ?
              \ copy(a:source.vimfiler_dummy_candidates(
              \           a:source.args, a:source.unite__context)) : []
      else
        let funcname = 'vimfiler_gather_candidates'
        return has_key(a:source, 'vimfiler_gather_candidates') ?
              \ copy(a:source.vimfiler_gather_candidates(
              \           a:source.args, a:source.unite__context)) : []
      endif
    endif

    " Recaching.
    if (context.is_redraw || a:source.unite__is_invalidate)
          \ && (!has_key(a:source, 'async_gather_candidates')
          \     || has_key(a:source, 'gather_candidates'))
      " Note: If the source has not gather_candidates, the recaching is
      " disabled.

      let a:source.unite__cached_candidates = []

      let funcname = 'gather_candidates'
      if has_key(a:source, 'gather_candidates')
        let a:source.unite__cached_candidates +=
              \ unite#helper#ignore_candidates(copy(
              \  a:source.gather_candidates(a:source.args,
              \  a:source.unite__context)), context_ignore)
      endif
    endif

    if has_key(a:source, 'change_candidates')
          \ && (context.is_redraw || context.is_changed
          \     || a:source.unite__is_invalidate)
      " Recaching.
      let funcname = 'change_candidates'
      let a:source.unite__cached_change_candidates =
            \ unite#helper#ignore_candidates(a:source.change_candidates(
            \     a:source.args, a:source.unite__context), context_ignore)
    endif

    if a:source.unite__context.is_async
      " Get asynchronous candidates.
      let funcname = 'async_gather_candidates'
      while 1
        let a:source.unite__cached_candidates +=
              \ unite#helper#ignore_candidates(
              \  a:source.async_gather_candidates(a:source.args, context),
              \  context_ignore)

        if (!context.sync && context.unite__is_interactive)
              \ || !a:source.unite__context.is_async
          break
        endif
      endwhile
    endif
  catch
    call unite#print_error(v:throwpoint)
    call unite#print_error(v:exception)
    call unite#print_error(
          \ 'Error occurred in ' . funcname . '!')
    call unite#print_error(
          \ 'Source name is ' . a:source.name)

    return []
  endtry

  return a:source.unite__cached_candidates
        \ + a:source.unite__cached_change_candidates
endfunction"}}}

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: foldmethod=marker