(*********************************************************************

	Copyright 1989 by Roman E. Maeder
	
	Adapted from
	Roman E. Maeder: Programming in Mathematica, Addison-Wesley, 1989.

	Permission is hereby granted to make copies of this file for
	any purpose other than direct profit, or as part of a
	commercial product, provided this copyright notice is left
	intact.  Sale, other than for the cost of media, is prohibited.
	
	Permission is hereby granted to reproduce part or all of
	this file, provided that the source is acknowledged.

 *********************************************************************)


Transcript::usage = "Transcript[filename] opens filename for a transcript
	of the current session. It clobbers $Post"
EndTranscript::usage = "EndTranscript[] closes the transcript opened with Transcript[]."

Begin["`Private`"]

`transcript=""

Transcript[filename_String] := (
	transcript = OpenWrite[filename];
	If[ Head[transcript] =!= String, Return[transcript] ];
	$Post := ($Post := MakeTranscript; Identity);
	transcript
	)

EndTranscript[] := (
	MakeTranscript;
	$Post =. ;
	Close[transcript]
	)

GetInputForm[v_ValueList] :=
	Block[{input},
		input = MapAt[HoldForm, v, {1, 2}] [[1,2]];
		input = Characters[ToString[InputForm[input]]];
		input = Drop[input, {1, 9}]; (* HoldForm[ *)
		input = Drop[input, -1];     (* ]         *)
		StringJoin @@ input
	]
	
MakeTranscript :=
	Block[{input, output},
		input  = GetInputForm[Take[DownValue[In],  {-2}]];
		output = GetInputForm[Take[DownValue[Out], {-1}]];
		WriteString[transcript, "(* In[", $Line-1, "]=\n", input, "\n*)\n"];
		WriteString[transcript, "\n", output, "\n\n"];
		Identity
	]

End[]
Null
