:right-sidebar: True Indenter =================================================================== .. currentmodule:: gi.repository.GtkSource .. class:: Indenter(*args, **kwargs) :no-contents-entry: Auto-indentation interface. By default, :obj:`~gi.repository.GtkSource.View` can auto-indent as you type when :obj:`~gi.repository.GtkSource.View.props.auto_indent` is enabled. The indentation simply copies the previous lines indentation. This can be changed by implementing ``GtkSourceIndenter`` and setting the :obj:`~gi.repository.GtkSource.View.props.indenter` property. Implementors of this interface should implement both :obj:`~gi.repository.Indenter.is_trigger` and :obj:`~gi.repository.Indenter.indent`\. :obj:`~gi.repository.Indenter.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 :obj:`~gi.repository.Gdk.KEY_Return` or :obj:`~gi.repository.Gdk.KEY_KP_Enter` without %GDK_SHIFT_MASK set. :obj:`~gi.repository.Indenter.indent` is called after text has been inserted into :obj:`~gi.repository.GtkSource.Buffer` when :obj:`~gi.repository.Indenter.is_trigger` returned :const:`True`. The :obj:`~gi.repository.Gtk.TextIter` is placed directly after the inserted character or characters. It may be beneficial to move the insertion mark using :obj:`~gi.repository.Gtk.TextBuffer.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. Methods ------- .. rst-class:: interim-class .. class:: Indenter :no-index: .. method:: indent(view: ~gi.repository.GtkSource.View, iter: ~gi.repository.Gtk.TextIter) -> ~gi.repository.Gtk.TextIter 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 :obj:`~gi.repository.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. :param view: a :obj:`~gi.repository.GtkSource.View` :param iter: the location of the indentation request .. method:: is_trigger(view: ~gi.repository.GtkSource.View, location: ~gi.repository.Gtk.TextIter, state: ~gi.repository.Gdk.ModifierType, keyval: int) -> bool 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 :obj:`~gi.repository.Gdk.KEY_Return` or :obj:`~gi.repository.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. :param view: a :obj:`~gi.repository.GtkSource.View` :param location: the location where ``ch`` is to be inserted :param state: modifier state for the insertion :param keyval: the keyval pressed such as :obj:`~gi.repository.Gdk.KEY_Return` Virtual Methods --------------- .. rst-class:: interim-class .. class:: Indenter :no-index: .. method:: do_indent(view: ~gi.repository.GtkSource.View, iter: ~gi.repository.Gtk.TextIter) -> ~gi.repository.Gtk.TextIter 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 :obj:`~gi.repository.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. :param view: a :obj:`~gi.repository.GtkSource.View` :param iter: the location of the indentation request .. method:: do_is_trigger(view: ~gi.repository.GtkSource.View, location: ~gi.repository.Gtk.TextIter, state: ~gi.repository.Gdk.ModifierType, keyval: int) -> bool 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 :obj:`~gi.repository.Gdk.KEY_Return` or :obj:`~gi.repository.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. :param view: a :obj:`~gi.repository.GtkSource.View` :param location: the location where ``ch`` is to be inserted :param state: modifier state for the insertion :param keyval: the keyval pressed such as :obj:`~gi.repository.Gdk.KEY_Return`