comparison autoload/lawrencium/mq.vim @ 139:065625e1bb31

Split plugin file into multiple extensions.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 13 Jun 2016 09:32:34 -0700
parents
children
comparison
equal deleted inserted replaced
138:a2d823c82e5f 139:065625e1bb31
1
2 function! lawrencium#mq#init() abort
3 call lawrencium#add_command("Hgqseries call lawrencium#mq#HgQSeries()")
4
5 call lawrencium#add_reader('qseries', "lawrencium#mq#read")
6 endfunction
7
8 function! lawrencium#mq#read(repo, path_parts, full_path) abort
9 let l:names = split(a:repo.RunCommand('qseries'), '\n')
10 let l:head = split(a:repo.RunCommand('qapplied', '-s'), '\n')
11 let l:tail = split(a:repo.RunCommand('qunapplied', '-s'), '\n')
12
13 let l:idx = 0
14 let l:curbuffer = bufname('%')
15 for line in l:head
16 call setbufvar(l:curbuffer, 'lawrencium_patchname_' . (l:idx + 1), l:names[l:idx])
17 call append(l:idx, "*" . line)
18 let l:idx = l:idx + 1
19 endfor
20 for line in l:tail
21 call setbufvar(l:curbuffer, 'lawrencium_patchname_' . (l:idx + 1), l:names[l:idx])
22 call append(l:idx, line)
23 let l:idx = l:idx + 1
24 endfor
25 call setbufvar(l:curbuffer, 'lawrencium_patchname_top', l:names[len(l:head) - 1])
26 set filetype=hgqseries
27 endfunction
28
29 function! lawrencium#mq#HgQSeries() abort
30 " Open the MQ series in the preview window and jump to it.
31 let l:repo = lawrencium#hg_repo()
32 let l:path = l:repo.GetLawrenciumPath('', 'qseries', '')
33 execute 'pedit ' . fnameescape(l:path)
34 wincmd P
35
36 " Make the series buffer a Lawrencium buffer.
37 let b:mercurial_dir = l:repo.root_dir
38 call lawrencium#define_commands()
39
40 " Add some commands and mappings.
41 command! -buffer Hgqseriesgoto :call s:HgQSeries_Goto()
42 command! -buffer Hgqserieseditmessage :call s:HgQSeries_EditMessage()
43 command! -buffer -nargs=+ Hgqseriesrename :call s:HgQSeries_Rename(<f-args>)
44 if g:lawrencium_define_mappings
45 nnoremap <buffer> <silent> <C-g> :Hgqseriesgoto<cr>
46 nnoremap <buffer> <silent> <C-e> :Hgqserieseditmessage<cr>
47 nnoremap <buffer> <silent> q :bdelete!<cr>
48 endif
49 endfunction
50
51 function! s:HgQSeries_GetCurrentPatchName() abort
52 let l:pos = getpos('.')
53 return getbufvar('%', 'lawrencium_patchname_' . l:pos[1])
54 endfunction
55
56 function! s:HgQSeries_Goto() abort
57 let l:repo = lawrencium#hg_repo()
58 let l:patchname = s:HgQSeries_GetCurrentPatchName()
59 if len(l:patchname) == 0
60 call lawrencium#error("No patch to go to here.")
61 return
62 endif
63 call l:repo.RunCommand('qgoto', l:patchname)
64 edit
65 endfunction
66
67 function! s:HgQSeries_Rename(...) abort
68 let l:repo = lawrencium#hg_repo()
69 let l:current_name = s:HgQSeries_GetCurrentPatchName()
70 if len(l:current_name) == 0
71 call lawrencium#error("No patch to rename here.")
72 return
73 endif
74 let l:new_name = '"' . join(a:000, ' ') . '"'
75 call l:repo.RunCommand('qrename', l:current_name, l:new_name)
76 edit
77 endfunction
78
79 function! s:HgQSeries_EditMessage() abort
80 let l:repo = lawrencium#hg_repo()
81 let l:patchname = getbufvar('%', 'lawrencium_patchname_top')
82 if len(l:patchname) == 0
83 call lawrencium#error("No patch to edit here.")
84 return
85 endif
86 let l:current = split(l:repo.RunCommand('qheader', l:patchname), '\n')
87
88 " Open a temp file to write the commit message.
89 let l:temp_file = lawrencium#tempname('hg-qrefedit-', '.txt')
90 split
91 execute 'edit ' . fnameescape(l:temp_file)
92 call append(0, 'HG: Enter the new commit message for patch "' . l:patchname . '" here.\n')
93 call append(0, '')
94 call append(0, l:current)
95 call cursor(1, 1)
96
97 " Make it a temp buffer that will actually change the commit message
98 " when it is saved and closed.
99 let b:mercurial_dir = l:repo.root_dir
100 let b:lawrencium_patchname = l:patchname
101 setlocal bufhidden=delete
102 setlocal filetype=hgcommit
103 autocmd BufDelete <buffer> call s:HgQSeries_EditMessage_Execute(expand('<afile>:p'))
104
105 call lawrencium#define_commands()
106 endfunction
107
108 function! s:HgQSeries_EditMessage_Execute(log_file) abort
109 if !filereadable(a:log_file)
110 call lawrencium#error("abort: Commit message not saved")
111 return
112 endif
113
114 " Clean all the 'HG:' lines.
115 let l:is_valid = lawrencium#clean_commit_file(a:log_file)
116 if !l:is_valid
117 call lawrencium#error("abort: Empty commit message")
118 return
119 endif
120
121 " Get the repo and edit the given patch.
122 let l:repo = lawrencium#hg_repo()
123 let l:hg_args = ['-s', '-l', a:log_file]
124 call l:repo.RunCommand('qref', l:hg_args)
125 endfunction
126