File: bindsearch.lua

package info (click to toggle)
notion 3%2B2012042300-1
  • links: PTS, VCS
  • area: non-free
  • in suites: wheezy
  • size: 4,724 kB
  • sloc: ansic: 45,614; makefile: 544; sh: 409; perl: 113
file content (97 lines) | stat: -rw-r--r-- 2,567 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
-- bindsearch.lua
-- 
-- Canaan Hadley-Voth.
--
-- Bindings are spread across distant files, some are created
-- dynamically, and some are negated by others.  I use this to 
-- tell me exactly what is bound right now.
--
-- Search (in all sections) for bound keys or bound commands.
-- There can be multiple search terms separated by spaces, but
-- they would have to appear in the specified order.  Output 
-- is a WMessage with a section of the bindings table.
-- 
--[[

Example: bindsearch("K X") yields:

WIonWS
 [kcb] = Mod1+K
 [action] = kpress
 [submap] = table: 0x8254478
 [cmd] = WIonWS.unsplit_at(_, _sub)
 [action] = kpress
 [kcb] = AnyModifier+X
 [func] = function: 0x80de2d0

cmdsearch("unsplit") would have found the same thing.

A search for " " shows all bindings.

--]]


-- In versions older than 20051210, print() is not defined to send wmessages.
-- Uncommenting this next line might be necessary.
-- local function print(msg) mod_query.message( ioncore.region_list("WScreen")[1], tostring(msg) ) end

local function strtable(tabl, f)
    local msg=""
    if not f then f=function(x) return x end end
    n=#tabl
    if n>0 then
	msg=tostring(n).." objects:"
    end
    for i in pairs(tabl) do
        msg=msg.." ["..tostring(i).."] = "..tostring(f(tabl[i])).."\n"
    end
    return msg
end


-- Possible fields are "kcb" (default), "cmd", and "class".
function bindsearch(str, field)

    local btabl = table.copy(ioncore.getbindings(), true)
    local fullmsg=""
    local fullkcb=""
    local class=""
    str=string.gsub(str, "%+", "%%%+")
    str=string.gsub(str, " ", ".*")
    --string.gsub(str, "@", "%%%@")

    local function domap(class, t, msg, kcb, show_all)
	local found
	for index,bind in pairs(t) do
	    found=false
	    if bind.area then
		fullkcb=kcb.." "..bind.kcb.."@"..bind.area
	    else
		fullkcb=kcb.." "..bind.kcb
	    end
	    fullkcb=string.gsub(fullkcb, "AnyModifier", " ")
	    if (field==nil or field=="kcb") and string.find(fullkcb, str) then
		found=true
	    elseif field=="cmd" and bind.cmd and 
	      string.find(string.lower(bind.cmd), string.lower(str)) then
		found=true
	    elseif field=="class" and class==str then
		found=true
	    end
	    if bind.submap then
		domap(class, bind.submap, msg..strtable(bind), fullkcb, found)
	    elseif found or show_all then
		fullmsg=fullmsg..msg..strtable(bind)
	    end
	end
    end

    for class, bindings in pairs(btabl) do
	domap(class, bindings, class.."\n", "", false)
    end
    print(fullmsg)
end

function cmdsearch(str)
    bindsearch(str, "cmd")
end