comparison plugin/lawrencium.vim @ 101:2078a8b475c2

More advanced way to know what window to pick to show a diff summary.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 29 Jul 2014 16:45:10 -0700
parents 596f94a7e0fd
children bc6003e7159e
comparison
equal deleted inserted replaced
100:596f94a7e0fd 101:2078a8b475c2
1035 endfunction 1035 endfunction
1036 1036
1037 function! s:HgStatus_DiffSummary(split) abort 1037 function! s:HgStatus_DiffSummary(split) abort
1038 " Get the path of the file the cursor is on. 1038 " Get the path of the file the cursor is on.
1039 let l:path = s:HgStatus_GetSelectedFile() 1039 let l:path = s:HgStatus_GetSelectedFile()
1040 if a:split < 3 1040 " Reuse the same diff summary window
1041 wincmd p 1041 let l:reuse_id = 'lawrencium_diffsum_for_' . bufnr('%')
1042 endif 1042 let l:split_prev_win = (a:split < 3)
1043 call s:HgDiffSummary(l:path, a:split) 1043 let l:args = {'reuse_id': l:reuse_id, 'use_prev_win': l:split_prev_win,
1044 \'split_mode': a:split}
1045 call s:HgDiffSummary(l:path, l:args)
1044 endfunction 1046 endfunction
1045 1047
1046 function! s:HgStatus_QNew(linestart, lineend, patchname, ...) abort 1048 function! s:HgStatus_QNew(linestart, lineend, patchname, ...) abort
1047 " Get the selected filenames. 1049 " Get the selected filenames.
1048 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) 1050 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R'])
1328 1330
1329 " }}} 1331 " }}}
1330 1332
1331 " Hgdiffsum, Hgdiffsumsplit, Hgvdiffsumsplit, Hgtabdiffsum {{{ 1333 " Hgdiffsum, Hgdiffsumsplit, Hgvdiffsumsplit, Hgtabdiffsum {{{
1332 1334
1333 function! s:HgDiffSummary(filename, split, ...) abort 1335 function! s:HgDiffSummary(filename, present_args, ...) abort
1334 " Default revisions to diff: the working directory (null string) 1336 " Default revisions to diff: the working directory (null string)
1335 " and the parent of the working directory (using Mercurial's revsets syntax). 1337 " and the parent of the working directory (using Mercurial's revsets syntax).
1336 " Otherwise, use the 1 or 2 revisions specified as extra parameters. 1338 " Otherwise, use the 1 or 2 revisions specified as extra parameters.
1337 let l:revs = '' 1339 let l:revs = ''
1338 if a:0 == 1 1340 if a:0 == 1
1353 " fancy filename modifiers. 1355 " fancy filename modifiers.
1354 let l:repo = s:hg_repo() 1356 let l:repo = s:hg_repo()
1355 let l:path = expand(a:filename) 1357 let l:path = expand(a:filename)
1356 call s:trace("Diff'ing revisions: '".l:revs."' on file: ".l:path) 1358 call s:trace("Diff'ing revisions: '".l:revs."' on file: ".l:path)
1357 let l:special = l:repo.GetLawrenciumPath(l:path, 'diff', l:revs) 1359 let l:special = l:repo.GetLawrenciumPath(l:path, 'diff', l:revs)
1360
1361 " Build the correct edit command, and switch to the correct window, based
1362 " on the presentation arguments we got.
1363 if type(a:present_args) == type(0)
1364 " Just got a split mode.
1365 let l:valid_args = {'split_mode': a:present_args}
1366 else
1367 " Got complex args.
1368 let l:valid_args = a:present_args
1369 endif
1370
1371 " First, see if we should reuse an existing window based on some buffer
1372 " variable.
1373 let l:target_winnr = -1
1374 let l:split = get(l:valid_args, 'split_mode', 0)
1375 let l:reuse_id = get(l:valid_args, 'reuse_id', '')
1376 if l:reuse_id != ''
1377 let l:target_winnr = s:find_buffer_window(l:reuse_id, 1)
1378 if l:target_winnr > 0 && l:split != 3
1379 " Unless we'll be opening in a new tab, don't split anymore, since
1380 " we found the exact window we wanted.
1381 let l:split = 0
1382 endif
1383 call s:trace("Looking for window with '".l:reuse_id."', found: ".l:target_winnr)
1384 endif
1385 " If we didn't find anything, see if we should use the current or previous
1386 " window.
1387 if l:target_winnr < 0
1388 let l:use_prev_win = get(l:valid_args, 'use_prev_win', 0)
1389 if l:use_prev_win
1390 let l:target_winnr = winnr('#')
1391 call s:trace("Will use previous window: ".l:target_winnr)
1392 endif
1393 endif
1394 " Now let's see what kind of split we want to use, if any.
1358 let l:cmd = 'edit ' 1395 let l:cmd = 'edit '
1359 if a:split == 1 1396 if l:split == 1
1360 let l:cmd = 'rightbelow split ' 1397 let l:cmd = 'rightbelow split '
1361 elseif a:split == 2 1398 elseif l:split == 2
1362 let l:cmd = 'rightbelow vsplit ' 1399 let l:cmd = 'rightbelow vsplit '
1363 elseif a:split == 3 1400 elseif l:split == 3
1364 let l:cmd = 'tabedit ' 1401 let l:cmd = 'tabedit '
1365 endif 1402 endif
1366 execute l:cmd . l:special 1403
1404 " All good now, proceed.
1405 if l:target_winnr > 0
1406 execute l:target_winnr . "wincmd w"
1407 endif
1408 execute 'keepalt ' . l:cmd . l:special
1409
1410 " Set the reuse ID if we had one.
1411 if l:reuse_id != ''
1412 call s:trace("Setting reuse ID '".l:reuse_id."' on buffer: ".bufnr('%'))
1413 call setbufvar('%', l:reuse_id, 1)
1414 endif
1367 endfunction 1415 endfunction
1368 1416
1369 call s:AddMainCommand("-nargs=* Hgdiffsum :call s:HgDiffSummary('%:p', 0, <f-args>)") 1417 call s:AddMainCommand("-nargs=* Hgdiffsum :call s:HgDiffSummary('%:p', 0, <f-args>)")
1370 call s:AddMainCommand("-nargs=* Hgdiffsumsplit :call s:HgDiffSummary('%:p', 1, <f-args>)") 1418 call s:AddMainCommand("-nargs=* Hgdiffsumsplit :call s:HgDiffSummary('%:p', 1, <f-args>)")
1371 call s:AddMainCommand("-nargs=* Hgvdiffsumsplit :call s:HgDiffSummary('%:p', 2, <f-args>)") 1419 call s:AddMainCommand("-nargs=* Hgvdiffsumsplit :call s:HgDiffSummary('%:p', 2, <f-args>)")
1625 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName()) 1673 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName())
1626 let l:path = l:repo.GetFullPath(l:log_path['path']) 1674 let l:path = l:repo.GetFullPath(l:log_path['path'])
1627 1675
1628 " Go to the window we were in before going in the log window, 1676 " Go to the window we were in before going in the log window,
1629 " and split for the diff summary from there. 1677 " and split for the diff summary from there.
1630 wincmd p 1678 let l:reuse_id = 'lawrencium_diffsum_for_' . bufnr('%')
1631 call s:HgDiffSummary(l:path, a:split, l:revs) 1679 let l:split_prev_win = (a:split < 3)
1680 let l:args = {'reuse_id': l:reuse_id, 'use_prev_win': l:split_prev_win,
1681 \'split_mode': a:split}
1682 call s:HgDiffSummary(l:path, l:args, l:revs)
1632 endfunction 1683 endfunction
1633 1684
1634 function! s:HgLog_GetSelectedRev(...) abort 1685 function! s:HgLog_GetSelectedRev(...) abort
1635 if a:0 == 1 1686 if a:0 == 1
1636 let l:line = getline(a:1) 1687 let l:line = getline(a:1)