annotate runtime/ftplugin/ocaml.vim @ 995:9aec644c45dc v7.0.121

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