comparison plugin/lawrencium.vim @ 67:1cf08e4a7947

Made Lawrencium file easier to extend.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 26 Nov 2012 21:07:43 -0800
parents 75e9d909758a
children 7afcd4d37062
comparison
equal deleted inserted replaced
66:75e9d909758a 67:1cf08e4a7947
243 endif 243 endif
244 endfunction 244 endfunction
245 245
246 " }}} 246 " }}}
247 247
248 " Mercurial Repository {{{ 248 " Mercurial Repository Object {{{
249 249
250 " Let's define a Mercurial repo 'class' using prototype-based object-oriented 250 " Let's define a Mercurial repo 'class' using prototype-based object-oriented
251 " programming. 251 " programming.
252 " 252 "
253 " The prototype dictionary. 253 " The prototype dictionary.
525 endfor 525 endfor
526 execute 'augroup lawrencium_buffer_' . l:bufobj.nr . '_unload' 526 execute 'augroup lawrencium_buffer_' . l:bufobj.nr . '_unload'
527 execute ' autocmd!' 527 execute ' autocmd!'
528 execute 'augroup end' 528 execute 'augroup end'
529 endfunction 529 endfunction
530
531 " }}}
532
533 " Lawrencium Files {{{
534
535 " Read revision (`hg cat`)
536 function! s:read_lawrencium_rev(repo, path_parts, full_path) abort
537 if a:path_parts['value'] == ''
538 call a:repo.ReadCommandOutput('cat', a:full_path)
539 else
540 call a:repo.ReadCommandOutput('cat', '-r', a:path_parts['value'], a:full_path)
541 endif
542 endfunction
543
544 " Status (`hg status`)
545 function! s:read_lawrencium_status(repo, path_parts, full_path) abort
546 if a:path_parts['path'] == ''
547 call a:repo.ReadCommandOutput('status')
548 else
549 call a:repo.ReadCommandOutput('status', a:full_path)
550 endif
551 setlocal filetype=hgstatus
552 endfunction
553
554 " Log (`hg log`)
555 let s:log_style_file = expand("<sfile>:h:h") . "/resources/hg_log.style"
556
557 function! s:read_lawrencium_log(repo, path_parts, full_path) abort
558 if a:path_parts['path'] == ''
559 call a:repo.ReadCommandOutput('log', '--style', shellescape(s:log_style_file))
560 else
561 call a:repo.ReadCommandOutput('log', '--style', shellescape(s:log_style_file), a:full_path)
562 endif
563 setlocal filetype=hglog
564 endfunction
565
566 " Diff revisions (`hg diff`)
567 function! s:read_lawrencium_diff(repo, path_parts, full_path) abort
568 let l:diffargs = []
569 let l:commaidx = stridx(a:path_parts['value'], ',')
570 if l:commaidx > 0
571 let l:rev1 = strpart(a:path_parts['value'], 0, l:commaidx)
572 let l:rev2 = strpart(a:path_parts['value'], l:commaidx + 1)
573 if l:rev1 == '-'
574 let l:diffargs = [ '-r', l:rev2 ]
575 elseif l:rev2 == '-'
576 let l:diffargs = [ '-r', l:rev1 ]
577 else
578 let l:diffargs = [ '-r', l:rev1, '-r', l:rev2 ]
579 endif
580 elseif a:path_parts['value'] != ''
581 let l:diffargs = [ '-c', a:path_parts['value'] ]
582 else
583 let l:diffargs = []
584 endif
585 if a:path_parts['path'] != '' && a:path_parts['path'] != '.'
586 call add(l:diffargs, a:full_path)
587 endif
588 call a:repo.ReadCommandOutput('diff', l:diffargs)
589 setlocal filetype=diff
590 setlocal nofoldenable
591 endfunction
592
593 " Annotate file
594 function! s:read_lawrencium_annotate(repo, path_parts, full_path) abort
595 call a:repo.ReadCommandOutput('annotate', '-c', '-n', '-u', '-d', '-q', a:full_path)
596 endfunction
597
598 " Generic read
599 let s:lawrencium_file_readers = {
600 \'rev': function('s:read_lawrencium_rev'),
601 \'log': function('s:read_lawrencium_log'),
602 \'diff': function('s:read_lawrencium_diff'),
603 \'status': function('s:read_lawrencium_status'),
604 \'annotate': function('s:read_lawrencium_annotate')
605 \}
606
607 function! s:ReadLawrenciumFile(path) abort
608 call s:trace("Reading Lawrencium file: " . a:path)
609 let l:path_parts = s:parse_lawrencium_path(a:path)
610 if l:path_parts['root'] == ''
611 call s:throw("Can't get repository root from: " . a:path)
612 endif
613 if !has_key(s:lawrencium_file_readers, l:path_parts['action'])
614 call s:throw("No registered reader for action: " . l:path_parts['action'])
615 endif
616
617 " Call the registered reader.
618 let l:repo = s:hg_repo(l:path_parts['root'])
619 let l:full_path = l:repo.root_dir . l:path_parts['path']
620 let LawrenciumFileReader = s:lawrencium_file_readers[l:path_parts['action']]
621 call LawrenciumFileReader(l:repo, l:path_parts, l:full_path)
622
623 " Setup the new buffer.
624 setlocal readonly
625 setlocal nomodified
626 setlocal bufhidden=delete
627 setlocal buftype=nofile
628 goto
629
630 " Remember the repo it belongs to and make
631 " the Lawrencium commands available.
632 let b:mercurial_dir = l:repo.root_dir
633 call s:DefineMainCommands()
634
635 return ''
636 endfunction
637
638 function! s:WriteLawrenciumFile(path) abort
639 call s:trace("Writing Lawrencium file: " . a:path)
640 endfunction
641
642 augroup lawrencium_files
643 autocmd!
644 autocmd BufReadCmd lawrencium://**//**//* exe s:ReadLawrenciumFile(expand('<amatch>'))
645 autocmd BufWriteCmd lawrencium://**//**//* exe s:WriteLawrenciumFile(expand('<amatch>'))
646 augroup END
530 647
531 " }}} 648 " }}}
532 649
533 " Buffer Commands Management {{{ 650 " Buffer Commands Management {{{
534 651
1408 1525
1409 call s:AddMainCommand("Hgannotate :call s:HgAnnotate()") 1526 call s:AddMainCommand("Hgannotate :call s:HgAnnotate()")
1410 1527
1411 " }}} 1528 " }}}
1412 1529
1413 " Lawrencium files {{{
1414
1415 let s:log_style_file = expand("<sfile>:h:h") . "/resources/hg_log.style"
1416
1417 function! s:ReadLawrenciumFile(path) abort
1418 call s:trace("Reading Lawrencium file '" . a:path)
1419 let l:comps = s:parse_lawrencium_path(a:path)
1420 if l:comps['root'] == ''
1421 call s:throw("Can't get repository root from: " . a:path)
1422 endif
1423
1424 let l:repo = s:hg_repo(l:comps['root'])
1425 let l:full_path = l:repo.root_dir . l:comps['path']
1426 if l:comps['action'] == 'rev'
1427 " Read revision (`hg cat`)
1428 if l:comps['value'] == ''
1429 call l:repo.ReadCommandOutput('cat', l:full_path)
1430 else
1431 call l:repo.ReadCommandOutput('cat', '-r', l:comps['value'], l:full_path)
1432 endif
1433 elseif l:comps['action'] == 'status'
1434 " Status (`hg status`)
1435 if l:comps['path'] == ''
1436 call l:repo.ReadCommandOutput('status')
1437 else
1438 call l:repo.ReadCommandOutput('status', l:full_path)
1439 endif
1440 setlocal filetype=hgstatus
1441 elseif l:comps['action'] == 'log'
1442 " Log (`hg log`)
1443 if l:comps['path'] == ''
1444 call l:repo.ReadCommandOutput('log', '--style', shellescape(s:log_style_file))
1445 else
1446 call l:repo.ReadCommandOutput('log', '--style', shellescape(s:log_style_file), l:full_path)
1447 endif
1448 setlocal filetype=hglog
1449 elseif l:comps['action'] == 'diff'
1450 " Diff revisions (`hg diff`)
1451 let l:diffargs = []
1452 let l:commaidx = stridx(l:comps['value'], ',')
1453 if l:commaidx > 0
1454 let l:rev1 = strpart(l:comps['value'], 0, l:commaidx)
1455 let l:rev2 = strpart(l:comps['value'], l:commaidx + 1)
1456 if l:rev1 == '-'
1457 let l:diffargs = [ '-r', l:rev2 ]
1458 elseif l:rev2 == '-'
1459 let l:diffargs = [ '-r', l:rev1 ]
1460 else
1461 let l:diffargs = [ '-r', l:rev1, '-r', l:rev2 ]
1462 endif
1463 elseif l:comps['value'] != ''
1464 let l:diffargs = [ '-c', l:comps['value'] ]
1465 else
1466 let l:diffargs = []
1467 endif
1468 if l:comps['path'] != '' && l:comps['path'] != '.'
1469 call add(l:diffargs, l:full_path)
1470 endif
1471 call l:repo.ReadCommandOutput('diff', l:diffargs)
1472 setlocal filetype=diff
1473 setlocal nofoldenable
1474 elseif l:comps['action'] == 'annotate'
1475 " Annotate file
1476 call l:repo.ReadCommandOutput('annotate', '-c', '-n', '-u', '-d', '-q', l:full_path)
1477 endif
1478
1479 " Setup the new buffer.
1480 setlocal readonly
1481 setlocal nomodified
1482 setlocal bufhidden=delete
1483 setlocal buftype=nofile
1484 goto
1485
1486 " Remember the repo it belongs to and make
1487 " the Lawrencium commands available.
1488 let b:mercurial_dir = l:repo.root_dir
1489 call s:DefineMainCommands()
1490
1491 return ''
1492 endfunction
1493
1494 augroup lawrencium_files
1495 autocmd!
1496 autocmd BufReadCmd lawrencium://**//**//* exe s:ReadLawrenciumFile(expand('<amatch>'))
1497 augroup END
1498
1499 " }}}
1500
1501 " Autoload Functions {{{ 1530 " Autoload Functions {{{
1502 1531
1503 " Prints a summary of the current repo (if any) that's appropriate for 1532 " Prints a summary of the current repo (if any) that's appropriate for
1504 " displaying on the status line. 1533 " displaying on the status line.
1505 function! lawrencium#statusline(...) 1534 function! lawrencium#statusline(...)