annotate runtime/ftplugin/ocaml.vim @ 201:300fac7c5a2b v7.0059

updated for version 7.0059
author vimboss
date Fri, 11 Mar 2005 22:51:16 +0000
parents 4ac1dce8dd5e
children 862863033fdd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
1 " Vim settings file
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
2 " Language: OCaml
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
3 " Maintainers: Mike Leary <leary@nwlink.com>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
4 " Markus Mottl <markus@oefai.at>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
5 " Stefano Zacchiroli <zack@bononia.it>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
6 " URL: http://www.oefai.at/~markus/vim/ftplugin/ocaml.vim
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
7 " Last Change: 2004 Apr 12 - better .ml/.mli-switching without Python (SZ)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
8 " 2003 Nov 21 - match_words-patterns and .ml/.mli-switching (MM)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
9 " 2003 Oct 16 - re-entered variable 'did_ocaml_dtypes' (MM)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
10 " 2003 Oct 15 - added Stefano Zacchirolis (SZ) Python-code for
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
11 " displaying type annotations (MM)
7
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
12
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
13 " Only do these settings when not done yet for this buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
14 if exists("b:did_ftplugin")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
15 finish
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
16 endif
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
17
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
18 " Don't do other file type settings for this buffer
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
19 let b:did_ftplugin = 1
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
20
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
21 set cpo-=C
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
22
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
23 " Error formats
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
24 setlocal efm=
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
25 \%EFile\ \"%f\"\\,\ line\ %l\\,\ characters\ %c-%*\\d:,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
26 \%EFile\ \"%f\"\\,\ line\ %l\\,\ character\ %c:%m,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
27 \%+EReference\ to\ unbound\ regexp\ name\ %m,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
28 \%Eocamlyacc:\ e\ -\ line\ %l\ of\ \"%f\"\\,\ %m,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
29 \%Wocamlyacc:\ w\ -\ %m,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
30 \%-Zmake%.%#,
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
31 \%C%m
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")
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
35 " Uncommenting
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
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
44 nnoremap <buffer> <Plug>LUncomOff <ESC>:s/^(\* \(.*\) \*)/\1/<CR>
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')
3fc0f57ecb91 updated for version 7.0001
vimboss
parents:
diff changeset
49 iabbrev <buffer> ASS (assert false)
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)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
54 let b:mw='\<let\>:\<and\>:\(\<in\>\|;;\),'
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
55 let b:mw=b:mw . '\<if\>:\<then\>:\<else\>,\<do\>:\<done\>,'
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
56 let b:mw=b:mw . '\<\(object\|sig\|struct\|begin\)\>:\<end\>'
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
57 let b:match_words=b:mw
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
58
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
59 " switching between interfaces (.mli) and implementations (.ml)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
60 if !exists("g:did_ocaml_switch")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
61 let g:did_ocaml_switch = 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
62 map ,s :call OCaml_switch(0)<CR>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
63 map ,S :call OCaml_switch(1)<CR>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
64 fun OCaml_switch(newwin)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
65 if (match(bufname(""), "\\.mli$") >= 0)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
66 let fname = substitute(bufname(""), "\\.mli$", ".ml", "")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
67 if (a:newwin == 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
68 exec "new " . fname
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
69 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
70 exec "arge " . fname
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
71 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
72 elseif (match(bufname(""), "\\.ml$") >= 0)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
73 let fname = bufname("") . "i"
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
74 if (a:newwin == 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
75 exec "new " . fname
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
76 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
77 exec "arge " . fname
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
78 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
79 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
80 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
81 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
82
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
83 " Vim support for OCaml 3.07 .annot files (requires Vim with python support)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
84 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
85 " Executing OCamlPrintType(<mode>) function will display in the Vim bottom
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
86 " 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
87 " 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
88 " selected ocaml value correspond to the highlighted text, otherwise (<mode>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
89 " can be anything else) it corresponds to the literal found at the current
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
90 " cursor position.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
91 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
92 " .annot files are parsed lazily the first time OCamlPrintType is invoked; is
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
93 " also possible to force the parsing using the OCamlParseAnnot() function.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
94 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
95 " Hitting the <F3> key will cause OCamlPrintType function to be invoked with
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
96 " the right argument depending on the current mode (visual or not).
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
97 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
98 " Copyright (C) <2003> Stefano Zacchiroli <zack@bononia.it>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
99 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
100 " Created: Wed, 01 Oct 2003 18:16:22 +0200 zack
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
101 " LastModified: Mon, 06 Oct 2003 11:05:39 +0200 zack
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
102 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
103 " This program is free software; you can redistribute it and/or modify
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
104 " it under the terms of the GNU General Public License as published by
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
105 " the Free Software Foundation; either version 2 of the License, or
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
106 " (at your option) any later version.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
107 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
108 " This program is distributed in the hope that it will be useful,
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
109 " but WITHOUT ANY WARRANTY; without even the implied warranty of
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
110 " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
111 " GNU General Public License for more details.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
112 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
113 " You should have received a copy of the GNU General Public License
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
114 " along with this program; if not, write to the Free Software
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
115 " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
116 "
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
117
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
118 if !has("python")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
119 echo "Python support not found: OCaml .annot support disabled"
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
120 finish
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
121 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
122
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
123 if !exists("g:did_ocaml_dtypes")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
124 let g:did_ocaml_dtypes = 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
125 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
126 finish
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
127 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
128
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
129 python << EOF
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
130
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
131 import re
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
132 import os
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
133 import string
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
134 import time
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
135 import vim
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
136
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
137 debug = False
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
138
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
139 class AnnExc(Exception):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
140 def __init__(self, reason):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
141 self.reason = reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
142
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
143 no_annotations = AnnExc("No type annotations (.annot) file found")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
144 annotation_not_found = AnnExc("No type annotation found for the given text")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
145 def malformed_annotations(lineno):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
146 return AnnExc("Malformed .annot file (line = %d)" % lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
147
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
148 class Annotations:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
149 """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
150 .annot ocaml file representation
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
151
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
152 File format (copied verbatim from caml-types.el)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
153
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
154 file ::= block *
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
155 block ::= position <SP> position <LF> annotation *
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
156 position ::= filename <SP> num <SP> num <SP> num
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
157 annotation ::= keyword open-paren <LF> <SP> <SP> data <LF> close-paren
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
158
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
159 <SP> is a space character (ASCII 0x20)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
160 <LF> is a line-feed character (ASCII 0x0A)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
161 num is a sequence of decimal digits
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
162 filename is a string with the lexical conventions of O'Caml
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
163 open-paren is an open parenthesis (ASCII 0x28)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
164 close-paren is a closed parenthesis (ASCII 0x29)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
165 data is any sequence of characters where <LF> is always followed by
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
166 at least two space characters.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
167
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
168 - in each block, the two positions are respectively the start and the
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
169 - end of the range described by the block.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
170 - 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
171 is the line number, the second num is the offset of the beginning
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
172 of the line, the third num is the offset of the position itself.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
173 - the char number within the line is the difference between the third
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
174 and second nums.
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
175
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
176 For the moment, the only possible keyword is \"type\"."
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
177 """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
178
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
179 def __init__(self):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
180 self.__filename = None # last .annot parsed file
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
181 self.__ml_filename = None # as above but s/.annot/.ml/
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
182 self.__timestamp = None # last parse action timestamp
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
183 self.__annot = {}
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
184 self.__re = re.compile(
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
185 '^"[^"]+"\s+(\d+)\s+(\d+)\s+(\d+)\s+"[^"]+"\s+(\d+)\s+(\d+)\s+(\d+)$')
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
186
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
187 def __parse(self, fname):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
188 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
189 f = open(fname)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
190 line = f.readline() # position line
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
191 lineno = 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
192 while (line != ""):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
193 m = self.__re.search(line)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
194 if (not m):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
195 raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
196 line1 = int(m.group(1))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
197 col1 = int(m.group(3)) - int(m.group(2))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
198 line2 = int(m.group(4))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
199 col2 = int(m.group(6)) - int(m.group(5))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
200 line = f.readline() # "type(" string
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
201 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
202 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
203 type = []
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
204 line = f.readline() # type description
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
205 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
206 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
207 while line != ")\n":
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
208 type.append(string.strip(line))
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
209 line = f.readline()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
210 lineno += 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
211 if (line == ""): raise malformed_annotations(lineno)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
212 type = string.join(type, "\n")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
213 self.__annot[(line1, col1), (line2, col2)] = type
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
214 line = f.readline() # position line
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
215 f.close()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
216 self.__filename = fname
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
217 self.__ml_filename = re.sub("\.annot$", ".ml", fname)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
218 self.__timestamp = int(time.time())
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
219 except IOError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
220 raise no_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 def parse(self):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
223 annot_file = re.sub("\.ml$", ".annot", vim.current.buffer.name)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
224 self.__parse(annot_file)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
225
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
226 def get_type(self, (line1, col1), (line2, col2)):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
227 if debug:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
228 print line1, col1, line2, col2
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
229 if vim.current.buffer.name == None:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
230 raise no_annotations
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
231 if vim.current.buffer.name != self.__ml_filename or \
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
232 os.stat(self.__filename).st_mtime > self.__timestamp:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
233 self.parse()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
234 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
235 return self.__annot[(line1, col1), (line2, col2)]
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
236 except KeyError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
237 raise annotation_not_found
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
238
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
239 word_char_RE = re.compile("^[\w.]$")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
240
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
241 # TODO this function should recognize ocaml literals, actually it's just an
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
242 # hack that recognize continuous sequences of word_char_RE above
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
243 def findBoundaries(line, col):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
244 """ given a cursor position (as returned by vim.current.window.cursor)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
245 return two integers identify the beggining and end column of the word at
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
246 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
247 column cursor position twice """
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
248 left, right = col, col
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
249 line = line - 1 # mismatch vim/python line indexes
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
250 (begin_col, end_col) = (0, len(vim.current.buffer[line]) - 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
251 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
252 while word_char_RE.search(vim.current.buffer[line][left - 1]):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
253 left = left - 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
254 except IndexError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
255 pass
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
256 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
257 while word_char_RE.search(vim.current.buffer[line][right + 1]):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
258 right = right + 1
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
259 except IndexError:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
260 pass
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
261 return (left, right)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
262
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
263 annot = Annotations() # global annotation object
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
264
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
265 def printOCamlType(mode):
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
266 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
267 if mode == "visual": # visual mode: lookup highlighted text
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
268 (line1, col1) = vim.current.buffer.mark("<")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
269 (line2, col2) = vim.current.buffer.mark(">")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
270 else: # any other mode: lookup word at cursor position
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
271 (line, col) = vim.current.window.cursor
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
272 (col1, col2) = findBoundaries(line, col)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
273 (line1, line2) = (line, line)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
274 begin_mark = (line1, col1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
275 end_mark = (line2, col2 + 1)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
276 print annot.get_type(begin_mark, end_mark)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
277 except AnnExc, exc:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
278 print exc.reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
279
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
280 def parseOCamlAnnot():
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
281 try:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
282 annot.parse()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
283 except AnnExc, exc:
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
284 print exc.reason
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
285
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
286 EOF
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
287
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
288 fun OCamlPrintType(current_mode)
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
289 if (a:current_mode == "visual")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
290 python printOCamlType("visual")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
291 else
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
292 python printOCamlType("normal")
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
293 endif
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
294 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
295
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
296 fun OCamlParseAnnot()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
297 python parseOCamlAnnot()
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
298 endfun
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
299
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
300 map <F3> :call OCamlPrintType("normal")<RETURN>
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 7
diff changeset
301 vmap <F3> :call OCamlPrintType("visual")<RETURN>