factor out link finding algorithm to its own file.
authorraster <raster@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 13 Jul 2012 09:12:01 +0000 (09:12 +0000)
committerraster <raster@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 13 Jul 2012 09:12:01 +0000 (09:12 +0000)
git-svn-id: http://svn.enlightenment.org/svn/e/trunk/terminology@73800 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/bin/Makefile.am
src/bin/termio.c
src/bin/termio.h
src/bin/termiolink.c [new file with mode: 0644]
src/bin/termiolink.h [new file with mode: 0644]

index 4f2f0f8..f2dec31 100644 (file)
@@ -25,6 +25,7 @@ options_video.c options_video.h \
 options_theme.c options_theme.h \
 options_wallpaper.c options_wallpaper.h \
 termio.c termio.h \
+termiolink.c termiolink.h \
 termpty.c termpty.h \
 termptydbl.c termptydbl.h \
 termptyesc.c termptyesc.h \
index d31cb97..5d7792d 100644 (file)
@@ -4,6 +4,7 @@
 #include <Elementary.h>
 #include <Ecore_Input.h>
 #include "termio.h"
+#include "termiolink.h"
 #include "termpty.h"
 #include "utf8.h"
 #include "col.h"
@@ -76,193 +77,6 @@ static Evas_Smart_Class _parent_sc = EVAS_SMART_CLASS_INIT_NULL;
 
 static void _smart_calculate(Evas_Object *obj);
 
-static Eina_Bool
-coord_back(Termio *sd, int *x, int *y)
-{
-   (*x)--;
-   if ((*x) < 0)
-     {
-        if ((*y) <= 0)
-          {
-             (*x)++;
-             return EINA_FALSE;
-          }
-        (*x) = sd->grid.w - 1;
-        (*y)--;
-     }
-   return EINA_TRUE;
-}
-
-static Eina_Bool
-coord_forward(Termio *sd, int *x, int *y)
-{
-   (*x)++;
-   if ((*x) >= sd->grid.w)
-     {
-        if ((*y) >= (sd->grid.h - 1))
-          {
-             (*x)--;
-             return EINA_FALSE;
-          }
-        (*x) = 0;
-        (*y)++;
-     }
-   return EINA_TRUE;
-}
-
-static char *
-_magic_string_find(Evas_Object *obj, int cx, int cy, int *x1r, int *y1r, int *x2r, int *y2r)
-{
-   Termio *sd = evas_object_smart_data_get(obj);
-   char *s;
-   char endmatch = 0;
-   int x1, x2, y1, y2, len;
-   Eina_Bool goback = EINA_TRUE, goforward = EINA_FALSE, extend = EINA_FALSE;
-   
-   if (!sd) return NULL;
-   x1 = cx;
-   y1 = cy;
-   x2 = cx;
-   y2 = cy;
-   if (!coord_back(sd, &x1, &y1)) goback = EINA_FALSE;
-   for (;;)
-     {
-        s = termio_selection_get(obj,
-                                 x1, y1 - sd->scroll,
-                                 x2, y2 - sd->scroll);
-        if (!s) break;
-        if (goback)
-          {
-             if      ((!strncasecmp(s, "http://", 7))||
-                      (!strncasecmp(s, "https://", 8)) ||
-                      (!strncasecmp(s, "file://", 7)) ||
-                      (!strncasecmp(s, "ftp://", 6)))
-               {
-                  goback = EINA_FALSE;
-                  coord_back(sd, &x1, &y1);
-                  free(s);
-                  s = termio_selection_get(obj,
-                                           x1, y1 - sd->scroll,
-                                           x2, y2 - sd->scroll);
-                  if (!s) break;
-                  if (s[0] == '"') endmatch = '"';
-                  else if (s[0] == '\'') endmatch = '\'';
-                  else if (s[0] == '<') endmatch = '>';
-                  coord_forward(sd, &x1, &y1);
-                  free(s);
-                  s = termio_selection_get(obj,
-                                           x1, y1 - sd->scroll,
-                                           x2, y2 - sd->scroll);
-                  if (!s) break;
-               }
-             else if ((isspace(s[0])) ||
-                      (s[0] == '"') ||
-                      (s[0] == '\'') ||
-                      (s[0] == '<') ||
-                      (s[0] == '='))
-               {
-                  if (s[0] == '"') endmatch = '"';
-                  else if (s[0] == '\'') endmatch = '\'';
-                  else if (s[0] == '<') endmatch = '>';
-                  if ((!strncasecmp((s + 1), "www.", 4)) ||
-                      (!strncasecmp((s + 1), "ftp.", 4)) ||
-                      (!strncasecmp((s + 1), "/", 1)))
-                    {
-                       goback = EINA_FALSE;
-                       coord_forward(sd, &x1, &y1);
-                    }
-                  else if (strchr((s + 2), '@'))
-                    {
-                       goback = EINA_FALSE;
-                       coord_forward(sd, &x1, &y1);
-                    }
-                  else if (s[0] == '=')
-                    {
-                    }
-                  else
-                    {
-                       free(s);
-                       s = NULL;
-                       break;
-                    }
-               }
-          }
-        if (goforward)
-          {
-             len = strlen(s);
-             if (len > 1)
-               {
-                  if (((endmatch) && (s[len - 1] == endmatch)) ||
-                      ((!endmatch) && 
-                          ((isspace(s[len - 1])) || (s[len - 1] == '>'))
-                      ))
-                    {
-                       goforward = EINA_FALSE;
-                       coord_back(sd, &x2, &y2);
-                    }
-               }
-          }
-        
-        if (goforward)
-          {
-             if (!coord_forward(sd, &x2, &y2)) goforward = EINA_FALSE;
-          }
-        if (goback)
-          {
-             if (!coord_back(sd, &x1, &y1)) goback = EINA_FALSE;
-          }
-        if ((!extend) && (!goback))
-          {
-             goforward = EINA_TRUE;
-             extend = EINA_TRUE;
-          }
-        if ((!goback) && (!goforward))
-          {
-             free(s);
-             s = termio_selection_get(obj, 
-                                      x1, y1 - sd->scroll, 
-                                      x2, y2 - sd->scroll);
-             break;
-          }
-        free(s);
-        s = NULL;
-     }
-   if (s)
-     {
-        len = strlen(s);
-        while (len > 1)
-          {
-             if (isspace(s[len - 1]))
-               {
-                  s[len - 1] = 0;
-                  len--;
-               }
-             else break;
-          }
-        if ((!isspace(s[0])) && (len > 1))
-          {
-             if ((strchr(s, '@')) ||
-                 (!strncasecmp(s, "http://", 7))||
-                 (!strncasecmp(s, "https://", 8)) ||
-                 (!strncasecmp(s, "ftp://", 6)) ||
-                 (!strncasecmp(s, "file://", 7)) ||
-                 (!strncasecmp(s, "www.", 4)) ||
-                 (!strncasecmp(s, "ftp.", 4)) ||
-                 (!strncasecmp(s, "/", 1))
-                )
-               {
-                  if (x1r) *x1r = x1;
-                  if (y1r) *y1r = y1;
-                  if (x2r) *x2r = x2;
-                  if (y2r) *y2r = y2;
-                  return s;
-               }
-          }
-        free(s);
-     }
-   return NULL;
-}
-
 static void
 _activate_link(Evas_Object *obj)
 {
@@ -511,7 +325,7 @@ _smart_mouseover_apply(Evas_Object *obj)
    
    if (!sd) return;
 
-   s = _magic_string_find(obj, sd->mouse.cx, sd->mouse.cy,
+   s = _termio_link_find(obj, sd->mouse.cx, sd->mouse.cy,
                          &x1, &y1, &x2, &y2);
    if (!s)
      {
@@ -2530,3 +2344,25 @@ termio_mouseover_suspend_pushpop(Evas_Object *obj, int dir)
    if (sd->link.suspend < 0) sd->link.suspend = 0;
    _smart_update_queue(obj, sd);
 }
+
+void
+termio_size_get(Evas_Object *obj, int *w, int *h)
+{
+   Termio *sd = evas_object_smart_data_get(obj);
+   if (!sd)
+     {
+        if (w) *w = 0;
+        if (h) *h = 0;
+        return;
+     }
+   if (w) *w = sd->grid.w;
+   if (h) *h = sd->grid.h;
+}
+
+int
+termio_scroll_get(Evas_Object *obj)
+{
+   Termio *sd = evas_object_smart_data_get(obj);
+   if (!sd) return 0;
+   return sd->scroll;
+}
index 108d535..781a69e 100644 (file)
@@ -12,4 +12,7 @@ void         termio_copy_clipboard(Evas_Object *obj);
 void         termio_paste_clipboard(Evas_Object *obj);
 const char  *termio_link_get(const Evas_Object *obj);
 void         termio_mouseover_suspend_pushpop(Evas_Object *obj, int dir);
+void         termio_size_get(Evas_Object *obj, int *w, int *h);
+int          termio_scroll_get(Evas_Object *obj);
+
 #endif
diff --git a/src/bin/termiolink.c b/src/bin/termiolink.c
new file mode 100644 (file)
index 0000000..4bd6c84
--- /dev/null
@@ -0,0 +1,181 @@
+#include "private.h"
+#include <Elementary.h>
+#include "termio.h"
+
+static Eina_Bool
+coord_back(int *x, int *y, int w, int h __UNUSED__)
+{
+   (*x)--;
+   if ((*x) < 0)
+     {
+        if ((*y) <= 0)
+          {
+             (*x)++;
+             return EINA_FALSE;
+          }
+        (*x) = w - 1;
+        (*y)--;
+     }
+   return EINA_TRUE;
+}
+
+static Eina_Bool
+coord_forward(int *x, int *y, int w, int h)
+{
+   (*x)++;
+   if ((*x) >= w)
+     {
+        if ((*y) >= (h - 1))
+          {
+             (*x)--;
+             return EINA_FALSE;
+          }
+        (*x) = 0;
+        (*y)++;
+     }
+   return EINA_TRUE;
+}
+
+char *
+_termio_link_find(Evas_Object *obj, int cx, int cy, int *x1r, int *y1r, int *x2r, int *y2r)
+{
+   char *s;
+   char endmatch = 0;
+   int x1, x2, y1, y2, len, w = 0, h = 0, sc;
+   Eina_Bool goback = EINA_TRUE, goforward = EINA_FALSE, extend = EINA_FALSE;
+   
+   x1 = x2 = cx;
+   y1 = y2 = cy;
+   termio_size_get(obj, &w, &h);
+   sc = termio_scroll_get(obj);
+   if ((w <= 0) || (h <= 0)) return NULL;
+   if (!coord_back(&x1, &y1, w, h)) goback = EINA_FALSE;
+   for (;;)
+     {
+        s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+        if (!s) break;
+        if (goback)
+          {
+             if      ((!strncasecmp(s, "http://", 7))||
+                      (!strncasecmp(s, "https://", 8)) ||
+                      (!strncasecmp(s, "file://", 7)) ||
+                      (!strncasecmp(s, "ftp://", 6)))
+               {
+                  goback = EINA_FALSE;
+                  coord_back(&x1, &y1, w, h);
+                  free(s);
+                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+                  if (!s) break;
+                  if (s[0] == '"') endmatch = '"';
+                  else if (s[0] == '\'') endmatch = '\'';
+                  else if (s[0] == '<') endmatch = '>';
+                  coord_forward(&x1, &y1, w, h);
+                  free(s);
+                  s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+                  if (!s) break;
+               }
+             else if ((isspace(s[0])) ||
+                      (s[0] == '"') ||
+                      (s[0] == '\'') ||
+                      (s[0] == '<') ||
+                      (s[0] == '='))
+               {
+                  if (s[0] == '"') endmatch = '"';
+                  else if (s[0] == '\'') endmatch = '\'';
+                  else if (s[0] == '<') endmatch = '>';
+                  if ((!strncasecmp((s + 1), "www.", 4)) ||
+                      (!strncasecmp((s + 1), "ftp.", 4)) ||
+                      (!strncasecmp((s + 1), "/", 1)))
+                    {
+                       goback = EINA_FALSE;
+                       coord_forward(&x1, &y1, w, h);
+                    }
+                  else if (strchr((s + 2), '@'))
+                    {
+                       goback = EINA_FALSE;
+                       coord_forward(&x1, &y1, w, h);
+                    }
+                  else if (s[0] == '=')
+                    {
+                    }
+                  else
+                    {
+                       free(s);
+                       s = NULL;
+                       break;
+                    }
+               }
+          }
+        if (goforward)
+          {
+             len = strlen(s);
+             if (len > 1)
+               {
+                  if (((endmatch) && (s[len - 1] == endmatch)) ||
+                      ((!endmatch) && 
+                          ((isspace(s[len - 1])) || (s[len - 1] == '>'))
+                      ))
+                    {
+                       goforward = EINA_FALSE;
+                       coord_back(&x2, &y2, w, h);
+                    }
+               }
+          }
+        
+        if (goforward)
+          {
+             if (!coord_forward(&x2, &y2, w, h)) goforward = EINA_FALSE;
+          }
+        if (goback)
+          {
+             if (!coord_back(&x1, &y1, w, h)) goback = EINA_FALSE;
+          }
+        if ((!extend) && (!goback))
+          {
+             goforward = EINA_TRUE;
+             extend = EINA_TRUE;
+          }
+        if ((!goback) && (!goforward))
+          {
+             free(s);
+             s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc);
+             break;
+          }
+        free(s);
+        s = NULL;
+     }
+   if (s)
+     {
+        len = strlen(s);
+        while (len > 1)
+          {
+             if (isspace(s[len - 1]))
+               {
+                  s[len - 1] = 0;
+                  len--;
+               }
+             else break;
+          }
+        if ((!isspace(s[0])) && (len > 1))
+          {
+             if ((strchr(s, '@')) ||
+                 (!strncasecmp(s, "http://", 7))||
+                 (!strncasecmp(s, "https://", 8)) ||
+                 (!strncasecmp(s, "ftp://", 6)) ||
+                 (!strncasecmp(s, "file://", 7)) ||
+                 (!strncasecmp(s, "www.", 4)) ||
+                 (!strncasecmp(s, "ftp.", 4)) ||
+                 (!strncasecmp(s, "/", 1))
+                )
+               {
+                  if (x1r) *x1r = x1;
+                  if (y1r) *y1r = y1;
+                  if (x2r) *x2r = x2;
+                  if (y2r) *y2r = y2;
+                  return s;
+               }
+          }
+        free(s);
+     }
+   return NULL;
+}
diff --git a/src/bin/termiolink.h b/src/bin/termiolink.h
new file mode 100644 (file)
index 0000000..c082a5f
--- /dev/null
@@ -0,0 +1,2 @@
+char *_termio_link_find(Evas_Object *obj, int cx, int cy, int *x1r, int *y1r, int *x2r, int *y2r);
+