comparison plugin/lawrencium.vim @ 87:e688b658ce5b

Add variants of existing commands that open things in a new tab page.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 23 Apr 2014 21:43:34 -0700
parents 685dfc4b5862
children 08e0ab5985d2
comparison
equal deleted inserted replaced
86:685dfc4b5862 87:e688b658ce5b
886 execute "setlocal winfixheight" 886 execute "setlocal winfixheight"
887 execute "setlocal winheight=" . (line('$') + 1) 887 execute "setlocal winheight=" . (line('$') + 1)
888 execute "resize " . (line('$') + 1) 888 execute "resize " . (line('$') + 1)
889 889
890 " Add some nice commands. 890 " Add some nice commands.
891 command! -buffer Hgstatusedit :call s:HgStatus_FileEdit() 891 command! -buffer Hgstatusedit :call s:HgStatus_FileEdit(0)
892 command! -buffer Hgstatusdiff :call s:HgStatus_Diff(0) 892 command! -buffer Hgstatusdiff :call s:HgStatus_Diff(0)
893 command! -buffer Hgstatusvdiff :call s:HgStatus_Diff(1) 893 command! -buffer Hgstatusvdiff :call s:HgStatus_Diff(1)
894 command! -buffer Hgstatusdiffsum :call s:HgStatus_DiffSummary(0) 894 command! -buffer Hgstatustabdiff :call s:HgStatus_Diff(2)
895 command! -buffer Hgstatusvdiffsum :call s:HgStatus_DiffSummary(1) 895 command! -buffer Hgstatusdiffsum :call s:HgStatus_DiffSummary(1)
896 command! -buffer Hgstatusvdiffsum :call s:HgStatus_DiffSummary(2)
897 command! -buffer Hgstatustabdiffsum :call s:HgStatus_DiffSummary(3)
896 command! -buffer Hgstatusrefresh :call s:HgStatus_Refresh() 898 command! -buffer Hgstatusrefresh :call s:HgStatus_Refresh()
897 command! -buffer -range Hgstatusaddremove :call s:HgStatus_AddRemove(<line1>, <line2>) 899 command! -buffer -range Hgstatusaddremove :call s:HgStatus_AddRemove(<line1>, <line2>)
898 command! -buffer -range=% -bang Hgstatuscommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 0) 900 command! -buffer -range=% -bang Hgstatuscommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 0)
899 command! -buffer -range=% -bang Hgstatusvcommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 1) 901 command! -buffer -range=% -bang Hgstatusvcommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 1)
900 command! -buffer -range=% -nargs=+ Hgstatusqnew :call s:HgStatus_QNew(<line1>, <line2>, <f-args>) 902 command! -buffer -range=% -nargs=+ Hgstatusqnew :call s:HgStatus_QNew(<line1>, <line2>, <f-args>)
903 " Add some handy mappings. 905 " Add some handy mappings.
904 if g:lawrencium_define_mappings 906 if g:lawrencium_define_mappings
905 nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr> 907 nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr>
906 nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr> 908 nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr>
907 nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr> 909 nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr>
908 nnoremap <buffer> <silent> <C-D> :Hgstatusdiff<cr> 910 nnoremap <buffer> <silent> <C-D> :Hgstatustabdiff<cr>
909 nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr> 911 nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr>
910 nnoremap <buffer> <silent> <C-U> :Hgstatusdiffsum<cr> 912 nnoremap <buffer> <silent> <C-U> :Hgstatusdiffsum<cr>
911 nnoremap <buffer> <silent> <C-H> :Hgstatusvdiffsum<cr> 913 nnoremap <buffer> <silent> <C-H> :Hgstatusvdiffsum<cr>
912 nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr> 914 nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr>
913 nnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr> 915 nnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr>
923 " Just re-edit the buffer, it will reload the contents by calling 925 " Just re-edit the buffer, it will reload the contents by calling
924 " the matching Mercurial command. 926 " the matching Mercurial command.
925 edit 927 edit
926 endfunction 928 endfunction
927 929
928 function! s:HgStatus_FileEdit() abort 930 function! s:HgStatus_FileEdit(newtab) abort
929 " Get the path of the file the cursor is on. 931 " Get the path of the file the cursor is on.
930 let l:filename = s:HgStatus_GetSelectedFile() 932 let l:filename = s:HgStatus_GetSelectedFile()
931 933
932 " If the file is already open in a window, jump to that window. 934 let l:cleanupbufnr = -1
933 " Otherwise, jump to the previous window and open it there. 935 if a:newtab == 0
934 for nr in range(1, winnr('$')) 936 " If the file is already open in a window, jump to that window.
935 let l:br = winbufnr(nr) 937 " Otherwise, jump to the previous window and open it there.
936 let l:bpath = fnamemodify(bufname(l:br), ':p') 938 for nr in range(1, winnr('$'))
937 if l:bpath ==# l:filename 939 let l:br = winbufnr(nr)
938 execute nr . 'wincmd w' 940 let l:bpath = fnamemodify(bufname(l:br), ':p')
939 return 941 if l:bpath ==# l:filename
940 endif 942 execute nr . 'wincmd w'
941 endfor 943 return
942 wincmd p 944 endif
945 endfor
946 wincmd p
947 else
948 " Just open a new tab so we can edit the file there.
949 " We don't use `tabedit` because it messes up the current window
950 " if it happens to be the same file.
951 " We'll just have to clean up the default empty buffer created.
952 tabnew
953 let l:cleanupbufnr = bufnr('%')
954 endif
943 execute 'edit ' . escape(l:filename, ' \') 955 execute 'edit ' . escape(l:filename, ' \')
956 if l:cleanupbufnr >= 0
957 execute 'bdelete ' . l:cleanupbufnr
958 endif
944 endfunction 959 endfunction
945 960
946 function! s:HgStatus_AddRemove(linestart, lineend) abort 961 function! s:HgStatus_AddRemove(linestart, lineend) abort
947 " Get the selected filenames. 962 " Get the selected filenames.
948 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['!', '?']) 963 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['!', '?'])
969 984
970 " Run `Hgcommit` on those paths. 985 " Run `Hgcommit` on those paths.
971 call s:HgCommit(a:bang, a:vertical, l:filenames) 986 call s:HgCommit(a:bang, a:vertical, l:filenames)
972 endfunction 987 endfunction
973 988
974 function! s:HgStatus_Diff(vertical) abort 989 function! s:HgStatus_Diff(split) abort
975 " Open the file and run `Hgdiff` on it. 990 " Open the file and run `Hgdiff` on it.
976 call s:HgStatus_FileEdit() 991 " We also need to translate the split mode for it... if we already
977 call s:HgDiff('%:p', a:vertical) 992 " opened the file in a new tab, `HgDiff` only needs to do a vertical
978 endfunction 993 " split (i.e. split=1).
979 994 let l:newtab = 0
980 function! s:HgStatus_DiffSummary(vertical) abort 995 let l:hgdiffsplit = a:split
996 if a:split == 2
997 let l:newtab = 1
998 let l:hgdiffsplit = 1
999 endif
1000 call s:HgStatus_FileEdit(l:newtab)
1001 call s:HgDiff('%:p', l:hgdiffsplit)
1002 endfunction
1003
1004 function! s:HgStatus_DiffSummary(split) abort
981 " Get the path of the file the cursor is on. 1005 " Get the path of the file the cursor is on.
982 let l:path = s:HgStatus_GetSelectedFile() 1006 let l:path = s:HgStatus_GetSelectedFile()
983 let l:split_type = 1 1007 if a:split < 3
984 if a:vertical 1008 wincmd p
985 let l:split_type = 2 1009 endif
986 endif 1010 call s:HgDiffSummary(l:path, a:split)
987 wincmd p
988 call s:HgDiffSummary(l:path, l:split_type)
989 endfunction 1011 endfunction
990 1012
991 function! s:HgStatus_QNew(linestart, lineend, patchname, ...) abort 1013 function! s:HgStatus_QNew(linestart, lineend, patchname, ...) abort
992 " Get the selected filenames. 1014 " Get the selected filenames.
993 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) 1015 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R'])
1111 1133
1112 call s:AddMainCommand("-bang -nargs=+ -complete=customlist,s:ListRepoFiles Hgvimgrep :call s:HgVimGrep(<bang>0, <f-args>)") 1134 call s:AddMainCommand("-bang -nargs=+ -complete=customlist,s:ListRepoFiles Hgvimgrep :call s:HgVimGrep(<bang>0, <f-args>)")
1113 1135
1114 " }}} 1136 " }}}
1115 1137
1116 " Hgdiff, Hgvdiff {{{ 1138 " Hgdiff, Hgvdiff, Hgtabdiff {{{
1117 1139
1118 function! s:HgDiff(filename, vertical, ...) abort 1140 function! s:HgDiff(filename, split, ...) abort
1119 " Default revisions to diff: the working directory (null string) 1141 " Default revisions to diff: the working directory (null string)
1120 " and the parent of the working directory (using Mercurial's revsets syntax). 1142 " and the parent of the working directory (using Mercurial's revsets syntax).
1121 " Otherwise, use the 1 or 2 revisions specified as extra parameters. 1143 " Otherwise, use the 1 or 2 revisions specified as extra parameters.
1122 let l:rev1 = '' 1144 let l:rev1 = ''
1123 let l:rev2 = 'p1()' 1145 let l:rev2 = 'p1()'
1146 " We'll keep a list of buffers in this diff, so when one exits, the 1168 " We'll keep a list of buffers in this diff, so when one exits, the
1147 " others' 'diff' flag is turned off. 1169 " others' 'diff' flag is turned off.
1148 let l:diff_buffers = [] 1170 let l:diff_buffers = []
1149 1171
1150 " Get the first file and open it. 1172 " Get the first file and open it.
1173 let l:cleanupbufnr = -1
1151 if l:rev1 == '' 1174 if l:rev1 == ''
1152 if bufexists(l:path) 1175 if a:split == 2
1153 execute 'buffer ' . fnameescape(l:path) 1176 " Don't use `tabedit` here because if `l:path` is the same as
1177 " the current path, it will also reload the buffer in the current
1178 " tab/window for some reason, which causes all state to be lost
1179 " (all folds get collapsed again, cursor is moved to start, etc.)
1180 tabnew
1181 let l:cleanupbufnr = bufnr('%')
1182 execute 'edit ' . fnameescape(l:path)
1154 else 1183 else
1155 execute 'edit ' . fnameescape(l:path) 1184 if bufexists(l:path)
1185 execute 'buffer ' . fnameescape(l:path)
1186 else
1187 execute 'edit ' . fnameescape(l:path)
1188 endif
1156 endif 1189 endif
1157 " Make it part of the diff group. 1190 " Make it part of the diff group.
1158 call s:HgDiff_DiffThis() 1191 call s:HgDiff_DiffThis()
1159 else 1192 else
1160 let l:rev_path = l:repo.GetLawrenciumPath(l:path, 'rev', l:rev1) 1193 let l:rev_path = l:repo.GetLawrenciumPath(l:path, 'rev', l:rev1)
1194 if a:split == 2
1195 " See comments above about avoiding `tabedit`.
1196 tabnew
1197 let l:cleanupbufnr = bufnr('%')
1198 endif
1161 execute 'edit ' . fnameescape(l:rev_path) 1199 execute 'edit ' . fnameescape(l:rev_path)
1162 " Make it part of the diff group. 1200 " Make it part of the diff group.
1163 call s:HgDiff_DiffThis() 1201 call s:HgDiff_DiffThis()
1164 endif 1202 endif
1203 if l:cleanupbufnr >= 0
1204 execute 'bdelete ' . l:cleanupbufnr
1205 endif
1165 1206
1166 " Get the second file and open it too. 1207 " Get the second file and open it too.
1167 let l:diffsplit = 'diffsplit' 1208 let l:diffsplit = 'diffsplit'
1168 if a:vertical 1209 if a:split >= 1
1169 let l:diffsplit = 'vertical diffsplit' 1210 let l:diffsplit = 'vertical diffsplit'
1170 endif 1211 endif
1171 if l:rev2 == '' 1212 if l:rev2 == ''
1172 execute l:diffsplit . ' ' . fnameescape(l:path) 1213 execute l:diffsplit . ' ' . fnameescape(l:path)
1173 else 1214 else
1248 autocmd BufWinLeave * call s:HgDiff_CleanUp() 1289 autocmd BufWinLeave * call s:HgDiff_CleanUp()
1249 augroup end 1290 augroup end
1250 1291
1251 call s:AddMainCommand("-nargs=* Hgdiff :call s:HgDiff('%:p', 0, <f-args>)") 1292 call s:AddMainCommand("-nargs=* Hgdiff :call s:HgDiff('%:p', 0, <f-args>)")
1252 call s:AddMainCommand("-nargs=* Hgvdiff :call s:HgDiff('%:p', 1, <f-args>)") 1293 call s:AddMainCommand("-nargs=* Hgvdiff :call s:HgDiff('%:p', 1, <f-args>)")
1253 1294 call s:AddMainCommand("-nargs=* Hgtabdiff :call s:HgDiff('%:p', 2, <f-args>)")
1254 " }}} 1295
1255 1296 " }}}
1256 " Hgdiffsum, Hgdiffsumsplit, Hgvdiffsumsplit {{{ 1297
1298 " Hgdiffsum, Hgdiffsumsplit, Hgvdiffsumsplit, Hgtabdiffsum {{{
1257 1299
1258 function! s:HgDiffSummary(filename, split, ...) abort 1300 function! s:HgDiffSummary(filename, split, ...) abort
1259 " Default revisions to diff: the working directory (null string) 1301 " Default revisions to diff: the working directory (null string)
1260 " and the parent of the working directory (using Mercurial's revsets syntax). 1302 " and the parent of the working directory (using Mercurial's revsets syntax).
1261 " Otherwise, use the 1 or 2 revisions specified as extra parameters. 1303 " Otherwise, use the 1 or 2 revisions specified as extra parameters.
1283 let l:cmd = 'edit ' 1325 let l:cmd = 'edit '
1284 if a:split == 1 1326 if a:split == 1
1285 let l:cmd = 'rightbelow split ' 1327 let l:cmd = 'rightbelow split '
1286 elseif a:split == 2 1328 elseif a:split == 2
1287 let l:cmd = 'rightbelow vsplit ' 1329 let l:cmd = 'rightbelow vsplit '
1330 elseif a:split == 3
1331 let l:cmd = 'tabedit '
1288 endif 1332 endif
1289 execute l:cmd . l:special 1333 execute l:cmd . l:special
1290 endfunction 1334 endfunction
1291 1335
1292 call s:AddMainCommand("-nargs=* Hgdiffsum :call s:HgDiffSummary('%:p', 0, <f-args>)") 1336 call s:AddMainCommand("-nargs=* Hgdiffsum :call s:HgDiffSummary('%:p', 0, <f-args>)")
1293 call s:AddMainCommand("-nargs=* Hgdiffsumsplit :call s:HgDiffSummary('%:p', 1, <f-args>)") 1337 call s:AddMainCommand("-nargs=* Hgdiffsumsplit :call s:HgDiffSummary('%:p', 1, <f-args>)")
1294 call s:AddMainCommand("-nargs=* Hgvdiffsumsplit :call s:HgDiffSummary('%:p', 2, <f-args>)") 1338 call s:AddMainCommand("-nargs=* Hgvdiffsumsplit :call s:HgDiffSummary('%:p', 2, <f-args>)")
1339 call s:AddMainCommand("-nargs=* Hgtabdiffsum :call s:HgDiffSummary('%:p', 3, <f-args>)")
1295 1340
1296 " }}} 1341 " }}}
1297 1342
1298 " Hgcommit {{{ 1343 " Hgcommit {{{
1299 1344
1451 endif 1496 endif
1452 wincmd P 1497 wincmd P
1453 1498
1454 " Add some other nice commands and mappings. 1499 " Add some other nice commands and mappings.
1455 let l:is_file = (l:path != '' && filereadable(l:repo.GetFullPath(l:path))) 1500 let l:is_file = (l:path != '' && filereadable(l:repo.GetFullPath(l:path)))
1456 command! -buffer -nargs=* Hglogdiffsum :call s:HgLog_DiffSummary(0, <f-args>) 1501 command! -buffer -nargs=* Hglogdiffsum :call s:HgLog_DiffSummary(1, <f-args>)
1457 command! -buffer -nargs=* Hglogvdiffsum :call s:HgLog_DiffSummary(1, <f-args>) 1502 command! -buffer -nargs=* Hglogvdiffsum :call s:HgLog_DiffSummary(2, <f-args>)
1503 command! -buffer -nargs=* Hglogtabdiffsum :call s:HgLog_DiffSummary(3, <f-args>)
1458 if l:is_file 1504 if l:is_file
1459 command! -buffer Hglogrevedit :call s:HgLog_FileRevEdit() 1505 command! -buffer Hglogrevedit :call s:HgLog_FileRevEdit()
1460 command! -buffer -nargs=* Hglogdiff :call s:HgLog_Diff(0, <f-args>) 1506 command! -buffer -nargs=* Hglogdiff :call s:HgLog_Diff(0, <f-args>)
1461 command! -buffer -nargs=* Hglogvdiff :call s:HgLog_Diff(1, <f-args>) 1507 command! -buffer -nargs=* Hglogvdiff :call s:HgLog_Diff(1, <f-args>)
1508 command! -buffer -nargs=* Hglogtabdiff :call s:HgLog_Diff(2, <f-args>)
1462 endif 1509 endif
1463 1510
1464 if g:lawrencium_define_mappings 1511 if g:lawrencium_define_mappings
1465 nnoremap <buffer> <silent> <C-U> :Hglogdiffsum<cr> 1512 nnoremap <buffer> <silent> <C-U> :Hglogdiffsum<cr>
1466 nnoremap <buffer> <silent> <C-H> :Hglogvdiffsum<cr> 1513 nnoremap <buffer> <silent> <C-H> :Hglogvdiffsum<cr>
1467 nnoremap <buffer> <silent> <cr> :Hglogvdiffsum<cr> 1514 nnoremap <buffer> <silent> <cr> :Hglogvdiffsum<cr>
1468 nnoremap <buffer> <silent> q :bdelete!<cr> 1515 nnoremap <buffer> <silent> q :bdelete!<cr>
1469 if l:is_file 1516 if l:is_file
1470 nnoremap <buffer> <silent> <C-E> :Hglogrevedit<cr> 1517 nnoremap <buffer> <silent> <C-E> :Hglogrevedit<cr>
1471 nnoremap <buffer> <silent> <C-D> :Hglogdiff<cr> 1518 nnoremap <buffer> <silent> <C-D> :Hglogtabdiff<cr>
1472 nnoremap <buffer> <silent> <C-V> :Hglogvdiff<cr> 1519 nnoremap <buffer> <silent> <C-V> :Hglogvdiff<cr>
1473 endif 1520 endif
1474 endif 1521 endif
1475 1522
1476 " Clean up when the log buffer is deleted. 1523 " Clean up when the log buffer is deleted.
1496 " and open the revision there. 1543 " and open the revision there.
1497 wincmd p 1544 wincmd p
1498 call s:edit_deletable_buffer('lawrencium_rev_for', l:bufobj.nr, l:path) 1545 call s:edit_deletable_buffer('lawrencium_rev_for', l:bufobj.nr, l:path)
1499 endfunction 1546 endfunction
1500 1547
1501 function! s:HgLog_Diff(vertical, ...) abort 1548 function! s:HgLog_Diff(split, ...) abort
1502 let l:revs = [] 1549 let l:revs = []
1503 if a:0 >= 2 1550 if a:0 >= 2
1504 let l:revs = [a:1, a:2] 1551 let l:revs = [a:1, a:2]
1505 elseif a:0 == 1 1552 elseif a:0 == 1
1506 let l:revs = [a:1, 'p1('.a:1.')'] 1553 let l:revs = [a:1, 'p1('.a:1.')']
1514 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName()) 1561 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName())
1515 let l:path = l:repo.GetFullPath(l:log_path['path']) 1562 let l:path = l:repo.GetFullPath(l:log_path['path'])
1516 1563
1517 " Go to the window we were in before going to the log window, 1564 " Go to the window we were in before going to the log window,
1518 " and open the split diff there. 1565 " and open the split diff there.
1519 wincmd p 1566 if a:split < 2
1520 call s:HgDiff(l:path, a:vertical, l:revs) 1567 wincmd p
1521 endfunction 1568 endif
1522 1569 call s:HgDiff(l:path, a:split, l:revs)
1523 function! s:HgLog_DiffSummary(vertical, ...) abort 1570 endfunction
1571
1572 function! s:HgLog_DiffSummary(split, ...) abort
1524 let l:revs = [] 1573 let l:revs = []
1525 if a:0 >= 2 1574 if a:0 >= 2
1526 let l:revs = [a:1, a:2] 1575 let l:revs = [a:1, a:2]
1527 elseif a:0 == 1 1576 elseif a:0 == 1
1528 let l:revs = [a:1] 1577 let l:revs = [a:1]
1529 else 1578 else
1530 let l:revs = [s:HgLog_GetSelectedRev()] 1579 let l:revs = [s:HgLog_GetSelectedRev()]
1531 endif 1580 endif
1532 1581
1533 let l:split_type = 1
1534 if a:vertical
1535 let l:split_type = 2
1536 endif
1537
1538 let l:repo = s:hg_repo() 1582 let l:repo = s:hg_repo()
1539 let l:bufobj = s:buffer_obj() 1583 let l:bufobj = s:buffer_obj()
1540 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName()) 1584 let l:log_path = s:parse_lawrencium_path(l:bufobj.GetName())
1541 let l:path = l:repo.GetFullPath(l:log_path['path']) 1585 let l:path = l:repo.GetFullPath(l:log_path['path'])
1542 1586
1543 " Go to the window we were in before going in the log window, 1587 " Go to the window we were in before going in the log window,
1544 " and split for the diff summary from there. 1588 " and split for the diff summary from there.
1545 wincmd p 1589 wincmd p
1546 call s:HgDiffSummary(l:path, l:split_type, l:revs) 1590 call s:HgDiffSummary(l:path, a:split, l:revs)
1547 endfunction 1591 endfunction
1548 1592
1549 function! s:HgLog_GetSelectedRev(...) abort 1593 function! s:HgLog_GetSelectedRev(...) abort
1550 if a:0 == 1 1594 if a:0 == 1
1551 let l:line = getline(a:1) 1595 let l:line = getline(a:1)