2 " Author: Peter Odding <peter@peterodding.com>
3 " Last Change: September 4, 2011
4 " URL: http://peterodding.com/code/vim/easytags/
5 " Requires: Exuberant Ctags (http://ctags.sf.net)
7 " Support for automatic update using the GLVS plug-in.
8 " GetLatestVimScripts: 3114 1 :AutoInstall: easytags.zip
10 " Don't source the plug-in when it's already been loaded or &compatible is set.
11 if &cp || exists('g:loaded_easytags')
15 " Configuration defaults and initialization. {{{1
17 if !exists('g:easytags_file')
18 if xolox#misc#os#is_win()
19 let g:easytags_file = '~\_vimtags'
21 let g:easytags_file = '~/.vimtags'
25 if !exists('g:easytags_by_filetype')
26 let g:easytags_by_filetype = ''
29 if !exists('g:easytags_events')
30 let g:easytags_events = []
31 if !exists('g:easytags_on_cursorhold') || g:easytags_on_cursorhold
32 call extend(g:easytags_events, ['CursorHold', 'CursorHoldI'])
34 if exists('g:easytags_always_enabled') && g:easytags_always_enabled
35 call extend(g:easytags_events, ['BufReadPost', 'BufWritePost', 'FocusGained', 'ShellCmdPost', 'ShellFilterPost'])
39 if !exists('g:easytags_ignored_filetypes')
40 let g:easytags_ignored_filetypes = '^tex$'
43 if !exists('g:easytags_python_script')
44 let g:easytags_python_script = expand('<sfile>:p:h') . '/../misc/easytags/highlight.py'
47 function! s:InitEasyTags(version)
48 " Check that the location of Exuberant Ctags has been configured or that the
49 " correct version of the program exists in one of its default locations.
50 if exists('g:easytags_cmd') && s:CheckCtags(g:easytags_cmd, a:version)
53 " On Ubuntu Linux, Exuberant Ctags is installed as `ctags'. On Debian Linux,
54 " Exuberant Ctags is installed as `exuberant-ctags'. On Free-BSD, Exuberant
55 " Ctags is installed as `exctags'.
56 for name in ['ctags', 'exuberant-ctags', 'exctags']
57 if s:CheckCtags(name, a:version)
58 let g:easytags_cmd = name
64 function! s:CheckCtags(name, version)
65 " Not every executable out there named `ctags' is in fact Exuberant Ctags.
66 " This function makes sure it is because the easytags plug-in requires the
67 " --list-languages option (and more).
69 let command = a:name . ' --version'
71 let listing = join(xolox#shell#execute(command, 1), '\n')
72 catch /^Vim\%((\a\+)\)\=:E117/
73 " Ignore missing shell.vim plug-in.
74 let listing = system(command)
76 " xolox#shell#execute() converts shell errors to exceptions and since
77 " we're checking whether one of several executables exists we don't want
78 " to throw an error when the first one doesn't!
81 let pattern = 'Exuberant Ctags \zs\(\d\+\(\.\d\+\)*\|Development\)'
82 let g:easytags_ctags_version = matchstr(listing, pattern)
83 if g:easytags_ctags_version == 'Development'
86 return s:VersionToNumber(g:easytags_ctags_version) >= a:version
91 function! s:VersionToNumber(s)
92 let values = split(a:s, '\.')
95 elseif len(values) >= 2
96 return values[0] * 10 + values[1][0]
100 if !s:InitEasyTags(55)
101 if exists('g:easytags_suppress_ctags_warning') && g:easytags_suppress_ctags_warning
104 if !exists('g:easytags_ctags_version') || empty(g:easytags_ctags_version)
105 let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags isn't installed!"
106 if executable('apt-get')
107 let s:msg .= " On Ubuntu & Debian you can install Exuberant Ctags by"
108 let s:msg .= " installing the package named `exuberant-ctags':"
109 let s:msg .= " sudo apt-get install exuberant-ctags"
111 let s:msg .= " Please download & install Exuberant Ctags from http://ctags.sf.net"
113 echomsg printf(s:msg, g:xolox#easytags#version)
115 let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags 5.5"
116 let s:msg .= " or newer is required while you have version %s installed!"
117 echomsg printf(s:msg, g:xolox#easytags#version, g:easytags_ctags_version)
123 " The plug-in initializes the &tags option as soon as possible so that the
124 " global tags file is available when using "vim -t some_tag". If &tags is
125 " reset, we'll try again on the "VimEnter" automatic command event (below).
126 call xolox#easytags#register(1)
128 " The :UpdateTags and :HighlightTags commands. {{{1
130 command! -bar -bang -nargs=* -complete=file UpdateTags call xolox#easytags#update(0, <q-bang> == '!', [<f-args>])
131 command! -bar HighlightTags call xolox#easytags#highlight()
132 command! -bang TagsByFileType call xolox#easytags#by_filetype(<q-bang> == '!')
134 " Automatic commands. {{{1
136 augroup PluginEasyTags
138 " This is the alternative way of registering the global tags file using
139 " the automatic command event "VimEnter". Apparently this makes the
140 " plug-in behave better when used together with tplugin?
141 autocmd VimEnter * call xolox#easytags#register(1)
142 " Define the automatic commands to perform updating/highlighting.
143 for s:eventname in g:easytags_events
144 execute 'autocmd' s:eventname '* call xolox#easytags#autoload(' string(s:eventname) ')'
146 " Define an automatic command to register file type specific tags files?
147 if !empty(g:easytags_by_filetype)
148 autocmd FileType * call xolox#easytags#register(0)
150 " After reloading a buffer the dynamic syntax highlighting is lost. The
151 " following code makes sure the highlighting is refreshed afterwards.
152 autocmd BufReadPost * unlet! b:easytags_last_highlighted
157 " Make sure the plug-in is only loaded once.
158 let g:loaded_easytags = 1