Subclass of [class@Gtk.
TextBuffer].
A `GtkSourceBuffer` object is the model for [class@View] widgets. It extends the [class@Gtk.TextBuffer] class by adding features useful to
display and edit source code such as syntax highlighting and bracket matching.
To create a `GtkSourceBuffer` use [ctor@GtkSource.Buffer.new] or [ctor@GtkSource.Buffer.new_with_language]. The second form is just a
convenience function which allows you to initially set a [class@Language]. You can also directly create a [class@View] and get its [class@Buffer
] with [method@Gtk.TextView.get_buffer].
The highlighting is enabled by default, but you can disable it with [method@Buffer.set_highlight_syntax].
Context Classes:
It is possible to retrieve some information from the syntax highlighting engine. The default context classes that are applied to regions of a
`GtkSourceBuffer`:
- **comment**: the region delimits a comment;
- **no-spell-check**: the region should not be spell checked;
- **path**: the region delimits a path to a file;
- **string**: the region delimits a string.
Custom language definition files can create their own context classes, since the functions like [method@Buffer.iter_has_context_class] take a
string parameter as the context class.
`GtkSourceBuffer` provides an API to access the context classes: [method@Buffer.iter_has_context_class], [
method@Buffer.get_context_classes_at_iter], [method@Buffer.iter_forward_to_context_class_toggle] and [
method@Buffer.iter_backward_to_context_class_toggle].
And the [signal@GtkSource.Buffer:GtkSourceBuffer:highlight-updated
] signal permits to be notified when a context class region
changes.
Each context class has also an associated [class@Gtk.TextTag] with the name `gtksourceview:context-classes:<name>`. For example to
retrieve the [class@Gtk.TextTag] for the string context class, one can write: ```c GtkTextTagTable *tag_table; GtkTextTag *tag;
tag_table = gtk_text_buffer_get_tag_table (buffer); tag = gtk_text_tag_table_lookup (tag_table, "gtksourceview:context-classes:string"); ```
The tag must be used for read-only purposes.
Accessing a context class via the associated [class@Gtk.TextTag] is less convenient than the `GtkSourceBuffer` API, because:
- The tag doesn't always exist, you need to listen to the [signal@Gtk.TextTagTable:
GtkSourceBuffer:tag-added
] and [
signal@Gtk.TextTagTable:GtkSourceBuffer:tag-removed
] signals.
- Instead of the [signal@GtkSource.Buffer:
GtkSourceBuffer:highlight-updated
] signal, you can listen to the [
signal@Gtk.TextBuffer:GtkSourceBuffer:apply-tag
] and [signal@Gtk.TextBuffer:GtkSourceBuffer:remove-tag
] signals.
A possible use-case for accessing a context class via the associated [class@Gtk.TextTag] is to read the region but without adding a hard
dependency on the GtkSourceView library (for example for a spell-checking library that wants to read the no-spell-check region).
- public bool backward_iter_to_source_mark (ref TextIter iter, string? category)
Moves `iter` to the position of the previous [class@Mark] of the given
category.
- public void change_case (ChangeCaseType case_type, TextIter start, TextIter end)
Changes the case of the text between the specified iterators.
- public unowned Mark create_source_mark (string? name, string category, TextIter where)
Creates a source mark in the `buffer` of category `category`.
- public void ensure_highlight (TextIter start, TextIter end)
Forces buffer to analyze and highlight the given area synchronously.
- public bool forward_iter_to_source_mark (ref TextIter iter, string? category)
Moves `iter` to the position of the next [class@Mark] of the given
`category`.
- public string[] get_context_classes_at_iter (TextIter iter)
Get all defined context classes at iter
.
- public bool get_highlight_matching_brackets ()
Determines whether bracket match highlighting is activated for the source
buffer.
- public bool get_highlight_syntax ()
Determines whether syntax highlighting is activated in the source buffer.
- public bool get_implicit_trailing_newline ()
- public unowned Language? get_language ()
Returns the [class@Language] associated with the buffer, see [method@Buffer.
- public bool get_loading ()
- public SList<unowned Mark> get_source_marks_at_iter (TextIter iter, string? category)
Returns the list of marks of the given category at iter
.
- public SList<unowned Mark> get_source_marks_at_line (int line, string? category)
Returns the list of marks of the given category at line
.
- public unowned StyleScheme? get_style_scheme ()
Returns the [class@StyleScheme] associated with the buffer, see [
method@Buffer.
- public bool iter_backward_to_context_class_toggle (ref TextIter iter, string context_class)
Moves backward to the next toggle (on or off) of the context class.
- public bool iter_forward_to_context_class_toggle (ref TextIter iter, string context_class)
Moves forward to the next toggle (on or off) of the context class.
- public bool iter_has_context_class (TextIter iter, string context_class)
Check if the class context_class
is set on iter
.
- public void join_lines (TextIter start, TextIter end)
Joins the lines of text between the specified iterators.
- public void remove_source_marks (TextIter start, TextIter end, string? category)
Remove all marks of category
between start
and
end
from the buffer.
- public void set_highlight_matching_brackets (bool highlight)
Controls the bracket match highlighting function in the buffer.
- public void set_highlight_syntax (bool highlight)
Controls whether syntax is highlighted in the buffer.
- public void set_implicit_trailing_newline (bool implicit_trailing_newline)
Sets whether the this has an implicit
trailing newline.
- public void set_language (Language? language)
Associates a [class@Language] with the buffer.
- public void set_style_scheme (StyleScheme? scheme)
Sets a [class@StyleScheme] to be used by the buffer and the view.
- public void sort_lines (TextIter start, TextIter end, SortFlags flags, int column)
Sort the lines of text between the specified iterators.