sas.vim 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. " Vim indent file
  2. " Language: SAS
  3. " Maintainer: Zhen-Huan Hu <wildkeny@gmail.com>
  4. " Version: 3.0.3
  5. " Last Change: Jun 26, 2018
  6. if exists("b:did_indent")
  7. finish
  8. endif
  9. let b:did_indent = 1
  10. setlocal indentexpr=GetSASIndent()
  11. setlocal indentkeys+=;,=~data,=~proc,=~macro
  12. if exists("*GetSASIndent")
  13. finish
  14. endif
  15. let s:cpo_save = &cpo
  16. set cpo&vim
  17. " Regex that captures the start of a data/proc section
  18. let s:section_str = '\v%(^|;)\s*%(data|proc)>'
  19. " Regex that captures the end of a run-processing section
  20. let s:section_run = '\v%(^|;)\s*run\s*;'
  21. " Regex that captures the end of a data/proc section
  22. let s:section_end = '\v%(^|;)\s*%(quit|enddata)\s*;'
  23. " Regex that captures the start of a control block (anything inside a section)
  24. let s:block_str = '\v<%(do>%([^;]+<%(to|over|while)>[^;]+)=|%(compute|define\s+%(column|footer|header|style|table|tagset|crosstabs|statgraph)|edit|layout|method|select)>[^;]+|begingraph)\s*;'
  25. " Regex that captures the end of a control block (anything inside a section)
  26. let s:block_end = '\v<%(end|endcomp|endlayout|endgraph)\s*;'
  27. " Regex that captures the start of a macro
  28. let s:macro_str = '\v%(^|;)\s*\%macro>'
  29. " Regex that captures the end of a macro
  30. let s:macro_end = '\v%(^|;)\s*\%mend\s*;'
  31. " Regex that defines the end of the program
  32. let s:program_end = '\v%(^|;)\s*endsas\s*;'
  33. " List of procs supporting run-processing
  34. let s:run_processing_procs = [
  35. \ 'catalog', 'chart', 'datasets', 'document', 'ds2', 'plot', 'sql',
  36. \ 'gareabar', 'gbarline', 'gchart', 'gkpi', 'gmap', 'gplot', 'gradar', 'greplay', 'gslide', 'gtile',
  37. \ 'anova', 'arima', 'catmod', 'factex', 'glm', 'model', 'optex', 'plan', 'reg',
  38. \ 'iml',
  39. \ ]
  40. " Find the line number of previous keyword defined by the regex
  41. function! s:PrevMatch(lnum, regex)
  42. let prev_lnum = prevnonblank(a:lnum - 1)
  43. while prev_lnum > 0
  44. let prev_line = getline(prev_lnum)
  45. if prev_line =~? a:regex
  46. break
  47. else
  48. let prev_lnum = prevnonblank(prev_lnum - 1)
  49. endif
  50. endwhile
  51. return prev_lnum
  52. endfunction
  53. " Main function
  54. function! GetSASIndent()
  55. let prev_lnum = prevnonblank(v:lnum - 1)
  56. if prev_lnum ==# 0
  57. " Leave the indentation of the first line unchanged
  58. return indent(1)
  59. else
  60. let prev_line = getline(prev_lnum)
  61. " Previous non-blank line contains the start of a macro/section/block
  62. " while not the end of a macro/section/block (at the same line)
  63. if (prev_line =~? s:section_str && prev_line !~? s:section_run && prev_line !~? s:section_end) ||
  64. \ (prev_line =~? s:block_str && prev_line !~? s:block_end) ||
  65. \ (prev_line =~? s:macro_str && prev_line !~? s:macro_end)
  66. let ind = indent(prev_lnum) + shiftwidth()
  67. elseif prev_line =~? s:section_run && prev_line !~? s:section_end
  68. let prev_section_str_lnum = s:PrevMatch(v:lnum, s:section_str)
  69. let prev_section_end_lnum = max([
  70. \ s:PrevMatch(v:lnum, s:section_end),
  71. \ s:PrevMatch(v:lnum, s:macro_end ),
  72. \ s:PrevMatch(v:lnum, s:program_end)])
  73. " Check if the section supports run-processing
  74. if prev_section_end_lnum < prev_section_str_lnum &&
  75. \ getline(prev_section_str_lnum) =~? '\v%(^|;)\s*proc\s+%(' .
  76. \ join(s:run_processing_procs, '|') . ')>'
  77. let ind = indent(prev_lnum) + shiftwidth()
  78. else
  79. let ind = indent(prev_lnum)
  80. endif
  81. else
  82. let ind = indent(prev_lnum)
  83. endif
  84. endif
  85. " Re-adjustments based on the inputs of the current line
  86. let curr_line = getline(v:lnum)
  87. if curr_line =~? s:program_end
  88. " End of the program
  89. " Same indentation as the first non-blank line
  90. return indent(nextnonblank(1))
  91. elseif curr_line =~? s:macro_end
  92. " Current line is the end of a macro
  93. " Match the indentation of the start of the macro
  94. return indent(s:PrevMatch(v:lnum, s:macro_str))
  95. elseif curr_line =~? s:block_end && curr_line !~? s:block_str
  96. " Re-adjust if current line is the end of a block
  97. " while not the beginning of a block (at the same line)
  98. " Returning the indent of previous block start directly
  99. " would not work due to nesting
  100. let ind = ind - shiftwidth()
  101. elseif curr_line =~? s:section_str || curr_line =~? s:section_run || curr_line =~? s:section_end
  102. " Re-adjust if current line is the start/end of a section
  103. " since the end of a section could be inexplicit
  104. let prev_section_str_lnum = s:PrevMatch(v:lnum, s:section_str)
  105. " Check if the previous section supports run-processing
  106. if getline(prev_section_str_lnum) =~? '\v%(^|;)\s*proc\s+%(' .
  107. \ join(s:run_processing_procs, '|') . ')>'
  108. let prev_section_end_lnum = max([
  109. \ s:PrevMatch(v:lnum, s:section_end),
  110. \ s:PrevMatch(v:lnum, s:macro_end ),
  111. \ s:PrevMatch(v:lnum, s:program_end)])
  112. else
  113. let prev_section_end_lnum = max([
  114. \ s:PrevMatch(v:lnum, s:section_end),
  115. \ s:PrevMatch(v:lnum, s:section_run),
  116. \ s:PrevMatch(v:lnum, s:macro_end ),
  117. \ s:PrevMatch(v:lnum, s:program_end)])
  118. endif
  119. if prev_section_end_lnum < prev_section_str_lnum
  120. let ind = ind - shiftwidth()
  121. endif
  122. endif
  123. return ind
  124. endfunction
  125. let &cpo = s:cpo_save
  126. unlet s:cpo_save