]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextiter.c
Fix bug #486369, by passing the selection info along when pasting.
[~andy/gtk] / gtk / gtktextiter.c
index 241529d389564dc10d6b74250ad82fee9cfc98ae..715f7e596496632f3a87bf52e8b0c80105c2cdc2 100644 (file)
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
  */
 
+#define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
+#include <config.h>
 #include "gtktextiter.h"
 #include "gtktextbtree.h"
 #include "gtktextiterprivate.h"
+#include "gtkintl.h"
 #include "gtkdebug.h"
+#include "gtkalias.h"
 #include <string.h>
-#include <ctype.h>
+
+#define FIX_OVERFLOWS(varname) if ((varname) == G_MININT) (varname) = G_MININT + 1
 
 typedef struct _GtkTextRealIter GtkTextRealIter;
 
@@ -62,6 +67,10 @@ struct _GtkTextRealIter
      and ditto for char offsets. */
   gint segment_byte_offset;
   gint segment_char_offset;
+
+  /* padding */
+  gint pad1;
+  gpointer pad2;
 };
 
 /* These "set" functions should not assume any fields
@@ -291,24 +300,12 @@ iter_init_from_char_offset (GtkTextIter *iter,
   return real;
 }
 
-static inline void
-invalidate_segment (GtkTextRealIter *iter)
-{
-  iter->segments_changed_stamp -= 1;
-}
-
 static inline void
 invalidate_char_index (GtkTextRealIter *iter)
 {
   iter->cached_char_index = -1;
 }
 
-static inline void
-invalidate_line_number (GtkTextRealIter *iter)
-{
-  iter->cached_line_number = -1;
-}
-
 static inline void
 adjust_char_index (GtkTextRealIter *iter, gint count)
 {
@@ -323,28 +320,6 @@ adjust_line_number (GtkTextRealIter *iter, gint count)
     iter->cached_line_number += count;
 }
 
-static inline void
-adjust_char_offsets (GtkTextRealIter *iter, gint count)
-{
-  if (iter->line_char_offset >= 0)
-    {
-      iter->line_char_offset += count;
-      g_assert (iter->segment_char_offset >= 0);
-      iter->segment_char_offset += count;
-    }
-}
-
-static inline void
-adjust_byte_offsets (GtkTextRealIter *iter, gint count)
-{
-  if (iter->line_byte_offset >= 0)
-    {
-      iter->line_byte_offset += count;
-      g_assert (iter->segment_byte_offset >= 0);
-      iter->segment_byte_offset += count;
-    }
-}
-
 static inline void
 ensure_char_offsets (GtkTextRealIter *iter)
 {
@@ -379,7 +354,7 @@ is_segment_start (GtkTextRealIter *real)
   return real->segment_byte_offset == 0 || real->segment_char_offset == 0;
 }
 
-#if 1
+#ifdef G_ENABLE_DEBUG
 static void
 check_invariants (const GtkTextIter *iter)
 {
@@ -387,14 +362,14 @@ check_invariants (const GtkTextIter *iter)
     _gtk_text_iter_check (iter);
 }
 #else
-#define check_invariants (x)
+#define check_invariants(x)
 #endif
 
 /**
  * gtk_text_iter_get_buffer:
  * @iter: an iterator
  *
- * Return the #GtkTextBuffer this iterator is associated with
+ * Returns the #GtkTextBuffer this iterator is associated with.
  *
  * Return value: the buffer
  **/
@@ -419,7 +394,7 @@ gtk_text_iter_get_buffer (const GtkTextIter *iter)
  * gtk_text_iter_copy:
  * @iter: an iterator
  *
- * Create a dynamically-allocated copy of an iterator. This function
+ * Creates a dynamically-allocated copy of an iterator. This function
  * is not useful in applications, because iterators can be copied with a
  * simple assignment (<literal>GtkTextIter i = j;</literal>). The
  * function is used by language bindings.
@@ -433,7 +408,7 @@ gtk_text_iter_copy (const GtkTextIter *iter)
 
   g_return_val_if_fail (iter != NULL, NULL);
 
-  new_iter = g_new (GtkTextIter, 1);
+  new_iter = g_slice_new (GtkTextIter);
 
   *new_iter = *iter;
 
@@ -448,14 +423,26 @@ gtk_text_iter_copy (const GtkTextIter *iter)
  * is intended for use in language bindings, and is not
  * especially useful for applications, because iterators can
  * simply be allocated on the stack.
- *
  **/
 void
 gtk_text_iter_free (GtkTextIter *iter)
 {
   g_return_if_fail (iter != NULL);
 
-  g_free (iter);
+  g_slice_free (GtkTextIter, iter);
+}
+
+GType
+gtk_text_iter_get_type (void)
+{
+  static GType our_type = 0;
+  
+  if (our_type == 0)
+    our_type = g_boxed_type_register_static (I_("GtkTextIter"),
+                                            (GBoxedCopyFunc) gtk_text_iter_copy,
+                                            (GBoxedFreeFunc) gtk_text_iter_free);
+
+  return our_type;
 }
 
 GtkTextLineSegment*
@@ -463,7 +450,7 @@ _gtk_text_iter_get_indexable_segment (const GtkTextIter *iter)
 {
   GtkTextRealIter *real;
 
-  g_return_val_if_fail (iter != NULL, 0);
+  g_return_val_if_fail (iter != NULL, NULL);
 
   real = gtk_text_iter_make_real (iter);
 
@@ -482,7 +469,7 @@ _gtk_text_iter_get_any_segment (const GtkTextIter *iter)
 {
   GtkTextRealIter *real;
 
-  g_return_val_if_fail (iter != NULL, 0);
+  g_return_val_if_fail (iter != NULL, NULL);
 
   real = gtk_text_iter_make_real (iter);
 
@@ -541,7 +528,7 @@ _gtk_text_iter_get_text_line (const GtkTextIter *iter)
 {
   const GtkTextRealIter *real;
 
-  g_return_val_if_fail (iter != NULL, 0);
+  g_return_val_if_fail (iter != NULL, NULL);
 
   real = (const GtkTextRealIter*)iter;
 
@@ -555,7 +542,7 @@ _gtk_text_iter_get_btree (const GtkTextIter *iter)
 {
   const GtkTextRealIter *real;
 
-  g_return_val_if_fail (iter != NULL, 0);
+  g_return_val_if_fail (iter != NULL, NULL);
 
   real = (const GtkTextRealIter*)iter;
 
@@ -697,6 +684,17 @@ gtk_text_iter_get_line_index (const GtkTextIter *iter)
   return real->line_byte_offset;
 }
 
+/**
+ * gtk_text_iter_get_visible_line_offset:
+ * @iter: a #GtkTextIter
+ * 
+ * Returns the offset in characters from the start of the
+ * line to the given @iter, not counting characters that
+ * are invisible due to tags with the "invisible" flag
+ * toggled on.
+ * 
+ * Return value: offset in visible characters from the start of the line 
+ **/
 gint
 gtk_text_iter_get_visible_line_offset (const GtkTextIter *iter)
 {
@@ -715,9 +713,11 @@ gtk_text_iter_get_visible_line_offset (const GtkTextIter *iter)
   ensure_char_offsets (real);
 
   check_invariants (iter);
-
+  
   vis_offset = real->line_char_offset;
 
+  g_assert (vis_offset >= 0);
+  
   _gtk_text_btree_get_iter_at_line (real->tree,
                                     &pos,
                                     real->line,
@@ -748,6 +748,18 @@ gtk_text_iter_get_visible_line_offset (const GtkTextIter *iter)
   return vis_offset;
 }
 
+
+/**
+ * gtk_text_iter_get_visible_line_index:
+ * @iter: a #GtkTextIter
+ * 
+ * Returns the number of bytes from the start of the
+ * line to the given @iter, not counting bytes that
+ * are invisible due to tags with the "invisible" flag
+ * toggled on.
+ * 
+ * Return value: byte index of @iter with respect to the start of the line
+ **/
 gint
 gtk_text_iter_get_visible_line_index (const GtkTextIter *iter)
 {
@@ -763,12 +775,14 @@ gtk_text_iter_get_visible_line_index (const GtkTextIter *iter)
   if (real == NULL)
     return 0;
 
-  ensure_char_offsets (real);
+  ensure_byte_offsets (real);
 
   check_invariants (iter);
 
   vis_offset = real->line_byte_offset;
 
+  g_assert (vis_offset >= 0);
+  
   _gtk_text_btree_get_iter_at_line (real->tree,
                                     &pos,
                                     real->line,
@@ -808,7 +822,7 @@ gtk_text_iter_get_visible_line_index (const GtkTextIter *iter)
  * @iter: an iterator
  *
  * Returns the Unicode character at this iterator.  (Equivalent to
- * operator* on a C++ iterator.)  If the iterator points at a
+ * operator* on a C++ iterator.)  If the element at this iterator is a
  * non-character element, such as an image embedded in the buffer, the
  * Unicode "unknown" character 0xFFFC is returned. If invoked on
  * the end iterator, zero is returned; zero is not a valid Unicode character.
@@ -954,9 +968,9 @@ gtk_text_iter_get_visible_text (const GtkTextIter  *start,
  * gtk_text_iter_get_pixbuf:
  * @iter: an iterator
  *
- * If the location pointed to by @iter contains a pixbuf, the pixbuf
- * is returned (with no new reference count added). Otherwise,
- * NULL is returned.
+ * If the element at @iter is a pixbuf, the pixbuf is returned
+ * (with no new reference count added). Otherwise,
+ * %NULL is returned.
  *
  * Return value: the pixbuf at @iter
  **/
@@ -984,9 +998,9 @@ gtk_text_iter_get_pixbuf (const GtkTextIter *iter)
  * gtk_text_iter_get_child_anchor:
  * @iter: an iterator
  *
- * If the location pointed to by @iter contains a child anchor, the
+ * If the location at @iter contains a child anchor, the
  * anchor is returned (with no new reference count added). Otherwise,
- * NULL is returned.
+ * %NULL is returned.
  *
  * Return value: the anchor at @iter
  **/
@@ -1057,10 +1071,10 @@ gtk_text_iter_get_marks (const GtkTextIter *iter)
 /**
  * gtk_text_iter_get_toggled_tags:
  * @iter: an iterator
- * @toggled_on: TRUE to get toggled-on tags
+ * @toggled_on: %TRUE to get toggled-on tags
  *
  * Returns a list of #GtkTextTag that are toggled on or off at this
- * point.  (If @toggled_on is TRUE, the list contains tags that are
+ * point.  (If @toggled_on is %TRUE, the list contains tags that are
  * toggled on.) If a tag is toggled on at @iter, then some non-empty
  * range of characters following @iter has that tag applied to it.  If
  * a tag is toggled off, then some non-empty range following @iter
@@ -1115,11 +1129,11 @@ gtk_text_iter_get_toggled_tags  (const GtkTextIter  *iter,
 /**
  * gtk_text_iter_begins_tag:
  * @iter: an iterator
- * @tag: a #GtkTextTag, or NULL
+ * @tag: a #GtkTextTag, or %NULL
  *
- * Returns TRUE if @tag is toggled on at exactly this point. If @tag
- * is NULL, returns TRUE if any tag is toggled on at this point. Note
- * that the gtk_text_iter_begins_tag () returns TRUE if @iter is the
+ * Returns %TRUE if @tag is toggled on at exactly this point. If @tag
+ * is %NULL, returns %TRUE if any tag is toggled on at this point. Note
+ * that the gtk_text_iter_begins_tag () returns %TRUE if @iter is the
  * <emphasis>start</emphasis> of the tagged range;
  * gtk_text_iter_has_tag () tells you whether an iterator is
  * <emphasis>within</emphasis> a tagged range.
@@ -1161,11 +1175,11 @@ gtk_text_iter_begins_tag    (const GtkTextIter  *iter,
 /**
  * gtk_text_iter_ends_tag:
  * @iter: an iterator
- * @tag: a #GtkTextTag, or NULL
+ * @tag: a #GtkTextTag, or %NULL
  *
- * Returns TRUE if @tag is toggled off at exactly this point. If @tag
- * is NULL, returns TRUE if any tag is toggled off at this point. Note
- * that the gtk_text_iter_ends_tag () returns TRUE if @iter is the
+ * Returns %TRUE if @tag is toggled off at exactly this point. If @tag
+ * is %NULL, returns %TRUE if any tag is toggled off at this point. Note
+ * that the gtk_text_iter_ends_tag () returns %TRUE if @iter is the
  * <emphasis>end</emphasis> of the tagged range;
  * gtk_text_iter_has_tag () tells you whether an iterator is
  * <emphasis>within</emphasis> a tagged range.
@@ -1208,7 +1222,7 @@ gtk_text_iter_ends_tag   (const GtkTextIter  *iter,
 /**
  * gtk_text_iter_toggles_tag:
  * @iter: an iterator
- * @tag: a #GtkTextTag, or NULL
+ * @tag: a #GtkTextTag, or %NULL
  *
  * This is equivalent to (gtk_text_iter_begins_tag () ||
  * gtk_text_iter_ends_tag ()), i.e. it tells you whether a range with
@@ -1217,8 +1231,8 @@ gtk_text_iter_ends_tag   (const GtkTextIter  *iter,
  * Return value: whether @tag is toggled on or off at @iter
  **/
 gboolean
-gtk_text_iter_toggles_tag       (const GtkTextIter  *iter,
-                                 GtkTextTag         *tag)
+gtk_text_iter_toggles_tag (const GtkTextIter  *iter,
+                           GtkTextTag         *tag)
 {
   GtkTextRealIter *real;
   GtkTextLineSegment *seg;
@@ -1252,13 +1266,13 @@ gtk_text_iter_toggles_tag       (const GtkTextIter  *iter,
  * @iter: an iterator
  * @tag: a #GtkTextTag
  *
- * Returns TRUE if @iter is within a range tagged with @tag.
+ * Returns %TRUE if @iter is within a range tagged with @tag.
  *
  * Return value: whether @iter is tagged with @tag
  **/
 gboolean
-gtk_text_iter_has_tag           (const GtkTextIter   *iter,
-                                 GtkTextTag          *tag)
+gtk_text_iter_has_tag (const GtkTextIter   *iter,
+                       GtkTextTag          *tag)
 {
   GtkTextRealIter *real;
 
@@ -1275,13 +1289,13 @@ gtk_text_iter_has_tag           (const GtkTextIter   *iter,
   if (real->line_byte_offset >= 0)
     {
       return _gtk_text_line_byte_has_tag (real->line, real->tree,
-                                         real->line_byte_offset, tag);
+                                          real->line_byte_offset, tag);
     }
   else
     {
       g_assert (real->line_char_offset >= 0);
       return _gtk_text_line_char_has_tag (real->line, real->tree,
-                                         real->line_char_offset, tag);
+                                          real->line_char_offset, tag);
     }
 }
 
@@ -1312,8 +1326,7 @@ gtk_text_iter_get_tags (const GtkTextIter *iter)
   /* No tags, use default style */
   if (tags == NULL || tag_count == 0)
     {
-      if (tags)
-        g_free (tags);
+      g_free (tags);
 
       return NULL;
     }
@@ -1338,14 +1351,21 @@ gtk_text_iter_get_tags (const GtkTextIter *iter)
 /**
  * gtk_text_iter_editable:
  * @iter: an iterator
- * @default_setting: TRUE if text is editable by default
+ * @default_setting: %TRUE if text is editable by default
  *
- * Returns whether @iter is within an editable region of text.
- * Non-editable text is "locked" and can't be changed by the user via
- * #GtkTextView. This function is simply a convenience wrapper around
- * gtk_text_iter_get_attributes (). If no tags applied to this text
- * affect editability, @default_setting will be returned.
+ * Returns whether the character at @iter is within an editable region
+ * of text.  Non-editable text is "locked" and can't be changed by the
+ * user via #GtkTextView. This function is simply a convenience
+ * wrapper around gtk_text_iter_get_attributes (). If no tags applied
+ * to this text affect editability, @default_setting will be returned.
  *
+ * You don't want to use this function to decide whether text can be
+ * inserted at @iter, because for insertion you don't want to know
+ * whether the char at @iter is inside an editable range, you want to
+ * know whether a new character inserted at @iter would be inside an
+ * editable range. Use gtk_text_iter_can_insert() to handle this
+ * case.
+ * 
  * Return value: whether @iter is inside an editable range
  **/
 gboolean
@@ -1355,6 +1375,8 @@ gtk_text_iter_editable (const GtkTextIter *iter,
   GtkTextAttributes *values;
   gboolean retval;
 
+  g_return_val_if_fail (iter != NULL, FALSE);
+  
   values = gtk_text_attributes_new ();
 
   values->editable = default_setting;
@@ -1368,28 +1390,68 @@ gtk_text_iter_editable (const GtkTextIter *iter,
   return retval;
 }
 
+/**
+ * gtk_text_iter_can_insert:
+ * @iter: an iterator
+ * @default_editability: %TRUE if text is editable by default
+ * 
+ * Considering the default editability of the buffer, and tags that
+ * affect editability, determines whether text inserted at @iter would
+ * be editable. If text inserted at @iter would be editable then the
+ * user should be allowed to insert text at @iter.
+ * gtk_text_buffer_insert_interactive() uses this function to decide
+ * whether insertions are allowed at a given position.
+ * 
+ * Return value: whether text inserted at @iter would be editable
+ **/
+gboolean
+gtk_text_iter_can_insert (const GtkTextIter *iter,
+                          gboolean           default_editability)
+{
+  g_return_val_if_fail (iter != NULL, FALSE);
+  
+  if (gtk_text_iter_editable (iter, default_editability))
+    return TRUE;
+  /* If at start/end of buffer, default editability is used */
+  else if ((gtk_text_iter_is_start (iter) ||
+            gtk_text_iter_is_end (iter)) &&
+           default_editability)
+    return TRUE;
+  else
+    {
+      /* if iter isn't editable, and the char before iter is,
+       * then iter is the first char in an editable region
+       * and thus insertion at iter results in editable text.
+       */
+      GtkTextIter prev = *iter;
+      gtk_text_iter_backward_char (&prev);
+      return gtk_text_iter_editable (&prev, default_editability);
+    }
+}
+
+
 /**
  * gtk_text_iter_get_language:
  * @iter: an iterator
  *
  * A convenience wrapper around gtk_text_iter_get_attributes (),
  * which returns the language in effect at @iter. If no tags affecting
- * language apply to @iter, the return value is identical to that of
+ * language apply to @iter, the return value is identical to that of
  * gtk_get_default_language ().
  *
  * Return value: language in effect at @iter
  **/
-gchar*
+PangoLanguage *
 gtk_text_iter_get_language (const GtkTextIter *iter)
 {
   GtkTextAttributes *values;
-  gchar *retval;
-
+  PangoLanguage *retval;
+  
   values = gtk_text_attributes_new ();
 
   gtk_text_iter_get_attributes (iter, values);
 
-  retval = g_strdup (values->language);
+  retval = values->language;
 
   gtk_text_attributes_unref (values);
 
@@ -1400,7 +1462,7 @@ gtk_text_iter_get_language (const GtkTextIter *iter)
  * gtk_text_iter_starts_line:
  * @iter: an iterator
  *
- * Returns TRUE if @iter begins a paragraph,
+ * Returns %TRUE if @iter begins a paragraph,
  * i.e. if gtk_text_iter_get_line_offset () would return 0.
  * However this function is potentially more efficient than
  * gtk_text_iter_get_line_offset () because it doesn't have to compute
@@ -1437,25 +1499,24 @@ gtk_text_iter_starts_line (const GtkTextIter   *iter)
  * gtk_text_iter_ends_line:
  * @iter: an iterator
  *
- * Returns TRUE if @iter points to the start of the paragraph delimiter
- * characters for a line (delimiters will be either a newline, a
- * carriage return, a carriage return followed by a newline, or a
- * Unicode paragraph separator character). Note that an iterator pointing
- * to the \n of a \r\n pair will not be counted as the end of a line,
- * the line ends before the \r.
+ * Returns %TRUE if @iter points to the start of the paragraph
+ * delimiter characters for a line (delimiters will be either a
+ * newline, a carriage return, a carriage return followed by a
+ * newline, or a Unicode paragraph separator character). Note that an
+ * iterator pointing to the \n of a \r\n pair will not be counted as
+ * the end of a line, the line ends before the \r. The end iterator is
+ * considered to be at the end of a line, even though there are no
+ * paragraph delimiter chars there.
  *
  * Return value: whether @iter is at the end of a line
  **/
 gboolean
 gtk_text_iter_ends_line (const GtkTextIter   *iter)
 {
-  GtkTextRealIter *real;
   gunichar wc;
   
   g_return_val_if_fail (iter != NULL, FALSE);
 
-  real = gtk_text_iter_make_real (iter);
-  
   check_invariants (iter);
 
   /* Only one character has type G_UNICODE_PARAGRAPH_SEPARATOR in
@@ -1465,14 +1526,27 @@ gtk_text_iter_ends_line (const GtkTextIter   *iter)
 
   wc = gtk_text_iter_get_char (iter);
   
-  if (wc == '\r' || wc == PARAGRAPH_SEPARATOR)
+  if (wc == '\r' || wc == PARAGRAPH_SEPARATOR || wc == 0) /* wc == 0 is end iterator */
     return TRUE;
   else if (wc == '\n')
     {
+      GtkTextIter tmp = *iter;
+
       /* need to determine if a \r precedes the \n, in which case
-       * we aren't the end of the line
+       * we aren't the end of the line.
+       * Note however that if \r and \n are on different lines, they
+       * both are terminators. This for instance may happen after
+       * deleting some text:
+
+          1 some text\r    delete 'a'    1 some text\r
+          2 a\n            --------->    2 \n
+          3 ...                          3 ...
+
        */
-      GtkTextIter tmp = *iter;
+
+      if (gtk_text_iter_get_line_offset (&tmp) == 0)
+        return TRUE;
+
       if (!gtk_text_iter_backward_char (&tmp))
         return TRUE;
 
@@ -1486,7 +1560,7 @@ gtk_text_iter_ends_line (const GtkTextIter   *iter)
  * gtk_text_iter_is_end:
  * @iter: an iterator
  *
- * Returns TRUE if @iter is the end iterator, i.e. one past the last
+ * Returns %TRUE if @iter is the end iterator, i.e. one past the last
  * dereferenceable iterator in the buffer. gtk_text_iter_is_end () is
  * the most efficient way to check whether an iterator is the end
  * iterator.
@@ -1507,20 +1581,32 @@ gtk_text_iter_is_end (const GtkTextIter *iter)
 
   check_invariants (iter);
 
-  return _gtk_text_line_is_last (real->line, real->tree);
+  if (!_gtk_text_line_contains_end_iter (real->line, real->tree))
+    return FALSE;
+
+  /* Now we need the segments validated */
+  real = gtk_text_iter_make_real (iter);
+
+  if (real == NULL)
+    return FALSE;
+  
+  return _gtk_text_btree_is_end (real->tree, real->line,
+                                 real->segment,
+                                 real->segment_byte_offset,
+                                 real->segment_char_offset);
 }
 
 /**
- * gtk_text_iter_is_first:
+ * gtk_text_iter_is_start:
  * @iter: an iterator
  *
- * Returns TRUE if @iter is the first iterator in the buffer, that is
+ * Returns %TRUE if @iter is the first iterator in the buffer, that is
  * if @iter has a character offset of 0.
  *
  * Return value: whether @iter is the first in the buffer
  **/
 gboolean
-gtk_text_iter_is_first (const GtkTextIter *iter)
+gtk_text_iter_is_start (const GtkTextIter *iter)
 {
   return gtk_text_iter_get_offset (iter) == 0;
 }
@@ -1541,7 +1627,7 @@ gtk_text_iter_get_chars_in_line (const GtkTextIter   *iter)
   gint count;
   GtkTextLineSegment *seg;
 
-  g_return_val_if_fail (iter != NULL, FALSE);
+  g_return_val_if_fail (iter != NULL, 0);
 
   real = gtk_text_iter_make_surreal (iter);
 
@@ -1571,6 +1657,9 @@ gtk_text_iter_get_chars_in_line (const GtkTextIter   *iter)
       seg = seg->next;
     }
 
+  if (_gtk_text_line_contains_end_iter (real->line, real->tree))
+    count -= 1; /* Dump the newline that was in the last segment of the end iter line */
+  
   return count;
 }
 
@@ -1590,7 +1679,7 @@ gtk_text_iter_get_bytes_in_line (const GtkTextIter   *iter)
   gint count;
   GtkTextLineSegment *seg;
 
-  g_return_val_if_fail (iter != NULL, FALSE);
+  g_return_val_if_fail (iter != NULL, 0);
 
   real = gtk_text_iter_make_surreal (iter);
 
@@ -1619,6 +1708,9 @@ gtk_text_iter_get_bytes_in_line (const GtkTextIter   *iter)
       seg = seg->next;
     }
 
+  if (_gtk_text_line_contains_end_iter (real->line, real->tree))
+    count -= 1; /* Dump the newline that was in the last segment of the end iter line */
+  
   return count;
 }
 
@@ -1629,16 +1721,18 @@ gtk_text_iter_get_bytes_in_line (const GtkTextIter   *iter)
  *
  * Computes the effect of any tags applied to this spot in the
  * text. The @values parameter should be initialized to the default
- * settings you wish to use if no tags are in effect.
+ * settings you wish to use if no tags are in effect. You'd typically
+ * obtain the defaults from gtk_text_view_get_default_attributes().
+ *
  * gtk_text_iter_get_attributes () will modify @values, applying the
  * effects of any tags present at @iter. If any tags affected @values,
- * the function returns TRUE.
+ * the function returns %TRUE.
  *
- * Return value: TRUE if @values was modified
+ * Return value: %TRUE if @values was modified
  **/
 gboolean
 gtk_text_iter_get_attributes (const GtkTextIter  *iter,
-                              GtkTextAttributes *values)
+                              GtkTextAttributes  *values)
 {
   GtkTextTag** tags;
   gint tag_count = 0;
@@ -1649,8 +1743,7 @@ gtk_text_iter_get_attributes (const GtkTextIter  *iter,
   /* No tags, use default style */
   if (tags == NULL || tag_count == 0)
     {
-      if (tags)
-        g_free (tags);
+      g_free (tags);
 
       return FALSE;
     }
@@ -1674,18 +1767,23 @@ gtk_text_iter_get_attributes (const GtkTextIter  *iter,
 /* The return value of this indicates WHETHER WE MOVED.
  * The return value of public functions indicates
  * (MOVEMENT OCCURRED && NEW ITER IS DEREFERENCEABLE)
+ *
+ * This function will not change the iterator if
+ * it's already on the last (end iter) line, i.e. it
+ * won't move to the end of the last line.
  */
 static gboolean
 forward_line_leaving_caches_unmodified (GtkTextRealIter *real)
 {
-  GtkTextLine *new_line;
-
-  new_line = _gtk_text_line_next (real->line);
-
-  g_assert (new_line != real->line);
-
-  if (new_line != NULL)
+  if (!_gtk_text_line_contains_end_iter (real->line, real->tree))
     {
+      GtkTextLine *new_line;
+      
+      new_line = _gtk_text_line_next (real->line);
+      g_assert (new_line);
+      g_assert (new_line != real->line);
+      g_assert (!_gtk_text_line_is_last (new_line, real->tree));
+      
       real->line = new_line;
 
       real->line_byte_offset = 0;
@@ -1704,32 +1802,23 @@ forward_line_leaving_caches_unmodified (GtkTextRealIter *real)
     }
   else
     {
-      /* There is no way to move forward; we were already
-         at the "end" index. (the end index is the last
-         line pointer, segment_byte_offset of 0) */
-
-      g_assert (real->line_char_offset == 0 ||
-                real->line_byte_offset == 0);
-
-      /* The only indexable segment allowed on the bogus
-         line at the end is a single char segment containing
-         a newline. */
-      if (real->segments_changed_stamp ==
-          _gtk_text_btree_get_segments_changed_stamp (real->tree))
-        {
-          g_assert (real->segment->type == &gtk_text_char_type);
-          g_assert (real->segment->char_count == 1);
-        }
-      /* We leave real->line as-is */
-
+      /* There is no way to move forward a line; we were already at
+       * the line containing the end iterator.
+       * However we may not be at the end iterator itself.
+       */
+      
       return FALSE;
     }
 }
 
-
+#if 0
 /* The return value of this indicates WHETHER WE MOVED.
  * The return value of public functions indicates
  * (MOVEMENT OCCURRED && NEW ITER IS DEREFERENCEABLE)
+ *
+ * This function is currently unused, thus it is #if-0-ed. It is
+ * left here, since it's non-trivial code that might be useful in
+ * the future.
  */
 static gboolean
 backward_line_leaving_caches_unmodified (GtkTextRealIter *real)
@@ -1770,6 +1859,7 @@ backward_line_leaving_caches_unmodified (GtkTextRealIter *real)
       return FALSE;
     }
 }
+#endif 
 
 /* The return value indicates (MOVEMENT OCCURRED && NEW ITER IS
  * DEREFERENCEABLE)
@@ -1895,7 +1985,7 @@ _gtk_text_iter_forward_indexable_segment (GtkTextIter *iter)
 
       check_invariants (iter);
 
-      return TRUE;
+      return !gtk_text_iter_is_end (iter);
     }
   else
     {
@@ -1914,17 +2004,23 @@ _gtk_text_iter_forward_indexable_segment (GtkTextIter *iter)
 
           check_invariants (iter);
 
-          if (gtk_text_iter_is_end (iter))
-            return FALSE;
-          else
-            return TRUE;
+          return !gtk_text_iter_is_end (iter);
         }
       else
         {
-          /* End of buffer */
-
+          /* End of buffer, but iter is still at start of last segment,
+           * not at the end iterator. We put it on the end iterator.
+           */
+          
           check_invariants (iter);
 
+          g_assert (!_gtk_text_line_is_last (real->line, real->tree));
+          g_assert (_gtk_text_line_contains_end_iter (real->line, real->tree));
+
+          gtk_text_iter_forward_to_line_end (iter);
+
+          g_assert (gtk_text_iter_is_end (iter));
+          
           return FALSE;
         }
     }
@@ -2011,7 +2107,7 @@ _gtk_text_iter_backward_indexable_segment (GtkTextIter *iter)
    * segment just before our current segment.
    */
   g_assert (seg != real->segment);
-  while (seg != real->segment)
+  do
     {
       prev_seg = seg;
       prev_any_seg = any_seg;
@@ -2021,6 +2117,7 @@ _gtk_text_iter_backward_indexable_segment (GtkTextIter *iter)
       while (seg->char_count == 0)
         seg = seg->next;
     }
+  while (seg != real->segment);
 
   g_assert (prev_seg != NULL);
   g_assert (prev_any_seg != NULL);
@@ -2091,10 +2188,10 @@ _gtk_text_iter_backward_indexable_segment (GtkTextIter *iter)
  * gtk_text_iter_forward_char () may actually move onto an image instead
  * of a character, if you have images in your buffer.  If @iter is the
  * end iterator or one character before it, @iter will now point at
- * the end iterator, and gtk_text_iter_forward_char () returns FALSE for
+ * the end iterator, and gtk_text_iter_forward_char () returns %FALSE for
  * convenience when writing loops.
  *
- * Return value: whether the new position is the end iterator
+ * Return value: whether @iter moved and is dereferenceable
  **/
 gboolean
 gtk_text_iter_forward_char (GtkTextIter *iter)
@@ -2118,9 +2215,9 @@ gtk_text_iter_forward_char (GtkTextIter *iter)
  * gtk_text_iter_backward_char:
  * @iter: an iterator
  *
- * Moves backward by one character offset. Returns TRUE if movement
+ * Moves backward by one character offset. Returns %TRUE if movement
  * was possible; if @iter was the first in the buffer (character
- * offset 0), gtk_text_iter_backward_char () returns FALSE for convenience when
+ * offset 0), gtk_text_iter_backward_char () returns %FALSE for convenience when
  * writing loops.
  *
  * Return value: whether movement was possible
@@ -2160,7 +2257,7 @@ gtk_text_iter_backward_char (GtkTextIter *iter)
  * buffer). The return value indicates whether the new position of
  * @iter is different from its original position, and dereferenceable
  * (the last iterator in the buffer is not dereferenceable). If @count
- * is 0, the function does nothing and returns FALSE.
+ * is 0, the function does nothing and returns %FALSE.
  *
  * Return value: whether @iter moved and is dereferenceable
  **/
@@ -2171,6 +2268,8 @@ gtk_text_iter_forward_chars (GtkTextIter *iter, gint count)
 
   g_return_val_if_fail (iter != NULL, FALSE);
 
+  FIX_OVERFLOWS (count);
+  
   real = gtk_text_iter_make_real (iter);
 
   if (real == NULL)
@@ -2228,8 +2327,8 @@ gtk_text_iter_forward_chars (GtkTextIter *iter, gint count)
  * past the start or end of the buffer, moves to the start or end of
  * the buffer).  The return value indicates whether the iterator moved
  * onto a dereferenceable position; if the iterator didn't move, or
- * moved onto the end iterator, then FALSE is returned. If @count is 0,
- * the function does nothing and returns FALSE.
+ * moved onto the end iterator, then %FALSE is returned. If @count is 0,
+ * the function does nothing and returns %FALSE.
  *
  * Return value: whether @iter moved and is dereferenceable
  *
@@ -2241,6 +2340,8 @@ gtk_text_iter_backward_chars (GtkTextIter *iter, gint count)
 
   g_return_val_if_fail (iter != NULL, FALSE);
 
+  FIX_OVERFLOWS (count);
+  
   real = gtk_text_iter_make_real (iter);
 
   if (real == NULL)
@@ -2253,34 +2354,35 @@ gtk_text_iter_backward_chars (GtkTextIter *iter, gint count)
   ensure_char_offsets (real);
   check_invariants (iter);
 
-  if (count <= real->segment_char_offset)
+  /* <, not <=, because if count == segment_char_offset
+   * we're going to the front of the segment and the any_segment
+   * might change
+   */
+  if (count < real->segment_char_offset)
     {
       /* Optimize the within-segment case */
       g_assert (real->segment->char_count > 0);
       g_assert (real->segment->type == &gtk_text_char_type);
 
-      real->segment_char_offset -= count;
-      g_assert (real->segment_char_offset >= 0);
-
       if (real->line_byte_offset >= 0)
         {
+          const char *p;
           gint new_byte_offset;
-          gint i;
-
-          new_byte_offset = 0;
-          i = 0;
-          while (i < real->segment_char_offset)
-            {
-              const char * start = real->segment->body.chars + new_byte_offset;
-              new_byte_offset += g_utf8_next_char (start) - start;
 
-              ++i;
-            }
+          /* if in the last fourth of the segment walk backwards */
+          if (count < real->segment_char_offset / 4)
+            p = g_utf8_offset_to_pointer (real->segment->body.chars + real->segment_byte_offset, 
+                                          -count);
+          else
+            p = g_utf8_offset_to_pointer (real->segment->body.chars,
+                                          real->segment_char_offset - count);
 
+          new_byte_offset = p - real->segment->body.chars;
           real->line_byte_offset -= (real->segment_byte_offset - new_byte_offset);
           real->segment_byte_offset = new_byte_offset;
         }
 
+      real->segment_char_offset -= count;
       real->line_char_offset -= count;
 
       adjust_char_index (real, 0 - count);
@@ -2308,6 +2410,7 @@ gtk_text_iter_backward_chars (GtkTextIter *iter, gint count)
           new_char_index = current_char_index - count;
           if (new_char_index < 0)
             new_char_index = 0;
+
           gtk_text_iter_set_offset (iter, new_char_index);
 
           check_invariants (iter);
@@ -2376,10 +2479,10 @@ gtk_text_iter_backward_text_chars (GtkTextIter *iter,
  * gtk_text_iter_forward_line:
  * @iter: an iterator
  *
- * Moves @iter to the start of the next line. Returns TRUE if there
- * was a next line to move to, and FALSE if @iter was simply moved to
- * the end of the buffer and is now not dereferenceable, or if @iter was
- * already at the end of the buffer.
+ * Moves @iter to the start of the next line. If the iter is already on the
+ * last line of the buffer, moves the iter to the end of the current line.
+ * If after the operation, the iter is at the end of the buffer and not
+ * dereferencable, returns %FALSE. Otherwise, returns %TRUE.
  *
  * Return value: whether @iter can be dereferenced
  **/
@@ -2389,7 +2492,7 @@ gtk_text_iter_forward_line (GtkTextIter *iter)
   GtkTextRealIter *real;
 
   g_return_val_if_fail (iter != NULL, FALSE);
-
+  
   real = gtk_text_iter_make_real (iter);
 
   if (real == NULL)
@@ -2411,6 +2514,11 @@ gtk_text_iter_forward_line (GtkTextIter *iter)
     }
   else
     {
+      /* On the last line, move to end of it */
+      
+      if (!gtk_text_iter_is_end (iter))
+        gtk_text_iter_forward_to_end (iter);
+      
       check_invariants (iter);
       return FALSE;
     }
@@ -2420,11 +2528,11 @@ gtk_text_iter_forward_line (GtkTextIter *iter)
  * gtk_text_iter_backward_line:
  * @iter: an iterator
  *
- * Moves @iter to the start of the previous line. Returns TRUE if
+ * Moves @iter to the start of the previous line. Returns %TRUE if
  * @iter could be moved; i.e. if @iter was at character offset 0, this
- * function returns FALSE. Therefore if @iter was already on line 0,
+ * function returns %FALSE. Therefore if @iter was already on line 0,
  * but not at the start of the line, @iter is snapped to the start of
- * the line and the function returns TRUE. (Note that this implies that
+ * the line and the function returns %TRUE. (Note that this implies that
  * in a loop calling this function, the line number may not change on
  * every iteration, if your first iteration is on line 0.)
  *
@@ -2476,7 +2584,7 @@ gtk_text_iter_backward_line (GtkTextIter *iter)
   /* Find first segment in line */
   real->any_segment = real->line->segments;
   real->segment = _gtk_text_line_byte_to_segment (real->line,
-                                                 0, &offset);
+                                                  0, &offset);
 
   g_assert (offset == 0);
 
@@ -2491,9 +2599,27 @@ gtk_text_iter_backward_line (GtkTextIter *iter)
   return TRUE;
 }
 
+
+/**
+ * gtk_text_iter_forward_lines:
+ * @iter: a #GtkTextIter
+ * @count: number of lines to move forward
+ *
+ * Moves @count lines forward, if possible (if @count would move
+ * past the start or end of the buffer, moves to the start or end of
+ * the buffer).  The return value indicates whether the iterator moved
+ * onto a dereferenceable position; if the iterator didn't move, or
+ * moved onto the end iterator, then %FALSE is returned. If @count is 0,
+ * the function does nothing and returns %FALSE. If @count is negative,
+ * moves backward by 0 - @count lines.
+ *
+ * Return value: whether @iter moved and is dereferenceable
+ **/
 gboolean
 gtk_text_iter_forward_lines (GtkTextIter *iter, gint count)
 {
+  FIX_OVERFLOWS (count);
+  
   if (count < 0)
     return gtk_text_iter_backward_lines (iter, 0 - count);
   else if (count == 0)
@@ -2507,22 +2633,44 @@ gtk_text_iter_forward_lines (GtkTextIter *iter, gint count)
     {
       gint old_line;
 
+      if (gtk_text_iter_is_end (iter))
+        return FALSE;
+      
       old_line = gtk_text_iter_get_line (iter);
 
       gtk_text_iter_set_line (iter, old_line + count);
 
-      check_invariants (iter);
-
-      /* return whether it moved, and is dereferenceable. */
-      return
-        (gtk_text_iter_get_line (iter) != old_line) &&
-        !gtk_text_iter_is_end (iter);
+      if ((gtk_text_iter_get_line (iter) - old_line) < count)
+        {
+          /* count went past the last line, so move to end of last line */
+          if (!gtk_text_iter_is_end (iter))
+            gtk_text_iter_forward_to_end (iter);
+        }
+      
+      return !gtk_text_iter_is_end (iter);
     }
 }
 
+/**
+ * gtk_text_iter_backward_lines:
+ * @iter: a #GtkTextIter
+ * @count: number of lines to move backward
+ *
+ * Moves @count lines backward, if possible (if @count would move
+ * past the start or end of the buffer, moves to the start or end of
+ * the buffer).  The return value indicates whether the iterator moved
+ * onto a dereferenceable position; if the iterator didn't move, or
+ * moved onto the end iterator, then %FALSE is returned. If @count is 0,
+ * the function does nothing and returns %FALSE. If @count is negative,
+ * moves forward by 0 - @count lines.
+ *
+ * Return value: whether @iter moved and is dereferenceable
+ **/
 gboolean
 gtk_text_iter_backward_lines (GtkTextIter *iter, gint count)
 {
+  FIX_OVERFLOWS (count);
+  
   if (count < 0)
     return gtk_text_iter_forward_lines (iter, 0 - count);
   else if (count == 0)
@@ -2543,52 +2691,208 @@ gtk_text_iter_backward_lines (GtkTextIter *iter, gint count)
     }
 }
 
-typedef gboolean (* FindLogAttrFunc) (const PangoLogAttr *attrs,
-                                      gint                offset,
-                                      gint                min_offset,
-                                      gint                len,
-                                      gint               *found_offset,
-                                      gboolean            already_moved_initially);
-
-typedef gboolean (* TestLogAttrFunc) (const PangoLogAttr *attrs,
-                                      gint                offset,
-                                      gint                min_offset,
-                                      gint                len);
-
-/* Word funcs */
-
-static gboolean
-find_word_end_func (const PangoLogAttr *attrs,
-                    gint          offset,
-                    gint          min_offset,
-                    gint          len,
-                    gint         *found_offset,
-                    gboolean      already_moved_initially)
+/**
+ * gtk_text_iter_forward_visible_line:
+ * @iter: an iterator
+ *
+ * Moves @iter to the start of the next visible line. Returns %TRUE if there
+ * was a next line to move to, and %FALSE if @iter was simply moved to
+ * the end of the buffer and is now not dereferenceable, or if @iter was
+ * already at the end of the buffer.
+ *
+ * Return value: whether @iter can be dereferenced
+ * 
+ * Since: 2.8
+ **/
+gboolean
+gtk_text_iter_forward_visible_line (GtkTextIter *iter)
 {
-  if (!already_moved_initially)
-    ++offset;
-
-  /* Find end of next word */
-  while (offset < min_offset + len &&
-         !attrs[offset].is_word_end)
-    ++offset;
-
-  *found_offset = offset;
-
-  return offset < min_offset + len;
+  while (gtk_text_iter_forward_line (iter))
+    {
+      if (!_gtk_text_btree_char_is_invisible (iter))
+        return TRUE;
+      else
+        {
+          do
+            {
+              if (!gtk_text_iter_forward_char (iter))
+                return FALSE;
+          
+              if (!_gtk_text_btree_char_is_invisible (iter))
+                return TRUE;
+            }
+          while (!gtk_text_iter_ends_line (iter));
+        }
+    }
+    
+  return FALSE;
 }
 
-static gboolean
-is_word_end_func (const PangoLogAttr *attrs,
-                  gint          offset,
-                  gint          min_offset,
-                  gint          len)
+/**
+ * gtk_text_iter_backward_visible_line:
+ * @iter: an iterator
+ *
+ * Moves @iter to the start of the previous visible line. Returns %TRUE if
+ * @iter could be moved; i.e. if @iter was at character offset 0, this
+ * function returns %FALSE. Therefore if @iter was already on line 0,
+ * but not at the start of the line, @iter is snapped to the start of
+ * the line and the function returns %TRUE. (Note that this implies that
+ * in a loop calling this function, the line number may not change on
+ * every iteration, if your first iteration is on line 0.)
+ *
+ * Return value: whether @iter moved
+ *
+ * Since: 2.8
+ **/
+gboolean
+gtk_text_iter_backward_visible_line (GtkTextIter *iter)
 {
-  return attrs[offset].is_word_end;
-}
-
-static gboolean
-find_word_start_func (const PangoLogAttr *attrs,
+  while (gtk_text_iter_backward_line (iter))
+    {
+      if (!_gtk_text_btree_char_is_invisible (iter))
+        return TRUE;
+      else
+        {
+          do
+            {
+              if (!gtk_text_iter_backward_char (iter))
+                return FALSE;
+          
+              if (!_gtk_text_btree_char_is_invisible (iter))
+                return TRUE;
+            }
+          while (!gtk_text_iter_starts_line (iter));
+        }
+    }
+    
+  return FALSE;
+}
+
+/**
+ * gtk_text_iter_forward_visible_lines:
+ * @iter: a #GtkTextIter
+ * @count: number of lines to move forward
+ *
+ * Moves @count visible lines forward, if possible (if @count would move
+ * past the start or end of the buffer, moves to the start or end of
+ * the buffer).  The return value indicates whether the iterator moved
+ * onto a dereferenceable position; if the iterator didn't move, or
+ * moved onto the end iterator, then %FALSE is returned. If @count is 0,
+ * the function does nothing and returns %FALSE. If @count is negative,
+ * moves backward by 0 - @count lines.
+ *
+ * Return value: whether @iter moved and is dereferenceable
+ * 
+ * Since: 2.8
+ **/
+gboolean
+gtk_text_iter_forward_visible_lines (GtkTextIter *iter,
+                                     gint         count)
+{
+  FIX_OVERFLOWS (count);
+  
+  if (count < 0)
+    return gtk_text_iter_backward_visible_lines (iter, 0 - count);
+  else if (count == 0)
+    return FALSE;
+  else if (count == 1)
+    {
+      check_invariants (iter);
+      return gtk_text_iter_forward_visible_line (iter);
+    }
+  else
+    {
+      while (gtk_text_iter_forward_visible_line (iter) && count > 0)
+        count--;
+      return count == 0;
+    }    
+}
+
+/**
+ * gtk_text_iter_backward_visible_lines:
+ * @iter: a #GtkTextIter
+ * @count: number of lines to move backward
+ *
+ * Moves @count visible lines backward, if possible (if @count would move
+ * past the start or end of the buffer, moves to the start or end of
+ * the buffer).  The return value indicates whether the iterator moved
+ * onto a dereferenceable position; if the iterator didn't move, or
+ * moved onto the end iterator, then %FALSE is returned. If @count is 0,
+ * the function does nothing and returns %FALSE. If @count is negative,
+ * moves forward by 0 - @count lines.
+ *
+ * Return value: whether @iter moved and is dereferenceable
+ *
+ * Since: 2.8
+ **/
+gboolean
+gtk_text_iter_backward_visible_lines (GtkTextIter *iter,
+                                      gint         count)
+{
+  FIX_OVERFLOWS (count);
+  
+  if (count < 0)
+    return gtk_text_iter_forward_visible_lines (iter, 0 - count);
+  else if (count == 0)
+    return FALSE;
+  else if (count == 1)
+    {
+      return gtk_text_iter_backward_visible_line (iter);
+    }
+  else
+    {
+      while (gtk_text_iter_backward_visible_line (iter) && count > 0)
+        count--;
+      return count == 0;
+    }
+}
+
+typedef gboolean (* FindLogAttrFunc) (const PangoLogAttr *attrs,
+                                      gint                offset,
+                                      gint                min_offset,
+                                      gint                len,
+                                      gint               *found_offset,
+                                      gboolean            already_moved_initially);
+
+typedef gboolean (* TestLogAttrFunc) (const PangoLogAttr *attrs,
+                                      gint                offset,
+                                      gint                min_offset,
+                                      gint                len);
+
+/* Word funcs */
+
+static gboolean
+find_word_end_func (const PangoLogAttr *attrs,
+                    gint          offset,
+                    gint          min_offset,
+                    gint          len,
+                    gint         *found_offset,
+                    gboolean      already_moved_initially)
+{
+  if (!already_moved_initially)
+    ++offset;
+
+  /* Find end of next word */
+  while (offset < min_offset + len &&
+         !attrs[offset].is_word_end)
+    ++offset;
+
+  *found_offset = offset;
+
+  return offset < min_offset + len;
+}
+
+static gboolean
+is_word_end_func (const PangoLogAttr *attrs,
+                  gint          offset,
+                  gint          min_offset,
+                  gint          len)
+{
+  return attrs[offset].is_word_end;
+}
+
+static gboolean
+find_word_start_func (const PangoLogAttr *attrs,
                       gint          offset,
                       gint          min_offset,
                       gint          len,
@@ -2628,7 +2932,10 @@ inside_word_func (const PangoLogAttr *attrs,
          !(attrs[offset].is_word_start || attrs[offset].is_word_end))
     --offset;
 
-  return attrs[offset].is_word_start;
+  if (offset >= 0)
+    return attrs[offset].is_word_start;
+  else
+    return FALSE;
 }
 
 /* Sentence funcs */
@@ -2723,9 +3030,14 @@ test_log_attrs (const GtkTextIter *iter,
 
   offset = gtk_text_iter_get_line_offset (iter);
 
-  g_assert (char_len > 0);
+  /* char_len may be 0 and attrs will be NULL if so, if
+   * iter is the end iter and the last line is empty.
+   * 
+   * offset may be equal to char_len, since attrs contains an entry
+   * for one past the end
+   */
   
-  if (offset < char_len)
+  if (attrs && offset <= char_len)
     result = (* func) (attrs, offset, 0, char_len);
 
   return result;
@@ -2749,9 +3061,11 @@ find_line_log_attrs (const GtkTextIter *iter,
 
   offset = gtk_text_iter_get_line_offset (iter);
   
-  g_assert (char_len > 0);
+  /* char_len may be 0 and attrs will be NULL if so, if
+   * iter is the end iter and the last line is empty
+   */
   
-  if (offset < char_len)
+  if (attrs)
     result = (* func) (attrs, offset, 0, char_len, found_offset,
                        already_moved_initially);
 
@@ -2787,26 +3101,91 @@ find_by_log_attrs (GtkTextIter    *iter,
         }
       else
         {                    
-          /* go to end of previous line */
-          gtk_text_iter_set_line_offset (iter, 0);
-          
-          if (gtk_text_iter_backward_char (iter))
-            return find_by_log_attrs (iter, func, forward,
-                                      TRUE);
+          /* go to end of previous line. need to check that
+           * line is > 0 because backward_line snaps to start of
+           * line 0 if it's on line 0
+           */
+          if (gtk_text_iter_get_line (iter) > 0 && 
+              gtk_text_iter_backward_line (iter))
+            {
+              if (!gtk_text_iter_ends_line (iter))
+                gtk_text_iter_forward_to_line_end (iter);
+              
+              return find_by_log_attrs (iter, func, forward,
+                                        TRUE);
+            }
           else
             return FALSE;
         }
     }
   else
-    {
+    {      
       gtk_text_iter_set_line_offset (iter, offset);
 
       return
-        !gtk_text_iter_equal (iter, &orig) &&
+        (already_moved_initially || !gtk_text_iter_equal (iter, &orig)) &&
         !gtk_text_iter_is_end (iter);
     }
 }
 
+static gboolean 
+find_visible_by_log_attrs (GtkTextIter    *iter,
+                          FindLogAttrFunc func,
+                          gboolean        forward,
+                          gboolean        already_moved_initially)
+{
+  GtkTextIter pos;
+
+  g_return_val_if_fail (iter != NULL, FALSE);
+  
+  pos = *iter;
+  
+  while (find_by_log_attrs (&pos, func, forward, already_moved_initially)) 
+    {
+      if (!_gtk_text_btree_char_is_invisible (&pos)) 
+       {
+         *iter = pos;
+         return TRUE;
+       }
+  }
+
+  return FALSE;
+}
+
+typedef gboolean (* OneStepFunc) (GtkTextIter *iter);
+typedef gboolean (* MultipleStepFunc) (GtkTextIter *iter, gint count);
+                                 
+static gboolean 
+move_multiple_steps (GtkTextIter *iter, 
+                    gint count,
+                    OneStepFunc step_forward,
+                    MultipleStepFunc n_steps_backward)
+{
+  g_return_val_if_fail (iter != NULL, FALSE);
+
+  FIX_OVERFLOWS (count);
+  
+  if (count == 0)
+    return FALSE;
+  
+  if (count < 0)
+    return n_steps_backward (iter, -count);
+  
+  if (!step_forward (iter))
+    return FALSE;
+  --count;
+
+  while (count > 0)
+    {
+      if (!step_forward (iter))
+        break;
+      --count;
+    }
+  
+  return !gtk_text_iter_is_end (iter);  
+}
+              
+
 /**
  * gtk_text_iter_forward_word_end:
  * @iter: a #GtkTextIter
@@ -2826,10 +3205,10 @@ gtk_text_iter_forward_word_end (GtkTextIter *iter)
 }
 
 /**
- * gtk_text_iter_forward_word_end:
+ * gtk_text_iter_backward_word_start:
  * @iter: a #GtkTextIter
  * 
- * Moves backward to the next word start. (If @iter is currently on a
+ * Moves backward to the previous word start. (If @iter is currently on a
  * word start, moves backward to the next one after that.) Word breaks
  * are determined by Pango and should be correct for nearly any
  * language (if not, the correct fix would be to the Pango word break
@@ -2860,25 +3239,9 @@ gboolean
 gtk_text_iter_forward_word_ends (GtkTextIter      *iter,
                                  gint              count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
-
-  if (count == 0)
-    return FALSE;
-
-  if (count < 0)
-    return gtk_text_iter_backward_word_starts (iter, -count);
-
-  if (!gtk_text_iter_forward_word_end (iter))
-    return FALSE;
-  --count;
-
-  while (count > 0)
-    {
-      if (!gtk_text_iter_forward_word_end (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_forward_word_end,
+                             gtk_text_iter_backward_word_starts);
 }
 
 /**
@@ -2886,7 +3249,7 @@ gtk_text_iter_forward_word_ends (GtkTextIter      *iter,
  * @iter: a #GtkTextIter
  * @count: number of times to move
  * 
- * Calls gtk_text_iter_backward_word_starts() up to @count times.
+ * Calls gtk_text_iter_backward_word_start() up to @count times.
  *
  * Return value: %TRUE if @iter moved and is not the end iterator 
  **/
@@ -2894,22 +3257,89 @@ gboolean
 gtk_text_iter_backward_word_starts (GtkTextIter      *iter,
                                     gint               count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_backward_word_start,
+                             gtk_text_iter_forward_word_ends);
+}
 
-  if (count < 0)
-    return gtk_text_iter_forward_word_ends (iter, -count);
+/**
+ * gtk_text_iter_forward_visible_word_end:
+ * @iter: a #GtkTextIter
+ * 
+ * Moves forward to the next visible word end. (If @iter is currently on a
+ * word end, moves forward to the next one after that.) Word breaks
+ * are determined by Pango and should be correct for nearly any
+ * language (if not, the correct fix would be to the Pango word break
+ * algorithms).
+ * 
+ * Return value: %TRUE if @iter moved and is not the end iterator 
+ *
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_forward_visible_word_end (GtkTextIter *iter)
+{
+  return find_visible_by_log_attrs (iter, find_word_end_func, TRUE, FALSE);
+}
 
-  if (!gtk_text_iter_backward_word_start (iter))
-    return FALSE;
-  --count;
+/**
+ * gtk_text_iter_backward_visible_word_start:
+ * @iter: a #GtkTextIter
+ * 
+ * Moves backward to the previous visible word start. (If @iter is currently 
+ * on a word start, moves backward to the next one after that.) Word breaks
+ * are determined by Pango and should be correct for nearly any
+ * language (if not, the correct fix would be to the Pango word break
+ * algorithms).
+ * 
+ * Return value: %TRUE if @iter moved and is not the end iterator 
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_backward_visible_word_start (GtkTextIter      *iter)
+{
+  return find_visible_by_log_attrs (iter, find_word_start_func, FALSE, FALSE);
+}
 
-  while (count > 0)
-    {
-      if (!gtk_text_iter_backward_word_start (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+/**
+ * gtk_text_iter_forward_visible_word_ends:
+ * @iter: a #GtkTextIter
+ * @count: number of times to move
+ * 
+ * Calls gtk_text_iter_forward_visible_word_end() up to @count times.
+ *
+ * Return value: %TRUE if @iter moved and is not the end iterator 
+ *
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_forward_visible_word_ends (GtkTextIter *iter,
+                                        gint         count)
+{
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_forward_visible_word_end,
+                             gtk_text_iter_backward_visible_word_starts);
+}
+
+/**
+ * gtk_text_iter_backward_visible_word_starts
+ * @iter: a #GtkTextIter
+ * @count: number of times to move
+ * 
+ * Calls gtk_text_iter_backward_visible_word_start() up to @count times.
+ *
+ * Return value: %TRUE if @iter moved and is not the end iterator 
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_backward_visible_word_starts (GtkTextIter *iter,
+                                           gint         count)
+{
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_backward_visible_word_start,
+                             gtk_text_iter_forward_visible_word_ends);
 }
 
 /**
@@ -3037,7 +3467,7 @@ gtk_text_iter_forward_sentence_end (GtkTextIter *iter)
  * gtk_text_iter_backward_sentence_start:
  * @iter: a #GtkTextIter
  * 
- * Moves backward to the next sentence start; if @iter is already at
+ * Moves backward to the previous sentence start; if @iter is already at
  * the start of a sentence, moves backward to the next one.  Sentence
  * boundaries are determined by Pango and should be correct for nearly
  * any language (if not, the correct fix would be to the Pango text
@@ -3059,7 +3489,9 @@ gtk_text_iter_backward_sentence_start (GtkTextIter      *iter)
  * @iter: a #GtkTextIter
  * @count: number of sentences to move
  * 
- * Calls gtk_text_iter_forward_sentence_end() up to @count times.
+ * Calls gtk_text_iter_forward_sentence_end() @count times (or until
+ * gtk_text_iter_forward_sentence_end() returns %FALSE). If @count is
+ * negative, moves backward instead of forward.
  * 
  * Return value: %TRUE if @iter moved and is not the end iterator
  **/
@@ -3067,33 +3499,19 @@ gboolean
 gtk_text_iter_forward_sentence_ends (GtkTextIter      *iter,
                                      gint              count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
-
-  if (count == 0)
-    return FALSE;
-
-  if (count < 0)
-    return gtk_text_iter_backward_sentence_starts (iter, -count);
-
-  if (!gtk_text_iter_forward_sentence_end (iter))
-    return FALSE;
-  --count;
-
-  while (count > 0)
-    {
-      if (!gtk_text_iter_forward_sentence_end (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_forward_sentence_end,
+                             gtk_text_iter_backward_sentence_starts);
 }
 
 /**
- * gtk_text_iter_forward_sentence_ends:
+ * gtk_text_iter_backward_sentence_starts:
  * @iter: a #GtkTextIter
  * @count: number of sentences to move
  * 
- * Calls gtk_text_iter_backward_sentence_start() up to @count times.
+ * Calls gtk_text_iter_backward_sentence_start() up to @count times,
+ * or until it returns %FALSE. If @count is negative, moves forward
+ * instead of backward.
  * 
  * Return value: %TRUE if @iter moved and is not the end iterator
  **/
@@ -3101,22 +3519,9 @@ gboolean
 gtk_text_iter_backward_sentence_starts (GtkTextIter      *iter,
                                         gint               count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
-
-  if (count < 0)
-    return gtk_text_iter_forward_sentence_ends (iter, -count);
-
-  if (!gtk_text_iter_backward_sentence_start (iter))
-    return FALSE;
-  --count;
-
-  while (count > 0)
-    {
-      if (!gtk_text_iter_backward_sentence_start (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_backward_sentence_start,
+                             gtk_text_iter_forward_sentence_ends);
 }
 
 static gboolean
@@ -3146,7 +3551,7 @@ find_backward_cursor_pos_func (const PangoLogAttr *attrs,
                                gint          len,
                                gint         *found_offset,
                                gboolean      already_moved_initially)
-{
+{  
   if (!already_moved_initially)
     --offset;
 
@@ -3197,7 +3602,7 @@ gtk_text_iter_forward_cursor_position (GtkTextIter *iter)
  * 
  * Like gtk_text_iter_forward_cursor_position(), but moves backward.
  * 
- * Return value: %TRUE if we moved and the new position is dereferenceable
+ * Return value: %TRUE if we moved
  **/
 gboolean
 gtk_text_iter_backward_cursor_position (GtkTextIter *iter)
@@ -3219,25 +3624,9 @@ gboolean
 gtk_text_iter_forward_cursor_positions (GtkTextIter *iter,
                                         gint         count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
-
-  if (count == 0)
-    return FALSE;
-  
-  if (count < 0)
-    return gtk_text_iter_backward_cursor_positions (iter, -count);
-  
-  if (!gtk_text_iter_forward_cursor_position (iter))
-    return FALSE;
-  --count;
-
-  while (count > 0)
-    {
-      if (!gtk_text_iter_forward_cursor_position (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_forward_cursor_position,
+                             gtk_text_iter_backward_cursor_positions);
 }
 
 /**
@@ -3254,25 +3643,85 @@ gboolean
 gtk_text_iter_backward_cursor_positions (GtkTextIter *iter,
                                          gint         count)
 {
-  g_return_val_if_fail (iter != NULL, FALSE);
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_backward_cursor_position,
+                             gtk_text_iter_forward_cursor_positions);
+}
 
-  if (count == 0)
-    return FALSE;
+/**
+ * gtk_text_iter_forward_visible_cursor_position:
+ * @iter: a #GtkTextIter
+ * 
+ * Moves @iter forward to the next visible cursor position. See 
+ * gtk_text_iter_forward_cursor_position() for details.
+ * 
+ * Return value: %TRUE if we moved and the new position is dereferenceable
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_forward_visible_cursor_position (GtkTextIter *iter)
+{
+  return find_visible_by_log_attrs (iter, find_forward_cursor_pos_func, TRUE, FALSE);
+}
 
-  if (count < 0)
-    return gtk_text_iter_forward_cursor_positions (iter, -count);
-  
-  if (!gtk_text_iter_backward_cursor_position (iter))
-    return FALSE;
-  --count;
+/**
+ * gtk_text_iter_backward_visible_cursor_position:
+ * @iter: a #GtkTextIter
+ * 
+ * Moves @iter forward to the previous visible cursor position. See 
+ * gtk_text_iter_backward_cursor_position() for details.
+ * 
+ * Return value: %TRUE if we moved and the new position is dereferenceable
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_backward_visible_cursor_position (GtkTextIter *iter)
+{
+  return find_visible_by_log_attrs (iter, find_backward_cursor_pos_func, FALSE, FALSE);
+}
 
-  while (count > 0)
-    {
-      if (!gtk_text_iter_backward_cursor_position (iter))
-        break;
-      --count;
-    }
-  return TRUE;
+/**
+ * gtk_text_iter_forward_visible_cursor_positions:
+ * @iter: a #GtkTextIter
+ * @count: number of positions to move
+ * 
+ * Moves up to @count visible cursor positions. See
+ * gtk_text_iter_forward_cursor_position() for details.
+ * 
+ * Return value: %TRUE if we moved and the new position is dereferenceable
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_forward_visible_cursor_positions (GtkTextIter *iter,
+                                               gint         count)
+{
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_forward_visible_cursor_position,
+                             gtk_text_iter_backward_visible_cursor_positions);
+}
+
+/**
+ * gtk_text_iter_backward_visible_cursor_positions:
+ * @iter: a #GtkTextIter
+ * @count: number of positions to move
+ *
+ * Moves up to @count visible cursor positions. See
+ * gtk_text_iter_backward_cursor_position() for details.
+ * 
+ * Return value: %TRUE if we moved and the new position is dereferenceable
+ * 
+ * Since: 2.4
+ **/
+gboolean
+gtk_text_iter_backward_visible_cursor_positions (GtkTextIter *iter,
+                                                gint         count)
+{
+  return move_multiple_steps (iter, count, 
+                             gtk_text_iter_backward_visible_cursor_position,
+                             gtk_text_iter_forward_visible_cursor_positions);
 }
 
 /**
@@ -3327,7 +3776,7 @@ gtk_text_iter_set_line_offset (GtkTextIter *iter,
     iter_set_from_char_offset (real, real->line, char_on_line);
   else
     gtk_text_iter_forward_line (iter); /* set to start of next line */
-
+  
   check_invariants (iter);
 }
 
@@ -3396,6 +3845,8 @@ gtk_text_iter_set_visible_line_offset (GtkTextIter *iter,
 
   g_return_if_fail (iter != NULL);
   
+  gtk_text_iter_set_line_offset (iter, 0);
+
   pos = *iter;
 
   /* For now we use a ludicrously slow implementation */
@@ -3417,12 +3868,6 @@ gtk_text_iter_set_visible_line_offset (GtkTextIter *iter,
     gtk_text_iter_forward_line (iter);
 }
 
-static gint
-bytes_in_char (GtkTextIter *iter)
-{
-  return g_unichar_to_utf8 (gtk_text_iter_get_char (iter), NULL);
-}
-
 /**
  * gtk_text_iter_set_visible_line_index:
  * @iter: a #GtkTextIter
@@ -3433,36 +3878,51 @@ bytes_in_char (GtkTextIter *iter)
  * in the index.
  **/
 void
-gtk_text_iter_set_visible_line_index  (GtkTextIter *iter,
-                                       gint         byte_on_line)
+gtk_text_iter_set_visible_line_index (GtkTextIter *iter,
+                                      gint         byte_on_line)
 {
-  gint bytes_seen = 0;
+  GtkTextRealIter *real;
+  gint offset = 0;
   GtkTextIter pos;
-
-  g_return_if_fail (iter != NULL);
+  GtkTextLineSegment *seg;
   
+  g_return_if_fail (iter != NULL);
+
+  gtk_text_iter_set_line_offset (iter, 0);
+
   pos = *iter;
 
-  /* For now we use a ludicrously slow implementation */
-  while (bytes_seen < byte_on_line)
+  real = gtk_text_iter_make_real (&pos);
+
+  if (real == NULL)
+    return;
+
+  ensure_byte_offsets (real);
+
+  check_invariants (&pos);
+
+  seg = _gtk_text_iter_get_indexable_segment (&pos);
+
+  while (seg != NULL && byte_on_line > 0)
     {
       if (!_gtk_text_btree_char_is_invisible (&pos))
-        bytes_seen += bytes_in_char (&pos);
-
-      if (!gtk_text_iter_forward_char (&pos))
-        break;
+        {
+          if (byte_on_line < seg->byte_count)
+            {
+              iter_set_from_byte_offset (real, real->line, offset + byte_on_line);
+              byte_on_line = 0;
+              break;
+            }
+          else
+            byte_on_line -= seg->byte_count;
+        }
 
-      if (bytes_seen >= byte_on_line)
-        break;
+      offset += seg->byte_count;
+      _gtk_text_iter_forward_indexable_segment (&pos);
+      seg = _gtk_text_iter_get_indexable_segment (&pos);
     }
 
-  if (bytes_seen > byte_on_line)
-    g_warning ("%s: Incorrect visible byte index %d falls in the middle of a UTF-8 "
-               "character; this will crash the text buffer. "
-               "Byte indexes must refer to the start of a character.",
-               G_STRLOC, byte_on_line);
-  
-  if (_gtk_text_iter_get_text_line (&pos) == _gtk_text_iter_get_text_line (iter))
+  if (byte_on_line == 0)
     *iter = pos;
   else
     gtk_text_iter_forward_line (iter);
@@ -3473,7 +3933,9 @@ gtk_text_iter_set_visible_line_index  (GtkTextIter *iter,
  * @iter: a #GtkTextIter
  * @line_number: line number (counted from 0)
  *
- * Moves iterator @iter to the start of the line @line_number.
+ * Moves iterator @iter to the start of the line @line_number.  If
+ * @line_number is negative or larger than the number of lines in the
+ * buffer, moves @iter to the start of the last line in the buffer.
  * 
  **/
 void
@@ -3493,7 +3955,7 @@ gtk_text_iter_set_line (GtkTextIter *iter,
 
   check_invariants (iter);
 
-  line = _gtk_text_btree_get_line (real->tree, line_number, &real_line);
+  line = _gtk_text_btree_get_line_no_last (real->tree, line_number, &real_line);
 
   iter_set_from_char_offset (real, line, 0);
 
@@ -3510,7 +3972,6 @@ gtk_text_iter_set_line (GtkTextIter *iter,
  *
  * Sets @iter to point to @char_offset. @char_offset counts from the start
  * of the entire text buffer, starting with 0.
- * 
  **/
 void
 gtk_text_iter_set_offset (GtkTextIter *iter,
@@ -3554,7 +4015,6 @@ gtk_text_iter_set_offset (GtkTextIter *iter,
  * Moves @iter forward to the "end iterator," which points one past the last
  * valid character in the buffer. gtk_text_iter_get_char() called on the
  * end iterator returns 0, which is convenient for writing loops.
- * 
  **/
 void
 gtk_text_iter_forward_to_end  (GtkTextIter *iter)
@@ -3574,6 +4034,36 @@ gtk_text_iter_forward_to_end  (GtkTextIter *iter)
   gtk_text_buffer_get_end_iter (buffer, iter);
 }
 
+/* FIXME this and gtk_text_iter_forward_to_line_end() could be cleaned up
+ * and made faster. Look at iter_ends_line() for inspiration, perhaps.
+ * If all else fails we could cache the para delimiter pos in the iter.
+ * I think forward_to_line_end() actually gets called fairly often.
+ */
+static int
+find_paragraph_delimiter_for_line (GtkTextIter *iter)
+{
+  GtkTextIter end;
+  end = *iter;
+
+  if (_gtk_text_line_contains_end_iter (_gtk_text_iter_get_text_line (&end),
+                                        _gtk_text_iter_get_btree (&end)))
+    {
+      gtk_text_iter_forward_to_end (&end);
+    }
+  else
+    {
+      /* if we aren't on the last line, go forward to start of next line, then scan
+       * back for the delimiters on the previous line
+       */
+      gtk_text_iter_forward_line (&end);
+      gtk_text_iter_backward_char (&end);
+      while (!gtk_text_iter_ends_line (&end))
+        gtk_text_iter_backward_char (&end);
+    }
+
+  return gtk_text_iter_get_line_offset (&end);
+}
+
 /**
  * gtk_text_iter_forward_to_line_end:
  * @iter: a #GtkTextIter
@@ -3583,7 +4073,9 @@ gtk_text_iter_forward_to_end  (GtkTextIter *iter)
  * return/newline in sequence, or the Unicode paragraph separator
  * character. If the iterator is already at the paragraph delimiter
  * characters, moves to the paragraph delimiter characters for the
- * next line.
+ * next line. If @iter is on the last line in the buffer, which does
+ * not end in paragraph delimiters, moves to the end iterator (end of
+ * the last line), and returns %FALSE.
  * 
  * Return value: %TRUE if we moved and the new location is not the end iterator
  **/
@@ -3593,17 +4085,17 @@ gtk_text_iter_forward_to_line_end (GtkTextIter *iter)
   gint current_offset;
   gint new_offset;
 
+  
   g_return_val_if_fail (iter != NULL, FALSE);
 
   current_offset = gtk_text_iter_get_line_offset (iter);
-  /* FIXME assumption that line ends in a newline; broken */
-  new_offset = gtk_text_iter_get_chars_in_line (iter) - 1;
-
+  new_offset = find_paragraph_delimiter_for_line (iter);
+  
   if (current_offset < new_offset)
     {
       /* Move to end of this line. */
       gtk_text_iter_set_line_offset (iter, new_offset);
-      return TRUE;
+      return !gtk_text_iter_is_end (iter);
     }
   else
     {
@@ -3615,7 +4107,7 @@ gtk_text_iter_forward_to_line_end (GtkTextIter *iter)
            */
           if (!gtk_text_iter_ends_line (iter))
             gtk_text_iter_forward_to_line_end (iter);
-          return TRUE;
+          return !gtk_text_iter_is_end (iter);
         }
       else
         return FALSE;
@@ -3625,12 +4117,12 @@ gtk_text_iter_forward_to_line_end (GtkTextIter *iter)
 /**
  * gtk_text_iter_forward_to_tag_toggle:
  * @iter: a #GtkTextIter
- * @tag: a #GtkTextTag, or NULL
+ * @tag: a #GtkTextTag, or %NULL
  *
  * Moves forward to the next toggle (on or off) of the
  * #GtkTextTag @tag, or to the next toggle of any tag if
- * @tag is NULL. If no matching tag toggles are found,
- * returns FALSE, otherwise TRUE. Does not return toggles
+ * @tag is %NULL. If no matching tag toggles are found,
+ * returns %FALSE, otherwise %TRUE. Does not return toggles
  * located at @iter, only toggles after @iter. Sets @iter to
  * the location of the toggle, or to the end of the buffer
  * if no toggle is found.
@@ -3656,7 +4148,7 @@ gtk_text_iter_forward_to_tag_toggle (GtkTextIter *iter,
 
   current_line = real->line;
   next_line = _gtk_text_line_next_could_contain_tag (current_line,
-                                                    real->tree, tag);
+                                                     real->tree, tag);
 
   while (_gtk_text_iter_forward_indexable_segment (iter))
     {
@@ -3678,8 +4170,8 @@ gtk_text_iter_forward_to_tag_toggle (GtkTextIter *iter,
 
           current_line = real->line;
           next_line = _gtk_text_line_next_could_contain_tag (current_line,
-                                                            real->tree,
-                                                            tag);
+                                                             real->tree,
+                                                             tag);
         }
 
       if (gtk_text_iter_toggles_tag (iter, tag))
@@ -3707,12 +4199,12 @@ gtk_text_iter_forward_to_tag_toggle (GtkTextIter *iter,
 /**
  * gtk_text_iter_backward_to_tag_toggle:
  * @iter: a #GtkTextIter
- * @tag: a #GtkTextTag, or NULL
+ * @tag: a #GtkTextTag, or %NULL
  *
  * Moves backward to the next toggle (on or off) of the
  * #GtkTextTag @tag, or to the next toggle of any tag if
- * @tag is NULL. If no matching tag toggles are found,
- * returns FALSE, otherwise TRUE. Does not return toggles
+ * @tag is %NULL. If no matching tag toggles are found,
+ * returns %FALSE, otherwise %TRUE. Does not return toggles
  * located at @iter, only toggles before @iter. Sets @iter
  * to the location of the toggle, or the start of the buffer
  * if no toggle is found.
@@ -4084,23 +4576,32 @@ strbreakup (const char *string,
  * gtk_text_iter_forward_search:
  * @iter: start of search
  * @str: a search string
- * @visible_only: if %TRUE, search only visible text
- * @slice: if %TRUE, @str contains 0xFFFC when we want to match widgets, pixbufs
+ * @flags: flags affecting how the search is done
  * @match_start: return location for start of match, or %NULL
  * @match_end: return location for end of match, or %NULL
  * @limit: bound for the search, or %NULL for the end of the buffer
  * 
- * Searches forward for @str. Any match is returned as the range @match_start,
- * @match_end. If you specify @visible_only or @slice, the match may have
- * invisible text, pixbufs, or child widgets interspersed in @str.
+ * Searches forward for @str. Any match is returned by setting 
+ * @match_start to the first character of the match and @match_end to the 
+ * first character after the match. The search will not continue past
+ * @limit. Note that a search is a linear or O(n) operation, so you
+ * may wish to use @limit to avoid locking up your UI on large
+ * buffers.
  * 
+ * If the #GTK_TEXT_SEARCH_VISIBLE_ONLY flag is present, the match may
+ * have invisible text interspersed in @str. i.e. @str will be a
+ * possibly-noncontiguous subsequence of the matched range. similarly,
+ * if you specify #GTK_TEXT_SEARCH_TEXT_ONLY, the match may have
+ * pixbufs or child widgets mixed inside the matched range. If these
+ * flags are not given, the match must be exact; the special 0xFFFC
+ * character in @str will match embedded pixbufs or child widgets.
+ *
  * Return value: whether a match was found
  **/
 gboolean
 gtk_text_iter_forward_search (const GtkTextIter *iter,
                               const gchar       *str,
-                              gboolean           visible_only,
-                              gboolean           slice,
+                              GtkTextSearchFlags flags,
                               GtkTextIter       *match_start,
                               GtkTextIter       *match_end,
                               const GtkTextIter *limit)
@@ -4109,7 +4610,9 @@ gtk_text_iter_forward_search (const GtkTextIter *iter,
   GtkTextIter match;
   gboolean retval = FALSE;
   GtkTextIter search;
-
+  gboolean visible_only;
+  gboolean slice;
+  
   g_return_val_if_fail (iter != NULL, FALSE);
   g_return_val_if_fail (str != NULL, FALSE);
 
@@ -4138,6 +4641,9 @@ gtk_text_iter_forward_search (const GtkTextIter *iter,
         return FALSE;
     }
 
+  visible_only = (flags & GTK_TEXT_SEARCH_VISIBLE_ONLY) != 0;
+  slice = (flags & GTK_TEXT_SEARCH_TEXT_ONLY) == 0;
+  
   /* locate all lines */
 
   lines = strbreakup (str, "\n", -1);
@@ -4161,7 +4667,7 @@ gtk_text_iter_forward_search (const GtkTextIter *iter,
         {
           if (limit == NULL ||
               (limit &&
-               gtk_text_iter_compare (&end, limit) < 0))
+               gtk_text_iter_compare (&end, limit) <= 0))
             {
               retval = TRUE;
               
@@ -4253,7 +4759,7 @@ lines_window_init (LinesWindow       *win,
   /* If we start on line 1, there are 2 lines to search (0 and 1), so
    * n_lines can be 2.
    */
-  if (gtk_text_iter_is_first (start) ||
+  if (gtk_text_iter_is_start (start) ||
       gtk_text_iter_get_line (start) + 1 < win->n_lines)
     {
       /* Already at the end, or not enough lines to match */
@@ -4363,49 +4869,11 @@ lines_window_free (LinesWindow *win)
   g_strfreev (win->lines);
 }
 
-static gchar*
-my_strrstr (const gchar *haystack,
-            const gchar *needle)
-{
-  /* FIXME GLib should have a nice implementation in it, this
-   * is slow-ass crap.
-   */
-
-  gint haystack_len = strlen (haystack);
-  gint needle_len = strlen (needle);
-  const gchar *needle_end = needle + needle_len;
-  const gchar *haystack_rend = haystack - 1;
-  const gchar *needle_rend = needle - 1;
-  const gchar *p;
-
-  p = haystack + haystack_len;
-  while (p != haystack)
-    {
-      const gchar *n = needle_end - 1;
-      const gchar *s = p - 1;
-      while (s != haystack_rend &&
-             n != needle_rend &&
-             *s == *n)
-        {
-          --n;
-          --s;
-        }
-
-      if (n == needle_rend)
-        return (gchar*)++s;
-
-      --p;
-    }
-
-  return NULL;
-}
-
 /**
  * gtk_text_iter_backward_search:
  * @iter: a #GtkTextIter where the search begins
  * @str: search string
- * @visible_only: if %TRUE search only visible text
- * @slice: if %TRUE the search string contains 0xFFFC to match pixbufs, widgets
+ * @flags: bitmask of flags affecting the search
  * @match_start: return location for start of match, or %NULL
  * @match_end: return location for end of match, or %NULL
  * @limit: location of last possible @match_start, or %NULL for start of buffer
@@ -4417,8 +4885,7 @@ my_strrstr (const gchar *haystack,
 gboolean
 gtk_text_iter_backward_search (const GtkTextIter *iter,
                                const gchar       *str,
-                               gboolean           visible_only,
-                               gboolean           slice,
+                               GtkTextSearchFlags flags,
                                GtkTextIter       *match_start,
                                GtkTextIter       *match_end,
                                const GtkTextIter *limit)
@@ -4428,7 +4895,9 @@ gtk_text_iter_backward_search (const GtkTextIter *iter,
   gint n_lines;
   LinesWindow win;
   gboolean retval = FALSE;
-
+  gboolean visible_only;
+  gboolean slice;
+  
   g_return_val_if_fail (iter != NULL, FALSE);
   g_return_val_if_fail (str != NULL, FALSE);
 
@@ -4456,6 +4925,9 @@ gtk_text_iter_backward_search (const GtkTextIter *iter,
         return FALSE;
     }
 
+  visible_only = (flags & GTK_TEXT_SEARCH_VISIBLE_ONLY) != 0;
+  slice = (flags & GTK_TEXT_SEARCH_TEXT_ONLY) == 0;
+  
   /* locate all lines */
 
   lines = strbreakup (str, "\n", -1);
@@ -4492,7 +4964,7 @@ gtk_text_iter_backward_search (const GtkTextIter *iter,
        * end in '\n', so this will only match at the
        * end of the first line, which is correct.
        */
-      first_line_match = my_strrstr (*win.lines, *lines);
+      first_line_match = g_strrstr (*win.lines, *lines);
 
       if (first_line_match &&
           vectors_equal_ignoring_trailing (lines + 1, win.lines + 1))
@@ -4666,9 +5138,8 @@ gtk_text_iter_compare (const GtkTextIter *lhs,
  * @start: start of range
  * @end: end of range
  * 
- * @start and @end must be in order, unlike most text buffer
- * functions, for efficiency reasons. The function returns %TRUE if
- * @iter falls in the range [@start, @end).
+ * Checks whether @iter falls in the range [@start, @end).
+ * @start and @end must be in ascending order.
  * 
  * Return value: %TRUE if @iter is in the range
  **/
@@ -4677,12 +5148,17 @@ gtk_text_iter_in_range (const GtkTextIter *iter,
                         const GtkTextIter *start,
                         const GtkTextIter *end)
 {
+  g_return_val_if_fail (iter != NULL, FALSE);
+  g_return_val_if_fail (start != NULL, FALSE);
+  g_return_val_if_fail (end != NULL, FALSE);
+  g_return_val_if_fail (gtk_text_iter_compare (start, end) <= 0, FALSE);
+  
   return gtk_text_iter_compare (iter, start) >= 0 &&
     gtk_text_iter_compare (iter, end) < 0;
 }
 
 /**
- * gtk_text_iter_reorder:
+ * gtk_text_iter_order:
  * @first: a #GtkTextIter
  * @second: another #GtkTextIter
  *
@@ -4695,8 +5171,8 @@ gtk_text_iter_in_range (const GtkTextIter *iter,
  * 
  **/
 void
-gtk_text_iter_reorder         (GtkTextIter *first,
-                               GtkTextIter *second)
+gtk_text_iter_order (GtkTextIter *first,
+                     GtkTextIter *second)
 {
   g_return_if_fail (first != NULL);
   g_return_if_fail (second != NULL);
@@ -4729,7 +5205,7 @@ _gtk_text_btree_get_iter_at_char (GtkTextBTree *tree,
   g_return_if_fail (tree != NULL);
 
   line = _gtk_text_btree_get_line_at_char (tree, char_index,
-                                          &line_start, &real_char_index);
+                                           &line_start, &real_char_index);
 
   iter_init_from_char_offset (iter, tree, line, real_char_index - line_start);
 
@@ -4740,9 +5216,9 @@ _gtk_text_btree_get_iter_at_char (GtkTextBTree *tree,
 
 void
 _gtk_text_btree_get_iter_at_line_char (GtkTextBTree *tree,
-                                       GtkTextIter *iter,
-                                       gint line_number,
-                                       gint char_on_line)
+                                       GtkTextIter  *iter,
+                                       gint          line_number,
+                                       gint          char_on_line)
 {
   GtkTextRealIter *real = (GtkTextRealIter*)iter;
   GtkTextLine *line;
@@ -4751,7 +5227,7 @@ _gtk_text_btree_get_iter_at_line_char (GtkTextBTree *tree,
   g_return_if_fail (iter != NULL);
   g_return_if_fail (tree != NULL);
 
-  line = _gtk_text_btree_get_line (tree, line_number, &real_line);
+  line = _gtk_text_btree_get_line_no_last (tree, line_number, &real_line);
   
   iter_init_from_char_offset (iter, tree, line, char_on_line);
 
@@ -4774,7 +5250,7 @@ _gtk_text_btree_get_iter_at_line_byte (GtkTextBTree   *tree,
   g_return_if_fail (iter != NULL);
   g_return_if_fail (tree != NULL);
 
-  line = _gtk_text_btree_get_line (tree, line_number, &real_line);
+  line = _gtk_text_btree_get_line_no_last (tree, line_number, &real_line);
 
   iter_init_from_byte_offset (iter, tree, line, byte_index);
 
@@ -4801,8 +5277,8 @@ _gtk_text_btree_get_iter_at_line      (GtkTextBTree   *tree,
 
 gboolean
 _gtk_text_btree_get_iter_at_first_toggle (GtkTextBTree   *tree,
-                                         GtkTextIter    *iter,
-                                         GtkTextTag     *tag)
+                                          GtkTextIter    *iter,
+                                          GtkTextTag     *tag)
 {
   GtkTextLine *line;
 
@@ -4821,7 +5297,10 @@ _gtk_text_btree_get_iter_at_first_toggle (GtkTextBTree   *tree,
   else
     {
       iter_init_from_byte_offset (iter, tree, line, 0);
-      gtk_text_iter_forward_to_tag_toggle (iter, tag);
+
+      if (!gtk_text_iter_toggles_tag (iter, tag))
+        gtk_text_iter_forward_to_tag_toggle (iter, tag);
+
       check_invariants (iter);
       return TRUE;
     }
@@ -4829,36 +5308,23 @@ _gtk_text_btree_get_iter_at_first_toggle (GtkTextBTree   *tree,
 
 gboolean
 _gtk_text_btree_get_iter_at_last_toggle  (GtkTextBTree   *tree,
-                                         GtkTextIter    *iter,
-                                         GtkTextTag     *tag)
+                                          GtkTextIter    *iter,
+                                          GtkTextTag     *tag)
 {
-  GtkTextLine *line;
-
   g_return_val_if_fail (iter != NULL, FALSE);
   g_return_val_if_fail (tree != NULL, FALSE);
 
-  line = _gtk_text_btree_last_could_contain_tag (tree, tag);
-
-  if (line == NULL)
-    {
-      /* Set iter to first in tree */
-      _gtk_text_btree_get_iter_at_line_char (tree, iter, 0, 0);
-      check_invariants (iter);
-      return FALSE;
-    }
-  else
-    {
-      iter_init_from_byte_offset (iter, tree, line, -1);
-      gtk_text_iter_backward_to_tag_toggle (iter, tag);
-      check_invariants (iter);
-      return TRUE;
-    }
+  _gtk_text_btree_get_end_iter (tree, iter);
+  gtk_text_iter_backward_to_tag_toggle (iter, tag);
+  check_invariants (iter);
+  
+  return TRUE;
 }
 
 gboolean
 _gtk_text_btree_get_iter_at_mark_name (GtkTextBTree *tree,
-                                      GtkTextIter *iter,
-                                      const gchar *mark_name)
+                                       GtkTextIter *iter,
+                                       const gchar *mark_name)
 {
   GtkTextMark *mark;
 
@@ -4879,8 +5345,8 @@ _gtk_text_btree_get_iter_at_mark_name (GtkTextBTree *tree,
 
 void
 _gtk_text_btree_get_iter_at_mark (GtkTextBTree *tree,
-                                 GtkTextIter *iter,
-                                 GtkTextMark *mark)
+                                  GtkTextIter *iter,
+                                  GtkTextMark *mark)
 {
   GtkTextLineSegment *seg;
 
@@ -4898,16 +5364,18 @@ _gtk_text_btree_get_iter_at_mark (GtkTextBTree *tree,
 
 void
 _gtk_text_btree_get_iter_at_child_anchor (GtkTextBTree       *tree,
-                                         GtkTextIter        *iter,
-                                         GtkTextChildAnchor *anchor)
+                                          GtkTextIter        *iter,
+                                          GtkTextChildAnchor *anchor)
 {
   GtkTextLineSegment *seg;
 
   g_return_if_fail (iter != NULL);
   g_return_if_fail (tree != NULL);
   g_return_if_fail (GTK_IS_TEXT_CHILD_ANCHOR (anchor));
+  
+  seg = anchor->segment;  
 
-  seg = anchor->segment;
+  g_assert (seg->body.child.line != NULL);
   
   iter_init_from_segment (iter, tree,
                           seg->body.child.line, seg);
@@ -4928,28 +5396,6 @@ _gtk_text_btree_get_end_iter         (GtkTextBTree   *tree,
   check_invariants (iter);
 }
 
-void
-gtk_text_iter_spew (const GtkTextIter *iter, const gchar *desc)
-{
-  GtkTextRealIter *real = (GtkTextRealIter*)iter;
-
-  g_return_if_fail (iter != NULL);
-
-  if (real->chars_changed_stamp != _gtk_text_btree_get_chars_changed_stamp (real->tree))
-    g_print (" %20s: <invalidated iterator>\n", desc);
-  else
-    {
-      check_invariants (iter);
-      g_print (" %20s: line %d / char %d / line char %d / line byte %d\n",
-               desc,
-               gtk_text_iter_get_line (iter),
-               gtk_text_iter_get_offset (iter),
-               gtk_text_iter_get_line_offset (iter),
-               gtk_text_iter_get_line_index (iter));
-      check_invariants (iter);
-    }
-}
-
 void
 _gtk_text_iter_check (const GtkTextIter *iter)
 {
@@ -5128,5 +5574,10 @@ _gtk_text_iter_check (const GtkTextIter *iter)
             g_error ("wrong char index was cached");
         }
     }
+
+  if (_gtk_text_line_is_last (real->line, real->tree))
+    g_error ("Iterator was on last line (past the end iterator)");
 }
 
+#define __GTK_TEXT_ITER_C__
+#include "gtkaliasdef.c"