Home Download Buy Blog Forum Support

Need some help writing a plugin

Need some help writing a plugin

Postby Antonio.Trindade on Fri Mar 30, 2012 4:15 am

Hi everybody.

I have just started using Sublime Text 2 and I find it very good, light and fast!

I like to use Emacs for LaTeX editing, but because I'm Portuguese, I have to use a lot of accents.
Emacs has a plugin that automatically replaces accented characters with LaTeX control sequences on save and replaces them back to accents on open.
I would like to write such a plugin for ST2, but I have absolutely no ideia where to start.

I'd appreciate any help.

Thanks in advance
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Re: Need some help writing a plugin

Postby phillip.koebbe on Fri Mar 30, 2012 11:37 am

I'm sorry that I don't have a lot of time to get very detailed, but here is a place to start:

http://www.sublimetext.com/docs/2/api_r ... ntListener

and you can take a look at many plugins [1] to get an idea of how they are structured. I have one [2] that uses on_load and on_post_save that might help you. You'd want to replace on_post_save with on_pre_save for your purposes.

Good luck! If you get stuck and need help, there are many quite talented plugin developers lurking on the forums that I'm sure could get you going in the right direction.

[1] http://wbond.net/sublime_packages/community
[2] https://github.com/phillipkoebbe/DetectSyntax
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Need some help writing a plugin

Postby Antonio.Trindade on Fri Mar 30, 2012 8:13 pm

I have the following code:
import sublime, sublime_plugin

class IsotexCommand(sublime_plugin.TextCommand):
def run(self, edit):
bufferRegion = sublime.Region(0, self.view.size())
bufferContent = self.view.substr(bufferRegion)
bufferContent.replace('{\\~a}', u'ã')

I must be trying to do something very stupid or my understanding of the API is really messed up... :-) Anyway, the ideia is to select the whole buffer, replace the LaTeX accent control sequence with an Unicode accented character.

Can someone tell me what I am doing wrong?

Thanks!
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Re: Need some help writing a plugin

Postby phillip.koebbe on Fri Mar 30, 2012 8:24 pm

Hi Antonio

Here is some code that I wrote for myself to do a multiple find and replace. I'm sorry it doesn't have any comments (shame on me!), but I think it should be pretty self explanatory. Basically, if there is a selection, use it, if not, select the whole document. The key is to reverse the selection regions and process them backwards. When you find/replace, the offsets of the selections change and it is pretty hairy to keep them straight. Working backwards avoids that problem.

Code: Select all
      view = self.view

      if view.has_non_empty_selection_region():
         self.operation_regions = view.sel()
      else:
         self.operation_regions = [sublime.Region(0, view.size())]

      operation_regions = []

      for operation_region in self.operation_regions:
         operation_regions.append(operation_region)

      operation_regions.reverse()

      edit = view.begin_edit()

      for line in self.lines:
         if line.startswith('#'):
            continue

         try:
            find, replace = line.split(self.separator)
         except:
            continue
         
         find = find.strip()
         replace = replace.strip()

         for region in operation_regions:
            region_as_string = view.substr(region)
            if region_as_string.find(find) > -1:
               view.replace(edit, region, region_as_string.replace(find, replace))

      view.end_edit(edit)
      try:
         self.operation_regions.clear()
      except:
         pass
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Need some help writing a plugin

Postby Antonio.Trindade on Fri Mar 30, 2012 8:45 pm

Hi Phillip.

Thanks for your prompt and swift reply! :-)

I have adapted your code, but I get a NameError: name 'view' is not defined, in the line that says: "if view.has_non_empty_selection_region():"

my command is derived from the sublime_plugin.TextCommand class, as you can see above.
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Re: Need some help writing a plugin

Postby phillip.koebbe on Fri Mar 30, 2012 10:02 pm

That's very interesting. Will please post it all again (and please use a code block so it's easier to read)? The code that I posted is from a TextCommand also, and the relevant portion prior to the line you referenced is

Code: Select all
class MfrReplaceCommand(sublime_plugin.TextCommand):
   def run(self, win_cmd):
      view = self.view


I have to disclaim something here: I haven't used this code in a month or two, so I don't know if there have been any changes to the TextCommand API. Whereas the API documentation calls the first parameter "edit", I have "self". Not sure if there is a problem there or not.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Need some help writing a plugin

Postby Antonio.Trindade on Fri Mar 30, 2012 10:37 pm

OK, here goes all my code:
Code: Select all
# coding=utf8

import sublime, sublime_plugin

class IsoTeXCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      view = self.view
         if view.has_non_empty_selection_region():
            self.operation_regions = view.sel()
         else:
            self.operation_regions = [sublime.Region(0, view.size())]
         operation_regions = []
         for operation_region in self.operation_regions:
            operation_regions.append(operation_region)
         operation_regions.reverse()
      operation_regions = [sublime.Region(0, view.size())]
      edit = view.begin_edit()
      for line in self.lines:
         if line.startswith('%'):
            continue
         try:
           find, replace = line.split(self.separator)
        except:
           continue
           find = '{\\\'a}'
         replace = u'á'
         for region in operation_regions:
         region_as_string = view.substr(region)
         if region_as_string.find(find) > -1:
            view.replace(edit, region, region_as_string.replace(find, replace))
         view.end_edit(edit)
         try:
            self.operation_regions.clear()
         except:
            pass
#      bufferRegion = sublime.Region(0, self.view.size())
#      bufferContent = self.view.substr(bufferRegion)
#      bufferContent.replace('{\\~a}', u'ã')
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Re: Need some help writing a plugin

Postby Antonio.Trindade on Fri Mar 30, 2012 10:44 pm

After fixing some wrong indentations, ST2 does not complain any more. It reloads the plugin just fine.

But it does nothing when I invoke it (I am using Ctrl+Alt+Shift+A shortcut)...
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Re: Need some help writing a plugin

Postby phillip.koebbe on Sat Mar 31, 2012 12:55 am

Antonio.Trindade wrote:After fixing some wrong indentations, ST2 does not complain any more. It reloads the plugin just fine.

But it does nothing when I invoke it (I am using Ctrl+Alt+Shift+A shortcut)...


What does your keybinding definition look like? It should be something like

Code: Select all
{ "keys": ["ctrl+alt+shift+a"], "command": "iso_te_x" }
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Need some help writing a plugin

Postby Antonio.Trindade on Sat Mar 31, 2012 1:53 am

I added the following code:
Code: Select all
   def __init__(self, view):
      self.view = view
      print 'Init!'

   def run(self, edit):
      print 'I am here!'
      view = self.view
      .
      .
      .


I can see 'Init!' printed on the console, but no 'I am here!'... :-S Therefore I know the plugin is being loaded and init'ed, but it does not run... Even stranger than that is that 'Init!' is printed twice...
Antonio.Trindade
 
Posts: 6
Joined: Fri Mar 30, 2012 4:07 am

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 4 guests