Sublime Forum

[Fixed] Storing settings for a view in the session

#1

Hi,

Is there a way to detect that ST2 is reopening tabs / continuing a session (hot exit)?
I’ve made the code below (somewhat simplified) but it crashes.

	def on_load(self,view):
		window = sublime.active_window()
		rightIndex = len(window.views()) - 1
		window.set_view_index(view, window.active_group(), rightIndex)
		window.run_command("select_by_index", {"index": rightIndex})

Apparently, running window.run_command on every reopening tab isn’t possible (too fast?).

Is there a way to detect that ST2 is reopening tabs / continuing a session (hot exit)? My code does not need to run in that case. Or, if that is not possible, could anyone give me advice on how to prevent this code from crashing ST2?

Thanks!

0 Likes

#2

Do you want to open new tab at right ?
https://github.com/stylishmedia/SublimeText-Tabright

0 Likes

#3

Hi,

This is the plugin I created. The problem I describe above is a problem that is currently with the plugin. I would like to fix it, but I need some help from the community…

0 Likes

#4

Oops, didn’t know :blush:

Do you mean your code crash ST2 or there’s only an exception in the console ?

I don’t remember well, but I think that for my plugin I use the on_activated event to solve this issue.
When you enter the on_load event, the view is not yet assigned to a window, so I store a flag in the settings to tell the on_activated event to do the job (https://github.com/bizoo/SortTabs/blob/master/auto_sort_tabs.py).

0 Likes

#5

:smiley:

No problem, thanks for taking the time to help! I’m not sure I understand what you mean yet.

The problem I am facing now, is that I use the on_load event to change the order of the tabs, every time a person opens a new file.
But the on_load event also fires, when you start SublimeText2 and the program automatically loads all files that were open in the previous session. (This is a feature called Hot Exit / Remember Open Files. I personally do not use this feature, but some users of course do.)
In this case, the on_load event fires but I would not like to change the order of the tabs in that case.

Maybe I should think of another solution?

I hope I explained well…

Thanks, once again!

0 Likes

#6

From memory, settings are stored in the session.
I think that you can add a settings for each view you processed:

self.view.settings().set('Tabright_processed', True)

So next time you enter the on_load event, you first checked the settings and do nothing if it’s already processed:

[code]if not self.view.settings().get(‘Tabright_processed’, False):
self.view.settings().set(‘Tabright_processed’, True):

your processing code[/code]

Settings will be discarded when you close the file.

This is totally hypothetical and not tested…

0 Likes

#7

Thanks! That was really helpful, I learned something about Sublimetext!

I implemented your solution and I’m waiting to find out if it works for the user reporting the problem in the first place. Thanks for helping me improve this tiny plugin!

0 Likes