comparison plugin/lawrencium.vim @ 11:b4baab0a4a92

Made most regex use the 'very-magic' syntax. Temp windows (status, diff, etc.) delete the buffer on exit.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 12 Dec 2011 15:21:20 -0800
parents 7d16084d40a9
children a7bf37a97a1b
comparison
equal deleted inserted replaced
10:7d16084d40a9 11:b4baab0a4a92
92 endfunction 92 endfunction
93 93
94 " Gets a full path given a repo-relative path 94 " Gets a full path given a repo-relative path
95 function! s:HgRepo.GetFullPath(path) abort 95 function! s:HgRepo.GetFullPath(path) abort
96 let l:root_dir = self.root_dir 96 let l:root_dir = self.root_dir
97 if a:path =~# '^[/\\]' 97 if a:path =~# '\v^[/\\]'
98 let l:root_dir = s:stripslash(l:root_dir) 98 let l:root_dir = s:stripslash(l:root_dir)
99 endif 99 endif
100 return l:root_dir . a:path 100 return l:root_dir . a:path
101 endfunction 101 endfunction
102 102
103 " Gets a list of files matching a root-relative pattern. 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 104 " If a flag is passed and is TRUE, a slash will be appended to all
105 " directories. 105 " directories.
106 function! s:HgRepo.Glob(pattern, ...) abort 106 function! s:HgRepo.Glob(pattern, ...) abort
107 let l:root_dir = self.root_dir 107 let l:root_dir = self.root_dir
108 if (a:pattern =~# '^[/\\]') 108 if (a:pattern =~# '\v^[/\\]')
109 let l:root_dir = s:stripslash(l:root_dir) 109 let l:root_dir = s:stripslash(l:root_dir)
110 endif 110 endif
111 let l:matches = split(glob(l:root_dir . a:pattern), '\n') 111 let l:matches = split(glob(l:root_dir . a:pattern), '\n')
112 if a:0 && a:1 112 if a:0 && a:1
113 for l:idx in range(len(l:matches)) 113 for l:idx in range(len(l:matches))
158 " If the file is not in a Mercurial repo, just exit silently. 158 " If the file is not in a Mercurial repo, just exit silently.
159 function! s:setup_buffer_commands() abort 159 function! s:setup_buffer_commands() abort
160 call s:trace("Scanning buffer '" . bufname('%') . "' for Lawrencium setup...") 160 call s:trace("Scanning buffer '" . bufname('%') . "' for Lawrencium setup...")
161 let l:do_setup = 1 161 let l:do_setup = 1
162 if exists('b:mercurial_dir') 162 if exists('b:mercurial_dir')
163 if b:mercurial_dir =~# '/^\s*$/' 163 if b:mercurial_dir =~# '\v^\s*$'
164 unlet b:mercurial_dir 164 unlet b:mercurial_dir
165 else 165 else
166 let l:do_setup = 0 166 let l:do_setup = 0
167 endif 167 endif
168 endif 168 endif
247 247
248 function! s:HgStatus() abort 248 function! s:HgStatus() abort
249 " Get the repo and the `hg status` output. 249 " Get the repo and the `hg status` output.
250 let l:repo = s:hg_repo() 250 let l:repo = s:hg_repo()
251 let l:status_text = l:repo.RunCommand('status') 251 let l:status_text = l:repo.RunCommand('status')
252 let l:status_lines = split(l:status_text, '\n') 252 if l:status_text ==# '\v\%^\s*\%$'
253 echo "Nothing modified."
254 endif
253 255
254 " Open a new temp buffer in the preview window, jump to it, 256 " Open a new temp buffer in the preview window, jump to it,
255 " and paste the `hg status` output in there. 257 " and paste the `hg status` output in there.
256 " Also, make it a nice size, but restore the `previewheight` setting after
257 " we're done.
258 let l:temp_file = tempname() 258 let l:temp_file = tempname()
259 let l:temp_file = fnamemodify(l:temp_file, ':h') . 'hg-status-' . fnamemodify(l:temp_file, ':t') . '.txt' 259 let l:temp_file = fnamemodify(l:temp_file, ':h') . 'hg-status-' . fnamemodify(l:temp_file, ':t') . '.txt'
260 let l:preview_height = &previewheight 260 let l:preview_height = &previewheight
261 let l:status_lines = split(l:status_text, '\n')
261 execute "setlocal previewheight=" . (len(l:status_lines) + 1) 262 execute "setlocal previewheight=" . (len(l:status_lines) + 1)
262 execute "pedit " . l:temp_file 263 execute "pedit " . l:temp_file
263 wincmd p 264 wincmd p
264 call append(0, l:status_lines) 265 call append(0, l:status_lines)
266 " Make it a nice size.
265 execute "setlocal previewheight=" . l:preview_height 267 execute "setlocal previewheight=" . l:preview_height
268 " Make sure it's deleted when we exit the window.
269 setlocal bufhidden=delete
266 270
267 " Setup the buffer correctly: readonly, and with the correct repo linked 271 " Setup the buffer correctly: readonly, and with the correct repo linked
268 " to it. 272 " to it.
269 let b:mercurial_dir = l:repo.root_dir 273 let b:mercurial_dir = l:repo.root_dir
270 setlocal buftype=nofile 274 setlocal buftype=nofile
308 function! s:HgStatus_GetSelectedPath() abort 312 function! s:HgStatus_GetSelectedPath() abort
309 let l:repo = s:hg_repo() 313 let l:repo = s:hg_repo()
310 let l:line = getline('.') 314 let l:line = getline('.')
311 " Yay, awesome, Vim's regex syntax is fucked up like shit, especially for 315 " Yay, awesome, Vim's regex syntax is fucked up like shit, especially for
312 " look-aheads and look-behinds. See for yourself: 316 " look-aheads and look-behinds. See for yourself:
313 let l:filename = matchstr(l:line, '\([MARC\!\?I ]\s\)\@<=.*') 317 let l:filename = matchstr(l:line, '\v([MARC\!\?I ]\s)@<=.*')
314 let l:filename = l:repo.GetFullPath(l:filename) 318 let l:filename = l:repo.GetFullPath(l:filename)
315 return l:filename 319 return l:filename
316 endfunction 320 endfunction
317 321
318 call s:AddMainCommand("Hgstatus :execute s:HgStatus()") 322 call s:AddMainCommand("Hgstatus :execute s:HgStatus()")
362 if bufexists(l:path) 366 if bufexists(l:path)
363 execute 'buffer ' . fnameescape(l:path) 367 execute 'buffer ' . fnameescape(l:path)
364 else 368 else
365 execute 'edit ' . fnameescape(l:path) 369 execute 'edit ' . fnameescape(l:path)
366 endif 370 endif
371 " Make it part of the diff group.
372 diffthis
367 else 373 else
368 let l:temp_file = tempname() 374 let l:temp_file = tempname()
369 call l:repo.RunCommand('cat', '-r', '"'.l:rev1.'"', '-o', l:temp_file, l:path) 375 call l:repo.RunCommand('cat', '-r', '"'.l:rev1.'"', '-o', l:temp_file, l:path)
370 execute 'edit ' . fnameescape(l:temp_file) 376 execute 'edit ' . fnameescape(l:temp_file)
371 endif 377 " Make it part of the diff group.
372 " Set it up to be part of the diff windows, set its repo dir. 378 diffthis
373 diffthis 379 " Remember the repo it belongs to.
374 let b:mercurial_dir = l:repo.root_dir 380 let b:mercurial_dir = l:repo.root_dir
381 " Make sure it's deleted when we move away from it.
382 setlocal bufhidden=delete
383 endif
375 384
376 " Get the second file and open it too. 385 " Get the second file and open it too.
377 let l:diffsplit = 'diffsplit' 386 let l:diffsplit = 'diffsplit'
378 if a:vertical 387 if a:vertical
379 let l:diffsplit = 'vertical diffsplit' 388 let l:diffsplit = 'vertical diffsplit'
382 execute l:diffsplit . ' ' . fnameescape(l:path) 391 execute l:diffsplit . ' ' . fnameescape(l:path)
383 else 392 else
384 let l:temp_file = tempname() 393 let l:temp_file = tempname()
385 call l:repo.RunCommand('cat', '-r', '"'.l:rev2.'"', '-o', l:temp_file, l:path) 394 call l:repo.RunCommand('cat', '-r', '"'.l:rev2.'"', '-o', l:temp_file, l:path)
386 execute l:diffsplit . ' ' . fnameescape(l:temp_file) 395 execute l:diffsplit . ' ' . fnameescape(l:temp_file)
387 endif 396 " Remember the repo it belongs to.
388 " Set its repo dir. 397 let b:mercurial_dir = l:repo.root_dir
389 let b:mercurial_dir = l:repo.root_dir 398 " Make sure it's deleted when we move away from it.
399 setlocal bufhidden=delete
400 endif
390 endfunction 401 endfunction
391 402
392 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :execute s:HgDiff('%:p', 0, <f-args>)") 403 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :execute s:HgDiff('%:p', 0, <f-args>)")
393 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :execute s:HgDiff('%:p', 1, <f-args>)") 404 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :execute s:HgDiff('%:p', 1, <f-args>)")
394 405
440 let l:status_lines = split(a:repo.RunCommand('status'), "\n") 451 let l:status_lines = split(a:repo.RunCommand('status'), "\n")
441 for l:line in l:status_lines 452 for l:line in l:status_lines
442 if l:line ==# '' 453 if l:line ==# ''
443 continue 454 continue
444 endif 455 endif
445 let l:type = matchstr(l:line, '^[MARC\!\?I ]') 456 let l:type = matchstr(l:line, '\v^[MARC\!\?I ]')
446 let l:path = l:line[2:] 457 let l:path = l:line[2:]
447 let l:msg .= "HG: " . s:hg_status_messages[l:type] . ' ' . l:path . "\n" 458 let l:msg .= "HG: " . s:hg_status_messages[l:type] . ' ' . l:path . "\n"
448 endfor 459 endfor
449 460
450 return l:msg 461 return l:msg