cmake-indent.vim 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. " =============================================================================
  2. "
  3. " Program: CMake - Cross-Platform Makefile Generator
  4. " Module: $RCSfile$
  5. " Language: VIM
  6. " Date: $Date$
  7. " Version: $Revision$
  8. "
  9. " =============================================================================
  10. " Vim indent file
  11. " Language: CMake (ft=cmake)
  12. " Author: Andy Cedilnik <[email protected]>
  13. " Maintainer: Andy Cedilnik <[email protected]>
  14. " Last Change: $Date$
  15. " Version: $Revision$
  16. "
  17. " Licence: The CMake license applies to this file. See
  18. " http://www.cmake.org/HTML/Copyright.html
  19. " This implies that distribution with Vim is allowed
  20. if exists("b:did_indent")
  21. finish
  22. endif
  23. let b:did_indent = 1
  24. setlocal indentexpr=CMakeGetIndent(v:lnum)
  25. " Only define the function once.
  26. if exists("*CMakeGetIndent")
  27. finish
  28. endif
  29. fun! CMakeGetIndent(lnum)
  30. let this_line = getline(a:lnum)
  31. " Find a non-blank line above the current line.
  32. let lnum = a:lnum
  33. let lnum = prevnonblank(lnum - 1)
  34. let previous_line = getline(lnum)
  35. " Hit the start of the file, use zero indent.
  36. if lnum == 0
  37. return 0
  38. endif
  39. let ind = indent(lnum)
  40. let or = '\|'
  41. " Regular expressions used by line indentation function.
  42. let cmake_regex_comment = '#.*'
  43. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  44. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  45. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  46. \ or . '\$(' . cmake_regex_identifier . ')' .
  47. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  48. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  49. let cmake_indent_blank_regex = '^\s*$'
  50. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  51. \ '\s*(' . cmake_regex_arguments .
  52. \ '\(' . cmake_regex_comment . '\)\?$'
  53. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  54. \ ')\s*' .
  55. \ '\(' . cmake_regex_comment . '\)\?$'
  56. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\)\s*('
  57. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\)\s*('
  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 + &sw
  64. endif
  65. if previous_line =~? cmake_indent_open_regex
  66. let ind = ind + &sw
  67. endif
  68. endif
  69. " Subtract
  70. if this_line =~? cmake_indent_end_regex
  71. let ind = ind - &sw
  72. endif
  73. if previous_line =~? cmake_indent_close_regex
  74. let ind = ind - &sw
  75. endif
  76. return ind
  77. endfun