home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / epoch / generic-bind.el < prev    next >
Encoding:
Text File  |  1991-03-30  |  8.3 KB  |  201 lines

  1. ; Date: Tue, 18 Sep 90 16:51:27 EDT
  2. ; From: Ken Laprade <laprade@trantor.harris-atd.com>
  3. ; Subject: Generic function key binding
  4. ; Here is a function I came up with to generically setup function key
  5. ; definitions.  There is a different definition of the function for epoch, X,
  6. ; or emacstool.  The same invocations of the function can be used in all
  7. ; cases.  Thus these bindings work for me no matter which emacs I run:
  8. ;    (define-function-key "F1" 'describe-function)
  9. ;    (define-function-key "M-F1" 'describe-variable)
  10. ;    (define-function-key "R5" "\C-V")
  11. ;    (define-function-key "M-R5" 'scroll-other-window)
  12. ;    (define-function-key "R6" "\M->")
  13. ;    (define-function-key "M-R6" 'end-of-buffer-other-window)
  14. ;    (define-function-key "S-R5" 'scroll-other-screen-up)
  15. ;    (define-function-key "S-R6" 'end-of-buffer-other-screen)
  16. ;
  17. ; These functions are written with a Sun3 keyboard in mind, but they could be
  18. ; easily adapted to anything.
  19. ;
  20. ; This is the epoch version:
  21. ;
  22. ; -- 
  23. ; Ken Laprade            INTERNET: laprade@trantor.harris-atd.com
  24. ; Harris Corporation         Usenet:  ...!uunet!x102a!trantor!laprade
  25. ; PO Box 37, MS 3A/1912        Voice: (407)727-4433
  26. ; Melbourne, FL 32902        FAX: (407)729-2537
  27. ;
  28. ; ----------
  29.  
  30. ;;; Support for generic function key binding (by Ken Laprade):
  31. ;;;
  32. (setq with-shift (epoch::mod-to-shiftmask 0))
  33. (setq with-control (epoch::mod-to-shiftmask 2))
  34. (setq with-meta (epoch::mod-to-shiftmask 3))
  35.  
  36. (defvar function-key-prefix-string "\C-X*"
  37.   "Prefix for function key definitions created by define-function-key.")
  38.  
  39. (defun define-function-key (key def &optional epoch-def)
  40.   "Globally define a generic function key for epoch.  KEY is a string naming
  41. the key (as known to X) with optional `M', `S', or `C' prefixes specifying
  42. meta, shift and control qualifiers.  Prefixes are separated from the key
  43. name by a hyphen.  DEF is the key definition.  It may be any valid
  44. ddefinition for define-key.  If it is a string, it will be directly bound
  45. to the key using rebind-key.  If the optional EPOCH-DEF is supplied, it
  46. is used rather than def."
  47.   (interactive "sFunction key name: \nxDefinition: ")
  48.   (if epoch-def (setq def epoch-def))
  49.   (let* ((i (string-match "-" key))
  50.      (keyname (let ((name (substring key (if i (1+ i) 0))))
  51.             (cond ((string-match "R8" name) "Up")
  52.               ((string-match "R10" name) "Left")
  53.               ((string-match "R12" name) "Right")
  54.               ((string-match "R14" name) "Down")
  55.               (t name))))
  56.      (meta (string-match "M.*-" key))
  57.      (shift (string-match "S.*-" key))
  58.      (control (string-match "C.*-" key))
  59.      (quals (logior (if meta with-meta 0)
  60.             (if shift with-shift 0)
  61.             (if control with-control 0))))
  62.     (if (stringp def)
  63.     (rebind-key keyname quals def)
  64.       ;; Make an intermediate binding for the definition.
  65.       (let* ((keytype (cond ((string-match "L\\([1-9]\\|10\\)$" key) "L")
  66.                 ((string-match "F\\([1-9]\\)$" key) "F")
  67.                 ((string-match "R\\([1-9]\\|1[0-5]\\)$" key) "R")
  68.                 (t keyname)))
  69.          (keynum (and (match-beginning 1)
  70.               (string-to-int (substring key (match-beginning 1) (match-end 1)))))
  71.          (binding (concat function-key-prefix-string
  72.                   (if meta "M") (if shift "S") (if control "C")
  73.                   keytype
  74.                   (if keynum (format "%02d" keynum)))))
  75.     (rebind-key keyname quals binding)
  76.     (global-set-key binding def)))))
  77. ----------
  78. This is the version for X emacs:
  79. ----------
  80. ;;; Support for generic function key binding (by Ken Laprade):
  81. ;;;
  82. (defun define-function-key (key def &optional ignored)
  83.   "Globally define a generic function key for Sun3 keyboards.  KEY is a
  84. string naming the key (L1-L10, F1-F9, R1-15) with optional `M', `S', or `C'
  85. prefixes specifying meta, control, and shift qualifiers.  Prefixes are
  86. separated from the key name by a hyphen.  Raw keys (as from the normal X
  87. version) do not provide the shift or control prefixes, but the emacstool
  88. version does.  Both maps are setup by this function.  DEF is the key
  89. definition.  It may be any valid definition for define-key."
  90.   (interactive "sFunction key name: \nxDefinition: ")
  91.   (let* ((i (string-match "-" key))
  92.      (keyname (let ((name (substring key (if i (1+ i) 0))))
  93.             (cond ((string-match "Up" name) "R8")
  94.               ((string-match "Left" name) "R10")
  95.               ((string-match "Right" name) "R12")
  96.               ((string-match "Down" name) "R14")
  97.               (t name))))
  98.      (meta (string-match "M.*-" key))
  99.      (shift (string-match "S.*-" key))
  100.      (control (string-match "C.*-" key))
  101.      (keybase (cond ((string-match "L\\([1-9]\\|10\\)$" keyname) 191)
  102.               ((string-match "F\\([1-9]\\)$" keyname) 223)
  103.               ((string-match "R\\([1-9]\\|1[0-5]\\)$" keyname) 207)
  104.               (t 0)))    ; Unknown key.
  105.      (keynum (if (match-beginning 1)
  106.               (string-to-int (substring keyname (match-beginning 1) (match-end 1)))
  107.            0)))
  108.     ;; The raw key version:
  109.     (or shift control (not sun-esc-bracket)
  110.     (global-set-key (format "%s[%03dz"
  111.                 (if meta "\M-\e" "\e")
  112.                 (+ keybase keynum))
  113.             def))))
  114.  
  115. (defvar sun-raw-map (make-sparse-keymap) "*Keymap for ESC-[ encoded keyboard")
  116. (defvar sun-meta-raw-map (make-sparse-keymap) "*Keymap for Meta-ESC-[ encoded keyboard")
  117.  
  118. (defvar sun-esc-bracket t
  119.   "*If non-nil, rebind ESC [ as prefix for Sun function keys.")
  120.  
  121. ;;; Hook in the function key maps:
  122. (if sun-esc-bracket
  123.     (progn
  124.       (define-key esc-map "[" sun-raw-map)        ; Install sun-raw-map
  125.       (global-set-key "\M-\e[" sun-meta-raw-map)    ; Install sun-meta-raw-map
  126.       (define-key esc-map "[A" 'previous-line )        ; R8
  127.       (define-key esc-map "[B" 'next-line)        ; R14
  128.       (define-key esc-map "[C" 'forward-char)        ; R12
  129.       (define-key esc-map "[D" 'backward-char)        ; R10
  130.       (define-key esc-map "[[" 'backward-paragraph)    ; the original esc-[
  131.       ))
  132. ----------
  133. And this is what I threw into our local copy of term/sun.el:
  134. ----------
  135. ;;; Support for generic function key binding (by Ken Laprade):
  136. ;;;
  137. (defun define-function-key (key def &optional ignored)
  138.   "Globally define a generic function key for Sun3 keyboards.  KEY is a
  139. string naming the key (L1-L10, F1-F9, R1-15) with optional `M', `S', or `C'
  140. prefixes specifying meta, control, and shift qualifiers.  Prefixes are
  141. separated from the key name by a hyphen.  Raw keys (as from the normal X
  142. version) do not provide the shift or control prefixes, but the emacstool
  143. version does.  Both maps are setup by this function.  DEF is the key
  144. definition.  It may be any valid definition for define-key."
  145.   (interactive "sFunction key name: \nxDefinition: ")
  146.   (let* ((i (string-match "-" key))
  147.      (keyname (let ((name (substring key (if i (1+ i) 0))))
  148.             (cond ((string-match "Up" name) "R8")
  149.               ((string-match "Left" name) "R10")
  150.               ((string-match "Right" name) "R12")
  151.               ((string-match "Down" name) "R14")
  152.               (t name))))
  153.      (meta (string-match "M.*-" key))
  154.      (shift (string-match "S.*-" key))
  155.      (control (string-match "C.*-" key))
  156.      (keybase (cond ((string-match "L\\([1-9]\\|10\\)$" keyname) 191)
  157.               ((string-match "F\\([1-9]\\)$" keyname) 223)
  158.               ((string-match "R\\([1-9]\\|1[0-5]\\)$" keyname) 207)
  159.               (t 0)))    ; Unknown key.
  160.      (keynum (if (match-beginning 1)
  161.               (string-to-int (substring keyname (match-beginning 1) (match-end 1)))
  162.            0)))
  163.     ;; The raw key version:
  164.     (or shift control (not sun-esc-bracket)
  165.     (global-set-key (format "%s[%03dz"
  166.                 (if meta "\M-\e" "\e")
  167.                 (+ keybase keynum))
  168.             def))
  169.     ;; The emacstool version:
  170.     (global-set-key (format "\C-X*%c%c"
  171.                 (+ keynum ?`)
  172.                 (+ (cond ((= keybase 191) ?l)
  173.                      ((= keybase 223) ?t)
  174.                      ((= keybase 207) ?r)
  175.                      ((= keybase 0) ??))
  176.                    (if control -64 0)
  177.                    (if shift -32 0)
  178.                    (if meta 128 0)))
  179.             def)))
  180.  
  181. (defvar sup-map (make-keymap) "Default keymap for SUP (meta-meta) commands.")
  182. (defvar sun-raw-map (make-sparse-keymap) "*Keymap for ESC-[ encoded keyboard")
  183. (defvar sun-meta-raw-map (make-sparse-keymap) "*Keymap for Meta-ESC-[ encoded keyboard")
  184. (defvar suntool-map (make-sparse-keymap) "*Keymap for Emacstool bindings.")
  185.  
  186. ;;; Hook in the function key maps:
  187. (define-key ctl-x-map "*" suntool-map)
  188. (if sun-esc-bracket
  189.     (progn
  190.       (define-key esc-map "[" sun-raw-map)        ; Install sun-raw-map
  191.       (define-key esc-map "\e" sup-map)            ; Install sup-map
  192.       (define-key sup-map "\e" sun-meta-raw-map)    ; Install sun-meta-raw-map
  193.       (define-key esc-map "[A" 'previous-line )        ; R8
  194.       (define-key esc-map "[B" 'next-line)        ; R14
  195.       (define-key esc-map "[C" 'forward-char)        ; R12
  196.       (define-key esc-map "[D" 'backward-char)        ; R10
  197.       (define-key esc-map "[[" 'backward-paragraph)    ; the original esc-[
  198.       ))
  199.