
###########################################################################
#                                                                         #
#  @OrdinaryLayout extension to @DocumentLayout.                          #
#                                                                         #
#  Jeffrey H. Kingston                                                    #
#  August 1994                                                            #
#                                                                         #
#  This package extends DocumentLayout for ordinary documents.            #
#                                                                         #
###########################################################################

extend @DocumentLayout
export @Doc @Document @Text
def @OrdinaryLayout

    named @IndexWord			{ index			}
    named @IndexAWord			{ index			}
    named @IndexBWord			{ index			}
    named @AppendixWord			{ appendix		}

    named @SectionNumbers		{ Arabic		}
    named @SubSectionNumbers		{ Arabic		}
    named @SubSubSectionNumbers		{ Arabic		}
    named @AppendixNumbers		{ UCAlpha		}
    named @SubAppendixNumbers		{ Arabic		}
    named @SubSubAppendixNumbers	{ Arabic		}

    named @SectionHeadingFont		{ Bold			}
    named @SectionHeadingBreak		{ ragged 1.2fx nohyphen }
    named @SectionHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @SubSectionHeadingFont	{ Bold			}
    named @SubSectionHeadingBreak	{ ragged 1.2fx nohyphen	}
    named @SubSectionHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @SubSubSectionHeadingFont	{ Slope			}
    named @SubSubSectionHeadingBreak	{ ragged 1.2fx nohyphen	}
    named @SubSubSectionHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @AppendixHeadingFont		{ Bold			}
    named @AppendixHeadingBreak		{ ragged 1.2fx nohyphen	}
    named @AppendixHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @SubAppendixHeadingFont	{ Bold			}
    named @SubAppendixHeadingBreak	{ ragged 1.2fx nohyphen	}
    named @SubAppendixHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @SubSubAppendixHeadingFont	{ Slope			}
    named @SubSubAppendixHeadingBreak	{ ragged 1.2fx nohyphen	}
    named @SubSubAppendixHeadingFormat
	    left number right title	{ number @DotSep title	}

    named @ReferencesHeadingFont	{ Bold			}
    named @ReferencesHeadingBreak	{ ragged 1.2fx nohyphen	}
    named @ReferencesHeadingFormat
	    right title			{ title			}

    named @IndexHeadingFont		{ Bold			}
    named @IndexHeadingBreak		{ ragged 1.2fx nohyphen	}
    named @IndexHeadingFormat
	    right title			{ title			}

    named @IndexAHeadingFont		{ Bold			}
    named @IndexAHeadingBreak		{ ragged 1.2fx nohyphen	}
    named @IndexAHeadingFormat
	    right title			{ title			}

    named @IndexBHeadingFont		{ Bold			}
    named @IndexBHeadingBreak		{ ragged 1.2fx nohyphen	}
    named @IndexBHeadingFormat
	    right title			{ title			}

    named @SectionGap			{ 2.00v			}
    named @SubSectionGap		{ 1.50v			}
    named @SubSubSectionGap		{ 1.50v			}
    named @AppendixGap			{ 2.00v			}
    named @SubAppendixGap		{ 1.50v			}
    named @SubSubAppendixGap		{ 1.50v			}

    named @SectionInContents		{ Yes			}
    named @SubSectionInContents		{ Yes			}
    named @SubSubSectionInContents	{ No			}
    named @AppendixInContents		{ Yes			}
    named @SubAppendixInContents	{ Yes			}
    named @SubSubAppendixInContents	{ No			}
    named @ReferencesInContents		{ Yes			}
    named @IndexInContents		{ Yes			}
    named @IndexAInContents		{ Yes			}
    named @IndexBInContents		{ Yes			}

    named @SectionNumInTheorems		{ No			}
    named @SubSectionNumInTheorems	{ No			}
    named @SubSubSectionNumInTheorems	{ No			}
    named @AppendixNumInTheorems	{ No			}
    named @SubAppendixNumInTheorems	{ No			}
    named @SubSubAppendixNumInTheorems	{ No			}

    named @SectionNumInDisplays		{ Yes			}
    named @SubSectionNumInDisplays	{ No			}
    named @SubSubSectionNumInDisplays	{ No			}
    named @AppendixNumInDisplays	{ Yes			}
    named @SubAppendixNumInDisplays	{ No			}
    named @SubSubAppendixNumInDisplays	{ No			}

    named @SectionNumInFigures		{ No			}
    named @SubSectionNumInFigures	{ No			}
    named @SubSubSectionNumInFigures	{ No			}
    named @AppendixNumInFigures		{ No			}
    named @SubAppendixNumInFigures	{ No			}
    named @SubSubAppendixNumInFigures	{ No			}

    named @SectionNumInTables		{ No			}
    named @SubSectionNumInTables	{ No			}
    named @SubSubSectionNumInTables	{ No			}
    named @AppendixNumInTables		{ No			}
    named @SubAppendixNumInTables	{ No			}
    named @SubSubAppendixNumInTables	{ No			}
@Begin

    #######################################################################
    #                                                                     #
    #  Lists of sections, sub(-sub)sections, appendices, etc.             #
    #                                                                     #
    #######################################################################

    def @TextPlace { @Galley }

    export num
    def @SectionList named @Tag {} right num
    {
	@Galley
        //@SectionGap	@SectionList @Next num
    }

    export num
    def @SubSectionList named @Tag {} right num
    {
	@Galley
        //@SubSectionGap    @SubSectionList @Next num
    }

    export num
    def @SubSubSectionList named @Tag {} right num
    {
	@Galley
        //@SubSubSectionGap @SubSubSectionList @Next num
    }

    export num
    def @AppendixList named @Tag {} right num
    {
	@Galley
        //@AppendixGap	@AppendixList @Next num
    }

    export num
    def @SubAppendixList named @Tag {} right num
    {
	@Galley
        //@SubAppendixGap   @SubAppendixList @Next num
    }

    export num
    def @SubSubAppendixList named @Tag {} right num
    {
	@Galley
        //@SubSubAppendixGap   @SubSubAppendixList @Next num
    }


    #######################################################################
    #                                                                     #
    #  @SendFull - send full-width text.                                  #
    #                                                                     #
    #######################################################################

    def @SendFull force into { @FullPlace&&following }
	right x
    {
	x
    }


    #######################################################################
    #                                                                     #
    #  Document.                                                          #
    #                                                                     #
    #######################################################################

    def @Document
        named @InitialFont { @InitialFont }
        named @InitialBreak { @InitialBreak }
        named @InitialSpace { @InitialSpace }
        named @InitialLanguage { @InitialLanguage }
        named @PageHeaders { @PageHeaders }
        named @ColumnNumber { @ColumnNumber }
        named @FirstPageNumber { @FirstPageNumber }
	named @OptimizePages { @OptimizePages }
    {
	def @ReferencesPart
	    named @Tag {}
	{
		@ReferencesHeadingFont @Font @ReferencesHeadingBreak @Break
		  { @ReferencesHeadingFormat @RefHeading @RefListTitle }
	    //  @PageMarker&&preceding @Tagged @Tag
	    //  @ReferencesInContents @MajorContentsEntry
		    title { @RefHeading @RefListTitle }
		    pagenum { @PageOf @Tag }
	    //  NonStart @Runner @MajorTitle { @RefHeading @RefListTitle }
            //@RefListGap  @ReferencesSection
	    //  NonStart @Runner @MajorTitle { @RefHeading @RefListTitle }
	}

        def @IndexPart
	    named @Tag {}
        {
		@SendFull {
		       Start @Runner @MajorTitle { index @WordVal @IndexWord }
		    // @IndexHeadingFont @Font @IndexHeadingBreak @Break {
			  @IndexHeadingFormat { index @WordVal @IndexWord }
			}
		}
            //  @PageMarker&&preceding @Tagged @Tag
            //  @IndexInContents @MajorContentsEntry
		    title { index @WordVal @IndexWord }
		    pagenum { @PageOf @Tag }
            //  NonStart @Runner @MajorTitle { index @WordVal @IndexWord }
            //  @IndexSection { NonStart @Runner @MajorTitle {
		    index @WordVal @IndexWord
		} }
        }

        def @IndexAPart
	    named @Tag {}
        {
		@SendFull {
		       Start @Runner @MajorTitle { index @WordVal @IndexAWord }
		    // @IndexAHeadingFont @Font @IndexAHeadingBreak @Break {
			  @IndexAHeadingFormat { index @WordVal @IndexAWord }
			}
		}
            //  @PageMarker&&preceding @Tagged @Tag
            //  @IndexAInContents @MajorContentsEntry
		    title { index @WordVal @IndexAWord }
		    pagenum { @PageOf @Tag }
            //  NonStart @Runner @MajorTitle { index @WordVal @IndexAWord }
            //  @IndexASection { NonStart @Runner @MajorTitle {
		    index @WordVal @IndexAWord
		} }
        }

        def @IndexBPart
	    named @Tag {}
        {
		@SendFull {
		       Start @Runner @MajorTitle { index @WordVal @IndexBWord }
		    // @IndexBHeadingFont @Font @IndexBHeadingBreak @Break {
			  @IndexBHeadingFormat { index @WordVal @IndexBWord }
			}
		}
            //  @PageMarker&&preceding @Tagged @Tag
            //  @IndexBInContents @MajorContentsEntry
		    title { index @WordVal @IndexBWord }
		    pagenum { @PageOf @Tag }
            //  NonStart @Runner @MajorTitle { index @WordVal @IndexBWord }
            //  @IndexBSection { NonStart @Runner @MajorTitle {
		    index @WordVal @IndexBWord
		} }
        }

        def @DocumentBody force into { @ColPlace&&preceding }
	    named @Optimize { @OptimizePages }
        {
	    # this whole comment is now obsolete:
	    # It's important that the first component of @DocumentBody be
	    # indefinite.  If it was definite, then @DocumentBody would
	    # attach immediately to the first page.  This would prevent
	    # @FullWidth from attaching to that page, with the result that
	    # a document starting with @FullWidth would start on page 2!

	    //  @FootNoteThrough @Do @BeginFootNoteCounter
	    //	@TextPlace
	    //@SectionGap @EndFigureList
	    //@SectionGap @EndNoteList 1
	    //@SectionGap @MakeReferences @Do @ReferencesPart
	    //  @MakeIndexA @Do @IndexAPart
	    //  @MakeIndexB @Do @IndexBPart
	    //  @MakeIndex  @Do @IndexPart
            //	NonStart @Runner
        }

        @InitialFont @Font @InitialBreak @Break @InitialLanguage @Language
	@InitialSpace @Space { @ColourCommand @InitialColour } @SetColour
        {
		Yes @BeginAllCounters {}
		Yes @BeginDisplayCounter {}
		Yes @BeginFigureCounter {}
		Yes @BeginTableCounter {}
	    //  @BeginDefinitionCounter {}
	    //  @BeginClaimCounter {}
	    //  @BeginPropositionCounter {}
	    //  @BeginLemmaCounter {}
	    //  @BeginCorollaryCounter {}
	    //  @BeginExampleCounter {}
            //  @PageList
		    @ColumnNumber { @ColumnNumber }
		    @PageHeaders { @PageHeaders }
		    extra { Yes }
		    @FirstPageNumber
	    //  @DocumentBody
        }
    }

    macro @Doc { @Document // }


    #######################################################################
    #                                                                     #
    #  @Text containing sections, appendices, etc.                        #
    #                                                                     #
    #######################################################################

    export @ContentsGoesHere @FullWidth
	   @BeginSections @EndSections @Section
	   @BeginAppendices @EndAppendices @Appendix
    def @Text force into { @TextPlace&&preceding }
        body @Body
    {
	def @ContentsGoesHere { @ContentsSection }

	export @ContentsGoesHere
	def @FullWidth body @Body
	{
	    def @ContentsGoesHere {}

            @SendFull { Start @Runner // @Body }
	    & NonStart @Runner & &2.1b &
	}

        def @EndSectionsPlace { @Galley }

        def @EndSections force into { @EndSectionsPlace&&preceding }
	{}

        macro @BeginSections
	{   //@SectionGap @SectionList 1
	    // @EndSectionsPlace //
	}

        export @BeginSubSections @EndSubSections @SubSection
        def @Section force into { @SectionList&&preceding }
            named @Tag {}
            named @Title {}
            named @RunningTitle { dft }
	    named @InitialLanguage {}
	    named @BypassNumber { dft }
            body @Body
        {

            def @SectionNum
	    {
	      @BypassNumber @Dft {
		@SectionNumbers @Then {
		    @SectionNumbers @Num { @SectionList&&@Tag @Open { num } }
		}
	      }
	    }

            def @SectionTitle { @InitialLanguage @Language { @RunningTitle @Dft @Title } }

            def @EndSubSectionsPlace { @Galley }

            def @EndSubSections force into { @EndSubSectionsPlace&&preceding }
	    {}

            macro @BeginSubSections
            {	//@SubSectionGap @SubSectionList 1
		// @EndSubSectionsPlace //
	    }

	    export @BeginSubSubSections @EndSubSubSections @SubSubSection
            def @SubSection force into { @SubSectionList&&preceding }
		named @Tag {}
		named @Title {}
		named @RunningTitle { dft }
		named @InitialLanguage { @InitialLanguage }
		named @BypassNumber { dft }
		body @Body
            {

                def @SubSectionNum
		{
		  @BypassNumber @Dft {
		    @SubSectionNumbers @Then {
			@SectionNum @DotJoin @SubSectionNumbers @Num
			{ @SubSectionList&&@Tag @Open { num } }
		    }
		  }
		}

                def @EndSubSubSectionsPlace { @Galley }

                def @EndSubSubSections force into
		    { @EndSubSubSectionsPlace&&preceding }
		{}

                macro @BeginSubSubSections
                {   //@SubSubSectionGap @SubSubSectionList 1
		    // @EndSubSubSectionsPlace //
		}

		def @SubSubSection force into { @SubSubSectionList&&preceding }
		    named @Tag {}
		    named @Title {}
		    named @RunningTitle { dft }
		    named @InitialLanguage { @InitialLanguage }
		    named @BypassNumber { dft }
		    body @Body
		{

		    def @SubSubSectionNum
	            {
		      @BypassNumber @Dft {
		        @SubSubSectionNumbers @Then {
		            @SubSectionNum @DotJoin @SubSubSectionNumbers @Num
		            { @SubSubSectionList&&@Tag @Open { num } }
		        }
		      }
	            }

	                @InitialLanguage @Language @SubSubSectionHeadingFont @Font
			@SubSubSectionHeadingBreak @Break @Protect
			  { @SubSubSectionNum @SubSubSectionHeadingFormat @Title }
	            //  @NumberMarker @Tag { @Tag } @Value { @SubSubSectionNum }
	            //  @SubSubSectionList&&preceding @Tagged @Tag
	            //  @PageMarker&&preceding @Tagged @Tag
	            //  @SubSubSectionInContents @ContentsEntry
			    indent { 6f }
			    number { @SubSubSectionNum }
			    title { @InitialLanguage @Language @Title }
			    pagenum { @PageOf @Tag }
		    //  @SubSubSectionNumInTheorems @BeginAllCounters @SubSubSectionNum
		    //  @SubSubSectionNumInDisplays @BeginDisplayCounter @SubSubSectionNum
		    //  @SubSubSectionNumInFigures @BeginFigureCounter @SubSubSectionNum
		    //  @SubSubSectionNumInTables @BeginTableCounter @SubSubSectionNum
	            //  @InitialLanguage @Language @Body
	        }

		    @InitialLanguage @Language @SubSectionHeadingFont @Font
	            @SubSectionHeadingBreak @Break @Protect
		      { @SubSectionNum @SubSectionHeadingFormat @Title }
	        //  @NumberMarker @Tag { @Tag } @Value { @SubSectionNum }
	        //  @SubSectionList&&preceding @Tagged @Tag
	        //  @PageMarker&&preceding @Tagged @Tag
	        //  @SubSectionInContents @ContentsEntry
		        indent { 3f }
		        number { @SubSectionNum }
		        title { @InitialLanguage @Language @Title }
		        pagenum { @PageOf @Tag }
		//  @SubSectionNumInTheorems @BeginAllCounters @SubSectionNum
		//  @SubSectionNumInDisplays @BeginDisplayCounter @SubSectionNum
		//  @SubSectionNumInFigures @BeginFigureCounter @SubSectionNum
		//  @SubSectionNumInTables @BeginTableCounter @SubSectionNum
	        //  @InitialLanguage @Language @Body
            }

                @InitialLanguage @Language @SectionHeadingFont @Font
		@SectionHeadingBreak @Break @Protect
		  { @SectionNum @SectionHeadingFormat @Title }
            //  @NumberMarker @Tag { @Tag } @Value { @SectionNum }
            //  @SectionList&&preceding @Tagged @Tag
            //  @PageMarker&&preceding @Tagged @Tag
            //  @SectionInContents @MajorContentsEntry
		    number { @SectionNum }
		    title { @InitialLanguage @Language @Title }
		    pagenum { @PageOf @Tag }
            //  NonStart @Runner
	            @MajorNum { @SectionNum }
	            @MajorTitle { @SectionTitle }
	    //  @SectionNumInTheorems @BeginAllCounters @SectionNum
	    //  @SectionNumInDisplays @BeginDisplayCounter @SectionNum
	    //  @SectionNumInFigures @BeginFigureCounter @SectionNum
	    //  @SectionNumInTables @BeginTableCounter @SectionNum
            //  @InitialLanguage @Language @Body
            //  NonStart @Runner
	            @MajorNum { @SectionNum }
	            @MajorTitle { @SectionTitle }

        }

        def @EndAppendicesPlace { @Galley }

        def @EndAppendices force into { @EndAppendicesPlace&&preceding }
	{}

        macro @BeginAppendices
        {   //@AppendixGap @AppendixList 1
	    // @EndAppendicesPlace //
	}

	export @BeginSubAppendices @EndSubAppendices @SubAppendix
	def @Appendix force into { @AppendixList&&preceding }
            named @Tag {}
            named @Title {}
            named @RunningTitle { dft }
	    named @InitialLanguage {}
	    named @BypassNumber { dft }
            body @Body
	{
            def @EndSubAppendicesPlace { @Galley }

            def @EndSubAppendices force into
		{ @EndSubAppendicesPlace&&preceding }
	    {}

            macro @BeginSubAppendices
            {	//@SubAppendixGap @SubAppendixList 1
		// @EndSubAppendicesPlace //
	    }

            def @AppendixShortNum
            {
	      @BypassNumber @Dft {
		@AppendixNumbers @Num { @AppendixList&&@Tag @Open { num } }
	      }
            }

            def @AppendixLongNum
            {
		@AppendixNumbers @Then { @InitialLanguage @Language {
		    appendix @WordVal @AppendixWord @AppendixShortNum
		} }
            }

            def @AppendixTitle
	    {
		@InitialLanguage @Language { @RunningTitle @Dft @Title }
	    }

            export @BeginSubSubAppendices @EndSubSubAppendices @SubSubAppendix
            def @SubAppendix force into { @SubAppendixList&&preceding }
		named @Tag {}
		named @Title {}
		named @RunningTitle { dft }
		named @InitialLanguage { @InitialLanguage }
		named @BypassNumber { dft }
		body @Body
            {

		def @SubAppendixNum
		{
		  @BypassNumber @Dft {
		    @SubAppendixNumbers @Then {
		        @AppendixShortNum @DotJoin @SubAppendixNumbers @Num
		        { @SubAppendixList&&@Tag @Open { num } }
		    }
		  }
		}

		def @EndSubSubAppendicesPlace { @Galley }

		def @EndSubSubAppendices force into
		    { @EndSubSubAppendicesPlace&&preceding }
		{}

		macro @BeginSubSubAppendices
		{   //@SubSubAppendixGap @SubSubAppendixList 1
		    // @EndSubSubAppendicesPlace //
		}

		def @SubSubAppendix force into {@SubSubAppendixList&&preceding}
		    named @Tag {}
		    named @Title {}
		    named @RunningTitle { dft }
		    named @InitialLanguage { @InitialLanguage }
		    named @BypassNumber { dft }
		    body @Body
		{

                    def @SubSubAppendixNum
		    {
		      @BypassNumber @Dft {
			@SubSubAppendixNumbers @Then {
			    @SubAppendixNum @DotJoin @SubSubAppendixNumbers
			    @Num { @SubSubAppendixList&&@Tag @Open { num } }
		        }
		      }
		    }

                    def @SubSubAppendixTitle { @RunningTitle @Dft @Title }

			@InitialLanguage @Language @SubSubAppendixHeadingFont @Font
			@SubSubAppendixHeadingBreak @Break @Protect
			  { @SubSubAppendixNum @SubSubAppendixHeadingFormat @Title }
		    //  @NumberMarker @Tag { @Tag } @Value { @SubSubAppendixNum }
		    //  @SubSubAppendixList&&preceding @Tagged @Tag
		    //  @PageMarker&&preceding @Tagged @Tag
		    //  @SubSubAppendixInContents @ContentsEntry
			    indent { 6f }
			    number { @SubSubAppendixNum }
			    title { @InitialLanguage @Language @Title }
			    pagenum { @PageOf @Tag }
		    //  @SubSubAppendixNumInTheorems @BeginAllCounters @SubSubAppendixNum
		    //  @SubSubAppendixNumInDisplays @BeginDisplayCounter @SubSubAppendixNum
		    //  @SubSubAppendixNumInFigures @BeginFigureCounter @SubSubAppendixNum
		    //  @SubSubAppendixNumInTables @BeginTableCounter @SubSubAppendixNum
		    //  @InitialLanguage @Language @Body
		}

                    @InitialLanguage @Language @SubAppendixHeadingFont @Font
		    @SubAppendixHeadingBreak @Break @Protect
		      { @SubAppendixNum @SubAppendixHeadingFormat @Title }
		//  @NumberMarker @Tag { @Tag } @Value { @SubAppendixNum }
		//  @SubAppendixList&&preceding @Tagged @Tag
		//  @PageMarker&&preceding @Tagged @Tag
		//  @SubAppendixInContents @ContentsEntry
			indent { 3f }
			number { @SubAppendixNum }
			title { @InitialLanguage @Language @Title }
			pagenum { @PageOf @Tag }
		//  @SubAppendixNumInTheorems @BeginAllCounters @SubAppendixNum
		//  @SubAppendixNumInDisplays @BeginDisplayCounter @SubAppendixNum
		//  @SubAppendixNumInFigures @BeginFigureCounter @SubAppendixNum
		//  @SubAppendixNumInTables @BeginTableCounter @SubAppendixNum
		//  @InitialLanguage @Language @Body

	    }

		@InitialLanguage @Language @AppendixHeadingFont @Font 
		@AppendixHeadingBreak @Break @Protect
		  { @AppendixLongNum @AppendixHeadingFormat @Title }
            //  @NumberMarker @Tag { @Tag } @Value { @AppendixShortNum }
            //  @AppendixList&&preceding @Tagged @Tag
            //  @PageMarker&&preceding @Tagged @Tag
            //  @AppendixInContents @MajorContentsEntry
		    number { @AppendixLongNum }
		    title { @InitialLanguage @Language @Title }
		    pagenum { @PageOf @Tag }
            //  NonStart @Runner
	            @MajorNum { @AppendixLongNum }
	            @MajorTitle { @AppendixTitle }
	    //  @AppendixNumInTheorems @BeginAllCounters @AppendixShortNum
	    //  @AppendixNumInDisplays @BeginDisplayCounter @AppendixShortNum
	    //  @AppendixNumInFigures @BeginFigureCounter @AppendixShortNum
	    //  @AppendixNumInTables @BeginTableCounter @AppendixShortNum
	    //  @Body
            //  NonStart @Runner
	            @MajorNum { @AppendixLongNum }
	            @MajorTitle { @AppendixTitle }
	}

	//  Start @Runner
	//  @Body
	//  NonStart @Runner

    }

@End @OrdinaryLayout
