On Sat, May 29, 2010 at 08:31:40AM +0200, Uli Schlachter wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
> 
> Am 29.05.2010 01:39, Perry Hargrave wrote:
> > tag.delete(t, fb):
> >     If there are > 0 clients and only 1 tag, then don't delete the tag.
> It might be necessary to check that target_tag and fallback_tag are different.
> Also, if target_tag is the first tag on that screen and no fallback_tag is
> given, the code below might leave sticky clients attached to no tag.
> My proposal (see below for the first line):
> 

disregard my 'target_tag.screen = nil' comment. was lost in the shuffle,
you are right (of course) the tag is still there, so I will add this...

-- 
perry

>From 682f4b4b30428a2e2d01e413d4b62017a6e8374f Mon Sep 17 00:00:00 2001
From: Perry Hargrave <[email protected]>
Date: Fri, 28 May 2010 10:52:55 -0700
Subject: [PATCH 2/5] tag.lua: delete() intelligently delete tags

tag.delete(t, fb):
    If there are > 0 clients and only 1 tag, then don't delete the tag.

    Delete a tag 't' if there are no clients exclusively assigned to it.

Signed-off-by: Perry Hargrave <[email protected]>
---
 lib/awful/tag.lua.in |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

diff --git a/lib/awful/tag.lua.in b/lib/awful/tag.lua.in
index 9f229de..ce04c39 100644
--- a/lib/awful/tag.lua.in
+++ b/lib/awful/tag.lua.in
@@ -48,6 +48,55 @@ function add(name, props)
     return newtag
 end
 
+--- Delete a tag.
+-- @param target_tag Optional tag object to delete, [tag.selected()]
+-- @param fallback_tag Tag to assign stickied tags to. [screen[]:tags()[1]]
+-- If there are no clients exclusively on this tag then delete it.
+function delete(target_tag, fallback_tag)
+    -- abort if no tag is passed or currently selected
+    local target_tag = target_tag or selected()
+    if target_tag == nil then return end
+    local s_tags = capi.screen[target_tag.screen]:tags()
+
+    -- Find suitable tag for moving clients to
+    local fallback_tag = fallback_tag
+    if fallback_tag == target_tag then return end
+
+    -- no fallback tag given, find the first suitable one
+    if fallback_tag == nil then
+        if target_tag ~= s_tags[1] then
+            fallback_tag = s_tags[1]
+        else
+            fallback_tag = s_tags[2]
+        end
+    end
+
+    -- check the number and state of clients on this tag
+    -- if there are clients, but only one tag, then don't delete it
+    local clients = target_tag:clients()
+    if #clients > 0 and #s_tags == 1 then
+        return
+    end
+
+    -- move any affected clients (stickied or multi-tagged) off
+    for _, c in pairs(clients) do
+        local c_tags = c:tags()
+        local i_tag = util.table.hasitem(c_tags, target_tag)
+
+        if (not c.sticky) and (#c_tags == 1) then
+            return
+        elseif i_tag ~= nil then
+            table.remove(c_tags, i_tag)
+            if c.sticky then
+                c:tags({fallback_tag})
+            end
+        end
+    end
+
+    -- delete the tag
+    target_tag.screen = nil
+end
+
 --- Create a set of tags and attach it to a screen.
 -- @param names The tag name, in a table
 -- @param screen The tag screen, or 1 if not set.
-- 
1.7.1

Reply via email to