File: mod_menu.lua

package info (click to toggle)
ion3 20050502-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,188 kB
  • ctags: 5,450
  • sloc: ansic: 39,916; perl: 2,445; sh: 1,229; makefile: 639; ruby: 90
file content (395 lines) | stat: -rw-r--r-- 10,270 bytes parent folder | download
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
--
-- ion/mod_menu/mod_menu.lua -- Helper functions for defining menus.
-- 
-- Copyright (c) Tuomo Valkonen 2004-2005.
--
-- Ion is free software; you can redistribute it and/or modify it under
-- the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--

-- This is a slight abuse of the _LOADED variable perhaps, but library-like 
-- packages should handle checking if they're loaded instead of confusing 
-- the user with require/include differences.
if _LOADED["mod_menu"] then return end

if not ioncore.load_module("mod_menu") then
    return
end

local mod_menu=_G["mod_menu"]

assert(mod_menu)

-- Table to hold defined menus.
local menus={}


-- Menu construction {{{

--DOC
-- Define a new menu with \var{name} being the menu's name and \var{tab} 
-- being a table of menu entries.
function mod_menu.defmenu(name, tab)
    menus[name]=tab
end

--DOC
-- Returns a menu defined with \fnref{mod_menu.defmenu}.
function mod_menu.getmenu(name)
    return menus[name]
end

--DOC
-- Define context menu for context \var{ctx}, \var{tab} being a table 
-- of menu entries.
function mod_menu.defctxmenu(ctx, tab)
    menus["ctxmenu-"..ctx]=tab
end

--DOC
-- Returns a context menu defined with \fnref{mod_menu.defctxmenu}.
function mod_menu.getctxmenu(name)
    return menus["ctxmenu-"..name]
end


function mod_menu.evalmenu(menu, args)
    if type(menu)=="string" then
        return mod_menu.evalmenu(menus[menu], args)
    elseif type(menu)=="function" then
        if args then
            return menu(unpack(args))
        else
            return menu()
        end
    elseif type(menu)=="table" then
        return menu
    end
end


--DOC
-- Use this function to define normal menu entries. The string \var{name} 
-- is the string shown in the visual representation of menu, and the
-- parameter \var{cmd} and \var{guard} are similar to those of
-- \fnref{ioncore.defbindings}.
function mod_menu.menuentry(name, cmd, guard)
    local fn, gfn=ioncore.compile_cmd(cmd, guard)
    if fn then
        return {name=ioncore.gettext(name), func=fn, guard_func=gfn}
    end
end

--DOC
-- Use this function to define menu entries for submenus. The parameter
-- \fnref{sub_or_name} is either a table of menu entries or the name
-- of an already defined menu. The initial menu entry to highlight can be
-- specified in \var{initial} as either an integer starting from 1, or a
-- function that returns such a number.
function mod_menu.submenu(name, sub_or_name, initial)
    return {
        name=ioncore.gettext(name),
        initial=initial or 0,
        submenu_fn=function()
                       return mod_menu.evalmenu(sub_or_name)
                   end
    }
end


-- }}}


-- Menu commands {{{

local function menu_(reg, sub, menu_or_name, fn, check)
    if check then
        -- Check that no other menus are open in reg.
        local l=reg:llist(2)
        for i, r in l do
            if obj_is(r, "WMenu") then
                return
            end
        end
    end

    menu=mod_menu.evalmenu(menu_or_name, {reg, sub})
    
    return fn(reg, function(e) e.func(reg, sub) end, menu)
end


--DOC
-- Display a menu in the lower-left corner of \var{mplex}.
-- The variable \var{menu_or_name} is either the name of a menu
-- defined with \fnref{mod_menu.defmenu} or directly a table similar
-- to ones passesd to this function. When this function is
-- called from a binding handler, \var{sub} should be set to
-- the second argument of to the binding handler (\var{_sub})
-- so that the menu handler will get the same parameters as the
-- binding handler. The initial menu entry to highlight can be
-- specified in \var{initial} as an integer starting from 1.
function mod_menu.menu(mplex, sub, menu_or_name, initial) 
   local function menu_stdmenu(m, s, menu)
      return mod_menu.do_menu(m, s, menu, false, initial or 0)
   end
   return menu_(mplex, sub, menu_or_name, menu_stdmenu, true, initial)
end

--DOC
-- This function is similar to \fnref{mod_menu.menu}, but
-- a style with possibly bigger font and menu entries is used.
function mod_menu.bigmenu(mplex, sub, menu_or_name, initial) 
    local function menu_bigmenu(m, s, menu)
        return mod_menu.do_menu(m, s, menu, true, initial or 0)
    end
    return menu_(mplex, sub, menu_or_name, menu_bigmenu, true, initial)
end

--DOC
-- This function is similar to \fnref{mod_menu.menu}, but input
-- is grabbed and \var{key} is used to cycle through the menu.
function mod_menu.grabmenu(mplex, sub, menu_or_name, key, initial) 
    local function menu_grabmenu(m, s, menu)
        return mod_menu.do_grabmenu(m, s, menu, false, key, initial or 0)
    end
    return menu_(mplex, sub, menu_or_name, menu_grabmenu, true, initial)
end

--DOC
-- This function is similar to \fnref{mod_menu.bigmenu}, but input
-- is grabbed and \var{key} is used to cycle through the menu.
function mod_menu.biggrabmenu(mplex, sub, menu_or_name, key, initial) 
    local function menu_biggrabmenu(m, s, menu)
        return mod_menu.do_grabmenu(m, s, menu, true, key, initial or 0)
    end
    return menu_(mplex, sub, menu_or_name, menu_biggrabmenu, true, initial)
end

--DOC
-- This function displays a drop-down menu and should only
-- be called from a mouse press handler. The parameters are
-- similar to those of \fnref{mod_menu.menu}.
function mod_menu.pmenu(win, sub, menu_or_name) 
    return menu_(win, sub, menu_or_name, mod_menu.do_pmenu)
end

-- }}}


-- Workspace and window lists {{{

local function makelist(list)
    local function mkentry(tgt)
        return menuentry(tgt:name(), function() tgt:goto() end)
    end
    local entries=table.map(mkentry, list)
    table.sort(entries, function(a, b) return a.name < b.name end)
    return entries
end

function menus.windowlist()
    return makelist(ioncore.clientwin_list())
end

function menus.workspacelist()
    return makelist(ioncore.region_list("WGenWS"))
end

-- }}}


-- Style menu {{{


local function mplex_of(reg)
    while reg and not obj_is(reg, "WMPlex") do
        reg=reg:parent()
    end
    return reg
end

local function selectstyle(look, where)
    dopath(look)

    local fname=ioncore.get_savefile('look')

    local function writeit()
        local f, err=io.open(fname, 'w')
        if not f then
            mod_query.message(where, err)
        else
            f:write(string.format('dopath("%s")\n', look))
            f:close()
        end
    end

    if not mod_query then
        if fname then
            writeit()
        end
        return
    end
    
    where=mplex_of(where)
    if not where then 
        return 
    end
    
    if not fname then
        query_message(where, TR("Cannot save selection."))
        return
    end
    
    mod_query.query_yesno(where, TR("Save look selection in %s?", fname),
                          writeit)
end

local function receive_styles(str)
    local data=""
    
    while str do
        data=data .. str
        if string.len(data)>ioncore.RESULT_DATA_LIMIT then
            error(TR("Too much result data"))
        end
        str=coroutine.yield()
    end
    
    local found={}
    local styles={}
    local stylemenu={}
    
    for look in string.gfind(data, "(look[-_][^\n]*)%.lua\n") do
        if not found[look] then
            found[look]=true
            table.insert(styles, look)
        end
    end
    
    table.sort(styles)
    
    for _, look in ipairs(styles) do
        local look_=look
        table.insert(stylemenu, menuentry(look,  
                                          function(where)
                                              selectstyle(look_, where)
                                          end))
    end
    
    table.insert(stylemenu, menuentry(TR("Refresh list"),
                                      mod_menu.refresh_styles))
    
    menus.stylemenu=stylemenu
end


--DOC
-- Refresh list of known style files.
function mod_menu.refresh_styles()
    local cmd=ioncore.lookup_script("ion-completefile")
    if cmd then
        local path=ioncore.get_paths().searchpath
        local function mkarg(s)
            if s=="" then
                return ""
            else
                return (" "..string.shell_safe(s).."/look_"..
                        " "..string.shell_safe(s).."/look-")
            end
        end

        cmd=cmd..string.gsub(path..":", "([^:]*):", mkarg)
        
        ioncore.popen_bgread(cmd, coroutine.wrap(receive_styles))
    end
end

-- }}}


-- Context menu {{{


local function classes(reg)
    local function classes_(t)
        if t.__parentclass then
            classes_(t.__parentclass)
        end
        coroutine.yield(t.__typename)
    end
    return coroutine.wrap(function() classes_(reg) end)
end


local function get_ctxmenu(reg, sub, is_par)    
    local m={}
    
    local function cp(m2)
        local m3={}
        for k, v in m2 do
            local v2=table.copy(v)
            
            if v2.func then
                local ofunc=v2.func
                v2.func=function() return ofunc(reg, sub) end
            end
            
            if v2.submenu_fn then
                local ofn=v2.submenu_fn
                v2.submenu_fn=function() return cp(ofn()) end
            end
            
            m3[k]=v2
        end
        return m3
    end
    
    for s in classes(reg) do
        local m2=mod_menu.evalmenu("ctxmenu-"..s)
        if m2 then
            if is_par then
                m=table.icat(m, cp(m2))
            else
                m=table.icat(m, m2)
            end
        end
    end
    return m
end

function menus.ctxmenu(reg, sub)
    local m=get_ctxmenu(reg, sub, false);
    
    sub=reg
    reg=reg:manager()
    
    while reg do
        local mm = get_ctxmenu(reg, sub, true)
        if table.getn(mm)>0 then
            table.insert(m, mod_menu.submenu(reg:name(), mm))
        end
        sub=reg
        reg=reg:manager()
    end
    
    return m
end

-- }}}
export(mod_menu,
       "defmenu",
       "defctxmenu",
       "menuentry",
       "submenu")

mod_menu.refresh_styles()

-- Mark ourselves loaded.
_LOADED["mod_menu"]=true


-- Load configuration file
dopath('cfg_menu', true)