/*
 * Copyright (c) 1995, 1996 Gunther Schadow.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

:- style_check(+string).

/*
 * format to a stream
 */
fformat(Stream,Format,List) :-
	sformat(STRING,Format,List),
	write(Stream,STRING).

the_indent(0).
indent_step(2).

:- format_predicate(0'n, format_newline(_Nm)).
:- format_predicate(0'>, format_indent(_Nm)).
:- format_predicate(0'<, format_exdent(_Nm)).
:- format_predicate(0'/, format_fixindent(_Nm)).
:- format_predicate(0'', format_dquote(_)).

:- redefine_system_predicate(nl).

nl :- format_newline(1).

format_newline(_) :-
	current_output(Stream),
	nl(Stream),
	the_indent(Ind),
	tab(Stream, Ind).

format_indent(Nm) :-
	current_output(Stream),
	the_indent(Ind),
	indent_step(Step),
	( Nm = default ->
	    NewInd is Ind + Step;
	    NewInd is Ind + Step * Nm ),
	asserta(the_indent(NewInd)),
	forward(Stream, NewInd).

format_exdent(_) :-
	the_indent(Ind),
	( Ind > 0 ->
	    retract(the_indent(Ind));
	    true ).
	
format_fixindent(Nm) :-
	current_output(Stream),
	( Nm = default ->
	    line_position(Stream, Pos); 
	    Pos = Nm ),
	asserta(the_indent(Pos)).
	
format_dquote(_) :- put(34).

/*********************
 *
 * Old formatting techniques -- do not use them anymore!
 */

/*
column_width(8).
right_threshold(48).
right_margin(60).
 */

column_width(8).
right_threshold(8).
right_margin(60).

:- format_predicate(0'B, format_spc_break(_Lm)).
:- format_predicate(0'S, format_dquote(_)).
:- format_predicate(b, format_tab_break(_Lm)).
:- format_predicate(v, format_vtab(_Cw)).

forward(Stream, Ind) :-
	line_position(Stream, Pos),
	Diff is Ind - Pos;
	( Diff > 0 ->
	    tab(Stream, Diff);
	    true ).

format_spc_break(_) :-
	current_output(Stream),
	line_position(Stream, Pos),
	right_margin(Rs),
	Pos < Rs, !,
	tab(Stream, 1).

format_spc_break(Lm) :- 
	current_output(Stream),
	nl(Stream),
	tab(Stream, Lm).

format_tab_break(Lm) :- 
	current_output(Stream),
	line_position(Stream, Pos),
	column_width(Cw),
	right_threshold(Rs),
	Pos + Cw - ( ( Pos - Lm ) mod Cw ) > Rs, !, 
	nl(Stream),
	tab(Stream, Lm).

format_tab_break(Lm) :-
	current_output(Stream),
	line_position(Stream, Pos),
	column_width(Cw),
	Sp is Cw - ( ( Pos - Lm ) mod Cw ), 
	tab(Stream, Sp).

format_vtab(Cw) :- 
	current_output(Stream),
	line_position(Stream, Pos),
	Sp is Cw - ( Pos mod Cw ), 
	tab(Stream, Sp).

