Skip to content

Commit fe6ba73

Browse files
committed
Improve plug-in initialization (make it more robust)
See also issue #58 on GitHub: #58
1 parent 5cf3313 commit fe6ba73

File tree

2 files changed

+94
-89
lines changed

2 files changed

+94
-89
lines changed

autoload/xolox/easytags.vim

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,79 @@
11
" Vim script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: June 20, 2013
3+
" Last Change: June 22, 2013
44
" URL: http://peterodding.com/code/vim/easytags/
55

6-
let g:xolox#easytags#version = '3.3.9'
6+
let g:xolox#easytags#version = '3.3.10'
77

8-
" Public interface through (automatic) commands. {{{1
8+
" Plug-in initialization. {{{1
9+
10+
function! xolox#easytags#initialize(min_version) " {{{2
11+
" Check that the location of Exuberant Ctags has been configured or that the
12+
" correct version of the program exists in one of its default locations.
13+
if exists('g:easytags_cmd') && xolox#easytags#check_ctags_compatible(g:easytags_cmd, a:min_version)
14+
return 1
15+
endif
16+
if xolox#misc#os#is_win()
17+
" FIXME The code below that searches the $PATH is not used on Windows at
18+
" the moment because xolox#misc#path#which() generally produces absolute
19+
" paths and on Windows these absolute paths tend to contain spaces which
20+
" makes xolox#shell#execute_with_dll() fail. I've tried quoting the
21+
" program name with double quotes but it fails just the same (it works
22+
" with system() though). Anyway the problem of having multiple conflicting
23+
" versions of Exuberant Ctags installed is not that relevant to Windows
24+
" since it doesn't have a package management system. I still want to fix
25+
" xolox#shell#execute_with_dll() though.
26+
if xolox#easytags#check_ctags_compatible('ctags', a:min_version)
27+
let g:easytags_cmd = 'ctags'
28+
return 1
29+
endif
30+
else
31+
" Exuberant Ctags can be installed under several names:
32+
" - On Ubuntu Linux, Exuberant Ctags is installed as `ctags-exuberant'
33+
" (and possibly `ctags' but that one can't be trusted :-)
34+
" - On Debian Linux, Exuberant Ctags is installed as `exuberant-ctags'.
35+
" - On Free-BSD, Exuberant Ctags is installed as `exctags'.
36+
" IIUC on Mac OS X the program /usr/bin/ctags is installed by default but
37+
" unusable and when the user installs Exuberant Ctags in an alternative
38+
" location, it doesn't come before /usr/bin/ctags in the search path. To
39+
" solve this problem in a general way and to save every Mac user out there
40+
" some frustration the plug-in will search the path and consider every
41+
" possible location, meaning that as long as Exuberant Ctags is installed
42+
" in the $PATH the plug-in should find it automatically.
43+
for program in xolox#misc#path#which('exuberant-ctags', 'ctags-exuberant', 'ctags', 'exctags')
44+
if xolox#easytags#check_ctags_compatible(program, a:min_version)
45+
let g:easytags_cmd = program
46+
return 1
47+
endif
48+
endfor
49+
endif
50+
endfunction
51+
52+
function! xolox#easytags#check_ctags_compatible(name, min_version) " {{{2
53+
" Not every executable out there named `ctags' is in fact Exuberant Ctags.
54+
" This function makes sure it is because the easytags plug-in requires the
55+
" --list-languages option (and more).
56+
call xolox#misc#msg#debug("easytags.vim %s: Checking if Exuberant Ctags is installed as '%s'.", g:xolox#easytags#version, a:name)
57+
if executable(a:name)
58+
let command = a:name . ' --version'
59+
let result = xolox#misc#os#exec({'command': command, 'check': 0})
60+
if result['exit_code'] == 0
61+
let pattern = 'Exuberant Ctags \zs\(\d\+\(\.\d\+\)*\|Development\)'
62+
let g:easytags_ctags_version = matchstr(get(result['stdout'], 0, ''), pattern)
63+
call xolox#misc#msg#debug("easytags.vim %s: Executable '%s' reported version '%s'.", g:xolox#easytags#version, a:name, g:easytags_ctags_version)
64+
if g:easytags_ctags_version == 'Development'
65+
call xolox#misc#msg#debug("easytags.vim %s: Assuming development build is compatible ..", g:xolox#easytags#version, a:name)
66+
return 1
67+
elseif xolox#misc#version#at_least(a:min_version, g:easytags_ctags_version)
68+
call xolox#misc#msg#debug("easytags.vim %s: Version is compatible! :-)", g:xolox#easytags#version)
69+
return 1
70+
else
71+
call xolox#misc#msg#debug("easytags.vim %s: Version is not compatible! :-(", g:xolox#easytags#version)
72+
return 0
73+
endif
74+
endif
75+
endif
76+
endfunction
977

1078
function! xolox#easytags#register(global) " {{{2
1179
" Parse the &tags option and get a list of all tags files *including
@@ -34,6 +102,8 @@ function! xolox#easytags#register(global) " {{{2
34102
endif
35103
endfunction
36104

105+
" Public interface through (automatic) commands. {{{1
106+
37107
" The localtime() when the CursorHold event last fired.
38108
let s:last_automatic_run = 0
39109

plugin/easytags.vim

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim plug-in
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: June 20, 2013
3+
" Last Change: June 22, 2013
44
" URL: http://peterodding.com/code/vim/easytags/
55
" Requires: Exuberant Ctags (http://ctags.sf.net)
66

@@ -48,96 +48,31 @@ if !exists('g:easytags_python_script')
4848
let g:easytags_python_script = expand('<sfile>:p:h') . '/../misc/easytags/highlight.py'
4949
endif
5050

51-
function! s:InitEasyTags(version)
52-
" Check that the location of Exuberant Ctags has been configured or that the
53-
" correct version of the program exists in one of its default locations.
54-
if exists('g:easytags_cmd') && s:CheckCtags(g:easytags_cmd, a:version)
55-
return 1
56-
endif
57-
if xolox#misc#os#is_win()
58-
" FIXME The code below that searches the $PATH is not used on Windows at
59-
" the moment because xolox#misc#path#which() generally produces absolute
60-
" paths and on Windows these absolute paths tend to contain spaces which
61-
" makes xolox#shell#execute_with_dll() fail. I've tried quoting the
62-
" program name with double quotes but it fails just the same (it works
63-
" with system() though). Anyway the problem of having multiple conflicting
64-
" versions of Exuberant Ctags installed is not that relevant to Windows
65-
" since it doesn't have a package management system. I still want to fix
66-
" xolox#shell#execute_with_dll() though.
67-
if s:CheckCtags('ctags', a:version)
68-
let g:easytags_cmd = 'ctags'
69-
return 1
70-
endif
71-
else
72-
" Exuberant Ctags can be installed under multiple names:
73-
" - On Ubuntu Linux, Exuberant Ctags is installed as `ctags-exuberant'
74-
" (and possibly `ctags' but that one can't be trusted :-)
75-
" - On Debian Linux, Exuberant Ctags is installed as `exuberant-ctags'.
76-
" - On Free-BSD, Exuberant Ctags is installed as `exctags'.
77-
" IIUC on Mac OS X the program /usr/bin/ctags is installed by default but
78-
" unusable and when the user installs Exuberant Ctags in an alternative
79-
" location, it doesn't come before /usr/bin/ctags in the search path. To
80-
" solve this problem in a general way and to save every Mac user out there
81-
" some frustration the plug-in will search the path and consider every
82-
" possible location, meaning that as long as Exuberant Ctags is installed
83-
" in the $PATH the plug-in should find it automatically.
84-
for program in xolox#misc#path#which('exuberant-ctags', 'ctags-exuberant', 'ctags', 'exctags')
85-
if s:CheckCtags(program, a:version)
86-
let g:easytags_cmd = program
87-
return 1
88-
endif
89-
endfor
90-
endif
91-
endfunction
92-
93-
function! s:CheckCtags(name, version)
94-
" Not every executable out there named `ctags' is in fact Exuberant Ctags.
95-
" This function makes sure it is because the easytags plug-in requires the
96-
" --list-languages option (and more).
97-
if executable(a:name)
98-
let command = a:name . ' --version'
99-
let result = xolox#misc#os#exec({'command': command, 'check': 0})
100-
if result['exit_code'] == 0
101-
let pattern = 'Exuberant Ctags \zs\(\d\+\(\.\d\+\)*\|Development\)'
102-
let g:easytags_ctags_version = matchstr(result['stdout'][0], pattern)
103-
if g:easytags_ctags_version == 'Development'
104-
return 1
51+
" Make sure Exuberant Ctags >= 5.5 is installed.
52+
if !xolox#easytags#initialize('5.5')
53+
" Did the user configure the plug-in to suppress the regular warning message?
54+
if !(exists('g:easytags_suppress_ctags_warning') && g:easytags_suppress_ctags_warning)
55+
" Explain to the user what went wrong:
56+
if !exists('g:easytags_ctags_version') || empty(g:easytags_ctags_version)
57+
" Exuberant Ctags is not installed / could not be found.
58+
let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags isn't installed!"
59+
if executable('apt-get')
60+
let s:msg .= " On Ubuntu & Debian you can install Exuberant Ctags by"
61+
let s:msg .= " installing the package named `exuberant-ctags':"
62+
let s:msg .= " sudo apt-get install exuberant-ctags"
10563
else
106-
return s:VersionToNumber(g:easytags_ctags_version) >= a:version
64+
let s:msg .= " Please download & install Exuberant Ctags from http://ctags.sf.net"
10765
endif
108-
endif
109-
endif
110-
endfunction
111-
112-
function! s:VersionToNumber(s)
113-
let values = split(a:s, '\.')
114-
if len(values) == 1
115-
return values[0] * 10
116-
elseif len(values) >= 2
117-
return values[0] * 10 + values[1][0]
118-
endif
119-
endfunction
120-
121-
if !s:InitEasyTags(55)
122-
if exists('g:easytags_suppress_ctags_warning') && g:easytags_suppress_ctags_warning
123-
finish
124-
endif
125-
if !exists('g:easytags_ctags_version') || empty(g:easytags_ctags_version)
126-
let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags isn't installed!"
127-
if executable('apt-get')
128-
let s:msg .= " On Ubuntu & Debian you can install Exuberant Ctags by"
129-
let s:msg .= " installing the package named `exuberant-ctags':"
130-
let s:msg .= " sudo apt-get install exuberant-ctags"
66+
call xolox#misc#msg#warn(s:msg, g:xolox#easytags#version)
13167
else
132-
let s:msg .= " Please download & install Exuberant Ctags from http://ctags.sf.net"
68+
" The installed version is too old.
69+
let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags 5.5"
70+
let s:msg .= " or newer is required while you have version %s installed!"
71+
call xolox#misc#msg#warn(s:msg, g:xolox#easytags#version, g:easytags_ctags_version)
13372
endif
134-
echomsg printf(s:msg, g:xolox#easytags#version)
135-
else
136-
let s:msg = "easytags.vim %s: Plug-in not loaded because Exuberant Ctags 5.5"
137-
let s:msg .= " or newer is required while you have version %s installed!"
138-
echomsg printf(s:msg, g:xolox#easytags#version, g:easytags_ctags_version)
73+
unlet s:msg
13974
endif
140-
unlet s:msg
75+
" Stop loading the plug-in; don't define the (automatic) commands.
14176
finish
14277
endif
14378

0 commit comments

Comments
 (0)