;;; -*-emacs-lisp-*-
;;; ====================================================================
;;;  @Emacs-Lisp-file{
;;;     author          = "Nelson H. F. Beebe",
;;;     version         = "1.06",
;;;     date            = "27 December 2002",
;;;     time            = "15:04:34 MST",
;;;     filename        = "sun4.el",
;;;     address         = "Center for Scientific Computing
;;;                        University of Utah
;;;                        Department of Mathematics, 110 LCB
;;;                        155 S 1400 E RM 233
;;;                        Salt Lake City, UT 84112-0090
;;;                        USA",
;;;     telephone       = "+1 801 581 5254",
;;;     FAX             = "+1 801 581 4148",
;;;     checksum        = "25626 109 524 4974",
;;;     email           = "beebe@math.utah.edu (Internet), beebe@acm.org,
;;;                        beebe@computer.org, beebe@ieee.org",
;;;     codetable       = "ISO/ASCII",
;;;     keywords        = "emacs, function key",
;;;     supported       = "yes",
;;;     docstring       = "This file provides GNU Emacs functions for
;;;                        rebinding some of the function keys on the
;;;                        Sun 4 and 5 keyboard to run functions similar to
;;;                        the key names.  Emacs versions 18, 19, and
;;;                        later are supported.
;;;
;;;                        The checksum field above contains a CRC-16
;;;                        checksum as the first value, followed by the
;;;                        equivalent of the standard UNIX wc (word
;;;                        count) utility output of lines, words, and
;;;                        characters.  This is produced by Robert
;;;                        Solovay's checksum utility.",
;;;  }
;;; ====================================================================

(defun enable-Sun-4-keys ()
  "Enable the use of the Sun-4 function keys.  This unavoidably breaks
the standard Emacs command ESC <left bracket>; therefore, it is not
done by default, but only if you give this command."
  (interactive)
  (cond
   ((string-equal (substring emacs-version 0 2) "18")
    (defvar Sun-4-map nil
      "The Sun-4-map maps a few of the function keys on the Sun-4 keyboard.")
    (if (not Sun-4-map)
        (progn
          (setq Sun-4-map (lookup-key global-map "\e["))
          (if (not (keymapp Sun-4-map))
              (setq Sun-4-map (make-sparse-keymap)))))
    (global-set-key "\e[" Sun-4-map)
    (global-set-key "\e[-1z"  'help-for-help) ;HELP
    (global-set-key "\e[193z" 'call-last-kbd-macro) ;Again
    (global-set-key "\e[195z"  'undo)	;Undo
    (global-set-key "\e[197z" 'copy-region-as-kill) ;Copy
    (global-set-key "\e[199z" 'yank)	;Paste
    (global-set-key "\e[200z" 'search-forward) ;Find
    (global-set-key "\e[201z" 'kill-region) ;Cut
    (global-set-key "\e[214z" 'beginning-of-buffer) ;Home
    (global-set-key "\e[216z" 'scroll-down) ;PgUp
    (global-set-key "\e[220z" 'end-of-buffer) ;End
    (global-set-key "\e[222z" 'scroll-up) ;PgDn

    ;; Restore lost functionality of F1
    (global-set-key "\e[224z" 'backward-paragraph)

    ;; Disable top-row of function keys F2--F9 (F10 == Help, F11 and F12
    ;; are dead keys in Emacs) so hitting them accidentally doesn't move
    ;; point and insert garbage into the buffer
    (global-set-key "\e[225z" 'nil)
    (global-set-key "\e[226z" 'nil)
    (global-set-key "\e[227z" 'nil)
    (global-set-key "\e[228z" 'nil)
    (global-set-key "\e[229z" 'nil)
    (global-set-key "\e[230z" 'nil)
    (global-set-key "\e[231z" 'nil)
    (global-set-key "\e[232z" 'nil))
   ((string-lessp "18" (substring emacs-version 0 2)) ;version 19 or greater
    (global-set-key [help]  'help-for-help) ;HELP
    (global-set-key [f12] 'call-last-kbd-macro) ;Again
    (global-set-key [f14] 'undo)	;Undo
    (global-set-key [f16] 'copy-region-as-kill) ;Copy
    (global-set-key [f18] 'yank)	;Paste
    (global-set-key [f19] 'search-forward) ;Find
    (global-set-key [f20] 'kill-region) ;Cut
    (global-set-key [f27] 'beginning-of-buffer) ;Home
    (global-set-key [home] 'beginning-of-buffer) ;Home
    (global-set-key [f29] 'scroll-down) ;PgUp
    (global-set-key [f33] 'end-of-buffer) ;End
    (global-set-key [end] 'end-of-buffer) ;End
    (global-set-key [f35] 'scroll-up)	;PgDn
    (global-set-key [kp-enter] 'newline) ;Enter
    (global-set-key [del] 'backward-delete-char-untabify)	;Del

    ;; Disable top-row of function keys F2--F9 (F10 == Help, F11 and F12
    ;; are dead keys in Emacs) so hitting them accidentally doesn't move
    ;; point and insert garbage into the buffer
    (global-set-key [f1] 'nil)
    (global-set-key [f2] 'nil)
    (global-set-key [f3] 'nil)
    (global-set-key [f4] 'nil)
    (global-set-key [f5] 'nil)
    (global-set-key [f6] 'nil)
    (global-set-key [f7] 'nil)
    (global-set-key [f8] 'nil)
    (global-set-key [f9] 'nil)
    (global-set-key [f10] 'nil)
    (global-set-key [f11] 'nil))))
