Skip to content

Commit 9b311c8

Browse files
author
skywind3000
committed
new :CPatchEdit command
1 parent 80576d3 commit 9b311c8

File tree

1 file changed

+133
-1
lines changed

1 file changed

+133
-1
lines changed

plugin/cpatch.vim

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
" cpatch.vim - load colorscheme patch automatically
55
"
66
" Created by skywind on 2024/01/05
7-
" Last Modified: 2024/01/05 14:48:57
7+
" Last Modified: 2024/01/07 20:33
88
"
99
" Homepage: https://github.com/skywind3000/vim-color-patch
1010
"
@@ -44,6 +44,12 @@ let g:cpatch_name = get(g:, 'cpatch_name', '')
4444
" runtime bang
4545
let g:cpatch_bang = get(g:, 'cpatch_bang', 0)
4646

47+
" color patch edit path: for CPatchEdit
48+
let g:cpatch_edit = get(g:, 'cpatch_edit', '~/.vim/cpatch')
49+
50+
" split mode
51+
let g:cpatch_split = get(g:, 'cpatch_split', 'auto')
52+
4753
" don't load .lua files
4854
let g:cpatch_disable_lua = get(g:, 'cpatch_disable_lua', 0)
4955

@@ -149,3 +155,129 @@ augroup END
149155

150156

151157

158+
"----------------------------------------------------------------------
159+
" edit patch
160+
"----------------------------------------------------------------------
161+
function! s:CPatchEdit(mods, name) abort
162+
let name = a:name
163+
if name == ''
164+
let name = get(g:, 'colors_name', '')
165+
endif
166+
if name == ''
167+
let name = '__init__'
168+
endif
169+
let home = fnamemodify(g:cpatch_edit, ':p')
170+
if !isdirectory(home)
171+
try
172+
call mkdir(home, 'p')
173+
catch
174+
echohl ErrorMsg
175+
echo v:exception
176+
echohl None
177+
return 3
178+
endtry
179+
endif
180+
let home = (home =~ '\v[\/\\]$')? home : (home .. '/')
181+
let home = tr(home, '\', '/')
182+
let path = printf("%s%s", home, name)
183+
if name !~ '\v\.vim$' && name !~ '\v\.lua$'
184+
let path = path .. '.vim'
185+
endif
186+
let name = fnameescape(path)
187+
let mods = g:cpatch_split
188+
let newfile = (filereadable(path) == 0)? 1 : 0
189+
if a:mods != ''
190+
if a:mods != 'auto'
191+
exec a:mods . ' split ' . name
192+
elseif winwidth(0) >= 160
193+
exec 'vert split ' . name
194+
else
195+
exec 'split ' . name
196+
endif
197+
elseif mods == ''
198+
exec 'split ' . name
199+
elseif mods == 'auto'
200+
if winwidth(0) >= 160
201+
exec 'vert split ' . name
202+
else
203+
exec 'split ' . name
204+
endif
205+
elseif mods == 'tab'
206+
exec 'tabe ' . name
207+
else
208+
exec mods . ' split ' . name
209+
endif
210+
if newfile
211+
let content = []
212+
let n = fnamemodify(name, ':t:r')
213+
let u = 'https://github.com/skywind3000/vim-color-patch'
214+
if name =~ '\.vim$'
215+
let content += [printf('" edit patch for %s', n)]
216+
let content += ['" ' .. u]
217+
elseif name =~ '\.lua$'
218+
let content += [printf('-- edit patch for %s', n)]
219+
let content += ['-- ' .. u]
220+
endif
221+
if len(content) > 0 && line('$') == 1
222+
call append(0, content)
223+
exec 'set nomodified'
224+
endif
225+
endif
226+
return 0
227+
endfunc
228+
229+
230+
"----------------------------------------------------------------------
231+
" completion
232+
"----------------------------------------------------------------------
233+
function! s:complete(ArgLead, CmdLine, CursorPos)
234+
let candidate = []
235+
let result = []
236+
let items = {}
237+
let home = fnamemodify(g:cpatch_edit, ':p')
238+
if home !~ '\v[\/\\]$'
239+
let home = home .. '/'
240+
endif
241+
let part = glob(home .. '*', 1)
242+
for n in split(part, "\n")
243+
if n =~ '\.vim$'
244+
let t = fnamemodify(n, ':t:r')
245+
let items[t] = 1
246+
elseif n =~ '\.lua$'
247+
let t = fnamemodify(n, ':t')
248+
let items[t] = 1
249+
endif
250+
endfor
251+
let cname = get(g:, 'colors_name', '')
252+
if cname != ''
253+
let items[cname] = 1
254+
endif
255+
let items['__init__'] = 1
256+
let names = keys(items)
257+
call sort(names)
258+
let hidden = (a:ArgLead =~ '^_')? 1 : 0
259+
if a:ArgLead == ''
260+
for name in names
261+
if name !~ '^__'
262+
let candidate += [name]
263+
endif
264+
endfor
265+
else
266+
for name in names
267+
if stridx(name, a:ArgLead) == 0
268+
let candidate += [name]
269+
endif
270+
endfor
271+
endif
272+
return candidate
273+
endfunc
274+
275+
276+
"----------------------------------------------------------------------
277+
" command definition
278+
"----------------------------------------------------------------------
279+
command! -nargs=? -range=0 -complete=customlist,s:complete
280+
\ CPatchEdit call s:CPatchEdit('<mods>', <q-args>)
281+
282+
283+

0 commit comments

Comments
 (0)