codekingpro/portable-devtools
114k
1" Vim indent file2" 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 127"8" License: The CMake license applies to this file. See9" https://cmake.org/licensing10" This implies that distribution with Vim is allowed11 12if exists("b:did_indent")13 finish14endif15let b:did_indent = 116 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")24 finish25endif26let s:keepcpo= &cpo27set cpo&vim28 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:lnum34 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 == 039 return 040 endif41 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_comment54 let cmake_indent_blank_regex = '^\s*$'55 let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .56 \ '\s*(' . cmake_regex_arguments .57 \ '\(' . cmake_regex_comment . '\)\?$'58 let cmake_indent_close_regex = '^' . cmake_regex_arguments .59 \ ')\s*' .60 \ '\(' . cmake_regex_comment . '\)\?$'61 62 let cmake_closing_parens_line = '^\s*\()\+\)\s*$'63 64 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_line68 if previous_line !~? cmake_indent_open_regex69 let ind = ind - shiftwidth()70 endif71 else72 " Add73 if previous_line =~? cmake_indent_comment_line " Handle comments74 let ind = ind75 else76 if previous_line =~? cmake_indent_begin_regex77 let ind = ind + shiftwidth()78 endif79 if previous_line =~? cmake_indent_open_regex80 let ind = ind + shiftwidth()81 endif82 endif83 84 " Subtract85 if this_line =~? cmake_indent_end_regex86 let ind = ind - shiftwidth()87 endif88 if previous_line !~? cmake_closing_parens_line89 if previous_line =~? cmake_indent_close_regex90 let ind = ind - shiftwidth()91 endif92 endif93 endif94 95 return ind96endfun97 98let &cpo = s:keepcpo99unlet s:keepcpo100 