Mercurial > vim-lawrencium
comparison autoload/lawrencium/status.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#status#init() abort | |
3 call lawrencium#add_command("Hgstatus :call lawrencium#status#HgStatus()") | |
4 | |
5 call lawrencium#add_reader('status', "lawrencium#status#read", 1) | |
6 endfunction | |
7 | |
8 function! lawrencium#status#read(repo, path_parts, full_path) abort | |
9 if a:path_parts['path'] == '' | |
10 call a:repo.ReadCommandOutput('status') | |
11 else | |
12 call a:repo.ReadCommandOutput('status', a:full_path) | |
13 endif | |
14 setlocal nomodified | |
15 setlocal filetype=hgstatus | |
16 setlocal bufhidden=delete | |
17 setlocal buftype=nofile | |
18 endfunction | |
19 | |
20 function! lawrencium#status#HgStatus() abort | |
21 " Get the repo and the Lawrencium path for `hg status`. | |
22 let l:repo = lawrencium#hg_repo() | |
23 let l:status_path = l:repo.GetLawrenciumPath('', 'status', '') | |
24 | |
25 " Open the Lawrencium buffer in a new split window of the right size. | |
26 if g:lawrencium_status_win_split_above | |
27 execute "keepalt leftabove split " . fnameescape(l:status_path) | |
28 else | |
29 execute "keepalt rightbelow split " . fnameescape(l:status_path) | |
30 endif | |
31 | |
32 if (line('$') == 1 && getline(1) == '') | |
33 " Buffer is empty, which means there are not changes... | |
34 " Quit and display a message. | |
35 " TODO: figure out why the first `echom` doesn't show when alone. | |
36 bdelete | |
37 echom "Nothing was modified." | |
38 echom "" | |
39 return | |
40 endif | |
41 | |
42 execute "setlocal winfixheight" | |
43 if !g:lawrencium_status_win_split_even | |
44 execute "setlocal winheight=" . (line('$') + 1) | |
45 execute "resize " . (line('$') + 1) | |
46 endif | |
47 | |
48 " Add some nice commands. | |
49 command! -buffer Hgstatusedit :call s:HgStatus_FileEdit(0) | |
50 command! -buffer Hgstatusdiff :call s:HgStatus_Diff(0) | |
51 command! -buffer Hgstatusvdiff :call s:HgStatus_Diff(1) | |
52 command! -buffer Hgstatustabdiff :call s:HgStatus_Diff(2) | |
53 command! -buffer Hgstatusdiffsum :call s:HgStatus_DiffSummary(1) | |
54 command! -buffer Hgstatusvdiffsum :call s:HgStatus_DiffSummary(2) | |
55 command! -buffer Hgstatustabdiffsum :call s:HgStatus_DiffSummary(3) | |
56 command! -buffer Hgstatusrefresh :call s:HgStatus_Refresh() | |
57 command! -buffer -range -bang Hgstatusrevert :call s:HgStatus_Revert(<line1>, <line2>, <bang>0) | |
58 command! -buffer -range Hgstatusaddremove :call s:HgStatus_AddRemove(<line1>, <line2>) | |
59 command! -buffer -range=% -bang Hgstatuscommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 0) | |
60 command! -buffer -range=% -bang Hgstatusvcommit :call s:HgStatus_Commit(<line1>, <line2>, <bang>0, 1) | |
61 command! -buffer -range=% -nargs=+ Hgstatusqnew :call s:HgStatus_QNew(<line1>, <line2>, <f-args>) | |
62 command! -buffer -range=% Hgstatusqrefresh :call s:HgStatus_QRefresh(<line1>, <line2>) | |
63 | |
64 " Add some handy mappings. | |
65 if g:lawrencium_define_mappings | |
66 nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr> | |
67 nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr> | |
68 nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr> | |
69 nnoremap <buffer> <silent> <C-D> :Hgstatustabdiff<cr> | |
70 nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr> | |
71 nnoremap <buffer> <silent> <C-U> :Hgstatusdiffsum<cr> | |
72 nnoremap <buffer> <silent> <C-H> :Hgstatusvdiffsum<cr> | |
73 nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr> | |
74 nnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr> | |
75 nnoremap <buffer> <silent> <C-R> :Hgstatusrefresh<cr> | |
76 nnoremap <buffer> <silent> q :bdelete!<cr> | |
77 | |
78 vnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr> | |
79 vnoremap <buffer> <silent> <C-S> :Hgstatuscommit<cr> | |
80 endif | |
81 endfunction | |
82 | |
83 function! s:HgStatus_Refresh(...) abort | |
84 if a:0 > 0 | |
85 let l:win_nr = bufwinnr(a:1) | |
86 call lawrencium#trace("Switching back to status window ".l:win_nr) | |
87 if l:win_nr < 0 | |
88 call lawrencium#throw("Can't find the status window anymore!") | |
89 endif | |
90 execute l:win_nr . 'wincmd w' | |
91 " Delete everything in the buffer, and re-read the status into it. | |
92 " TODO: In theory I would only have to do `edit` like below when we're | |
93 " already in the window, but for some reason Vim just goes bonkers and | |
94 " weird shit happens. I have no idea why, hence the work-around here | |
95 " to bypass the whole `BufReadCmd` auto-command altogether, and just | |
96 " edit the buffer in place. | |
97 normal! ggVGd | |
98 call lawrencium#read_lawrencium_file(b:lawrencium_path) | |
99 return | |
100 endif | |
101 | |
102 " Just re-edit the buffer, it will reload the contents by calling | |
103 " the matching Mercurial command. | |
104 edit | |
105 endfunction | |
106 | |
107 function! s:HgStatus_FileEdit(newtab) abort | |
108 " Get the path of the file the cursor is on. | |
109 let l:filename = s:HgStatus_GetSelectedFile() | |
110 | |
111 let l:cleanupbufnr = -1 | |
112 if a:newtab == 0 | |
113 " If the file is already open in a window, jump to that window. | |
114 " Otherwise, jump to the previous window and open it there. | |
115 for nr in range(1, winnr('$')) | |
116 let l:br = winbufnr(nr) | |
117 let l:bpath = fnamemodify(bufname(l:br), ':p') | |
118 if l:bpath ==# l:filename | |
119 execute nr . 'wincmd w' | |
120 return | |
121 endif | |
122 endfor | |
123 wincmd p | |
124 else | |
125 " Just open a new tab so we can edit the file there. | |
126 " We don't use `tabedit` because it messes up the current window | |
127 " if it happens to be the same file. | |
128 " We'll just have to clean up the default empty buffer created. | |
129 tabnew | |
130 let l:cleanupbufnr = bufnr('%') | |
131 endif | |
132 execute 'edit ' . fnameescape(l:filename) | |
133 if l:cleanupbufnr >= 0 | |
134 execute 'bdelete ' . l:cleanupbufnr | |
135 endif | |
136 endfunction | |
137 | |
138 function! s:HgStatus_AddRemove(linestart, lineend) abort | |
139 " Get the selected filenames. | |
140 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['!', '?']) | |
141 if len(l:filenames) == 0 | |
142 call lawrencium#error("No files to add or remove in selection or current line.") | |
143 return | |
144 endif | |
145 | |
146 " Run `addremove` on those paths. | |
147 let l:repo = lawrencium#hg_repo() | |
148 call l:repo.RunCommand('addremove', l:filenames) | |
149 | |
150 " Refresh the status window. | |
151 call s:HgStatus_Refresh() | |
152 endfunction | |
153 | |
154 function! s:HgStatus_Revert(linestart, lineend, bang) abort | |
155 " Get the selected filenames. | |
156 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) | |
157 if len(l:filenames) == 0 | |
158 call lawrencium#error("No files to revert in selection or current line.") | |
159 return | |
160 endif | |
161 | |
162 " Run `revert` on those paths. | |
163 " If the bang modifier is specified, revert with no backup. | |
164 let l:repo = lawrencium#hg_repo() | |
165 if a:bang | |
166 call insert(l:filenames, '-C', 0) | |
167 endif | |
168 call l:repo.RunCommand('revert', l:filenames) | |
169 | |
170 " Refresh the status window. | |
171 call s:HgStatus_Refresh() | |
172 endfunction | |
173 | |
174 function! s:HgStatus_Commit(linestart, lineend, bang, vertical) abort | |
175 " Get the selected filenames. | |
176 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) | |
177 if len(l:filenames) == 0 | |
178 call lawrencium#error("No files to commit in selection or file.") | |
179 return | |
180 endif | |
181 | |
182 " Run `Hgcommit` on those paths. | |
183 let l:buf_nr = bufnr('%') | |
184 let l:callback = 'call s:HgStatus_Refresh('.l:buf_nr.')' | |
185 call lawrencium#commit#HgCommit(a:bang, a:vertical, l:callback, l:filenames) | |
186 endfunction | |
187 | |
188 function! s:HgStatus_Diff(split) abort | |
189 " Open the file and run `Hgdiff` on it. | |
190 " We also need to translate the split mode for it... if we already | |
191 " opened the file in a new tab, `HgDiff` only needs to do a vertical | |
192 " split (i.e. split=1). | |
193 let l:newtab = 0 | |
194 let l:hgdiffsplit = a:split | |
195 if a:split == 2 | |
196 let l:newtab = 1 | |
197 let l:hgdiffsplit = 1 | |
198 endif | |
199 call s:HgStatus_FileEdit(l:newtab) | |
200 call lawrencium#diff#HgDiff('%:p', l:hgdiffsplit) | |
201 endfunction | |
202 | |
203 function! s:HgStatus_DiffSummary(split) abort | |
204 " Get the path of the file the cursor is on. | |
205 let l:path = s:HgStatus_GetSelectedFile() | |
206 " Reuse the same diff summary window | |
207 let l:reuse_id = 'lawrencium_diffsum_for_' . bufnr('%') | |
208 let l:split_prev_win = (a:split < 3) | |
209 let l:args = {'reuse_id': l:reuse_id, 'use_prev_win': l:split_prev_win, | |
210 \'avoid_win': winnr(), 'split_mode': a:split} | |
211 call lawrencium#diff#HgDiffSummary(l:path, l:args) | |
212 endfunction | |
213 | |
214 function! s:HgStatus_QNew(linestart, lineend, patchname, ...) abort | |
215 " Get the selected filenames. | |
216 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) | |
217 if len(l:filenames) == 0 | |
218 call lawrencium#error("No files in selection or file to create patch.") | |
219 return | |
220 endif | |
221 | |
222 " Run `Hg qnew` on those paths. | |
223 let l:repo = lawrencium#hg_repo() | |
224 call insert(l:filenames, a:patchname, 0) | |
225 if a:0 > 0 | |
226 call insert(l:filenames, '-m', 0) | |
227 let l:message = '"' . join(a:000, ' ') . '"' | |
228 call insert(l:filenames, l:message, 1) | |
229 endif | |
230 call l:repo.RunCommand('qnew', l:filenames) | |
231 | |
232 " Refresh the status window. | |
233 call s:HgStatus_Refresh() | |
234 endfunction | |
235 | |
236 function! s:HgStatus_QRefresh(linestart, lineend) abort | |
237 " Get the selected filenames. | |
238 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['M', 'A', 'R']) | |
239 if len(l:filenames) == 0 | |
240 call lawrencium#error("No files in selection or file to refresh the patch.") | |
241 return | |
242 endif | |
243 | |
244 " Run `Hg qrefresh` on those paths. | |
245 let l:repo = lawrencium#hg_repo() | |
246 call insert(l:filenames, '-s', 0) | |
247 call l:repo.RunCommand('qrefresh', l:filenames) | |
248 | |
249 " Refresh the status window. | |
250 call s:HgStatus_Refresh() | |
251 endfunction | |
252 | |
253 | |
254 function! s:HgStatus_GetSelectedFile() abort | |
255 let l:filenames = s:HgStatus_GetSelectedFiles() | |
256 return l:filenames[0] | |
257 endfunction | |
258 | |
259 function! s:HgStatus_GetSelectedFiles(...) abort | |
260 if a:0 >= 2 | |
261 let l:lines = getline(a:1, a:2) | |
262 else | |
263 let l:lines = [] | |
264 call add(l:lines, getline('.')) | |
265 endif | |
266 let l:filenames = [] | |
267 let l:repo = lawrencium#hg_repo() | |
268 for line in l:lines | |
269 if a:0 >= 3 | |
270 let l:status = s:HgStatus_GetFileStatus(line) | |
271 if index(a:3, l:status) < 0 | |
272 continue | |
273 endif | |
274 endif | |
275 " Yay, awesome, Vim's regex syntax is fucked up like shit, especially for | |
276 " look-aheads and look-behinds. See for yourself: | |
277 let l:filename = matchstr(l:line, '\v(^[MARC\!\?I ]\s)@<=.*') | |
278 let l:filename = l:repo.GetFullPath(l:filename) | |
279 call add(l:filenames, l:filename) | |
280 endfor | |
281 return l:filenames | |
282 endfunction | |
283 | |
284 function! s:HgStatus_GetFileStatus(...) abort | |
285 let l:line = a:0 ? a:1 : getline('.') | |
286 return matchstr(l:line, '\v^[MARC\!\?I ]') | |
287 endfunction | |
288 |