672bdccc468aecec76539ba18a8a269076721fed
[platform/upstream/cmake.git] / Auxiliary / vim / indent / cmake.vim
1 " Vim indent file
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
7 "
8 " License:      The CMake license applies to this file. See
9 "               https://cmake.org/licensing
10 "               This implies that distribution with Vim is allowed
11
12 if exists("b:did_indent")
13   finish
14 endif
15 let b:did_indent = 1
16
17 setlocal indentexpr=CMakeGetIndent(v:lnum)
18 setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
19
20 " Only define the function once.
21 if exists("*CMakeGetIndent")
22   finish
23 endif
24 let s:keepcpo= &cpo
25 set cpo&vim
26
27 fun! CMakeGetIndent(lnum)
28   let this_line = getline(a:lnum)
29
30   " Find a non-blank line above the current line.
31   let lnum = a:lnum
32   let lnum = prevnonblank(lnum - 1)
33   let previous_line = getline(lnum)
34
35   " Hit the start of the file, use zero indent.
36   if lnum == 0
37     return 0
38   endif
39
40   let ind = indent(lnum)
41
42   let or = '\|'
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 . '\\.' . '\)*'
50
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 .
57                     \            ')\s*' .
58                     \            '\(' . cmake_regex_comment . '\)\?$'
59
60   let cmake_closing_parens_line = '^\s*\()\+\)\s*$'
61
62   let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
63   let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
64
65   if this_line =~? cmake_closing_parens_line
66     if previous_line !~? cmake_indent_open_regex
67       let ind = ind - shiftwidth()
68     endif
69   else
70     " Add
71     if previous_line =~? cmake_indent_comment_line " Handle comments
72       let ind = ind
73     else
74       if previous_line =~? cmake_indent_begin_regex
75         let ind = ind + shiftwidth()
76       endif
77       if previous_line =~? cmake_indent_open_regex
78         let ind = ind + shiftwidth()
79       endif
80     endif
81
82     " Subtract
83     if this_line =~? cmake_indent_end_regex
84       let ind = ind - shiftwidth()
85     endif
86     if previous_line !~? cmake_closing_parens_line
87       if previous_line =~? cmake_indent_close_regex
88         let ind = ind - shiftwidth()
89       endif
90     endif
91   endif
92
93   return ind
94 endfun
95
96 let &cpo = s:keepcpo
97 unlet s:keepcpo