mattst
mattst

Reputation: 13990

Getting the Sublime Text recently closed file list with the API

Is there any way to get the list of recently closed files using the Sublime Text API?

The SO Where is the list of recent files stored? question says they are stored in the Session.sublime_session config file, although I think that file is only updated when ST closes and files closed during the current session are in Auto Save Session.sublime_session.

However the .sublime_session files can not be loaded using the API's load_settings(base_name) method. I am not at all sure it would be safe to open a .sublime_session file directly.

You can open the most recently closed files using the following command (indexed from 0)...

sublime.active_window().run_command('open_recent_file', {"index": 0})

...but is there a way of getting a list of the recently closed files instead (with the API)?

If not, would it be safe to open Auto Save Session.sublime_session for reading?

Thanks.

Upvotes: 1

Views: 348

Answers (1)

mattst
mattst

Reputation: 13990

Firstly, as far as I can tell, it is safe to open Auto Save Session.sublime_session for reading.

Auto Save Session.sublime_session is the file which has the 'live' data in it for ST v3; it looks like ST v2 just uses Session.sublime_session and does not use an auto save equivalent.

The list of recently closed files is stored in the JSON using the file_history key. However it is more complex than I had realized. Recently closed file lists are held for each individual window, this can be seen easily if there are multiple ST windows open, Open Recent in the file menu will show you which files have been recently closed in that window only.

The recently closed file lists (note: plural) can be retrieved using something like the following:

win_index = 0
auto_save_session_path = "/path/to/Auto Save Session.sublime_session"
with open(auto_save_session_path) as session_data_file:
    session_data = json.load(session_data_file)
    recently_closed_files = session_data["windows"][win_index]["file_history"]

Clearly that retrieves just the first window's file_history, to get them all it would be necessary to loop through the list of window entries held in the windows key.

So which window in the JSON corresponds to the active ST window?

Unfortunately this turns out to be more tricky than I'd hoped. Each entry in the windows key list has a helpful sounding window_id, e.g. "window_id": 425, but its value does NOT correspond to the integer returned by the API's sublime.active_window().id() method. The JSON "window_id": 425 entry in my file corresponded to window id() number 3. Clearly ST holds (at least) two different IDs for each window, one for the .sublime_session files and one for the Python API.

The only way I can see to ID the active window is by using the window's currently open buffers. Each entry in the JSON windows key list has a buffers list which holds the currently open files in the window, these could be compared to the files open in the active window (which could be retrieved using the API) to determine which window entry in the JSON file contains the recently closed file list of the active ST window.

This method would have some theoretical pitfalls. For example it wouldn't work if there were 2 windows open with no open files in them or if a single file (the same one) was open in 2 windows. Of course in real world usage this wouldn't generally happen; multiple windows are opened by users so that they can have different files in them.

It turns out that there is a another problem; the frequency with which Auto Save Session.sublime_session is updated. I don't think there is a static fixed time interval. Saving a file triggers an immediate update, but opening and closing files and windows do not, and often there is a gap of at least several minutes between updates.

This means that a plugin that uses the recently closed file list from a .sublime_session file can not even guarantee to contain the most recently closed file.

Keith's suggestion to monitor file closures and maintain an independent list of recently closed files, as done by GotoRecent, would seem to be the way forward, until such time as the ST API adds the ability to get the recently closed files lists.

I hope this helps anyone looking into this issue or who wants to use a .sublime_session file for some other data.

Upvotes: 2

Related Questions