{==============================================================}
{			IXDONE.PAS			       }
{Copyright (C) 1986 David E. Cortesi--see notice in IXMAIN.PAS.}
{ This include file contains declarations and code related to  }
{ writing the output file, INDEX.OUT in the current directory. }
{ See IXTEXT.PAS for support functions. 		       }
{							       }
{ History:						       }
{ append comma to term string when page numbers follow 11/3/86 }
{ initial writing 6/5/86				       }
{==============================================================}

var initial : nchar;

{------ Write a list of page-numbers. Since in collecting the -}
{------ numbers we might have built overlapping ranges, we ----}
{------ must first tidy the list, during which we discard some-}
{------ page records forever ----------------------------------}
procedure outpages(t : tree);
var p, q : ppage;
begin
    p := t^.pages;
    if (p <> nil) then q := p^.next else q := nil;
    while(q <> nil) do begin
	if ((p^.num2+1) >= q^.num1) then begin
	    {range q^ can at most extend the range of p^ up}
	    if (q^.num2 > p^.num2) then p^.num2 := q^.num2;
	    p^.next := q^.next {forget that page record}
	end
	else p := q;
	q := q^.next
    end;
    p := t^.pages;
    if (p <> nil) then begin
	nextout(ord(','));
	nextout(asciiblank);
	nextout(asciiblank)
    end;
    while (p <> nil) do begin
	with p^ do begin
	    if (num1 = 0) then num1 := num1 + 1;
	    if (num1 <= num2) then begin
		intwrite(num1);
		if (num1 < num2) then begin
		    nextout(ord('-'));
		    intwrite(num2)
		end
	    end
	end;
	p := p^.next;
	if (p <> nil) then begin
	    nextout(ord(','));
	    nextout(asciiblank)
	end
    end
end;

{------ write one term to the output, inserting a blank line --}
{------ if its initial varies from the last and we are at the -}
{------ outermost level.  Dump extra/leading/trailing blanks. -}
procedure outterm(t : tree);
var c : nchar;
    i : tdepth;
    j : strindex;
    s : boolean;
begin
    stget(t^.term,workterm);
    if (indlevel = 0) then begin
	c := workterm[1];
	if (chr(c) in allowset) then c := c-32;
	if (c <> initial) then begin
	    nextout(asciicr);
	    initial := c
	end
    end
    else
	for i := 1 to indlevel do begin
	    nextout(asciiblank);
	    nextout(asciiblank);
	    nextout(asciiblank)
	end;
    j := 1;
    s := false;
    while (workterm[j] <> null) do begin
	c := workterm[j];
	if (c <> asciiblank) then begin
	    nextout(c);
	    s := true; {ok to print next space}
	end
	else begin
	    if (s) then nextout(c);
	    s := false {omit further spaces}
	end;
	j := j+1
    end
end;

{------ Display all the terms in a (sub)tree to the output ----}
{------ by walking all its nodes.  Where a term has a subtree -}
{------ recurse to write it. ----------------------------------}
procedure outtree(t : tree);
var position : walk;
    p : tree;
begin
    p := initwalk(t,position);
    while (p <> nil) do begin
	outterm(p);
	outpages(p);
	nextout(asciicr);
	if (p^.subtree <> nil) then begin
	    indlevel := indlevel+1;
	    outtree(p^.subtree);
	    indlevel := indlevel-1
	end;
	p := wstep(position)
    end
end;

{------ write the whole tree to the output --------------------}
procedure finish;
begin
    initout;
    indlevel := 0;
    outtree(maintree);
    nextout(asciieof)
end;
-----------}
pro