-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.lua
executable file
·514 lines (374 loc) · 10.4 KB
/
main.lua
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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 NewApp(source, name, exec)
local app={}
app.type="app"
app.name=name
app.termapp=false
app.ignore=false
app.query=false
app.fileselect=false
app.source=source
app.exec=exec
return app
end
function NewGroup(name)
if group_configs[name]==nil
then
group_configs[name]={}
group_configs[name].ignore=false
group_configs[name].icons=""
end
return group_configs[name]
end
function MenuAddGroup(group)
local conf, parent
if strutil.strlen(group) ==0 then return nil end
conf=group_configs[group]
if conf ~=nil and conf.parent ~=nil
then
parent=MenuAddGroup(conf.parent)
else
parent=menu_config
end
if parent[group] == nil
then
parent[group]={}
parent[group].type="group"
parent[group].name=group
-- parent[group].size=0
end
return parent[group]
end
function MenuAddItem(menu_name, item)
local group
group=MenuAddGroup(menu_name)
if group ~= nil then table.insert(group, item) end
end
function ProcessImpliedApps(implied_apps)
local toks, item, config
toks=strutil.TOKENIZER(implied_apps, ",")
item=toks:next()
while item ~= nil
do
config=app_configs[string.lower(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=group_configs[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()
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 SelectFaves(faves)
local toks, item, name
toks=strutil.TOKENIZER(faves, ",")
item=toks:next()
while item
do
name=string.lower(item)
if app_configs[name] ~= nil then table.insert(faves_config, app_configs[name]) 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(unsorted)
local sorted={}
local name, item
if unsorted.type==nil then return unsorted end
sorted.name=unsorted.name
sorted.type=unsorted.type
for name,item in pairs(unsorted)
do
if item.type ~= nil
then
if item.type=="group"
then
item=SortMenu(item)
end
table.insert(sorted, item)
end
end
table.sort(sorted, SortMenuCompare)
return sorted
end
function IconPathAdd(prefix)
local fslist, dir
local subdirs={"128x128","64x64","48x48","32x32", "16x16"}
settings.icon_path=settings.icon_path..prefix.."/share/icons:"
settings.icon_path=settings.icon_path .. prefix.. "/share/pixmaps/:"
fslist=filesys.GLOB(prefix.."/share/icons/*")
dir=fslist:next()
while dir ~= nil
do
if fslist:info().type == "directory"
then
for i,sub in ipairs(subdirs)
do
settings.icon_path=settings.icon_path .. dir .. "/" .. sub .."/:"
settings.icon_path=settings.icon_path .. dir .. "/" .. sub .."/apps/:"
settings.icon_path=settings.icon_path .. dir .. "/" .. sub .."/places/:"
settings.icon_path=settings.icon_path .. dir .. "/" .. sub .."/devices/:"
end
end
dir=fslist:next()
end
end
--scan a directory for applications
function ScanDir(dir)
local config, files, curr, name
files=filesys.GLOB(dir.."/*")
curr=files:next()
while curr ~= nil
do
--does the application exist in the list of applications loaded from the config file?
name=string.lower(filesys.basename(curr))
config=app_configs[name]
if config ~= nil and config.group ~=nil
then
-- is it a dialog app, like XDialog, Zenity or Qarma? If so then add it to the dialog_apps table
if config.group == "DialogApp"
then
--do nothing for now, one day we hope to automagically find qarma, zenity, xdialog etc
else
--it's a normal app, so add it to it's menu group
MenuAddItem(config.group, config)
if config.implies ~= nil then ProcessImpliedApps(config.implies) end
end
end
curr=files:next()
end
end
function ScanDirectoriesInPath()
local toks, dir, prefix, PATH
PATH=process.getenv("PATH")
toks=strutil.TOKENIZER(PATH,":")
dir=toks:next()
while dir ~= nil
do
ScanDir(dir)
prefix=string.gsub(dir, "/bin$", "")
LoadDesktopFiles(prefix)
IconPathAdd(prefix)
dir=toks:next()
end
end
function CopyGroup(in_group, out_group)
local name, item
for i,item in ipairs(in_group)
do
if item.type ~= nil
then
table.insert(out_group, item)
end
end
end
--parent is the group that items will be moved to if
--this group is too small. For top level items this will
--either be the 'Misc' group, or it could be nil
function PostProcessGroup(parent, group, min_size)
local name, item
--first go through subitems of this group, possibly moving stuff out of them into this group
for name,item in pairs(group)
do
if item.type=="group"
then
if PostProcessGroup(group, item, settings.submenu_size) == false then group[name]=nil end
end
end
--if parent is nil then don't try moving/deleting this group, otherwise, if the group is too
--small, move it into the parent
if parent ~= nil and #group < min_size
then
CopyGroup(group, parent)
-- we don't try to delete the group from the parent here,
-- because the parent is the group we are moving things to, which
-- is not always the real parent of the group we are considering
-- so we return false, and let calling function delete the group
return false
end
return true
end
function PostProcessItems()
local name, item, misc
-- create misc group if needed, else leave as nil
if settings.misc_group > 0
then
misc={}
misc.type="group"
misc.name="Misc"
end
for name,item in pairs(menu_config)
do
if item.type=="group"
then
if PostProcessGroup(misc, item, settings.misc_group) == false then menu_config[name]=nil end
end
end
--if we created a misc group, and it has items in it, then add it to the menu
if misc ~= nil and #misc > 0 then table.insert(menu_config, 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
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.type="group"
menu_config.name="RootMenu"
ParseCommandLine(arg)
LoadConfig()
ScanDirectoriesInPath()
IconsLoad(settings.icon_path)
LoadDesktopFiles(process.getenv("HOME").."/.local/")
PostProcessItems()
SelectFaves(settings.faves)
sorted=SortMenu(menu_config)
if settings.output==""
then
io.stderr:write( "ERROR: no output specified.\n")
else
OutputWindowManagerFiles()
end