Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Sublime text toggle menu plugin with ctypes

By: a guest on Mar 26th, 2010  |  syntax: Python  |  size: 1.61 KB  |  hits: 144  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # openLastClosedFile plugin v1.0.1
  2. #
  3. # example keybinding:
  4. # <binding key="shift+ctrl+t" command="openLastClosedFile"/>
  5.  
  6. import sublime, sublimeplugin, os
  7. from ctypes import wintypes, windll
  8.  
  9. # portable solution, should work with win2000 and later
  10. CSIDL_APPDATA = 26
  11. _SHGetFolderPath = windll.shell32.SHGetFolderPathA
  12. _SHGetFolderPath.argtypes = [wintypes.HWND,
  13.         wintypes.c_int,
  14.         wintypes.HANDLE,
  15.         wintypes.DWORD, wintypes.LPCSTR]
  16.  
  17. appdataPath = wintypes.create_string_buffer(wintypes.MAX_PATH)
  18. result = _SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, appdataPath)
  19. if result <> 0:
  20.         print "[Error] SHGetFolderPath returned: " + str(result)
  21.  
  22. # we need to be able to load the filenames when command gets called
  23. class openLastClosedFileSecure(sublimeplugin.Plugin):
  24.         def onClose(self, view):
  25.                 # untitled / not saved files have no filename
  26.                 if view.fileName() <> None:
  27.                         file = open(appdataPath.value+"\\sublime text\\Packages\\User\\openLastClosedFile.tmp", 'a')
  28.                         file.writelines(view.fileName()+"\n")
  29.                         file.close()
  30.  
  31.  
  32. class openLastClosedFileCommand(sublimeplugin.TextCommand):
  33.         def run(self, view, args):
  34.                 fileName = appdataPath.value+"\\sublime text\\Packages\\User\\openLastClosedFile.tmp"
  35.  
  36.                 if os.path.isfile(fileName):
  37.                         readFile = open(fileName,"r")
  38.                         lines = readFile.readlines()
  39.                         readFile.close()
  40.  
  41.                         length = len(lines)
  42.                        
  43.                         if length > 0:
  44.                                 lastline = lines[length-1]
  45.                                 lastline = lastline.rstrip("\n")
  46.                                
  47.                                 view.window().openFile(lastline, 0, 0)
  48.                                
  49.                                 writeFile = open(fileName, "w")
  50.                                 writeFile.writelines([item for item in lines[0:length-1]])
  51.                                 writeFile.close()