import sublime, sublime_plugin


class DetectModelinesCommand(sublime_plugin.EventListener):

    def check_first_line():
        view.set_status('foobar', view.line(0))

    def on_load(self, view):
        self.check_first_line()

    def on_post_save_async(self, view):
        self.check_first_line()


@@ -7,17 +7,23 @@
  sublime
-,
+%0Aimport
  sublime
@@ -36,142 +36,1628 @@
 n%0A%0A%0A
-class DetectModelinesCommand(sublime_plugin.EventListener):%0A%0A    def check_first_line():%0A        view.set_status('foobar', view.line(0
+def do_modelines(flags):%0A    # if modelines enabled and total_lines %3E 0:%0A    if total_lines %3E 0:%0A%0A        # Prevent recursive entry%0A        if entered:%0A            return%0A        entered += 1%0A%0A        # Check modeline_count lines at the beginning of the file%0A        for line_num in range(1, modeline_count +1):%0A            # if the line exists and checking if it's a modeline encounters an error, pretent the file is empty from now on%0A            if line_num %3C= total_lines and check_modeline(line_num, flags) == FAIL:%0A                total_lines = 0%0A%0A        # Check modeline_count lines from the end of the file%0A        for line_num in %5Btotal_lines - d for d in range(modeline_count)%5D:%0A            if (line_num %3E 0%0A                and line_num %3E modeline_count%0A                and line_num %3E modeline_count - total_lines%0A                and check_modeline(line_num, flags) == FAIL):%0A                total_lines = 0%0A%0A        entered -= 1%0A%0A%0Adef check_modeline(line_num, flags):%0A    line = line_at(line_num)%0A    for i in range(len(line)):%0A        if (i - 1 == -1 or line%5Bi - 1%5D == ' '%0A            and line%5Bi:i + 4%5D.lower() == 'vim:'%0A            or line%5Bi:i+3%5D == 'vi:' or (i != 0 and line%5Bi:i+3%5D == 'ex:')):%0A                # skipping vim version checking @ ll5137-5150 in buffer.c%0A            break%0A%0A    # if modeline init found%0A    if i + 1 != len(line):%0A        while not line%5Bi - 1%5D == ':':%0A            i += 1%0A%0A        line = line%5Bi:%5D%0A        if line == '':%0A            return FAIL%0A%0A%0Aclass DetectModelinesCommand(sublime_plugin.EventListener):%0A%0A    def _check_first_line(self, view):%0A        print(view.line_endings(
 ))%0A%0A
@@ -1690,32 +1690,33 @@
 ):%0A        self.
+_
 check_first_line
@@ -1716,16 +1716,20 @@
 st_line(
+view
 )%0A%0A    d
@@ -1776,16 +1776,17 @@
    self.
+_
 check_fi
@@ -1794,11 +1794,15 @@
 st_line(
+view
 )%0A%0A

Copy in pseudo code