cmake.vim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 et
  16. setlocal indentexpr=CMakeGetIndent(v:lnum)
  17. setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
  18. " Only define the function once.
  19. if exists("*CMakeGetIndent")
  20. finish
  21. endif
  22. fun! CMakeGetIndent(lnum)
  23. let this_line = getline(a:lnum)
  24. " Find a non-blank line above the current line.
  25. let lnum = a:lnum
  26. let lnum = prevnonblank(lnum - 1)
  27. let previous_line = getline(lnum)
  28. " Hit the start of the file, use zero indent.
  29. if lnum == 0
  30. return 0
  31. endif
  32. let ind = indent(lnum)
  33. let or = '\|'
  34. " Regular expressions used by line indentation function.
  35. let cmake_regex_comment = '#.*'
  36. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  37. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  38. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  39. \ or . '\$(' . cmake_regex_identifier . ')' .
  40. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  41. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  42. let cmake_indent_blank_regex = '^\s*$'
  43. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  44. \ '\s*(' . cmake_regex_arguments .
  45. \ '\(' . cmake_regex_comment . '\)\?$'
  46. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  47. \ ')\s*' .
  48. \ '\(' . cmake_regex_comment . '\)\?$'
  49. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  50. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  51. " Add
  52. if previous_line =~? cmake_indent_comment_line " Handle comments
  53. let ind = ind
  54. else
  55. if previous_line =~? cmake_indent_begin_regex
  56. let ind = ind + &sw
  57. endif
  58. if previous_line =~? cmake_indent_open_regex
  59. let ind = ind + &sw
  60. endif
  61. endif
  62. " Subtract
  63. if this_line =~? cmake_indent_end_regex
  64. let ind = ind - &sw
  65. endif
  66. if previous_line =~? cmake_indent_close_regex
  67. let ind = ind - &sw
  68. endif
  69. return ind
  70. endfun