# Shamelessly ripped and modified from
# https://github.com/SublimeText/TrailingSpaces

import sublime
import sublime_plugin

DEFAULT_MAX_FILE_SIZE = 1048576
DEFAULT_COLOR_SCOPE_NAME = "invalid"
DEFAULT_IS_ENABLED = True
DEFAULT_REGEX = '(\t+ +)|( +\t+)|[\u2026\u2018\u2019\u201c\u201d\u2013\u2014]|[\t ]+$'
DEFAULT_DELAY = 3000

# Set whether the plugin is on or off.
highlighter_settings = sublime.load_settings('highlighter.sublime-settings')
hmw_enabled = bool(highlighter_settings.get('highlighter_enabled', DEFAULT_IS_ENABLED))
hmw_regex = highlighter_settings.get('highlighter_regex', DEFAULT_REGEX)

# Determine if the view is a find results view.
def is_find_results(view):
    return view.settings().get('syntax') and \
        "Find Results" in view.settings().get('syntax')


# Return an array of regions matching regex.
def find_regexes(view):
    return view.find_all(hmw_regex)


# Highlight regex matches.
def highlighter(view):
    max_size = highlighter_settings.get('highlighter_max_file_size',
                                DEFAULT_MAX_FILE_SIZE)
    color_scope_name = highlighter_settings.get('highlighter_scope_name',
                                        DEFAULT_COLOR_SCOPE_NAME)
    if view.size() <= max_size and not is_find_results(view):
        regions = find_regexes(view)
        view.add_regions('HighlighterListener', regions,
                         color_scope_name, "", sublime.DRAW_EMPTY)


# Highlight matching regions.
class HighlighterListener(sublime_plugin.EventListener):
    def __init__(self):
        self.pending = 0

    def on_modified(self, view):
        if hmw_enabled:
            self.pending = self.pending + 1
            sublime.set_timeout(lambda: self.parse(view),
                highlighter_settings.get('highlighter_delay',
                    DEFAULT_DELAY))

    def parse(self, view):
        self.pending = self.pending - 1
        if self.pending > 0:
            return
        highlighter(view)

    def on_activated(self, view):
        if hmw_enabled:
            highlighter(view)

    def on_load(self, view):
        if hmw_enabled:
            highlighter(view)

@@ -81,16 +81,27 @@
 Spaces%0A%0A
+import sys%0A
 import s
@@ -107,16 +107,16 @@
 sublime%0A
-
 import s
@@ -338,59 +338,49 @@
 00%0A%0A
-# Set whether the plugin is on or off.%0Ahighlighter_
+def plugin_loaded():%0A    global Pref%0A    
 sett
@@ -384,16 +384,17 @@
 ettings 
+ 
 = sublim
@@ -445,168 +445,693 @@
 s')%0A
-hmw_enabled = bool(highlighter_settings.get('highlighter_enabled', DEFAULT_IS_ENABLED))%0Ahmw_regex = highlighter_settings.get('highlighter_regex', DEFAULT_REGEX)
+%0A    class Pref:%0A        def load(self):%0A            Pref.enabled          = bool(settings.get('highlighter_enabled', DEFAULT_IS_ENABLED))%0A            Pref.regex            = settings.get('highlighter_regex', DEFAULT_REGEX)%0A            Pref.max_size         = settings.get('highlighter_max_file_size', DEFAULT_MAX_FILE_SIZE)%0A            Pref.color_scope_name = settings.get('highlighter_scope_name', DEFAULT_COLOR_SCOPE_NAME)%0A            Pref.delay            = settings.get('highlighter_delay', DEFAULT_DELAY)%0A%0A    Pref = Pref()%0A    Pref.load()%0A%0A    settings.add_on_change('reload', lambda: Pref.load())%0A%0A# ST2 backwards compatibility%0Aif sys.version_info%5B0%5D == 2:%0A    plugin_loaded()%0A
 %0A%0A# 
@@ -1402,20 +1402,21 @@
 ind_all(
-hmw_
+Pref.
 regex)%0A%0A
@@ -1470,272 +1470,8 @@
 w):%0A
-    max_size = highlighter_settings.get('highlighter_max_file_size',%0A                                DEFAULT_MAX_FILE_SIZE)%0A    color_scope_name = highlighter_settings.get('highlighter_scope_name',%0A                                        DEFAULT_COLOR_SCOPE_NAME)%0A
     
@@ -1488,16 +1488,21 @@
 ze() %3C= 
+Pref.
 max_size
@@ -1652,16 +1652,21 @@
         
+Pref.
 color_sc
@@ -1874,36 +1874,37 @@
 ew):%0A        if 
-hmw_
+Pref.
 enabled:%0A       
@@ -2001,105 +2001,19 @@
 ew),
-%0A                highlighter_settings.get('highlighter_delay',%0A                    DEFAULT_DELAY)
+ Pref.delay
 )%0A%0A 
@@ -2190,36 +2190,37 @@
 ew):%0A        if 
-hmw_
+Pref.
 enabled:%0A       
@@ -2287,12 +2287,13 @@
  if 
-hmw_
+Pref.
 enab

use plugin_loaded()-event fo plugin initialization