[comp.emacs] How to define VT2xx or VT3xx function keys in .emacs file?

DHASKIN@CLARKU.BITNET (03/04/89)

Okay, I *know* this can be done, and I'm apparently missing something
obvious.  I would like Emacs to start up with the Do key define as M-x
and Insert Here defined as yank.  I have no problem doing it once I'm
*in* Emacs, with either global-set-key or:

        (define-key esc-map "[26~" 'execute-extended-command)

but when I put the above line in my .emacs file, I get an "Error in Init
File" or some such thing when Emacs starts up.  (BTW, I know that [26~ may
not be *quite* the sequence, but y'know what I mean).

Is this the right direction?  Or does it involve the CSI-map?  I feel sort of
foolish, it should be easy.  But then again, I do recall a discussion a
few months ago about what one could and couldn't do in a .emacs file.

Much thanks in advance -

Denis
(dhaskin@clarku.bitnet or denis@bambam.wellesley.edu)

Ram-Ashwin@cs.yale.edu (Ashwin Ram) (03/05/89)

In article <36761@bbn.COM>, DHASKIN@CLARKU.BITNET writes:
>           I would like Emacs to start up with the Do key define as M-x
> and Insert Here defined as yank.  I have no problem doing it once I'm
> *in* Emacs, with either global-set-key or:
> 
>         (define-key esc-map "[26~" 'execute-extended-command)
> 
> but when I put the above line in my .emacs file, I get an "Error in Init
> File" or some such thing when Emacs starts up.

[Please try to specify the type and version of Emacs you're using when you
report problems.  I assume you're using GNU Emacs.]

I think you're probably getting the error "key sequence [26~ uses illegal
prefix characters".  My guess is that the key sequence ESC-[ is already
defined at the time the define-key is executed (I think it is bound to
backward-paragraph or some such thing by default).  You can't define-key a
key sequence if some prefix of that key sequence is already bound to a
command.

The reason you don't get the error when you do it by hand is that you (or
something you load) probably undefine ESC-[.  Try executing the above form by
hand in a fresh Emacs without loading your .emacs file and see if you get
this error.  Alternatively, do a (setq debug-on-error t) and try to get a
backtrace.

To fix the problem, move whatever undefines ESC-[ (or whatever the illegal
prefix is) up in your file so that it is before whatever tries to use it as a
prefix in a key sequence.


In my opinion, this is a very misleading error message.  Many people,
including me, have been tripped up by this sort of error.  I suggest that the
error message be changed to "the prefix ESC [ is already bound to a command"
or something similar.

I also think that the "error in init file" situation is badly handled.
Firstly, the error message should reflect what the error was.  Secondly,
Emacs should tell you where the error occurred, or better still visit the
offending file and position itself at the point where the error was
encountered.

-- Ashwin.

jr@bbn.com (John Robinson) (03/07/89)

In article <36761@bbn.COM>, DHASKIN@CLARKU writes:
>Okay, I *know* this can be done, and I'm apparently missing something
>obvious.  I would like Emacs to start up with the Do key define as M-x
>and Insert Here defined as yank.  I have no problem doing it once I'm
>*in* Emacs, with either global-set-key or:
>
>        (define-key esc-map "[26~" 'execute-extended-command)
>
>but when I put the above line in my .emacs file, I get an "Error in Init
>File" or some such thing when Emacs starts up.  (BTW, I know that [26~ may
>not be *quite* the sequence, but y'know what I mean).
>
>Is this the right direction?  Or does it involve the CSI-map?  I feel sort of
>foolish, it should be easy.  But then again, I do recall a discussion a
>few months ago about what one could and couldn't do in a .emacs file.

I believe this is because the key bindings at the time your .emacs
file is executed are simply the defaults.  ESC-[ starts off bound to
backward-paragraph, and so can't be the prefix of a longer command
string.

After it has finished you .emacs, emacs reads terminal-specific
initialization from the file ..emacs/lisp/term/vt200.el (assuming your
TERM variable is "vt200"), which will set things so that ESC-[ is a
prefix.  Here's how that happens:

  (global-set-key "\e[" CSI-map)

Once ESC-[ is bound to a keymap, things are fine.  This won't happen
until you call the function (enable-arrow-keys).  Look in vt200.el for
more information on what happens at startup.

Now, since vt200.el is normally read *after* your .emacs, you can't
modify bindings that depend on its keymap settings.  However, there is
a way:

  term-setup-hook's value is nil

  Documentation:
  Function to be called after loading terminal-specific lisp code.
  It is called with no arguments.  You can use this to override the
  definitions made by the terminal-specific file.

You could do something like this in your .emacs:

  (defun my-vt200-init ()
    "Enable the VT200 function keys and bind the Do key"

    (enable-arrow-keys)
    (define-key esc-map "[26~" 'execute-extended-command))

  (setq term-setup-hook 'my-vt200-init)

[Programming style aside: this ought to be careful not to trample an
existing term-setup-hook; to be really cute it should append to the
hook, avoiding signing itself up twice by scanning the list first.
There oughta be a package to help with this...any volunteers?]

Rather than define-key, you should use global-set-key:

  (global-set-key "\e[26~" 'execute-extended-command)

This avoids having to know the name of esc-map, which just could
change under you some day.

Having said all this, there is another way that ought to work in your
.emacs file:

  (require 'keypad)
  (keypad-default "x" 'execute-etended-command)

The beauty of this is that it will work across all terminals that have
a "Do" key, and avoids the hassle of the term-setup-hook.  Details in
lisp/keypad.el.

Let me also echo the complaints about the un-help-ful-ness [that's
kind-er-and-gen-tle-er speak] of "Error in Init file"!

p.s. to Kyle Jones: another one for the frequent questions file?
--
/jr
jr@bbn.com or bbn!jr