Mercurial > vim-lawrencium
comparison plugin/lawrencium.vim @ 106:77f77b2c2738
Callback system for `Hgcommit` so we can refresh the `Hgstatus` window.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Fri, 15 Aug 2014 17:08:40 -0700 |
parents | fc5ffa4614b4 |
children | 6846e12f8ec8 |
comparison
equal
deleted
inserted
replaced
105:fc5ffa4614b4 | 106:77f77b2c2738 |
---|---|
795 setlocal bufhidden=delete | 795 setlocal bufhidden=delete |
796 setlocal buftype=nofile | 796 setlocal buftype=nofile |
797 endif | 797 endif |
798 goto | 798 goto |
799 | 799 |
800 " Remember the real Lawrencium path, because Vim can fuck up the slashes | |
801 " on Windows. | |
802 let b:lawrencium_path = a:path | |
803 | |
800 " Remember the repo it belongs to and make | 804 " Remember the repo it belongs to and make |
801 " the Lawrencium commands available. | 805 " the Lawrencium commands available. |
802 let b:mercurial_dir = l:repo.root_dir | 806 let b:mercurial_dir = l:repo.root_dir |
803 call s:DefineMainCommands() | 807 call s:DefineMainCommands() |
804 | 808 |
1028 vnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr> | 1032 vnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr> |
1029 vnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr> | 1033 vnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr> |
1030 endif | 1034 endif |
1031 endfunction | 1035 endfunction |
1032 | 1036 |
1033 function! s:HgStatus_Refresh() abort | 1037 function! s:HgStatus_Refresh(...) abort |
1038 if a:0 > 0 | |
1039 let l:win_nr = bufwinnr(a:1) | |
1040 call s:trace("Switching back to status window ".l:win_nr) | |
1041 if l:win_nr < 0 | |
1042 call s:throw("Can't find the status window anymore!") | |
1043 endif | |
1044 execute l:win_nr . 'wincmd w' | |
1045 " Delete everything in the buffer, and re-read the status into it. | |
1046 " TODO: In theory I would only have to do `edit` like below when we're | |
1047 " already in the window, but for some reason Vim just goes bonkers and | |
1048 " weird shit happens. I have no idea why, hence the work-around here | |
1049 " to bypass the whole `BufReadCmd` auto-command altogether, and just | |
1050 " edit the buffer in place. | |
1051 normal! ggVGd | |
1052 call s:ReadLawrenciumFile(b:lawrencium_path) | |
1053 return | |
1054 endif | |
1055 | |
1034 " Just re-edit the buffer, it will reload the contents by calling | 1056 " Just re-edit the buffer, it will reload the contents by calling |
1035 " the matching Mercurial command. | 1057 " the matching Mercurial command. |
1036 edit | 1058 edit |
1037 endfunction | 1059 endfunction |
1038 | 1060 |
1090 call s:error("No files to commit in selection or file.") | 1112 call s:error("No files to commit in selection or file.") |
1091 return | 1113 return |
1092 endif | 1114 endif |
1093 | 1115 |
1094 " Run `Hgcommit` on those paths. | 1116 " Run `Hgcommit` on those paths. |
1095 call s:HgCommit(a:bang, a:vertical, l:filenames) | 1117 let l:buf_nr = bufnr('%') |
1118 let l:callback = 'call s:HgStatus_Refresh('.l:buf_nr.')' | |
1119 call s:HgCommit(a:bang, a:vertical, l:callback, l:filenames) | |
1096 endfunction | 1120 endfunction |
1097 | 1121 |
1098 function! s:HgStatus_Diff(split) abort | 1122 function! s:HgStatus_Diff(split) abort |
1099 " Open the file and run `Hgdiff` on it. | 1123 " Open the file and run `Hgdiff` on it. |
1100 " We also need to translate the split mode for it... if we already | 1124 " We also need to translate the split mode for it... if we already |
1497 | 1521 |
1498 " }}} | 1522 " }}} |
1499 | 1523 |
1500 " Hgcommit {{{ | 1524 " Hgcommit {{{ |
1501 | 1525 |
1502 function! s:HgCommit(bang, vertical, ...) abort | 1526 function! s:HgCommit(bang, vertical, callback, ...) abort |
1503 " Get the repo we'll be committing into. | 1527 " Get the repo we'll be committing into. |
1504 let l:repo = s:hg_repo() | 1528 let l:repo = s:hg_repo() |
1505 | 1529 |
1506 " Get the list of files to commit. | 1530 " Get the list of files to commit. |
1507 " It can either be several files passed as extra parameters, or an | 1531 " It can either be several files passed as extra parameters, or an |
1524 | 1548 |
1525 " Setup the auto-command that will actually commit on write/exit, | 1549 " Setup the auto-command that will actually commit on write/exit, |
1526 " and make the buffer delete itself on exit. | 1550 " and make the buffer delete itself on exit. |
1527 let b:mercurial_dir = l:repo.root_dir | 1551 let b:mercurial_dir = l:repo.root_dir |
1528 let b:lawrencium_commit_files = l:filenames | 1552 let b:lawrencium_commit_files = l:filenames |
1553 if type(a:callback) == type([]) | |
1554 let b:lawrencium_commit_pre_callback = a:callback[0] | |
1555 let b:lawrencium_commit_post_callback = a:callback[1] | |
1556 let b:lawrencium_commit_abort_callback = a:callback[2] | |
1557 else | |
1558 let b:lawrencium_commit_pre_callback = 0 | |
1559 let b:lawrencium_commit_post_callback = a:callback | |
1560 let b:lawrencium_commit_abort_callback = 0 | |
1561 endif | |
1529 setlocal bufhidden=delete | 1562 setlocal bufhidden=delete |
1530 setlocal filetype=hgcommit | 1563 setlocal filetype=hgcommit |
1531 if a:bang | 1564 if a:bang |
1532 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 0) | 1565 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 0) |
1533 else | 1566 else |
1576 | 1609 |
1577 function! s:HgCommit_Execute(log_file, show_output) abort | 1610 function! s:HgCommit_Execute(log_file, show_output) abort |
1578 " Check if the user actually saved a commit message. | 1611 " Check if the user actually saved a commit message. |
1579 if !filereadable(a:log_file) | 1612 if !filereadable(a:log_file) |
1580 call s:error("abort: Commit message not saved") | 1613 call s:error("abort: Commit message not saved") |
1614 if exists('b:lawrencium_commit_abort_callback') && | |
1615 \type(b:lawrencium_commit_abort_callback) == type("") && | |
1616 \b:lawrencium_commit_abort_callback != '' | |
1617 call s:trace("Executing abort callback: ".b:lawrencium_commit_abort_callback) | |
1618 execute b:lawrencium_commit_abort_callback | |
1619 endif | |
1581 return | 1620 return |
1621 endif | |
1622 | |
1623 " Execute a pre-callback if there is one. | |
1624 if exists('b:lawrencium_commit_pre_callback') && | |
1625 \type(b:lawrencium_commit_pre_callback) == type("") && | |
1626 \b:lawrencium_commit_pre_callback != '' | |
1627 call s:trace("Executing pre callback: ".b:lawrencium_commit_pre_callback) | |
1628 execute b:lawrencium_commit_pre_callback | |
1582 endif | 1629 endif |
1583 | 1630 |
1584 call s:trace("Committing with log file: " . a:log_file) | 1631 call s:trace("Committing with log file: " . a:log_file) |
1585 | 1632 |
1586 " Clean all the 'HG: ' lines. | 1633 " Clean all the 'HG: ' lines. |
1599 call s:trace("Output from hg commit:", 1) | 1646 call s:trace("Output from hg commit:", 1) |
1600 for l:output_line in split(l:output, '\n') | 1647 for l:output_line in split(l:output, '\n') |
1601 echom l:output_line | 1648 echom l:output_line |
1602 endfor | 1649 endfor |
1603 endif | 1650 endif |
1604 endfunction | 1651 |
1605 | 1652 " Execute a post-callback if there is one. |
1606 call s:AddMainCommand("-bang -nargs=* -complete=customlist,s:ListRepoFiles Hgcommit :call s:HgCommit(<bang>0, 0, <f-args>)") | 1653 if exists('b:lawrencium_commit_post_callback') && |
1607 call s:AddMainCommand("-bang -nargs=* -complete=customlist,s:ListRepoFiles Hgvcommit :call s:HgCommit(<bang>0, 1, <f-args>)") | 1654 \type(b:lawrencium_commit_post_callback) == type("") && |
1655 \b:lawrencium_commit_post_callback != '' | |
1656 call s:trace("Executing post callback: ".b:lawrencium_commit_post_callback) | |
1657 execute b:lawrencium_commit_post_callback | |
1658 endif | |
1659 endfunction | |
1660 | |
1661 call s:AddMainCommand("-bang -nargs=* -complete=customlist,s:ListRepoFiles Hgcommit :call s:HgCommit(<bang>0, 0, 0, <f-args>)") | |
1662 call s:AddMainCommand("-bang -nargs=* -complete=customlist,s:ListRepoFiles Hgvcommit :call s:HgCommit(<bang>0, 1, 0, <f-args>)") | |
1608 | 1663 |
1609 " }}} | 1664 " }}} |
1610 | 1665 |
1611 " Hgrevert {{{ | 1666 " Hgrevert {{{ |
1612 | 1667 |