comparison plugin/lawrencium.vim @ 59:396da6e76952

Added `Hgannotate` command.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 14 Nov 2012 22:30:49 -0800
parents 621185a5fa48
children ea794e48d4e2
comparison
equal deleted inserted replaced
58:621185a5fa48 59:396da6e76952
28 let g:lawrencium_trace = 0 28 let g:lawrencium_trace = 0
29 endif 29 endif
30 30
31 if !exists('g:lawrencium_define_mappings') 31 if !exists('g:lawrencium_define_mappings')
32 let g:lawrencium_define_mappings = 1 32 let g:lawrencium_define_mappings = 1
33 endif
34
35 if !exists('g:lawrencium_annotate_width_offset')
36 let g:lawrencium_annotate_width_offset = 0
33 endif 37 endif
34 38
35 " }}} 39 " }}}
36 40
37 " Utility {{{ 41 " Utility {{{
1162 call s:AddMainCommand("-nargs=? -complete=customlist,s:ListRepoDirs Hgrepolog :call s:HgLog(0, <f-args>)") 1166 call s:AddMainCommand("-nargs=? -complete=customlist,s:ListRepoDirs Hgrepolog :call s:HgLog(0, <f-args>)")
1163 call s:AddMainCommand("-nargs=? -complete=customlist,s:ListRepoFiles Hglog :call s:HgLog(1, <f-args>)") 1167 call s:AddMainCommand("-nargs=? -complete=customlist,s:ListRepoFiles Hglog :call s:HgLog(1, <f-args>)")
1164 1168
1165 " }}} 1169 " }}}
1166 1170
1171 " Hgannotate {{{
1172
1173 function! s:HgAnnotate() abort
1174 " Get the Lawrencium path for the annotated file.
1175 let l:path = expand('%:p')
1176 let l:bufnr = bufnr('%')
1177 let l:repo = s:hg_repo()
1178 let l:annotation_path = l:repo.GetLawrenciumPath(l:path, 'annotate', '')
1179
1180 " Check if we're trying to annotate something with local changes.
1181 let l:has_local_edits = 0
1182 let l:path_status = l:repo.RunCommand('status', l:path)
1183 if l:path_status != ''
1184 call s:trace("Found local edits for '" . l:path . "'. Will annotate parent revision.")
1185 let l:has_local_edits = 1
1186 endif
1187
1188 if l:has_local_edits
1189 " Just open the output of the command.
1190 echom "Local edits found, will show the annotations for the parent revision."
1191 execute 'edit ' . l:annotation_path
1192 setlocal nowrap nofoldenable
1193 setlocal filetype=hgannotate
1194 else
1195 " Store some info about the current buffer.
1196 let l:cur_bufnr = bufnr('%')
1197 let l:cur_topline = line('w0') + &scrolloff
1198 let l:cur_line = line('.')
1199 let l:cur_wrap = &wrap
1200 let l:cur_foldenable = &foldenable
1201
1202 " Open the annotated file in a split buffer on the left, after
1203 " having disabled wrapping and folds on the current file.
1204 " Make both windows scroll-bound.
1205 setlocal scrollbind nowrap nofoldenable
1206 execute 'keepalt leftabove vsplit ' . l:annotation_path
1207 setlocal nonumber
1208 setlocal scrollbind nowrap nofoldenable foldcolumn=0
1209 setlocal filetype=hgannotate
1210
1211 " When the annotated window is closed, restore the settings we
1212 " changed on the current buffer.
1213 execute 'autocmd BufDelete <buffer> call s:HgAnnotate_Delete(' . l:cur_bufnr . ', ' . l:cur_wrap . ', ' . l:cur_foldenable . ')'
1214
1215 execute l:cur_topline
1216 normal! zt
1217 execute l:cur_line
1218 syncbind
1219
1220 " Set the correct window width for the annotations.
1221 let l:column_count = strlen(matchstr(getline('.'), '[^:]*:')) + g:lawrencium_annotate_width_offset - 1
1222 execute "vertical resize " . l:column_count
1223 set winfixwidth
1224 endif
1225
1226 " Make the annotate buffer a Lawrencium buffer.
1227 let b:mercurial_dir = l:repo.root_dir
1228 let b:lawrencium_annotated_path = l:path
1229 let b:lawrencium_annotated_bufnr = l:bufnr
1230 call s:DefineMainCommands()
1231
1232 " Add some other nice commands and mappings.
1233 command! -buffer Hgannotatediffsum :call s:HgAnnotate_DiffSummary()
1234 if g:lawrencium_define_mappings
1235 nnoremap <buffer> <silent> <cr> :Hgannotatediffsum<cr>
1236 endif
1237 endfunction
1238
1239 function! s:HgAnnotate_Delete(orig_bufnr, orig_wrap, orig_foldenable) abort
1240 execute 'call setwinvar(bufwinnr(' . a:orig_bufnr . '), "&scrollbind", 0)'
1241 if a:orig_wrap
1242 execute 'call setwinvar(bufwinnr(' . a:orig_bufnr . '), "&wrap", 1)'
1243 endif
1244 if a:orig_foldenable
1245 execute 'call setwinvar(bufwinnr(' . a:orig_bufnr . '), "&foldenable", 1)'
1246 endif
1247 endfunction
1248
1249 function! s:HgAnnotate_DiffSummary() abort
1250 let l:line = getline('.')
1251 let l:rev_hash = matchstr(l:line, '\v[a-f0-9]{12}')
1252 let l:repo = s:hg_repo()
1253 let l:path = l:repo.GetLawrenciumPath(b:lawrencium_annotated_path, 'diff', l:rev_hash)
1254 execute b:lawrencium_annotated_bufnr . 'wincmd w'
1255 execute 'keepalt rightbelow split ' . fnameescape(l:path)
1256 endfunction
1257
1258 call s:AddMainCommand("Hgannotate :call s:HgAnnotate()")
1259
1260 " }}}
1261
1167 " Lawrencium files {{{ 1262 " Lawrencium files {{{
1168 1263
1169 function! s:ReadLawrenciumFile(path) abort 1264 function! s:ReadLawrenciumFile(path) abort
1170 call s:trace("Reading Lawrencium file '" . a:path) 1265 call s:trace("Reading Lawrencium file '" . a:path)
1171 let l:comps = s:parse_lawrencium_path(a:path) 1266 let l:comps = s:parse_lawrencium_path(a:path)
1204 if l:comps['path'] != '' && l:comps['path'] != '.' 1299 if l:comps['path'] != '' && l:comps['path'] != '.'
1205 call add(l:diffargs, l:full_path) 1300 call add(l:diffargs, l:full_path)
1206 endif 1301 endif
1207 call l:repo.ReadCommandOutput('diff', l:diffargs) 1302 call l:repo.ReadCommandOutput('diff', l:diffargs)
1208 setlocal filetype=diff 1303 setlocal filetype=diff
1304 elseif l:comps['action'] == 'annotate'
1305 " Annotate file
1306 call l:repo.ReadCommandOutput('annotate', '-c', '-n', '-u', '-d', '-q', l:full_path)
1209 endif 1307 endif
1210 1308
1211 " Setup the new buffer. 1309 " Setup the new buffer.
1212 setlocal readonly 1310 setlocal readonly
1213 setlocal nomodified 1311 setlocal nomodified