Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1 | " Vim indent file |
Bram Moolenaar | 945e2db | 2010-06-05 17:43:32 +0200 | [diff] [blame] | 2 | " Program: CMake - Cross-Platform Makefile Generator |
| 3 | " Module: $RCSfile: cmake-indent.vim,v $ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 4 | " Language: CMake (ft=cmake) |
| 5 | " Author: Andy Cedilnik <andy.cedilnik@kitware.com> |
Bram Moolenaar | 945e2db | 2010-06-05 17:43:32 +0200 | [diff] [blame] | 6 | " Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com> |
| 7 | " Last Change: $Date: 2008-01-16 16:53:53 $ |
| 8 | " Version: $Revision: 1.9 $ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 9 | " |
| 10 | " Licence: The CMake license applies to this file. See |
| 11 | " http://www.cmake.org/HTML/Copyright.html |
| 12 | " This implies that distribution with Vim is allowed |
| 13 | |
| 14 | if exists("b:did_indent") |
| 15 | finish |
| 16 | endif |
| 17 | let b:did_indent = 1 |
| 18 | |
| 19 | setlocal indentexpr=CMakeGetIndent(v:lnum) |
Bram Moolenaar | 945e2db | 2010-06-05 17:43:32 +0200 | [diff] [blame] | 20 | setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE( |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 21 | |
| 22 | " Only define the function once. |
| 23 | if exists("*CMakeGetIndent") |
| 24 | finish |
| 25 | endif |
| 26 | |
| 27 | fun! 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 Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 52 | let cmake_indent_blank_regex = '^\s*$' |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 945e2db | 2010-06-05 17:43:32 +0200 | [diff] [blame] | 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*(' |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 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 + &sw |
| 70 | endif |
| 71 | if previous_line =~? cmake_indent_open_regex |
| 72 | let ind = ind + &sw |
| 73 | endif |
| 74 | endif |
| 75 | |
| 76 | " Subtract |
| 77 | if this_line =~? cmake_indent_end_regex |
| 78 | let ind = ind - &sw |
| 79 | endif |
| 80 | if previous_line =~? cmake_indent_close_regex |
| 81 | let ind = ind - &sw |
| 82 | endif |
| 83 | |
| 84 | return ind |
| 85 | endfun |