]> Pileus Git - ~andy/gtk/blob - gtk/gtktextbuffer.c
Change FSF Address
[~andy/gtk] / gtk / gtktextbuffer.c
1 /* GTK - The GIMP Toolkit
2  * gtktextbuffer.c Copyright (C) 2000 Red Hat, Inc.
3  *                 Copyright (C) 2004 Nokia Corporation
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21  * file for a list of people on the GTK+ Team.  See the ChangeLog
22  * files for a list of changes.  These files are distributed with
23  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24  */
25
26 #include "config.h"
27 #include <string.h>
28 #include <stdarg.h>
29
30 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
31 #include "gtkclipboard.h"
32 #include "gtkdnd.h"
33 #include "gtkinvisible.h"
34 #include "gtkmarshalers.h"
35 #include "gtktextbuffer.h"
36 #include "gtktextbufferrichtext.h"
37 #include "gtktextbtree.h"
38 #include "gtktextiterprivate.h"
39 #include "gtktexttagprivate.h"
40 #include "gtkprivate.h"
41 #include "gtkintl.h"
42
43
44 /**
45  * SECTION:gtktextbuffer
46  * @Short_description: Stores attributed text for display in a GtkTextView
47  * @Title: GtkTextBuffer
48  * @See_also: #GtkTextView, #GtkTextIter, #GtkTextMark
49  *
50  * You may wish to begin by reading the <link linkend="TextWidget">text widget
51  * conceptual overview</link> which gives an overview of all the objects and data
52  * types related to the text widget and how they work together.
53  */
54
55
56 typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
57
58 struct _GtkTextBufferPrivate
59 {
60   GtkTargetList  *copy_target_list;
61   GtkTargetEntry *copy_target_entries;
62   GtkTargetList  *paste_target_list;
63   GtkTargetEntry *paste_target_entries;
64
65   gint            n_copy_target_entries;
66   gint            n_paste_target_entries;
67
68   GtkTextTagTable *tag_table;
69   GtkTextBTree *btree;
70
71   GSList *clipboard_contents_buffers;
72   GSList *selection_clipboards;
73
74   GtkTextLogAttrCache *log_attr_cache;
75
76   guint user_action_count;
77
78   /* Whether the buffer has been modified since last save */
79   guint modified : 1;
80   guint has_selection : 1;
81 };
82
83
84 typedef struct _ClipboardRequest ClipboardRequest;
85
86 struct _ClipboardRequest
87 {
88   GtkTextBuffer *buffer;
89   guint interactive : 1;
90   guint default_editable : 1;
91   guint replace_selection : 1;
92 };
93
94 enum {
95   INSERT_TEXT,
96   INSERT_PIXBUF,
97   INSERT_CHILD_ANCHOR,
98   DELETE_RANGE,
99   CHANGED,
100   MODIFIED_CHANGED,
101   MARK_SET,
102   MARK_DELETED,
103   APPLY_TAG,
104   REMOVE_TAG,
105   BEGIN_USER_ACTION,
106   END_USER_ACTION,
107   PASTE_DONE,
108   LAST_SIGNAL
109 };
110
111 enum {
112   PROP_0,
113
114   /* Construct */
115   PROP_TAG_TABLE,
116
117   /* Normal */
118   PROP_TEXT,
119   PROP_HAS_SELECTION,
120   PROP_CURSOR_POSITION,
121   PROP_COPY_TARGET_LIST,
122   PROP_PASTE_TARGET_LIST
123 };
124
125 static void gtk_text_buffer_finalize   (GObject            *object);
126
127 static void gtk_text_buffer_real_insert_text           (GtkTextBuffer     *buffer,
128                                                         GtkTextIter       *iter,
129                                                         const gchar       *text,
130                                                         gint               len);
131 static void gtk_text_buffer_real_insert_pixbuf         (GtkTextBuffer     *buffer,
132                                                         GtkTextIter       *iter,
133                                                         GdkPixbuf         *pixbuf);
134 static void gtk_text_buffer_real_insert_anchor         (GtkTextBuffer     *buffer,
135                                                         GtkTextIter       *iter,
136                                                         GtkTextChildAnchor *anchor);
137 static void gtk_text_buffer_real_delete_range          (GtkTextBuffer     *buffer,
138                                                         GtkTextIter       *start,
139                                                         GtkTextIter       *end);
140 static void gtk_text_buffer_real_apply_tag             (GtkTextBuffer     *buffer,
141                                                         GtkTextTag        *tag,
142                                                         const GtkTextIter *start_char,
143                                                         const GtkTextIter *end_char);
144 static void gtk_text_buffer_real_remove_tag            (GtkTextBuffer     *buffer,
145                                                         GtkTextTag        *tag,
146                                                         const GtkTextIter *start_char,
147                                                         const GtkTextIter *end_char);
148 static void gtk_text_buffer_real_changed               (GtkTextBuffer     *buffer);
149 static void gtk_text_buffer_real_mark_set              (GtkTextBuffer     *buffer,
150                                                         const GtkTextIter *iter,
151                                                         GtkTextMark       *mark);
152
153 static GtkTextBTree* get_btree (GtkTextBuffer *buffer);
154 static void          free_log_attr_cache (GtkTextLogAttrCache *cache);
155
156 static void remove_all_selection_clipboards       (GtkTextBuffer *buffer);
157 static void update_selection_clipboards           (GtkTextBuffer *buffer);
158
159 static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
160
161 static void gtk_text_buffer_free_target_lists     (GtkTextBuffer *buffer);
162
163 static guint signals[LAST_SIGNAL] = { 0 };
164
165 static void gtk_text_buffer_set_property (GObject         *object,
166                                           guint            prop_id,
167                                           const GValue    *value,
168                                           GParamSpec      *pspec);
169 static void gtk_text_buffer_get_property (GObject         *object,
170                                           guint            prop_id,
171                                           GValue          *value,
172                                           GParamSpec      *pspec);
173 static void gtk_text_buffer_notify       (GObject         *object,
174                                           GParamSpec      *pspec);
175
176 G_DEFINE_TYPE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
177
178 static void
179 gtk_text_buffer_class_init (GtkTextBufferClass *klass)
180 {
181   GObjectClass *object_class = G_OBJECT_CLASS (klass);
182
183   object_class->finalize = gtk_text_buffer_finalize;
184   object_class->set_property = gtk_text_buffer_set_property;
185   object_class->get_property = gtk_text_buffer_get_property;
186   object_class->notify       = gtk_text_buffer_notify;
187  
188   klass->insert_text = gtk_text_buffer_real_insert_text;
189   klass->insert_pixbuf = gtk_text_buffer_real_insert_pixbuf;
190   klass->insert_child_anchor = gtk_text_buffer_real_insert_anchor;
191   klass->delete_range = gtk_text_buffer_real_delete_range;
192   klass->apply_tag = gtk_text_buffer_real_apply_tag;
193   klass->remove_tag = gtk_text_buffer_real_remove_tag;
194   klass->changed = gtk_text_buffer_real_changed;
195   klass->mark_set = gtk_text_buffer_real_mark_set;
196
197   /* Construct */
198   g_object_class_install_property (object_class,
199                                    PROP_TAG_TABLE,
200                                    g_param_spec_object ("tag-table",
201                                                         P_("Tag Table"),
202                                                         P_("Text Tag Table"),
203                                                         GTK_TYPE_TEXT_TAG_TABLE,
204                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
205
206   /* Normal properties*/
207   
208   /**
209    * GtkTextBuffer:text:
210    *
211    * The text content of the buffer. Without child widgets and images,
212    * see gtk_text_buffer_get_text() for more information.
213    *
214    * Since: 2.8
215    */
216   g_object_class_install_property (object_class,
217                                    PROP_TEXT,
218                                    g_param_spec_string ("text",
219                                                         P_("Text"),
220                                                         P_("Current text of the buffer"),
221                                                         "",
222                                                         GTK_PARAM_READWRITE));
223
224   /**
225    * GtkTextBuffer:has-selection:
226    *
227    * Whether the buffer has some text currently selected.
228    *
229    * Since: 2.10
230    */
231   g_object_class_install_property (object_class,
232                                    PROP_HAS_SELECTION,
233                                    g_param_spec_boolean ("has-selection",
234                                                          P_("Has selection"),
235                                                          P_("Whether the buffer has some text currently selected"),
236                                                          FALSE,
237                                                          GTK_PARAM_READABLE));
238
239   /**
240    * GtkTextBuffer:cursor-position:
241    *
242    * The position of the insert mark (as offset from the beginning 
243    * of the buffer). It is useful for getting notified when the 
244    * cursor moves.
245    *
246    * Since: 2.10
247    */
248   g_object_class_install_property (object_class,
249                                    PROP_CURSOR_POSITION,
250                                    g_param_spec_int ("cursor-position",
251                                                      P_("Cursor position"),
252                                                      P_("The position of the insert mark (as offset from the beginning of the buffer)"),
253                                                      0, G_MAXINT, 0,
254                                                      GTK_PARAM_READABLE));
255
256   /**
257    * GtkTextBuffer:copy-target-list:
258    *
259    * The list of targets this buffer supports for clipboard copying
260    * and as DND source.
261    *
262    * Since: 2.10
263    */
264   g_object_class_install_property (object_class,
265                                    PROP_COPY_TARGET_LIST,
266                                    g_param_spec_boxed ("copy-target-list",
267                                                        P_("Copy target list"),
268                                                        P_("The list of targets this buffer supports for clipboard copying and DND source"),
269                                                        GTK_TYPE_TARGET_LIST,
270                                                        GTK_PARAM_READABLE));
271
272   /**
273    * GtkTextBuffer:paste-target-list:
274    *
275    * The list of targets this buffer supports for clipboard pasting
276    * and as DND destination.
277    *
278    * Since: 2.10
279    */
280   g_object_class_install_property (object_class,
281                                    PROP_PASTE_TARGET_LIST,
282                                    g_param_spec_boxed ("paste-target-list",
283                                                        P_("Paste target list"),
284                                                        P_("The list of targets this buffer supports for clipboard pasting and DND destination"),
285                                                        GTK_TYPE_TARGET_LIST,
286                                                        GTK_PARAM_READABLE));
287
288   /**
289    * GtkTextBuffer::insert-text:
290    * @textbuffer: the object which received the signal
291    * @location: position to insert @text in @textbuffer
292    * @text: the UTF-8 text to be inserted
293    * @len: length of the inserted text in bytes
294    * 
295    * The ::insert-text signal is emitted to insert text in a #GtkTextBuffer.
296    * Insertion actually occurs in the default handler.  
297    * 
298    * Note that if your handler runs before the default handler it must not 
299    * invalidate the @location iter (or has to revalidate it). 
300    * The default signal handler revalidates it to point to the end of the 
301    * inserted text.
302    * 
303    * See also: 
304    * gtk_text_buffer_insert(), 
305    * gtk_text_buffer_insert_range().
306    */
307   signals[INSERT_TEXT] =
308     g_signal_new (I_("insert-text"),
309                   G_OBJECT_CLASS_TYPE (object_class),
310                   G_SIGNAL_RUN_LAST,
311                   G_STRUCT_OFFSET (GtkTextBufferClass, insert_text),
312                   NULL, NULL,
313                   _gtk_marshal_VOID__BOXED_STRING_INT,
314                   G_TYPE_NONE,
315                   3,
316                   GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
317                   G_TYPE_STRING | G_SIGNAL_TYPE_STATIC_SCOPE,
318                   G_TYPE_INT);
319
320   /**
321    * GtkTextBuffer::insert-pixbuf:
322    * @textbuffer: the object which received the signal
323    * @location: position to insert @pixbuf in @textbuffer
324    * @pixbuf: the #GdkPixbuf to be inserted
325    * 
326    * The ::insert-pixbuf signal is emitted to insert a #GdkPixbuf 
327    * in a #GtkTextBuffer. Insertion actually occurs in the default handler.
328    * 
329    * Note that if your handler runs before the default handler it must not 
330    * invalidate the @location iter (or has to revalidate it). 
331    * The default signal handler revalidates it to be placed after the 
332    * inserted @pixbuf.
333    * 
334    * See also: gtk_text_buffer_insert_pixbuf().
335    */
336   signals[INSERT_PIXBUF] =
337     g_signal_new (I_("insert-pixbuf"),
338                   G_OBJECT_CLASS_TYPE (object_class),
339                   G_SIGNAL_RUN_LAST,
340                   G_STRUCT_OFFSET (GtkTextBufferClass, insert_pixbuf),
341                   NULL, NULL,
342                   _gtk_marshal_VOID__BOXED_OBJECT,
343                   G_TYPE_NONE,
344                   2,
345                   GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
346                   GDK_TYPE_PIXBUF);
347
348
349   /**
350    * GtkTextBuffer::insert-child-anchor:
351    * @textbuffer: the object which received the signal
352    * @location: position to insert @anchor in @textbuffer
353    * @anchor: the #GtkTextChildAnchor to be inserted
354    * 
355    * The ::insert-child-anchor signal is emitted to insert a
356    * #GtkTextChildAnchor in a #GtkTextBuffer.
357    * Insertion actually occurs in the default handler.
358    * 
359    * Note that if your handler runs before the default handler it must
360    * not invalidate the @location iter (or has to revalidate it). 
361    * The default signal handler revalidates it to be placed after the 
362    * inserted @anchor.
363    * 
364    * See also: gtk_text_buffer_insert_child_anchor().
365    */
366   signals[INSERT_CHILD_ANCHOR] =
367     g_signal_new (I_("insert-child-anchor"),
368                   G_OBJECT_CLASS_TYPE (object_class),
369                   G_SIGNAL_RUN_LAST,
370                   G_STRUCT_OFFSET (GtkTextBufferClass, insert_child_anchor),
371                   NULL, NULL,
372                   _gtk_marshal_VOID__BOXED_OBJECT,
373                   G_TYPE_NONE,
374                   2,
375                   GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
376                   GTK_TYPE_TEXT_CHILD_ANCHOR);
377   
378   /**
379    * GtkTextBuffer::delete-range:
380    * @textbuffer: the object which received the signal
381    * @start: the start of the range to be deleted
382    * @end: the end of the range to be deleted
383    * 
384    * The ::delete-range signal is emitted to delete a range 
385    * from a #GtkTextBuffer. 
386    * 
387    * Note that if your handler runs before the default handler it must not 
388    * invalidate the @start and @end iters (or has to revalidate them). 
389    * The default signal handler revalidates the @start and @end iters to 
390    * both point point to the location where text was deleted. Handlers
391    * which run after the default handler (see g_signal_connect_after())
392    * do not have access to the deleted text.
393    * 
394    * See also: gtk_text_buffer_delete().
395    */
396   signals[DELETE_RANGE] =
397     g_signal_new (I_("delete-range"),
398                   G_OBJECT_CLASS_TYPE (object_class),
399                   G_SIGNAL_RUN_LAST,
400                   G_STRUCT_OFFSET (GtkTextBufferClass, delete_range),
401                   NULL, NULL,
402                   _gtk_marshal_VOID__BOXED_BOXED,
403                   G_TYPE_NONE,
404                   2,
405                   GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE,
406                   GTK_TYPE_TEXT_ITER | G_SIGNAL_TYPE_STATIC_SCOPE);
407
408   /**
409    * GtkTextBuffer::changed:
410    * @textbuffer: the object which received the signal
411    * 
412    * The ::changed signal is emitted when the content of a #GtkTextBuffer 
413    * has changed.
414    */
415   signals[CHANGED] =
416     g_signal_new (I_("changed"),
417                   G_OBJECT_CLASS_TYPE (object_class),
418                   G_SIGNAL_RUN_LAST,                   
419                   G_STRUCT_OFFSET (GtkTextBufferClass, changed),
420                   NULL, NULL,
421                   _gtk_marshal_VOID__VOID,
422                   G_TYPE_NONE,
423                   0);
424
425   /**
426    * GtkTextBuffer::modified-changed:
427    * @textbuffer: the object which received the signal
428    * 
429    * The ::modified-changed signal is emitted when the modified bit of a 
430    * #GtkTextBuffer flips.
431    * 
432    * See also:
433    * gtk_text_buffer_set_modified().
434    */
435   signals[MODIFIED_CHANGED] =
436     g_signal_new (I_("modified-changed"),
437                   G_OBJECT_CLASS_TYPE (object_class),
438                   G_SIGNAL_RUN_LAST,
439                   G_STRUCT_OFFSET (GtkTextBufferClass, modified_changed),
440                   NULL, NULL,
441                   _gtk_marshal_VOID__VOID,
442                   G_TYPE_NONE,
443                   0);
444
445   /**
446    * GtkTextBuffer::mark-set:
447    * @textbuffer: the object which received the signal
448    * @location: The location of @mark in @textbuffer
449    * @mark: The mark that is set
450    * 
451    * The ::mark-set signal is emitted as notification
452    * after a #GtkTextMark is set.
453    * 
454    * See also: 
455    * gtk_text_buffer_create_mark(),
456    * gtk_text_buffer_move_mark().
457    */
458   signals[MARK_SET] =
459     g_signal_new (I_("mark-set"),
460                   G_OBJECT_CLASS_TYPE (object_class),
461                   G_SIGNAL_RUN_LAST,                   
462                   G_STRUCT_OFFSET (GtkTextBufferClass, mark_set),
463                   NULL, NULL,
464                   _gtk_marshal_VOID__BOXED_OBJECT,
465                   G_TYPE_NONE,
466                   2,
467                   GTK_TYPE_TEXT_ITER,
468                   GTK_TYPE_TEXT_MARK);
469
470   /**
471    * GtkTextBuffer::mark-deleted:
472    * @textbuffer: the object which received the signal
473    * @mark: The mark that was deleted
474    * 
475    * The ::mark-deleted signal is emitted as notification
476    * after a #GtkTextMark is deleted. 
477    * 
478    * See also:
479    * gtk_text_buffer_delete_mark().
480    */
481   signals[MARK_DELETED] =
482     g_signal_new (I_("mark-deleted"),
483                   G_OBJECT_CLASS_TYPE (object_class),
484                   G_SIGNAL_RUN_LAST,                   
485                   G_STRUCT_OFFSET (GtkTextBufferClass, mark_deleted),
486                   NULL, NULL,
487                   _gtk_marshal_VOID__OBJECT,
488                   G_TYPE_NONE,
489                   1,
490                   GTK_TYPE_TEXT_MARK);
491
492    /**
493    * GtkTextBuffer::apply-tag:
494    * @textbuffer: the object which received the signal
495    * @tag: the applied tag
496    * @start: the start of the range the tag is applied to
497    * @end: the end of the range the tag is applied to
498    * 
499    * The ::apply-tag signal is emitted to apply a tag to a
500    * range of text in a #GtkTextBuffer. 
501    * Applying actually occurs in the default handler.
502    * 
503    * Note that if your handler runs before the default handler it must not 
504    * invalidate the @start and @end iters (or has to revalidate them). 
505    * 
506    * See also: 
507    * gtk_text_buffer_apply_tag(),
508    * gtk_text_buffer_insert_with_tags(),
509    * gtk_text_buffer_insert_range().
510    */ 
511   signals[APPLY_TAG] =
512     g_signal_new (I_("apply-tag"),
513                   G_OBJECT_CLASS_TYPE (object_class),
514                   G_SIGNAL_RUN_LAST,
515                   G_STRUCT_OFFSET (GtkTextBufferClass, apply_tag),
516                   NULL, NULL,
517                   _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
518                   G_TYPE_NONE,
519                   3,
520                   GTK_TYPE_TEXT_TAG,
521                   GTK_TYPE_TEXT_ITER,
522                   GTK_TYPE_TEXT_ITER);
523
524
525    /**
526    * GtkTextBuffer::remove-tag:
527    * @textbuffer: the object which received the signal
528    * @tag: the tag to be removed
529    * @start: the start of the range the tag is removed from
530    * @end: the end of the range the tag is removed from
531    * 
532    * The ::remove-tag signal is emitted to remove all occurrences of @tag from
533    * a range of text in a #GtkTextBuffer. 
534    * Removal actually occurs in the default handler.
535    * 
536    * Note that if your handler runs before the default handler it must not 
537    * invalidate the @start and @end iters (or has to revalidate them). 
538    * 
539    * See also: 
540    * gtk_text_buffer_remove_tag(). 
541    */ 
542   signals[REMOVE_TAG] =
543     g_signal_new (I_("remove-tag"),
544                   G_OBJECT_CLASS_TYPE (object_class),
545                   G_SIGNAL_RUN_LAST,
546                   G_STRUCT_OFFSET (GtkTextBufferClass, remove_tag),
547                   NULL, NULL,
548                   _gtk_marshal_VOID__OBJECT_BOXED_BOXED,
549                   G_TYPE_NONE,
550                   3,
551                   GTK_TYPE_TEXT_TAG,
552                   GTK_TYPE_TEXT_ITER,
553                   GTK_TYPE_TEXT_ITER);
554
555    /**
556    * GtkTextBuffer::begin-user-action:
557    * @textbuffer: the object which received the signal
558    * 
559    * The ::begin-user-action signal is emitted at the beginning of a single
560    * user-visible operation on a #GtkTextBuffer.
561    * 
562    * See also: 
563    * gtk_text_buffer_begin_user_action(),
564    * gtk_text_buffer_insert_interactive(),
565    * gtk_text_buffer_insert_range_interactive(),
566    * gtk_text_buffer_delete_interactive(),
567    * gtk_text_buffer_backspace(),
568    * gtk_text_buffer_delete_selection().
569    */ 
570   signals[BEGIN_USER_ACTION] =
571     g_signal_new (I_("begin-user-action"),
572                   G_OBJECT_CLASS_TYPE (object_class),
573                   G_SIGNAL_RUN_LAST,                   
574                   G_STRUCT_OFFSET (GtkTextBufferClass, begin_user_action),
575                   NULL, NULL,
576                   _gtk_marshal_VOID__VOID,
577                   G_TYPE_NONE,
578                   0);
579
580    /**
581    * GtkTextBuffer::end-user-action:
582    * @textbuffer: the object which received the signal
583    * 
584    * The ::end-user-action signal is emitted at the end of a single
585    * user-visible operation on the #GtkTextBuffer.
586    * 
587    * See also: 
588    * gtk_text_buffer_end_user_action(),
589    * gtk_text_buffer_insert_interactive(),
590    * gtk_text_buffer_insert_range_interactive(),
591    * gtk_text_buffer_delete_interactive(),
592    * gtk_text_buffer_backspace(),
593    * gtk_text_buffer_delete_selection(),
594    * gtk_text_buffer_backspace().
595    */ 
596   signals[END_USER_ACTION] =
597     g_signal_new (I_("end-user-action"),
598                   G_OBJECT_CLASS_TYPE (object_class),
599                   G_SIGNAL_RUN_LAST,                   
600                   G_STRUCT_OFFSET (GtkTextBufferClass, end_user_action),
601                   NULL, NULL,
602                   _gtk_marshal_VOID__VOID,
603                   G_TYPE_NONE,
604                   0);
605
606    /**
607    * GtkTextBuffer::paste-done:
608    * @textbuffer: the object which received the signal
609    * 
610    * The paste-done signal is emitted after paste operation has been completed.
611    * This is useful to properly scroll the view to the end of the pasted text.
612    * See gtk_text_buffer_paste_clipboard() for more details.
613    * 
614    * Since: 2.16
615    */ 
616   signals[PASTE_DONE] =
617     g_signal_new (I_("paste-done"),
618                   G_OBJECT_CLASS_TYPE (object_class),
619                   G_SIGNAL_RUN_LAST,
620                   G_STRUCT_OFFSET (GtkTextBufferClass, paste_done),
621                   NULL, NULL,
622                   _gtk_marshal_VOID__OBJECT,
623                   G_TYPE_NONE,
624                   1,
625                   GTK_TYPE_CLIPBOARD);
626
627   g_type_class_add_private (object_class, sizeof (GtkTextBufferPrivate));
628 }
629
630 static void
631 gtk_text_buffer_init (GtkTextBuffer *buffer)
632 {
633   buffer->priv = G_TYPE_INSTANCE_GET_PRIVATE (buffer,
634                                               GTK_TYPE_TEXT_BUFFER,
635                                               GtkTextBufferPrivate);
636
637   buffer->priv->clipboard_contents_buffers = NULL;
638   buffer->priv->tag_table = NULL;
639
640   /* allow copying of arbiatray stuff in the internal rich text format */
641   gtk_text_buffer_register_serialize_tagset (buffer, NULL);
642 }
643
644 static void
645 set_table (GtkTextBuffer *buffer, GtkTextTagTable *table)
646 {
647   GtkTextBufferPrivate *priv = buffer->priv;
648
649   g_return_if_fail (priv->tag_table == NULL);
650
651   if (table)
652     {
653       priv->tag_table = table;
654       g_object_ref (priv->tag_table);
655       _gtk_text_tag_table_add_buffer (table, buffer);
656     }
657 }
658
659 static GtkTextTagTable*
660 get_table (GtkTextBuffer *buffer)
661 {
662   GtkTextBufferPrivate *priv = buffer->priv;
663
664   if (priv->tag_table == NULL)
665     {
666       priv->tag_table = gtk_text_tag_table_new ();
667       _gtk_text_tag_table_add_buffer (priv->tag_table, buffer);
668     }
669
670   return priv->tag_table;
671 }
672
673 static void
674 gtk_text_buffer_set_property (GObject         *object,
675                               guint            prop_id,
676                               const GValue    *value,
677                               GParamSpec      *pspec)
678 {
679   GtkTextBuffer *text_buffer;
680
681   text_buffer = GTK_TEXT_BUFFER (object);
682
683   switch (prop_id)
684     {
685     case PROP_TAG_TABLE:
686       set_table (text_buffer, g_value_get_object (value));
687       break;
688
689     case PROP_TEXT:
690       gtk_text_buffer_set_text (text_buffer,
691                                 g_value_get_string (value), -1);
692       break;
693
694     default:
695       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
696       break;
697     }
698 }
699
700 static void
701 gtk_text_buffer_get_property (GObject         *object,
702                               guint            prop_id,
703                               GValue          *value,
704                               GParamSpec      *pspec)
705 {
706   GtkTextBuffer *text_buffer;
707   GtkTextIter iter;
708
709   text_buffer = GTK_TEXT_BUFFER (object);
710
711   switch (prop_id)
712     {
713     case PROP_TAG_TABLE:
714       g_value_set_object (value, get_table (text_buffer));
715       break;
716
717     case PROP_TEXT:
718       {
719         GtkTextIter start, end;
720
721         gtk_text_buffer_get_start_iter (text_buffer, &start);
722         gtk_text_buffer_get_end_iter (text_buffer, &end);
723
724         g_value_take_string (value,
725                             gtk_text_buffer_get_text (text_buffer,
726                                                       &start, &end, FALSE));
727         break;
728       }
729
730     case PROP_HAS_SELECTION:
731       g_value_set_boolean (value, text_buffer->priv->has_selection);
732       break;
733
734     case PROP_CURSOR_POSITION:
735       gtk_text_buffer_get_iter_at_mark (text_buffer, &iter, 
736                                         gtk_text_buffer_get_insert (text_buffer));
737       g_value_set_int (value, gtk_text_iter_get_offset (&iter));
738       break;
739
740     case PROP_COPY_TARGET_LIST:
741       g_value_set_boxed (value, gtk_text_buffer_get_copy_target_list (text_buffer));
742       break;
743
744     case PROP_PASTE_TARGET_LIST:
745       g_value_set_boxed (value, gtk_text_buffer_get_paste_target_list (text_buffer));
746       break;
747
748     default:
749       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
750       break;
751     }
752 }
753
754 static void
755 gtk_text_buffer_notify (GObject    *object,
756                         GParamSpec *pspec)
757 {
758   if (!strcmp (pspec->name, "copy-target-list") ||
759       !strcmp (pspec->name, "paste-target-list"))
760     {
761       gtk_text_buffer_free_target_lists (GTK_TEXT_BUFFER (object));
762     }
763 }
764
765 /**
766  * gtk_text_buffer_new:
767  * @table: (allow-none): a tag table, or %NULL to create a new one
768  *
769  * Creates a new text buffer.
770  *
771  * Return value: a new text buffer
772  **/
773 GtkTextBuffer*
774 gtk_text_buffer_new (GtkTextTagTable *table)
775 {
776   GtkTextBuffer *text_buffer;
777
778   text_buffer = g_object_new (GTK_TYPE_TEXT_BUFFER, "tag-table", table, NULL);
779
780   return text_buffer;
781 }
782
783 static void
784 gtk_text_buffer_finalize (GObject *object)
785 {
786   GtkTextBuffer *buffer;
787   GtkTextBufferPrivate *priv;
788
789   buffer = GTK_TEXT_BUFFER (object);
790   priv = buffer->priv;
791
792   remove_all_selection_clipboards (buffer);
793
794   if (priv->tag_table)
795     {
796       _gtk_text_tag_table_remove_buffer (priv->tag_table, buffer);
797       g_object_unref (priv->tag_table);
798       priv->tag_table = NULL;
799     }
800
801   if (priv->btree)
802     {
803       _gtk_text_btree_unref (priv->btree);
804       priv->btree = NULL;
805     }
806
807   if (priv->log_attr_cache)
808     free_log_attr_cache (priv->log_attr_cache);
809
810   priv->log_attr_cache = NULL;
811
812   gtk_text_buffer_free_target_lists (buffer);
813
814   G_OBJECT_CLASS (gtk_text_buffer_parent_class)->finalize (object);
815 }
816
817 static GtkTextBTree*
818 get_btree (GtkTextBuffer *buffer)
819 {
820   GtkTextBufferPrivate *priv = buffer->priv;
821
822   if (priv->btree == NULL)
823     priv->btree = _gtk_text_btree_new (gtk_text_buffer_get_tag_table (buffer),
824                                        buffer);
825
826   return priv->btree;
827 }
828
829 GtkTextBTree*
830 _gtk_text_buffer_get_btree (GtkTextBuffer *buffer)
831 {
832   return get_btree (buffer);
833 }
834
835 /**
836  * gtk_text_buffer_get_tag_table:
837  * @buffer: a #GtkTextBuffer
838  *
839  * Get the #GtkTextTagTable associated with this buffer.
840  *
841  * Return value: (transfer none): the buffer's tag table
842  **/
843 GtkTextTagTable*
844 gtk_text_buffer_get_tag_table (GtkTextBuffer *buffer)
845 {
846   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
847
848   return get_table (buffer);
849 }
850
851 /**
852  * gtk_text_buffer_set_text:
853  * @buffer: a #GtkTextBuffer
854  * @text: UTF-8 text to insert
855  * @len: length of @text in bytes
856  *
857  * Deletes current contents of @buffer, and inserts @text instead. If
858  * @len is -1, @text must be nul-terminated. @text must be valid UTF-8.
859  **/
860 void
861 gtk_text_buffer_set_text (GtkTextBuffer *buffer,
862                           const gchar   *text,
863                           gint           len)
864 {
865   GtkTextIter start, end;
866
867   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
868   g_return_if_fail (text != NULL);
869
870   if (len < 0)
871     len = strlen (text);
872
873   gtk_text_buffer_get_bounds (buffer, &start, &end);
874
875   gtk_text_buffer_delete (buffer, &start, &end);
876
877   if (len > 0)
878     {
879       gtk_text_buffer_get_iter_at_offset (buffer, &start, 0);
880       gtk_text_buffer_insert (buffer, &start, text, len);
881     }
882   
883   g_object_notify (G_OBJECT (buffer), "text");
884 }
885
886  
887
888 /*
889  * Insertion
890  */
891
892 static void
893 gtk_text_buffer_real_insert_text (GtkTextBuffer *buffer,
894                                   GtkTextIter   *iter,
895                                   const gchar   *text,
896                                   gint           len)
897 {
898   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
899   g_return_if_fail (iter != NULL);
900   
901   _gtk_text_btree_insert (iter, text, len);
902
903   g_signal_emit (buffer, signals[CHANGED], 0);
904   g_object_notify (G_OBJECT (buffer), "cursor-position");
905 }
906
907 static void
908 gtk_text_buffer_emit_insert (GtkTextBuffer *buffer,
909                              GtkTextIter   *iter,
910                              const gchar   *text,
911                              gint           len)
912 {
913   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
914   g_return_if_fail (iter != NULL);
915   g_return_if_fail (text != NULL);
916
917   if (len < 0)
918     len = strlen (text);
919
920   g_return_if_fail (g_utf8_validate (text, len, NULL));
921   
922   if (len > 0)
923     {
924       g_signal_emit (buffer, signals[INSERT_TEXT], 0,
925                      iter, text, len);
926     }
927 }
928
929 /**
930  * gtk_text_buffer_insert:
931  * @buffer: a #GtkTextBuffer
932  * @iter: a position in the buffer
933  * @text: text in UTF-8 format
934  * @len: length of text in bytes, or -1
935  *
936  * Inserts @len bytes of @text at position @iter.  If @len is -1,
937  * @text must be nul-terminated and will be inserted in its
938  * entirety. Emits the "insert-text" signal; insertion actually occurs
939  * in the default handler for the signal. @iter is invalidated when
940  * insertion occurs (because the buffer contents change), but the
941  * default signal handler revalidates it to point to the end of the
942  * inserted text.
943  **/
944 void
945 gtk_text_buffer_insert (GtkTextBuffer *buffer,
946                         GtkTextIter   *iter,
947                         const gchar   *text,
948                         gint           len)
949 {
950   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
951   g_return_if_fail (iter != NULL);
952   g_return_if_fail (text != NULL);
953   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
954   
955   gtk_text_buffer_emit_insert (buffer, iter, text, len);
956 }
957
958 /**
959  * gtk_text_buffer_insert_at_cursor:
960  * @buffer: a #GtkTextBuffer
961  * @text: text in UTF-8 format
962  * @len: length of text, in bytes
963  *
964  * Simply calls gtk_text_buffer_insert(), using the current
965  * cursor position as the insertion point.
966  **/
967 void
968 gtk_text_buffer_insert_at_cursor (GtkTextBuffer *buffer,
969                                   const gchar   *text,
970                                   gint           len)
971 {
972   GtkTextIter iter;
973
974   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
975   g_return_if_fail (text != NULL);
976
977   gtk_text_buffer_get_iter_at_mark (buffer, &iter,
978                                     gtk_text_buffer_get_insert (buffer));
979
980   gtk_text_buffer_insert (buffer, &iter, text, len);
981 }
982
983 /**
984  * gtk_text_buffer_insert_interactive:
985  * @buffer: a #GtkTextBuffer
986  * @iter: a position in @buffer
987  * @text: some UTF-8 text
988  * @len: length of text in bytes, or -1
989  * @default_editable: default editability of buffer
990  *
991  * Like gtk_text_buffer_insert(), but the insertion will not occur if
992  * @iter is at a non-editable location in the buffer. Usually you
993  * want to prevent insertions at ineditable locations if the insertion
994  * results from a user action (is interactive).
995  *
996  * @default_editable indicates the editability of text that doesn't
997  * have a tag affecting editability applied to it. Typically the
998  * result of gtk_text_view_get_editable() is appropriate here.
999  *
1000  * Return value: whether text was actually inserted
1001  **/
1002 gboolean
1003 gtk_text_buffer_insert_interactive (GtkTextBuffer *buffer,
1004                                     GtkTextIter   *iter,
1005                                     const gchar   *text,
1006                                     gint           len,
1007                                     gboolean       default_editable)
1008 {
1009   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1010   g_return_val_if_fail (text != NULL, FALSE);
1011   g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, FALSE);
1012
1013   if (gtk_text_iter_can_insert (iter, default_editable))
1014     {
1015       gtk_text_buffer_begin_user_action (buffer);
1016       gtk_text_buffer_emit_insert (buffer, iter, text, len);
1017       gtk_text_buffer_end_user_action (buffer);
1018       return TRUE;
1019     }
1020   else
1021     return FALSE;
1022 }
1023
1024 /**
1025  * gtk_text_buffer_insert_interactive_at_cursor:
1026  * @buffer: a #GtkTextBuffer
1027  * @text: text in UTF-8 format
1028  * @len: length of text in bytes, or -1
1029  * @default_editable: default editability of buffer
1030  *
1031  * Calls gtk_text_buffer_insert_interactive() at the cursor
1032  * position.
1033  *
1034  * @default_editable indicates the editability of text that doesn't
1035  * have a tag affecting editability applied to it. Typically the
1036  * result of gtk_text_view_get_editable() is appropriate here.
1037  * 
1038  * Return value: whether text was actually inserted
1039  **/
1040 gboolean
1041 gtk_text_buffer_insert_interactive_at_cursor (GtkTextBuffer *buffer,
1042                                               const gchar   *text,
1043                                               gint           len,
1044                                               gboolean       default_editable)
1045 {
1046   GtkTextIter iter;
1047
1048   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1049   g_return_val_if_fail (text != NULL, FALSE);
1050
1051   gtk_text_buffer_get_iter_at_mark (buffer, &iter,
1052                                     gtk_text_buffer_get_insert (buffer));
1053
1054   return gtk_text_buffer_insert_interactive (buffer, &iter, text, len,
1055                                              default_editable);
1056 }
1057
1058 static gboolean
1059 possibly_not_text (gunichar ch,
1060                    gpointer user_data)
1061 {
1062   return ch == GTK_TEXT_UNKNOWN_CHAR;
1063 }
1064
1065 static void
1066 insert_text_range (GtkTextBuffer     *buffer,
1067                    GtkTextIter       *iter,
1068                    const GtkTextIter *orig_start,
1069                    const GtkTextIter *orig_end,
1070                    gboolean           interactive)
1071 {
1072   gchar *text;
1073
1074   text = gtk_text_iter_get_text (orig_start, orig_end);
1075
1076   gtk_text_buffer_emit_insert (buffer, iter, text, -1);
1077
1078   g_free (text);
1079 }
1080
1081 typedef struct _Range Range;
1082 struct _Range
1083 {
1084   GtkTextBuffer *buffer;
1085   GtkTextMark *start_mark;
1086   GtkTextMark *end_mark;
1087   GtkTextMark *whole_end_mark;
1088   GtkTextIter *range_start;
1089   GtkTextIter *range_end;
1090   GtkTextIter *whole_end;
1091 };
1092
1093 static Range*
1094 save_range (GtkTextIter *range_start,
1095             GtkTextIter *range_end,
1096             GtkTextIter *whole_end)
1097 {
1098   Range *r;
1099
1100   r = g_new (Range, 1);
1101
1102   r->buffer = gtk_text_iter_get_buffer (range_start);
1103   g_object_ref (r->buffer);
1104   
1105   r->start_mark = 
1106     gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1107                                  NULL,
1108                                  range_start,
1109                                  FALSE);
1110   r->end_mark = 
1111     gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1112                                  NULL,
1113                                  range_end,
1114                                  TRUE);
1115
1116   r->whole_end_mark = 
1117     gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (range_start),
1118                                  NULL,
1119                                  whole_end,
1120                                  TRUE);
1121
1122   r->range_start = range_start;
1123   r->range_end = range_end;
1124   r->whole_end = whole_end;
1125
1126   return r;
1127 }
1128
1129 static void
1130 restore_range (Range *r)
1131 {
1132   gtk_text_buffer_get_iter_at_mark (r->buffer,
1133                                     r->range_start,
1134                                     r->start_mark);
1135       
1136   gtk_text_buffer_get_iter_at_mark (r->buffer,
1137                                     r->range_end,
1138                                     r->end_mark);
1139       
1140   gtk_text_buffer_get_iter_at_mark (r->buffer,
1141                                     r->whole_end,
1142                                     r->whole_end_mark);  
1143   
1144   gtk_text_buffer_delete_mark (r->buffer, r->start_mark);
1145   gtk_text_buffer_delete_mark (r->buffer, r->end_mark);
1146   gtk_text_buffer_delete_mark (r->buffer, r->whole_end_mark);
1147
1148   /* Due to the gravities on the marks, the ordering could have
1149    * gotten mangled; we switch to an empty range in that
1150    * case
1151    */
1152   
1153   if (gtk_text_iter_compare (r->range_start, r->range_end) > 0)
1154     *r->range_start = *r->range_end;
1155
1156   if (gtk_text_iter_compare (r->range_end, r->whole_end) > 0)
1157     *r->range_end = *r->whole_end;
1158   
1159   g_object_unref (r->buffer);
1160   g_free (r); 
1161 }
1162
1163 static void
1164 insert_range_untagged (GtkTextBuffer     *buffer,
1165                        GtkTextIter       *iter,
1166                        const GtkTextIter *orig_start,
1167                        const GtkTextIter *orig_end,
1168                        gboolean           interactive)
1169 {
1170   GtkTextIter range_start;
1171   GtkTextIter range_end;
1172   GtkTextIter start, end;
1173   Range *r;
1174   
1175   if (gtk_text_iter_equal (orig_start, orig_end))
1176     return;
1177
1178   start = *orig_start;
1179   end = *orig_end;
1180   
1181   range_start = start;
1182   range_end = start;
1183   
1184   while (TRUE)
1185     {
1186       if (gtk_text_iter_equal (&range_start, &range_end))
1187         {
1188           /* Figure out how to move forward */
1189
1190           g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1191           
1192           if (gtk_text_iter_equal (&range_end, &end))
1193             {
1194               /* nothing left to do */
1195               break;
1196             }
1197           else if (gtk_text_iter_get_char (&range_end) == GTK_TEXT_UNKNOWN_CHAR)
1198             {
1199               GdkPixbuf *pixbuf = NULL;
1200               GtkTextChildAnchor *anchor = NULL;
1201               pixbuf = gtk_text_iter_get_pixbuf (&range_end);
1202               anchor = gtk_text_iter_get_child_anchor (&range_end);
1203
1204               if (pixbuf)
1205                 {
1206                   r = save_range (&range_start,
1207                                   &range_end,
1208                                   &end);
1209
1210                   gtk_text_buffer_insert_pixbuf (buffer,
1211                                                  iter,
1212                                                  pixbuf);
1213
1214                   restore_range (r);
1215                   r = NULL;
1216                   
1217                   gtk_text_iter_forward_char (&range_end);
1218                   
1219                   range_start = range_end;
1220                 }
1221               else if (anchor)
1222                 {
1223                   /* Just skip anchors */
1224
1225                   gtk_text_iter_forward_char (&range_end);
1226                   range_start = range_end;
1227                 }
1228               else
1229                 {
1230                   /* The GTK_TEXT_UNKNOWN_CHAR was in a text segment, so
1231                    * keep going. 
1232                    */
1233                   gtk_text_iter_forward_find_char (&range_end,
1234                                                    possibly_not_text, NULL,
1235                                                    &end);
1236                   
1237                   g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1238                 }
1239             }
1240           else
1241             {
1242               /* Text segment starts here, so forward search to
1243                * find its possible endpoint
1244                */
1245               gtk_text_iter_forward_find_char (&range_end,
1246                                                possibly_not_text, NULL,
1247                                                &end);
1248               
1249               g_assert (gtk_text_iter_compare (&range_end, &end) <= 0);
1250             }
1251         }
1252       else
1253         {
1254           r = save_range (&range_start,
1255                           &range_end,
1256                           &end);
1257           
1258           insert_text_range (buffer,
1259                              iter,
1260                              &range_start,
1261                              &range_end,
1262                              interactive);
1263
1264           restore_range (r);
1265           r = NULL;
1266           
1267           range_start = range_end;
1268         }
1269     }
1270 }
1271
1272 static void
1273 insert_range_not_inside_self (GtkTextBuffer     *buffer,
1274                               GtkTextIter       *iter,
1275                               const GtkTextIter *orig_start,
1276                               const GtkTextIter *orig_end,
1277                               gboolean           interactive)
1278 {
1279   /* Find each range of uniformly-tagged text, insert it,
1280    * then apply the tags.
1281    */
1282   GtkTextIter start = *orig_start;
1283   GtkTextIter end = *orig_end;
1284   GtkTextIter range_start;
1285   GtkTextIter range_end;
1286   
1287   if (gtk_text_iter_equal (orig_start, orig_end))
1288     return;
1289   
1290   gtk_text_iter_order (&start, &end);
1291
1292   range_start = start;
1293   range_end = start;  
1294   
1295   while (TRUE)
1296     {
1297       gint start_offset;
1298       GtkTextIter start_iter;
1299       GSList *tags;
1300       GSList *tmp_list;
1301       Range *r;
1302       
1303       if (gtk_text_iter_equal (&range_start, &end))
1304         break; /* All done */
1305
1306       g_assert (gtk_text_iter_compare (&range_start, &end) < 0);
1307       
1308       gtk_text_iter_forward_to_tag_toggle (&range_end, NULL);
1309
1310       g_assert (!gtk_text_iter_equal (&range_start, &range_end));
1311
1312       /* Clamp to the end iterator */
1313       if (gtk_text_iter_compare (&range_end, &end) > 0)
1314         range_end = end;
1315       
1316       /* We have a range with unique tags; insert it, and
1317        * apply all tags.
1318        */
1319       start_offset = gtk_text_iter_get_offset (iter);
1320
1321       r = save_range (&range_start, &range_end, &end);
1322       
1323       insert_range_untagged (buffer, iter, &range_start, &range_end, interactive);
1324
1325       restore_range (r);
1326       r = NULL;
1327       
1328       gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start_offset);
1329       
1330       tags = gtk_text_iter_get_tags (&range_start);
1331       tmp_list = tags;
1332       while (tmp_list != NULL)
1333         {
1334           gtk_text_buffer_apply_tag (buffer,
1335                                      tmp_list->data,
1336                                      &start_iter,
1337                                      iter);
1338           
1339           tmp_list = g_slist_next (tmp_list);
1340         }
1341       g_slist_free (tags);
1342
1343       range_start = range_end;
1344     }
1345 }
1346
1347 static void
1348 gtk_text_buffer_real_insert_range (GtkTextBuffer     *buffer,
1349                                    GtkTextIter       *iter,
1350                                    const GtkTextIter *orig_start,
1351                                    const GtkTextIter *orig_end,
1352                                    gboolean           interactive)
1353 {
1354   GtkTextBuffer *src_buffer;
1355   
1356   /* Find each range of uniformly-tagged text, insert it,
1357    * then apply the tags.
1358    */  
1359   if (gtk_text_iter_equal (orig_start, orig_end))
1360     return;
1361
1362   if (interactive)
1363     gtk_text_buffer_begin_user_action (buffer);
1364   
1365   src_buffer = gtk_text_iter_get_buffer (orig_start);
1366   
1367   if (gtk_text_iter_get_buffer (iter) != src_buffer ||
1368       !gtk_text_iter_in_range (iter, orig_start, orig_end))
1369     {
1370       insert_range_not_inside_self (buffer, iter, orig_start, orig_end, interactive);
1371     }
1372   else
1373     {
1374       /* If you insert a range into itself, it could loop infinitely
1375        * because the region being copied keeps growing as we insert. So
1376        * we have to separately copy the range before and after
1377        * the insertion point.
1378        */
1379       GtkTextIter start = *orig_start;
1380       GtkTextIter end = *orig_end;
1381       GtkTextIter range_start;
1382       GtkTextIter range_end;
1383       Range *first_half;
1384       Range *second_half;
1385
1386       gtk_text_iter_order (&start, &end);
1387       
1388       range_start = start;
1389       range_end = *iter;
1390       first_half = save_range (&range_start, &range_end, &end);
1391
1392       range_start = *iter;
1393       range_end = end;
1394       second_half = save_range (&range_start, &range_end, &end);
1395
1396       restore_range (first_half);
1397       insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1398
1399       restore_range (second_half);
1400       insert_range_not_inside_self (buffer, iter, &range_start, &range_end, interactive);
1401     }
1402   
1403   if (interactive)
1404     gtk_text_buffer_end_user_action (buffer);
1405 }
1406
1407 /**
1408  * gtk_text_buffer_insert_range:
1409  * @buffer: a #GtkTextBuffer
1410  * @iter: a position in @buffer
1411  * @start: a position in a #GtkTextBuffer
1412  * @end: another position in the same buffer as @start
1413  *
1414  * Copies text, tags, and pixbufs between @start and @end (the order
1415  * of @start and @end doesn't matter) and inserts the copy at @iter.
1416  * Used instead of simply getting/inserting text because it preserves
1417  * images and tags. If @start and @end are in a different buffer from
1418  * @buffer, the two buffers must share the same tag table.
1419  *
1420  * Implemented via emissions of the insert_text and apply_tag signals,
1421  * so expect those.
1422  **/
1423 void
1424 gtk_text_buffer_insert_range (GtkTextBuffer     *buffer,
1425                               GtkTextIter       *iter,
1426                               const GtkTextIter *start,
1427                               const GtkTextIter *end)
1428 {
1429   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1430   g_return_if_fail (iter != NULL);
1431   g_return_if_fail (start != NULL);
1432   g_return_if_fail (end != NULL);
1433   g_return_if_fail (gtk_text_iter_get_buffer (start) ==
1434                     gtk_text_iter_get_buffer (end));
1435   g_return_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1436                     buffer->priv->tag_table);
1437   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1438   
1439   gtk_text_buffer_real_insert_range (buffer, iter, start, end, FALSE);
1440 }
1441
1442 /**
1443  * gtk_text_buffer_insert_range_interactive:
1444  * @buffer: a #GtkTextBuffer
1445  * @iter: a position in @buffer
1446  * @start: a position in a #GtkTextBuffer
1447  * @end: another position in the same buffer as @start
1448  * @default_editable: default editability of the buffer
1449  *
1450  * Same as gtk_text_buffer_insert_range(), but does nothing if the
1451  * insertion point isn't editable. The @default_editable parameter
1452  * indicates whether the text is editable at @iter if no tags
1453  * enclosing @iter affect editability. Typically the result of
1454  * gtk_text_view_get_editable() is appropriate here.
1455  *
1456  * Returns: whether an insertion was possible at @iter
1457  **/
1458 gboolean
1459 gtk_text_buffer_insert_range_interactive (GtkTextBuffer     *buffer,
1460                                           GtkTextIter       *iter,
1461                                           const GtkTextIter *start,
1462                                           const GtkTextIter *end,
1463                                           gboolean           default_editable)
1464 {
1465   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1466   g_return_val_if_fail (iter != NULL, FALSE);
1467   g_return_val_if_fail (start != NULL, FALSE);
1468   g_return_val_if_fail (end != NULL, FALSE);
1469   g_return_val_if_fail (gtk_text_iter_get_buffer (start) ==
1470                         gtk_text_iter_get_buffer (end), FALSE);
1471   g_return_val_if_fail (gtk_text_iter_get_buffer (start)->priv->tag_table ==
1472                         buffer->priv->tag_table, FALSE);
1473
1474   if (gtk_text_iter_can_insert (iter, default_editable))
1475     {
1476       gtk_text_buffer_real_insert_range (buffer, iter, start, end, TRUE);
1477       return TRUE;
1478     }
1479   else
1480     return FALSE;
1481 }
1482
1483 /**
1484  * gtk_text_buffer_insert_with_tags:
1485  * @buffer: a #GtkTextBuffer
1486  * @iter: an iterator in @buffer
1487  * @text: UTF-8 text
1488  * @len: length of @text, or -1
1489  * @first_tag: first tag to apply to @text
1490  * @...: %NULL-terminated list of tags to apply
1491  *
1492  * Inserts @text into @buffer at @iter, applying the list of tags to
1493  * the newly-inserted text. The last tag specified must be %NULL to
1494  * terminate the list. Equivalent to calling gtk_text_buffer_insert(),
1495  * then gtk_text_buffer_apply_tag() on the inserted text;
1496  * gtk_text_buffer_insert_with_tags() is just a convenience function.
1497  **/
1498 void
1499 gtk_text_buffer_insert_with_tags (GtkTextBuffer *buffer,
1500                                   GtkTextIter   *iter,
1501                                   const gchar   *text,
1502                                   gint           len,
1503                                   GtkTextTag    *first_tag,
1504                                   ...)
1505 {
1506   gint start_offset;
1507   GtkTextIter start;
1508   va_list args;
1509   GtkTextTag *tag;
1510
1511   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1512   g_return_if_fail (iter != NULL);
1513   g_return_if_fail (text != NULL);
1514   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1515   
1516   start_offset = gtk_text_iter_get_offset (iter);
1517
1518   gtk_text_buffer_insert (buffer, iter, text, len);
1519
1520   if (first_tag == NULL)
1521     return;
1522
1523   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1524
1525   va_start (args, first_tag);
1526   tag = first_tag;
1527   while (tag)
1528     {
1529       gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1530
1531       tag = va_arg (args, GtkTextTag*);
1532     }
1533
1534   va_end (args);
1535 }
1536
1537 /**
1538  * gtk_text_buffer_insert_with_tags_by_name:
1539  * @buffer: a #GtkTextBuffer
1540  * @iter: position in @buffer
1541  * @text: UTF-8 text
1542  * @len: length of @text, or -1
1543  * @first_tag_name: name of a tag to apply to @text
1544  * @...: more tag names
1545  *
1546  * Same as gtk_text_buffer_insert_with_tags(), but allows you
1547  * to pass in tag names instead of tag objects.
1548  **/
1549 void
1550 gtk_text_buffer_insert_with_tags_by_name  (GtkTextBuffer *buffer,
1551                                            GtkTextIter   *iter,
1552                                            const gchar   *text,
1553                                            gint           len,
1554                                            const gchar   *first_tag_name,
1555                                            ...)
1556 {
1557   gint start_offset;
1558   GtkTextIter start;
1559   va_list args;
1560   const gchar *tag_name;
1561
1562   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1563   g_return_if_fail (iter != NULL);
1564   g_return_if_fail (text != NULL);
1565   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1566   
1567   start_offset = gtk_text_iter_get_offset (iter);
1568
1569   gtk_text_buffer_insert (buffer, iter, text, len);
1570
1571   if (first_tag_name == NULL)
1572     return;
1573
1574   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_offset);
1575
1576   va_start (args, first_tag_name);
1577   tag_name = first_tag_name;
1578   while (tag_name)
1579     {
1580       GtkTextTag *tag;
1581
1582       tag = gtk_text_tag_table_lookup (buffer->priv->tag_table,
1583                                        tag_name);
1584
1585       if (tag == NULL)
1586         {
1587           g_warning ("%s: no tag with name '%s'!", G_STRLOC, tag_name);
1588           va_end (args);
1589           return;
1590         }
1591
1592       gtk_text_buffer_apply_tag (buffer, tag, &start, iter);
1593
1594       tag_name = va_arg (args, const gchar*);
1595     }
1596
1597   va_end (args);
1598 }
1599
1600
1601 /*
1602  * Deletion
1603  */
1604
1605 static void
1606 gtk_text_buffer_real_delete_range (GtkTextBuffer *buffer,
1607                                    GtkTextIter   *start,
1608                                    GtkTextIter   *end)
1609 {
1610   gboolean has_selection;
1611
1612   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1613   g_return_if_fail (start != NULL);
1614   g_return_if_fail (end != NULL);
1615
1616   _gtk_text_btree_delete (start, end);
1617
1618   /* may have deleted the selection... */
1619   update_selection_clipboards (buffer);
1620
1621   has_selection = gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL);
1622   if (has_selection != buffer->priv->has_selection)
1623     {
1624       buffer->priv->has_selection = has_selection;
1625       g_object_notify (G_OBJECT (buffer), "has-selection");
1626     }
1627
1628   g_signal_emit (buffer, signals[CHANGED], 0);
1629   g_object_notify (G_OBJECT (buffer), "cursor-position");
1630 }
1631
1632 static void
1633 gtk_text_buffer_emit_delete (GtkTextBuffer *buffer,
1634                              GtkTextIter *start,
1635                              GtkTextIter *end)
1636 {
1637   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1638   g_return_if_fail (start != NULL);
1639   g_return_if_fail (end != NULL);
1640
1641   if (gtk_text_iter_equal (start, end))
1642     return;
1643
1644   gtk_text_iter_order (start, end);
1645
1646   g_signal_emit (buffer,
1647                  signals[DELETE_RANGE],
1648                  0,
1649                  start, end);
1650 }
1651
1652 /**
1653  * gtk_text_buffer_delete:
1654  * @buffer: a #GtkTextBuffer
1655  * @start: a position in @buffer
1656  * @end: another position in @buffer
1657  *
1658  * Deletes text between @start and @end. The order of @start and @end
1659  * is not actually relevant; gtk_text_buffer_delete() will reorder
1660  * them. This function actually emits the "delete-range" signal, and
1661  * the default handler of that signal deletes the text. Because the
1662  * buffer is modified, all outstanding iterators become invalid after
1663  * calling this function; however, the @start and @end will be
1664  * re-initialized to point to the location where text was deleted.
1665  **/
1666 void
1667 gtk_text_buffer_delete (GtkTextBuffer *buffer,
1668                         GtkTextIter   *start,
1669                         GtkTextIter   *end)
1670 {
1671   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1672   g_return_if_fail (start != NULL);
1673   g_return_if_fail (end != NULL);
1674   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
1675   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
1676   
1677   gtk_text_buffer_emit_delete (buffer, start, end);
1678 }
1679
1680 /**
1681  * gtk_text_buffer_delete_interactive:
1682  * @buffer: a #GtkTextBuffer
1683  * @start_iter: start of range to delete
1684  * @end_iter: end of range
1685  * @default_editable: whether the buffer is editable by default
1686  *
1687  * Deletes all <emphasis>editable</emphasis> text in the given range.
1688  * Calls gtk_text_buffer_delete() for each editable sub-range of
1689  * [@start,@end). @start and @end are revalidated to point to
1690  * the location of the last deleted range, or left untouched if
1691  * no text was deleted.
1692  *
1693  * Return value: whether some text was actually deleted
1694  **/
1695 gboolean
1696 gtk_text_buffer_delete_interactive (GtkTextBuffer *buffer,
1697                                     GtkTextIter   *start_iter,
1698                                     GtkTextIter   *end_iter,
1699                                     gboolean       default_editable)
1700 {
1701   GtkTextMark *end_mark;
1702   GtkTextMark *start_mark;
1703   GtkTextIter iter;
1704   gboolean current_state;
1705   gboolean deleted_stuff = FALSE;
1706
1707   /* Delete all editable text in the range start_iter, end_iter */
1708
1709   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
1710   g_return_val_if_fail (start_iter != NULL, FALSE);
1711   g_return_val_if_fail (end_iter != NULL, FALSE);
1712   g_return_val_if_fail (gtk_text_iter_get_buffer (start_iter) == buffer, FALSE);
1713   g_return_val_if_fail (gtk_text_iter_get_buffer (end_iter) == buffer, FALSE);
1714
1715   
1716   gtk_text_buffer_begin_user_action (buffer);
1717   
1718   gtk_text_iter_order (start_iter, end_iter);
1719
1720   start_mark = gtk_text_buffer_create_mark (buffer, NULL,
1721                                             start_iter, TRUE);
1722   end_mark = gtk_text_buffer_create_mark (buffer, NULL,
1723                                           end_iter, FALSE);
1724
1725   gtk_text_buffer_get_iter_at_mark (buffer, &iter, start_mark);
1726
1727   current_state = gtk_text_iter_editable (&iter, default_editable);
1728
1729   while (TRUE)
1730     {
1731       gboolean new_state;
1732       gboolean done = FALSE;
1733       GtkTextIter end;
1734
1735       gtk_text_iter_forward_to_tag_toggle (&iter, NULL);
1736
1737       gtk_text_buffer_get_iter_at_mark (buffer, &end, end_mark);
1738
1739       if (gtk_text_iter_compare (&iter, &end) >= 0)
1740         {
1741           done = TRUE;
1742           iter = end; /* clamp to the last boundary */
1743         }
1744
1745       new_state = gtk_text_iter_editable (&iter, default_editable);
1746
1747       if (current_state == new_state)
1748         {
1749           if (done)
1750             {
1751               if (current_state)
1752                 {
1753                   /* We're ending an editable region. Delete said region. */
1754                   GtkTextIter start;
1755
1756                   gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1757
1758                   gtk_text_buffer_emit_delete (buffer, &start, &iter);
1759
1760                   deleted_stuff = TRUE;
1761
1762                   /* revalidate user's iterators. */
1763                   *start_iter = start;
1764                   *end_iter = iter;
1765                 }
1766
1767               break;
1768             }
1769           else
1770             continue;
1771         }
1772
1773       if (current_state && !new_state)
1774         {
1775           /* End of an editable region. Delete it. */
1776           GtkTextIter start;
1777
1778           gtk_text_buffer_get_iter_at_mark (buffer, &start, start_mark);
1779
1780           gtk_text_buffer_emit_delete (buffer, &start, &iter);
1781
1782           /* It's more robust to ask for the state again then to assume that
1783            * we're on the next not-editable segment. We don't know what the
1784            * ::delete-range handler did.... maybe it deleted the following
1785            * not-editable segment because it was associated with the editable
1786            * segment.
1787            */
1788           current_state = gtk_text_iter_editable (&iter, default_editable);
1789           deleted_stuff = TRUE;
1790
1791           /* revalidate user's iterators. */
1792           *start_iter = start;
1793           *end_iter = iter;
1794         }
1795       else
1796         {
1797           /* We are at the start of an editable region. We won't be deleting
1798            * the previous region. Move start mark to start of this region.
1799            */
1800
1801           g_assert (!current_state && new_state);
1802
1803           gtk_text_buffer_move_mark (buffer, start_mark, &iter);
1804
1805           current_state = TRUE;
1806         }
1807
1808       if (done)
1809         break;
1810     }
1811
1812   gtk_text_buffer_delete_mark (buffer, start_mark);
1813   gtk_text_buffer_delete_mark (buffer, end_mark);
1814
1815   gtk_text_buffer_end_user_action (buffer);
1816   
1817   return deleted_stuff;
1818 }
1819
1820 /*
1821  * Extracting textual buffer contents
1822  */
1823
1824 /**
1825  * gtk_text_buffer_get_text:
1826  * @buffer: a #GtkTextBuffer
1827  * @start: start of a range
1828  * @end: end of a range
1829  * @include_hidden_chars: whether to include invisible text
1830  *
1831  * Returns the text in the range [@start,@end). Excludes undisplayed
1832  * text (text marked with tags that set the invisibility attribute) if
1833  * @include_hidden_chars is %FALSE. Does not include characters
1834  * representing embedded images, so byte and character indexes into
1835  * the returned string do <emphasis>not</emphasis> correspond to byte
1836  * and character indexes into the buffer. Contrast with
1837  * gtk_text_buffer_get_slice().
1838  *
1839  * Return value: an allocated UTF-8 string
1840  **/
1841 gchar*
1842 gtk_text_buffer_get_text (GtkTextBuffer     *buffer,
1843                           const GtkTextIter *start,
1844                           const GtkTextIter *end,
1845                           gboolean           include_hidden_chars)
1846 {
1847   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1848   g_return_val_if_fail (start != NULL, NULL);
1849   g_return_val_if_fail (end != NULL, NULL);
1850   g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1851   g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1852   
1853   if (include_hidden_chars)
1854     return gtk_text_iter_get_text (start, end);
1855   else
1856     return gtk_text_iter_get_visible_text (start, end);
1857 }
1858
1859 /**
1860  * gtk_text_buffer_get_slice:
1861  * @buffer: a #GtkTextBuffer
1862  * @start: start of a range
1863  * @end: end of a range
1864  * @include_hidden_chars: whether to include invisible text
1865  *
1866  * Returns the text in the range [@start,@end). Excludes undisplayed
1867  * text (text marked with tags that set the invisibility attribute) if
1868  * @include_hidden_chars is %FALSE. The returned string includes a
1869  * 0xFFFC character whenever the buffer contains
1870  * embedded images, so byte and character indexes into
1871  * the returned string <emphasis>do</emphasis> correspond to byte
1872  * and character indexes into the buffer. Contrast with
1873  * gtk_text_buffer_get_text(). Note that 0xFFFC can occur in normal
1874  * text as well, so it is not a reliable indicator that a pixbuf or
1875  * widget is in the buffer.
1876  *
1877  * Return value: an allocated UTF-8 string
1878  **/
1879 gchar*
1880 gtk_text_buffer_get_slice (GtkTextBuffer     *buffer,
1881                            const GtkTextIter *start,
1882                            const GtkTextIter *end,
1883                            gboolean           include_hidden_chars)
1884 {
1885   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
1886   g_return_val_if_fail (start != NULL, NULL);
1887   g_return_val_if_fail (end != NULL, NULL);
1888   g_return_val_if_fail (gtk_text_iter_get_buffer (start) == buffer, NULL);
1889   g_return_val_if_fail (gtk_text_iter_get_buffer (end) == buffer, NULL);
1890   
1891   if (include_hidden_chars)
1892     return gtk_text_iter_get_slice (start, end);
1893   else
1894     return gtk_text_iter_get_visible_slice (start, end);
1895 }
1896
1897 /*
1898  * Pixbufs
1899  */
1900
1901 static void
1902 gtk_text_buffer_real_insert_pixbuf (GtkTextBuffer *buffer,
1903                                     GtkTextIter   *iter,
1904                                     GdkPixbuf     *pixbuf)
1905
1906   _gtk_text_btree_insert_pixbuf (iter, pixbuf);
1907
1908   g_signal_emit (buffer, signals[CHANGED], 0);
1909 }
1910
1911 /**
1912  * gtk_text_buffer_insert_pixbuf:
1913  * @buffer: a #GtkTextBuffer
1914  * @iter: location to insert the pixbuf
1915  * @pixbuf: a #GdkPixbuf
1916  *
1917  * Inserts an image into the text buffer at @iter. The image will be
1918  * counted as one character in character counts, and when obtaining
1919  * the buffer contents as a string, will be represented by the Unicode
1920  * "object replacement character" 0xFFFC. Note that the "slice"
1921  * variants for obtaining portions of the buffer as a string include
1922  * this character for pixbufs, but the "text" variants do
1923  * not. e.g. see gtk_text_buffer_get_slice() and
1924  * gtk_text_buffer_get_text().
1925  **/
1926 void
1927 gtk_text_buffer_insert_pixbuf (GtkTextBuffer *buffer,
1928                                GtkTextIter   *iter,
1929                                GdkPixbuf     *pixbuf)
1930 {
1931   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1932   g_return_if_fail (iter != NULL);
1933   g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1934   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1935   
1936   g_signal_emit (buffer, signals[INSERT_PIXBUF], 0,
1937                  iter, pixbuf);
1938 }
1939
1940 /*
1941  * Child anchor
1942  */
1943
1944
1945 static void
1946 gtk_text_buffer_real_insert_anchor (GtkTextBuffer      *buffer,
1947                                     GtkTextIter        *iter,
1948                                     GtkTextChildAnchor *anchor)
1949 {
1950   _gtk_text_btree_insert_child_anchor (iter, anchor);
1951
1952   g_signal_emit (buffer, signals[CHANGED], 0);
1953 }
1954
1955 /**
1956  * gtk_text_buffer_insert_child_anchor:
1957  * @buffer: a #GtkTextBuffer
1958  * @iter: location to insert the anchor
1959  * @anchor: a #GtkTextChildAnchor
1960  *
1961  * Inserts a child widget anchor into the text buffer at @iter. The
1962  * anchor will be counted as one character in character counts, and
1963  * when obtaining the buffer contents as a string, will be represented
1964  * by the Unicode "object replacement character" 0xFFFC. Note that the
1965  * "slice" variants for obtaining portions of the buffer as a string
1966  * include this character for child anchors, but the "text" variants do
1967  * not. E.g. see gtk_text_buffer_get_slice() and
1968  * gtk_text_buffer_get_text(). Consider
1969  * gtk_text_buffer_create_child_anchor() as a more convenient
1970  * alternative to this function. The buffer will add a reference to
1971  * the anchor, so you can unref it after insertion.
1972  **/
1973 void
1974 gtk_text_buffer_insert_child_anchor (GtkTextBuffer      *buffer,
1975                                      GtkTextIter        *iter,
1976                                      GtkTextChildAnchor *anchor)
1977 {
1978   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
1979   g_return_if_fail (iter != NULL);
1980   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
1981   g_return_if_fail (gtk_text_iter_get_buffer (iter) == buffer);
1982   
1983   g_signal_emit (buffer, signals[INSERT_CHILD_ANCHOR], 0,
1984                  iter, anchor);
1985 }
1986
1987 /**
1988  * gtk_text_buffer_create_child_anchor:
1989  * @buffer: a #GtkTextBuffer
1990  * @iter: location in the buffer
1991  * 
1992  * This is a convenience function which simply creates a child anchor
1993  * with gtk_text_child_anchor_new() and inserts it into the buffer
1994  * with gtk_text_buffer_insert_child_anchor(). The new anchor is
1995  * owned by the buffer; no reference count is returned to
1996  * the caller of gtk_text_buffer_create_child_anchor().
1997  * 
1998  * Return value: (transfer none): the created child anchor
1999  **/
2000 GtkTextChildAnchor*
2001 gtk_text_buffer_create_child_anchor (GtkTextBuffer *buffer,
2002                                      GtkTextIter   *iter)
2003 {
2004   GtkTextChildAnchor *anchor;
2005   
2006   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2007   g_return_val_if_fail (iter != NULL, NULL);
2008   g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2009   
2010   anchor = gtk_text_child_anchor_new ();
2011
2012   gtk_text_buffer_insert_child_anchor (buffer, iter, anchor);
2013
2014   g_object_unref (anchor);
2015
2016   return anchor;
2017 }
2018
2019 /*
2020  * Mark manipulation
2021  */
2022
2023 static void
2024 gtk_text_buffer_mark_set (GtkTextBuffer     *buffer,
2025                           const GtkTextIter *location,
2026                           GtkTextMark       *mark)
2027 {
2028   /* IMO this should NOT work like insert_text and delete_range,
2029    * where the real action happens in the default handler.
2030    * 
2031    * The reason is that the default handler would be _required_,
2032    * i.e. the whole widget would start breaking and segfaulting if the
2033    * default handler didn't get run. So you can't really override the
2034    * default handler or stop the emission; that is, this signal is
2035    * purely for notification, and not to allow users to modify the
2036    * default behavior.
2037    */
2038
2039   g_object_ref (mark);
2040
2041   g_signal_emit (buffer,
2042                  signals[MARK_SET],
2043                  0,
2044                  location,
2045                  mark);
2046
2047   g_object_unref (mark);
2048 }
2049
2050 /**
2051  * gtk_text_buffer_set_mark:
2052  * @buffer:       a #GtkTextBuffer
2053  * @mark_name:    name of the mark
2054  * @iter:         location for the mark
2055  * @left_gravity: if the mark is created by this function, gravity for 
2056  *                the new mark
2057  * @should_exist: if %TRUE, warn if the mark does not exist, and return
2058  *                immediately
2059  *
2060  * Move the mark to the given position, if not @should_exist, 
2061  * create the mark.
2062  *
2063  * Return value: mark
2064  **/
2065 static GtkTextMark*
2066 gtk_text_buffer_set_mark (GtkTextBuffer     *buffer,
2067                           GtkTextMark       *existing_mark,
2068                           const gchar       *mark_name,
2069                           const GtkTextIter *iter,
2070                           gboolean           left_gravity,
2071                           gboolean           should_exist)
2072 {
2073   GtkTextIter location;
2074   GtkTextMark *mark;
2075
2076   g_return_val_if_fail (gtk_text_iter_get_buffer (iter) == buffer, NULL);
2077   
2078   mark = _gtk_text_btree_set_mark (get_btree (buffer),
2079                                    existing_mark,
2080                                    mark_name,
2081                                    left_gravity,
2082                                    iter,
2083                                    should_exist);
2084   
2085   _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2086                                    &location,
2087                                    mark);
2088
2089   gtk_text_buffer_mark_set (buffer, &location, mark);
2090
2091   return mark;
2092 }
2093
2094 /**
2095  * gtk_text_buffer_create_mark:
2096  * @buffer: a #GtkTextBuffer
2097  * @mark_name: (allow-none): name for mark, or %NULL
2098  * @where: location to place mark
2099  * @left_gravity: whether the mark has left gravity
2100  *
2101  * Creates a mark at position @where. If @mark_name is %NULL, the mark
2102  * is anonymous; otherwise, the mark can be retrieved by name using
2103  * gtk_text_buffer_get_mark(). If a mark has left gravity, and text is
2104  * inserted at the mark's current location, the mark will be moved to
2105  * the left of the newly-inserted text. If the mark has right gravity
2106  * (@left_gravity = %FALSE), the mark will end up on the right of
2107  * newly-inserted text. The standard left-to-right cursor is a mark
2108  * with right gravity (when you type, the cursor stays on the right
2109  * side of the text you're typing).
2110  *
2111  * The caller of this function does <emphasis>not</emphasis> own a 
2112  * reference to the returned #GtkTextMark, so you can ignore the 
2113  * return value if you like. Marks are owned by the buffer and go 
2114  * away when the buffer does.
2115  *
2116  * Emits the "mark-set" signal as notification of the mark's initial
2117  * placement.
2118  *
2119  * Return value: (transfer none): the new #GtkTextMark object
2120  **/
2121 GtkTextMark*
2122 gtk_text_buffer_create_mark (GtkTextBuffer     *buffer,
2123                              const gchar       *mark_name,
2124                              const GtkTextIter *where,
2125                              gboolean           left_gravity)
2126 {
2127   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2128
2129   return gtk_text_buffer_set_mark (buffer, NULL, mark_name, where,
2130                                    left_gravity, FALSE);
2131 }
2132
2133 /**
2134  * gtk_text_buffer_add_mark:
2135  * @buffer: a #GtkTextBuffer
2136  * @mark: the mark to add
2137  * @where: location to place mark
2138  *
2139  * Adds the mark at position @where. The mark must not be added to
2140  * another buffer, and if its name is not %NULL then there must not
2141  * be another mark in the buffer with the same name.
2142  *
2143  * Emits the "mark-set" signal as notification of the mark's initial
2144  * placement.
2145  *
2146  * Since: 2.12
2147  **/
2148 void
2149 gtk_text_buffer_add_mark (GtkTextBuffer     *buffer,
2150                           GtkTextMark       *mark,
2151                           const GtkTextIter *where)
2152 {
2153   const gchar *name;
2154
2155   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2156   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2157   g_return_if_fail (where != NULL);
2158   g_return_if_fail (gtk_text_mark_get_buffer (mark) == NULL);
2159
2160   name = gtk_text_mark_get_name (mark);
2161
2162   if (name != NULL && gtk_text_buffer_get_mark (buffer, name) != NULL)
2163     {
2164       g_critical ("Mark %s already exists in the buffer", name);
2165       return;
2166     }
2167
2168   gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, FALSE);
2169 }
2170
2171 /**
2172  * gtk_text_buffer_move_mark:
2173  * @buffer: a #GtkTextBuffer
2174  * @mark: a #GtkTextMark
2175  * @where: new location for @mark in @buffer
2176  *
2177  * Moves @mark to the new location @where. Emits the "mark-set" signal
2178  * as notification of the move.
2179  **/
2180 void
2181 gtk_text_buffer_move_mark (GtkTextBuffer     *buffer,
2182                            GtkTextMark       *mark,
2183                            const GtkTextIter *where)
2184 {
2185   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2186   g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2187   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2188
2189   gtk_text_buffer_set_mark (buffer, mark, NULL, where, FALSE, TRUE);
2190 }
2191
2192 /**
2193  * gtk_text_buffer_get_iter_at_mark:
2194  * @buffer: a #GtkTextBuffer
2195  * @iter: (out): iterator to initialize
2196  * @mark: a #GtkTextMark in @buffer
2197  *
2198  * Initializes @iter with the current position of @mark.
2199  **/
2200 void
2201 gtk_text_buffer_get_iter_at_mark (GtkTextBuffer *buffer,
2202                                   GtkTextIter   *iter,
2203                                   GtkTextMark   *mark)
2204 {
2205   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2206   g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2207   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2208
2209   _gtk_text_btree_get_iter_at_mark (get_btree (buffer),
2210                                     iter,
2211                                     mark);
2212 }
2213
2214 /**
2215  * gtk_text_buffer_delete_mark:
2216  * @buffer: a #GtkTextBuffer
2217  * @mark: a #GtkTextMark in @buffer
2218  *
2219  * Deletes @mark, so that it's no longer located anywhere in the
2220  * buffer. Removes the reference the buffer holds to the mark, so if
2221  * you haven't called g_object_ref() on the mark, it will be freed. Even
2222  * if the mark isn't freed, most operations on @mark become
2223  * invalid, until it gets added to a buffer again with 
2224  * gtk_text_buffer_add_mark(). Use gtk_text_mark_get_deleted() to  
2225  * find out if a mark has been removed from its buffer.
2226  * The "mark-deleted" signal will be emitted as notification after 
2227  * the mark is deleted.
2228  **/
2229 void
2230 gtk_text_buffer_delete_mark (GtkTextBuffer *buffer,
2231                              GtkTextMark   *mark)
2232 {
2233   g_return_if_fail (GTK_IS_TEXT_MARK (mark));
2234   g_return_if_fail (!gtk_text_mark_get_deleted (mark));
2235   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2236
2237   g_object_ref (mark);
2238
2239   _gtk_text_btree_remove_mark (get_btree (buffer), mark);
2240
2241   /* See rationale above for MARK_SET on why we emit this after
2242    * removing the mark, rather than removing the mark in a default
2243    * handler.
2244    */
2245   g_signal_emit (buffer, signals[MARK_DELETED],
2246                  0,
2247                  mark);
2248
2249   g_object_unref (mark);
2250 }
2251
2252 /**
2253  * gtk_text_buffer_get_mark:
2254  * @buffer: a #GtkTextBuffer
2255  * @name: a mark name
2256  *
2257  * Returns the mark named @name in buffer @buffer, or %NULL if no such
2258  * mark exists in the buffer.
2259  *
2260  * Return value: (transfer none): a #GtkTextMark, or %NULL
2261  **/
2262 GtkTextMark*
2263 gtk_text_buffer_get_mark (GtkTextBuffer *buffer,
2264                           const gchar   *name)
2265 {
2266   GtkTextMark *mark;
2267
2268   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2269   g_return_val_if_fail (name != NULL, NULL);
2270
2271   mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2272
2273   return mark;
2274 }
2275
2276 /**
2277  * gtk_text_buffer_move_mark_by_name:
2278  * @buffer: a #GtkTextBuffer
2279  * @name: name of a mark
2280  * @where: new location for mark
2281  *
2282  * Moves the mark named @name (which must exist) to location @where.
2283  * See gtk_text_buffer_move_mark() for details.
2284  **/
2285 void
2286 gtk_text_buffer_move_mark_by_name (GtkTextBuffer     *buffer,
2287                                    const gchar       *name,
2288                                    const GtkTextIter *where)
2289 {
2290   GtkTextMark *mark;
2291
2292   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2293   g_return_if_fail (name != NULL);
2294
2295   mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2296
2297   if (mark == NULL)
2298     {
2299       g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2300       return;
2301     }
2302
2303   gtk_text_buffer_move_mark (buffer, mark, where);
2304 }
2305
2306 /**
2307  * gtk_text_buffer_delete_mark_by_name:
2308  * @buffer: a #GtkTextBuffer
2309  * @name: name of a mark in @buffer
2310  *
2311  * Deletes the mark named @name; the mark must exist. See
2312  * gtk_text_buffer_delete_mark() for details.
2313  **/
2314 void
2315 gtk_text_buffer_delete_mark_by_name (GtkTextBuffer *buffer,
2316                                      const gchar   *name)
2317 {
2318   GtkTextMark *mark;
2319
2320   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2321   g_return_if_fail (name != NULL);
2322
2323   mark = _gtk_text_btree_get_mark_by_name (get_btree (buffer), name);
2324
2325   if (mark == NULL)
2326     {
2327       g_warning ("%s: no mark named '%s'", G_STRLOC, name);
2328       return;
2329     }
2330
2331   gtk_text_buffer_delete_mark (buffer, mark);
2332 }
2333
2334 /**
2335  * gtk_text_buffer_get_insert:
2336  * @buffer: a #GtkTextBuffer
2337  *
2338  * Returns the mark that represents the cursor (insertion point).
2339  * Equivalent to calling gtk_text_buffer_get_mark() to get the mark
2340  * named "insert", but very slightly more efficient, and involves less
2341  * typing.
2342  *
2343  * Return value: (transfer none): insertion point mark
2344  **/
2345 GtkTextMark*
2346 gtk_text_buffer_get_insert (GtkTextBuffer *buffer)
2347 {
2348   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2349
2350   return _gtk_text_btree_get_insert (get_btree (buffer));
2351 }
2352
2353 /**
2354  * gtk_text_buffer_get_selection_bound:
2355  * @buffer: a #GtkTextBuffer
2356  *
2357  * Returns the mark that represents the selection bound.  Equivalent
2358  * to calling gtk_text_buffer_get_mark() to get the mark named
2359  * "selection_bound", but very slightly more efficient, and involves
2360  * less typing.
2361  *
2362  * The currently-selected text in @buffer is the region between the
2363  * "selection_bound" and "insert" marks. If "selection_bound" and
2364  * "insert" are in the same place, then there is no current selection.
2365  * gtk_text_buffer_get_selection_bounds() is another convenient function
2366  * for handling the selection, if you just want to know whether there's a
2367  * selection and what its bounds are.
2368  *
2369  * Return value: (transfer none): selection bound mark
2370  **/
2371 GtkTextMark*
2372 gtk_text_buffer_get_selection_bound (GtkTextBuffer *buffer)
2373 {
2374   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2375
2376   return _gtk_text_btree_get_selection_bound (get_btree (buffer));
2377 }
2378
2379 /**
2380  * gtk_text_buffer_get_iter_at_child_anchor:
2381  * @buffer: a #GtkTextBuffer
2382  * @iter: (out): an iterator to be initialized
2383  * @anchor: a child anchor that appears in @buffer
2384  *
2385  * Obtains the location of @anchor within @buffer.
2386  **/
2387 void
2388 gtk_text_buffer_get_iter_at_child_anchor (GtkTextBuffer      *buffer,
2389                                           GtkTextIter        *iter,
2390                                           GtkTextChildAnchor *anchor)
2391 {
2392   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2393   g_return_if_fail (iter != NULL);
2394   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
2395   g_return_if_fail (!gtk_text_child_anchor_get_deleted (anchor));
2396   
2397   _gtk_text_btree_get_iter_at_child_anchor (get_btree (buffer),
2398                                            iter,
2399                                            anchor);
2400 }
2401
2402 /**
2403  * gtk_text_buffer_place_cursor:
2404  * @buffer: a #GtkTextBuffer
2405  * @where: where to put the cursor
2406  *
2407  * This function moves the "insert" and "selection_bound" marks
2408  * simultaneously.  If you move them to the same place in two steps
2409  * with gtk_text_buffer_move_mark(), you will temporarily select a
2410  * region in between their old and new locations, which can be pretty
2411  * inefficient since the temporarily-selected region will force stuff
2412  * to be recalculated. This function moves them as a unit, which can
2413  * be optimized.
2414  **/
2415 void
2416 gtk_text_buffer_place_cursor (GtkTextBuffer     *buffer,
2417                               const GtkTextIter *where)
2418 {
2419   gtk_text_buffer_select_range (buffer, where, where);
2420 }
2421
2422 /**
2423  * gtk_text_buffer_select_range:
2424  * @buffer: a #GtkTextBuffer
2425  * @ins: where to put the "insert" mark
2426  * @bound: where to put the "selection_bound" mark
2427  *
2428  * This function moves the "insert" and "selection_bound" marks
2429  * simultaneously.  If you move them in two steps
2430  * with gtk_text_buffer_move_mark(), you will temporarily select a
2431  * region in between their old and new locations, which can be pretty
2432  * inefficient since the temporarily-selected region will force stuff
2433  * to be recalculated. This function moves them as a unit, which can
2434  * be optimized.
2435  *
2436  * Since: 2.4
2437  **/
2438 void
2439 gtk_text_buffer_select_range (GtkTextBuffer     *buffer,
2440                               const GtkTextIter *ins,
2441                               const GtkTextIter *bound)
2442 {
2443   GtkTextIter real_ins;
2444   GtkTextIter real_bound;
2445
2446   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2447
2448   real_ins = *ins;
2449   real_bound = *bound;
2450
2451   _gtk_text_btree_select_range (get_btree (buffer), &real_ins, &real_bound);
2452   gtk_text_buffer_mark_set (buffer, &real_ins,
2453                             gtk_text_buffer_get_insert (buffer));
2454   gtk_text_buffer_mark_set (buffer, &real_bound,
2455                             gtk_text_buffer_get_selection_bound (buffer));
2456 }
2457
2458 /*
2459  * Tags
2460  */
2461
2462 /**
2463  * gtk_text_buffer_create_tag:
2464  * @buffer: a #GtkTextBuffer
2465  * @tag_name: (allow-none): name of the new tag, or %NULL
2466  * @first_property_name: (allow-none): name of first property to set, or %NULL
2467  * @...: %NULL-terminated list of property names and values
2468  *
2469  * Creates a tag and adds it to the tag table for @buffer.
2470  * Equivalent to calling gtk_text_tag_new() and then adding the
2471  * tag to the buffer's tag table. The returned tag is owned by
2472  * the buffer's tag table, so the ref count will be equal to one.
2473  *
2474  * If @tag_name is %NULL, the tag is anonymous.
2475  *
2476  * If @tag_name is non-%NULL, a tag called @tag_name must not already
2477  * exist in the tag table for this buffer.
2478  *
2479  * The @first_property_name argument and subsequent arguments are a list
2480  * of properties to set on the tag, as with g_object_set().
2481  *
2482  * Return value: (transfer none): a new tag
2483  */
2484 GtkTextTag*
2485 gtk_text_buffer_create_tag (GtkTextBuffer *buffer,
2486                             const gchar   *tag_name,
2487                             const gchar   *first_property_name,
2488                             ...)
2489 {
2490   GtkTextTag *tag;
2491   va_list list;
2492   
2493   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
2494
2495   tag = gtk_text_tag_new (tag_name);
2496
2497   gtk_text_tag_table_add (get_table (buffer), tag);
2498
2499   if (first_property_name)
2500     {
2501       va_start (list, first_property_name);
2502       g_object_set_valist (G_OBJECT (tag), first_property_name, list);
2503       va_end (list);
2504     }
2505   
2506   g_object_unref (tag);
2507
2508   return tag;
2509 }
2510
2511 static void
2512 gtk_text_buffer_real_apply_tag (GtkTextBuffer     *buffer,
2513                                 GtkTextTag        *tag,
2514                                 const GtkTextIter *start,
2515                                 const GtkTextIter *end)
2516 {
2517   if (tag->priv->table != buffer->priv->tag_table)
2518     {
2519       g_warning ("Can only apply tags that are in the tag table for the buffer");
2520       return;
2521     }
2522   
2523   _gtk_text_btree_tag (start, end, tag, TRUE);
2524 }
2525
2526 static void
2527 gtk_text_buffer_real_remove_tag (GtkTextBuffer     *buffer,
2528                                  GtkTextTag        *tag,
2529                                  const GtkTextIter *start,
2530                                  const GtkTextIter *end)
2531 {
2532   if (tag->priv->table != buffer->priv->tag_table)
2533     {
2534       g_warning ("Can only remove tags that are in the tag table for the buffer");
2535       return;
2536     }
2537   
2538   _gtk_text_btree_tag (start, end, tag, FALSE);
2539 }
2540
2541 static void
2542 gtk_text_buffer_real_changed (GtkTextBuffer *buffer)
2543 {
2544   gtk_text_buffer_set_modified (buffer, TRUE);
2545 }
2546
2547 static void
2548 gtk_text_buffer_real_mark_set (GtkTextBuffer     *buffer,
2549                                const GtkTextIter *iter,
2550                                GtkTextMark       *mark)
2551 {
2552   GtkTextMark *insert;
2553   
2554   insert = gtk_text_buffer_get_insert (buffer);
2555
2556   if (mark == insert || mark == gtk_text_buffer_get_selection_bound (buffer))
2557     {
2558       gboolean has_selection;
2559
2560       update_selection_clipboards (buffer);
2561     
2562       has_selection = gtk_text_buffer_get_selection_bounds (buffer,
2563                                                             NULL,
2564                                                             NULL);
2565
2566       if (has_selection != buffer->priv->has_selection)
2567         {
2568           buffer->priv->has_selection = has_selection;
2569           g_object_notify (G_OBJECT (buffer), "has-selection");
2570         }
2571     }
2572     
2573     if (mark == insert)
2574       g_object_notify (G_OBJECT (buffer), "cursor-position");
2575 }
2576
2577 static void
2578 gtk_text_buffer_emit_tag (GtkTextBuffer     *buffer,
2579                           GtkTextTag        *tag,
2580                           gboolean           apply,
2581                           const GtkTextIter *start,
2582                           const GtkTextIter *end)
2583 {
2584   GtkTextIter start_tmp = *start;
2585   GtkTextIter end_tmp = *end;
2586
2587   g_return_if_fail (tag != NULL);
2588
2589   gtk_text_iter_order (&start_tmp, &end_tmp);
2590
2591   if (apply)
2592     g_signal_emit (buffer, signals[APPLY_TAG],
2593                    0,
2594                    tag, &start_tmp, &end_tmp);
2595   else
2596     g_signal_emit (buffer, signals[REMOVE_TAG],
2597                    0,
2598                    tag, &start_tmp, &end_tmp);
2599 }
2600
2601 /**
2602  * gtk_text_buffer_apply_tag:
2603  * @buffer: a #GtkTextBuffer
2604  * @tag: a #GtkTextTag
2605  * @start: one bound of range to be tagged
2606  * @end: other bound of range to be tagged
2607  *
2608  * Emits the "apply-tag" signal on @buffer. The default
2609  * handler for the signal applies @tag to the given range.
2610  * @start and @end do not have to be in order.
2611  **/
2612 void
2613 gtk_text_buffer_apply_tag (GtkTextBuffer     *buffer,
2614                            GtkTextTag        *tag,
2615                            const GtkTextIter *start,
2616                            const GtkTextIter *end)
2617 {
2618   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2619   g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2620   g_return_if_fail (start != NULL);
2621   g_return_if_fail (end != NULL);
2622   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2623   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2624   g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2625   
2626   gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2627 }
2628
2629 /**
2630  * gtk_text_buffer_remove_tag:
2631  * @buffer: a #GtkTextBuffer
2632  * @tag: a #GtkTextTag
2633  * @start: one bound of range to be untagged
2634  * @end: other bound of range to be untagged
2635  *
2636  * Emits the "remove-tag" signal. The default handler for the signal
2637  * removes all occurrences of @tag from the given range. @start and
2638  * @end don't have to be in order.
2639  **/
2640 void
2641 gtk_text_buffer_remove_tag (GtkTextBuffer     *buffer,
2642                             GtkTextTag        *tag,
2643                             const GtkTextIter *start,
2644                             const GtkTextIter *end)
2645
2646 {
2647   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2648   g_return_if_fail (GTK_IS_TEXT_TAG (tag));
2649   g_return_if_fail (start != NULL);
2650   g_return_if_fail (end != NULL);
2651   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2652   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2653   g_return_if_fail (tag->priv->table == buffer->priv->tag_table);
2654   
2655   gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2656 }
2657
2658 /**
2659  * gtk_text_buffer_apply_tag_by_name:
2660  * @buffer: a #GtkTextBuffer
2661  * @name: name of a named #GtkTextTag
2662  * @start: one bound of range to be tagged
2663  * @end: other bound of range to be tagged
2664  *
2665  * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2666  * get a #GtkTextTag, then calls gtk_text_buffer_apply_tag().
2667  **/
2668 void
2669 gtk_text_buffer_apply_tag_by_name (GtkTextBuffer     *buffer,
2670                                    const gchar       *name,
2671                                    const GtkTextIter *start,
2672                                    const GtkTextIter *end)
2673 {
2674   GtkTextTag *tag;
2675
2676   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2677   g_return_if_fail (name != NULL);
2678   g_return_if_fail (start != NULL);
2679   g_return_if_fail (end != NULL);
2680   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2681   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2682
2683   tag = gtk_text_tag_table_lookup (get_table (buffer),
2684                                    name);
2685
2686   if (tag == NULL)
2687     {
2688       g_warning ("Unknown tag `%s'", name);
2689       return;
2690     }
2691
2692   gtk_text_buffer_emit_tag (buffer, tag, TRUE, start, end);
2693 }
2694
2695 /**
2696  * gtk_text_buffer_remove_tag_by_name:
2697  * @buffer: a #GtkTextBuffer
2698  * @name: name of a #GtkTextTag
2699  * @start: one bound of range to be untagged
2700  * @end: other bound of range to be untagged
2701  *
2702  * Calls gtk_text_tag_table_lookup() on the buffer's tag table to
2703  * get a #GtkTextTag, then calls gtk_text_buffer_remove_tag().
2704  **/
2705 void
2706 gtk_text_buffer_remove_tag_by_name (GtkTextBuffer     *buffer,
2707                                     const gchar       *name,
2708                                     const GtkTextIter *start,
2709                                     const GtkTextIter *end)
2710 {
2711   GtkTextTag *tag;
2712
2713   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2714   g_return_if_fail (name != NULL);
2715   g_return_if_fail (start != NULL);
2716   g_return_if_fail (end != NULL);
2717   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2718   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2719   
2720   tag = gtk_text_tag_table_lookup (get_table (buffer),
2721                                    name);
2722
2723   if (tag == NULL)
2724     {
2725       g_warning ("Unknown tag `%s'", name);
2726       return;
2727     }
2728
2729   gtk_text_buffer_emit_tag (buffer, tag, FALSE, start, end);
2730 }
2731
2732 static gint
2733 pointer_cmp (gconstpointer a,
2734              gconstpointer b)
2735 {
2736   if (a < b)
2737     return -1;
2738   else if (a > b)
2739     return 1;
2740   else
2741     return 0;
2742 }
2743
2744 /**
2745  * gtk_text_buffer_remove_all_tags:
2746  * @buffer: a #GtkTextBuffer
2747  * @start: one bound of range to be untagged
2748  * @end: other bound of range to be untagged
2749  * 
2750  * Removes all tags in the range between @start and @end.  Be careful
2751  * with this function; it could remove tags added in code unrelated to
2752  * the code you're currently writing. That is, using this function is
2753  * probably a bad idea if you have two or more unrelated code sections
2754  * that add tags.
2755  **/
2756 void
2757 gtk_text_buffer_remove_all_tags (GtkTextBuffer     *buffer,
2758                                  const GtkTextIter *start,
2759                                  const GtkTextIter *end)
2760 {
2761   GtkTextIter first, second, tmp;
2762   GSList *tags;
2763   GSList *tmp_list;
2764   GSList *prev, *next;
2765   GtkTextTag *tag;
2766   
2767   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2768   g_return_if_fail (start != NULL);
2769   g_return_if_fail (end != NULL);
2770   g_return_if_fail (gtk_text_iter_get_buffer (start) == buffer);
2771   g_return_if_fail (gtk_text_iter_get_buffer (end) == buffer);
2772   
2773   first = *start;
2774   second = *end;
2775
2776   gtk_text_iter_order (&first, &second);
2777
2778   /* Get all tags turned on at the start */
2779   tags = gtk_text_iter_get_tags (&first);
2780   
2781   /* Find any that are toggled on within the range */
2782   tmp = first;
2783   while (gtk_text_iter_forward_to_tag_toggle (&tmp, NULL))
2784     {
2785       GSList *toggled;
2786       GSList *tmp_list2;
2787
2788       if (gtk_text_iter_compare (&tmp, &second) >= 0)
2789         break; /* past the end of the range */
2790       
2791       toggled = gtk_text_iter_get_toggled_tags (&tmp, TRUE);
2792
2793       /* We could end up with a really big-ass list here.
2794        * Fix it someday.
2795        */
2796       tmp_list2 = toggled;
2797       while (tmp_list2 != NULL)
2798         {
2799           tags = g_slist_prepend (tags, tmp_list2->data);
2800
2801           tmp_list2 = g_slist_next (tmp_list2);
2802         }
2803
2804       g_slist_free (toggled);
2805     }
2806   
2807   /* Sort the list */
2808   tags = g_slist_sort (tags, pointer_cmp);
2809
2810   /* Strip duplicates */
2811   tag = NULL;
2812   prev = NULL;
2813   tmp_list = tags;
2814   while (tmp_list != NULL)
2815     {
2816       if (tag == tmp_list->data)
2817         {
2818           /* duplicate */
2819           next = tmp_list->next;
2820           if (prev)
2821             prev->next = next;
2822
2823           tmp_list->next = NULL;
2824
2825           g_slist_free (tmp_list);
2826
2827           tmp_list = next;
2828           /* prev is unchanged */
2829         }
2830       else
2831         {
2832           /* not a duplicate */
2833           tag = GTK_TEXT_TAG (tmp_list->data);
2834           prev = tmp_list;
2835           tmp_list = tmp_list->next;
2836         }
2837     }
2838
2839   g_slist_foreach (tags, (GFunc) g_object_ref, NULL);
2840   
2841   tmp_list = tags;
2842   while (tmp_list != NULL)
2843     {
2844       tag = GTK_TEXT_TAG (tmp_list->data);
2845
2846       gtk_text_buffer_remove_tag (buffer, tag, &first, &second);
2847       
2848       tmp_list = tmp_list->next;
2849     }
2850
2851   g_slist_foreach (tags, (GFunc) g_object_unref, NULL);
2852   
2853   g_slist_free (tags);
2854 }
2855
2856
2857 /*
2858  * Obtain various iterators
2859  */
2860
2861 /**
2862  * gtk_text_buffer_get_iter_at_line_offset:
2863  * @buffer: a #GtkTextBuffer
2864  * @iter: (out): iterator to initialize
2865  * @line_number: line number counting from 0
2866  * @char_offset: char offset from start of line
2867  *
2868  * Obtains an iterator pointing to @char_offset within the given
2869  * line. The @char_offset must exist, offsets off the end of the line
2870  * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
2871  * UTF-8 may encode one character as multiple bytes.
2872  **/
2873 void
2874 gtk_text_buffer_get_iter_at_line_offset (GtkTextBuffer *buffer,
2875                                          GtkTextIter   *iter,
2876                                          gint           line_number,
2877                                          gint           char_offset)
2878 {
2879   g_return_if_fail (iter != NULL);
2880   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2881
2882   _gtk_text_btree_get_iter_at_line_char (get_btree (buffer),
2883                                          iter, line_number, char_offset);
2884 }
2885
2886 /**
2887  * gtk_text_buffer_get_iter_at_line_index:
2888  * @buffer: a #GtkTextBuffer 
2889  * @iter: (out): iterator to initialize 
2890  * @line_number: line number counting from 0
2891  * @byte_index: byte index from start of line
2892  *
2893  * Obtains an iterator pointing to @byte_index within the given line.
2894  * @byte_index must be the start of a UTF-8 character, and must not be
2895  * beyond the end of the line.  Note <emphasis>bytes</emphasis>, not
2896  * characters; UTF-8 may encode one character as multiple bytes.
2897  **/
2898 void
2899 gtk_text_buffer_get_iter_at_line_index  (GtkTextBuffer *buffer,
2900                                          GtkTextIter   *iter,
2901                                          gint           line_number,
2902                                          gint           byte_index)
2903 {
2904   g_return_if_fail (iter != NULL);
2905   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2906
2907   _gtk_text_btree_get_iter_at_line_byte (get_btree (buffer),
2908                                          iter, line_number, byte_index);
2909 }
2910
2911 /**
2912  * gtk_text_buffer_get_iter_at_line:
2913  * @buffer: a #GtkTextBuffer 
2914  * @iter: (out): iterator to initialize
2915  * @line_number: line number counting from 0
2916  * 
2917  * Initializes @iter to the start of the given line.
2918  **/
2919 void
2920 gtk_text_buffer_get_iter_at_line (GtkTextBuffer *buffer,
2921                                   GtkTextIter   *iter,
2922                                   gint           line_number)
2923 {
2924   g_return_if_fail (iter != NULL);
2925   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2926
2927   gtk_text_buffer_get_iter_at_line_offset (buffer, iter, line_number, 0);
2928 }
2929
2930 /**
2931  * gtk_text_buffer_get_iter_at_offset:
2932  * @buffer: a #GtkTextBuffer 
2933  * @iter: (out): iterator to initialize
2934  * @char_offset: char offset from start of buffer, counting from 0, or -1
2935  *
2936  * Initializes @iter to a position @char_offset chars from the start
2937  * of the entire buffer. If @char_offset is -1 or greater than the number
2938  * of characters in the buffer, @iter is initialized to the end iterator,
2939  * the iterator one past the last valid character in the buffer.
2940  **/
2941 void
2942 gtk_text_buffer_get_iter_at_offset (GtkTextBuffer *buffer,
2943                                     GtkTextIter   *iter,
2944                                     gint           char_offset)
2945 {
2946   g_return_if_fail (iter != NULL);
2947   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2948
2949   _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, char_offset);
2950 }
2951
2952 /**
2953  * gtk_text_buffer_get_start_iter:
2954  * @buffer: a #GtkTextBuffer
2955  * @iter: (out): iterator to initialize
2956  *
2957  * Initialized @iter with the first position in the text buffer. This
2958  * is the same as using gtk_text_buffer_get_iter_at_offset() to get
2959  * the iter at character offset 0.
2960  **/
2961 void
2962 gtk_text_buffer_get_start_iter (GtkTextBuffer *buffer,
2963                                 GtkTextIter   *iter)
2964 {
2965   g_return_if_fail (iter != NULL);
2966   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2967
2968   _gtk_text_btree_get_iter_at_char (get_btree (buffer), iter, 0);
2969 }
2970
2971 /**
2972  * gtk_text_buffer_get_end_iter:
2973  * @buffer: a #GtkTextBuffer 
2974  * @iter: (out): iterator to initialize
2975  *
2976  * Initializes @iter with the "end iterator," one past the last valid
2977  * character in the text buffer. If dereferenced with
2978  * gtk_text_iter_get_char(), the end iterator has a character value of
2979  * 0. The entire buffer lies in the range from the first position in
2980  * the buffer (call gtk_text_buffer_get_start_iter() to get
2981  * character position 0) to the end iterator.
2982  **/
2983 void
2984 gtk_text_buffer_get_end_iter (GtkTextBuffer *buffer,
2985                               GtkTextIter   *iter)
2986 {
2987   g_return_if_fail (iter != NULL);
2988   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
2989
2990   _gtk_text_btree_get_end_iter (get_btree (buffer), iter);
2991 }
2992
2993 /**
2994  * gtk_text_buffer_get_bounds:
2995  * @buffer: a #GtkTextBuffer 
2996  * @start: (out): iterator to initialize with first position in the buffer
2997  * @end: (out): iterator to initialize with the end iterator
2998  *
2999  * Retrieves the first and last iterators in the buffer, i.e. the
3000  * entire buffer lies within the range [@start,@end).
3001  **/
3002 void
3003 gtk_text_buffer_get_bounds (GtkTextBuffer *buffer,
3004                             GtkTextIter   *start,
3005                             GtkTextIter   *end)
3006 {
3007   g_return_if_fail (start != NULL);
3008   g_return_if_fail (end != NULL);
3009   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3010
3011   _gtk_text_btree_get_iter_at_char (get_btree (buffer), start, 0);
3012   _gtk_text_btree_get_end_iter (get_btree (buffer), end);
3013 }
3014
3015 /*
3016  * Modified flag
3017  */
3018
3019 /**
3020  * gtk_text_buffer_get_modified:
3021  * @buffer: a #GtkTextBuffer 
3022  * 
3023  * Indicates whether the buffer has been modified since the last call
3024  * to gtk_text_buffer_set_modified() set the modification flag to
3025  * %FALSE. Used for example to enable a "save" function in a text
3026  * editor.
3027  * 
3028  * Return value: %TRUE if the buffer has been modified
3029  **/
3030 gboolean
3031 gtk_text_buffer_get_modified (GtkTextBuffer *buffer)
3032 {
3033   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3034
3035   return buffer->priv->modified;
3036 }
3037
3038 /**
3039  * gtk_text_buffer_set_modified:
3040  * @buffer: a #GtkTextBuffer 
3041  * @setting: modification flag setting
3042  *
3043  * Used to keep track of whether the buffer has been modified since the
3044  * last time it was saved. Whenever the buffer is saved to disk, call
3045  * gtk_text_buffer_set_modified (@buffer, FALSE). When the buffer is modified,
3046  * it will automatically toggled on the modified bit again. When the modified
3047  * bit flips, the buffer emits a "modified-changed" signal.
3048  **/
3049 void
3050 gtk_text_buffer_set_modified (GtkTextBuffer *buffer,
3051                               gboolean       setting)
3052 {
3053   gboolean fixed_setting;
3054
3055   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3056
3057   fixed_setting = setting != FALSE;
3058
3059   if (buffer->priv->modified == fixed_setting)
3060     return;
3061   else
3062     {
3063       buffer->priv->modified = fixed_setting;
3064       g_signal_emit (buffer, signals[MODIFIED_CHANGED], 0);
3065     }
3066 }
3067
3068 /**
3069  * gtk_text_buffer_get_has_selection:
3070  * @buffer: a #GtkTextBuffer 
3071  * 
3072  * Indicates whether the buffer has some text currently selected.
3073  * 
3074  * Return value: %TRUE if the there is text selected
3075  *
3076  * Since: 2.10
3077  **/
3078 gboolean
3079 gtk_text_buffer_get_has_selection (GtkTextBuffer *buffer)
3080 {
3081   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3082
3083   return buffer->priv->has_selection;
3084 }
3085
3086
3087 /*
3088  * Assorted other stuff
3089  */
3090
3091 /**
3092  * gtk_text_buffer_get_line_count:
3093  * @buffer: a #GtkTextBuffer 
3094  * 
3095  * Obtains the number of lines in the buffer. This value is cached, so
3096  * the function is very fast.
3097  * 
3098  * Return value: number of lines in the buffer
3099  **/
3100 gint
3101 gtk_text_buffer_get_line_count (GtkTextBuffer *buffer)
3102 {
3103   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3104
3105   return _gtk_text_btree_line_count (get_btree (buffer));
3106 }
3107
3108 /**
3109  * gtk_text_buffer_get_char_count:
3110  * @buffer: a #GtkTextBuffer 
3111  * 
3112  * Gets the number of characters in the buffer; note that characters
3113  * and bytes are not the same, you can't e.g. expect the contents of
3114  * the buffer in string form to be this many bytes long. The character
3115  * count is cached, so this function is very fast.
3116  * 
3117  * Return value: number of characters in the buffer
3118  **/
3119 gint
3120 gtk_text_buffer_get_char_count (GtkTextBuffer *buffer)
3121 {
3122   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), 0);
3123
3124   return _gtk_text_btree_char_count (get_btree (buffer));
3125 }
3126
3127 /* Called when we lose the primary selection.
3128  */
3129 static void
3130 clipboard_clear_selection_cb (GtkClipboard *clipboard,
3131                               gpointer      data)
3132 {
3133   /* Move selection_bound to the insertion point */
3134   GtkTextIter insert;
3135   GtkTextIter selection_bound;
3136   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3137
3138   gtk_text_buffer_get_iter_at_mark (buffer, &insert,
3139                                     gtk_text_buffer_get_insert (buffer));
3140   gtk_text_buffer_get_iter_at_mark (buffer, &selection_bound,
3141                                     gtk_text_buffer_get_selection_bound (buffer));
3142
3143   if (!gtk_text_iter_equal (&insert, &selection_bound))
3144     gtk_text_buffer_move_mark (buffer,
3145                                gtk_text_buffer_get_selection_bound (buffer),
3146                                &insert);
3147 }
3148
3149 /* Called when we have the primary selection and someone else wants our
3150  * data in order to paste it.
3151  */
3152 static void
3153 clipboard_get_selection_cb (GtkClipboard     *clipboard,
3154                             GtkSelectionData *selection_data,
3155                             guint             info,
3156                             gpointer          data)
3157 {
3158   GtkTextBuffer *buffer = GTK_TEXT_BUFFER (data);
3159   GtkTextIter start, end;
3160
3161   if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3162     {
3163       if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3164         {
3165           /* Provide the address of the buffer; this will only be
3166            * used within-process
3167            */
3168           gtk_selection_data_set (selection_data,
3169                                   gtk_selection_data_get_target (selection_data),
3170                                   8, /* bytes */
3171                                   (void*)&buffer,
3172                                   sizeof (buffer));
3173         }
3174       else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3175         {
3176           guint8 *str;
3177           gsize   len;
3178
3179           str = gtk_text_buffer_serialize (buffer, buffer,
3180                                            gtk_selection_data_get_target (selection_data),
3181                                            &start, &end, &len);
3182
3183           gtk_selection_data_set (selection_data,
3184                                   gtk_selection_data_get_target (selection_data),
3185                                   8, /* bytes */
3186                                   str, len);
3187           g_free (str);
3188         }
3189       else
3190         {
3191           gchar *str;
3192
3193           str = gtk_text_iter_get_visible_text (&start, &end);
3194           gtk_selection_data_set_text (selection_data, str, -1);
3195           g_free (str);
3196         }
3197     }
3198 }
3199
3200 static GtkTextBuffer *
3201 create_clipboard_contents_buffer (GtkTextBuffer *buffer)
3202 {
3203   GtkTextBuffer *contents;
3204
3205   contents = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
3206
3207   g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard-source"),
3208                      buffer);
3209   g_object_set_data (G_OBJECT (contents), I_("gtk-text-buffer-clipboard"),
3210                      GINT_TO_POINTER (1));
3211
3212   /*  Ref the source buffer as long as the clipboard contents buffer
3213    *  exists, because it's needed for serializing the contents buffer.
3214    *  See http://bugzilla.gnome.org/show_bug.cgi?id=339195
3215    */
3216   g_object_ref (buffer);
3217   g_object_weak_ref (G_OBJECT (contents), (GWeakNotify) g_object_unref, buffer);
3218
3219   return contents;
3220 }
3221
3222 /* Provide cut/copied data */
3223 static void
3224 clipboard_get_contents_cb (GtkClipboard     *clipboard,
3225                            GtkSelectionData *selection_data,
3226                            guint             info,
3227                            gpointer          data)
3228 {
3229   GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3230
3231   g_assert (contents); /* This should never be called unless we own the clipboard */
3232
3233   if (info == GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS)
3234     {
3235       /* Provide the address of the clipboard buffer; this will only
3236        * be used within-process. OK to supply a NULL value for contents.
3237        */
3238       gtk_selection_data_set (selection_data,
3239                               gtk_selection_data_get_target (selection_data),
3240                               8, /* bytes */
3241                               (void*)&contents,
3242                               sizeof (contents));
3243     }
3244   else if (info == GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT)
3245     {
3246       GtkTextBuffer *clipboard_source_buffer;
3247       GtkTextIter start, end;
3248       guint8 *str;
3249       gsize   len;
3250
3251       clipboard_source_buffer = g_object_get_data (G_OBJECT (contents),
3252                                                    "gtk-text-buffer-clipboard-source");
3253
3254       gtk_text_buffer_get_bounds (contents, &start, &end);
3255
3256       str = gtk_text_buffer_serialize (clipboard_source_buffer, contents,
3257                                        gtk_selection_data_get_target (selection_data),
3258                                        &start, &end, &len);
3259
3260       gtk_selection_data_set (selection_data,
3261                               gtk_selection_data_get_target (selection_data),
3262                               8, /* bytes */
3263                               str, len);
3264       g_free (str);
3265     }
3266   else
3267     {
3268       gchar *str;
3269       GtkTextIter start, end;
3270
3271       gtk_text_buffer_get_bounds (contents, &start, &end);
3272
3273       str = gtk_text_iter_get_visible_text (&start, &end);
3274       gtk_selection_data_set_text (selection_data, str, -1);
3275       g_free (str);
3276     }
3277 }
3278
3279 static void
3280 clipboard_clear_contents_cb (GtkClipboard *clipboard,
3281                              gpointer      data)
3282 {
3283   GtkTextBuffer *contents = GTK_TEXT_BUFFER (data);
3284
3285   g_object_unref (contents);
3286 }
3287
3288 static void
3289 get_paste_point (GtkTextBuffer *buffer,
3290                  GtkTextIter   *iter,
3291                  gboolean       clear_afterward)
3292 {
3293   GtkTextIter insert_point;
3294   GtkTextMark *paste_point_override;
3295
3296   paste_point_override = gtk_text_buffer_get_mark (buffer,
3297                                                    "gtk_paste_point_override");
3298
3299   if (paste_point_override != NULL)
3300     {
3301       gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3302                                         paste_point_override);
3303       if (clear_afterward)
3304         gtk_text_buffer_delete_mark (buffer, paste_point_override);
3305     }
3306   else
3307     {
3308       gtk_text_buffer_get_iter_at_mark (buffer, &insert_point,
3309                                         gtk_text_buffer_get_insert (buffer));
3310     }
3311
3312   *iter = insert_point;
3313 }
3314
3315 static void
3316 pre_paste_prep (ClipboardRequest *request_data,
3317                 GtkTextIter      *insert_point)
3318 {
3319   GtkTextBuffer *buffer = request_data->buffer;
3320   
3321   get_paste_point (buffer, insert_point, TRUE);
3322
3323   /* If we're going to replace the selection, we insert before it to
3324    * avoid messing it up, then we delete the selection after inserting.
3325    */
3326   if (request_data->replace_selection)
3327     {
3328       GtkTextIter start, end;
3329       
3330       if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3331         *insert_point = start;
3332     }
3333 }
3334
3335 static void
3336 post_paste_cleanup (ClipboardRequest *request_data)
3337 {
3338   if (request_data->replace_selection)
3339     {
3340       GtkTextIter start, end;
3341       
3342       if (gtk_text_buffer_get_selection_bounds (request_data->buffer,
3343                                                 &start, &end))
3344         {
3345           if (request_data->interactive)
3346             gtk_text_buffer_delete_interactive (request_data->buffer,
3347                                                 &start,
3348                                                 &end,
3349                                                 request_data->default_editable);
3350           else
3351             gtk_text_buffer_delete (request_data->buffer, &start, &end);
3352         }
3353     }
3354 }
3355
3356 static void
3357 emit_paste_done (GtkTextBuffer *buffer,
3358                  GtkClipboard  *clipboard)
3359 {
3360   g_signal_emit (buffer, signals[PASTE_DONE], 0, clipboard);
3361 }
3362
3363 static void
3364 free_clipboard_request (ClipboardRequest *request_data)
3365 {
3366   g_object_unref (request_data->buffer);
3367   g_free (request_data);
3368 }
3369
3370 /* Called when we request a paste and receive the text data
3371  */
3372 static void
3373 clipboard_text_received (GtkClipboard *clipboard,
3374                          const gchar  *str,
3375                          gpointer      data)
3376 {
3377   ClipboardRequest *request_data = data;
3378   GtkTextBuffer *buffer = request_data->buffer;
3379
3380   if (str)
3381     {
3382       GtkTextIter insert_point;
3383       
3384       if (request_data->interactive) 
3385         gtk_text_buffer_begin_user_action (buffer);
3386
3387       pre_paste_prep (request_data, &insert_point);
3388       
3389       if (request_data->interactive) 
3390         gtk_text_buffer_insert_interactive (buffer, &insert_point,
3391                                             str, -1, request_data->default_editable);
3392       else
3393         gtk_text_buffer_insert (buffer, &insert_point,
3394                                 str, -1);
3395
3396       post_paste_cleanup (request_data);
3397       
3398       if (request_data->interactive) 
3399         gtk_text_buffer_end_user_action (buffer);
3400
3401       emit_paste_done (buffer, clipboard);
3402     }
3403   else
3404     {
3405       /* It may happen that we set a point override but we are not inserting
3406          any text, so we must remove it afterwards */
3407       GtkTextMark *paste_point_override;
3408
3409       paste_point_override = gtk_text_buffer_get_mark (buffer,
3410                                                        "gtk_paste_point_override");
3411
3412       if (paste_point_override != NULL)
3413         gtk_text_buffer_delete_mark (buffer, paste_point_override);
3414     }
3415
3416   free_clipboard_request (request_data);
3417 }
3418
3419 static GtkTextBuffer*
3420 selection_data_get_buffer (GtkSelectionData *selection_data,
3421                            ClipboardRequest *request_data)
3422 {
3423   GdkWindow *owner;
3424   GtkTextBuffer *src_buffer = NULL;
3425
3426   /* If we can get the owner, the selection is in-process */
3427   owner = gdk_selection_owner_get_for_display (gtk_selection_data_get_display (selection_data),
3428                                                gtk_selection_data_get_selection (selection_data));
3429
3430   if (owner == NULL)
3431     return NULL;
3432   
3433   if (gdk_window_get_window_type (owner) == GDK_WINDOW_FOREIGN)
3434     return NULL;
3435
3436   if (gtk_selection_data_get_data_type (selection_data) != gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"))
3437     return NULL;
3438
3439   if (gtk_selection_data_get_length (selection_data) != sizeof (src_buffer))
3440     return NULL;
3441
3442   memcpy (&src_buffer, gtk_selection_data_get_data (selection_data), sizeof (src_buffer));
3443
3444   if (src_buffer == NULL)
3445     return NULL;
3446   
3447   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (src_buffer), NULL);
3448
3449   if (gtk_text_buffer_get_tag_table (src_buffer) !=
3450       gtk_text_buffer_get_tag_table (request_data->buffer))
3451     return NULL;
3452   
3453   return src_buffer;
3454 }
3455
3456 #if 0
3457 /* These are pretty handy functions; maybe something like them
3458  * should be in the public API. Also, there are other places in this
3459  * file where they could be used.
3460  */
3461 static gpointer
3462 save_iter (const GtkTextIter *iter,
3463            gboolean           left_gravity)
3464 {
3465   return gtk_text_buffer_create_mark (gtk_text_iter_get_buffer (iter),
3466                                       NULL,
3467                                       iter,
3468                                       TRUE);
3469 }
3470
3471 static void
3472 restore_iter (const GtkTextIter *iter,
3473               gpointer           save_id)
3474 {
3475   gtk_text_buffer_get_iter_at_mark (gtk_text_mark_get_buffer (save_id),
3476                                     (GtkTextIter*) iter,
3477                                     save_id);
3478   gtk_text_buffer_delete_mark (gtk_text_mark_get_buffer (save_id),
3479                                save_id);
3480 }
3481 #endif
3482
3483 static void
3484 clipboard_rich_text_received (GtkClipboard *clipboard,
3485                               GdkAtom       format,
3486                               const guint8 *text,
3487                               gsize         length,
3488                               gpointer      data)
3489 {
3490   ClipboardRequest *request_data = data;
3491   GtkTextIter insert_point;
3492   gboolean retval = TRUE;
3493   GError *error = NULL;
3494
3495   if (text != NULL && length > 0)
3496     {
3497       pre_paste_prep (request_data, &insert_point);
3498
3499       if (request_data->interactive)
3500         gtk_text_buffer_begin_user_action (request_data->buffer);
3501
3502       if (!request_data->interactive ||
3503           gtk_text_iter_can_insert (&insert_point,
3504                                     request_data->default_editable))
3505         {
3506           retval = gtk_text_buffer_deserialize (request_data->buffer,
3507                                                 request_data->buffer,
3508                                                 format,
3509                                                 &insert_point,
3510                                                 text, length,
3511                                                 &error);
3512         }
3513
3514       if (!retval)
3515         {
3516           g_warning ("error pasting: %s\n", error->message);
3517           g_clear_error (&error);
3518         }
3519
3520       if (request_data->interactive)
3521         gtk_text_buffer_end_user_action (request_data->buffer);
3522
3523       emit_paste_done (request_data->buffer, clipboard);
3524
3525       if (retval)
3526         {
3527           post_paste_cleanup (request_data);
3528           return;
3529         }
3530     }
3531
3532   /* Request the text selection instead */
3533   gtk_clipboard_request_text (clipboard,
3534                               clipboard_text_received,
3535                               data);
3536 }
3537
3538 static void
3539 paste_from_buffer (GtkClipboard      *clipboard,
3540                    ClipboardRequest  *request_data,
3541                    GtkTextBuffer     *src_buffer,
3542                    const GtkTextIter *start,
3543                    const GtkTextIter *end)
3544 {
3545   GtkTextIter insert_point;
3546   GtkTextBuffer *buffer = request_data->buffer;
3547   
3548   /* We're about to emit a bunch of signals, so be safe */
3549   g_object_ref (src_buffer);
3550   
3551   pre_paste_prep (request_data, &insert_point);
3552   
3553   if (request_data->interactive) 
3554     gtk_text_buffer_begin_user_action (buffer);
3555
3556   if (!gtk_text_iter_equal (start, end))
3557     {
3558       if (!request_data->interactive ||
3559           (gtk_text_iter_can_insert (&insert_point,
3560                                      request_data->default_editable)))
3561         gtk_text_buffer_real_insert_range (buffer,
3562                                            &insert_point,
3563                                            start,
3564                                            end,
3565                                            request_data->interactive);
3566     }
3567
3568   post_paste_cleanup (request_data);
3569       
3570   if (request_data->interactive) 
3571     gtk_text_buffer_end_user_action (buffer);
3572
3573   emit_paste_done (buffer, clipboard);
3574
3575   g_object_unref (src_buffer);
3576
3577   free_clipboard_request (request_data);
3578 }
3579
3580 static void
3581 clipboard_clipboard_buffer_received (GtkClipboard     *clipboard,
3582                                      GtkSelectionData *selection_data,
3583                                      gpointer          data)
3584 {
3585   ClipboardRequest *request_data = data;
3586   GtkTextBuffer *src_buffer;
3587
3588   src_buffer = selection_data_get_buffer (selection_data, request_data); 
3589
3590   if (src_buffer)
3591     {
3592       GtkTextIter start, end;
3593
3594       if (g_object_get_data (G_OBJECT (src_buffer), "gtk-text-buffer-clipboard"))
3595         {
3596           gtk_text_buffer_get_bounds (src_buffer, &start, &end);
3597
3598           paste_from_buffer (clipboard, request_data, src_buffer,
3599                              &start, &end);
3600         }
3601       else
3602         {
3603           if (gtk_text_buffer_get_selection_bounds (src_buffer, &start, &end))
3604             paste_from_buffer (clipboard, request_data, src_buffer,
3605                                &start, &end);
3606         }
3607     }
3608   else
3609     {
3610       if (gtk_clipboard_wait_is_rich_text_available (clipboard,
3611                                                      request_data->buffer))
3612         {
3613           /* Request rich text */
3614           gtk_clipboard_request_rich_text (clipboard,
3615                                            request_data->buffer,
3616                                            clipboard_rich_text_received,
3617                                            data);
3618         }
3619       else
3620         {
3621           /* Request the text selection instead */
3622           gtk_clipboard_request_text (clipboard,
3623                                       clipboard_text_received,
3624                                       data);
3625         }
3626     }
3627 }
3628
3629 typedef struct
3630 {
3631   GtkClipboard *clipboard;
3632   guint ref_count;
3633 } SelectionClipboard;
3634
3635 static void
3636 update_selection_clipboards (GtkTextBuffer *buffer)
3637 {
3638   GtkTextBufferPrivate *priv;
3639   GSList               *tmp_list = buffer->priv->selection_clipboards;
3640
3641   priv = buffer->priv;
3642
3643   gtk_text_buffer_get_copy_target_list (buffer);
3644
3645   while (tmp_list)
3646     {
3647       GtkTextIter start;
3648       GtkTextIter end;
3649       
3650       SelectionClipboard *selection_clipboard = tmp_list->data;
3651       GtkClipboard *clipboard = selection_clipboard->clipboard;
3652
3653       /* Determine whether we have a selection and adjust X selection
3654        * accordingly.
3655        */
3656       if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3657         {
3658           if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (buffer))
3659             gtk_clipboard_clear (clipboard);
3660         }
3661       else
3662         {
3663           /* Even if we already have the selection, we need to update our
3664            * timestamp.
3665            */
3666           if (!gtk_clipboard_set_with_owner (clipboard,
3667                                              priv->copy_target_entries,
3668                                              priv->n_copy_target_entries,
3669                                              clipboard_get_selection_cb,
3670                                              clipboard_clear_selection_cb,
3671                                              G_OBJECT (buffer)))
3672             clipboard_clear_selection_cb (clipboard, buffer);
3673         }
3674
3675       tmp_list = tmp_list->next;
3676     }
3677 }
3678
3679 static SelectionClipboard *
3680 find_selection_clipboard (GtkTextBuffer *buffer,
3681                           GtkClipboard  *clipboard)
3682 {
3683   GSList *tmp_list = buffer->priv->selection_clipboards;
3684   while (tmp_list)
3685     {
3686       SelectionClipboard *selection_clipboard = tmp_list->data;
3687       if (selection_clipboard->clipboard == clipboard)
3688         return selection_clipboard;
3689       
3690       tmp_list = tmp_list->next;
3691     }
3692
3693   return NULL;
3694 }
3695
3696 /**
3697  * gtk_text_buffer_add_selection_clipboard:
3698  * @buffer: a #GtkTextBuffer
3699  * @clipboard: a #GtkClipboard
3700  * 
3701  * Adds @clipboard to the list of clipboards in which the selection 
3702  * contents of @buffer are available. In most cases, @clipboard will be 
3703  * the #GtkClipboard of type %GDK_SELECTION_PRIMARY for a view of @buffer.
3704  **/
3705 void
3706 gtk_text_buffer_add_selection_clipboard (GtkTextBuffer *buffer,
3707                                          GtkClipboard  *clipboard)
3708 {
3709   SelectionClipboard *selection_clipboard;
3710
3711   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3712   g_return_if_fail (clipboard != NULL);
3713
3714   selection_clipboard = find_selection_clipboard (buffer, clipboard);
3715   if (selection_clipboard)
3716     {
3717       selection_clipboard->ref_count++;
3718     }
3719   else
3720     {
3721       selection_clipboard = g_new (SelectionClipboard, 1);
3722
3723       selection_clipboard->clipboard = clipboard;
3724       selection_clipboard->ref_count = 1;
3725
3726       buffer->priv->selection_clipboards = g_slist_prepend (buffer->priv->selection_clipboards,
3727                                                             selection_clipboard);
3728     }
3729 }
3730
3731 /**
3732  * gtk_text_buffer_remove_selection_clipboard:
3733  * @buffer: a #GtkTextBuffer
3734  * @clipboard: a #GtkClipboard added to @buffer by 
3735  *             gtk_text_buffer_add_selection_clipboard()
3736  * 
3737  * Removes a #GtkClipboard added with 
3738  * gtk_text_buffer_add_selection_clipboard().
3739  **/
3740 void 
3741 gtk_text_buffer_remove_selection_clipboard (GtkTextBuffer *buffer,
3742                                             GtkClipboard  *clipboard)
3743 {
3744   SelectionClipboard *selection_clipboard;
3745
3746   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
3747   g_return_if_fail (clipboard != NULL);
3748
3749   selection_clipboard = find_selection_clipboard (buffer, clipboard);
3750   g_return_if_fail (selection_clipboard != NULL);
3751
3752   selection_clipboard->ref_count--;
3753   if (selection_clipboard->ref_count == 0)
3754     {
3755       if (gtk_clipboard_get_owner (selection_clipboard->clipboard) == G_OBJECT (buffer))
3756         gtk_clipboard_clear (selection_clipboard->clipboard);
3757
3758       buffer->priv->selection_clipboards = g_slist_remove (buffer->priv->selection_clipboards,
3759                                                            selection_clipboard);
3760       
3761       g_free (selection_clipboard);
3762     }
3763 }
3764
3765 static void
3766 remove_all_selection_clipboards (GtkTextBuffer *buffer)
3767 {
3768   GtkTextBufferPrivate *priv = buffer->priv;
3769
3770   g_slist_foreach (priv->selection_clipboards, (GFunc)g_free, NULL);
3771   g_slist_free (priv->selection_clipboards);
3772   priv->selection_clipboards = NULL;
3773 }
3774
3775 /**
3776  * gtk_text_buffer_paste_clipboard:
3777  * @buffer: a #GtkTextBuffer
3778  * @clipboard: the #GtkClipboard to paste from
3779  * @override_location: (allow-none): location to insert pasted text, or %NULL for
3780  *                     at the cursor
3781  * @default_editable: whether the buffer is editable by default
3782  *
3783  * Pastes the contents of a clipboard at the insertion point, or
3784  * at @override_location. (Note: pasting is asynchronous, that is,
3785  * we'll ask for the paste data and return, and at some point later
3786  * after the main loop runs, the paste data will be inserted.)
3787  **/
3788 void
3789 gtk_text_buffer_paste_clipboard (GtkTextBuffer *buffer,
3790                                  GtkClipboard  *clipboard,
3791                                  GtkTextIter   *override_location,
3792                                  gboolean       default_editable)
3793 {
3794   ClipboardRequest *data = g_new (ClipboardRequest, 1);
3795   GtkTextIter paste_point;
3796   GtkTextIter start, end;
3797
3798   if (override_location != NULL)
3799     gtk_text_buffer_create_mark (buffer,
3800                                  "gtk_paste_point_override",
3801                                  override_location, FALSE);
3802
3803   data->buffer = g_object_ref (buffer);
3804   data->interactive = TRUE;
3805   data->default_editable = !!default_editable;
3806
3807   /* When pasting with the cursor inside the selection area, you
3808    * replace the selection with the new text, otherwise, you
3809    * simply insert the new text at the point where the click
3810    * occured, unselecting any selected text. The replace_selection
3811    * flag toggles this behavior.
3812    */
3813   data->replace_selection = FALSE;
3814   
3815   get_paste_point (buffer, &paste_point, FALSE);
3816   if (gtk_text_buffer_get_selection_bounds (buffer, &start, &end) &&
3817       (gtk_text_iter_in_range (&paste_point, &start, &end) ||
3818        gtk_text_iter_equal (&paste_point, &end)))
3819     data->replace_selection = TRUE;
3820
3821   gtk_clipboard_request_contents (clipboard,
3822                                   gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
3823                                   clipboard_clipboard_buffer_received, data);
3824 }
3825
3826 /**
3827  * gtk_text_buffer_delete_selection:
3828  * @buffer: a #GtkTextBuffer 
3829  * @interactive: whether the deletion is caused by user interaction
3830  * @default_editable: whether the buffer is editable by default
3831  *
3832  * Deletes the range between the "insert" and "selection_bound" marks,
3833  * that is, the currently-selected text. If @interactive is %TRUE,
3834  * the editability of the selection will be considered (users can't delete
3835  * uneditable text).
3836  * 
3837  * Return value: whether there was a non-empty selection to delete
3838  **/
3839 gboolean
3840 gtk_text_buffer_delete_selection (GtkTextBuffer *buffer,
3841                                   gboolean interactive,
3842                                   gboolean default_editable)
3843 {
3844   GtkTextIter start;
3845   GtkTextIter end;
3846
3847   if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3848     {
3849       return FALSE; /* No selection */
3850     }
3851   else
3852     {
3853       if (interactive)
3854         gtk_text_buffer_delete_interactive (buffer, &start, &end, default_editable);
3855       else
3856         gtk_text_buffer_delete (buffer, &start, &end);
3857
3858       return TRUE; /* We deleted stuff */
3859     }
3860 }
3861
3862 /**
3863  * gtk_text_buffer_backspace:
3864  * @buffer: a #GtkTextBuffer
3865  * @iter: a position in @buffer
3866  * @interactive: whether the deletion is caused by user interaction
3867  * @default_editable: whether the buffer is editable by default
3868  * 
3869  * Performs the appropriate action as if the user hit the delete
3870  * key with the cursor at the position specified by @iter. In the
3871  * normal case a single character will be deleted, but when
3872  * combining accents are involved, more than one character can
3873  * be deleted, and when precomposed character and accent combinations
3874  * are involved, less than one character will be deleted.
3875  * 
3876  * Because the buffer is modified, all outstanding iterators become 
3877  * invalid after calling this function; however, the @iter will be
3878  * re-initialized to point to the location where text was deleted. 
3879  *
3880  * Return value: %TRUE if the buffer was modified
3881  *
3882  * Since: 2.6
3883  **/
3884 gboolean
3885 gtk_text_buffer_backspace (GtkTextBuffer *buffer,
3886                            GtkTextIter   *iter,
3887                            gboolean       interactive,
3888                            gboolean       default_editable)
3889 {
3890   gchar *cluster_text;
3891   GtkTextIter start;
3892   GtkTextIter end;
3893   gboolean retval = FALSE;
3894   const PangoLogAttr *attrs;
3895   int offset;
3896   gboolean backspace_deletes_character;
3897
3898   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
3899   g_return_val_if_fail (iter != NULL, FALSE);
3900
3901   start = *iter;
3902   end = *iter;
3903
3904   attrs = _gtk_text_buffer_get_line_log_attrs (buffer, &start, NULL);
3905
3906   /* For no good reason, attrs is NULL for the empty last line in
3907    * a buffer. Special case that here. (#156164)
3908    */
3909   if (attrs)
3910     {
3911       offset = gtk_text_iter_get_line_offset (&start);
3912       backspace_deletes_character = attrs[offset].backspace_deletes_character;
3913     }
3914   else
3915     backspace_deletes_character = FALSE;
3916
3917   gtk_text_iter_backward_cursor_position (&start);
3918
3919   if (gtk_text_iter_equal (&start, &end))
3920     return FALSE;
3921     
3922   cluster_text = gtk_text_iter_get_text (&start, &end);
3923
3924   if (interactive)
3925     gtk_text_buffer_begin_user_action (buffer);
3926   
3927   if (gtk_text_buffer_delete_interactive (buffer, &start, &end,
3928                                           default_editable))
3929     {
3930       /* special case \r\n, since we never want to reinsert \r */
3931       if (backspace_deletes_character && strcmp ("\r\n", cluster_text))
3932         {
3933           gchar *normalized_text = g_utf8_normalize (cluster_text,
3934                                                      strlen (cluster_text),
3935                                                      G_NORMALIZE_NFD);
3936           glong len = g_utf8_strlen (normalized_text, -1);
3937
3938           if (len > 1)
3939             gtk_text_buffer_insert_interactive (buffer,
3940                                                 &start,
3941                                                 normalized_text,
3942                                                 g_utf8_offset_to_pointer (normalized_text, len - 1) - normalized_text,
3943                                                 default_editable);
3944           
3945           g_free (normalized_text);
3946         }
3947
3948       retval = TRUE;
3949     }
3950   
3951   if (interactive)
3952     gtk_text_buffer_end_user_action (buffer);
3953   
3954   g_free (cluster_text);
3955
3956   /* Revalidate the users iter */
3957   *iter = start;
3958
3959   return retval;
3960 }
3961
3962 static void
3963 cut_or_copy (GtkTextBuffer *buffer,
3964              GtkClipboard  *clipboard,
3965              gboolean       delete_region_after,
3966              gboolean       interactive,
3967              gboolean       default_editable)
3968 {
3969   GtkTextBufferPrivate *priv;
3970
3971   /* We prefer to cut the selected region between selection_bound and
3972    * insertion point. If that region is empty, then we cut the region
3973    * between the "anchor" and the insertion point (this is for
3974    * C-space and M-w and other Emacs-style copy/yank keys). Note that
3975    * insert and selection_bound are guaranteed to exist, but the
3976    * anchor only exists sometimes.
3977    */
3978   GtkTextIter start;
3979   GtkTextIter end;
3980
3981   priv = buffer->priv;
3982
3983   gtk_text_buffer_get_copy_target_list (buffer);
3984
3985   if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
3986     {
3987       /* Let's try the anchor thing */
3988       GtkTextMark * anchor = gtk_text_buffer_get_mark (buffer, "anchor");
3989
3990       if (anchor == NULL)
3991         return;
3992       else
3993         {
3994           gtk_text_buffer_get_iter_at_mark (buffer, &end, anchor);
3995           gtk_text_iter_order (&start, &end);
3996         }
3997     }
3998
3999   if (!gtk_text_iter_equal (&start, &end))
4000     {
4001       GtkTextIter ins;
4002       GtkTextBuffer *contents;
4003
4004       contents = create_clipboard_contents_buffer (buffer);
4005
4006       gtk_text_buffer_get_iter_at_offset (contents, &ins, 0);
4007       
4008       gtk_text_buffer_insert_range (contents, &ins, &start, &end);
4009                                     
4010       if (!gtk_clipboard_set_with_data (clipboard,
4011                                         priv->copy_target_entries,
4012                                         priv->n_copy_target_entries,
4013                                         clipboard_get_contents_cb,
4014                                         clipboard_clear_contents_cb,
4015                                         contents))
4016         g_object_unref (contents);
4017       else
4018         gtk_clipboard_set_can_store (clipboard,
4019                                      priv->copy_target_entries + 1,
4020                                      priv->n_copy_target_entries - 1);
4021
4022       if (delete_region_after)
4023         {
4024           if (interactive)
4025             gtk_text_buffer_delete_interactive (buffer, &start, &end,
4026                                                 default_editable);
4027           else
4028             gtk_text_buffer_delete (buffer, &start, &end);
4029         }
4030     }
4031 }
4032
4033 /**
4034  * gtk_text_buffer_cut_clipboard:
4035  * @buffer: a #GtkTextBuffer
4036  * @clipboard: the #GtkClipboard object to cut to
4037  * @default_editable: default editability of the buffer
4038  *
4039  * Copies the currently-selected text to a clipboard, then deletes
4040  * said text if it's editable.
4041  **/
4042 void
4043 gtk_text_buffer_cut_clipboard (GtkTextBuffer *buffer,
4044                                GtkClipboard  *clipboard,
4045                                gboolean       default_editable)
4046 {
4047   gtk_text_buffer_begin_user_action (buffer);
4048   cut_or_copy (buffer, clipboard, TRUE, TRUE, default_editable);
4049   gtk_text_buffer_end_user_action (buffer);
4050 }
4051
4052 /**
4053  * gtk_text_buffer_copy_clipboard:
4054  * @buffer: a #GtkTextBuffer 
4055  * @clipboard: the #GtkClipboard object to copy to
4056  *
4057  * Copies the currently-selected text to a clipboard.
4058  **/
4059 void
4060 gtk_text_buffer_copy_clipboard (GtkTextBuffer *buffer,
4061                                 GtkClipboard  *clipboard)
4062 {
4063   cut_or_copy (buffer, clipboard, FALSE, TRUE, TRUE);
4064 }
4065
4066 /**
4067  * gtk_text_buffer_get_selection_bounds:
4068  * @buffer: a #GtkTextBuffer a #GtkTextBuffer
4069  * @start: (out): iterator to initialize with selection start
4070  * @end: (out): iterator to initialize with selection end
4071  *
4072  * Returns %TRUE if some text is selected; places the bounds
4073  * of the selection in @start and @end (if the selection has length 0,
4074  * then @start and @end are filled in with the same value).
4075  * @start and @end will be in ascending order. If @start and @end are
4076  * NULL, then they are not filled in, but the return value still indicates
4077  * whether text is selected.
4078  *
4079  * Return value: whether the selection has nonzero length
4080  **/
4081 gboolean
4082 gtk_text_buffer_get_selection_bounds (GtkTextBuffer *buffer,
4083                                       GtkTextIter   *start,
4084                                       GtkTextIter   *end)
4085 {
4086   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
4087
4088   return _gtk_text_btree_get_selection_bounds (get_btree (buffer), start, end);
4089 }
4090
4091 /**
4092  * gtk_text_buffer_begin_user_action:
4093  * @buffer: a #GtkTextBuffer
4094  * 
4095  * Called to indicate that the buffer operations between here and a
4096  * call to gtk_text_buffer_end_user_action() are part of a single
4097  * user-visible operation. The operations between
4098  * gtk_text_buffer_begin_user_action() and
4099  * gtk_text_buffer_end_user_action() can then be grouped when creating
4100  * an undo stack. #GtkTextBuffer maintains a count of calls to
4101  * gtk_text_buffer_begin_user_action() that have not been closed with
4102  * a call to gtk_text_buffer_end_user_action(), and emits the 
4103  * "begin-user-action" and "end-user-action" signals only for the 
4104  * outermost pair of calls. This allows you to build user actions 
4105  * from other user actions.
4106  *
4107  * The "interactive" buffer mutation functions, such as
4108  * gtk_text_buffer_insert_interactive(), automatically call begin/end
4109  * user action around the buffer operations they perform, so there's
4110  * no need to add extra calls if you user action consists solely of a
4111  * single call to one of those functions.
4112  **/
4113 void
4114 gtk_text_buffer_begin_user_action (GtkTextBuffer *buffer)
4115 {
4116   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4117
4118   buffer->priv->user_action_count += 1;
4119   
4120   if (buffer->priv->user_action_count == 1)
4121     {
4122       /* Outermost nested user action begin emits the signal */
4123       g_signal_emit (buffer, signals[BEGIN_USER_ACTION], 0);
4124     }
4125 }
4126
4127 /**
4128  * gtk_text_buffer_end_user_action:
4129  * @buffer: a #GtkTextBuffer
4130  * 
4131  * Should be paired with a call to gtk_text_buffer_begin_user_action().
4132  * See that function for a full explanation.
4133  **/
4134 void
4135 gtk_text_buffer_end_user_action (GtkTextBuffer *buffer)
4136 {
4137   g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
4138   g_return_if_fail (buffer->priv->user_action_count > 0);
4139   
4140   buffer->priv->user_action_count -= 1;
4141   
4142   if (buffer->priv->user_action_count == 0)
4143     {
4144       /* Ended the outermost-nested user action end, so emit the signal */
4145       g_signal_emit (buffer, signals[END_USER_ACTION], 0);
4146     }
4147 }
4148
4149 static void
4150 gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer)
4151 {
4152   GtkTextBufferPrivate *priv = buffer->priv;
4153
4154   if (priv->copy_target_list)
4155     {
4156       gtk_target_list_unref (priv->copy_target_list);
4157       priv->copy_target_list = NULL;
4158
4159       gtk_target_table_free (priv->copy_target_entries,
4160                              priv->n_copy_target_entries);
4161       priv->copy_target_entries = NULL;
4162       priv->n_copy_target_entries = 0;
4163     }
4164
4165   if (priv->paste_target_list)
4166     {
4167       gtk_target_list_unref (priv->paste_target_list);
4168       priv->paste_target_list = NULL;
4169
4170       gtk_target_table_free (priv->paste_target_entries,
4171                              priv->n_paste_target_entries);
4172       priv->paste_target_entries = NULL;
4173       priv->n_paste_target_entries = 0;
4174     }
4175 }
4176
4177 static GtkTargetList *
4178 gtk_text_buffer_get_target_list (GtkTextBuffer   *buffer,
4179                                  gboolean         deserializable,
4180                                  GtkTargetEntry **entries,
4181                                  gint            *n_entries)
4182 {
4183   GtkTargetList *target_list;
4184
4185   target_list = gtk_target_list_new (NULL, 0);
4186
4187   gtk_target_list_add (target_list,
4188                        gdk_atom_intern_static_string ("GTK_TEXT_BUFFER_CONTENTS"),
4189                        GTK_TARGET_SAME_APP,
4190                        GTK_TEXT_BUFFER_TARGET_INFO_BUFFER_CONTENTS);
4191
4192   gtk_target_list_add_rich_text_targets (target_list,
4193                                          GTK_TEXT_BUFFER_TARGET_INFO_RICH_TEXT,
4194                                          deserializable,
4195                                          buffer);
4196
4197   gtk_target_list_add_text_targets (target_list,
4198                                     GTK_TEXT_BUFFER_TARGET_INFO_TEXT);
4199
4200   *entries = gtk_target_table_new_from_list (target_list, n_entries);
4201
4202   return target_list;
4203 }
4204
4205 /**
4206  * gtk_text_buffer_get_copy_target_list:
4207  * @buffer: a #GtkTextBuffer
4208  *
4209  * This function returns the list of targets this text buffer can
4210  * provide for copying and as DND source. The targets in the list are
4211  * added with %info values from the #GtkTextBufferTargetInfo enum,
4212  * using gtk_target_list_add_rich_text_targets() and
4213  * gtk_target_list_add_text_targets().
4214  *
4215  * Return value: (transfer none): the #GtkTargetList
4216  *
4217  * Since: 2.10
4218  **/
4219 GtkTargetList *
4220 gtk_text_buffer_get_copy_target_list (GtkTextBuffer *buffer)
4221 {
4222   GtkTextBufferPrivate *priv;
4223
4224   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4225
4226   priv = buffer->priv;
4227
4228   if (! priv->copy_target_list)
4229     priv->copy_target_list =
4230       gtk_text_buffer_get_target_list (buffer, FALSE,
4231                                        &priv->copy_target_entries,
4232                                        &priv->n_copy_target_entries);
4233
4234   return priv->copy_target_list;
4235 }
4236
4237 /**
4238  * gtk_text_buffer_get_paste_target_list:
4239  * @buffer: a #GtkTextBuffer
4240  *
4241  * This function returns the list of targets this text buffer supports
4242  * for pasting and as DND destination. The targets in the list are
4243  * added with %info values from the #GtkTextBufferTargetInfo enum,
4244  * using gtk_target_list_add_rich_text_targets() and
4245  * gtk_target_list_add_text_targets().
4246  *
4247  * Return value: (transfer none): the #GtkTargetList
4248  *
4249  * Since: 2.10
4250  **/
4251 GtkTargetList *
4252 gtk_text_buffer_get_paste_target_list (GtkTextBuffer *buffer)
4253 {
4254   GtkTextBufferPrivate *priv;
4255
4256   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4257
4258   priv = buffer->priv;
4259
4260   if (! priv->paste_target_list)
4261     priv->paste_target_list =
4262       gtk_text_buffer_get_target_list (buffer, TRUE,
4263                                        &priv->paste_target_entries,
4264                                        &priv->n_paste_target_entries);
4265
4266   return priv->paste_target_list;
4267 }
4268
4269 /*
4270  * Logical attribute cache
4271  */
4272
4273 #define ATTR_CACHE_SIZE 2
4274
4275 typedef struct _CacheEntry CacheEntry;
4276 struct _CacheEntry
4277 {
4278   gint line;
4279   gint char_len;
4280   PangoLogAttr *attrs;
4281 };
4282
4283 struct _GtkTextLogAttrCache
4284 {
4285   gint chars_changed_stamp;
4286   CacheEntry entries[ATTR_CACHE_SIZE];
4287 };
4288
4289 static void
4290 free_log_attr_cache (GtkTextLogAttrCache *cache)
4291 {
4292   gint i = 0;
4293   while (i < ATTR_CACHE_SIZE)
4294     {
4295       g_free (cache->entries[i].attrs);
4296       ++i;
4297     }
4298   g_free (cache);
4299 }
4300
4301 static void
4302 clear_log_attr_cache (GtkTextLogAttrCache *cache)
4303 {
4304   gint i = 0;
4305   while (i < ATTR_CACHE_SIZE)
4306     {
4307       g_free (cache->entries[i].attrs);
4308       cache->entries[i].attrs = NULL;
4309       ++i;
4310     }
4311 }
4312
4313 static PangoLogAttr*
4314 compute_log_attrs (const GtkTextIter *iter,
4315                    gint              *char_lenp)
4316 {
4317   GtkTextIter start;
4318   GtkTextIter end;
4319   gchar *paragraph;
4320   gint char_len, byte_len;
4321   PangoLogAttr *attrs = NULL;
4322   
4323   start = *iter;
4324   end = *iter;
4325
4326   gtk_text_iter_set_line_offset (&start, 0);
4327   gtk_text_iter_forward_line (&end);
4328
4329   paragraph = gtk_text_iter_get_slice (&start, &end);
4330   char_len = g_utf8_strlen (paragraph, -1);
4331   byte_len = strlen (paragraph);
4332
4333   g_assert (char_len > 0);
4334
4335   if (char_lenp)
4336     *char_lenp = char_len;
4337   
4338   attrs = g_new (PangoLogAttr, char_len + 1);
4339
4340   /* FIXME we need to follow PangoLayout and allow different language
4341    * tags within the paragraph
4342    */
4343   pango_get_log_attrs (paragraph, byte_len, -1,
4344                        gtk_text_iter_get_language (&start),
4345                        attrs,
4346                        char_len + 1);
4347   
4348   g_free (paragraph);
4349
4350   return attrs;
4351 }
4352
4353 /* The return value from this is valid until you call this a second time.
4354  */
4355 const PangoLogAttr*
4356 _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer     *buffer,
4357                                      const GtkTextIter *anywhere_in_line,
4358                                      gint              *char_len)
4359 {
4360   GtkTextBufferPrivate *priv;
4361   gint line;
4362   GtkTextLogAttrCache *cache;
4363   gint i;
4364   
4365   g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
4366   g_return_val_if_fail (anywhere_in_line != NULL, NULL);
4367
4368   priv = buffer->priv;
4369
4370   /* special-case for empty last line in buffer */
4371   if (gtk_text_iter_is_end (anywhere_in_line) &&
4372       gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
4373     {
4374       if (char_len)
4375         *char_len = 0;
4376       return NULL;
4377     }
4378   
4379   /* FIXME we also need to recompute log attrs if the language tag at
4380    * the start of a paragraph changes
4381    */
4382   
4383   if (priv->log_attr_cache == NULL)
4384     {
4385       priv->log_attr_cache = g_new0 (GtkTextLogAttrCache, 1);
4386       priv->log_attr_cache->chars_changed_stamp =
4387         _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer));
4388     }
4389   else if (priv->log_attr_cache->chars_changed_stamp !=
4390            _gtk_text_btree_get_chars_changed_stamp (get_btree (buffer)))
4391     {
4392       clear_log_attr_cache (priv->log_attr_cache);
4393     }
4394   
4395   cache = priv->log_attr_cache;
4396   line = gtk_text_iter_get_line (anywhere_in_line);
4397
4398   i = 0;
4399   while (i < ATTR_CACHE_SIZE)
4400     {
4401       if (cache->entries[i].attrs &&
4402           cache->entries[i].line == line)
4403         {
4404           if (char_len)
4405             *char_len = cache->entries[i].char_len;
4406           return cache->entries[i].attrs;
4407         }
4408       ++i;
4409     }
4410   
4411   /* Not in cache; open up the first cache entry */
4412   g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
4413   
4414   g_memmove (cache->entries + 1, cache->entries,
4415              sizeof (CacheEntry) * (ATTR_CACHE_SIZE - 1));
4416
4417   cache->entries[0].line = line;
4418   cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
4419                                                &cache->entries[0].char_len);
4420
4421   if (char_len)
4422     *char_len = cache->entries[0].char_len;
4423   
4424   return cache->entries[0].attrs;
4425 }
4426
4427 void
4428 _gtk_text_buffer_notify_will_remove_tag (GtkTextBuffer *buffer,
4429                                          GtkTextTag    *tag)
4430 {
4431   /* This removes tag from the buffer, but DOESN'T emit the
4432    * remove-tag signal, because we can't afford to have user
4433    * code messing things up at this point; the tag MUST be removed
4434    * entirely.
4435    */
4436   if (buffer->priv->btree)
4437     _gtk_text_btree_notify_will_remove_tag (buffer->priv->btree, tag);
4438 }
4439
4440 /*
4441  * Debug spew
4442  */
4443
4444 void
4445 _gtk_text_buffer_spew (GtkTextBuffer *buffer)
4446 {
4447   _gtk_text_btree_spew (get_btree (buffer));
4448 }
4449
4450 void
4451 _gtk_text_buffer_get_text_before (GtkTextBuffer   *buffer,
4452                                   AtkTextBoundary  boundary_type,
4453                                   GtkTextIter     *position,
4454                                   GtkTextIter     *start,
4455                                   GtkTextIter     *end)
4456 {
4457   gint line_number;
4458
4459   *start = *position;
4460   *end = *start;
4461
4462   switch (boundary_type)
4463     {
4464     case ATK_TEXT_BOUNDARY_CHAR:
4465       gtk_text_iter_backward_char (start);
4466       break;
4467
4468     case ATK_TEXT_BOUNDARY_WORD_START:
4469       if (!gtk_text_iter_starts_word (start))
4470         gtk_text_iter_backward_word_start (start);
4471       *end = *start;
4472       gtk_text_iter_backward_word_start (start);
4473       break;
4474
4475     case ATK_TEXT_BOUNDARY_WORD_END:
4476       if (gtk_text_iter_inside_word (start) &&
4477           !gtk_text_iter_starts_word (start))
4478         gtk_text_iter_backward_word_start (start);
4479       while (!gtk_text_iter_ends_word (start))
4480         {
4481           if (!gtk_text_iter_backward_char (start))
4482             break;
4483         }
4484       *end = *start;
4485       gtk_text_iter_backward_word_start (start);
4486       while (!gtk_text_iter_ends_word (start))
4487         {
4488           if (!gtk_text_iter_backward_char (start))
4489             break;
4490         }
4491       break;
4492
4493     case ATK_TEXT_BOUNDARY_SENTENCE_START:
4494       if (!gtk_text_iter_starts_sentence (start))
4495         gtk_text_iter_backward_sentence_start (start);
4496       *end = *start;
4497       gtk_text_iter_backward_sentence_start (start);
4498       break;
4499
4500     case ATK_TEXT_BOUNDARY_SENTENCE_END:
4501       if (gtk_text_iter_inside_sentence (start) &&
4502           !gtk_text_iter_starts_sentence (start))
4503         gtk_text_iter_backward_sentence_start (start);
4504       while (!gtk_text_iter_ends_sentence (start))
4505         {
4506           if (!gtk_text_iter_backward_char (start))
4507             break;
4508         }
4509       *end = *start;
4510       gtk_text_iter_backward_sentence_start (start);
4511       while (!gtk_text_iter_ends_sentence (start))
4512         {
4513           if (!gtk_text_iter_backward_char (start))
4514             break;
4515         }
4516       break;
4517
4518     case ATK_TEXT_BOUNDARY_LINE_START:
4519       line_number = gtk_text_iter_get_line (start);
4520       if (line_number == 0)
4521         {
4522           gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4523         }
4524       else
4525         {
4526           gtk_text_iter_backward_line (start);
4527           gtk_text_iter_forward_line (start);
4528         }
4529       *end = *start;
4530       gtk_text_iter_backward_line (start);
4531       break;
4532
4533     case ATK_TEXT_BOUNDARY_LINE_END:
4534       line_number = gtk_text_iter_get_line (start);
4535       if (line_number == 0)
4536         {
4537           gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4538           *end = *start;
4539         }
4540       else
4541         {
4542           gtk_text_iter_backward_line (start);
4543           *end = *start;
4544           while (!gtk_text_iter_ends_line (start))
4545             {
4546               if (!gtk_text_iter_backward_char (start))
4547                 break;
4548             }
4549           gtk_text_iter_forward_to_line_end (end);
4550         }
4551       break;
4552     }
4553 }
4554
4555 void
4556 _gtk_text_buffer_get_text_at (GtkTextBuffer   *buffer,
4557                               AtkTextBoundary  boundary_type,
4558                               GtkTextIter     *position,
4559                               GtkTextIter     *start,
4560                               GtkTextIter     *end)
4561 {
4562   gint line_number;
4563
4564   *start = *position;
4565   *end = *start;
4566
4567   switch (boundary_type)
4568     {
4569     case ATK_TEXT_BOUNDARY_CHAR:
4570       gtk_text_iter_forward_char (end);
4571       break;
4572
4573     case ATK_TEXT_BOUNDARY_WORD_START:
4574       if (!gtk_text_iter_starts_word (start))
4575         gtk_text_iter_backward_word_start (start);
4576       if (gtk_text_iter_inside_word (end))
4577         gtk_text_iter_forward_word_end (end);
4578       while (!gtk_text_iter_starts_word (end))
4579         {
4580           if (!gtk_text_iter_forward_char (end))
4581             break;
4582         }
4583       break;
4584
4585     case ATK_TEXT_BOUNDARY_WORD_END:
4586       if (gtk_text_iter_inside_word (start) &&
4587           !gtk_text_iter_starts_word (start))
4588         gtk_text_iter_backward_word_start (start);
4589       while (!gtk_text_iter_ends_word (start))
4590         {
4591           if (!gtk_text_iter_backward_char (start))
4592             break;
4593         }
4594       gtk_text_iter_forward_word_end (end);
4595       break;
4596
4597     case ATK_TEXT_BOUNDARY_SENTENCE_START:
4598       if (!gtk_text_iter_starts_sentence (start))
4599         gtk_text_iter_backward_sentence_start (start);
4600       if (gtk_text_iter_inside_sentence (end))
4601         gtk_text_iter_forward_sentence_end (end);
4602       while (!gtk_text_iter_starts_sentence (end))
4603         {
4604           if (!gtk_text_iter_forward_char (end))
4605             break;
4606         }
4607       break;
4608
4609     case ATK_TEXT_BOUNDARY_SENTENCE_END:
4610       if (gtk_text_iter_inside_sentence (start) &&
4611           !gtk_text_iter_starts_sentence (start))
4612         gtk_text_iter_backward_sentence_start (start);
4613       while (!gtk_text_iter_ends_sentence (start))
4614         {
4615           if (!gtk_text_iter_backward_char (start))
4616             break;
4617         }
4618       gtk_text_iter_forward_sentence_end (end);
4619       break;
4620
4621     case ATK_TEXT_BOUNDARY_LINE_START:
4622       line_number = gtk_text_iter_get_line (start);
4623       if (line_number == 0)
4624         {
4625           gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4626         }
4627       else
4628         {
4629           gtk_text_iter_backward_line (start);
4630           gtk_text_iter_forward_line (start);
4631         }
4632       gtk_text_iter_forward_line (end);
4633       break;
4634
4635     case ATK_TEXT_BOUNDARY_LINE_END:
4636       line_number = gtk_text_iter_get_line (start);
4637       if (line_number == 0)
4638         {
4639           gtk_text_buffer_get_iter_at_offset (buffer, start, 0);
4640         }
4641       else
4642         {
4643           gtk_text_iter_backward_line (start);
4644           gtk_text_iter_forward_line (start);
4645         }
4646       while (!gtk_text_iter_ends_line (start))
4647         {
4648           if (!gtk_text_iter_backward_char (start))
4649             break;
4650         }
4651       gtk_text_iter_forward_to_line_end (end);
4652       break;
4653    }
4654 }
4655
4656 void
4657 _gtk_text_buffer_get_text_after (GtkTextBuffer   *buffer,
4658                                  AtkTextBoundary  boundary_type,
4659                                  GtkTextIter     *position,
4660                                  GtkTextIter     *start,
4661                                  GtkTextIter     *end)
4662 {
4663   *start = *position;
4664   *end = *start;
4665
4666   switch (boundary_type)
4667     {
4668     case ATK_TEXT_BOUNDARY_CHAR:
4669       gtk_text_iter_forward_char (start);
4670       gtk_text_iter_forward_chars (end, 2);
4671       break;
4672
4673     case ATK_TEXT_BOUNDARY_WORD_START:
4674       if (gtk_text_iter_inside_word (end))
4675         gtk_text_iter_forward_word_end (end);
4676       while (!gtk_text_iter_starts_word (end))
4677         {
4678           if (!gtk_text_iter_forward_char (end))
4679             break;
4680         }
4681       *start = *end;
4682       if (!gtk_text_iter_is_end (end))
4683         {
4684           gtk_text_iter_forward_word_end (end);
4685           while (!gtk_text_iter_starts_word (end))
4686             {
4687               if (!gtk_text_iter_forward_char (end))
4688                 break;
4689             }
4690         }
4691       break;
4692
4693     case ATK_TEXT_BOUNDARY_WORD_END:
4694       gtk_text_iter_forward_word_end (end);
4695       *start = *end;
4696       if (!gtk_text_iter_is_end (end))
4697         gtk_text_iter_forward_word_end (end);
4698       break;
4699
4700     case ATK_TEXT_BOUNDARY_SENTENCE_START:
4701       if (gtk_text_iter_inside_sentence (end))
4702         gtk_text_iter_forward_sentence_end (end);
4703       while (!gtk_text_iter_starts_sentence (end))
4704         {
4705           if (!gtk_text_iter_forward_char (end))
4706             break;
4707         }
4708       *start = *end;
4709       if (!gtk_text_iter_is_end (end))
4710         {
4711           gtk_text_iter_forward_sentence_end (end);
4712           while (!gtk_text_iter_starts_sentence (end))
4713             {
4714               if (!gtk_text_iter_forward_char (end))
4715                 break;
4716             }
4717         }
4718       break;
4719
4720     case ATK_TEXT_BOUNDARY_SENTENCE_END:
4721       gtk_text_iter_forward_sentence_end (end);
4722       *start = *end;
4723       if (!gtk_text_iter_is_end (end))
4724         gtk_text_iter_forward_sentence_end (end);
4725       break;
4726
4727     case ATK_TEXT_BOUNDARY_LINE_START:
4728       gtk_text_iter_forward_line (end);
4729       *start = *end;
4730       gtk_text_iter_forward_line (end);
4731       break;
4732
4733     case ATK_TEXT_BOUNDARY_LINE_END:
4734       gtk_text_iter_forward_line (start);
4735       *end = *start;
4736       if (!gtk_text_iter_is_end (start))
4737         {
4738           while (!gtk_text_iter_ends_line (start))
4739             {
4740               if (!gtk_text_iter_backward_char (start))
4741                 break;
4742             }
4743           gtk_text_iter_forward_to_line_end (end);
4744         }
4745       break;
4746     }
4747 }