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