Sublime Forum

Get view read only state on load (workspace)

#1

I’m trying to get views is_read_only state.
In .sublime-workspace file, I see buffers[id].settings.read_only
Why can’t I get these value on load, since after loading (?) these views will have this state ?

class ReadonlyCheckViewEventListener(sublime_plugin.ViewEventListener):
	def on_load(self):
		print("ReadOnly Check: on_load of self.view %d" % self.view.id())
		print("self.view.is_read_only() == %s" % str(self.view.is_read_only()))
0 Likes

#2

What did you get? Your snippet works for me. Or you imply ST sets (restores) readonly state after you call the API so you get a wrong value?

0 Likes

#3

Yes, it seems ST sets readonly state after on_load. So when I open a workspace (with a read_only view)
I always get False

Just to clarify:

Open a new window and a new view in it.
Set read_only to True for this view
Save as workspace. Close the window then re-open it.

on_load will print False (True is expected)
Re-check read_only manually? >>> True

0 Likes

#4

There’s something I can do properly for that, instead of just put a timeout and hope there’ll be no problems? ^^

0 Likes

#5

One potential (didn’t try) workaround may be put your code in a function and have the function called in an async thread by sublime.timeout_async with delay=0 (by default).

class ReadonlyCheckViewEventListener(sublime_plugin.ViewEventListener):
    def on_load(self):
        sublime.timeout_async(self.foo)
        
    def foo(self):
        print("ReadOnly Check: on_load of self.view %d" % self.view.id())
        print("self.view.is_read_only() == %s" % str(self.view.is_read_only()))
0 Likes

#6

Yeah, that’s what I did after all. But with no async, and since on_load it’s always false, I tried with 1s of delay.
It’s seems to work for now.
But it’s sad to not have a proper way to do this :smile:
Thanks for your help anyway :slight_smile:

0 Likes