GtkSourceIndenter — Auto-indentation interface


Types and Values

Object Hierarchy

    ╰── GtkSourceIndenter


GtkSourceIndenter requires GObject.


#include <gtksourceview/gtksource.h>


By default, GtkSourceView can auto-indent as you type when “auto-indent” is enabled. The indentation simply copies the previous lines indentation.

This can be changed by implementing GtkSourceIndenter and setting the “indenter” property.

Implementors of this interface should implement both GtkSourceIndenterInterface.is_trigger and GtkSourceIndenterInterface.indent.

GtkSourceIndenterInterface.is_trigger is called upon key-press to determine of the key press should trigger an indentation. The default implementation of the interface checks to see if the key was GDK_KEY_Return or GDK_KEY_KP_Enter without GDK_SHIFT_MASK set.

GtkSourceIndenterInterface.indent is called after text has been inserted into GtkSourceBuffer when GtkSourceIndenterInterface.is_trigger returned TRUE. The GtkTextIter is placed directly after the inserted character or characters.

It may be beneficial to move the insertion mark using gtk_text_buffer_select_range() depending on how the indenter changes the indentation.

All changes are encapsulated within a single user action so that the user may undo them using standard undo/redo accelerators.


gtk_source_indenter_is_trigger ()

gtk_source_indenter_is_trigger (GtkSourceIndenter *self,
                                GtkSourceView *view,
                                const GtkTextIter *location,
                                GdkModifierType state,
                                guint keyval);

This function is used to determine if a key pressed should cause the indenter to automatically indent.

The default implementation of this virtual method will check to see if keyval is GDK_KEY_Return or GDK_KEY_KP_Enter and state does not have GDK_SHIFT_MASK set. This is to allow the user to avoid indentation when Shift+Return is pressed. Other indenters may want to copy this behavior to provide a consistent experience to users.



a GtkSourceIndenter



a GtkSourceView



the location where ch is to be inserted



modifier state for the insertion



the keyval pressed such as GDK_KEY_Return



TRUE if indentation should be automatically triggered; otherwise FALSE and no indentation will be performed.

Since: 5.0

gtk_source_indenter_indent ()

gtk_source_indenter_indent (GtkSourceIndenter *self,
                            GtkSourceView *view,
                            GtkTextIter *iter);

This function should be implemented to alter the indentation of text within the view. view is provided so that the indenter may retrieve settings such as indentation and tab widths.

iter is the location where the indentation was requested. This typically is after having just inserted a newline (\n) character but can be other situations such as a manually requested indentation or reformatting.

See gtk_source_indenter_is_trigger() for how to trigger indentation on various characters inserted into the buffer.

The implementor of this function is expected to keep iter valid across calls to the function and should contain the location of the insert mark after calling this function.

The default implementation for this virtual function will copy the indentation of the previous line.



a GtkSourceIndenter



a GtkSourceView



the location of the indentation request.


Since: 5.0

Types and Values


typedef struct _GtkSourceIndenter GtkSourceIndenter;

struct GtkSourceIndenterInterface

struct GtkSourceIndenterInterface {
	GTypeInterface parent_iface;

	gboolean (*is_trigger) (GtkSourceIndenter *self,
	                        GtkSourceView     *view,
	                        const GtkTextIter *location,
	                        GdkModifierType    state,
	                        guint              keyval);
	void     (*indent)     (GtkSourceIndenter  *self,
	                        GtkSourceView     *view,
	                        GtkTextIter       *iter);

The virtual function table for GtkSourceIndenter.


GTypeInterface parent_iface;


is_trigger ()

the virtual function pointer for gtk_source_indenter_is_trigger()


indent ()

the virtual function pointer for gtk_source_indenter_indent()


Since: 5.0

See Also