8fc0a8007b7b1a2d479054fd3b8bfecd58e5a453
[stack/conf/vim.git] / addons / TagHighlight / autoload / TagHighlight / Option.vim
1 " Tag Highlighter:
2 "   Author:  A. S. Budden <abudden _at_ gmail _dot_ com>
3 " Copyright: Copyright (C) 2009-2011 A. S. Budden
4 "            Permission is hereby granted to use and distribute this code,
5 "            with or without modifications, provided that this copyright
6 "            notice is copied with it. Like anything else that's free,
7 "            the TagHighlight plugin is provided *as is* and comes with no
8 "            warranty of any kind, either expressed or implied. By using
9 "            this plugin, you agree that in no event will the copyright
10 "            holder be liable for any damages resulting from the use
11 "            of this software.
12
13 " ---------------------------------------------------------------------
14 try
15         if &cp || v:version < 700 || (exists('g:loaded_TagHLOption') && (g:plugin_development_mode != 1))
16                 throw "Already loaded"
17         endif
18 catch
19         finish
20 endtry
21 let g:loaded_TagHLOption = 1
22
23 let s:log_defaults = 1
24 let g:TagHighlightOptionDefaults = {}
25
26 function! TagHighlight#Option#LoadOptionFileIfPresent()
27         let option_file = TagHighlight#Find#LocateFile('CONFIG', '')
28
29         " Check whether we've found the option file
30         if ! option_file['Exists']
31                 call TagHLDebug("No project config file", "Information")
32                 return
33         endif
34
35         " Got an option file, load it in:
36         let b:TagHighlightConfigFileOptions = TagHighlight#LoadDataFile#LoadFile(option_file['FullPath'])
37
38         return option_file
39 endfunction
40
41 function! TagHighlight#Option#LoadOptions()
42         if has_key(g:TagHighlightPrivate, 'PluginOptions')
43                 return
44         endif
45
46         let g:TagHighlightPrivate['PluginOptions'] = []
47         let options = TagHighlight#LoadDataFile#LoadDataFile('options.txt')
48
49         for option_dest in keys(options)
50                 if has_key(options[option_dest], 'VimOptionMap')
51                         let option = deepcopy(options[option_dest])
52                         let option['Destination'] = option_dest
53                         let g:TagHighlightPrivate['PluginOptions'] += [option]
54                 endif
55         endfor
56 endfunction
57
58 function! TagHighlight#Option#GetOption(name)
59         " Check we've loaded the options
60         call TagHighlight#Option#LoadOptions()
61
62         " Check this option exists
63         let found = 0
64         for option in g:TagHighlightPrivate['PluginOptions']
65                 if option['VimOptionMap'] == a:name
66                         let found = 1
67                         break
68                 endif
69         endfor
70         if ! found
71                 throw "Unrecognised option:" .a:name
72         endif
73
74         " Option priority (highest first):
75         " * buffer dictionary,
76         " * config file dictionary
77         " * global dictionary,
78         for var in ["g:TagHighlightSettings","b:TagHighlightConfigFileOptions","b:TagHighlightSettings"]
79                 if exists(var)
80                         exe 'let present = has_key(' . var . ', a:name)'
81                         if present
82                                 exe 'let opt = ' . var . '[a:name]'
83                         endif
84                 endif
85         endfor
86
87         if ! exists('opt')
88                 " We haven't found it, return the default
89                 " Special cases first
90                 if a:name == "DefaultDirModePriority"
91                         if TagHighlight#Option#GetOption("Recurse")
92                                 let opt = ["Explicit","UpFromFile","CurrentDirectory"]
93                         else
94                                 let opt = ["FileDirectory"]
95                         endif
96                 else
97                         " Normal case
98                         let opt = option['Default']
99                 endif
100         else
101         endif
102
103         if option['Type'] =~ 'list'
104                 let result = []
105                 if type(opt) == type('')
106                         if opt == '[]' || opt == ''
107                                 let parsed_opt = []
108                         else
109                                 let parsed_opt = [opt]
110                         endif
111                 else
112                         let parsed_opt = opt
113                 endif
114                 for part in parsed_opt
115                         if part =~ '^OPT(\k\+)$'
116                                 let value_name = part[4:len(part)-2]
117                                 let result += [TagHighlight#Option#GetOption(value_name)]
118                         else
119                                 let result += [part]
120                         endif
121                 endfor
122         elseif option['Type'] == 'bool'
123                 if opt =~ 'True' || opt == 1
124                         let result = 1
125                 elseif opt =~ 'False' || opt == 0
126                         let result = 0
127                 else
128                         throw "Unrecognised bool value"
129                 endif
130         elseif option['Type'] == 'string'
131                 if opt =~ '^OPT(\k\+)$'
132                         let value_name = opt[4:len(opt)-2]
133                         let result = TagHighlight#Option#GetOption(value_name)
134                 else
135                         let result = opt
136                 endif
137         elseif option['Type'] == 'dict'
138                 " This is a complex one: just assume it's valid Vim script
139                 if type(opt) == type([])
140                         " Probably a multi-entry dict that has automatically been
141                         " split: rejoin
142                         let result = eval(join(opt, ', '))
143                 elseif type(opt) == type("")
144                         let result = eval(opt)
145                 else
146                         let result = opt
147                 endif
148         elseif option['Type'] == 'int'
149                 let result = str2nr(opt)
150         endif
151         return result
152 endfunction
153
154 function! TagHighlight#Option#CopyOptions()
155         let result = {}
156         for var in ["g:TagHighlightSettings","b:TagHighlightConfigFileOptions","b:TagHighlightSettings"]
157                 if exists(var)
158                         for key in keys(eval(var))
159                                 if type(eval(var)[key]) == type([])
160                                         let result[key] = eval(var)[key][:]
161                                 elseif type(eval(var)[key]) == type({})
162                                         let result[key] = deepcopy(eval(var)[key])
163                                 else
164                                         let result[key] = eval(var)[key]
165                                 endif
166                         endfor
167                 endif
168         endfor
169         return result
170 endfunction