1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
4 * Pidgin 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
8 * The following copyright notice applies to this file:
10 * Copyright (C) 2000 - 2005 Paolo Maggi
11 * Copyright (C) 2002, 2003 Jeroen Zwartepoorte
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Library General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301, USA.
29 * Parts of this file are copied from the gedit and glimmer project.
37 #include "gtksourceiter.h"
39 #define GTK_TEXT_UNKNOWN_CHAR 0xFFFC
41 /* this function acts like g_utf8_offset_to_pointer() except that if it finds a
42 * decomposable character it consumes the decomposition length from the given
43 * offset. So it's useful when the offset was calculated for the normalized
44 * version of str, but we need a pointer to str itself. */
46 pointer_from_offset_skipping_decomp (const gchar
*str
, gint offset
)
48 gchar
*casefold
, *normal
;
54 q
= g_utf8_next_char (p
);
55 casefold
= g_utf8_casefold (p
, q
- p
);
56 normal
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
57 offset
-= g_utf8_strlen (normal
, -1);
66 g_utf8_strcasestr (const gchar
*haystack
, const gchar
*needle
)
70 const gchar
*ret
= NULL
;
73 gchar
*caseless_haystack
;
76 g_return_val_if_fail (haystack
!= NULL
, NULL
);
77 g_return_val_if_fail (needle
!= NULL
, NULL
);
79 casefold
= g_utf8_casefold (haystack
, -1);
80 caseless_haystack
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
83 needle_len
= g_utf8_strlen (needle
, -1);
84 haystack_len
= g_utf8_strlen (caseless_haystack
, -1);
88 ret
= (gchar
*)haystack
;
92 if (haystack_len
< needle_len
)
98 p
= (gchar
*)caseless_haystack
;
99 needle_len
= strlen (needle
);
104 if ((strncmp (p
, needle
, needle_len
) == 0))
106 ret
= pointer_from_offset_skipping_decomp (haystack
, i
);
110 p
= g_utf8_next_char (p
);
115 g_free (caseless_haystack
);
121 g_utf8_strrcasestr (const gchar
*haystack
, const gchar
*needle
)
125 const gchar
*ret
= NULL
;
128 gchar
*caseless_haystack
;
131 g_return_val_if_fail (haystack
!= NULL
, NULL
);
132 g_return_val_if_fail (needle
!= NULL
, NULL
);
134 casefold
= g_utf8_casefold (haystack
, -1);
135 caseless_haystack
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
138 needle_len
= g_utf8_strlen (needle
, -1);
139 haystack_len
= g_utf8_strlen (caseless_haystack
, -1);
143 ret
= (gchar
*)haystack
;
147 if (haystack_len
< needle_len
)
153 i
= haystack_len
- needle_len
;
154 p
= g_utf8_offset_to_pointer (caseless_haystack
, i
);
155 needle_len
= strlen (needle
);
159 if (strncmp (p
, needle
, needle_len
) == 0)
161 ret
= pointer_from_offset_skipping_decomp (haystack
, i
);
165 if (p
> caseless_haystack
)
166 p
= g_utf8_prev_char (p
);
174 g_free (caseless_haystack
);
180 g_utf8_caselessnmatch (const char *s1
, const char *s2
,
181 gssize n1
, gssize n2
)
184 gchar
*normalized_s1
;
185 gchar
*normalized_s2
;
188 gboolean ret
= FALSE
;
190 g_return_val_if_fail (s1
!= NULL
, FALSE
);
191 g_return_val_if_fail (s2
!= NULL
, FALSE
);
192 g_return_val_if_fail (n1
> 0, FALSE
);
193 g_return_val_if_fail (n2
> 0, FALSE
);
195 casefold
= g_utf8_casefold (s1
, n1
);
196 normalized_s1
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
199 casefold
= g_utf8_casefold (s2
, n2
);
200 normalized_s2
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
203 len_s1
= strlen (normalized_s1
);
204 len_s2
= strlen (normalized_s2
);
209 ret
= (strncmp (normalized_s1
, normalized_s2
, len_s2
) == 0);
212 g_free (normalized_s1
);
213 g_free (normalized_s2
);
219 forward_chars_with_skipping (GtkTextIter
*iter
,
221 gboolean skip_invisible
,
222 gboolean skip_nontext
,
223 gboolean skip_decomp
)
227 g_return_if_fail (count
>= 0);
233 gboolean ignored
= FALSE
;
235 /* minimal workaround to avoid the infinite loop of bug #168247.
236 * It doesn't fix the problemjust the symptom...
238 if (gtk_text_iter_is_end (iter
))
241 if (skip_nontext
&& gtk_text_iter_get_char (iter
) == GTK_TEXT_UNKNOWN_CHAR
)
245 if (!ignored
&& skip_invisible
&&
246 /* _gtk_text_btree_char_is_invisible (iter)*/ FALSE
)
250 if (!ignored
&& skip_decomp
)
252 /* being UTF8 correct sucks; this accounts for extra
253 offsets coming from canonical decompositions of
254 UTF8 characters (e.g. accented characters) which
255 g_utf8_normalize() performs */
260 buffer_len
= g_unichar_to_utf8 (gtk_text_iter_get_char (iter
), buffer
);
261 normal
= g_utf8_normalize (buffer
, buffer_len
, G_NORMALIZE_NFD
);
262 i
-= (g_utf8_strlen (normal
, -1) - 1);
266 gtk_text_iter_forward_char (iter
);
274 lines_match (const GtkTextIter
*start
,
276 gboolean visible_only
,
278 GtkTextIter
*match_start
,
279 GtkTextIter
*match_end
)
286 if (*lines
== NULL
|| **lines
== '\0')
289 *match_start
= *start
;
296 gtk_text_iter_forward_line (&next
);
298 /* No more text in buffer, but *lines is nonempty */
299 if (gtk_text_iter_equal (start
, &next
))
305 line_text
= gtk_text_iter_get_visible_slice (start
, &next
);
307 line_text
= gtk_text_iter_get_slice (start
, &next
);
312 line_text
= gtk_text_iter_get_visible_text (start
, &next
);
314 line_text
= gtk_text_iter_get_text (start
, &next
);
317 if (match_start
) /* if this is the first line we're matching */
319 found
= g_utf8_strcasestr (line_text
, *lines
);
323 /* If it's not the first line, we have to match from the
326 if (g_utf8_caselessnmatch (line_text
, *lines
, strlen (line_text
),
339 /* Get offset to start of search string */
340 offset
= g_utf8_strlen (line_text
, found
- line_text
);
344 /* If match start needs to be returned, set it to the
345 * start of the search string.
347 forward_chars_with_skipping (&next
, offset
, visible_only
, !slice
, FALSE
);
353 /* Go to end of search string */
354 forward_chars_with_skipping (&next
, g_utf8_strlen (*lines
, -1), visible_only
, !slice
, TRUE
);
363 /* pass NULL for match_start, since we don't need to find the
366 return lines_match (&next
, lines
, visible_only
, slice
, NULL
, match_end
);
370 backward_lines_match (const GtkTextIter
*start
,
372 gboolean visible_only
,
374 GtkTextIter
*match_start
,
375 GtkTextIter
*match_end
)
377 GtkTextIter line
, next
;
382 if (*lines
== NULL
|| **lines
== '\0')
385 *match_start
= *start
;
391 line
= next
= *start
;
392 if (gtk_text_iter_get_line_offset (&next
) == 0)
394 if (!gtk_text_iter_backward_line (&next
))
398 gtk_text_iter_set_line_offset (&next
, 0);
403 line_text
= gtk_text_iter_get_visible_slice (&next
, &line
);
405 line_text
= gtk_text_iter_get_slice (&next
, &line
);
410 line_text
= gtk_text_iter_get_visible_text (&next
, &line
);
412 line_text
= gtk_text_iter_get_text (&next
, &line
);
415 if (match_start
) /* if this is the first line we're matching */
417 found
= g_utf8_strrcasestr (line_text
, *lines
);
421 /* If it's not the first line, we have to match from the
424 if (g_utf8_caselessnmatch (line_text
, *lines
, strlen (line_text
),
437 /* Get offset to start of search string */
438 offset
= g_utf8_strlen (line_text
, found
- line_text
);
440 forward_chars_with_skipping (&next
, offset
, visible_only
, !slice
, FALSE
);
442 /* If match start needs to be returned, set it to the
443 * start of the search string.
450 /* Go to end of search string */
451 forward_chars_with_skipping (&next
, g_utf8_strlen (*lines
, -1), visible_only
, !slice
, TRUE
);
460 /* try to match the rest of the lines forward, passing NULL
461 * for match_start so lines_match will try to match the entire
463 return lines_match (&next
, lines
, visible_only
,
464 slice
, NULL
, match_end
);
467 /* strsplit () that retains the delimiter as part of the string. */
469 strbreakup (const char *string
,
470 const char *delimiter
,
473 GSList
*string_list
= NULL
, *slist
;
474 gchar
**str_array
, *s
, *casefold
, *new_string
;
477 g_return_val_if_fail (string
!= NULL
, NULL
);
478 g_return_val_if_fail (delimiter
!= NULL
, NULL
);
481 max_tokens
= G_MAXINT
;
483 s
= strstr (string
, delimiter
);
486 guint delimiter_len
= strlen (delimiter
);
492 len
= s
- string
+ delimiter_len
;
493 new_string
= g_new (gchar
, len
+ 1);
494 strncpy (new_string
, string
, len
);
496 casefold
= g_utf8_casefold (new_string
, -1);
498 new_string
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
500 string_list
= g_slist_prepend (string_list
, new_string
);
502 string
= s
+ delimiter_len
;
503 s
= strstr (string
, delimiter
);
504 } while (--max_tokens
&& s
);
510 casefold
= g_utf8_casefold (string
, -1);
511 new_string
= g_utf8_normalize (casefold
, -1, G_NORMALIZE_NFD
);
513 string_list
= g_slist_prepend (string_list
, new_string
);
516 str_array
= g_new (gchar
*, n
);
520 str_array
[i
--] = NULL
;
521 for (slist
= string_list
; slist
; slist
= slist
->next
)
522 str_array
[i
--] = slist
->data
;
524 g_slist_free (string_list
);
529 static GtkTextSearchFlags
530 _source_flags_to_text_flags(GtkSourceSearchFlags flags
)
532 GtkTextSearchFlags text_flags
= 0;
534 if (flags
& GTK_SOURCE_SEARCH_VISIBLE_ONLY
)
535 text_flags
|= GTK_TEXT_SEARCH_VISIBLE_ONLY
;
536 if (flags
& GTK_SOURCE_SEARCH_TEXT_ONLY
)
537 text_flags
|= GTK_TEXT_SEARCH_TEXT_ONLY
;
543 * gtk_source_iter_forward_search:
544 * @iter: start of search.
545 * @str: a search string.
546 * @flags: flags affecting how the search is done.
547 * @match_start: return location for start of match, or %%NULL.
548 * @match_end: return location for end of match, or %%NULL.
549 * @limit: bound for the search, or %%NULL for the end of the buffer.
551 * Searches forward for @str. Any match is returned by setting
552 * @match_start to the first character of the match and @match_end to the
553 * first character after the match. The search will not continue past
554 * @limit. Note that a search is a linear or O(n) operation, so you
555 * may wish to use @limit to avoid locking up your UI on large
558 * If the #GTK_SOURCE_SEARCH_VISIBLE_ONLY flag is present, the match may
559 * have invisible text interspersed in @str. i.e. @str will be a
560 * possibly-noncontiguous subsequence of the matched range. similarly,
561 * if you specify #GTK_SOURCE_SEARCH_TEXT_ONLY, the match may have
562 * pixbufs or child widgets mixed inside the matched range. If these
563 * flags are not given, the match must be exact; the special 0xFFFC
564 * character in @str will match embedded pixbufs or child widgets.
565 * If you specify the #GTK_SOURCE_SEARCH_CASE_INSENSITIVE flag, the text will
566 * be matched regardless of what case it is in.
568 * Same as gtk_text_iter_forward_search(), but supports case insensitive
571 * Return value: whether a match was found.
574 gtk_source_iter_forward_search (const GtkTextIter
*iter
,
576 GtkSourceSearchFlags flags
,
577 GtkTextIter
*match_start
,
578 GtkTextIter
*match_end
,
579 const GtkTextIter
*limit
)
581 gchar
**lines
= NULL
;
583 gboolean retval
= FALSE
;
585 gboolean visible_only
;
588 g_return_val_if_fail (iter
!= NULL
, FALSE
);
589 g_return_val_if_fail (str
!= NULL
, FALSE
);
591 if ((flags
& GTK_SOURCE_SEARCH_CASE_INSENSITIVE
) == 0)
592 return gtk_text_iter_forward_search (iter
, str
, _source_flags_to_text_flags(flags
),
593 match_start
, match_end
,
596 if (limit
&& gtk_text_iter_compare (iter
, limit
) >= 0)
601 /* If we can move one char, return the empty string there */
604 if (gtk_text_iter_forward_char (&match
))
606 if (limit
&& gtk_text_iter_equal (&match
, limit
))
610 *match_start
= match
;
621 visible_only
= (flags
& GTK_SOURCE_SEARCH_VISIBLE_ONLY
) != 0;
622 slice
= (flags
& GTK_SOURCE_SEARCH_TEXT_ONLY
) == 0;
624 /* locate all lines */
625 lines
= strbreakup (str
, "\n", -1);
631 /* This loop has an inefficient worst-case, where
632 * gtk_text_iter_get_text () is called repeatedly on
637 if (limit
&& gtk_text_iter_compare (&search
, limit
) >= 0)
640 if (lines_match (&search
, (const gchar
**)lines
,
641 visible_only
, slice
, &match
, &end
))
644 (limit
&& gtk_text_iter_compare (&end
, limit
) <= 0))
649 *match_start
= match
;
655 } while (gtk_text_iter_forward_line (&search
));
657 g_strfreev ((gchar
**)lines
);
663 * gtk_source_iter_backward_search:
664 * @iter: a #GtkTextIter where the search begins.
665 * @str: search string.
666 * @flags: bitmask of flags affecting the search.
667 * @match_start: return location for start of match, or %%NULL.
668 * @match_end: return location for end of match, or %%NULL.
669 * @limit: location of last possible @match_start, or %%NULL for start of buffer.
671 * Same as gtk_text_iter_backward_search(), but supports case insensitive
674 * Return value: whether a match was found.
677 gtk_source_iter_backward_search (const GtkTextIter
*iter
,
679 GtkSourceSearchFlags flags
,
680 GtkTextIter
*match_start
,
681 GtkTextIter
*match_end
,
682 const GtkTextIter
*limit
)
684 gchar
**lines
= NULL
;
686 gboolean retval
= FALSE
;
688 gboolean visible_only
;
691 g_return_val_if_fail (iter
!= NULL
, FALSE
);
692 g_return_val_if_fail (str
!= NULL
, FALSE
);
694 if ((flags
& GTK_SOURCE_SEARCH_CASE_INSENSITIVE
) == 0)
695 return gtk_text_iter_backward_search (iter
, str
, _source_flags_to_text_flags(flags
),
696 match_start
, match_end
,
699 if (limit
&& gtk_text_iter_compare (iter
, limit
) <= 0)
704 /* If we can move one char, return the empty string there */
707 if (gtk_text_iter_backward_char (&match
))
709 if (limit
&& gtk_text_iter_equal (&match
, limit
))
713 *match_start
= match
;
724 visible_only
= (flags
& GTK_SOURCE_SEARCH_VISIBLE_ONLY
) != 0;
725 slice
= (flags
& GTK_SOURCE_SEARCH_TEXT_ONLY
) == 0;
727 /* locate all lines */
728 lines
= strbreakup (str
, "\n", -1);
734 /* This loop has an inefficient worst-case, where
735 * gtk_text_iter_get_text () is called repeatedly on
740 if (limit
&& gtk_text_iter_compare (&search
, limit
) <= 0)
743 if (backward_lines_match (&search
, (const gchar
**)lines
,
744 visible_only
, slice
, &match
, &end
))
746 if (limit
== NULL
|| (limit
&&
747 gtk_text_iter_compare (&end
, limit
) > 0))
752 *match_start
= match
;
759 if (gtk_text_iter_get_line_offset (&search
) == 0)
761 if (!gtk_text_iter_backward_line (&search
))
766 gtk_text_iter_set_line_offset (&search
, 0);
770 g_strfreev ((gchar
**)lines
);
776 * gtk_source_iter_find_matching_bracket is implemented in gtksourcebuffer.c