Skip to content

Instantly share code, notes, and snippets.

@bhavanki
Created August 15, 2012 21:08
Show Gist options
  • Save bhavanki/3363700 to your computer and use it in GitHub Desktop.
Save bhavanki/3363700 to your computer and use it in GitHub Desktop.
Sublime Text 2 plugin for emacs-like file open. Based on https://bitbucket.org/chrisguilbeau/cg-sublime.
import sublime, sublime_plugin
from os import listdir
from os.path import commonprefix
from os.path import isdir
#from os import getenv
from os import getcwd
from os import sep
class PromptOpenFilePathCommand(sublime_plugin.WindowCommand):
def run(self):
#currentDir = getenv('HOME') + '/'
currentDir = getcwd() + sep
activeView = self.window.active_view()
if activeView:
currentFilePath = activeView.file_name()
if currentFilePath:
currentFileParts = currentFilePath.split(sep)
currentDir = sep.join(
part for part in currentFileParts[:-1]) + sep
self._ip = self.window.show_input_panel(
"Open file:",
currentDir,
self.on_done,
self.on_change,
self.on_cancel
)
self._ip.settings().set('tab_completion', False)
self._cw = self.window.new_file()
self._cw.set_name('Completions')
self._cw.set_scratch(True)
self._cw.settings().set('word_wrap', True)
self._cw.settings().set('gutter', False)
self._cw.settings().set('command_mode', True)
completionEdit = self._cw.begin_edit()
self._cw.replace(completionEdit,
sublime.Region(0, self._cw.size()),
'Ambiguous tab completions will appear here.')
self._cw.end_edit(completionEdit)
def on_cancel(self):
self.window.run_command('close_file',
{'name' : 'Completions'})
def list_completions(self, filesInDir):
longest = max(filesInDir, key=len)
maxlen = len(longest)
colwidth = maxlen + 1
ncol = 80 // colwidth
curcol = 0
text = ''
for f in filesInDir:
coltext = f + (' ' * (colwidth - len(f)))
text = text + coltext
curcol += 1
if (curcol >= ncol):
text = text + '\n'
curcol = 0
#return '\n'.join(f for f in filesInDir)
return text
def on_change(self, text):
if not text:
return
if text.endswith('\t'):
currentFilePath = text.strip('\t')
currentFileParts = currentFilePath.split(sep)
currentFile = currentFileParts[-1]
currentDir = sep.join(
part for part in currentFileParts[:-1]) + sep
filesInDir = [
fileName
for fileName in listdir(currentDir)
if fileName.startswith(currentFile)
and not fileName.startswith('.')
and not fileName.endswith('~')
]
if filesInDir:
if len(filesInDir) > 1:
#sublime.status_message(
# ', '.join(f for f in filesInDir))
completions = self.list_completions(filesInDir)
completionEdit = self._cw.begin_edit()
self._cw.replace(completionEdit,
sublime.Region(0, self._cw.size()),
completions)
self._cw.end_edit(completionEdit)
newPath = currentDir + commonprefix(filesInDir)
else:
newPath = currentDir + filesInDir[0]
else:
newPath = text[:-1]
sublime.status_message(
'No files match "%s"' % currentFile)
theEdit = self._ip.begin_edit()
allTextRegion = self._ip.full_line(0)
self._ip.replace(
theEdit,
allTextRegion,
newPath + (
isdir(newPath) and not newPath.endswith(sep) and sep or '')
)
self._ip.end_edit(theEdit)
def on_done(self, text):
try:
self.on_cancel()
self.window.open_file(text)
numGroups = self.window.num_groups()
currentGroup = self.window.active_group()
if currentGroup < numGroups - 1:
newGroup = currentGroup + 1
else:
newGroup = 0
self.window.run_command("move_to_group", {"group": newGroup} )
except:
sublime.status_message('Unable to open "%s"' % text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment