Fri, 19 Mar 2004 17:34:33 +0000
[gaim-migrate @ 9206]
" You can once again show how evil you are by typing >:)
and getting it to render in spite of escaped HTML.
This patch changes around the parsing code to catch
smileys before eating just any HTML entity we bump into
on the street. We try to catch entities at the
beginning of smileys first, and if we're sure they're
not smileys, then we eat them for breakfast. The patch
also deals with eating any subsequent entities that may
appear in any smileys (like :-&) so we don't end up
with trailing leftovers. This patch description is
making me hungry.
FYI, I know this gtkimhtml is supposed to be not gaim
dependent, but both the gaim_* functions that were
preexisting and newly used in gtkimhtml code are all
non-gaim dependent utility functions from util.c, so I felt
their use was justified and acceptable." --Kevin Stange
committer: Luke Schierer <lschiere@pidgin.im>
| 7358 | 1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 | * gtksourceiter.h | |
| 3 | * | |
| 8046 | 4 | * Gaim is the legal property of its developers, whose names are too numerous |
| 5 | * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 6 | * source distribution. | |
| 7358 | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify | |
| 9 | * it under the terms of the GNU Library General Public License as published by | |
| 10 | * the Free Software Foundation; either version 2 of the License, or | |
| 11 | * (at your option) any later version. | |
| 12 | * | |
| 13 | * This program is distributed in the hope that it will be useful, | |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 | * GNU Library General Public License for more details. | |
| 17 | * | |
| 18 | * You should have received a copy of the GNU Library General Public License | |
| 19 | * along with this program; if not, write to the Free Software | |
| 20 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 21 | */ | |
| 22 | ||
| 23 | /* | |
| 24 | * Parts of this file are copied from the gedit and glimmer project. | |
| 25 | */ | |
| 26 | ||
| 27 | #ifdef HAVE_CONFIG_H | |
| 28 | #include <config.h> | |
| 29 | #endif | |
| 30 | ||
| 31 | #include <string.h> | |
| 32 | #include "gtksourceiter.h" | |
| 33 | ||
| 34 | #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC | |
| 35 | ||
| 36 | static gchar * | |
| 37 | g_utf8_strcasestr (const gchar *haystack, const gchar *needle) | |
| 38 | { | |
| 39 | gsize needle_len; | |
| 40 | gsize haystack_len; | |
| 41 | gchar *ret = NULL; | |
| 42 | gchar *p; | |
| 43 | gchar *casefold; | |
| 44 | gchar *caseless_haystack; | |
| 45 | gint i; | |
| 46 | ||
| 47 | g_return_val_if_fail (haystack != NULL, NULL); | |
| 48 | g_return_val_if_fail (needle != NULL, NULL); | |
| 49 | ||
| 50 | casefold = g_utf8_casefold (haystack, -1); | |
| 51 | caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 52 | g_free (casefold); | |
| 53 | ||
| 54 | needle_len = g_utf8_strlen (needle, -1); | |
| 55 | haystack_len = g_utf8_strlen (caseless_haystack, -1); | |
| 56 | ||
| 57 | if (needle_len == 0) | |
| 58 | { | |
| 59 | ret = (gchar *)haystack; | |
| 60 | goto finally_1; | |
| 61 | } | |
| 62 | ||
| 63 | if (haystack_len < needle_len) | |
| 64 | { | |
| 65 | ret = NULL; | |
| 66 | goto finally_1; | |
| 67 | } | |
| 68 | ||
| 69 | p = (gchar*)caseless_haystack; | |
| 70 | needle_len = strlen (needle); | |
| 71 | i = 0; | |
| 72 | ||
| 73 | while (*p) | |
| 74 | { | |
| 75 | if ((strncmp (p, needle, needle_len) == 0)) | |
| 76 | { | |
| 77 | ret = g_utf8_offset_to_pointer (haystack, i); | |
| 78 | goto finally_1; | |
| 79 | } | |
| 80 | ||
| 81 | p = g_utf8_next_char (p); | |
| 82 | i++; | |
| 83 | } | |
| 84 | ||
| 85 | finally_1: | |
| 86 | g_free (caseless_haystack); | |
| 87 | ||
| 88 | return ret; | |
| 89 | } | |
| 90 | ||
| 91 | static gchar * | |
| 92 | g_utf8_strrcasestr (const gchar *haystack, const gchar *needle) | |
| 93 | { | |
| 94 | gsize needle_len; | |
| 95 | gsize haystack_len; | |
| 96 | gchar *ret = NULL; | |
| 97 | gchar *p; | |
| 98 | gchar *casefold; | |
| 99 | gchar *caseless_haystack; | |
| 100 | gint i; | |
| 101 | ||
| 102 | g_return_val_if_fail (haystack != NULL, NULL); | |
| 103 | g_return_val_if_fail (needle != NULL, NULL); | |
| 104 | ||
| 105 | casefold = g_utf8_casefold (haystack, -1); | |
| 106 | caseless_haystack = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 107 | g_free (casefold); | |
| 108 | ||
| 109 | needle_len = g_utf8_strlen (needle, -1); | |
| 110 | haystack_len = g_utf8_strlen (caseless_haystack, -1); | |
| 111 | ||
| 112 | if (needle_len == 0) | |
| 113 | { | |
| 114 | ret = (gchar *)haystack; | |
| 115 | goto finally_1; | |
| 116 | } | |
| 117 | ||
| 118 | if (haystack_len < needle_len) | |
| 119 | { | |
| 120 | ret = NULL; | |
| 121 | goto finally_1; | |
| 122 | } | |
| 123 | ||
| 124 | haystack_len = strlen (caseless_haystack); | |
| 125 | needle_len = strlen (needle); | |
| 126 | p = (gchar *)caseless_haystack + haystack_len - needle_len; | |
| 127 | i = haystack_len - needle_len; | |
| 128 | ||
| 129 | while (p >= caseless_haystack) | |
| 130 | { | |
| 131 | if (strncasecmp (p, needle, needle_len) == 0) | |
| 132 | { | |
| 133 | ret = g_utf8_offset_to_pointer (haystack, i); | |
| 134 | goto finally_1; | |
| 135 | } | |
| 136 | ||
| 137 | p = g_utf8_prev_char (p); | |
| 138 | i--; | |
| 139 | } | |
| 140 | ||
| 141 | finally_1: | |
| 142 | g_free (caseless_haystack); | |
| 143 | ||
| 144 | return ret; | |
| 145 | } | |
| 146 | ||
| 147 | static gboolean | |
| 148 | g_utf8_caselessnmatch (const char *s1, const char *s2, | |
| 149 | gssize n1, gssize n2) | |
| 150 | { | |
| 151 | gchar *casefold; | |
| 152 | gchar *normalized_s1; | |
| 153 | gchar *normalized_s2; | |
| 154 | gint len_s1; | |
| 155 | gint len_s2; | |
| 156 | gboolean ret = FALSE; | |
| 157 | ||
| 158 | g_return_val_if_fail (s1 != NULL, FALSE); | |
| 159 | g_return_val_if_fail (s2 != NULL, FALSE); | |
| 160 | g_return_val_if_fail (n1 > 0, FALSE); | |
| 161 | g_return_val_if_fail (n2 > 0, FALSE); | |
| 162 | ||
| 163 | casefold = g_utf8_casefold (s1, n1); | |
| 164 | normalized_s1 = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 165 | g_free (casefold); | |
| 166 | ||
| 167 | casefold = g_utf8_casefold (s2, n2); | |
| 168 | normalized_s2 = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 169 | g_free (casefold); | |
| 170 | ||
| 171 | len_s1 = strlen (normalized_s1); | |
| 172 | len_s2 = strlen (normalized_s2); | |
| 173 | ||
| 174 | if (len_s1 < len_s2) | |
| 175 | goto finally_2; | |
| 176 | ||
| 177 | ret = (strncmp (normalized_s1, normalized_s2, len_s2) == 0); | |
| 178 | ||
| 179 | finally_2: | |
| 180 | g_free (normalized_s1); | |
| 181 | g_free (normalized_s2); | |
| 182 | ||
| 183 | return ret; | |
| 184 | } | |
| 185 | ||
| 186 | static void | |
| 187 | forward_chars_with_skipping (GtkTextIter *iter, | |
| 188 | gint count, | |
| 189 | gboolean skip_invisible, | |
| 190 | gboolean skip_nontext) | |
| 191 | { | |
| 192 | gint i; | |
| 193 | ||
| 194 | g_return_if_fail (count >= 0); | |
| 195 | ||
| 196 | i = count; | |
| 197 | ||
| 198 | while (i > 0) | |
| 199 | { | |
| 200 | gboolean ignored = FALSE; | |
| 201 | ||
| 202 | if (skip_nontext && gtk_text_iter_get_char (iter) == GTK_TEXT_UNKNOWN_CHAR) | |
| 203 | ignored = TRUE; | |
| 204 | ||
| 205 | if (!ignored && skip_invisible && | |
| 206 | /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE) | |
| 207 | ignored = TRUE; | |
| 208 | ||
| 209 | gtk_text_iter_forward_char (iter); | |
| 210 | ||
| 211 | if (!ignored) | |
| 212 | --i; | |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 216 | static gboolean | |
| 217 | lines_match (const GtkTextIter *start, | |
| 218 | const gchar **lines, | |
| 219 | gboolean visible_only, | |
| 220 | gboolean slice, | |
| 221 | GtkTextIter *match_start, | |
| 222 | GtkTextIter *match_end) | |
| 223 | { | |
| 224 | GtkTextIter next; | |
| 225 | gchar *line_text; | |
| 226 | const gchar *found; | |
| 227 | gint offset; | |
| 228 | ||
| 229 | if (*lines == NULL || **lines == '\0') | |
| 230 | { | |
| 231 | if (match_start) | |
| 232 | *match_start = *start; | |
| 233 | if (match_end) | |
| 234 | *match_end = *start; | |
| 235 | return TRUE; | |
| 236 | } | |
| 237 | ||
| 238 | next = *start; | |
| 239 | gtk_text_iter_forward_line (&next); | |
| 240 | ||
| 241 | /* No more text in buffer, but *lines is nonempty */ | |
| 242 | if (gtk_text_iter_equal (start, &next)) | |
| 243 | return FALSE; | |
| 244 | ||
| 245 | if (slice) | |
| 246 | { | |
| 247 | if (visible_only) | |
| 248 | line_text = gtk_text_iter_get_visible_slice (start, &next); | |
| 249 | else | |
| 250 | line_text = gtk_text_iter_get_slice (start, &next); | |
| 251 | } | |
| 252 | else | |
| 253 | { | |
| 254 | if (visible_only) | |
| 255 | line_text = gtk_text_iter_get_visible_text (start, &next); | |
| 256 | else | |
| 257 | line_text = gtk_text_iter_get_text (start, &next); | |
| 258 | } | |
| 259 | ||
| 260 | if (match_start) /* if this is the first line we're matching */ | |
| 261 | { | |
| 262 | found = g_utf8_strcasestr (line_text, *lines); | |
| 263 | } | |
| 264 | else | |
| 265 | { | |
| 266 | /* If it's not the first line, we have to match from the | |
| 267 | * start of the line. | |
| 268 | */ | |
| 269 | if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text), | |
| 270 | strlen (*lines))) | |
| 271 | found = line_text; | |
| 272 | else | |
| 273 | found = NULL; | |
| 274 | } | |
| 275 | ||
| 276 | if (found == NULL) | |
| 277 | { | |
| 278 | g_free (line_text); | |
| 279 | return FALSE; | |
| 280 | } | |
| 281 | ||
| 282 | /* Get offset to start of search string */ | |
| 283 | offset = g_utf8_strlen (line_text, found - line_text); | |
| 284 | ||
| 285 | next = *start; | |
| 286 | ||
| 287 | /* If match start needs to be returned, set it to the | |
| 288 | * start of the search string. | |
| 289 | */ | |
| 290 | if (match_start) | |
| 291 | { | |
| 292 | *match_start = next; | |
| 293 | ||
| 294 | forward_chars_with_skipping (match_start, offset, | |
| 295 | visible_only, !slice); | |
| 296 | } | |
| 297 | ||
| 298 | /* Go to end of search string */ | |
| 299 | offset += g_utf8_strlen (*lines, -1); | |
| 300 | ||
| 301 | forward_chars_with_skipping (&next, offset, visible_only, !slice); | |
| 302 | ||
| 303 | g_free (line_text); | |
| 304 | ||
| 305 | ++lines; | |
| 306 | ||
| 307 | if (match_end) | |
| 308 | *match_end = next; | |
| 309 | ||
| 310 | /* pass NULL for match_start, since we don't need to find the | |
| 311 | * start again. | |
| 312 | */ | |
| 313 | return lines_match (&next, lines, visible_only, slice, NULL, match_end); | |
| 314 | } | |
| 315 | ||
| 316 | static gboolean | |
| 317 | backward_lines_match (const GtkTextIter *start, | |
| 318 | const gchar **lines, | |
| 319 | gboolean visible_only, | |
| 320 | gboolean slice, | |
| 321 | GtkTextIter *match_start, | |
| 322 | GtkTextIter *match_end) | |
| 323 | { | |
| 324 | GtkTextIter line, next; | |
| 325 | gchar *line_text; | |
| 326 | const gchar *found; | |
| 327 | gint offset; | |
| 328 | ||
| 329 | if (*lines == NULL || **lines == '\0') | |
| 330 | { | |
| 331 | if (match_start) | |
| 332 | *match_start = *start; | |
| 333 | if (match_end) | |
| 334 | *match_end = *start; | |
| 335 | return TRUE; | |
| 336 | } | |
| 337 | ||
| 338 | line = next = *start; | |
| 339 | if (gtk_text_iter_get_line_offset (&next) == 0) | |
| 340 | { | |
| 341 | if (!gtk_text_iter_backward_line (&next)) | |
| 342 | return FALSE; | |
| 343 | } | |
| 344 | else | |
| 345 | gtk_text_iter_set_line_offset (&next, 0); | |
| 346 | ||
| 347 | if (slice) | |
| 348 | { | |
| 349 | if (visible_only) | |
| 350 | line_text = gtk_text_iter_get_visible_slice (&next, &line); | |
| 351 | else | |
| 352 | line_text = gtk_text_iter_get_slice (&next, &line); | |
| 353 | } | |
| 354 | else | |
| 355 | { | |
| 356 | if (visible_only) | |
| 357 | line_text = gtk_text_iter_get_visible_text (&next, &line); | |
| 358 | else | |
| 359 | line_text = gtk_text_iter_get_text (&next, &line); | |
| 360 | } | |
| 361 | ||
| 362 | if (match_start) /* if this is the first line we're matching */ | |
| 363 | { | |
| 364 | found = g_utf8_strrcasestr (line_text, *lines); | |
| 365 | } | |
| 366 | else | |
| 367 | { | |
| 368 | /* If it's not the first line, we have to match from the | |
| 369 | * start of the line. | |
| 370 | */ | |
| 371 | if (g_utf8_caselessnmatch (line_text, *lines, strlen (line_text), | |
| 372 | strlen (*lines))) | |
| 373 | found = line_text; | |
| 374 | else | |
| 375 | found = NULL; | |
| 376 | } | |
| 377 | ||
| 378 | if (found == NULL) | |
| 379 | { | |
| 380 | g_free (line_text); | |
| 381 | return FALSE; | |
| 382 | } | |
| 383 | ||
| 384 | /* Get offset to start of search string */ | |
| 385 | offset = g_utf8_strlen (line_text, found - line_text); | |
| 386 | ||
| 387 | /* If match start needs to be returned, set it to the | |
| 388 | * start of the search string. | |
| 389 | */ | |
| 390 | if (match_start) | |
| 391 | { | |
| 392 | *match_start = next; | |
| 393 | gtk_text_iter_set_visible_line_offset (match_start, offset); | |
| 394 | } | |
| 395 | ||
| 396 | /* Go to end of search string */ | |
| 397 | offset += g_utf8_strlen (*lines, -1); | |
| 398 | ||
| 399 | forward_chars_with_skipping (&next, offset, visible_only, !slice); | |
| 400 | ||
| 401 | g_free (line_text); | |
| 402 | ||
| 403 | ++lines; | |
| 404 | ||
| 405 | if (match_end) | |
| 406 | *match_end = next; | |
| 407 | ||
| 408 | /* try to match the rest of the lines forward, passing NULL | |
| 409 | * for match_start so lines_match will try to match the entire | |
| 410 | * line */ | |
| 411 | return lines_match (&next, lines, visible_only, | |
| 412 | slice, NULL, match_end); | |
| 413 | } | |
| 414 | ||
| 415 | /* strsplit () that retains the delimiter as part of the string. */ | |
| 416 | static gchar ** | |
| 417 | strbreakup (const char *string, | |
| 418 | const char *delimiter, | |
| 419 | gint max_tokens) | |
| 420 | { | |
| 421 | GSList *string_list = NULL, *slist; | |
| 422 | gchar **str_array, *s, *casefold, *new_string; | |
| 423 | guint i, n = 1; | |
| 424 | ||
| 425 | g_return_val_if_fail (string != NULL, NULL); | |
| 426 | g_return_val_if_fail (delimiter != NULL, NULL); | |
| 427 | ||
| 428 | if (max_tokens < 1) | |
| 429 | max_tokens = G_MAXINT; | |
| 430 | ||
| 431 | s = strstr (string, delimiter); | |
| 432 | if (s) | |
| 433 | { | |
| 434 | guint delimiter_len = strlen (delimiter); | |
| 435 | ||
| 436 | do | |
| 437 | { | |
| 438 | guint len; | |
| 439 | ||
| 440 | len = s - string + delimiter_len; | |
| 441 | new_string = g_new (gchar, len + 1); | |
| 442 | strncpy (new_string, string, len); | |
| 443 | new_string[len] = 0; | |
| 444 | casefold = g_utf8_casefold (new_string, -1); | |
| 445 | g_free (new_string); | |
| 446 | new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 447 | g_free (casefold); | |
| 448 | string_list = g_slist_prepend (string_list, new_string); | |
| 449 | n++; | |
| 450 | string = s + delimiter_len; | |
| 451 | s = strstr (string, delimiter); | |
| 452 | } while (--max_tokens && s); | |
| 453 | } | |
| 454 | ||
| 455 | if (*string) | |
| 456 | { | |
| 457 | n++; | |
| 458 | casefold = g_utf8_casefold (string, -1); | |
| 459 | new_string = g_utf8_normalize (casefold, -1, G_NORMALIZE_ALL); | |
| 460 | g_free (casefold); | |
| 461 | string_list = g_slist_prepend (string_list, new_string); | |
| 462 | } | |
| 463 | ||
| 464 | str_array = g_new (gchar*, n); | |
| 465 | ||
| 466 | i = n - 1; | |
| 467 | ||
| 468 | str_array[i--] = NULL; | |
| 469 | for (slist = string_list; slist; slist = slist->next) | |
| 470 | str_array[i--] = slist->data; | |
| 471 | ||
| 472 | g_slist_free (string_list); | |
| 473 | ||
| 474 | return str_array; | |
| 475 | } | |
| 476 | ||
| 477 | /** | |
| 478 | * gtk_source_iter_forward_search: | |
| 479 | * @iter: start of search | |
| 480 | * @str: a search string | |
| 481 | * @flags: flags affecting how the search is done | |
| 482 | * @match_start: return location for start of match, or %NULL | |
| 483 | * @match_end: return location for end of match, or %NULL | |
| 484 | * @limit: bound for the search, or %NULL for the end of the buffer | |
| 485 | * | |
| 486 | * Searches forward for @str. Any match is returned by setting | |
| 487 | * @match_start to the first character of the match and @match_end to the | |
| 488 | * first character after the match. The search will not continue past | |
| 489 | * @limit. Note that a search is a linear or O(n) operation, so you | |
| 490 | * may wish to use @limit to avoid locking up your UI on large | |
| 491 | * buffers. | |
| 492 | * | |
| 493 | * If the #GTK_SOURCE_SEARCH_VISIBLE_ONLY flag is present, the match may | |
| 494 | * have invisible text interspersed in @str. i.e. @str will be a | |
| 495 | * possibly-noncontiguous subsequence of the matched range. similarly, | |
| 496 | * if you specify #GTK_SOURCE_SEARCH_TEXT_ONLY, the match may have | |
| 497 | * pixbufs or child widgets mixed inside the matched range. If these | |
| 498 | * flags are not given, the match must be exact; the special 0xFFFC | |
| 499 | * character in @str will match embedded pixbufs or child widgets. | |
| 500 | * If you specify the #GTK_SOURCE_SEARCH_CASE_INSENSITIVE flag, the text will | |
| 501 | * be matched regardless of what case it is in. | |
| 502 | * | |
| 503 | * Same as gtk_text_iter_forward_search(), but supports case insensitive | |
| 504 | * searching. | |
| 505 | * | |
| 506 | * Return value: whether a match was found | |
| 507 | **/ | |
| 508 | gboolean | |
| 509 | gtk_source_iter_forward_search (const GtkTextIter *iter, | |
| 510 | const gchar *str, | |
| 511 | GtkSourceSearchFlags flags, | |
| 512 | GtkTextIter *match_start, | |
| 513 | GtkTextIter *match_end, | |
| 514 | const GtkTextIter *limit) | |
| 515 | { | |
| 516 | gchar **lines = NULL; | |
| 517 | GtkTextIter match; | |
| 518 | gboolean retval = FALSE; | |
| 519 | GtkTextIter search; | |
| 520 | gboolean visible_only; | |
| 521 | gboolean slice; | |
| 522 | ||
| 523 | g_return_val_if_fail (iter != NULL, FALSE); | |
| 524 | g_return_val_if_fail (str != NULL, FALSE); | |
| 525 | ||
| 526 | if ((flags & GTK_SOURCE_SEARCH_CASE_INSENSITIVE) == 0) | |
| 527 | return gtk_text_iter_forward_search (iter, str, flags, | |
| 528 | match_start, match_end, | |
| 529 | limit); | |
| 530 | ||
| 531 | if (limit && gtk_text_iter_compare (iter, limit) >= 0) | |
| 532 | return FALSE; | |
| 533 | ||
| 534 | if (*str == '\0') | |
| 535 | { | |
| 536 | /* If we can move one char, return the empty string there */ | |
| 537 | match = *iter; | |
| 538 | ||
| 539 | if (gtk_text_iter_forward_char (&match)) | |
| 540 | { | |
| 541 | if (limit && gtk_text_iter_equal (&match, limit)) | |
| 542 | return FALSE; | |
| 543 | ||
| 544 | if (match_start) | |
| 545 | *match_start = match; | |
| 546 | if (match_end) | |
| 547 | *match_end = match; | |
| 548 | return TRUE; | |
| 549 | } | |
| 550 | else | |
| 551 | { | |
| 552 | return FALSE; | |
| 553 | } | |
| 554 | } | |
| 555 | ||
| 556 | visible_only = (flags & GTK_SOURCE_SEARCH_VISIBLE_ONLY) != 0; | |
| 557 | slice = (flags & GTK_SOURCE_SEARCH_TEXT_ONLY) == 0; | |
| 558 | ||
| 559 | /* locate all lines */ | |
| 560 | lines = strbreakup (str, "\n", -1); | |
| 561 | ||
| 562 | search = *iter; | |
| 563 | ||
| 564 | do | |
| 565 | { | |
| 566 | /* This loop has an inefficient worst-case, where | |
| 567 | * gtk_text_iter_get_text () is called repeatedly on | |
| 568 | * a single line. | |
| 569 | */ | |
| 570 | GtkTextIter end; | |
| 571 | ||
| 572 | if (limit && gtk_text_iter_compare (&search, limit) >= 0) | |
| 573 | break; | |
| 574 | ||
| 575 | if (lines_match (&search, (const gchar**)lines, | |
| 576 | visible_only, slice, &match, &end)) | |
| 577 | { | |
| 578 | if (limit == NULL || (limit && | |
| 579 | gtk_text_iter_compare (&end, limit) < 0)) | |
| 580 | { | |
| 581 | retval = TRUE; | |
| 582 | ||
| 583 | if (match_start) | |
| 584 | *match_start = match; | |
| 585 | if (match_end) | |
| 586 | *match_end = end; | |
| 587 | } | |
| 588 | break; | |
| 589 | } | |
| 590 | } while (gtk_text_iter_forward_line (&search)); | |
| 591 | ||
| 592 | g_strfreev ((gchar**)lines); | |
| 593 | ||
| 594 | return retval; | |
| 595 | } | |
| 596 | ||
| 597 | /** | |
| 598 | * gtk_source_iter_backward_search: | |
| 599 | * @iter: a #GtkTextIter where the search begins | |
| 600 | * @str: search string | |
| 601 | * @flags: bitmask of flags affecting the search | |
| 602 | * @match_start: return location for start of match, or %NULL | |
| 603 | * @match_end: return location for end of match, or %NULL | |
| 604 | * @limit: location of last possible @match_start, or %NULL for start of buffer | |
| 605 | * | |
| 606 | * Same as gtk_text_iter_backward_search(), but supports case insensitive | |
| 607 | * searching. | |
| 608 | * | |
| 609 | * Return value: whether a match was found | |
| 610 | **/ | |
| 611 | gboolean | |
| 612 | gtk_source_iter_backward_search (const GtkTextIter *iter, | |
| 613 | const gchar *str, | |
| 614 | GtkSourceSearchFlags flags, | |
| 615 | GtkTextIter *match_start, | |
| 616 | GtkTextIter *match_end, | |
| 617 | const GtkTextIter *limit) | |
| 618 | { | |
| 619 | gchar **lines = NULL; | |
| 620 | GtkTextIter match; | |
| 621 | gboolean retval = FALSE; | |
| 622 | GtkTextIter search; | |
| 623 | gboolean visible_only; | |
| 624 | gboolean slice; | |
| 625 | ||
| 626 | g_return_val_if_fail (iter != NULL, FALSE); | |
| 627 | g_return_val_if_fail (str != NULL, FALSE); | |
| 628 | ||
| 629 | if ((flags & GTK_SOURCE_SEARCH_CASE_INSENSITIVE) == 0) | |
| 630 | return gtk_text_iter_backward_search (iter, str, flags, | |
| 631 | match_start, match_end, | |
| 632 | limit); | |
| 633 | ||
| 634 | if (limit && gtk_text_iter_compare (iter, limit) <= 0) | |
| 635 | return FALSE; | |
| 636 | ||
| 637 | if (*str == '\0') | |
| 638 | { | |
| 639 | /* If we can move one char, return the empty string there */ | |
| 640 | match = *iter; | |
| 641 | ||
| 642 | if (gtk_text_iter_backward_char (&match)) | |
| 643 | { | |
| 644 | if (limit && gtk_text_iter_equal (&match, limit)) | |
| 645 | return FALSE; | |
| 646 | ||
| 647 | if (match_start) | |
| 648 | *match_start = match; | |
| 649 | if (match_end) | |
| 650 | *match_end = match; | |
| 651 | return TRUE; | |
| 652 | } | |
| 653 | else | |
| 654 | { | |
| 655 | return FALSE; | |
| 656 | } | |
| 657 | } | |
| 658 | ||
| 659 | visible_only = (flags & GTK_SOURCE_SEARCH_VISIBLE_ONLY) != 0; | |
| 660 | slice = (flags & GTK_SOURCE_SEARCH_TEXT_ONLY) == 0; | |
| 661 | ||
| 662 | /* locate all lines */ | |
| 663 | lines = strbreakup (str, "\n", -1); | |
| 664 | ||
| 665 | search = *iter; | |
| 666 | ||
| 667 | while (TRUE) | |
| 668 | { | |
| 669 | /* This loop has an inefficient worst-case, where | |
| 670 | * gtk_text_iter_get_text () is called repeatedly on | |
| 671 | * a single line. | |
| 672 | */ | |
| 673 | GtkTextIter end; | |
| 674 | ||
| 675 | if (limit && gtk_text_iter_compare (&search, limit) <= 0) | |
| 676 | break; | |
| 677 | ||
| 678 | if (backward_lines_match (&search, (const gchar**)lines, | |
| 679 | visible_only, slice, &match, &end)) | |
| 680 | { | |
| 681 | if (limit == NULL || (limit && | |
| 682 | gtk_text_iter_compare (&end, limit) > 0)) | |
| 683 | { | |
| 684 | retval = TRUE; | |
| 685 | ||
| 686 | if (match_start) | |
| 687 | *match_start = match; | |
| 688 | if (match_end) | |
| 689 | *match_end = end; | |
| 690 | } | |
| 691 | break; | |
| 692 | } | |
| 693 | ||
| 694 | if (gtk_text_iter_get_line_offset (&search) == 0) | |
| 695 | { | |
| 696 | if (!gtk_text_iter_backward_line (&search)) | |
| 697 | break; | |
| 698 | } | |
| 699 | else | |
| 700 | { | |
| 701 | gtk_text_iter_set_line_offset (&search, 0); | |
| 702 | } | |
| 703 | } | |
| 704 | ||
| 705 | g_strfreev ((gchar**)lines); | |
| 706 | ||
| 707 | return retval; | |
| 708 | } | |
| 709 | ||
| 710 | /* | |
| 711 | * gtk_source_iter_find_matching_bracket is implemented in gtksourcebuffer.c | |
| 712 | */ |