August Feng

Creating menu items with emacs

About

We need three things in order to create a new menu on MacOS for emacs.

Keymap

First we need to create a keymap. This is also used by modes to create keybindings.

  (defvar foobar (make-sparse-keymap "foobar"))

define-key

The define-key function is used to map keys to a function. For example, this following function will write "helloworld" on C-c z:

  (define-key (current-global-map) (kbd "C-c z") (lambda () (interactive) (message "helloworld")))

It's also used to create menu items:

  (define-key foobar [helloworld]
              '(menu-item "foobar" (lambda () (interactive) (message "helloworld"))))

To see this menu item, we need to attach it to the global menu bar:

  (define-key (current-global-map) [menu-bar helloworld]
              (cons "helloworld" foobar))

As you may have observed, the define-key function is (quite) overloaded. In the first usage, we're using a vector as an argument for KEY and a command as an argument for DEF.

In the second usage, we're using a an extended menu items definitions.

In the third usage, we're using a cons as argument for DEF.