SearchContext
Superclasses: Object
Search context.
A GtkSourceSearchContext
is used for the search and replace in a
Buffer
. The search settings are represented by a
SearchSettings
object. There can be a many-to-many relationship
between buffers and search settings, with the search contexts in-between: a
search settings object can be shared between several search contexts; and a
buffer can contain several search contexts at the same time.
The total number of search occurrences can be retrieved with
get_occurrences_count
. To know the position of a
certain match, use get_occurrence_position
.
The buffer is scanned asynchronously, so it doesn’t block the user interface. For each search, the buffer is scanned at most once. After that, navigating through the occurrences doesn’t require to re-scan the buffer entirely.
To search forward, use forward
or
forward_async
for the asynchronous version.
The backward search is done similarly. To replace a search match, or all
matches, use replace
and
replace_all
.
The search occurrences are highlighted by default. To disable it, use
set_highlight
. You can enable the search
highlighting for several GtkSourceSearchContext
’s attached to the
same buffer. Moreover, each of those GtkSourceSearchContext
’s can
have a different text style associated. Use
set_match_style
to specify the Style
to apply on search matches.
Note that the highlight
and
match_style
properties are in the
GtkSourceSearchContext
class, not SearchSettings
. Appearance
settings should be tied to one, and only one buffer, as different buffers can
have different style scheme associated (a SearchSettings
object
can be bound indirectly to several buffers).
The concept of “current match” doesn’t exist yet. A way to highlight differently the current match is to select it.
A search occurrence’s position doesn’t depend on the cursor position or other parameters. Take for instance the buffer “aaaa” with the search text “aa”. The two occurrences are at positions [0:2] and [2:4]. If you begin to search at position 1, you will get the occurrence [2:4], not [1:3]. This is a prerequisite for regular expression searches. The pattern “.*” matches the entire line. If the cursor is at the middle of the line, you don’t want the rest of the line as the occurrence, you want an entire line. (As a side note, regular expression searches can also match multiple lines.)
In the GtkSourceView source code, there is an example of how to use the search and replace API: see the tests/test-search.c file. It is a mini application for the search and replace, with a basic user interface.
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.