SearchContext
Superclasses: Object
- Constructors:
SearchContext(**properties)
new(buffer:GtkSource.Buffer, settings:GtkSource.SearchSettings=None) -> GtkSource.SearchContext
Constructors
- class SearchContext
- classmethod new(buffer: Buffer, settings: SearchSettings | None = None) SearchContext
Creates a new search context, associated with
buffer
, and customized withsettings
.If
settings
isNone
, a newSearchSettings
object will be created, that you can retrieve withget_settings
.- Parameters:
buffer – a
Buffer
.settings – a
SearchSettings
, orNone
.
Methods
- class SearchContext
- backward(iter: TextIter) tuple[bool, TextIter, TextIter, bool]
Synchronous backward search.
It is recommended to use the asynchronous functions instead, to not block the user interface. However, if you are sure that the
buffer
is small, this function is more convenient to use.If the
wrap_around
property isFalse
, this function doesn’t try to wrap around.The
has_wrapped_around
out parameter is set independently of whether a match is found. So if this function returnsFalse
,has_wrapped_around
will have the same value as thewrap_around
property.- Parameters:
iter – start of search.
- backward_async(iter: TextIter, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
The asynchronous version of
backward
.See the
AsyncResult
documentation to know how to use this function.If the operation is cancelled, the
callback
will only be called ifcancellable
was notNone
. The method takes ownership ofcancellable
, so you can unref it after calling this function.- Parameters:
iter – start of search.
cancellable – a
Cancellable
, orNone
.callback – a
AsyncReadyCallback
to call when the operation is finished.user_data – the data to pass to the
callback
function.
- backward_finish(result: AsyncResult) tuple[bool, TextIter, TextIter, bool]
Finishes a backward search started with
backward_async
.See the documentation of
backward
for more details.- Parameters:
result – a
AsyncResult
.
- forward(iter: TextIter) tuple[bool, TextIter, TextIter, bool]
Synchronous forward search.
It is recommended to use the asynchronous functions instead, to not block the user interface. However, if you are sure that the
buffer
is small, this function is more convenient to use.If the
wrap_around
property isFalse
, this function doesn’t try to wrap around.The
has_wrapped_around
out parameter is set independently of whether a match is found. So if this function returnsFalse
,has_wrapped_around
will have the same value as thewrap_around
property.- Parameters:
iter – start of search.
- forward_async(iter: TextIter, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None
The asynchronous version of
forward
.See the
AsyncResult
documentation to know how to use this function.If the operation is cancelled, the
callback
will only be called ifcancellable
was notNone
. The method takes ownership ofcancellable
, so you can unref it after calling this function.- Parameters:
iter – start of search.
cancellable – a
Cancellable
, orNone
.callback – a
AsyncReadyCallback
to call when the operation is finished.user_data – the data to pass to the
callback
function.
- forward_finish(result: AsyncResult) tuple[bool, TextIter, TextIter, bool]
Finishes a forward search started with
forward_async
.See the documentation of
forward
for more details.- Parameters:
result – a
AsyncResult
.
- get_occurrence_position(match_start: TextIter, match_end: TextIter) int
Gets the position of a search occurrence.
If the buffer is not already fully scanned, the position may be unknown, and -1 is returned. If 0 is returned, it means that this part of the buffer has already been scanned, and that
match_start
andmatch_end
don’t delimit an occurrence.- Parameters:
match_start – the start of the occurrence.
match_end – the end of the occurrence.
- get_occurrences_count() int
Gets the total number of search occurrences.
If the buffer is not already fully scanned, the total number of occurrences is unknown, and -1 is returned.
- get_regex_error() GError | None
Regular expression patterns must follow certain rules. If
search_text
breaks a rule, the error can be retrieved with this function.The error domain is
RegexError
.Free the return value with
free
.
- get_settings() SearchSettings
- replace(match_start: TextIter, match_end: TextIter, replace: str, replace_length: int) bool
Replaces a search match by another text. If
match_start
andmatch_end
doesn’t correspond to a search match,False
is returned.match_start
andmatch_end
iters are revalidated to point to the replacement text boundaries.For a regular expression replacement, you can check if
replace
is valid by callingcheck_replacement
. Thereplace
text can contain backreferences.- Parameters:
match_start – the start of the match to replace.
match_end – the end of the match to replace.
replace – the replacement text.
replace_length – the length of
replace
in bytes, or -1.
- replace_all(replace: str, replace_length: int) int
Replaces all search matches by another text.
It is a synchronous function, so it can block the user interface.
For a regular expression replacement, you can check if
replace
is valid by callingcheck_replacement
. Thereplace
text can contain backreferences.- Parameters:
replace – the replacement text.
replace_length – the length of
replace
in bytes, or -1.
Properties
- class SearchContext
-
- props.occurrences_count: int
The total number of search occurrences. If the search is disabled, the value is 0. If the buffer is not already fully scanned, the value is -1.
- props.regex_error: GError
If the regex search pattern doesn’t follow all the rules, this
Error
property will be set. If the pattern is valid, the value isNone
.Free with
free
.
- props.settings: SearchSettings
The
SearchSettings
associated to the search context.This property is construct-only since version 4.0.