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