-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_cdo_complete_to_your_vimrc
132 lines (116 loc) · 4.64 KB
/
add_cdo_complete_to_your_vimrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"++++++++++++++++++++++++++++++++++++
"++++++++++ CDO COMPLETION ++++++++++
"++++++++++++++++++++++++++++++++++++
" reads in all cdo operators from your locally installed cdo
" by executing cdo --operator and completes those if
" * you hit <C-X><C-U>
" * cdo_completion is enabled
" * cdo_version is later than 1.7.1
"
" also a variable completion for your most-used files is done if
" * filepath of those datasets is set
" * vars completion is 'ENABLE'd
" 'ENABLE' cdo completion or 'DISABLE'
let s:cdo_completion = 'ENABLE'
" cdo completion
" if filename ends with 'sh' use cdo completion
if s:cdo_completion == 'ENABLE'
let s:completion_language='cdo'
" check for cdo version
let s:cdo_version_data = system('cdo --version 2>&1 | head -n 1 | grep -o "[0-9].[0-9].[0-9]"')
let s:cdo_version = s:cdo_version_data[0] . s:cdo_version_data[2] . s:cdo_version_data[4] " gets cdo version 1.7.2 as 170
silent echo s:cdo_version
" check if cdo version is capable of cdo --operator, so if version later than 1.7.1
if s:cdo_version <=170
echo "need to update to cdo 1.7.1 or later to use vim autocompletion for cdo"
let s:cdo_completion = 'DISABLE'
elseif s:cdo_version == ''
echo "cannot get cdo -V, needed for autocompletion"
let s:cdo_completion = 'DISABLE'
" if cdo --operator works
elseif s:cdo_version >= 171
silent echo "for vim autocompletion use C-X C-U"
let s:cdo_completion = 'ENABLE'
"cdo does --operators
"get cdo operators from your cdo version
let s:cdo_data = split(system('cdo --operators'), nr2char(10))
" check if list has entries
if empty(s:cdo_data)
echo "ERROR:List from 'cdo --operators' is empty"
endif
set completefunc=CDOComplete
" modify completition options, more info in :help completeopt or
" http://vimdoc.sourceforge.net/htmldoc/options.html#'completeopt'
set completeopt=longest,menu
" to be executed by user by C-X C-U
fun! CDOComplete(findstart, base)
if a:findstart && s:cdo_completion == 'ENABLE'
let l:line = getline('.')
let l:start = col('.') - 1
while l:start > 0 && l:line[l:start - 1] =~ '\a'
let l:start -= 1
endwhile
return start
elseif s:cdo_completion == 'ENABLE'
" Record what matches − we pass this to complete() later
let l:res = []
" Find cdo matches
for l:line in s:cdo_data
" Check if it matches what we're trying to complete
if split(l:line)[0] =~ '^' . a:base
" It matches! See :help complete() for the full docs on the key names
" for this dict.
call add(l:res, {
\ 'icase': 1,
\ 'word': split(l:line)[0],
\ 'abbr': split(l:line)[0],
\ 'menu': 'CDO: ' . join(split(l:line)[1:]),
\ })
endif
endfor
" varibale completion
if s:vars_completion == 'ENABLE'
" Find variable matches
for l:line in s:vars_data
" Check if it matches what we're trying to complete
if split(l:line)[1] =~ '^' . a:base
" It matches! See :help complete() for the full docs on the key names
" for this dict.
call add(l:res, {
\ 'icase': 1,
\ 'word': split(l:line)[1],
\ 'abbr': split(l:line)[1],
\ 'menu': 'Variable: ' . join(split(l:line)[2:]),
\ })
endif
endfor
endif
return res
endif
endfun
endif "for check cdo_version > 1.7.1
endif
"++++++++++++++++++++++++++++++++++++
"++++++++ Variable COMPLETION +++++++
"++++++++++++++++++++++++++++++++++++
" reads in all variables from certain datasets
" by executing cdo vardes datasets and completes those if
" * you hit <C-X><C-U>
" * vars_completion is enabled
" * cdo is installed
" * ncl_completion or cdo_completion are enabled
" 'ENABLE' variablecompletion or 'DISABLE'
let s:vars_completion = 'ENABLE'
" variable completion data gathering
" set path for file to act cdo vardes on
" CHANGE for your own data
if s:vars_completion == 'ENABLE'
silent echo "vars completion enabled"
let s:vars_data_dir = '/work/mh1007/mpiesm1/experiments/lkm0101/outdata/'
let s:vars_data_strs = ['hamocc/lkm0101_hamocc_data_2d_mm_19990101_19991231.nc', 'mpiom/lkm0101_mpiom_data_2d_mm_19990101_19991231.nc', 'mpiom/lkm0101_mpiom_data_3d_mm_19990101_19991231.nc', 'hamocc/lkm0101_hamocc_data_3d_ym_19990101_19991231.nc' ]
let s:vars_data = []
for s:vars_data_str in s:vars_data_strs
let s:cdo_vardes_str = 'cdo vardes ' . s:vars_data_dir . s:vars_data_str
let s:vars_data += split(system(s:cdo_vardes_str), nr2char(10))
endfor
endif