(provide 'fkeys)
(require 'toolbox "/afs/.athena.mit.edu/contrib/consult/lib/elisp/toolbox")

;;; Function Keys

(defvar F1 nil)
(defvar F2 nil)
(defvar F3 nil)
(defvar F4 nil)
(defvar F5 nil)
(defvar F6 nil)
(defvar F7 nil)
(defvar F8 nil)
(defvar F9 nil)
(defvar F10 nil)
(defvar F11 nil)		; RT, RIOS, SUN, SGI
(defvar F12 nil)
(defvar F13 nil)		; VAX only
(defvar F14 nil)		; VAX only
;;; NOTE: no F15-F16!
(defvar F17 nil)		; VAX only
(defvar F18 nil)		; VAX only
(defvar F19 nil)		; VAX only
(defvar F20 nil)		; VAX only

;;; Other Keys

(defvar INSERT nil)

(defvar HELP nil)		; VAX and SUN+E19
(defvar DO nil)			; VAX only
(defvar FIND nil)		; VAX and SUN+E19
(defvar REMOVE nil)		; VAX only
(defvar SELECT nil)		; VAX only
(defvar PREV-SCREEN nil)	; VAX only
(defvar NEXT-SCREEN nil)	; VAX only

(defvar PRINT-SCREEN nil)	; RT, RIOS, SUN, SGI
(defvar SCROLL-LOCK nil)	; RIOS and SUN
(defvar PAUSE nil)		; RIOS and SUN
(defvar HOME nil)		; RIOS, SUN, SGI
(defvar PAGE-UP nil)		; RIOS, SUN, SGI
(defvar PAGE-DOWN nil)		; RIOS, SUN, SGI
(defvar END nil)		; RIOS, SUN, SGI

(defvar STOP nil)		; SUN+E19 only
(defvar AGAIN nil)		; SUN+E19 only
(defvar PROPS nil)		; SUN+E19 only
(defvar UNDO nil)		; SUN+E19 only
(defvar COPY nil)		; SUN+E19 only
(defvar OPEN nil)		; SUN+E19 only
(defvar PASTE nil)		; SUN+E19 only
(defvar CUT nil)		; SUN+E19 only


(if (string-lessp emacs-version "19")
;; In emacs 18, function keys generate ESC + [ + other character(s).
;; Thus, we want to bind our function-key map to "[" in esc-map.
    (progn
      (defvar fnkey-map (make-sparse-keymap)
	"Keymap for special keys.")
      (define-key esc-map "[" fnkey-map) )
;; In emacs 19, function keys generate a single keycode.
;; This means that the map to modify is the global map.
  (defvar fnkey-map nil
    "Keymap for special keys.
With Emacs 19, this is the same as global-map.")
  (setq fnkey-map global-map))

(defun setup-vax-keys ()
  (setq F1 "11~")
  (setq F2 "12~")
  (setq F3 "13~")
  (setq F4 "14~")
  (setq F5 "15~")
  (setq F6 "17~")
  (setq F7 "18~")
  (setq F8 "19~")
  (setq F9 "20~")
  (setq F10 "21~")
  (setq F12 "24~")
  (setq F13 "25~")
  (setq F14 "26~")
  (setq F17 "31~")
  (setq F18 "32~")
  (setq F19 "33~")
  (setq F20 "34~")
  (setq HELP "28~")
  (setq DO "29~")
  (setq FIND "1~")
  (setq INSERT "2~")
  (setq REMOVE "3~")
  (setq SELECT "4~")
  (setq PREV-SCREEN "5~")
  (setq NEXT-SCREEN "6~"))

(defun setup-decmips-keys ()
  (interactive)
  (setup-vax-keys))

(defun setup-rt-keys ()
  (interactive)
  (setq F1 "11~")
  (setq F2 "12~")
  (setq F3 "13~")
  (setq F4 "14~")
  (setq F5 "15~")
  (setq F6 "17~")
  (setq F7 "18~")
  (setq F8 "19~")
  (setq F9 "20~")
  (setq F10 "21~")
  (setq F11 "23~")
  (setq F12 "24~")
  (setq PRINT-SCREEN "-1~")
  (setq INSERT "2~"))

(defun setup-rs6000-keys ()
  (interactive)
  (if window-system
      (setup-rs6000-x-keys)
      (setup-rs6000-tty-keys)))

(defun setup-rs6000-x-keys ()
  (interactive)
  (setq F1 "001q")
  (setq F2 "002q")
  (setq F3 "003q")
  (setq F4 "004q")
  (setq F5 "005q")
  (setq F6 "006q")
  (setq F7 "007q")
  (setq F8 "008q")
  (setq F9 "009q")
  (setq F10 "010q")
  (setq F11 "011q")
  (setq F12 "012q")
  (setq PRINT-SCREEN "209q")
  (setq SCROLL-LOCK "213q")
  (setq PAUSE "217q")
  (setq INSERT "139q")
  (setq HOME "H")
  (setq PAGE-UP "150q")
  (setq PAGE-DOWN "154q")
  (setq END "146q"))

(defun setup-rs6000-tty-keys ()
  (interactive)
  (setq F1 "11~")
  (setq F2 "12~")
  (setq F3 "13~")
  (setq F4 "14~")
  (setq F5 "15~")
  (setq F6 "17~")
  (setq F7 "18~")
  (setq F8 "19~")
  (setq F9 "20~")
  (setq F10 "21~")
  (setq F11 "23~")
  (setq F12 "24~")
  (setq INSERT "2~")
  (setq HOME "\^@")
  (setq PAGE-UP "5~")
  (setq PAGE-DOWN "6~")
  (setq END "\e"))

(defun setup-sun-stupid-keyboard-keys ()
  (interactive)
  (setq F1 "224z")
  (setq F2 "225z")
  (setq F3 "226z")
  (setq F4 "227z")
  (setq F5 "228z")
  (setq F6 "229z")
  (setq F7 "230z")
  (setq F8 "231z")
  (setq F9 "232z")
  (setq F10 "-1z")
  (setq F11 "192z")
  (setq F12 "193z")
  (setq HOME "214z")
  (setq PAGE-UP "216z")
  (setq PAGE-DOWN "222z")
  (setq END "220z")
  (setq PRINT-SCREEN "209z")
  (setq SCROLL-LOCK "210z")
  (setq PAUSE "208z"))

(defun setup-e18-keys ()
  (interactive)
  (let ((machine (machtype)))
    (cond ((or (equal machine "vax")
	       (equal machine "decmips"))
	   (setup-vax-keys))
	  ((equal machine "rt")
	   (setup-rt-keys))
	  ((equal machine "rsaix")
	   (setup-rs6000-keys))
	  ((equal machine "sun4")
	   (setup-sun-stupid-keyboard-keys))
	  ((equal machine "sgi")
	   (setup-sgi-keys))
	  (t (error "fkeys.el: Unknown machine type!  (%s)" machine)))))

; Added by William Chuang (wchuang@mit.edu)
;
; Note that Emacs19 (which the Athena SGIs use) references
; control and function keys within brackets by their name.
; The names are simple to check -- just type the key, and
; then check "Control-h l" to see the last 100 characters
; typed.

(defun setup-generic-e19-keys ()
  (setq F1 [f1])
  (setq F2 [f2])
  (setq F3 [f3])
  (setq F4 [f4])
  (setq F5 [f5])
  (setq F6 [f6])
  (setq F7 [f7])
  (setq F8 [f8])
  (setq F9 [f9])
  (setq F10 [f10])
  (setq INSERT [insert])
  (setq PAGE-UP [prior])
  (setq PAGE-DOWN [next])
  (setq HOME [home])
  (setq END [end]))

(defun setup-sgi-e19-keys ()
  (setq F11 [f11])
  (setq F12 [f12])
  (setq PRINT-SCREEN [print]))

(defun setup-sun-e19-keys ()
  (setq F11 [f35])
  (setq F12 [f36])
  (setq PRINT-SCREEN [f22])
  (setq PAUSE [f21])
  (setq SCROLL-LOCK [f23])
  (setq HELP [help])
  (setq STOP [f11])
  (setq AGAIN [f12])
  (setq PROPS [f13])
  (setq UNDO [f14])
  (setq COPY [f16])
  (setq OPEN [f17])
  (setq PASTE [f18])
  (setq FIND [f19])
  (setq CUT [f20]))

(defun setup-e19-keys ()
  (interactive)
  ;; generic setup
  (setup-generic-e19-keys)
  ;; machine-dependent setup
  (let ((machine (machtype)))
    (cond
     ((equal machine "sun4")
      (setup-sun-e19-keys))
     ((equal machine "sgi")
      (setup-sgi-e19-keys)) )) )

(defun setup-keys ()
  (interactive)
  (if (string-lessp emacs-version "19")
      (setup-e18-keys)
    (setup-e19-keys)) )

(setup-keys)
