|
4 | 4 | " cpatch.vim - load colorscheme patch automatically |
5 | 5 | " |
6 | 6 | " Created by skywind on 2024/01/05 |
7 | | -" Last Modified: 2024/01/05 14:48:57 |
| 7 | +" Last Modified: 2024/01/07 20:33 |
8 | 8 | " |
9 | 9 | " Homepage: https://github.com/skywind3000/vim-color-patch |
10 | 10 | " |
@@ -44,6 +44,12 @@ let g:cpatch_name = get(g:, 'cpatch_name', '') |
44 | 44 | " runtime bang |
45 | 45 | let g:cpatch_bang = get(g:, 'cpatch_bang', 0) |
46 | 46 |
|
| 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 | + |
47 | 53 | " don't load .lua files |
48 | 54 | let g:cpatch_disable_lua = get(g:, 'cpatch_disable_lua', 0) |
49 | 55 |
|
@@ -149,3 +155,129 @@ augroup END |
149 | 155 |
|
150 | 156 |
|
151 | 157 |
|
| 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