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