# Path: opennode/oms/util.py
# def exception_logger(fun):
#     @functools.wraps(fun)
#     def wrapper(*args, **kwargs):
#         try:
#             res = fun(*args, **kwargs)
#             if isinstance(res, defer.Deferred):
#                 @res
#                 def on_error(failure):
#                     log.msg("Got unhandled exception: %s" % failure.getErrorMessage(), system='debug')
#                     if get_config().getboolean('debug', 'print_exceptions'):
#                         log.err(failure, system='debug')
#             return res
#         except Exception:
#             if get_config().getboolean('debug', 'print_exceptions'):
#                 log.err(system='debug')
#             raise
#     return wrapper
# 
# def find_nth(haystack, needle, n, start_boundary=None):
#     start = haystack.find(needle, start_boundary)
#     while start >= 0 and n > 1:
#         start = haystack.find(needle, start + len(needle))
#         n -= 1
#     return start
# 
# Path: opennode/oms/endpoint/ssh/terminal.py
# CTRL_A = '\x01'
# 
# CTRL_C = '\x03'
# 
# CTRL_E = '\x05'
# 
# CTRL_K = '\x0b'
# 
# CTRL_X = '\x18'
# 
# CTRL_Y = '\x19'
# 
# CTRL_S = '\x13'
# 
# CTRL_G = '\x07'
# 
# CTRL_L = '\x0c'
# 
# CTRL_W = '\x17'
# 
# DARK_RED = '\x1b[0;31m'
# 
# RESET_COLOR = '\x1b[0m'


import os
        import string
from contextlib import closing
from copy import copy
from twisted.internet import defer
from twisted.conch.insults import insults
from twisted.python import log
from opennode.oms.util import exception_logger, find_nth
from opennode.oms.endpoint.ssh.terminal import CTRL_A, CTRL_C, CTRL_E, CTRL_K, CTRL_X, CTRL_Y, CTRL_S, CTRL_G, CTRL_L, CTRL_W, DARK_RED, RESET_COLOR




class Editor(object):

    MODELINE_HEIGHT = 2
    COMMENT_COLOR = DARK_RED

    def __init__(self, parent):
        self.parent = parent
        self.terminal = parent.terminal
        self.buffer = ""
        self.old_dirty = False
        self.dirty = False
        self.status_line = ""
        self.has_quit = False
        self.kill_ring = ""
        self.last_handler = None

        self.prefix = None
        self.prefixes = [CTRL_X]

# Path: opennode/oms/util.py
# def exception_logger(fun):
#     @functools.wraps(fun)
#     def wrapper(*args, **kwargs):
#         try:
#             res = fun(*args, **kwargs)
#             if isinstance(res, defer.Deferred):
#                 @res
#                 def on_error(failure):
#                     log.msg("Got unhandled exception: %s" % failure.getErrorMessage(), system='debug')
#                     if get_config().getboolean('debug', 'print_exceptions'):
#                         log.err(failure, system='debug')
#             return res
#         except Exception:
#             if get_config().getboolean('debug', 'print_exceptions'):
#                 log.err(system='debug')
#             raise
#     return wrapper
# 
# def find_nth(haystack, needle, n, start_boundary=None):
#     start = haystack.find(needle, start_boundary)
#     while start >= 0 and n > 1:
#         start = haystack.find(needle, start + len(needle))
#         n -= 1
#     return start
# 
# Path: opennode/oms/endpoint/ssh/terminal.py
# CTRL_A = '\x01'
# 
# CTRL_C = '\x03'
# 
# CTRL_E = '\x05'
# 
# CTRL_K = '\x0b'
# 
# CTRL_X = '\x18'
# 
# CTRL_Y = '\x19'
# 
# CTRL_S = '\x13'
# 
# CTRL_G = '\x07'
# 
# CTRL_L = '\x0c'
# 
# CTRL_W = '\x17'
# 
# DARK_RED = '\x1b[0;31m'
# 
# RESET_COLOR = '\x1b[0m'


# Path: opennode/oms/endpoint/ssh/editor.py
import os
        import string
from contextlib import closing
from copy import copy
from twisted.internet import defer
from twisted.conch.insults import insults
from twisted.python import log
from opennode.oms.util import exception_logger, find_nth
from opennode.oms.endpoint.ssh.terminal import CTRL_A, CTRL_C, CTRL_E, CTRL_K, CTRL_X, CTRL_Y, CTRL_S, CTRL_G, CTRL_L, CTRL_W, DARK_RED, RESET_COLOR





class Editor(object):

    MODELINE_HEIGHT = 2
    COMMENT_COLOR = DARK_RED

    def __init__(self, parent):
        self.parent = parent
        self.terminal = parent.terminal
        self.buffer = ""
        self.old_dirty = False
        self.dirty = False
        self.status_line = ""
        self.has_quit = False
        self.kill_ring = ""
        self.last_handler = None

        self.prefix = None
        self.prefixes = [CTRL_X]

        self.key_handlers = {(CTRL_X, CTRL_S): self.handle_SAVE,