Home Download Buy Blog Forum Support

Opening files Emacs-style

Re: Opening files Emacs-style

Postby chrisguilbeau on Thu Apr 05, 2012 3:28 pm

I was in the same boat as you and wrote a plugin that allows you to open a file by path from the prompt. Autocomplete and everything. It can be downloaded from:

https://bitbucket.org/chrisguilbeau/cg-sublime

Or if you just want to copy the text and paste it into a file:

Code: Select all
import sublime, sublime_plugin
from os import listdir
from os.path import commonprefix
from os.path import isdir
from os import getenv

class PromptOpenFilePath(sublime_plugin.WindowCommand):

    def run(self):
        currentDir = getenv('HOME') + '/'
        activeView = self.window.active_view()
        if activeView:
            currentFilePath = activeView.file_name()
            if currentFilePath:
                currentFileParts = currentFilePath.split('/')
                currentDir = '/'.join(
                    part for part in currentFileParts[:-1]) + '/'
        self._ip = self.window.show_input_panel(
            "Open file:",
            currentDir,
            self.on_done,
            self.on_change,
            None
        )

    def on_change(self, text):
        if not text:
            return
        if text.endswith('\t'):
            currentFilePath = text.strip('\t')
            currentFileParts = currentFilePath.split('/')
            currentFile = currentFileParts[-1]
            currentDir = '/'.join(
                part for part in currentFileParts[:-1]) + '/'
            filesInDir = [
                fileName
                for fileName in listdir(currentDir)
                if fileName.startswith(currentFile)
            ]
            if filesInDir:
                if len(filesInDir) > 1:
                    sublime.status_message(
                        ', '.join(f for f in filesInDir))
                    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('/') and '/' or '')
                )
            self._ip.end_edit(theEdit)

    def on_done(self, text):
        try:
            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)
chrisguilbeau
 
Posts: 19
Joined: Fri Nov 11, 2011 5:06 am

Re: Opening files Emacs-style

Postby C0D312 on Thu Apr 05, 2012 4:57 pm

@chrisguilbeau's plugin seems to work great. It's a shame window.open_file doesn't open folders :( but other than that, it should work perfectly.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Opening files Emacs-style

Postby rob on Thu Apr 05, 2012 8:40 pm

The package "sublime files" goes some way towards doing what I'd like, but it doesn't tab auto-complete, which is a pity.
rob
 
Posts: 5
Joined: Thu Apr 05, 2012 1:21 pm

Re: Opening files Emacs-style

Postby agibsonsw on Thu Apr 05, 2012 9:21 pm

Presumably replacing '/' with os.path.sep would enable prompt_open_file_path to work with Windows.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Opening files Emacs-style

Postby rob on Thu Apr 05, 2012 9:53 pm

Ah, didn't see you post, Chris. Thanks, the script works well. I found a minor bug which I will PM you about.
rob
 
Posts: 5
Joined: Thu Apr 05, 2012 1:21 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: nomis, pgrippi and 12 guests