home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / functions / setup-term.el < prev    next >
Encoding:
Text File  |  1990-07-22  |  11.9 KB  |  301 lines

  1. ;From: jbw@USWEST.COM (Joe Wells)
  2. ;Newsgroups: gnu.emacs.bug
  3. ;Subject: binding keys consistently under different interfaces
  4. ;Message-ID: <8907191758.AA01264@ketchum.uswest.com>
  5. ;Date: 19 Jul 89 17:58:17 GMT
  6. ;Distribution: gnu
  7. ;Organization: GNUs Not Usenet
  8. ;Lines: 291
  9. ;
  10. ;jsol@BU-IT.BU.EDU writes:
  11. ; > One of our users has extensively configured EMACS to work with
  12. ; > suntools using the sun-raw-map feature. He wants to make this
  13. ; > work with X11R3. Does anyone have a non-painful way for this to
  14. ; > happen? Specifically he has bound all of his keyboard characters
  15. ; > (the ones on the top of the keyboard, labelled F1 through Fsomething)
  16. ; > to useful functions within emacs and wants to keep the "look and feel"
  17. ; > of the emacs configuration he used to However.
  18. ;
  19. ;There may be a simple solution.  If your user is running Emacs in an xterm
  20. ;window, there is a option to xterm that tells it to use the same character
  21. ;sequences for function keys that are used on the Sun console or under
  22. ;suntools in a tty subwindow.  Another alternative is to run Emacs as a
  23. ;separate X client, in which case I believe the function key character
  24. ;sequences also follow the Sun standard.
  25. ;
  26. ;However, in case neither of those solutions work, I am including a
  27. ;function that I use, along with a demonstration of its use.  I have
  28. ;written a function that will bind the Sun 3 function keys consistently in
  29. ;all of the environments with which I am familiar.  You'll will have to
  30. ;look at it and see what you can use.  I hope this helps.
  31. ;
  32. ;--
  33. ;Joe Wells <jbw@uswest.com>
  34. ;jbw%ketchum.uswest.com@boulder.colorado.edu
  35. ;----------------------------------------------------------------------
  36. ;; Last changed: Wed Jul 19 11:49:22 1989 by jbw ( Joe Wells #360 x2392) on ketchum
  37. ;; Sun specific stuff
  38.  
  39. ;; Xterm:
  40. ;; L1-4: "\e[23~" through "\e[26~"
  41. ;; L5-6: "\e[28~" through "\e[29~"
  42. ;; L7-10: "\e[31~" through "\e[34~"
  43. ;; F1-5: "\e[11~" through "\e[15~"
  44. ;; F6-9: "\e[17~" through "\e[20~"
  45. ;; Setup Sun 3 function key bindings for most environments
  46. ;; normal S C M C-S M-S C-M C-M-S
  47. (defun define-sun3-function-key (key &rest binding-list)
  48.   "Bind Sun 3 function key KEY to BINDING.  Under the Emacstool
  49. environment, additional BINDINGS may be specified after the first.  In
  50. that case the first BINDING affects the unshifted function key, and the
  51. subsequent bindings affect these modifications of the key, in this order:
  52. shift-key, control-key, meta-key, control-shift-key, meta-shift-key,
  53. control-meta-key, control-meta-shift-key.  If all eight bindings are not
  54. specified, the last binding specified is used for the rest.  Under other
  55. environments, all BINDINGS after the first are ignored.  If no binding is
  56. specified, the function undefined is used."
  57.   (if (symbolp key)
  58.       (setq key (symbol-name key))
  59.     (if (not (stringp key))
  60.     (error "Key must be a string or symbol")))
  61.   (or binding-list
  62.       (setq binding-list '(undefined)))
  63.   (let* ((key-group (aref key 0))
  64.      (key-number (car (read-from-string key 1)))
  65.      (emacstool-number-letter (+ key-number ?a -1))
  66.      (emacstool-group-letter (cond ((memq key-group '(?L ?l)) ?l)
  67.                        ((memq key-group '(?R ?r)) ?r)
  68.                        ((memq key-group '(?F ?f ?T ?t)) ?t)))
  69.      (raw-group-number (cdr (assq emacstool-group-letter
  70.                       '((?l . 191) (?r . 207) (?t . 223)))))
  71.      (xterm-group-number (cdr (assq emacstool-group-letter
  72.                     '((?l . 22) (?t . 10) (?r . nil)))))
  73.      (xterm-number-offset
  74.       (cond ((eq emacstool-group-letter ?l)
  75.          (cond ((>= key-number 7) 2)
  76.                ((>= key-number 5) 1)
  77.                (t 0)))
  78.         ((eq emacstool-group-letter ?t)
  79.          (if (>= key-number 6) 1 0))
  80.         (t nil)))
  81.      (emacstool-shift-values '(0 -32 -64 128 -96 96 64 32))
  82.      (raw-key-string
  83.       (concat (prin1-to-string (+ raw-group-number key-number)) "z"))
  84.      (xterm-key-string
  85.       (if xterm-group-number
  86.           (concat (prin1-to-string
  87.                (+ xterm-group-number key-number xterm-number-offset))
  88.               "~")))
  89.      (arrow-key-string
  90.       (cdr (assoc key '(("R8" . "A") ("R10" . "D")
  91.                 ("R12" . "C") ("R14" . "B")))))
  92.      key-string)
  93.     ;; Raw Sun key binding
  94.     (define-key sun-raw-map raw-key-string (car binding-list))
  95.     ;; Xterm key binding
  96.     (if xterm-key-string
  97.     (define-key sun-raw-map xterm-key-string (car binding-list)))
  98.     ;; Arrow key binding
  99.     (if arrow-key-string
  100.     (define-key sun-raw-map arrow-key-string (car binding-list)))
  101.     ;; Emacstool key bindings
  102.     (while (consp emacstool-shift-values)
  103.       (setq key-string (concat (list emacstool-number-letter)
  104.                    (list (+ emacstool-group-letter
  105.                     (car emacstool-shift-values)))))
  106.       (define-key suntool-map key-string (or (car binding-list) 'undefined))
  107.       (if (cdr binding-list)
  108.       (setq binding-list (cdr binding-list)))
  109.       (setq emacstool-shift-values (cdr emacstool-shift-values)))))
  110.  
  111. ;; Setup necessary keymaps, variables, and functions
  112. (or (and (boundp 'sun-raw-map) sun-raw-map)
  113.     (setq sun-raw-map (make-sparse-keymap)))
  114. (or (and (boundp 'suntool-map) suntool-map)
  115.     (setq suntool-map (make-sparse-keymap)))
  116. (or (not sun-esc-bracket)
  117.     (eq (lookup-key esc-map "[") sun-raw-map)
  118.     (eq (lookup-key esc-map "[") 'sun-raw-map)
  119.     (define-key esc-map "[" 'sun-raw-map))
  120. (or (fboundp 'undefined)
  121.     (fset 'undefined
  122.       '(lambda nil
  123.          (interactive)
  124.          (byte-code "\300\210\301 \210\302\303!\207"
  125.             [nil beep message "Undefined key sequence."] 3))))
  126. (setq meta-flag t)
  127.  
  128. (provide 'sun-stuff)
  129. ;----------------------------------------------------------------------
  130. ;; This is in my .emacs file.
  131. ;; You will have to delete references to some of the stuff that I use to
  132. ;; make all of this work.
  133.  
  134. (autoload 'define-sun3-function-key "sun-stuff")
  135. (setq term-setup-hook 'setup-term)
  136.  
  137. (defun term-name-prefix (name prefix)
  138.   "Returns true if NAME begins with PREFIX.  Both must be strings."
  139.   (and (>= (length name) (length prefix))
  140.        (string-equal (substring name 0 (length prefix)) prefix)))
  141.  
  142. (defun setup-term ()
  143.   "Setup keymaps based on the terminal type.  This should be run after
  144. the file in lisp/term/<type>.el has been run.  It can be run as the
  145. value of term-setup-hook."
  146.   (interactive)
  147.   (cond ((term-name-prefix term "vt2")
  148.      (enable-arrow-keys)
  149.      (setup-arrow-keys)
  150.      (setup-terminal-keymap CSI-map
  151.        '(("A" . previous-line)      ; cursor up (normal)
  152.          ("B" . next-line)          ; cursor down (normal)
  153.          ("C" . forward-char)       ; cursor forward (normal)
  154.          ("D" . backward-char)      ; cursor backward (normal)
  155.          ;; VT220 keys
  156.          ("28~" . help-for-help)    ; "Help" key
  157.          ("29~" . execute-extended-command) ; "Do" key
  158.          ("1~" . re-search-forward) ; "Find" key
  159.          ("2~" . yank)              ; "Insert Here" key
  160.          ("3~" . kill-region)       ; "Remove" key
  161.          ("4~" . set-mark-command)  ; "Select" key
  162.          ("5~" . scroll-down-in-place) ; "Prev Screen" key
  163.          ("6~" . scroll-up-in-place) ; "Next Screen" key
  164.          ("17~" . delete-other-windows) ; F6
  165.          ("18~" . delete-window)    ; F7
  166.          ("19~" . enlarge-window)   ; F8
  167.          ("20~" . split-window-vertically) ; F9
  168.          ("21~" . other-window)     ; F10
  169.          ("23~" . ESC-prefix)       ; F11 (ESC)
  170.          ("24~" . bury-buffer)      ; F12 (BS)
  171.          ("25~" . buffer-menu)      ; F13 (LF)
  172.          ("26~" . switch-to-buffer) ; F14
  173.          ("31~" . delete-indentation) ; F17
  174.          ("32~" . delete-horizontal-space) ; F18
  175.          ("33~" . loop-word)        ; F19
  176.          ("34~" . loop-grab)))      ; F20
  177.      ;; VT100 and VT220 keypad keys
  178.      (setup-terminal-keymap SS3-map
  179.        '(("A" . previous-line)      ; cursor up (application)
  180.          ("B" . next-line)          ; cursor down (application)
  181.          ("C" . forward-char)       ; cursor fwd (application)
  182.          ("D" . backward-char)      ; cursor bwd (application)
  183.          ("p" . overwrite-mode)     ; numpad 0 (Ins)
  184.          ("q" . end-of-line)        ; numpad 1 (End)
  185.          ("r" . next-line)          ; numpad 2 (Crsr Dn)
  186.          ("s" . scroll-up-in-place) ; numpad 3 (Pg Dn)
  187.          ("t" . backward-char)      ; numpad 4 (Crsr Lf)
  188.          ("u" . recenter)           ; numpad 5 (nothing)
  189.          ("v" . forward-char)       ; numpad 6 (Crsr Rt)
  190.          ("w" . beginning-of-line)  ; numpad 7 (Home)
  191.          ("x" . previous-line)      ; numpad 8 (Crsr Up)
  192.          ("y" . scroll-down-in-place) ; numpad 9 (Pg Up)
  193.          ("m" . undefined)          ; numpad -
  194.          ("l" . undefined)          ; numpad ,
  195.          ("M" . newline)            ; numpad Enter
  196.          ("n" . delete-char)        ; numpad . (Del)
  197.          ("P" . undefined)          ; PF1
  198.          ("Q" . find-tag-at-point)  ; PF2
  199.          ("R" . describe-function-called-at-point) ; PF3
  200.          ("S" . describe-variable-at-point)))) ; PF4 (Sys Request)
  201.     ((term-name-prefix term "vt1")  ; working at home?
  202.      (setq search-slow-speed 9600)) ; ugly hack to fix emacs bug
  203.     ((term-name-prefix term "sun")
  204.      (mapcar (function
  205.           (lambda (x)
  206.             (apply 'define-sun3-function-key x)))
  207.          '((R1 find-tag-at-point)
  208.            (R2 describe-function-called-at-point)
  209.            (R3 describe-variable-at-point)
  210.            (R4 set-mark-command exchange-point-and-mark)
  211.            (R5 yank yank-pop)
  212.            (R6 kill-region copy-region-as-kill)
  213.            (R7 beginning-of-line beginning-of-buffer)
  214.            (R8 previous-line)
  215.            (R9 scroll-down-in-place backward-page)
  216.            (R10 backward-char)
  217.            (R11 recenter)
  218.            (R12 forward-char)
  219.            (R13 end-of-line end-of-buffer)
  220.            (R14 next-line)
  221.            (R15 scroll-up-in-place forward-page)
  222.            (L1)
  223.            (L2 redraw-display)    ; Again
  224.            ;; M-S-Again: rerun-prev-command
  225.            ;; M-Again: prev-complex-command
  226.            (L3 buffer-menu)    ; Props
  227.            (L4 undo)        ; Undo
  228.            (L5)                    ; Expose
  229.            (L6 sun-select-region) ; Put
  230.            ;; C-Put: copy-region-as-kill
  231.            (L7)                    ; Open
  232.            (L8 sun-yank-selection) ; Get
  233.            ;; C-Get: yank
  234.            (L9 delete-horizontal-space) ; Find
  235.            ;; Find: research-forward (sun.el)
  236.            ;; C-Find: re-search-forward
  237.            ;; M-Find: research-backward (sun.el)
  238.            ;; C-M-Find: re-search-backward
  239.            (L10 delete-indentation) ; Delete
  240.            ;; Delete: kill-region-and-unmark
  241.            ;; S-Delete: yank
  242.            ;; M-Delete: exchange-point-and-mark
  243.            ;; C-Delete: interactive-pop-mark
  244.            (F1 re-search-forward)
  245.            (F2 switch-to-buffer)
  246.            (F3 bury-buffer)
  247.            ;; F3: scroll-down-in-place (sun.el)
  248.            ;; S-F3: scroll-down
  249.            (F4 split-window-vertically)
  250.            ;; F4: scroll-up-in-place (sun.el)
  251.            ;; S-F4: scroll-up
  252.            (F5 other-window)
  253.            (F6 shrink-window shrink-window-horizontally)
  254.            (F7 enlarge-window enlarge-window-horizontally)
  255.            (F8 delete-window)
  256.            (F9 delete-other-windows)))
  257.      (define-key ctl-x-map "\C-z" 'hide-emacstool)
  258.      (define-key ctl-x-map "z" 'hide-emacstool)
  259.      (define-key esc-map "z" 'hide-emacstool)
  260.      (setup-arrow-keys)
  261.      ;; Fix name conflict with lisp/term/sun.el
  262.      (fmakunbound 'scroll-down-in-place)
  263.      (fmakunbound 'scroll-up-in-place)
  264.      (autoload 'scroll-down-in-place "scroll-fix" nil t)
  265.      (autoload 'scroll-up-in-place "scroll-fix" nil t))
  266.     ((term-name-prefix term "xterm")
  267.      (setq SS3-map (make-keymap))
  268.      (setup-terminal-keymap SS3-map
  269.        '(("A" . previous-line)      ; up arrow
  270.          ("B" . next-line)          ; down-arrow
  271.          ("C" . forward-char)       ; right-arrow
  272.          ("D" . backward-char)      ; left-arrow
  273.          ("Q" . tab-to-tab-stop)    ; L8
  274.          ("R" . save-some-buffers)  ; L9
  275.          ("S" . other-window)))     ; L10
  276.      (define-key esc-map "O" SS3-map))
  277.     ((or (term-name-prefix term "h19")
  278.          (term-name-prefix term "z19"))
  279.      (setq SS3-map (make-keymap))
  280.      (setup-terminal-keymap SS3-map
  281.        '(("x" . previous-line)
  282.          ("r" . next-line)
  283.          ("v" . forward-char)
  284.          ("t" . backward-char)
  285.          ("p" . scroll-down)
  286.          ("M" . scroll-up)
  287.          ("n" . set-mark-command)
  288.          ("w" . beginning-of-buffer)
  289.          ("y" . end-of-buffer)
  290.          ("u" . other-window)))
  291.      (define-key esc-map "O" SS3-map))))
  292.  
  293. (defun undefined ()
  294.   "If a key is bound to this function, that means it has been
  295. deliberately undefined.  This function rings the bell and
  296. prints a message saying the key sequence is undefined."
  297.   (interactive)
  298.   (beep)
  299.   ;; and give us a message, since we always have the bell turned off
  300.   (message "Undefined key sequence."))
  301.