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