blob: 845bdd7655bc88c067c47e2fd5955d919cc801a1 [file] [log] [blame]
Bram Moolenaarc1e37902006-04-18 21:55:01 +00001" Vim indent file
2" Language: CMake (ft=cmake)
3" Author: Andy Cedilnik <andy.cedilnik@kitware.com>
Bram Moolenaar37c64c72017-09-19 22:06:03 +02004" Maintainer: Dimitri Merejkowsky <d.merej@gmail.com>
5" Former Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
Bram Moolenaar24a98a02017-09-27 22:23:55 +02006" Last Change: 2017 Sep 24
Bram Moolenaarc1e37902006-04-18 21:55:01 +00007"
8" Licence: The CMake license applies to this file. See
Bram Moolenaar37c64c72017-09-19 22:06:03 +02009" https://cmake.org/licensing
Bram Moolenaarc1e37902006-04-18 21:55:01 +000010" 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)
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
Bram Moolenaarc1e37902006-04-18 21:55:01 +000019
20" Only define the function once.
21if exists("*CMakeGetIndent")
22 finish
23endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020024let s:keepcpo= &cpo
25set cpo&vim
Bram Moolenaarc1e37902006-04-18 21:55:01 +000026
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
Bram Moolenaar57657d82006-04-21 22:12:41 +000052 let cmake_indent_blank_regex = '^\s*$'
Bram Moolenaarc1e37902006-04-18 21:55:01 +000053 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
Bram Moolenaar945e2db2010-06-05 17:43:32 +020061 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*('
Bram Moolenaarc1e37902006-04-18 21:55:01 +000063
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
Bram Moolenaar24a98a02017-09-27 22:23:55 +020069 let ind = ind + shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000070 endif
71 if previous_line =~? cmake_indent_open_regex
Bram Moolenaar24a98a02017-09-27 22:23:55 +020072 let ind = ind + shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000073 endif
74 endif
75
76 " Subtract
77 if this_line =~? cmake_indent_end_regex
Bram Moolenaar24a98a02017-09-27 22:23:55 +020078 let ind = ind - shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000079 endif
80 if previous_line =~? cmake_indent_close_regex
Bram Moolenaar24a98a02017-09-27 22:23:55 +020081 let ind = ind - shiftwidth()
Bram Moolenaarc1e37902006-04-18 21:55:01 +000082 endif
83
84 return ind
85endfun
Bram Moolenaar8e52a592012-05-18 21:49:28 +020086
87let &cpo = s:keepcpo
88unlet s:keepcpo