comparison plugin/lawrencium.vim @ 46:6a4f5200d8da

`:Hg!` command changes: - Ability to edit in a normal buffer instead of the preview window. - Set syntax coloring according to the Mercurial command. - Updated the documentation. Miscellaneous cleanup. New pretty banner in the documentation. New "global settings" section in the documentation. Added `graphlog` syntax file.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 07 Nov 2012 07:14:15 -0800
parents ea0ae8f6af81
children 85e39bdd7089
comparison
equal deleted inserted replaced
45:ea0ae8f6af81 46:6a4f5200d8da
28 let g:lawrencium_trace = 0 28 let g:lawrencium_trace = 0
29 endif 29 endif
30 30
31 if !exists('g:lawrencium_define_mappings') 31 if !exists('g:lawrencium_define_mappings')
32 let g:lawrencium_define_mappings = 1 32 let g:lawrencium_define_mappings = 1
33 endif
34
35 if !exists('g:lawrencium_hg_bang_edit_command')
36 let g:lawrencium_hg_bang_edit_command = 'pedit'
33 endif 37 endif
34 38
35 " }}} 39 " }}}
36 40
37 " Utility {{{ 41 " Utility {{{
311 execute 'cd! -' 315 execute 'cd! -'
312 endif 316 endif
313 if a:bang 317 if a:bang
314 " Open the output of the command in a temp file. 318 " Open the output of the command in a temp file.
315 let l:temp_file = s:tempname('hg-output-', '.txt') 319 let l:temp_file = s:tempname('hg-output-', '.txt')
316 execute 'pedit ' . l:temp_file 320 execute g:lawrencium_hg_bang_edit_command . ' ' . l:temp_file
317 wincmd p 321 wincmd p
318 call append(0, split(l:output, '\n')) 322 call append(0, split(l:output, '\n'))
323 call cursor(1, 1)
324
325 " Make it a temp buffer
326 setlocal bufhidden=delete
327 setlocal buftype=nofile
328
329 " Try to find a nice syntax to set given the current command.
330 let l:command_name = s:GetHgCommandName(a:000)
331 if l:command_name != '' && exists('g:lawrencium_hg_commands_file_types')
332 let l:file_type = get(g:lawrencium_hg_commands_file_types, l:command_name, '')
333 if l:file_type != ''
334 execute 'setlocal ft=' . l:file_type
335 endif
336 endif
319 else 337 else
320 " Just print out the output of the command. 338 " Just print out the output of the command.
321 echo l:output 339 echo l:output
322 endif 340 endif
323 endfunction 341 endfunction
328 execute "source " . s:usage_file 346 execute "source " . s:usage_file
329 else 347 else
330 call s:error("Can't find the Mercurial usage file. Auto-completion will be disabled in Lawrencium.") 348 call s:error("Can't find the Mercurial usage file. Auto-completion will be disabled in Lawrencium.")
331 endif 349 endif
332 350
351 " Include the command file type mappings.
352 let s:file_type_mappings = expand("<sfile>:h:h") . '/resources/hg_command_file_types.vim'
353 if filereadable(s:file_type_mappings)
354 execute "source " . s:file_type_mappings
355 endif
356
333 function! s:CompleteHg(ArgLead, CmdLine, CursorPos) 357 function! s:CompleteHg(ArgLead, CmdLine, CursorPos)
334 " Don't do anything if the usage file was not sourced. 358 " Don't do anything if the usage file was not sourced.
335 if !exists('g:lawrencium_hg_commands') || !exists('g:lawrencium_hg_options') 359 if !exists('g:lawrencium_hg_commands') || !exists('g:lawrencium_hg_options')
336 return [] 360 return []
337 endif 361 endif
349 endif 373 endif
350 374
351 " Try completing a command (note that there could be global options before 375 " Try completing a command (note that there could be global options before
352 " the command name). 376 " the command name).
353 if a:CmdLine =~# '\v^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*[a-zA-Z]+$' 377 if a:CmdLine =~# '\v^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*[a-zA-Z]+$'
354 echom " - matched command"
355 return filter(keys(g:lawrencium_hg_commands), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") 378 return filter(keys(g:lawrencium_hg_commands), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
356 endif 379 endif
357 380
358 " Try completing a command's options. 381 " Try completing a command's options.
359 let l:cmd = matchstr(a:CmdLine, '\v(^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*)@<=[a-zA-Z]+') 382 let l:cmd = matchstr(a:CmdLine, '\v(^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*)@<=[a-zA-Z]+')
360 if strlen(l:cmd) > 0
361 echom " - matched command option for " . l:cmd . " with : " . l:arglead
362 endif
363 if strlen(l:cmd) > 0 && l:arglead[0] ==# '-' 383 if strlen(l:cmd) > 0 && l:arglead[0] ==# '-'
364 if has_key(g:lawrencium_hg_commands, l:cmd) 384 if has_key(g:lawrencium_hg_commands, l:cmd)
365 " Return both command options and global options together. 385 " Return both command options and global options together.
366 let l:copts = filter(copy(g:lawrencium_hg_commands[l:cmd]), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") 386 let l:copts = filter(copy(g:lawrencium_hg_commands[l:cmd]), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
367 let l:gopts = filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") 387 let l:gopts = filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
372 " Just auto-complete with filenames unless it's an option. 392 " Just auto-complete with filenames unless it's an option.
373 if l:arglead[0] ==# '-' 393 if l:arglead[0] ==# '-'
374 return [] 394 return []
375 else 395 else
376 return s:ListRepoFiles(a:ArgLead, a:CmdLine, a:CursorPos) 396 return s:ListRepoFiles(a:ArgLead, a:CmdLine, a:CursorPos)
397 endfunction
398
399 function! s:GetHgCommandName(args) abort
400 for l:a in a:args
401 if stridx(l:a, '-') != 0
402 return l:a
403 endif
404 endfor
405 return ''
377 endfunction 406 endfunction
378 407
379 call s:AddMainCommand("-bang -complete=customlist,s:CompleteHg -nargs=* Hg :call s:Hg(<bang>0, <f-args>)") 408 call s:AddMainCommand("-bang -complete=customlist,s:CompleteHg -nargs=* Hg :call s:Hg(<bang>0, <f-args>)")
380 409
381 " }}} 410 " }}}
449 " Get the repo and the `hg status` output. 478 " Get the repo and the `hg status` output.
450 let l:repo = s:hg_repo() 479 let l:repo = s:hg_repo()
451 let l:status_text = l:repo.RunCommand('status') 480 let l:status_text = l:repo.RunCommand('status')
452 481
453 " Replace the contents of the current buffer with it, and refresh. 482 " Replace the contents of the current buffer with it, and refresh.
454 echo "Writing to " . expand('%:p')
455 let l:path = expand('%:p') 483 let l:path = expand('%:p')
456 let l:status_lines = split(l:status_text, '\n') 484 let l:status_lines = split(l:status_text, '\n')
457 call writefile(l:status_lines, l:path) 485 call writefile(l:status_lines, l:path)
458 edit 486 edit
459 endfunction 487 endfunction