diff options
author | Boris Faure <billiob@gmail.com> | 2015-03-08 20:22:44 +0100 |
---|---|---|
committer | Boris Faure <billiob@gmail.com> | 2015-03-08 20:22:44 +0100 |
commit | 688bf414e4622e4fd6d2b2cd377eba569a39f580 (patch) | |
tree | 023580858255c932f45cc30bebdd200fc3066930 /src | |
parent | b4ba773e17027c160667cd5bd7c9dfaf541ff878 (diff) |
right trim selections
Also add a (near-dumb) string buffer abstraction because working with
Eina_Strbuf, an opaque structure is a PITA.
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/termio.c | 117 | ||||
-rw-r--r-- | src/bin/termio.h | 2 | ||||
-rw-r--r-- | src/bin/termiolink.c | 11 |
3 files changed, 98 insertions, 32 deletions
diff --git a/src/bin/termio.c b/src/bin/termio.c index b092d0a..50818a6 100644 --- a/src/bin/termio.c +++ b/src/bin/termio.c | |||
@@ -1935,18 +1935,62 @@ end: | |||
1935 | /* }}} */ | 1935 | /* }}} */ |
1936 | /* {{{ Selection */ | 1936 | /* {{{ Selection */ |
1937 | 1937 | ||
1938 | struct termio_sb { | ||
1939 | char *buf; | ||
1940 | size_t len; | ||
1941 | size_t alloc; | ||
1942 | }; | ||
1943 | |||
1944 | static int | ||
1945 | _sb_add(struct termio_sb *sb, const char *s, size_t len) | ||
1946 | { | ||
1947 | size_t new_len = sb->len + len; | ||
1948 | |||
1949 | if (new_len >= sb->alloc) | ||
1950 | { | ||
1951 | size_t new_alloc = ((new_len + 15) / 16) * 24; | ||
1952 | char *new_buf; | ||
1953 | |||
1954 | new_buf = realloc(sb->buf, new_alloc); | ||
1955 | if (new_buf == NULL) | ||
1956 | return -1; | ||
1957 | sb->buf = new_buf; | ||
1958 | sb->alloc = new_alloc; | ||
1959 | } | ||
1960 | memcpy(sb->buf + sb->len, s, len); | ||
1961 | sb->len += len; | ||
1962 | sb->buf[sb->len] = '\0'; | ||
1963 | return 0; | ||
1964 | } | ||
1965 | |||
1966 | /* unlike eina_strbuf_rtrim, only trims \t, \f, ' ' */ | ||
1967 | void | ||
1968 | _sb_spaces_rtrim(struct termio_sb *sb) | ||
1969 | { | ||
1970 | if (!sb->buf) | ||
1971 | return; | ||
1972 | |||
1973 | while (sb->len > 0) | ||
1974 | { | ||
1975 | char c = sb->buf[sb->len - 1]; | ||
1976 | if ((c != ' ') && (c != '\t') && (c != '\f')) | ||
1977 | break; | ||
1978 | sb->len--; | ||
1979 | } | ||
1980 | sb->buf[sb->len] = '\0'; | ||
1981 | } | ||
1982 | |||
1983 | |||
1938 | char * | 1984 | char * |
1939 | termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | 1985 | termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, |
1940 | size_t *len) | 1986 | size_t *lenp, |
1987 | Eina_Bool rtrim) | ||
1941 | { | 1988 | { |
1942 | Termio *sd = evas_object_smart_data_get(obj); | 1989 | Termio *sd = evas_object_smart_data_get(obj); |
1943 | Eina_Strbuf *sb; | 1990 | struct termio_sb sb = {.buf = NULL, .len = 0, .alloc = 0}; |
1944 | char *s; | ||
1945 | int x, y; | 1991 | int x, y; |
1946 | size_t len_backup; | ||
1947 | 1992 | ||
1948 | EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL); | 1993 | EINA_SAFETY_ON_NULL_RETURN_VAL(sd, NULL); |
1949 | sb = eina_strbuf_new(); | ||
1950 | termpty_cellcomp_freeze(sd->pty); | 1994 | termpty_cellcomp_freeze(sd->pty); |
1951 | for (y = c1y; y <= c2y; y++) | 1995 | for (y = c1y; y <= c2y; y++) |
1952 | { | 1996 | { |
@@ -1960,7 +2004,9 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | |||
1960 | if (w > sd->grid.w) w = sd->grid.w; | 2004 | if (w > sd->grid.w) w = sd->grid.w; |
1961 | if (y == c1y && c1x >= w) | 2005 | if (y == c1y && c1x >= w) |
1962 | { | 2006 | { |
1963 | eina_strbuf_append_char(sb, '\n'); | 2007 | if (rtrim) |
2008 | _sb_spaces_rtrim(&sb); | ||
2009 | if (_sb_add(&sb, "\n", 1) < 0) goto err; | ||
1964 | continue; | 2010 | continue; |
1965 | } | 2011 | } |
1966 | start_x = c1x; | 2012 | start_x = c1x; |
@@ -1993,12 +2039,16 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | |||
1993 | { | 2039 | { |
1994 | last0 = -1; | 2040 | last0 = -1; |
1995 | if ((y != c2y) || (x != end_x)) | 2041 | if ((y != c2y) || (x != end_x)) |
1996 | eina_strbuf_append_char(sb, '\n'); | 2042 | { |
2043 | if (rtrim) | ||
2044 | _sb_spaces_rtrim(&sb); | ||
2045 | if (_sb_add(&sb, "\n", 1) < 0) goto err; | ||
2046 | } | ||
1997 | break; | 2047 | break; |
1998 | } | 2048 | } |
1999 | else if (cells[x].att.tab) | 2049 | else if (cells[x].att.tab) |
2000 | { | 2050 | { |
2001 | eina_strbuf_append_char(sb, '\t'); | 2051 | if (_sb_add(&sb, "\t", 1) < 0) goto err; |
2002 | x = ((x + 8) / 8) * 8; | 2052 | x = ((x + 8) / 8) * 8; |
2003 | x--; | 2053 | x--; |
2004 | } | 2054 | } |
@@ -2013,18 +2063,22 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | |||
2013 | last0 = -1; | 2063 | last0 = -1; |
2014 | while (v >= 0) | 2064 | while (v >= 0) |
2015 | { | 2065 | { |
2016 | eina_strbuf_append_char(sb, ' '); | 2066 | if (_sb_add(&sb, " ", 1) < 0) goto err; |
2017 | v--; | 2067 | v--; |
2018 | } | 2068 | } |
2019 | } | 2069 | } |
2020 | txtlen = codepoint_to_utf8(cells[x].codepoint, txt); | 2070 | txtlen = codepoint_to_utf8(cells[x].codepoint, txt); |
2021 | if (txtlen > 0) | 2071 | if (txtlen > 0) |
2022 | eina_strbuf_append_length(sb, txt, txtlen); | 2072 | if (_sb_add(&sb, txt, txtlen) < 0) goto err; |
2023 | if ((x == (w - 1)) && | 2073 | if ((x == (w - 1)) && |
2024 | ((x != c2x) || (y != c2y))) | 2074 | ((x != c2x) || (y != c2y))) |
2025 | { | 2075 | { |
2026 | if (!cells[x].att.autowrapped) | 2076 | if (!cells[x].att.autowrapped) |
2027 | eina_strbuf_append_char(sb, '\n'); | 2077 | { |
2078 | if (rtrim) | ||
2079 | _sb_spaces_rtrim(&sb); | ||
2080 | if (_sb_add(&sb, "\n", 1) < 0) goto err; | ||
2081 | } | ||
2028 | } | 2082 | } |
2029 | } | 2083 | } |
2030 | } | 2084 | } |
@@ -2053,7 +2107,12 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | |||
2053 | break; | 2107 | break; |
2054 | } | 2108 | } |
2055 | } | 2109 | } |
2056 | if (!have_more) eina_strbuf_append_char(sb, '\n'); | 2110 | if (!have_more) |
2111 | { | ||
2112 | if (rtrim) | ||
2113 | _sb_spaces_rtrim(&sb); | ||
2114 | if (_sb_add(&sb, "\n", 1) < 0) goto err; | ||
2115 | } | ||
2057 | else | 2116 | else |
2058 | { | 2117 | { |
2059 | for (x = last0; x <= end_x; x++) | 2118 | for (x = last0; x <= end_x; x++) |
@@ -2067,25 +2126,30 @@ termio_selection_get(Evas_Object *obj, int c1x, int c1y, int c2x, int c2y, | |||
2067 | } | 2126 | } |
2068 | #endif | 2127 | #endif |
2069 | if (x >= w) break; | 2128 | if (x >= w) break; |
2070 | eina_strbuf_append_char(sb, ' '); | 2129 | if (_sb_add(&sb, " ", 1) < 0) goto err; |
2071 | } | 2130 | } |
2072 | } | 2131 | } |
2073 | } | 2132 | } |
2074 | else eina_strbuf_append_char(sb, '\n'); | 2133 | else |
2134 | { | ||
2135 | if (rtrim) | ||
2136 | _sb_spaces_rtrim(&sb); | ||
2137 | if (_sb_add(&sb, "\n", 1) < 0) goto err; | ||
2138 | } | ||
2075 | } | 2139 | } |
2076 | } | 2140 | } |
2077 | termpty_cellcomp_thaw(sd->pty); | 2141 | termpty_cellcomp_thaw(sd->pty); |
2078 | 2142 | ||
2079 | if (!len) len = &len_backup; | 2143 | if (rtrim) |
2080 | *len = eina_strbuf_length_get(sb); | 2144 | _sb_spaces_rtrim(&sb); |
2081 | if (!*len) | 2145 | |
2082 | { | 2146 | if (lenp) |
2083 | eina_strbuf_free(sb); | 2147 | *lenp = sb.len; |
2084 | return NULL; | 2148 | |
2085 | } | 2149 | return sb.buf; |
2086 | s = eina_strbuf_string_steal(sb); | 2150 | err: |
2087 | eina_strbuf_free(sb); | 2151 | free(sb.buf); |
2088 | return s; | 2152 | return NULL; |
2089 | } | 2153 | } |
2090 | 2154 | ||
2091 | 2155 | ||
@@ -2212,7 +2276,7 @@ termio_take_selection(Evas_Object *obj, Elm_Sel_Type type) | |||
2212 | for (i = start_y; i <= end_y; i++) | 2276 | for (i = start_y; i <= end_y; i++) |
2213 | { | 2277 | { |
2214 | char *tmp = termio_selection_get(obj, start_x, i, end_x, i, | 2278 | char *tmp = termio_selection_get(obj, start_x, i, end_x, i, |
2215 | &len); | 2279 | &len, EINA_TRUE); |
2216 | 2280 | ||
2217 | if (tmp) | 2281 | if (tmp) |
2218 | { | 2282 | { |
@@ -2233,7 +2297,8 @@ termio_take_selection(Evas_Object *obj, Elm_Sel_Type type) | |||
2233 | } | 2297 | } |
2234 | else if ((start_x != end_x) || (start_y != end_y)) | 2298 | else if ((start_x != end_x) || (start_y != end_y)) |
2235 | { | 2299 | { |
2236 | s = termio_selection_get(obj, start_x, start_y, end_x, end_y, &len); | 2300 | s = termio_selection_get(obj, start_x, start_y, end_x, end_y, &len, |
2301 | EINA_TRUE); | ||
2237 | } | 2302 | } |
2238 | 2303 | ||
2239 | if (s) | 2304 | if (s) |
diff --git a/src/bin/termio.h b/src/bin/termio.h index d18ebaf..56dfeb7 100644 --- a/src/bin/termio.h +++ b/src/bin/termio.h | |||
@@ -13,7 +13,7 @@ void termio_theme_set(Evas_Object *obj, Evas_Object *theme); | |||
13 | Evas_Object *termio_theme_get(Evas_Object *obj); | 13 | Evas_Object *termio_theme_get(Evas_Object *obj); |
14 | char *termio_selection_get(Evas_Object *obj, | 14 | char *termio_selection_get(Evas_Object *obj, |
15 | int c1x, int c1y, int c2x, int c2y, | 15 | int c1x, int c1y, int c2x, int c2y, |
16 | size_t *len); | 16 | size_t *len, Eina_Bool right_trim); |
17 | Eina_Bool termio_selection_exists(const Evas_Object *obj); | 17 | Eina_Bool termio_selection_exists(const Evas_Object *obj); |
18 | void termio_scroll_delta(Evas_Object *obj, int delta, int by_page); | 18 | void termio_scroll_delta(Evas_Object *obj, int delta, int by_page); |
19 | void termio_scroll_set(Evas_Object *obj, int scroll); | 19 | void termio_scroll_set(Evas_Object *obj, int scroll); |
diff --git a/src/bin/termiolink.c b/src/bin/termiolink.c index ad6542d..c0151b1 100644 --- a/src/bin/termiolink.c +++ b/src/bin/termiolink.c | |||
@@ -112,7 +112,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, | |||
112 | for (;;) | 112 | for (;;) |
113 | { | 113 | { |
114 | prev_len = len; | 114 | prev_len = len; |
115 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len); | 115 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len, EINA_FALSE); |
116 | if (!s) break; | 116 | if (!s) break; |
117 | if (goback) | 117 | if (goback) |
118 | { | 118 | { |
@@ -125,7 +125,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, | |||
125 | coord_back(&x1, &y1, w, h); | 125 | coord_back(&x1, &y1, w, h); |
126 | free(s); | 126 | free(s); |
127 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, | 127 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, |
128 | &len); | 128 | &len, EINA_FALSE); |
129 | if (!s) break; | 129 | if (!s) break; |
130 | switch (s[0]) | 130 | switch (s[0]) |
131 | { | 131 | { |
@@ -142,7 +142,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, | |||
142 | free(s); | 142 | free(s); |
143 | prev_len = len; | 143 | prev_len = len; |
144 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, | 144 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, |
145 | &len); | 145 | &len, EINA_FALSE); |
146 | if (!s) break; | 146 | if (!s) break; |
147 | } | 147 | } |
148 | else | 148 | else |
@@ -164,7 +164,7 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, | |||
164 | goforward = EINA_TRUE; | 164 | goforward = EINA_TRUE; |
165 | free(s); | 165 | free(s); |
166 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, | 166 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, |
167 | &len); | 167 | &len, EINA_FALSE); |
168 | if (!s) break; | 168 | if (!s) break; |
169 | } | 169 | } |
170 | } | 170 | } |
@@ -197,7 +197,8 @@ _termio_link_find(Evas_Object *obj, int cx, int cy, | |||
197 | if ((!goback) && (!goforward)) | 197 | if ((!goback) && (!goforward)) |
198 | { | 198 | { |
199 | free(s); | 199 | free(s); |
200 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len); | 200 | s = termio_selection_get(obj, x1, y1 - sc, x2, y2 - sc, &len, |
201 | EINA_FALSE); | ||
201 | break; | 202 | break; |
202 | } | 203 | } |
203 | free(s); | 204 | free(s); |