blob: 9cc9e025c41fb8d09bcf016541eef5a2dda59eb9 [file] [log] [blame]
Bram Moolenaar036986f2017-03-16 17:41:02 +01001" Vim indent file
2" Language: SAS
3" Maintainer: Zhen-Huan Hu <wildkeny@gmail.com>
Bram Moolenaar6dc819b2018-07-03 16:42:19 +02004" Version: 3.0.3
5" Last Change: Jun 26, 2018
Bram Moolenaar036986f2017-03-16 17:41:02 +01006
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetSASIndent()
13setlocal indentkeys+=;,=~data,=~proc,=~macro
14
15if exists("*GetSASIndent")
16 finish
17endif
18
19let s:cpo_save = &cpo
20set cpo&vim
21
22" Regex that captures the start of a data/proc section
23let s:section_str = '\v%(^|;)\s*%(data|proc)>'
24" Regex that captures the end of a run-processing section
25let s:section_run = '\v%(^|;)\s*run\s*;'
26" Regex that captures the end of a data/proc section
27let s:section_end = '\v%(^|;)\s*%(quit|enddata)\s*;'
28
29" Regex that captures the start of a control block (anything inside a section)
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020030let 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*;'
Bram Moolenaar036986f2017-03-16 17:41:02 +010031" Regex that captures the end of a control block (anything inside a section)
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020032let s:block_end = '\v<%(end|endcomp|endlayout|endgraph)\s*;'
Bram Moolenaar036986f2017-03-16 17:41:02 +010033
34" Regex that captures the start of a macro
35let s:macro_str = '\v%(^|;)\s*\%macro>'
36" Regex that captures the end of a macro
37let s:macro_end = '\v%(^|;)\s*\%mend\s*;'
38
39" Regex that defines the end of the program
40let s:program_end = '\v%(^|;)\s*endsas\s*;'
41
42" List of procs supporting run-processing
43let s:run_processing_procs = [
44 \ 'catalog', 'chart', 'datasets', 'document', 'ds2', 'plot', 'sql',
45 \ 'gareabar', 'gbarline', 'gchart', 'gkpi', 'gmap', 'gplot', 'gradar', 'greplay', 'gslide', 'gtile',
46 \ 'anova', 'arima', 'catmod', 'factex', 'glm', 'model', 'optex', 'plan', 'reg',
47 \ 'iml',
48 \ ]
49
50" Find the line number of previous keyword defined by the regex
51function! s:PrevMatch(lnum, regex)
52 let prev_lnum = prevnonblank(a:lnum - 1)
53 while prev_lnum > 0
54 let prev_line = getline(prev_lnum)
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020055 if prev_line =~? a:regex
Bram Moolenaar036986f2017-03-16 17:41:02 +010056 break
57 else
58 let prev_lnum = prevnonblank(prev_lnum - 1)
59 endif
60 endwhile
61 return prev_lnum
62endfunction
63
64" Main function
65function! GetSASIndent()
66 let prev_lnum = prevnonblank(v:lnum - 1)
67 if prev_lnum ==# 0
68 " Leave the indentation of the first line unchanged
69 return indent(1)
70 else
71 let prev_line = getline(prev_lnum)
72 " Previous non-blank line contains the start of a macro/section/block
73 " while not the end of a macro/section/block (at the same line)
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020074 if (prev_line =~? s:section_str && prev_line !~? s:section_run && prev_line !~? s:section_end) ||
75 \ (prev_line =~? s:block_str && prev_line !~? s:block_end) ||
76 \ (prev_line =~? s:macro_str && prev_line !~? s:macro_end)
77 let ind = indent(prev_lnum) + shiftwidth()
78 elseif prev_line =~? s:section_run && prev_line !~? s:section_end
Bram Moolenaar036986f2017-03-16 17:41:02 +010079 let prev_section_str_lnum = s:PrevMatch(v:lnum, s:section_str)
80 let prev_section_end_lnum = max([
81 \ s:PrevMatch(v:lnum, s:section_end),
82 \ s:PrevMatch(v:lnum, s:macro_end ),
83 \ s:PrevMatch(v:lnum, s:program_end)])
84 " Check if the section supports run-processing
85 if prev_section_end_lnum < prev_section_str_lnum &&
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020086 \ getline(prev_section_str_lnum) =~? '\v%(^|;)\s*proc\s+%(' .
Bram Moolenaar036986f2017-03-16 17:41:02 +010087 \ join(s:run_processing_procs, '|') . ')>'
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020088 let ind = indent(prev_lnum) + shiftwidth()
Bram Moolenaar036986f2017-03-16 17:41:02 +010089 else
90 let ind = indent(prev_lnum)
91 endif
92 else
93 let ind = indent(prev_lnum)
94 endif
95 endif
96 " Re-adjustments based on the inputs of the current line
97 let curr_line = getline(v:lnum)
Bram Moolenaar6dc819b2018-07-03 16:42:19 +020098 if curr_line =~? s:program_end
Bram Moolenaar036986f2017-03-16 17:41:02 +010099 " End of the program
100 " Same indentation as the first non-blank line
101 return indent(nextnonblank(1))
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200102 elseif curr_line =~? s:macro_end
Bram Moolenaar036986f2017-03-16 17:41:02 +0100103 " Current line is the end of a macro
104 " Match the indentation of the start of the macro
105 return indent(s:PrevMatch(v:lnum, s:macro_str))
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200106 elseif curr_line =~? s:block_end && curr_line !~? s:block_str
Bram Moolenaar036986f2017-03-16 17:41:02 +0100107 " Re-adjust if current line is the end of a block
108 " while not the beginning of a block (at the same line)
109 " Returning the indent of previous block start directly
110 " would not work due to nesting
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200111 let ind = ind - shiftwidth()
112 elseif curr_line =~? s:section_str || curr_line =~? s:section_run || curr_line =~? s:section_end
Bram Moolenaar036986f2017-03-16 17:41:02 +0100113 " Re-adjust if current line is the start/end of a section
114 " since the end of a section could be inexplicit
115 let prev_section_str_lnum = s:PrevMatch(v:lnum, s:section_str)
116 " Check if the previous section supports run-processing
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200117 if getline(prev_section_str_lnum) =~? '\v%(^|;)\s*proc\s+%(' .
Bram Moolenaar036986f2017-03-16 17:41:02 +0100118 \ join(s:run_processing_procs, '|') . ')>'
119 let prev_section_end_lnum = max([
120 \ s:PrevMatch(v:lnum, s:section_end),
121 \ s:PrevMatch(v:lnum, s:macro_end ),
122 \ s:PrevMatch(v:lnum, s:program_end)])
123 else
124 let prev_section_end_lnum = max([
125 \ s:PrevMatch(v:lnum, s:section_end),
126 \ s:PrevMatch(v:lnum, s:section_run),
127 \ s:PrevMatch(v:lnum, s:macro_end ),
128 \ s:PrevMatch(v:lnum, s:program_end)])
129 endif
130 if prev_section_end_lnum < prev_section_str_lnum
Bram Moolenaar6dc819b2018-07-03 16:42:19 +0200131 let ind = ind - shiftwidth()
Bram Moolenaar036986f2017-03-16 17:41:02 +0100132 endif
133 endif
134 return ind
135endfunction
136
137let &cpo = s:cpo_save
138unlet s:cpo_save