[comp.emacs] Arrow keys in xterm

jes@mth.msu.edu (Jesse) (09/13/90)

How do I make xterm arrow keys work inside EMACS?
My EMACS doesn't link with X-Window and machines are Suns.
Thanks in advance.

Jesse

Mike.Williams@comp.vuw.ac.nz (Mike Williams) (09/13/90)

+-- In article <1990Sep12.181012.8563@msuinfo.cl.msu.edu> 
    jes@mth.msu.edu (Jesse) writes:
| 
| How do I make xterm arrow keys work inside EMACS?

    My solution is to redefine the xterm key mappings to send the normal
  sequences for {forward,backward}-char, {next,previous}-line, and so on.
  This also works brilliantly for tcsh, and for programs that use the GNU
  Readline interface (like bash).  It will probably mess up any interactive
  program that relies on the standard ANSI terminal arrow-key codes.

--- cut -------------------------------------------------------------------
XTerm*VT100.Translations: #override \
        <Key>Up:                string("^P") \n\
        <Key>Down:              string("^N") \n\
        <Key>Left:              string("^B") \n\
        <Key>Right:             string("^F") \n\
       m<Key>Up:                string("^[p") \n\
       m<Key>Down:              string("^[n") \n\
       m<Key>Left:              string("^[b") \n\
       m<Key>Right:             string("^[f") \n\
        <Key>F1:                string("pd") string(0x0d) \n\
        <Key>Prior:             scroll-back(1,halfpage) \n\
        <Key>Next:              scroll-forw(1,halfpage) \n\
        <Key>Home:              scroll-back(1,page) \n\
        <Key>End:               scroll-forw(1,page) \n\
        Shift<Key>Up:           scroll-back(1,line) \n\
        Shift<Key>Down:         scroll-forw(1,line) \n\
        <Key>Break:             hard-reset() \n\
        <Key>KP_Equal:          set-scrollbar(toggle) \n\
        <Key>KP_Divide:         set-scroll-on-tty-output(toggle) \n\
        <Key>KP_Multiply:       soft-reset() \n\
        <Key>KP_Subtract:       scroll-back(1,halfpage) \n\
        <Key>KP_Add:            scroll-forw(1,halfpage) \n\
        <Key>Insert:            insert-selection(CUT_BUFFER0,PRIMARY) \n\
        <Btn2Up>:               insert-selection(CUT_BUFFER0,PRIMARY) \n\
        <Key>F2:                string("d -lg ") \n\
        <Key>F3:                string("dirs") string(0x0d) \n\
        <Key>F10:               string("top 20 | cat") string(0x0d) \n\
        <Key>F12:               string("| less") string(0x0d) \n
--- end -------------------------------------------------------------------

    This needs to be loaded using xrdb(1).  If you have a ~/.xrdb or
  ~/.Xdefaults file, that's a good place for it.

    Note that the character sequence "^P" denotes Control-P (as usual), and
  "^[" denotes Escape.  You'll have to replace this with the real
  characters, as xterm doesn't understand "^"-quoting.  Do this using
  quoted-insert (C-q).

--
      /-------------------- byrd@comp.vuw.ac.nz --------------------\ 
      | Mike Williams, Victoria University of Wellington, Aotearoa. |
      \------------ What's another word for Thesaurus? -------------/

fraser@ukfca1 (Fraser McCrossan) (09/26/90)

jes@mth.msu.edu (Jesse) asks:

> How do I make xterm arrow keys work inside EMACS?
> My EMACS doesn't link with X-Window and machines are Suns.

I have been using some Emacs key bindings for arrow keys and other Sun function
keys for some time now. There are few variants on the general method, but here
is the one I use [I beg to submit that this is a better solution than
redefining the xterm keys themselves, as a previous poster suggested]:

First, run Emacs in an xterm invoked with the `-sf' option; this causes the
function keys to send actual Sun function key codes, rather than standard X
codes. Then, use some key definitions in your ~/.emacs file for the appropriate
keys. Sun functions keys generate an escape character followed by two or more
extra characters. Normally when `define-key' is called it interprets all
characters but the last as prefix keys, and generates keyboard tables as
appropriate, but it turns out that the common initial sequences ESC O and ESC [
are already bound to functions. These must be unbound before function keys can
be used. You will also find that on the Sun type 4 keyboard, the keys L1 (Stop)
and F11 generate the same escape sequence, as do L2 (Again) and F12. Annoying,
this.

Key definitions (for global definitions at least) are entered as (for example):

(define-key global map "<key sequence>" 'function-to-be-called)

The easiest way I have found to set these up is to use Emacs to edit the
~/.emacs file, and type something like:

(define-key global-map " C-q [FUNCTION KEY] " 'function)

A set of (example) definitions is given below, with escape characters shown as
\E [so stupid mailers don't get confused]:

;; Define Sun custom keys
;; unset key sequence ESC-O A, so that cursor keys work
(global-unset-key "\EO")
;; unset key sequence ESC-[ (normally forward-paragraph)
;; so that Sun keyboard function keys do useful things
(global-unset-key "\E[")
;; right (cursor) keypad functions
;; R8 up
(define-key global-map "\EOA" 'previous-line)
;; R14 down
(define-key global-map "\EOB" 'next-line)
;; R10 left
(define-key global-map "\EOD" 'backward-char)
;; R12 right
(define-key global-map "\EOC" 'forward-char)
;; R9 "PgUp" (as M-v)
(define-key global-map "\E[216z" 'scroll-down)
;; R15 "PgDn" (as C-v)
(define-key global-map "\E[222z" 'scroll-up)
;; R7 "Home" (As C-a)
(define-key global-map "\E[214z" 'beginning-of-line)
;; R13 "End" (As C-e)
(define-key global-map "\E[220z" 'end-of-line)
;; SHIFT-R7 "Home" (As M-<)
(define-key global-map "\EOw" 'beginning-of-buffer)
;; SHIFT-R13 "End" (As M->)
(define-key global-map "\EOq" 'end-of-buffer)
;; R11 middle
(define-key global-map "\E[218z" 'set-mark-command)
;; R4 "=" (as C-x 1)
(define-key global-map "\E[211z" 'delete-other-windows)
;; R5 "/" (as C-x 2)
(define-key global-map "\E[212z" 'split-window-vertically)
;; "*" (as C-x 0)
(define-key global-map "\E[213z" 'delete-window)
;; "-"
(define-key global-map "\EOm" 'shrink-window)
;; "+" (as C-x ^)
(define-key global-map "\EOk" 'enlarge-window)
;; "EnterOA" (as C-x o)
(define-key global-map "\EOM" 'other-window)
;; SHIFT-R8 up
(define-key global-map "\EOx" 'backward-paragraph)
;; SHIFT-R14 down
(define-key global-map "\EOr" 'forward-paragraph)
;; SHIFT-R10 left
(define-key global-map "\EOt" 'backward-word)
;; SHIFT-R12 right
(define-key global-map "\EOv" 'forward-word)
;; SHIFT-R11 (As C-x x)
(define-key global-map "\EOu" 'exchange-point-and-mark)
;; R2 "PrSc" (as C-l)
(define-key global-map "\E[209z" 'recenter)
;; "Ins" in mail-mode
(define-key mail-mode-map "\E[2z" 'quote-last-kill)

;; left (function) keypad functions
;; L10 "Cut" (as C-w)
(define-key global-map "\E[201z" 'kill-region)
;; L8 "Paste" (as C-y)
(define-key global-map "\E[199z" 'yank)
;; L9 "Find" (as M-s)
(define-key global-map "\E[200z" 'word-search-forward)
;; L4 "Undo" (as C-x u)
(define-key global-map "\E[195z" 'advertised-undo)
;; L6 "Copy" (as M-w)
(define-key global-map "\E[197z" 'copy-region-as-kill)
;; "Help" (as C-h i)
(define-key global-map "\E[196z" 'info)
;; L3 "Props" (as C-x ESC) (would have used L2 "Again", but this is 
;; the same as F12)
(define-key global-map "\E[194z" 'repeat-complex-command)

;; Top row keys
;; F1 (as C-x C-f)
(define-key global-map "\E[224z" 'find-file)
;; F2 (as C-x C-s)
(define-key global-map "\E[225z" 'save-buffer)
;; F3 (as C-x s)
(define-key global-map "\E[226z" 'save-some-buffers)
;; F4 (as C-x i)
(define-key global-map "\E[227z" 'insert-file)
;; F5 (as C-x C-w)
(define-key global-map "\E[228z" 'write-file)
;; F6 (as C-@)
(define-key global-map "\E[229z" 'set-mark-command)
;; F7 (as M-%)
(define-key global-map "\E[230z" 'query-replace)
;; F8
(define-key global-map "\E[231z" 'query-replace-regexp)
;; F9 (as C-x k)
(define-key global-map "\E[232z" 'kill-buffer)
;; F10
(define-key global-map "\E[233z" 'buffer-menu)
;; F11 (as C-x b)
(define-key global-map "\E[192z" 'switch-to-buffer)
;; F12
(define-key global-map "\E[193z" 'rmail)
;; 
;; End of Sun custom keys

> Thanks in advance.

You're welcome.

> Jesse

	Fraz
-- 
Fraser McCrossan|fraser%ukfca1.uk.ate.slb.com@asc.slb.com|(+044)202-893535 x388
  Schlumberger Technologies,Ferndown Ind. Est,Wimborne,Dorset,BH21 7PP,ENGLAND
	Ach 's math dhomh bhith seo an drasd / A cur failt air a' bhlas
  'San tir a tha cho ur dhomh an diugh / Is a bha i nuair bha mi 'nam phaisd