Ok, I've been looking at it now. I'm sorry for the trouble... I should
have been more careful.

I've come up with three possible solutions:

1) Use something like this:
   
http://stackoverflow.com/questions/8248698/recommended-way-to-have-2-modules-recursively-refer-to-each-other-in-lua-5-2
   It involves requiring modules inside the functions that use them.

2) Move the code in client.lua involving focus.history to a third
location. this way screen.lua and client.lua can both require it.

3) Duplicate the code in screen.lua needed by client.lua.

I like 1): it is simple, and requires no changes to the library. I
attach a patch with this approach. What do you think?

Abdó.

Uli Schlachter <psyc...@znc.in> writes:

> So awful.client tries to use awful.client and awful.screen by these names.
> However, they aren't available this way. awful.client would be just "client"
> inside of the module and awful.screen needs a 'local screen =
> require("awful.screen")' at the beginning of the file.
>
> However, this doesn't work:
>
> lib/awful/screen.lua:15: loop or previous error loading module 'awful.client'
>
> We now have a cyclic dependency between awful.screen and awful.client. This is
> bad, bad, bad, bad.
>
> If no one has any good suggestions on this, I will have to revert Abdó's 
> commits
> to fix this. Which isn't exactly good either...
>
> @Abdó: Ideas? How could those functions be done differently? :-(


>From ecad145d643ff75fdae8e1241fc3862c0afa4b8b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Abd=C3=B3=20Roig-Maranges?= <abdo.r...@gmail.com>
Date: Fri, 28 Sep 2012 23:53:58 +0200
Subject: [PATCH] Fixes module namespace issues in screen.lua and client.lua

The wrong module names were introduced in commits:
0e2960ebf372507017d6dba4e573c28dbd028478 and
d799ac76aa9d182abc4d80810e4c552e6e4d7e17.

Once fixed, client.lua and screen.lua mutually require each other, so we must
use a trick, and load the modules inside the functions that need them.
---
 lib/awful/client.lua.in | 27 +++++++++++++++++----------
 lib/awful/screen.lua.in |  5 ++++-
 2 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/lib/awful/client.lua.in b/lib/awful/client.lua.in
index defa4bf..840ca13 100644
--- a/lib/awful/client.lua.in
+++ b/lib/awful/client.lua.in
@@ -20,10 +20,14 @@ local capi =
     screen = screen,
 }
 
+-- we use require("awful.screen") inside functions to prevent circular dependencies.
+local screen
+
 --- Useful client manipulation functions.
 -- awful.client
 local client = {}
 
+
 -- Private data
 client.data = {}
 client.data.focus = {}
@@ -277,6 +281,7 @@ end
 -- @param dir The direction, can be either "up", "down", "left" or "right".
 -- @param c Optional client.
 function client.focus.global_bydirection(dir, c)
+    screen = screen or require("awful.screen")
     local sel = c or capi.client.focus
     local scr = capi.mouse.screen
     if sel then
@@ -288,7 +293,7 @@ function client.focus.global_bydirection(dir, c)
 
     -- if focus not changed, we must change screen
     if sel == capi.client.focus then
-        awful.screen.focus_bydirection(dir, scr)
+        screen.focus_bydirection(dir, scr)
         if scr ~= capi.mouse.screen then
             local cltbl = client.visible(capi.mouse.screen)
             local geomtbl = {}
@@ -338,10 +343,11 @@ end
 -- @param dir The direction, can be either "up", "down", "left" or "right".
 -- @param c Optional client.
 function client.swap.global_bydirection(dir, c)
+    screen = screen or require("awful.screen")
     local sel = c or capi.client.focus
-    local screen = capi.mouse.screen
+    local scr = capi.mouse.screen
     if sel then
-        screen = sel.screen
+        scr = sel.screen
     end
 
     if sel then
@@ -355,15 +361,15 @@ function client.swap.global_bydirection(dir, c)
 
         -- swapping to an empty screen
         elseif sel.screen ~= c.screen and sel == c then
-	    awful.client.movetoscreen(sel, capi.mouse.screen)
+	    client.movetoscreen(sel, capi.mouse.screen)
 
         --swapping to a nonempty screen
         elseif sel.screen ~= c.screen and sel ~= c then
-	    awful.client.movetoscreen(sel, c.screen)
-            awful.client.movetoscreen(c, screen)
+	    client.movetoscreen(sel, c.screen)
+            client.movetoscreen(c, scr)
         end
 
-        awful.screen.focus(sel.screen)
+        screen.focus(sel.screen)
         capi.client.focus = sel
     end
 end
@@ -484,6 +490,7 @@ end
 -- @param c The client to move.
 -- @param s The screen number, default to current + 1.
 function client.movetoscreen(c, s)
+    screen = screen or require("awful.screen")
     local sel = c or capi.client.focus
     if sel then
         local sc = capi.screen.count()
@@ -492,7 +499,7 @@ function client.movetoscreen(c, s)
         end
         if s > sc then s = 1 elseif s < 1 then s = sc end
         sel.screen = s
-        awful.screen.focus(s)
+        screen.focus(s)
     end
 end
 
@@ -577,11 +584,11 @@ function client.floating.set(c, s)
     local c = c or capi.client.focus
     if c and client.property.get(c, "floating") ~= s then
         client.property.set(c, "floating", s)
-        local screen = c.screen
+        local scr = c.screen
         if s == true then
             c:geometry(client.property.get(c, "floating_geometry"))
         end
-        c.screen = screen
+        c.screen = scr
     end
 end
 
diff --git a/lib/awful/screen.lua.in b/lib/awful/screen.lua.in
index 00fd16a..e6bc1e2 100644
--- a/lib/awful/screen.lua.in
+++ b/lib/awful/screen.lua.in
@@ -12,7 +12,9 @@ local capi =
     client = client
 }
 local util = require("awful.util")
-local client = require("awful.client")
+
+-- we use require("awful.client") inside functions to prevent circular dependencies.
+local client
 
 --- Screen module for awful
 -- awful.screen
@@ -40,6 +42,7 @@ end
 --- Give the focus to a screen, and move pointer. Keeps relative position of the pointer on the screen.
 -- @param screen Screen number.
 function screen.focus(_screen)
+    client = client or require("awful.client")
     if _screen > capi.screen.count() then _screen = capi.mouse.screen end
 
     -- screen and pos for current screen
-- 
1.7.12.1

Reply via email to