justify←{⎕IO ⎕ML←0 1                ⍝ Justify line-vector to width ⍺.
    segs←{¯1+⍵{(⍵,⍴⍺)-¯1,⍵}⍸⍵}      ⍝ 1-separated segment sizes.
    split←{((⍵|⍺)>⍳⍵)+⌊⍺÷1⌈⍵}       ⍝ ⍺ split up ⍵ ways.
    lf sp(⎕UCS 13 32)=⊂⍵           ⍝ <LF> and <SP> chars in line-vector.
    sizessegs lf                   ⍝ line sizes.
    ⍺←⌈/sizes                       ⍝ default width is longest line.
    blankssegs~(lfsp)/sp          ⍝ original number of blanks per line.
    requiredblanks+⍺-sizes         ⍝ required      ..      ..      ..
    brepsrequired split¨blanks     ⍝ blank replication vectors.
    last←1⌈¯1+⍴sizes                ⍝ last line split point.
    brep←∊(lastbreps)lastbreps  ⍝ blank replication vector.
    ((~sp)+sp\∊brep)\⍵              ⍝ ⍺-justified line-vector.
}

code_colours

test script

Back to: notes

Back to: Workspaces