Menu

[7c54b3]: / joe / mmenu.c  Maximize  Restore  History

Download this file

134 lines (109 with data), 2.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Menu of macros
* Copyright
* (C) 1992 Joseph H. Allen
*
* This file is part of JOE (Joe's Own Editor)
*/
#include "types.h"
static B *menuhist = NULL; /* Menu history */
static struct rc_menu *menus; /* All menus */
static char **smenus = NULL; /* Completion list */
/* Find a menu */
static struct rc_menu *find_menu(char *s)
{
struct rc_menu *m;
for (m = menus; m; m = m->next)
if (!zcmp(m->name, s))
break;
return m;
}
/* Create a new menu, push it on menus list */
struct rc_menu *create_menu(char *name, MACRO *bs)
{
struct rc_menu *menu = find_menu(name);
if (menu)
return menu;
menu = (struct rc_menu *)joe_malloc(SIZEOF(struct rc_menu));
menu->name = zdup(name);
menu->next = menus;
menus = menu;
menu->last_position = 0;
menu->size = 0;
menu->entries = 0;
menu->backs = bs;
return menu;
}
/* Add an entry to a menu */
void add_menu_entry(struct rc_menu *menu, char *entry_name, MACRO *m)
{
struct rc_menu_entry *e = (struct rc_menu_entry *)joe_malloc(SIZEOF(struct rc_menu_entry));
e->m = m;
e->name = zdup(entry_name);
++menu->size;
if (!menu->entries) {
menu->entries = (struct rc_menu_entry **)joe_malloc(menu->size * SIZEOF(struct rc_menu_entry *));
} else {
menu->entries = (struct rc_menu_entry **)joe_realloc(menu->entries, menu->size * SIZEOF(struct rc_menu_entry *));
}
menu->entries[menu->size - 1] = e;
}
int menu_flg; /* Record of key used to select menu entry */
/* Display macro menu if it exists */
int display_menu(BW *bw, struct rc_menu *menu)
{
char **s = vamk(20);
int x;
for (x = 0; x != menu->size; ++x) {
s = vaadd(s, stagen(NULL, bw, menu->entries[x]->name, ' '));
}
x = choose(bw->parent, bw->parent, s, &menu->last_position);
if (x == -1) {
/* Abort */
return -1;
} else if (x == 3) {
/* Backspace */
if (menu->backs)
return exmacro(menu->backs, 1, NO_MORE_DATA);
else
return 0;
}
menu_flg = x;
return exmacro(menu->entries[menu->last_position]->m, 1, NO_MORE_DATA);
}
/* Build array of menu name from list of menus for completion */
static char **getmenus(void)
{
char **s = vaensure(NULL, 20);
struct rc_menu *m;
vaperm(s);
for (m = menus; m; m = m->next)
s = vaadd(s, vsncpy(NULL, 0, sz(m->name)));
vasort(av(s));
return s;
}
/* When user hits tab */
static int menucmplt(BW *bw, int k)
{
if (!smenus)
smenus = getmenus();
return simple_cmplt(bw,smenus);
}
/* Menu of macros command: prompt for a menu to bring up */
int umenu(W *w, int k)
{
char *s = ask(w, joe_gettext(_("Menu: ")), &menuhist, "menu", menucmplt, locale_map, 0, 0, NULL);
struct rc_menu *menu;
BW *bw;
WIND_BW(bw, w);
if (!s)
return -1;
menu = find_menu(s);
if (!menu) {
msgnw(w, joe_gettext(_("No such menu")));
return -1;
} else {
bw->b->o.readonly = bw->o.readonly = bw->b->rdonly;
return display_menu(bw, menu);
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.