blob: c1aa3bff865fdacf935839b15eb0e21e2ceec09d [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>
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +09006" Last Change: 2023 Dec 12
Bram Moolenaarc1e37902006-04-18 21:55:01 +00007"
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +09008" License: 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
Bram Moolenaarcbaff5e2022-04-08 17:45:08 +010020let b:undo_indent = "setl inde< indk<"
21
Bram Moolenaarc1e37902006-04-18 21:55:01 +000022" 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 . '\)\?$'
Bram Moolenaarc1e37902006-04-18 21:55:01 +000058 let cmake_indent_close_regex = '^' . cmake_regex_arguments .
59 \ ')\s*' .
60 \ '\(' . cmake_regex_comment . '\)\?$'
61
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +090062 let cmake_closing_parens_line = '^\s*\()\+\)\s*$'
Bram Moolenaarc1e37902006-04-18 21:55:01 +000063
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +090064 let cmake_indent_begin_regex = '^\s*\(BLOCK\|IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
65 let cmake_indent_end_regex = '^\s*\(ENDBLOCK\|ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
66
67 if this_line =~? cmake_closing_parens_line
68 if previous_line !~? cmake_indent_open_regex
69 let ind = ind - shiftwidth()
70 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +000071 else
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +090072 " Add
73 if previous_line =~? cmake_indent_comment_line " Handle comments
74 let ind = ind
75 else
76 if previous_line =~? cmake_indent_begin_regex
77 let ind = ind + shiftwidth()
78 endif
79 if previous_line =~? cmake_indent_open_regex
80 let ind = ind + shiftwidth()
81 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +000082 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +000083
Eisuke Kawashima3afc9f22023-12-15 04:38:29 +090084 " Subtract
85 if this_line =~? cmake_indent_end_regex
86 let ind = ind - shiftwidth()
87 endif
88 if previous_line !~? cmake_closing_parens_line
89 if previous_line =~? cmake_indent_close_regex
90 let ind = ind - shiftwidth()
91 endif
92 endif
Bram Moolenaarc1e37902006-04-18 21:55:01 +000093 endif
94
95 return ind
96endfun
Bram Moolenaar8e52a592012-05-18 21:49:28 +020097
98let &cpo = s:keepcpo
99unlet s:keepcpo