Class Buffer
- All Implemented Interfaces:
Proxy
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 Buffer()
or
withLanguage(org.gnome.gtksourceview.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
TextView.getBuffer()
.
The highlighting is enabled by default, but you can disable it with
setHighlightSyntax(boolean)
.
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 iterHasContextClass(org.gnome.gtk.TextIter, java.lang.String)
take
a string parameter as the context class.
GtkSourceBuffer
provides an API to access the context classes:
iterHasContextClass(org.gnome.gtk.TextIter, java.lang.String)
,
getContextClassesAtIter(org.gnome.gtk.TextIter)
,
iterForwardToContextClassToggle(org.gnome.gtk.TextIter, java.lang.String)
and
iterBackwardToContextClassToggle(org.gnome.gtk.TextIter, java.lang.String)
.
And the GtkSource.Buffer::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
Gtk.TextTagTable::tag-added
andGtk.TextTagTable::tag-removed
signals. - Instead of the
GtkSource.Buffer::highlight-updated
signal, you can listen to theGtk.TextBuffer::apply-tag
andGtk.TextBuffer::remove-tag
signals.
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).
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface declaration of theBracketMatchedCallback
callback.static class
static class
Buffer.Builder<B extends Buffer.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theCursorMovedCallback
callback.static interface
Functional interface declaration of theHighlightUpdatedCallback
callback.static interface
Functional interface declaration of theSourceMarkUpdatedCallback
callback.Nested classes/interfaces inherited from class org.gnome.gtk.TextBuffer
TextBuffer.ApplyTagCallback, TextBuffer.BeginUserActionCallback, TextBuffer.ChangedCallback, TextBuffer.DeleteRangeCallback, TextBuffer.EndUserActionCallback, TextBuffer.InsertChildAnchorCallback, TextBuffer.InsertPaintableCallback, TextBuffer.InsertTextCallback, TextBuffer.MarkDeletedCallback, TextBuffer.MarkSetCallback, TextBuffer.ModifiedChangedCallback, TextBuffer.PasteDoneCallback, TextBuffer.RedoCallback, TextBuffer.RemoveTagCallback, TextBuffer.TextBufferClass, TextBuffer.UndoCallback
Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
-
Constructor Summary
ConstructorDescriptionBuffer()
CallsBuffer(org.gnome.gtk.TextTagTable)
with table =null
Buffer
(MemorySegment address) Create a Buffer proxy instance for the provided memory address.Buffer
(@Nullable TextTagTable table) Creates a new source buffer. -
Method Summary
Modifier and TypeMethodDescriptionprotected Buffer
asParent()
Returns this instance as if it were its parent type.boolean
backwardIterToSourceMark
(TextIter iter, @Nullable String category) Movesiter
to the position of the previousMark
of the given category.protected void
bracketMatched
(TextIter iter, BracketMatchType state) static Buffer.Builder
<? extends Buffer.Builder> builder()
ABuffer.Builder
object constructs aBuffer
with the specified properties.void
changeCase
(ChangeCaseType caseType, TextIter start, TextIter end) Changes the case of the text between the specified iterators.createSourceMark
(@Nullable String name, String category, TextIter where) Creates a source mark in thebuffer
of categorycategory
.createSourceTag
(@Nullable String tagName, @Nullable String firstPropertyName, Object... varargs) In short, this is the same function asTextBuffer.createTag(java.lang.String, java.lang.String, java.lang.Object...)
but instead of creating aTextTag
, this function creates aTag
.void
emitBracketMatched
(@Nullable TextIter iter, BracketMatchType state) Emits the "bracket-matched" signal.void
Emits the "cursor-moved" signal.void
emitHighlightUpdated
(TextIter start, TextIter end) Emits the "highlight-updated" signal.void
Emits the "source-mark-updated" signal.void
ensureHighlight
(TextIter start, TextIter end) Forces buffer to analyze and highlight the given area synchronously.boolean
forwardIterToSourceMark
(TextIter iter, @Nullable String category) String[]
Get all defined context classes atiter
.boolean
Determines whether bracket match highlighting is activated for the source buffer.boolean
Determines whether syntax highlighting is activated in the source buffer.boolean
Returns theLanguage
associated with the buffer, seesetLanguage(org.gnome.gtksourceview.Language)
.boolean
static MemoryLayout
The memory layout of the native struct.getSourceMarksAtIter
(TextIter iter, @Nullable String category) Returns the list of marks of the given category atiter
.getSourceMarksAtLine
(int line, @Nullable String category) Returns the list of marks of the given category atline
.Returns theStyleScheme
associated with the buffer, seesetStyleScheme(org.gnome.gtksourceview.StyleScheme)
.static Type
getType()
Get the GType of the Buffer classboolean
iterBackwardToContextClassToggle
(TextIter iter, String contextClass) Moves backward to the next toggle (on or off) of the context class.boolean
iterForwardToContextClassToggle
(TextIter iter, String contextClass) Moves forward to the next toggle (on or off) of the context class.boolean
iterHasContextClass
(TextIter iter, String contextClass) Check if the classcontextClass
is set oniter
.void
Joins the lines of text between the specified iterators.iter
is set to a valid iterator pointing to the matching bracket ifstate
isBracketMatchType.FOUND
.onCursorMoved
(Buffer.CursorMovedCallback handler) The "cursor-moved" signal is emitted when then insertion mark has moved.The ::highlight-updated signal is emitted when the syntax highlighting and context classes are updated in a certain region of thebuffer
.The ::source-mark-updated signal is emitted each time a mark is added to, moved or removed from thebuffer
.void
removeSourceMarks
(TextIter start, TextIter end, @Nullable String category) Remove all marks ofcategory
betweenstart
andend
from the buffer.void
setHighlightMatchingBrackets
(boolean highlight) Controls the bracket match highlighting function in the buffer.void
setHighlightSyntax
(boolean highlight) Controls whether syntax is highlighted in the buffer.void
setImplicitTrailingNewline
(boolean implicitTrailingNewline) Sets whether the this Buffer has an implicit trailing newline.void
setLanguage
(@Nullable Language language) Associates aLanguage
with the buffer.void
setStyleScheme
(@Nullable StyleScheme scheme) Sets aStyleScheme
to be used by the buffer and the view.void
Sort the lines of text between the specified iterators.void
Sort the lines of text between the specified iterators.static Buffer
withLanguage
(Language language) Creates a new source buffer using the highlighting patterns inlanguage
.Methods inherited from class org.gnome.gtk.TextBuffer
addCommitNotify, addCommitNotify, addMark, addSelectionClipboard, applyTag, applyTagByName, backspace, beginIrreversibleAction, beginUserAction, changed, copyClipboard, createChildAnchor, createMark, createTag, cutClipboard, delete, deleteInteractive, deleteMark, deleteMarkByName, deleteRange, deleteSelection, emitApplyTag, emitBeginUserAction, emitChanged, emitDeleteRange, emitEndUserAction, emitInsertChildAnchor, emitInsertPaintable, emitInsertText, emitMarkDeleted, emitMarkSet, emitModifiedChanged, emitPasteDone, emitRedo, emitRemoveTag, emitUndo, endIrreversibleAction, endUserAction, getBounds, getCanRedo, getCanUndo, getCharCount, getEnableUndo, getEndIter, getHasSelection, getInsert, getIterAtChildAnchor, getIterAtLine, getIterAtLineIndex, getIterAtLineOffset, getIterAtMark, getIterAtOffset, getLineCount, getMark, getMaxUndoLevels, getModified, getSelectionBound, getSelectionBounds, getSelectionContent, getSlice, getStartIter, getTagTable, getText, insert, insertAtCursor, insertChildAnchor, insertInteractive, insertInteractiveAtCursor, insertMarkup, insertPaintable, insertRange, insertRangeInteractive, insertText, insertWithTags, insertWithTagsByName, markDeleted, markSet, modifiedChanged, moveMark, moveMarkByName, onApplyTag, onBeginUserAction, onChanged, onDeleteRange, onEndUserAction, onInsertChildAnchor, onInsertPaintable, onInsertText, onMarkDeleted, onMarkSet, onModifiedChanged, onPasteDone, onRedo, onRemoveTag, onUndo, pasteClipboard, pasteDone, placeCursor, redo, removeAllTags, removeCommitNotify, removeSelectionClipboard, removeTag, removeTagByName, selectRange, setEnableUndo, setMaxUndoLevels, setModified, setText, undo
Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Buffer
Create a Buffer proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Buffer
Creates a new source buffer.- Parameters:
table
- aGtkTextTagTable
, ornull
to create a new one.
-
Buffer
public Buffer()CallsBuffer(org.gnome.gtk.TextTagTable)
with table =null
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type.- Overrides:
asParent
in classTextBuffer
-
withLanguage
Creates a new source buffer using the highlighting patterns inlanguage
.This is equivalent to creating a new source buffer with a new tag table and then calling
setLanguage(org.gnome.gtksourceview.Language)
.- Parameters:
language
- aGtkSourceLanguage
.- Returns:
- a new source buffer which will highlight text
according to the highlighting patterns in
language
.
-
backwardIterToSourceMark
Movesiter
to the position of the previousMark
of the given category.Returns
true
ifiter
was moved. Ifcategory
is NULL, the previous source mark can be of any category.- Parameters:
iter
- an iterator.category
- category to search for, ornull
- Returns:
- whether
iter
was moved.
-
changeCase
Changes the case of the text between the specified iterators.Since 5.4, this function will update the position of
start
andend
to surround the modified text.- Parameters:
caseType
- how to change the case.start
- aGtkTextIter
.end
- aGtkTextIter
.
-
createSourceMark
Creates a source mark in thebuffer
of categorycategory
.A source mark is a
TextMark
but 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
, aMark
can be anonymous if the passedname
isnull
. 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, ornull
.category
- a string defining the mark category.where
- location to place the mark.- Returns:
- a new
Mark
, owned by the buffer.
-
createSourceTag
public TextTag createSourceTag(@Nullable @Nullable String tagName, @Nullable @Nullable String firstPropertyName, Object... varargs) In short, this is the same function asTextBuffer.createTag(java.lang.String, java.lang.String, java.lang.Object...)
but instead of creating aTextTag
, this function creates aTag
.This function creates a
Tag
and adds it to the tag table forbuffer
. Equivalent to callingTextTag()
and then adding the tag to the buffer’s tag table. The returned tag is owned by the buffer’s tag table, so the ref count will be equal to one.If
tag_name
isnull
, the tag is anonymous.If
tag_name
is non-null
, a tag calledtag_name
must not already exist in the tag table for this buffer.The
first_property_name
argument and subsequent arguments are a list of properties to set on the tag, as withGObject.set(java.lang.String, java.lang.Object...)
.- Parameters:
tagName
- name of the new tag, ornull
firstPropertyName
- name of first property to set, ornull
varargs
-null
-terminated list of property names and values- Returns:
- a new
Tag
.
-
ensureHighlight
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.
-
forwardIterToSourceMark
Movesiter
to the position of the nextMark
of the givencategory
.Returns
true
ifiter
was moved. Ifcategory
is NULL, the next source mark can be of any category.- Parameters:
iter
- an iterator.category
- category to search for, ornull
- Returns:
- whether
iter
was moved.
-
getContextClassesAtIter
Get all defined context classes atiter
.See the
Buffer
description for the list of default context classes.- Parameters:
iter
- aGtkTextIter
.- Returns:
- a new
null
terminated array of context class names. Use g_strfreev() to free the array if it is no longer needed.
-
getHighlightMatchingBrackets
public boolean getHighlightMatchingBrackets()Determines whether bracket match highlighting is activated for the source buffer.- Returns:
true
if the source buffer will highlight matching brackets.
-
getHighlightSyntax
public boolean getHighlightSyntax()Determines whether syntax highlighting is activated in the source buffer.- Returns:
true
if syntax highlighting is enabled,false
otherwise.
-
getImplicitTrailingNewline
public boolean getImplicitTrailingNewline() -
getLanguage
Returns theLanguage
associated with the buffer, seesetLanguage(org.gnome.gtksourceview.Language)
.The returned object should not be unreferenced by the user.
- Returns:
- the
Language
associated with the buffer, ornull
.
-
getLoading
public boolean getLoading() -
getSourceMarksAtIter
Returns the list of marks of the given category atiter
.If
category
isnull
it returns all marks atiter
.- Parameters:
iter
- an iterator.category
- category to search for, ornull
- Returns:
- a newly allocated
GSList
.
-
getSourceMarksAtLine
Returns the list of marks of the given category atline
.If
category
isnull
, all marks atline
are returned.- Parameters:
line
- a line number.category
- category to search for, ornull
- Returns:
- a newly allocated
GSList
.
-
getStyleScheme
Returns theStyleScheme
associated with the buffer, seesetStyleScheme(org.gnome.gtksourceview.StyleScheme)
.The returned object should not be unreferenced by the user.
- Returns:
- the
StyleScheme
associated with the buffer, ornull
.
-
iterBackwardToContextClassToggle
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
. Setsiter
to the location of the toggle, or to the end of the buffer if no toggle is found.See the
Buffer
description for the list of default context classes.- Parameters:
iter
- aGtkTextIter
.contextClass
- the context class.- Returns:
- whether we found a context class toggle before
iter
-
iterForwardToContextClassToggle
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
. Setsiter
to the location of the toggle, or to the end of the buffer if no toggle is found.See the
Buffer
description for the list of default context classes.- Parameters:
iter
- aGtkTextIter
.contextClass
- the context class.- Returns:
- whether we found a context class toggle after
iter
-
iterHasContextClass
Check if the classcontextClass
is set oniter
.See the
Buffer
description for the list of default context classes.- Parameters:
iter
- aGtkTextIter
.contextClass
- class to search for.- Returns:
- whether
iter
has the context class.
-
joinLines
-
removeSourceMarks
Remove all marks ofcategory
betweenstart
andend
from the buffer.If
category
is NULL, all marks in the range will be removed.- Parameters:
start
- aGtkTextIter
.end
- aGtkTextIter
.category
- category to search for, ornull
.
-
setHighlightMatchingBrackets
public void setHighlightMatchingBrackets(boolean highlight) 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
-true
if you want matching brackets highlighted.
-
setHighlightSyntax
public void setHighlightSyntax(boolean highlight) Controls whether syntax is highlighted in the buffer.If
highlight
istrue
, the text will be highlighted according to the syntax patterns specified in theLanguage
set withsetLanguage(org.gnome.gtksourceview.Language)
.If
highlight
isfalse
, syntax highlighting is disabled and all theTextTag
objects that have been added by the syntax highlighting engine are removed from the buffer.- Parameters:
highlight
-true
to enable syntax highlighting,false
to disable it.
-
setImplicitTrailingNewline
public void setImplicitTrailingNewline(boolean implicitTrailingNewline) Sets whether the this Buffer has an implicit trailing newline.If an explicit trailing newline is present in a
TextBuffer
,TextView
shows it as an empty line. This is generally not what the user expects.If
implicitTrailingNewline
istrue
(the default value):- when a
FileLoader
loads the content of a file into the this Buffer, the trailing newline (if present in the file) is not inserted into the this Buffer. - when a
FileSaver
saves the content of the this Buffer into a file, a trailing newline is added to the file.
On the other hand, if
implicitTrailingNewline
isfalse
, the file's content is not modified when loaded into the this Buffer, and the this Buffer's content is not modified when saved into a file.- Parameters:
implicitTrailingNewline
- the new value.
- when a
-
setLanguage
Associates aLanguage
with the buffer.Note that a
Language
affects not only the syntax highlighting, but also the context classes. If you want to disable just the syntax highlighting, seesetHighlightSyntax(boolean)
.The buffer holds a reference to
language
.- Parameters:
language
- aGtkSourceLanguage
to set, ornull
.
-
setStyleScheme
Sets aStyleScheme
to be used by the buffer and the view.Note that a
StyleScheme
affects not only the syntax highlighting, but also otherView
features such as highlighting the current line, matching brackets, the line numbers, etc.Instead of setting a
null
scheme
, it is better to disable syntax highlighting withsetHighlightSyntax(boolean)
, and setting theStyleScheme
with 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
- aGtkSourceStyleScheme
ornull
.
-
sortLines
Sort the lines of text between the specified iterators.- Parameters:
start
- aGtkTextIter
.end
- aGtkTextIter
.flags
-GtkSourceSortFlags
specifying how the sort should behavecolumn
- sort considering the text starting at the given column
-
sortLines
Sort the lines of text between the specified iterators.- Parameters:
start
- aGtkTextIter
.end
- aGtkTextIter
.flags
-GtkSourceSortFlags
specifying how the sort should behavecolumn
- sort considering the text starting at the given column
-
bracketMatched
-
onBracketMatched
public SignalConnection<Buffer.BracketMatchedCallback> onBracketMatched(Buffer.BracketMatchedCallback handler) iter
is set to a valid iterator pointing to the matching bracket ifstate
isBracketMatchType.FOUND
. Otherwiseiter
is meaningless.The signal is emitted only when the
state
changes, typically when the cursor moves.A use-case for this signal is to show messages in a
Statusbar
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitBracketMatched
Emits the "bracket-matched" signal. SeeonBracketMatched(org.gnome.gtksourceview.Buffer.BracketMatchedCallback)
. -
onCursorMoved
public SignalConnection<Buffer.CursorMovedCallback> onCursorMoved(Buffer.CursorMovedCallback handler) The "cursor-moved" signal is emitted when then insertion mark has moved.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitCursorMoved
public void emitCursorMoved()Emits the "cursor-moved" signal. SeeonCursorMoved(org.gnome.gtksourceview.Buffer.CursorMovedCallback)
. -
onHighlightUpdated
public SignalConnection<Buffer.HighlightUpdatedCallback> onHighlightUpdated(Buffer.HighlightUpdatedCallback handler) The ::highlight-updated signal is emitted when the syntax highlighting and context classes are updated in a certain region of thebuffer
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitHighlightUpdated
Emits the "highlight-updated" signal. SeeonHighlightUpdated(org.gnome.gtksourceview.Buffer.HighlightUpdatedCallback)
. -
onSourceMarkUpdated
public SignalConnection<Buffer.SourceMarkUpdatedCallback> onSourceMarkUpdated(Buffer.SourceMarkUpdatedCallback handler) The ::source-mark-updated signal is emitted each time a mark is added to, moved or removed from thebuffer
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitSourceMarkUpdated
Emits the "source-mark-updated" signal. SeeonSourceMarkUpdated(org.gnome.gtksourceview.Buffer.SourceMarkUpdatedCallback)
. -
builder
ABuffer.Builder
object constructs aBuffer
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withBuffer.Builder.build()
.
-