├── .gitignore ├── LICENSE_MIT.txt ├── README.md ├── autoload ├── ocpindent.py └── ocpindent.vim └── indent ├── ocaml.vim └── omlet.vim /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /LICENSE_MIT.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Frédéric Bour. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Use with ocp-indent >= 1.2.0. 2 | 3 | Just add this directory to your runtime path. 4 | 5 | set rtp+= 6 | 7 | BEWARE: By default, ocp-indent only works with spaces, not tabs. So be sure to `:set expandtab` either globally or for your ocaml files, for example by putting a line au FileType ocaml setlocal expandtab in your `.vimrc` 8 | 9 | Released under MIT License. 10 | 11 | ### Passing custom arguments to ocp-indent 12 | 13 | You can set the variables ```g:ocp_indent_args``` (global settings) or ```b:ocp_indent_args``` (buffer local settings) to pass arguments to ocp-indent. 14 | 15 | Set variable to a string or a list of strings. 16 | 17 | This may work for enabling tab-based indenting if required -- see the ocp-indent documentation. 18 | -------------------------------------------------------------------------------- /autoload/ocpindent.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import vim 4 | import time 5 | import subprocess 6 | 7 | ocp_indent_path = "ocp-indent" 8 | ocp_lastline = None 9 | ocp_lasttime = None 10 | ocp_linefst = 0 11 | ocp_linebuf = [] 12 | ocp_inarow = 0 13 | 14 | def ocp_indent(lines): 15 | if lines: 16 | if type(lines) == int: 17 | end = lines 18 | lines = "%d-%d" % (lines,lines) 19 | else: 20 | end = lines[1] 21 | lines = "%d-%d" % lines 22 | end += 1 23 | 24 | buffer = vim.current.buffer 25 | content = buffer[:end] 26 | content = "\n".join(content) 27 | 28 | # Fix https://github.com/def-lkb/ocp-indent-vim/issues/4 29 | in_comment = content.count("(*") > content.count("*)") 30 | 31 | # Find end of comment (~ ignoring nested comments) or next non-empty line 32 | offset0 = 34 33 | offset1 = 55 34 | while end < len(buffer): 35 | # I like rabbits 36 | offset = offset0 + offset1 37 | offset0 = offset1 38 | offset1 = offset 39 | 40 | end0 = end 41 | end += offset 42 | 43 | padding = "\n".join(buffer[end0:end]) 44 | content = content + "\n" + padding 45 | if in_comment: 46 | if padding.count("*)") > 0: 47 | break 48 | else: 49 | if padding.strip() != "": 50 | break 51 | args = vim.eval("exists('b:ocp_indent_args') ? b:ocp_indent_args : exists ('g:ocp_indent_args') ? g:ocp_indent_args : []") 52 | if type(args) != list: 53 | args = [args] 54 | process = subprocess.Popen( 55 | [ocp_indent_path] + args + ["--lines",lines,"--numeric"], 56 | stdin=subprocess.PIPE, stdout=subprocess.PIPE, 57 | universal_newlines=True, 58 | stderr=open(os.devnull,"w")) 59 | out, _ = process.communicate(input=content) 60 | return [int(line) for line in out.splitlines()] 61 | 62 | def vim_contiguous(line1, line2): 63 | if not line1 or not line2: return False 64 | if line2 == line1 - 1: return True 65 | if line1 >= line2: return False 66 | if "".join(vim.current.buffer[line1+1:line2-1]).strip() == "": return True 67 | return False 68 | 69 | def vim_indentline(): 70 | global ocp_lastline, ocp_lasttime, ocp_linefst, ocp_linebuf, ocp_inarow 71 | line = int(vim.eval("v:lnum")) 72 | if vim_contiguous(ocp_lastline,line) and abs(time.time() - ocp_lasttime) < 0.1: 73 | # Possibly a selection indentation, use a threshold to detect consecutive calls 74 | if ocp_inarow > 2: 75 | if not (line >= ocp_linefst and line < ocp_linefst + len(ocp_linebuf)): 76 | ocp_linefst = line 77 | ocp_linebuf = ocp_indent((line, min(line + 1000, len(vim.current.buffer)))) 78 | ocp_lasttime = time.time() 79 | ocp_lastline = line 80 | return ocp_linebuf[line - ocp_linefst] 81 | else: 82 | # Increment counter 83 | ocp_inarow += 1 84 | else: 85 | # Not a selection indentation 86 | ocp_inarow = 0 87 | 88 | # Current line indentation 89 | ocp_linebuf = [] 90 | indent = ocp_indent(line) 91 | indent = indent.pop() 92 | ocp_lasttime = time.time() 93 | ocp_lastline = line 94 | return indent 95 | 96 | def vim_equal(): 97 | r = vim.current.range 98 | w = vim.current.window 99 | pos = w.cursor 100 | vim.command("0,'>!%s --lines %d-" % (ocp_indent_path, r.start+1)) 101 | w.cursor = pos 102 | -------------------------------------------------------------------------------- /autoload/ocpindent.vim: -------------------------------------------------------------------------------- 1 | let s:current_dir=expand(":p:h") 2 | if has("python") 3 | py <,0;;,0>],0\|],0>},0\|,0},0],0) 13 | setlocal equalprg= 14 | setlocal expandtab 15 | -------------------------------------------------------------------------------- /indent/omlet.vim: -------------------------------------------------------------------------------- 1 | if exists("b:did_indent") || expand('%:e') == 'mly' 2 | finish 3 | endif 4 | let b:did_indent = 1 5 | if expand('%:e') == 'mll' 6 | let b:ocp_indent_args="--syntax=mll" 7 | endif 8 | 9 | call ocpindent#init() 10 | 11 | setlocal indentexpr=ocpindent#OcpIndentLine() 12 | setlocal indentkeys=0{,0},:,0#,!^F,o,O,e,0=and\ ,0=class\ ,0=constraint\ ,0=done\ ,0=else\ ,0=end\ ,0=exception\ ,0=external\ ,0=if\ ,0=in\ ,0=include\ ,0=inherit\ ,0=initializer\ ,0=let\ ,0=module\ ,0=struct\ ,0=sig\ ,0=method\ ,0=open\ ,0=then\ ,0=type\ ,0=val\ ,0=with\ ,*,0;;,0>],0\|],0>},0\|,0},0],0) 13 | setlocal equalprg= 14 | setlocal expandtab 15 | --------------------------------------------------------------------------------