; Run Standard ML in a window
; FDC, 27 Jan 1986

(setq-default sml-active 0)

(defun
    (sml-newline old-line	; feed current line to sml
	(end-of-line)
	(if (! (eobp))		; if it's not the last line of the buffer,
	    (progn		; copy it to the bottom
		(beginning-of-line)
		(set-mark)
		(end-of-line)
		(setq old-line (region-to-string))
		(end-of-file)
		(insert-string
		    (substr old-line (current-column)
			(- 1 (current-column))))
	    ))
	(newline)		; put in a newline
	(exit-emacs)		; resume sml
    )
    (quit-sml
	(if (= sml-active 1)
	    (progn
		(pop-to-buffer "SML")
		(end-of-file)
		(insert-character 255)	; hack to get EOF to program
		(exit-emacs)
		(setq sml-active 0))
	)
	(novalue)
    )
    (sml			; execute sml in a window
	(if (error-occured (getenv "ml"))
	    (progn
		(message "Sorry, you should have said 'setup ml' %before entering emacs")
		(novalue))
	    (progn
		(pop-to-buffer "SML")
		(if (= sml-active 1)
		    (progn
			(end-of-file)
			(message "Resuming SML session")
			(sit-for 5))
		    (progn
			(setq sml-active 1)
			(setq needs-checkpointing 0)
			(erase-buffer)
			(message "Loading SML .....")
			(sit-for 0)
			(local-bind-to-key "sml-newline" '^M')
			(start-program "ml" "SML")
			(setq sml-active 0))
		))
	)
	(novalue)
    )
)
