db36c1d36c3114c1187b663d0c45857645638b2a
[stack/conf/vim.git] / addons / TagHighlight / plugin / TagHighlight.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_TagHighlight') && (g:plugin_development_mode != 1))
16                 throw "Already loaded"
17         endif
18 catch
19         finish
20 endtry
21 let g:loaded_TagHighlight = 1
22
23 let old_versions = globpath(&rtp, 'plugin/ctags_highlighting.vim')
24 if len(old_versions) > 0
25         echoerr "Legacy ctags highlighter found.  This highlighter is"
26                                 \ "intended to replace ctags_highlighter.  See the"
27                                 \ "user documentation in doc/TagHighlight.txt for"
28                                 \ "more information."
29         finish
30 endif
31
32 if ! exists('g:TagHighlightSettings')
33         let g:TagHighlightSettings = {}
34 endif
35
36 let g:TagHighlightPrivate = {}
37
38 let s:plugin_paths = split(globpath(&rtp, 'plugin/TagHighlight/TagHighlight.py'), '\n')
39 if len(s:plugin_paths) == 1
40         let g:TagHighlightPrivate['PluginPath'] = fnamemodify(s:plugin_paths[0], ':p:h')
41 elseif len(s:plugin_paths) == 0
42         echoerr "Cannot find TagHighlight.py"
43 else
44         echoerr "Multiple plugin installs found: something has gone wrong!"
45 endif
46
47 " Update types & tags
48 command! -bar UpdateTypesFile 
49                         \ silent call TagHighlight#Generation#UpdateAndRead(0)
50
51 command! -bar UpdateTypesFileOnly 
52                         \ silent call TagHighlight#Generation#UpdateAndRead(1)
53
54 command! -nargs=1 UpdateTypesFileDebug 
55                         \ call TagHighlight#Debug#DebugUpdateTypesFile(<f-args>)
56
57 function! s:LoadLanguages()
58         " This loads the language data files.
59         let language_files = split(glob(g:TagHighlightPrivate['PluginPath'] . '/data/languages/*.txt'), '\n')
60         let g:TagHighlightPrivate['ExtensionLookup'] = {}
61         let g:TagHighlightPrivate['FileTypeLookup'] = {}
62         let g:TagHighlightPrivate['SyntaxLookup'] = {}
63         let g:TagHighlightPrivate['SpecialSyntaxHandlers'] = {}
64         for language_file in language_files
65                 let entries = TagHighlight#LoadDataFile#LoadFile(language_file)
66                 if has_key(entries, 'Suffix') && has_key(entries, 'VimExtensionMatcher') 
67                                         \ && has_key(entries, 'VimFileTypes') && has_key(entries, 'VimSyntaxes')
68                         let g:TagHighlightPrivate['ExtensionLookup'][entries['VimExtensionMatcher']] = entries['Suffix']
69
70                         if type(entries['VimFileTypes']) == type([])
71                                 let ftkey = join(entries['VimFileTypes'], ",")
72                         else
73                                 let ftkey = entries['VimFileTypes']
74                         endif
75                         let g:TagHighlightPrivate['FileTypeLookup'][ftkey] = entries['Suffix']
76
77                         if type(entries['VimSyntaxes']) == type([])
78                                 let stkey = join(entries['VimSyntaxes'], ",")
79                         else
80                                 let stkey = entries['VimSyntaxes']
81                         endif
82                         let g:TagHighlightPrivate['SyntaxLookup'][stkey] = entries['Suffix']
83                 else
84                         echoerr "Could not load language from file " . language_file
85                 endif
86                 if has_key(entries, 'SpecialSyntaxHandlers')
87                         if type(entries['SpecialSyntaxHandlers']) == type([])
88                                 let handlers = entries['SpecialSyntaxHandlers']
89                         else
90                                 let handlers = [entries['SpecialSyntaxHandlers']]
91                         endif
92                         let g:TagHighlightPrivate['SpecialSyntaxHandlers'][entries['Suffix']] = handlers
93                 endif
94         endfor
95 endfunction
96
97 function! s:LoadKinds()
98         " Load the list of kinds (ignoring ctags information) into
99         " Vim.  This is used to make the default links
100         let g:TagHighlightPrivate['Kinds'] = TagHighlight#LoadDataFile#LoadDataFile('kinds.txt')
101         " Use a dictionary to get all unique entries
102         let tag_names_dict = {}
103         for entry in keys(g:TagHighlightPrivate['Kinds'])
104                 for key in keys(g:TagHighlightPrivate['Kinds'][entry])
105                         let tag_names_dict[g:TagHighlightPrivate['Kinds'][entry][key]] = ""
106                 endfor
107         endfor
108         let g:TagHighlightPrivate['AllTypes'] = sort(keys(tag_names_dict))
109 endfunction
110
111 function! TagHLDebug(str, level)
112         if TagHighlight#Debug#DebugLevelIncludes(a:level)
113                 try
114                         let debug_file = TagHighlight#Option#GetOption('DebugFile')
115                         let print_time = TagHighlight#Option#GetOption('DebugPrintTime')
116                 catch /Unrecognised option/
117                         " Probably haven't loaded the option definitions
118                         " yet, so assume no debug log file
119                         let debug_file = 'None'
120                 endtry
121                 if debug_file == 'None'
122                         echomsg a:str
123                 else
124                         exe 'redir >>' debug_file
125                         if print_time && exists("*strftime")
126                                 silent echo strftime("%H.%M.%S") . ": " . a:str
127                         else
128                                 silent echo a:str
129                         endif
130                         redir END
131                 endif
132         endif
133 endfunction
134
135 call s:LoadLanguages()
136 call s:LoadKinds()
137
138 for tagname in g:TagHighlightPrivate['AllTypes']
139         let simplename = substitute(tagname, '^CTags', '', '')
140         exe 'hi default link' tagname simplename
141         " Highlight everything as a keyword by default
142         exe 'hi default link' simplename 'Keyword'
143 endfor
144
145 if ! has_key(g:TagHighlightPrivate, 'AutoCommandsLoaded')
146         let g:TagHighlightPrivate['AutoCommandsLoaded'] = 1
147         autocmd BufRead,BufNewFile * call TagHighlight#ReadTypes#ReadTypesByExtension()
148         autocmd Syntax * call TagHighlight#ReadTypes#ReadTypesBySyntax()
149         autocmd FileType * call TagHighlight#ReadTypes#ReadTypesByFileType()
150 endif
151 command! ReadTypes call TagHighlight#ReadTypes#ReadTypesByOption()