Home Download Buy Blog Forum Support

SublimeText modelines plugin

SublimeText modelines plugin

Postby guillermooo on Wed Feb 10, 2010 10:13 pm

Here's my first public plugin! If you want to use a similar feature to Vim's modelines, you might like it.

Code: Select all
import sublime, sublimeplugin

class ExecuteSublimeTextModeLinesCommand(sublimeplugin.TextCommand):
   This plugin provides a feature similar to vim modelines.
   Modelines set options local to the view by declaring them in the
   source code file itself.
      # st: gutter false
      # st: autoIndent false
   Note that only MAX_LINES_TO_CHECK are searched for modelines.
   TODO: let user declare multiple options per line.
   TODO: some options will fail (rulers).
   TODO: use each language's single-line comment character in

   def _getAllLines(self, view):
      r = sublime.Region(0, view.size())
      return view.splitByNewlines(r)
   def _getModelineCandidates(self, view):
      ls = self._getAllLines(view)   
      endPoint = ls[ min(self.MAX_LINES_TO_CHECK, len(ls)) - 1 ].end()
      modelinesRegion = sublime.Region(0, endPoint)
      return view.substr(modelinesRegion).split('\n')
   def run(self, view, args):
      whereOptionStarts = len(self.SUBLIMETEXT_MODELINE_PREFIX) + 1
      for l in self._getModelineCandidates(view):
         if l.startswith(self.SUBLIMETEXT_MODELINE_PREFIX):
               name, value = l[whereOptionStarts:].split(' ', 1)
            except ValueError:
               print "Modeline syntax error:", l
            view.options().set(name, value)

EDIT: I've posted this to the appropriate forum too...
Posts: 738
Joined: Thu Jul 23, 2009 9:06 am

Return to Ideas and Feature Requests

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests