cmake.vim 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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: 2017 Sep 24
  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. 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_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  51. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  52. " Add
  53. if previous_line =~? cmake_indent_comment_line " Handle comments
  54. let ind = ind
  55. else
  56. if previous_line =~? cmake_indent_begin_regex
  57. let ind = ind + shiftwidth()
  58. endif
  59. if previous_line =~? cmake_indent_open_regex
  60. let ind = ind + shiftwidth()
  61. endif
  62. endif
  63. " Subtract
  64. if this_line =~? cmake_indent_end_regex
  65. let ind = ind - shiftwidth()
  66. endif
  67. if previous_line =~? cmake_indent_close_regex
  68. let ind = ind - shiftwidth()
  69. endif
  70. return ind
  71. endfun
  72. let &cpo = s:keepcpo
  73. unlet s:keepcpo