import code, re, string, StringIO, sys, traceback
import color, completer, lex, method, mode
from lex import Grammar, PatternRule
from mode.python import PythonGrammar
from point import Point

def insert_line(w):
    s = w.buffer.make_string()
    w.mode.lines.append(s)
    w.buffer.set_data('')

class MiniInsertTab(method.Method):
    def _execute(self, w, **vargs):
        w.insert_string_at_cursor(' ' * w.mode.tabwidth)
class MiniInsertLine(method.Method):
    def _execute(self, w, **vargs):
        insert_line(w)
class MiniInsertComplete(method.Method):
    def execute(self, w, **vargs):
        insert_line(w)
        w.buffer.callback('\n'.join(w.mode.lines))
        w.application.close_mini_buffer()
class MiniInsertCancel(method.Method):
    def execute(self, w, **vargs):
        w.application.close_mini_buffer()

class InsertMini(mode.Fundamental):
    modename = 'InsertMini'
    actions = [MiniInsertLine, MiniInsertComplete, MiniInsertCancel,
               MiniInsertTab]
    def __init__(self, w):
        mode.Fundamental.__init__(self, w)
        self.bindings    = {}
        self.saved_input = ""

        self.history = ['']
        self.hindex  = 0
        self.lines   = []

        self.add_bindings('start-of-line', ('C-a', 'HOME',))
        self.add_bindings('end-of-line', ('C-e', 'END',))
        self.add_bindings('backward', ('C-b', 'L_ARROW',))
        self.add_bindings('forward', ('C-f', 'R_ARROW',))
        self.add_bindings('delete-left', ('DELETE', 'BACKSPACE',))
        self.add_bindings('delete-left-word', ('M-DELETE', 'M-BACKSPACE',))
        self.add_bindings('delete-right', ('C-d',))
        self.add_bindings('delete-right-word', ('M-d',))
        self.add_bindings('kill-region', ('C-w',))
        self.add_bindings('copy-region', ('M-w',))
        self.add_bindings('kill', ('C-k',))
        self.add_bindings('copy', ('M-k',))
        self.add_bindings('yank', ('C-y',))
        self.add_bindings('pop-kill', ('M-y',))
        self.add_bindings('right-word', ('M-f',))
        self.add_bindings('left-word', ('M-b',))
        self.add_bindings('set-mark', ('C-@',))
        self.add_bindings('switch-mark', ('C-x C-x',))
        self.add_bindings('undo', ('C-/', 'C-x u',))
        self.add_bindings('redo', ('M-/', 'M-_', 'C-x r',))
        self.add_bindings('toggle-margins', ('M-m',))
        self.add_bindings('transpose-words', ('M-t',))
        self.add_bindings('delete-left-whitespace', ('C-c DELETE', 'C-c BACKSPACE',))
        self.add_bindings('delete-right-whitespace', ('C-c d',))
        self.add_bindings('insert-space', ('SPACE',))
        self.add_bindings('insert-tab', ('TAB',))
        self.add_bindings('mini-insert-line', ('RETURN',))
        self.add_bindings('mini-insert-complete', ('M-RETURN',))
        self.add_bindings('mini-insert-cancel', ('C-]', 'C-g'))
        self.add_bindings('mini-insert-tab', ('TAB',))
        for c in string.letters + string.digits + string.punctuation:
            self.add_binding('insert-string-%s' % c, c)

install = InsertMini.install