<?xml version="1.0" encoding="Shift_JIS" ?>

<Define> ^LoadFile ()
    <Argument> ^type ^sym ^from </Argument> <Sequence> ()
	<Define> ^file (GetElement sym 'file "type" type) </Define>
	<If> (= (ListLength file) 1)
	    <Then> <set> ^file ^from </set></Then>
	    <Else><Sequence> ()
		<set> ^file (GetElement file 1) </set>
		<If> (= (GetChar file 0) (GetChar "/" 0))
		    <Then> <set> ^file (+ "xldoc" file) </set> </Then>
		    <Else> <set> ^file (+ (Local dir) file) </set> </Else>
		</If>
	    </Sequence></Else>
	</If>
	<Load option="exec"> ^file </Load>
    </Sequence>
</Define>

<Define> ^InsertPoint ()
    <Argument> ^ptr </Argument>
    <If> (> mr_tl_x mr_br_x)
	<Then><Sequence> ()
	    <set> ^mr_tl_x (GetElement ptr 0) </set>
	    <set> ^mr_tl_y (GetElement ptr 1) </set>
	    <set> ^mr_br_x ^mr_tl_x </set>
	    <set> ^mr_br_y ^mr_tl_y </set>
	</Sequence></Then>
	<Else><Sequence> ()
	    <If> (> mr_tl_x (GetElement ptr 0))
		<Then><set> ^mr_tl_x (GetElement ptr 0)</set>
		</Then></If>
	    <If> (> mr_tl_y (GetElement ptr 1))
		<Then><set> ^mr_tl_y (GetElement ptr 1)</set>
		</Then></If>
	    <If> (&lt; mr_br_x (GetElement ptr 0))
		<Then><set> ^mr_br_x (GetElement ptr 0)</set>
		</Then></If>
	    <If> (&lt; mr_br_y (GetElement ptr 1))
		<Then><set> ^mr_br_y (GetElement ptr 1)</set>
		</Then></If>
	</Sequence></Else>
    </If>
</Define>

<Define Order="Applicative">
    ^OldVectorMinrect ()
    <Argument> ^sym ^from </Argument>
    <Sequence> ()
	<Define> ^vector <NewEnvironment><CurrentEnvironment/>
		</NewEnvironment></Define>
	<Sequence> ^vector
	    <DefineDefault Order="Applicative">
        	() ^sym ^sym
	    </DefineDefault>
	    <Define Order="Normal"> ^meta ^sym ^sym </Define>
	    <Define> ^mr_tl_x 0m </Define>
	    <Define> ^mr_tl_y 0m </Define>
	    <Define> ^mr_br_x -1m </Define>
	    <Define> ^mr_br_y -1m </Define>
	    <Define> ^point ()
		<Argument> ^name ^no ^ptr </Argument>
		<InsertPoint> ^ptr </InsertPoint>
	    </Define>
	    <Define Order="Normal"> ^mark ^ptr
		<Sequence> ()
		<Define> ^point () 
		<Arguments> ^x ^y </Arguments>
			<InsertPoint> (List x y) </InsertPoint>
		</Define>
		<Eval> <CurrentEnvironment/> (GetElement ptr 1) </Eval>
		</Sequence>
	    </Define>
	    <Define> ^svg (NewEnvironment (CurrentEnvironment)) </Define>
	    <svg type="raw">
		<Define> ^path ()
		<Arguments/>
		<Sequence> ()
		    <Define> ^dd (DivideString d " ") </Define>
		    <For> (Not (= dd ()))
		    <Sequence> ()
			<set> ^dd (cdr dd) </set>
			<Define> ^x ([Cast type="floating"]
					(car dd)) </Define>
			<set> ^dd (cdr dd) </set>
			<Define> ^y ([Cast type="floating"]
					(car dd)) </Define>
			<InsertPoint> (List x y) </InsertPoint>
			<set> ^dd (cdr dd) </set>
		    </Sequence>
		    </For>
		</Sequence>
		</Define>
	    </svg>
	</Sequence>
	<LoadFile> xl ^sym ^from </LoadFile>
	<vector>
	    <List> ^'mr
		<List>
			<List> ^mr_tl_x ^mr_tl_y </List>
			<List> ^mr_br_x ^mr_br_y </List>
		</List>
	    </List>
	</vector>
    </Sequence>
</Define>

<Define Order="Applicative">
    ^OldMapMinrect ()
    <Argument> ^sym ^from </Argument>
    <Sequence> ()
	<Define> ^map <NewEnvironment><CurrentEnvironment/>
		</NewEnvironment></Define>
	<Sequence> ^map
	    <DefineDefault Order="Applicative">
        	() ^sym ^sym
	    </DefineDefault>
	    <Define Order="Normal"> ^meta ^sym ^sym </Define>
	    <Define> ^mr_tl_x 0m </Define>
	    <Define> ^mr_tl_y 0m </Define>
	    <Define> ^mr_br_x -1m </Define>
	    <Define> ^mr_br_y -1m </Define>
	    <Define> ^map-count 0 </Define>
	    <Define> ^point-map ()
		<Argument> ^name ^ptr-src ^ptr-dest </Argument><Sequence> ()
			<InsertPoint> ^ptr-dest </InsertPoint>
			<set> ^map-count (+ map-count 1) </set>
		</Sequence>
	    </Define>
	</Sequence>
	<If> (Neq (Prefix (GetElement (GetElement sym 'src) 1)) ".crd")
		<Then><LoadFile> xl ^sym ^from </LoadFile>
		</Then></If>
	<map>
	    <If> (>= 3 map-count)
		<Then>
		    <List> ^'mr
			<List>
				<List> 0m 0m </List>
				<List> -1m -1m </List>
			</List>
		    </List>
		</Then>
		<Else>
		    <List> ^'mr
			<List>
				<List> ^mr_tl_x ^mr_tl_y </List>
				<List> ^mr_br_x ^mr_br_y </List>
			</List>
		    </List>
		</Else>
	    </If>
	</map>
    </Sequence>
</Define>

<Define> ^coordinate
 <NewEnvironment><CurrentEnvironment/></NewEnvironment>
</Define>
<Sequence> ^coordinate
    <DefineDefault Order="Normal">
        ^sym ^sym
    </DefineDefault>
    <Define Order="Normal"> ^meta ^sym
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> coordinate </category>
                        </quote>
                    </InsertBefore>
                </MakeElement>
            </List>
        </Save>
    </Define>
</Sequence>


<Define> ^map
    <NewEnvironment><CurrentEnvironment/></NewEnvironment>
</Define>
<Sequence> ^map
    <DefineDefault Order="Applicative">
        () ^sym ^sym
    </DefineDefault>
    <Define Order="Normal"> ^meta ^sym <Sequence> ()
	<Define> ^minrect <OldMapMinrect> ^sym <Local> ^from </Local>
			</OldMapMinrect></Define>
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> map </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter>
			^'file
			^minrect
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
    </Sequence></Define>
</Sequence>


<Define> ^luster
    <NewEnvironment><CurrentEnvironment/></NewEnvironment>
</Define>
<Sequence> ^luster
    <DefineDefault Order="Applicative">
        () ^sym ^sym
    </DefineDefault>
    <Define Order="Applicative">
	^LusterMinrectR64 ()
	<Argments> ^sym </Argments>
	<Sequence> ()
	    <Define> ^file (GetElement sym 'file "type" "r64")
	    </Define>
	    <Define> ^base-unit (GE sym 'base-unit) </Define>
	    <If> (= base-unit ())
	    <Then>
		<Define> ^unit m </Define>
		<Define> ^system base </Define>
		<Define> ^reso-unit dot/m </Define>
	        <Define> ^base-unit ^'<base-unit unit="m" system="base"/>
		</Define>
	    </Then>
	    <Else>
		<Define> ^unit (GetAttribute (car base-unit) "unit") </Define>
		<Define> ^reso-unit (+ "dot/" unit) </Define>
		<Define> ^system (GetAttribute (car base-unit) "system")
		</Define>
		<If> (= system ()) <Then>
		    <Define> ^system base </Define>
		</Then></If>
	    </Else>
	    </If>
	    <If> (Not (= file ()) )
	    <Then><Sequence> ()

	    <If> (= (ListLength file) 1)
		<Then> <set> ^file (Local from)</set> </Then>
		<Else> <set> ^file (GetElement file 1) </set> </Else>
	    </If>
	    <If> (= (GetChar file 0) (GetChar "/" 0))
		<Then>
		    <set> ^file (+ "xldocs" file) </set>
		</Then>
		<Else>
		    <set> ^file (+ "xldocs/" (Local dir) file) </set>
		</Else>
	    </If>
	    <Define> ^sts
	    	(Parse ([Shell pipe="on" cmdout="on"]
			"r64status " file))
	    </Define>
	    <Define> ^reso
		(ConvertUnit
			(GetElement (GetElement sym 'scan-resolution) 1)
			reso-unit)
	    </Define>
	    <Define>
		^scale
		(Eval (CurrentEnvironment)
			(GetElement (GetElement sym 'scale) 1))
	    </Define>
	    <List>
		(List 'mr
		    (List
			(List 0 0)
			(List 
				(/ (GetElement sts 1)
					(* reso scale))
				(/ (GetElement sts 2)
					(* reso scale)))))
		(List 'r64
			(List 'type (GetElement sts 0))
			(List 'level (GetElement sts 3))
			(List 'rect
				(List	(GetElement sts 1)
					(GetElement sts 2))))
		^base-unit
	    </List>
	    </Sequence></Then>
	    <Else> () </Else> </If>
	</Sequence>
    </Define>
    <Define Order="Applicative">
	^LusterMinrectCR ()
	<Argments> ^sym </Argments>
	<Sequence> ()
	    <Define> ^file (GetElement sym 'file "type" "cr")
	    </Define>
	    <Define> ^base-unit (GE sym 'base-unit) </Define>
	    <If> (= base-unit ())
	    <Then>
		<Define> ^unit m </Define>
		<Define> ^system base </Define>
		<Define> ^reso-unit dot/m </Define>
	        <Define> ^base-unit ^'<base-unit unit="m" system="base"/>
		</Define>
	    </Then>
	    <Else>
		<Define> ^unit (GetAttribute (car base-unit) "unit") </Define>
		<Define> ^reso-unit (+ "dot/" unit) </Define>
		<Define> ^system (GetAttribute (car base-unit) "system")
		</Define>
		<If> (= system ()) <Then>
		    <Define> ^system base </Define>
		</Then></If>
	    </Else>
	    </If>

	    <If> (Not (= file ()) )
	    <Then><Sequence> ()

	    <If> (= (ListLength file) 1)
		<Then> <set> ^file (Local from)</set> </Then>
		<Else> <set> ^file (GetElement file 1) </set> </Else>
	    </If>
	    <If> (= (GetChar file 0) (GetChar "/" 0))
		<Then>
		    <set> ^file (+ "xldocs" file) </set>
		</Then>
		<Else>
		    <set> ^file (+ "xldocs/" (Local dir) file) </set>
		</Else>
	    </If>
	    <Define> ^sts
	    	(Parse ([Shell pipe="on" cmdout="on"]
			"r64status " file))
	    </Define>
	    <Define> ^reso
		(ConvertUnit
			(GetElement (GetElement sym 'scan-resolution) 1)
			reso-unit)
	    </Define>
	    <Define>
		^scale
		(Eval (CurrentEnvironment)
			(GetElement (GetElement sym 'scale) 1))
	    </Define>
	    <List>
		(List 'mr
		    (List
			(List 0 0)
			(List 
				(/ (GetElement sts 1)
					(* reso scale))
				(/ (GetElement sts 2)
					(* reso scale)))))
		(List 'cr
			(List 'type (GetElement sts 0))
			(List 'level (GetElement sts 3))
			(List 'rect
				(List	(GetElement sts 1)
					(GetElement sts 2))))
		^base-unit
	    </List>
	    </Sequence></Then>
	    <Else> () </Else> </If>
	</Sequence>
    </Define>
    <Define Order="Normal"> ^meta ^sym <Sequence> ()
	<Define> ^some-element1
		<LusterMinrectR64> ^sym </LusterMinrectR64>
	</Define>
	<Define> ^some-element2
		<LusterMinrectCR> ^sym </LusterMinrectCR>
	</Define>
	<Switch>
	<Case> (And (= some-element1 ()) (= some-element2 ()))
		(PrintStd "R64 or CR format is required")
		(Exit)
	</Case>
	<Case> (= some-element1 ())
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> luster </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter> ^'file
			<GetElement> ^some-element2 0</GetElement>
		    </InsertAfter>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element2 1</GetElement>
		    </InsertAfter>
		    <Delete> ^'base-unit </Delete>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element2 2 </GetElement>
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
	</Case>
	<Case> (= some-element2 ())
(PrintStd some-element1)
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> luster </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter> ^'file
			<GetElement> ^some-element1 0</GetElement>
		    </InsertAfter>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element1 1</GetElement>
		    </InsertAfter>
		    <Delete> ^'base-unit </Delete>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element1 2 </GetElement>
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
	</Case>
	<Case> 1
(PrintStd some-element2)
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> luster </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter> ^'file
			<GetElement> ^some-element1 0</GetElement>
		    </InsertAfter>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element1 1</GetElement>
		    </InsertAfter>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element2 1</GetElement>
		    </InsertAfter>
		    <Delete> ^'base-unit </Delete>
		    <InsertAfter> ^'mr
			<GetElement> ^some-element1 2 </GetElement>
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
	</Case>
	</Switch>
    </Sequence></Define>
</Sequence>


<Define> ^vector
    <NewEnvironment><CurrentEnvironment/></NewEnvironment>
</Define>
<Sequence> ^vector
    <DefineDefault Order="Applicative">
        () ^sym ^sym
    </DefineDefault>
    <Define Order="Normal"> ^meta ^sym <Sequence> ()
	<Define> ^file-tag (GetElement sym 'file "type" "pdb")
	</Define>
	<If> (Not (= file-tag ()))
	<Then>	<Break><meta-pdb> ^sym </meta-pdb></Break>
	</Then>
	</If>
	<Define> ^file-tag (GetElement sym 'file "type" "xl")
	</Define>
	<If> (Not (= file-tag ()))
	<Then>	<Break><meta-xl> ^sym </meta-xl></Break>
	</Then>
	</If>
    </Sequence></Define>
    <Define Order="Applicative"> ^meta-xl ()
	<Arguments> ^sym </Arguments>
	<Sequence> ()
	<Define> ^minrect 
		<OldVectorMinrect> ^sym <Local> ^from </Local>
		</OldVectorMinrect>
	</Define>
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> vector </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter>
			^'file
			^minrect
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
    </Sequence></Define>
    <Define Order="Applicative"> ^meta-pdb ()
	<Arguments> ^sym </Arguments>
	<Sequence> ()
	<Define> ^file-tag (GetElement sym 'file "type" "pdb")
	</Define>
	<Define> ^file (GetElement file-tag 1)
	</Define>
	<Define> ^base-unit (GE sym 'base-unit)
	</Define>
	<If> (= (GetChar file 0) (GetChar "/" 0))
		<Then>
		    <set> ^file (+ "xldocs" file) </set>
		</Then>
		<Else>
		    <set> ^file (+ "xldocs/" (Local dir) file) </set>
		</Else>
	</If>
	<Define> ^ret
	    	(Parse ([Shell pipe="on" cmdout="on"]
			"pdbstatus " file))
	</Define>
	<Define> ^minrect
		(List 'mr (GetElement ret 4))
	</Define>
	<Define> ^level
		(List 'level (GetElement ret 1))
	</Define>
	<Define> ^reso
		(List 'resolution (GetElement ret 2))
	</Define>
	<Define> ^unit (GetElement ret 3)
	</Define>
	<If> (= base-unit ())
	<Then>
		<Define> ^base-unit (quote <base-unit unit="$unit"/>) </Define>
	</Then>
	<Else>
		<Define> ^base-unit
			(SetAttribute
				base-unit
				"unit" unit)
		</Define>
	</Else>
	</If>
        <Save option="xl" oflag="creat:trunc" mode="0755"
		encoding="Shift_JIS">
	    <Local> ^to </Local>
            <List>
                <quote><?xml version="1.0"
                    encoding="Shift_JIS" ?>
                </quote>
                <MakeElement>
                    ^sym
                    <InsertBefore>
                        ^'file
                        <quote>
                        <category> vector </category>
                        </quote>
                    </InsertBefore>
		    <InsertAfter>
			^'file
			^minrect
		    </InsertAfter>
		    <InsertAfter>
			^'mr
			^level
		    </InsertAfter>
		    <InsertAfter>
			^'mr
			^reso
		    </InsertAfter>
		    <Delete> ^'base-unit </Delete>
		    <InsertAfter>
			^'mr
			^base-unit
		    </InsertAfter>
                </MakeElement>
            </List>
        </Save>
    </Sequence></Define>
</Sequence>

