cmake.vim 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. " Vim indent file
  2. " Language: CMake (ft=cmake)
  3. " Author: Andy Cedilnik <[email protected]>
  4. " Maintainer: Karthik Krishnan <[email protected]>
  5. " Last Change: $Date$
  6. " Version: $Revision$
  7. "
  8. " Licence: The CMake license applies to this file. See
  9. " https://cmake.org/licensing
  10. " This implies that distribution with Vim is allowed
  11. if exists("b:did_indent")
  12. finish
  13. endif
  14. let b:did_indent = 1
  15. setlocal indentexpr=CMakeGetIndent(v:lnum)
  16. setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
  17. " Only define the function once.
  18. if exists("*CMakeGetIndent")
  19. finish
  20. endif
  21. fun! CMakeGetIndent(lnum)
  22. let this_line = getline(a:lnum)
  23. " Find a non-blank line above the current line.
  24. let lnum = a:lnum
  25. let lnum = prevnonblank(lnum - 1)
  26. let previous_line = getline(lnum)
  27. " Hit the start of the file, use zero indent.
  28. if lnum == 0
  29. return 0
  30. endif
  31. let ind = indent(lnum)
  32. let or = '\|'
  33. " Regular expressions used by line indentation function.
  34. let cmake_regex_comment = '#.*'
  35. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  36. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  37. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  38. \ or . '\$(' . cmake_regex_identifier . ')' .
  39. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  40. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  41. let cmake_indent_blank_regex = '^\s*$'
  42. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  43. \ '\s*(' . cmake_regex_arguments .
  44. \ '\(' . cmake_regex_comment . '\)\?$'
  45. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  46. \ ')\s*' .
  47. \ '\(' . cmake_regex_comment . '\)\?$'
  48. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  49. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  50. " Add
  51. if previous_line =~? cmake_indent_comment_line " Handle comments
  52. let ind = ind
  53. else
  54. if previous_line =~? cmake_indent_begin_regex
  55. let ind = ind + &sw
  56. endif
  57. if previous_line =~? cmake_indent_open_regex
  58. let ind = ind + &sw
  59. endif
  60. endif
  61. " Subtract
  62. if this_line =~? cmake_indent_end_regex
  63. let ind = ind - &sw
  64. endif
  65. if previous_line =~? cmake_indent_close_regex
  66. let ind = ind - &sw
  67. endif
  68. return ind
  69. endfun