<p>I'm trying to keep dependencies to a minimum for a program I contribute to, it's a small text editor.</p>

<p>GTK Textview doesn't seem to come with a built-in undo function. Is there any reference implementation I've been missing on so far? Is everyone writing their own undo function for their TextView widgets?</p>

<p>I'll be happy about any sample code - most happy about python sample code, as our project is in python.</p>

[{'answer_id': 76453, 'author': 'mlibby', 'author_id': 13468, 'author_profile': 'https://Stackoverflow.com/users/13468', 'pm_score': 3, 'selected': True, 'text': '<p>As far as I know, GTK TextView doesn\'t include an undo function. So while I am not familiar with Python\'s GTK library, I would think it doesn\'t have one.</p>\n\n<p>The Ruby-GNOME2 project has a <a href="http://ruby-gnome2.sourceforge.jp/hiki.cgi?Simple+Text+Editor" rel="nofollow noreferrer">sample text editor</a> that has undo/redo functionality. Basically they are connecting to the insert_text and delete_range signals of the TextView widget and recording the events and associated data in a list.</p>\n'}, {'answer_id': 80992, 'author': 'Kai', 'author_id': 2963, 'author_profile': 'https://Stackoverflow.com/users/2963', 'pm_score': 2, 'selected': False, 'text': '<p>Depending on just how dependency-averse you are, and what kind of text editor you\'re building, <a href="http://projects.gnome.org/gtksourceview/" rel="nofollow noreferrer">GtkSourceView</a> adds undo/redo among many other things. Very worth looking at if you want some of the other <a href="http://projects.gnome.org/gtksourceview/features.html" rel="nofollow noreferrer">features</a> it offers.</p>\n'}, {'answer_id': 586115, 'author': 'Florian Heinle', 'author_id': 64673, 'author_profile': 'https://Stackoverflow.com/users/64673', 'pm_score': 3, 'selected': False, 'text': '<p>as a follwow-up: I ported gtksourceview\'s undo mechanism to python: <a href="http://bitbucket.org/tiax/gtk-textbuffer-with-undo/" rel="noreferrer">http://bitbucket.org/tiax/gtk-textbuffer-with-undo/</a></p>\n\n<p>serves as a drop-in replacement for gtksourceview\'s undo</p>\n\n<p>(OP here, but launchpad open-id doesn\'t work anymore)</p>\n'}, {'answer_id': 48927176, 'author': 'oxidworks', 'author_id': 1907997, 'author_profile': 'https://Stackoverflow.com/users/1907997', 'pm_score': 0, 'selected': False, 'text': '<h1>Use GtkSource</h1>\n<ul>\n<li><a href="https://wiki.gnome.org/Projects/GtkSourceView" rel="nofollow noreferrer">https://wiki.gnome.org/Projects/GtkSourceView</a></li>\n<li><a href="https://lazka.github.io/pgi-docs/GtkSource-3.0/" rel="nofollow noreferrer">https://lazka.github.io/pgi-docs/GtkSource-3.0/</a></li>\n<li><a href="https://lazka.github.io/pgi-docs/GtkSource-3.0/classes.html" rel="nofollow noreferrer">https://lazka.github.io/pgi-docs/GtkSource-3.0/classes.html</a></li>\n</ul>\n<p>.</p>\n<ul>\n<li>[Cmnd] + [Z] for undo (default)</li>\n<li>[Cmnd] + [Shift] + [Z] for redo (default)</li>\n<li>[Cmnd] + [Y] for redo (added manually)</li>\n</ul>\n<h1>example:</h1>\n<pre><code>#!/usr/bin/env python3\n# -*- coding: utf-8 -*-\n\nimport gi\ngi.require_version(\'Gtk\', \'3.0\')\nfrom gi.repository import Gtk\nfrom gi.repository import Gdk\ngi.require_version(\'GtkSource\', \'3.0\')\nfrom gi.repository import GtkSource\n\nimport os\n\n\nclass TreeviewWindow(Gtk.Window):\n    def __init__(self):\n        Gtk.Window.__init__(self, title=&quot;TreeviewWindow&quot;)\n        self.set_size_request(300, 300)\n        self.connect(&quot;key-press-event&quot;, self._key_press_event)\n        self.mainbox = Gtk.VBox(spacing=10)\n        self.add(self.mainbox) \n\n        self.textbuffer = GtkSource.Buffer()\n        textview = GtkSource.View(buffer=self.textbuffer)\n        textview.set_editable(True)\n        textview.set_cursor_visible(True)\n        textview.set_show_line_numbers(True)\n        self.mainbox.pack_start(textview, True, True, 0)\n        self.show_all()  \n\n    def _key_press_event(self, widget, event):\n        keyval_name = Gdk.keyval_name(event.keyval)\n        ctrl = (event.state &amp; Gdk.ModifierType.CONTROL_MASK)\n        if ctrl and keyval_name == \'y\':\n            if self.textbuffer.can_redo():\n                self.textbuffer.do_redo(self.textbuffer)\n        \n    def main(self):\n        Gtk.main()\n        \nif __name__ == &quot;__main__&quot;:\n    base = TreeviewWindow()\n    base.main()\n    \n</code></pre>\n'}]
['https://Stackoverflow.com/questions/76096', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/64673/']