Class Buffer

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class Buffer extends TextBuffer

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 Buffer() or withLanguage(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(TextIter, String) take a string parameter as the context class.

GtkSourceBuffer provides an API to access the context classes: iterHasContextClass(TextIter, String), getContextClassesAtIter(TextIter), iterForwardToContextClassToggle(TextIter, String) and iterBackwardToContextClassToggle(TextIter, 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");
buffer = GtkSource.Buffer()

tag_table = buffer.get_tag_table()
tag = tag_table.lookup(name="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 and Gtk.TextTagTable::tag-removed signals.
  • Instead of the GtkSource.Buffer::highlight-updated signal, you can listen to the Gtk.TextBuffer::apply-tag and Gtk.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).

  • Constructor Details

    • Buffer

      public Buffer(MemorySegment address)
      Create a Buffer instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Buffer

      public Buffer(@Nullable TextTagTable table)
      Creates a new source buffer.
      Parameters:
      table - a GtkTextTagTable, or null to create a new one.
    • Buffer

      public Buffer()
      Create a new Buffer.
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Buffer class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • asParent

      protected Buffer asParent()
      Return this instance as if it were its parent type. Comparable to the Java super keyword, but ensures the parent typeclass is also used in native code.
      Overrides:
      asParent in class TextBuffer
      Returns:
      the instance as if it were its parent type
    • withLanguage

      public static Buffer withLanguage(Language language)

      Creates a new source buffer using the highlighting patterns in language.

      This is equivalent to creating a new source buffer with a new tag table and then calling setLanguage(Language).

      Parameters:
      language - a GtkSourceLanguage.
      Returns:
      a new source buffer which will highlight text according to the highlighting patterns in language.
    • backwardIterToSourceMark

      public boolean backwardIterToSourceMark(TextIter iter, @Nullable String category)

      Moves iter to the position of the previous Mark of the given category.

      Returns true if iter was moved. If category is NULL, the previous source mark can be of any category.

      Parameters:
      iter - an iterator.
      category - category to search for, or null
      Returns:
      whether iter was moved.
    • changeCase

      public void changeCase(ChangeCaseType caseType, TextIter start, TextIter end)

      Changes the case of the text between the specified iterators.

      Since 5.4, this function will update the position of start and end to surround the modified text.

      Parameters:
      caseType - how to change the case.
      start - a GtkTextIter.
      end - a GtkTextIter.
    • createSourceMark

      public Mark createSourceMark(@Nullable String name, String category, TextIter where)

      Creates a source mark in the buffer of category category.

      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, a Mark can be anonymous if the passed name is null. 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 null.
      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 String tagName, @Nullable String firstPropertyName, Object... varargs)

      In short, this is the same function as TextBuffer#createTag but instead of creating a TextTag, this function creates a Tag.

      This function creates a Tag and adds it to the tag table for buffer. Equivalent to calling TextTag() 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 is null, the tag is anonymous.

      If tag_name is non-null, a tag called tag_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 with GObject#set.

      Parameters:
      tagName - name of the new tag, or null
      firstPropertyName - name of first property to set, or null
      varargs - null-terminated list of property names and values
      Returns:
      a new Tag.
    • ensureHighlight

      public void ensureHighlight(TextIter start, TextIter end)

      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

      public boolean forwardIterToSourceMark(TextIter iter, @Nullable String category)

      Moves iter to the position of the next Mark of the given category.

      Returns true if iter was moved. If category is NULL, the next source mark can be of any category.

      Parameters:
      iter - an iterator.
      category - category to search for, or null
      Returns:
      whether iter was moved.
    • getContextClassesAtIter

      public String[] getContextClassesAtIter(TextIter iter)

      Get all defined context classes at iter.

      See the Buffer description for the list of default context classes.

      Parameters:
      iter - a GtkTextIter.
      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

      public @Nullable Language getLanguage()

      Returns the Language associated with the buffer, see setLanguage(Language).

      The returned object should not be unreferenced by the user.

      Returns:
      the Language associated with the buffer, or null.
    • getLoading

      public boolean getLoading()
    • getMarkup

      public String getMarkup(TextIter start, TextIter end)

      Returns the text in the specified range converting any text formatting to equivalent Pango markup tags. This allows the styled text to be displayed in other widgets that support Pango markup, such as GtkLabel.

      For very long ranges this function can take long enough that you could potentially miss frame renderings.

      Parameters:
      start - start of range as a GtkTextIter
      end - end of range as a GtkTextIter
      Returns:
      a newly-allocated string containing the text with Pango markup, or null if start and end are invalid.
      Since:
      5.18
    • getSourceMarksAtIter

      public SList<Mark> getSourceMarksAtIter(TextIter iter, @Nullable String category)

      Returns the list of marks of the given category at iter.

      If category is null it returns all marks at iter.

      Parameters:
      iter - an iterator.
      category - category to search for, or null
      Returns:
      a newly allocated GSList.
    • getSourceMarksAtLine

      public SList<Mark> getSourceMarksAtLine(int line, @Nullable String category)

      Returns the list of marks of the given category at line.

      If category is null, all marks at line are returned.

      Parameters:
      line - a line number.
      category - category to search for, or null
      Returns:
      a newly allocated GSList.
    • getStyleScheme

      public @Nullable StyleScheme getStyleScheme()

      Returns the StyleScheme associated with the buffer, see setStyleScheme(StyleScheme).

      The returned object should not be unreferenced by the user.

      Returns:
      the StyleScheme associated with the buffer, or null.
    • iterBackwardToContextClassToggle

      public boolean iterBackwardToContextClassToggle(TextIter iter, String contextClass)

      Moves backward to the next toggle (on or off) of the context class.

      If no matching context class toggles are found, returns false, otherwise true. Does not return toggles located at iter, only toggles after iter. Sets iter 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 - a GtkTextIter.
      contextClass - the context class.
      Returns:
      whether we found a context class toggle before iter
    • iterForwardToContextClassToggle

      public boolean iterForwardToContextClassToggle(TextIter iter, String contextClass)

      Moves forward to the next toggle (on or off) of the context class.

      If no matching context class toggles are found, returns false, otherwise true. Does not return toggles located at iter, only toggles after iter. Sets iter 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 - a GtkTextIter.
      contextClass - the context class.
      Returns:
      whether we found a context class toggle after iter
    • iterHasContextClass

      public boolean iterHasContextClass(TextIter iter, String contextClass)

      Check if the class contextClass is set on iter.

      See the Buffer description for the list of default context classes.

      Parameters:
      iter - a GtkTextIter.
      contextClass - class to search for.
      Returns:
      whether iter has the context class.
    • joinLines

      public void joinLines(TextIter start, TextIter end)
      Joins the lines of text between the specified iterators.
      Parameters:
      start - a GtkTextIter.
      end - a GtkTextIter.
    • removeSourceMarks

      public void removeSourceMarks(TextIter start, TextIter end, @Nullable String category)

      Remove all marks of category between start and end from the buffer.

      If category is NULL, all marks in the range will be removed.

      Parameters:
      start - a GtkTextIter.
      end - a GtkTextIter.
      category - category to search for, or null.
    • 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 is true, the text will be highlighted according to the syntax patterns specified in the Language set with setLanguage(Language).

      If highlight is false, syntax highlighting is disabled and all the TextTag 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 is true (the default value):

      • when a FileLoader loads the content of a file into the buffer, the trailing newline (if present in the file) is not inserted into the 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 is false, the file's content is not modified when loaded into the buffer, and the buffer's content is not modified when saved into a file.

      Parameters:
      implicitTrailingNewline - the new value.
    • setLanguage

      public void setLanguage(@Nullable Language language)

      Associates a Language 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, see setHighlightSyntax(boolean).

      The buffer holds a reference to language.

      Parameters:
      language - a GtkSourceLanguage to set, or null.
    • setStyleScheme

      public void setStyleScheme(@Nullable StyleScheme scheme)

      Sets a StyleScheme to be used by the buffer and the view.

      Note that a StyleScheme affects not only the syntax highlighting, but also other View 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 with setHighlightSyntax(boolean), and setting the StyleScheme 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 - a GtkSourceStyleScheme or null.
    • sortLines

      public void sortLines(TextIter start, TextIter end, Set<SortFlags> flags, int column)
      Sort the lines of text between the specified iterators.
      Parameters:
      start - a GtkTextIter.
      end - a GtkTextIter.
      flags - GtkSourceSortFlags specifying how the sort should behave
      column - sort considering the text starting at the given column
    • sortLines

      public void sortLines(TextIter start, TextIter end, SortFlags flags, int column)
      Sort the lines of text between the specified iterators.
      Parameters:
      start - a GtkTextIter.
      end - a GtkTextIter.
      flags - GtkSourceSortFlags specifying how the sort should behave
      column - sort considering the text starting at the given column
    • bracketMatched

      protected void bracketMatched(TextIter iter, BracketMatchType state)
    • onBracketMatched

      iter is set to a valid iterator pointing to the matching bracket if state is BracketMatchType.FOUND. Otherwise iter 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

      public void emitBracketMatched(@Nullable TextIter iter, BracketMatchType state)
      Emits the "bracket-matched" signal. See onBracketMatched(Buffer.BracketMatchedCallback).
    • onCursorMoved

      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. See onCursorMoved(Buffer.CursorMovedCallback).
    • onHighlightUpdated

      The ::highlight-updated signal is emitted when the syntax highlighting and context classes are updated in a certain region of the buffer.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitHighlightUpdated

      public void emitHighlightUpdated(@Nullable TextIter start, @Nullable TextIter end)
      Emits the "highlight-updated" signal. See onHighlightUpdated(Buffer.HighlightUpdatedCallback).
    • onSourceMarkUpdated

      The ::source-mark-updated signal is emitted each time a mark is added to, moved or removed from the buffer.
      Parameters:
      handler - the signal handler
      Returns:
      a signal handler ID to keep track of the signal connection
      See Also:
    • emitSourceMarkUpdated

      public void emitSourceMarkUpdated(@Nullable TextMark mark)
      Emits the "source-mark-updated" signal. See onSourceMarkUpdated(Buffer.SourceMarkUpdatedCallback).
    • builder

      public static Buffer.Builder<? extends Buffer.Builder> builder()
      A Buffer.Builder object constructs a Buffer with the specified properties. Use the various set...() methods to set properties, and finish construction with Buffer.Builder.build().
      Returns:
      the builder object