comparison autoload/lawrencium/log.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#log#init() abort
3 call lawrencium#add_command("Hglogthis :call lawrencium#log#HgLog(0, '%:p')")
4 call lawrencium#add_command("Hgvlogthis :call lawrencium#log#HgLog(1, '%:p')")
5 call lawrencium#add_command("-nargs=* -complete=customlist,lawrencium#list_repo_files Hglog :call lawrencium#log#HgLog(0, <f-args>)")
6 call lawrencium#add_command("-nargs=* -complete=customlist,lawrencium#list_repo_files Hgvlog :call lawrencium#log#HgLog(1, <f-args>)")
7
8 call lawrencium#add_reader("log", "lawrencium#log#read")
9 call lawrencium#add_reader("logpatch", "lawrencium#log#read_patch")
10 endfunction
11
12 let s:log_style_file = expand("<sfile>:h:h") . "/resources/hg_log.style"
13
14 function! lawrencium#log#read(repo, path_parts, full_path) abort
15 let l:log_opts = join(split(a:path_parts['value'], ','))
16 let l:log_cmd = "log " . l:log_opts
17
18 if a:path_parts['path'] == ''
19 call a:repo.ReadCommandOutput(l:log_cmd, '--style', s:log_style_file)
20 else
21 call a:repo.ReadCommandOutput(l:log_cmd, '--style', s:log_style_file, a:full_path)
22 endif
23 setlocal filetype=hglog
24 endfunction
25
26 function! lawrencium#log#read_patch(repo, path_parts, full_path) abort
27 let l:log_cmd = 'log --patch --verbose --rev ' . a:path_parts['value']
28
29 if a:path_parts['path'] == ''
30 call a:repo.ReadCommandOutput(l:log_cmd)
31 else
32 call a:repo.ReadCommandOutput(l:log_cmd, a:full_path)
33 endif
34 setlocal filetype=diff
35 endfunction
36
37 function! lawrencium#log#HgLog(vertical, ...) abort
38 " Get the file or directory to get the log from.
39 " (empty string is for the whole repository)
40 let l:repo = lawrencium#hg_repo()
41 if a:0 > 0 && matchstr(a:1, '\v-*') == ""
42 let l:path = l:repo.GetRelativePath(expand(a:1))
43 else
44 let l:path = ''
45 endif
46
47 " Get the Lawrencium path for this `hg log`,
48 " open it in a preview window and jump to it.
49 if a:0 > 0 && l:path != ""
50 let l:log_opts = join(a:000[1:-1], ',')
51 else
52 let l:log_opts = join(a:000, ',')
53 endif
54
55 let l:log_path = l:repo.GetLawrenciumPath(l:path, 'log', l:log_opts)
56 if a:vertical
57 execute 'vertical pedit ' . fnameescape(l:log_path)
58 else
59 execute 'pedit ' . fnameescape(l:log_path)
60 endif
61 wincmd P
62
63 " Add some other nice commands and mappings.
64 let l:is_file = (l:path != '' && filereadable(l:repo.GetFullPath(l:path)))
65 command! -buffer -nargs=* Hglogdiffsum :call s:HgLog_DiffSummary(1, <f-args>)
66 command! -buffer -nargs=* Hglogvdiffsum :call s:HgLog_DiffSummary(2, <f-args>)
67 command! -buffer -nargs=* Hglogtabdiffsum :call s:HgLog_DiffSummary(3, <f-args>)
68 command! -buffer -nargs=+ -complete=file Hglogexport :call s:HgLog_ExportPatch(<f-args>)
69 if l:is_file
70 command! -buffer Hglogrevedit :call s:HgLog_FileRevEdit()
71 command! -buffer -nargs=* Hglogdiff :call s:HgLog_Diff(0, <f-args>)
72 command! -buffer -nargs=* Hglogvdiff :call s:HgLog_Diff(1, <f-args>)
73 command! -buffer -nargs=* Hglogtabdiff :call s:HgLog_Diff(2, <f-args>)
74 endif
75
76 if g:lawrencium_define_mappings
77 nnoremap <buffer> <silent> <C-U> :Hglogdiffsum<cr>
78 nnoremap <buffer> <silent> <C-H> :Hglogvdiffsum<cr>
79 nnoremap <buffer> <silent> <cr> :Hglogvdiffsum<cr>
80 nnoremap <buffer> <silent> q :bdelete!<cr>
81 if l:is_file
82 nnoremap <buffer> <silent> <C-E> :Hglogrevedit<cr>
83 nnoremap <buffer> <silent> <C-D> :Hglogtabdiff<cr>
84 nnoremap <buffer> <silent> <C-V> :Hglogvdiff<cr>
85 endif
86 endif
87
88 " Clean up when the log buffer is deleted.
89 let l:bufobj = lawrencium#buffer_obj()
90 call l:bufobj.OnDelete('call s:HgLog_Delete(' . l:bufobj.nr . ')')
91 endfunction
92
93 function! s:HgLog_Delete(bufnr)
94 if g:lawrencium_auto_close_buffers
95 call lawrencium#delete_dependency_buffers('lawrencium_diff_for', a:bufnr)
96 call lawrencium#delete_dependency_buffers('lawrencium_rev_for', a:bufnr)
97 endif
98 endfunction
99
100 function! s:HgLog_FileRevEdit()
101 let l:repo = lawrencium#hg_repo()
102 let l:bufobj = lawrencium#buffer_obj()
103 let l:rev = s:HgLog_GetSelectedRev()
104 let l:log_path = lawrencium#parse_lawrencium_path(l:bufobj.GetName())
105 let l:path = l:repo.GetLawrenciumPath(l:log_path['path'], 'rev', l:rev)
106
107 " Go to the window we were in before going in the log window,
108 " and open the revision there.
109 wincmd p
110 call lawrencium#edit_deletable_buffer('lawrencium_rev_for', l:bufobj.nr, l:path)
111 endfunction
112
113 function! s:HgLog_Diff(split, ...) abort
114 let l:revs = []
115 if a:0 >= 2
116 let l:revs = [a:1, a:2]
117 elseif a:0 == 1
118 let l:revs = ['p1('.a:1.')', a:1]
119 else
120 let l:sel = s:HgLog_GetSelectedRev()
121 let l:revs = ['p1('.l:sel.')', l:sel]
122 endif
123
124 let l:repo = lawrencium#hg_repo()
125 let l:bufobj = lawrencium#buffer_obj()
126 let l:log_path = lawrencium#parse_lawrencium_path(l:bufobj.GetName())
127 let l:path = l:repo.GetFullPath(l:log_path['path'])
128
129 " Go to the window we were in before going to the log window,
130 " and open the split diff there.
131 if a:split < 2
132 wincmd p
133 endif
134 call lawrencium#diff#HgDiff(l:path, a:split, l:revs)
135 endfunction
136
137 function! s:HgLog_DiffSummary(split, ...) abort
138 let l:revs = []
139 if a:0 >= 2
140 let l:revs = [a:1, a:2]
141 elseif a:0 == 1
142 let l:revs = [a:1]
143 else
144 let l:revs = [s:HgLog_GetSelectedRev()]
145 endif
146
147 let l:repo = lawrencium#hg_repo()
148 let l:bufobj = lawrencium#buffer_obj()
149 let l:log_path = lawrencium#parse_lawrencium_path(l:bufobj.GetName())
150 let l:path = l:repo.GetFullPath(l:log_path['path'])
151
152 " Go to the window we were in before going in the log window,
153 " and split for the diff summary from there.
154 let l:reuse_id = 'lawrencium_diffsum_for_' . bufnr('%')
155 let l:split_prev_win = (a:split < 3)
156 let l:args = {'reuse_id': l:reuse_id, 'use_prev_win': l:split_prev_win,
157 \'split_mode': a:split}
158 call lawrencium#diff#HgDiffSummary(l:path, l:args, l:revs)
159 endfunction
160
161 function! s:HgLog_GetSelectedRev(...) abort
162 if a:0 == 1
163 let l:line = getline(a:1)
164 else
165 let l:line = getline('.')
166 endif
167 " Behold, Vim's look-ahead regex syntax again! WTF.
168 let l:rev = matchstr(l:line, '\v^(\d+)(\:)@=')
169 if l:rev == ''
170 call lawrencium#throw("Can't parse revision number from line: " . l:line)
171 endif
172 return l:rev
173 endfunction
174
175 function! s:HgLog_ExportPatch(...) abort
176 let l:patch_name = a:1
177 if !empty($HG_EXPORT_PATCH_DIR)
178 " Use the patch dir only if user has specified a relative path
179 if has('win32')
180 let l:is_patch_relative = (matchstr(l:patch_name, '\v^([a-zA-Z]:)?\\') == "")
181 else
182 let l:is_patch_relative = (matchstr(l:patch_name, '\v^/') == "")
183 endif
184 if l:is_patch_relative
185 let l:patch_name = lawrencium#normalizepath(
186 lawrencium#stripslash($HG_EXPORT_PATCH_DIR) . "/" . l:patch_name)
187 endif
188 endif
189
190 if a:0 == 2
191 let l:rev = a:2
192 else
193 let l:rev = s:HgLog_GetSelectedRev()
194 endif
195
196 let l:repo = lawrencium#hg_repo()
197 let l:export_args = ['-o', l:patch_name, '-r', l:rev]
198
199 call l:repo.RunCommand('export', l:export_args)
200
201 echom "Created patch: " . l:patch_name
202 endfunction
203