[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4. Customize key bindings and menus

When you start Emacs, it reads the file `~/.xemacs/init.el' in the `.xemacs/' subdirectory of 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 `init.el' 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 `init.el' file. After you make changes to your `init.el' 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 section `Lisp Eval' in XEmacs User's Manual).

4.1 Customize key bindings  Changing Key Bindings
4.2 Customizing Menus  Adding, Deleting, Enabling and Disabling Menus

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1 Customize key bindings

Most of Emacs commands use key sequences. See section `Keystrokes' in 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 init.el 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 init.el 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 `init.el' file:

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

You might want to have this statement in your `init.el' file because its easy to hit this command by mistake and it could be annoying to exit Emacs unintentionally. There is an 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:

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 section `Misc File Ops' in XEmacs User's Manual). However, the second example will bind the key only for C mode. See section `Major Modes' in XEmacs User's Manual, for more information on Major Modes in XEmacs.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.2 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 section 2.2 XEmacs 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 `init.el' file or evaluate it (see section 4. Customize key bindings and menus),

    (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
    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.

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs Webmaster on August, 3 2012 using texi2html