blob: 93a131b93839f74d96cf8aca33bd9e07d1b2fd4d [file] [log] [blame]
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001" Vim indent file
Bram Moolenaar945e2db2010-06-05 17:43:32 +02002" Program: CMake - Cross-Platform Makefile Generator
3" Module: $RCSfile: cmake-indent.vim,v $
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004" Language: CMake (ft=cmake)
5" Author: Andy Cedilnik <andy.cedilnik@kitware.com>
Bram Moolenaar945e2db2010-06-05 17:43:32 +02006" Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
7" Last Change: $Date: 2008-01-16 16:53:53 $
8" Version: $Revision: 1.9 $
Bram Moolenaarc1e37902006-04-18 21:55:01 +00009"
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
14if exists("b:did_indent")
15 finish
16endif
17let b:did_indent = 1
18
19setlocal indentexpr=CMakeGetIndent(v:lnum)
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
Bram Moolenaarc1e37902006-04-18 21:55:01 +000021
22" Only define the function once.
23if exists("*CMakeGetIndent")
24 finish
25endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020026let s:keepcpo= &cpo
27set cpo&vim
Bram Moolenaarc1e37902006-04-18 21:55:01 +000028
29fun! 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
Bram Moolenaar57657d82006-04-21 22:12:41 +000054 let cmake_indent_blank_regex = '^\s*$'
Bram Moolenaarc1e37902006-04-18 21:55:01 +000055 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
Bram Moolenaar945e2db2010-06-05 17:43:32 +020063 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*('
Bram Moolenaarc1e37902006-04-18 21:55:01 +000065
66 " Add
67 if previous_line =~? cmake_indent_comment_line " Handle comments
68 let ind = ind
69 else
70 if previous_line =~? cmake_indent_begin_regex
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020071 let ind = ind + shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000072 endif
73 if previous_line =~? cmake_indent_open_regex
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020074 let ind = ind + shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000075 endif
76 endif
77
78 " Subtract
79 if this_line =~? cmake_indent_end_regex
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020080 let ind = ind - shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000081 endif
82 if previous_line =~? cmake_indent_close_regex
Bram Moolenaar3ec574f2017-06-13 18:12:01 +020083 let ind = ind - shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000084 endif
85
86 return ind
87endfun
Bram Moolenaar8e52a592012-05-18 21:49:28 +020088
89let &cpo = s:keepcpo
90unlet s:keepcpo