# HG changeset patch # User Steve Losh # Date 1307929495 14400 # Node ID adb7e3942b34e4d74f8bb4ecf268eb2c8ac28ac5 # Parent 0a0ec65f5e57086748e1f974aa609ef3eebcf59c Omg something works. diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesome.py --- a/plugin/threesome.py Sun Jun 12 20:12:48 2011 -0400 +++ b/plugin/threesome.py Sun Jun 12 21:44:55 2011 -0400 @@ -8,6 +8,26 @@ sys.path.append(plugin_dir) break + +# Wrapper functions +threesome = None def ThreesomeInit(): - from threesomelib.init import init - init() + global threesome + import threesomelib.init as init + init.init() + threesome = init + +def ThreesomeDiff(): + threesome.current_mode.key_diff() + +def ThreesomeOriginal(): + threesome.current_mode.key_original() + +def ThreesomeOne(): + threesome.current_mode.key_one() + +def ThreesomeTwo(): + threesome.current_mode.key_two() + +def ThreesomeResult(): + threesome.current_mode.key_result() diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesome.vim --- a/plugin/threesome.vim Sun Jun 12 20:12:48 2011 -0400 +++ b/plugin/threesome.vim Sun Jun 12 21:44:55 2011 -0400 @@ -68,12 +68,32 @@ exe 'pyfile ' . python_module python ThreesomeInit() endfunction"}}} +function! s:ThreesomeDiff()"{{{ + python ThreesomeDiff() +endfunction"}}} +function! s:ThreesomeOriginal()"{{{ + python ThreesomeOriginal() +endfunction"}}} +function! s:ThreesomeOne()"{{{ + python ThreesomeOne() +endfunction"}}} +function! s:ThreesomeTwo()"{{{ + python ThreesomeTwo() +endfunction"}}} +function! s:ThreesomeResult()"{{{ + python ThreesomeResult() +endfunction"}}} "}}} "{{{ Commands command! -nargs=0 ThreesomeInit call s:ThreesomeInit() +command! -nargs=0 ThreesomeDiff call s:ThreesomeDiff() +command! -nargs=0 ThreesomeOriginal call s:ThreesomeOriginal() +command! -nargs=0 ThreesomeOne call s:ThreesomeOne() +command! -nargs=0 ThreesomeTwo call s:ThreesomeTwo() +command! -nargs=0 ThreesomeResult call s:ThreesomeResult() "}}} diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/init.py --- a/plugin/threesomelib/init.py Sun Jun 12 20:12:48 2011 -0400 +++ b/plugin/threesomelib/init.py Sun Jun 12 21:44:55 2011 -0400 @@ -1,2 +1,40 @@ +import vim +from modes import Grid +from util import windows +from util.buffers import buffers + + +CONFLICT_MARKER_START = '<<<<<<<' +CONFLICT_MARKER_MARK = '=======' +CONFLICT_MARKER_END = '>>>>>>>' + +current_mode = Grid + +def process_result(): + windows.close_all() + buffers.result.open() + + lines = [] + in_conflict = False + for line in buffers.result.lines: + if in_conflict: + if CONFLICT_MARKER_MARK in line: + lines.append(line) + if CONFLICT_MARKER_END in line: + in_conflict = False + continue + + if CONFLICT_MARKER_START in line: + in_conflict = True + continue + + lines.append(line) + + buffers.result.set_lines(lines) + + def init(): - pass + process_result() + current_mode.activate() + + diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/modes.py --- a/plugin/threesomelib/modes.py Sun Jun 12 20:12:48 2011 -0400 +++ b/plugin/threesomelib/modes.py Sun Jun 12 21:44:55 2011 -0400 @@ -1,2 +1,116 @@ +import vim +from util import keys, windows +from util.buffers import buffers +from util.io import error + +class Mode(object): + def __init__(self): + self._current_diff_mode = 0 + return super(Mode, self).__init__() + + + def key_diff(self, diffmode=None): + if diffmode is not None: + getattr(self, '_diff_%d' % diffmode)() + else: + next_diff_mode = self._current_diff_mode + 1 + if next_diff_mode >= self._number_of_diff_modes: + next_diff_mode = 0 + self.diff(next_diff_mode) + + + def key_original(self): + pass + + def key_one(self): + pass + + def key_two(self): + pass + + def key_result(self): + pass + + +class GridMode(Mode): + """ + Layout 1 Layout 2 + +-------------------+ +--------------------------+ + | Original | | One | Result | Two | + |1 | | | | | + +-------------------+ | | | | + | One | Two | | | | | + |2 |3 | | | | | + +-------------------+ | | | | + | Result | | | | | + |4 | |1 |2 |3 | + +-------------------+ +--------------------------+ + """ + + def __init__(self): + self._number_of_diff_modes = 2 + return super(GridMode, self).__init__() + def _init_layout(self): + # Open the layout + windows.close_all() + windows.split() + windows.split() + windows.focus(2) + windows.vsplit() + + # Put the buffers in the appropriate windows + windows.focus(1) + buffers.base.open() + + windows.focus(2) + buffers.one.open() + + windows.focus(3) + buffers.two.open() + + windows.focus(4) + buffers.result.open() + + def _init_keys(self): + keys.bind('d', ':ThreesomeDiff') + keys.bind('o', ':ThreesomeOriginal') + keys.bind('1', ':ThreesomeOne') + keys.bind('2', ':ThreesomeTwo') + keys.bind('r', ':ThreesomeResult') + + + def _diff_0(self): + vim.command('diffoff!') + self._current_diff_mode = 0 + + def _diff_1(self): + vim.command('diffoff!') + self._current_diff_mode = 1 + + for i in range(1, 5): + windows.focus(i) + vim.command('diffthis') + + + def activate(self): + self._init_layout() + self._init_keys() + + + def key_original(self): + windows.focus(1) + + def key_one(self): + windows.focus(2) + + def key_two(self): + windows.focus(3) + + def key_result(self): + windows.focus(4) + + + +Grid = GridMode() diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util.py --- a/plugin/threesomelib/util.py Sun Jun 12 20:12:48 2011 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,4 +0,0 @@ -import sys - -def error(m): - sys.stdout.write(m + '\n') diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/threesomelib/util/__init__.py Sun Jun 12 21:44:55 2011 -0400 @@ -0,0 +1,1 @@ +from misc import * diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util/buffers.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/threesomelib/util/buffers.py Sun Jun 12 21:44:55 2011 -0400 @@ -0,0 +1,39 @@ +import vim + + +class Buffer(object): + def __init__(self, i): + self.number = i + 1 + self._buffer = vim.buffers[i] + self.name = self._buffer.name + + def open(self): + vim.command('%dbuffer' % self.number) + + def set_lines(self, lines): + self._buffer[:] = lines + + @property + def lines(self): + for line in self._buffer: + yield line + + +class _BufferList(object): + @property + def base(self): + return Buffer(0) + + @property + def one(self): + return Buffer(1) + + @property + def two(self): + return Buffer(2) + + @property + def result(self): + return Buffer(3) + +buffers = _BufferList() diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util/io.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/threesomelib/util/io.py Sun Jun 12 21:44:55 2011 -0400 @@ -0,0 +1,5 @@ +import sys + + +def error(m): + sys.stdout.write(m + '\n') diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util/keys.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/threesomelib/util/keys.py Sun Jun 12 21:44:55 2011 -0400 @@ -0,0 +1,5 @@ +import vim + + +def bind(key, to, options='', mode=None, leader=''): + vim.command('nnoremap %s %s%s %s' % (options, leader, key, to)) diff -r 0a0ec65f5e57 -r adb7e3942b34 plugin/threesomelib/util/windows.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugin/threesomelib/util/windows.py Sun Jun 12 21:44:55 2011 -0400 @@ -0,0 +1,20 @@ +import vim +from io import error + + +def focus(winnr): + vim.command('%dwincmd w' % winnr) + +def close(winnr): + focus(winnr) + vim.command('wincmd c') + +def close_all(): + for winnr in range(len(vim.windows) - 1): + close(winnr) + +def split(): + vim.command('wincmd s') + +def vsplit(): + vim.command('wincmd v')