Sublime Forum

New API: Buffer.clear_undo_stack()

#1

== BUILD 4114 == 3 September 2021 ==

When closing a (multi-file) project I want to clear all undo data.
I am assuming todays new API routine will do the trick.

on_pre_close_project(window)
Buffer.clear_undo_stack()

Please help with the code layout for this. I have demonstrated many other (py) working examples but nothing for Buffer.clear_undo_stack(). No matter what and after many hours, it always comes up undefined.

“c:\Users\markr\AppData\Roaming\Sublime Text\Packages\User\On_Project_Close_Clear_Buffers.py”

API Reference
http://www.sublimetext.com/docs/api_reference.html#sublime.View:ver-dev

0 Likes

Disable saving session of a file after closing sublime text 4
#2

Unfortunately the changelog had the wrong thing in it. It’s a method on View, not Buffer.

0 Likes

#4

Can’t we do anything to prevent previous changes to undo after close S4 ?

0 Likes

#5

Good to go. Close and reopen project to clear all file undo history.

import sublime
import sublime_plugin

class Clear_Undo_Stack(sublime_plugin.ViewEventListener):
    def on_load(self):
        self.view.clear_undo_stack()

# class clearundoCommand(sublime_plugin.TextCommand):
#     def run(self, edit):
#         self.view.clear_undo_stack()
#         #
#         # Warning: Can't clear undo stack from text command
#         #
#         #obj = self.view
#         #for attr in dir(obj):
#         #  print("obj.%s = %r" % (attr, getattr(obj, attr)))
#         #
#         # Finds: obj.clear_undo_stack = <bound method View.clear_undo_stack of View(16)>
#
# Open the Python console (Ctrl+`)
# print('\n'*100) to clear screen
# Type: view.run_command("clearundo") and press enter.

API Reference
http://www.sublimetext.com/docs/api_reference.html#ver-dev

Dev Builds Changelog BUILD 4114 3 September 2021
http://www.sublimetext.com/dev

API: Added Buffer.clear_undo_stack()

Please update with: View.clear_undo_stack()

0 Likes

Disable saving session of a file after closing sublime text 4