Home Download Buy Blog Forum Support

openLastClosedFile

Re: openLastClosedFile

Postby rdougan on Fri Jun 24, 2011 11:03 pm

Excellent plugin. Thanks.

Slightly off topic, but what is the easiest way to debug these plugins? Say, nothing happens; where do I go? Are there any log/puts commands in python? Does it go anywhere?

Thanks!
rdougan
 
Posts: 44
Joined: Sat Apr 09, 2011 2:37 am

Re: openLastClosedFile

Postby Phunky on Sat Jun 25, 2011 12:36 pm

Thank you so much ehamiter, that got the plugin working :)
Phunky
 
Posts: 35
Joined: Mon Feb 07, 2011 5:02 pm

Re: openLastClosedFile

Postby jbjornson on Thu Jul 14, 2011 2:43 pm

Inspired by dresende's code, but not quite content, I implemented a slightly enhanced version of this plugin. This plugin keeps track of both closed files as well as the files that have been opened. The default behavior of the plugin is to open the most recently closed file, but it can also provide access to a searchable quick panel with the recently closed files followed by a history of the files that have been opened. This can be helpful when using multiple projects or when wanting to reopen a file that you closed a few hours or days ago.

The implementation uses the sublime.Settings API methods to store and cache the file open/close history. The history file is saved in the User sub-directory of the Packages directory (as History.sublime-settings). The history is formatted in JSON like the rest of the sublime settings.

Code: Select all
import sublime, sublime_plugin
import os

HISTORY_SETTINGS_FILE = "History.sublime-settings"
HISTORY_MAX_ENTRIES=500

def get_history(setting_name):
    """load the history using sublime's built-in functionality for accessing settings"""
    history = sublime.load_settings(HISTORY_SETTINGS_FILE)
    if history.has(setting_name):
        return history.get(setting_name)
    else:
        return []

def set_history(setting_name, setting_values):
    """save the history using sublime's built-in functionality for accessing settings"""
    history = sublime.load_settings(HISTORY_SETTINGS_FILE)
    history.set(setting_name, setting_values)
    sublime.save_settings(HISTORY_SETTINGS_FILE)


class OpenRecentlyClosedFileEvent(sublime_plugin.EventListener):
    """class to keep a history of the files that have been opened and closed"""

    def on_close(self, view):
        self.add_to_history(view, "closed", "opened")

    def on_load(self, view):
        self.add_to_history(view, "opened", "closed")

    def add_to_history(self, view, add_to_setting, remove_from_setting):
        filename = os.path.normpath(view.file_name())
        if filename != None:
            add_to_list = get_history(add_to_setting)
            remove_from_list = get_history(remove_from_setting)

            # remove this file from both of the lists
            while filename in remove_from_list:
                remove_from_list.remove(filename)
            while filename in add_to_list:
                add_to_list.remove(filename)

            # add this file to the top of the "add_to_list" (but only if the file actually exists)
            if os.path.exists(filename):
                add_to_list.insert(0, filename)

            # write the history back (making sure to limit the length of the histories)
            set_history(add_to_setting, add_to_list[0:HISTORY_MAX_ENTRIES])
            set_history(remove_from_setting, remove_from_list[0:HISTORY_MAX_ENTRIES])

class OpenRecentlyClosedFileCommand(sublime_plugin.WindowCommand):
    """class to either open the last closed file or show a quick panel with the file access history (closed files first)"""

    def run(self, show_quick_panel=False):
        self.reload_history()
        if show_quick_panel:
            self.window.show_quick_panel(self.file_list, self.open_file)
        else:
            self.open_file(0)

    def reload_history(self):
        history = sublime.load_settings(HISTORY_SETTINGS_FILE)
        self.file_list = get_history("closed") + get_history("opened")

    def open_file(self, index):
        if index >= 0 and len(self.file_list) > index:
            self.window.open_file(self.file_list[index])


I use the following shortcuts to access the plugin:
Code: Select all
{ "keys": ["ctrl+shift+t"], "command": "open_recently_closed_file", "args": {"show_quick_panel": true} },
{ "keys": ["ctrl+alt+shift+t"], "command": "open_recently_closed_file" }
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: openLastClosedFile

Postby jbjornson on Mon Aug 29, 2011 2:53 pm

I added an updated version and uploaded it to github. Unfortunately commiting to github is blocked from my work so I had to add it as a gist.

The updated version now keeps both a global and a per-project history. This allows you to access the file history specific to the active project or the file history across all projects. I find this extremely helpful when opening a project after a few days and forgetting the name of a file that I closed when I last accessed that project.

https://gist.github.com/1133602
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: openLastClosedFile

Postby skube on Mon Mar 05, 2012 9:38 pm

This is sweet jbjornson! I find myself constantly closing files by accident all the time. I only wish it would restore the tab to the same location, like Firefox does.
skube
 
Posts: 86
Joined: Tue Feb 28, 2012 8:42 pm

Re: openLastClosedFile

Postby jbjornson on Tue Mar 06, 2012 8:33 am

Glad you like it :-)

I just had a quick check of the api and it looks like there is something that might help with that: window.set_view_index(view, group, index)

I'll have a look and see if I can get something to work.
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: openLastClosedFile

Postby jbjornson on Tue May 08, 2012 3:47 pm

skube wrote:This is sweet jbjornson! I find myself constantly closing files by accident all the time. I only wish it would restore the tab to the same location, like Firefox does.


Done (finally). It should now restore the tab to the previous location. Please let me know if it works...
https://gist.github.com/1133602
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: openLastClosedFile

Postby FichteFoll on Thu Jun 14, 2012 7:32 pm

I mirrored this plugin on github (here) so it can be installed via Package Control (just search for "File History").
FichteFoll
 
Posts: 388
Joined: Fri Mar 16, 2012 11:49 pm
Location: Germany

Re: openLastClosedFile

Postby jbjornson on Fri Jun 15, 2012 7:33 am

FichteFoll wrote:I mirrored this plugin on github (here) so it can be installed via Package Control (just search for "File History").


Thanks FichteFoll!
jbjornson
 
Posts: 348
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Previous

Return to Plugin Announcements

Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests