Extract RC_FOCUS implementation to clean up duplicated code.
authorRui Guo <firemeteor.guo@gmail.com>
Wed, 24 Jun 2009 14:09:50 +0000 (24 22:09 +0800)
committerRui Guo <firemeteor.guo@gmail.com>
Wed, 24 Jun 2009 14:09:50 +0000 (24 22:09 +0800)
src/lua.c
src/process.c

index 6373061..07341f4 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
@@ -558,33 +558,11 @@ canvas_select(lua_State *L)
   struct canvas *c = check_canvas(L, 1);
   if (!display || D_forecv == c)
     return 0;
+  /* FIXME: why do we need this? */
   SetCanvasWindow(c, Layer2Window(c->c_layer));
-  D_forecv = c;
-
-  /* XXX: the following all is duplicated from process.c:DoAction.
-   * Should these be in some better place?
-   */
-  ResizeCanvas(&D_canvas);
-  RecreateCanvasChain();
-  RethinkDisplayViewports();
-  ResizeLayersToCanvases();    /* redisplays */
-  fore = D_fore = Layer2Window(D_forecv->c_layer);
-  flayer = D_forecv->c_layer;
-#ifdef RXVT_OSC
-  if (D_xtermosc[2] || D_xtermosc[3])
-    {
-      Activate(-1);
-      break;
-    }
-#endif
-  RefreshHStatus();
-#ifdef RXVT_OSC
-  RefreshXtermOSC();
-#endif
-  flayer = D_forecv->c_layer;
-  CV_CALL(D_forecv, LayRestore();LaySetCursor());
-  WindowChanged(0, 'F');
-  return 1;
+
+  FocusCanvas(c);
+  return 0;
 }
 
 static const luaL_reg canvas_methods[] = {
index 8f627b4..d6cfda6 100644 (file)
@@ -1182,6 +1182,54 @@ int new;
   WindowChanged((struct win *)0, 0);
 }
 
+void
+FocusCanvas(struct canvas *cv)
+{
+  D_forecv = cv;
+
+  if ((focusminwidth && (focusminwidth < 0 || D_forecv->c_xe - D_forecv->c_xs + 1 < focusminwidth)) ||
+      (focusminheight && (focusminheight < 0 || D_forecv->c_ye - D_forecv->c_ys + 1 < focusminheight)))
+    {
+      ResizeCanvas(&D_canvas);
+      RecreateCanvasChain();
+      RethinkDisplayViewports();
+      ResizeLayersToCanvases();        /* redisplays */
+    }
+#ifdef SCRIPT
+  {
+    struct win *oldfore = D_fore;
+    fore = D_fore = Layer2Window(D_forecv->c_layer);
+    if (oldfore != fore)
+      {
+        if (oldfore)
+          trigger_sevent(&oldfore->w_sev.onleave, display, oldfore);
+        if (fore)
+          trigger_sevent(&fore->w_sev.onfocus, display, oldfore);
+        trigger_sevent(&globalevents.forechanged, display, display ? D_fore : fore);
+      }
+  }
+#else
+  fore = D_fore = Layer2Window(D_forecv->c_layer);
+#endif
+  if (D_other == fore)
+    D_other = 0;
+  flayer = D_forecv->c_layer;
+#ifdef RXVT_OSC
+  if (D_xtermosc[2] || D_xtermosc[3])
+    {
+      Activate(-1);
+      break;
+    }
+#endif
+  RefreshHStatus();
+#ifdef RXVT_OSC
+  RefreshXtermOSC();
+#endif
+  flayer = D_forecv->c_layer;
+  CV_CALL(D_forecv, LayRestore();LaySetCursor());
+  WindowChanged(0, 'F');
+}
+
 /*ARGSUSED*/
 void
 DoAction(act, key)
@@ -3994,70 +4042,33 @@ int key;
       LaySetCursor();
       break;
     case RC_FOCUS:
-      if (!*args || !strcmp(*args, "down"))
-       D_forecv = D_forecv->c_next ? D_forecv->c_next : D_cvlist;
-      else if (!strcmp(*args, "up"))
-       {
-         struct canvas *cv;
-         for (cv = D_cvlist; cv->c_next && cv->c_next != D_forecv; cv = cv->c_next)
-           ;
-         D_forecv = cv;
-       }
-      else if (!strcmp(*args, "top"))
-       D_forecv = D_cvlist;
-      else if (!strcmp(*args, "bottom"))
-       {
-         struct canvas *cv;
-         for (cv = D_cvlist; cv->c_next; cv = cv->c_next)
-           ;
-         D_forecv = cv;
-       }
-      else
-       {
-         Msg(0, "%s: usage: focus [up|down|top|bottom]", rc_name);
-         break;
-       }
-      if ((focusminwidth && (focusminwidth < 0 || D_forecv->c_xe - D_forecv->c_xs + 1 < focusminwidth)) ||
-          (focusminheight && (focusminheight < 0 || D_forecv->c_ye - D_forecv->c_ys + 1 < focusminheight)))
-       {
-         ResizeCanvas(&D_canvas);
-         RecreateCanvasChain();
-         RethinkDisplayViewports();
-         ResizeLayersToCanvases();     /* redisplays */
-       }
-#ifdef SCRIPT
       {
-        struct win *oldfore = D_fore;
-        fore = D_fore = Layer2Window(D_forecv->c_layer);
-        if (oldfore != fore)
+        struct canvas *n_forecv = NULL;
+        if (!*args || !strcmp(*args, "down"))
+          n_forecv = D_forecv->c_next ? D_forecv->c_next : D_cvlist;
+        else if (!strcmp(*args, "up"))
+          {
+            struct canvas *cv;
+            for (cv = D_cvlist; cv->c_next && cv->c_next != D_forecv; cv = cv->c_next)
+              ;
+            n_forecv = cv;
+          }
+        else if (!strcmp(*args, "top"))
+          n_forecv = D_cvlist;
+        else if (!strcmp(*args, "bottom"))
           {
-            if (oldfore)
-              trigger_sevent(&oldfore->w_sev.onleave, display, oldfore);
-            if (fore)
-              trigger_sevent(&fore->w_sev.onfocus, display, oldfore);
-            trigger_sevent(&globalevents.forechanged, display, display ? D_fore : fore);
+            struct canvas *cv;
+            for (cv = D_cvlist; cv->c_next; cv = cv->c_next)
+              ;
+            n_forecv = cv;
           }
+        else
+          {
+            Msg(0, "%s: usage: focus [up|down|top|bottom]", rc_name);
+            break;
+          }
+        FocusCanvas(n_forecv);
       }
-#else
-      fore = D_fore = Layer2Window(D_forecv->c_layer);
-#endif
-      if (D_other == fore)
-       D_other = 0;
-      flayer = D_forecv->c_layer;
-#ifdef RXVT_OSC
-      if (D_xtermosc[2] || D_xtermosc[3])
-       {
-         Activate(-1);
-         break;
-       }
-#endif
-      RefreshHStatus();
-#ifdef RXVT_OSC
-      RefreshXtermOSC();
-#endif
-      flayer = D_forecv->c_layer;
-      CV_CALL(D_forecv, LayRestore();LaySetCursor());
-      WindowChanged(0, 'F');
       break;
     case RC_RESIZE:
       i = 0;