annotate runtime/ftplugin/ocaml.vim @ 1349:da1d62fe5379 v7.1.063

updated for version 7.1-063
author vimboss
date Sat, 11 Aug 2007 14:00:30 +0000
parents e63691e7c504
children 73fe8baea242
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
1 " Language: OCaml
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
2 " Maintainer: David Baelde <firstname.name@ens-lyon.org>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
3 " Mike Leary <leary@nwlink.com>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
4 " Markus Mottl <markus.mottl@gmail.com>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
5 " Stefano Zacchiroli <zack@bononia.it>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
6 " URL: http://www.ocaml.info/vim/ftplugin/ocaml.vim
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 826
diff changeset
7 " Last Change: 2006 May 01 - Added .annot support for file.whateverext (SZ)
e63691e7c504 updated for version 7.1a
vimboss
parents: 826
diff changeset
8 " 2006 Apr 11 - Fixed an initialization bug; fixed ASS abbrev (MM)
826
1cdd2661f34c updated for version 7.0d01
vimboss
parents: 659
diff changeset
9 " 2005 Oct 13 - removed GPL; better matchit support (MM, SZ)
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
10 "
826
1cdd2661f34c updated for version 7.0d01
vimboss
parents: 659
diff changeset
11 if exists("b:did_ftplugin")
1cdd2661f34c updated for version 7.0d01
vimboss
parents: 659
diff changeset
12 finish
1cdd2661f34c updated for version 7.0d01
vimboss
parents: 659
diff changeset
13 endif
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
14 let b:did_ftplugin=1
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
16 " Error handling -- helps moving where the compiler wants you to go
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
17 let s:cposet=&cpoptions
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 set cpo-=C
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 setlocal efm=
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
20 \%EFile\ \"%f\"\\,\ line\ %l\\,\ characters\ %c-%*\\d:,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
21 \%EFile\ \"%f\"\\,\ line\ %l\\,\ character\ %c:%m,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
22 \%+EReference\ to\ unbound\ regexp\ name\ %m,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
23 \%Eocamlyacc:\ e\ -\ line\ %l\ of\ \"%f\"\\,\ %m,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
24 \%Wocamlyacc:\ w\ -\ %m,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
25 \%-Zmake%.%#,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
26 \%C%m,
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
27 \%D%*\\a[%*\\d]:\ Entering\ directory\ `%f',
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
28 \%X%*\\a[%*\\d]:\ Leaving\ directory\ `%f',
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
29 \%D%*\\a:\ Entering\ directory\ `%f',
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
30 \%X%*\\a:\ Leaving\ directory\ `%f',
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
31 \%DMaking\ %*\\a\ in\ %f
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
32
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
33 " Add mappings, unless the user didn't want this.
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
34 if !exists("no_plugin_maps") && !exists("no_ocaml_maps")
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
35 " (un)commenting
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
36 if !hasmapto('<Plug>Comment')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
37 nmap <buffer> <LocalLeader>c <Plug>LUncomOn
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
38 vmap <buffer> <LocalLeader>c <Plug>BUncomOn
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
39 nmap <buffer> <LocalLeader>C <Plug>LUncomOff
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
40 vmap <buffer> <LocalLeader>C <Plug>BUncomOff
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
41 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
42
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
43 nnoremap <buffer> <Plug>LUncomOn mz0i(* <ESC>$A *)<ESC>`z
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
44 nnoremap <buffer> <Plug>LUncomOff :s/^(\* \(.*\) \*)/\1/<CR>:noh<CR>
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
45 vnoremap <buffer> <Plug>BUncomOn <ESC>:'<,'><CR>`<O<ESC>0i(*<ESC>`>o<ESC>0i*)<ESC>`<
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
46 vnoremap <buffer> <Plug>BUncomOff <ESC>:'<,'><CR>`<dd`>dd`<
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
47
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
48 if !hasmapto('<Plug>Abbrev')
826
1cdd2661f34c updated for version 7.0d01
vimboss
parents: 659
diff changeset
49 iabbrev <buffer> ASS (assert (0=1) (* XXX *))
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
50 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
51 endif
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
52
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
53 " Let % jump between structure elements (due to Issac Trotts)
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
54 let b:mw = ''
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
55 let b:mw = b:mw . ',\<let\>:\<and\>:\(\<in\>\|;;\)'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
56 let b:mw = b:mw . ',\<if\>:\<then\>:\<else\>'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
57 let b:mw = b:mw . ',\<\(for\|while\)\>:\<do\>:\<done\>,'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
58 let b:mw = b:mw . ',\<\(object\|sig\|struct\|begin\)\>:\<end\>'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
59 let b:mw = b:mw . ',\<\(match\|try\)\>:\<with\>'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
60 let b:match_words = b:mw
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
61
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
62 let b:match_ignorecase=0
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
63
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
64 " switching between interfaces (.mli) and implementations (.ml)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
65 if !exists("g:did_ocaml_switch")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
66 let g:did_ocaml_switch = 1
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
67 map <LocalLeader>s :call OCaml_switch(0)<CR>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
68 map <LocalLeader>S :call OCaml_switch(1)<CR>
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
69 fun OCaml_switch(newwin)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
70 if (match(bufname(""), "\\.mli$") >= 0)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
71 let fname = substitute(bufname(""), "\\.mli$", ".ml", "")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
72 if (a:newwin == 1)
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
73 exec "new " . fname
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
74 else
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
75 exec "arge " . fname
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
76 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
77 elseif (match(bufname(""), "\\.ml$") >= 0)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
78 let fname = bufname("") . "i"
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
79 if (a:newwin == 1)
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
80 exec "new " . fname
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
81 else
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
82 exec "arge " . fname
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
83 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
84 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
85 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
86 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
87
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
88 " Folding support
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
89
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
90 " Get the modeline because folding depends on indentation
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
91 let s:s = line2byte(line('.'))+col('.')-1
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
92 if search('^\s*(\*:o\?caml:')
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
93 let s:modeline = getline(".")
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
94 else
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
95 let s:modeline = ""
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
96 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
97 if s:s > 0
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
98 exe 'goto' s:s
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
99 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
100
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
101 " Get the indentation params
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
102 let s:m = matchstr(s:modeline,'default\s*=\s*\d\+')
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
103 if s:m != ""
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
104 let s:idef = matchstr(s:m,'\d\+')
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
105 elseif exists("g:omlet_indent")
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
106 let s:idef = g:omlet_indent
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
107 else
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
108 let s:idef = 2
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
109 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
110 let s:m = matchstr(s:modeline,'struct\s*=\s*\d\+')
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
111 if s:m != ""
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
112 let s:i = matchstr(s:m,'\d\+')
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
113 elseif exists("g:omlet_indent_struct")
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
114 let s:i = g:omlet_indent_struct
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
115 else
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
116 let s:i = s:idef
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
117 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
118
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
119 " Set the folding method
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
120 if exists("g:ocaml_folding")
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
121 setlocal foldmethod=expr
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
122 setlocal foldexpr=OMLetFoldLevel(v:lnum)
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
123 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
124
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
125 " - Only definitions below, executed once -------------------------------------
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
126
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
127 if exists("*OMLetFoldLevel")
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
128 finish
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
129 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
130
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
131 function s:topindent(lnum)
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
132 let l = a:lnum
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
133 while l > 0
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
134 if getline(l) =~ '\s*\%(\<struct\>\|\<sig\>\|\<object\>\)'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
135 return indent(l)
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
136 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
137 let l = l-1
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
138 endwhile
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
139 return -s:i
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
140 endfunction
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
141
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
142 function OMLetFoldLevel(l)
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
143
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
144 " This is for not merging blank lines around folds to them
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
145 if getline(a:l) !~ '\S'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
146 return -1
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
147 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
148
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
149 " We start folds for modules, classes, and every toplevel definition
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
150 if getline(a:l) =~ '^\s*\%(\<val\>\|\<module\>\|\<class\>\|\<type\>\|\<method\>\|\<initializer\>\|\<inherit\>\|\<exception\>\|\<external\>\)'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
151 exe 'return ">' (indent(a:l)/s:i)+1 '"'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
152 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
153
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
154 " Toplevel let are detected thanks to the indentation
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
155 if getline(a:l) =~ '^\s*let\>' && indent(a:l) == s:i+s:topindent(a:l)
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
156 exe 'return ">' (indent(a:l)/s:i)+1 '"'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
157 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
158
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
159 " We close fold on end which are associated to struct, sig or object.
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
160 " We use syntax information to do that.
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
161 if getline(a:l) =~ '^\s*end\>' && synIDattr(synID(a:l, indent(a:l)+1, 0), "name") != "ocamlKeyword"
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
162 return (indent(a:l)/s:i)+1
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
163 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
164
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
165 " Folds end on ;;
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
166 if getline(a:l) =~ '^\s*;;'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
167 exe 'return "<' (indent(a:l)/s:i)+1 '"'
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
168 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
169
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
170 " Comments around folds aren't merged to them.
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
171 if synIDattr(synID(a:l, indent(a:l)+1, 0), "name") == "ocamlComment"
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
172 return -1
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
173 endif
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
174
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
175 return '='
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
176 endfunction
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
177
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
178 " Vim support for OCaml .annot files (requires Vim with python support)
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
179 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
180 " Executing OCamlPrintType(<mode>) function will display in the Vim bottom
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
181 " line(s) the type of an ocaml value getting it from the corresponding .annot
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
182 " file (if any). If Vim is in visual mode, <mode> should be "visual" and the
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
183 " selected ocaml value correspond to the highlighted text, otherwise (<mode>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
184 " can be anything else) it corresponds to the literal found at the current
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
185 " cursor position.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
186 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
187 " .annot files are parsed lazily the first time OCamlPrintType is invoked; is
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
188 " also possible to force the parsing using the OCamlParseAnnot() function.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
189 "
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
190 " Typing ',3' will cause OCamlPrintType function to be invoked with
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
191 " the right argument depending on the current mode (visual or not).
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
192 "
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
193 " Copyright (C) <2003-2004> Stefano Zacchiroli <zack@bononia.it>
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
194 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
195 " Created: Wed, 01 Oct 2003 18:16:22 +0200 zack
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
196 " LastModified: Wed, 25 Aug 2004 18:28:39 +0200 zack
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
197
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
198 if !has("python")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
199 finish
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
200 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
201
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
202 python << EOF
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
203
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
204 import re
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
205 import os
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 826
diff changeset
206 import os.path
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
207 import string
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
208 import time
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
209 import vim
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
210
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
211 debug = False
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
212
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
213 class AnnExc(Exception):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
214 def __init__(self, reason):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
215 self.reason = reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
216
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
217 no_annotations = AnnExc("No type annotations (.annot) file found")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
218 annotation_not_found = AnnExc("No type annotation found for the given text")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
219 def malformed_annotations(lineno):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
220 return AnnExc("Malformed .annot file (line = %d)" % lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
221
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
222 class Annotations:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
223 """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
224 .annot ocaml file representation
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
225
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
226 File format (copied verbatim from caml-types.el)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
227
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
228 file ::= block *
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
229 block ::= position <SP> position <LF> annotation *
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
230 position ::= filename <SP> num <SP> num <SP> num
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
231 annotation ::= keyword open-paren <LF> <SP> <SP> data <LF> close-paren
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
232
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
233 <SP> is a space character (ASCII 0x20)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
234 <LF> is a line-feed character (ASCII 0x0A)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
235 num is a sequence of decimal digits
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
236 filename is a string with the lexical conventions of O'Caml
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
237 open-paren is an open parenthesis (ASCII 0x28)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
238 close-paren is a closed parenthesis (ASCII 0x29)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
239 data is any sequence of characters where <LF> is always followed by
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
240 at least two space characters.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
241
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
242 - in each block, the two positions are respectively the start and the
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
243 end of the range described by the block.
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
244 - in a position, the filename is the name of the file, the first num
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
245 is the line number, the second num is the offset of the beginning
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
246 of the line, the third num is the offset of the position itself.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
247 - the char number within the line is the difference between the third
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
248 and second nums.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
249
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
250 For the moment, the only possible keyword is \"type\"."
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
251 """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
252
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
253 def __init__(self):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
254 self.__filename = None # last .annot parsed file
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
255 self.__ml_filename = None # as above but s/.annot/.ml/
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
256 self.__timestamp = None # last parse action timestamp
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
257 self.__annot = {}
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
258 self.__re = re.compile(
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
259 '^"[^"]*"\s+(\d+)\s+(\d+)\s+(\d+)\s+"[^"]*"\s+(\d+)\s+(\d+)\s+(\d+)$')
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
260
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
261 def __parse(self, fname):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
262 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
263 f = open(fname)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
264 line = f.readline() # position line
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
265 lineno = 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
266 while (line != ""):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
267 m = self.__re.search(line)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
268 if (not m):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
269 raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
270 line1 = int(m.group(1))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
271 col1 = int(m.group(3)) - int(m.group(2))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
272 line2 = int(m.group(4))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
273 col2 = int(m.group(6)) - int(m.group(5))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
274 line = f.readline() # "type(" string
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
275 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
276 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
277 type = []
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
278 line = f.readline() # type description
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
279 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
280 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
281 while line != ")\n":
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
282 type.append(string.strip(line))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
283 line = f.readline()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
284 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
285 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
286 type = string.join(type, "\n")
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
287 key = ((line1, col1), (line2, col2))
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
288 if not self.__annot.has_key(key):
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
289 self.__annot[key] = type
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
290 line = f.readline() # position line
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
291 f.close()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
292 self.__filename = fname
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 826
diff changeset
293 self.__ml_filename = vim.current.buffer.name
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
294 self.__timestamp = int(time.time())
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
295 except IOError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
296 raise no_annotations
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
297
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
298 def parse(self):
1121
e63691e7c504 updated for version 7.1a
vimboss
parents: 826
diff changeset
299 annot_file = os.path.splitext(vim.current.buffer.name)[0] + ".annot"
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
300 self.__parse(annot_file)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
301
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
302 def get_type(self, (line1, col1), (line2, col2)):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
303 if debug:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
304 print line1, col1, line2, col2
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
305 if vim.current.buffer.name == None:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
306 raise no_annotations
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
307 if vim.current.buffer.name != self.__ml_filename or \
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
308 os.stat(self.__filename).st_mtime > self.__timestamp:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
309 self.parse()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
310 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
311 return self.__annot[(line1, col1), (line2, col2)]
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
312 except KeyError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
313 raise annotation_not_found
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
314
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
315 word_char_RE = re.compile("^[\w.]$")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
316
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
317 # TODO this function should recognize ocaml literals, actually it's just an
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
318 # hack that recognize continuous sequences of word_char_RE above
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
319 def findBoundaries(line, col):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
320 """ given a cursor position (as returned by vim.current.window.cursor)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
321 return two integers identify the beggining and end column of the word at
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
322 cursor position, if any. If no word is at the cursor position return the
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
323 column cursor position twice """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
324 left, right = col, col
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
325 line = line - 1 # mismatch vim/python line indexes
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
326 (begin_col, end_col) = (0, len(vim.current.buffer[line]) - 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
327 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
328 while word_char_RE.search(vim.current.buffer[line][left - 1]):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
329 left = left - 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
330 except IndexError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
331 pass
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
332 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
333 while word_char_RE.search(vim.current.buffer[line][right + 1]):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
334 right = right + 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
335 except IndexError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
336 pass
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
337 return (left, right)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
338
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
339 annot = Annotations() # global annotation object
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
340
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
341 def printOCamlType(mode):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
342 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
343 if mode == "visual": # visual mode: lookup highlighted text
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
344 (line1, col1) = vim.current.buffer.mark("<")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
345 (line2, col2) = vim.current.buffer.mark(">")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
346 else: # any other mode: lookup word at cursor position
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
347 (line, col) = vim.current.window.cursor
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
348 (col1, col2) = findBoundaries(line, col)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
349 (line1, line2) = (line, line)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
350 begin_mark = (line1, col1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
351 end_mark = (line2, col2 + 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
352 print annot.get_type(begin_mark, end_mark)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
353 except AnnExc, exc:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
354 print exc.reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
355
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
356 def parseOCamlAnnot():
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
357 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
358 annot.parse()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
359 except AnnExc, exc:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
360 print exc.reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
361
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
362 EOF
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
363
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
364 fun! OCamlPrintType(current_mode)
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
365 if (a:current_mode == "visual")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
366 python printOCamlType("visual")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
367 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
368 python printOCamlType("normal")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
369 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
370 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
371
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
372 fun! OCamlParseAnnot()
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
373 python parseOCamlAnnot()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
374 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
375
557
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
376 map <LocalLeader>t :call OCamlPrintType("normal")<RETURN>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
377 vmap <LocalLeader>t :call OCamlPrintType("visual")<RETURN>
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
378
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
379 let &cpoptions=s:cposet
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
380 unlet s:cposet
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
381
862863033fdd updated for version 7.0158
vimboss
parents: 20
diff changeset
382 " vim:sw=2