cmake.vim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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: 2023 Dec 12
  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. 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_closing_parens_line = '^\s*\()\+\)\s*$'
  52. let cmake_indent_begin_regex = '^\s*\(BLOCK\|IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  53. let cmake_indent_end_regex = '^\s*\(ENDBLOCK\|ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  54. if this_line =~? cmake_closing_parens_line
  55. if previous_line !~? cmake_indent_open_regex
  56. let ind = ind - shiftwidth()
  57. endif
  58. else
  59. " Add
  60. if previous_line =~? cmake_indent_comment_line " Handle comments
  61. let ind = ind
  62. else
  63. if previous_line =~? cmake_indent_begin_regex
  64. let ind = ind + shiftwidth()
  65. endif
  66. if previous_line =~? cmake_indent_open_regex
  67. let ind = ind + shiftwidth()
  68. endif
  69. endif
  70. " Subtract
  71. if this_line =~? cmake_indent_end_regex
  72. let ind = ind - shiftwidth()
  73. endif
  74. if previous_line !~? cmake_closing_parens_line
  75. if previous_line =~? cmake_indent_close_regex
  76. let ind = ind - shiftwidth()
  77. endif
  78. endif
  79. endif
  80. return ind
  81. endfun
  82. let &cpo = s:keepcpo
  83. unlet s:keepcpo