1 /* GTK - The GIMP Toolkit
2 * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
3 * Copyright (C) 2004 Nokia Corporation
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
32 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
33 #include "gtkclipboard.h"
35 #include "gtkinvisible.h"
36 #include "gtkmarshalers.h"
37 #include "gtktextbuffer.h"
38 #include "gtktextbufferrichtext.h"
39 #include "gtktextbtree.h"
40 #include "gtktextiterprivate.h"
41 #include "gtkprivate.h"
45 typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
47 struct _GtkTextBufferPrivate
49 GtkTargetList *copy_target_list;
50 GtkTargetEntry *copy_target_entries;
51 gint n_copy_target_entries;
53 GtkTargetList *paste_target_list;
54 GtkTargetEntry *paste_target_entries;
55 gint n_paste_target_entries;
57 GtkTextTagTable *tag_table;
60 GSList *clipboard_contents_buffers;
61 GSList *selection_clipboards;
63 GtkTextLogAttrCache *log_attr_cache;
65 guint user_action_count;
67 /* Whether the buffer has been modified since last save */
70 guint has_selection : 1;
74 typedef struct _ClipboardRequest ClipboardRequest;
76 struct _ClipboardRequest
78 GtkTextBuffer *buffer;
80 gboolean default_editable;
81 gboolean is_clipboard;
82 gboolean replace_selection;
111 PROP_CURSOR_POSITION,
112 PROP_COPY_TARGET_LIST,
113 PROP_PASTE_TARGET_LIST
116 static void gtk_text_buffer_finalize (GObject *object);
118 static void gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
122 static void gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
125 static void gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
127 GtkTextChildAnchor *anchor);
128 static void gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
131 static void gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
133 const GtkTextIter *start_char,
134 const GtkTextIter *end_char);
135 static void gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
137 const GtkTextIter *start_char,
138 const GtkTextIter *end_char);
139 static void gtk_text_buffer_real_changed (GtkTextBuffer *buffer);
140 static void gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
141 const GtkTextIter *iter,
144 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
145 static void free_log_attr_cache (GtkTextLogAttrCache *cache);
147 static void remove_all_selection_clipboards (GtkTextBuffer *buffer);
148 static void update_selection_clipboards (GtkTextBuffer *buffer);
150 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
152 static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
154 static guint signals[LAST_SIGNAL] = { 0 };
156 static void gtk_text_buffer_set_property (GObject *object,
160 static void gtk_text_buffer_get_property (GObject *object,
164 static void gtk_text_buffer_notify (GObject *object,
167 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
170 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
172 GObjectClass *object_class = G_OBJECT_CLASS (klass);
174 object_class->finalize = gtk_text_buffer_finalize;
175 object_class->set_property = gtk_text_buffer_set_property;
176 object_class->get_property = gtk_text_buffer_get_property;
177 object_class->notify = gtk_text_buffer_notify;
179 klass->insert_text = gtk_text_buffer_real_insert_text;
180 klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
181 klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
182 klass->delete_range = gtk_text_buffer_real_delete_range;
183 klass->apply_tag = gtk_text_buffer_real_apply_tag;
184 klass->remove_tag = gtk_text_buffer_real_remove_tag;
185 klass->changed = gtk_text_buffer_real_changed;
186 klass->mark_set = gtk_text_buffer_real_mark_set;
189 g_object_class_install_property (object_class,
191 g_param_spec_object ("tag-table",
193 P_("Text Tag Table"),
194 GTK_TYPE_TEXT_TAG_TABLE,
195 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
197 /* Normal properties*/
200 * GtkTextBuffer:text:
202 * The text content of the buffer. Without child widgets and images,
203 * see gtk_text_buffer_get_text() for more information.
207 g_object_class_install_property (object_class,
209 g_param_spec_string ("text",
211 P_("Current text of the buffer"),
213 GTK_PARAM_READWRITE));
216 * GtkTextBuffer:has-selection:
218 * Whether the buffer has some text currently selected.
222 g_object_class_install_property (object_class,
224 g_param_spec_boolean ("has-selection",
226 P_("Whether the buffer has some text currently selected"),
228 GTK_PARAM_READABLE));
231 * GtkTextBuffer:cursor-position:
233 * The position of the insert mark (as offset from the beginning
234 * of the buffer). It is useful for getting notified when the
239 g_object_class_install_property (object_class,
240 PROP_CURSOR_POSITION,
241 g_param_spec_int ("cursor-position",
242 P_("Cursor position"),
243 P_("The position of the insert mark (as offset from the beginning of the buffer)"),
245 GTK_PARAM_READABLE));
248 * GtkTextBuffer:copy-target-list:
250 * The list of targets this buffer supports for clipboard copying
255 g_object_class_install_property (object_class,
256 PROP_COPY_TARGET_LIST,
257 g_param_spec_boxed ("copy-target-list",
258 P_("Copy target list"),
259 P_("The list of targets this buffer supports for clipboard copying and DND source"),
260 GTK_TYPE_TARGET_LIST,
261 GTK_PARAM_READABLE));
264 * GtkTextBuffer:paste-target-list:
266 * The list of targets this buffer supports for clipboard pasting
267 * and as DND destination.
271 g_object_class_install_property (object_class,
272 PROP_PASTE_TARGET_LIST,
273 g_param_spec_boxed ("paste-target-list",
274 P_("Paste target list"),
275 P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
276 GTK_TYPE_TARGET_LIST,
277 GTK_PARAM_READABLE));
280 * GtkTextBuffer::insert-text:
281 * @textbuffer: the object which received the signal
282 * @location: position to insert @text in @textbuffer
283 * @text: the UTF-8 text to be inserted
284 * @len: length of the inserted text in bytes
286 * The ::insert-text signal is emitted to insert text in a #GtkTextBuffer.
287 * Insertion actually occurs in the default handler.
289 * Note that if your handler runs before the default handler it must not
290 * invalidate the @location iter (or has to revalidate it).
291 * The default signal handler revalidates it to point to the end of the
295 * gtk_text_buffer_insert(),
296 * gtk_text_buffer_insert_range().
298 signals[INSERT_TEXT] =
299 g_signal_new (I_("insert-text"),
300 G_OBJECT_CLASS_TYPE (object_class),
302 G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
304 _gtk_marshal_VOID__BOXED_STRING_INT,
307 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
308 G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
312 * GtkTextBuffer::insert-pixbuf:
313 * @textbuffer: the object which received the signal
314 * @location: position to insert @pixbuf in @textbuffer
315 * @pixbuf: the #GdkPixbuf to be inserted
317 * The ::insert-pixbuf signal is emitted to insert a #GdkPixbuf
318 * in a #GtkTextBuffer. Insertion actually occurs in the default handler.
320 * Note that if your handler runs before the default handler it must not
321 * invalidate the @location iter (or has to revalidate it).
322 * The default signal handler revalidates it to be placed after the
325 * See also: gtk_text_buffer_insert_pixbuf().
327 signals[INSERT_PIXBUF] =
328 g_signal_new (I_("insert-pixbuf"),
329 G_OBJECT_CLASS_TYPE (object_class),
331 G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
333 _gtk_marshal_VOID__BOXED_OBJECT,
336 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
341 * GtkTextBuffer::insert-child-anchor:
342 * @textbuffer: the object which received the signal
343 * @location: position to insert @anchor in @textbuffer
344 * @anchor: the #GtkTextChildAnchor to be inserted
346 * The ::insert-child-anchor signal is emitted to insert a
347 * #GtkTextChildAnchor in a #GtkTextBuffer.
348 * Insertion actually occurs in the default handler.
350 * Note that if your handler runs before the default handler it must
351 * not invalidate the @location iter (or has to revalidate it).
352 * The default signal handler revalidates it to be placed after the
355 * See also: gtk_text_buffer_insert_child_anchor().
357 signals[INSERT_CHILD_ANCHOR] =
358 g_signal_new (I_("insert-child-anchor"),
359 G_OBJECT_CLASS_TYPE (object_class),
361 G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
363 _gtk_marshal_VOID__BOXED_OBJECT,
366 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
367 GTK_TYPE_TEXT_CHILD_ANCHOR);
370 * GtkTextBuffer::delete-range:
371 * @textbuffer: the object which received the signal
372 * @start: the start of the range to be deleted
373 * @end: the end of the range to be deleted
375 * The ::delete-range signal is emitted to delete a range
376 * from a #GtkTextBuffer.
378 * Note that if your handler runs before the default handler it must not
379 * invalidate the @start and @end iters (or has to revalidate them).
380 * The default signal handler revalidates the @start and @end iters to
381 * both point point to the location where text was deleted. Handlers
382 * which run after the default handler (see g_signal_connect_after())
383 * do not have access to the deleted text.
385 * See also: gtk_text_buffer_delete().
387 signals[DELETE_RANGE] =
388 g_signal_new (I_("delete-range"),
389 G_OBJECT_CLASS_TYPE (object_class),
391 G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
393 _gtk_marshal_VOID__BOXED_BOXED,
396 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
397 GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
400 * GtkTextBuffer::changed:
401 * @textbuffer: the object which received the signal
403 * The ::changed signal is emitted when the content of a #GtkTextBuffer
407 g_signal_new (I_("changed"),
408 G_OBJECT_CLASS_TYPE (object_class),
410 G_STRUCT_OFFSET (GtkTextBufferClass, changed),
412 _gtk_marshal_VOID__VOID,
417 * GtkTextBuffer::modified-changed:
418 * @textbuffer: the object which received the signal
420 * The ::modified-changed signal is emitted when the modified bit of a
421 * #GtkTextBuffer flips.
424 * gtk_text_buffer_set_modified().
426 signals[MODIFIED_CHANGED] =
427 g_signal_new (I_("modified-changed"),
428 G_OBJECT_CLASS_TYPE (object_class),
430 G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
432 _gtk_marshal_VOID__VOID,
437 * GtkTextBuffer::mark-set:
438 * @textbuffer: the object which received the signal
439 * @location: The location of @mark in @textbuffer
440 * @mark: The mark that is set
442 * The ::mark-set signal is emitted as notification
443 * after a #GtkTextMark is set.
446 * gtk_text_buffer_create_mark(),
447 * gtk_text_buffer_move_mark().
450 g_signal_new (I_("mark-set"),
451 G_OBJECT_CLASS_TYPE (object_class),
453 G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
455 _gtk_marshal_VOID__BOXED_OBJECT,
462 * GtkTextBuffer::mark-deleted:
463 * @textbuffer: the object which received the signal
464 * @mark: The mark that was deleted
466 * The ::mark-deleted signal is emitted as notification
467 * after a #GtkTextMark is deleted.
470 * gtk_text_buffer_delete_mark().
472 signals[MARK_DELETED] =
473 g_signal_new (I_("mark-deleted"),
474 G_OBJECT_CLASS_TYPE (object_class),
476 G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
478 _gtk_marshal_VOID__OBJECT,
484 * GtkTextBuffer::apply-tag:
485 * @textbuffer: the object which received the signal
486 * @tag: the applied tag
487 * @start: the start of the range the tag is applied to
488 * @end: the end of the range the tag is applied to
490 * The ::apply-tag signal is emitted to apply a tag to a
491 * range of text in a #GtkTextBuffer.
492 * Applying actually occurs in the default handler.
494 * Note that if your handler runs before the default handler it must not
495 * invalidate the @start and @end iters (or has to revalidate them).
498 * gtk_text_buffer_apply_tag(),
499 * gtk_text_buffer_insert_with_tags(),
500 * gtk_text_buffer_insert_range().
503 g_signal_new (I_("apply-tag"),
504 G_OBJECT_CLASS_TYPE (object_class),
506 G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
508 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
517 * GtkTextBuffer::remove-tag:
518 * @textbuffer: the object which received the signal
519 * @tag: the tag to be removed
520 * @start: the start of the range the tag is removed from
521 * @end: the end of the range the tag is removed from
523 * The ::remove-tag signal is emitted to remove all occurrences of @tag from
524 * a range of text in a #GtkTextBuffer.
525 * Removal actually occurs in the default handler.
527 * Note that if your handler runs before the default handler it must not
528 * invalidate the @start and @end iters (or has to revalidate them).
531 * gtk_text_buffer_remove_tag().
533 signals[REMOVE_TAG] =
534 g_signal_new (I_("remove-tag"),
535 G_OBJECT_CLASS_TYPE (object_class),
537 G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
539 _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
547 * GtkTextBuffer::begin-user-action:
548 * @textbuffer: the object which received the signal
550 * The ::begin-user-action signal is emitted at the beginning of a single
551 * user-visible operation on a #GtkTextBuffer.
554 * gtk_text_buffer_begin_user_action(),
555 * gtk_text_buffer_insert_interactive(),
556 * gtk_text_buffer_insert_range_interactive(),
557 * gtk_text_buffer_delete_interactive(),
558 * gtk_text_buffer_backspace(),
559 * gtk_text_buffer_delete_selection().
561 signals[BEGIN_USER_ACTION] =
562 g_signal_new (I_("begin-user-action"),
563 G_OBJECT_CLASS_TYPE (object_class),
565 G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
567 _gtk_marshal_VOID__VOID,
572 * GtkTextBuffer::end-user-action:
573 * @textbuffer: the object which received the signal
575 * The ::end-user-action signal is emitted at the end of a single
576 * user-visible operation on the #GtkTextBuffer.
579 * gtk_text_buffer_end_user_action(),
580 * gtk_text_buffer_insert_interactive(),
581 * gtk_text_buffer_insert_range_interactive(),
582 * gtk_text_buffer_delete_interactive(),
583 * gtk_text_buffer_backspace(),
584 * gtk_text_buffer_delete_selection(),
585 * gtk_text_buffer_backspace().
587 signals[END_USER_ACTION] =
588 g_signal_new (I_("end-user-action"),
589 G_OBJECT_CLASS_TYPE (object_class),
591 G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
593 _gtk_marshal_VOID__VOID,
598 * GtkTextBuffer::paste-done:
599 * @textbuffer: the object which received the signal
601 * The paste-done signal is emitted after paste operation has been completed.
602 * This is useful to properly scroll the view to the end of the pasted text.
603 * See gtk_text_buffer_paste_clipboard() for more details.
607 signals[PASTE_DONE] =
608 g_signal_new (I_("paste-done"),
609 G_OBJECT_CLASS_TYPE (object_class),
611 G_STRUCT_OFFSET (GtkTextBufferClass, paste_done),
613 _gtk_marshal_VOID__OBJECT,
618 g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
622 gtk_text_buffer_init (GtkTextBuffer *buffer)
624 buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer,
625 GTK_TYPE_TEXT_BUFFER,
626 GtkTextBufferPrivate);
628 buffer->priv->clipboard_contents_buffers = NULL;
629 buffer->priv->tag_table = NULL;
631 /* allow copying of arbiatray stuff in the internal rich text format */
632 gtk_text_buffer_register_serialize_tagset (buffer, NULL);
636 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
638 GtkTextBufferPrivate *priv = buffer->priv;
640 g_return_if_fail (priv->tag_table == NULL);
644 priv->tag_table = table;
645 g_object_ref (priv->tag_table);
646 _gtk_text_tag_table_add_buffer (table, buffer);
650 static GtkTextTagTable*
651 get_table (GtkTextBuffer *buffer)
653 GtkTextBufferPrivate *priv = buffer->priv;
655 if (priv->tag_table == NULL)
657 priv->tag_table = gtk_text_tag_table_new ();
658 _gtk_text_tag_table_add_buffer (priv->tag_table, buffer);
661 return priv->tag_table;
665 gtk_text_buffer_set_property (GObject *object,
670 GtkTextBuffer *text_buffer;
672 text_buffer = GTK_TEXT_BUFFER (object);
677 set_table (text_buffer, g_value_get_object (value));
681 gtk_text_buffer_set_text (text_buffer,
682 g_value_get_string (value), -1);
686 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692 gtk_text_buffer_get_property (GObject *object,
697 GtkTextBuffer *text_buffer;
700 text_buffer = GTK_TEXT_BUFFER (object);
705 g_value_set_object (value, get_table (text_buffer));
710 GtkTextIter start, end;
712 gtk_text_buffer_get_start_iter (text_buffer, &start);
713 gtk_text_buffer_get_end_iter (text_buffer, &end);
715 g_value_take_string (value,
716 gtk_text_buffer_get_text (text_buffer,
717 &start, &end, FALSE));
721 case PROP_HAS_SELECTION:
722 g_value_set_boolean (value, text_buffer->priv->has_selection);
725 case PROP_CURSOR_POSITION:
726 gtk_text_buffer_get_iter_at_mark (text_buffer, &iter,
727 gtk_text_buffer_get_insert (text_buffer));
728 g_value_set_int (value, gtk_text_iter_get_offset (&iter));
731 case PROP_COPY_TARGET_LIST:
732 g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
735 case PROP_PASTE_TARGET_LIST:
736 g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
740 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
746 gtk_text_buffer_notify (GObject *object,
749 if (!strcmp (pspec->name, "copy-target-list") ||
750 !strcmp (pspec->name, "paste-target-list"))
752 gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
757 * gtk_text_buffer_new:
758 * @table: (allow-none): a tag table, or %NULL to create a new one
760 * Creates a new text buffer.
762 * Return value: a new text buffer
765 gtk_text_buffer_new (GtkTextTagTable *table)
767 GtkTextBuffer *text_buffer;
769 text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
775 gtk_text_buffer_finalize (GObject *object)
777 GtkTextBuffer *buffer;
778 GtkTextBufferPrivate *priv;
780 buffer = GTK_TEXT_BUFFER (object);
783 remove_all_selection_clipboards (buffer);
787 _gtk_text_tag_table_remove_buffer (priv->tag_table, buffer);
788 g_object_unref (priv->tag_table);
789 priv->tag_table = NULL;
794 _gtk_text_btree_unref (priv->btree);
798 if (priv->log_attr_cache)
799 free_log_attr_cache (priv->log_attr_cache);
801 priv->log_attr_cache = NULL;
803 gtk_text_buffer_free_target_lists (buffer);
805 G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
809 get_btree (GtkTextBuffer *buffer)
811 GtkTextBufferPrivate *priv = buffer->priv;
813 if (priv->btree == NULL)
814 priv->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
821 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
823 return get_btree (buffer);
827 * gtk_text_buffer_get_tag_table:
828 * @buffer: a #GtkTextBuffer
830 * Get the #GtkTextTagTable associated with this buffer.
832 * Return value: (transfer none): the buffer's tag table
835 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
837 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
839 return get_table (buffer);
843 * gtk_text_buffer_set_text:
844 * @buffer: a #GtkTextBuffer
845 * @text: UTF-8 text to insert
846 * @len: length of @text in bytes
848 * Deletes current contents of @buffer, and inserts @text instead. If
849 * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
852 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
856 GtkTextIter start, end;
858 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
859 g_return_if_fail (text != NULL);
864 gtk_text_buffer_get_bounds (buffer, &start, &end);
866 gtk_text_buffer_delete (buffer, &start, &end);
870 gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
871 gtk_text_buffer_insert (buffer, &start, text, len);
874 g_object_notify (G_OBJECT (buffer), "text");
884 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
889 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
890 g_return_if_fail (iter != NULL);
892 _gtk_text_btree_insert (iter, text, len);
894 g_signal_emit (buffer, signals[CHANGED], 0);
895 g_object_notify (G_OBJECT (buffer), "cursor-position");
899 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
904 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
905 g_return_if_fail (iter != NULL);
906 g_return_if_fail (text != NULL);
911 g_return_if_fail (g_utf8_validate (text, len, NULL));
915 g_signal_emit (buffer, signals[INSERT_TEXT], 0,
921 * gtk_text_buffer_insert:
922 * @buffer: a #GtkTextBuffer
923 * @iter: a position in the buffer
924 * @text: text in UTF-8 format
925 * @len: length of text in bytes, or -1
927 * Inserts @len bytes of @text at position @iter. If @len is -1,
928 * @text must be nul-terminated and will be inserted in its
929 * entirety. Emits the "insert-text" signal; insertion actually occurs
930 * in the default handler for the signal. @iter is invalidated when
931 * insertion occurs (because the buffer contents change), but the
932 * default signal handler revalidates it to point to the end of the
936 gtk_text_buffer_insert (GtkTextBuffer *buffer,
941 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
942 g_return_if_fail (iter != NULL);
943 g_return_if_fail (text != NULL);
944 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
946 gtk_text_buffer_emit_insert (buffer, iter, text, len);
950 * gtk_text_buffer_insert_at_cursor:
951 * @buffer: a #GtkTextBuffer
952 * @text: text in UTF-8 format
953 * @len: length of text, in bytes
955 * Simply calls gtk_text_buffer_insert(), using the current
956 * cursor position as the insertion point.
959 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
965 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
966 g_return_if_fail (text != NULL);
968 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
969 gtk_text_buffer_get_insert (buffer));
971 gtk_text_buffer_insert (buffer, &iter, text, len);
975 * gtk_text_buffer_insert_interactive:
976 * @buffer: a #GtkTextBuffer
977 * @iter: a position in @buffer
978 * @text: some UTF-8 text
979 * @len: length of text in bytes, or -1
980 * @default_editable: default editability of buffer
982 * Like gtk_text_buffer_insert(), but the insertion will not occur if
983 * @iter is at a non-editable location in the buffer. Usually you
984 * want to prevent insertions at ineditable locations if the insertion
985 * results from a user action (is interactive).
987 * @default_editable indicates the editability of text that doesn't
988 * have a tag affecting editability applied to it. Typically the
989 * result of gtk_text_view_get_editable() is appropriate here.
991 * Return value: whether text was actually inserted
994 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
998 gboolean default_editable)
1000 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1001 g_return_val_if_fail (text != NULL, FALSE);
1002 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
1004 if (gtk_text_iter_can_insert (iter, default_editable))
1006 gtk_text_buffer_begin_user_action (buffer);
1007 gtk_text_buffer_emit_insert (buffer, iter, text, len);
1008 gtk_text_buffer_end_user_action (buffer);
1016 * gtk_text_buffer_insert_interactive_at_cursor:
1017 * @buffer: a #GtkTextBuffer
1018 * @text: text in UTF-8 format
1019 * @len: length of text in bytes, or -1
1020 * @default_editable: default editability of buffer
1022 * Calls gtk_text_buffer_insert_interactive() at the cursor
1025 * @default_editable indicates the editability of text that doesn't
1026 * have a tag affecting editability applied to it. Typically the
1027 * result of gtk_text_view_get_editable() is appropriate here.
1029 * Return value: whether text was actually inserted
1032 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
1035 gboolean default_editable)
1039 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1040 g_return_val_if_fail (text != NULL, FALSE);
1042 gtk_text_buffer_get_iter_at_mark (buffer, &iter,
1043 gtk_text_buffer_get_insert (buffer));
1045 return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
1050 possibly_not_text (gunichar ch,
1053 return ch == GTK_TEXT_UNKNOWN_CHAR;
1057 insert_text_range (GtkTextBuffer *buffer,
1059 const GtkTextIter *orig_start,
1060 const GtkTextIter *orig_end,
1061 gboolean interactive)
1065 text = gtk_text_iter_get_text (orig_start, orig_end);
1067 gtk_text_buffer_emit_insert (buffer, iter, text, -1);
1072 typedef struct _Range Range;
1075 GtkTextBuffer *buffer;
1076 GtkTextMark *start_mark;
1077 GtkTextMark *end_mark;
1078 GtkTextMark *whole_end_mark;
1079 GtkTextIter *range_start;
1080 GtkTextIter *range_end;
1081 GtkTextIter *whole_end;
1085 save_range (GtkTextIter *range_start,
1086 GtkTextIter *range_end,
1087 GtkTextIter *whole_end)
1091 r = g_new (Range, 1);
1093 r->buffer = gtk_text_iter_get_buffer (range_start);
1094 g_object_ref (r->buffer);
1097 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1102 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1108 gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1113 r->range_start = range_start;
1114 r->range_end = range_end;
1115 r->whole_end = whole_end;
1121 restore_range (Range *r)
1123 gtk_text_buffer_get_iter_at_mark (r->buffer,
1127 gtk_text_buffer_get_iter_at_mark (r->buffer,
1131 gtk_text_buffer_get_iter_at_mark (r->buffer,
1135 gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
1136 gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
1137 gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
1139 /* Due to the gravities on the marks, the ordering could have
1140 * gotten mangled; we switch to an empty range in that
1144 if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
1145 *r->range_start = *r->range_end;
1147 if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
1148 *r->range_end = *r->whole_end;
1150 g_object_unref (r->buffer);
1155 insert_range_untagged (GtkTextBuffer *buffer,
1157 const GtkTextIter *orig_start,
1158 const GtkTextIter *orig_end,
1159 gboolean interactive)
1161 GtkTextIter range_start;
1162 GtkTextIter range_end;
1163 GtkTextIter start, end;
1166 if (gtk_text_iter_equal (orig_start, orig_end))
1169 start = *orig_start;
1172 range_start = start;
1177 if (gtk_text_iter_equal (&range_start, &range_end))
1179 /* Figure out how to move forward */
1181 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1183 if (gtk_text_iter_equal (&range_end, &end))
1185 /* nothing left to do */
1188 else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
1190 GdkPixbuf *pixbuf = NULL;
1191 GtkTextChildAnchor *anchor = NULL;
1192 pixbuf = gtk_text_iter_get_pixbuf (&range_end);
1193 anchor = gtk_text_iter_get_child_anchor (&range_end);
1197 r = save_range (&range_start,
1201 gtk_text_buffer_insert_pixbuf (buffer,
1208 gtk_text_iter_forward_char (&range_end);
1210 range_start = range_end;
1214 /* Just skip anchors */
1216 gtk_text_iter_forward_char (&range_end);
1217 range_start = range_end;
1221 /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1224 gtk_text_iter_forward_find_char (&range_end,
1225 possibly_not_text, NULL,
1228 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1233 /* Text segment starts here, so forward search to
1234 * find its possible endpoint
1236 gtk_text_iter_forward_find_char (&range_end,
1237 possibly_not_text, NULL,
1240 g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1245 r = save_range (&range_start,
1249 insert_text_range (buffer,
1258 range_start = range_end;
1264 insert_range_not_inside_self (GtkTextBuffer *buffer,
1266 const GtkTextIter *orig_start,
1267 const GtkTextIter *orig_end,
1268 gboolean interactive)
1270 /* Find each range of uniformly-tagged text, insert it,
1271 * then apply the tags.
1273 GtkTextIter start = *orig_start;
1274 GtkTextIter end = *orig_end;
1275 GtkTextIter range_start;
1276 GtkTextIter range_end;
1278 if (gtk_text_iter_equal (orig_start, orig_end))
1281 gtk_text_iter_order (&start, &end);
1283 range_start = start;
1289 GtkTextIter start_iter;
1294 if (gtk_text_iter_equal (&range_start, &end))
1295 break; /* All done */
1297 g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1299 gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1301 g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1303 /* Clamp to the end iterator */
1304 if (gtk_text_iter_compare (&range_end, &end) > 0)
1307 /* We have a range with unique tags; insert it, and
1310 start_offset = gtk_text_iter_get_offset (iter);
1312 r = save_range (&range_start, &range_end, &end);
1314 insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1319 gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1321 tags = gtk_text_iter_get_tags (&range_start);
1323 while (tmp_list != NULL)
1325 gtk_text_buffer_apply_tag (buffer,
1330 tmp_list = g_slist_next (tmp_list);
1332 g_slist_free (tags);
1334 range_start = range_end;
1339 gtk_text_buffer_real_insert_range (GtkTextBuffer *buffer,
1341 const GtkTextIter *orig_start,
1342 const GtkTextIter *orig_end,
1343 gboolean interactive)
1345 GtkTextBuffer *src_buffer;
1347 /* Find each range of uniformly-tagged text, insert it,
1348 * then apply the tags.
1350 if (gtk_text_iter_equal (orig_start, orig_end))
1354 gtk_text_buffer_begin_user_action (buffer);
1356 src_buffer = gtk_text_iter_get_buffer (orig_start);
1358 if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1359 !gtk_text_iter_in_range (iter, orig_start, orig_end))
1361 insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1365 /* If you insert a range into itself, it could loop infinitely
1366 * because the region being copied keeps growing as we insert. So
1367 * we have to separately copy the range before and after
1368 * the insertion point.
1370 GtkTextIter start = *orig_start;
1371 GtkTextIter end = *orig_end;
1372 GtkTextIter range_start;
1373 GtkTextIter range_end;
1377 gtk_text_iter_order (&start, &end);
1379 range_start = start;
1381 first_half = save_range (&range_start, &range_end, &end);
1383 range_start = *iter;
1385 second_half = save_range (&range_start, &range_end, &end);
1387 restore_range (first_half);
1388 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1390 restore_range (second_half);
1391 insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1395 gtk_text_buffer_end_user_action (buffer);
1399 * gtk_text_buffer_insert_range:
1400 * @buffer: a #GtkTextBuffer
1401 * @iter: a position in @buffer
1402 * @start: a position in a #GtkTextBuffer
1403 * @end: another position in the same buffer as @start
1405 * Copies text, tags, and pixbufs between @start and @end (the order
1406 * of @start and @end doesn't matter) and inserts the copy at @iter.
1407 * Used instead of simply getting/inserting text because it preserves
1408 * images and tags. If @start and @end are in a different buffer from
1409 * @buffer, the two buffers must share the same tag table.
1411 * Implemented via emissions of the insert_text and apply_tag signals,
1415 gtk_text_buffer_insert_range (GtkTextBuffer *buffer,
1417 const GtkTextIter *start,
1418 const GtkTextIter *end)
1420 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1421 g_return_if_fail (iter != NULL);
1422 g_return_if_fail (start != NULL);
1423 g_return_if_fail (end != NULL);
1424 g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1425 gtk_text_iter_get_buffer (end));
1426 g_return_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1427 buffer->priv->tag_table);
1428 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1430 gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1434 * gtk_text_buffer_insert_range_interactive:
1435 * @buffer: a #GtkTextBuffer
1436 * @iter: a position in @buffer
1437 * @start: a position in a #GtkTextBuffer
1438 * @end: another position in the same buffer as @start
1439 * @default_editable: default editability of the buffer
1441 * Same as gtk_text_buffer_insert_range(), but does nothing if the
1442 * insertion point isn't editable. The @default_editable parameter
1443 * indicates whether the text is editable at @iter if no tags
1444 * enclosing @iter affect editability. Typically the result of
1445 * gtk_text_view_get_editable() is appropriate here.
1447 * Returns: whether an insertion was possible at @iter
1450 gtk_text_buffer_insert_range_interactive (GtkTextBuffer *buffer,
1452 const GtkTextIter *start,
1453 const GtkTextIter *end,
1454 gboolean default_editable)
1456 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1457 g_return_val_if_fail (iter != NULL, FALSE);
1458 g_return_val_if_fail (start != NULL, FALSE);
1459 g_return_val_if_fail (end != NULL, FALSE);
1460 g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1461 gtk_text_iter_get_buffer (end), FALSE);
1462 g_return_val_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1463 buffer->priv->tag_table, FALSE);
1465 if (gtk_text_iter_can_insert (iter, default_editable))
1467 gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1475 * gtk_text_buffer_insert_with_tags:
1476 * @buffer: a #GtkTextBuffer
1477 * @iter: an iterator in @buffer
1479 * @len: length of @text, or -1
1480 * @first_tag: first tag to apply to @text
1481 * @Varargs: NULL-terminated list of tags to apply
1483 * Inserts @text into @buffer at @iter, applying the list of tags to
1484 * the newly-inserted text. The last tag specified must be NULL to
1485 * terminate the list. Equivalent to calling gtk_text_buffer_insert(),
1486 * then gtk_text_buffer_apply_tag() on the inserted text;
1487 * gtk_text_buffer_insert_with_tags() is just a convenience function.
1490 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1494 GtkTextTag *first_tag,
1502 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1503 g_return_if_fail (iter != NULL);
1504 g_return_if_fail (text != NULL);
1505 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1507 start_offset = gtk_text_iter_get_offset (iter);
1509 gtk_text_buffer_insert (buffer, iter, text, len);
1511 if (first_tag == NULL)
1514 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1516 va_start (args, first_tag);
1520 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1522 tag = va_arg (args, GtkTextTag*);
1529 * gtk_text_buffer_insert_with_tags_by_name:
1530 * @buffer: a #GtkTextBuffer
1531 * @iter: position in @buffer
1533 * @len: length of @text, or -1
1534 * @first_tag_name: name of a tag to apply to @text
1535 * @Varargs: more tag names
1537 * Same as gtk_text_buffer_insert_with_tags(), but allows you
1538 * to pass in tag names instead of tag objects.
1541 gtk_text_buffer_insert_with_tags_by_name (GtkTextBuffer *buffer,
1545 const gchar *first_tag_name,
1551 const gchar *tag_name;
1553 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1554 g_return_if_fail (iter != NULL);
1555 g_return_if_fail (text != NULL);
1556 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1558 start_offset = gtk_text_iter_get_offset (iter);
1560 gtk_text_buffer_insert (buffer, iter, text, len);
1562 if (first_tag_name == NULL)
1565 gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1567 va_start (args, first_tag_name);
1568 tag_name = first_tag_name;
1573 tag = gtk_text_tag_table_lookup (buffer->priv->tag_table,
1578 g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1583 gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1585 tag_name = va_arg (args, const gchar*);
1597 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1601 gboolean has_selection;
1603 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1604 g_return_if_fail (start != NULL);
1605 g_return_if_fail (end != NULL);
1607 _gtk_text_btree_delete (start, end);
1609 /* may have deleted the selection... */
1610 update_selection_clipboards (buffer);
1612 has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1613 if (has_selection != buffer->priv->has_selection)
1615 buffer->priv->has_selection = has_selection;
1616 g_object_notify (G_OBJECT (buffer), "has-selection");
1619 g_signal_emit (buffer, signals[CHANGED], 0);
1620 g_object_notify (G_OBJECT (buffer), "cursor-position");
1624 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1628 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1629 g_return_if_fail (start != NULL);
1630 g_return_if_fail (end != NULL);
1632 if (gtk_text_iter_equal (start, end))
1635 gtk_text_iter_order (start, end);
1637 g_signal_emit (buffer,
1638 signals[DELETE_RANGE],
1644 * gtk_text_buffer_delete:
1645 * @buffer: a #GtkTextBuffer
1646 * @start: a position in @buffer
1647 * @end: another position in @buffer
1649 * Deletes text between @start and @end. The order of @start and @end
1650 * is not actually relevant; gtk_text_buffer_delete() will reorder
1651 * them. This function actually emits the "delete-range" signal, and
1652 * the default handler of that signal deletes the text. Because the
1653 * buffer is modified, all outstanding iterators become invalid after
1654 * calling this function; however, the @start and @end will be
1655 * re-initialized to point to the location where text was deleted.
1658 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1662 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1663 g_return_if_fail (start != NULL);
1664 g_return_if_fail (end != NULL);
1665 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1666 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1668 gtk_text_buffer_emit_delete (buffer, start, end);
1672 * gtk_text_buffer_delete_interactive:
1673 * @buffer: a #GtkTextBuffer
1674 * @start_iter: start of range to delete
1675 * @end_iter: end of range
1676 * @default_editable: whether the buffer is editable by default
1678 * Deletes all <emphasis>editable</emphasis> text in the given range.
1679 * Calls gtk_text_buffer_delete() for each editable sub-range of
1680 * [@start,@end). @start and @end are revalidated to point to
1681 * the location of the last deleted range, or left untouched if
1682 * no text was deleted.
1684 * Return value: whether some text was actually deleted
1687 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1688 GtkTextIter *start_iter,
1689 GtkTextIter *end_iter,
1690 gboolean default_editable)
1692 GtkTextMark *end_mark;
1693 GtkTextMark *start_mark;
1695 gboolean current_state;
1696 gboolean deleted_stuff = FALSE;
1698 /* Delete all editable text in the range start_iter, end_iter */
1700 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1701 g_return_val_if_fail (start_iter != NULL, FALSE);
1702 g_return_val_if_fail (end_iter != NULL, FALSE);
1703 g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1704 g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1707 gtk_text_buffer_begin_user_action (buffer);
1709 gtk_text_iter_order (start_iter, end_iter);
1711 start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1713 end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1716 gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1718 current_state = gtk_text_iter_editable (&iter, default_editable);
1723 gboolean done = FALSE;
1726 gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1728 gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1730 if (gtk_text_iter_compare (&iter, &end) >= 0)
1733 iter = end; /* clamp to the last boundary */
1736 new_state = gtk_text_iter_editable (&iter, default_editable);
1738 if (current_state == new_state)
1744 /* We're ending an editable region. Delete said region. */
1747 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1749 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1751 deleted_stuff = TRUE;
1753 /* revalidate user's iterators. */
1754 *start_iter = start;
1764 if (current_state && !new_state)
1766 /* End of an editable region. Delete it. */
1769 gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1771 gtk_text_buffer_emit_delete (buffer, &start, &iter);
1773 /* It's more robust to ask for the state again then to assume that
1774 * we're on the next not-editable segment. We don't know what the
1775 * ::delete-range handler did.... maybe it deleted the following
1776 * not-editable segment because it was associated with the editable
1779 current_state = gtk_text_iter_editable (&iter, default_editable);
1780 deleted_stuff = TRUE;
1782 /* revalidate user's iterators. */
1783 *start_iter = start;
1788 /* We are at the start of an editable region. We won't be deleting
1789 * the previous region. Move start mark to start of this region.
1792 g_assert (!current_state && new_state);
1794 gtk_text_buffer_move_mark (buffer, start_mark, &iter);
1796 current_state = TRUE;
1803 gtk_text_buffer_delete_mark (buffer, start_mark);
1804 gtk_text_buffer_delete_mark (buffer, end_mark);
1806 gtk_text_buffer_end_user_action (buffer);
1808 return deleted_stuff;
1812 * Extracting textual buffer contents
1816 * gtk_text_buffer_get_text:
1817 * @buffer: a #GtkTextBuffer
1818 * @start: start of a range
1819 * @end: end of a range
1820 * @include_hidden_chars: whether to include invisible text
1822 * Returns the text in the range [@start,@end). Excludes undisplayed
1823 * text (text marked with tags that set the invisibility attribute) if
1824 * @include_hidden_chars is %FALSE. Does not include characters
1825 * representing embedded images, so byte and character indexes into
1826 * the returned string do <emphasis>not</emphasis> correspond to byte
1827 * and character indexes into the buffer. Contrast with
1828 * gtk_text_buffer_get_slice().
1830 * Return value: an allocated UTF-8 string
1833 gtk_text_buffer_get_text (GtkTextBuffer *buffer,
1834 const GtkTextIter *start,
1835 const GtkTextIter *end,
1836 gboolean include_hidden_chars)
1838 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1839 g_return_val_if_fail (start != NULL, NULL);
1840 g_return_val_if_fail (end != NULL, NULL);
1841 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1842 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1844 if (include_hidden_chars)
1845 return gtk_text_iter_get_text (start, end);
1847 return gtk_text_iter_get_visible_text (start, end);
1851 * gtk_text_buffer_get_slice:
1852 * @buffer: a #GtkTextBuffer
1853 * @start: start of a range
1854 * @end: end of a range
1855 * @include_hidden_chars: whether to include invisible text
1857 * Returns the text in the range [@start,@end). Excludes undisplayed
1858 * text (text marked with tags that set the invisibility attribute) if
1859 * @include_hidden_chars is %FALSE. The returned string includes a
1860 * 0xFFFC character whenever the buffer contains
1861 * embedded images, so byte and character indexes into
1862 * the returned string <emphasis>do</emphasis> correspond to byte
1863 * and character indexes into the buffer. Contrast with
1864 * gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal
1865 * text as well, so it is not a reliable indicator that a pixbuf or
1866 * widget is in the buffer.
1868 * Return value: an allocated UTF-8 string
1871 gtk_text_buffer_get_slice (GtkTextBuffer *buffer,
1872 const GtkTextIter *start,
1873 const GtkTextIter *end,
1874 gboolean include_hidden_chars)
1876 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1877 g_return_val_if_fail (start != NULL, NULL);
1878 g_return_val_if_fail (end != NULL, NULL);
1879 g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1880 g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1882 if (include_hidden_chars)
1883 return gtk_text_iter_get_slice (start, end);
1885 return gtk_text_iter_get_visible_slice (start, end);
1893 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1897 _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1899 g_signal_emit (buffer, signals[CHANGED], 0);
1903 * gtk_text_buffer_insert_pixbuf:
1904 * @buffer: a #GtkTextBuffer
1905 * @iter: location to insert the pixbuf
1906 * @pixbuf: a #GdkPixbuf
1908 * Inserts an image into the text buffer at @iter. The image will be
1909 * counted as one character in character counts, and when obtaining
1910 * the buffer contents as a string, will be represented by the Unicode
1911 * "object replacement character" 0xFFFC. Note that the "slice"
1912 * variants for obtaining portions of the buffer as a string include
1913 * this character for pixbufs, but the "text" variants do
1914 * not. e.g. see gtk_text_buffer_get_slice() and
1915 * gtk_text_buffer_get_text().
1918 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1922 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1923 g_return_if_fail (iter != NULL);
1924 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1925 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1927 g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1937 gtk_text_buffer_real_insert_anchor (GtkTextBuffer *buffer,
1939 GtkTextChildAnchor *anchor)
1941 _gtk_text_btree_insert_child_anchor (iter, anchor);
1943 g_signal_emit (buffer, signals[CHANGED], 0);
1947 * gtk_text_buffer_insert_child_anchor:
1948 * @buffer: a #GtkTextBuffer
1949 * @iter: location to insert the anchor
1950 * @anchor: a #GtkTextChildAnchor
1952 * Inserts a child widget anchor into the text buffer at @iter. The
1953 * anchor will be counted as one character in character counts, and
1954 * when obtaining the buffer contents as a string, will be represented
1955 * by the Unicode "object replacement character" 0xFFFC. Note that the
1956 * "slice" variants for obtaining portions of the buffer as a string
1957 * include this character for child anchors, but the "text" variants do
1958 * not. E.g. see gtk_text_buffer_get_slice() and
1959 * gtk_text_buffer_get_text(). Consider
1960 * gtk_text_buffer_create_child_anchor() as a more convenient
1961 * alternative to this function. The buffer will add a reference to
1962 * the anchor, so you can unref it after insertion.
1965 gtk_text_buffer_insert_child_anchor (GtkTextBuffer *buffer,
1967 GtkTextChildAnchor *anchor)
1969 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1970 g_return_if_fail (iter != NULL);
1971 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1972 g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1974 g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1979 * gtk_text_buffer_create_child_anchor:
1980 * @buffer: a #GtkTextBuffer
1981 * @iter: location in the buffer
1983 * This is a convenience function which simply creates a child anchor
1984 * with gtk_text_child_anchor_new() and inserts it into the buffer
1985 * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1986 * owned by the buffer; no reference count is returned to
1987 * the caller of gtk_text_buffer_create_child_anchor().
1989 * Return value: (transfer none): the created child anchor
1992 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
1995 GtkTextChildAnchor *anchor;
1997 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1998 g_return_val_if_fail (iter != NULL, NULL);
1999 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2001 anchor = gtk_text_child_anchor_new ();
2003 gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
2005 g_object_unref (anchor);
2015 gtk_text_buffer_mark_set (GtkTextBuffer *buffer,
2016 const GtkTextIter *location,
2019 /* IMO this should NOT work like insert_text and delete_range,
2020 * where the real action happens in the default handler.
2022 * The reason is that the default handler would be _required_,
2023 * i.e. the whole widget would start breaking and segfaulting if the
2024 * default handler didn't get run. So you can't really override the
2025 * default handler or stop the emission; that is, this signal is
2026 * purely for notification, and not to allow users to modify the
2030 g_object_ref (mark);
2032 g_signal_emit (buffer,
2038 g_object_unref (mark);
2042 * gtk_text_buffer_set_mark:
2043 * @buffer: a #GtkTextBuffer
2044 * @mark_name: name of the mark
2045 * @iter: location for the mark
2046 * @left_gravity: if the mark is created by this function, gravity for
2048 * @should_exist: if %TRUE, warn if the mark does not exist, and return
2051 * Move the mark to the given position, if not @should_exist,
2054 * Return value: mark
2057 gtk_text_buffer_set_mark (GtkTextBuffer *buffer,
2058 GtkTextMark *existing_mark,
2059 const gchar *mark_name,
2060 const GtkTextIter *iter,
2061 gboolean left_gravity,
2062 gboolean should_exist)
2064 GtkTextIter location;
2067 g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2069 mark = _gtk_text_btree_set_mark (get_btree (buffer),
2076 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2080 gtk_text_buffer_mark_set (buffer, &location, mark);
2086 * gtk_text_buffer_create_mark:
2087 * @buffer: a #GtkTextBuffer
2088 * @mark_name: (allow-none): name for mark, or %NULL
2089 * @where: location to place mark
2090 * @left_gravity: whether the mark has left gravity
2092 * Creates a mark at position @where. If @mark_name is %NULL, the mark
2093 * is anonymous; otherwise, the mark can be retrieved by name using
2094 * gtk_text_buffer_get_mark(). If a mark has left gravity, and text is
2095 * inserted at the mark's current location, the mark will be moved to
2096 * the left of the newly-inserted text. If the mark has right gravity
2097 * (@left_gravity = %FALSE), the mark will end up on the right of
2098 * newly-inserted text. The standard left-to-right cursor is a mark
2099 * with right gravity (when you type, the cursor stays on the right
2100 * side of the text you're typing).
2102 * The caller of this function does <emphasis>not</emphasis> own a
2103 * reference to the returned #GtkTextMark, so you can ignore the
2104 * return value if you like. Marks are owned by the buffer and go
2105 * away when the buffer does.
2107 * Emits the "mark-set" signal as notification of the mark's initial
2110 * Return value: (transfer none): the new #GtkTextMark object
2113 gtk_text_buffer_create_mark (GtkTextBuffer *buffer,
2114 const gchar *mark_name,
2115 const GtkTextIter *where,
2116 gboolean left_gravity)
2118 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2120 return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
2121 left_gravity, FALSE);
2125 * gtk_text_buffer_add_mark:
2126 * @buffer: a #GtkTextBuffer
2127 * @mark: the mark to add
2128 * @where: location to place mark
2130 * Adds the mark at position @where. The mark must not be added to
2131 * another buffer, and if its name is not %NULL then there must not
2132 * be another mark in the buffer with the same name.
2134 * Emits the "mark-set" signal as notification of the mark's initial
2140 gtk_text_buffer_add_mark (GtkTextBuffer *buffer,
2142 const GtkTextIter *where)
2146 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2147 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2148 g_return_if_fail (where != NULL);
2149 g_return_if_fail (gtk_text_mark_get_buffer (mark) == NULL);
2151 name = gtk_text_mark_get_name (mark);
2153 if (name != NULL && gtk_text_buffer_get_mark (buffer, name) != NULL)
2155 g_critical ("Mark %s already exists in the buffer", name);
2159 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, FALSE);
2163 * gtk_text_buffer_move_mark:
2164 * @buffer: a #GtkTextBuffer
2165 * @mark: a #GtkTextMark
2166 * @where: new location for @mark in @buffer
2168 * Moves @mark to the new location @where. Emits the "mark-set" signal
2169 * as notification of the move.
2172 gtk_text_buffer_move_mark (GtkTextBuffer *buffer,
2174 const GtkTextIter *where)
2176 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2177 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2178 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2180 gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
2184 * gtk_text_buffer_get_iter_at_mark:
2185 * @buffer: a #GtkTextBuffer
2186 * @iter: (out): iterator to initialize
2187 * @mark: a #GtkTextMark in @buffer
2189 * Initializes @iter with the current position of @mark.
2192 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
2196 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2197 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2198 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2200 _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2206 * gtk_text_buffer_delete_mark:
2207 * @buffer: a #GtkTextBuffer
2208 * @mark: a #GtkTextMark in @buffer
2210 * Deletes @mark, so that it's no longer located anywhere in the
2211 * buffer. Removes the reference the buffer holds to the mark, so if
2212 * you haven't called g_object_ref() on the mark, it will be freed. Even
2213 * if the mark isn't freed, most operations on @mark become
2214 * invalid, until it gets added to a buffer again with
2215 * gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to
2216 * find out if a mark has been removed from its buffer.
2217 * The "mark-deleted" signal will be emitted as notification after
2218 * the mark is deleted.
2221 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
2224 g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2225 g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2226 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2228 g_object_ref (mark);
2230 _gtk_text_btree_remove_mark (get_btree (buffer), mark);
2232 /* See rationale above for MARK_SET on why we emit this after
2233 * removing the mark, rather than removing the mark in a default
2236 g_signal_emit (buffer, signals[MARK_DELETED],
2240 g_object_unref (mark);
2244 * gtk_text_buffer_get_mark:
2245 * @buffer: a #GtkTextBuffer
2246 * @name: a mark name
2248 * Returns the mark named @name in buffer @buffer, or %NULL if no such
2249 * mark exists in the buffer.
2251 * Return value: (transfer none): a #GtkTextMark, or %NULL
2254 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2259 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2260 g_return_val_if_fail (name != NULL, NULL);
2262 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2268 * gtk_text_buffer_move_mark_by_name:
2269 * @buffer: a #GtkTextBuffer
2270 * @name: name of a mark
2271 * @where: new location for mark
2273 * Moves the mark named @name (which must exist) to location @where.
2274 * See gtk_text_buffer_move_mark() for details.
2277 gtk_text_buffer_move_mark_by_name (GtkTextBuffer *buffer,
2279 const GtkTextIter *where)
2283 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2284 g_return_if_fail (name != NULL);
2286 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2290 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2294 gtk_text_buffer_move_mark (buffer, mark, where);
2298 * gtk_text_buffer_delete_mark_by_name:
2299 * @buffer: a #GtkTextBuffer
2300 * @name: name of a mark in @buffer
2302 * Deletes the mark named @name; the mark must exist. See
2303 * gtk_text_buffer_delete_mark() for details.
2306 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2311 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2312 g_return_if_fail (name != NULL);
2314 mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2318 g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2322 gtk_text_buffer_delete_mark (buffer, mark);
2326 * gtk_text_buffer_get_insert:
2327 * @buffer: a #GtkTextBuffer
2329 * Returns the mark that represents the cursor (insertion point).
2330 * Equivalent to calling gtk_text_buffer_get_mark() to get the mark
2331 * named "insert", but very slightly more efficient, and involves less
2334 * Return value: (transfer none): insertion point mark
2337 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2339 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2341 return _gtk_text_btree_get_insert (get_btree (buffer));
2345 * gtk_text_buffer_get_selection_bound:
2346 * @buffer: a #GtkTextBuffer
2348 * Returns the mark that represents the selection bound. Equivalent
2349 * to calling gtk_text_buffer_get_mark() to get the mark named
2350 * "selection_bound", but very slightly more efficient, and involves
2353 * The currently-selected text in @buffer is the region between the
2354 * "selection_bound" and "insert" marks. If "selection_bound" and
2355 * "insert" are in the same place, then there is no current selection.
2356 * gtk_text_buffer_get_selection_bounds() is another convenient function
2357 * for handling the selection, if you just want to know whether there's a
2358 * selection and what its bounds are.
2360 * Return value: (transfer none): selection bound mark
2363 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2365 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2367 return _gtk_text_btree_get_selection_bound (get_btree (buffer));
2371 * gtk_text_buffer_get_iter_at_child_anchor:
2372 * @buffer: a #GtkTextBuffer
2373 * @iter: (out): an iterator to be initialized
2374 * @anchor: a child anchor that appears in @buffer
2376 * Obtains the location of @anchor within @buffer.
2379 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer *buffer,
2381 GtkTextChildAnchor *anchor)
2383 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2384 g_return_if_fail (iter != NULL);
2385 g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2386 g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2388 _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2394 * gtk_text_buffer_place_cursor:
2395 * @buffer: a #GtkTextBuffer
2396 * @where: where to put the cursor
2398 * This function moves the "insert" and "selection_bound" marks
2399 * simultaneously. If you move them to the same place in two steps
2400 * with gtk_text_buffer_move_mark(), you will temporarily select a
2401 * region in between their old and new locations, which can be pretty
2402 * inefficient since the temporarily-selected region will force stuff
2403 * to be recalculated. This function moves them as a unit, which can
2407 gtk_text_buffer_place_cursor (GtkTextBuffer *buffer,
2408 const GtkTextIter *where)
2410 gtk_text_buffer_select_range (buffer, where, where);
2414 * gtk_text_buffer_select_range:
2415 * @buffer: a #GtkTextBuffer
2416 * @ins: where to put the "insert" mark
2417 * @bound: where to put the "selection_bound" mark
2419 * This function moves the "insert" and "selection_bound" marks
2420 * simultaneously. If you move them in two steps
2421 * with gtk_text_buffer_move_mark(), you will temporarily select a
2422 * region in between their old and new locations, which can be pretty
2423 * inefficient since the temporarily-selected region will force stuff
2424 * to be recalculated. This function moves them as a unit, which can
2430 gtk_text_buffer_select_range (GtkTextBuffer *buffer,
2431 const GtkTextIter *ins,
2432 const GtkTextIter *bound)
2434 GtkTextIter real_ins;
2435 GtkTextIter real_bound;
2437 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2440 real_bound = *bound;
2442 _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2443 gtk_text_buffer_mark_set (buffer, &real_ins,
2444 gtk_text_buffer_get_insert (buffer));
2445 gtk_text_buffer_mark_set (buffer, &real_bound,
2446 gtk_text_buffer_get_selection_bound (buffer));
2454 * gtk_text_buffer_create_tag:
2455 * @buffer: a #GtkTextBuffer
2456 * @tag_name: (allow-none): name of the new tag, or %NULL
2457 * @first_property_name: (allow-none): name of first property to set, or %NULL
2458 * @Varargs: %NULL-terminated list of property names and values
2460 * Creates a tag and adds it to the tag table for @buffer.
2461 * Equivalent to calling gtk_text_tag_new() and then adding the
2462 * tag to the buffer's tag table. The returned tag is owned by
2463 * the buffer's tag table, so the ref count will be equal to one.
2465 * If @tag_name is %NULL, the tag is anonymous.
2467 * If @tag_name is non-%NULL, a tag called @tag_name must not already
2468 * exist in the tag table for this buffer.
2470 * The @first_property_name argument and subsequent arguments are a list
2471 * of properties to set on the tag, as with g_object_set().
2473 * Return value: (transfer none): a new tag
2476 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2477 const gchar *tag_name,
2478 const gchar *first_property_name,
2484 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2486 tag = gtk_text_tag_new (tag_name);
2488 gtk_text_tag_table_add (get_table (buffer), tag);
2490 if (first_property_name)
2492 va_start (list, first_property_name);
2493 g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2497 g_object_unref (tag);
2503 gtk_text_buffer_real_apply_tag (GtkTextBuffer *buffer,
2505 const GtkTextIter *start,
2506 const GtkTextIter *end)
2508 if (tag->table != buffer->priv->tag_table)
2510 g_warning ("Can only apply tags that are in the tag table for the buffer");
2514 _gtk_text_btree_tag (start, end, tag, TRUE);
2518 gtk_text_buffer_real_remove_tag (GtkTextBuffer *buffer,
2520 const GtkTextIter *start,
2521 const GtkTextIter *end)
2523 if (tag->table != buffer->priv->tag_table)
2525 g_warning ("Can only remove tags that are in the tag table for the buffer");
2529 _gtk_text_btree_tag (start, end, tag, FALSE);
2533 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2535 gtk_text_buffer_set_modified (buffer, TRUE);
2539 gtk_text_buffer_real_mark_set (GtkTextBuffer *buffer,
2540 const GtkTextIter *iter,
2543 GtkTextMark *insert;
2545 insert = gtk_text_buffer_get_insert (buffer);
2547 if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2549 gboolean has_selection;
2551 update_selection_clipboards (buffer);
2553 has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2557 if (has_selection != buffer->priv->has_selection)
2559 buffer->priv->has_selection = has_selection;
2560 g_object_notify (G_OBJECT (buffer), "has-selection");
2565 g_object_notify (G_OBJECT (buffer), "cursor-position");
2569 gtk_text_buffer_emit_tag (GtkTextBuffer *buffer,
2572 const GtkTextIter *start,
2573 const GtkTextIter *end)
2575 GtkTextIter start_tmp = *start;
2576 GtkTextIter end_tmp = *end;
2578 g_return_if_fail (tag != NULL);
2580 gtk_text_iter_order (&start_tmp, &end_tmp);
2583 g_signal_emit (buffer, signals[APPLY_TAG],
2585 tag, &start_tmp, &end_tmp);
2587 g_signal_emit (buffer, signals[REMOVE_TAG],
2589 tag, &start_tmp, &end_tmp);
2593 * gtk_text_buffer_apply_tag:
2594 * @buffer: a #GtkTextBuffer
2595 * @tag: a #GtkTextTag
2596 * @start: one bound of range to be tagged
2597 * @end: other bound of range to be tagged
2599 * Emits the "apply-tag" signal on @buffer. The default
2600 * handler for the signal applies @tag to the given range.
2601 * @start and @end do not have to be in order.
2604 gtk_text_buffer_apply_tag (GtkTextBuffer *buffer,
2606 const GtkTextIter *start,
2607 const GtkTextIter *end)
2609 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2610 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2611 g_return_if_fail (start != NULL);
2612 g_return_if_fail (end != NULL);
2613 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2614 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2615 g_return_if_fail (tag->table == buffer->priv->tag_table);
2617 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2621 * gtk_text_buffer_remove_tag:
2622 * @buffer: a #GtkTextBuffer
2623 * @tag: a #GtkTextTag
2624 * @start: one bound of range to be untagged
2625 * @end: other bound of range to be untagged
2627 * Emits the "remove-tag" signal. The default handler for the signal
2628 * removes all occurrences of @tag from the given range. @start and
2629 * @end don't have to be in order.
2632 gtk_text_buffer_remove_tag (GtkTextBuffer *buffer,
2634 const GtkTextIter *start,
2635 const GtkTextIter *end)
2638 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2639 g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2640 g_return_if_fail (start != NULL);
2641 g_return_if_fail (end != NULL);
2642 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2643 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2644 g_return_if_fail (tag->table == buffer->priv->tag_table);
2646 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2650 * gtk_text_buffer_apply_tag_by_name:
2651 * @buffer: a #GtkTextBuffer
2652 * @name: name of a named #GtkTextTag
2653 * @start: one bound of range to be tagged
2654 * @end: other bound of range to be tagged
2656 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2657 * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2660 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer *buffer,
2662 const GtkTextIter *start,
2663 const GtkTextIter *end)
2667 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2668 g_return_if_fail (name != NULL);
2669 g_return_if_fail (start != NULL);
2670 g_return_if_fail (end != NULL);
2671 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2672 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2674 tag = gtk_text_tag_table_lookup (get_table (buffer),
2679 g_warning ("Unknown tag `%s'", name);
2683 gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2687 * gtk_text_buffer_remove_tag_by_name:
2688 * @buffer: a #GtkTextBuffer
2689 * @name: name of a #GtkTextTag
2690 * @start: one bound of range to be untagged
2691 * @end: other bound of range to be untagged
2693 * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2694 * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2697 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer *buffer,
2699 const GtkTextIter *start,
2700 const GtkTextIter *end)
2704 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2705 g_return_if_fail (name != NULL);
2706 g_return_if_fail (start != NULL);
2707 g_return_if_fail (end != NULL);
2708 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2709 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2711 tag = gtk_text_tag_table_lookup (get_table (buffer),
2716 g_warning ("Unknown tag `%s'", name);
2720 gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2724 pointer_cmp (gconstpointer a,
2736 * gtk_text_buffer_remove_all_tags:
2737 * @buffer: a #GtkTextBuffer
2738 * @start: one bound of range to be untagged
2739 * @end: other bound of range to be untagged
2741 * Removes all tags in the range between @start and @end. Be careful
2742 * with this function; it could remove tags added in code unrelated to
2743 * the code you're currently writing. That is, using this function is
2744 * probably a bad idea if you have two or more unrelated code sections
2748 gtk_text_buffer_remove_all_tags (GtkTextBuffer *buffer,
2749 const GtkTextIter *start,
2750 const GtkTextIter *end)
2752 GtkTextIter first, second, tmp;
2755 GSList *prev, *next;
2758 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2759 g_return_if_fail (start != NULL);
2760 g_return_if_fail (end != NULL);
2761 g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2762 g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2767 gtk_text_iter_order (&first, &second);
2769 /* Get all tags turned on at the start */
2770 tags = gtk_text_iter_get_tags (&first);
2772 /* Find any that are toggled on within the range */
2774 while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2779 if (gtk_text_iter_compare (&tmp, &second) >= 0)
2780 break; /* past the end of the range */
2782 toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2784 /* We could end up with a really big-ass list here.
2787 tmp_list2 = toggled;
2788 while (tmp_list2 != NULL)
2790 tags = g_slist_prepend (tags, tmp_list2->data);
2792 tmp_list2 = g_slist_next (tmp_list2);
2795 g_slist_free (toggled);
2799 tags = g_slist_sort (tags, pointer_cmp);
2801 /* Strip duplicates */
2805 while (tmp_list != NULL)
2807 if (tag == tmp_list->data)
2810 next = tmp_list->next;
2814 tmp_list->next = NULL;
2816 g_slist_free (tmp_list);
2819 /* prev is unchanged */
2823 /* not a duplicate */
2824 tag = GTK_TEXT_TAG (tmp_list->data);
2826 tmp_list = tmp_list->next;
2830 g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2833 while (tmp_list != NULL)
2835 tag = GTK_TEXT_TAG (tmp_list->data);
2837 gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2839 tmp_list = tmp_list->next;
2842 g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2844 g_slist_free (tags);
2849 * Obtain various iterators
2853 * gtk_text_buffer_get_iter_at_line_offset:
2854 * @buffer: a #GtkTextBuffer
2855 * @iter: (out): iterator to initialize
2856 * @line_number: line number counting from 0
2857 * @char_offset: char offset from start of line
2859 * Obtains an iterator pointing to @char_offset within the given
2860 * line. The @char_offset must exist, offsets off the end of the line
2861 * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2862 * UTF-8 may encode one character as multiple bytes.
2865 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2870 g_return_if_fail (iter != NULL);
2871 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2873 _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2874 iter, line_number, char_offset);
2878 * gtk_text_buffer_get_iter_at_line_index:
2879 * @buffer: a #GtkTextBuffer
2880 * @iter: (out): iterator to initialize
2881 * @line_number: line number counting from 0
2882 * @byte_index: byte index from start of line
2884 * Obtains an iterator pointing to @byte_index within the given line.
2885 * @byte_index must be the start of a UTF-8 character, and must not be
2886 * beyond the end of the line. Note <emphasis>bytes</emphasis>, not
2887 * characters; UTF-8 may encode one character as multiple bytes.
2890 gtk_text_buffer_get_iter_at_line_index (GtkTextBuffer *buffer,
2895 g_return_if_fail (iter != NULL);
2896 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2898 _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2899 iter, line_number, byte_index);
2903 * gtk_text_buffer_get_iter_at_line:
2904 * @buffer: a #GtkTextBuffer
2905 * @iter: (out): iterator to initialize
2906 * @line_number: line number counting from 0
2908 * Initializes @iter to the start of the given line.
2911 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2915 g_return_if_fail (iter != NULL);
2916 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2918 gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2922 * gtk_text_buffer_get_iter_at_offset:
2923 * @buffer: a #GtkTextBuffer
2924 * @iter: (out): iterator to initialize
2925 * @char_offset: char offset from start of buffer, counting from 0, or -1
2927 * Initializes @iter to a position @char_offset chars from the start
2928 * of the entire buffer. If @char_offset is -1 or greater than the number
2929 * of characters in the buffer, @iter is initialized to the end iterator,
2930 * the iterator one past the last valid character in the buffer.
2933 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2937 g_return_if_fail (iter != NULL);
2938 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2940 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2944 * gtk_text_buffer_get_start_iter:
2945 * @buffer: a #GtkTextBuffer
2946 * @iter: (out): iterator to initialize
2948 * Initialized @iter with the first position in the text buffer. This
2949 * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2950 * the iter at character offset 0.
2953 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2956 g_return_if_fail (iter != NULL);
2957 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2959 _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2963 * gtk_text_buffer_get_end_iter:
2964 * @buffer: a #GtkTextBuffer
2965 * @iter: (out): iterator to initialize
2967 * Initializes @iter with the "end iterator," one past the last valid
2968 * character in the text buffer. If dereferenced with
2969 * gtk_text_iter_get_char(), the end iterator has a character value of
2970 * 0. The entire buffer lies in the range from the first position in
2971 * the buffer (call gtk_text_buffer_get_start_iter() to get
2972 * character position 0) to the end iterator.
2975 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2978 g_return_if_fail (iter != NULL);
2979 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2981 _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2985 * gtk_text_buffer_get_bounds:
2986 * @buffer: a #GtkTextBuffer
2987 * @start: (out): iterator to initialize with first position in the buffer
2988 * @end: (out): iterator to initialize with the end iterator
2990 * Retrieves the first and last iterators in the buffer, i.e. the
2991 * entire buffer lies within the range [@start,@end).
2994 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
2998 g_return_if_fail (start != NULL);
2999 g_return_if_fail (end != NULL);
3000 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3002 _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
3003 _gtk_text_btree_get_end_iter (get_btree (buffer), end);
3011 * gtk_text_buffer_get_modified:
3012 * @buffer: a #GtkTextBuffer
3014 * Indicates whether the buffer has been modified since the last call
3015 * to gtk_text_buffer_set_modified() set the modification flag to
3016 * %FALSE. Used for example to enable a "save" function in a text
3019 * Return value: %TRUE if the buffer has been modified
3022 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
3024 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3026 return buffer->priv->modified;
3030 * gtk_text_buffer_set_modified:
3031 * @buffer: a #GtkTextBuffer
3032 * @setting: modification flag setting
3034 * Used to keep track of whether the buffer has been modified since the
3035 * last time it was saved. Whenever the buffer is saved to disk, call
3036 * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
3037 * it will automatically toggled on the modified bit again. When the modified
3038 * bit flips, the buffer emits a "modified-changed" signal.
3041 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
3044 gboolean fixed_setting;
3046 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3048 fixed_setting = setting != FALSE;
3050 if (buffer->priv->modified == fixed_setting)
3054 buffer->priv->modified = fixed_setting;
3055 g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
3060 * gtk_text_buffer_get_has_selection:
3061 * @buffer: a #GtkTextBuffer
3063 * Indicates whether the buffer has some text currently selected.
3065 * Return value: %TRUE if the there is text selected
3070 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
3072 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3074 return buffer->priv->has_selection;
3079 * Assorted other stuff
3083 * gtk_text_buffer_get_line_count:
3084 * @buffer: a #GtkTextBuffer
3086 * Obtains the number of lines in the buffer. This value is cached, so
3087 * the function is very fast.
3089 * Return value: number of lines in the buffer
3092 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
3094 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3096 return _gtk_text_btree_line_count (get_btree (buffer));
3100 * gtk_text_buffer_get_char_count:
3101 * @buffer: a #GtkTextBuffer
3103 * Gets the number of characters in the buffer; note that characters
3104 * and bytes are not the same, you can't e.g. expect the contents of
3105 * the buffer in string form to be this many bytes long. The character
3106 * count is cached, so this function is very fast.
3108 * Return value: number of characters in the buffer
3111 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
3113 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3115 return _gtk_text_btree_char_count (get_btree (buffer));
3118 /* Called when we lose the primary selection.
3121 clipboard_clear_selection_cb (GtkClipboard *clipboard,
3124 /* Move selection_bound to the insertion point */
3126 GtkTextIter selection_bound;
3127 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3129 gtk_text_buffer_get_iter_at_mark (buffer, &insert,
3130 gtk_text_buffer_get_insert (buffer));
3131 gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
3132 gtk_text_buffer_get_selection_bound (buffer));
3134 if (!gtk_text_iter_equal (&insert, &selection_bound))
3135 gtk_text_buffer_move_mark (buffer,
3136 gtk_text_buffer_get_selection_bound (buffer),
3140 /* Called when we have the primary selection and someone else wants our
3141 * data in order to paste it.
3144 clipboard_get_selection_cb (GtkClipboard *clipboard,
3145 GtkSelectionData *selection_data,
3149 GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3150 GtkTextIter start, end;
3152 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3154 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3156 /* Provide the address of the buffer; this will only be
3157 * used within-process
3159 gtk_selection_data_set (selection_data,
3160 selection_data->target,
3165 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3170 str = gtk_text_buffer_serialize (buffer, buffer,
3171 selection_data->target,
3172 &start, &end, &len);
3174 gtk_selection_data_set (selection_data,
3175 selection_data->target,
3184 str = gtk_text_iter_get_visible_text (&start, &end);
3185 gtk_selection_data_set_text (selection_data, str, -1);
3191 static GtkTextBuffer *
3192 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
3194 GtkTextBuffer *contents;
3196 contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
3198 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
3200 g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
3201 GINT_TO_POINTER (1));
3203 /* Ref the source buffer as long as the clipboard contents buffer
3204 * exists, because it's needed for serializing the contents buffer.
3205 * See http://bugzilla.gnome.org/show_bug.cgi?id=339195
3207 g_object_ref (buffer);
3208 g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
3213 /* Provide cut/copied data */
3215 clipboard_get_contents_cb (GtkClipboard *clipboard,
3216 GtkSelectionData *selection_data,
3220 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3222 g_assert (contents); /* This should never be called unless we own the clipboard */
3224 if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3226 /* Provide the address of the clipboard buffer; this will only
3227 * be used within-process. OK to supply a NULL value for contents.
3229 gtk_selection_data_set (selection_data,
3230 selection_data->target,
3235 else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3237 GtkTextBuffer *clipboard_source_buffer;
3238 GtkTextIter start, end;
3242 clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3243 "gtk-text-buffer-clipboard-source");
3245 gtk_text_buffer_get_bounds (contents, &start, &end);
3247 str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3248 selection_data->target,
3249 &start, &end, &len);
3251 gtk_selection_data_set (selection_data,
3252 selection_data->target,
3260 GtkTextIter start, end;
3262 gtk_text_buffer_get_bounds (contents, &start, &end);
3264 str = gtk_text_iter_get_visible_text (&start, &end);
3265 gtk_selection_data_set_text (selection_data, str, -1);
3271 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3274 GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3276 g_object_unref (contents);
3280 get_paste_point (GtkTextBuffer *buffer,
3282 gboolean clear_afterward)
3284 GtkTextIter insert_point;
3285 GtkTextMark *paste_point_override;
3287 paste_point_override = gtk_text_buffer_get_mark (buffer,
3288 "gtk_paste_point_override");
3290 if (paste_point_override != NULL)
3292 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3293 paste_point_override);
3294 if (clear_afterward)
3295 gtk_text_buffer_delete_mark (buffer,
3296 gtk_text_buffer_get_mark (buffer,
3297 "gtk_paste_point_override"));
3301 gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3302 gtk_text_buffer_get_insert (buffer));
3305 *iter = insert_point;
3309 pre_paste_prep (ClipboardRequest *request_data,
3310 GtkTextIter *insert_point)
3312 GtkTextBuffer *buffer = request_data->buffer;
3314 get_paste_point (buffer, insert_point, TRUE);
3316 /* If we're going to replace the selection, we insert before it to
3317 * avoid messing it up, then we delete the selection after inserting.
3319 if (request_data->replace_selection)
3321 GtkTextIter start, end;
3323 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3324 *insert_point = start;
3329 post_paste_cleanup (ClipboardRequest *request_data)
3331 if (request_data->replace_selection)
3333 GtkTextIter start, end;
3335 if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3338 if (request_data->interactive)
3339 gtk_text_buffer_delete_interactive (request_data->buffer,
3342 request_data->default_editable);
3344 gtk_text_buffer_delete (request_data->buffer, &start, &end);
3350 emit_paste_done (GtkTextBuffer *buffer,
3351 GtkClipboard *clipboard)
3353 g_signal_emit (buffer, signals[PASTE_DONE], 0, clipboard);
3357 free_clipboard_request (ClipboardRequest *request_data)
3359 g_object_unref (request_data->buffer);
3360 g_free (request_data);
3363 /* Called when we request a paste and receive the text data
3366 clipboard_text_received (GtkClipboard *clipboard,
3370 ClipboardRequest *request_data = data;
3371 GtkTextBuffer *buffer = request_data->buffer;
3375 GtkTextIter insert_point;
3377 if (request_data->interactive)
3378 gtk_text_buffer_begin_user_action (buffer);
3380 pre_paste_prep (request_data, &insert_point);
3382 if (request_data->interactive)
3383 gtk_text_buffer_insert_interactive (buffer, &insert_point,
3384 str, -1, request_data->default_editable);
3386 gtk_text_buffer_insert (buffer, &insert_point,
3389 post_paste_cleanup (request_data);
3391 if (request_data->interactive)
3392 gtk_text_buffer_end_user_action (buffer);
3394 emit_paste_done (buffer, clipboard);
3397 free_clipboard_request (request_data);
3400 static GtkTextBuffer*
3401 selection_data_get_buffer (GtkSelectionData *selection_data,
3402 ClipboardRequest *request_data)
3405 GtkTextBuffer *src_buffer = NULL;
3407 /* If we can get the owner, the selection is in-process */
3408 owner = gdk_selection_owner_get_for_display (selection_data->display,
3409 selection_data->selection);
3414 if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3417 if (selection_data->type !=
3418 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3421 if (selection_data->length != sizeof (src_buffer))
3424 memcpy (&src_buffer, selection_data->data, sizeof (src_buffer));
3426 if (src_buffer == NULL)
3429 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3431 if (gtk_text_buffer_get_tag_table (src_buffer) !=
3432 gtk_text_buffer_get_tag_table (request_data->buffer))
3439 /* These are pretty handy functions; maybe something like them
3440 * should be in the public API. Also, there are other places in this
3441 * file where they could be used.
3444 save_iter (const GtkTextIter *iter,
3445 gboolean left_gravity)
3447 return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3454 restore_iter (const GtkTextIter *iter,
3457 gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3458 (GtkTextIter*) iter,
3460 gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3466 clipboard_rich_text_received (GtkClipboard *clipboard,
3472 ClipboardRequest *request_data = data;
3473 GtkTextIter insert_point;
3474 gboolean retval = TRUE;
3475 GError *error = NULL;
3477 if (text != NULL && length > 0)
3479 pre_paste_prep (request_data, &insert_point);
3481 if (request_data->interactive)
3482 gtk_text_buffer_begin_user_action (request_data->buffer);
3484 if (!request_data->interactive ||
3485 gtk_text_iter_can_insert (&insert_point,
3486 request_data->default_editable))
3488 retval = gtk_text_buffer_deserialize (request_data->buffer,
3489 request_data->buffer,
3498 g_warning ("error pasting: %s\n", error->message);
3499 g_clear_error (&error);
3502 if (request_data->interactive)
3503 gtk_text_buffer_end_user_action (request_data->buffer);
3505 emit_paste_done (request_data->buffer, clipboard);
3509 post_paste_cleanup (request_data);
3514 /* Request the text selection instead */
3515 gtk_clipboard_request_text (clipboard,
3516 clipboard_text_received,
3521 paste_from_buffer (GtkClipboard *clipboard,
3522 ClipboardRequest *request_data,
3523 GtkTextBuffer *src_buffer,
3524 const GtkTextIter *start,
3525 const GtkTextIter *end)
3527 GtkTextIter insert_point;
3528 GtkTextBuffer *buffer = request_data->buffer;
3530 /* We're about to emit a bunch of signals, so be safe */
3531 g_object_ref (src_buffer);
3533 pre_paste_prep (request_data, &insert_point);
3535 if (request_data->interactive)
3536 gtk_text_buffer_begin_user_action (buffer);
3538 if (!gtk_text_iter_equal (start, end))
3540 if (!request_data->interactive ||
3541 (gtk_text_iter_can_insert (&insert_point,
3542 request_data->default_editable)))
3543 gtk_text_buffer_real_insert_range (buffer,
3547 request_data->interactive);
3550 post_paste_cleanup (request_data);
3552 if (request_data->interactive)
3553 gtk_text_buffer_end_user_action (buffer);
3555 emit_paste_done (buffer, clipboard);
3557 g_object_unref (src_buffer);
3559 free_clipboard_request (request_data);
3563 clipboard_clipboard_buffer_received (GtkClipboard *clipboard,
3564 GtkSelectionData *selection_data,
3567 ClipboardRequest *request_data = data;
3568 GtkTextBuffer *src_buffer;
3570 src_buffer = selection_data_get_buffer (selection_data, request_data);
3574 GtkTextIter start, end;
3576 if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3578 gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3580 paste_from_buffer (clipboard, request_data, src_buffer,
3585 if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3586 paste_from_buffer (clipboard, request_data, src_buffer,
3592 if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3593 request_data->buffer))
3595 /* Request rich text */
3596 gtk_clipboard_request_rich_text (clipboard,
3597 request_data->buffer,
3598 clipboard_rich_text_received,
3603 /* Request the text selection instead */
3604 gtk_clipboard_request_text (clipboard,
3605 clipboard_text_received,
3613 GtkClipboard *clipboard;
3615 } SelectionClipboard;
3618 update_selection_clipboards (GtkTextBuffer *buffer)
3620 GtkTextBufferPrivate *priv;
3621 GSList *tmp_list = buffer->priv->selection_clipboards;
3623 priv = buffer->priv;
3625 gtk_text_buffer_get_copy_target_list (buffer);
3632 SelectionClipboard *selection_clipboard = tmp_list->data;
3633 GtkClipboard *clipboard = selection_clipboard->clipboard;
3635 /* Determine whether we have a selection and adjust X selection
3638 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3640 if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3641 gtk_clipboard_clear (clipboard);
3645 /* Even if we already have the selection, we need to update our
3648 if (!gtk_clipboard_set_with_owner (clipboard,
3649 priv->copy_target_entries,
3650 priv->n_copy_target_entries,
3651 clipboard_get_selection_cb,
3652 clipboard_clear_selection_cb,
3654 clipboard_clear_selection_cb (clipboard, buffer);
3657 tmp_list = tmp_list->next;
3661 static SelectionClipboard *
3662 find_selection_clipboard (GtkTextBuffer *buffer,
3663 GtkClipboard *clipboard)
3665 GSList *tmp_list = buffer->priv->selection_clipboards;
3668 SelectionClipboard *selection_clipboard = tmp_list->data;
3669 if (selection_clipboard->clipboard == clipboard)
3670 return selection_clipboard;
3672 tmp_list = tmp_list->next;
3679 * gtk_text_buffer_add_selection_clipboard:
3680 * @buffer: a #GtkTextBuffer
3681 * @clipboard: a #GtkClipboard
3683 * Adds @clipboard to the list of clipboards in which the selection
3684 * contents of @buffer are available. In most cases, @clipboard will be
3685 * the #GtkClipboard of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3688 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3689 GtkClipboard *clipboard)
3691 SelectionClipboard *selection_clipboard;
3693 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3694 g_return_if_fail (clipboard != NULL);
3696 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3697 if (selection_clipboard)
3699 selection_clipboard->ref_count++;
3703 selection_clipboard = g_new (SelectionClipboard, 1);
3705 selection_clipboard->clipboard = clipboard;
3706 selection_clipboard->ref_count = 1;
3708 buffer->priv->selection_clipboards = g_slist_prepend (buffer->priv->selection_clipboards,
3709 selection_clipboard);
3714 * gtk_text_buffer_remove_selection_clipboard:
3715 * @buffer: a #GtkTextBuffer
3716 * @clipboard: a #GtkClipboard added to @buffer by
3717 * gtk_text_buffer_add_selection_clipboard()
3719 * Removes a #GtkClipboard added with
3720 * gtk_text_buffer_add_selection_clipboard().
3723 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3724 GtkClipboard *clipboard)
3726 SelectionClipboard *selection_clipboard;
3728 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3729 g_return_if_fail (clipboard != NULL);
3731 selection_clipboard = find_selection_clipboard (buffer, clipboard);
3732 g_return_if_fail (selection_clipboard != NULL);
3734 selection_clipboard->ref_count--;
3735 if (selection_clipboard->ref_count == 0)
3737 if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3738 gtk_clipboard_clear (selection_clipboard->clipboard);
3740 buffer->priv->selection_clipboards = g_slist_remove (buffer->priv->selection_clipboards,
3741 selection_clipboard);
3743 g_free (selection_clipboard);
3748 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3750 GtkTextBufferPrivate *priv = buffer->priv;
3752 g_slist_foreach (priv->selection_clipboards, (GFunc)g_free, NULL);
3753 g_slist_free (priv->selection_clipboards);
3754 priv->selection_clipboards = NULL;
3758 * gtk_text_buffer_paste_clipboard:
3759 * @buffer: a #GtkTextBuffer
3760 * @clipboard: the #GtkClipboard to paste from
3761 * @override_location: (allow-none): location to insert pasted text, or %NULL for
3763 * @default_editable: whether the buffer is editable by default
3765 * Pastes the contents of a clipboard at the insertion point, or
3766 * at @override_location. (Note: pasting is asynchronous, that is,
3767 * we'll ask for the paste data and return, and at some point later
3768 * after the main loop runs, the paste data will be inserted.)
3771 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3772 GtkClipboard *clipboard,
3773 GtkTextIter *override_location,
3774 gboolean default_editable)
3776 ClipboardRequest *data = g_new (ClipboardRequest, 1);
3777 GtkTextIter paste_point;
3778 GtkTextIter start, end;
3780 if (override_location != NULL)
3781 gtk_text_buffer_create_mark (buffer,
3782 "gtk_paste_point_override",
3783 override_location, FALSE);
3785 data->buffer = g_object_ref (buffer);
3786 data->interactive = TRUE;
3787 data->default_editable = default_editable;
3789 /* When pasting with the cursor inside the selection area, you
3790 * replace the selection with the new text, otherwise, you
3791 * simply insert the new text at the point where the click
3792 * occured, unselecting any selected text. The replace_selection
3793 * flag toggles this behavior.
3795 data->replace_selection = FALSE;
3797 get_paste_point (buffer, &paste_point, FALSE);
3798 if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3799 (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3800 gtk_text_iter_equal (&paste_point, &end)))
3801 data->replace_selection = TRUE;
3803 gtk_clipboard_request_contents (clipboard,
3804 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3805 clipboard_clipboard_buffer_received, data);
3809 * gtk_text_buffer_delete_selection:
3810 * @buffer: a #GtkTextBuffer
3811 * @interactive: whether the deletion is caused by user interaction
3812 * @default_editable: whether the buffer is editable by default
3814 * Deletes the range between the "insert" and "selection_bound" marks,
3815 * that is, the currently-selected text. If @interactive is %TRUE,
3816 * the editability of the selection will be considered (users can't delete
3819 * Return value: whether there was a non-empty selection to delete
3822 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3823 gboolean interactive,
3824 gboolean default_editable)
3829 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3831 return FALSE; /* No selection */
3836 gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3838 gtk_text_buffer_delete (buffer, &start, &end);
3840 return TRUE; /* We deleted stuff */
3845 * gtk_text_buffer_backspace:
3846 * @buffer: a #GtkTextBuffer
3847 * @iter: a position in @buffer
3848 * @interactive: whether the deletion is caused by user interaction
3849 * @default_editable: whether the buffer is editable by default
3851 * Performs the appropriate action as if the user hit the delete
3852 * key with the cursor at the position specified by @iter. In the
3853 * normal case a single character will be deleted, but when
3854 * combining accents are involved, more than one character can
3855 * be deleted, and when precomposed character and accent combinations
3856 * are involved, less than one character will be deleted.
3858 * Because the buffer is modified, all outstanding iterators become
3859 * invalid after calling this function; however, the @iter will be
3860 * re-initialized to point to the location where text was deleted.
3862 * Return value: %TRUE if the buffer was modified
3867 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3869 gboolean interactive,
3870 gboolean default_editable)
3872 gchar *cluster_text;
3875 gboolean retval = FALSE;
3876 const PangoLogAttr *attrs;
3878 gboolean backspace_deletes_character;
3880 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3881 g_return_val_if_fail (iter != NULL, FALSE);
3886 attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3888 /* For no good reason, attrs is NULL for the empty last line in
3889 * a buffer. Special case that here. (#156164)
3893 offset = gtk_text_iter_get_line_offset (&start);
3894 backspace_deletes_character = attrs[offset].backspace_deletes_character;
3897 backspace_deletes_character = FALSE;
3899 gtk_text_iter_backward_cursor_position (&start);
3901 if (gtk_text_iter_equal (&start, &end))
3904 cluster_text = gtk_text_iter_get_text (&start, &end);
3907 gtk_text_buffer_begin_user_action (buffer);
3909 if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3912 /* special case \r\n, since we never want to reinsert \r */
3913 if (backspace_deletes_character && strcmp ("\r\n", cluster_text))
3915 gchar *normalized_text = g_utf8_normalize (cluster_text,
3916 strlen (cluster_text),
3918 glong len = g_utf8_strlen (normalized_text, -1);
3921 gtk_text_buffer_insert_interactive (buffer,
3924 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3927 g_free (normalized_text);
3934 gtk_text_buffer_end_user_action (buffer);
3936 g_free (cluster_text);
3938 /* Revalidate the users iter */
3945 cut_or_copy (GtkTextBuffer *buffer,
3946 GtkClipboard *clipboard,
3947 gboolean delete_region_after,
3948 gboolean interactive,
3949 gboolean default_editable)
3951 GtkTextBufferPrivate *priv;
3953 /* We prefer to cut the selected region between selection_bound and
3954 * insertion point. If that region is empty, then we cut the region
3955 * between the "anchor" and the insertion point (this is for
3956 * C-space and M-w and other Emacs-style copy/yank keys). Note that
3957 * insert and selection_bound are guaranteed to exist, but the
3958 * anchor only exists sometimes.
3963 priv = buffer->priv;
3965 gtk_text_buffer_get_copy_target_list (buffer);
3967 if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3969 /* Let's try the anchor thing */
3970 GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3976 gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3977 gtk_text_iter_order (&start, &end);
3981 if (!gtk_text_iter_equal (&start, &end))
3984 GtkTextBuffer *contents;
3986 contents = create_clipboard_contents_buffer (buffer);
3988 gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
3990 gtk_text_buffer_insert_range (contents, &ins, &start, &end);
3992 if (!gtk_clipboard_set_with_data (clipboard,
3993 priv->copy_target_entries,
3994 priv->n_copy_target_entries,
3995 clipboard_get_contents_cb,
3996 clipboard_clear_contents_cb,
3998 g_object_unref (contents);
4000 gtk_clipboard_set_can_store (clipboard,
4001 priv->copy_target_entries + 1,
4002 priv->n_copy_target_entries - 1);
4004 if (delete_region_after)
4007 gtk_text_buffer_delete_interactive (buffer, &start, &end,
4010 gtk_text_buffer_delete (buffer, &start, &end);
4016 * gtk_text_buffer_cut_clipboard:
4017 * @buffer: a #GtkTextBuffer
4018 * @clipboard: the #GtkClipboard object to cut to
4019 * @default_editable: default editability of the buffer
4021 * Copies the currently-selected text to a clipboard, then deletes
4022 * said text if it's editable.
4025 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
4026 GtkClipboard *clipboard,
4027 gboolean default_editable)
4029 gtk_text_buffer_begin_user_action (buffer);
4030 cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
4031 gtk_text_buffer_end_user_action (buffer);
4035 * gtk_text_buffer_copy_clipboard:
4036 * @buffer: a #GtkTextBuffer
4037 * @clipboard: the #GtkClipboard object to copy to
4039 * Copies the currently-selected text to a clipboard.
4042 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
4043 GtkClipboard *clipboard)
4045 cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
4049 * gtk_text_buffer_get_selection_bounds:
4050 * @buffer: a #GtkTextBuffer a #GtkTextBuffer
4051 * @start: (out): iterator to initialize with selection start
4052 * @end: (out): iterator to initialize with selection end
4054 * Returns %TRUE if some text is selected; places the bounds
4055 * of the selection in @start and @end (if the selection has length 0,
4056 * then @start and @end are filled in with the same value).
4057 * @start and @end will be in ascending order. If @start and @end are
4058 * NULL, then they are not filled in, but the return value still indicates
4059 * whether text is selected.
4061 * Return value: whether the selection has nonzero length
4064 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
4068 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
4070 return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
4074 * gtk_text_buffer_begin_user_action:
4075 * @buffer: a #GtkTextBuffer
4077 * Called to indicate that the buffer operations between here and a
4078 * call to gtk_text_buffer_end_user_action() are part of a single
4079 * user-visible operation. The operations between
4080 * gtk_text_buffer_begin_user_action() and
4081 * gtk_text_buffer_end_user_action() can then be grouped when creating
4082 * an undo stack. #GtkTextBuffer maintains a count of calls to
4083 * gtk_text_buffer_begin_user_action() that have not been closed with
4084 * a call to gtk_text_buffer_end_user_action(), and emits the
4085 * "begin-user-action" and "end-user-action" signals only for the
4086 * outermost pair of calls. This allows you to build user actions
4087 * from other user actions.
4089 * The "interactive" buffer mutation functions, such as
4090 * gtk_text_buffer_insert_interactive(), automatically call begin/end
4091 * user action around the buffer operations they perform, so there's
4092 * no need to add extra calls if you user action consists solely of a
4093 * single call to one of those functions.
4096 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
4098 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4100 buffer->priv->user_action_count += 1;
4102 if (buffer->priv->user_action_count == 1)
4104 /* Outermost nested user action begin emits the signal */
4105 g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
4110 * gtk_text_buffer_end_user_action:
4111 * @buffer: a #GtkTextBuffer
4113 * Should be paired with a call to gtk_text_buffer_begin_user_action().
4114 * See that function for a full explanation.
4117 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
4119 g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4120 g_return_if_fail (buffer->priv->user_action_count > 0);
4122 buffer->priv->user_action_count -= 1;
4124 if (buffer->priv->user_action_count == 0)
4126 /* Ended the outermost-nested user action end, so emit the signal */
4127 g_signal_emit (buffer, signals[END_USER_ACTION], 0);
4132 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
4134 GtkTextBufferPrivate *priv = buffer->priv;
4136 if (priv->copy_target_list)
4138 gtk_target_list_unref (priv->copy_target_list);
4139 priv->copy_target_list = NULL;
4141 gtk_target_table_free (priv->copy_target_entries,
4142 priv->n_copy_target_entries);
4143 priv->copy_target_entries = NULL;
4144 priv->n_copy_target_entries = 0;
4147 if (priv->paste_target_list)
4149 gtk_target_list_unref (priv->paste_target_list);
4150 priv->paste_target_list = NULL;
4152 gtk_target_table_free (priv->paste_target_entries,
4153 priv->n_paste_target_entries);
4154 priv->paste_target_entries = NULL;
4155 priv->n_paste_target_entries = 0;
4159 static GtkTargetList *
4160 gtk_text_buffer_get_target_list (GtkTextBuffer *buffer,
4161 gboolean deserializable,
4162 GtkTargetEntry **entries,
4165 GtkTargetList *target_list;
4167 target_list = gtk_target_list_new (NULL, 0);
4169 gtk_target_list_add (target_list,
4170 gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
4171 GTK_TARGET_SAME_APP,
4172 GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
4174 gtk_target_list_add_rich_text_targets (target_list,
4175 GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
4179 gtk_target_list_add_text_targets (target_list,
4180 GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
4182 *entries = gtk_target_table_new_from_list (target_list, n_entries);
4188 * gtk_text_buffer_get_copy_target_list:
4189 * @buffer: a #GtkTextBuffer
4191 * This function returns the list of targets this text buffer can
4192 * provide for copying and as DND source. The targets in the list are
4193 * added with %info values from the #GtkTextBufferTargetInfo enum,
4194 * using gtk_target_list_add_rich_text_targets() and
4195 * gtk_target_list_add_text_targets().
4197 * Return value: the #GtkTargetList
4202 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
4204 GtkTextBufferPrivate *priv;
4206 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4208 priv = buffer->priv;
4210 if (! priv->copy_target_list)
4211 priv->copy_target_list =
4212 gtk_text_buffer_get_target_list (buffer, FALSE,
4213 &priv->copy_target_entries,
4214 &priv->n_copy_target_entries);
4216 return priv->copy_target_list;
4220 * gtk_text_buffer_get_paste_target_list:
4221 * @buffer: a #GtkTextBuffer
4223 * This function returns the list of targets this text buffer supports
4224 * for pasting and as DND destination. The targets in the list are
4225 * added with %info values from the #GtkTextBufferTargetInfo enum,
4226 * using gtk_target_list_add_rich_text_targets() and
4227 * gtk_target_list_add_text_targets().
4229 * Return value: the #GtkTargetList
4234 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4236 GtkTextBufferPrivate *priv;
4238 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4240 priv = buffer->priv;
4242 if (! priv->paste_target_list)
4243 priv->paste_target_list =
4244 gtk_text_buffer_get_target_list (buffer, TRUE,
4245 &priv->paste_target_entries,
4246 &priv->n_paste_target_entries);
4248 return priv->paste_target_list;
4252 * Logical attribute cache
4255 #define ATTR_CACHE_SIZE 2
4257 typedef struct _CacheEntry CacheEntry;
4262 PangoLogAttr *attrs;
4265 struct _GtkTextLogAttrCache
4267 gint chars_changed_stamp;
4268 CacheEntry entries[ATTR_CACHE_SIZE];
4272 free_log_attr_cache (GtkTextLogAttrCache *cache)
4275 while (i < ATTR_CACHE_SIZE)
4277 g_free (cache->entries[i].attrs);
4284 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4287 while (i < ATTR_CACHE_SIZE)
4289 g_free (cache->entries[i].attrs);
4290 cache->entries[i].attrs = NULL;
4295 static PangoLogAttr*
4296 compute_log_attrs (const GtkTextIter *iter,
4302 gint char_len, byte_len;
4303 PangoLogAttr *attrs = NULL;
4308 gtk_text_iter_set_line_offset (&start, 0);
4309 gtk_text_iter_forward_line (&end);
4311 paragraph = gtk_text_iter_get_slice (&start, &end);
4312 char_len = g_utf8_strlen (paragraph, -1);
4313 byte_len = strlen (paragraph);
4315 g_assert (char_len > 0);
4318 *char_lenp = char_len;
4320 attrs = g_new (PangoLogAttr, char_len + 1);
4322 /* FIXME we need to follow PangoLayout and allow different language
4323 * tags within the paragraph
4325 pango_get_log_attrs (paragraph, byte_len, -1,
4326 gtk_text_iter_get_language (&start),
4335 /* The return value from this is valid until you call this a second time.
4338 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
4339 const GtkTextIter *anywhere_in_line,
4342 GtkTextBufferPrivate *priv;
4344 GtkTextLogAttrCache *cache;
4347 g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4348 g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4350 priv = buffer->priv;
4352 /* special-case for empty last line in buffer */
4353 if (gtk_text_iter_is_end (anywhere_in_line) &&
4354 gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4361 /* FIXME we also need to recompute log attrs if the language tag at
4362 * the start of a paragraph changes
4365 if (priv->log_attr_cache == NULL)
4367 priv->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4368 priv->log_attr_cache->chars_changed_stamp =
4369 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4371 else if (priv->log_attr_cache->chars_changed_stamp !=
4372 _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4374 clear_log_attr_cache (priv->log_attr_cache);
4377 cache = priv->log_attr_cache;
4378 line = gtk_text_iter_get_line (anywhere_in_line);
4381 while (i < ATTR_CACHE_SIZE)
4383 if (cache->entries[i].attrs &&
4384 cache->entries[i].line == line)
4387 *char_len = cache->entries[i].char_len;
4388 return cache->entries[i].attrs;
4393 /* Not in cache; open up the first cache entry */
4394 g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4396 g_memmove (cache->entries + 1, cache->entries,
4397 sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4399 cache->entries[0].line = line;
4400 cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4401 &cache->entries[0].char_len);
4404 *char_len = cache->entries[0].char_len;
4406 return cache->entries[0].attrs;
4410 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4413 /* This removes tag from the buffer, but DOESN'T emit the
4414 * remove-tag signal, because we can't afford to have user
4415 * code messing things up at this point; the tag MUST be removed
4418 if (buffer->priv->btree)
4419 _gtk_text_btree_notify_will_remove_tag (buffer->priv->btree, tag);
4427 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4429 _gtk_text_btree_spew (get_btree (buffer));