2 " Language: CMake (ft=cmake)
3 " Author: Andy Cedilnik <andy.cedilnik@kitware.com>
4 " Maintainer: Dimitri Merejkowsky <d.merej@gmail.com>
5 " Former Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
6 " Last Change: 2022 Mar 22
8 " License: The CMake license applies to this file. See
9 " https://cmake.org/licensing
10 " This implies that distribution with Vim is allowed
12 if exists("b:did_indent")
17 setlocal indentexpr=CMakeGetIndent(v:lnum)
18 setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
20 " Only define the function once.
21 if exists("*CMakeGetIndent")
27 fun! CMakeGetIndent(lnum)
28 let this_line = getline(a:lnum)
30 " Find a non-blank line above the current line.
32 let lnum = prevnonblank(lnum - 1)
33 let previous_line = getline(lnum)
35 " Hit the start of the file, use zero indent.
40 let ind = indent(lnum)
43 " Regular expressions used by line indentation function.
44 let cmake_regex_comment = '#.*'
45 let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
46 let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
47 let cmake_regex_arguments = '\(' . cmake_regex_quoted .
48 \ or . '\$(' . cmake_regex_identifier . ')' .
49 \ or . '[^()\\#"]' . or . '\\.' . '\)*'
51 let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
52 let cmake_indent_blank_regex = '^\s*$'
53 let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
54 \ '\s*(' . cmake_regex_arguments .
55 \ '\(' . cmake_regex_comment . '\)\?$'
56 let cmake_indent_close_regex = '^' . cmake_regex_arguments .
58 \ '\(' . cmake_regex_comment . '\)\?$'
60 let cmake_closing_parens_line = '^\s*\()\+\)\s*$'
62 let cmake_indent_begin_regex = '^\s*\(BLOCK\|IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
63 let cmake_indent_end_regex = '^\s*\(ENDBLOCK\|ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
65 if this_line =~? cmake_closing_parens_line
66 if previous_line !~? cmake_indent_open_regex
67 let ind = ind - shiftwidth()
71 if previous_line =~? cmake_indent_comment_line " Handle comments
74 if previous_line =~? cmake_indent_begin_regex
75 let ind = ind + shiftwidth()
77 if previous_line =~? cmake_indent_open_regex
78 let ind = ind + shiftwidth()
83 if this_line =~? cmake_indent_end_regex
84 let ind = ind - shiftwidth()
86 if previous_line !~? cmake_closing_parens_line
87 if previous_line =~? cmake_indent_close_regex
88 let ind = ind - shiftwidth()