cmake.vim 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. " Vim indent file
  2. " Language: CMake (ft=cmake)
  3. " Author: Andy Cedilnik <[email protected]>
  4. " Maintainer: Dimitri Merejkowsky <[email protected]>
  5. " Former Maintainer: Karthik Krishnan <[email protected]>
  6. " Last Change: 2022 Mar 22
  7. "
  8. " License: 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. let s:keepcpo= &cpo
  22. set cpo&vim
  23. fun! CMakeGetIndent(lnum)
  24. let this_line = getline(a:lnum)
  25. " Find a non-blank line above the current line.
  26. let lnum = a:lnum
  27. let lnum = prevnonblank(lnum - 1)
  28. let previous_line = getline(lnum)
  29. " Hit the start of the file, use zero indent.
  30. if lnum == 0
  31. return 0
  32. endif
  33. let ind = indent(lnum)
  34. let or = '\|'
  35. " Regular expressions used by line indentation function.
  36. let cmake_regex_comment = '#.*'
  37. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  38. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  39. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  40. \ or . '\$(' . cmake_regex_identifier . ')' .
  41. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  42. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  43. let cmake_indent_blank_regex = '^\s*$'
  44. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  45. \ '\s*(' . cmake_regex_arguments .
  46. \ '\(' . cmake_regex_comment . '\)\?$'
  47. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  48. \ ')\s*' .
  49. \ '\(' . cmake_regex_comment . '\)\?$'
  50. let cmake_closing_parens_line = '^\s*\()\+\)\s*$'
  51. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  52. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  53. if this_line =~? cmake_closing_parens_line
  54. if previous_line !~? cmake_indent_open_regex
  55. let ind = ind - shiftwidth()
  56. endif
  57. else
  58. " Add
  59. if previous_line =~? cmake_indent_comment_line " Handle comments
  60. let ind = ind
  61. else
  62. if previous_line =~? cmake_indent_begin_regex
  63. let ind = ind + shiftwidth()
  64. endif
  65. if previous_line =~? cmake_indent_open_regex
  66. let ind = ind + shiftwidth()
  67. endif
  68. endif
  69. " Subtract
  70. if this_line =~? cmake_indent_end_regex
  71. let ind = ind - shiftwidth()
  72. endif
  73. if previous_line !~? cmake_closing_parens_line
  74. if previous_line =~? cmake_indent_close_regex
  75. let ind = ind - shiftwidth()
  76. endif
  77. endif
  78. endif
  79. return ind
  80. endfun
  81. let &cpo = s:keepcpo
  82. unlet s:keepcpo