comparison plugin/lawrencium.vim @ 73:785d1a1faa6c

Changes and fixes to the `Hglog` window: - Fixed a bug with showing diffs. - Made the diff behaviour and commands more consistent with those of the `Hgstatus` window.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 16 Feb 2014 22:33:11 -0800
parents a987094d5ae6
children e023d7764361
comparison
equal deleted inserted replaced
72:a987094d5ae6 73:785d1a1faa6c
45 " Utility {{{ 45 " Utility {{{
46 46
47 " Strips the ending slash in a path. 47 " Strips the ending slash in a path.
48 function! s:stripslash(path) 48 function! s:stripslash(path)
49 return fnamemodify(a:path, ':s?[/\\]$??') 49 return fnamemodify(a:path, ':s?[/\\]$??')
50 endfunction
51
52 " Surrounds the given string with double quotes.
53 function! s:addquotes(str)
54 return '"' . a:str . '"'
50 endfunction 55 endfunction
51 56
52 " Normalizes the slashes in a path. 57 " Normalizes the slashes in a path.
53 function! s:normalizepath(path) 58 function! s:normalizepath(path)
54 if exists('+shellslash') && &shellslash 59 if exists('+shellslash') && &shellslash
545 550
546 " Lawrencium Files {{{ 551 " Lawrencium Files {{{
547 552
548 " Read revision (`hg cat`) 553 " Read revision (`hg cat`)
549 function! s:read_lawrencium_rev(repo, path_parts, full_path) abort 554 function! s:read_lawrencium_rev(repo, path_parts, full_path) abort
550 if a:path_parts['value'] == '' 555 let l:rev = a:path_parts['value']
556 if l:rev == ''
551 call a:repo.ReadCommandOutput('cat', a:full_path) 557 call a:repo.ReadCommandOutput('cat', a:full_path)
552 else 558 else
553 call a:repo.ReadCommandOutput('cat', '-r', a:path_parts['value'], a:full_path) 559 call a:repo.ReadCommandOutput('cat', '-r', s:addquotes(l:rev), a:full_path)
554 endif 560 endif
555 endfunction 561 endfunction
556 562
557 " Status (`hg status`) 563 " Status (`hg status`)
558 function! s:read_lawrencium_status(repo, path_parts, full_path) abort 564 function! s:read_lawrencium_status(repo, path_parts, full_path) abort
585 let l:commaidx = stridx(a:path_parts['value'], ',') 591 let l:commaidx = stridx(a:path_parts['value'], ',')
586 if l:commaidx > 0 592 if l:commaidx > 0
587 let l:rev1 = strpart(a:path_parts['value'], 0, l:commaidx) 593 let l:rev1 = strpart(a:path_parts['value'], 0, l:commaidx)
588 let l:rev2 = strpart(a:path_parts['value'], l:commaidx + 1) 594 let l:rev2 = strpart(a:path_parts['value'], l:commaidx + 1)
589 if l:rev1 == '-' 595 if l:rev1 == '-'
590 let l:diffargs = [ '-r', l:rev2 ] 596 let l:diffargs = [ '-r', s:addquotes(l:rev2) ]
591 elseif l:rev2 == '-' 597 elseif l:rev2 == '-'
592 let l:diffargs = [ '-r', l:rev1 ] 598 let l:diffargs = [ '-r', s:addquotes(l:rev1) ]
593 else 599 else
594 let l:diffargs = [ '-r', l:rev1, '-r', l:rev2 ] 600 let l:diffargs = [ '-r', s:addquotes(l:rev1), '-r', s:addquotes(l:rev2) ]
595 endif 601 endif
596 elseif a:path_parts['value'] != '' 602 elseif a:path_parts['value'] != ''
597 let l:diffargs = [ '-c', a:path_parts['value'] ] 603 let l:diffargs = [ '-c', a:path_parts['value'] ]
598 else 604 else
599 let l:diffargs = [] 605 let l:diffargs = []
1110 " and the parent of the working directory (using Mercurial's revsets syntax). 1116 " and the parent of the working directory (using Mercurial's revsets syntax).
1111 " Otherwise, use the 1 or 2 revisions specified as extra parameters. 1117 " Otherwise, use the 1 or 2 revisions specified as extra parameters.
1112 let l:rev1 = '' 1118 let l:rev1 = ''
1113 let l:rev2 = 'p1()' 1119 let l:rev2 = 'p1()'
1114 if a:0 == 1 1120 if a:0 == 1
1115 let l:rev2 = a:1 1121 if type(a:1) == type([])
1122 if len(a:1) >= 2
1123 let l:rev1 = a:1[0]
1124 let l:rev2 = a:1[1]
1125 elseif len(a:1) == 1
1126 let l:rev2 = a:1[0]
1127 endif
1128 else
1129 let l:rev2 = a:1
1130 endif
1116 elseif a:0 == 2 1131 elseif a:0 == 2
1117 let l:rev1 = a:1 1132 let l:rev1 = a:1
1118 let l:rev2 = a:2 1133 let l:rev2 = a:2
1119 endif 1134 endif
1120 1135
1150 let l:diffsplit = 'vertical diffsplit' 1165 let l:diffsplit = 'vertical diffsplit'
1151 endif 1166 endif
1152 if l:rev2 == '' 1167 if l:rev2 == ''
1153 execute l:diffsplit . ' ' . fnameescape(l:path) 1168 execute l:diffsplit . ' ' . fnameescape(l:path)
1154 else 1169 else
1155 let l:rev_path = l:repo.GetLawrenciumPath(l:path, 'rev', l:rev1) 1170 let l:rev_path = l:repo.GetLawrenciumPath(l:path, 'rev', l:rev2)
1156 execute l:diffsplit . ' ' . fnameescape(l:rev_path) 1171 execute l:diffsplit . ' ' . fnameescape(l:rev_path)
1157 endif 1172 endif
1158 endfunction 1173 endfunction
1159 1174
1160 function! s:HgDiff_DiffThis() abort 1175 function! s:HgDiff_DiffThis() abort
1240 " Default revisions to diff: the working directory (null string) 1255 " Default revisions to diff: the working directory (null string)
1241 " and the parent of the working directory (using Mercurial's revsets syntax). 1256 " and the parent of the working directory (using Mercurial's revsets syntax).
1242 " Otherwise, use the 1 or 2 revisions specified as extra parameters. 1257 " Otherwise, use the 1 or 2 revisions specified as extra parameters.
1243 let l:revs = '' 1258 let l:revs = ''
1244 if a:0 == 1 1259 if a:0 == 1
1245 let l:revs = a:1 1260 if type(a:1) == type([])
1261 if len(a:1) >= 2
1262 let l:revs = a:1[0] . ',' . a:1[1]
1263 elseif len(a:1) == 1
1264 let l:revs = a:1[0]
1265 endif
1266 else
1267 let l:revs = a:1
1268 endif
1246 elseif a:0 >= 2 1269 elseif a:0 >= 2
1247 let l:revs = a:1 . ',' . a:2 1270 let l:revs = a:1 . ',' . a:2
1248 endif 1271 endif
1249 1272
1250 " Get the current repo, and expand the given filename in case it contains 1273 " Get the current repo, and expand the given filename in case it contains
1421 endif 1444 endif
1422 wincmd P 1445 wincmd P
1423 1446
1424 " Add some other nice commands and mappings. 1447 " Add some other nice commands and mappings.
1425 let l:is_file = (l:path != '' && filereadable(l:repo.GetFullPath(l:path))) 1448 let l:is_file = (l:path != '' && filereadable(l:repo.GetFullPath(l:path)))
1426 command! -buffer -nargs=* Hglogdiff :call s:HgLog_Diff(<f-args>) 1449 command! -buffer -nargs=* Hglogdiffsum :call s:HgLog_DiffSummary(0, <f-args>)
1450 command! -buffer -nargs=* Hglogvdiffsum :call s:HgLog_DiffSummary(1, <f-args>)
1427 if l:is_file 1451 if l:is_file
1428 command! -buffer Hglogrevedit :call s:HgLog_FileRevEdit() 1452 command! -buffer Hglogrevedit :call s:HgLog_FileRevEdit()
1453 command! -buffer -nargs=* Hglogdiff :call s:HgLog_Diff(0, <f-args>)
1454 command! -buffer -nargs=* Hglogvdiff :call s:HgLog_Diff(1, <f-args>)
1429 endif 1455 endif
1430 1456
1431 if g:lawrencium_define_mappings 1457 if g:lawrencium_define_mappings
1432 nnoremap <buffer> <silent> <cr> :Hglogdiff<cr> 1458 nnoremap <buffer> <silent> <C-U> :Hglogdiffsum<cr>
1433 nnoremap <buffer> <silent> q :bdelete!<cr> 1459 nnoremap <buffer> <silent> <C-H> :Hglogvdiffsum<cr>
1460 nnoremap <buffer> <silent> <cr> :Hglogvdiffsum<cr>
1461 nnoremap <buffer> <silent> q :bdelete!<cr>
1434 if l:is_file 1462 if l:is_file
1435 nnoremap <buffer> <silent> <C-E> :Hglogrevedit<cr> 1463 nnoremap <buffer> <silent> <C-E> :Hglogrevedit<cr>
1464 nnoremap <buffer> <silent> <C-D> :Hglogdiff<cr>
1465 nnoremap <buffer> <silent> <C-V> :Hglogvdiff<cr>
1436 endif 1466 endif
1437 endif 1467 endif
1438 1468
1439 " Clean up when the log buffer is deleted. 1469 " Clean up when the log buffer is deleted.
1440 let l:bufobj = s:buffer_obj() 1470 let l:bufobj = s:buffer_obj()
1459 " and open the revision there. 1489 " and open the revision there.
1460 wincmd p 1490 wincmd p
1461 call s:edit_deletable_buffer('lawrencium_rev_for', l:bufobj.nr, l:path) 1491 call s:edit_deletable_buffer('lawrencium_rev_for', l:bufobj.nr, l:path)
1462 endfunction 1492 endfunction
1463 1493
1464 function! s:HgLog_Diff(...) abort 1494 function! s:HgLog_Diff(vertical, ...) abort
1495 let l:revs = []
1465 if a:0 >= 2 1496 if a:0 >= 2
1466 let l:revs = a:1 . ',' . a:2 1497 let l:revs = [a:1, a:2]
1467 elseif a:0 == 1 1498 elseif a:0 == 1
1468 let l:revs = a:1 1499 let l:revs = [a:1, 'p1('.a:1.')']
1469 else 1500 else
1470 let l:revs = s:HgLog_GetSelectedRev() 1501 let l:sel = s:HgLog_GetSelectedRev()
1471 endif 1502 let l:revs = [l:sel, 'p1('.l:sel.')']
1503 endif
1504
1472 let l:repo = s:hg_repo() 1505 let l:repo = s:hg_repo()
1473 let l:bufobj = s:buffer_obj() 1506 let l:bufobj = s:buffer_obj()
1474 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName()) 1507 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName())
1475 let l:path = l:repo.GetLawrenciumPath(l:log_path['path'], 'diff', l:revs) 1508 let l:path = l:repo.GetFullPath(l:log_path['path'])
1509
1510 " Go to the window we were in before going to the log window,
1511 " and open the split diff there.
1512 wincmd p
1513 call s:HgDiff(l:path, a:vertical, l:revs)
1514 endfunction
1515
1516 function! s:HgLog_DiffSummary(vertical, ...) abort
1517 let l:revs = []
1518 if a:0 >= 2
1519 let l:revs = [a:1, a:2]
1520 elseif a:0 == 1
1521 let l:revs = [a:1]
1522 else
1523 let l:revs = [s:HgLog_GetSelectedRev()]
1524 endif
1525
1526 let l:split_type = 1
1527 if a:vertical
1528 let l:split_type = 2
1529 endif
1530
1531 let l:repo = s:hg_repo()
1532 let l:bufobj = s:buffer_obj()
1533 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName())
1534 let l:path = l:repo.GetFullPath(l:log_path['path'])
1476 1535
1477 " Go to the window we were in before going in the log window, 1536 " Go to the window we were in before going in the log window,
1478 " and open the diff there. 1537 " and split for the diff summary from there.
1479 wincmd p 1538 wincmd p
1480 call s:edit_deletable_buffer('lawrencium_diff_for', l:bufobj.nr, l:path) 1539 call s:HgDiffSummary(l:path, l:split_type, l:revs)
1481 endfunction 1540 endfunction
1482 1541
1483 function! s:HgLog_GetSelectedRev(...) abort 1542 function! s:HgLog_GetSelectedRev(...) abort
1484 if a:0 == 1 1543 if a:0 == 1
1485 let l:line = getline(a:1) 1544 let l:line = getline(a:1)