cmake-indent.vim 2.79 KB
Newer Older
Brad King's avatar
Brad King committed
1
" =============================================================================
2
"
Brad King's avatar
Brad King committed
3 4
"   Program:   CMake - Cross-Platform Makefile Generator
"   Module:    $RCSfile$
5
"   Language:  VIM
Brad King's avatar
Brad King committed
6 7
"   Date:      $Date$
"   Version:   $Revision$
8
"
Brad King's avatar
Brad King committed
9 10
" =============================================================================

11
" Vim indent file
Andy Cedilnik's avatar
Andy Cedilnik committed
12 13
" Language:     CMake (ft=cmake)
" Author:       Andy Cedilnik <andy.cedilnik@kitware.com>
Karthik Krishnan's avatar
Karthik Krishnan committed
14
" Maintainer:   Karthik Krishnan <karthik.krishnan@kitware.com>
Andy Cedilnik's avatar
Andy Cedilnik committed
15 16
" Last Change:  $Date$
" Version:      $Revision$
17 18
"
" Licence:      The CMake license applies to this file. See
19
"               https://cmake.org/licensing
20
"               This implies that distribution with Vim is allowed
21 22 23 24 25 26 27

if exists("b:did_indent")
  finish
endif
let b:did_indent = 1

setlocal indentexpr=CMakeGetIndent(v:lnum)
28
setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

" Only define the function once.
if exists("*CMakeGetIndent")
  finish
endif

fun! CMakeGetIndent(lnum)
  let this_line = getline(a:lnum)

  " Find a non-blank line above the current line.
  let lnum = a:lnum
  let lnum = prevnonblank(lnum - 1)
  let previous_line = getline(lnum)

  " Hit the start of the file, use zero indent.
  if lnum == 0
    return 0
  endif

  let ind = indent(lnum)

  let or = '\|'
  " Regular expressions used by line indentation function.
  let cmake_regex_comment = '#.*'
  let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  let cmake_regex_arguments = '\(' . cmake_regex_quoted .
                    \       or . '\$(' . cmake_regex_identifier . ')' .
                    \       or . '[^()\\#"]' . or . '\\.' . '\)*'

  let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
Andy Cedilnik's avatar
Andy Cedilnik committed
60
  let cmake_indent_blank_regex = '^\s*$'
61 62 63 64 65 66 67 68
  let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
                    \           '\s*(' . cmake_regex_arguments .
                    \           '\(' . cmake_regex_comment . '\)\?$'

  let cmake_indent_close_regex = '^' . cmake_regex_arguments .
                    \            ')\s*' .
                    \            '\(' . cmake_regex_comment . '\)\?$'

Karthik Krishnan's avatar
Karthik Krishnan committed
69 70
  let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

  " Add
  if previous_line =~? cmake_indent_comment_line " Handle comments
    let ind = ind
  else
    if previous_line =~? cmake_indent_begin_regex
      let ind = ind + &sw
    endif
    if previous_line =~? cmake_indent_open_regex
      let ind = ind + &sw
    endif
  endif

  " Subtract
  if this_line =~? cmake_indent_end_regex
    let ind = ind - &sw
  endif
  if previous_line =~? cmake_indent_close_regex
    let ind = ind - &sw
  endif

  return ind
endfun