// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Scheme;
import bdc.scheme.Stack;
import bdc.scheme.exception.ArgumentTypeException;
import bdc.scheme.expression.Procedure0;
import bdc.util.MultiPrintWriter;
import java.io.PrintWriter;

/**
    (transcript-off)

    see TranscriptOn
*/
public class TranscriptOff extends Procedure0
{
    public Object apply0 (Stack stack) throws ArgumentTypeException
    {
        /*
            Get the output and error MultiPrintWriters
        */
        PrintWriter outPW = stack.currentOutputPort;
        PrintWriter errPW = stack.currentErrorPort;
        if (!((outPW instanceof MultiPrintWriter) &&
              (errPW instanceof MultiPrintWriter)))
        {
            return Scheme.Unspecified;
        }

        /*
            Remove the transcript port from so it no longer receives output
        */
        MultiPrintWriter out = (MultiPrintWriter)outPW;
        MultiPrintWriter err = (MultiPrintWriter)errPW;
        MultiPrintWriter transcript = stack.currentTranscriptPort;
        stack.currentTranscriptPort = null;
        out.removeStream(transcript.stream());
        err.removeStream(transcript.stream());

        /*
            Close the transcript
        */
        transcript.flush();
        transcript.close();

        return Scheme.Unspecified;
    }
}
