]> git.madduck.net Git - etc/awesome.git/blob - util/init.lua

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

1163f4926c20b88bf0a5e3cd742863ebbce8c1a5
[etc/awesome.git] / util / init.lua
1
2 --[[
3                                                    
4      Lain                                          
5      Layouts, widgets and utilities for Awesome WM 
6                                                    
7      Utilities section                             
8                                                    
9      Licensed under GNU General Public License v2  
10       * (c) 2013,      Luke Bonham                 
11       * (c) 2010-2012, Peter Hofmann               
12                                                    
13 --]]
14
15 local awful        = require("awful")
16 local sqrt         = math.sqrt
17 local pairs        = pairs
18 local client       = client
19 local tonumber     = tonumber
20 local wrequire     = require("lain.helpers").wrequire
21 local setmetatable = setmetatable
22
23 -- Lain utilities submodule
24 -- lain.util
25 local util = { _NAME = "lain.util" }
26
27 -- Like awful.menu.clients, but only show clients of currently selected tags
28 function util.menu_clients_current_tags(menu, args)
29     -- List of currently selected tags.
30     local cls_tags = awful.screen.focused().selected_tags
31
32     if cls_tags == nil then return nil end
33
34     -- Final list of menu items.
35     local cls_t = {}
36
37     -- For each selected tag get all clients of that tag and add them to
38     -- the menu. A click on a menu item will raise that client.
39     for i = 1,#cls_tags do
40         local t   = cls_tags[i]
41         local cls = t:clients()
42
43         for k, c in pairs(cls) do
44             cls_t[#cls_t + 1] = { awful.util.escape(c.name) or "",
45                                   function ()
46                                       c.minimized = false
47                                       client.focus = c
48                                       c:raise()
49                                   end,
50                                   c.icon }
51         end
52     end
53
54     -- No clients? Then quit.
55     if #cls_t <= 0 then return nil end
56
57     -- menu may contain some predefined values, otherwise start with a
58     -- fresh menu.
59     if not menu then menu = {} end
60
61     -- Set the list of items and show the menu.
62     menu.items = cls_t
63     local m = awful.menu(menu)
64     m:show(args)
65
66     return m
67 end
68
69 -- Magnify a client: set it to "float" and resize it.
70 function util.magnify_client(c, width_f, height_f)
71     if c and not c.floating then
72         util.magnified_client = c
73         util.mc(c, width_f, height_f)
74     else
75         util.magnified_client = nil
76         c.floating = false
77     end
78 end
79
80 -- https://github.com/copycat-killer/lain/issues/195
81 function util.mc(c, width_f, height_f)
82     c = c or util.magnified_client
83     if not c then return end
84
85     c.floating   = true
86     local s      = awful.screen.focused()
87     local mg     = s.workarea
88     local g      = {}
89     local mwfact = width_f or s.selected_tag.master_width_factor or 0.5
90     g.width      = sqrt(mwfact) * mg.width
91     g.height     = sqrt(height_f or mwfact) * mg.height
92     g.x          = mg.x + (mg.width - g.width) / 2
93     g.y          = mg.y + (mg.height - g.height) / 2
94
95     if c then c:geometry(g) end -- if c is still a valid object
96 end
97
98 -- Non-empty tag browsing
99 -- direction in {-1, 1} <-> {previous, next} non-empty tag
100 function util.tag_view_nonempty(direction, sc)
101    local s = sc or awful.screen.focused()
102
103    for i = 1, #s.tags do
104        awful.tag.viewidx(direction, s)
105        if #s.clients > 0 then
106            return
107        end
108    end
109 end
110
111 -- {{{ Dynamic tagging
112
113 -- Add a new tag
114 function util.add_tag(layout)
115     awful.prompt.run {
116         prompt       = "New tag name: ",
117         textbox      = awful.screen.focused().mypromptbox.widget,
118         exe_callback = function(name)
119             if not name or #name == 0 then return end
120             awful.tag.add(name, { screen = awful.screen.focused(), layout = layout or awful.layout.layouts[1] }):view_only()
121         end
122     }
123 end
124
125 -- Rename current tag
126 function util.rename_tag()
127     awful.prompt.run {
128         prompt       = "Rename tag: ",
129         textbox      = awful.screen.focused().mypromptbox.widget,
130         exe_callback = function(new_name)
131             if not new_name or #new_name == 0 then return end
132             local t = awful.screen.focused().selected_tag
133             if t then
134                 t.name = new_name
135             end
136         end
137     }
138 end
139
140 -- Move current tag
141 -- pos in {-1, 1} <-> {previous, next} tag position
142 function util.move_tag(pos)
143     local tag = awful.screen.focused().selected_tag
144     if tonumber(pos) <= -1 then
145         awful.tag.move(tag.index - 1, tag)
146     else
147         awful.tag.move(tag.index + 1, tag)
148     end
149 end
150
151 -- Delete current tag
152 -- Any rule set on the tag shall be broken
153 function util.delete_tag()
154     local t = awful.screen.focused().selected_tag
155     if not t then return end
156     t:delete()
157 end
158
159 -- }}}
160
161 -- On the fly useless gaps change
162 function util.useless_gaps_resize(thatmuch)
163     local scr = awful.screen.focused()
164     scr.selected_tag.gap = scr.selected_tag.gap + tonumber(thatmuch)
165     awful.layout.arrange(scr)
166 end
167
168 return setmetatable(util, { __index = wrequire })