xref: /vim-8.2.3635/runtime/indent/cmake.vim (revision 24a98a0e)
1" Vim indent file
2" Language:     CMake (ft=cmake)
3" Author:       Andy Cedilnik <[email protected]>
4" Maintainer:   Dimitri Merejkowsky <[email protected]>
5" Former Maintainer: Karthik Krishnan <[email protected]>
6" Last Change:  2017 Sep 24
7"
8" Licence:      The CMake license applies to this file. See
9"               https://cmake.org/licensing
10"               This implies that distribution with Vim is allowed
11
12if exists("b:did_indent")
13  finish
14endif
15let b:did_indent = 1
16
17setlocal indentexpr=CMakeGetIndent(v:lnum)
18setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
19
20" Only define the function once.
21if exists("*CMakeGetIndent")
22  finish
23endif
24let s:keepcpo= &cpo
25set cpo&vim
26
27fun! 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
57  let cmake_indent_close_regex = '^' . cmake_regex_arguments .
58                    \            ')\s*' .
59                    \            '\(' . cmake_regex_comment . '\)\?$'
60
61  let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
62  let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
63
64  " Add
65  if previous_line =~? cmake_indent_comment_line " Handle comments
66    let ind = ind
67  else
68    if previous_line =~? cmake_indent_begin_regex
69      let ind = ind + shiftwidth()
70    endif
71    if previous_line =~? cmake_indent_open_regex
72      let ind = ind + shiftwidth()
73    endif
74  endif
75
76  " Subtract
77  if this_line =~? cmake_indent_end_regex
78    let ind = ind - shiftwidth()
79  endif
80  if previous_line =~? cmake_indent_close_regex
81    let ind = ind - shiftwidth()
82  endif
83
84  return ind
85endfun
86
87let &cpo = s:keepcpo
88unlet s:keepcpo
89