cmake.vim 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. " Vim indent file
  2. " Language: CMake (ft=cmake)
  3. " Author: Andy Cedilnik <andy.cedilnik@kitware.com>
  4. " Maintainer: Dimitri Merejkowsky <d.merej@gmail.com>
  5. " Former Maintainer: Karthik Krishnan <karthik.krishnan@kitware.com>
  6. " Last Change: 2022 Apr 06
  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. let b:undo_indent = "setl inde< indk<"
  18. " Only define the function once.
  19. if exists("*CMakeGetIndent")
  20. finish
  21. endif
  22. let s:keepcpo= &cpo
  23. set cpo&vim
  24. fun! CMakeGetIndent(lnum)
  25. let this_line = getline(a:lnum)
  26. " Find a non-blank line above the current line.
  27. let lnum = a:lnum
  28. let lnum = prevnonblank(lnum - 1)
  29. let previous_line = getline(lnum)
  30. " Hit the start of the file, use zero indent.
  31. if lnum == 0
  32. return 0
  33. endif
  34. let ind = indent(lnum)
  35. let or = '\|'
  36. " Regular expressions used by line indentation function.
  37. let cmake_regex_comment = '#.*'
  38. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  39. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  40. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  41. \ or . '\$(' . cmake_regex_identifier . ')' .
  42. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  43. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  44. let cmake_indent_blank_regex = '^\s*$'
  45. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  46. \ '\s*(' . cmake_regex_arguments .
  47. \ '\(' . cmake_regex_comment . '\)\?$'
  48. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  49. \ ')\s*' .
  50. \ '\(' . cmake_regex_comment . '\)\?$'
  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. " Add
  54. if previous_line =~? cmake_indent_comment_line " Handle comments
  55. let ind = ind
  56. else
  57. if previous_line =~? cmake_indent_begin_regex
  58. let ind = ind + shiftwidth()
  59. endif
  60. if previous_line =~? cmake_indent_open_regex
  61. let ind = ind + shiftwidth()
  62. endif
  63. endif
  64. " Subtract
  65. if this_line =~? cmake_indent_end_regex
  66. let ind = ind - shiftwidth()
  67. endif
  68. if previous_line =~? cmake_indent_close_regex
  69. let ind = ind - shiftwidth()
  70. endif
  71. return ind
  72. endfun
  73. let &cpo = s:keepcpo
  74. unlet s:keepcpo