/runtime/indent/perl.vim

https://bitbucket.org/ultra_iter/vim-qt · Vim Script · 180 lines · 118 code · 22 blank · 40 comment · 60 complexity · b553632333e2b8b497e5d16dafe7da19 MD5 · raw file

  1. " Vim indent file
  2. " Language: Perl 5
  3. " Author: Andy Lester <andy@petdance.com>
  4. " URL: http://github.com/petdance/vim-perl/tree/master
  5. " Last Change: June 3, 2009
  6. " Suggestions and improvements by :
  7. " Aaron J. Sherman (use syntax for hints)
  8. " Artem Chuprina (play nice with folding)
  9. " TODO things that are not or not properly indented (yet) :
  10. " - Continued statements
  11. " print "foo",
  12. " "bar";
  13. " print "foo"
  14. " if bar();
  15. " - Multiline regular expressions (m//x)
  16. " (The following probably needs modifying the perl syntax file)
  17. " - qw() lists
  18. " - Heredocs with terminators that don't match \I\i*
  19. " Only load this indent file when no other was loaded.
  20. if exists("b:did_indent")
  21. finish
  22. endif
  23. let b:did_indent = 1
  24. " Is syntax highlighting active ?
  25. let b:indent_use_syntax = has("syntax")
  26. setlocal indentexpr=GetPerlIndent()
  27. setlocal indentkeys+=0=,0),0],0=or,0=and
  28. if !b:indent_use_syntax
  29. setlocal indentkeys+=0=EO
  30. endif
  31. " Only define the function once.
  32. if exists("*GetPerlIndent")
  33. finish
  34. endif
  35. let s:cpo_save = &cpo
  36. set cpo-=C
  37. function GetPerlIndent()
  38. " Get the line to be indented
  39. let cline = getline(v:lnum)
  40. " Indent POD markers to column 0
  41. if cline =~ '^\s*=\L\@!'
  42. return 0
  43. endif
  44. " Don't reindent coments on first column
  45. if cline =~ '^#.'
  46. return 0
  47. endif
  48. " Get current syntax item at the line's first char
  49. let csynid = ''
  50. if b:indent_use_syntax
  51. let csynid = synIDattr(synID(v:lnum,1,0),"name")
  52. endif
  53. " Don't reindent POD and heredocs
  54. if csynid == "perlPOD" || csynid == "perlHereDoc" || csynid =~ "^pod"
  55. return indent(v:lnum)
  56. endif
  57. " Indent end-of-heredocs markers to column 0
  58. if b:indent_use_syntax
  59. " Assumes that an end-of-heredoc marker matches \I\i* to avoid
  60. " confusion with other types of strings
  61. if csynid == "perlStringStartEnd" && cline =~ '^\I\i*$'
  62. return 0
  63. endif
  64. else
  65. " Without syntax hints, assume that end-of-heredocs markers begin with EO
  66. if cline =~ '^\s*EO'
  67. return 0
  68. endif
  69. endif
  70. " Now get the indent of the previous perl line.
  71. " Find a non-blank line above the current line.
  72. let lnum = prevnonblank(v:lnum - 1)
  73. " Hit the start of the file, use zero indent.
  74. if lnum == 0
  75. return 0
  76. endif
  77. let line = getline(lnum)
  78. let ind = indent(lnum)
  79. " Skip heredocs, POD, and comments on 1st column
  80. if b:indent_use_syntax
  81. let skippin = 2
  82. while skippin
  83. let synid = synIDattr(synID(lnum,1,0),"name")
  84. if (synid == "perlStringStartEnd" && line =~ '^\I\i*$')
  85. \ || (skippin != 2 && synid == "perlPOD")
  86. \ || (skippin != 2 && synid == "perlHereDoc")
  87. \ || synid == "perlComment"
  88. \ || synid =~ "^pod"
  89. let lnum = prevnonblank(lnum - 1)
  90. if lnum == 0
  91. return 0
  92. endif
  93. let line = getline(lnum)
  94. let ind = indent(lnum)
  95. let skippin = 1
  96. else
  97. let skippin = 0
  98. endif
  99. endwhile
  100. else
  101. if line =~ "^EO"
  102. let lnum = search("<<[\"']\\=EO", "bW")
  103. let line = getline(lnum)
  104. let ind = indent(lnum)
  105. endif
  106. endif
  107. " Indent blocks enclosed by {}, (), or []
  108. if b:indent_use_syntax
  109. " Find a real opening brace
  110. let bracepos = match(line, '[(){}\[\]]', matchend(line, '^\s*[)}\]]'))
  111. while bracepos != -1
  112. let synid = synIDattr(synID(lnum, bracepos + 1, 0), "name")
  113. " If the brace is highlighted in one of those groups, indent it.
  114. " 'perlHereDoc' is here only to handle the case '&foo(<<EOF)'.
  115. if synid == ""
  116. \ || synid == "perlMatchStartEnd"
  117. \ || synid == "perlHereDoc"
  118. \ || synid =~ "^perlFiledescStatement"
  119. \ || synid =~ '^perl\(Sub\|Block\)Fold'
  120. let brace = strpart(line, bracepos, 1)
  121. if brace == '(' || brace == '{' || brace == '['
  122. let ind = ind + &sw
  123. else
  124. let ind = ind - &sw
  125. endif
  126. endif
  127. let bracepos = match(line, '[(){}\[\]]', bracepos + 1)
  128. endwhile
  129. let bracepos = matchend(cline, '^\s*[)}\]]')
  130. if bracepos != -1
  131. let synid = synIDattr(synID(v:lnum, bracepos, 0), "name")
  132. if synid == ""
  133. \ || synid == "perlMatchStartEnd"
  134. \ || synid =~ '^perl\(Sub\|Block\)Fold'
  135. let ind = ind - &sw
  136. endif
  137. endif
  138. else
  139. if line =~ '[{\[(]\s*\(#[^)}\]]*\)\=$'
  140. let ind = ind + &sw
  141. endif
  142. if cline =~ '^\s*[)}\]]'
  143. let ind = ind - &sw
  144. endif
  145. endif
  146. " Indent lines that begin with 'or' or 'and'
  147. if cline =~ '^\s*\(or\|and\)\>'
  148. if line !~ '^\s*\(or\|and\)\>'
  149. let ind = ind + &sw
  150. endif
  151. elseif line =~ '^\s*\(or\|and\)\>'
  152. let ind = ind - &sw
  153. endif
  154. return ind
  155. endfunction
  156. let &cpo = s:cpo_save
  157. unlet s:cpo_save
  158. " vim:ts=8:sts=4:sw=4:expandtab:ft=vim