fe90b2982c7fbd0e6f29473b43156c27a040e52f
[stack/conf/vim.git] / addons / TagHighlight / autoload / TagHighlight / Generation.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_TagHLGeneration') && (g:plugin_development_mode != 1))
16                 throw "Already loaded"
17         endif
18 catch
19         finish
20 endtry
21 let g:loaded_TagHLGeneration = 1
22
23 function! s:UpdateTypesFile()
24         " Load the version information if we haven't already
25         call TagHighlight#Version#LoadVersionInfo()
26
27         " Debug information for configuration
28         if TagHighlight#Debug#DebugLevelIncludes('Information')
29                 call TagHLDebug("Running UpdateTypesFile function at " . strftime("%Y%m%d-%H%M%S"), "Information")
30                 call TagHLDebug("Current directory is " . getcwd(), "Information")
31                 call TagHLDebug("Current file is " . expand('%:p'), "Information")
32                 call TagHLDebug("Release Info:" . string(g:TagHighlightPrivate['PluginVersion']), "Information")
33                 call TagHLDebug("Global options (g:TagHighlightSettings): " . string(g:TagHighlightSettings), "Information")
34                 if exists('b:TagHighlightSettings')
35                         call TagHLDebug("Buffer options (b:TagHighlightSettings): " . string(b:TagHighlightSettings), "Information")
36                 else
37                         call TagHLDebug("No buffer options set", "Information")
38                 endif
39         endif
40
41         " Load the option file
42         let option_file_info = TagHighlight#Option#LoadOptionFileIfPresent()
43         " Debug information for configuration
44         if TagHighlight#Debug#DebugLevelIncludes('Information') && option_file_info['Exists']
45                 call TagHLDebug("Project config file options: " . string(b:TagHighlightConfigFileOptions), "Information")
46         else
47                 call TagHLDebug("Project config file does not exist", "Information")
48         endif
49         
50         " Most simple options are automatic.  The options below are
51         " handled manually.
52         
53         " Find the ctags path
54         let ctags_option = TagHighlight#Option#GetOption('CtagsExecutable')
55         if ctags_option == 'None'
56                 " Option not set: search for 'ctags' in the path
57                 call TagHLDebug("CtagsExecutable not set, searching for 'ctags' in path", "Information")
58                 let b:TagHighlightSettings['CtagsExeFull'] = TagHighlight#RunPythonScript#FindExeInPath('ctags')
59         elseif ctags_option =~ '[\\/]'
60                 " Option set and includes '/' or '\': must be explicit
61                 " path to named executable: just pass to mktypes
62                 call TagHLDebug("CtagsExecutable set with path delimiter, using as explicit path", "Information")
63                 let b:TagHighlightSettings['CtagsExeFull'] = ctags_option
64         else
65                 " Option set but doesn't include path separator: search
66                 " in the path
67                 call TagHLDebug("CtagsExecutable set without path delimiter, searching in path", "Information")
68                 let b:TagHighlightSettings['CtagsExeFull'] = TagHighlight#RunPythonScript#FindExeInPath(ctags_option)
69         endif
70
71         let tag_file_info = TagHighlight#Find#LocateFile('TAGS', '')
72         if tag_file_info['Found'] == 1
73                 let b:TagHighlightSettings['CtagsFileLocation'] = tag_file_info['Directory']
74         endif
75
76         let types_file_info = TagHighlight#Find#LocateFile('TYPES', '*')
77         if types_file_info['Found'] == 1
78                 let b:TagHighlightSettings['TypesFileLocation'] = types_file_info['Directory']
79         endif
80
81         if TagHighlight#Option#GetOption('SourceDir') =~ 'None'
82                 " The source directory has not been set.  If a project config file was
83                 " found, use that directory.  If not, but a types file was found,
84                 " use that directory.  If not, but a tag file was found, use that
85                 " directory.  If not, use the current directory.
86                 call TagHLDebug("No source dir set", "Information")
87                 call TagHLDebug("Current directory is now " . getcwd(), "Information")
88                 if ! TagHighlight#Option#GetOption('Recurse')
89                         call TagHLDebug("Non-recursive mode, using file directory", "Information")
90                         let file = expand('<afile>')
91                         if len(file) == 0
92                                 let file = expand('%')
93                         endif
94                         call TagHLDebug("File is " . file . "(" . fnamemodify(file, ':p:h') . ")", "Information")
95                         let b:TagHighlightSettings['SourceDir'] = fnamemodify(file, ':p:h')
96                 elseif option_file_info['Found'] == 1 && option_file_info['Exists'] == 1
97                         call TagHLDebug("Using project config file directory", "Information")
98                         let b:TagHighlightSettings['SourceDir'] = option_file_info['Directory']
99                 elseif types_file_info['Found'] == 1 && types_file_info['Exists'] == 1
100                         call TagHLDebug("Using types file directory", "Information")
101                         let b:TagHighlightSettings['SourceDir'] = types_file_info['Directory']
102                 elseif tag_file_info['Found'] == 1 && tag_file_info['Exists'] == 1
103                         call TagHLDebug("Using tags file directory", "Information")
104                         let b:TagHighlightSettings['SourceDir'] = tag_file_info['Directory']
105                 else
106                         call TagHLDebug("Using current directory", "Information")
107                         let b:TagHighlightSettings['SourceDir'] = '.'
108                 endif
109         else
110                 call TagHLDebug("Source dir set explicitly to " . TagHighlight#Option#GetOption("SourceDir"), "Information")
111         endif
112
113         " If a types file does not exist and this option is set, just quit now
114         if TagHighlight#Option#GetOption('OnlyGenerateTypesIfPresent') == 1
115                 if types_file_info['Exists'] == 0
116                         call TagHLDebug("Types file does not exist, not generating new files", "Information")
117                         return
118                 endif
119         endif
120
121         if tag_file_info['Exists'] == 1
122                 if TagHighlight#Option#GetOption('DoNotGenerateTagsIfPresent') == 1
123                         " This will be unset in UpdateAndRead
124                         call TagHLDebug("Tag file doesn't exist and DoNotGenerateTagsIfPresent set, not generating new tag file", "Information")
125                         let b:TagHighlightSettings['DoNotGenerateTags'] = 1
126                 endif
127         elseif TagHighlight#Option#GetOption('DoNotGenerateTags') == 1
128                 echoerr "Cannot create types file without generating tags: tags file does not exist"
129                 return
130         endif
131
132         " Call any PreUpdate hooks
133         let preupdate_hooks = TagHighlight#Option#GetOption('PreUpdateHooks')
134         for preupdate_hook in preupdate_hooks
135                 call TagHLDebug("Calling pre-update hook " . preupdate_hook, "Information")
136                 exe 'call' preupdate_hook . '()'
137         endfor
138         
139         call TagHLDebug("Running generator with options:", "Information")
140         for var in ["g:TagHighlightSettings","b:TagHighlightConfigFileOptions","b:TagHighlightSettings"]
141                 if exists(var)
142                         call TagHLDebug(" - " . var . ": " . string(eval(var)), "Information")
143                 else
144                         call TagHLDebug(" - " . var . ": UNSET", "Information")
145                 endif
146         endfor
147         let RunOptions = TagHighlight#Option#CopyOptions()
148         call TagHighlight#RunPythonScript#RunGenerator(RunOptions)
149
150         let postupdate_hooks = TagHighlight#Option#GetOption('PostUpdateHooks')
151         for postupdate_hook in postupdate_hooks
152                 call TagHLDebug("Calling post-update hook " . postupdate_hook, "Information")
153                 exe 'call' postupdate_hook . '()'
154         endfor
155
156         call TagHLDebug("UpdateTypesFile() complete, current directory is now " . getcwd(), "Information")
157 endfunction
158
159 function! TagHighlight#Generation#UpdateAndRead(skiptags)
160         call TagHLDebug("UpdateAndRead() called with parameter " . a:skiptags, "Information")
161         let restore_options = 0
162         if exists('b:TagHighlightSettings')
163                 let stored_options = deepcopy(b:TagHighlightSettings)
164                 let restore_options = 1
165         else
166                 let b:TagHighlightSettings = {}
167         endif
168
169         " Start with a copy of the settings so that we can tweak things
170         if a:skiptags
171                 call TagHLDebug("Skipping tag generation", "Information")
172                 let b:TagHighlightSettings['DoNotGenerateTags'] = 1
173         endif
174         
175         call s:UpdateTypesFile()
176         let SavedTabNr = tabpagenr()
177         let SavedWinNr = winnr()
178         tabdo windo call TagHighlight#ReadTypes#ReadTypesByOption()
179         exe 'tabn' SavedTabNr
180         exe SavedWinNr . 'wincmd w'
181
182         unlet b:TagHighlightSettings
183         if restore_options
184                 let b:TagHighlightSettings = deepcopy(stored_options)
185         endif
186         call TagHLDebug("UpdateAndRead() complete", "Information")
187 endfunction