Setting Keys

Defining a key to invoke a certain function is accomplished using the setkey function. This function takes two arguments: the function to be executed and the key binding. For example, suppose that you want to bind the key CTRL-A to cause the cursor to go to the beginning of the current line. The jed function that causes this is bol (See the jed Programmer's Manual for a complete list of functions). Putting the line:

      setkey ("bol", "^A");
in the startup file jed.rc (.jedrc) file will perform the binding. Here ^A consists of the two characters ^ and A which jed will interpret as the single character Ctrl-A. For more examples, see either of the S-Lang files emacs.sl or edt.sl.

The first argument to the setkey function may be any S-Lang expression. Well, almost any. The only restriction is that the newline character cannot appear in the expression. For example, the line

 
      setkey ("bol();skip_white ();",  "^A");
defines the Ctrl-A key such that when it is pressed, the editing point will move the beginning of the line and then skip whitespace up to the first non-whitespace character on the line.

In addition to being able to define keys to execute functions, it is also possible to define a key to directly insert a string of characters. For example, suppose that you want to define a key to insert the string int main(int argc, char **argv) whenever you press the key ESC M. This may be accomplished as follows:

      setkey (" int main(int argc, char **argv)", "\em");

Notice two things. First of all, the key sequence ESC M has been written as "\em" where \e will be interpreted by jed as ESC. The other salient feature is that the first argument to setkey, the ``function'' argument, begins with a space. This tells jed that it is not be interpreted as the name of a function; rather, the characters following the space are to be inserted into the buffer. Omitting the space character would cause jed to execute a function called int main(int argc, char **argv) which would fail and generate an error.

Finally, it is possible to define a key to execute a series of keystrokes similar to a keyboard macro. This is done by prefixing the ``function'' name with the @ character. This instructs jed to interpret the characters following the @ character as characters entered from the keyboard and execute any function that they are bound to. For example, consider the following key definition which will generate a C language comment to comment out the current line of text. In C, this may be achieved by inserting symbol "/*" at the beginning of the line and inserting "*/" at the end of the line. Hence, the sequence is clear (Emacs keybindings):

To bind this sequence of steps to the key sequence ESC ;, simply use

     setkey("@\001/*\005*/", "\e;");

Again, the prefix @ lets jed know that the remaining characters will carry out the functions they are currently bound to. Also pay particular attention to the way CTRL-A and CTRL-E have been written. Do not attempt to use the ^ to represent ``CTRL''. It does not have the same meaning in the first argument to the setkey function as it does in the second argument. To have control characters in the first argument, you must enter them as \xyz where xyz is a three digit decimal number coinciding with the ASCII value of the character. In this notation, the ESC character could have been written as \027. See the S-Lang Programmer's Reference Manual for further discussion of this notation.

The setkey function sets a key in the global keymap from which all others are derived. It is also possible to use the function local_setkey which operates only upon the current keymap which may or may not be the global map.