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