blob: 753a2b04be6fccd6adb21c342a03c7f11d3acb4d [file] [log] [blame]
Bram Moolenaar00a927d2010-05-14 23:24:24 +02001" Vim indent file
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02002" Language: Perl 6
3" Maintainer: vim-perl <vim-perl@googlegroups.com>
Bram Moolenaar2c7f8c52020-04-20 19:52:53 +02004" Homepage: https://github.com/vim-perl/vim-perl
5" Bugs/requests: https://github.com/vim-perl/vim-perl/issues
6" Last Change: 2020 Apr 15
dkearns0382f052023-08-29 05:32:59 +10007" 2023 Aug 28 by Vim Project (undo_indent)
Bram Moolenaar543b7ef2013-06-01 14:50:56 +02008" Contributors: Andy Lester <andy@petdance.com>
9" Hinrik Örn Sigurðsson <hinrik.sig@gmail.com>
Bram Moolenaar00a927d2010-05-14 23:24:24 +020010"
Bram Moolenaar543b7ef2013-06-01 14:50:56 +020011" Adapted from indent/perl.vim by Rafael Garcia-Suarez <rgarciasuarez@free.fr>
Bram Moolenaar00a927d2010-05-14 23:24:24 +020012
13" Suggestions and improvements by :
14" Aaron J. Sherman (use syntax for hints)
15" Artem Chuprina (play nice with folding)
16" TODO:
17" This file still relies on stuff from the Perl 5 syntax file, which Perl 6
18" does not use.
19"
20" Things that are not or not properly indented (yet) :
21" - Continued statements
22" print "foo",
23" "bar";
24" print "foo"
25" if bar();
26" - Multiline regular expressions (m//x)
27" (The following probably needs modifying the perl syntax file)
28" - qw() lists
29" - Heredocs with terminators that don't match \I\i*
30
31" Only load this indent file when no other was loaded.
32if exists("b:did_indent")
33 finish
34endif
35let b:did_indent = 1
36
37" Is syntax highlighting active ?
38let b:indent_use_syntax = has("syntax")
39
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020040setlocal indentexpr=GetRakuIndent()
Bram Moolenaar00a927d2010-05-14 23:24:24 +020041
42" we reset it first because the Perl 5 indent file might have been loaded due
43" to a .pl/pm file extension, and indent files don't clean up afterwards
44setlocal indentkeys&
45
46setlocal indentkeys+=0=,0),0],0>,0»,0=or,0=and
47if !b:indent_use_syntax
48 setlocal indentkeys+=0=EO
49endif
50
dkearns0382f052023-08-29 05:32:59 +100051let b:undo_indent = "setlocal indentexpr< indentkeys<"
52
Bram Moolenaar00a927d2010-05-14 23:24:24 +020053let s:cpo_save = &cpo
54set cpo-=C
55
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020056function! GetRakuIndent()
Bram Moolenaar00a927d2010-05-14 23:24:24 +020057
58 " Get the line to be indented
59 let cline = getline(v:lnum)
60
61 " Indent POD markers to column 0
62 if cline =~ '^\s*=\L\@!'
63 return 0
64 endif
65
Bram Moolenaar00a927d2010-05-14 23:24:24 +020066 " Get current syntax item at the line's first char
67 let csynid = ''
68 if b:indent_use_syntax
69 let csynid = synIDattr(synID(v:lnum,1,0),"name")
70 endif
71
72 " Don't reindent POD and heredocs
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020073 if csynid =~ "^rakuPod"
Bram Moolenaar00a927d2010-05-14 23:24:24 +020074 return indent(v:lnum)
75 endif
76
77
78 " Now get the indent of the previous perl line.
79
80 " Find a non-blank line above the current line.
81 let lnum = prevnonblank(v:lnum - 1)
82 " Hit the start of the file, use zero indent.
83 if lnum == 0
84 return 0
85 endif
86 let line = getline(lnum)
87 let ind = indent(lnum)
88 " Skip heredocs, POD, and comments on 1st column
89 if b:indent_use_syntax
90 let skippin = 2
91 while skippin
92 let synid = synIDattr(synID(lnum,1,0),"name")
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020093 if (synid =~ "^rakuPod" || synid =~ "rakuComment")
Bram Moolenaar00a927d2010-05-14 23:24:24 +020094 let lnum = prevnonblank(lnum - 1)
95 if lnum == 0
96 return 0
97 endif
98 let line = getline(lnum)
99 let ind = indent(lnum)
100 let skippin = 1
101 else
102 let skippin = 0
103 endif
104 endwhile
105 endif
106
107 if line =~ '[<«\[{(]\s*\(#[^)}\]»>]*\)\=$'
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +0200108 let ind = ind + &sw
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200109 endif
110 if cline =~ '^\s*[)}\]»>]'
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +0200111 let ind = ind - &sw
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200112 endif
113
114 " Indent lines that begin with 'or' or 'and'
115 if cline =~ '^\s*\(or\|and\)\>'
116 if line !~ '^\s*\(or\|and\)\>'
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +0200117 let ind = ind + &sw
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200118 endif
119 elseif line =~ '^\s*\(or\|and\)\>'
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +0200120 let ind = ind - &sw
Bram Moolenaar00a927d2010-05-14 23:24:24 +0200121 endif
122
123 return ind
124
125endfunction
126
127let &cpo = s:cpo_save
128unlet s:cpo_save
129
130" vim:ts=8:sts=4:sw=4:expandtab:ft=vim