Home Download Buy Blog Forum Support

openLastClosedFile

openLastClosedFile

Postby Shoozza on Fri Mar 26, 2010 1:55 pm

I'm used to press ctrl+shift+t to get a closed tab back in my browser that's why I did the same thing in sublime text and since there was no such keybinding available I implemented my own plugin and added a keymap for it.

This plugin allows you to reopen closed files.
You can open more then one closed file by repeating the command.

Changelog v1.0.2:
Fixed not using package folder to save tmp file when --data commandline option is used

Changelog v1.0.1:
Fixed wrong loading filename

Code: Select all
# openLastClosedFile plugin v1.0.2
#
# example keybinding:
# <binding key="shift+ctrl+t" command="openLastClosedFile"/>
#
# Known issues:
#   there has to be one tab open to make the keybinding work

import sublime, sublimeplugin, os

# we need to be able to load the filenames when command gets called
class openLastClosedFileSecure(sublimeplugin.Plugin):
   def onClose(self, view):
      # untitled / not saved files have no filename
      if view.fileName() <> None:
         file = open(sublime.packagesPath()+"\\User\\openLastClosedFile.tmp", 'a')
         file.writelines(view.fileName()+"\n")
         file.close()
      
      print sublime.packagesPath()


class openLastClosedFileCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      fileName = sublime.packagesPath()+"\\User\\openLastClosedFile.tmp"

      if os.path.isfile(fileName):
         readFile = open(fileName,"r")
         lines = readFile.readlines()
         readFile.close()

         length = len(lines)
         
         if length > 0:
            lastline = lines[length-1]
            lastline = lastline.rstrip("\n")
            
            view.window().openFile(lastline, 0, 0)
            
            writeFile = open(fileName, "w")
            writeFile.writelines([item for item in lines[0:length-1]])
            writeFile.close()


get it here: http://pastebin.com/rXQ3XNcb

(old version v1.0.1) http://pastebin.com/Fw3ZUJ1z
Last edited by Shoozza on Fri Mar 26, 2010 5:08 pm, edited 1 time in total.
Shoozza
 
Posts: 7
Joined: Wed Mar 24, 2010 4:11 pm

Re: openLastClosedFile

Postby gpfsmurf on Fri Mar 26, 2010 4:48 pm

The packages are not always in appdata: see the "--data" command line option. You can find them using "sublime.packagesPath()"
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: openLastClosedFile

Postby Shoozza on Fri Mar 26, 2010 5:10 pm

Thanks! I updated the plugin to use sublime.packagesPath().
Shoozza
 
Posts: 7
Joined: Wed Mar 24, 2010 4:11 pm

Re: openLastClosedFile

Postby dresende on Tue Jun 14, 2011 10:16 am

This is my version of this plugin :)

Code: Select all
import sublime, sublime_plugin
import os

last_closed_file_path = sublime.packages_path() + "/User/last_closed_file.path"

class OpenLastClosedFileCommand(sublime_plugin.WindowCommand):
   def run(self):
      if os.path.exists(last_closed_file_path):
         self.window.open_file(open(last_closed_file_path).read().strip())

class OpenLastClosedFileEvent(sublime_plugin.EventListener):
   def on_close(self, view):
      f = open(last_closed_file_path, "wb")
      f.write(view.file_name())
      f.close()


P.S.: It works without anything opened.
dresende
 
Posts: 4
Joined: Mon Jun 13, 2011 7:57 pm

Re: openLastClosedFile

Postby dresende on Tue Jun 14, 2011 10:22 am

Minor tweak not to record unsaved disk files (New files without name):

Code: Select all
import sublime, sublime_plugin
import os

last_closed_file_path = sublime.packages_path() + "/User/last_closed_file.path"

class OpenLastClosedFileCommand(sublime_plugin.WindowCommand):
   def run(self):
      if os.path.exists(last_closed_file_path):
         self.window.open_file(open(last_closed_file_path).read().strip())

class OpenLastClosedFileEvent(sublime_plugin.EventListener):
   def on_close(self, view):
      if view.file_name() != None:
         f = open(last_closed_file_path, "wb")
         f.write(view.file_name())
         f.close()
dresende
 
Posts: 4
Joined: Mon Jun 13, 2011 7:57 pm

Re: openLastClosedFile

Postby dresende on Tue Jun 14, 2011 10:55 am

I'm on fire.. now with history length, defined on line 5.

Code: Select all
import sublime, sublime_plugin
import os

last_closed_file_path = sublime.packages_path() + "/User/last_closed_file.path"
last_closed_file_max = 10

def get_history():
   f = open(last_closed_file_path, "rb")
   history = f.readlines()
   f.close()

   return history

def save_history(history):
   f = open(last_closed_file_path, "wb")
   f.write("\n".join(map(str, history)))
   f.close()

class OpenLastClosedFileCommand(sublime_plugin.WindowCommand):
   def run(self):
      if os.path.exists(last_closed_file_path):
         history = get_history()

         if len(history) > 0:
            self.window.open_file(history.pop().strip())
            save_history(history)

class OpenLastClosedFileEvent(sublime_plugin.EventListener):
   def on_close(self, view):
      if view.file_name() != None:
         history = get_history()

         history.append(view.file_name())
         if len(history) >last_closed_file_max:
            history = history[-last_closed_file_max:]

         save_history(history)
dresende
 
Posts: 4
Joined: Mon Jun 13, 2011 7:57 pm

Re: openLastClosedFile

Postby Phunky on Fri Jun 17, 2011 8:56 am

Oh boy I never knew about this plugin :) I was asking how to do this in the suggestion forums and had a shortcut key to open the single last file but this is great :D thank you so much!
Phunky
 
Posts: 35
Joined: Mon Feb 07, 2011 5:02 pm

Re: openLastClosedFile

Postby Phunky on Fri Jun 17, 2011 9:05 am

I'm being really dumb, but how do you setup this plugin? I've yet to add any plugins to Sublime so all I did was select "New Plugin" from the menu and pasted the code in, saved the file then setup the key binding as mentioned in the example but it does not seem to run.
Phunky
 
Posts: 35
Joined: Mon Feb 07, 2011 5:02 pm

Re: openLastClosedFile

Postby ehamiter on Fri Jun 24, 2011 6:26 pm

Phunky,

Your user key bindings are probably the older style. Throw this in there:

Code: Select all
{ "keys": ["ctrl+shift+t"], "command": "open_last_closed_file" }


...and I had to create a file in User called "last_closed_file.path" before it started to work.

I noticed something though, at least with dresende's latest version-- once a closed file is re-opened, it will start to walk the directory up each time you invoke the plugin. That is, say I close this file:

/Users/ehamiter/Library/Application Support/Sublime Text 2/Packages/User/foo.txt


then if I hit ctrl+shift+t again, it will "re-open" the User directory, then the Packages directory, etc. Think it needs a bit of tuning but I really like the concept.
ehamiter
 
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am

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

Next

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 12 guests