Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 1 | " Language: OCaml |
| 2 | " Maintainer: David Baelde <firstname.name@ens-lyon.org> |
| 3 | " Mike Leary <leary@nwlink.com> |
| 4 | " Markus Mottl <markus.mottl@gmail.com> |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 5 | " Pierre Vittet <pierre-vittet@pvittet.com> |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 6 | " Stefano Zacchiroli <zack@bononia.it> |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 7 | " Vincent Aravantinos <firstname.name@imag.fr> |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 8 | " URL: http://www.ocaml.info/vim/ftplugin/ocaml.vim |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 9 | " Last Change: |
| 10 | " 2012 Jan 15 - Bugfix :reloading .annot file does not close |
| 11 | " splitted view (Pierre Vittet) |
| 12 | " 2011 Nov 28 - Bugfix + support of multiple ml annotation file |
| 13 | " (Pierre Vittet) |
| 14 | " 2010 Jul 10 - Bugfix, thanks to Pat Rondon |
Bram Moolenaar | 9c754c4 | 2010-07-10 15:52:35 +0200 | [diff] [blame] | 15 | " 2008 Jul 17 - Bugfix related to fnameescape (VA) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 16 | |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 17 | if exists("b:did_ftplugin") |
| 18 | finish |
| 19 | endif |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 20 | let b:did_ftplugin=1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 21 | |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 22 | " some macro |
| 23 | if exists('*fnameescape') |
| 24 | function! s:Fnameescape(s) |
| 25 | return fnameescape(a:s) |
| 26 | endfun |
| 27 | else |
| 28 | function! s:Fnameescape(s) |
| 29 | return escape(a:s," \t\n*?[{`$\\%#'\"|!<") |
| 30 | endfun |
| 31 | endif |
| 32 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 33 | " Error handling -- helps moving where the compiler wants you to go |
| 34 | let s:cposet=&cpoptions |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 35 | set cpo&vim |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 36 | setlocal efm= |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 37 | \%EFile\ \"%f\"\\,\ line\ %l\\,\ characters\ %c-%*\\d:, |
| 38 | \%EFile\ \"%f\"\\,\ line\ %l\\,\ character\ %c:%m, |
| 39 | \%+EReference\ to\ unbound\ regexp\ name\ %m, |
| 40 | \%Eocamlyacc:\ e\ -\ line\ %l\ of\ \"%f\"\\,\ %m, |
| 41 | \%Wocamlyacc:\ w\ -\ %m, |
| 42 | \%-Zmake%.%#, |
| 43 | \%C%m, |
| 44 | \%D%*\\a[%*\\d]:\ Entering\ directory\ `%f', |
| 45 | \%X%*\\a[%*\\d]:\ Leaving\ directory\ `%f', |
| 46 | \%D%*\\a:\ Entering\ directory\ `%f', |
| 47 | \%X%*\\a:\ Leaving\ directory\ `%f', |
| 48 | \%DMaking\ %*\\a\ in\ %f |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 49 | |
| 50 | " Add mappings, unless the user didn't want this. |
| 51 | if !exists("no_plugin_maps") && !exists("no_ocaml_maps") |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 52 | " (un)commenting |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 53 | if !hasmapto('<Plug>Comment') |
| 54 | nmap <buffer> <LocalLeader>c <Plug>LUncomOn |
| 55 | vmap <buffer> <LocalLeader>c <Plug>BUncomOn |
| 56 | nmap <buffer> <LocalLeader>C <Plug>LUncomOff |
| 57 | vmap <buffer> <LocalLeader>C <Plug>BUncomOff |
| 58 | endif |
| 59 | |
| 60 | nnoremap <buffer> <Plug>LUncomOn mz0i(* <ESC>$A *)<ESC>`z |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 61 | nnoremap <buffer> <Plug>LUncomOff :s/^(\* \(.*\) \*)/\1/<CR>:noh<CR> |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | vnoremap <buffer> <Plug>BUncomOn <ESC>:'<,'><CR>`<O<ESC>0i(*<ESC>`>o<ESC>0i*)<ESC>`< |
| 63 | vnoremap <buffer> <Plug>BUncomOff <ESC>:'<,'><CR>`<dd`>dd`< |
| 64 | |
| 65 | if !hasmapto('<Plug>Abbrev') |
Bram Moolenaar | 4c3f536 | 2006-04-11 21:38:50 +0000 | [diff] [blame] | 66 | iabbrev <buffer> ASS (assert (0=1) (* XXX *)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 67 | endif |
| 68 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 69 | |
| 70 | " Let % jump between structure elements (due to Issac Trotts) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 71 | let b:mw = '' |
| 72 | let b:mw = b:mw . ',\<let\>:\<and\>:\(\<in\>\|;;\)' |
| 73 | let b:mw = b:mw . ',\<if\>:\<then\>:\<else\>' |
| 74 | let b:mw = b:mw . ',\<\(for\|while\)\>:\<do\>:\<done\>,' |
| 75 | let b:mw = b:mw . ',\<\(object\|sig\|struct\|begin\)\>:\<end\>' |
| 76 | let b:mw = b:mw . ',\<\(match\|try\)\>:\<with\>' |
| 77 | let b:match_words = b:mw |
| 78 | |
| 79 | let b:match_ignorecase=0 |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 80 | |
| 81 | " switching between interfaces (.mli) and implementations (.ml) |
| 82 | if !exists("g:did_ocaml_switch") |
| 83 | let g:did_ocaml_switch = 1 |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 84 | map <LocalLeader>s :call OCaml_switch(0)<CR> |
| 85 | map <LocalLeader>S :call OCaml_switch(1)<CR> |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 86 | fun OCaml_switch(newwin) |
| 87 | if (match(bufname(""), "\\.mli$") >= 0) |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 88 | let fname = s:Fnameescape(substitute(bufname(""), "\\.mli$", ".ml", "")) |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 89 | if (a:newwin == 1) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 90 | exec "new " . fname |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 91 | else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 92 | exec "arge " . fname |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 93 | endif |
| 94 | elseif (match(bufname(""), "\\.ml$") >= 0) |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 95 | let fname = s:Fnameescape(bufname("")) . "i" |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 96 | if (a:newwin == 1) |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 97 | exec "new " . fname |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 98 | else |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 99 | exec "arge " . fname |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 100 | endif |
| 101 | endif |
| 102 | endfun |
| 103 | endif |
| 104 | |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 105 | " Folding support |
| 106 | |
| 107 | " Get the modeline because folding depends on indentation |
| 108 | let s:s = line2byte(line('.'))+col('.')-1 |
| 109 | if search('^\s*(\*:o\?caml:') |
| 110 | let s:modeline = getline(".") |
| 111 | else |
| 112 | let s:modeline = "" |
| 113 | endif |
| 114 | if s:s > 0 |
| 115 | exe 'goto' s:s |
| 116 | endif |
| 117 | |
| 118 | " Get the indentation params |
| 119 | let s:m = matchstr(s:modeline,'default\s*=\s*\d\+') |
| 120 | if s:m != "" |
| 121 | let s:idef = matchstr(s:m,'\d\+') |
| 122 | elseif exists("g:omlet_indent") |
| 123 | let s:idef = g:omlet_indent |
| 124 | else |
| 125 | let s:idef = 2 |
| 126 | endif |
| 127 | let s:m = matchstr(s:modeline,'struct\s*=\s*\d\+') |
| 128 | if s:m != "" |
| 129 | let s:i = matchstr(s:m,'\d\+') |
| 130 | elseif exists("g:omlet_indent_struct") |
| 131 | let s:i = g:omlet_indent_struct |
| 132 | else |
| 133 | let s:i = s:idef |
| 134 | endif |
| 135 | |
| 136 | " Set the folding method |
| 137 | if exists("g:ocaml_folding") |
| 138 | setlocal foldmethod=expr |
| 139 | setlocal foldexpr=OMLetFoldLevel(v:lnum) |
| 140 | endif |
| 141 | |
| 142 | " - Only definitions below, executed once ------------------------------------- |
| 143 | |
| 144 | if exists("*OMLetFoldLevel") |
| 145 | finish |
| 146 | endif |
| 147 | |
| 148 | function s:topindent(lnum) |
| 149 | let l = a:lnum |
| 150 | while l > 0 |
| 151 | if getline(l) =~ '\s*\%(\<struct\>\|\<sig\>\|\<object\>\)' |
| 152 | return indent(l) |
| 153 | endif |
| 154 | let l = l-1 |
| 155 | endwhile |
| 156 | return -s:i |
| 157 | endfunction |
| 158 | |
| 159 | function OMLetFoldLevel(l) |
| 160 | |
| 161 | " This is for not merging blank lines around folds to them |
| 162 | if getline(a:l) !~ '\S' |
| 163 | return -1 |
| 164 | endif |
| 165 | |
| 166 | " We start folds for modules, classes, and every toplevel definition |
| 167 | if getline(a:l) =~ '^\s*\%(\<val\>\|\<module\>\|\<class\>\|\<type\>\|\<method\>\|\<initializer\>\|\<inherit\>\|\<exception\>\|\<external\>\)' |
| 168 | exe 'return ">' (indent(a:l)/s:i)+1 '"' |
| 169 | endif |
| 170 | |
| 171 | " Toplevel let are detected thanks to the indentation |
| 172 | if getline(a:l) =~ '^\s*let\>' && indent(a:l) == s:i+s:topindent(a:l) |
| 173 | exe 'return ">' (indent(a:l)/s:i)+1 '"' |
| 174 | endif |
| 175 | |
| 176 | " We close fold on end which are associated to struct, sig or object. |
| 177 | " We use syntax information to do that. |
| 178 | if getline(a:l) =~ '^\s*end\>' && synIDattr(synID(a:l, indent(a:l)+1, 0), "name") != "ocamlKeyword" |
| 179 | return (indent(a:l)/s:i)+1 |
| 180 | endif |
| 181 | |
| 182 | " Folds end on ;; |
| 183 | if getline(a:l) =~ '^\s*;;' |
| 184 | exe 'return "<' (indent(a:l)/s:i)+1 '"' |
| 185 | endif |
| 186 | |
| 187 | " Comments around folds aren't merged to them. |
| 188 | if synIDattr(synID(a:l, indent(a:l)+1, 0), "name") == "ocamlComment" |
| 189 | return -1 |
| 190 | endif |
| 191 | |
| 192 | return '=' |
| 193 | endfunction |
| 194 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 195 | " Vim support for OCaml .annot files |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 196 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 197 | " Last Change: 2007 Jul 17 |
| 198 | " Maintainer: Vincent Aravantinos <vincent.aravantinos@gmail.com> |
| 199 | " License: public domain |
| 200 | " |
| 201 | " Originally inspired by 'ocaml-dtypes.vim' by Stefano Zacchiroli. |
| 202 | " The source code is quite radically different for we not use python anymore. |
| 203 | " However this plugin should have the exact same behaviour, that's why the |
| 204 | " following lines are the quite exact copy of Stefano's original plugin : |
| 205 | " |
| 206 | " << |
| 207 | " Executing Ocaml_print_type(<mode>) function will display in the Vim bottom |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 208 | " line(s) the type of an ocaml value getting it from the corresponding .annot |
| 209 | " file (if any). If Vim is in visual mode, <mode> should be "visual" and the |
| 210 | " selected ocaml value correspond to the highlighted text, otherwise (<mode> |
| 211 | " can be anything else) it corresponds to the literal found at the current |
| 212 | " cursor position. |
| 213 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 214 | " Typing '<LocalLeader>t' (LocalLeader defaults to '\', see :h LocalLeader) |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 215 | " will cause " Ocaml_print_type function to be invoked with the right |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 216 | " argument depending on the current mode (visual or not). |
| 217 | " >> |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 218 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 219 | " If you find something not matching this behaviour, please signal it. |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 220 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 221 | " Differences are: |
| 222 | " - no need for python support |
| 223 | " + plus : more portable |
| 224 | " + minus: no more lazy parsing, it looks very fast however |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 225 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 226 | " - ocamlbuild support, ie. |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 227 | " + the plugin finds the _build directory and looks for the |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 228 | " corresponding file inside; |
| 229 | " + if the user decides to change the name of the _build directory thanks |
| 230 | " to the '-build-dir' option of ocamlbuild, the plugin will manage in |
| 231 | " most cases to find it out (most cases = if the source file has a unique |
| 232 | " name among your whole project); |
| 233 | " + if ocamlbuild is not used, the usual behaviour holds; ie. the .annot |
| 234 | " file should be in the same directory as the source file; |
| 235 | " + for vim plugin programmers: |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 236 | " the variable 'b:_build_dir' contains the inferred path to the build |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 237 | " directory, even if this one is not named '_build'. |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 238 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 239 | " Bonus : |
| 240 | " - latin1 accents are handled |
| 241 | " - lists are handled, even on multiple lines, you don't need the visual mode |
| 242 | " (the cursor must be on the first bracket) |
| 243 | " - parenthesized expressions, arrays, and structures (ie. '(...)', '[|...|]', |
| 244 | " and '{...}') are handled the same way |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 245 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 246 | " Copied from Stefano's original plugin : |
| 247 | " << |
| 248 | " .annot ocaml file representation |
| 249 | " |
| 250 | " File format (copied verbatim from caml-types.el) |
| 251 | " |
| 252 | " file ::= block * |
| 253 | " block ::= position <SP> position <LF> annotation * |
| 254 | " position ::= filename <SP> num <SP> num <SP> num |
| 255 | " annotation ::= keyword open-paren <LF> <SP> <SP> data <LF> close-paren |
| 256 | " |
| 257 | " <SP> is a space character (ASCII 0x20) |
| 258 | " <LF> is a line-feed character (ASCII 0x0A) |
| 259 | " num is a sequence of decimal digits |
| 260 | " filename is a string with the lexical conventions of O'Caml |
| 261 | " open-paren is an open parenthesis (ASCII 0x28) |
| 262 | " close-paren is a closed parenthesis (ASCII 0x29) |
| 263 | " data is any sequence of characters where <LF> is always followed by |
| 264 | " at least two space characters. |
| 265 | " |
| 266 | " - in each block, the two positions are respectively the start and the |
| 267 | " end of the range described by the block. |
| 268 | " - in a position, the filename is the name of the file, the first num |
| 269 | " is the line number, the second num is the offset of the beginning |
| 270 | " of the line, the third num is the offset of the position itself. |
| 271 | " - the char number within the line is the difference between the third |
| 272 | " and second nums. |
| 273 | " |
| 274 | " For the moment, the only possible keyword is \"type\"." |
| 275 | " >> |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 276 | |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 277 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 278 | " 1. Finding the annotation file even if we use ocamlbuild |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 279 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 280 | " In: two strings representing paths |
| 281 | " Out: one string representing the common prefix between the two paths |
| 282 | function! s:Find_common_path (p1,p2) |
| 283 | let temp = a:p2 |
| 284 | while matchstr(a:p1,temp) == '' |
| 285 | let temp = substitute(temp,'/[^/]*$','','') |
| 286 | endwhile |
| 287 | return temp |
| 288 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 289 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 290 | " After call: |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 291 | " |
| 292 | " Following information have been put in s:annot_file_list, using |
| 293 | " annot_file_name name as key: |
| 294 | " - annot_file_path : |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 295 | " path to the .annot file corresponding to the |
| 296 | " source file (dealing with ocamlbuild stuff) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 297 | " - _build_path: |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 298 | " path to the build directory even if this one is |
| 299 | " not named '_build' |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 300 | " - date_of_last annot: |
| 301 | " Set to 0 until we load the file. It contains the |
| 302 | " date at which the file has been loaded. |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 303 | function! s:Locate_annotation() |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 304 | let annot_file_name = s:Fnameescape(expand('%:t:r')).'.annot' |
| 305 | if !exists ("s:annot_file_list[annot_file_name]") |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 306 | silent exe 'cd' s:Fnameescape(expand('%:p:h')) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 307 | " 1st case : the annot file is in the same directory as the buffer (no ocamlbuild) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 308 | let annot_file_path = findfile(annot_file_name,'.') |
| 309 | if annot_file_path != '' |
| 310 | let annot_file_path = getcwd().'/'.annot_file_path |
| 311 | let _build_path = '' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 312 | else |
| 313 | " 2nd case : the buffer and the _build directory are in the same directory |
| 314 | " .. |
| 315 | " / \ |
| 316 | " / \ |
| 317 | " _build .ml |
| 318 | " |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 319 | let _build_path = finddir('_build','.') |
| 320 | if _build_path != '' |
| 321 | let _build_path = getcwd().'/'._build_path |
| 322 | let annot_file_path = findfile(annot_file_name,'_build') |
| 323 | if annot_file_path != '' |
| 324 | let annot_file_path = getcwd().'/'.annot_file_path |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 325 | endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 326 | else |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 327 | " 3rd case : the _build directory is in a directory higher in the file hierarchy |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 328 | " (it can't be deeper by ocamlbuild requirements) |
| 329 | " .. |
| 330 | " / \ |
| 331 | " / \ |
| 332 | " _build ... |
| 333 | " \ |
| 334 | " \ |
| 335 | " .ml |
| 336 | " |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 337 | let _build_path = finddir('_build',';') |
| 338 | if _build_path != '' |
| 339 | let project_path = substitute(_build_path,'/_build$','','') |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 340 | let path_relative_to_project = s:Fnameescape(substitute(expand('%:p:h'),project_path.'/','','')) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 341 | let annot_file_path = findfile(annot_file_name,project_path.'/_build/'.path_relative_to_project) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 342 | else |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 343 | let annot_file_path = findfile(annot_file_name,'**') |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 344 | "4th case : what if the user decided to change the name of the _build directory ? |
| 345 | " -> we relax the constraints, it should work in most cases |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 346 | if annot_file_path != '' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 347 | " 4a. we suppose the renamed _build directory is in the current directory |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 348 | let _build_path = matchstr(annot_file_path,'^[^/]*') |
| 349 | if annot_file_path != '' |
| 350 | let annot_file_path = getcwd().'/'.annot_file_path |
| 351 | let _build_path = getcwd().'/'._build_path |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 352 | endif |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 353 | else |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 354 | let annot_file_name = '' |
| 355 | "(Pierre Vittet: I have commented 4b because this was chrashing |
| 356 | "my vim (it produced infinite loop)) |
| 357 | " |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 358 | " 4b. anarchy : the renamed _build directory may be higher in the hierarchy |
| 359 | " this will work if the file for which we are looking annotations has a unique name in the whole project |
| 360 | " if this is not the case, it may still work, but no warranty here |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 361 | "let annot_file_path = findfile(annot_file_name,'**;') |
| 362 | "let project_path = s:Find_common_path(annot_file_path,expand('%:p:h')) |
| 363 | "let _build_path = matchstr(annot_file_path,project_path.'/[^/]*') |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 364 | endif |
| 365 | endif |
| 366 | endif |
| 367 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 368 | |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 369 | if annot_file_path == '' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 370 | throw 'E484: no annotation file found' |
| 371 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 372 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 373 | silent exe 'cd' '-' |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 374 | let s:annot_file_list[annot_file_name]= [annot_file_path, _build_path, 0] |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 375 | endif |
| 376 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 377 | |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 378 | " This variable contain a dictionnary of list. Each element of the dictionnary |
| 379 | " represent an annotation system. An annotation system is a list with : |
| 380 | " - annotation file name as it's key |
| 381 | " - annotation file path as first element of the contained list |
| 382 | " - build path as second element of the contained list |
| 383 | " - annot_file_last_mod (contain the date of .annot file) as third element |
| 384 | let s:annot_file_list = {} |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 385 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 386 | " 2. Finding the type information in the annotation file |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 387 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 388 | " a. The annotation file is opened in vim as a buffer that |
| 389 | " should be (almost) invisible to the user. |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 390 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 391 | " After call: |
| 392 | " The current buffer is now the one containing the .annot file. |
| 393 | " We manage to keep all this hidden to the user's eye. |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 394 | function! s:Enter_annotation_buffer(annot_file_path) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 395 | let s:current_pos = getpos('.') |
| 396 | let s:current_hidden = &l:hidden |
| 397 | set hidden |
| 398 | let s:current_buf = bufname('%') |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 399 | if bufloaded(a:annot_file_path) |
| 400 | silent exe 'keepj keepalt' 'buffer' s:Fnameescape(a:annot_file_path) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 401 | else |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 402 | silent exe 'keepj keepalt' 'view' s:Fnameescape(a:annot_file_path) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 403 | endif |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 404 | call setpos(".", [0, 0 , 0 , 0]) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 405 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 406 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 407 | " After call: |
| 408 | " The original buffer has been restored in the exact same state as before. |
| 409 | function! s:Exit_annotation_buffer() |
Bram Moolenaar | e37d50a | 2008-08-06 17:06:04 +0000 | [diff] [blame] | 410 | silent exe 'keepj keepalt' 'buffer' s:Fnameescape(s:current_buf) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 411 | let &l:hidden = s:current_hidden |
| 412 | call setpos('.',s:current_pos) |
| 413 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 414 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 415 | " After call: |
| 416 | " The annot file is loaded and assigned to a buffer. |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 417 | " This also handles the modification date of the .annot file, eg. after a |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 418 | " compilation (return an updated annot_file_list). |
| 419 | function! s:Load_annotation(annot_file_name) |
| 420 | let annot = s:annot_file_list[a:annot_file_name] |
| 421 | let annot_file_path = annot[0] |
| 422 | let annot_file_last_mod = 0 |
| 423 | if exists("annot[2]") |
| 424 | let annot_file_last_mod = annot[2] |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 425 | endif |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 426 | if bufloaded(annot_file_path) && annot_file_last_mod < getftime(annot_file_path) |
| 427 | " if there is a more recent file |
| 428 | let nr = bufnr(annot_file_path) |
| 429 | silent exe 'keepj keepalt' 'bunload' nr |
| 430 | endif |
| 431 | if !bufloaded(annot_file_path) |
| 432 | call s:Enter_annotation_buffer(annot_file_path) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 433 | setlocal nobuflisted |
| 434 | setlocal bufhidden=hide |
| 435 | setlocal noswapfile |
| 436 | setlocal buftype=nowrite |
| 437 | call s:Exit_annotation_buffer() |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 438 | let annot[2] = getftime(annot_file_path) |
| 439 | " List updated with the new date |
| 440 | let s:annot_file_list[a:annot_file_name] = annot |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 441 | endif |
| 442 | endfun |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 443 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 444 | "b. 'search' and 'match' work to find the type information |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 445 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 446 | "In: - lin1,col1: postion of expression first char |
| 447 | " - lin2,col2: postion of expression last char |
| 448 | "Out: - the pattern to be looked for to find the block |
| 449 | " Must be called in the source buffer (use of line2byte) |
| 450 | function! s:Block_pattern(lin1,lin2,col1,col2) |
| 451 | let start_num1 = a:lin1 |
| 452 | let start_num2 = line2byte(a:lin1) - 1 |
| 453 | let start_num3 = start_num2 + a:col1 |
Bram Moolenaar | 9c754c4 | 2010-07-10 15:52:35 +0200 | [diff] [blame] | 454 | let path = '"\(\\"\|[^"]\)\+"' |
| 455 | let start_pos = path.' '.start_num1.' '.start_num2.' '.start_num3 |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 456 | let end_num1 = a:lin2 |
| 457 | let end_num2 = line2byte(a:lin2) - 1 |
| 458 | let end_num3 = end_num2 + a:col2 |
Bram Moolenaar | 9c754c4 | 2010-07-10 15:52:35 +0200 | [diff] [blame] | 459 | let end_pos = path.' '.end_num1.' '.end_num2.' '.end_num3 |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 460 | return '^'.start_pos.' '.end_pos."$" |
| 461 | " rq: the '^' here is not totally correct regarding the annot file "grammar" |
| 462 | " but currently the annotation file respects this, and it's a little bit faster with the '^'; |
| 463 | " can be removed safely. |
| 464 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 465 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 466 | "In: (the cursor position should be at the start of an annotation) |
| 467 | "Out: the type information |
| 468 | " Must be called in the annotation buffer (use of search) |
| 469 | function! s:Match_data() |
| 470 | " rq: idem as previously, in the following, the '^' at start of patterns is not necessary |
| 471 | keepj while search('^type($','ce',line(".")) == 0 |
| 472 | keepj if search('^.\{-}($','e') == 0 |
| 473 | throw "no_annotation" |
| 474 | endif |
| 475 | keepj if searchpair('(','',')') == 0 |
| 476 | throw "malformed_annot_file" |
| 477 | endif |
| 478 | endwhile |
| 479 | let begin = line(".") + 1 |
| 480 | keepj if searchpair('(','',')') == 0 |
| 481 | throw "malformed_annot_file" |
| 482 | endif |
| 483 | let end = line(".") - 1 |
| 484 | return join(getline(begin,end),"\n") |
| 485 | endfun |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 486 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 487 | "In: the pattern to look for in order to match the block |
| 488 | "Out: the type information (calls s:Match_data) |
| 489 | " Should be called in the annotation buffer |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 490 | function! s:Extract_type_data(block_pattern, annot_file_name) |
| 491 | let annot_file_path = s:annot_file_list[a:annot_file_name][0] |
| 492 | call s:Enter_annotation_buffer(annot_file_path) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 493 | try |
| 494 | if search(a:block_pattern,'e') == 0 |
| 495 | throw "no_annotation" |
| 496 | endif |
| 497 | call cursor(line(".") + 1,1) |
| 498 | let annotation = s:Match_data() |
| 499 | finally |
| 500 | call s:Exit_annotation_buffer() |
| 501 | endtry |
| 502 | return annotation |
| 503 | endfun |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 504 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 505 | "c. link this stuff with what the user wants |
| 506 | " ie. get the expression selected/under the cursor |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 507 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 508 | let s:ocaml_word_char = '\w|[À-ÿ]|''' |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 509 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 510 | "In: the current mode (eg. "visual", "normal", etc.) |
| 511 | "Out: the borders of the expression we are looking for the type |
| 512 | function! s:Match_borders(mode) |
| 513 | if a:mode == "visual" |
| 514 | let cur = getpos(".") |
| 515 | normal `< |
| 516 | let col1 = col(".") |
| 517 | let lin1 = line(".") |
| 518 | normal `> |
| 519 | let col2 = col(".") |
| 520 | let lin2 = line(".") |
| 521 | call cursor(cur[1],cur[2]) |
| 522 | return [lin1,lin2,col1-1,col2] |
| 523 | else |
| 524 | let cursor_line = line(".") |
| 525 | let cursor_col = col(".") |
| 526 | let line = getline('.') |
| 527 | if line[cursor_col-1:cursor_col] == '[|' |
| 528 | let [lin2,col2] = searchpairpos('\[|','','|\]','n') |
| 529 | return [cursor_line,lin2,cursor_col-1,col2+1] |
| 530 | elseif line[cursor_col-1] == '[' |
| 531 | let [lin2,col2] = searchpairpos('\[','','\]','n') |
| 532 | return [cursor_line,lin2,cursor_col-1,col2] |
| 533 | elseif line[cursor_col-1] == '(' |
| 534 | let [lin2,col2] = searchpairpos('(','',')','n') |
| 535 | return [cursor_line,lin2,cursor_col-1,col2] |
| 536 | elseif line[cursor_col-1] == '{' |
| 537 | let [lin2,col2] = searchpairpos('{','','}','n') |
| 538 | return [cursor_line,lin2,cursor_col-1,col2] |
| 539 | else |
| 540 | let [lin1,col1] = searchpos('\v%('.s:ocaml_word_char.'|\.)*','ncb') |
| 541 | let [lin2,col2] = searchpos('\v%('.s:ocaml_word_char.'|\.)*','nce') |
| 542 | if col1 == 0 || col2 == 0 |
| 543 | throw "no_expression" |
| 544 | endif |
| 545 | return [cursor_line,cursor_line,col1-1,col2] |
| 546 | endif |
| 547 | endif |
| 548 | endfun |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 549 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 550 | "In: the current mode (eg. "visual", "normal", etc.) |
| 551 | "Out: the type information (calls s:Extract_type_data) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 552 | function! s:Get_type(mode, annot_file_name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 553 | let [lin1,lin2,col1,col2] = s:Match_borders(a:mode) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 554 | return s:Extract_type_data(s:Block_pattern(lin1,lin2,col1,col2), a:annot_file_name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 555 | endfun |
Bram Moolenaar | f1568ec | 2011-12-14 21:17:39 +0100 | [diff] [blame] | 556 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 557 | "d. main |
| 558 | "In: the current mode (eg. "visual", "normal", etc.) |
| 559 | "After call: the type information is displayed |
| 560 | if !exists("*Ocaml_get_type") |
| 561 | function Ocaml_get_type(mode) |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 562 | let annot_file_name = s:Fnameescape(expand('%:t:r')).'.annot' |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 563 | call s:Locate_annotation() |
Bram Moolenaar | b8ff1fb | 2012-02-04 21:59:01 +0100 | [diff] [blame] | 564 | call s:Load_annotation(annot_file_name) |
| 565 | return s:Get_type(a:mode, annot_file_name) |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 566 | endfun |
| 567 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 568 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 569 | if !exists("*Ocaml_get_type_or_not") |
| 570 | function Ocaml_get_type_or_not(mode) |
| 571 | let t=reltime() |
| 572 | try |
| 573 | return Ocaml_get_type(a:mode) |
| 574 | catch |
| 575 | return "" |
| 576 | endtry |
| 577 | endfun |
| 578 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 579 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 580 | if !exists("*Ocaml_print_type") |
| 581 | function Ocaml_print_type(mode) |
| 582 | if expand("%:e") == "mli" |
| 583 | echohl ErrorMsg | echo "No annotations for interface (.mli) files" | echohl None |
| 584 | return |
| 585 | endif |
| 586 | try |
| 587 | echo Ocaml_get_type(a:mode) |
| 588 | catch /E484:/ |
| 589 | echohl ErrorMsg | echo "No type annotations (.annot) file found" | echohl None |
| 590 | catch /no_expression/ |
| 591 | echohl ErrorMsg | echo "No expression found under the cursor" | echohl None |
| 592 | catch /no_annotation/ |
| 593 | echohl ErrorMsg | echo "No type annotation found for the given text" | echohl None |
| 594 | catch /malformed_annot_file/ |
| 595 | echohl ErrorMsg | echo "Malformed .annot file" | echohl None |
| 596 | endtry |
| 597 | endfun |
| 598 | endif |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 599 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 600 | " Maps |
| 601 | map <silent> <LocalLeader>t :call Ocaml_print_type("normal")<CR> |
| 602 | vmap <silent> <LocalLeader>t :<C-U>call Ocaml_print_type("visual")<CR>`< |
Bram Moolenaar | a5792f5 | 2005-11-23 21:25:05 +0000 | [diff] [blame] | 603 | |
| 604 | let &cpoptions=s:cposet |
| 605 | unlet s:cposet |
| 606 | |
Bram Moolenaar | 3577c6f | 2008-06-24 21:16:56 +0000 | [diff] [blame] | 607 | " vim:sw=2 fdm=indent |