first commit
[archive.git] / ASCHE_0 / asche0.ath.cx / files / app.lua
blob005a25c9a0c6d37e13fd1b1da0546de6139f7246
1 --
2 -- By Jeremy Hankins <nowan@nowan.org>
3 -- Multi-head support added by Johannes Segitz
4 -- Cycling contributed by
5 -- Antti Kaihola <antti.kaihola@linux-aktivaattori.org>
6 --
7 -- Time-stamp: <2005-11-16 12:02:46 Jeremy Hankins>
8 -- <2005-08-26 11:35:00 kaihola>
9 --
10 -- With these functions you can bind a key to start an application if
11 -- it's not already running, or cycle its windows if it is. You can
12 -- use it by doing something like this in your bindings file:
14 -- kpress(META.."C", "app.byname('xterm -title shell', 'shell')"),
15 -- kpress(META.."T", "app.byclass('emacs', 'Emacs')"),
16 -- kpress(META.."M", "app.byinstance('xterm -name muttTerm -e mutt', 'XTerm', 'muttTerm')"),
18 -- The byname function expects an executable and a window title, the
19 -- byclass expects a class, the byinstance expects a class and an
20 -- instance.
22 -- NOTE: cycling windows requires ioncore.current(), so it will only
23 -- work with snapshot XXX or later.
25 -- If you use a multihead setup you can use something like this to start
26 -- applictions on your current screen
28 -- kpress(MOD1.."C", "app.byname('xterm -title shell', 'shell', _)"),
29 -- kpress(MOD1.."T", "app.byclass('emacs', 'Emacs', _)"),
31 -- For emacs users there's also app.emacs_eval, and app.query_editfile,
32 -- which interacts with any currently running emacs process (using
33 -- gnuclient), or starts emacs to run the given command.
34 -- app.query_editfile is a replacement for query_lib.query_editfile to
35 -- use the currently running emacs rather than ion-edit.
38 app={}
40 function app.match_class(class, instance)
41 -- Return matching client windows as a table.
42 -- If the current window is among them, it's placed last on the list
43 -- and its successor at the beginning of the list. This facilitates
44 -- cycling multiple windows of an application.
45 local result = {}
46 local offset = 0
47 local currwin = ioncore.current()
48 ioncore.clientwin_i(function (win)
49 if class == win:get_ident().class then
50 if instance then
51 if instance == win:get_ident().instance then
52 table.insert(result, table.getn(result)-offset+1, win)
53 end
54 else
55 table.insert(result, table.getn(result)-offset+1, win)
56 end
57 end
58 if win == currwin then
59 -- Current client window found, continue filling the table from
60 -- the beginning.
61 offset = table.getn(result)
62 end
63 return true
64 end)
66 return result
67 end
69 function app.byname(prog, name, where)
70 local win = ioncore.lookup_clientwin(name)
71 if win then
72 ioncore.defer(function () win:goto() end)
73 else
74 if where then
75 ioncore.exec_on(where, prog)
76 else
77 ioncore.exec(prog)
78 end
79 end
80 end
82 function app.byclass(prog, class, where)
83 local win = app.match_class(class)[1]
84 if win then
85 ioncore.defer(function () win:goto() end)
86 else
87 if where then
88 ioncore.exec_on(where, prog)
89 else
90 ioncore.exec(prog)
91 end
92 end
93 end
95 function app.byinstance(prog, class, instance, where)
96 local win = app.match_class(class, instance)[1]
97 if win then
98 ioncore.defer(function () win:goto() end)
99 else
100 if where then
101 ioncore.exec_on(where, prog)
102 else
103 ioncore.exec(prog)
108 function app.emacs_eval(expr)
109 local emacswin = app.match_class("Emacs")[1]
110 if emacswin then
111 ioncore.exec("gnuclient -batch -eval '"..expr.."'")
112 ioncore.defer(function () emacswin:goto() end)
113 else
114 ioncore.exec("emacs -eval '"..expr.."'")
118 function app.query_editfile(mplex, dir)
119 local function handler(file)
120 app.emacs_eval("(find-file \""..file.."\")")
123 mod_query.do_query(mplex,
124 'Edit file:',
125 dir or mod_query.get_initdir(),
126 handler,
127 mod_query.file_completor)
130 -----------------------------------------------------------------------
131 -- By ASCHE <asche0@gmail.com>
132 -- based on named_scratchpad.lua by Etan Reisner
134 -- With this function you can bind a key to start an application in
135 -- pad if it's not already running, or show pad its windows if it is.
137 -- Last updated: 2008-02-26
138 -- Worked at: ion-3rc-20080103
140 -----------------------------------------------------------------------
141 -- Usage:
143 -- defwinprop {
144 -- class = "class",
145 -- instance = "instance",
146 -- target = "class",
147 -- }
149 -- defwinprop {
150 -- class = "XTerm",
151 -- instance = "instance",
152 -- target = "instance",
153 -- }
155 -- defbindings("WMPlex.toplevel", {
156 -- kpress("binding", "app.pad_byclass ('command', 'class', width, height, _)")
157 -- kpress("binding", "app.pad_byinstance('command', 'instance', width, height, _)")
158 -- })
159 -----------------------------------------------------------------------
160 -- Example:
162 -- defwinprop {
163 -- class = "Linuxdcpp",
164 -- instance = "linuxdcpp",
165 -- target = "Linuxdcpp",
166 -- }
168 -- defwinprop {
169 -- class = "XTerm",
170 -- instance = "my_xterm",
171 -- target = "my_xterm",
172 -- }
174 -- defbindings("WMPlex.toplevel", {
175 -- kpress("F1", "app.pad_byclass ('linuxdcpp', 'Linuxdcpp', 1024, 768, _)")
176 -- kpress("F2", "app.pad_byinstance('xterm -name my_xterm', 'my_xterm', 640, 480, _)")
177 -- })
178 -----------------------------------------------------------------------
180 function app.toggle_pad(reg, name, width, height)
181 local named_sp = ioncore.lookup_region(name, "WFrame")
183 if not named_sp then
184 local scr = reg:screen_of()
185 local geom_scr = scr:geom()
187 local geom_loc = {
188 w = width,
189 h = height,
191 geom_loc.x = (geom_scr.w - geom_loc.w) / 2
192 geom_loc.y = (geom_scr.h - geom_loc.h) / 2
194 named_sp = scr:attach_new({
195 type = "WFrame",
196 name = name,
197 unnumbered = true,
198 modal = true,
199 hidden = true,
200 sizepolicy = 5,
201 geom = geom_loc,
205 WRegion.rqorder(reg, "back")
206 mod_sp.set_shown(named_sp, "toggle")
209 -----------------------------------------------------------------------
211 function app.pad_byclass(prog, class, width, height, reg)
212 app.toggle_pad(reg, class, width, height)
214 local win = app.match_class(class)[1]
215 if not win then
216 ioncore.exec_on(reg, prog)
220 -----------------------------------------------------------------------
222 function app.pad_byinstance(prog, instance, width, height, reg)
223 app.toggle_pad(reg, instance, width, height)
225 local win = app.match_class("XTerm", instance)[1]
226 if not win then
227 ioncore.exec_on(reg, prog)
231 -----------------------------------------------------------------------