Mercurial > vim-lawrencium
comparison autoload/lawrencium/hg.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#hg#init() abort | |
3 call lawrencium#add_command("-bang -complete=customlist,lawrencium#hg#CompleteHg -nargs=* Hg :call lawrencium#hg#Hg(<bang>0, <f-args>)") | |
4 endfunction | |
5 | |
6 function! lawrencium#hg#Hg(bang, ...) abort | |
7 let l:repo = lawrencium#hg_repo() | |
8 if g:lawrencium_auto_cd | |
9 " Temporary set the current directory to the root of the repo | |
10 " to make auto-completed paths work magically. | |
11 execute 'cd! ' . fnameescape(l:repo.root_dir) | |
12 endif | |
13 let l:output = call(l:repo.RunCommandEx, [0] + a:000, l:repo) | |
14 if g:lawrencium_auto_cd | |
15 execute 'cd! -' | |
16 endif | |
17 silent doautocmd User HgCmdPost | |
18 if a:bang | |
19 " Open the output of the command in a temp file. | |
20 let l:temp_file = lawrencium#tempname('hg-output-', '.txt') | |
21 split | |
22 execute 'edit ' . fnameescape(l:temp_file) | |
23 call append(0, split(l:output, '\n')) | |
24 call cursor(1, 1) | |
25 | |
26 " Make it a temp buffer | |
27 setlocal bufhidden=delete | |
28 setlocal buftype=nofile | |
29 | |
30 " Try to find a nice syntax to set given the current command. | |
31 let l:command_name = s:GetHgCommandName(a:000) | |
32 if l:command_name != '' && exists('g:lawrencium_hg_commands_file_types') | |
33 let l:file_type = get(g:lawrencium_hg_commands_file_types, l:command_name, '') | |
34 if l:file_type != '' | |
35 execute 'setlocal ft=' . l:file_type | |
36 endif | |
37 endif | |
38 else | |
39 " Just print out the output of the command. | |
40 echo l:output | |
41 endif | |
42 endfunction | |
43 | |
44 " Include the generated HG usage file. | |
45 let s:usage_file = expand("<sfile>:h:h:h") . "/resources/hg_usage.vim" | |
46 if filereadable(s:usage_file) | |
47 execute "source " . fnameescape(s:usage_file) | |
48 else | |
49 call lawrencium#error("Can't find the Mercurial usage file. Auto-completion will be disabled in Lawrencium.") | |
50 endif | |
51 | |
52 " Include the command file type mappings. | |
53 let s:file_type_mappings = expand("<sfile>:h:h") . '/resources/hg_command_file_types.vim' | |
54 if filereadable(s:file_type_mappings) | |
55 execute "source " . fnameescape(s:file_type_mappings) | |
56 endif | |
57 | |
58 function! lawrencium#hg#CompleteHg(ArgLead, CmdLine, CursorPos) | |
59 " Don't do anything if the usage file was not sourced. | |
60 if !exists('g:lawrencium_hg_commands') || !exists('g:lawrencium_hg_options') | |
61 return [] | |
62 endif | |
63 | |
64 " a:ArgLead seems to be the number 0 when completing a minus '-'. | |
65 " Gotta find out why... | |
66 let l:arglead = a:ArgLead | |
67 if type(a:ArgLead) == type(0) | |
68 let l:arglead = '-' | |
69 endif | |
70 | |
71 " Try completing a global option, before any command name. | |
72 if a:CmdLine =~# '\v^Hg(\s+\-[a-zA-Z0-9\-_]*)+$' | |
73 return filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") | |
74 endif | |
75 | |
76 " Try completing a command (note that there could be global options before | |
77 " the command name). | |
78 if a:CmdLine =~# '\v^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*[a-zA-Z]+$' | |
79 return filter(keys(g:lawrencium_hg_commands), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") | |
80 endif | |
81 | |
82 " Try completing a command's options. | |
83 let l:cmd = matchstr(a:CmdLine, '\v(^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*)@<=[a-zA-Z]+') | |
84 if strlen(l:cmd) > 0 && l:arglead[0] ==# '-' | |
85 if has_key(g:lawrencium_hg_commands, l:cmd) | |
86 " Return both command options and global options together. | |
87 let l:copts = filter(copy(g:lawrencium_hg_commands[l:cmd]), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") | |
88 let l:gopts = filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead") | |
89 return l:copts + l:gopts | |
90 endif | |
91 endif | |
92 | |
93 " Just auto-complete with filenames unless it's an option. | |
94 if l:arglead[0] ==# '-' | |
95 return [] | |
96 else | |
97 return lawrencium#list_repo_files(a:ArgLead, a:CmdLine, a:CursorPos) | |
98 endfunction | |
99 | |
100 function! s:GetHgCommandName(args) abort | |
101 for l:a in a:args | |
102 if stridx(l:a, '-') != 0 | |
103 return l:a | |
104 endif | |
105 endfor | |
106 return '' | |
107 endfunction | |
108 |