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