2005-08-19 21:46:12 -04:00
|
|
|
! Copyright (C) 2003, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
|
|
|
IN: io
|
2005-12-19 02:12:40 -05:00
|
|
|
USING: errors generic hashtables kernel namespaces sequences
|
|
|
|
strings styles ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
2005-12-19 02:12:40 -05:00
|
|
|
! Default stream
|
2005-12-16 21:12:35 -05:00
|
|
|
SYMBOL: stdio
|
|
|
|
|
|
|
|
: close ( -- ) stdio get stream-close ;
|
|
|
|
|
2005-12-19 02:12:40 -05:00
|
|
|
: readln ( -- string/f ) stdio get stream-readln ;
|
|
|
|
: read1 ( -- char/f ) stdio get stream-read1 ;
|
2005-12-16 21:12:35 -05:00
|
|
|
: read ( count -- string ) stdio get stream-read ;
|
|
|
|
|
|
|
|
: write1 ( char -- ) stdio get stream-write1 ;
|
|
|
|
: write ( string -- ) stdio get stream-write ;
|
|
|
|
: flush ( -- ) stdio get stream-flush ;
|
|
|
|
|
|
|
|
: terpri ( -- ) stdio get stream-terpri ;
|
2005-12-19 23:18:15 -05:00
|
|
|
: terpri* ( -- ) stdio get stream-terpri* ;
|
2005-08-19 21:46:12 -04:00
|
|
|
: format ( string style -- ) stdio get stream-format ;
|
2005-12-16 22:24:39 -05:00
|
|
|
|
|
|
|
: with-nesting ( style quot -- )
|
|
|
|
swap stdio get with-nested-stream ;
|
2005-12-16 21:12:35 -05:00
|
|
|
|
|
|
|
: print ( string -- ) stdio get stream-print ;
|
2005-08-19 21:46:12 -04:00
|
|
|
|
|
|
|
: with-stream ( stream quot -- )
|
|
|
|
#! Close the stream no matter what happens.
|
2005-09-21 01:12:16 -04:00
|
|
|
[ swap stdio set [ close ] cleanup ] with-scope ; inline
|
2005-08-19 21:46:12 -04:00
|
|
|
|
|
|
|
: with-stream* ( stream quot -- )
|
|
|
|
#! Close the stream if there is an error.
|
2005-09-21 01:12:16 -04:00
|
|
|
[ swap stdio set [ close rethrow ] recover ] with-scope ;
|
|
|
|
inline
|
2005-12-19 02:12:40 -05:00
|
|
|
|
|
|
|
SYMBOL: style-stack
|
|
|
|
|
2005-12-19 23:18:15 -05:00
|
|
|
: >style ( style -- )
|
|
|
|
#! Push a style on the style stack.
|
|
|
|
dup hashtable? [ "Style must be a hashtable" throw ] unless
|
|
|
|
style-stack [ ?push ] change ;
|
|
|
|
|
|
|
|
: style> ( -- style )
|
|
|
|
style-stack get pop ;
|
2005-12-19 02:12:40 -05:00
|
|
|
|
|
|
|
: with-style ( style quot -- )
|
2005-12-19 23:18:15 -05:00
|
|
|
[ >r >style r> call style> drop ] with-scope ; inline
|
2005-12-19 02:12:40 -05:00
|
|
|
|
2005-12-28 20:25:17 -05:00
|
|
|
: current-style ( -- style )
|
|
|
|
#! Always returns a fresh hashtable.
|
|
|
|
style-stack get hash-concat ;
|
2005-12-19 02:12:40 -05:00
|
|
|
|
|
|
|
: format* ( string -- ) current-style format ;
|
|
|
|
|
2006-01-16 02:48:15 -05:00
|
|
|
: bl ( -- ) H{ { word-break t } } [ " " format* ] with-style ;
|
2005-12-28 20:25:17 -05:00
|
|
|
|
2005-12-19 23:18:15 -05:00
|
|
|
: with-nesting* ( quot -- )
|
|
|
|
current-style swap with-nesting ; inline
|
|
|
|
|
2005-12-19 02:12:40 -05:00
|
|
|
: write-object ( object quot -- )
|
|
|
|
>r presented associate r> with-style ;
|
|
|
|
|
|
|
|
: simple-object ( string object -- )
|
|
|
|
#! Writes a clickable presentation with the specified string.
|
|
|
|
[ format* ] write-object ;
|
|
|
|
|
|
|
|
: write-outliner ( content caption -- )
|
|
|
|
#! Takes a pair of quotations.
|
|
|
|
>r outline associate r> with-nesting terpri ;
|
|
|
|
|
|
|
|
: simple-outliner ( string object content -- )
|
|
|
|
[ simple-object ] write-outliner ;
|