annotate plugin/lawrencium.vim @ 31:3a0f7bb6ea64

Hgstatus window improvements and bug fixes: - `addremove` command replaces `add`, and can run on a selection range. - Lawrencium commands are available (along with in the diff windows). - Default mappings are optional. - Updated documentation.
author Ludovic Chabant <ludovic@chabant.com>
date Sun, 25 Dec 2011 22:40:30 -0800
parents 0cdfdab43907
children 799c7b57e19a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 " lawrencium.vim - A Mercurial wrapper
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 " Maintainer: Ludovic Chabant <http://ludovic.chabant.com>
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 " Version: 0.1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 " Globals {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 if !exists('g:lawrencium_debug')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 let g:lawrencium_debug = 0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 if (exists('g:loaded_lawrencium') || &cp) && !g:lawrencium_debug
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 finish
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
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
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 let g:loaded_lawrencium = 1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 if !exists('g:lawrencium_hg_executable')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 let g:lawrencium_hg_executable = 'hg'
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 if !exists('g:lawrencium_trace')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 let g:lawrencium_trace = 0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
27 if !exists('g:lawrencium_define_mappings')
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
28 let g:lawrencium_define_mappings = 1
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
29 endif
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
30
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 " Utility {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 " Strips the ending slash in a path.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 function! s:stripslash(path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37 return fnamemodify(a:path, ':s?[/\\]$??')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40 " Normalizes the slashes in a path.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 function! s:normalizepath(path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 if exists('+shellslash') && &shellslash
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
43 return substitute(a:path, '\\', '/', '')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
44 elseif has('win32')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
45 return substitute(a:path, '/', '\\', '')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
46 else
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
47 return a:path
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
48 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
49 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
50
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
51 " Like tempname() but with some control over the filename.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
52 function! s:tempname(name, ...)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
53 let l:path = tempname()
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
54 let l:result = fnamemodify(l:path, ':h') . '/' . a:name . fnamemodify(l:path, ':t')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
55 if a:0 > 0
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
56 let l:result = l:result . a:1
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
57 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
58 return l:result
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
59 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
60
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61 " Prints a message if debug tracing is enabled.
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
62 function! s:trace(message, ...)
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
63 if g:lawrencium_trace || (a:0 && a:1)
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 let l:message = "lawrencium: " . a:message
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65 echom l:message
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
66 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
67 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
68
18
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
69 " Prints an error message with 'lawrencium error' prefixed to it.
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
70 function! s:error(message)
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
71 echom "lawrencium error: " . a:message
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
72 endfunction
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
73
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
74 " Throw a Lawrencium exception message.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
75 function! s:throw(message)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
76 let v:errmsg = "lawrencium: " . a:message
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
77 throw v:errmsg
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
78 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
79
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
80 " Finds the repository root given a path inside that repository.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
81 " Throw an error if not repository is found.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
82 function! s:find_repo_root(path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
83 let l:path = s:stripslash(a:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
84 let l:previous_path = ""
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
85 while l:path != l:previous_path
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
86 if isdirectory(l:path . '/.hg/store')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
87 return simplify(fnamemodify(l:path, ':p'))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
88 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
89 let l:previous_path = l:path
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
90 let l:path = fnamemodify(l:path, ':h')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
91 endwhile
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
92 call s:throw("No Mercurial repository found above: " . a:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
93 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
94
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
95 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
96
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
97 " Mercurial Repository {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
98
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
99 " Let's define a Mercurial repo 'class' using prototype-based object-oriented
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
100 " programming.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
101 "
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
102 " The prototype dictionary.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
103 let s:HgRepo = {}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
104
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
105 " Constructor
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
106 function! s:HgRepo.New(path) abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
107 let l:newRepo = copy(self)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
108 let l:newRepo.root_dir = s:find_repo_root(a:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
109 call s:trace("Built new Mercurial repository object at : " . l:newRepo.root_dir)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
110 return l:newRepo
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
111 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
112
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
113 " Gets a full path given a repo-relative path
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
114 function! s:HgRepo.GetFullPath(path) abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
115 let l:root_dir = self.root_dir
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
116 if a:path =~# '\v^[/\\]'
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
117 let l:root_dir = s:stripslash(l:root_dir)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
118 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
119 return l:root_dir . a:path
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
120 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
121
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
122 " Gets a list of files matching a root-relative pattern.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
123 " If a flag is passed and is TRUE, a slash will be appended to all
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
124 " directories.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
125 function! s:HgRepo.Glob(pattern, ...) abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
126 let l:root_dir = self.root_dir
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
127 if (a:pattern =~# '\v^[/\\]')
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
128 let l:root_dir = s:stripslash(l:root_dir)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
129 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
130 let l:matches = split(glob(l:root_dir . a:pattern), '\n')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
131 if a:0 && a:1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
132 for l:idx in range(len(l:matches))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
133 if !filereadable(l:matches[l:idx])
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
134 let l:matches[l:idx] = l:matches[l:idx] . '/'
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
135 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
136 endfor
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
137 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
138 let l:strip_len = len(l:root_dir)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
139 call map(l:matches, 'v:val[l:strip_len : -1]')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
140 return l:matches
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
141 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
142
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
143 " Runs a Mercurial command in the repo
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
144 function! s:HgRepo.RunCommand(command, ...) abort
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
145 " If there's only one argument, and it's a list, then use that as the
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
146 " argument list.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
147 let l:arg_list = a:000
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
148 if a:0 == 1 && type(a:1) == type([])
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
149 let l:arg_list = a:1
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
150 endif
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
151 let l:hg_command = g:lawrencium_hg_executable . ' --repository ' . shellescape(s:stripslash(self.root_dir))
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
152 let l:hg_command = l:hg_command . ' ' . a:command . ' ' . join(l:arg_list, ' ')
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
153 call s:trace("Running Mercurial command: " . l:hg_command)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
154 return system(l:hg_command)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
155 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
156
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
157 " Repo cache map
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
158 let s:buffer_repos = {}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
159
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
160 " Get a cached repo
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
161 function! s:hg_repo(...) abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
162 " Use the given path, or the mercurial directory of the current buffer.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
163 if a:0 == 0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
164 if exists('b:mercurial_dir')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
165 let l:path = b:mercurial_dir
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
166 else
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
167 let l:path = s:find_repo_root(expand('%:p'))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
168 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
169 else
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
170 let l:path = a:1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
171 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
172 " Find a cache repo instance, or make a new one.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
173 if has_key(s:buffer_repos, l:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
174 return get(s:buffer_repos, l:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
175 else
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
176 let l:repo = s:HgRepo.New(l:path)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
177 let s:buffer_repos[l:path] = l:repo
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
178 return l:repo
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
179 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
180 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
181
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
182 " Sets up the current buffer with Lawrencium commands if it contains a file from a Mercurial repo.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
183 " If the file is not in a Mercurial repo, just exit silently.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
184 function! s:setup_buffer_commands() abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
185 call s:trace("Scanning buffer '" . bufname('%') . "' for Lawrencium setup...")
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
186 let l:do_setup = 1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
187 if exists('b:mercurial_dir')
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
188 if b:mercurial_dir =~# '\v^\s*$'
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
189 unlet b:mercurial_dir
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
190 else
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
191 let l:do_setup = 0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
192 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
193 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
194 try
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
195 let l:repo = s:hg_repo()
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
196 catch /^lawrencium\:/
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
197 return
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
198 endtry
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
199 let b:mercurial_dir = l:repo.root_dir
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
200 if exists('b:mercurial_dir') && l:do_setup
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
201 call s:trace("Setting Mercurial commands for buffer '" . bufname('%'))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
202 call s:trace(" with repo : " . expand(b:mercurial_dir))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
203 silent doautocmd User Lawrencium
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
204 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
205 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
206
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
207 augroup lawrencium_detect
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
208 autocmd!
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
209 autocmd BufNewFile,BufReadPost * call s:setup_buffer_commands()
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
210 autocmd VimEnter * if expand('<amatch>')==''|call s:setup_buffer_commands()|endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
211 augroup end
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
212
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
213 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
214
14
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
215 " Buffer Commands Management {{{
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
216
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
217 " 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
218 " batch when we need to.
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
219 let s:main_commands = []
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
220
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
221 function! s:AddMainCommand(command) abort
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
222 let s:main_commands += [a:command]
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
223 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
224
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
225 function! s:DefineMainCommands()
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
226 for l:command in s:main_commands
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
227 execute 'command! -buffer ' . l:command
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
228 endfor
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
229 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
230
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
231 augroup lawrencium_main
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
232 autocmd!
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
233 autocmd User Lawrencium call s:DefineMainCommands()
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
234 augroup end
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
235
14
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
236 " }}}
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
237
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
238 " Commands Auto-Complete {{{
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
239
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
240 " 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
241 function! s:ListRepoFiles(ArgLead, CmdLine, CursorPos) abort
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
242 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
243 call map(l:matches, 's:normalizepath(v:val)')
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
244 return l:matches
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
245 endfunction
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
246
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
247 " 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
248 function! s:ListRepoDirs(ArgLead, CmdLine, CursorPos) abort
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
249 let l:matches = s:hg_repo().Glob(a:ArgLead . '*/')
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
250 call map(l:matches, 's:normalizepath(v:val)')
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
251 return l:matches
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
252 endfunction
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
253
14
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
254 " }}}
eab2680e6818 Better fold sections.
Ludovic Chabant <ludovic@chabant.com>
parents: 12
diff changeset
255
4
b6e4446ed292 HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
256 " Hg {{{
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
257
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
258 function! s:Hg(bang, ...) abort
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
259 let l:repo = s:hg_repo()
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
260 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
261 if a:bang
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
262 " Open the output of the command in a temp file.
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
263 let l:temp_file = s:tempname('hg-output-', '.txt')
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
264 execute 'pedit ' . l:temp_file
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
265 wincmd p
21
d0acefc1ec9a Fixed multi-line output of `:Hg`.
Ludovic Chabant <ludovic@chabant.com>
parents: 18
diff changeset
266 call append(0, split(l:output, '\n'))
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
267 else
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
268 " Just print out the output of the command.
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
269 echo l:output
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
270 endif
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
271 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
272
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
273 " Include the generated HG usage file.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
274 let s:usage_file = expand("<sfile>:h:h") . "/resources/hg_usage.vim"
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
275 if filereadable(s:usage_file)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
276 execute "source " . s:usage_file
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
277 else
18
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
278 call s:error("Can't find the Mercurial usage file. Auto-completion will be disabled in Lawrencium.")
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
279 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
280
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
281 function! s:CompleteHg(ArgLead, CmdLine, CursorPos)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
282 " Don't do anything if the usage file was not sourced.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
283 if !exists('g:lawrencium_hg_commands') || !exists('g:lawrencium_hg_options')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
284 return []
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
285 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
286
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
287 " a:ArgLead seems to be the number 0 when completing a minus '-'.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
288 " Gotta find out why...
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
289 let l:arglead = a:ArgLead
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
290 if type(a:ArgLead) == type(0)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
291 let l:arglead = '-'
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
292 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
293
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
294 " Try completing a global option, before any command name.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
295 if a:CmdLine =~# '\v^Hg(\s+\-[a-zA-Z0-9\-_]*)+$'
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
296 return filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
297 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
298
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
299 " Try completing a command (note that there could be global options before
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
300 " the command name).
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
301 if a:CmdLine =~# '\v^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*[a-zA-Z]+$'
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
302 echom " - matched command"
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
303 return filter(keys(g:lawrencium_hg_commands), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
304 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
305
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
306 " Try completing a command's options.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
307 let l:cmd = matchstr(a:CmdLine, '\v(^Hg\s+(\-[a-zA-Z0-9\-_]+\s+)*)@<=[a-zA-Z]+')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
308 if strlen(l:cmd) > 0
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
309 echom " - matched command option for " . l:cmd . " with : " . l:arglead
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
310 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
311 if strlen(l:cmd) > 0 && l:arglead[0] ==# '-'
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
312 if has_key(g:lawrencium_hg_commands, l:cmd)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
313 " Return both command options and global options together.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
314 let l:copts = filter(copy(g:lawrencium_hg_commands[l:cmd]), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
315 let l:gopts = filter(copy(g:lawrencium_hg_options), "v:val[0:strlen(l:arglead)-1] ==# l:arglead")
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
316 return l:copts + l:gopts
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
317 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
318 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
319
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
320 " Just auto-complete with filenames unless it's an option.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
321 if l:arglead[0] ==# '-'
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
322 return []
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
323 else
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
324 return s:ListRepoFiles(a:ArgLead, a:CmdLine, a:CursorPos)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
325 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
326
26
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
327 call s:AddMainCommand("-bang -complete=customlist,s:CompleteHg -nargs=* Hg :call s:Hg(<bang>0, <f-args>)")
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
328
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
329 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
330
4
b6e4446ed292 HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
331 " Hgstatus {{{
b6e4446ed292 HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
332
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
333 function! s:HgStatus() abort
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
334 " 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
335 let l:repo = s:hg_repo()
b6e4446ed292 HgStatus now outputs to the location window.
Ludovic Chabant <ludovic@chabant.com>
parents: 0
diff changeset
336 let l:status_text = l:repo.RunCommand('status')
16
724f6db3baa2 Don't show `hg commit` output if there's nothing to show.
Ludovic Chabant <ludovic@chabant.com>
parents: 15
diff changeset
337 if l:status_text ==# '\v%^\s*%$'
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
338 echo "Nothing modified."
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
339 endif
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
340
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
341 " 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
342 " and paste the `hg status` output in there.
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
343 let l:temp_file = s:tempname('hg-status-', '.txt')
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
344 let l:preview_height = &previewheight
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
345 let l:status_lines = split(l:status_text, '\n')
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
346 execute "setlocal previewheight=" . (len(l:status_lines) + 1)
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
347 execute "pedit " . l:temp_file
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
348 wincmd p
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
349 call append(0, l:status_lines)
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
350 call cursor(1, 1)
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
351 " Make it a nice size.
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
352 execute "setlocal previewheight=" . l:preview_height
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
353 " Make sure it's deleted when we exit the window.
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
354 setlocal bufhidden=delete
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
355
7
adc267e2f0f4 Added syntax highlighting for hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
356 " 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
357 " to it.
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
358 let b:mercurial_dir = l:repo.root_dir
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
359 setlocal nomodified
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
360 setlocal nomodifiable
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
361 setlocal readonly
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
362 setlocal buftype=nofile
7
adc267e2f0f4 Added syntax highlighting for hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 6
diff changeset
363 setlocal syntax=hgstatus
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
364
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
365 " Make commands available.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
366 call s:DefineMainCommands()
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
367
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
368 " Add some nice commands.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
369 command! -buffer Hgstatusedit :call s:HgStatus_FileEdit()
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
370 command! -buffer -range Hgstatusaddremove :call s:HgStatus_AddRemove(<line1>, <line2>)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
371 command! -buffer Hgstatusdiff :call s:HgStatus_Diff(0)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
372 command! -buffer Hgstatusvdiff :call s:HgStatus_Diff(1)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
373 command! -buffer Hgstatusrefresh :call s:HgStatus_Refresh()
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
374
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
375 " Add some handy mappings.
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
376 if g:lawrencium_define_mappings
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
377 nnoremap <buffer> <silent> <cr> :Hgstatusedit<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
378 nnoremap <buffer> <silent> <C-N> :call search('^[MARC\!\?I ]\s.', 'We')<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
379 nnoremap <buffer> <silent> <C-P> :call search('^[MARC\!\?I ]\s.', 'Wbe')<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
380 nnoremap <buffer> <silent> <C-D> :Hgstatusdiff<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
381 nnoremap <buffer> <silent> <C-V> :Hgstatusvdiff<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
382 nnoremap <buffer> <silent> <C-A> :Hgstatusaddremove<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
383 nnoremap <buffer> <silent> <C-R> :Hgstatusrefresh<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
384 nnoremap <buffer> <silent> q :bdelete!<cr>
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
385 endif
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
386
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
387 " Make sure the file is deleted with the buffer.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
388 autocmd BufDelete <buffer> call s:HgStatus_CleanUp(expand('<afile>:p'))
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
389 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
390
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
391 function! s:HgStatus_CleanUp(path) abort
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
392 " If the `hg status` output has been saved to disk (e.g. because of a
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
393 " refresh we did), let's delete it.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
394 if filewritable(a:path)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
395 call s:trace("Cleaning up status log: " . a:path)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
396 call delete(a:path)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
397 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
398 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
399
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
400 function! s:HgStatus_Refresh() abort
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
401 " Get the repo and the `hg status` output.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
402 let l:repo = s:hg_repo()
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
403 let l:status_text = l:repo.RunCommand('status')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
404
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
405 " Replace the contents of the current buffer with it, and refresh.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
406 echo "Writing to " . expand('%:p')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
407 let l:path = expand('%:p')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
408 let l:status_lines = split(l:status_text, '\n')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
409 call writefile(l:status_lines, l:path)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
410 edit
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
411 endfunction
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
412
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
413 function! s:HgStatus_FileEdit() abort
9
82a49134a85c Added keyboard shortcuts to Hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
414 " Get the path of the file the cursor is on.
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
415 let l:filename = s:HgStatus_GetSelectedFile()
24
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
416
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
417 " If the file is already open in a window, jump to that window.
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
418 " Otherwise, jump to the previous window and open it there.
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
419 for nr in range(1, winnr('$'))
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
420 let l:br = winbufnr(nr)
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
421 let l:bpath = fnamemodify(bufname(l:br), ':p')
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
422 if l:bpath ==# l:filename
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
423 execute nr . 'wincmd w'
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
424 return
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
425 endif
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
426 endfor
6
1da613c13d81 Better hg-status window.
Ludovic Chabant <ludovic@chabant.com>
parents: 5
diff changeset
427 wincmd p
24
21a879a09f20 Trying to keep the cursor line when open an already opened file in `Hgstatus`.
Ludovic Chabant <ludovic@chabant.com>
parents: 23
diff changeset
428 execute 'edit ' . l:filename
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
429 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
430
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
431 function! s:HgStatus_AddRemove(linestart, lineend) abort
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
432 " Get the selected filenames.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
433 let l:filenames = s:HgStatus_GetSelectedFiles(a:linestart, a:lineend, ['!', '?'])
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
434 if len(l:filenames) == 0
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
435 call s:error("No files to add or remove in selection or current line.")
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
436 endif
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
437
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
438 " Run `addremove` on those paths.
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
439 let l:repo = s:hg_repo()
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
440 call l:repo.RunCommand('addremove', l:filenames)
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
441
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
442 " Refresh the status window.
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
443 call s:HgStatus_Refresh()
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
444 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
445
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
446 function! s:HgStatus_Diff(vertical) abort
25
0e952b7c79d7 Fixed a bug with opening a diff from `Hgstatus`
Ludovic Chabant <ludovic@chabant.com>
parents: 24
diff changeset
447 " Open the file and run `Hgdiff` on it.
0e952b7c79d7 Fixed a bug with opening a diff from `Hgstatus`
Ludovic Chabant <ludovic@chabant.com>
parents: 24
diff changeset
448 call s:HgStatus_FileEdit()
0e952b7c79d7 Fixed a bug with opening a diff from `Hgstatus`
Ludovic Chabant <ludovic@chabant.com>
parents: 24
diff changeset
449 call s:HgDiff('%:p', a:vertical)
9
82a49134a85c Added keyboard shortcuts to Hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
450 endfunction
82a49134a85c Added keyboard shortcuts to Hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
451
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
452 function! s:HgStatus_GetSelectedFile() abort
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
453 let l:filenames = s:HgStatus_GetSelectedFiles()
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
454 return l:filenames[0]
9
82a49134a85c Added keyboard shortcuts to Hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
455 endfunction
82a49134a85c Added keyboard shortcuts to Hgstatus window.
Ludovic Chabant <ludovic@chabant.com>
parents: 8
diff changeset
456
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
457 function! s:HgStatus_GetSelectedFiles(...) abort
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
458 if a:0 >= 2
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
459 let l:lines = getline(a:1, a:2)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
460 else
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
461 let l:lines = []
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
462 call add(l:lines, getline('.'))
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
463 endif
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
464 let l:filenames = []
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
465 let l:repo = s:hg_repo()
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
466 for line in l:lines
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
467 if a:0 >= 3
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
468 let l:status = s:HgStatus_GetFileStatus(line)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
469 if index(a:3, l:status) < 0
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
470 continue
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
471 endif
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
472 endif
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
473 " Yay, awesome, Vim's regex syntax is fucked up like shit, especially for
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
474 " look-aheads and look-behinds. See for yourself:
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
475 let l:filename = matchstr(l:line, '\v(^[MARC\!\?I ]\s)@<=.*')
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
476 let l:filename = l:repo.GetFullPath(l:filename)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
477 call add(l:filenames, l:filename)
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
478 endfor
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
479 return l:filenames
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
480 endfunction
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
481
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
482 function! s:HgStatus_GetFileStatus(...) abort
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
483 let l:line = a:0 ? a:1 : getline('.')
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
484 return matchstr(l:line, '\v^[MARC\!\?I ]')
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
485 endfunction
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
486
26
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
487 call s:AddMainCommand("Hgstatus :call s:HgStatus()")
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
488
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
489 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
490
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
491 " Hgcd, Hglcd {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
492
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
493 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoDirs Hgcd :cd<bang> `=s:hg_repo().GetFullPath(<q-args>)`")
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
494 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoDirs Hglcd :lcd<bang> `=s:hg_repo().GetFullPath(<q-args>)`")
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
495
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
496 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
497
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
498 " Hgedit {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
499
27
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
500 function! s:HgEdit(bang, filename) abort
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
501 let l:full_path = s:hg_repo().GetFullPath(a:filename)
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
502 if a:bang
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
503 execute "edit! " . l:full_path
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
504 else
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
505 execute "edit " . l:full_path
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
506 endif
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
507 endfunction
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
508
09115be355e2 Fixed a bug with running `Hgedit` on a directory with a trailing backslash.
Ludovic Chabant <ludovic@chabant.com>
parents: 26
diff changeset
509 call s:AddMainCommand("-bang -nargs=? -complete=customlist,s:ListRepoFiles Hgedit :call s:HgEdit(<bang>0, <f-args>)")
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
510
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
511 " }}}
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
512
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
513 " Hgdiff {{{
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
514
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
515 function! s:HgDiff(filename, vertical, ...) abort
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
516 " Default revisions to diff: the working directory (special Lawrencium
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
517 " 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
518 " Mercurial's revsets syntax).
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
519 let l:rev1 = 'lawrencium#_wdir_'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
520 let l:rev2 = 'p1()'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
521 if a:0 == 1
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
522 let l:rev2 = a:1
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
523 elseif a:0 == 2
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
524 let l:rev1 = a:1
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
525 let l:rev2 = a:2
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
526 endif
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
527
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
528 " 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
529 " fancy filename modifiers.
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
530 let l:repo = s:hg_repo()
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
531 let l:path = expand(a:filename)
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
532 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
533
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
534 " 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
535 " others' 'diff' flag is turned off.
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
536 let l:diff_buffers = []
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
537
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
538 " Get the first file and open it.
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
539 if l:rev1 == 'lawrencium#_wdir_'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
540 if bufexists(l:path)
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
541 execute 'buffer ' . fnameescape(l:path)
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
542 else
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
543 execute 'edit ' . fnameescape(l:path)
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
544 endif
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
545 " Make it part of the diff group.
23
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
546 call s:HgDiff_DiffThis()
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
547 else
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
548 let l:temp_file = tempname()
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
549 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
550 execute 'edit ' . fnameescape(l:temp_file)
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
551 " Make it part of the diff group.
23
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
552 call s:HgDiff_DiffThis()
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
553 " Remember the repo it belongs to.
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
554 let b:mercurial_dir = l:repo.root_dir
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
555 " Make sure it's deleted when we move away from it.
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
556 setlocal bufhidden=delete
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
557 " Make commands available.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
558 call s:DefineMainCommands()
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
559 endif
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
560
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
561 " Get the second file and open it too.
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
562 let l:diffsplit = 'diffsplit'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
563 if a:vertical
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
564 let l:diffsplit = 'vertical diffsplit'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
565 endif
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
566 if l:rev2 == 'lawrencium#_wdir_'
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
567 execute l:diffsplit . ' ' . fnameescape(l:path)
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
568 else
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
569 let l:temp_file = tempname()
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
570 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
571 execute l:diffsplit . ' ' . fnameescape(l:temp_file)
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
572 " Remember the repo it belongs to.
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
573 let b:mercurial_dir = l:repo.root_dir
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
574 " Make sure it's deleted when we move away from it.
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
575 setlocal bufhidden=delete
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
576 " Make commands available.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
577 call s:DefineMainCommands()
8
1e155bfa94ad Added 'Hg!' and 'Hgdiff/Hgvdiff'.
Ludovic Chabant <ludovic@chabant.com>
parents: 7
diff changeset
578 endif
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
579 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
580
23
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
581 function! s:HgDiff_DiffThis() abort
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
582 " Store some commands to run when we exit diff mode.
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
583 " It's needed because `diffoff` reverts those settings to their default
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
584 " values, instead of their previous ones.
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
585 if !&diff
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
586 call s:trace('Enabling diff mode on ' . bufname('%'))
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
587 let w:lawrencium_diffoff = {}
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
588 let w:lawrencium_diffoff['&diff'] = 0
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
589 let w:lawrencium_diffoff['&wrap'] = &l:wrap
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
590 let w:lawrencium_diffoff['&scrollopt'] = &l:scrollopt
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
591 let w:lawrencium_diffoff['&scrollbind'] = &l:scrollbind
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
592 let w:lawrencium_diffoff['&cursorbind'] = &l:cursorbind
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
593 let w:lawrencium_diffoff['&foldmethod'] = &l:foldmethod
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
594 let w:lawrencium_diffoff['&foldcolumn'] = &l:foldcolumn
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
595 diffthis
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
596 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
597 endfunction
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
598
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
599 function! s:HgDiff_DiffOff(...) abort
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
600 " Get the window name (given as a paramter, or current window).
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
601 let l:nr = a:0 ? a:1 : winnr()
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
602
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
603 " Run the commands we saved in `HgDiff_DiffThis`, or just run `diffoff`.
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
604 let l:backup = getwinvar(l:nr, 'lawrencium_diffoff')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
605 if type(l:backup) == type({}) && len(l:backup) > 0
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
606 call s:trace('Disabling diff mode on ' . l:nr)
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
607 for key in keys(l:backup)
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
608 call setwinvar(l:nr, key, l:backup[key])
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
609 endfor
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
610 call setwinvar(l:nr, 'lawrencium_diffoff', {})
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
611 else
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
612 call s:trace('Disabling diff mode on ' . l:nr . ' (but no true restore)')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
613 diffoff
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
614 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
615 endfunction
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
616
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
617 function! s:HgDiff_GetDiffWindows() abort
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
618 let l:result = []
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
619 for nr in range(1, winnr('$'))
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
620 if getwinvar(nr, '&diff')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
621 call add(l:result, nr)
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
622 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
623 endfor
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
624 return l:result
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
625 endfunction
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
626
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
627 function! s:HgDiff_CleanUp() abort
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
628 " If we're not leaving a diff window, do nothing.
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
629 if !&diff
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
630 return
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
631 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
632
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
633 " If there will be only one diff window left (plus the one we're leaving),
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
634 " turn off diff everywhere.
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
635 let l:nrs = s:HgDiff_GetDiffWindows()
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
636 if len(l:nrs) <= 2
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
637 call s:trace('Disabling diff mode in ' . len(l:nrs) . ' windows.')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
638 for nr in l:nrs
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
639 if getwinvar(nr, '&diff')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
640 call s:HgDiff_DiffOff(nr)
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
641 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
642 endfor
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
643 else
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
644 call s:trace('Still ' . len(l:nrs) . ' diff windows open.')
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
645 endif
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
646 endfunction
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
647
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
648 augroup lawrencium_diff
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
649 autocmd!
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
650 autocmd BufWinLeave * call s:HgDiff_CleanUp()
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
651 augroup end
84bceffbb19c Restore window settings when a diff window is closed.
Ludovic Chabant <ludovic@chabant.com>
parents: 21
diff changeset
652
26
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
653 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgdiff :call s:HgDiff('%:p', 0, <f-args>)")
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
654 call s:AddMainCommand("-nargs=* -complete=customlist,s:ListRepoFiles Hgvdiff :call s:HgDiff('%:p', 1, <f-args>)")
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
655
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
656 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
657
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
658 " Hgcommit {{{
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
659
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
660 function! s:HgCommit(bang, vertical) abort
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
661 " Get the repo we'll be committing into.
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
662 let l:repo = s:hg_repo()
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
663
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
664 " Open a commit message file.
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
665 let l:commit_path = s:tempname('hg-editor-', '.txt')
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
666 let l:split = a:vertical ? 'vsplit' : 'split'
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
667 execute l:split . ' ' . l:commit_path
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
668 call append(0, ['', ''])
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
669 call append(2, split(s:HgCommit_GenerateMessage(l:repo), '\n'))
28
0cdfdab43907 `Hgcommit` now puts the cursor at the beginning of the commit message.
Ludovic Chabant <ludovic@chabant.com>
parents: 27
diff changeset
670 call cursor(1, 1)
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
671
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
672 " Setup the auto-command that will actually commit on write/exit,
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
673 " and make the buffer delete itself on exit.
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
674 let b:mercurial_dir = l:repo.root_dir
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
675 setlocal bufhidden=delete
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
676 setlocal syntax=hgcommit
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
677 if a:bang
15
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
678 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 0)
f02e37f395ae Added ability to add files from the `hg status` window.
Ludovic Chabant <ludovic@chabant.com>
parents: 14
diff changeset
679 else
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
680 autocmd BufDelete <buffer> call s:HgCommit_Execute(expand('<afile>:p'), 1)
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
681 endif
31
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
682 " Make commands available.
3a0f7bb6ea64 Hgstatus window improvements and bug fixes:
Ludovic Chabant <ludovic@chabant.com>
parents: 28
diff changeset
683 call s:DefineMainCommands()
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
684 endfunction
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
685
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
686 let s:hg_status_messages = {
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
687 \'M': 'modified',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
688 \'A': 'added',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
689 \'R': 'removed',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
690 \'C': 'clean',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
691 \'!': 'missing',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
692 \'?': 'not tracked',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
693 \'I': 'ignored',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
694 \' ': '',
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
695 \}
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
696
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
697 function! s:HgCommit_GenerateMessage(repo) abort
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
698 let l:msg = "HG: Enter commit message. Lines beginning with 'HG:' are removed.\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
699 let l:msg .= "HG: Leave message empty to abort commit.\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
700 let l:msg .= "HG: Write and quit buffer to proceed.\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
701 let l:msg .= "HG: --\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
702 let l:msg .= "HG: user: " . split(a:repo.RunCommand('showconfig ui.username'), '\n')[0] . "\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
703 let l:msg .= "HG: branch '" . split(a:repo.RunCommand('branch'), '\n')[0] . "'\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
704
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
705 let l:status_lines = split(a:repo.RunCommand('status'), "\n")
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
706 for l:line in l:status_lines
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
707 if l:line ==# ''
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
708 continue
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
709 endif
11
b4baab0a4a92 Made most regex use the 'very-magic' syntax.
Ludovic Chabant <ludovic@chabant.com>
parents: 10
diff changeset
710 let l:type = matchstr(l:line, '\v^[MARC\!\?I ]')
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
711 let l:path = l:line[2:]
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
712 let l:msg .= "HG: " . s:hg_status_messages[l:type] . ' ' . l:path . "\n"
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
713 endfor
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
714
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
715 return l:msg
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
716 endfunction
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
717
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
718 function! s:HgCommit_Execute(log_file, show_output) abort
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
719 " Check if the user actually saved a commit message.
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
720 if !filereadable(a:log_file)
18
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
721 call s:error("abort: Commit message not saved")
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
722 return
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
723 endif
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
724
12
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
725 call s:trace("Committing with log file: " . a:log_file)
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
726
18
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
727 " Clean up all the 'HG:' lines from the commit message, and see if there's
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
728 " any message left (Mercurial does this automatically, usually, but
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
729 " apparently not when you feed it a log file...).
12
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
730 let l:lines = readfile(a:log_file)
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
731 call filter(l:lines, "v:val !~# '\\v^HG:'")
18
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
732 if len(filter(copy(l:lines), "v:val !~# '\\v^\\s*$'")) == 0
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
733 call s:error("abort: Empty commit message")
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
734 return
4f04d5e052eb Abort commit if the commit message is empty.
Ludovic Chabant <ludovic@chabant.com>
parents: 17
diff changeset
735 endif
12
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
736 call writefile(l:lines, a:log_file)
a7bf37a97a1b Clean the 'HG:' lines from the commit message (apparently 'hg commit' doesn't do it with -o).
Ludovic Chabant <ludovic@chabant.com>
parents: 11
diff changeset
737
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
738 " Get the repo and commit with the given message.
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
739 let l:repo = s:hg_repo()
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
740 let l:output = l:repo.RunCommand('commit', '-l', a:log_file)
16
724f6db3baa2 Don't show `hg commit` output if there's nothing to show.
Ludovic Chabant <ludovic@chabant.com>
parents: 15
diff changeset
741 if a:show_output && l:output !~# '\v%^\s*%$'
724f6db3baa2 Don't show `hg commit` output if there's nothing to show.
Ludovic Chabant <ludovic@chabant.com>
parents: 15
diff changeset
742 call s:trace("Output from hg commit:", 1)
17
5c6c605d0660 Better output for `hg commit`.
Ludovic Chabant <ludovic@chabant.com>
parents: 16
diff changeset
743 for l:output_line in split(l:output, '\n')
5c6c605d0660 Better output for `hg commit`.
Ludovic Chabant <ludovic@chabant.com>
parents: 16
diff changeset
744 echom l:output_line
5c6c605d0660 Better output for `hg commit`.
Ludovic Chabant <ludovic@chabant.com>
parents: 16
diff changeset
745 endfor
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
746 endif
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
747 endfunction
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
748
26
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
749 call s:AddMainCommand("-bang Hgcommit :call s:HgCommit(<bang>0, 0)")
de588a4bca10 Fixed completely wrong code that somehow almost ran completely fine.
Ludovic Chabant <ludovic@chabant.com>
parents: 25
diff changeset
750 call s:AddMainCommand("-bang Hgvcommit :call s:HgCommit(<bang>0, 1)")
10
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
751
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
752 " }}}
7d16084d40a9 Added 'Hgcommit' command (and this very change is committed with it!).
Ludovic Chabant <ludovic@chabant.com>
parents: 9
diff changeset
753
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
754 " Autoload Functions {{{
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
755
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
756 " Prints a summary of the current repo (if any) that's appropriate for
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
757 " displaying on the status line.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
758 function! lawrencium#statusline(...)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
759 if !exists('b:mercurial_dir')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
760 return ''
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
761 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
762 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
763 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
764 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
765 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
766 return l:prefix . l:branch . l:suffix
0
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
767 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
768
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
769 " Rescans the current buffer for setting up Mercurial commands.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
770 " Passing '1' as the parameter enables debug traces temporarily.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
771 function! lawrencium#rescan(...)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
772 if exists('b:mercurial_dir')
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
773 unlet b:mercurial_dir
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
774 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
775 if a:0 && a:1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
776 let l:trace_backup = g:lawrencium_trace
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
777 let g:lawrencium_trace = 1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
778 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
779 call s:setup_buffer_commands()
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
780 if a:0 && a:1
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
781 let g:lawrencium_trace = l:trace_backup
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
782 endif
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
783 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
784
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
785 " Enables/disables the debug trace.
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
786 function! lawrencium#debugtrace(...)
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
787 let g:lawrencium_trace = (a:0 == 0 || (a:0 && a:1))
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
788 echom "Lawrencium debug trace is now " . (g:lawrencium_trace ? "enabled." : "disabled.")
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
789 endfunction
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
790
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
791 " }}}
0a5b490dc35d Initial commit
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
792