Mercurial > vim
annotate plugins/hghelp/hghelp.vim @ 15:7edd5f6fe136
This is another test of commiting with the message this time
| author | Luka Sitas <lsitas@avatarasoftware.com> |
|---|---|
| date | Tue, 02 Dec 2025 12:13:02 -0500 |
| parents | 6ee7fce1cba8 |
| children | 412c33afd395 |
| rev | line source |
|---|---|
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
1 let g:hg_format_command = "" |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
2 |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
3 |
|
8
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
4 function PopupCommand(command) |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
5 let buf = term_start(a:command, {'hidden': 1, 'term_finish': 'close'}) |
|
9
17c557de03e0
Updated commit window
Luka Sitas <lsitas@avatarasoftware.com>
parents:
8
diff
changeset
|
6 let winid = popup_create(buf, {'minwidth': 120, 'minheight': 28}) |
|
8
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
7 endfunction |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
8 |
| 13 | 9 function! ExecuteCommand(command) |
| 10 " a:command is a shell command string, e.g. 'hg status' | |
|
8
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
11 return system(a:command . ' 2>&1') |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
12 endfunction |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
13 |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
14 function! s:CommitWithFormat() |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
15 wa |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
16 if exists("g:hg_format_command") && !empty(g:hg_format_command) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
17 execute '!'.g:hg_format_command |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
18 endif |
|
8
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
19 execute |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
20 call ExecuteCommand('!hg addremove') |
|
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
21 call PopupCommand('hg commit') |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
22 endfunction |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
23 |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
24 function! s:OpenHgDiff(file) |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
25 let l:current_file = a:file !=# '' ? a:file : expand('%') |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
26 let l:filetype = &filetype |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
27 let l:temp_file = tempname() |
|
7
34930197e9d4
Updating hg diff tool.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
6
diff
changeset
|
28 let l:command = 'hg cat -r . ' . l:current_file . ' > ' . l:temp_file |
|
8
9c80955f0c6e
I think I got popups working on the commit
Luka Sitas <lsitas@avatarasoftware.com>
parents:
7
diff
changeset
|
29 call ExecuteCommand(l:command) |
|
7
34930197e9d4
Updating hg diff tool.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
6
diff
changeset
|
30 execute 'tabe ' . l:temp_file |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
31 execute 'setfiletype' l:filetype |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
32 setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
33 execute 'vert diffsplit' l:current_file |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
34 endfunction |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
35 |
|
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
36 command HGDiff call s:OpenHgDiff(expand('%')) |
| 13 | 37 command! HGCommit call <SID>HgCommitUI() |
| 38 " command Commit call s:CommitWithFormat() | |
| 39 " map <C-k> :Commit<CR> | |
| 40 " | |
| 41 | |
| 42 nnoremap <Leader>df :HGDiff<CR> | |
| 43 nnoremap <C-k> :HGCommit<CR> | |
| 44 | |
| 45 | |
| 46 | |
| 47 " HG UI | |
| 48 function! s:HgCommitUI() | |
| 49 " Save current window to go back if needed | |
| 50 let l:save_winid = win_getid() | |
| 51 | |
| 52 " Get hg status | |
| 53 let l:status = ExecuteCommand('hg status -T"[ ] {status} {path}\n"') | |
| 54 if v:shell_error != 0 | |
| 55 echohl ErrorMsg | |
| 56 echom "hg status failed:" | |
| 57 echom l:status | |
| 58 echohl None | |
| 59 return | |
| 60 endif | |
| 61 let l:lines = split(l:status, "\n") | |
| 62 if empty(l:lines) | |
| 63 echo "No changes to commit." | |
| 64 return | |
| 65 endif | |
| 66 | |
| 67 " Open a new tab for this commit UI | |
| 68 tabnew | |
| 69 " Top window for file selection | |
| 70 enew | |
| 71 let b:hgcommit_type = 'filelist' | |
|
15
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
72 call setline(1, l:lines) |
| 13 | 73 setlocal buftype=acwrite bufhidden=wipe nobuflisted noswapfile |
| 74 setlocal nomodifiable | |
| 75 setlocal cursorline | |
| 76 nnoremap <silent> <buffer> <CR> :call <SID>ToggleHgFileSelection()<CR> | |
| 77 nnoremap <silent> <buffer> <leader>aa :call <SID>ToggleHgSelectAll()<CR> | |
| 78 | |
| 79 " Split for commit message | |
| 80 belowright split | |
| 81 resize 10 | |
| 82 enew | |
| 83 let b:hgcommit_type = 'message' | |
| 84 setlocal buftype=acwrite bufhidden=wipe nobuflisted noswapfile | |
| 85 " Optional: initial comment | |
| 86 call setline(1, ['# Enter commit message above. Lines starting with # are ignored.']) | |
| 87 " Map to commit | |
|
15
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
88 nnoremap <silent> <buffer> <leader>c :call <SID>DoHgCommit()<CR> |
| 13 | 89 |
| 90 " Go back to top window to start there | |
| 91 wincmd k | |
| 92 endfunction | |
| 93 | |
| 94 function! s:FormatHgStatusLines(lines) abort | |
| 95 let l:out = [] | |
| 96 for l in a:lines | |
| 97 if empty(l) | |
| 98 continue | |
| 99 endif | |
| 100 " Typical hg status line: "M path/to/file" | |
| 101 " We keep status char and the rest of line, but prepend [ ] | |
| 102 call add(l:out, l) | |
| 103 endfor | |
| 104 return l:out | |
| 105 endfunction | |
| 106 | |
| 107 function! s:ToggleHgFileSelection() abort | |
| 108 if get(b:, 'hgcommit_type', '') !=# 'filelist' | |
| 109 return | |
| 110 endif | |
| 111 setlocal modifiable | |
| 112 let l:lnum = line('.') | |
| 113 let l:line = getline(l:lnum) | |
| 114 if l:line =~# '^\[ \]' | |
| 115 " select | |
| 116 let l:line = substitute(l:line, '^\[ \]', '[x]', '') | |
| 117 elseif l:line =~# '^\[x\]' | |
| 118 " unselect | |
| 119 let l:line = substitute(l:line, '^\[x\]', '[ ]', '') | |
| 120 endif | |
| 121 call setline(l:lnum, l:line) | |
| 122 setlocal nomodifiable | |
| 123 endfunction | |
| 124 | |
| 125 | |
| 126 function! s:ToggleHgSelectAll() abort | |
| 127 if get(b:, 'hgcommit_type', '') !=# 'filelist' | |
| 128 return | |
| 129 endif | |
| 130 setlocal modifiable | |
| 131 let l:lines = getline(1, '$') | |
| 132 | |
| 133 " Decide if we're selecting all or deselecting all: | |
| 134 let l:any_unselected = 0 | |
| 135 for l:l in l:lines | |
| 136 if l:l =~# '^\[ \]' | |
| 137 let l:any_unselected = 1 | |
| 138 break | |
| 139 endif | |
| 140 endfor | |
|
6
3017fd33ad8b
Better support for db tables.
Luka Sitas <lsitas@avatarasoftware.com>
parents:
diff
changeset
|
141 |
| 13 | 142 let l:new = [] |
| 143 if l:any_unselected | |
| 144 " select all | |
| 145 for l:l in l:lines | |
| 146 if l:l =~# '^\[ \]' | |
| 147 call add(l:new, substitute(l:l, '^\[ \]', '[x]', '')) | |
| 148 else | |
| 149 call add(l:new, l:l) | |
| 150 endif | |
| 151 endfor | |
| 152 else | |
| 153 " deselect all | |
| 154 for l:l in l:lines | |
| 155 if l:l =~# '^\[x\]' | |
| 156 call add(l:new, substitute(l:l, '^\[x\]', '[ ]', '')) | |
| 157 else | |
| 158 call add(l:new, l:l) | |
| 159 endif | |
| 160 endfor | |
| 161 endif | |
| 162 | |
| 163 call setline(1, l:new) | |
| 164 setlocal nomodifiable | |
| 165 endfunction | |
| 166 | |
| 167 | |
| 168 function! s:GetSelectedHgFiles() abort | |
| 169 " Find the filelist buffer in this tab | |
| 170 let l:files = [] | |
| 171 for l:w in range(1, winnr('$')) | |
| 172 execute l:w . 'wincmd w' | |
| 173 if get(b:, 'hgcommit_type', '') ==# 'filelist' | |
| 174 let l:lines = getline(1, '$') | |
| 175 for l:L in l:lines | |
| 176 if l:L =~# '^\[x\] ' | |
| 177 " Format: "[x] X path/to/file" | |
| 178 " Strip "[x] " | |
| 179 let l:rest = substitute(l:L, '^\[x\] ', '', '') | |
| 180 " First char is hg status, then space, then filename | |
| 181 if len(l:rest) >= 3 | |
| 182 let l:file = strpart(l:rest, 2) | |
| 183 call add(l:files, l:file) | |
| 184 endif | |
| 185 endif | |
| 186 endfor | |
| 187 break | |
| 188 endif | |
| 189 endfor | |
| 190 " Go back to original window (optional; this simple version just leaves you where you end) | |
| 191 return l:files | |
| 192 endfunction | |
| 193 | |
|
15
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
194 function! s:GetHgCommitMessage() abort |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
195 let l:msg_lines = [] |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
196 for l:w in range(1, winnr('$')) |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
197 execute l:w . 'wincmd w' |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
198 if get(b:, 'hgcommit_type', '') ==# 'message' |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
199 " Get commit message lines, ignoring leading '#' lines |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
200 let l:all_lines = getline(1, '$') |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
201 let l:msg_lines = [] |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
202 for l:L in l:all_lines |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
203 if l:L =~# '^#' |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
204 continue |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
205 endif |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
206 call add(l:msg_lines, l:L) |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
207 endfor |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
208 endif |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
209 endfor |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
210 return l:msg_lines |
|
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
211 endfunction |
| 13 | 212 |
| 213 function! s:DoHgCommit() abort | |
| 214 if get(b:, 'hgcommit_type', '') !=# 'message' | |
| 215 return | |
| 216 endif | |
| 217 | |
| 218 " Get selected files | |
| 219 let l:files = s:GetSelectedHgFiles() | |
| 220 if empty(l:files) | |
| 221 echohl ErrorMsg | |
| 222 echom "No files selected for commit." | |
| 223 echohl None | |
| 224 return | |
| 225 endif | |
| 226 | |
| 227 | |
|
15
7edd5f6fe136
This is another test of commiting with the message this time
Luka Sitas <lsitas@avatarasoftware.com>
parents:
14
diff
changeset
|
228 let l:msg_lines = s:GetHgCommitMessage() |
| 13 | 229 let l:msg = join(l:msg_lines, "\n") |
| 230 if empty(l:msg) | |
| 231 echohl ErrorMsg | |
| 232 echom "Commit message is empty." | |
| 233 echohl None | |
| 234 return | |
| 235 endif | |
| 236 | |
| 237 " Write commit message to temp file | |
| 238 let l:tmpfile = tempname() | |
| 239 call writefile(l:msg_lines, l:tmpfile) | |
| 240 | |
| 241 " Build command: hg commit -l tmpfile file1 file2 ... | |
| 242 let l:cmd = 'hg commit -l ' . shellescape(l:tmpfile) | |
| 243 for l:f in l:files | |
| 244 let l:cmd .= ' ' . shellescape(l:f) | |
| 245 endfor | |
| 246 | |
| 247 " Run it | |
| 248 let l:out = ExecuteCommand(l:cmd) | |
| 249 let l:status = v:shell_error | |
| 250 | |
| 251 " Clean up temp file | |
| 252 call delete(l:tmpfile) | |
| 253 | |
| 254 if l:status != 0 | |
| 255 echohl ErrorMsg | |
| 256 echom "hg commit failed:" | |
| 257 echom l:out | |
| 258 echohl None | |
|
14
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
259 return |
| 13 | 260 endif |
| 261 | |
|
14
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
262 echom "hg commit succeeded." |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
263 |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
264 " Close only the commit UI buffers in this tab |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
265 try |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
266 for l:w in range(1, winnr('$')) |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
267 execute l:w . 'wincmd w' |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
268 if get(b:, 'hgcommit_type', '') ==# 'filelist' |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
269 bwipeout! |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
270 elseif get(b:, 'hgcommit_type', '') ==# 'message' |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
271 bwipeout! |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
272 endif |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
273 endfor |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
274 catch /^Vim\%((\a\+)\)\=:E/ |
|
6ee7fce1cba8
[x] M plugins/hghelp/hghelp.vim
Luka Sitas <lsitas@avatarasoftware.com>
parents:
13
diff
changeset
|
275 endtry |
| 13 | 276 endfunction |
