GtkSource.SearchContext¶
- Subclasses:
None
Methods¶
- Inherited:
- Structs:
class |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Virtual Methods¶
- Inherited:
Properties¶
Name |
Type |
Flags |
Short Description |
---|---|---|---|
r/w/co |
The associated |
||
r/w/c |
Highlight search occurrences |
||
r/w/c |
The text style for matches |
||
r |
Total number of search occurrences |
||
r |
Regular expression error |
||
r/w/co |
The associated |
Signals¶
- Inherited:
Fields¶
- Inherited:
Class Details¶
- class GtkSource.SearchContext(**kwargs)¶
- Bases:
- Abstract:
No
- Structure:
Search context.
A
GtkSourceSearchContext
is used for the search and replace in a [class`Buffer`]. The search settings are represented by a [class`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 [method`SearchContext`.get_occurrences_count]. To know the position of a certain match, use [method`SearchContext`.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 [method`SearchContext`.forward] or [method`SearchContext`.forward_async] for the asynchronous version. The backward search is done similarly. To replace a search match, or all matches, use [method`SearchContext`.replace] and [method`SearchContext`.replace_all].
The search occurrences are highlighted by default. To disable it, use [method`SearchContext`.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 [method`SearchContext`.set_match_style] to specify the [class`Style`] to apply on search matches.
Note that the [property`SearchContext`:py:data::highlight<GtkSource.SearchContext.props.highlight>] and [property`SearchContext`:py:data::match-style<GtkSource.SearchContext.props.match_style>] properties are in the
GtkSourceSearchContext
class, not [class`SearchSettings`]. Appearance settings should be tied to one, and only one buffer, as different buffers can have different style scheme associated (a [class`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
GtkSource.View
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.- classmethod new(buffer, settings)¶
- Parameters:
buffer (
GtkSource.Buffer
) – aGtkSource.Buffer
.settings (
GtkSource.SearchSettings
orNone
) – aGtkSource.SearchSettings
, orNone
.
- Returns:
a new search context.
- Return type:
Creates a new search context, associated with buffer, and customized with settings.
If settings is
None
, a new [class`SearchSettings`] object will be created, that you can retrieve with [method`SearchContext`.get_settings].
- backward(iter)¶
- Parameters:
iter (
Gtk.TextIter
) – start of search.- Returns:
whether a match was found.
- Return type:
(
bool
, match_start:Gtk.TextIter
, match_end:Gtk.TextIter
, has_wrapped_around: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 [property`SearchSettings`:py:data::wrap-around<GtkSource.SearchContext.props.wrap_around>] property is
False
, 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 returns
False
, has_wrapped_around will have the same value as the [property`SearchSettings`:py:data::wrap-around<GtkSource.SearchContext.props.wrap_around>] property.
- backward_async(iter, cancellable, callback, *user_data)¶
- Parameters:
iter (
Gtk.TextIter
) – start of search.cancellable (
Gio.Cancellable
orNone
) – aGio.Cancellable
, orNone
.callback (
Gio.AsyncReadyCallback
orNone
) – aGio.AsyncReadyCallback
to call when the operation is finished.user_data (
object
orNone
) – the data to pass to the callback function.
The asynchronous version of [method`SearchContext`.backward].
See the [iface`Gio`.AsyncResult] documentation to know how to use this function.
If the operation is cancelled, the callback will only be called if cancellable was not
None
. The method takes ownership of cancellable, so you can unref it after calling this function.
- backward_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) – aGio.AsyncResult
.- Raises:
- Returns:
whether a match was found.
- Return type:
(
bool
, match_start:Gtk.TextIter
, match_end:Gtk.TextIter
, has_wrapped_around:bool
)
Finishes a backward search started with [method`SearchContext`.backward_async].
See the documentation of [method`SearchContext`.backward] for more details.
- forward(iter)¶
- Parameters:
iter (
Gtk.TextIter
) – start of search.- Returns:
whether a match was found.
- Return type:
(
bool
, match_start:Gtk.TextIter
, match_end:Gtk.TextIter
, has_wrapped_around: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 [property`SearchSettings`:py:data::wrap-around<GtkSource.SearchContext.props.wrap_around>] property is
False
, 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 returns
False
, has_wrapped_around will have the same value as the [property`SearchSettings`:py:data::wrap-around<GtkSource.SearchContext.props.wrap_around>] property.
- forward_async(iter, cancellable, callback, *user_data)¶
- Parameters:
iter (
Gtk.TextIter
) – start of search.cancellable (
Gio.Cancellable
orNone
) – aGio.Cancellable
, orNone
.callback (
Gio.AsyncReadyCallback
orNone
) – aGio.AsyncReadyCallback
to call when the operation is finished.user_data (
object
orNone
) – the data to pass to the callback function.
The asynchronous version of [method`SearchContext`.forward].
See the [iface`Gio`.AsyncResult] documentation to know how to use this function.
If the operation is cancelled, the callback will only be called if cancellable was not
None
. The method takes ownership of cancellable, so you can unref it after calling this function.
- forward_finish(result)¶
- Parameters:
result (
Gio.AsyncResult
) – aGio.AsyncResult
.- Raises:
- Returns:
whether a match was found.
- Return type:
(
bool
, match_start:Gtk.TextIter
, match_end:Gtk.TextIter
, has_wrapped_around:bool
)
Finishes a forward search started with [method`SearchContext`.forward_async].
See the documentation of [method`SearchContext`.forward] for more details.
- get_buffer()¶
- Returns:
the associated buffer.
- Return type:
- get_match_style()¶
- Returns:
the
GtkSource.Style
to apply on search matches.- Return type:
- get_occurrence_position(match_start, match_end)¶
- Parameters:
match_start (
Gtk.TextIter
) – the start of the occurrence.match_end (
Gtk.TextIter
) – the end of the occurrence.
- Returns:
the position of the search occurrence. The first occurrence has the position 1 (not 0). Returns 0 if match_start and match_end don’t delimit an occurrence. Returns -1 if the position is not yet known.
- Return type:
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 and match_end don’t delimit an occurrence.
- get_occurrences_count()¶
- Returns:
the total number of search occurrences, or -1 if unknown.
- Return type:
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()¶
- Returns:
the
GLib.Error
, orNone
if the pattern is valid.- Return type:
GLib.Error
orNone
Regular expression patterns must follow certain rules. If [property`SearchSettings`:py:data::search-text<GtkSource.SearchContext.props.search_text>] breaks a rule, the error can be retrieved with this function.
The error domain is [error`GLib`.RegexError].
Free the return value with [method`GLib`.Error.free].
- get_settings()¶
- Returns:
the search settings.
- Return type:
- replace(match_start, match_end, replace, replace_length)¶
- Parameters:
match_start (
Gtk.TextIter
) – the start of the match to replace.match_end (
Gtk.TextIter
) – the end of the match to replace.replace (
str
) – the replacement text.replace_length (
int
) – the length of replace in bytes, or -1.
- Raises:
- Returns:
whether the match has been replaced.
- Return type:
Replaces a search match by another text. If match_start and match_end doesn’t correspond to a search match,
False
is returned.match_start and match_end iters are revalidated to point to the replacement text boundaries.
For a regular expression replacement, you can check if replace is valid by calling [func`GLib`.Regex.check_replacement]. The replace text can contain backreferences.
- replace_all(replace, replace_length)¶
- Parameters:
- Raises:
- Returns:
the number of replaced matches.
- Return type:
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 calling [func`GLib`.Regex.check_replacement]. The replace text can contain backreferences.
- set_highlight(highlight)¶
- Parameters:
highlight (
bool
) – the setting.
Enables or disables the search occurrences highlighting.
- set_match_style(match_style)¶
- Parameters:
match_style (
GtkSource.Style
orNone
) – aGtkSource.Style
, orNone
.
Set the style to apply on search matches.
If match_style is
None
, default theme’s scheme ‘match-style’ will be used. To enable or disable the search highlighting, use [method`SearchContext`.set_highlight].
Property Details¶
- GtkSource.SearchContext.props.buffer¶
- Name:
buffer
- Type:
- Default Value:
- Flags:
The [class`Buffer`] associated to the search context.
- GtkSource.SearchContext.props.highlight¶
-
Highlight the search occurrences.
- GtkSource.SearchContext.props.match_style¶
- Name:
match-style
- Type:
- Default Value:
- Flags:
A [class`Style`], or
None
for theme’s scheme default style.
- GtkSource.SearchContext.props.occurrences_count¶
-
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.
- GtkSource.SearchContext.props.regex_error¶
- Name:
regex-error
- Type:
- Default Value:
- Flags:
If the regex search pattern doesn’t follow all the rules, this
GLib.Error
property will be set. If the pattern is valid, the value isNone
.Free with [method`GLib`.Error.free].
- GtkSource.SearchContext.props.settings¶
- Name:
settings
- Type:
- Default Value:
- Flags:
The [class`SearchSettings`] associated to the search context.
This property is construct-only since version 4.0.