Skip to content

Commit

Permalink
Compare mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjl committed Jun 13, 2011
1 parent a2a2595 commit d5458da
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 3 deletions.
11 changes: 9 additions & 2 deletions plugin/threesome.py
Expand Up @@ -17,8 +17,6 @@ def ThreesomeInit():
init.init()
threesome = init

def ThreesomeDiff():
threesome.modes.current_mode.key_diff()

def ThreesomeOriginal():
threesome.modes.current_mode.key_original()
Expand All @@ -32,8 +30,17 @@ def ThreesomeTwo():
def ThreesomeResult():
threesome.modes.current_mode.key_result()


def ThreesomeGrid():
threesome.modes.key_grid()

def ThreesomeLoupe():
threesome.modes.key_loupe()

def ThreesomeCompare():
threesome.modes.key_compare()


def ThreesomeDiff():
threesome.modes.current_mode.key_diff()

4 changes: 4 additions & 0 deletions plugin/threesome.vim
Expand Up @@ -75,6 +75,9 @@ endfunction"}}}
function! s:ThreesomeLoupe()"{{{
python ThreesomeLoupe()
endfunction"}}}
function! s:ThreesomeCompare()"{{{
python ThreesomeCompare()
endfunction"}}}

function! s:ThreesomeOriginal()"{{{
python ThreesomeOriginal()
Expand All @@ -101,6 +104,7 @@ command! -nargs=0 ThreesomeInit call s:ThreesomeInit()

command! -nargs=0 ThreesomeGrid call s:ThreesomeGrid()
command! -nargs=0 ThreesomeLoupe call s:ThreesomeLoupe()
command! -nargs=0 ThreesomeCompare call s:ThreesomeCompare()

command! -nargs=0 ThreesomeOriginal call s:ThreesomeOriginal()
command! -nargs=0 ThreesomeOne call s:ThreesomeOne()
Expand Down
1 change: 1 addition & 0 deletions plugin/threesomelib/init.py
Expand Up @@ -31,6 +31,7 @@ def process_result():
def bind_global_keys():
keys.bind('g', ':ThreesomeGrid<cr>')
keys.bind('l', ':ThreesomeLoupe<cr>')
keys.bind('c', ':ThreesomeCompare<cr>')

keys.bind('o', ':ThreesomeOriginal<cr>')
keys.bind('1', ':ThreesomeOne<cr>')
Expand Down
122 changes: 122 additions & 0 deletions plugin/threesomelib/modes.py
Expand Up @@ -150,9 +150,127 @@ def key_result(self):
windows.focus(1)
buffers.result.open()

class CompareMode(Mode):
def __init__(self):
self._number_of_diff_modes = 2
return super(CompareMode, self).__init__()


def _init_layout(self):
# Open the layout
windows.close_all()
windows.vsplit()

# Put the buffers in the appropriate windows
windows.focus(1)
buffers.original.open()

windows.focus(2)
buffers.result.open()


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, 3):
windows.focus(i)
vim.command('diffthis')


def activate(self):
self._init_layout()
super(CompareMode, self).activate()


def key_original(self):
windows.focus(1)
buffers.original.open()
self.diff(self._current_diff_mode)

def key_one(self):
def open_one(winnr):
windows.focus(winnr)
buffers.one.open()
self.diff(self._current_diff_mode)

curwindow = windows.currentnr()

# If file one is showing, go to it.
windows.focus(1)
if buffers.current.name == buffers.one.name:
return

windows.focus(2)
if buffers.current.name == buffers.one.name:
return

# If both the original and result are showing, open file one in the
# current window.
windows.focus(1)
if buffers.current.name == buffers.original.name:
windows.focus(2)
if buffers.current.name == buffers.result.name:
open_one(curwindow)
return

# If file two is in window 1, then we open file one in window 1.
windows.focus(1)
if buffers.current.name == buffers.two.name:
open_one(1)
return

# Otherwise, open file one in window 1.
open_one(1)

def key_two(self):
def open_two(winnr):
windows.focus(winnr)
buffers.two.open()
self.diff(self._current_diff_mode)

curwindow = windows.currentnr()

# If file two is showing, go to it.
windows.focus(1)
if buffers.current.name == buffers.two.name:
return

windows.focus(2)
if buffers.current.name == buffers.two.name:
return

# If both the original and result are showing, open file two in the
# current window.
windows.focus(1)
if buffers.current.name == buffers.original.name:
windows.focus(2)
if buffers.current.name == buffers.result.name:
open_two(curwindow)
return

# If file one is in window 2, then we open file two in window 2.
windows.focus(2)
if buffers.current.name == buffers.two.name:
open_two(2)
return

# Otherwise, open file two in window 2.
open_two(2)

def key_result(self):
windows.focus(2)
buffers.result.open()
self.diff(self._current_diff_mode)


grid = GridMode()
loupe = LoupeMode()
compare = CompareMode()


def key_grid():
Expand All @@ -164,3 +282,7 @@ def key_loupe():
global current_mode
current_mode = loupe
loupe.activate()
def key_compare():
global current_mode
current_mode = compare
compare.activate()
17 changes: 17 additions & 0 deletions plugin/threesomelib/util/bufferlib.py
@@ -1,5 +1,7 @@
import os
import vim

ap = os.path.abspath

class Buffer(object):
def __init__(self, i):
Expand Down Expand Up @@ -36,4 +38,19 @@ def two(self):
def result(self):
return Buffer(3)


@property
def current(self):
bufname = ap(vim.eval('bufname("%")'))

if bufname == ap(self.original.name):
return self.original
elif bufname == ap(self.one.name):
return self.one
elif bufname == ap(self.two.name):
return self.two
elif bufname == ap(self.result.name):
return self.result


buffers = _BufferList()
5 changes: 4 additions & 1 deletion plugin/threesomelib/util/windows.py
@@ -1,5 +1,4 @@
import vim
from io import error


def focus(winnr):
Expand All @@ -18,3 +17,7 @@ def split():

def vsplit():
vim.command('wincmd v')

def currentnr():
return int(vim.eval('winnr()'))

0 comments on commit d5458da

Please sign in to comment.