refmt←{⎕IO←0                        ⍝ Reformat dfn/op.
    ⍺←4 ⋄ dent csep←⍺               ⍝ default indents and ⍝-separation
    unqt←{~≠\⍵=''''}                ⍝ mask of unquoted tokens
    code coms←↓⍉↑{                  ⍝ code and comment vectors
        umskunqt ⍵                 ⍝ unquoted chars
        cmsk←∨\umsk∧⍵='⍝'           ⍝ comment mask
        code coms(0 1=⊂cmsk)/¨⊂⍵   ⍝ code / comments
        rlb←{(∨\' '≠⍵)/⍵}           ⍝ remove leading blanks
        clean←⌽rlbrlb code         ⍝ cleaned code
        clean coms                  ⍝ clean code / comment
    }¨↓↑⍵                           ⍝ for each line
    dents←{                         ⍝ indentation for each line
        toks(unqt){⍺\⍺/⍵}⍵       ⍝ unquoted code characters
        dvec←-⌿0 1⌽+\'{}'∘.=toks    ⍝ brace-depth of each token
        lmasktoks='·'              ⍝ start of each line
        (dent×lmask/dvec)/¨' '      ⍝ indentation for each line
    }↑,/'·',¨code                   ⍝ collected code vector
    scoms(csep⍴' ')coms         ⍝ separated comments
    icode←↓↑dentscode             ⍝ indented code lines
    rtb←⌽∘{(∨\⍵≠' ')/⍵}∘⌽           ⍝ remove trailing blanks
    qnricode,∘rtb¨scoms            ⍝ reunited code and comments
    ↑⍣(1=≡⍵)qnr                    ⍝ ⎕cr or ⎕nr
}
code_colours

test script

Back to: notes

Back to: Workspaces