File: cwin_sp.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 (206 lines) | stat: -rw-r--r-- 4,218 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
-- Creates scratchpads on a per-frame/per-clientwin basis.

-- I realized that sometimes it might be useful to have a scratchpad for
-- notes and things for only one frame/cwin, and not globally.

-- This script creates two bindings:
--   CTRL-"space" to create/toggle per-frame scratchpads
-- and
--   CTRL-META-"space" to create/toggle per-cwin scratchpads

-- The per-frame sps are smarter and won't stay showing across desktops, the
-- per-cwin sps are dumber and will.

local cwinsps = nil
local cwinsps_rev = nil
local framesps = nil
local framesps_rev = nil

--{{{ load_sps

local function load_sps()
    local cwint = ioncore.read_savefile("cwin_sps")
    local framet = ioncore.read_savefile("frame_sps")

--{{{ Tuomo does this in check_and_create in mod_sp so I can do it here
    local hook

    hook = ioncore.get_hook("ioncore_post_layout_setup_hook")
    if hook then
	hook:remove(load_sps)
    end
--}}}


-- Initialize these here so I can test for nil to see if I've done my startup stuff
    cwinsps = setmetatable({}, {__mode="kv"})
    cwinsps_rev = setmetatable({}, {__mode="kv"})
    framesps = setmetatable({}, {__mode="kv"})
    framesps_rev = setmetatable({}, {__mode="kv"})

    if cwint then
	for k,v in pairs(cwint) do
	    local reg, sp

	    reg = ioncore.lookup_region(k)
	    sp = ioncore.lookup_region(v)

	    if reg and sp then
		cwinsps[reg] = sp
		cwinsps_rev[sp] = reg
	    end
	end
    end

    if framet then
	for k,v in pairs(framet) do
	    local reg, sp

	    reg = ioncore.lookup_region(k)
	    sp = ioncore.lookup_region(v)

	    if reg and sp then
		framesps[reg] = sp
		framesps_rev[sp] = reg
	    end
	end
    end
end

--}}} load_sps

--{{{ save_sps

local function save_sps()
    local t = {}

    for k,v in pairs(cwinsps) do
	if obj_exists(k) and obj_exists(v) then
	    t[k:name()] = v:name()
	end
    end
    ioncore.write_savefile("cwin_sps", t)

    t = {}
    for k,v in pairs(framesps) do
	if obj_exists(k) and obj_exists(v) then
	    t[k:name()] = v:name()
	end
    end
    ioncore.write_savefile("frame_sps", t)
end

--}}} save_sps

--{{{ toggle_cwin_sp

function toggle_cwin_sp(parent, cwin)
    local g = {}
    local cg = {}
    local pg = {}
    local sp = nil

    if not cwinsps then
	load_sps()
    end

    if cwin then
	sp = cwinsps[cwin]
	pg = parent:geom()
	cg = cwin:geom()

	g.x = pg.x + cg.x
	g.y = pg.y + cg.y
	g.w = cg.w
	g.h = cg.h
    end

    if not sp or not obj_exists(sp) then
	local sp_rev = cwinsps_rev[parent]

	if sp_rev then
	    mod_sp.set_shown(parent, 'toggle')
	else
	    local scr = cwin:screen_of()
	    local name = cwin:name().."_cwin_sp"

	    sp = scr:attach_new({type = 'WFrame', layer = 2,
	                         name = name, sizepolicy = 3,
	                         frame_style = 'frame-scratchpad',
	                         geom = g})

	    cwinsps[cwin] = sp
	    cwinsps_rev[sp] = cwin
	end
    else
	mod_sp.set_shown(sp, 'toggle')
    end
end

--}}} toggle_cwin_sp

--{{{ toggle_frame_sp

function toggle_frame_sp(frame)
    local sp = nil

    if not framesps then
	load_sps()
    end

    sp = framesps[frame]

    if (not sp or not obj_exists(sp)) then
	local sp_rev = framesps_rev[frame]

	if sp_rev then
	    mod_sp.set_shown(frame, 'toggle')
	else
	    local name = frame:name().."_frame_sp"

	    sp = frame:attach_new({type = 'WFrame', layer = 2,
	                           name = name, sizepolicy = 3,
	                           frame_style = 'frame-scratchpad'})

	    framesps[frame] = sp
	    framesps_rev[sp] = frame
	end
    else
	mod_sp.set_shown(sp, 'toggle')
    end
end

--}}} toggle_frame_sp

--{{{ Init

local function setup_hooks()
    local hook

    hook = ioncore.get_hook("ioncore_post_layout_setup_hook")
    if hook then
	hook:add(load_sps)
    end

    hook = ioncore.get_hook("ioncore_snapshot_hook")
    if hook then
	hook:add(save_sps)
    end
end

--}}} Init

--{{{ Bindings

defbindings("WFrame", {
--    kpress(CTRL.."space", "toggle_frame_sp(_)"),
--    kpress(CTRL..META.."space", "toggle_cwin_sp(_, _sub)"),
    kpress("Control+space", "toggle_frame_sp(_)"),
    kpress(META.."Control+space", "toggle_cwin_sp(_, _sub)"),
})

--}}}

setup_hooks()

-- vim:foldmethod=marker