(INCLUDED_FCNTL): Define, if include fcntl.h.
[bpt/emacs.git] / lisp / emacs-lisp / easymenu.el
CommitLineData
029b623a
RS
1;;; easymenu.el --- support the easymenu interface for defining a menu.
2
3;; Keywords: emulations
4
5;; Copyright (C) 1994 Free Software Foundation, Inc.
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation; either version 2, or (at your option)
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs; see the file COPYING. If not, write to
21;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23;;; This is compatible with easymenu.el by Per Abrahamsen
24;;; but it is much simpler as it doesn't try to support other Emacs versions.
25;;; The code was mostly derived from lmenu.el.
26
27;;; Code:
28
a8226f67 29;;;###autoload
029b623a
RS
30(defun easy-menu-define (symbol maps doc menu)
31 "Define a menu bar submenu in maps MAPS, according to MENU.
32The arguments SYMBOL and DOC are ignored; they are present for
a8226f67
RS
33compatibility only. In other Emacs versions they may be used
34as a variable to hold the menu data, and a doc string for that variable.
029b623a
RS
35
36The first element of MENU must be a string. It is the menu bar item name.
37The rest of the elements are menu items.
38
a8226f67 39A menu item is usually a vector of three elements: [NAME CALLBACK t]
029b623a 40
a8226f67 41NAME is a string--the menu item name.
029b623a 42
a8226f67
RS
43CALLBACK is a command to run when the item is chosen,
44or a list to evaluate when the item is chosen.
029b623a 45
a8226f67
RS
46A menu item can be a string. Then that string appears in the menu as
47unselectable text. A string consisting solely of hyphens is displayed
48as a solid horizontal line.
029b623a 49
a8226f67 50A menu item can be a list. It is treated as a submenu.
029b623a
RS
51The first element should be the submenu name. That's used as the
52menu item in the top-level menu. The cdr of the submenu list
53is a list of menu items, as above."
a8226f67 54 (or (keymapp maps) (setq maps (list maps)))
029b623a 55 (let ((keymap (easy-menu-keymap (car menu) (cdr menu))))
a8226f67
RS
56 (while maps
57 (define-key (car maps) (vector 'menu-bar (intern (car menu)))
58 (cons (car menu) keymap))
59 (setq maps (cdr maps)))))
60
61(defvar easy-menu-item-count 0)
029b623a
RS
62
63;; Return a menu keymap corresponding to a Lucid-style menu list
64;; MENU-ITEMS, and with name MENU-NAME.
65(defun easy-menu-keymap (menu-name menu-items)
66 (let ((menu (make-sparse-keymap menu-name)))
67 ;; Process items in reverse order,
68 ;; since the define-key loop reverses them again.
69 (setq menu-items (reverse menu-items))
70 (while menu-items
71 (let* ((item (car menu-items))
72 (callback (if (vectorp item) (aref item 1)))
73 command enabler name)
74 (cond ((stringp item)
75 (setq command nil)
76 (setq name (if (string-match "^-+$" item) "" item)))
77 ((consp item)
a8226f67 78 (setq command (easy-menu-keymap (car item) (cdr item)))
029b623a
RS
79 (setq name (car item)))
80 ((vectorp item)
81 (setq command (make-symbol (format "menu-function-%d"
a8226f67 82 easy-menu-item-count)))
029b623a 83 (setq enabler (make-symbol (format "menu-function-%d-enabler"
a8226f67
RS
84 easy-menu-item-count)))
85 (setq easy-menu-item-count (1+ easy-menu-item-count))
029b623a
RS
86 (put command 'menu-enable enabler)
87 (set enabler (aref item 2))
88 (setq name (aref item 0))
89 (if (symbolp callback)
90 (fset command callback)
91 (fset command (list 'lambda () '(interactive) callback)))))
92 (if (null command)
93 ;; Handle inactive strings specially--allow any number
94 ;; of identical ones.
95 (setcdr menu (cons (list nil name) (cdr menu)))
96 (if name
97 (define-key menu (vector (intern name)) (cons name command)))))
98 (setq menu-items (cdr menu-items)))
99 menu))
100
101(provide 'easymenu)
102
103;;; easymenu.el ends here