(Fx_popup_menu): Add a vector of prefix keys for the panes.
[bpt/emacs.git] / src / xmenu.c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1986, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* X pop-up deck-of-cards menu facility for gnuemacs.
21 *
22 * Written by Jon Arnold and Roman Budzianowski
23 * Mods and rewrite by Robert Krawitz
24 *
25 */
26
27 #ifdef XDEBUG
28 #include <stdio.h>
29 #endif
30
31 /* On 4.3 this loses if it comes after xterm.h. */
32 #include <signal.h>
33 #include "config.h"
34 #include "lisp.h"
35 #include "frame.h"
36 #include "window.h"
37 #include "keyboard.h"
38 #include "blockinput.h"
39
40 /* This may include sys/types.h, and that somehow loses
41 if this is not done before the other system files. */
42 #include "xterm.h"
43
44 /* Load sys/types.h if not already loaded.
45 In some systems loading it twice is suicidal. */
46 #ifndef makedev
47 #include <sys/types.h>
48 #endif
49
50 #include "dispextern.h"
51
52 #ifdef HAVE_X11
53 #include "../oldXMenu/XMenu.h"
54 #else
55 #include <X/XMenu.h>
56 #endif
57
58 #define min(x,y) (((x) < (y)) ? (x) : (y))
59 #define max(x,y) (((x) > (y)) ? (x) : (y))
60
61 #define NUL 0
62
63 #ifndef TRUE
64 #define TRUE 1
65 #define FALSE 0
66 #endif TRUE
67
68 #ifdef HAVE_X11
69 extern Display *x_current_display;
70 #else
71 #define ButtonReleaseMask ButtonReleased
72 #endif /* not HAVE_X11 */
73
74 extern Lisp_Object Qmenu_enable;
75 Lisp_Object xmenu_show ();
76 extern int x_error_handler ();
77
78 /*************************************************************/
79
80 #if 0
81 /* Ignoring the args is easiest. */
82 xmenu_quit ()
83 {
84 error ("Unknown XMenu error");
85 }
86 #endif
87
88 DEFUN ("x-popup-menu",Fx_popup_menu, Sx_popup_menu, 1, 2, 0,
89 "Pop up a deck-of-cards menu and return user's selection.\n\
90 POSITION is a position specification. This is either a mouse button event\n\
91 or a list ((XOFFSET YOFFSET) WINDOW)\n\
92 where XOFFSET and YOFFSET are positions in characters from the top left\n\
93 corner of WINDOW's frame. (WINDOW may be a frame object instead of a window.)\n\
94 This controls the position of the center of the first line\n\
95 in the first pane of the menu, not the top left of the menu as a whole.\n\
96 \n\
97 MENU is a specifier for a menu. For the simplest case, MENU is a keymap.\n\
98 The menu items come from key bindings that have a menu string as well as\n\
99 a definition; actually, the \"definition\" in such a key binding looks like\n\
100 \(STRING . REAL-DEFINITION). To give the menu a title, put a string into\n\
101 the keymap as a top-level element.\n\n\
102 You can also use a list of keymaps as MENU.\n\
103 Then each keymap makes a separate pane.\n\
104 When MENU is a keymap or a list of keymaps, the return value\n\
105 is a list of events.\n\n\
106 Alternatively, you can specify a menu of multiple panes\n\
107 with a list of the form (TITLE PANE1 PANE2...),\n\
108 where each pane is a list of form (TITLE ITEM1 ITEM2...).\n\
109 Each ITEM is normally a cons cell (STRING . VALUE);\n\
110 but a string can appear as an item--that makes a nonselectable line\n\
111 in the menu.\n\
112 With this form of menu, the return value is VALUE from the chosen item.")
113 (position, menu)
114 Lisp_Object position, menu;
115 {
116 int number_of_panes;
117 Lisp_Object XMenu_return, keymap, tem;
118 int XMenu_xpos, XMenu_ypos;
119 char **menus;
120 char ***names;
121 int **enables;
122 Lisp_Object **obj_list;
123 Lisp_Object *prefixes;
124 int *items;
125 char *title;
126 char *error_name;
127 Lisp_Object ltitle, selection;
128 int i, j;
129 FRAME_PTR f;
130 Lisp_Object x, y, window;
131
132 /* Decode the first argument: find the window and the coordinates. */
133 tem = Fcar (position);
134 if (XTYPE (tem) == Lisp_Cons)
135 {
136 window = Fcar (Fcdr (position));
137 x = Fcar (tem);
138 y = Fcar (Fcdr (tem));
139 }
140 else
141 {
142 tem = Fcar (Fcdr (position)); /* EVENT_START (position) */
143 window = Fcar (tem); /* POSN_WINDOW (tem) */
144 tem = Fcar (Fcdr (Fcdr (tem))); /* POSN_WINDOW_POSN (tem) */
145 x = Fcar (tem);
146 y = Fcdr (tem);
147 }
148 CHECK_NUMBER (x, 0);
149 CHECK_NUMBER (y, 0);
150
151 if (XTYPE (window) == Lisp_Frame)
152 {
153 f = XFRAME (window);
154
155 XMenu_xpos = 0;
156 XMenu_ypos = 0;
157 }
158 else if (XTYPE (window) == Lisp_Window)
159 {
160 CHECK_LIVE_WINDOW (window, 0);
161 f = XFRAME (WINDOW_FRAME (XWINDOW (window)));
162
163 XMenu_xpos = FONT_WIDTH (f->display.x->font) * XWINDOW (window)->left;
164 XMenu_ypos = FONT_HEIGHT (f->display.x->font) * XWINDOW (window)->top;
165 }
166
167 XMenu_xpos += FONT_WIDTH (f->display.x->font) * XINT (x);
168 XMenu_ypos += FONT_HEIGHT (f->display.x->font) * XINT (y);
169
170 XMenu_xpos += f->display.x->left_pos;
171 XMenu_ypos += f->display.x->top_pos;
172
173 keymap = Fkeymapp (menu);
174 tem = Qnil;
175 if (XTYPE (menu) == Lisp_Cons)
176 tem = Fkeymapp (Fcar (menu));
177 if (!NILP (keymap))
178 {
179 /* We were given a keymap. Extract menu info from the keymap. */
180 Lisp_Object prompt;
181 keymap = get_keymap (menu);
182
183 /* Search for a string appearing directly as an element of the keymap.
184 That string is the title of the menu. */
185 prompt = map_prompt (keymap);
186 if (!NILP (prompt))
187 title = (char *) XSTRING (prompt)->data;
188
189 /* Extract the detailed info to make one pane. */
190 number_of_panes = keymap_panes (&obj_list, &menus, &names, &enables,
191 &items, &menu, 1);
192 /* The menu title seems to be ignored,
193 so put it in the pane title. */
194 if (menus[0] == 0)
195 menus[0] = title;
196 }
197 else if (!NILP (tem))
198 {
199 /* We were given a list of keymaps. */
200 Lisp_Object prompt;
201 int nmaps = XFASTINT (Flength (menu));
202 Lisp_Object *maps
203 = (Lisp_Object *) alloca (nmaps * sizeof (Lisp_Object));
204 int i;
205 title = 0;
206
207 /* The first keymap that has a prompt string
208 supplies the menu title. */
209 for (tem = menu, i = 0; XTYPE (tem) == Lisp_Cons; tem = Fcdr (tem))
210 {
211 maps[i++] = keymap = get_keymap (Fcar (tem));
212
213 prompt = map_prompt (keymap);
214 if (title == 0 && !NILP (prompt))
215 title = (char *) XSTRING (prompt)->data;
216 }
217
218 /* Extract the detailed info to make one pane. */
219 number_of_panes = keymap_panes (&obj_list, &menus, &names, &enables,
220 &items, &prefixes, maps, nmaps);
221 /* The menu title seems to be ignored,
222 so put it in the pane title. */
223 if (menus[0] == 0)
224 menus[0] = title;
225 }
226 else
227 {
228 /* We were given an old-fashioned menu. */
229 ltitle = Fcar (menu);
230 CHECK_STRING (ltitle, 1);
231 title = (char *) XSTRING (ltitle)->data;
232 prefixes = 0;
233 number_of_panes = list_of_panes (&obj_list, &menus, &names, &enables,
234 &items, Fcdr (menu));
235 }
236 #ifdef XDEBUG
237 fprintf (stderr, "Panes = %d\n", number_of_panes);
238 for (i = 0; i < number_of_panes; i++)
239 {
240 fprintf (stderr, "Pane %d has lines %d title %s\n",
241 i, items[i], menus[i]);
242 for (j = 0; j < items[i]; j++)
243 fprintf (stderr, " Item %d %s\n", j, names[i][j]);
244 }
245 #endif
246 BLOCK_INPUT;
247 {
248 Window root;
249 int root_x, root_y;
250 int dummy_int;
251 unsigned int dummy_uint;
252 Window dummy_window;
253
254 /* Figure out which root window F is on. */
255 XGetGeometry (x_current_display, FRAME_X_WINDOW (f), &root,
256 &dummy_int, &dummy_int, &dummy_uint, &dummy_uint,
257 &dummy_uint, &dummy_uint);
258
259 /* Translate the menu co-ordinates within f to menu co-ordinates
260 on that root window. */
261 if (! XTranslateCoordinates (x_current_display,
262 FRAME_X_WINDOW (f), root,
263 XMenu_xpos, XMenu_ypos, &root_x, &root_y,
264 &dummy_window))
265 /* But XGetGeometry said root was the root window of f's screen! */
266 abort ();
267
268 selection = xmenu_show (root, XMenu_xpos, XMenu_ypos, names, enables,
269 menus, prefixes, items, number_of_panes, obj_list,
270 title, &error_name);
271 }
272 UNBLOCK_INPUT;
273 /* fprintf (stderr, "selection = %x\n", selection); */
274 if (selection != NUL)
275 { /* selected something */
276 XMenu_return = selection;
277 }
278 else
279 { /* nothing selected */
280 XMenu_return = Qnil;
281 }
282 /* now free up the strings */
283 for (i = 0; i < number_of_panes; i++)
284 {
285 xfree (names[i]);
286 xfree (enables[i]);
287 xfree (obj_list[i]);
288 }
289 xfree (menus);
290 xfree (obj_list);
291 xfree (names);
292 xfree (enables);
293 xfree (items);
294 /* free (title); */
295 if (error_name) error (error_name);
296 return XMenu_return;
297 }
298
299 struct indices {
300 int pane;
301 int line;
302 };
303
304 Lisp_Object
305 xmenu_show (parent, startx, starty, line_list, enable_list, pane_list,
306 prefixes, line_cnt, pane_cnt, item_list, title, error)
307 Window parent;
308 int startx, starty; /* upper left corner position BROKEN */
309 char **line_list[]; /* list of strings for items */
310 int *enable_list[]; /* list of strings for items */
311 char *pane_list[]; /* list of pane titles */
312 Lisp_Object *prefixes; /* Prefix key for each pane */
313 char *title;
314 int pane_cnt; /* total number of panes */
315 Lisp_Object *item_list[]; /* All items */
316 int line_cnt[]; /* Lines in each pane */
317 char **error; /* Error returned */
318 {
319 XMenu *GXMenu;
320 int last, panes, selidx, lpane, status;
321 int lines, sofar;
322 Lisp_Object entry;
323 /* struct indices *datap, *datap_save; */
324 char *datap;
325 int ulx, uly, width, height;
326 int dispwidth, dispheight;
327
328 if (pane_cnt == 0)
329 return 0;
330
331 BLOCK_INPUT;
332 *error = (char *) 0; /* Initialize error pointer to null */
333 GXMenu = XMenuCreate (XDISPLAY parent, "emacs");
334 if (GXMenu == NUL)
335 {
336 *error = "Can't create menu";
337 UNBLOCK_INPUT;
338 return (0);
339 }
340
341 for (panes = 0, lines = 0; panes < pane_cnt;
342 lines += line_cnt[panes], panes++)
343 ;
344 /* datap = (struct indices *) xmalloc (lines * sizeof (struct indices)); */
345 /* datap = (char *) xmalloc (lines * sizeof (char));
346 datap_save = datap;*/
347
348 for (panes = 0, sofar = 0; panes < pane_cnt;
349 sofar += line_cnt[panes], panes++)
350 {
351 /* create all the necessary panes */
352 lpane = XMenuAddPane (XDISPLAY GXMenu, pane_list[panes], TRUE);
353 if (lpane == XM_FAILURE)
354 {
355 XMenuDestroy (XDISPLAY GXMenu);
356 *error = "Can't create pane";
357 UNBLOCK_INPUT;
358 return (0);
359 }
360 for (selidx = 0; selidx < line_cnt[panes]; selidx++)
361 {
362 /* add the selection stuff to the menus */
363 /* datap[selidx+sofar].pane = panes;
364 datap[selidx+sofar].line = selidx; */
365 if (XMenuAddSelection (XDISPLAY GXMenu, lpane, 0,
366 line_list[panes][selidx],
367 enable_list[panes][selidx])
368 == XM_FAILURE)
369 {
370 XMenuDestroy (XDISPLAY GXMenu);
371 /* free (datap); */
372 *error = "Can't add selection to menu";
373 /* error ("Can't add selection to menu"); */
374 UNBLOCK_INPUT;
375 return (0);
376 }
377 }
378 }
379 /* all set and ready to fly */
380 XMenuRecompute (XDISPLAY GXMenu);
381 dispwidth = DisplayWidth (x_current_display, XDefaultScreen (x_current_display));
382 dispheight = DisplayHeight (x_current_display, XDefaultScreen (x_current_display));
383 startx = min (startx, dispwidth);
384 starty = min (starty, dispheight);
385 startx = max (startx, 1);
386 starty = max (starty, 1);
387 XMenuLocate (XDISPLAY GXMenu, 0, 0, startx, starty,
388 &ulx, &uly, &width, &height);
389 if (ulx+width > dispwidth)
390 {
391 startx -= (ulx + width) - dispwidth;
392 ulx = dispwidth - width;
393 }
394 if (uly+height > dispheight)
395 {
396 starty -= (uly + height) - dispheight;
397 uly = dispheight - height;
398 }
399 if (ulx < 0) startx -= ulx;
400 if (uly < 0) starty -= uly;
401
402 XMenuSetFreeze (GXMenu, TRUE);
403 panes = selidx = 0;
404
405 status = XMenuActivate (XDISPLAY GXMenu, &panes, &selidx,
406 startx, starty, ButtonReleaseMask, &datap);
407 switch (status)
408 {
409 case XM_SUCCESS:
410 #ifdef XDEBUG
411 fprintf (stderr, "pane= %d line = %d\n", panes, selidx);
412 #endif
413 entry = item_list[panes][selidx];
414 if (prefixes != 0)
415 {
416 entry = Fcons (entry, Qnil);
417 if (!NILP (prefixes[panes]))
418 entry = Fcons (prefixes[panes], entry);
419 }
420 break;
421 case XM_FAILURE:
422 /* free (datap_save); */
423 XMenuDestroy (XDISPLAY GXMenu);
424 *error = "Can't activate menu";
425 /* error ("Can't activate menu"); */
426 case XM_IA_SELECT:
427 case XM_NO_SELECT:
428 entry = Qnil;
429 break;
430 }
431 XMenuDestroy (XDISPLAY GXMenu);
432 UNBLOCK_INPUT;
433 /* free (datap_save);*/
434 return (entry);
435 }
436
437 syms_of_xmenu ()
438 {
439 defsubr (&Sx_popup_menu);
440 }
441 \f
442 /* Construct the vectors that describe a menu
443 and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS.
444 Each of those four values is a vector indexed by pane number.
445 Return the number of panes.
446
447 KEYMAPS is a vector of keymaps. NMAPS gives the length of KEYMAPS. */
448
449 int
450 keymap_panes (vector, panes, names, enables, items, prefixes, keymaps, nmaps)
451 Lisp_Object ***vector; /* RETURN all menu objects */
452 char ***panes; /* RETURN pane names */
453 char ****names; /* RETURN all line names */
454 int ***enables; /* RETURN enable-flags of lines */
455 int **items; /* RETURN number of items per pane */
456 Lisp_Object **prefixes; /* RETURN vector of prefix keys, per pane */
457 Lisp_Object *keymaps;
458 int nmaps;
459 {
460 /* Number of panes we have made. */
461 int p = 0;
462 /* Number of panes we have space for. */
463 int npanes_allocated = nmaps;
464 int mapno;
465
466 if (npanes_allocated < 4)
467 npanes_allocated = 4;
468
469 /* Make space for an estimated number of panes. */
470 *vector = (Lisp_Object **) xmalloc (npanes_allocated * sizeof (Lisp_Object *));
471 *panes = (char **) xmalloc (npanes_allocated * sizeof (char *));
472 *items = (int *) xmalloc (npanes_allocated * sizeof (int));
473 *names = (char ***) xmalloc (npanes_allocated * sizeof (char **));
474 *enables = (int **) xmalloc (npanes_allocated * sizeof (int *));
475 *prefixes = (Lisp_Object *) xmalloc (npanes_allocated * sizeof (Lisp_Object));
476
477 /* Loop over the given keymaps, making a pane for each map.
478 But don't make a pane that is empty--ignore that map instead.
479 P is the number of panes we have made so far. */
480 for (mapno = 0; mapno < nmaps; mapno++)
481 single_keymap_panes (keymaps[mapno], panes, vector, names, enables, items,
482 prefixes, &p, &npanes_allocated, "");
483
484 /* Return the number of panes. */
485 return p;
486 }
487
488 /* This is a recursive subroutine of the previous function.
489 It handles one keymap, KEYMAP.
490 The other arguments are passed along
491 or point to local variables of the previous function. */
492
493 single_keymap_panes (keymap, panes, vector, names, enables, items, prefixes,
494 p_ptr, npanes_allocated_ptr, pane_name)
495 Lisp_Object keymap;
496 Lisp_Object ***vector; /* RETURN all menu objects */
497 char ***panes; /* RETURN pane names */
498 char ****names; /* RETURN all line names */
499 int ***enables; /* RETURN enable flags of lines */
500 int **items; /* RETURN number of items per pane */
501 Lisp_Object **prefixes; /* RETURN vector of prefix keys, per pane */
502 int *p_ptr;
503 int *npanes_allocated_ptr;
504 char *pane_name;
505 {
506 int i;
507 Lisp_Object pending_maps;
508 Lisp_Object tail, item, item1, item2, table;
509
510 pending_maps = Qnil;
511
512 /* Make sure we have room for another pane. */
513 if (*p_ptr == *npanes_allocated_ptr)
514 {
515 *npanes_allocated_ptr *= 2;
516
517 *vector
518 = (Lisp_Object **) xrealloc (*vector,
519 *npanes_allocated_ptr * sizeof (Lisp_Object *));
520 *panes
521 = (char **) xrealloc (*panes,
522 *npanes_allocated_ptr * sizeof (char *));
523 *items
524 = (int *) xrealloc (*items,
525 *npanes_allocated_ptr * sizeof (int));
526 *prefixes
527 = (Lisp_Object *) xrealloc (*prefixes,
528 (*npanes_allocated_ptr
529 * sizeof (Lisp_Object)));
530 *names
531 = (char ***) xrealloc (*names,
532 *npanes_allocated_ptr * sizeof (char **));
533 *enables
534 = (int **) xrealloc (*enables,
535 *npanes_allocated_ptr * sizeof (int *));
536 }
537
538 /* When a menu comes from keymaps, don't give names to the panes. */
539 (*panes)[*p_ptr] = pane_name;
540
541 /* Normally put nil as pane's prefix key.
542 Caller will override this if appropriate. */
543 (*prefixes)[*p_ptr] = Qnil;
544
545 /* Get the length of the list level of the keymap. */
546 i = XFASTINT (Flength (keymap));
547
548 /* Add in lengths of any arrays. */
549 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
550 if (XTYPE (XCONS (tail)->car) == Lisp_Vector)
551 i += XVECTOR (XCONS (tail)->car)->size;
552
553 /* Create vectors for the names and values of the items in the pane.
554 I is an upper bound for the number of items. */
555 (*vector)[*p_ptr] = (Lisp_Object *) xmalloc (i * sizeof (Lisp_Object));
556 (*names)[*p_ptr] = (char **) xmalloc (i * sizeof (char *));
557 (*enables)[*p_ptr] = (int *) xmalloc (i * sizeof (int));
558
559 /* I is now the index of the next unused slots. */
560 i = 0;
561 for (tail = keymap; XTYPE (tail) == Lisp_Cons; tail = XCONS (tail)->cdr)
562 {
563 /* Look at each key binding, and if it has a menu string,
564 make a menu item from it. */
565 item = XCONS (tail)->car;
566 if (XTYPE (item) == Lisp_Cons)
567 {
568 item1 = XCONS (item)->cdr;
569 if (XTYPE (item1) == Lisp_Cons)
570 {
571 item2 = XCONS (item1)->car;
572 if (XTYPE (item2) == Lisp_String)
573 {
574 Lisp_Object def, tem;
575 Lisp_Object enabled;
576
577 def = Fcdr (item1);
578 enabled = Qt;
579 if (XTYPE (def) == Lisp_Symbol)
580 {
581 /* No property, or nil, means enable.
582 Otherwise, enable if value is not nil. */
583 tem = Fget (def, Qmenu_enable);
584 if (!NILP (tem))
585 enabled = Feval (tem);
586 }
587 tem = Fkeymapp (def);
588 if (XSTRING (item2)->data[0] == '@' && !NILP (tem))
589 pending_maps = Fcons (Fcons (def, Fcons (item2, XCONS (item)->car)),
590 pending_maps);
591 else
592 {
593 (*names)[*p_ptr][i] = (char *) XSTRING (item2)->data;
594 /* The menu item "value" is the key bound here. */
595 (*vector)[*p_ptr][i] = XCONS (item)->car;
596 (*enables)[*p_ptr][i]
597 = (NILP (def) ? -1 : !NILP (enabled) ? 1 : 0);
598 i++;
599 }
600 }
601 }
602 }
603 else if (XTYPE (item) == Lisp_Vector)
604 {
605 /* Loop over the char values represented in the vector. */
606 int len = XVECTOR (item)->size;
607 int c;
608 for (c = 0; c < len; c++)
609 {
610 Lisp_Object character;
611 XFASTINT (character) = c;
612 item1 = XVECTOR (item)->contents[c];
613 if (XTYPE (item1) == Lisp_Cons)
614 {
615 item2 = XCONS (item1)->car;
616 if (XTYPE (item2) == Lisp_String)
617 {
618 Lisp_Object tem;
619 Lisp_Object def;
620 Lisp_Object enabled;
621
622 def = Fcdr (item1);
623 enabled = Qt;
624 if (XTYPE (def) == Lisp_Symbol)
625 {
626 tem = Fget (def, Qmenu_enable);
627 /* No property, or nil, means enable.
628 Otherwise, enable if value is not nil. */
629 if (!NILP (tem))
630 enabled = Feval (tem);
631 }
632
633 tem = Fkeymapp (def);
634 if (XSTRING (item2)->data[0] == '@' && !NILP (tem))
635 pending_maps = Fcons (Fcons (def, Fcons (item2, character)),
636 pending_maps);
637 else
638 {
639 (*names)[*p_ptr][i] = (char *) XSTRING (item2)->data;
640 /* The menu item "value" is the key bound here. */
641 (*vector)[*p_ptr][i] = character;
642 (*enables)[*p_ptr][i]
643 = (NILP (def) ? -1 : !NILP (enabled) ? 1 : 0);
644 i++;
645 }
646 }
647 }
648 }
649 }
650 }
651 /* Record the number of items in the pane. */
652 (*items)[*p_ptr] = i;
653
654 /* If we just made an empty pane, get rid of it. */
655 if (i == 0)
656 {
657 xfree ((*vector)[*p_ptr]);
658 xfree ((*names)[*p_ptr]);
659 xfree ((*enables)[*p_ptr]);
660 }
661 /* Otherwise, advance past it. */
662 else
663 (*p_ptr)++;
664
665 /* Process now any submenus which want to be panes at this level. */
666 while (!NILP (pending_maps))
667 {
668 Lisp_Object elt, eltcdr;
669 int panenum = *p_ptr;
670 elt = Fcar (pending_maps);
671 eltcdr = XCONS (elt)->cdr;
672 single_keymap_panes (Fcar (elt), panes, vector, names, enables, items,
673 prefixes, p_ptr, npanes_allocated_ptr,
674 /* Add 1 to discard the @. */
675 (char *) XSTRING (XCONS (eltcdr)->car)->data + 1);
676 (*prefixes)[panenum] = XCONS (eltcdr)->cdr;
677 pending_maps = Fcdr (pending_maps);
678 }
679 }
680 \f
681 /* Construct the vectors that describe a menu
682 and store them in *VECTOR, *PANES, *NAMES, *ENABLES and *ITEMS.
683 Each of those four values is a vector indexed by pane number.
684 Return the number of panes.
685
686 MENU is the argument that was given to Fx_popup_menu. */
687
688 int
689 list_of_panes (vector, panes, names, enables, items, menu)
690 Lisp_Object ***vector; /* RETURN all menu objects */
691 char ***panes; /* RETURN pane names */
692 char ****names; /* RETURN all line names */
693 int ***enables; /* RETURN enable flags of lines */
694 int **items; /* RETURN number of items per pane */
695 Lisp_Object menu;
696 {
697 Lisp_Object tail, item, item1;
698 int i;
699
700 if (XTYPE (menu) != Lisp_Cons) menu = wrong_type_argument (Qlistp, menu);
701
702 i = XFASTINT (Flength (menu));
703
704 *vector = (Lisp_Object **) xmalloc (i * sizeof (Lisp_Object *));
705 *panes = (char **) xmalloc (i * sizeof (char *));
706 *items = (int *) xmalloc (i * sizeof (int));
707 *names = (char ***) xmalloc (i * sizeof (char **));
708 *enables = (int **) xmalloc (i * sizeof (int *));
709
710 for (i = 0, tail = menu; !NILP (tail); tail = Fcdr (tail), i++)
711 {
712 item = Fcdr (Fcar (tail));
713 if (XTYPE (item) != Lisp_Cons) (void) wrong_type_argument (Qlistp, item);
714 #ifdef XDEBUG
715 fprintf (stderr, "list_of_panes check tail, i=%d\n", i);
716 #endif
717 item1 = Fcar (Fcar (tail));
718 CHECK_STRING (item1, 1);
719 #ifdef XDEBUG
720 fprintf (stderr, "list_of_panes check pane, i=%d%s\n", i,
721 XSTRING (item1)->data);
722 #endif
723 (*panes)[i] = (char *) XSTRING (item1)->data;
724 (*items)[i] = list_of_items ((*vector)+i, (*names)+i, (*enables)+i, item);
725 /* (*panes)[i] = (char *) xmalloc ((XSTRING (item1)->size)+1);
726 bcopy (XSTRING (item1)->data, (*panes)[i], XSTRING (item1)->size + 1)
727 ; */
728 }
729 return i;
730 }
731 \f
732 /* Construct the lists of values and names for a single pane, from the
733 alist PANE. Put them in *VECTOR and *NAMES. Put the enable flags
734 int *ENABLES. Return the number of items. */
735
736 int
737 list_of_items (vector, names, enables, pane)
738 Lisp_Object **vector; /* RETURN menu "objects" */
739 char ***names; /* RETURN line names */
740 int **enables; /* RETURN enable flags of lines */
741 Lisp_Object pane;
742 {
743 Lisp_Object tail, item, item1;
744 int i;
745
746 if (XTYPE (pane) != Lisp_Cons) pane = wrong_type_argument (Qlistp, pane);
747
748 i = XFASTINT (Flength (pane));
749
750 *vector = (Lisp_Object *) xmalloc (i * sizeof (Lisp_Object));
751 *names = (char **) xmalloc (i * sizeof (char *));
752 *enables = (int *) xmalloc (i * sizeof (int));
753
754 for (i = 0, tail = pane; !NILP (tail); tail = Fcdr (tail), i++)
755 {
756 item = Fcar (tail);
757 if (STRINGP (item))
758 {
759 (*vector)[i] = Qnil;
760 (*names)[i] = (char *) XSTRING (item)->data;
761 (*enables)[i] = -1;
762 }
763 else
764 {
765 CHECK_CONS (item, 0);
766 (*vector)[i] = Fcdr (item);
767 item1 = Fcar (item);
768 CHECK_STRING (item1, 1);
769 (*names)[i] = (char *) XSTRING (item1)->data;
770 (*enables)[i] = 1;
771 }
772 }
773 return i;
774 }