GtkSourceBuffer

Subclass of gtk.text_buffer.TextBuffer.

A gtksource.buffer.Buffer object is the model for class@View widgets. It extends the gtk.text_buffer.TextBuffer class by adding features useful to display and edit source code such as syntax highlighting and bracket matching.

To create a gtksource.buffer.Buffer use gtksource.buffer.Buffer.new_ or gtksource.buffer.Buffer.newWithLanguage. 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 gtk.text_view.TextView.getBuffer.

The highlighting is enabled by default, but you can disable it with gtksource.buffer.Buffer.setHighlightSyntax.

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 gtksource.buffer.Buffer:

  • 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 gtksource.buffer.Buffer.iterHasContextClass take a string parameter as the context class.

gtksource.buffer.Buffer provides an API to access the context classes: gtksource.buffer.Buffer.iterHasContextClass, gtksource.buffer.Buffer.getContextClassesAtIter, gtksource.buffer.Buffer.iterForwardToContextClassToggle and gtksource.buffer.Buffer.iterBackwardToContextClassToggle.

And the signal@GtkSource.Buffer::highlight-updated signal permits to be notified when a context class region changes.

Each context class has also an associated gtk.text_tag.TextTag with the name gtksourceview:context-classes:<name>. For example to retrieve the gtk.text_tag.TextTag for the string context class, one can write:

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 gtk.text_tag.TextTag is less convenient than the gtksource.buffer.Buffer API, because:

  • The tag doesn't always exist, you need to listen to the signal@Gtk.TextTagTable::tag-added and signal@Gtk.TextTagTable::tag-removed signals.
  • Instead of the signal@GtkSource.Buffer::highlight-updated signal, you can listen to the signal@Gtk.TextBuffer::apply-tag and signal@Gtk.TextBuffer::remove-tag signals.

A possible use-case for accessing a context class via the associated gtk.text_tag.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).

struct GtkSourceBuffer {}

Members

Variables

parentInstance
GtkTextBuffer parentInstance;