robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
34f008e247
master
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: 2023 Dec 12 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 12if exists ( "b:did_indent" ) 13finish 14endif 15let b: did_indent = 1 16 17setlocal indentexpr = CMakeGetIndent (v:lnum) 18setlocal indentkeys += =ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE( 19 20let b: undo_indent = "setl inde< indk<" 21 22" Only define the function once. 23if exists ( "*CMakeGetIndent" ) 24finish 25endif 26let s: keepcpo = & cpo 27set cpo & vim 28 29fun ! CMakeGetIndent ( lnum ) 30let this_line = getline ( a: lnum ) 31 32" Find a non-blank line above the current line. 33let lnum = a: lnum 34let lnum = prevnonblank ( lnum - 1 ) 35let previous_line = getline ( lnum ) 36 37" Hit the start of the file, use zero indent. 38if lnum == 0 39return 0 40endif 41 42let ind = indent ( lnum ) 43 44let or = '\|' 45" Regular expressions used by line indentation function. 46let cmake_regex_comment = '#.*' 47let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*' 48let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"' 49let cmake_regex_arguments = '\(' . cmake_regex_quoted . 50 \or . '\$(' . cmake_regex_identifier . ')' . 51 \or . '[^()\\#"]' . or . '\\.' . '\)*' 52 53let cmake_indent_comment_line = '^\s*' . cmake_regex_comment 54let cmake_indent_blank_regex = '^\s*$' 55let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier . 56 \'\s*(' . cmake_regex_arguments . 57 \'\(' . cmake_regex_comment . '\)\?$' 58let cmake_indent_close_regex = '^' . cmake_regex_arguments . 59 \')\s*' . 60 \'\(' . cmake_regex_comment . '\)\?$' 61 62let cmake_closing_parens_line = '^\s*\()\+\)\s*$' 63 64let cmake_indent_begin_regex = '^\s*\(BLOCK\|IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*(' 65let cmake_indent_end_regex = '^\s*\(ENDBLOCK\|ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*(' 66 67if this_line =~? cmake_closing_parens_line 68if previous_line !~? cmake_indent_open_regex 69let ind = ind - shiftwidth () 70endif 71else 72" Add 73if previous_line =~? cmake_indent_comment_line " Handle comments 74let ind = ind 75else 76if previous_line =~? cmake_indent_begin_regex 77let ind = ind + shiftwidth () 78endif 79if previous_line =~? cmake_indent_open_regex 80let ind = ind + shiftwidth () 81endif 82endif 83 84" Subtract 85if this_line =~? cmake_indent_end_regex 86let ind = ind - shiftwidth () 87endif 88if previous_line !~? cmake_closing_parens_line 89if previous_line =~? cmake_indent_close_regex 90let ind = ind - shiftwidth () 91endif 92endif 93endif 94 95return ind 96endfun 97 98let & cpo = s: keepcpo 99unlet s: keepcpo