annotate lisp/emacs-lisp/easymenu.el @ 42411:c67f88ae0e05

Remove trailing whitespaces.
author Pavel Janík <Pavel@Janik.cz>
date Sat, 29 Dec 2001 22:21:10 +0000
parents 198c1186dee3
children af8676236636
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38431
853c3674f20a Fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 34541
diff changeset
1 ;;; easymenu.el --- support the easymenu interface for defining a menu
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
34541
99cd5704dacb (easy-menu-current-active-maps): Test
Gerd Moellmann <gerd@gnu.org>
parents: 34520
diff changeset
3 ;; Copyright (C) 1994, 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
4
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Keywords: emulations
38431
853c3674f20a Fixes to follow coding conventions.
Pavel Janík <Pavel@Janik.cz>
parents: 34541
diff changeset
6 ;; Author: Richard Stallman <rms@gnu.org>
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; any later version.
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; GNU General Public License for more details.
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
23 ;; Boston, MA 02111-1307, USA.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
25 ;;; Commentary:
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
26
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
27 ;; This is compatible with easymenu.el by Per Abrahamsen
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
28 ;; but it is much simpler as it doesn't try to support other Emacs versions.
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 14108
diff changeset
29 ;; The code was mostly derived from lmenu.el.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;; Code:
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
33 (defcustom easy-menu-precalculate-equivalent-keybindings t
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
34 "Determine when equivalent key bindings are computed for easy-menu menus.
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
35 It can take some time to calculate the equivalent key bindings that are shown
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
36 in a menu. If the variable is on, then this calculation gives a (maybe
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
37 noticeable) delay when a mode is first entered. If the variable is off, then
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
38 this delay will come when a menu is displayed the first time. If you never use
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
39 menus, turn this variable off, otherwise it is probably better to keep it on."
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
40 :type 'boolean
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
41 :group 'menu
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
42 :version "20.3")
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
43
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
44 (defsubst easy-menu-intern (s)
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
45 (if (stringp s) (intern s) s))
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
46
6542
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
47 ;;;###autoload
34520
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
48 (put 'easy-menu-define 'lisp-indent-function 'defun)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
49 ;;;###autoload
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
50 (defmacro easy-menu-define (symbol maps doc menu)
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 "Define a menu bar submenu in maps MAPS, according to MENU.
11512
a196f05240a9 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 10415
diff changeset
52 The menu keymap is stored in symbol SYMBOL, both as its value
a196f05240a9 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 10415
diff changeset
53 and as its function definition. DOC is used as the doc string for SYMBOL.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 The first element of MENU must be a string. It is the menu bar item name.
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
56 It may be followed by the following keyword argument pairs
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
57
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
58 :filter FUNCTION
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
59
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
60 FUNCTION is a function with one argument, the menu. It returns the actual
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
61 menu displayed.
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
62
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
63 :visible INCLUDE
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
64
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
65 INCLUDE is an expression; this menu is only visible if this
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
66 expression has a non-nil value. `:include' is an alias for `:visible'.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
67
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
68 :active ENABLE
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
69
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
70 ENABLE is an expression; the menu is enabled for selection
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
71 whenever this expression's value is non-nil.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
72
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
73 The rest of the elements in MENU, are menu items.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
75 A menu item is usually a vector of three elements: [NAME CALLBACK ENABLE]
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76
6542
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
77 NAME is a string--the menu item name.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78
6542
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
79 CALLBACK is a command to run when the item is chosen,
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
80 or a list to evaluate when the item is chosen.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81
8541
39e8d792604f (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 8085
diff changeset
82 ENABLE is an expression; the item is enabled for selection
39e8d792604f (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 8085
diff changeset
83 whenever this expression's value is non-nil.
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
84
26428
f572944ca41f * emacs-lisp/debug.el (debugger-env-macro):
Sam Steingold <sds@gnu.org>
parents: 25224
diff changeset
85 Alternatively, a menu item may have the form:
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
86
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
87 [ NAME CALLBACK [ KEYWORD ARG ] ... ]
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
88
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
89 Where KEYWORD is one of the symbols defined below.
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
90
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
91 :keys KEYS
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
92
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
93 KEYS is a string; a complex keyboard equivalent to this menu item.
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
94 This is normally not needed because keyboard equivalents are usually
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
95 computed automatically.
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
96 KEYS is expanded with `substitute-command-keys' before it is used.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
97
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
98 :key-sequence KEYS
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
99
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
100 KEYS is nil, a string or a vector; nil or a keyboard equivalent to this
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
101 menu item.
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
102 This is a hint that will considerably speed up Emacs' first display of
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
103 a menu. Use `:key-sequence nil' when you know that this menu item has no
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
104 keyboard equivalent.
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
105
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
106 :active ENABLE
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
107
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
108 ENABLE is an expression; the item is enabled for selection
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
109 whenever this expression's value is non-nil.
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
110
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
111 :included INCLUDE
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
112
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
113 INCLUDE is an expression; this item is only visible if this
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
114 expression has a non-nil value.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
115
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
116 :suffix FORM
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
117
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
118 FORM is an expression that will be dynamically evaluated and whose
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
119 value will be concatenated to the menu entry's NAME.
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
120
14108
cab8d383f10d (easy-menu-define): Doc fix.
Karl Heuer <kwzh@gnu.org>
parents: 11860
diff changeset
121 :style STYLE
26428
f572944ca41f * emacs-lisp/debug.el (debugger-env-macro):
Sam Steingold <sds@gnu.org>
parents: 25224
diff changeset
122
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
123 STYLE is a symbol describing the type of menu item. The following are
26428
f572944ca41f * emacs-lisp/debug.el (debugger-env-macro):
Sam Steingold <sds@gnu.org>
parents: 25224
diff changeset
124 defined:
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
125
16812
af96712b7f5d (easy-menu-create-keymaps): Menu item STYLE toggle (checkbox)
Richard M. Stallman <rms@gnu.org>
parents: 16160
diff changeset
126 toggle: A checkbox.
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
127 Prepend the name with `(*) ' or `( ) ' depending on if selected or not.
16812
af96712b7f5d (easy-menu-create-keymaps): Menu item STYLE toggle (checkbox)
Richard M. Stallman <rms@gnu.org>
parents: 16160
diff changeset
128 radio: A radio button.
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
129 Prepend the name with `[X] ' or `[ ] ' depending on if selected or not.
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
130 button: Surround the name with `[' and `]'. Use this for an item in the
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
131 menu bar itself.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
132 anything else means an ordinary menu item.
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
133
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
134 :selected SELECTED
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
135
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
136 SELECTED is an expression; the checkbox or radio button is selected
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
137 whenever this expression's value is non-nil.
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
138
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
139 :help HELP
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
140
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
141 HELP is a string, the help to display for the menu item.
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
142
6542
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
143 A menu item can be a string. Then that string appears in the menu as
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
144 unselectable text. A string consisting solely of hyphens is displayed
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
145 as a solid horizontal line.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
147 A menu item can be a list with the same format as MENU. This is a submenu."
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
148 `(progn
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
149 (defvar ,symbol nil ,doc)
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
150 (easy-menu-do-define (quote ,symbol) ,maps ,doc ,menu)))
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
151
11860
0c39d1945e5e (easy-menu-do-define): Add autoload cookie.
Karl Heuer <kwzh@gnu.org>
parents: 11833
diff changeset
152 ;;;###autoload
9733
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
153 (defun easy-menu-do-define (symbol maps doc menu)
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
154 ;; We can't do anything that might differ between Emacs dialects in
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
155 ;; `easy-menu-define' in order to make byte compiled files
a721dbc48e10 (easy-menu-define): Call `easy-menu-do-define' to do
Richard M. Stallman <rms@gnu.org>
parents: 9586
diff changeset
156 ;; compatible. Therefore everything interesting is done in this
26428
f572944ca41f * emacs-lisp/debug.el (debugger-env-macro):
Sam Steingold <sds@gnu.org>
parents: 25224
diff changeset
157 ;; function.
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
158 (let ((keymap (easy-menu-create-menu (car menu) (cdr menu))))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
159 (set symbol keymap)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
160 (fset symbol
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
161 `(lambda (event) ,doc (interactive "@e")
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
162 ;; FIXME: XEmacs uses popup-menu which calls the binding
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
163 ;; while x-popup-menu only returns the selection.
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
164 (x-popup-menu event
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
165 (or (and (symbolp ,symbol)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
166 (funcall
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
167 (or (plist-get (get ,symbol 'menu-prop)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
168 :filter)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
169 'identity)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
170 (symbol-function ,symbol)))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
171 ,symbol))))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
172 (mapcar (lambda (map)
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
173 (define-key map (vector 'menu-bar (easy-menu-intern (car menu)))
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
174 (cons 'menu-item
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
175 (cons (car menu)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
176 (if (not (symbolp keymap))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
177 (list keymap)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
178 (cons (symbol-function keymap)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
179 (get keymap 'menu-prop)))))))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
180 (if (keymapp maps) (list maps) maps))))
6542
1d9da8160357 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6529
diff changeset
181
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
182 (defun easy-menu-filter-return (menu &optional name)
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
183 "Convert MENU to the right thing to return from a menu filter.
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
184 MENU is a menu as computed by `easy-menu-define' or `easy-menu-create-menu' or
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
185 a symbol whose value is such a menu.
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
186 In Emacs a menu filter must return a menu (a keymap), in XEmacs a filter must
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
187 return a menu items list (without menu name and keywords).
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
188 This function returns the right thing in the two cases.
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
189 If NAME is provided, it is used for the keymap."
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
190 (when (and (not (keymapp menu)) (consp menu))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
191 ;; If it's a cons but not a keymap, then it can't be right
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
192 ;; unless it's an XEmacs menu.
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
193 (setq menu (easy-menu-create-menu (or name "") menu)))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
194 (easy-menu-get-map menu nil)) ; Get past indirections.
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195
9586
bada2dc32adc (easy-menu-create-keymaps): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents: 8541
diff changeset
196 ;;;###autoload
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
197 (defun easy-menu-create-menu (menu-name menu-items)
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
198 "Create a menu called MENU-NAME with items described in MENU-ITEMS.
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
199 MENU-NAME is a string, the name of the menu. MENU-ITEMS is a list of items
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
200 possibly preceded by keyword pairs as described in `easy-menu-define'."
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
201 (let ((menu (make-sparse-keymap menu-name))
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
202 prop keyword arg label enable filter visible help)
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
203 ;; Look for keywords.
29054
25099b559af9 (easy-menu-create-menu, easy-menu-do-add-item): Use keywordp.
Dave Love <fx@gnu.org>
parents: 28522
diff changeset
204 (while (and menu-items
25099b559af9 (easy-menu-create-menu, easy-menu-do-add-item): Use keywordp.
Dave Love <fx@gnu.org>
parents: 28522
diff changeset
205 (cdr menu-items)
25099b559af9 (easy-menu-create-menu, easy-menu-do-add-item): Use keywordp.
Dave Love <fx@gnu.org>
parents: 28522
diff changeset
206 (keywordp (setq keyword (car menu-items))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
207 (setq arg (cadr menu-items))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
208 (setq menu-items (cddr menu-items))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
209 (cond
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
210 ((eq keyword :filter)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
211 (setq filter `(lambda (menu)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
212 (easy-menu-filter-return (,arg menu) ,menu-name))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
213 ((eq keyword :active) (setq enable (or arg ''nil)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
214 ((eq keyword :label) (setq label arg))
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
215 ((eq keyword :help) (setq help arg))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
216 ((or (eq keyword :included) (eq keyword :visible))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
217 (setq visible (or arg ''nil)))))
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
218 (if (equal visible ''nil)
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
219 nil ; Invisible menu entry, return nil.
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
220 (if (and visible (not (easy-menu-always-true visible)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
221 (setq prop (cons :visible (cons visible prop))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
222 (if (and enable (not (easy-menu-always-true enable)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
223 (setq prop (cons :enable (cons enable prop))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
224 (if filter (setq prop (cons :filter (cons filter prop))))
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
225 (if help (setq prop (cons :help (cons help prop))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
226 (if label (setq prop (cons nil (cons label prop))))
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
227 (if filter
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
228 ;; The filter expects the menu in its XEmacs form and the pre-filter
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
229 ;; form will only be passed to the filter anyway, so we'd better
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
230 ;; not convert it at all (it will be converted on the fly by
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
231 ;; easy-menu-filter-return).
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
232 (setq menu menu-items)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
233 (setq menu (append menu (mapcar 'easy-menu-convert-item menu-items))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
234 (when prop
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
235 (setq menu (easy-menu-make-symbol menu 'noexp))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
236 (put menu 'menu-prop prop))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
237 menu)))
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
239
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
240 ;; Known button types.
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
241 (defvar easy-menu-button-prefix
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
242 '((radio . :radio) (toggle . :toggle)))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
243
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
244 (defun easy-menu-do-add-item (menu item &optional before)
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
245 (setq item (easy-menu-convert-item item))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
246 (easy-menu-define-key-intern menu (car item) (cdr item) before))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
247
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
248 (defvar easy-menu-converted-items-table (make-hash-table :test 'equal))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
249
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
250 (defun easy-menu-convert-item (item)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
251 "Memoize the value returned by `easy-menu-convert-item-1' called on ITEM.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
252 This makes key-shortcut-caching work a *lot* better when this
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
253 conversion is done from within a filter.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
254 This also helps when the NAME of the entry is recreated each time:
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
255 since the menu is built and traversed separately, the lookup
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
256 would always fail because the key is `equal' but not `eq'."
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
257 (or (gethash item easy-menu-converted-items-table)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
258 (puthash item (easy-menu-convert-item-1 item)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
259 easy-menu-converted-items-table)))
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
260
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
261 (defun easy-menu-convert-item-1 (item)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
262 "Parse an item description and add the item to a keymap.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
263 This is the function that is used for item definition by the other easy-menu
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
264 functions.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
265 MENU is a sparse keymap i.e. a list starting with the symbol `keymap'.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
266 ITEM defines an item as in `easy-menu-define'.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
267 Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
268 put item before BEFORE in MENU, otherwise if item is already present in
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
269 MENU, just change it, otherwise put it last in MENU."
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
270 (let (name command label prop remove help)
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
271 (cond
24987
aee690417f63 (easy-menu-do-add-item): Support for new seperator types.
Gerd Moellmann <gerd@gnu.org>
parents: 24341
diff changeset
272 ((stringp item) ; An item or separator.
aee690417f63 (easy-menu-do-add-item): Support for new seperator types.
Gerd Moellmann <gerd@gnu.org>
parents: 24341
diff changeset
273 (setq label item))
aee690417f63 (easy-menu-do-add-item): Support for new seperator types.
Gerd Moellmann <gerd@gnu.org>
parents: 24341
diff changeset
274 ((consp item) ; A sub-menu
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
275 (setq label (setq name (car item)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
276 (setq command (cdr item))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
277 (if (not (keymapp command))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
278 (setq command (easy-menu-create-menu name command)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
279 (if (null command)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
280 ;; Invisible menu item. Don't insert into keymap.
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
281 (setq remove t)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
282 (when (and (symbolp command) (setq prop (get command 'menu-prop)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
283 (when (null (car prop))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
284 (setq label (cadr prop))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
285 (setq prop (cddr prop)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
286 (setq command (symbol-function command)))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
287 ((vectorp item) ; An item.
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
288 (let* ((ilen (length item))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
289 (active (if (> ilen 2) (or (aref item 2) ''nil) t))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
290 (no-name (not (symbolp (setq command (aref item 1)))))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
291 cache cache-specified)
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
292 (setq label (setq name (aref item 0)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
293 (if no-name (setq command (easy-menu-make-symbol command)))
29054
25099b559af9 (easy-menu-create-menu, easy-menu-do-add-item): Use keywordp.
Dave Love <fx@gnu.org>
parents: 28522
diff changeset
294 (if (keywordp active)
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
295 (let ((count 2)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
296 keyword arg suffix visible style selected keys)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
297 (setq active nil)
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
298 (while (> ilen count)
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
299 (setq keyword (aref item count))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
300 (setq arg (aref item (1+ count)))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
301 (setq count (+ 2 count))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
302 (cond
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
303 ((or (eq keyword :included) (eq keyword :visible))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
304 (setq visible (or arg ''nil)))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
305 ((eq keyword :key-sequence)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
306 (setq cache arg cache-specified t))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
307 ((eq keyword :keys) (setq keys arg no-name nil))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
308 ((eq keyword :label) (setq label arg))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
309 ((eq keyword :active) (setq active (or arg ''nil)))
28522
7fcfdde7365a (easy-menu-create-menu): Process menu
Gerd Moellmann <gerd@gnu.org>
parents: 26428
diff changeset
310 ((eq keyword :help) (setq prop (cons :help (cons arg prop))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
311 ((eq keyword :suffix) (setq suffix arg))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
312 ((eq keyword :style) (setq style arg))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
313 ((eq keyword :selected) (setq selected (or arg ''nil)))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
314 (if suffix
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
315 (setq label
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
316 (if (stringp suffix)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
317 (if (stringp label) (concat label " " suffix)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
318 (list 'concat label (concat " " suffix)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
319 (if (stringp label)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
320 (list 'concat (concat label " ") suffix)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
321 (list 'concat label " " suffix)))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
322 (cond
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
323 ((eq style 'button)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
324 (setq label (if (stringp label) (concat "[" label "]")
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
325 (list 'concat "[" label "]"))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
326 ((and selected
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
327 (setq style (assq style easy-menu-button-prefix)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
328 (setq prop (cons :button
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
329 (cons (cons (cdr style) selected) prop)))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
330 (when (stringp keys)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
331 (if (string-match "^[^\\]*\\(\\\\\\[\\([^]]+\\)]\\)[^\\]*$"
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
332 keys)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
333 (let ((prefix
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
334 (if (< (match-beginning 0) (match-beginning 1))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
335 (substring keys 0 (match-beginning 1))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
336 (postfix
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
337 (if (< (match-end 1) (match-end 0))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
338 (substring keys (match-end 1))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
339 (cmd (intern (substring keys (match-beginning 2)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
340 (match-end 2)))))
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
341 (setq keys (and (or prefix postfix)
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
342 (cons prefix postfix)))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
343 (setq keys
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
344 (and (or keys (not (eq command cmd)))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
345 (cons cmd keys))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
346 (setq cache-specified nil))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
347 (if keys (setq prop (cons :keys (cons keys prop)))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
348 (if (and visible (not (easy-menu-always-true visible)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
349 (if (equal visible ''nil)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
350 ;; Invisible menu item. Don't insert into keymap.
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
351 (setq remove t)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
352 (setq prop (cons :visible (cons visible prop)))))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
353 (if (and active (not (easy-menu-always-true active)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
354 (setq prop (cons :enable (cons active prop))))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
355 (if (and (or no-name cache-specified)
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
356 (or (null cache) (stringp cache) (vectorp cache)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
357 (setq prop (cons :key-sequence (cons cache prop))))))
23955
9f9e1b450ff5 (easy-menu-get-map): Change global map only if this menu exists in the
Richard M. Stallman <rms@gnu.org>
parents: 23939
diff changeset
358 (t (error "Invalid menu item in easymenu")))
30207
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
359 ;; `intern' the name so as to merge multiple entries with the same name.
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
360 ;; It also makes it easier/possible to lookup/change menu bindings
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
361 ;; via keymap functions.
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
362 (cons (easy-menu-intern name)
30207
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
363 (and (not remove)
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
364 (cons 'menu-item
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
365 (cons label
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
366 (and name
584fa6f665f1 (easy-menu-convert-item-1): Intern the label.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30194
diff changeset
367 (cons command prop))))))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
368
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
369 (defun easy-menu-define-key-intern (menu key item &optional before)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
370 "Like easy-menu-define-key, but interns KEY and BEFORE if they are strings."
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
371 (easy-menu-define-key menu (easy-menu-intern key) item
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
372 (easy-menu-intern before)))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
373
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
374 (defun easy-menu-define-key (menu key item &optional before)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
375 "Add binding in MENU for KEY => ITEM. Similar to `define-key-after'.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
376 If KEY is not nil then delete any duplications. If ITEM is nil, then
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
377 don't insert, only delete.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
378 Optional argument BEFORE is nil or a key in MENU. If BEFORE is not nil
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
379 put binding before BEFORE in MENU, otherwise if binding is already
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
380 present in MENU, just change it, otherwise put it last in MENU.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
381 KEY and BEFORE don't have to be symbols, comparison is done with equal
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
382 not with eq."
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
383 (let ((inserted (null item)) ; Fake already inserted.
21807
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
384 tail done)
20801
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
385 (while (not done)
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
386 (cond
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
387 ((or (setq done (or (null (cdr menu)) (keymapp (cdr menu))))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
388 (and before (equal (car-safe (cadr menu)) before)))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
389 ;; If key is nil, stop here, otherwise keep going past the
20801
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
390 ;; inserted element so we can delete any duplications that come
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
391 ;; later.
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
392 (if (null key) (setq done t))
20801
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
393 (unless inserted ; Don't insert more than once.
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
394 (setcdr menu (cons (cons key item) (cdr menu)))
20801
8aeddd528f57 (easy-menu-add-item); The BEFORE argument works
Richard M. Stallman <rms@gnu.org>
parents: 20791
diff changeset
395 (setq inserted t)
21807
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
396 (setq menu (cdr menu)))
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
397 (setq menu (cdr menu)))
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
398 ((and key (equal (car-safe (cadr menu)) key))
21807
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
399 (if (or inserted ; Already inserted or
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
400 (and before ; wanted elsewhere and
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
401 (setq tail (cddr menu)) ; not last item and not
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
402 (not (keymapp tail))
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
403 (not (equal (car-safe (car tail)) before)))) ; in position
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
404 (setcdr menu (cddr menu)) ; Remove item.
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
405 (setcdr (cadr menu) item) ; Change item.
21807
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
406 (setq inserted t)
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
407 (setq menu (cdr menu))))
c62197b13ece (easy-menu-define-key): Fixed bug with BEFORE
Richard M. Stallman <rms@gnu.org>
parents: 21745
diff changeset
408 (t (setq menu (cdr menu)))))))
26428
f572944ca41f * emacs-lisp/debug.el (debugger-env-macro):
Sam Steingold <sds@gnu.org>
parents: 25224
diff changeset
409
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
410 (defun easy-menu-always-true (x)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
411 "Return true if X never evaluates to nil."
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
412 (if (consp x) (and (eq (car x) 'quote) (cadr x))
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
413 (or (eq x t) (not (symbolp x)))))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
414
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
415 (defvar easy-menu-item-count 0)
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
416
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
417 (defun easy-menu-make-symbol (callback &optional noexp)
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
418 "Return a unique symbol with CALLBACK as function value.
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
419 When non-nil, NOEXP indicates that CALLBACK cannot be an expression
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
420 \(i.e. does not need to be turned into a function)."
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
421 (let ((command
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
422 (make-symbol (format "menu-function-%d" easy-menu-item-count))))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
423 (setq easy-menu-item-count (1+ easy-menu-item-count))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
424 (fset command
30057
6413c7b9a6c3 (easy-menu-define): Docstring fix.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 29054
diff changeset
425 (if (or (keymapp callback) noexp) callback
21745
38a6d62cddb9 Use new menu item format. Don't simulate button prefix.
Richard M. Stallman <rms@gnu.org>
parents: 21689
diff changeset
426 `(lambda () (interactive) ,callback)))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
427 command))
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
428
22033
aa1d105f0d67 (easy-menu-change): Add autoload cookie.
Richard M. Stallman <rms@gnu.org>
parents: 21807
diff changeset
429 ;;;###autoload
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
430 (defun easy-menu-change (path name items &optional before)
8085
c7eb887a1e78 (easy-menu-change): New function.
Richard M. Stallman <rms@gnu.org>
parents: 7614
diff changeset
431 "Change menu found at PATH as item NAME to contain ITEMS.
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
432 PATH is a list of strings for locating the menu that
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
433 should contain a submenu named NAME.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
434 ITEMS is a list of menu items, as in `easy-menu-define'.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
435 These items entirely replace the previous items in that submenu.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
436
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
437 If the menu located by PATH has no submenu named NAME, add one.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
438 If the optional argument BEFORE is present, add it just before
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
439 the submenu named BEFORE, otherwise add it at the end of the menu.
8085
c7eb887a1e78 (easy-menu-change): New function.
Richard M. Stallman <rms@gnu.org>
parents: 7614
diff changeset
440
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
441 Either call this from `menu-bar-update-hook' or use a menu filter,
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
442 to implement dynamic menus."
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
443 (easy-menu-add-item nil path (cons name items) before))
8085
c7eb887a1e78 (easy-menu-change): New function.
Richard M. Stallman <rms@gnu.org>
parents: 7614
diff changeset
444
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
445 ;; XEmacs needs the following two functions to add and remove menus.
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
446 ;; In Emacs this is done automatically when switching keymaps, so
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
447 ;; here easy-menu-remove is a noop and easy-menu-add only precalculates
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
448 ;; equivalent keybindings (if easy-menu-precalculate-equivalent-keybindings
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
449 ;; is on).
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
450 (defalias 'easy-menu-remove 'ignore)
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
451
22196
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
452 (defun easy-menu-add (menu &optional map)
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
453 "Maybe precalculate equivalent key bindings.
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
454 Do it if `easy-menu-precalculate-equivalent-keybindings' is on,"
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
455 (when easy-menu-precalculate-equivalent-keybindings
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
456 (if (and (symbolp menu) (not (keymapp menu)) (boundp menu))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
457 (setq menu (symbol-value menu)))
4f4d9c92ae33 (easy-menu-do-add-item): Small simplifications.
Richard M. Stallman <rms@gnu.org>
parents: 22033
diff changeset
458 (if (keymapp menu) (x-popup-menu nil menu))))
6600
f75ac1f3d99c *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 6542
diff changeset
459
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
460 (defun easy-menu-add-item (map path item &optional before)
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
461 "To the submenu of MAP with path PATH, add ITEM.
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
462
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
463 If an item with the same name is already present in this submenu,
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
464 then ITEM replaces it. Otherwise, ITEM is added to this submenu.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
465 In the latter case, ITEM is normally added at the end of the submenu.
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
466 However, if BEFORE is a string and there is an item in the submenu
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
467 with that name, then ITEM is added before that item.
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
468
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
469 MAP should normally be a keymap; nil stands for the global menu-bar keymap.
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
470 It can also be a symbol, which has earlier been used as the first
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
471 argument in a call to `easy-menu-define', or the value of such a symbol.
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
472
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
473 PATH is a list of strings for locating the submenu where ITEM is to be
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
474 added. If PATH is nil, MAP itself is used. Otherwise, the first
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
475 element should be the name of a submenu directly under MAP. This
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
476 submenu is then traversed recursively with the remaining elements of PATH.
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
477
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
478 ITEM is either defined as in `easy-menu-define' or a non-nil value returned
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
479 by `easy-menu-item-present-p' or `easy-menu-remove-item' or a menu defined
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
480 earlier by `easy-menu-define' or `easy-menu-create-menu'."
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
481 (setq map (easy-menu-get-map map path
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
482 (and (null map) (null path)
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
483 (stringp (car-safe item))
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
484 (car item))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
485 (if (and (consp item) (consp (cdr item)) (eq (cadr item) 'menu-item))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
486 ;; This is a value returned by `easy-menu-item-present-p' or
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
487 ;; `easy-menu-remove-item'.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
488 (easy-menu-define-key-intern map (car item) (cdr item) before)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
489 (if (or (keymapp item)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
490 (and (symbolp item) (keymapp (symbol-value item))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
491 ;; Item is a keymap, find the prompt string and use as item name.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
492 (let ((tail (easy-menu-get-map item nil)) name)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
493 (if (not (keymapp item)) (setq item tail))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
494 (while (and (null name) (consp (setq tail (cdr tail)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
495 (not (keymapp tail)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
496 (if (stringp (car tail)) (setq name (car tail)) ; Got a name.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
497 (setq tail (cdr tail))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
498 (setq item (cons name item))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
499 (easy-menu-do-add-item map item before)))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
500
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
501 (defun easy-menu-item-present-p (map path name)
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
502 "In submenu of MAP with path PATH, return true iff item NAME is present.
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
503 MAP and PATH are defined as in `easy-menu-add-item'.
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
504 NAME should be a string, the name of the element to be looked for."
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
505 (easy-menu-return-item (easy-menu-get-map map path) name))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
506
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
507 (defun easy-menu-remove-item (map path name)
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
508 "From submenu of MAP with path PATH remove item NAME.
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
509 MAP and PATH are defined as in `easy-menu-add-item'.
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
510 NAME should be a string, the name of the element to be removed."
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
511 (setq map (easy-menu-get-map map path))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
512 (let ((ret (easy-menu-return-item map name)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
513 (if ret (easy-menu-define-key-intern map name nil))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
514 ret))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
515
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
516 (defun easy-menu-return-item (menu name)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
517 "In menu MENU try to look for menu item with name NAME.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
518 If a menu item is found, return (NAME . item), otherwise return nil.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
519 If item is an old format item, a new format item is returned."
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
520 (let ((item (lookup-key menu (vector (easy-menu-intern name))))
23991
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
521 ret enable cache label)
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
522 (cond
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
523 ((stringp (car-safe item))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
524 ;; This is the old menu format. Convert it to new format.
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
525 (setq label (car item))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
526 (when (stringp (car (setq item (cdr item)))) ; Got help string
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
527 (setq ret (list :help (car item)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
528 (setq item (cdr item)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
529 (when (and (consp item) (consp (car item))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
530 (or (null (caar item)) (numberp (caar item))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
531 (setq cache (car item)) ; Got cache
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
532 (setq item (cdr item)))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
533 (and (symbolp item) (setq enable (get item 'menu-enable)) ; Got enable
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
534 (setq ret (cons :enable (cons enable ret))))
3dee93390da0 (easy-menu-define): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents: 23955
diff changeset
535 (if cache (setq ret (cons cache ret)))
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
536 (cons name (cons 'menu-enable (cons label (cons item ret)))))
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
537 (item ; (or (symbolp item) (keymapp item) (eq (car-safe item) 'menu-item))
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
538 (cons name item)) ; Keymap or new menu format
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
539 )))
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
540
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
541 (defun easy-menu-get-map-look-for-name (name submap)
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
542 (while (and submap (not (or (equal (car-safe (cdr-safe (car submap))) name)
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
543 (equal (car-safe (cdr-safe (cdr-safe (car submap)))) name))))
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
544 (setq submap (cdr submap)))
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
545 submap)
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
546
34520
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
547 ;; This should really be in keymap.c
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
548 (defun easy-menu-current-active-maps ()
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
549 (let ((maps (list (current-local-map) global-map)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
550 (dolist (minor minor-mode-map-alist)
34541
99cd5704dacb (easy-menu-current-active-maps): Test
Gerd Moellmann <gerd@gnu.org>
parents: 34520
diff changeset
551 (when (and (boundp (car minor))
99cd5704dacb (easy-menu-current-active-maps): Test
Gerd Moellmann <gerd@gnu.org>
parents: 34520
diff changeset
552 (symbol-value (car minor)))
99cd5704dacb (easy-menu-current-active-maps): Test
Gerd Moellmann <gerd@gnu.org>
parents: 34520
diff changeset
553 (push (cdr minor) maps)))
34520
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
554 (delq nil maps)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
555
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
556 (defun easy-menu-get-map (map path &optional to-modify)
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
557 "Return a sparse keymap in which to add or remove an item.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
558 MAP and PATH are as defined in `easy-menu-add-item'.
25224
31847deec8cb (easy-menu-get-map-look-for-name): New fn.
Karl Heuer <kwzh@gnu.org>
parents: 24987
diff changeset
559
30194
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
560 TO-MODIFY, if non-nil, is the name of the item the caller
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
561 wants to modify in the map that we return.
00cce196e38b Doc fixes.
Dave Love <fx@gnu.org>
parents: 30057
diff changeset
562 In some cases we use that to select between the local and global maps."
34520
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
563 (setq map
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
564 (catch 'found
42014
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
565 (let* ((key (vconcat (unless map '(menu-bar))
198c1186dee3 (easy-menu-intern): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 38431
diff changeset
566 (mapcar 'easy-menu-intern path)))
34520
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
567 (maps (mapcar (lambda (map)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
568 (setq map (lookup-key map key))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
569 (while (and (symbolp map) (keymapp map))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
570 (setq map (symbol-function map)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
571 map)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
572 (if map
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
573 (list (if (and (symbolp map)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
574 (not (keymapp map)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
575 (symbol-value map) map))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
576 (easy-menu-current-active-maps)))))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
577 ;; Prefer a map that already contains the to-be-modified entry.
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
578 (when to-modify
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
579 (dolist (map maps)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
580 (when (and map (not (integerp map))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
581 (easy-menu-get-map-look-for-name to-modify map))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
582 (throw 'found map))))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
583 ;; Use the first valid map.
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
584 (dolist (map maps)
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
585 (when (and map (not (integerp map)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
586 (throw 'found map)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
587 ;; Otherwise, make one up.
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
588 ;; Hardcoding current-local-map is lame, but it's difficult
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
589 ;; to know what the caller intended for us to do ;-(
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
590 (let* ((name (if path (format "%s" (car (reverse path)))))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
591 (newmap (make-sparse-keymap name)))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
592 (define-key (or map (current-local-map)) key
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
593 (if name (cons name newmap) newmap))
bea29075080b (easy-menu-define): Setup indentation.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 30209
diff changeset
594 newmap))))
23939
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
595 (or (keymapp map) (error "Malformed menu in easy-menu: (%s)" map))
8d7a5687c803 (easy-menu-get-map): If MENU is nil, use global menu-bar map.
Richard M. Stallman <rms@gnu.org>
parents: 22196
diff changeset
596 map)
20791
0c51c56d0a4f easy-menu-define): Use ` and , read-macros
Richard M. Stallman <rms@gnu.org>
parents: 19761
diff changeset
597
6529
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
598 (provide 'easymenu)
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
599
79c305d1edcb Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
600 ;;; easymenu.el ends here