TextIter
- class TextIter(*args, **kwargs)
An iterator for the contents of a GtkTextBuffer
.
You may wish to begin by reading the text widget conceptual overview, which gives an overview of all the objects and data types related to the text widget and how they work together.
Methods
- class TextIter
- assign(other: TextIter) None
Assigns the value of
other
toiter
.This function is not useful in applications, because iterators can be assigned with
GtkTextIter i = j;
.The function is used by language bindings.
- Parameters:
other – another
GtkTextIter
- backward_char() bool
Moves backward by one character offset.
Returns
True
if movement was possible; ifiter
was the first in the buffer (character offset 0), this function returnsFalse
for convenience when writing loops.
- backward_chars(count: int) bool
Moves
count
characters 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. Ifcount
is 0, the function does nothing and returnsFalse
.- Parameters:
count – number of characters to move
- backward_cursor_position() bool
Like
forward_cursor_position
, but moves backward.
- backward_cursor_positions(count: int) bool
Moves up to
count
cursor positions.See
forward_cursor_position
for details.- Parameters:
count – number of positions to move
- backward_find_char(pred: Callable[[...], bool], limit: TextIter | None = None, *user_data: Any) bool
Same as
forward_find_char
, but goes backward fromiter
.- Parameters:
pred – function to be called on each character
limit – search limit
user_data – user data for
pred
- backward_line() bool
Moves
iter
to the start of the previous line.Returns
True
ifiter
could be moved; i.e. ifiter
was at character offset 0, this function returnsFalse
. Therefore, ifiter
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 returnsTrue
. (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.)
- backward_lines(count: int) bool
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. Ifcount
is 0, the function does nothing and returnsFalse
. Ifcount
is negative, moves forward by 0 -count
lines.- Parameters:
count – number of lines to move backward
- backward_search(str: str, flags: TextSearchFlags, limit: TextIter | None = None) tuple[bool, TextIter, TextIter]
Same as
forward_search
, but moves backward.match_end
will never be set to aGtkTextIter
located afteriter
, even if there is a possiblematch_start
before or atiter
.- Parameters:
str – search string
flags – bitmask of flags affecting the search
limit – location of last possible
match_start
, orNone
for start of buffer
- backward_sentence_start() bool
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.
- backward_sentence_starts(count: int) bool
Calls
backward_sentence_start
up tocount
times.If
count
is negative, moves forward instead of backward.- Parameters:
count – number of sentences to move
- backward_to_tag_toggle(tag: TextTag | None = None) bool
Moves backward to the next toggle (on or off) of the
tag
, or to the next toggle of any tag iftag
isNone
.If no matching tag toggles are found, returns
False
, otherwiseTrue
. Does not return toggles located atiter
, only toggles beforeiter
. Setsiter
to the location of the toggle, or the start of the buffer if no toggle is found.- Parameters:
tag – a
GtkTextTag
- backward_visible_cursor_position() bool
Moves
iter
backward to the previous visible cursor position.See
backward_cursor_position
for details.
- backward_visible_cursor_positions(count: int) bool
Moves up to
count
visible cursor positions.See
backward_cursor_position
for details.- Parameters:
count – number of positions to move
- backward_visible_line() bool
Moves
iter
to the start of the previous visible line.Returns
True
ifiter
could be moved; i.e. ifiter
was at character offset 0, this function returnsFalse
. Therefore ifiter
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 returnsTrue
. (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.)
- backward_visible_lines(count: int) bool
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. Ifcount
is 0, the function does nothing and returnsFalse
. Ifcount
is negative, moves forward by 0 -count
lines.- Parameters:
count – number of lines to move backward
- backward_visible_word_start() bool
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.
- backward_visible_word_starts(count: int) bool
Calls
backward_visible_word_start
up tocount
times.- Parameters:
count – number of times to move
- backward_word_start() bool
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
- backward_word_starts(count: int) bool
Calls
backward_word_start
up tocount
times.- Parameters:
count – number of times to move
- can_insert(default_editability: bool) bool
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 atiter
.insert_interactive
uses this function to decide whether insertions are allowed at a given position.- Parameters:
default_editability –
True
if text is editable by default
- compare(rhs: TextIter) int
A qsort()-style function that returns negative if
lhs
is less thanrhs
, positive iflhs
is greater thanrhs
, and 0 if they’re equal.Ordering is in character offset order, i.e. the first character in the buffer is less than the second character in the buffer.
- Parameters:
rhs – another
GtkTextIter
- editable(default_setting: bool) bool
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
. 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 atiter
is inside an editable range, you want to know whether a new character inserted atiter
would be inside an editable range. Usecan_insert
to handle this case.- Parameters:
default_setting –
True
if text is editable by default
- ends_line() bool
Returns
True
ifiter
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 rn 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.
- ends_sentence() bool
Determines whether
iter
ends a sentence.Sentence boundaries are determined by Pango and should be correct for nearly any language.
- ends_tag(tag: TextTag | None = None) bool
Returns
True
iftag
is toggled off at exactly this point.If
tag
isNone
, returnsTrue
if any tag is toggled off at this point.Note that if this function returns
True
, it means thatiter
is at the end of the tagged range, but that the character atiter
is outside the tagged range. In other words, unlikestarts_tag
, if this function returnsTrue
,has_tag
will returnFalse
for the same parameters.- Parameters:
tag – a
GtkTextTag
- ends_word() bool
Determines whether
iter
ends a natural-language word.Word breaks are determined by Pango and should be correct for nearly any language.
- equal(rhs: TextIter) bool
Tests whether two iterators are equal, using the fastest possible mechanism.
This function is very fast; you can expect it to perform better than e.g. getting the character offset for each iterator and comparing the offsets yourself. Also, it’s a bit faster than
compare
.- Parameters:
rhs – another
GtkTextIter
- forward_char() bool
Moves
iter
forward by one character offset.Note that images embedded in the buffer occupy 1 character slot, so this function 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 this function returnsFalse
for convenience when writing loops.
- forward_chars(count: int) bool
Moves
count
characters 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 new position of
iter
is different from its original position, and dereferenceable (the last iterator in the buffer is not dereferenceable). Ifcount
is 0, the function does nothing and returnsFalse
.- Parameters:
count – number of characters to move, may be negative
- forward_cursor_position() bool
Moves
iter
forward by a single cursor position.Cursor positions are (unsurprisingly) positions where the cursor can appear. Perhaps surprisingly, there may not be a cursor position between all characters. The most common example for European languages would be a carriage return/newline sequence.
For some Unicode characters, the equivalent of say the letter “a” with an accent mark will be represented as two characters, first the letter then a “combining mark” that causes the accent to be rendered; so the cursor can’t go between those two characters.
See also the
LogAttr
struct and thebreak
function.
- forward_cursor_positions(count: int) bool
Moves up to
count
cursor positions.See
forward_cursor_position
for details.- Parameters:
count – number of positions to move
- forward_find_char(pred: Callable[[...], bool], limit: TextIter | None = None, *user_data: Any) bool
Advances
iter
, callingpred
on each character.If
pred
returnsTrue
, returnsTrue
and stops scanning. Ifpred
never returnsTrue
,iter
is set tolimit
iflimit
is non-None
, otherwise to the end iterator.- Parameters:
pred – a function to be called on each character
limit – search limit
user_data – user data for
pred
- forward_line() bool
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 dereferenceable, returns
False
. Otherwise, returnsTrue
.
- forward_lines(count: int) bool
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. Ifcount
is 0, the function does nothing and returnsFalse
. Ifcount
is negative, moves backward by 0 -count
lines.- Parameters:
count – number of lines to move forward
- forward_search(str: str, flags: TextSearchFlags, limit: TextIter | None = None) tuple[bool, TextIter, TextIter]
Searches forward for
str
.Any match is returned by setting
match_start
to the first character of the match andmatch_end
to the first character after the match. The search will not continue pastlimit
. Note that a search is a linear or O(n) operation, so you may wish to uselimit
to avoid locking up your UI on large buffers.match_start
will never be set to aGtkTextIter
located beforeiter
, even if there is a possiblematch_end
after or atiter
.- Parameters:
str – a search string
flags – flags affecting how the search is done
limit – location of last possible
match_end
, orNone
for the end of the buffer
- forward_sentence_end() bool
Moves forward to the next sentence end.
If
iter
is at the end of a sentence, moves to the next end of sentence.Sentence boundaries are determined by Pango and should be correct for nearly any language.
- forward_sentence_ends(count: int) bool
Calls
forward_sentence_end
count
times.If
count
is negative, moves backward instead of forward.- Parameters:
count – number of sentences to move
- forward_to_end() None
Moves
iter
forward to the “end iterator”, which points one past the last valid character in the buffer.get_char
called on the end iterator returns 0, which is convenient for writing loops.
- forward_to_line_end() bool
Moves the iterator to point to the paragraph delimiter characters.
The possible characters are either a newline, a carriage return, a carriage 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. 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 returnsFalse
.
- forward_to_tag_toggle(tag: TextTag | None = None) bool
Moves forward to the next toggle (on or off) of the
tag
, or to the next toggle of any tag iftag
isNone
.If no matching tag toggles are found, returns
False
, otherwiseTrue
. Does not return toggles located atiter
, only toggles afteriter
. Setsiter
to the location of the toggle, or to the end of the buffer if no toggle is found.- Parameters:
tag – a
GtkTextTag
- forward_visible_cursor_position() bool
Moves
iter
forward to the next visible cursor position.See
forward_cursor_position
for details.
- forward_visible_cursor_positions(count: int) bool
Moves up to
count
visible cursor positions.See
forward_cursor_position
for details.- Parameters:
count – number of positions to move
- forward_visible_line() bool
Moves
iter
to the start of the next visible line.Returns
True
if there was a next line to move to, andFalse
ifiter
was simply moved to the end of the buffer and is now not dereferenceable, or ifiter
was already at the end of the buffer.
- forward_visible_lines(count: int) bool
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. Ifcount
is 0, the function does nothing and returnsFalse
. Ifcount
is negative, moves backward by 0 -count
lines.- Parameters:
count – number of lines to move forward
- forward_visible_word_end() bool
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
- forward_visible_word_ends(count: int) bool
Calls
forward_visible_word_end
up tocount
times.- Parameters:
count – number of times to move
- forward_word_end() bool
Moves forward to the next 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.
- forward_word_ends(count: int) bool
Calls
forward_word_end
up tocount
times.- Parameters:
count – number of times to move
- free() None
Free an iterator allocated on the heap.
This function is intended for use in language bindings, and is not especially useful for applications, because iterators can simply be allocated on the stack.
- get_buffer() TextBuffer
Returns the
GtkTextBuffer
this iterator is associated with.
- get_bytes_in_line() int
Returns the number of bytes in the line containing
iter
, including the paragraph delimiters.
- get_char() str
The Unicode character at this iterator is returned.
Equivalent to 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.
So you can write a loop which ends when this function returns 0.
- get_chars_in_line() int
Returns the number of characters in the line containing
iter
, including the paragraph delimiters.
- get_child_anchor() TextChildAnchor | None
If the location at
iter
contains a child anchor, the anchor is returned.Otherwise,
None
is returned.
- get_language() Language
Returns the language in effect at
iter
.If no tags affecting language apply to
iter
, the return value is identical to that ofget_default_language
.
- get_line() int
Returns the line number containing the iterator.
Lines in a
GtkTextBuffer
are numbered beginning with 0 for the first line in the buffer.
- get_line_index() int
Returns the byte index of the iterator, counting from the start of a newline-terminated line.
Remember that
GtkTextBuffer
encodes text in UTF-8, and that characters can require a variable number of bytes to represent.
- get_line_offset() int
Returns the character offset of the iterator, counting from the start of a newline-terminated line.
The first character on the line has offset 0.
- get_marks() list[TextMark]
Returns a list of all
GtkTextMark
at this location.Because marks are not iterable (they don’t take up any “space” in the buffer, they are just marks in between iterable locations), multiple marks can exist in the same place.
The returned list is not in any meaningful order.
- get_offset() int
Returns the character offset of an iterator.
Each character in a
GtkTextBuffer
has an offset, starting with 0 for the first character in the buffer. Useget_iter_at_offset
to convert an offset back into an iterator.
- get_paintable() Paintable | None
If the element at
iter
is a paintable, the paintable is returned.Otherwise,
None
is returned.
- get_slice(end: TextIter) str
Returns the text in the given range.
A “slice” is an array of characters encoded in UTF-8 format, including the Unicode “unknown” character 0xFFFC for iterable non-character elements in the buffer, such as images. Because images are encoded in the slice, byte and character offsets in the returned array will correspond to byte offsets in the text buffer. Note that 0xFFFC can occur in normal text as well, so it is not a reliable indicator that a paintable or widget is in the buffer.
- Parameters:
end – iterator at end of a range
- get_tags() list[TextTag]
Returns a list of tags that apply to
iter
, in ascending order of priority.The highest-priority tags are last.
The
GtkTextTag
’s in the list don’t have a reference added, but you have to free the list itself.
- get_text(end: TextIter) str
Returns text in the given range.
If the range contains non-text elements such as images, the character and byte offsets in the returned string will not correspond to character and byte offsets in the buffer. If you want offsets to correspond, see
get_slice
.- Parameters:
end – iterator at end of a range
- get_toggled_tags(toggled_on: bool) list[TextTag]
Returns a list of
GtkTextTag
that are toggled on or off at this point.If
toggled_on
isTrue
, the list contains tags that are toggled on. If a tag is toggled on atiter
, then some non-empty range of characters followingiter
has that tag applied to it. If a tag is toggled off, then some non-empty range followingiter
does not have the tag applied to it.- Parameters:
toggled_on –
True
to get toggled-on tags
- get_visible_line_index() int
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.
- get_visible_line_offset() int
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.
- get_visible_slice(end: TextIter) str
Returns visible text in the given range.
Like
get_slice
, but invisible text is not included. Invisible text is usually invisible because aGtkTextTag
with the “invisible” attribute turned on has been applied to it.- Parameters:
end – iterator at end of range
- get_visible_text(end: TextIter) str
Returns visible text in the given range.
Like
get_text
, but invisible text is not included. Invisible text is usually invisible because aGtkTextTag
with the “invisible” attribute turned on has been applied to it.- Parameters:
end – iterator at end of range
- has_tag(tag: TextTag) bool
Returns
True
ifiter
points to a character that is part of a range tagged withtag
.See also
starts_tag
andends_tag
.- Parameters:
tag – a
GtkTextTag
- in_range(start: TextIter, end: TextIter) bool
Checks whether
iter
falls in the range [start
,end
).start
andend
must be in ascending order.- Parameters:
start – start of range
end – end of range
- inside_sentence() bool
Determines whether
iter
is inside a sentence (as opposed to in between two sentences, e.g. after a period and before the first letter of the next sentence).Sentence boundaries are determined by Pango and should be correct for nearly any language.
- inside_word() bool
Determines whether the character pointed by
iter
is part of a natural-language word (as opposed to say inside some whitespace).Word breaks are determined by Pango and should be correct for nearly any language.
Note that if
starts_word
returnsTrue
, then this function returnsTrue
too, sinceiter
points to the first character of the word.
- is_cursor_position() bool
Determine if
iter
is at a cursor position.See
forward_cursor_position
orLogAttr
orbreak
for details on what a cursor position is.
- is_end() bool
Returns
True
ifiter
is the end iterator.This means it is one past the last dereferenceable iterator in the buffer.
is_end
is the most efficient way to check whether an iterator is the end iterator.
- order(second: TextIter) None
Swaps the value of
first
andsecond
ifsecond
comes beforefirst
in the buffer.That is, ensures that
first
andsecond
are in sequence. Most text buffer functions that take a range call this automatically on your behalf, so there’s no real reason to call it yourself in those cases. There are some exceptions, such asin_range
, that expect a pre-sorted range.- Parameters:
second – another
GtkTextIter
- set_line(line_number: int) None
Moves iterator
iter
to the start of the lineline_number
.If
line_number
is negative or larger than or equal to the number of lines in the buffer, movesiter
to the start of the last line in the buffer.- Parameters:
line_number – line number (counted from 0)
- set_line_index(byte_on_line: int) None
Same as
set_line_offset
, but works with a byte index. The given byte index must be at the start of a character, it can’t be in the middle of a UTF-8 encoded character.- Parameters:
byte_on_line – a byte index relative to the start of
iter
’s current line
- set_line_offset(char_on_line: int) None
Moves
iter
within a line, to a new character (not byte) offset.The given character offset must be less than or equal to the number of characters in the line; if equal,
iter
moves to the start of the next line. Seeset_line_index
if you have a byte index rather than a character offset.- Parameters:
char_on_line – a character offset relative to the start of
iter
’s current line
- set_offset(char_offset: int) None
Sets
iter
to point tochar_offset
.char_offset
counts from the start of the entire text buffer, starting with 0.- Parameters:
char_offset – a character number
- set_visible_line_index(byte_on_line: int) None
Like
set_line_index
, but the index is in visible bytes, i.e. text with a tag making it invisible is not counted in the index.- Parameters:
byte_on_line – a byte index
- set_visible_line_offset(char_on_line: int) None
Like
set_line_offset
, but the offset is in visible characters, i.e. text with a tag making it invisible is not counted in the offset.- Parameters:
char_on_line – a character offset
- starts_line() bool
Returns
True
ifiter
begins a paragraph.This is the case if
get_line_offset
would return 0. However this function is potentially more efficient thanget_line_offset
, because it doesn’t have to compute the offset, it just has to see whether it’s 0.
- starts_sentence() bool
Determines whether
iter
begins a sentence.Sentence boundaries are determined by Pango and should be correct for nearly any language.
- starts_tag(tag: TextTag | None = None) bool
Returns
True
iftag
is toggled on at exactly this point.If
tag
isNone
, returnsTrue
if any tag is toggled on at this point.Note that if this function returns
True
, it means thatiter
is at the beginning of the tagged range, and that the character atiter
is inside the tagged range. In other words, unlikeends_tag
, if this function returnsTrue
,has_tag
will also returnTrue
for the same parameters.- Parameters:
tag – a
GtkTextTag
- starts_word() bool
Determines whether
iter
begins a natural-language word.Word breaks are determined by Pango and should be correct for nearly any language.
- toggles_tag(tag: TextTag | None = None) bool
Gets whether a range with
tag
applied to it begins or ends atiter
.This is equivalent to (
starts_tag()
||ends_tag()
)- Parameters:
tag – a
GtkTextTag