; edtsim
; A hack to give emacs use of an EDT-style keypad.  The keypad layout
; is VT100, however keypad functions are standard emacs.
; The VT100 control stuff is stolen from VT100.ml by Dave Oran on _BERGIL::
; [***] edtsim.ml, 20, Update by Dan L Pierson on _GALAXY::
; editno=20
;
(progn
  (declare-global
    &backing-up
    &killed-word
    &killed-character
    &killed-line)
  (setq &backing-up 0)
  (defun
    (vt100-arrow 
      c
      (setq c (get-tty-character))
      (if
	(= c 'A')	(previous-line)
	(= c 'B')	(next-line)
	(= c 'C')	(forward-character)
	(= c 'D')	(backward-character)
	)
      )
    (vt100-keypad 
      char save-mark
      (setq char (get-tty-character))
      (if 
	(= char 'p') (if &backing-up
			 (if (bolp)
			     (progn
			       (previous-line)
			       (beginning-of-line))
			     (beginning-of-line))
			 (progn
			   (next-line)
			   (beginning-of-line)))
	(= char 'q') (if &backing-up
			 (backward-word)
			 (forward-word))
	(= char 'r') (if &backing-up
			 (progn
			   (previous-line)
			   (end-of-line))
			 (if (eolp)
			     (progn
			       (next-line)
			       (end-of-line))
			     (end-of-line)))
	(= char 's') (if &backing-up
			 (backward-character)
			 (forward-character))
	(= char 't') (setq &backing-up 0)
	(= char 'u') (setq &backing-up 1)
	(= char 'v') (progn
		       (copy-region-to-buffer "PASTE")
		       (delete-to-killbuffer))
	(= char 'w') (if &backing-up
			 (previous-page)
			 (next-page))
	(= char 'x') (line-to-top-of-window)
	(= char 'y') (progn
		       (append-region-to-buffer "PASTE")
		       (delete-to-killbuffer))
	(= char 'm') (save-excursion
		       (if
			 (error-occured
			   (setq save-mark (mark)))
			 (setq save-mark 0))
		       (set-mark)
		       (if &backing-up
			   (progn
			     (backward-word)
			     (if
			       (! (< save-mark (dot)))
			       (if
				 (< save-mark (mark))
				 (setq save-mark 0)
				 (setq save-mark
				       (- save-mark 
					  (length (region-to-string)))))))
			   (progn
			     (forward-word)
			     (if
			       (! (< save-mark (mark)))
			       (if
				 (< save-mark (dot))
				 (setq save-mark 0)
				 (setq save-mark
				       (- save-mark
					  (length (region-to-string))))))))
		       (setq &killed-word (region-to-string))
		       (delete-to-killbuffer)
		       (goto-character save-mark)
		       (set-mark))
	(= char 'l') (progn
		       (setq &killed-character (following-char))
		       (delete-next-character))
	(= char 'n') (progn 
		       (set-mark)
		       (message "Mark set"))
	(= char 'M') (push-back-character 13)
	(= char 'P') (vt100-Gold-key)
	(= char 'Q') (apropos (get-tty-string "Keyword:"))
	(= char 'R') (if &backing-up
			 (search-reverse "")
			 (search-forward ""))
	(= char 'S') (save-excursion
		       (if
			 (error-occured
			   (setq save-mark (mark)))
			 (setq save-mark 0))
		       (set-mark)
		       (if &backing-up
			   (progn
			     (if (bolp) (progn
					  (previous-line)
					  (beginning-of-line))
				 (beginning-of-line))
			     (if
			       (! (< save-mark (dot)))
			       (if
				 (< save-mark (mark))
				 (setq save-mark 0)
				 (setq save-mark
				       (- save-mark 
					  (length (region-to-string)))))))
			   (progn
			     (next-line)
			     (beginning-of-line)
			     (if
			       (! (< save-mark (mark)))
			       (if
				 (< save-mark (dot))
				 (setq save-mark 0)
				 (setq save-mark
				       (- save-mark
					  (length (region-to-string))))))))
		       (setq &killed-line (region-to-string))
		       (delete-to-killbuffer)
		       (goto-character save-mark)
		       (set-mark)) ))
    (vt100-Gold-key
      char
      (setq char (get-tty-character))
      (if
	(= char 27) 
	(progn
	  (setq char (get-tty-character))
	  (if (= char 'O')
	      (progn
		(setq char (get-tty-character))
		(if
		  (= char 'p') (newline-and-backup)
		  (= char 'q') (case-region-invert)
		  (= char 'r') (kill-to-end-of-line)
		  (= char 's') (insert-string
				 (char-to-string 
				   (get-tty-string "Character code:")))
		  (= char 't') (end-of-file)
		  (= char 'u') (beginning-of-file)
		  (= char 'v') (progn 
				 (save-excursion
				   (temp-use-buffer "PASTE")
				   (beginning-of-file)
				   (set-mark)
				   (end-of-file)
				   (region-to-kill-ring))
				 (yank-from-kill-ring))
		  (= char 'w') (execute-extended-command
				 (get-tty-string "Command:"))
		  (= char 'x') (justify-paragraph)
		  (= char 'y') (query-replace-string
				 (get-tty-string "Old string:")
				 (get-tty-string "New string:"))
		  (= char 'm') (insert-string &killed-word)
		  (= char 'l') (insert-string 
				 (char-to-string &killed-character))
		  (= char 'n') (exchange-dot-and-mark)
		  (= char 'M') (query-replace-string
				 (get-tty-string "Old string:")
				 (get-tty-string "New string:"))
		  (= char 'P') (describe-key 
				 (progn
				   (message "Key:")
				   (quote-character 
				     (get-tty-character))))
		  (= char 'Q') (describe-command
				 (get-tty-string "Command:"))
		  (= char 'R') (if &backing-up
				   (search-reverse 
				     (get-tty-string "Search(R):"))
				   (search-forward
				     (get-tty-string "Search(F):")))
		  (= char 'S') (insert-string &killed-line) ))))))
    (vt100-black-screen		       ; Make VT100 screen black background
      (temp-use-buffer "Hack buffer")  ; get a hack buffer
      (erase-buffer)		       ; clean it out
      (insert-string "[?5l")	       ; get string to hack
      (write-named-file "tt:")         ; output it to the terminal
      (progn)
      )
    (vt100-white-screen		       ; Make VT100 screen white background
      (temp-use-buffer "Hack buffer")  ; get a hack buffer
      (erase-buffer)		       ; clean it out
      (insert-string "[?5h")         ; get string to hack
      (write-named-file "tt:")         ; output it to the terminal
      (progn)
      )
    (vt100-kp-off		       ; Turn off VT100 keypad
      (temp-use-buffer "Hack buffer")  ; get a hack buffer
      (erase-buffer)		       ; clean it out
      (insert-string ">")	       ; get string to hack
      (write-named-file "tt:")         ; output it to the terminal
      (progn)
      )
    (vt100-kp-on		       ; Turn on VT100 keypad
      (temp-use-buffer "Hack buffer")  ; get a hack buffer
      (erase-buffer)		       ; clean it out
      (insert-string "=")	       ; get string to hack
      (write-named-file "tt:")         ; output it to the terminal
      (progn)
      )
    (vt100-exit-emacs		       ; Clean up before exiting emacs
      (vt100-kp-off)
      (bind-to-key "exit-emacs" '')
      (push-back-character '')
      )
    )
  )
