; HACK TO OUTPUT MACSYMA EXPRESSIONS BY RANDOM TYPEOUT

(DECLARE (SPECIAL RAN-COUNT OLDROW OLDCOL TYO-COUNT RAN-FLAG
		  BYTE-PTR OUTARRAY $RANDISP RANARY-SIZE
		  TTYHEIGHT BKPTHT BKPTDP OUT-SIZE RUBOUT-TTY)
	 (FIXNUM (IDPB FIXNUM FIXNUM NOTYPE) (TYO** FIXNUM)
		 (TYO* FIXNUM) CH RAN-COUNT TYO-COUNT
		 OLDROW OLDCOL BYTE-PTR RANARY-SIZE BKPTHT BKPTDP
		 TTYHEIGHT OUT-SIZE))

(ARRAY RANARY FIXNUM 504. 3)
(SETQ $RANDISP nil RANARY-SIZE 500. RAN-COUNT 0 RAN-FLAG NIL)

($NOFIX '$RANDISP)  ; do it this way so it can be killed
(DEFUN $RANDISP NIL (SETQ $RANDISP (NOT $RANDISP)))

; PATCHES TO DISPLA FUNCTIONS

(DEFUN PUTOUT (RESULT W) (DECLARE (FIXNUM W H CH))
  (PROG (H)
    (TTYINTSOFF)
    (SETQ OLDROW (CAR (CURSORPOS)) OLDCOL 0 H (+ OLDROW BKPTHT BKPTDP))
    (CURSORPOS* OLDROW 0)
    (DO ((CH (COND (RUBOUT-TTY 135.)(T 138.)))) ((= H OLDROW))
	(TYO* CH) (SETQ OLDROW (1+ OLDROW)))
    (SETQ RAN-FLAG $RANDISP RAN-COUNT 0)			;;;
    (DRAW RESULT (- OLDROW BKPTDP 1) W)
    (COND ($RANDISP (SETQ RAN-FLAG NIL OLDROW BKPTHT OLDCOL 0) ;;;
		    (FORCEOUT*))) ;;;
    (CURSORPOS* (SETQ H (MIN (- TTYHEIGHT 2) H)) 0)
    (FORCE-OUTPUT DISPLAY-FILE)
    (SETCURSORPOS H 0)
    (LISTEN)
    (TTYINTSON)))

(DEFUN CURSORPOS* (ROW COL)
  (DECLARE (FIXNUM ROW COL))
  (COND ((NULL RAN-FLAG)
	 (+TYO 143. DISPLAY-FILE)
	 (+TYO ROW DISPLAY-FILE)
	 (+TYO COL DISPLAY-FILE)))
  (SETQ OLDROW ROW OLDCOL COL))

(DEFUN TYO* (BYTE)
  (DECLARE (FIXNUM BYTE))
  (COND (RAN-FLAG (TYO** BYTE))		;;
	(T 				;;
	 (COND ((= 8. BYTE) (SETQ OLDCOL (1- OLDCOL)))
	       ((> 128. BYTE) (SETQ OLDCOL (1+ OLDCOL))))
	 (+TYO BYTE DISPLAY-FILE))))

; ROUTINES FOR RANDOM DISPLAY

(DEFUN TYO** (BYTE)
  (DECLARE (FIXNUM BYTE))
  (STORE (RANARY RAN-COUNT 0) OLDROW)
  (STORE (RANARY RAN-COUNT 1) OLDCOL)
  (STORE (RANARY RAN-COUNT 2) BYTE)
  (SETQ RAN-COUNT (1+ RAN-COUNT))
  (COND ((> RAN-COUNT RANARY-SIZE)
	 (SETQ RANARY-SIZE (+ RANARY-SIZE 100.))
	 (*REARRAY 'RANARY 'FIXNUM RANARY-SIZE 3)))
  (COND ((= 8. BYTE) (SETQ OLDCOL (1- OLDCOL)))
	((= 32. BYTE) (SETQ OLDCOL (1+ OLDCOL) RAN-COUNT (1- RAN-COUNT)))
	((> 128. BYTE) (SETQ OLDCOL (1+ OLDCOL))))
    BYTE)

(DEFUN FORCEOUT* ()
       (DO ((I (RANDOM RAN-COUNT) (RANDOM RAN-COUNT))
	    (COUNT 0))
	   ((= COUNT RAN-COUNT))
	   (COND ((< (RANARY I 0) 0))
		 (T (CURSORPOS* (RANARY I 0) (RANARY I 1))
		    (+TYO (RANARY I 2) DISPLAY-FILE)
		    (STORE (RANARY I 0) -1)
		    (SETQ COUNT (1+ COUNT))))))

