! Copyright (C) 2003, 2006 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. IN: prettyprint-internals USING: alien arrays generic hashtables io kernel math namespaces parser sequences strings styles vectors words prettyprint ; ! Sections TUPLE: section start end style ; C: section ( style length -- section ) >r position [ dup rot + dup ] change r> [ set-section-end ] keep [ set-section-start ] keep [ set-section-style ] keep ; GENERIC: section-fits? ( section -- ? ) M: section section-fits? ( section -- ? ) section-end last-newline get - text-fits? ; GENERIC: short-section ( section -- ) GENERIC: long-section ( section -- ) GENERIC: block-empty? ( section -- ? ) : pprint-section ( section -- ) { { [ margin get zero? ] [ short-section ] } { [ dup section-fits? ] [ short-section ] } { [ t ] [ long-section ] } } cond ; ! Block sections TUPLE: block sections ; C: block ( style -- block ) swap 0
over set-delegate V{ } clone over set-block-sections ; : pprinter-block ( -- block ) pprinter-stack get peek ; : add-section ( section -- ) dup block-empty? [ drop ] [ pprinter-block block-sections push ] if ; M: block block-empty? block-sections empty? ; M: block section-fits? ( section -- ? ) line-limit? [ drop t ] [ delegate section-fits? ] if ; : ( ] change ; : style> stdio [ delegate ] change ; : change-indent ( n -- ) tab-size get * indent [ + ] change ; : ( block -- ) -1 change-indent ; ! Text section TUPLE: text string ; C: text ( string style -- text ) [ >r over length 1+
r> set-delegate ] keep [ set-text-string ] keep ; M: text block-empty? drop f ; M: text short-section dup text-string swap section-style format ; M: text long-section dup section-start fresh-line short-section ; : styled-text ( string style -- ) add-section ; : text ( string -- ) H{ } styled-text ; ! Newline section TUPLE: newline ; C: newline ( -- section ) H{ } 0
over set-delegate ; M: newline block-empty? drop f ; M: newline section-fits? drop t ; M: newline short-section section-start fresh-line ; : newline ( -- ) add-section ; ! Inset section TUPLE: inset ; C: inset ( style -- block ) swap over set-delegate ; M: inset section-fits? ( section -- ? ) line-limit? [ drop t ] [ section-end last-newline get - 2 + text-fits? ] if ; : advance ( section -- ) dup newline? [ drop ] [ section-start last-newline get = [ bl ] unless ] if ; M: block short-section ( block -- ) dup