-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
executable file
·392 lines (273 loc) · 7.98 KB
/
main.lua
File metadata and controls
executable file
·392 lines (273 loc) · 7.98 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
function ProcessAppOverrides(app)
local name, config, item
config=overrides_config[app.name]
if config == nil then config=overrides_config[app.exec] end
if config == nil
then
for name,item in pairs(overrides_config)
do
if strutil.pmatch(name, app.name) == true
then
config=item
end
end
end
if config ~= nil
then
if strutil.strlen(config.group) > 0 then app.group=config.group end
if strutil.strlen(config.icon) > 0 then app.icon=config.icon end
if config.termapp == true then app.termapp=true end
if config.ignore == true then app.ignore=true end
if config.query == true then app.query=true end
end
end
function MenuAddGroup(group_name)
-- this function calls itself recursively, so
-- make sure everything is declared as local
local conf, parent, new_group, id
if strutil.strlen(group_name) ==0 then return nil end
id=string.lower(group_name)
conf=FindGroup(id)
if conf ~=nil and conf.parent ~=nil
then
parent=MenuAddGroup(conf.parent.name)
else
parent=menu_config
end
if parent.items[id] == nil
then
new_group=NewGroup(group_name)
--new_group.parent=parent
parent.items[id]=new_group
end
return parent.items[id]
end
function MenuAddItem(menu_name, item)
local group, existing
group=MenuAddGroup(menu_name)
if group ~= nil
then
item.parent=group
existing = group.items[item.name]
if existing == nil then group.items[item.name]=item end
end
end
function ProcessImpliedApps(implied_apps)
local toks, item, config
toks=strutil.TOKENIZER(implied_apps, ",")
item=toks:next()
while item ~= nil
do
config=AppConfigFind(item)
if config ~= nil and config.group ~=nil then MenuAddItem(config.group, config) end
item=toks:next()
end
end
function GroupInfoFind(group)
local conf, name
local icon=nil
conf=FindGroup(group.name)
if conf ~= nil
then
if strutil.strlen(conf.name) > 0 then name=conf.name end
if settings.find_icons == true
then
if strutil.strlen(conf.icons) > 0 then conf.icon=IconFindFromList(conf.icons) end
if conf.icon ~= nil then icon=conf.icon end
end
end
if name == nil then name=group.name end
if icon == nil
then
if settings.find_icons == true
then
icon=IconFindFromList("folder")
else
icon=""
end
end
return name, icon
end
function OpenOutputFile(Path)
local S
-- don't try to create or rename stdout!
if Path ~= "-"
then
filesys.mkdirPath(Path)
filesys.rename(Path, Path.."-")
end
S=stream.STREAM(Path, "w")
if S==nil
then
io.stderr:write( "ERROR: Failed to open output '"..Path.."'\n")
else
io.stderr:write( "writing to: "..Path.."\n")
end
return(S)
end
function OutputWindowManagerFiles(sorted)
local toks, item
toks=strutil.TOKENIZER(settings.output, ",")
item=toks:next()
while item ~= nil
do
if strutil.strlen(item) > 0
then
io.stderr:write( "output: " .. item.."\n")
if item=="blackbox"
then
Blackbox_MenuWrite(sorted, process.getenv("HOME") .. "/.blackbox/menu")
elseif item=="fluxbox"
then
Blackbox_MenuWrite(sorted, process.getenv("HOME") .. "/.fluxbox/menu")
elseif item=="openbox"
then
Openbox_MenuWrite(sorted, process.getenv("HOME") .. "/.config/openbox/menu.xml")
elseif item=="icewm"
then
IceWM_MenuWrite(sorted, process.getenv("HOME").."/.icewm/menu")
elseif item=="jwm"
then
JWM_MenuWrite(sorted, process.getenv("HOME").."/.menu.jwm")
elseif item=="fvwm"
then
FVWM_MenuWrite(sorted, process.getenv("HOME").."/.fvwm/fvwm.menu")
elseif item=="pekwm"
then
PekWM_MenuWrite(sorted, process.getenv("HOME").."/.pekwm/menu")
elseif item=="pwm"
then
PWM_MenuWrite(sorted, process.getenv("HOME").."/.pwm/rootmenu.conf")
elseif item=="mlvwm"
then
MLVWM_MenuWrite(sorted, process.getenv("HOME").."/.menu.mlvwm")
elseif item=="twm" or item=="vtwm" or item=="ctwm" or item=="mwm"
then
TWM_MenuWrite(sorted, process.getenv("HOME").."/.menu."..item)
elseif item=="moonwm"
then
XMenu_MenuWrite(sorted, process.getenv("HOME").."/.config/moonwm/favorites.xmenu")
elseif item=="xmenu"
then
XMenu_MenuWrite(sorted, process.getenv("HOME").."/.xmenu.menu")
elseif item=="ctrlmenu"
then
CtrlMenu_MenuWrite(sorted, process.getenv("HOME").."/.ctrlmenu.menu")
elseif item=="stdout:blackbox" or item=="stdout:fluxbox" then Blackbox_MenuWrite(sorted, "-")
elseif item=="stdout:icewm" then IceWM_MenuWrite(sorted, "-")
elseif item=="stdout:pekwm" then PekWM_MenuWrite(sorted, "-")
elseif item=="stdout:jwm" then JWM_MenuWrite(sorted, "-")
elseif item=="stdout:fvwm" then FVWM_MenuWrite(sorted, "-")
elseif item=="stdout:twm" then TWM_MenuWrite(sorted, "-")
elseif item=="stdout:vtwm" then TWM_MenuWrite(sorted, "-")
elseif item=="stdout:ctwm" then TWM_MenuWrite(sorted, "-")
elseif item=="stdout:moonwm" then XMenu_MenuWrite(sorted, "-")
elseif item=="stdout:xmenu" then XMenu_MenuWrite(sorted, "-")
elseif item=="stdout:ctrlmenu" then CtrlMenu_MenuWrite(sorted, "-")
end
end
item=toks:next()
end
end
function SortMenuCompare(m1, m2)
if m1.type=="group" and m2.type=="app" then return true end
if m1.type=="app" and m2.type=="group" then return false end
return m1.name < m2.name
end
function SortMenu(menu_item)
local sorted={}
local name, item
if menu_item.type==nil then return menu_item end
sorted={}
for name,item in pairs(menu_item.items)
do
if item.type ~= nil
then
if item.type=="group"
then
SortMenu(item)
end
table.insert(sorted, item)
end
end
table.sort(sorted, SortMenuCompare)
menu_item.items=sorted
end
function CollapseGroups(parent)
local name, item, count
local collapsed=false
for name,item in pairs(parent.items)
do
if item.parent==nil then item.parent=parent end
if item.type == "group"
then
-- we must postprocess subitems first, because subgroups might be collapsed, adding
-- items to this group
count=GroupCountItems(item)
if item.parent ~= nil and parent.name ~= "rootmenu" and count < settings.min_size
then
CopyGroupItems(item, item.parent)
item.parent.items[item.name]=nil
parent.items[item.name]=nil
collapsed=true
end
end
end
return collapsed
end
function PostProcessItems(parent)
local name, item, misc, more
-- create misc group if needed, else leave as nil
if settings.misc_group > 0 then misc=NewGroup("Misc") end
more=true
while more == true do more=CollapseGroups(parent) end
-- we must post process subgroups first, in case they collpase into
-- our group and thus mess up our list of items, causing us to miss some
for name,item in pairs(parent.items)
do
if item.parent==nil then item.parent=parent end
item.icon=AppIconFind(item, settings.icon_path)
if strutil.strlen(item.title) == 0 then item.title=item.name end
if item.type == "group" then PostProcessItems(item) end
end
--if we created a misc group, and it has items in it, then add it to the menu
if GroupCountItems(misc) > 0 then menu_config[misc.name]=misc end
end
--[[
'main' starts here ***********************************
]]--
settings.config = process.getenv("HOME") .. "/.config/menubuilder.conf"
settings.config = settings.config .. ":" .. process.getenv("HOME") .. "/.config/menubuilder/*.conf"
settings.config = settings.config .. ":" .. process.getenv("HOME") .. "/.menubuilder.conf"
settings.config = settings.config .. ":/etc/menubuilder.conf"
settings.term="xterm"
settings.output=""
settings.find_icons=true
settings.icon_path=process.getenv("ICON_PATH")
settings.submenu_size=0
settings.misc_group=0
settings.min_size=2
if settings.icon_path==nil
then
settings.icon_path=""
else
settings.icon_path=settings.icon_path .. ":"
end
-- don't allow this to be nil, or we can't append to it
if settings.icon_path == nil then settings.icon_path="" end
menu_config=NewGroup("RootMenu")
faves_config=NewGroup("Favorites")
ParseCommandLine(arg)
LoadConfig()
LoadDesktopFiles(process.getenv("HOME") .. "/.local/")
ScanDirectoriesInPath()
IconsLoad(settings.icon_path)
PostProcessItems(menu_config)
SelectFaves(settings.faves)
if settings.output==""
then
io.stderr:write( "ERROR: no output specified.\n")
else
SortMenu(menu_config)
OutputWindowManagerFiles(menu_config)
end