summaryrefslogtreecommitdiff
path: root/autoload/deoplete/custom.vim
blob: 2b77188596b7ad1a811ba655706b3c0c82d9076a (plain)
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
"=============================================================================
" FILE: custom.vim
" AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
" License: MIT license
"=============================================================================

function! deoplete#custom#_init() abort
  let s:custom = {}
  let s:custom.source = {}
  let s:custom.source._ = {}
  let s:custom.option = deoplete#init#_option()
endfunction
function! deoplete#custom#_init_buffer() abort
  let b:custom = {}
  let b:custom.option = {}
  let b:custom.source_vars = {}
endfunction

function! deoplete#custom#_get() abort
  if !exists('s:custom')
    call deoplete#custom#_init()
  endif

  return s:custom
endfunction
function! deoplete#custom#_get_buffer() abort
  if !exists('b:custom')
    call deoplete#custom#_init_buffer()
  endif

  return b:custom
endfunction

function! deoplete#custom#_get_source(source_name) abort
  let custom = deoplete#custom#_get().source

  if !has_key(custom, a:source_name)
    let custom[a:source_name] = {}
  endif

  return custom[a:source_name]
endfunction
function! deoplete#custom#_get_option(name) abort
  if has_key(deoplete#custom#_get_buffer().option, a:name)
    return deoplete#custom#_get_buffer().option[a:name]
  endif
  return deoplete#custom#_get().option[a:name]
endfunction
function! deoplete#custom#_get_filetype_option(name, filetype, default) abort
  let buffer_option = deoplete#custom#_get_buffer().option
  if has_key(buffer_option, a:name)
    " Use buffer_option instead
    return buffer_option[a:name]
  endif

  let option = deoplete#custom#_get_option(a:name)
  let filetype = has_key(option, a:filetype) ? a:filetype : '_'
  return get(option, filetype, a:default)
endfunction
function! deoplete#custom#_get_source_vars(name) abort
  let global_vars = get(deoplete#custom#_get_source(a:name), 'vars', {})
  let buffer_vars = get(deoplete#custom#_get_buffer().source_vars,
        \ a:name, {})
  return extend(copy(global_vars), buffer_vars)
endfunction

function! deoplete#custom#source(source_name, name_or_dict, ...) abort
  for key in deoplete#util#split(a:source_name)
    let custom_source = deoplete#custom#_get_source(key)
    call s:set_custom(custom_source, a:name_or_dict, get(a:000, 0, ''))
  endfor
endfunction

function! deoplete#custom#var(source_name, name_or_dict, ...) abort
  for key in deoplete#util#split(a:source_name)
    let custom_source = deoplete#custom#_get_source(key)
    let vars = get(custom_source, 'vars', {})
    call s:set_custom(vars, a:name_or_dict, get(a:000, 0, ''))
    call deoplete#custom#source(key, 'vars', vars)
  endfor
endfunction
function! deoplete#custom#buffer_var(source_name, name_or_dict, ...) abort
  let custom = deoplete#custom#_get_buffer().source_vars
  for key in deoplete#util#split(a:source_name)
    if !has_key(custom, key)
      let custom[key] = {}
    endif
    let vars = custom[key]
    call s:set_custom(vars, a:name_or_dict, get(a:000, 0, ''))
  endfor
endfunction

function! deoplete#custom#option(name_or_dict, ...) abort
  let custom = deoplete#custom#_get().option
  call s:set_custom(custom, a:name_or_dict, get(a:000, 0, ''))
endfunction
function! deoplete#custom#buffer_option(name_or_dict, ...) abort
  let custom = deoplete#custom#_get_buffer().option
  call s:set_custom(custom, a:name_or_dict, get(a:000, 0, ''))
endfunction

function! s:set_custom(dest, name_or_dict, value) abort
  if type(a:name_or_dict) == v:t_dict
    call extend(a:dest, a:name_or_dict)
  else
    call s:set_value(a:dest, a:name_or_dict, a:value)
  endif
endfunction
function! s:set_value(dest, name, value) abort
  if type(a:value) == v:t_dict && !empty(a:value)
    if !has_key(a:dest, a:name)
      let a:dest[a:name] = {}
    endif
    call extend(a:dest[a:name], a:value)
  else
    let a:dest[a:name] = a:value
  endif
endfunction