Indent.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import sublime, sublime_plugin, re, time
  2. from subprocess import call
  3. # view.run_command('example')
  4. class GnuIndent(sublime_plugin.EventListener):
  5. def on_post_save(self, view):
  6. view.run_command("gnu_indent", {"saving": True})
  7. class GnuIndentCommand(sublime_plugin.TextCommand):
  8. def run(self, edit, saving=False):
  9. view = self.view
  10. global_settings = sublime.load_settings(__name__ + '.sublime-settings')
  11. indent_on_save = view.settings().get('indent_on_save', global_settings.get('indent_on_save', False))
  12. if saving and not indent_on_save:
  13. return
  14. language = self.lang()
  15. if (language == "C++"):
  16. self.indent()
  17. # get the language
  18. def lang(self):
  19. raw_syntax = self.view.settings().get('syntax')
  20. raw_arr = re.split('/', raw_syntax)
  21. return raw_arr[1]
  22. # run indent
  23. def indent(self):
  24. filename = self.view.file_name()
  25. result = call("indent -npro -kr -i8 -ts8 -sob -l80 -ss -ncs -cp1 "+self.view.file_name(), shell=True)
  26. if (result==0):
  27. sublime.status_message("formated")
  28. # remove temp file
  29. os.remove(filename+"~")
  30. else:
  31. sublime.status_message("execute failed")