Customize key bindings and menus
********************************

When you start Emacs, it reads the file ~/.emacs in your home directory. You can use this file to initialize and customize Emacs to your liking. This file should contain lisp-code. You can customize your .emacs file to create new menus, disable menus, change key bindings, enable a minor mode, etc. Any kind of customization affects only a particular Emacs job that you do them in. If you want to save your customizations `permanently' i.e. for future use also, you have to put it in your .emacs file. After you make changes to your .emacs file and save it, the changes will be effective only after you start Emacs again i.e. for a new Emacs process. To try out some of the examples in this section, highlight that region and evaluate the region by giving the command M-x eval-region. You will be able to see the results of your customizations in that Emacs session only (see Lisp Eval (XEmacs User's Manual)).

Customize key bindings
======================

Most of Emacs commands use key sequences. See Keystrokes (XEmacs User's Manual), for more information about Keys and Commands. In Emacs, the keys themselves carry no meaning unless they are bound to a function. For example, C-n moves the cursor to the next line because its bound to the function next-line. Similarly, C-p moves to the previous line because its bound to the function previous-line. The functions themselves define a particular behavior. You can customize the key C-n to move to the previous line by binding it to previous-line and C-p to move to the next line by binding it to next-line. To bind keys to globally run commands you need to use the following syntax in your .emacs file:

     (global-set-key KEYS CMD)

Here, global-set-key is a function which will bind the keys to the specified cmd. For example, if you type the following in your .emacs file:

     (global-set-key "\C-p" 'next-line)
     (global-set-key "\C-n" 'previous-line)

then C-p will move to the next line and C-n to the previous line.

You can also disable a key binding, by using nil as the CMD in the syntax stated above. Here, nil stands for false which means disable a command or turn off a feature. If you want to enable a command or turn on a particular feature use t which stands for true.  For example, if you do not wish C-x C-c to Exit Emacs you can type the following expression in your .emacs file:

     (global-set-key "\C-x\C-c" nil)

You might want to have this statement in your .emacs file because its easy to hit this command by mistake and it could be annoying to exit Emacs unintentionally. There is a Exit Emacs option in the File menu which you might want to use instead. To make a particular key undefined you can also use:

     (global-unset-key "\C-x\C-c")

Now if you use the command C-x C-c, you will get an error saying that the command is undefined.

Some other customizations you could try are:

  * (global-set-key 'button3 'beginning-of-buffer)

     Now when you press the third button of your mouse, the cursor will be placed at the beginning-of-buffer.

  * (global-set-key 'f1 'goto-line)

     If you press the F1 key, you will be prompted for a line number. After you type the line number and hit RET, the cursor will be placed on that line number.

  * (global-set-key 'f2 'undo)

     Pressing F2 will undo the last command. If you have a undo key on your keyboard, try binding that key to the undo command.

Another syntax for customizing key bindings is: (define-key KEYMAP KEYS DEF) It defines KEYS to run DEF in the keymap KEYMAP.

KEYMAP is a keymap object which records the bindings of keys to the commands that they run.

KEYS is the sequence of keystrokes to bind.

DEF is anything that can be a key's definition:

Look at the following two examples:

     (define-key global-map "\C-xl" 'make-symbolic-link)
     (define-key c-mode-map "\C-xl" 'make-symbolic-link)

Both the examples bind the key C-xl to run the function make-symbolic-link (see Misc File Ops (XEmacs User's Manual)). However, the second example will bind the key only for C mode. See Major Modes (XEmacs User's Manual), for more information on Major Modes in XEmacs.

Customizing Menus
=================

You can customize any of the  XEmacs Pull-down-Menus. You can create your own menu, delete an existing one, enable a menu or disable a menu. For more information on the default menus available to you, see Pull-down Menus.

Some of the functions which are available to you for customization are:

 1. add-menu-item: (MENU-NAME ITEM-NAME FUNCTION ENABLED-P &optional BEFORE)

     This function will add a menu item to a menu, creating the menu first if necessary. If the named item already exists, the menu will remain unchanged. For example, if you add the following example to your .emacs file or evaluate it (see Customization Basics),

          (add-menu-item '("Edit") "Replace String" replace-string t "Clear")

     a sub-menu Replace String will be created under Edit menu before the sub-menu Clear. The Edit menu will now look like:

          Undo                    C-x u
          Cut                     cut
          Copy                    copy
          Paste                   paste
          Replace String
          Clear
          Start Macro Recording   C-x(
          End Macro Recording     C-x)
          Execute Last Macro      C-xe

     Replace String will now execute the function replace-string. Select this menu item. Emacs will prompt you for a string name to be replaced. Type a string and hit RET. Now type a new string to replace the old string and hit RET. All occurrences of the old string will be replaced by the new string. In this example,

     Edit is the MENU-NAME which identifies the menu into which the new menu item should be inserted.

     Replace String is the ITEM-NAME which names the menu item to be added.

     replace-string is the FUNCTION i.e. the command to be invoked when the menu item "Replace String" is selected.

     t is the ENABLED-P parameter which controls whether the menu item is selectable or not. This parameter can be either t (selectable), nil (not selectable), or a form to evaluate. This form is evaluated just before the menu is displayed, and the menu item will be selectable if the form returns non-nil.

     Clear is the &OPTIONAL BEFORE parameter which is the name of the menu before which the new menu or sub-menu should be added. The &OPTIONAL string means that this parameter is optional. You do not need to specify this parameter. If you do not specify this parameter in the example above, the Replace String menu item will be added at the end of the list of sub-menus in the Edit menu i.e. after Execute Last Macro.

     If you wish to add a new menu to the menubar, try:

          (add-menu-item nil "Bot" 'end-of-buffer t)

     This will create a new menu Bot on the menu bar. Selecting this menu will take you to the end of the buffer. Using nil for the parameter MENU-NAME will create a new menu. Your menu-bar will now look like:

          File Edit Options Buffers Bot                         Help

     The following example will illustrate how you can add sub-menus to the submenus themselves:

          (add-menu-item '("File" "Management") "Copy File" 'copy-file t)
          (add-menu-item '("File" "Management") "Delete File" 'delete-file t)
          (add-menu-item '("File" "Management") "Rename File" 'rename-file t)

     This will create a sub-menu Management under the File menu. When you select the submenu Management, it will contain three submenus: Copy File, Delete File and Rename File.

 2. delete-menu-item: (MENU-PATH) This function will remove the menu item defined by MENU-NAME from the menu hierarchy. Look at the following examples and the comments just above them which specify what the examples do.

          ;; deletes the "Replace String" menu item created earlier
          (delete-menu-item '("Edit" "Replace String"))

          ;; deletes the "Bot" menu created earlier
          (delete-menu-item '("Bot"))

          ;; deletes the sub-menu "Copy File" created earlier
          (delete-menu-item '("File" "File Management" "Copy File"))

          ;; deletes the sub-menu "Delete File" created earlier
          (delete-menu-item '("File" "Management" "Delete File"))

          ;; deletes the sub-menu "Rename File" created earlier
          (delete-menu-item '("File" "Management" "Rename File"))

 3. disable-menu-item: (MENU-NAME) Disables the specified menu item. The following example

          (disable-menu-item '("File" "Management" "Copy File"))

     will make the Copy File item unselectable. This menu-item would still be there but it will appear faded which would mean that it cannot be selected.

 4. enable-menu-item: (MENU-NAME) Enables the specified previously disabled menu item.

          (enable-menu-item '("File" "Management" "Copy File"))

     This will enable the sub-menu Copy File, which was disabled by the earlier command.

 5. relabel-menu-item: (MENU-NAME NEW-NAME) Change the string of the menu item specified by MENU-NAME to NEW-NAME.

          (relabel-menu-item '("File" "Open...") "Open File")

     This example will rename the Open... menu item from the File menu to Open File.