Sublime Forum

Is there a way to cancel closing a view from `on_pre_close`?

#1

I would like to mimic the “Save/Close/Cancel” functionality that you get when closing a view with unsaved changes, but on a scratch view, via plugin on ST3.
Apart from the lack of yes_no_cancel_dialog from earlier releases, I haven’t found a way to signal Sublime that the close should be aborted.
My current workaround is cloning the view (with run_command("clone") for lack of a view.clone() method) and opening a quick panel with the options.
Is there a better way?

0 Likes

#2

You can do what was proposes for the save command (except that it didn’t work there): Stop the user from saving based on a condition

I just verified that the “close” command is indeed forwarded to on_window_command (which means you need to access the view using window.active_view()).
This does obviously not work if the view is closed via API directly, e.g. view.close().

0 Likes

#3

Old topic, I know, but let me share my solution, in case someone finds it useful.

I use it to prevent accidentally loosing bookmarks associated with a cloned view of a file.

My on_pre_close performs

view.window().run_command(‘clone_file’)

After that it copies the list of bookmarks from the view to be closed into the newly created view.

I have this action performed automatically whenever the list of the view’s bookmarks is non-empty: in other words, allowing to close bookmark-less views only.

0 Likes