Released version 3-2014010502
[notion.git] / contrib / scripts / send_to_ws.lua
blob77ddd91442190e8520d4f3c70894b8144bb45265
1 -- Authors: Canaan Hadley-Voth <ugggg@hotmail.com>
2 -- License: Unknown
3 -- Last Changed: Unknown
4 --
5 -- send_to_ws.lua
6 --
7 -- written by Canaan Hadley-Voth, ugggg at hotmail
8 --
9 -- Sends a clientwin to another workspace.
11 -- On a tiled workspace, the frame sent to will be the most recently active.
12 -- Maximized windows are skipped over in search of an actual workspace.
13 -- Focus follows if jumpto is set.
15 -- send_to_ws(cwin, action, jumpto)
16 -- send_to_new_ws(cwin, wstype, jumpto)
18 -- The action argument can be a specific workspace number OR it can be
19 -- 'left', 'right', or 'any'.
21 -- Workspace numbers start at 0 because WScreen.switch_nth starts at 0.
22 -- (In both cases the bindings make it look like they start at 1.)
26 -- These are the bindings I use, as an example.
27 defbindings("WMPlex", {
28 submap(META.."K", {
29 submap("bracketleft", {
30 kpress("H", "send_to_ws(_sub, 'left', true)"),
31 kpress("L", "send_to_ws(_sub, 'right', true)"),
32 kpress("1", "send_to_ws(_sub, 0, true)"),
33 kpress("2", "send_to_ws(_sub, 1, true)"),
34 kpress("3", "send_to_ws(_sub, 2, true)"),
35 kpress("4", "send_to_ws(_sub, 3, true)"),
36 kpress("5", "send_to_ws(_sub, 4, true)"),
37 kpress("6", "send_to_ws(_sub, 5, true)"),
38 kpress("7", "send_to_ws(_sub, 6, true)"),
39 kpress("8", "send_to_ws(_sub, 7, true)"),
40 kpress("9", "send_to_ws(_sub, 8, true)"),
41 kpress("F", "send_to_new_ws(_sub, 'WGroupWS')"),
42 }),
43 }),
47 function send_to_ws(cwin, action, jumpto)
48 local destws, destwsindex, destwstype
49 local offset=0
50 local scr=cwin:screen_of()
51 local curws=cwin:manager()
52 while curws:manager()~=scr do
53 curws=curws:manager()
54 end
55 local curwsindex=scr:get_index(curws)
57 if type(action)=="string" then
58 if action=="left" then
59 offset=-1
60 elseif action=="right" then
61 offset=1
62 elseif action=="any" then
63 -- send to the right if a workspace exists there, otherwise left
64 if not(WMPlex.mx_nth(scr, curwsindex+1)) then
65 send_to_ws(cwin, 'left', jumpto)
66 return
67 else
68 send_to_ws(cwin, 'right', jumpto)
69 return
70 end
71 end
73 -- Skip over fullscreen client windows or frames or whatever else,
74 -- until an actual workspace is found.
75 destwsindex=curwsindex
76 destws=scr:mx_nth(destwsindex+offset)
77 destwstype=obj_typename(destws)
78 while destwstype~="WGroupWS" and destwstype~="WTiling" do
79 destwsindex=destwsindex+offset
80 destws=scr:mx_nth(destwsindex)
81 destwstype=obj_typename(destws)
82 -- If there are no workspaces to the right, make one.
83 -- (workspace type created can be set to "WGroupWS" instead.
84 -- If there are no workspaces to the left, give up.
85 if destwsindex>scr:mx_count() then
86 send_to_new_ws(cwin, "WTiling")
87 return
88 elseif destwsindex<0 then
89 return
90 end
91 end
92 elseif type(action)=="number" then
93 destwsindex=action
94 destws=scr:mx_nth(destwsindex)
95 destwstype=obj_typename(destws)
96 if destwsindex==curwsindex then return end
97 end
99 if destwstype=="WTiling" then
100 destws:current():attach(cwin, {switchto=true})
101 elseif destwstype=="WGroupWS" and obj_typename(destws:current())=="WTiling" then
102 destws:current():current():attach(cwin, {switchto=true})
103 elseif destwstype=="WGroupWS" then
104 destws:attach_framed(cwin)
107 if jumpto then
108 cwin:goto()
113 function send_to_new_ws(cwin, wstype, jumpto)
114 local scr=cwin:screen_of()
115 local n=scr:mx_count()
116 local newws
118 if wstype=="WGroupWS" then
119 newws=scr:attach_new{type="WGroupWS"}
120 newws:attach_framed(cwin)
121 else
122 newws=scr:attach_new{type="WTiling"}
123 newws:current():attach(cwin)
125 if jumpto then cwin:goto() end