cmake-indent.vim 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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: Karthik Krishnan <[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/licensing
  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. setlocal indentkeys+==ENDIF(,ENDFOREACH(,ENDMACRO(,ELSE(,ELSEIF(,ENDWHILE(
  26. " Only define the function once.
  27. if exists("*CMakeGetIndent")
  28. finish
  29. endif
  30. fun! CMakeGetIndent(lnum)
  31. let this_line = getline(a:lnum)
  32. " Find a non-blank line above the current line.
  33. let lnum = a:lnum
  34. let lnum = prevnonblank(lnum - 1)
  35. let previous_line = getline(lnum)
  36. " Hit the start of the file, use zero indent.
  37. if lnum == 0
  38. return 0
  39. endif
  40. let ind = indent(lnum)
  41. let or = '\|'
  42. " Regular expressions used by line indentation function.
  43. let cmake_regex_comment = '#.*'
  44. let cmake_regex_identifier = '[A-Za-z][A-Za-z0-9_]*'
  45. let cmake_regex_quoted = '"\([^"\\]\|\\.\)*"'
  46. let cmake_regex_arguments = '\(' . cmake_regex_quoted .
  47. \ or . '\$(' . cmake_regex_identifier . ')' .
  48. \ or . '[^()\\#"]' . or . '\\.' . '\)*'
  49. let cmake_indent_comment_line = '^\s*' . cmake_regex_comment
  50. let cmake_indent_blank_regex = '^\s*$'
  51. let cmake_indent_open_regex = '^\s*' . cmake_regex_identifier .
  52. \ '\s*(' . cmake_regex_arguments .
  53. \ '\(' . cmake_regex_comment . '\)\?$'
  54. let cmake_indent_close_regex = '^' . cmake_regex_arguments .
  55. \ ')\s*' .
  56. \ '\(' . cmake_regex_comment . '\)\?$'
  57. let cmake_indent_begin_regex = '^\s*\(IF\|MACRO\|FOREACH\|ELSE\|ELSEIF\|WHILE\|FUNCTION\)\s*('
  58. let cmake_indent_end_regex = '^\s*\(ENDIF\|ENDFOREACH\|ENDMACRO\|ELSE\|ELSEIF\|ENDWHILE\|ENDFUNCTION\)\s*('
  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 + &sw
  65. endif
  66. if previous_line =~? cmake_indent_open_regex
  67. let ind = ind + &sw
  68. endif
  69. endif
  70. " Subtract
  71. if this_line =~? cmake_indent_end_regex
  72. let ind = ind - &sw
  73. endif
  74. if previous_line =~? cmake_indent_close_regex
  75. let ind = ind - &sw
  76. endif
  77. return ind
  78. endfun