]> git.rmz.io Git - dotfiles.git/blob - vim/indent/cmake.vim
854e3cbf28d89c1b8232a33f50fdf2da1226797c
[dotfiles.git] / vim / indent / cmake.vim
1 " Vim indent file
2 " Program: CMake - Cross-Platform Makefile Generator
3 " Module: $RCSfile: cmake-indent.vim,v $
4 " Language: CMake (ft=cmake)
5 " Author: Andy Cedilnik <andy.cedilnik@kitware.com>
6 " Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
7 " Last Change: $Date: 2008-01-16 16:53:53 $
8 " Version: $Revision: 1.9 $
9 "
10 " Licence: The CMake license applies to this file. See
11 " http://www.cmake.org/HTML/Copyright.html
12 " This implies that distribution with Vim is allowed
13
14 if exists("b:did_indent")
15 finish
16 endif
17 let b:did_indent = 1
18
19 setlocal indentexpr=CMakeGetIndent(v:lnum)
20 setlocal indentkeys+=0=~ENDIF(,0=~ENDFOREACH(,0=~ENDMACRO(,0=~ELSE(,0=~ELSEIF(,0=~ENDWHILE(
21
22 " Only define the function once.
23 if exists("*CMakeGetIndent")
24 finish
25 endif
26 let s:keepcpo= &cpo
27 set cpo&vim
28
29 fun! CMakeGetIndent(lnum)
30 let this_line = getline(a:lnum)
31
32 " Find a non-blank line above the current line.
33 let lnum = a:lnum
34 let lnum = prevnonblank(lnum - 1)
35 let previous_line = getline(lnum)
36
37 " Hit the start of the file, use zero indent.
38 if lnum == 0
39 return 0
40 endif
41
42 let ind = indent(lnum)
43
44 let or = '\|'
45 " Regular expressions used by line indentation function.
46 let cmake_regex_comment = '#.*'
47 let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
48 let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
49 let cmake_regex_arguments = '\(' . cmake_regex_quoted .
50 \ or . '\$(' . cmake_regex_identifier . ')' .
51 \ or . '[^()\\#"]' . or . '\\.' . '\)*'
52
53 let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
54 let cmake_indent_blank_regex = '^\s*$'
55 let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
56 \ '\s*(' . cmake_regex_arguments .
57 \ '\(' . cmake_regex_comment . '\)\?$'
58
59 let cmake_indent_close_regex = '^' . cmake_regex_arguments .
60 \ ')\s*' .
61 \ '\(' . cmake_regex_comment . '\)\?$'
62
63 let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
64 let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*(' .
65 \ or . ')\s*$'
66
67 " Add
68 if previous_line =~? cmake_indent_comment_line " Handle comments
69 let ind = ind
70 else
71 if previous_line =~? cmake_indent_begin_regex
72 let ind = ind + &sw
73 endif
74 if previous_line =~? cmake_indent_open_regex
75 let ind = ind + &sw
76 endif
77 endif
78
79 " Subtract
80 if this_line =~? cmake_indent_end_regex
81 let ind = ind - &sw
82 endif
83 if previous_line =~? cmake_indent_close_regex
84 let ind = ind - &sw
85 endif
86
87 return ind
88 endfun
89
90 let &cpo = s:keepcpo
91 unlet s:keepcpo