Buffer#
Superclasses: TextBuffer, Object
Subclass of TextBuffer.
A GtkSourceBuffer object is the model for View widgets.
It extends the TextBuffer class by adding features useful to display
and edit source code such as syntax highlighting and bracket matching.
To create a GtkSourceBuffer use new or
new_with_language. The second form is just a convenience
function which allows you to initially set a Language. You can also
directly create a View and get its Buffer with
get_buffer.
The highlighting is enabled by default, but you can disable it with
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 iter_has_context_class take
a string parameter as the context class.
GtkSourceBuffer provides an API to access the context classes:
iter_has_context_class,
get_context_classes_at_iter,
iter_forward_to_context_class_toggle and
iter_backward_to_context_class_toggle.
And the highlight_updated signal permits to be notified
when a context class region changes.
Each context class has also an associated TextTag with the name
gtksourceview:context-classes:<name>. For example to
retrieve the 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 TextTag is less
convenient than the GtkSourceBuffer API, because:
- The tag doesn’t always exist, you need to listen to the
tag_addedandtag_removedsignals.
- Instead of the
highlight_updatedsignal, you can listen to the
apply_tagandremove_tagsignals.
- Instead of the
A possible use-case for accessing a context class via the associated
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).
Constructors#
- class Buffer
- classmethod new(table: TextTagTable | None = None) Buffer#
Creates a new source buffer.
- Parameters:
table – a
TextTagTable, orNoneto create a new one.
Methods#
- class Buffer
- backward_iter_to_source_mark(iter: TextIter, category: str | None = None) tuple[bool, TextIter]#
Moves
iterto the position of the previousMarkof the given category.Returns
Trueifiterwas moved. Ifcategoryis NULL, the previous source mark can be of any category.- Parameters:
iter – an iterator.
category – category to search for, or
None
- change_case(case_type: ChangeCaseType, start: TextIter, end: TextIter) None#
Changes the case of the text between the specified iterators.
Since 5.4, this function will update the position of
startandendto surround the modified text.
- create_source_mark(name: str | None, category: str, where: TextIter) Mark#
Creates a source mark in the
bufferof categorycategory.A source mark is a
TextMarkbut organized into categories. Depending on the category a pixbuf can be specified that will be displayed along the line of the mark.Like a
TextMark, aMarkcan be anonymous if the passednameisNone. Also, the buffer owns the marks so you shouldn’t unreference it.Marks always have left gravity and are moved to the beginning of the line when the user deletes the line they were in.
Typical uses for a source mark are bookmarks, breakpoints, current executing instruction indication in a source file, etc..
- Parameters:
name – the name of the mark, or
None.category – a string defining the mark category.
where – location to place the mark.
- do_bracket_matched(self, iter: TextIter, state: BracketMatchType) None#
- Parameters:
iter
state
- ensure_highlight(start: TextIter, end: TextIter) None#
Forces buffer to analyze and highlight the given area synchronously.
Note:
This is a potentially slow operation and should be used only when you need to make sure that some text not currently visible is highlighted, for instance before printing.
- Parameters:
start – start of the area to highlight.
end – end of the area to highlight.
- forward_iter_to_source_mark(iter: TextIter, category: str | None = None) tuple[bool, TextIter]#
Moves
iterto the position of the nextMarkof the givencategory.Returns
Trueifiterwas moved. Ifcategoryis NULL, the next source mark can be of any category.- Parameters:
iter – an iterator.
category – category to search for, or
None
- get_context_classes_at_iter(iter: TextIter) list[str]#
Get all defined context classes at
iter.See the
Bufferdescription for the list of default context classes.- Parameters:
iter – a
TextIter.
- get_highlight_matching_brackets() bool#
Determines whether bracket match highlighting is activated for the source buffer.
- get_highlight_syntax() bool#
Determines whether syntax highlighting is activated in the source buffer.
- get_language() Language | None#
Returns the
Languageassociated with the buffer, seeset_language.The returned object should not be unreferenced by the user.
- get_source_marks_at_iter(iter: TextIter, category: str | None = None) list[Mark]#
Returns the list of marks of the given category at
iter.If
categoryisNoneit returns all marks atiter.- Parameters:
iter – an iterator.
category – category to search for, or
None
- get_source_marks_at_line(line: int, category: str | None = None) list[Mark]#
Returns the list of marks of the given category at
line.If
categoryisNone, all marks atlineare returned.- Parameters:
line – a line number.
category – category to search for, or
None
- get_style_scheme() StyleScheme | None#
Returns the
StyleSchemeassociated with the buffer, seeset_style_scheme.The returned object should not be unreferenced by the user.
- iter_backward_to_context_class_toggle(iter: TextIter, context_class: str) tuple[bool, TextIter]#
Moves backward to the next toggle (on or off) of the context class.
If no matching context class toggles are found, returns
False, otherwiseTrue. Does not return toggles located atiter, only toggles afteriter. Setsiterto the location of the toggle, or to the end of the buffer if no toggle is found.See the
Bufferdescription for the list of default context classes.- Parameters:
iter – a
TextIter.context_class – the context class.
- iter_forward_to_context_class_toggle(iter: TextIter, context_class: str) tuple[bool, TextIter]#
Moves forward to the next toggle (on or off) of the context class.
If no matching context class toggles are found, returns
False, otherwiseTrue. Does not return toggles located atiter, only toggles afteriter. Setsiterto the location of the toggle, or to the end of the buffer if no toggle is found.See the
Bufferdescription for the list of default context classes.- Parameters:
iter – a
TextIter.context_class – the context class.
- iter_has_context_class(iter: TextIter, context_class: str) bool#
Check if the class
context_classis set oniter.See the
Bufferdescription for the list of default context classes.- Parameters:
iter – a
TextIter.context_class – class to search for.
- join_lines(start: TextIter, end: TextIter) None#
Joins the lines of text between the specified iterators.
- remove_source_marks(start: TextIter, end: TextIter, category: str | None = None) None#
Remove all marks of
categorybetweenstartandendfrom the buffer.If
categoryis NULL, all marks in the range will be removed.
- set_highlight_matching_brackets(highlight: bool) None#
Controls the bracket match highlighting function in the buffer.
If activated, when you position your cursor over a bracket character (a parenthesis, a square bracket, etc.) the matching opening or closing bracket character will be highlighted.
- Parameters:
highlight –
Trueif you want matching brackets highlighted.
- set_highlight_syntax(highlight: bool) None#
Controls whether syntax is highlighted in the buffer.
If
highlightisTrue, the text will be highlighted according to the syntax patterns specified in theLanguageset withset_language.If
highlightisFalse, syntax highlighting is disabled and all theTextTagobjects that have been added by the syntax highlighting engine are removed from the buffer.- Parameters:
highlight –
Trueto enable syntax highlighting,Falseto disable it.
- set_implicit_trailing_newline(implicit_trailing_newline: bool) None#
Sets whether the
bufferhas an implicit trailing newline.If an explicit trailing newline is present in a
TextBuffer,TextViewshows it as an empty line. This is generally not what the user expects.- If
implicit_trailing_newlineisTrue(the default value): when a
FileLoaderloads the content of a file into thebuffer, the trailing newline (if present in the file) is not inserted into thebuffer.when a
FileSaversaves the content of thebufferinto a file, a trailing newline is added to the file.
On the other hand, if
implicit_trailing_newlineisFalse, the file’s content is not modified when loaded into thebuffer, and thebuffer's content is not modified when saved into a file.- Parameters:
implicit_trailing_newline – the new value.
- If
- set_language(language: Language | None = None) None#
Associates a
Languagewith the buffer.Note that a
Languageaffects not only the syntax highlighting, but also the context classes. If you want to disable just the syntax highlighting, seeset_highlight_syntax.The buffer holds a reference to
language.- Parameters:
language – a
Languageto set, orNone.
- set_style_scheme(scheme: StyleScheme | None = None) None#
Sets a
StyleSchemeto be used by the buffer and the view.Note that a
StyleSchemeaffects not only the syntax highlighting, but also otherViewfeatures such as highlighting the current line, matching brackets, the line numbers, etc.Instead of setting a
Nonescheme, it is better to disable syntax highlighting withset_highlight_syntax, and setting theStyleSchemewith the “classic” or “tango” ID, because those two style schemes follow more closely the GTK theme (for example for the background color).The buffer holds a reference to
scheme.- Parameters:
scheme – a
StyleSchemeorNone.
Properties#
- class Buffer
-
- props.style_scheme: StyleScheme#
The type of the None singleton.
Signals#
- class Buffer.signals
- bracket_matched(iter: TextIter | None, state: BracketMatchType) None#
The type of the None singleton.
- Parameters:
iter – if found, the location of the matching bracket.
state – state of bracket matching.
Virtual Methods#
- class Buffer
- do_bracket_matched(iter: TextIter, state: BracketMatchType) None#
The type of the None singleton.
- Parameters:
iter
state
Fields#
- class Buffer
- parent_instance#