%%HP: T(3)A(D)F(.);

@
@ Written by Eric B. Davis
@ EXTENSIVELY Speed and Space Optimized by:
@	Eddie Wilborne
@	Pete Wilson
@
@ Enter the month and year on the stack
@ and this program will generate a calendar.
@ For July, 1990 requires 0.6721 seconds (7 times faster, 1/3 size)
@
@ [User Friendly Feature added by J.Horn: automatically uses current month
@   if none is given.  -jkh-]

\<<
  DEPTH
  IF 2 <
  THEN DATE IP DATE 100 * FP 10000 *
  END 1E6 / .01 + \-> mon yrb

  \<< 
    yrb mon + DUP 

    IF mon 12 ==              @ are we doing december?
    THEN yrb 1.000001 +       @ compute jan of next year
    ELSE yrb mon 1 + +        @ compute first day of next month
    END

    DDAYS 3 *

    1.0518 ROT DDAYS 7 MOD NEG 7 + 3 *

    \-> ndays n
    	@ ndays is # days in month times 3
	@ n is # days in first week times 3

    \<<

    @ The following string ends in a character 10, i.e. a line-feed
        "Su Mo Tu We Th Fr Sa\010"

    	"                  " 1 21 n - SUB

    	" 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31             "

	0 84 FOR o
		n o + 
    @ The following string contains a character 10, i.e. a line-feed
    		"\010"
		REPL
	21 STEP

	1 ndays SUB

	+	@ add padding for first week
	+	@ add day of week header line

	CLLCD
	1 DISP
	7 FREEZE
   \>>

  \>>

\>>
