From 75c4bc89c7fee85cbc657921002f19857b78fbc8 Mon Sep 17 00:00:00 2001 From: Rui Guo Date: Wed, 17 Jun 2009 22:51:59 +0800 Subject: [PATCH] Make window.number writable. 1. Abstract the RC_NUMBER logic to a ChangeWinNum function. 2. Implement setter to window.number using ChangeWinNum 3. Add a demo script to test it. 4. Update the document accordingly. 5. Also fix the title setter. --- src/drafts/scripting | 7 ++-- src/lua.c | 12 ++++++- src/process.c | 79 ++++++++++++++++++++++++++-------------------- src/scripts/movewindow.lua | 11 +++++++ 4 files changed, 71 insertions(+), 38 deletions(-) create mode 100644 src/scripts/movewindow.lua diff --git a/src/drafts/scripting b/src/drafts/scripting index 9a1631c..468a6c5 100644 --- a/src/drafts/scripting +++ b/src/drafts/scripting @@ -152,9 +152,12 @@ title: description: The title of the window. number: - mode: read only + mode: read write type: int. - description: The index in the window slots. + description: The index in the window slots. Assigning new value to the number + changes the position of this window in the window list. If the + new window is occupied by some other window, they are changed by + position. dir: mode: read only diff --git a/src/lua.c b/src/lua.c index fb00b38..621b1a1 100644 --- a/src/lua.c +++ b/src/lua.c @@ -400,12 +400,21 @@ window_change_title(lua_State *L) { struct win *w = check_window(L, 1); unsigned int len; - const char *title = luaL_checklstring(L, 2, &len); + const char *title = luaL_checklstring(L, 3, &len); ChangeAKA(w, (char *)title, len); return 0; } static int +window_change_number(lua_State *L) +{ + struct win *w = check_window(L, 1); + int newnum = luaL_checkint(L, 3); + ChangeWinNum(w->w_number, newnum); + return 0; +} + +static int window_get_monitor_status(lua_State *L) { struct win *w = check_window(L, 1); @@ -452,6 +461,7 @@ static const luaL_reg window_metamethods[] = { static const struct Xet_reg window_setters[] = { {"title", 0, 0, window_change_title/*absolute setter*/}, + {"number", 0, 0, window_change_number/*absolute setter*/}, {0, 0} }; diff --git a/src/process.c b/src/process.c index a4991a8..069a67e 100644 --- a/src/process.c +++ b/src/process.c @@ -1139,6 +1139,49 @@ char *data; LayProcess(&buf, &len); } +void +ChangeWinNum(old, new) +int old; +int new; +{ + struct win *pnew = wtab[new]; + struct win *pold = wtab[old]; + if (new < 0 || new >= maxwin) + { + Msg(0, "Given window position is invalid."); + return; + } + wtab[new] = pold; + pold->w_number = new; + wtab[old] = pnew; + if (pnew) + pnew->w_number = old; +#ifdef MULTIUSER + /* exchange the acls for these windows. */ + AclWinSwap(old, new); +#endif +#ifdef UTMPOK + /* exchange the utmp-slots for these windows */ + if ((pold->w_slot != (slot_t) -1) && (pold->w_slot != (slot_t) 0)) + { + RemoveUtmp(pold); + SetUtmp(pold); + } + if (pnew && (pnew->w_slot != (slot_t) -1) && (pnew->w_slot != (slot_t) 0)) + { + /* XXX: first display wins? */ + display = pold->w_layer.l_cvlist ? pold->w_layer.l_cvlist->c_display : 0; + RemoveUtmp(pnew); + SetUtmp(pnew); + } +#endif + + WindowChanged(pold, 'n'); + WindowChanged((struct win *)0, 'w'); + WindowChanged((struct win *)0, 'W'); + WindowChanged((struct win *)0, 0); +} + /*ARGSUSED*/ void DoAction(act, key) @@ -2946,41 +2989,7 @@ int key; n += old; else if (rel < 0) n = old - n; - if (n < 0 || n >= maxwin) - { - Msg(0, "Given window position is invalid."); - return; - } - p = wtab[n]; - wtab[n] = fore; - fore->w_number = n; - wtab[old] = p; - if (p) - p->w_number = old; -#ifdef MULTIUSER - /* exchange the acls for these windows. */ - AclWinSwap(old, n); -#endif -#ifdef UTMPOK - /* exchange the utmp-slots for these windows */ - if ((fore->w_slot != (slot_t) -1) && (fore->w_slot != (slot_t) 0)) - { - RemoveUtmp(fore); - SetUtmp(fore); - } - if (p && (p->w_slot != (slot_t) -1) && (p->w_slot != (slot_t) 0)) - { - /* XXX: first display wins? */ - display = fore->w_layer.l_cvlist ? fore->w_layer.l_cvlist->c_display : 0; - RemoveUtmp(p); - SetUtmp(p); - } -#endif - - WindowChanged(fore, 'n'); - WindowChanged((struct win *)0, 'w'); - WindowChanged((struct win *)0, 'W'); - WindowChanged((struct win *)0, 0); + ChangeWinNum(old, n); } break; case RC_SILENCE: diff --git a/src/scripts/movewindow.lua b/src/scripts/movewindow.lua new file mode 100644 index 0000000..4ef8083 --- /dev/null +++ b/src/scripts/movewindow.lua @@ -0,0 +1,11 @@ +function toleft() + local w = screen.display().fore + if w.number > 0 then + w.number = w.number - 1 + end +end + +function toright() + local w = screen.display().fore + w.number = w.number + 1 +end -- 2.11.4.GIT