=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/menu.c,v retrieving revision 1.7 retrieving revision 1.8 diff -c -r1.7 -r1.8 *** src/usr.bin/tmux/menu.c 2019/05/26 18:19:52 1.7 --- src/usr.bin/tmux/menu.c 2019/05/28 07:18:42 1.8 *************** *** 1,4 **** ! /* $OpenBSD: menu.c,v 1.7 2019/05/26 18:19:52 nicm Exp $ */ /* * Copyright (c) 2019 Nicholas Marriott --- 1,4 ---- ! /* $OpenBSD: menu.c,v 1.8 2019/05/28 07:18:42 nicm Exp $ */ /* * Copyright (c) 2019 Nicholas Marriott *************** *** 40,88 **** void *data; }; ! static void ! menu_add_item(struct menu *menu, struct menu_item *item, struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs) { struct menu_item *new_item; ! const char *key; ! char *name; u_int width; menu->items = xreallocarray(menu->items, menu->count + 1, sizeof *menu->items); new_item = &menu->items[menu->count++]; memset(new_item, 0, sizeof *new_item); ! if (item == NULL || *item->name == '\0') /* horizontal line */ return; ! if (fs != NULL) { ! name = format_single(qitem, item->name, c, fs->s, fs->wl, ! fs->wp); ! } else ! name = format_single(qitem, item->name, c, NULL, NULL, NULL); ! if (*name == '\0') { /* no item if empty after format expanded */ menu->count--; return; } ! if (item->key != KEYC_UNKNOWN) { key = key_string_lookup_key(item->key); ! xasprintf(&new_item->name, "%s#[default] #[align=right](%s)", ! name, key); } else ! xasprintf(&new_item->name, "%s", name); ! free(name); ! if (item->command != NULL) { ! if (fs != NULL) { ! new_item->command = format_single(qitem, item->command, ! c, fs->s, fs->wl, fs->wp); ! } else { ! new_item->command = format_single(qitem, item->command, ! c, NULL, NULL, NULL); ! } } else ! new_item->command = NULL; new_item->key = item->key; width = format_width(new_item->name); --- 40,102 ---- void *data; }; ! void ! menu_add_items(struct menu *menu, const struct menu_item *items, struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs) { + const struct menu_item *loop; + + for (loop = items; loop->name != NULL; loop++) + menu_add_item(menu, loop, qitem, c, fs); + } + + void + menu_add_item(struct menu *menu, const struct menu_item *item, + struct cmdq_item *qitem, struct client *c, struct cmd_find_state *fs) + { struct menu_item *new_item; ! const char *key, *cmd; ! char *s, *name; u_int width; + int line; + line = (item == NULL || item->name == NULL || *item->name == '\0'); + if (line && menu->count == 0) + return; + menu->items = xreallocarray(menu->items, menu->count + 1, sizeof *menu->items); new_item = &menu->items[menu->count++]; memset(new_item, 0, sizeof *new_item); ! if (line) return; ! ! if (fs != NULL) ! s = format_single(qitem, item->name, c, fs->s, fs->wl, fs->wp); ! else ! s = format_single(qitem, item->name, c, NULL, NULL, NULL); ! if (*s == '\0') { /* no item if empty after format expanded */ menu->count--; return; } ! if (item->key != KEYC_UNKNOWN && item->key != KEYC_NONE) { key = key_string_lookup_key(item->key); ! xasprintf(&name, "%s#[default] #[align=right](%s)", s, key); } else ! xasprintf(&name, "%s", s); ! new_item->name = name; ! free(s); ! cmd = item->command; ! if (cmd != NULL) { ! if (fs != NULL) ! s = format_single(qitem, cmd, c, fs->s, fs->wl, fs->wp); ! else ! s = format_single(qitem, cmd, c, NULL, NULL, NULL); } else ! s = NULL; ! new_item->command = s; new_item->key = item->key; width = format_width(new_item->name); *************** *** 90,145 **** menu->width = width; } - static void - menu_parse_item(struct menu *menu, const char *s, struct cmdq_item *qitem, - struct client *c, struct cmd_find_state *fs) - { - char *copy, *first; - const char *second, *third; - struct menu_item item; - - first = copy = xstrdup(s); - if ((second = format_skip(first, ",")) != NULL) { - *(char *)second++ = '\0'; - if ((third = format_skip(second, ",")) != NULL) { - *(char *)third++ = '\0'; - - item.name = first; - item.command = (char *)third; - item.key = key_string_lookup_string(second); - menu_add_item(menu, &item, qitem, c, fs); - } - } - free(copy); - } - struct menu * ! menu_create(const char *s, struct cmdq_item *qitem, struct client *c, ! struct cmd_find_state *fs, const char *title) { struct menu *menu; - char *copy, *string, *next; - if (*s == '\0') - return (NULL); - menu = xcalloc(1, sizeof *menu); menu->title = xstrdup(title); - copy = string = xstrdup(s); - do { - next = (char *)format_skip(string, "|"); - if (next != NULL) - *next++ = '\0'; - if (*string == '\0') { - if (menu->count != 0) - menu_add_item(menu, NULL, qitem, c, fs); - } else - menu_parse_item(menu, string, qitem, c, fs); - string = next; - } while (next != NULL); - free(copy); - return (menu); } --- 104,117 ---- menu->width = width; } struct menu * ! menu_create(const char *title) { struct menu *menu; menu = xcalloc(1, sizeof *menu); menu->title = xstrdup(title); return (menu); } *************** *** 149,160 **** u_int i; for (i = 0; i < menu->count; i++) { ! free(menu->items[i].name); ! free(menu->items[i].command); } free(menu->items); ! free(menu->title); free(menu); } --- 121,132 ---- u_int i; for (i = 0; i < menu->count; i++) { ! free((void *)menu->items[i].name); ! free((void *)menu->items[i].command); } free(menu->items); ! free((void *)menu->title); free(menu); }