Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 1 | " ============================================================================= |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 2 | " |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 3 | " Program: CMake - Cross-Platform Makefile Generator |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 4 | " Module: $RCSfile: cmake-indent.vim,v $ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 5 | " Language: VIM |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 6 | " Date: $Date: 2006/09/23 21:09:08 $ |
| 7 | " Version: $Revision: 1.7 $ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 8 | " |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 9 | " ============================================================================= |
| 10 | |
| 11 | " Vim indent file |
| 12 | " Language: CMake (ft=cmake) |
| 13 | " Author: Andy Cedilnik <andy.cedilnik@kitware.com> |
| 14 | " Maintainer: Andy Cedilnik <andy.cedilnik@kitware.com> |
Bram Moolenaar | 5c73622 | 2010-01-06 20:54:52 +0100 | [diff] [blame] | 15 | " Last Change: $Date: 2006/09/23 21:09:08 $ |
| 16 | " Version: $Revision: 1.7 $ |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 17 | " |
| 18 | " Licence: The CMake license applies to this file. See |
| 19 | " http://www.cmake.org/HTML/Copyright.html |
| 20 | " This implies that distribution with Vim is allowed |
| 21 | |
| 22 | if exists("b:did_indent") |
| 23 | finish |
| 24 | endif |
| 25 | let b:did_indent = 1 |
| 26 | |
| 27 | setlocal indentexpr=CMakeGetIndent(v:lnum) |
| 28 | |
| 29 | " Only define the function once. |
| 30 | if exists("*CMakeGetIndent") |
| 31 | finish |
| 32 | endif |
| 33 | |
| 34 | fun! CMakeGetIndent(lnum) |
| 35 | let this_line = getline(a:lnum) |
| 36 | |
| 37 | " Find a non-blank line above the current line. |
| 38 | let lnum = a:lnum |
| 39 | let lnum = prevnonblank(lnum - 1) |
| 40 | let previous_line = getline(lnum) |
| 41 | |
| 42 | " Hit the start of the file, use zero indent. |
| 43 | if lnum == 0 |
| 44 | return 0 |
| 45 | endif |
| 46 | |
| 47 | let ind = indent(lnum) |
| 48 | |
| 49 | let or = '\|' |
| 50 | " Regular expressions used by line indentation function. |
| 51 | let cmake_regex_comment = '#.*' |
| 52 | let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*' |
| 53 | let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"' |
| 54 | let cmake_regex_arguments = '\(' . cmake_regex_quoted . |
| 55 | \ or . '\$(' . cmake_regex_identifier . ')' . |
| 56 | \ or . '[^()\\#"]' . or . '\\.' . '\)*' |
| 57 | |
| 58 | let cmake_indent_comment_line = '^\s*' . cmake_regex_comment |
Bram Moolenaar | 57657d8 | 2006-04-21 22:12:41 +0000 | [diff] [blame] | 59 | let cmake_indent_blank_regex = '^\s*$' |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 60 | let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier . |
| 61 | \ '\s*(' . cmake_regex_arguments . |
| 62 | \ '\(' . cmake_regex_comment . '\)\?$' |
| 63 | |
| 64 | let cmake_indent_close_regex = '^' . cmake_regex_arguments . |
| 65 | \ ')\s*' . |
| 66 | \ '\(' . cmake_regex_comment . '\)\?$' |
| 67 | |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 68 | let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\)\s*(' |
| 69 | let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\)\s*(' |
Bram Moolenaar | c1e3790 | 2006-04-18 21:55:01 +0000 | [diff] [blame] | 70 | |
| 71 | " Add |
| 72 | if previous_line =~? cmake_indent_comment_line " Handle comments |
| 73 | let ind = ind |
| 74 | else |
| 75 | if previous_line =~? cmake_indent_begin_regex |
| 76 | let ind = ind + &sw |
| 77 | endif |
| 78 | if previous_line =~? cmake_indent_open_regex |
| 79 | let ind = ind + &sw |
| 80 | endif |
| 81 | endif |
| 82 | |
| 83 | " Subtract |
| 84 | if this_line =~? cmake_indent_end_regex |
| 85 | let ind = ind - &sw |
| 86 | endif |
| 87 | if previous_line =~? cmake_indent_close_regex |
| 88 | let ind = ind - &sw |
| 89 | endif |
| 90 | |
| 91 | return ind |
| 92 | endfun |