Home Download Buy Blog Forum Support

run a method on the content of a file buffer

run a method on the content of a file buffer

Postby ftable on Thu Dec 06, 2012 3:50 pm

Hi,

I do not really want to go in plugin development, but I have some python scripts that I would
like to execute on the content of a file in a window by pressing a combination of keys. In Textmate this was rather easy, the
content of a window could be passed through an environment variable and the output could be redirected to a new
window. I am puzzled how to
do this in Sublime. Can this only be done through a plugin? The API looks interesting but daunting
at the same time. Could someone complete the missing
lines below? Or forward me to a tutorial?

Code: Select all
from sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
   def run(self, edit):

        s = *content of window*
             replace content by f(s) where f is a function written in Python by me




thanks,
Frank
ftable
 
Posts: 4
Joined: Thu Dec 06, 2012 3:27 pm

Re: run a method on the content of a file buffer

Postby jbjornson on Fri Dec 07, 2012 9:13 am

...maybe something like this (untested):
Code: Select all
from sublime, sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
   def run(self, edit):
        region = sublime.Region(0, self.view.size())
        file_contents = self.view.substr(region)
        corrected_text = your_function(file_contents)
        self.view.replace(edit, region, corrected_text)
jbjornson
 
Posts: 340
Joined: Tue Oct 27, 2009 4:25 pm
Location: Zurich, Switzerland

Re: run a method on the content of a file buffer

Postby ftable on Fri Dec 07, 2012 1:33 pm

Thanks! This works perfect.
ftable
 
Posts: 4
Joined: Thu Dec 06, 2012 3:27 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests