Mercurial > vim-lawrencium
annotate plugin/lawrencium.vim @ 8:1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 11 Dec 2011 23:04:36 -0800 |
parents | adc267e2f0f4 |
children | 82a49134a85c |
rev | line source |
---|---|
0 | 1 " lawrencium.vim - A Mercurial wrapper |
2 " Maintainer: Ludovic Chabant <http://ludovic.chabant.com> | |
3 " Version: 0.1 | |
4 | |
5 " Globals {{{ | |
6 | |
7 if !exists('g:lawrencium_debug') | |
8 let g:lawrencium_debug = 0 | |
9 endif | |
10 | |
11 if (exists('g:loaded_lawrencium') || &cp) && !g:lawrencium_debug | |
12 finish | |
13 endif | |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
14 if (exists('g:loaded_lawrencium') && g:lawrencium_debug) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
15 echom "Reloaded Lawrencium." |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
16 endif |
0 | 17 let g:loaded_lawrencium = 1 |
18 | |
19 if !exists('g:lawrencium_hg_executable') | |
20 let g:lawrencium_hg_executable = 'hg' | |
21 endif | |
22 | |
23 if !exists('g:lawrencium_trace') | |
24 let g:lawrencium_trace = 0 | |
25 endif | |
26 | |
27 " }}} | |
28 | |
29 " Utility {{{ | |
30 | |
31 " Strips the ending slash in a path. | |
32 function! s:stripslash(path) | |
33 return fnamemodify(a:path, ':s?[/\\]$??') | |
34 endfunction | |
35 | |
36 " Normalizes the slashes in a path. | |
37 function! s:normalizepath(path) | |
38 if exists('+shellslash') && &shellslash | |
39 return substitute(a:path, '\\', '/', '') | |
40 elseif has('win32') | |
41 return substitute(a:path, '/', '\\', '') | |
42 else | |
43 return a:path | |
44 endif | |
45 endfunction | |
46 | |
47 " Prints a message if debug tracing is enabled. | |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
48 function! s:trace(message, ...) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
49 if g:lawrencium_trace || (a:0 && a:1) |
0 | 50 let l:message = "lawrencium: " . a:message |
51 echom l:message | |
52 endif | |
53 endfunction | |
54 | |
55 " Throw a Lawrencium exception message. | |
56 function! s:throw(message) | |
57 let v:errmsg = "lawrencium: " . a:message | |
58 throw v:errmsg | |
59 endfunction | |
60 | |
61 " Finds the repository root given a path inside that repository. | |
62 " Throw an error if not repository is found. | |
63 function! s:find_repo_root(path) | |
64 let l:path = s:stripslash(a:path) | |
65 let l:previous_path = "" | |
66 while l:path != l:previous_path | |
67 if isdirectory(l:path . '/.hg/store') | |
68 return simplify(fnamemodify(l:path, ':p')) | |
69 endif | |
70 let l:previous_path = l:path | |
71 let l:path = fnamemodify(l:path, ':h') | |
72 endwhile | |
73 call s:throw("No Mercurial repository found above: " . a:path) | |
74 endfunction | |
75 | |
76 " }}} | |
77 | |
78 " Mercurial Repository {{{ | |
79 | |
80 " Let's define a Mercurial repo 'class' using prototype-based object-oriented | |
81 " programming. | |
82 " | |
83 " The prototype dictionary. | |
84 let s:HgRepo = {} | |
85 | |
86 " Constructor | |
87 function! s:HgRepo.New(path) abort | |
88 let l:newRepo = copy(self) | |
89 let l:newRepo.root_dir = s:find_repo_root(a:path) | |
90 call s:trace("Built new Mercurial repository object at : " . l:newRepo.root_dir) | |
91 return l:newRepo | |
92 endfunction | |
93 | |
94 " Gets a full path given a repo-relative path | |
95 function! s:HgRepo.GetFullPath(path) abort | |
96 let l:root_dir = self.root_dir | |
97 if a:path =~# '^[/\\]' | |
98 let l:root_dir = s:stripslash(l:root_dir) | |
99 endif | |
100 return l:root_dir . a:path | |
101 endfunction | |
102 | |
103 " Gets a list of files matching a root-relative pattern. | |
104 " If a flag is passed and is TRUE, a slash will be appended to all | |
105 " directories. | |
106 function! s:HgRepo.Glob(pattern, ...) abort | |
107 let l:root_dir = self.root_dir | |
108 if (a:pattern =~# '^[/\\]') | |
109 let l:root_dir = s:stripslash(l:root_dir) | |
110 endif | |
111 let l:matches = split(glob(l:root_dir . a:pattern), '\n') | |
112 if a:0 && a:1 | |
113 for l:idx in range(len(l:matches)) | |
114 if !filereadable(l:matches[l:idx]) | |
115 let l:matches[l:idx] = l:matches[l:idx] . '/' | |
116 endif | |
117 endfor | |
118 endif | |
119 let l:strip_len = len(l:root_dir) | |
120 call map(l:matches, 'v:val[l:strip_len : -1]') | |
121 return l:matches | |
122 endfunction | |
123 | |
124 " Runs a Mercurial command in the repo | |
125 function! s:HgRepo.RunCommand(command, ...) abort | |
126 let l:hg_command = g:lawrencium_hg_executable . ' --repository ' . shellescape(s:stripslash(self.root_dir)) | |
127 let l:hg_command = l:hg_command . ' ' . a:command . ' ' . join(a:000, ' ') | |
128 call s:trace("Running Mercurial command: " . l:hg_command) | |
129 return system(l:hg_command) | |
130 endfunction | |
131 | |
132 " Repo cache map | |
133 let s:buffer_repos = {} | |
134 | |
135 " Get a cached repo | |
136 function! s:hg_repo(...) abort | |
137 " Use the given path, or the mercurial directory of the current buffer. | |
138 if a:0 == 0 | |
139 if exists('b:mercurial_dir') | |
140 let l:path = b:mercurial_dir | |
141 else | |
142 let l:path = s:find_repo_root(expand('%:p')) | |
143 endif | |
144 else | |
145 let l:path = a:1 | |
146 endif | |
147 " Find a cache repo instance, or make a new one. | |
148 if has_key(s:buffer_repos, l:path) | |
149 return get(s:buffer_repos, l:path) | |
150 else | |
151 let l:repo = s:HgRepo.New(l:path) | |
152 let s:buffer_repos[l:path] = l:repo | |
153 return l:repo | |
154 endif | |
155 endfunction | |
156 | |
157 " Sets up the current buffer with Lawrencium commands if it contains a file from a Mercurial repo. | |
158 " If the file is not in a Mercurial repo, just exit silently. | |
159 function! s:setup_buffer_commands() abort | |
160 call s:trace("Scanning buffer '" . bufname('%') . "' for Lawrencium setup...") | |
161 let l:do_setup = 1 | |
162 if exists('b:mercurial_dir') | |
163 if b:mercurial_dir =~# '/^\s*$/' | |
164 unlet b:mercurial_dir | |
165 else | |
166 let l:do_setup = 0 | |
167 endif | |
168 endif | |
169 try | |
170 let l:repo = s:hg_repo() | |
171 catch /^lawrencium\:/ | |
172 return | |
173 endtry | |
174 let b:mercurial_dir = l:repo.root_dir | |
175 if exists('b:mercurial_dir') && l:do_setup | |
176 call s:trace("Setting Mercurial commands for buffer '" . bufname('%')) | |
177 call s:trace(" with repo : " . expand(b:mercurial_dir)) | |
178 silent doautocmd User Lawrencium | |
179 endif | |
180 endfunction | |
181 | |
182 augroup lawrencium_detect | |
183 autocmd! | |
184 autocmd BufNewFile,BufReadPost * call s:setup_buffer_commands() | |
185 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_buffer_commands()|endif | |
186 augroup end | |
187 | |
188 " }}} | |
189 | |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
190 " Main Buffer Commands {{{ |
0 | 191 |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
192 " Store the commands for Lawrencium-enabled buffers so that we can add them in |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
193 " batch when we need to. |
0 | 194 let s:main_commands = [] |
195 | |
196 function! s:AddMainCommand(command) abort | |
197 let s:main_commands += [a:command] | |
198 endfunction | |
199 | |
200 function! s:DefineMainCommands() | |
201 for l:command in s:main_commands | |
202 execute 'command! -buffer ' . l:command | |
203 endfor | |
204 endfunction | |
205 | |
206 augroup lawrencium_main | |
207 autocmd! | |
208 autocmd User Lawrencium call s:DefineMainCommands() | |
209 augroup end | |
210 | |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
211 " Auto-complete function for commands that take repo-relative file paths. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
212 function! s:ListRepoFiles(ArgLead, CmdLine, CursorPos) abort |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
213 let l:matches = s:hg_repo().Glob(a:ArgLead . '*', 1) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
214 call map(l:matches, 's:normalizepath(v:val)') |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
215 return l:matches |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
216 endfunction |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
217 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
218 " Auto-complete function for commands that take repo-relative directory paths. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
219 function! s:ListRepoDirs(ArgLead, CmdLine, CursorPos) abort |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
220 let l:matches = s:hg_repo().Glob(a:ArgLead . '*/') |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
221 call map(l:matches, 's:normalizepath(v:val)') |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
222 return l:matches |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
223 endfunction |
0 | 224 |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
225 " Hg {{{ |
0 | 226 |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
227 function! s:Hg(bang, ...) abort |
0 | 228 let l:repo = s:hg_repo() |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
229 let l:output = call(l:repo.RunCommand, a:000, l:repo) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
230 if a:bang |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
231 " Open the output of the command in a temp file. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
232 let l:temp_file = tempname() |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
233 execute 'pedit ' . l:temp_file |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
234 wincmd p |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
235 call append(0, l:output) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
236 else |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
237 " Just print out the output of the command. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
238 echo l:output |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
239 endif |
0 | 240 endfunction |
241 | |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
242 call s:AddMainCommand("-bang -nargs=* Hg :execute s:Hg(<bang>0, <f-args>)") |
0 | 243 |
244 " }}} | |
245 | |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
246 " Hgstatus {{{ |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
247 |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
248 let s:hg_status_messages = { |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
249 \'M': 'modified', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
250 \'A': 'added', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
251 \'R': 'removed', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
252 \'C': 'clean', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
253 \'!': 'missing', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
254 \'?': 'not tracked', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
255 \'I': 'ignored', |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
256 \} |
0 | 257 |
258 function! s:HgStatus() abort | |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
259 " Get the repo and the `hg status` output. |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
260 let l:repo = s:hg_repo() |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
261 let l:status_text = l:repo.RunCommand('status') |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
262 let l:status_lines = split(l:status_text, '\n') |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
263 |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
264 " Open a new temp buffer in the preview window, jump to it, |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
265 " and paste the `hg status` output in there. |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
266 " Also, make it a nice size, but restore the `previewheight` setting after |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
267 " we're done. |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
268 let l:temp_file = tempname() |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
269 let l:temp_file = fnamemodify(l:temp_file, ':h') . 'hg-status-' . fnamemodify(l:temp_file, ':t') . '.txt' |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
270 let l:preview_height = &previewheight |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
271 execute "setlocal previewheight=" . (len(l:status_lines) + 1) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
272 execute "pedit " . l:temp_file |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
273 wincmd p |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
274 call append(0, l:status_lines) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
275 execute "setlocal previewheight=" . l:preview_height |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
276 |
7
adc267e2f0f4
Added syntax highlighting for hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
277 " Setup the buffer correctly: readonly, and with the correct repo linked |
adc267e2f0f4
Added syntax highlighting for hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
278 " to it. |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
279 let b:mercurial_dir = l:repo.root_dir |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
280 setlocal buftype=nofile |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
281 setlocal nomodified |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
282 setlocal nomodifiable |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
283 setlocal readonly |
7
adc267e2f0f4
Added syntax highlighting for hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents:
6
diff
changeset
|
284 setlocal syntax=hgstatus |
6
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
285 |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
286 " Add some handy mappings. |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
287 nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr> |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
288 nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.','Wbe')<cr> |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
289 nnoremap <buffer> <silent> <cr> :execute <SID>HgStatus_FileEdit()<cr> |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
290 nnoremap <buffer> <silent> q :bdelete<cr> |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
291 endfunction |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
292 |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
293 function! s:HgStatus_FileEdit() abort |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
294 let l:repo = s:hg_repo() |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
295 let l:line = getline('.') |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
296 " Yay, awesome, Vim's regex syntax is fucked up like shit, especially for |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
297 " look-aheads and look-behinds. See for yourself: |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
298 let l:filename = matchstr(l:line, '\([MARC\!\?I ]\s\)\@<=.*') |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
299 let l:filename = l:repo.GetFullPath(l:filename) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
300 " Go back to the previous window and open the file there, or open an |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
301 " existing buffer. |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
302 wincmd p |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
303 if bufexists(l:filename) |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
304 execute 'buffer ' . l:filename |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
305 else |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
306 execute 'edit ' . l:filename |
1da613c13d81
Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents:
5
diff
changeset
|
307 endif |
0 | 308 endfunction |
309 | |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
310 call s:AddMainCommand("Hgstatus :execute s:HgStatus()") |
0 | 311 |
312 " }}} | |
313 | |
314 " Hgcd, Hglcd {{{ | |
315 | |
316 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoDirs Hgcd :cd<bang> `=s:hg_repo().GetFullPath(<q-args>)`") | |
317 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoDirs Hglcd :lcd<bang> `=s:hg_repo().GetFullPath(<q-args>)`") | |
318 | |
319 " }}} | |
320 | |
321 " Hgedit {{{ | |
322 | |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
323 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoFiles Hgedit :edit<bang> `=s:hg_repo().GetFullPath(<q-args>)`") |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
324 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
325 " }}} |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
326 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
327 " Hgdiff {{{ |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
328 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
329 function! s:HgDiff(filename, vertical, ...) abort |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
330 " Default revisions to diff: the working directory (special Lawrencium |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
331 " hard-coded syntax) and the parent of the working directory (using |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
332 " Mercurial's revsets syntax). |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
333 let l:rev1 = 'lawrencium#_wdir_' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
334 let l:rev2 = 'p1()' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
335 if a:0 == 1 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
336 let l:rev2 = a:1 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
337 elseif a:0 == 2 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
338 let l:rev1 = a:1 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
339 let l:rev2 = a:2 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
340 endif |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
341 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
342 " Get the current repo, and expand the given filename in case it contains |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
343 " fancy filename modifiers. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
344 let l:repo = s:hg_repo() |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
345 let l:path = expand(a:filename) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
346 call s:trace("Diff'ing '".l:rev1."' and '".l:rev2."' on file: ".l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
347 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
348 " We'll keep a list of buffers in this diff, so when one exits, the |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
349 " others' 'diff' flag is turned off. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
350 let l:diff_buffers = [] |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
351 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
352 " Get the first file and open it. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
353 if l:rev1 == 'lawrencium#_wdir_' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
354 if bufexists(l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
355 execute 'buffer ' . fnameescape(l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
356 else |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
357 execute 'edit ' . fnameescape(l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
358 endif |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
359 else |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
360 let l:temp_file = tempname() |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
361 call l:repo.RunCommand('cat', '-r', '"'.l:rev1.'"', '-o', l:temp_file, l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
362 execute 'edit ' . fnameescape(l:temp_file) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
363 endif |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
364 " Set it up to be part of the diff windows, set its repo dir. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
365 diffthis |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
366 let b:mercurial_dir = l:repo.root_dir |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
367 |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
368 " Get the second file and open it too. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
369 let l:diffsplit = 'diffsplit' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
370 if a:vertical |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
371 let l:diffsplit = 'vertical diffsplit' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
372 endif |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
373 if l:rev2 == 'lawrencium#_wdir_' |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
374 execute l:diffsplit . ' ' . fnameescape(l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
375 else |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
376 let l:temp_file = tempname() |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
377 call l:repo.RunCommand('cat', '-r', '"'.l:rev2.'"', '-o', l:temp_file, l:path) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
378 execute l:diffsplit . ' ' . fnameescape(l:temp_file) |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
379 endif |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
380 " Set its repo dir. |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
381 let b:mercurial_dir = l:repo.root_dir |
0 | 382 endfunction |
383 | |
8
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
384 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :execute s:HgDiff('%:p', 0, <f-args>)") |
1e155bfa94ad
Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents:
7
diff
changeset
|
385 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :execute s:HgDiff('%:p', 1, <f-args>)") |
0 | 386 |
387 " }}} | |
388 | |
4
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
389 " }}} |
b6e4446ed292
HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents:
0
diff
changeset
|
390 |
0 | 391 " Autoload Functions {{{ |
392 | |
393 " Prints a summary of the current repo (if any) that's appropriate for | |
394 " displaying on the status line. | |
395 function! lawrencium#statusline(...) | |
396 if !exists('b:mercurial_dir') | |
397 return '' | |
398 endif | |
5
3a4f9f41a7e2
Use a hackish shortcut to get the current branch faster for the statusline.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
399 let l:prefix = (a:0 > 0 ? a:1 : '') |
3a4f9f41a7e2
Use a hackish shortcut to get the current branch faster for the statusline.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
400 let l:suffix = (a:0 > 1 ? a:2 : '') |
3a4f9f41a7e2
Use a hackish shortcut to get the current branch faster for the statusline.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
401 let l:branch_file = s:hg_repo().GetFullPath('.hg/branch') |
3a4f9f41a7e2
Use a hackish shortcut to get the current branch faster for the statusline.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
402 let l:branch = readfile(l:branch_file)[0] |
3a4f9f41a7e2
Use a hackish shortcut to get the current branch faster for the statusline.
Ludovic Chabant <ludovic@chabant.com>
parents:
4
diff
changeset
|
403 return l:prefix . l:branch . l:suffix |
0 | 404 endfunction |
405 | |
406 " Rescans the current buffer for setting up Mercurial commands. | |
407 " Passing '1' as the parameter enables debug traces temporarily. | |
408 function! lawrencium#rescan(...) | |
409 if exists('b:mercurial_dir') | |
410 unlet b:mercurial_dir | |
411 endif | |
412 if a:0 && a:1 | |
413 let l:trace_backup = g:lawrencium_trace | |
414 let g:lawrencium_trace = 1 | |
415 endif | |
416 call s:setup_buffer_commands() | |
417 if a:0 && a:1 | |
418 let g:lawrencium_trace = l:trace_backup | |
419 endif | |
420 endfunction | |
421 | |
422 " Enables/disables the debug trace. | |
423 function! lawrencium#debugtrace(...) | |
424 let g:lawrencium_trace = (a:0 == 0 || (a:0 && a:1)) | |
425 echom "Lawrencium debug trace is now " . (g:lawrencium_trace ? "enabled." : "disabled.") | |
426 endfunction | |
427 | |
428 " }}} | |
429 |