cmake-indent.vim 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. " =============================================================================
  2. "
  3. " Program: CMake - Cross-Platform Makefile Generator
  4. " Module: $RCSfile$
  5. " Language: C++
  6. " Date: $Date$
  7. " Version: $Revision$
  8. "
  9. " Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  10. " See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  11. "
  12. " This software is distributed WITHOUT ANY WARRANTY" without even
  13. " the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  14. " PURPOSE. See the above copyright notices for more information.
  15. "
  16. " =============================================================================
  17. " Vim indent file
  18. " Language: CMake (ft=cmake)
  19. " Author: Andy Cedilnik <[email protected]>
  20. " URL: http://www.cmake.org
  21. " Last Change: Sun Mar 23 15:41:55 EST 2003
  22. if exists("b:did_indent")
  23. finish
  24. endif
  25. let b:did_indent = 1
  26. setlocal indentexpr=CMakeGetIndent(v:lnum)
  27. " Only define the function once.
  28. if exists("*CMakeGetIndent")
  29. finish
  30. endif
  31. fun! CMakeGetIndent(lnum)
  32. let this_line = getline(a:lnum)
  33. " Find a non-blank line above the current line.
  34. let lnum = a:lnum
  35. let lnum = prevnonblank(lnum - 1)
  36. let previous_line = getline(lnum)
  37. " Hit the start of the file, use zero indent.
  38. if lnum == 0
  39. return 0
  40. endif
  41. let ind = indent(lnum)
  42. let or = '\|'
  43. " Regular expressions used by line indentation function.
  44. let cmake_regex_comment = '#.*'
  45. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  46. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  47. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  48. \ or . '\$(' . cmake_regex_identifier . ')' .
  49. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  50. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  51. let cmake_indent_blank_regex = '^\s*$')
  52. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  53. \ '\s*(' . cmake_regex_arguments .
  54. \ '\(' . cmake_regex_comment . '\)\?$'
  55. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  56. \ ')\s*' .
  57. \ '\(' . cmake_regex_comment . '\)\?$'
  58. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\)\s*('
  59. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\)\s*('
  60. " Add
  61. if previous_line =~? cmake_indent_comment_line " Handle comments
  62. let ind = ind
  63. else
  64. if previous_line =~? cmake_indent_begin_regex
  65. let ind = ind + &sw
  66. endif
  67. if previous_line =~? cmake_indent_open_regex
  68. let ind = ind + &sw
  69. endif
  70. endif
  71. " Subtract
  72. if this_line =~? cmake_indent_end_regex
  73. let ind = ind - &sw
  74. endif
  75. if previous_line =~? cmake_indent_close_regex
  76. let ind = ind - &sw
  77. endif
  78. return ind
  79. endfun