Mercurial > vim-lawrencium
comparison autoload/lawrencium/record.vim @ 139:065625e1bb31
Split plugin file into multiple extensions.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 13 Jun 2016 09:32:34 -0700 |
parents | |
children | 652a6f5df0f3 |
comparison
equal
deleted
inserted
replaced
138:a2d823c82e5f | 139:065625e1bb31 |
---|---|
1 | |
2 function! lawrencium#record#init() abort | |
3 call lawrencium#add_command("Hgrecord call lawrencium#record#HgRecord(0)") | |
4 call lawrencium#add_command("Hgvrecord call lawrencium#record#HgRecord(1)") | |
5 endfunction | |
6 | |
7 function! lawrencium#record#HgRecord(split) abort | |
8 let l:repo = lawrencium#hg_repo() | |
9 let l:orig_buf = lawrencium#buffer_obj() | |
10 let l:tmp_path = l:orig_buf.GetName(':p') . '~record' | |
11 let l:diff_id = localtime() | |
12 | |
13 " Start diffing on the current file, enable some commands. | |
14 call l:orig_buf.DefineCommand('Hgrecordabort', ':call s:HgRecord_Abort()') | |
15 call l:orig_buf.DefineCommand('Hgrecordcommit', ':call s:HgRecord_Execute()') | |
16 call lawrencium#diff#HgDiffThis(l:diff_id) | |
17 setlocal foldmethod=diff | |
18 | |
19 " Split the window and open the parent revision in the right or bottom | |
20 " window. Keep the current buffer in the left or top window... we're going | |
21 " to 'move' those changes into the parent revision. | |
22 let l:cmd = 'keepalt rightbelow split ' | |
23 if a:split == 1 | |
24 let l:cmd = 'keepalt rightbelow vsplit ' | |
25 endif | |
26 let l:rev_path = l:repo.GetLawrenciumPath(expand('%:p'), 'rev', '') | |
27 execute l:cmd . fnameescape(l:rev_path) | |
28 | |
29 " This new buffer with the parent revision is set as a Lawrencium buffer. | |
30 " Let's save it to an actual file and reopen it like that (somehow we | |
31 " could probably do it with `:saveas` instead but we'd need to reset a | |
32 " bunch of other buffer settings, and Vim weirdly creates another backup | |
33 " buffer when you do that). | |
34 execute 'keepalt write! ' . fnameescape(l:tmp_path) | |
35 execute 'keepalt edit! ' . fnameescape(l:tmp_path) | |
36 setlocal bufhidden=delete | |
37 let b:mercurial_dir = l:repo.root_dir | |
38 let b:lawrencium_record_for = l:orig_buf.GetName(':p') | |
39 let b:lawrencium_record_other_nr = l:orig_buf.nr | |
40 let b:lawrencium_record_commit_split = !a:split | |
41 call setbufvar(l:orig_buf.nr, 'lawrencium_record_for', '%') | |
42 call setbufvar(l:orig_buf.nr, 'lawrencium_record_other_nr', bufnr('%')) | |
43 | |
44 " Hookup the commit and abort commands. | |
45 let l:rec_buf = lawrencium#buffer_obj() | |
46 call l:rec_buf.OnDelete('call s:HgRecord_Execute()') | |
47 call l:rec_buf.DefineCommand('Hgrecordcommit', ':quit') | |
48 call l:rec_buf.DefineCommand('Hgrecordabort', ':call s:HgRecord_Abort()') | |
49 call lawrencium#define_commands() | |
50 | |
51 " Make it the other part of the diff. | |
52 call lawrencium#diff#HgDiffThis(l:diff_id) | |
53 setlocal foldmethod=diff | |
54 call l:rec_buf.SetVar('&filetype', l:orig_buf.GetVar('&filetype')) | |
55 call l:rec_buf.SetVar('&fileformat', l:orig_buf.GetVar('&fileformat')) | |
56 | |
57 if g:lawrencium_record_start_in_working_buffer | |
58 wincmd p | |
59 endif | |
60 endfunction | |
61 | |
62 function! s:HgRecord_Execute() abort | |
63 if exists('b:lawrencium_record_abort') | |
64 " Abort flag is set, let's just cleanup. | |
65 let l:buf_nr = b:lawrencium_record_for == '%' ? bufnr('%') : | |
66 \b:lawrencium_record_other_nr | |
67 call s:HgRecord_CleanUp(l:buf_nr) | |
68 call lawrencium#error("abort: User requested aborting the record operation.") | |
69 return | |
70 endif | |
71 | |
72 if !exists('b:lawrencium_record_for') | |
73 call lawrencium#throw("This doesn't seem like a record buffer, something's wrong!") | |
74 endif | |
75 if b:lawrencium_record_for == '%' | |
76 " Switch to the 'recording' buffer's window. | |
77 let l:buf_obj = lawrencium#buffer_obj(b:lawrencium_record_other_nr) | |
78 call l:buf_obj.MoveToFirstWindow() | |
79 endif | |
80 | |
81 " Setup the commit operation. | |
82 let l:split = b:lawrencium_record_commit_split | |
83 let l:working_bufnr = b:lawrencium_record_other_nr | |
84 let l:working_path = fnameescape(b:lawrencium_record_for) | |
85 let l:record_path = fnameescape(expand('%:p')) | |
86 let l:callbacks = [ | |
87 \'call s:HgRecord_PostExecutePre('.l:working_bufnr.', "'. | |
88 \escape(l:working_path, '\').'", "'. | |
89 \escape(l:record_path, '\').'")', | |
90 \'call s:HgRecord_PostExecutePost('.l:working_bufnr.', "'. | |
91 \escape(l:working_path, '\').'")', | |
92 \'call s:HgRecord_PostExecuteAbort('.l:working_bufnr.', "'. | |
93 \escape(l:record_path, '\').'")' | |
94 \] | |
95 call lawrencium#trace("Starting commit flow with callbacks: ".string(l:callbacks)) | |
96 call lawrencium#commit#HgCommit(0, l:split, l:callbacks, b:lawrencium_record_for) | |
97 endfunction | |
98 | |
99 function! s:HgRecord_PostExecutePre(working_bufnr, working_path, record_path) abort | |
100 " Just before committing, we switch the original file with the record | |
101 " file... we'll restore things in the post-callback below. | |
102 " We also switch on 'autoread' temporarily on the working buffer so that | |
103 " we don't have an annoying popup in gVim. | |
104 if has('dialog_gui') | |
105 call setbufvar(a:working_bufnr, '&autoread', 1) | |
106 endif | |
107 call lawrencium#trace("Backuping original file: ".a:working_path) | |
108 silent call rename(a:working_path, a:working_path.'~working') | |
109 call lawrencium#trace("Committing recorded changes using: ".a:record_path) | |
110 silent call rename(a:record_path, a:working_path) | |
111 sleep 200m | |
112 endfunction | |
113 | |
114 function! s:HgRecord_PostExecutePost(working_bufnr, working_path) abort | |
115 " Recover the back-up file from underneath the buffer. | |
116 call lawrencium#trace("Recovering original file: ".a:working_path) | |
117 silent call rename(a:working_path.'~working', a:working_path) | |
118 | |
119 " Clean up! | |
120 call s:HgRecord_CleanUp(a:working_bufnr) | |
121 | |
122 " Restore default 'autoread'. | |
123 if has('dialog_gui') | |
124 set autoread< | |
125 endif | |
126 endfunction | |
127 | |
128 function! s:HgRecord_PostExecuteAbort(working_bufnr, record_path) abort | |
129 call s:HgRecord_CleanUp(a:working_bufnr) | |
130 call lawrencium#trace("Delete discarded record file: ".a:record_path) | |
131 silent call delete(a:record_path) | |
132 endfunction | |
133 | |
134 function! s:HgRecord_Abort() abort | |
135 if b:lawrencium_record_for == '%' | |
136 " We're in the working directory buffer. Switch to the 'recording' | |
137 " buffer and quit. | |
138 let l:buf_obj = lawrencium#buffer_obj(b:lawrencium_record_other_nr) | |
139 call l:buf_obj.MoveToFirstWindow() | |
140 endif | |
141 " We're now in the 'recording' buffer... set the abort flag and quit, | |
142 " which will run the execution (it will early out and clean things up). | |
143 let b:lawrencium_record_abort = 1 | |
144 quit! | |
145 endfunction | |
146 | |
147 function! s:HgRecord_CleanUp(buf_nr) abort | |
148 " Get in the original buffer and clean the local commands/variables. | |
149 let l:buf_obj = lawrencium#buffer_obj(a:buf_nr) | |
150 call l:buf_obj.MoveToFirstWindow() | |
151 if !exists('b:lawrencium_record_for') || b:lawrencium_record_for != '%' | |
152 call lawrencium#throw("Cleaning up something else than the original buffer ". | |
153 \"for a record operation. That's suspiciously incorrect! ". | |
154 \"Aborting.") | |
155 endif | |
156 call l:buf_obj.DeleteCommand('Hgrecordabort') | |
157 call l:buf_obj.DeleteCommand('Hgrecordcommit') | |
158 unlet b:lawrencium_record_for | |
159 unlet b:lawrencium_record_other_nr | |
160 endfunction | |
161 |