// kate: space-indent on; indent-width 4; replace-tabs on; remove-trailing-spaces all;
// kate: syntax pascal; indent-mode pascal;

// these variables are understood by the pascal indenter ...
// kate: cfgIndentCase false;          // indent elements in a case statement
// kate: cfgIndentBegin 0;            // indent 'begin' this many spaces
// kate: debugMode false;              // show how indent is determined
// kate: cfgAutoInsertStar true;      // auto insert '*' in (* ... *)-comments
// kate: cfgSnapParen true;           // snap '* )' to '*)' in comments

begin {main program }

    if f1 > 0 then result := f1-1
    else begin
        f1 :=  -23;
    end;

    case e012 of
    2:
        if eeeeeeeeeeeeeeeeee
                or e33333333333
        then
            a := 12
        else
            for iiiiiiiiiii := 12
                            to 89 // CHECK 89 lines up with 12
            do begin
                if very_long_expression
                        or another_very_long_expression
                        and yet_another_very_long_expression then
                    s1;
                s11;
            end;
    1:
        if e
                +e2
                +e3
                +e2
                or 75  // CHECK: follows first continue line
        then
            for i := 1
                    to 89
            do begin
                a := 12 + 24
                          3 +  // CHECK 3 lines up with 24 above
                          19 +
                          25;
                s1 := e1
                      + e2;
            end;
    5:
        f32verylongfunctionnamezzzzzzzzzzzzzzz(
                               z, // user can realign this anywhere
                               a,b, // this should line up with 'z'''
                               c,
                               d,
                               e
                               );

    3: begin
            s0;
            s3;        // bad code, 'end' is missing
            //end;            // missing end, so next few case values interpreted as labels
5:
            if q then z;

            s3;              // code error - case value belongs here, not statement
5:
            if c2 then s2;

16:
            if c2 then
                s1;
17:
            if c2 then begin s1; s2 end;

4:
            if a then begin
                s2;
                s3 // no trailing semi, but it indents
            end;
        end; { ** bad code ** }   // crossed begin/end - matches '3: begin' above


    2: s2;
    3: while c4
            and e3  do // CHECK: should indent beyond while
            s8;
    1: s1;
    {
    23:
    }
    23: s1;
    s2:
        begin
            s2
            s3 // no trailing semi in line above, but it indents correctly
        end;         // need ';' to force alignment with new case value
    otherwise if a then b;
        if c0 then  begin
            if c1 then begin
            // it does!!
                if c3 then
                    write(              // CHECK: deeply nested levels of parens
                          f(5.5),
                          'test',   // should be aligned
                          notCaseValue:4,
                          f1(8,8),
                          ff( a,
                              b( f(13),
                                 12,
                                 x
                               ),
                              c( g(55) ),

                            ));

            end else  // statement unfinished
    otherwise       // duplicate eotherwise
        if a then begin
            b
        end else if a1 then begin
            c;
            c;
            if a
                    and a1 then
                b
            else
                c;   { CHECK: outdents only when 'c' is followed by ';' }
            if a then begin
                b;
                b1;
            end else
                s3;
            if a then b else c;
            if a then
                b
            else
                c;
            if a then b
            else f( arg,
                    arg2,
                    a1,
                    a2
                    # this is a pp line!!     // bug! silly place to put a pp line
                    f( a,
                       b
                     ));

            if a then
                b
            else if c1 then
                s1;
            if c2 then begin
                s33;
                s34
            end;    { !!! classic error, shouldn't be a semi colon here }
            else if c3 then begin
                s44;
                s45
            end else begin
                s;
                s;
            end

        end else if c1 then begin // comment
            s3
        end;

        while c3 do
            s4;
        end;     // spurious end

        s0;
        s1;
        s2;
        s3;
    end // case e012 of
    else if c2 then begin  // spurious else

        ss;

        if c1 then begin
            write( 'test',
                   notCaseValue:4
                 )
        end else      // this else has no statement
    end    else begin { should still be in sync }

        s1;

        s1;



        s2
    end;

    f(   a,
         b,
     )

     ff(  a,
          b,
          c
       )
       ))

       writeln( 'this is a string ', n:1, ' ', ' ', r:10:2 );

    with p do
    begin

    end;

    with p065 do
    begin
        x := 2;
    end;
    case e07 of
    0:
    1:  s1;
    2:  begin
            s2;
            s3;
        end;
    3: s3;
    12:

    15:
    4,5,6..9: begin s1; end;
    otherwise
        s6;


        s3;
        s1;

        s2;

        while e and
                e2 DO begin { comment }
            s0;
            s33;
            s44;
        end;

        if e97 then begin
            s3;
label:
            while c103 do begin
                s4;
                q := 0;
                case e of
                12,13..16,20: begin
                        s2;
                        s3;
                    end;
                8: writeln( 'test',
                            x,
                            r:4
                          );
                9:

                end;
                while c1
                   and c2 do begin
                    s5;
                    s9;
                end;

                s;
                s;
                s1;
                if e105 then
                    if c2 then begin

                        s3;
                    end; // if

                case e11 of
                2..4,5: begin
                        s1;
                        s2
                    end;


                3: a:= 13;


                89:

                    case e12 of
                    2,3:
                        a:=13;
                    2: begin
                            a:= 13
                        end;
                    5,6,
                    7:
                    otherwise

                        s3;
                        s4;
                    end;  // case e12 of

                otherwise


                    s4;
                    s5;
                    if c2 then
                        s5
                end; // case e11 of

                s3;

            end; // while c103

        end; // if e97

        repeat
            s3;
            s4;
        until c1;

        s0;
        s1;

        if e then begin
            s3;
            s4;
            s0;

            s1;

            s1;
            s1;
        end;

        repeat
            ss;
            s2;
            if e then
                ss;
            s3;
            s1;
        until e;

        if e then begin
            s1;
            s3;
            if e then begin
                s3;
                s5;
            end;
            s6;
            s2;
        end;


        of:=2; // more bad code

        repeat
            s1
        until c;


        a := 123456789;
        IF (e1) or  (* comment *)
                (e2)  then

        begin // CHECK: the begin should follow 'if' line, not 'then' line

            if e then begin s end
            else begin
                s1;
                s2;
            end;

            if e then
                s0;
            if e then begin
                s1;
                s2;       (  // unattached parens

                          )
                          s;  // problem caused by unattached parens
            end
            123456
            { comment }

            if e4 then
                while e5 do begin
                (* ...
                 *  CHECK: stars should line up
                 *
                 *
                 *)
                    (aa)
                    if a then b;   (* qwe
                                    *rty *)
                        cc;
                    s;
                    // comment

                end;


            f( a, b)

            case e of
            12,13: a:=12;
            1,2,3,4..6: writeln(
                                   a, 'TODO:', b );

            567:
                s2;

            88: writeln( 'zzzzzzzzzz',
                         a:12,
                         b
                       );
            99: writeln(
                           z:13,
                           b
                       );
            13: begin
            // no statement
                end;



            0: f( a,
                  b
                );
            12: ff (  a,
                      b
                   );
            (13+12): f( a,
                        37,508.18
                      );



            2: if a = 12 then
                    s1;
                else begin
                    s2;
                end;

            0: writeln( 'zzzzzzzzz',

                        a:3,
                        b:4,
                        'xxxxxxxxxx'

                      );

            1: s1;
            3:



            2: begin

                    s1;
                    s2;
                    s3;
                end;

            5 :
                writeln( 'this is ',
                         'multiline arg list'
                       );




            5:
            6:
                s1;

            9:
                c := 13;

            c2,3,4..7: begin

                    s4;
                    sl;
                    a := 13;

label:  // this is a label, not case value
                    cc;

                    b := 17;

                    writeln( 'strrrrrr', a:4 );

                end;
            c9 :
                s1;

            end; { case e of }
                        // comment
            ff(   a,      // this line aligns with 'end'
                  b,     // CHECK: keep relative indent
                  c
              )
              ;

        end;  // begin CHECK:

ggggg:


        f(
             a,
             b,
             c
         );


        writeln( a,

                 b,

                 b,    { check indent after cr here }
               );

        f(13);
    end; // case e07

end. // main
